]> Git Repo - binutils.git/blob - gas/config/tc-mips.c
bad7807c2d45b45b3c232cdc3b284b9ec9df829b
[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
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 #ifdef OBJ_MAYBE_ELF
48 /* Clean up namespace so we can include obj-elf.h too.  */
49 static int mips_output_flavor PARAMS ((void));
50 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
51 #undef OBJ_PROCESS_STAB
52 #undef OUTPUT_FLAVOR
53 #undef S_GET_ALIGN
54 #undef S_GET_SIZE
55 #undef S_SET_ALIGN
56 #undef S_SET_SIZE
57 #undef obj_frob_file
58 #undef obj_frob_file_after_relocs
59 #undef obj_frob_symbol
60 #undef obj_pop_insert
61 #undef obj_sec_sym_ok_for_reloc
62 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
63
64 #include "obj-elf.h"
65 /* Fix any of them that we actually care about.  */
66 #undef OUTPUT_FLAVOR
67 #define OUTPUT_FLAVOR mips_output_flavor()
68 #endif
69
70 #if defined (OBJ_ELF)
71 #include "elf/mips.h"
72 #endif
73
74 #ifndef ECOFF_DEBUGGING
75 #define NO_ECOFF_DEBUGGING
76 #define ECOFF_DEBUGGING 0
77 #endif
78
79 #include "ecoff.h"
80
81 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
82 static char *mips_regmask_frag;
83 #endif
84
85 #define ZERO 0
86 #define AT  1
87 #define TREG 24
88 #define PIC_CALL_REG 25
89 #define KT0 26
90 #define KT1 27
91 #define GP  28
92 #define SP  29
93 #define FP  30
94 #define RA  31
95
96 #define ILLEGAL_REG (32)
97
98 /* Allow override of standard little-endian ECOFF format.  */
99
100 #ifndef ECOFF_LITTLE_FORMAT
101 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
102 #endif
103
104 extern int target_big_endian;
105
106 /* The name of the readonly data section.  */
107 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
108                             ? ".data" \
109                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
110                             ? ".rdata" \
111                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
112                             ? ".rdata" \
113                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
114                             ? ".rodata" \
115                             : (abort (), ""))
116
117 /* The ABI to use.  */
118 enum mips_abi_level
119 {
120   NO_ABI = 0,
121   O32_ABI,
122   O64_ABI,
123   N32_ABI,
124   N64_ABI,
125   EABI_ABI
126 };
127
128 /* MIPS ABI we are using for this output file.  */
129 static enum mips_abi_level file_mips_abi = NO_ABI;
130
131 /* This is the set of options which may be modified by the .set
132    pseudo-op.  We use a struct so that .set push and .set pop are more
133    reliable.  */
134
135 struct mips_set_options
136 {
137   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
138      if it has not been initialized.  Changed by `.set mipsN', and the
139      -mipsN command line option, and the default CPU.  */
140   int isa;
141   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
142      if they have not been initialized.  Changed by `.set <asename>', by
143      command line options, and based on the default architecture.  */
144   int ase_mips3d;
145   int ase_mdmx;
146   /* Whether we are assembling for the mips16 processor.  0 if we are
147      not, 1 if we are, and -1 if the value has not been initialized.
148      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
149      -nomips16 command line options, and the default CPU.  */
150   int mips16;
151   /* Non-zero if we should not reorder instructions.  Changed by `.set
152      reorder' and `.set noreorder'.  */
153   int noreorder;
154   /* Non-zero if we should not permit the $at ($1) register to be used
155      in instructions.  Changed by `.set at' and `.set noat'.  */
156   int noat;
157   /* Non-zero if we should warn when a macro instruction expands into
158      more than one machine instruction.  Changed by `.set nomacro' and
159      `.set macro'.  */
160   int warn_about_macros;
161   /* Non-zero if we should not move instructions.  Changed by `.set
162      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
163   int nomove;
164   /* Non-zero if we should not optimize branches by moving the target
165      of the branch into the delay slot.  Actually, we don't perform
166      this optimization anyhow.  Changed by `.set bopt' and `.set
167      nobopt'.  */
168   int nobopt;
169   /* Non-zero if we should not autoextend mips16 instructions.
170      Changed by `.set autoextend' and `.set noautoextend'.  */
171   int noautoextend;
172   /* Restrict general purpose registers and floating point registers
173      to 32 bit.  This is initially determined when -mgp32 or -mfp32
174      is passed but can changed if the assembler code uses .set mipsN.  */
175   int gp32;
176   int fp32;
177   /* The ABI currently in use. This is changed by .set mipsN to loosen
178      restrictions and doesn't affect the whole file.  */
179   enum mips_abi_level abi;
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 mips16 field 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, NO_ABI
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 -mips3d was passed or implied by arguments passed on the
207    command line (e.g., by -march).  */
208 static int file_ase_mips3d;
209
210 /* True if -mdmx was passed or implied by arguments passed on the
211    command line (e.g., by -march).  */
212 static int file_ase_mdmx;
213
214 /* The argument of the -mcpu= flag.  Historical for code generation.  */
215 static int mips_cpu = CPU_UNKNOWN;
216
217 /* The argument of the -march= flag.  The architecture we are assembling.  */
218 static int mips_arch = CPU_UNKNOWN;
219
220 /* The argument of the -mtune= flag.  The architecture for which we
221    are optimizing.  */
222 static int mips_tune = CPU_UNKNOWN;
223
224 /* Whether we should mark the file EABI64 or EABI32.  */
225 static int mips_eabi64 = 0;
226
227 /* If they asked for mips1 or mips2 and a cpu that is
228    mips3 or greater, then mark the object file 32BITMODE.  */
229 static int mips_32bitmode = 0;
230
231 /* Some ISA's have delay slots for instructions which read or write
232    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
233    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
234    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
235    delay slot in this ISA.  The uses of this macro assume that any
236    ISA that has delay slots for one of these, has them for all.  They
237    also assume that ISAs which don't have delays for these insns, don't
238    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
239 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
240    (ISA) == ISA_MIPS1                       \
241    || (ISA) == ISA_MIPS2                    \
242    || (ISA) == ISA_MIPS3                    \
243    )
244
245 /*  Return true if ISA supports 64 bit gp register instructions.  */
246 #define ISA_HAS_64BIT_REGS(ISA) (    \
247    (ISA) == ISA_MIPS3                \
248    || (ISA) == ISA_MIPS4             \
249    || (ISA) == ISA_MIPS5             \
250    || (ISA) == ISA_MIPS64            \
251    )
252
253 #define HAVE_32BIT_GPRS                            \
254     (mips_opts.gp32                                \
255      || mips_opts.abi == O32_ABI                   \
256      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
257
258 #define HAVE_32BIT_FPRS                            \
259     (mips_opts.fp32                                \
260      || mips_opts.abi == O32_ABI                   \
261      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
262
263 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
264 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
265
266 #define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
267
268 #define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
269
270 /* We can only have 64bit addresses if the object file format
271    supports it.  */
272 #define HAVE_32BIT_ADDRESSES                           \
273    (HAVE_32BIT_GPRS                                    \
274     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
275          || ! HAVE_64BIT_OBJECTS)                      \
276         && mips_pic != EMBEDDED_PIC))
277
278 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
279
280 /* Return true if the given CPU supports the MIPS3D ASE.  */
281 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
282                                  )
283
284 /* Return true if the given CPU supports the MDMX ASE.  */
285 #define CPU_HAS_MDMX(cpu)       (0                    \
286                                  )
287
288 /* Whether the processor uses hardware interlocks to protect
289    reads from the HI and LO registers, and thus does not
290    require nops to be inserted.  */
291
292 #define hilo_interlocks (mips_arch == CPU_R4010                       \
293                          || mips_arch == CPU_SB1                      \
294                          )
295
296 /* Whether the processor uses hardware interlocks to protect reads
297    from the GPRs, and thus does not require nops to be inserted.  */
298 #define gpr_interlocks \
299   (mips_opts.isa != ISA_MIPS1  \
300    || mips_arch == CPU_R3900)
301
302 /* As with other "interlocks" this is used by hardware that has FP
303    (co-processor) interlocks.  */
304 /* Itbl support may require additional care here.  */
305 #define cop_interlocks (mips_arch == CPU_R4300                        \
306                         || mips_arch == CPU_SB1                       \
307                         )
308
309 /* Is this a mfhi or mflo instruction?  */
310 #define MF_HILO_INSN(PINFO) \
311           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
312
313 /* MIPS PIC level.  */
314
315 enum mips_pic_level
316 {
317   /* Do not generate PIC code.  */
318   NO_PIC,
319
320   /* Generate PIC code as in Irix 4.  This is not implemented, and I'm
321      not sure what it is supposed to do.  */
322   IRIX4_PIC,
323
324   /* Generate PIC code as in the SVR4 MIPS ABI.  */
325   SVR4_PIC,
326
327   /* Generate PIC code without using a global offset table: the data
328      segment has a maximum size of 64K, all data references are off
329      the $gp register, and all text references are PC relative.  This
330      is used on some embedded systems.  */
331   EMBEDDED_PIC
332 };
333
334 static enum mips_pic_level mips_pic;
335
336 /* Warn about all NOPS that the assembler generates.  */
337 static int warn_nops = 0;
338
339 /* 1 if we should generate 32 bit offsets from the $gp register in
340    SVR4_PIC mode.  Currently has no meaning in other modes.  */
341 static int mips_big_got = 0;
342
343 /* 1 if trap instructions should used for overflow rather than break
344    instructions.  */
345 static int mips_trap = 0;
346
347 /* 1 if double width floating point constants should not be constructed
348    by assembling two single width halves into two single width floating
349    point registers which just happen to alias the double width destination
350    register.  On some architectures this aliasing can be disabled by a bit
351    in the status register, and the setting of this bit cannot be determined
352    automatically at assemble time.  */
353 static int mips_disable_float_construction;
354
355 /* Non-zero if any .set noreorder directives were used.  */
356
357 static int mips_any_noreorder;
358
359 /* Non-zero if nops should be inserted when the register referenced in
360    an mfhi/mflo instruction is read in the next two instructions.  */
361 static int mips_7000_hilo_fix;
362
363 /* The size of the small data section.  */
364 static unsigned int g_switch_value = 8;
365 /* Whether the -G option was used.  */
366 static int g_switch_seen = 0;
367
368 #define N_RMASK 0xc4
369 #define N_VFP   0xd4
370
371 /* If we can determine in advance that GP optimization won't be
372    possible, we can skip the relaxation stuff that tries to produce
373    GP-relative references.  This makes delay slot optimization work
374    better.
375
376    This function can only provide a guess, but it seems to work for
377    gcc output.  It needs to guess right for gcc, otherwise gcc
378    will put what it thinks is a GP-relative instruction in a branch
379    delay slot.
380
381    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
382    fixed it for the non-PIC mode.  KR 95/04/07  */
383 static int nopic_need_relax PARAMS ((symbolS *, int));
384
385 /* handle of the OPCODE hash table */
386 static struct hash_control *op_hash = NULL;
387
388 /* The opcode hash table we use for the mips16.  */
389 static struct hash_control *mips16_op_hash = NULL;
390
391 /* This array holds the chars that always start a comment.  If the
392     pre-processor is disabled, these aren't very useful */
393 const char comment_chars[] = "#";
394
395 /* This array holds the chars that only start a comment at the beginning of
396    a line.  If the line seems to have the form '# 123 filename'
397    .line and .file directives will appear in the pre-processed output */
398 /* Note that input_file.c hand checks for '#' at the beginning of the
399    first line of the input file.  This is because the compiler outputs
400    #NO_APP at the beginning of its output.  */
401 /* Also note that C style comments are always supported.  */
402 const char line_comment_chars[] = "#";
403
404 /* This array holds machine specific line separator characters.  */
405 const char line_separator_chars[] = ";";
406
407 /* Chars that can be used to separate mant from exp in floating point nums */
408 const char EXP_CHARS[] = "eE";
409
410 /* Chars that mean this number is a floating point constant */
411 /* As in 0f12.456 */
412 /* or    0d1.2345e12 */
413 const char FLT_CHARS[] = "rRsSfFdDxXpP";
414
415 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
416    changed in read.c .  Ideally it shouldn't have to know about it at all,
417    but nothing is ideal around here.
418  */
419
420 static char *insn_error;
421
422 static int auto_align = 1;
423
424 /* When outputting SVR4 PIC code, the assembler needs to know the
425    offset in the stack frame from which to restore the $gp register.
426    This is set by the .cprestore pseudo-op, and saved in this
427    variable.  */
428 static offsetT mips_cprestore_offset = -1;
429
430 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
431    more optimizations, it can use a register value instead of a memory-saved
432    offset and even an other register than $gp as global pointer.  */
433 static offsetT mips_cpreturn_offset = -1;
434 static int mips_cpreturn_register = -1;
435 static int mips_gp_register = GP;
436 static int mips_gprel_offset = 0;
437
438 /* Whether mips_cprestore_offset has been set in the current function
439    (or whether it has already been warned about, if not).  */
440 static int mips_cprestore_valid = 0;
441
442 /* This is the register which holds the stack frame, as set by the
443    .frame pseudo-op.  This is needed to implement .cprestore.  */
444 static int mips_frame_reg = SP;
445
446 /* Whether mips_frame_reg has been set in the current function
447    (or whether it has already been warned about, if not).  */
448 static int mips_frame_reg_valid = 0;
449
450 /* To output NOP instructions correctly, we need to keep information
451    about the previous two instructions.  */
452
453 /* Whether we are optimizing.  The default value of 2 means to remove
454    unneeded NOPs and swap branch instructions when possible.  A value
455    of 1 means to not swap branches.  A value of 0 means to always
456    insert NOPs.  */
457 static int mips_optimize = 2;
458
459 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
460    equivalent to seeing no -g option at all.  */
461 static int mips_debug = 0;
462
463 /* The previous instruction.  */
464 static struct mips_cl_insn prev_insn;
465
466 /* The instruction before prev_insn.  */
467 static struct mips_cl_insn prev_prev_insn;
468
469 /* If we don't want information for prev_insn or prev_prev_insn, we
470    point the insn_mo field at this dummy integer.  */
471 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
472
473 /* Non-zero if prev_insn is valid.  */
474 static int prev_insn_valid;
475
476 /* The frag for the previous instruction.  */
477 static struct frag *prev_insn_frag;
478
479 /* The offset into prev_insn_frag for the previous instruction.  */
480 static long prev_insn_where;
481
482 /* The reloc type for the previous instruction, if any.  */
483 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
484
485 /* The reloc for the previous instruction, if any.  */
486 static fixS *prev_insn_fixp[3];
487
488 /* Non-zero if the previous instruction was in a delay slot.  */
489 static int prev_insn_is_delay_slot;
490
491 /* Non-zero if the previous instruction was in a .set noreorder.  */
492 static int prev_insn_unreordered;
493
494 /* Non-zero if the previous instruction uses an extend opcode (if
495    mips16).  */
496 static int prev_insn_extended;
497
498 /* Non-zero if the previous previous instruction was in a .set
499    noreorder.  */
500 static int prev_prev_insn_unreordered;
501
502 /* If this is set, it points to a frag holding nop instructions which
503    were inserted before the start of a noreorder section.  If those
504    nops turn out to be unnecessary, the size of the frag can be
505    decreased.  */
506 static fragS *prev_nop_frag;
507
508 /* The number of nop instructions we created in prev_nop_frag.  */
509 static int prev_nop_frag_holds;
510
511 /* The number of nop instructions that we know we need in
512    prev_nop_frag.  */
513 static int prev_nop_frag_required;
514
515 /* The number of instructions we've seen since prev_nop_frag.  */
516 static int prev_nop_frag_since;
517
518 /* For ECOFF and ELF, relocations against symbols are done in two
519    parts, with a HI relocation and a LO relocation.  Each relocation
520    has only 16 bits of space to store an addend.  This means that in
521    order for the linker to handle carries correctly, it must be able
522    to locate both the HI and the LO relocation.  This means that the
523    relocations must appear in order in the relocation table.
524
525    In order to implement this, we keep track of each unmatched HI
526    relocation.  We then sort them so that they immediately precede the
527    corresponding LO relocation.  */
528
529 struct mips_hi_fixup
530 {
531   /* Next HI fixup.  */
532   struct mips_hi_fixup *next;
533   /* This fixup.  */
534   fixS *fixp;
535   /* The section this fixup is in.  */
536   segT seg;
537 };
538
539 /* The list of unmatched HI relocs.  */
540
541 static struct mips_hi_fixup *mips_hi_fixup_list;
542
543 /* Map normal MIPS register numbers to mips16 register numbers.  */
544
545 #define X ILLEGAL_REG
546 static const int mips32_to_16_reg_map[] =
547 {
548   X, X, 2, 3, 4, 5, 6, 7,
549   X, X, X, X, X, X, X, X,
550   0, 1, X, X, X, X, X, X,
551   X, X, X, X, X, X, X, X
552 };
553 #undef X
554
555 /* Map mips16 register numbers to normal MIPS register numbers.  */
556
557 static const unsigned int mips16_to_32_reg_map[] =
558 {
559   16, 17, 2, 3, 4, 5, 6, 7
560 };
561 \f
562 /* Since the MIPS does not have multiple forms of PC relative
563    instructions, we do not have to do relaxing as is done on other
564    platforms.  However, we do have to handle GP relative addressing
565    correctly, which turns out to be a similar problem.
566
567    Every macro that refers to a symbol can occur in (at least) two
568    forms, one with GP relative addressing and one without.  For
569    example, loading a global variable into a register generally uses
570    a macro instruction like this:
571      lw $4,i
572    If i can be addressed off the GP register (this is true if it is in
573    the .sbss or .sdata section, or if it is known to be smaller than
574    the -G argument) this will generate the following instruction:
575      lw $4,i($gp)
576    This instruction will use a GPREL reloc.  If i can not be addressed
577    off the GP register, the following instruction sequence will be used:
578      lui $at,i
579      lw $4,i($at)
580    In this case the first instruction will have a HI16 reloc, and the
581    second reloc will have a LO16 reloc.  Both relocs will be against
582    the symbol i.
583
584    The issue here is that we may not know whether i is GP addressable
585    until after we see the instruction that uses it.  Therefore, we
586    want to be able to choose the final instruction sequence only at
587    the end of the assembly.  This is similar to the way other
588    platforms choose the size of a PC relative instruction only at the
589    end of assembly.
590
591    When generating position independent code we do not use GP
592    addressing in quite the same way, but the issue still arises as
593    external symbols and local symbols must be handled differently.
594
595    We handle these issues by actually generating both possible
596    instruction sequences.  The longer one is put in a frag_var with
597    type rs_machine_dependent.  We encode what to do with the frag in
598    the subtype field.  We encode (1) the number of existing bytes to
599    replace, (2) the number of new bytes to use, (3) the offset from
600    the start of the existing bytes to the first reloc we must generate
601    (that is, the offset is applied from the start of the existing
602    bytes after they are replaced by the new bytes, if any), (4) the
603    offset from the start of the existing bytes to the second reloc,
604    (5) whether a third reloc is needed (the third reloc is always four
605    bytes after the second reloc), and (6) whether to warn if this
606    variant is used (this is sometimes needed if .set nomacro or .set
607    noat is in effect).  All these numbers are reasonably small.
608
609    Generating two instruction sequences must be handled carefully to
610    ensure that delay slots are handled correctly.  Fortunately, there
611    are a limited number of cases.  When the second instruction
612    sequence is generated, append_insn is directed to maintain the
613    existing delay slot information, so it continues to apply to any
614    code after the second instruction sequence.  This means that the
615    second instruction sequence must not impose any requirements not
616    required by the first instruction sequence.
617
618    These variant frags are then handled in functions called by the
619    machine independent code.  md_estimate_size_before_relax returns
620    the final size of the frag.  md_convert_frag sets up the final form
621    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
622    one if needed.  */
623 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
624   ((relax_substateT) \
625    (((old) << 23) \
626     | ((new) << 16) \
627     | (((reloc1) + 64) << 9) \
628     | (((reloc2) + 64) << 2) \
629     | ((reloc3) ? (1 << 1) : 0) \
630     | ((warn) ? 1 : 0)))
631 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
632 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
633 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
634 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
635 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
636 #define RELAX_WARN(i) ((i) & 1)
637
638 /* For mips16 code, we use an entirely different form of relaxation.
639    mips16 supports two versions of most instructions which take
640    immediate values: a small one which takes some small value, and a
641    larger one which takes a 16 bit value.  Since branches also follow
642    this pattern, relaxing these values is required.
643
644    We can assemble both mips16 and normal MIPS code in a single
645    object.  Therefore, we need to support this type of relaxation at
646    the same time that we support the relaxation described above.  We
647    use the high bit of the subtype field to distinguish these cases.
648
649    The information we store for this type of relaxation is the
650    argument code found in the opcode file for this relocation, whether
651    the user explicitly requested a small or extended form, and whether
652    the relocation is in a jump or jal delay slot.  That tells us the
653    size of the value, and how it should be stored.  We also store
654    whether the fragment is considered to be extended or not.  We also
655    store whether this is known to be a branch to a different section,
656    whether we have tried to relax this frag yet, and whether we have
657    ever extended a PC relative fragment because of a shift count.  */
658 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
659   (0x80000000                                                   \
660    | ((type) & 0xff)                                            \
661    | ((small) ? 0x100 : 0)                                      \
662    | ((ext) ? 0x200 : 0)                                        \
663    | ((dslot) ? 0x400 : 0)                                      \
664    | ((jal_dslot) ? 0x800 : 0))
665 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
666 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
667 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
668 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
669 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
670 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
671 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
672 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
673 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
674 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
675 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
676 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
677 \f
678 /* Prototypes for static functions.  */
679
680 #ifdef __STDC__
681 #define internalError() \
682     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
683 #else
684 #define internalError() as_fatal (_("MIPS internal Error"));
685 #endif
686
687 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
688
689 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
690                                   unsigned int reg, enum mips_regclass class));
691 static int reg_needs_delay PARAMS ((unsigned int));
692 static void mips16_mark_labels PARAMS ((void));
693 static void append_insn PARAMS ((char *place,
694                                  struct mips_cl_insn * ip,
695                                  expressionS * p,
696                                  bfd_reloc_code_real_type *r,
697                                  boolean));
698 static void mips_no_prev_insn PARAMS ((int));
699 static void mips_emit_delays PARAMS ((boolean));
700 #ifdef USE_STDARG
701 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
702                                  const char *name, const char *fmt,
703                                  ...));
704 #else
705 static void macro_build ();
706 #endif
707 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
708                                         const char *, const char *,
709                                         va_list));
710 static void macro_build_jalr PARAMS ((int, expressionS *));
711 static void macro_build_lui PARAMS ((char *place, int *counter,
712                                      expressionS * ep, int regnum));
713 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
714 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
715                                          expressionS *));
716 static void load_register PARAMS ((int *, int, expressionS *, int));
717 static void load_address PARAMS ((int *, int, expressionS *, int *));
718 static void move_register PARAMS ((int *, int, int));
719 static void macro PARAMS ((struct mips_cl_insn * ip));
720 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
721 #ifdef LOSING_COMPILER
722 static void macro2 PARAMS ((struct mips_cl_insn * ip));
723 #endif
724 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
725 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
726 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
727                                   boolean, boolean, unsigned long *,
728                                   boolean *, unsigned short *));
729 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
730 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
731 static int my_getSmallExpression PARAMS ((expressionS *, char *));
732 static void my_getExpression PARAMS ((expressionS *, char *));
733 #ifdef OBJ_ELF
734 static int support_64bit_objects PARAMS((void));
735 #endif
736 static symbolS *get_symbol PARAMS ((void));
737 static void mips_align PARAMS ((int to, int fill, symbolS *label));
738 static void s_align PARAMS ((int));
739 static void s_change_sec PARAMS ((int));
740 static void s_cons PARAMS ((int));
741 static void s_float_cons PARAMS ((int));
742 static void s_mips_globl PARAMS ((int));
743 static void s_option PARAMS ((int));
744 static void s_mipsset PARAMS ((int));
745 static void s_abicalls PARAMS ((int));
746 static void s_cpload PARAMS ((int));
747 static void s_cpsetup PARAMS ((int));
748 static void s_cplocal PARAMS ((int));
749 static void s_cprestore PARAMS ((int));
750 static void s_cpreturn PARAMS ((int));
751 static void s_gpvalue PARAMS ((int));
752 static void s_gpword PARAMS ((int));
753 static void s_cpadd PARAMS ((int));
754 static void s_insn PARAMS ((int));
755 static void md_obj_begin PARAMS ((void));
756 static void md_obj_end PARAMS ((void));
757 static long get_number PARAMS ((void));
758 static void s_mips_ent PARAMS ((int));
759 static void s_mips_end PARAMS ((int));
760 static void s_mips_frame PARAMS ((int));
761 static void s_mips_mask PARAMS ((int));
762 static void s_mips_stab PARAMS ((int));
763 static void s_mips_weakext PARAMS ((int));
764 static void s_file PARAMS ((int));
765 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
766 static const char *mips_isa_to_str PARAMS ((int));
767 static const char *mips_cpu_to_str PARAMS ((int));
768 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
769 static void show PARAMS ((FILE *, char *, int *, int *));
770 #ifdef OBJ_ELF
771 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
772 #endif
773
774 /* Return values of my_getSmallExpression().  */
775
776 enum small_ex_type
777 {
778   S_EX_NONE = 0,
779   S_EX_REGISTER,
780
781   /* Direct relocation creation by %percent_op().  */
782   S_EX_HALF,
783   S_EX_HI,
784   S_EX_LO,
785   S_EX_GP_REL,
786   S_EX_GOT,
787   S_EX_CALL16,
788   S_EX_GOT_DISP,
789   S_EX_GOT_PAGE,
790   S_EX_GOT_OFST,
791   S_EX_GOT_HI,
792   S_EX_GOT_LO,
793   S_EX_NEG,
794   S_EX_HIGHER,
795   S_EX_HIGHEST,
796   S_EX_CALL_HI,
797   S_EX_CALL_LO
798 };
799
800 /* Table and functions used to map between CPU/ISA names, and
801    ISA levels, and CPU numbers.  */
802
803 struct mips_cpu_info
804 {
805   const char *name;           /* CPU or ISA name.  */
806   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
807   int isa;                    /* ISA level.  */
808   int cpu;                    /* CPU number (default CPU if ISA).  */
809 };
810
811 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
812 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
813 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
814 \f
815 /* Pseudo-op table.
816
817    The following pseudo-ops from the Kane and Heinrich MIPS book
818    should be defined here, but are currently unsupported: .alias,
819    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
820
821    The following pseudo-ops from the Kane and Heinrich MIPS book are
822    specific to the type of debugging information being generated, and
823    should be defined by the object format: .aent, .begin, .bend,
824    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
825    .vreg.
826
827    The following pseudo-ops from the Kane and Heinrich MIPS book are
828    not MIPS CPU specific, but are also not specific to the object file
829    format.  This file is probably the best place to define them, but
830    they are not currently supported: .asm0, .endr, .lab, .repeat,
831    .struct.  */
832
833 static const pseudo_typeS mips_pseudo_table[] =
834 {
835   /* MIPS specific pseudo-ops.  */
836   {"option", s_option, 0},
837   {"set", s_mipsset, 0},
838   {"rdata", s_change_sec, 'r'},
839   {"sdata", s_change_sec, 's'},
840   {"livereg", s_ignore, 0},
841   {"abicalls", s_abicalls, 0},
842   {"cpload", s_cpload, 0},
843   {"cpsetup", s_cpsetup, 0},
844   {"cplocal", s_cplocal, 0},
845   {"cprestore", s_cprestore, 0},
846   {"cpreturn", s_cpreturn, 0},
847   {"gpvalue", s_gpvalue, 0},
848   {"gpword", s_gpword, 0},
849   {"cpadd", s_cpadd, 0},
850   {"insn", s_insn, 0},
851
852   /* Relatively generic pseudo-ops that happen to be used on MIPS
853      chips.  */
854   {"asciiz", stringer, 1},
855   {"bss", s_change_sec, 'b'},
856   {"err", s_err, 0},
857   {"half", s_cons, 1},
858   {"dword", s_cons, 3},
859   {"weakext", s_mips_weakext, 0},
860
861   /* These pseudo-ops are defined in read.c, but must be overridden
862      here for one reason or another.  */
863   {"align", s_align, 0},
864   {"byte", s_cons, 0},
865   {"data", s_change_sec, 'd'},
866   {"double", s_float_cons, 'd'},
867   {"float", s_float_cons, 'f'},
868   {"globl", s_mips_globl, 0},
869   {"global", s_mips_globl, 0},
870   {"hword", s_cons, 1},
871   {"int", s_cons, 2},
872   {"long", s_cons, 2},
873   {"octa", s_cons, 4},
874   {"quad", s_cons, 3},
875   {"short", s_cons, 1},
876   {"single", s_float_cons, 'f'},
877   {"stabn", s_mips_stab, 'n'},
878   {"text", s_change_sec, 't'},
879   {"word", s_cons, 2},
880
881 #ifdef MIPS_STABS_ELF
882   { "extern", ecoff_directive_extern, 0},
883 #endif
884
885   { NULL, NULL, 0 },
886 };
887
888 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
889 {
890   /* These pseudo-ops should be defined by the object file format.
891      However, a.out doesn't support them, so we have versions here.  */
892   {"aent", s_mips_ent, 1},
893   {"bgnb", s_ignore, 0},
894   {"end", s_mips_end, 0},
895   {"endb", s_ignore, 0},
896   {"ent", s_mips_ent, 0},
897   {"file", s_file, 0},
898   {"fmask", s_mips_mask, 'F'},
899   {"frame", s_mips_frame, 0},
900   {"loc", s_ignore, 0},
901   {"mask", s_mips_mask, 'R'},
902   {"verstamp", s_ignore, 0},
903   { NULL, NULL, 0 },
904 };
905
906 extern void pop_insert PARAMS ((const pseudo_typeS *));
907
908 void
909 mips_pop_insert ()
910 {
911   pop_insert (mips_pseudo_table);
912   if (! ECOFF_DEBUGGING)
913     pop_insert (mips_nonecoff_pseudo_table);
914 }
915 \f
916 /* Symbols labelling the current insn.  */
917
918 struct insn_label_list
919 {
920   struct insn_label_list *next;
921   symbolS *label;
922 };
923
924 static struct insn_label_list *insn_labels;
925 static struct insn_label_list *free_insn_labels;
926
927 static void mips_clear_insn_labels PARAMS ((void));
928
929 static inline void
930 mips_clear_insn_labels ()
931 {
932   register struct insn_label_list **pl;
933
934   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
935     ;
936   *pl = insn_labels;
937   insn_labels = NULL;
938 }
939 \f
940 static char *expr_end;
941
942 /* Expressions which appear in instructions.  These are set by
943    mips_ip.  */
944
945 static expressionS imm_expr;
946 static expressionS offset_expr;
947
948 /* Relocs associated with imm_expr and offset_expr.  */
949
950 static bfd_reloc_code_real_type imm_reloc[3]
951   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
952 static bfd_reloc_code_real_type offset_reloc[3]
953   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
954
955 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
956
957 static boolean imm_unmatched_hi;
958
959 /* These are set by mips16_ip if an explicit extension is used.  */
960
961 static boolean mips16_small, mips16_ext;
962
963 #ifdef MIPS_STABS_ELF
964 /* The pdr segment for per procedure frame/regmask info */
965
966 static segT pdr_seg;
967 #endif
968
969 static const char *
970 mips_isa_to_str (isa)
971      int isa;
972 {
973   const struct mips_cpu_info *ci;
974   static char s[20];
975
976   ci = mips_cpu_info_from_isa (isa);
977   if (ci != NULL)
978     return (ci->name);
979
980   sprintf (s, "ISA#%d", isa);
981   return s;
982 }
983
984 static const char *
985 mips_cpu_to_str (cpu)
986      int cpu;
987 {
988   const struct mips_cpu_info *ci;
989   static char s[16];
990
991   ci = mips_cpu_info_from_cpu (cpu);
992   if (ci != NULL)
993     return (ci->name);
994
995   sprintf (s, "CPU#%d", cpu);
996   return s;
997 }
998
999 /* The default target format to use.  */
1000
1001 const char *
1002 mips_target_format ()
1003 {
1004   switch (OUTPUT_FLAVOR)
1005     {
1006     case bfd_target_aout_flavour:
1007       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1008     case bfd_target_ecoff_flavour:
1009       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1010     case bfd_target_coff_flavour:
1011       return "pe-mips";
1012     case bfd_target_elf_flavour:
1013 #ifdef TE_TMIPS
1014       /* This is traditional mips */
1015       return (target_big_endian
1016               ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
1017                  : "elf32-tradbigmips")
1018               : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
1019                  : "elf32-tradlittlemips"));
1020 #else
1021       return (target_big_endian
1022               ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
1023               : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1024                  : "elf32-littlemips"));
1025 #endif
1026     default:
1027       abort ();
1028       return NULL;
1029     }
1030 }
1031
1032 /* This function is called once, at assembler startup time.  It should
1033    set up all the tables, etc. that the MD part of the assembler will need.  */
1034
1035 void
1036 md_begin ()
1037 {
1038   register const char *retval = NULL;
1039   int i = 0;
1040   int broken = 0;
1041
1042   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1043     as_warn (_("Could not set architecture and machine"));
1044
1045   op_hash = hash_new ();
1046
1047   for (i = 0; i < NUMOPCODES;)
1048     {
1049       const char *name = mips_opcodes[i].name;
1050
1051       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1052       if (retval != NULL)
1053         {
1054           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1055                    mips_opcodes[i].name, retval);
1056           /* Probably a memory allocation problem?  Give up now.  */
1057           as_fatal (_("Broken assembler.  No assembly attempted."));
1058         }
1059       do
1060         {
1061           if (mips_opcodes[i].pinfo != INSN_MACRO)
1062             {
1063               if (!validate_mips_insn (&mips_opcodes[i]))
1064                 broken = 1;
1065             }
1066           ++i;
1067         }
1068       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1069     }
1070
1071   mips16_op_hash = hash_new ();
1072
1073   i = 0;
1074   while (i < bfd_mips16_num_opcodes)
1075     {
1076       const char *name = mips16_opcodes[i].name;
1077
1078       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1079       if (retval != NULL)
1080         as_fatal (_("internal: can't hash `%s': %s"),
1081                   mips16_opcodes[i].name, retval);
1082       do
1083         {
1084           if (mips16_opcodes[i].pinfo != INSN_MACRO
1085               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1086                   != mips16_opcodes[i].match))
1087             {
1088               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1089                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1090               broken = 1;
1091             }
1092           ++i;
1093         }
1094       while (i < bfd_mips16_num_opcodes
1095              && strcmp (mips16_opcodes[i].name, name) == 0);
1096     }
1097
1098   if (broken)
1099     as_fatal (_("Broken assembler.  No assembly attempted."));
1100
1101   /* We add all the general register names to the symbol table.  This
1102      helps us detect invalid uses of them.  */
1103   for (i = 0; i < 32; i++)
1104     {
1105       char buf[5];
1106
1107       sprintf (buf, "$%d", i);
1108       symbol_table_insert (symbol_new (buf, reg_section, i,
1109                                        &zero_address_frag));
1110     }
1111   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1112                                    &zero_address_frag));
1113   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1114                                    &zero_address_frag));
1115   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1116                                    &zero_address_frag));
1117   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1118                                    &zero_address_frag));
1119   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1120                                    &zero_address_frag));
1121   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1122                                    &zero_address_frag));
1123   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1124                                    &zero_address_frag));
1125   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1126                                    &zero_address_frag));
1127   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1128                                    &zero_address_frag));
1129
1130   mips_no_prev_insn (false);
1131
1132   mips_gprmask = 0;
1133   mips_cprmask[0] = 0;
1134   mips_cprmask[1] = 0;
1135   mips_cprmask[2] = 0;
1136   mips_cprmask[3] = 0;
1137
1138   /* set the default alignment for the text section (2**2) */
1139   record_alignment (text_section, 2);
1140
1141   if (USE_GLOBAL_POINTER_OPT)
1142     bfd_set_gp_size (stdoutput, g_switch_value);
1143
1144   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1145     {
1146       /* On a native system, sections must be aligned to 16 byte
1147          boundaries.  When configured for an embedded ELF target, we
1148          don't bother.  */
1149       if (strcmp (TARGET_OS, "elf") != 0)
1150         {
1151           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1152           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1153           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1154         }
1155
1156       /* Create a .reginfo section for register masks and a .mdebug
1157          section for debugging information.  */
1158       {
1159         segT seg;
1160         subsegT subseg;
1161         flagword flags;
1162         segT sec;
1163
1164         seg = now_seg;
1165         subseg = now_subseg;
1166
1167         /* The ABI says this section should be loaded so that the
1168            running program can access it.  However, we don't load it
1169            if we are configured for an embedded target */
1170         flags = SEC_READONLY | SEC_DATA;
1171         if (strcmp (TARGET_OS, "elf") != 0)
1172           flags |= SEC_ALLOC | SEC_LOAD;
1173
1174         if (file_mips_abi != N64_ABI)
1175           {
1176             sec = subseg_new (".reginfo", (subsegT) 0);
1177
1178             bfd_set_section_flags (stdoutput, sec, flags);
1179             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1180
1181 #ifdef OBJ_ELF
1182             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1183 #endif
1184           }
1185         else
1186           {
1187             /* The 64-bit ABI uses a .MIPS.options section rather than
1188                .reginfo section.  */
1189             sec = subseg_new (".MIPS.options", (subsegT) 0);
1190             bfd_set_section_flags (stdoutput, sec, flags);
1191             bfd_set_section_alignment (stdoutput, sec, 3);
1192
1193 #ifdef OBJ_ELF
1194             /* Set up the option header.  */
1195             {
1196               Elf_Internal_Options opthdr;
1197               char *f;
1198
1199               opthdr.kind = ODK_REGINFO;
1200               opthdr.size = (sizeof (Elf_External_Options)
1201                              + sizeof (Elf64_External_RegInfo));
1202               opthdr.section = 0;
1203               opthdr.info = 0;
1204               f = frag_more (sizeof (Elf_External_Options));
1205               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1206                                              (Elf_External_Options *) f);
1207
1208               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1209             }
1210 #endif
1211           }
1212
1213         if (ECOFF_DEBUGGING)
1214           {
1215             sec = subseg_new (".mdebug", (subsegT) 0);
1216             (void) bfd_set_section_flags (stdoutput, sec,
1217                                           SEC_HAS_CONTENTS | SEC_READONLY);
1218             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1219           }
1220
1221 #ifdef MIPS_STABS_ELF
1222         pdr_seg = subseg_new (".pdr", (subsegT) 0);
1223         (void) bfd_set_section_flags (stdoutput, pdr_seg,
1224                              SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1225         (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1226 #endif
1227
1228         subseg_set (seg, subseg);
1229       }
1230     }
1231
1232   if (! ECOFF_DEBUGGING)
1233     md_obj_begin ();
1234 }
1235
1236 void
1237 md_mips_end ()
1238 {
1239   if (! ECOFF_DEBUGGING)
1240     md_obj_end ();
1241 }
1242
1243 void
1244 md_assemble (str)
1245      char *str;
1246 {
1247   struct mips_cl_insn insn;
1248   bfd_reloc_code_real_type unused_reloc[3]
1249     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1250
1251   imm_expr.X_op = O_absent;
1252   imm_unmatched_hi = false;
1253   offset_expr.X_op = O_absent;
1254   imm_reloc[0] = BFD_RELOC_UNUSED;
1255   imm_reloc[1] = BFD_RELOC_UNUSED;
1256   imm_reloc[2] = BFD_RELOC_UNUSED;
1257   offset_reloc[0] = BFD_RELOC_UNUSED;
1258   offset_reloc[1] = BFD_RELOC_UNUSED;
1259   offset_reloc[2] = BFD_RELOC_UNUSED;
1260
1261   if (mips_opts.mips16)
1262     mips16_ip (str, &insn);
1263   else
1264     {
1265       mips_ip (str, &insn);
1266       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1267             str, insn.insn_opcode));
1268     }
1269
1270   if (insn_error)
1271     {
1272       as_bad ("%s `%s'", insn_error, str);
1273       return;
1274     }
1275
1276   if (insn.insn_mo->pinfo == INSN_MACRO)
1277     {
1278       if (mips_opts.mips16)
1279         mips16_macro (&insn);
1280       else
1281         macro (&insn);
1282     }
1283   else
1284     {
1285       if (imm_expr.X_op != O_absent)
1286         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1287       else if (offset_expr.X_op != O_absent)
1288         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1289       else
1290         append_insn (NULL, &insn, NULL, unused_reloc, false);
1291     }
1292 }
1293
1294 /* See whether instruction IP reads register REG.  CLASS is the type
1295    of register.  */
1296
1297 static int
1298 insn_uses_reg (ip, reg, class)
1299      struct mips_cl_insn *ip;
1300      unsigned int reg;
1301      enum mips_regclass class;
1302 {
1303   if (class == MIPS16_REG)
1304     {
1305       assert (mips_opts.mips16);
1306       reg = mips16_to_32_reg_map[reg];
1307       class = MIPS_GR_REG;
1308     }
1309
1310   /* Don't report on general register ZERO, since it never changes.  */
1311   if (class == MIPS_GR_REG && reg == ZERO)
1312     return 0;
1313
1314   if (class == MIPS_FP_REG)
1315     {
1316       assert (! mips_opts.mips16);
1317       /* If we are called with either $f0 or $f1, we must check $f0.
1318          This is not optimal, because it will introduce an unnecessary
1319          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1320          need to distinguish reading both $f0 and $f1 or just one of
1321          them.  Note that we don't have to check the other way,
1322          because there is no instruction that sets both $f0 and $f1
1323          and requires a delay.  */
1324       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1325           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1326               == (reg &~ (unsigned) 1)))
1327         return 1;
1328       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1329           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1330               == (reg &~ (unsigned) 1)))
1331         return 1;
1332     }
1333   else if (! mips_opts.mips16)
1334     {
1335       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1336           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1337         return 1;
1338       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1339           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1340         return 1;
1341     }
1342   else
1343     {
1344       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1345           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1346                                     & MIPS16OP_MASK_RX)]
1347               == reg))
1348         return 1;
1349       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1350           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1351                                     & MIPS16OP_MASK_RY)]
1352               == reg))
1353         return 1;
1354       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1355           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1356                                     & MIPS16OP_MASK_MOVE32Z)]
1357               == reg))
1358         return 1;
1359       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1360         return 1;
1361       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1362         return 1;
1363       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1364         return 1;
1365       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1366           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1367               & MIPS16OP_MASK_REGR32) == reg)
1368         return 1;
1369     }
1370
1371   return 0;
1372 }
1373
1374 /* This function returns true if modifying a register requires a
1375    delay.  */
1376
1377 static int
1378 reg_needs_delay (reg)
1379      unsigned int reg;
1380 {
1381   unsigned long prev_pinfo;
1382
1383   prev_pinfo = prev_insn.insn_mo->pinfo;
1384   if (! mips_opts.noreorder
1385       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1386       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1387           || (! gpr_interlocks
1388               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1389     {
1390       /* A load from a coprocessor or from memory.  All load
1391          delays delay the use of general register rt for one
1392          instruction on the r3000.  The r6000 and r4000 use
1393          interlocks.  */
1394       /* Itbl support may require additional care here.  */
1395       know (prev_pinfo & INSN_WRITE_GPR_T);
1396       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1397         return 1;
1398     }
1399
1400   return 0;
1401 }
1402
1403 /* Mark instruction labels in mips16 mode.  This permits the linker to
1404    handle them specially, such as generating jalx instructions when
1405    needed.  We also make them odd for the duration of the assembly, in
1406    order to generate the right sort of code.  We will make them even
1407    in the adjust_symtab routine, while leaving them marked.  This is
1408    convenient for the debugger and the disassembler.  The linker knows
1409    to make them odd again.  */
1410
1411 static void
1412 mips16_mark_labels ()
1413 {
1414   if (mips_opts.mips16)
1415     {
1416       struct insn_label_list *l;
1417       valueT val;
1418
1419       for (l = insn_labels; l != NULL; l = l->next)
1420         {
1421 #ifdef OBJ_ELF
1422           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1423             S_SET_OTHER (l->label, STO_MIPS16);
1424 #endif
1425           val = S_GET_VALUE (l->label);
1426           if ((val & 1) == 0)
1427             S_SET_VALUE (l->label, val + 1);
1428         }
1429     }
1430 }
1431
1432 /* Output an instruction.  PLACE is where to put the instruction; if
1433    it is NULL, this uses frag_more to get room.  IP is the instruction
1434    information.  ADDRESS_EXPR is an operand of the instruction to be
1435    used with RELOC_TYPE.  */
1436
1437 static void
1438 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1439      char *place;
1440      struct mips_cl_insn *ip;
1441      expressionS *address_expr;
1442      bfd_reloc_code_real_type *reloc_type;
1443      boolean unmatched_hi;
1444 {
1445   register unsigned long prev_pinfo, pinfo;
1446   char *f;
1447   fixS *fixp[3];
1448   int nops = 0;
1449
1450   /* Mark instruction labels in mips16 mode.  */
1451   mips16_mark_labels ();
1452
1453   prev_pinfo = prev_insn.insn_mo->pinfo;
1454   pinfo = ip->insn_mo->pinfo;
1455
1456   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1457     {
1458       int prev_prev_nop;
1459
1460       /* If the previous insn required any delay slots, see if we need
1461          to insert a NOP or two.  There are eight kinds of possible
1462          hazards, of which an instruction can have at most one type.
1463          (1) a load from memory delay
1464          (2) a load from a coprocessor delay
1465          (3) an unconditional branch delay
1466          (4) a conditional branch delay
1467          (5) a move to coprocessor register delay
1468          (6) a load coprocessor register from memory delay
1469          (7) a coprocessor condition code delay
1470          (8) a HI/LO special register delay
1471
1472          There are a lot of optimizations we could do that we don't.
1473          In particular, we do not, in general, reorder instructions.
1474          If you use gcc with optimization, it will reorder
1475          instructions and generally do much more optimization then we
1476          do here; repeating all that work in the assembler would only
1477          benefit hand written assembly code, and does not seem worth
1478          it.  */
1479
1480       /* This is how a NOP is emitted.  */
1481 #define emit_nop()                                      \
1482   (mips_opts.mips16                                     \
1483    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1484    : md_number_to_chars (frag_more (4), 0, 4))
1485
1486       /* The previous insn might require a delay slot, depending upon
1487          the contents of the current insn.  */
1488       if (! mips_opts.mips16
1489           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1490           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1491                && ! cop_interlocks)
1492               || (! gpr_interlocks
1493                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1494         {
1495           /* A load from a coprocessor or from memory.  All load
1496              delays delay the use of general register rt for one
1497              instruction on the r3000.  The r6000 and r4000 use
1498              interlocks.  */
1499           /* Itbl support may require additional care here.  */
1500           know (prev_pinfo & INSN_WRITE_GPR_T);
1501           if (mips_optimize == 0
1502               || insn_uses_reg (ip,
1503                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1504                                  & OP_MASK_RT),
1505                                 MIPS_GR_REG))
1506             ++nops;
1507         }
1508       else if (! mips_opts.mips16
1509                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1510                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1511                     && ! cop_interlocks)
1512                    || (mips_opts.isa == ISA_MIPS1
1513                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1514         {
1515           /* A generic coprocessor delay.  The previous instruction
1516              modified a coprocessor general or control register.  If
1517              it modified a control register, we need to avoid any
1518              coprocessor instruction (this is probably not always
1519              required, but it sometimes is).  If it modified a general
1520              register, we avoid using that register.
1521
1522              On the r6000 and r4000 loading a coprocessor register
1523              from memory is interlocked, and does not require a delay.
1524
1525              This case is not handled very well.  There is no special
1526              knowledge of CP0 handling, and the coprocessors other
1527              than the floating point unit are not distinguished at
1528              all.  */
1529           /* Itbl support may require additional care here. FIXME!
1530              Need to modify this to include knowledge about
1531              user specified delays!  */
1532           if (prev_pinfo & INSN_WRITE_FPR_T)
1533             {
1534               if (mips_optimize == 0
1535                   || insn_uses_reg (ip,
1536                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1537                                      & OP_MASK_FT),
1538                                     MIPS_FP_REG))
1539                 ++nops;
1540             }
1541           else if (prev_pinfo & INSN_WRITE_FPR_S)
1542             {
1543               if (mips_optimize == 0
1544                   || insn_uses_reg (ip,
1545                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1546                                      & OP_MASK_FS),
1547                                     MIPS_FP_REG))
1548                 ++nops;
1549             }
1550           else
1551             {
1552               /* We don't know exactly what the previous instruction
1553                  does.  If the current instruction uses a coprocessor
1554                  register, we must insert a NOP.  If previous
1555                  instruction may set the condition codes, and the
1556                  current instruction uses them, we must insert two
1557                  NOPS.  */
1558               /* Itbl support may require additional care here.  */
1559               if (mips_optimize == 0
1560                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1561                       && (pinfo & INSN_READ_COND_CODE)))
1562                 nops += 2;
1563               else if (pinfo & INSN_COP)
1564                 ++nops;
1565             }
1566         }
1567       else if (! mips_opts.mips16
1568                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1569                && (prev_pinfo & INSN_WRITE_COND_CODE)
1570                && ! cop_interlocks)
1571         {
1572           /* The previous instruction sets the coprocessor condition
1573              codes, but does not require a general coprocessor delay
1574              (this means it is a floating point comparison
1575              instruction).  If this instruction uses the condition
1576              codes, we need to insert a single NOP.  */
1577           /* Itbl support may require additional care here.  */
1578           if (mips_optimize == 0
1579               || (pinfo & INSN_READ_COND_CODE))
1580             ++nops;
1581         }
1582
1583       /* If we're fixing up mfhi/mflo for the r7000 and the
1584          previous insn was an mfhi/mflo and the current insn
1585          reads the register that the mfhi/mflo wrote to, then
1586          insert two nops.  */
1587
1588       else if (mips_7000_hilo_fix
1589                && MF_HILO_INSN (prev_pinfo)
1590                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1591                                       & OP_MASK_RD),
1592                                  MIPS_GR_REG))
1593         {
1594           nops += 2;
1595         }
1596
1597       /* If we're fixing up mfhi/mflo for the r7000 and the
1598          2nd previous insn was an mfhi/mflo and the current insn
1599          reads the register that the mfhi/mflo wrote to, then
1600          insert one nop.  */
1601
1602       else if (mips_7000_hilo_fix
1603                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1604                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1605                                        & OP_MASK_RD),
1606                                     MIPS_GR_REG))
1607
1608         {
1609           ++nops;
1610         }
1611
1612       else if (prev_pinfo & INSN_READ_LO)
1613         {
1614           /* The previous instruction reads the LO register; if the
1615              current instruction writes to the LO register, we must
1616              insert two NOPS.  Some newer processors have interlocks.
1617              Also the tx39's multiply instructions can be exectuted
1618              immediatly after a read from HI/LO (without the delay),
1619              though the tx39's divide insns still do require the
1620              delay.  */
1621           if (! (hilo_interlocks
1622                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1623               && (mips_optimize == 0
1624                   || (pinfo & INSN_WRITE_LO)))
1625             nops += 2;
1626           /* Most mips16 branch insns don't have a delay slot.
1627              If a read from LO is immediately followed by a branch
1628              to a write to LO we have a read followed by a write
1629              less than 2 insns away.  We assume the target of
1630              a branch might be a write to LO, and insert a nop
1631              between a read and an immediately following branch.  */
1632           else if (mips_opts.mips16
1633                    && (mips_optimize == 0
1634                        || (pinfo & MIPS16_INSN_BRANCH)))
1635             ++nops;
1636         }
1637       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1638         {
1639           /* The previous instruction reads the HI register; if the
1640              current instruction writes to the HI register, we must
1641              insert a NOP.  Some newer processors have interlocks.
1642              Also the note tx39's multiply above.  */
1643           if (! (hilo_interlocks
1644                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1645               && (mips_optimize == 0
1646                   || (pinfo & INSN_WRITE_HI)))
1647             nops += 2;
1648           /* Most mips16 branch insns don't have a delay slot.
1649              If a read from HI is immediately followed by a branch
1650              to a write to HI we have a read followed by a write
1651              less than 2 insns away.  We assume the target of
1652              a branch might be a write to HI, and insert a nop
1653              between a read and an immediately following branch.  */
1654           else if (mips_opts.mips16
1655                    && (mips_optimize == 0
1656                        || (pinfo & MIPS16_INSN_BRANCH)))
1657             ++nops;
1658         }
1659
1660       /* If the previous instruction was in a noreorder section, then
1661          we don't want to insert the nop after all.  */
1662       /* Itbl support may require additional care here.  */
1663       if (prev_insn_unreordered)
1664         nops = 0;
1665
1666       /* There are two cases which require two intervening
1667          instructions: 1) setting the condition codes using a move to
1668          coprocessor instruction which requires a general coprocessor
1669          delay and then reading the condition codes 2) reading the HI
1670          or LO register and then writing to it (except on processors
1671          which have interlocks).  If we are not already emitting a NOP
1672          instruction, we must check for these cases compared to the
1673          instruction previous to the previous instruction.  */
1674       if ((! mips_opts.mips16
1675            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1676            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1677            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1678            && (pinfo & INSN_READ_COND_CODE)
1679            && ! cop_interlocks)
1680           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1681               && (pinfo & INSN_WRITE_LO)
1682               && ! (hilo_interlocks
1683                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1684           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1685               && (pinfo & INSN_WRITE_HI)
1686               && ! (hilo_interlocks
1687                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1688         prev_prev_nop = 1;
1689       else
1690         prev_prev_nop = 0;
1691
1692       if (prev_prev_insn_unreordered)
1693         prev_prev_nop = 0;
1694
1695       if (prev_prev_nop && nops == 0)
1696         ++nops;
1697
1698       /* If we are being given a nop instruction, don't bother with
1699          one of the nops we would otherwise output.  This will only
1700          happen when a nop instruction is used with mips_optimize set
1701          to 0.  */
1702       if (nops > 0
1703           && ! mips_opts.noreorder
1704           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1705         --nops;
1706
1707       /* Now emit the right number of NOP instructions.  */
1708       if (nops > 0 && ! mips_opts.noreorder)
1709         {
1710           fragS *old_frag;
1711           unsigned long old_frag_offset;
1712           int i;
1713           struct insn_label_list *l;
1714
1715           old_frag = frag_now;
1716           old_frag_offset = frag_now_fix ();
1717
1718           for (i = 0; i < nops; i++)
1719             emit_nop ();
1720
1721           if (listing)
1722             {
1723               listing_prev_line ();
1724               /* We may be at the start of a variant frag.  In case we
1725                  are, make sure there is enough space for the frag
1726                  after the frags created by listing_prev_line.  The
1727                  argument to frag_grow here must be at least as large
1728                  as the argument to all other calls to frag_grow in
1729                  this file.  We don't have to worry about being in the
1730                  middle of a variant frag, because the variants insert
1731                  all needed nop instructions themselves.  */
1732               frag_grow (40);
1733             }
1734
1735           for (l = insn_labels; l != NULL; l = l->next)
1736             {
1737               valueT val;
1738
1739               assert (S_GET_SEGMENT (l->label) == now_seg);
1740               symbol_set_frag (l->label, frag_now);
1741               val = (valueT) frag_now_fix ();
1742               /* mips16 text labels are stored as odd.  */
1743               if (mips_opts.mips16)
1744                 ++val;
1745               S_SET_VALUE (l->label, val);
1746             }
1747
1748 #ifndef NO_ECOFF_DEBUGGING
1749           if (ECOFF_DEBUGGING)
1750             ecoff_fix_loc (old_frag, old_frag_offset);
1751 #endif
1752         }
1753       else if (prev_nop_frag != NULL)
1754         {
1755           /* We have a frag holding nops we may be able to remove.  If
1756              we don't need any nops, we can decrease the size of
1757              prev_nop_frag by the size of one instruction.  If we do
1758              need some nops, we count them in prev_nops_required.  */
1759           if (prev_nop_frag_since == 0)
1760             {
1761               if (nops == 0)
1762                 {
1763                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1764                   --prev_nop_frag_holds;
1765                 }
1766               else
1767                 prev_nop_frag_required += nops;
1768             }
1769           else
1770             {
1771               if (prev_prev_nop == 0)
1772                 {
1773                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1774                   --prev_nop_frag_holds;
1775                 }
1776               else
1777                 ++prev_nop_frag_required;
1778             }
1779
1780           if (prev_nop_frag_holds <= prev_nop_frag_required)
1781             prev_nop_frag = NULL;
1782
1783           ++prev_nop_frag_since;
1784
1785           /* Sanity check: by the time we reach the second instruction
1786              after prev_nop_frag, we should have used up all the nops
1787              one way or another.  */
1788           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1789         }
1790     }
1791
1792   if (*reloc_type > BFD_RELOC_UNUSED)
1793     {
1794       /* We need to set up a variant frag.  */
1795       assert (mips_opts.mips16 && address_expr != NULL);
1796       f = frag_var (rs_machine_dependent, 4, 0,
1797                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1798                                          mips16_small, mips16_ext,
1799                                          (prev_pinfo
1800                                           & INSN_UNCOND_BRANCH_DELAY),
1801                                          (*prev_insn_reloc_type
1802                                           == BFD_RELOC_MIPS16_JMP)),
1803                     make_expr_symbol (address_expr), 0, NULL);
1804     }
1805   else if (place != NULL)
1806     f = place;
1807   else if (mips_opts.mips16
1808            && ! ip->use_extend
1809            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1810     {
1811       /* Make sure there is enough room to swap this instruction with
1812          a following jump instruction.  */
1813       frag_grow (6);
1814       f = frag_more (2);
1815     }
1816   else
1817     {
1818       if (mips_opts.mips16
1819           && mips_opts.noreorder
1820           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1821         as_warn (_("extended instruction in delay slot"));
1822
1823       f = frag_more (4);
1824     }
1825
1826   fixp[0] = fixp[1] = fixp[2] = NULL;
1827   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1828     {
1829       if (address_expr->X_op == O_constant)
1830         {
1831           valueT tmp;
1832
1833           switch (*reloc_type)
1834             {
1835             case BFD_RELOC_32:
1836               ip->insn_opcode |= address_expr->X_add_number;
1837               break;
1838
1839             case BFD_RELOC_MIPS_HIGHEST:
1840               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1841               tmp >>= 16;
1842               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1843               break;
1844
1845             case BFD_RELOC_MIPS_HIGHER:
1846               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1847               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1848               break;
1849
1850             case BFD_RELOC_HI16_S:
1851               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1852                                   >> 16) & 0xffff;
1853               break;
1854
1855             case BFD_RELOC_HI16:
1856               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1857               break;
1858
1859             case BFD_RELOC_LO16:
1860               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1861               break;
1862
1863             case BFD_RELOC_MIPS_JMP:
1864               if ((address_expr->X_add_number & 3) != 0)
1865                 as_bad (_("jump to misaligned address (0x%lx)"),
1866                         (unsigned long) address_expr->X_add_number);
1867               if (address_expr->X_add_number & ~0xfffffff
1868                   || address_expr->X_add_number > 0x7fffffc)
1869                 as_bad (_("jump address range overflow (0x%lx)"),
1870                         (unsigned long) address_expr->X_add_number);
1871               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1872               break;
1873
1874             case BFD_RELOC_MIPS16_JMP:
1875               if ((address_expr->X_add_number & 3) != 0)
1876                 as_bad (_("jump to misaligned address (0x%lx)"),
1877                         (unsigned long) address_expr->X_add_number);
1878               if (address_expr->X_add_number & ~0xfffffff
1879                   || address_expr->X_add_number > 0x7fffffc)
1880                 as_bad (_("jump address range overflow (0x%lx)"),
1881                         (unsigned long) address_expr->X_add_number);
1882               ip->insn_opcode |=
1883                 (((address_expr->X_add_number & 0x7c0000) << 3)
1884                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1885                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1886               break;
1887
1888             case BFD_RELOC_16_PCREL:
1889               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1890               break;
1891
1892             case BFD_RELOC_16_PCREL_S2:
1893               goto need_reloc;
1894
1895             default:
1896               internalError ();
1897             }
1898         }
1899       else
1900         {
1901         need_reloc:
1902           /* Don't generate a reloc if we are writing into a variant frag.  */
1903           if (place == NULL)
1904             {
1905               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1906                                      address_expr,
1907                                      (*reloc_type == BFD_RELOC_16_PCREL
1908                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
1909                                      reloc_type[0]);
1910
1911               /* These relocations can have an addend that won't fit in
1912                  4 octets for 64bit assembly.  */
1913               if (HAVE_64BIT_GPRS &&
1914                   (*reloc_type == BFD_RELOC_16
1915                    || *reloc_type == BFD_RELOC_32
1916                    || *reloc_type == BFD_RELOC_MIPS_JMP
1917                    || *reloc_type == BFD_RELOC_HI16_S
1918                    || *reloc_type == BFD_RELOC_LO16
1919                    || *reloc_type == BFD_RELOC_GPREL16
1920                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
1921                    || *reloc_type == BFD_RELOC_GPREL32
1922                    || *reloc_type == BFD_RELOC_64
1923                    || *reloc_type == BFD_RELOC_CTOR
1924                    || *reloc_type == BFD_RELOC_MIPS_SUB
1925                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1926                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
1927                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1928                    || *reloc_type == BFD_RELOC_MIPS_REL16
1929                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1930                 fixp[0]->fx_no_overflow = 1;
1931
1932               if (unmatched_hi)
1933                 {
1934                   struct mips_hi_fixup *hi_fixup;
1935
1936                   assert (*reloc_type == BFD_RELOC_HI16_S);
1937                   hi_fixup = ((struct mips_hi_fixup *)
1938                               xmalloc (sizeof (struct mips_hi_fixup)));
1939                   hi_fixup->fixp = fixp[0];
1940                   hi_fixup->seg = now_seg;
1941                   hi_fixup->next = mips_hi_fixup_list;
1942                   mips_hi_fixup_list = hi_fixup;
1943                 }
1944
1945               if (reloc_type[1] != BFD_RELOC_UNUSED)
1946                 {
1947                   /* FIXME: This symbol can be one of
1948                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
1949                   address_expr->X_op = O_absent;
1950                   address_expr->X_add_symbol = 0;
1951                   address_expr->X_add_number = 0;
1952
1953                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1954                                          4, address_expr, false,
1955                                          reloc_type[1]);
1956
1957                   /* These relocations can have an addend that won't fit in
1958                      4 octets for 64bit assembly.  */
1959                   if (HAVE_64BIT_GPRS &&
1960                       (*reloc_type == BFD_RELOC_16
1961                        || *reloc_type == BFD_RELOC_32
1962                        || *reloc_type == BFD_RELOC_MIPS_JMP
1963                        || *reloc_type == BFD_RELOC_HI16_S
1964                        || *reloc_type == BFD_RELOC_LO16
1965                        || *reloc_type == BFD_RELOC_GPREL16
1966                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
1967                        || *reloc_type == BFD_RELOC_GPREL32
1968                        || *reloc_type == BFD_RELOC_64
1969                        || *reloc_type == BFD_RELOC_CTOR
1970                        || *reloc_type == BFD_RELOC_MIPS_SUB
1971                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1972                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
1973                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1974                        || *reloc_type == BFD_RELOC_MIPS_REL16
1975                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1976                     fixp[1]->fx_no_overflow = 1;
1977
1978                   if (reloc_type[2] != BFD_RELOC_UNUSED)
1979                     {
1980                       address_expr->X_op = O_absent;
1981                       address_expr->X_add_symbol = 0;
1982                       address_expr->X_add_number = 0;
1983
1984                       fixp[2] = fix_new_exp (frag_now,
1985                                              f - frag_now->fr_literal, 4,
1986                                              address_expr, false,
1987                                              reloc_type[2]);
1988
1989                       /* These relocations can have an addend that won't fit in
1990                          4 octets for 64bit assembly.  */
1991                       if (HAVE_64BIT_GPRS &&
1992                           (*reloc_type == BFD_RELOC_16
1993                            || *reloc_type == BFD_RELOC_32
1994                            || *reloc_type == BFD_RELOC_MIPS_JMP
1995                            || *reloc_type == BFD_RELOC_HI16_S
1996                            || *reloc_type == BFD_RELOC_LO16
1997                            || *reloc_type == BFD_RELOC_GPREL16
1998                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
1999                            || *reloc_type == BFD_RELOC_GPREL32
2000                            || *reloc_type == BFD_RELOC_64
2001                            || *reloc_type == BFD_RELOC_CTOR
2002                            || *reloc_type == BFD_RELOC_MIPS_SUB
2003                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2004                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2005                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2006                            || *reloc_type == BFD_RELOC_MIPS_REL16
2007                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2008                         fixp[2]->fx_no_overflow = 1;
2009                     }
2010                 }
2011             }
2012         }
2013     }
2014
2015   if (! mips_opts.mips16)
2016     md_number_to_chars (f, ip->insn_opcode, 4);
2017   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2018     {
2019       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2020       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2021     }
2022   else
2023     {
2024       if (ip->use_extend)
2025         {
2026           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2027           f += 2;
2028         }
2029       md_number_to_chars (f, ip->insn_opcode, 2);
2030     }
2031
2032   /* Update the register mask information.  */
2033   if (! mips_opts.mips16)
2034     {
2035       if (pinfo & INSN_WRITE_GPR_D)
2036         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2037       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2038         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2039       if (pinfo & INSN_READ_GPR_S)
2040         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2041       if (pinfo & INSN_WRITE_GPR_31)
2042         mips_gprmask |= 1 << RA;
2043       if (pinfo & INSN_WRITE_FPR_D)
2044         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2045       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2046         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2047       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2048         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2049       if ((pinfo & INSN_READ_FPR_R) != 0)
2050         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2051       if (pinfo & INSN_COP)
2052         {
2053           /* We don't keep enough information to sort these cases out.
2054              The itbl support does keep this information however, although
2055              we currently don't support itbl fprmats as part of the cop
2056              instruction.  May want to add this support in the future.  */
2057         }
2058       /* Never set the bit for $0, which is always zero.  */
2059       mips_gprmask &= ~1 << 0;
2060     }
2061   else
2062     {
2063       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2064         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2065                               & MIPS16OP_MASK_RX);
2066       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2067         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2068                               & MIPS16OP_MASK_RY);
2069       if (pinfo & MIPS16_INSN_WRITE_Z)
2070         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2071                               & MIPS16OP_MASK_RZ);
2072       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2073         mips_gprmask |= 1 << TREG;
2074       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2075         mips_gprmask |= 1 << SP;
2076       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2077         mips_gprmask |= 1 << RA;
2078       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2079         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2080       if (pinfo & MIPS16_INSN_READ_Z)
2081         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2082                               & MIPS16OP_MASK_MOVE32Z);
2083       if (pinfo & MIPS16_INSN_READ_GPR_X)
2084         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2085                               & MIPS16OP_MASK_REGR32);
2086     }
2087
2088   if (place == NULL && ! mips_opts.noreorder)
2089     {
2090       /* Filling the branch delay slot is more complex.  We try to
2091          switch the branch with the previous instruction, which we can
2092          do if the previous instruction does not set up a condition
2093          that the branch tests and if the branch is not itself the
2094          target of any branch.  */
2095       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2096           || (pinfo & INSN_COND_BRANCH_DELAY))
2097         {
2098           if (mips_optimize < 2
2099               /* If we have seen .set volatile or .set nomove, don't
2100                  optimize.  */
2101               || mips_opts.nomove != 0
2102               /* If we had to emit any NOP instructions, then we
2103                  already know we can not swap.  */
2104               || nops != 0
2105               /* If we don't even know the previous insn, we can not
2106                  swap.  */
2107               || ! prev_insn_valid
2108               /* If the previous insn is already in a branch delay
2109                  slot, then we can not swap.  */
2110               || prev_insn_is_delay_slot
2111               /* If the previous previous insn was in a .set
2112                  noreorder, we can't swap.  Actually, the MIPS
2113                  assembler will swap in this situation.  However, gcc
2114                  configured -with-gnu-as will generate code like
2115                    .set noreorder
2116                    lw   $4,XXX
2117                    .set reorder
2118                    INSN
2119                    bne  $4,$0,foo
2120                  in which we can not swap the bne and INSN.  If gcc is
2121                  not configured -with-gnu-as, it does not output the
2122                  .set pseudo-ops.  We don't have to check
2123                  prev_insn_unreordered, because prev_insn_valid will
2124                  be 0 in that case.  We don't want to use
2125                  prev_prev_insn_valid, because we do want to be able
2126                  to swap at the start of a function.  */
2127               || prev_prev_insn_unreordered
2128               /* If the branch is itself the target of a branch, we
2129                  can not swap.  We cheat on this; all we check for is
2130                  whether there is a label on this instruction.  If
2131                  there are any branches to anything other than a
2132                  label, users must use .set noreorder.  */
2133               || insn_labels != NULL
2134               /* If the previous instruction is in a variant frag, we
2135                  can not do the swap.  This does not apply to the
2136                  mips16, which uses variant frags for different
2137                  purposes.  */
2138               || (! mips_opts.mips16
2139                   && prev_insn_frag->fr_type == rs_machine_dependent)
2140               /* If the branch reads the condition codes, we don't
2141                  even try to swap, because in the sequence
2142                    ctc1 $X,$31
2143                    INSN
2144                    INSN
2145                    bc1t LABEL
2146                  we can not swap, and I don't feel like handling that
2147                  case.  */
2148               || (! mips_opts.mips16
2149                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2150                   && (pinfo & INSN_READ_COND_CODE))
2151               /* We can not swap with an instruction that requires a
2152                  delay slot, becase the target of the branch might
2153                  interfere with that instruction.  */
2154               || (! mips_opts.mips16
2155                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2156                   && (prev_pinfo
2157               /* Itbl support may require additional care here.  */
2158                       & (INSN_LOAD_COPROC_DELAY
2159                          | INSN_COPROC_MOVE_DELAY
2160                          | INSN_WRITE_COND_CODE)))
2161               || (! (hilo_interlocks
2162                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2163                   && (prev_pinfo
2164                       & (INSN_READ_LO
2165                          | INSN_READ_HI)))
2166               || (! mips_opts.mips16
2167                   && ! gpr_interlocks
2168                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2169               || (! mips_opts.mips16
2170                   && mips_opts.isa == ISA_MIPS1
2171                   /* Itbl support may require additional care here.  */
2172                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2173               /* We can not swap with a branch instruction.  */
2174               || (prev_pinfo
2175                   & (INSN_UNCOND_BRANCH_DELAY
2176                      | INSN_COND_BRANCH_DELAY
2177                      | INSN_COND_BRANCH_LIKELY))
2178               /* We do not swap with a trap instruction, since it
2179                  complicates trap handlers to have the trap
2180                  instruction be in a delay slot.  */
2181               || (prev_pinfo & INSN_TRAP)
2182               /* If the branch reads a register that the previous
2183                  instruction sets, we can not swap.  */
2184               || (! mips_opts.mips16
2185                   && (prev_pinfo & INSN_WRITE_GPR_T)
2186                   && insn_uses_reg (ip,
2187                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2188                                      & OP_MASK_RT),
2189                                     MIPS_GR_REG))
2190               || (! mips_opts.mips16
2191                   && (prev_pinfo & INSN_WRITE_GPR_D)
2192                   && insn_uses_reg (ip,
2193                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2194                                      & OP_MASK_RD),
2195                                     MIPS_GR_REG))
2196               || (mips_opts.mips16
2197                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2198                        && insn_uses_reg (ip,
2199                                          ((prev_insn.insn_opcode
2200                                            >> MIPS16OP_SH_RX)
2201                                           & MIPS16OP_MASK_RX),
2202                                          MIPS16_REG))
2203                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2204                           && insn_uses_reg (ip,
2205                                             ((prev_insn.insn_opcode
2206                                               >> MIPS16OP_SH_RY)
2207                                              & MIPS16OP_MASK_RY),
2208                                             MIPS16_REG))
2209                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2210                           && insn_uses_reg (ip,
2211                                             ((prev_insn.insn_opcode
2212                                               >> MIPS16OP_SH_RZ)
2213                                              & MIPS16OP_MASK_RZ),
2214                                             MIPS16_REG))
2215                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2216                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2217                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2218                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2219                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2220                           && insn_uses_reg (ip,
2221                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2222                                                                      insn_opcode),
2223                                             MIPS_GR_REG))))
2224               /* If the branch writes a register that the previous
2225                  instruction sets, we can not swap (we know that
2226                  branches write only to RD or to $31).  */
2227               || (! mips_opts.mips16
2228                   && (prev_pinfo & INSN_WRITE_GPR_T)
2229                   && (((pinfo & INSN_WRITE_GPR_D)
2230                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2231                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2232                       || ((pinfo & INSN_WRITE_GPR_31)
2233                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2234                                & OP_MASK_RT)
2235                               == RA))))
2236               || (! mips_opts.mips16
2237                   && (prev_pinfo & INSN_WRITE_GPR_D)
2238                   && (((pinfo & INSN_WRITE_GPR_D)
2239                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2240                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2241                       || ((pinfo & INSN_WRITE_GPR_31)
2242                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2243                                & OP_MASK_RD)
2244                               == RA))))
2245               || (mips_opts.mips16
2246                   && (pinfo & MIPS16_INSN_WRITE_31)
2247                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2248                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2249                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2250                               == RA))))
2251               /* If the branch writes a register that the previous
2252                  instruction reads, we can not swap (we know that
2253                  branches only write to RD or to $31).  */
2254               || (! mips_opts.mips16
2255                   && (pinfo & INSN_WRITE_GPR_D)
2256                   && insn_uses_reg (&prev_insn,
2257                                     ((ip->insn_opcode >> OP_SH_RD)
2258                                      & OP_MASK_RD),
2259                                     MIPS_GR_REG))
2260               || (! mips_opts.mips16
2261                   && (pinfo & INSN_WRITE_GPR_31)
2262                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2263               || (mips_opts.mips16
2264                   && (pinfo & MIPS16_INSN_WRITE_31)
2265                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2266               /* If we are generating embedded PIC code, the branch
2267                  might be expanded into a sequence which uses $at, so
2268                  we can't swap with an instruction which reads it.  */
2269               || (mips_pic == EMBEDDED_PIC
2270                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2271               /* If the previous previous instruction has a load
2272                  delay, and sets a register that the branch reads, we
2273                  can not swap.  */
2274               || (! mips_opts.mips16
2275                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2276               /* Itbl support may require additional care here.  */
2277                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2278                       || (! gpr_interlocks
2279                           && (prev_prev_insn.insn_mo->pinfo
2280                               & INSN_LOAD_MEMORY_DELAY)))
2281                   && insn_uses_reg (ip,
2282                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2283                                      & OP_MASK_RT),
2284                                     MIPS_GR_REG))
2285               /* If one instruction sets a condition code and the
2286                  other one uses a condition code, we can not swap.  */
2287               || ((pinfo & INSN_READ_COND_CODE)
2288                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2289               || ((pinfo & INSN_WRITE_COND_CODE)
2290                   && (prev_pinfo & INSN_READ_COND_CODE))
2291               /* If the previous instruction uses the PC, we can not
2292                  swap.  */
2293               || (mips_opts.mips16
2294                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2295               /* If the previous instruction was extended, we can not
2296                  swap.  */
2297               || (mips_opts.mips16 && prev_insn_extended)
2298               /* If the previous instruction had a fixup in mips16
2299                  mode, we can not swap.  This normally means that the
2300                  previous instruction was a 4 byte branch anyhow.  */
2301               || (mips_opts.mips16 && prev_insn_fixp[0])
2302               /* If the previous instruction is a sync, sync.l, or
2303                  sync.p, we can not swap.  */
2304               || (prev_pinfo & INSN_SYNC))
2305             {
2306               /* We could do even better for unconditional branches to
2307                  portions of this object file; we could pick up the
2308                  instruction at the destination, put it in the delay
2309                  slot, and bump the destination address.  */
2310               emit_nop ();
2311               /* Update the previous insn information.  */
2312               prev_prev_insn = *ip;
2313               prev_insn.insn_mo = &dummy_opcode;
2314             }
2315           else
2316             {
2317               /* It looks like we can actually do the swap.  */
2318               if (! mips_opts.mips16)
2319                 {
2320                   char *prev_f;
2321                   char temp[4];
2322
2323                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2324                   memcpy (temp, prev_f, 4);
2325                   memcpy (prev_f, f, 4);
2326                   memcpy (f, temp, 4);
2327                   if (prev_insn_fixp[0])
2328                     {
2329                       prev_insn_fixp[0]->fx_frag = frag_now;
2330                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2331                     }
2332                   if (prev_insn_fixp[1])
2333                     {
2334                       prev_insn_fixp[1]->fx_frag = frag_now;
2335                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2336                     }
2337                   if (prev_insn_fixp[2])
2338                     {
2339                       prev_insn_fixp[2]->fx_frag = frag_now;
2340                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2341                     }
2342                   if (fixp[0])
2343                     {
2344                       fixp[0]->fx_frag = prev_insn_frag;
2345                       fixp[0]->fx_where = prev_insn_where;
2346                     }
2347                   if (fixp[1])
2348                     {
2349                       fixp[1]->fx_frag = prev_insn_frag;
2350                       fixp[1]->fx_where = prev_insn_where;
2351                     }
2352                   if (fixp[2])
2353                     {
2354                       fixp[2]->fx_frag = prev_insn_frag;
2355                       fixp[2]->fx_where = prev_insn_where;
2356                     }
2357                 }
2358               else
2359                 {
2360                   char *prev_f;
2361                   char temp[2];
2362
2363                   assert (prev_insn_fixp[0] == NULL);
2364                   assert (prev_insn_fixp[1] == NULL);
2365                   assert (prev_insn_fixp[2] == NULL);
2366                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2367                   memcpy (temp, prev_f, 2);
2368                   memcpy (prev_f, f, 2);
2369                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2370                     {
2371                       assert (*reloc_type == BFD_RELOC_UNUSED);
2372                       memcpy (f, temp, 2);
2373                     }
2374                   else
2375                     {
2376                       memcpy (f, f + 2, 2);
2377                       memcpy (f + 2, temp, 2);
2378                     }
2379                   if (fixp[0])
2380                     {
2381                       fixp[0]->fx_frag = prev_insn_frag;
2382                       fixp[0]->fx_where = prev_insn_where;
2383                     }
2384                   if (fixp[1])
2385                     {
2386                       fixp[1]->fx_frag = prev_insn_frag;
2387                       fixp[1]->fx_where = prev_insn_where;
2388                     }
2389                   if (fixp[2])
2390                     {
2391                       fixp[2]->fx_frag = prev_insn_frag;
2392                       fixp[2]->fx_where = prev_insn_where;
2393                     }
2394                 }
2395
2396               /* Update the previous insn information; leave prev_insn
2397                  unchanged.  */
2398               prev_prev_insn = *ip;
2399             }
2400           prev_insn_is_delay_slot = 1;
2401
2402           /* If that was an unconditional branch, forget the previous
2403              insn information.  */
2404           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2405             {
2406               prev_prev_insn.insn_mo = &dummy_opcode;
2407               prev_insn.insn_mo = &dummy_opcode;
2408             }
2409
2410           prev_insn_fixp[0] = NULL;
2411           prev_insn_fixp[1] = NULL;
2412           prev_insn_fixp[2] = NULL;
2413           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2414           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2415           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2416           prev_insn_extended = 0;
2417         }
2418       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2419         {
2420           /* We don't yet optimize a branch likely.  What we should do
2421              is look at the target, copy the instruction found there
2422              into the delay slot, and increment the branch to jump to
2423              the next instruction.  */
2424           emit_nop ();
2425           /* Update the previous insn information.  */
2426           prev_prev_insn = *ip;
2427           prev_insn.insn_mo = &dummy_opcode;
2428           prev_insn_fixp[0] = NULL;
2429           prev_insn_fixp[1] = NULL;
2430           prev_insn_fixp[2] = NULL;
2431           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2432           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2433           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2434           prev_insn_extended = 0;
2435         }
2436       else
2437         {
2438           /* Update the previous insn information.  */
2439           if (nops > 0)
2440             prev_prev_insn.insn_mo = &dummy_opcode;
2441           else
2442             prev_prev_insn = prev_insn;
2443           prev_insn = *ip;
2444
2445           /* Any time we see a branch, we always fill the delay slot
2446              immediately; since this insn is not a branch, we know it
2447              is not in a delay slot.  */
2448           prev_insn_is_delay_slot = 0;
2449
2450           prev_insn_fixp[0] = fixp[0];
2451           prev_insn_fixp[1] = fixp[1];
2452           prev_insn_fixp[2] = fixp[2];
2453           prev_insn_reloc_type[0] = reloc_type[0];
2454           prev_insn_reloc_type[1] = reloc_type[1];
2455           prev_insn_reloc_type[2] = reloc_type[2];
2456           if (mips_opts.mips16)
2457             prev_insn_extended = (ip->use_extend
2458                                   || *reloc_type > BFD_RELOC_UNUSED);
2459         }
2460
2461       prev_prev_insn_unreordered = prev_insn_unreordered;
2462       prev_insn_unreordered = 0;
2463       prev_insn_frag = frag_now;
2464       prev_insn_where = f - frag_now->fr_literal;
2465       prev_insn_valid = 1;
2466     }
2467   else if (place == NULL)
2468     {
2469       /* We need to record a bit of information even when we are not
2470          reordering, in order to determine the base address for mips16
2471          PC relative relocs.  */
2472       prev_prev_insn = prev_insn;
2473       prev_insn = *ip;
2474       prev_insn_reloc_type[0] = reloc_type[0];
2475       prev_insn_reloc_type[1] = reloc_type[1];
2476       prev_insn_reloc_type[2] = reloc_type[2];
2477       prev_prev_insn_unreordered = prev_insn_unreordered;
2478       prev_insn_unreordered = 1;
2479     }
2480
2481   /* We just output an insn, so the next one doesn't have a label.  */
2482   mips_clear_insn_labels ();
2483
2484   /* We must ensure that a fixup associated with an unmatched %hi
2485      reloc does not become a variant frag.  Otherwise, the
2486      rearrangement of %hi relocs in frob_file may confuse
2487      tc_gen_reloc.  */
2488   if (unmatched_hi)
2489     {
2490       frag_wane (frag_now);
2491       frag_new (0);
2492     }
2493 }
2494
2495 /* This function forgets that there was any previous instruction or
2496    label.  If PRESERVE is non-zero, it remembers enough information to
2497    know whether nops are needed before a noreorder section.  */
2498
2499 static void
2500 mips_no_prev_insn (preserve)
2501      int preserve;
2502 {
2503   if (! preserve)
2504     {
2505       prev_insn.insn_mo = &dummy_opcode;
2506       prev_prev_insn.insn_mo = &dummy_opcode;
2507       prev_nop_frag = NULL;
2508       prev_nop_frag_holds = 0;
2509       prev_nop_frag_required = 0;
2510       prev_nop_frag_since = 0;
2511     }
2512   prev_insn_valid = 0;
2513   prev_insn_is_delay_slot = 0;
2514   prev_insn_unreordered = 0;
2515   prev_insn_extended = 0;
2516   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2517   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2518   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2519   prev_prev_insn_unreordered = 0;
2520   mips_clear_insn_labels ();
2521 }
2522
2523 /* This function must be called whenever we turn on noreorder or emit
2524    something other than instructions.  It inserts any NOPS which might
2525    be needed by the previous instruction, and clears the information
2526    kept for the previous instructions.  The INSNS parameter is true if
2527    instructions are to follow.  */
2528
2529 static void
2530 mips_emit_delays (insns)
2531      boolean insns;
2532 {
2533   if (! mips_opts.noreorder)
2534     {
2535       int nops;
2536
2537       nops = 0;
2538       if ((! mips_opts.mips16
2539            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2540            && (! cop_interlocks
2541                && (prev_insn.insn_mo->pinfo
2542                    & (INSN_LOAD_COPROC_DELAY
2543                       | INSN_COPROC_MOVE_DELAY
2544                       | INSN_WRITE_COND_CODE))))
2545           || (! hilo_interlocks
2546               && (prev_insn.insn_mo->pinfo
2547                   & (INSN_READ_LO
2548                      | INSN_READ_HI)))
2549           || (! mips_opts.mips16
2550               && ! gpr_interlocks
2551               && (prev_insn.insn_mo->pinfo
2552                   & INSN_LOAD_MEMORY_DELAY))
2553           || (! mips_opts.mips16
2554               && mips_opts.isa == ISA_MIPS1
2555               && (prev_insn.insn_mo->pinfo
2556                   & INSN_COPROC_MEMORY_DELAY)))
2557         {
2558           /* Itbl support may require additional care here.  */
2559           ++nops;
2560           if ((! mips_opts.mips16
2561                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2562                && (! cop_interlocks
2563                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2564               || (! hilo_interlocks
2565                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2566                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2567             ++nops;
2568
2569           if (prev_insn_unreordered)
2570             nops = 0;
2571         }
2572       else if ((! mips_opts.mips16
2573                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2574                 && (! cop_interlocks
2575                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2576                || (! hilo_interlocks
2577                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2578                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2579         {
2580           /* Itbl support may require additional care here.  */
2581           if (! prev_prev_insn_unreordered)
2582             ++nops;
2583         }
2584
2585       if (nops > 0)
2586         {
2587           struct insn_label_list *l;
2588
2589           if (insns)
2590             {
2591               /* Record the frag which holds the nop instructions, so
2592                  that we can remove them if we don't need them.  */
2593               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2594               prev_nop_frag = frag_now;
2595               prev_nop_frag_holds = nops;
2596               prev_nop_frag_required = 0;
2597               prev_nop_frag_since = 0;
2598             }
2599
2600           for (; nops > 0; --nops)
2601             emit_nop ();
2602
2603           if (insns)
2604             {
2605               /* Move on to a new frag, so that it is safe to simply
2606                  decrease the size of prev_nop_frag.  */
2607               frag_wane (frag_now);
2608               frag_new (0);
2609             }
2610
2611           for (l = insn_labels; l != NULL; l = l->next)
2612             {
2613               valueT val;
2614
2615               assert (S_GET_SEGMENT (l->label) == now_seg);
2616               symbol_set_frag (l->label, frag_now);
2617               val = (valueT) frag_now_fix ();
2618               /* mips16 text labels are stored as odd.  */
2619               if (mips_opts.mips16)
2620                 ++val;
2621               S_SET_VALUE (l->label, val);
2622             }
2623         }
2624     }
2625
2626   /* Mark instruction labels in mips16 mode.  */
2627   if (insns)
2628     mips16_mark_labels ();
2629
2630   mips_no_prev_insn (insns);
2631 }
2632
2633 /* Build an instruction created by a macro expansion.  This is passed
2634    a pointer to the count of instructions created so far, an
2635    expression, the name of the instruction to build, an operand format
2636    string, and corresponding arguments.  */
2637
2638 #ifdef USE_STDARG
2639 static void
2640 macro_build (char *place,
2641              int *counter,
2642              expressionS * ep,
2643              const char *name,
2644              const char *fmt,
2645              ...)
2646 #else
2647 static void
2648 macro_build (place, counter, ep, name, fmt, va_alist)
2649      char *place;
2650      int *counter;
2651      expressionS *ep;
2652      const char *name;
2653      const char *fmt;
2654      va_dcl
2655 #endif
2656 {
2657   struct mips_cl_insn insn;
2658   bfd_reloc_code_real_type r[3];
2659   va_list args;
2660
2661 #ifdef USE_STDARG
2662   va_start (args, fmt);
2663 #else
2664   va_start (args);
2665 #endif
2666
2667   /*
2668    * If the macro is about to expand into a second instruction,
2669    * print a warning if needed. We need to pass ip as a parameter
2670    * to generate a better warning message here...
2671    */
2672   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2673     as_warn (_("Macro instruction expanded into multiple instructions"));
2674
2675   /*
2676    * If the macro is about to expand into a second instruction,
2677    * and it is in a delay slot, print a warning.
2678    */
2679   if (place == NULL
2680       && *counter == 1
2681       && mips_opts.noreorder
2682       && (prev_prev_insn.insn_mo->pinfo
2683           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2684              | INSN_COND_BRANCH_LIKELY)) != 0)
2685     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2686
2687   if (place == NULL)
2688     ++*counter;         /* bump instruction counter */
2689
2690   if (mips_opts.mips16)
2691     {
2692       mips16_macro_build (place, counter, ep, name, fmt, args);
2693       va_end (args);
2694       return;
2695     }
2696
2697   r[0] = BFD_RELOC_UNUSED;
2698   r[1] = BFD_RELOC_UNUSED;
2699   r[2] = BFD_RELOC_UNUSED;
2700   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2701   assert (insn.insn_mo);
2702   assert (strcmp (name, insn.insn_mo->name) == 0);
2703
2704   /* Search until we get a match for NAME.  */
2705   while (1)
2706     {
2707       /* It is assumed here that macros will never generate 
2708          MDMX or MIPS-3D instructions.  */
2709       if (strcmp (fmt, insn.insn_mo->args) == 0
2710           && insn.insn_mo->pinfo != INSN_MACRO
2711           && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2712           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2713         break;
2714
2715       ++insn.insn_mo;
2716       assert (insn.insn_mo->name);
2717       assert (strcmp (name, insn.insn_mo->name) == 0);
2718     }
2719
2720   insn.insn_opcode = insn.insn_mo->match;
2721   for (;;)
2722     {
2723       switch (*fmt++)
2724         {
2725         case '\0':
2726           break;
2727
2728         case ',':
2729         case '(':
2730         case ')':
2731           continue;
2732
2733         case 't':
2734         case 'w':
2735         case 'E':
2736           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2737           continue;
2738
2739         case 'c':
2740           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2741           continue;
2742
2743         case 'T':
2744         case 'W':
2745           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2746           continue;
2747
2748         case 'd':
2749         case 'G':
2750           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2751           continue;
2752
2753         case 'U':
2754           {
2755             int tmp = va_arg (args, int);
2756
2757             insn.insn_opcode |= tmp << OP_SH_RT;
2758             insn.insn_opcode |= tmp << OP_SH_RD;
2759             continue;
2760           }
2761
2762         case 'V':
2763         case 'S':
2764           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2765           continue;
2766
2767         case 'z':
2768           continue;
2769
2770         case '<':
2771           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2772           continue;
2773
2774         case 'D':
2775           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2776           continue;
2777
2778         case 'B':
2779           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2780           continue;
2781
2782         case 'J':
2783           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2784           continue;
2785
2786         case 'q':
2787           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2788           continue;
2789
2790         case 'b':
2791         case 's':
2792         case 'r':
2793         case 'v':
2794           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2795           continue;
2796
2797         case 'i':
2798         case 'j':
2799         case 'o':
2800           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2801           assert (*r == BFD_RELOC_GPREL16
2802                   || *r == BFD_RELOC_MIPS_LITERAL
2803                   || *r == BFD_RELOC_MIPS_HIGHER
2804                   || *r == BFD_RELOC_HI16_S
2805                   || *r == BFD_RELOC_LO16
2806                   || *r == BFD_RELOC_MIPS_GOT16
2807                   || *r == BFD_RELOC_MIPS_CALL16
2808                   || *r == BFD_RELOC_MIPS_GOT_DISP
2809                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2810                   || *r == BFD_RELOC_MIPS_GOT_OFST
2811                   || *r == BFD_RELOC_MIPS_GOT_LO16
2812                   || *r == BFD_RELOC_MIPS_CALL_LO16
2813                   || (ep->X_op == O_subtract
2814                       && *r == BFD_RELOC_PCREL_LO16));
2815           continue;
2816
2817         case 'u':
2818           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2819           assert (ep != NULL
2820                   && (ep->X_op == O_constant
2821                       || (ep->X_op == O_symbol
2822                           && (*r == BFD_RELOC_MIPS_HIGHEST
2823                               || *r == BFD_RELOC_HI16_S
2824                               || *r == BFD_RELOC_HI16
2825                               || *r == BFD_RELOC_GPREL16
2826                               || *r == BFD_RELOC_MIPS_GOT_HI16
2827                               || *r == BFD_RELOC_MIPS_CALL_HI16))
2828                       || (ep->X_op == O_subtract
2829                           && *r == BFD_RELOC_PCREL_HI16_S)));
2830           continue;
2831
2832         case 'p':
2833           assert (ep != NULL);
2834           /*
2835            * This allows macro() to pass an immediate expression for
2836            * creating short branches without creating a symbol.
2837            * Note that the expression still might come from the assembly
2838            * input, in which case the value is not checked for range nor
2839            * is a relocation entry generated (yuck).
2840            */
2841           if (ep->X_op == O_constant)
2842             {
2843               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2844               ep = NULL;
2845             }
2846           else
2847             if (mips_pic == EMBEDDED_PIC)
2848               *r = BFD_RELOC_16_PCREL_S2;
2849             else
2850               *r = BFD_RELOC_16_PCREL;
2851           continue;
2852
2853         case 'a':
2854           assert (ep != NULL);
2855           *r = BFD_RELOC_MIPS_JMP;
2856           continue;
2857
2858         case 'C':
2859           insn.insn_opcode |= va_arg (args, unsigned long);
2860           continue;
2861
2862         default:
2863           internalError ();
2864         }
2865       break;
2866     }
2867   va_end (args);
2868   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2869
2870   append_insn (place, &insn, ep, r, false);
2871 }
2872
2873 static void
2874 mips16_macro_build (place, counter, ep, name, fmt, args)
2875      char *place;
2876      int *counter ATTRIBUTE_UNUSED;
2877      expressionS *ep;
2878      const char *name;
2879      const char *fmt;
2880      va_list args;
2881 {
2882   struct mips_cl_insn insn;
2883   bfd_reloc_code_real_type r[3]
2884     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2885
2886   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2887   assert (insn.insn_mo);
2888   assert (strcmp (name, insn.insn_mo->name) == 0);
2889
2890   while (strcmp (fmt, insn.insn_mo->args) != 0
2891          || insn.insn_mo->pinfo == INSN_MACRO)
2892     {
2893       ++insn.insn_mo;
2894       assert (insn.insn_mo->name);
2895       assert (strcmp (name, insn.insn_mo->name) == 0);
2896     }
2897
2898   insn.insn_opcode = insn.insn_mo->match;
2899   insn.use_extend = false;
2900
2901   for (;;)
2902     {
2903       int c;
2904
2905       c = *fmt++;
2906       switch (c)
2907         {
2908         case '\0':
2909           break;
2910
2911         case ',':
2912         case '(':
2913         case ')':
2914           continue;
2915
2916         case 'y':
2917         case 'w':
2918           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2919           continue;
2920
2921         case 'x':
2922         case 'v':
2923           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2924           continue;
2925
2926         case 'z':
2927           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2928           continue;
2929
2930         case 'Z':
2931           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2932           continue;
2933
2934         case '0':
2935         case 'S':
2936         case 'P':
2937         case 'R':
2938           continue;
2939
2940         case 'X':
2941           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2942           continue;
2943
2944         case 'Y':
2945           {
2946             int regno;
2947
2948             regno = va_arg (args, int);
2949             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2950             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2951           }
2952           continue;
2953
2954         case '<':
2955         case '>':
2956         case '4':
2957         case '5':
2958         case 'H':
2959         case 'W':
2960         case 'D':
2961         case 'j':
2962         case '8':
2963         case 'V':
2964         case 'C':
2965         case 'U':
2966         case 'k':
2967         case 'K':
2968         case 'p':
2969         case 'q':
2970           {
2971             assert (ep != NULL);
2972
2973             if (ep->X_op != O_constant)
2974               *r = (int) BFD_RELOC_UNUSED + c;
2975             else
2976               {
2977                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2978                               false, &insn.insn_opcode, &insn.use_extend,
2979                               &insn.extend);
2980                 ep = NULL;
2981                 *r = BFD_RELOC_UNUSED;
2982               }
2983           }
2984           continue;
2985
2986         case '6':
2987           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2988           continue;
2989         }
2990
2991       break;
2992     }
2993
2994   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2995
2996   append_insn (place, &insn, ep, r, false);
2997 }
2998
2999 /*
3000  * Generate a "jalr" instruction with a relocation hint to the called
3001  * function.  This occurs in NewABI PIC code.
3002  */
3003 static void
3004 macro_build_jalr (icnt, ep)
3005      int icnt;
3006      expressionS *ep;
3007 {
3008   if (HAVE_NEWABI)
3009     frag_more (0);
3010   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3011                RA, PIC_CALL_REG);
3012   if (HAVE_NEWABI)
3013     fix_new_exp (frag_now, 0, 0, ep, false, BFD_RELOC_MIPS_JALR);
3014 }
3015
3016 /*
3017  * Generate a "lui" instruction.
3018  */
3019 static void
3020 macro_build_lui (place, counter, ep, regnum)
3021      char *place;
3022      int *counter;
3023      expressionS *ep;
3024      int regnum;
3025 {
3026   expressionS high_expr;
3027   struct mips_cl_insn insn;
3028   bfd_reloc_code_real_type r[3]
3029     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3030   CONST char *name = "lui";
3031   CONST char *fmt = "t,u";
3032
3033   assert (! mips_opts.mips16);
3034
3035   if (place == NULL)
3036     high_expr = *ep;
3037   else
3038     {
3039       high_expr.X_op = O_constant;
3040       high_expr.X_add_number = ep->X_add_number;
3041     }
3042
3043   if (high_expr.X_op == O_constant)
3044     {
3045       /* we can compute the instruction now without a relocation entry */
3046       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3047                                 >> 16) & 0xffff;
3048       *r = BFD_RELOC_UNUSED;
3049     }
3050   else if (! HAVE_NEWABI)
3051     {
3052       assert (ep->X_op == O_symbol);
3053       /* _gp_disp is a special case, used from s_cpload.  */
3054       assert (mips_pic == NO_PIC
3055               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3056       *r = BFD_RELOC_HI16_S;
3057     }
3058
3059   /*
3060    * If the macro is about to expand into a second instruction,
3061    * print a warning if needed. We need to pass ip as a parameter
3062    * to generate a better warning message here...
3063    */
3064   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3065     as_warn (_("Macro instruction expanded into multiple instructions"));
3066
3067   if (place == NULL)
3068     ++*counter;         /* bump instruction counter */
3069
3070   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3071   assert (insn.insn_mo);
3072   assert (strcmp (name, insn.insn_mo->name) == 0);
3073   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3074
3075   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3076   if (*r == BFD_RELOC_UNUSED)
3077     {
3078       insn.insn_opcode |= high_expr.X_add_number;
3079       append_insn (place, &insn, NULL, r, false);
3080     }
3081   else
3082     append_insn (place, &insn, &high_expr, r, false);
3083 }
3084
3085 /*                      set_at()
3086  * Generates code to set the $at register to true (one)
3087  * if reg is less than the immediate expression.
3088  */
3089 static void
3090 set_at (counter, reg, unsignedp)
3091      int *counter;
3092      int reg;
3093      int unsignedp;
3094 {
3095   if (imm_expr.X_op == O_constant
3096       && imm_expr.X_add_number >= -0x8000
3097       && imm_expr.X_add_number < 0x8000)
3098     macro_build ((char *) NULL, counter, &imm_expr,
3099                  unsignedp ? "sltiu" : "slti",
3100                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3101   else
3102     {
3103       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3104       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3105                    unsignedp ? "sltu" : "slt",
3106                    "d,v,t", AT, reg, AT);
3107     }
3108 }
3109
3110 /* Warn if an expression is not a constant.  */
3111
3112 static void
3113 check_absolute_expr (ip, ex)
3114      struct mips_cl_insn *ip;
3115      expressionS *ex;
3116 {
3117   if (ex->X_op == O_big)
3118     as_bad (_("unsupported large constant"));
3119   else if (ex->X_op != O_constant)
3120     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3121 }
3122
3123 /* Count the leading zeroes by performing a binary chop. This is a
3124    bulky bit of source, but performance is a LOT better for the
3125    majority of values than a simple loop to count the bits:
3126        for (lcnt = 0; (lcnt < 32); lcnt++)
3127          if ((v) & (1 << (31 - lcnt)))
3128            break;
3129   However it is not code size friendly, and the gain will drop a bit
3130   on certain cached systems.
3131 */
3132 #define COUNT_TOP_ZEROES(v)             \
3133   (((v) & ~0xffff) == 0                 \
3134    ? ((v) & ~0xff) == 0                 \
3135      ? ((v) & ~0xf) == 0                \
3136        ? ((v) & ~0x3) == 0              \
3137          ? ((v) & ~0x1) == 0            \
3138            ? !(v)                       \
3139              ? 32                       \
3140              : 31                       \
3141            : 30                         \
3142          : ((v) & ~0x7) == 0            \
3143            ? 29                         \
3144            : 28                         \
3145        : ((v) & ~0x3f) == 0             \
3146          ? ((v) & ~0x1f) == 0           \
3147            ? 27                         \
3148            : 26                         \
3149          : ((v) & ~0x7f) == 0           \
3150            ? 25                         \
3151            : 24                         \
3152      : ((v) & ~0xfff) == 0              \
3153        ? ((v) & ~0x3ff) == 0            \
3154          ? ((v) & ~0x1ff) == 0          \
3155            ? 23                         \
3156            : 22                         \
3157          : ((v) & ~0x7ff) == 0          \
3158            ? 21                         \
3159            : 20                         \
3160        : ((v) & ~0x3fff) == 0           \
3161          ? ((v) & ~0x1fff) == 0         \
3162            ? 19                         \
3163            : 18                         \
3164          : ((v) & ~0x7fff) == 0         \
3165            ? 17                         \
3166            : 16                         \
3167    : ((v) & ~0xffffff) == 0             \
3168      ? ((v) & ~0xfffff) == 0            \
3169        ? ((v) & ~0x3ffff) == 0          \
3170          ? ((v) & ~0x1ffff) == 0        \
3171            ? 15                         \
3172            : 14                         \
3173          : ((v) & ~0x7ffff) == 0        \
3174            ? 13                         \
3175            : 12                         \
3176        : ((v) & ~0x3fffff) == 0         \
3177          ? ((v) & ~0x1fffff) == 0       \
3178            ? 11                         \
3179            : 10                         \
3180          : ((v) & ~0x7fffff) == 0       \
3181            ? 9                          \
3182            : 8                          \
3183      : ((v) & ~0xfffffff) == 0          \
3184        ? ((v) & ~0x3ffffff) == 0        \
3185          ? ((v) & ~0x1ffffff) == 0      \
3186            ? 7                          \
3187            : 6                          \
3188          : ((v) & ~0x7ffffff) == 0      \
3189            ? 5                          \
3190            : 4                          \
3191        : ((v) & ~0x3fffffff) == 0       \
3192          ? ((v) & ~0x1fffffff) == 0     \
3193            ? 3                          \
3194            : 2                          \
3195          : ((v) & ~0x7fffffff) == 0     \
3196            ? 1                          \
3197            : 0)
3198
3199 /* Is the given value a sign-extended 32-bit value?  */
3200 #define IS_SEXT_32BIT_NUM(x)                                            \
3201   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
3202    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3203
3204 /*                      load_register()
3205  *  This routine generates the least number of instructions neccessary to load
3206  *  an absolute expression value into a register.
3207  */
3208 static void
3209 load_register (counter, reg, ep, dbl)
3210      int *counter;
3211      int reg;
3212      expressionS *ep;
3213      int dbl;
3214 {
3215   int freg;
3216   expressionS hi32, lo32;
3217
3218   if (ep->X_op != O_big)
3219     {
3220       assert (ep->X_op == O_constant);
3221       if (ep->X_add_number < 0x8000
3222           && (ep->X_add_number >= 0
3223               || (ep->X_add_number >= -0x8000
3224                   && (! dbl
3225                       || ! ep->X_unsigned
3226                       || sizeof (ep->X_add_number) > 4))))
3227         {
3228           /* We can handle 16 bit signed values with an addiu to
3229              $zero.  No need to ever use daddiu here, since $zero and
3230              the result are always correct in 32 bit mode.  */
3231           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3232                        (int) BFD_RELOC_LO16);
3233           return;
3234         }
3235       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3236         {
3237           /* We can handle 16 bit unsigned values with an ori to
3238              $zero.  */
3239           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3240                        (int) BFD_RELOC_LO16);
3241           return;
3242         }
3243       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3244                 && (! dbl
3245                     || ! ep->X_unsigned
3246                     || sizeof (ep->X_add_number) > 4
3247                     || (ep->X_add_number & 0x80000000) == 0))
3248                || ((HAVE_32BIT_GPRS || ! dbl)
3249                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3250                || (HAVE_32BIT_GPRS
3251                    && ! dbl
3252                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3253                        == ~ (offsetT) 0xffffffff)))
3254         {
3255           /* 32 bit values require an lui.  */
3256           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3257                        (int) BFD_RELOC_HI16);
3258           if ((ep->X_add_number & 0xffff) != 0)
3259             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3260                          (int) BFD_RELOC_LO16);
3261           return;
3262         }
3263     }
3264
3265   /* The value is larger than 32 bits.  */
3266
3267   if (HAVE_32BIT_GPRS)
3268     {
3269       as_bad (_("Number (0x%lx) larger than 32 bits"),
3270               (unsigned long) ep->X_add_number);
3271       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3272                    (int) BFD_RELOC_LO16);
3273       return;
3274     }
3275
3276   if (ep->X_op != O_big)
3277     {
3278       hi32 = *ep;
3279       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3280       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3281       hi32.X_add_number &= 0xffffffff;
3282       lo32 = *ep;
3283       lo32.X_add_number &= 0xffffffff;
3284     }
3285   else
3286     {
3287       assert (ep->X_add_number > 2);
3288       if (ep->X_add_number == 3)
3289         generic_bignum[3] = 0;
3290       else if (ep->X_add_number > 4)
3291         as_bad (_("Number larger than 64 bits"));
3292       lo32.X_op = O_constant;
3293       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3294       hi32.X_op = O_constant;
3295       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3296     }
3297
3298   if (hi32.X_add_number == 0)
3299     freg = 0;
3300   else
3301     {
3302       int shift, bit;
3303       unsigned long hi, lo;
3304
3305       if (hi32.X_add_number == (offsetT) 0xffffffff)
3306         {
3307           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3308             {
3309               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3310                            reg, 0, (int) BFD_RELOC_LO16);
3311               return;
3312             }
3313           if (lo32.X_add_number & 0x80000000)
3314             {
3315               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3316                            (int) BFD_RELOC_HI16);
3317               if (lo32.X_add_number & 0xffff)
3318                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3319                              reg, reg, (int) BFD_RELOC_LO16);
3320               return;
3321             }
3322         }
3323
3324       /* Check for 16bit shifted constant.  We know that hi32 is
3325          non-zero, so start the mask on the first bit of the hi32
3326          value.  */
3327       shift = 17;
3328       do
3329         {
3330           unsigned long himask, lomask;
3331
3332           if (shift < 32)
3333             {
3334               himask = 0xffff >> (32 - shift);
3335               lomask = (0xffff << shift) & 0xffffffff;
3336             }
3337           else
3338             {
3339               himask = 0xffff << (shift - 32);
3340               lomask = 0;
3341             }
3342           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3343               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3344             {
3345               expressionS tmp;
3346
3347               tmp.X_op = O_constant;
3348               if (shift < 32)
3349                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3350                                     | (lo32.X_add_number >> shift));
3351               else
3352                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3353               macro_build ((char *) NULL, counter, &tmp,
3354                            "ori", "t,r,i", reg, 0,
3355                            (int) BFD_RELOC_LO16);
3356               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3357                            (shift >= 32) ? "dsll32" : "dsll",
3358                            "d,w,<", reg, reg,
3359                            (shift >= 32) ? shift - 32 : shift);
3360               return;
3361             }
3362           ++shift;
3363         }
3364       while (shift <= (64 - 16));
3365
3366       /* Find the bit number of the lowest one bit, and store the
3367          shifted value in hi/lo.  */
3368       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3369       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3370       if (lo != 0)
3371         {
3372           bit = 0;
3373           while ((lo & 1) == 0)
3374             {
3375               lo >>= 1;
3376               ++bit;
3377             }
3378           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3379           hi >>= bit;
3380         }
3381       else
3382         {
3383           bit = 32;
3384           while ((hi & 1) == 0)
3385             {
3386               hi >>= 1;
3387               ++bit;
3388             }
3389           lo = hi;
3390           hi = 0;
3391         }
3392
3393       /* Optimize if the shifted value is a (power of 2) - 1.  */
3394       if ((hi == 0 && ((lo + 1) & lo) == 0)
3395           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3396         {
3397           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3398           if (shift != 0)
3399             {
3400               expressionS tmp;
3401
3402               /* This instruction will set the register to be all
3403                  ones.  */
3404               tmp.X_op = O_constant;
3405               tmp.X_add_number = (offsetT) -1;
3406               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3407                            reg, 0, (int) BFD_RELOC_LO16);
3408               if (bit != 0)
3409                 {
3410                   bit += shift;
3411                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3412                                (bit >= 32) ? "dsll32" : "dsll",
3413                                "d,w,<", reg, reg,
3414                                (bit >= 32) ? bit - 32 : bit);
3415                 }
3416               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3417                            (shift >= 32) ? "dsrl32" : "dsrl",
3418                            "d,w,<", reg, reg,
3419                            (shift >= 32) ? shift - 32 : shift);
3420               return;
3421             }
3422         }
3423
3424       /* Sign extend hi32 before calling load_register, because we can
3425          generally get better code when we load a sign extended value.  */
3426       if ((hi32.X_add_number & 0x80000000) != 0)
3427         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3428       load_register (counter, reg, &hi32, 0);
3429       freg = reg;
3430     }
3431   if ((lo32.X_add_number & 0xffff0000) == 0)
3432     {
3433       if (freg != 0)
3434         {
3435           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3436                        "dsll32", "d,w,<", reg, freg, 0);
3437           freg = reg;
3438         }
3439     }
3440   else
3441     {
3442       expressionS mid16;
3443
3444       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3445         {
3446           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3447                        (int) BFD_RELOC_HI16);
3448           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3449                        "dsrl32", "d,w,<", reg, reg, 0);
3450           return;
3451         }
3452
3453       if (freg != 0)
3454         {
3455           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3456                        "d,w,<", reg, freg, 16);
3457           freg = reg;
3458         }
3459       mid16 = lo32;
3460       mid16.X_add_number >>= 16;
3461       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3462                    freg, (int) BFD_RELOC_LO16);
3463       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3464                    "d,w,<", reg, reg, 16);
3465       freg = reg;
3466     }
3467   if ((lo32.X_add_number & 0xffff) != 0)
3468     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3469                  (int) BFD_RELOC_LO16);
3470 }
3471
3472 /* Load an address into a register.  */
3473
3474 static void
3475 load_address (counter, reg, ep, used_at)
3476      int *counter;
3477      int reg;
3478      expressionS *ep;
3479      int *used_at;
3480 {
3481   char *p = NULL;
3482
3483   if (ep->X_op != O_constant
3484       && ep->X_op != O_symbol)
3485     {
3486       as_bad (_("expression too complex"));
3487       ep->X_op = O_constant;
3488     }
3489
3490   if (ep->X_op == O_constant)
3491     {
3492       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3493       return;
3494     }
3495
3496   if (mips_pic == NO_PIC)
3497     {
3498       /* If this is a reference to a GP relative symbol, we want
3499            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3500          Otherwise we want
3501            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3502            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3503          If we have an addend, we always use the latter form.
3504
3505          With 64bit address space and a usable $at we want
3506            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3507            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3508            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3509            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3510            dsll32       $reg,0
3511            dadd         $reg,$reg,$at
3512
3513          If $at is already in use, we use an path which is suboptimal
3514          on superscalar processors.
3515            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3516            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3517            dsll         $reg,16
3518            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3519            dsll         $reg,16
3520            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3521        */
3522       if (HAVE_64BIT_ADDRESSES)
3523         {
3524           /* We don't do GP optimization for now because RELAX_ENCODE can't
3525              hold the data for such large chunks.  */
3526
3527           if (*used_at == 0)
3528             {
3529               macro_build (p, counter, ep, "lui", "t,u",
3530                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3531               macro_build (p, counter, ep, "lui", "t,u",
3532                            AT, (int) BFD_RELOC_HI16_S);
3533               macro_build (p, counter, ep, "daddiu", "t,r,j",
3534                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3535               macro_build (p, counter, ep, "daddiu", "t,r,j",
3536                            AT, AT, (int) BFD_RELOC_LO16);
3537               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3538                            "d,w,<", reg, reg, 0);
3539               macro_build (p, counter, (expressionS *) NULL, "dadd",
3540                            "d,v,t", reg, reg, AT);
3541               *used_at = 1;
3542             }
3543           else
3544             {
3545               macro_build (p, counter, ep, "lui", "t,u",
3546                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3547               macro_build (p, counter, ep, "daddiu", "t,r,j",
3548                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3549               macro_build (p, counter, (expressionS *) NULL, "dsll",
3550                            "d,w,<", reg, reg, 16);
3551               macro_build (p, counter, ep, "daddiu", "t,r,j",
3552                            reg, reg, (int) BFD_RELOC_HI16_S);
3553               macro_build (p, counter, (expressionS *) NULL, "dsll",
3554                            "d,w,<", reg, reg, 16);
3555               macro_build (p, counter, ep, "daddiu", "t,r,j",
3556                            reg, reg, (int) BFD_RELOC_LO16);
3557             }
3558         }
3559       else
3560         {
3561           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3562               && ! nopic_need_relax (ep->X_add_symbol, 1))
3563             {
3564               frag_grow (20);
3565               macro_build ((char *) NULL, counter, ep,
3566                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3567                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3568               p = frag_var (rs_machine_dependent, 8, 0,
3569                             RELAX_ENCODE (4, 8, 0, 4, 0,
3570                                           mips_opts.warn_about_macros),
3571                             ep->X_add_symbol, 0, NULL);
3572             }
3573           macro_build_lui (p, counter, ep, reg);
3574           if (p != NULL)
3575             p += 4;
3576           macro_build (p, counter, ep,
3577                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3578                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3579         }
3580     }
3581   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3582     {
3583       expressionS ex;
3584
3585       /* If this is a reference to an external symbol, we want
3586            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3587          Otherwise we want
3588            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3589            nop
3590            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3591          If there is a constant, it must be added in after.  */
3592       ex.X_add_number = ep->X_add_number;
3593       ep->X_add_number = 0;
3594       frag_grow (20);
3595       macro_build ((char *) NULL, counter, ep,
3596                    HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3597                    reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3598       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3599       p = frag_var (rs_machine_dependent, 4, 0,
3600                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3601                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3602       macro_build (p, counter, ep,
3603                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3604                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3605       if (ex.X_add_number != 0)
3606         {
3607           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3608             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3609           ex.X_op = O_constant;
3610           macro_build ((char *) NULL, counter, &ex,
3611                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3612                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3613         }
3614     }
3615   else if (mips_pic == SVR4_PIC)
3616     {
3617       expressionS ex;
3618       int off;
3619
3620       /* This is the large GOT case.  If this is a reference to an
3621          external symbol, we want
3622            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3623            addu         $reg,$reg,$gp
3624            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3625          Otherwise, for a reference to a local symbol, we want
3626            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3627            nop
3628            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3629          If we have NewABI, we want
3630            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3631            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3632          If there is a constant, it must be added in after.  */
3633       ex.X_add_number = ep->X_add_number;
3634       ep->X_add_number = 0;
3635       if (HAVE_NEWABI)
3636         {
3637           macro_build ((char *) NULL, counter, ep,
3638                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3639                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3640           macro_build (p, counter, ep,
3641                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3642                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3643         }
3644       else
3645         {
3646           if (reg_needs_delay (mips_gp_register))
3647             off = 4;
3648           else
3649             off = 0;
3650           frag_grow (32);
3651           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3652                        (int) BFD_RELOC_MIPS_GOT_HI16);
3653           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3654                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3655                        reg, mips_gp_register);
3656           macro_build ((char *) NULL, counter, ep,
3657                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3658                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3659           p = frag_var (rs_machine_dependent, 12 + off, 0,
3660                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3661                                       mips_opts.warn_about_macros),
3662                         ep->X_add_symbol, 0, NULL);
3663           if (off > 0)
3664             {
3665               /* We need a nop before loading from $gp.  This special
3666                  check is required because the lui which starts the main
3667                  instruction stream does not refer to $gp, and so will not
3668                  insert the nop which may be required.  */
3669               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3670                 p += 4;
3671             }
3672           macro_build (p, counter, ep,
3673                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3674                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3675           p += 4;
3676           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3677           p += 4;
3678           macro_build (p, counter, ep,
3679                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3680                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3681         }
3682
3683       if (ex.X_add_number != 0)
3684         {
3685           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3686             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3687           ex.X_op = O_constant;
3688           macro_build ((char *) NULL, counter, &ex,
3689                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3690                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3691         }
3692     }
3693   else if (mips_pic == EMBEDDED_PIC)
3694     {
3695       /* We always do
3696            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3697        */
3698       macro_build ((char *) NULL, counter, ep,
3699                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3700                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3701     }
3702   else
3703     abort ();
3704 }
3705
3706 /* Move the contents of register SOURCE into register DEST.  */
3707
3708 static void
3709 move_register (counter, dest, source)
3710      int *counter;
3711      int dest;
3712      int source;
3713 {
3714   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3715                HAVE_32BIT_GPRS ? "addu" : "daddu",
3716                "d,v,t", dest, source, 0);
3717 }
3718
3719 /*
3720  *                      Build macros
3721  *   This routine implements the seemingly endless macro or synthesized
3722  * instructions and addressing modes in the mips assembly language. Many
3723  * of these macros are simple and are similar to each other. These could
3724  * probably be handled by some kind of table or grammer aproach instead of
3725  * this verbose method. Others are not simple macros but are more like
3726  * optimizing code generation.
3727  *   One interesting optimization is when several store macros appear
3728  * consecutivly that would load AT with the upper half of the same address.
3729  * The ensuing load upper instructions are ommited. This implies some kind
3730  * of global optimization. We currently only optimize within a single macro.
3731  *   For many of the load and store macros if the address is specified as a
3732  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3733  * first load register 'at' with zero and use it as the base register. The
3734  * mips assembler simply uses register $zero. Just one tiny optimization
3735  * we're missing.
3736  */
3737 static void
3738 macro (ip)
3739      struct mips_cl_insn *ip;
3740 {
3741   register int treg, sreg, dreg, breg;
3742   int tempreg;
3743   int mask;
3744   int icnt = 0;
3745   int used_at = 0;
3746   expressionS expr1;
3747   const char *s;
3748   const char *s2;
3749   const char *fmt;
3750   int likely = 0;
3751   int dbl = 0;
3752   int coproc = 0;
3753   int lr = 0;
3754   int imm = 0;
3755   offsetT maxnum;
3756   int off;
3757   bfd_reloc_code_real_type r;
3758   int hold_mips_optimize;
3759
3760   assert (! mips_opts.mips16);
3761
3762   treg = (ip->insn_opcode >> 16) & 0x1f;
3763   dreg = (ip->insn_opcode >> 11) & 0x1f;
3764   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3765   mask = ip->insn_mo->mask;
3766
3767   expr1.X_op = O_constant;
3768   expr1.X_op_symbol = NULL;
3769   expr1.X_add_symbol = NULL;
3770   expr1.X_add_number = 1;
3771
3772   switch (mask)
3773     {
3774     case M_DABS:
3775       dbl = 1;
3776     case M_ABS:
3777       /* bgez $a0,.+12
3778          move v0,$a0
3779          sub v0,$zero,$a0
3780          */
3781
3782       mips_emit_delays (true);
3783       ++mips_opts.noreorder;
3784       mips_any_noreorder = 1;
3785
3786       expr1.X_add_number = 8;
3787       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3788       if (dreg == sreg)
3789         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3790                      0);
3791       else
3792         move_register (&icnt, dreg, sreg);
3793       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3794                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3795
3796       --mips_opts.noreorder;
3797       return;
3798
3799     case M_ADD_I:
3800       s = "addi";
3801       s2 = "add";
3802       goto do_addi;
3803     case M_ADDU_I:
3804       s = "addiu";
3805       s2 = "addu";
3806       goto do_addi;
3807     case M_DADD_I:
3808       dbl = 1;
3809       s = "daddi";
3810       s2 = "dadd";
3811       goto do_addi;
3812     case M_DADDU_I:
3813       dbl = 1;
3814       s = "daddiu";
3815       s2 = "daddu";
3816     do_addi:
3817       if (imm_expr.X_op == O_constant
3818           && imm_expr.X_add_number >= -0x8000
3819           && imm_expr.X_add_number < 0x8000)
3820         {
3821           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3822                        (int) BFD_RELOC_LO16);
3823           return;
3824         }
3825       load_register (&icnt, AT, &imm_expr, dbl);
3826       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3827                    treg, sreg, AT);
3828       break;
3829
3830     case M_AND_I:
3831       s = "andi";
3832       s2 = "and";
3833       goto do_bit;
3834     case M_OR_I:
3835       s = "ori";
3836       s2 = "or";
3837       goto do_bit;
3838     case M_NOR_I:
3839       s = "";
3840       s2 = "nor";
3841       goto do_bit;
3842     case M_XOR_I:
3843       s = "xori";
3844       s2 = "xor";
3845     do_bit:
3846       if (imm_expr.X_op == O_constant
3847           && imm_expr.X_add_number >= 0
3848           && imm_expr.X_add_number < 0x10000)
3849         {
3850           if (mask != M_NOR_I)
3851             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3852                          sreg, (int) BFD_RELOC_LO16);
3853           else
3854             {
3855               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3856                            treg, sreg, (int) BFD_RELOC_LO16);
3857               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3858                            "d,v,t", treg, treg, 0);
3859             }
3860           return;
3861         }
3862
3863       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3864       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3865                    treg, sreg, AT);
3866       break;
3867
3868     case M_BEQ_I:
3869       s = "beq";
3870       goto beq_i;
3871     case M_BEQL_I:
3872       s = "beql";
3873       likely = 1;
3874       goto beq_i;
3875     case M_BNE_I:
3876       s = "bne";
3877       goto beq_i;
3878     case M_BNEL_I:
3879       s = "bnel";
3880       likely = 1;
3881     beq_i:
3882       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3883         {
3884           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3885                        0);
3886           return;
3887         }
3888       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3889       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3890       break;
3891
3892     case M_BGEL:
3893       likely = 1;
3894     case M_BGE:
3895       if (treg == 0)
3896         {
3897           macro_build ((char *) NULL, &icnt, &offset_expr,
3898                        likely ? "bgezl" : "bgez", "s,p", sreg);
3899           return;
3900         }
3901       if (sreg == 0)
3902         {
3903           macro_build ((char *) NULL, &icnt, &offset_expr,
3904                        likely ? "blezl" : "blez", "s,p", treg);
3905           return;
3906         }
3907       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3908                    AT, sreg, treg);
3909       macro_build ((char *) NULL, &icnt, &offset_expr,
3910                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3911       break;
3912
3913     case M_BGTL_I:
3914       likely = 1;
3915     case M_BGT_I:
3916       /* check for > max integer */
3917       maxnum = 0x7fffffff;
3918       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3919         {
3920           maxnum <<= 16;
3921           maxnum |= 0xffff;
3922           maxnum <<= 16;
3923           maxnum |= 0xffff;
3924         }
3925       if (imm_expr.X_op == O_constant
3926           && imm_expr.X_add_number >= maxnum
3927           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3928         {
3929         do_false:
3930           /* result is always false */
3931           if (! likely)
3932             {
3933               if (warn_nops)
3934                 as_warn (_("Branch %s is always false (nop)"),
3935                          ip->insn_mo->name);
3936               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3937                            "", 0);
3938             }
3939           else
3940             {
3941               if (warn_nops)
3942                 as_warn (_("Branch likely %s is always false"),
3943                          ip->insn_mo->name);
3944               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3945                            "s,t,p", 0, 0);
3946             }
3947           return;
3948         }
3949       if (imm_expr.X_op != O_constant)
3950         as_bad (_("Unsupported large constant"));
3951       ++imm_expr.X_add_number;
3952       /* FALLTHROUGH */
3953     case M_BGE_I:
3954     case M_BGEL_I:
3955       if (mask == M_BGEL_I)
3956         likely = 1;
3957       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3958         {
3959           macro_build ((char *) NULL, &icnt, &offset_expr,
3960                        likely ? "bgezl" : "bgez", "s,p", sreg);
3961           return;
3962         }
3963       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3964         {
3965           macro_build ((char *) NULL, &icnt, &offset_expr,
3966                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
3967           return;
3968         }
3969       maxnum = 0x7fffffff;
3970       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3971         {
3972           maxnum <<= 16;
3973           maxnum |= 0xffff;
3974           maxnum <<= 16;
3975           maxnum |= 0xffff;
3976         }
3977       maxnum = - maxnum - 1;
3978       if (imm_expr.X_op == O_constant
3979           && imm_expr.X_add_number <= maxnum
3980           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3981         {
3982         do_true:
3983           /* result is always true */
3984           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3985           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3986           return;
3987         }
3988       set_at (&icnt, sreg, 0);
3989       macro_build ((char *) NULL, &icnt, &offset_expr,
3990                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3991       break;
3992
3993     case M_BGEUL:
3994       likely = 1;
3995     case M_BGEU:
3996       if (treg == 0)
3997         goto do_true;
3998       if (sreg == 0)
3999         {
4000           macro_build ((char *) NULL, &icnt, &offset_expr,
4001                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4002           return;
4003         }
4004       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4005                    "d,v,t", AT, sreg, treg);
4006       macro_build ((char *) NULL, &icnt, &offset_expr,
4007                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4008       break;
4009
4010     case M_BGTUL_I:
4011       likely = 1;
4012     case M_BGTU_I:
4013       if (sreg == 0
4014           || (HAVE_32BIT_GPRS
4015               && imm_expr.X_op == O_constant
4016               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4017         goto do_false;
4018       if (imm_expr.X_op != O_constant)
4019         as_bad (_("Unsupported large constant"));
4020       ++imm_expr.X_add_number;
4021       /* FALLTHROUGH */
4022     case M_BGEU_I:
4023     case M_BGEUL_I:
4024       if (mask == M_BGEUL_I)
4025         likely = 1;
4026       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4027         goto do_true;
4028       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4029         {
4030           macro_build ((char *) NULL, &icnt, &offset_expr,
4031                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4032           return;
4033         }
4034       set_at (&icnt, sreg, 1);
4035       macro_build ((char *) NULL, &icnt, &offset_expr,
4036                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4037       break;
4038
4039     case M_BGTL:
4040       likely = 1;
4041     case M_BGT:
4042       if (treg == 0)
4043         {
4044           macro_build ((char *) NULL, &icnt, &offset_expr,
4045                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4046           return;
4047         }
4048       if (sreg == 0)
4049         {
4050           macro_build ((char *) NULL, &icnt, &offset_expr,
4051                        likely ? "bltzl" : "bltz", "s,p", treg);
4052           return;
4053         }
4054       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4055                    AT, treg, sreg);
4056       macro_build ((char *) NULL, &icnt, &offset_expr,
4057                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4058       break;
4059
4060     case M_BGTUL:
4061       likely = 1;
4062     case M_BGTU:
4063       if (treg == 0)
4064         {
4065           macro_build ((char *) NULL, &icnt, &offset_expr,
4066                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4067           return;
4068         }
4069       if (sreg == 0)
4070         goto do_false;
4071       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4072                    "d,v,t", AT, treg, sreg);
4073       macro_build ((char *) NULL, &icnt, &offset_expr,
4074                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4075       break;
4076
4077     case M_BLEL:
4078       likely = 1;
4079     case M_BLE:
4080       if (treg == 0)
4081         {
4082           macro_build ((char *) NULL, &icnt, &offset_expr,
4083                        likely ? "blezl" : "blez", "s,p", sreg);
4084           return;
4085         }
4086       if (sreg == 0)
4087         {
4088           macro_build ((char *) NULL, &icnt, &offset_expr,
4089                        likely ? "bgezl" : "bgez", "s,p", treg);
4090           return;
4091         }
4092       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4093                    AT, treg, sreg);
4094       macro_build ((char *) NULL, &icnt, &offset_expr,
4095                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4096       break;
4097
4098     case M_BLEL_I:
4099       likely = 1;
4100     case M_BLE_I:
4101       maxnum = 0x7fffffff;
4102       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4103         {
4104           maxnum <<= 16;
4105           maxnum |= 0xffff;
4106           maxnum <<= 16;
4107           maxnum |= 0xffff;
4108         }
4109       if (imm_expr.X_op == O_constant
4110           && imm_expr.X_add_number >= maxnum
4111           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4112         goto do_true;
4113       if (imm_expr.X_op != O_constant)
4114         as_bad (_("Unsupported large constant"));
4115       ++imm_expr.X_add_number;
4116       /* FALLTHROUGH */
4117     case M_BLT_I:
4118     case M_BLTL_I:
4119       if (mask == M_BLTL_I)
4120         likely = 1;
4121       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4122         {
4123           macro_build ((char *) NULL, &icnt, &offset_expr,
4124                        likely ? "bltzl" : "bltz", "s,p", sreg);
4125           return;
4126         }
4127       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4128         {
4129           macro_build ((char *) NULL, &icnt, &offset_expr,
4130                        likely ? "blezl" : "blez", "s,p", sreg);
4131           return;
4132         }
4133       set_at (&icnt, sreg, 0);
4134       macro_build ((char *) NULL, &icnt, &offset_expr,
4135                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4136       break;
4137
4138     case M_BLEUL:
4139       likely = 1;
4140     case M_BLEU:
4141       if (treg == 0)
4142         {
4143           macro_build ((char *) NULL, &icnt, &offset_expr,
4144                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4145           return;
4146         }
4147       if (sreg == 0)
4148         goto do_true;
4149       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4150                    "d,v,t", AT, treg, sreg);
4151       macro_build ((char *) NULL, &icnt, &offset_expr,
4152                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4153       break;
4154
4155     case M_BLEUL_I:
4156       likely = 1;
4157     case M_BLEU_I:
4158       if (sreg == 0
4159           || (HAVE_32BIT_GPRS
4160               && imm_expr.X_op == O_constant
4161               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4162         goto do_true;
4163       if (imm_expr.X_op != O_constant)
4164         as_bad (_("Unsupported large constant"));
4165       ++imm_expr.X_add_number;
4166       /* FALLTHROUGH */
4167     case M_BLTU_I:
4168     case M_BLTUL_I:
4169       if (mask == M_BLTUL_I)
4170         likely = 1;
4171       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4172         goto do_false;
4173       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4174         {
4175           macro_build ((char *) NULL, &icnt, &offset_expr,
4176                        likely ? "beql" : "beq",
4177                        "s,t,p", sreg, 0);
4178           return;
4179         }
4180       set_at (&icnt, sreg, 1);
4181       macro_build ((char *) NULL, &icnt, &offset_expr,
4182                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4183       break;
4184
4185     case M_BLTL:
4186       likely = 1;
4187     case M_BLT:
4188       if (treg == 0)
4189         {
4190           macro_build ((char *) NULL, &icnt, &offset_expr,
4191                        likely ? "bltzl" : "bltz", "s,p", sreg);
4192           return;
4193         }
4194       if (sreg == 0)
4195         {
4196           macro_build ((char *) NULL, &icnt, &offset_expr,
4197                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4198           return;
4199         }
4200       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4201                    AT, sreg, treg);
4202       macro_build ((char *) NULL, &icnt, &offset_expr,
4203                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4204       break;
4205
4206     case M_BLTUL:
4207       likely = 1;
4208     case M_BLTU:
4209       if (treg == 0)
4210         goto do_false;
4211       if (sreg == 0)
4212         {
4213           macro_build ((char *) NULL, &icnt, &offset_expr,
4214                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4215           return;
4216         }
4217       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4218                    "d,v,t", AT, sreg,
4219                    treg);
4220       macro_build ((char *) NULL, &icnt, &offset_expr,
4221                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4222       break;
4223
4224     case M_DDIV_3:
4225       dbl = 1;
4226     case M_DIV_3:
4227       s = "mflo";
4228       goto do_div3;
4229     case M_DREM_3:
4230       dbl = 1;
4231     case M_REM_3:
4232       s = "mfhi";
4233     do_div3:
4234       if (treg == 0)
4235         {
4236           as_warn (_("Divide by zero."));
4237           if (mips_trap)
4238             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4239                          "s,t", 0, 0);
4240           else
4241             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4242                          "c", 7);
4243           return;
4244         }
4245
4246       mips_emit_delays (true);
4247       ++mips_opts.noreorder;
4248       mips_any_noreorder = 1;
4249       if (mips_trap)
4250         {
4251           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4252                        "s,t", treg, 0);
4253           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4254                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4255         }
4256       else
4257         {
4258           expr1.X_add_number = 8;
4259           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4260           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4261                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4262           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4263                        "c", 7);
4264         }
4265       expr1.X_add_number = -1;
4266       macro_build ((char *) NULL, &icnt, &expr1,
4267                    dbl ? "daddiu" : "addiu",
4268                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4269       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4270       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4271       if (dbl)
4272         {
4273           expr1.X_add_number = 1;
4274           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4275                        (int) BFD_RELOC_LO16);
4276           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4277                        "d,w,<", AT, AT, 31);
4278         }
4279       else
4280         {
4281           expr1.X_add_number = 0x80000000;
4282           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4283                        (int) BFD_RELOC_HI16);
4284         }
4285       if (mips_trap)
4286         {
4287           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4288                        "s,t", sreg, AT);
4289           /* We want to close the noreorder block as soon as possible, so
4290              that later insns are available for delay slot filling.  */
4291           --mips_opts.noreorder;
4292         }
4293       else
4294         {
4295           expr1.X_add_number = 8;
4296           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4297           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4298                        0);
4299
4300           /* We want to close the noreorder block as soon as possible, so
4301              that later insns are available for delay slot filling.  */
4302           --mips_opts.noreorder;
4303
4304           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4305                        "c", 6);
4306         }
4307       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4308       break;
4309
4310     case M_DIV_3I:
4311       s = "div";
4312       s2 = "mflo";
4313       goto do_divi;
4314     case M_DIVU_3I:
4315       s = "divu";
4316       s2 = "mflo";
4317       goto do_divi;
4318     case M_REM_3I:
4319       s = "div";
4320       s2 = "mfhi";
4321       goto do_divi;
4322     case M_REMU_3I:
4323       s = "divu";
4324       s2 = "mfhi";
4325       goto do_divi;
4326     case M_DDIV_3I:
4327       dbl = 1;
4328       s = "ddiv";
4329       s2 = "mflo";
4330       goto do_divi;
4331     case M_DDIVU_3I:
4332       dbl = 1;
4333       s = "ddivu";
4334       s2 = "mflo";
4335       goto do_divi;
4336     case M_DREM_3I:
4337       dbl = 1;
4338       s = "ddiv";
4339       s2 = "mfhi";
4340       goto do_divi;
4341     case M_DREMU_3I:
4342       dbl = 1;
4343       s = "ddivu";
4344       s2 = "mfhi";
4345     do_divi:
4346       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4347         {
4348           as_warn (_("Divide by zero."));
4349           if (mips_trap)
4350             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4351                          "s,t", 0, 0);
4352           else
4353             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4354                          "c", 7);
4355           return;
4356         }
4357       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4358         {
4359           if (strcmp (s2, "mflo") == 0)
4360             move_register (&icnt, dreg, sreg);
4361           else
4362             move_register (&icnt, dreg, 0);
4363           return;
4364         }
4365       if (imm_expr.X_op == O_constant
4366           && imm_expr.X_add_number == -1
4367           && s[strlen (s) - 1] != 'u')
4368         {
4369           if (strcmp (s2, "mflo") == 0)
4370             {
4371               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4372                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4373             }
4374           else
4375             move_register (&icnt, dreg, 0);
4376           return;
4377         }
4378
4379       load_register (&icnt, AT, &imm_expr, dbl);
4380       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4381                    sreg, AT);
4382       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4383       break;
4384
4385     case M_DIVU_3:
4386       s = "divu";
4387       s2 = "mflo";
4388       goto do_divu3;
4389     case M_REMU_3:
4390       s = "divu";
4391       s2 = "mfhi";
4392       goto do_divu3;
4393     case M_DDIVU_3:
4394       s = "ddivu";
4395       s2 = "mflo";
4396       goto do_divu3;
4397     case M_DREMU_3:
4398       s = "ddivu";
4399       s2 = "mfhi";
4400     do_divu3:
4401       mips_emit_delays (true);
4402       ++mips_opts.noreorder;
4403       mips_any_noreorder = 1;
4404       if (mips_trap)
4405         {
4406           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4407                        "s,t", treg, 0);
4408           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4409                        sreg, treg);
4410           /* We want to close the noreorder block as soon as possible, so
4411              that later insns are available for delay slot filling.  */
4412           --mips_opts.noreorder;
4413         }
4414       else
4415         {
4416           expr1.X_add_number = 8;
4417           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4418           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4419                        sreg, treg);
4420
4421           /* We want to close the noreorder block as soon as possible, so
4422              that later insns are available for delay slot filling.  */
4423           --mips_opts.noreorder;
4424           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4425                        "c", 7);
4426         }
4427       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4428       return;
4429
4430     case M_DLA_AB:
4431       dbl = 1;
4432     case M_LA_AB:
4433       /* Load the address of a symbol into a register.  If breg is not
4434          zero, we then add a base register to it.  */
4435
4436       if (dbl && HAVE_32BIT_GPRS)
4437         as_warn (_("dla used to load 32-bit register"));
4438
4439       if (! dbl && HAVE_64BIT_OBJECTS)
4440         as_warn (_("la used to load 64-bit address"));
4441
4442       if (treg == breg)
4443         {
4444           tempreg = AT;
4445           used_at = 1;
4446         }
4447       else
4448         {
4449           tempreg = treg;
4450           used_at = 0;
4451         }
4452
4453       /* When generating embedded PIC code, we permit expressions of
4454          the form
4455            la   $treg,foo-bar
4456            la   $treg,foo-bar($breg)
4457          where bar is an address in the current section.  These are used
4458          when getting the addresses of functions.  We don't permit
4459          X_add_number to be non-zero, because if the symbol is
4460          external the relaxing code needs to know that any addend is
4461          purely the offset to X_op_symbol.  */
4462       if (mips_pic == EMBEDDED_PIC
4463           && offset_expr.X_op == O_subtract
4464           && (symbol_constant_p (offset_expr.X_op_symbol)
4465               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4466               : (symbol_equated_p (offset_expr.X_op_symbol)
4467                  && (S_GET_SEGMENT
4468                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4469                       ->X_add_symbol)
4470                      == now_seg)))
4471           && (offset_expr.X_add_number == 0
4472               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4473         {
4474           if (breg == 0)
4475             {
4476               tempreg = treg;
4477               used_at = 0;
4478               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4479                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4480             }
4481           else
4482             {
4483               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4484                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4485               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4486                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4487                            "d,v,t", tempreg, tempreg, breg);
4488             }
4489           macro_build ((char *) NULL, &icnt, &offset_expr,
4490                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4491                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4492           if (! used_at)
4493             return;
4494           break;
4495         }
4496
4497       if (offset_expr.X_op != O_symbol
4498           && offset_expr.X_op != O_constant)
4499         {
4500           as_bad (_("expression too complex"));
4501           offset_expr.X_op = O_constant;
4502         }
4503
4504       if (offset_expr.X_op == O_constant)
4505         load_register (&icnt, tempreg, &offset_expr,
4506                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4507                         ? (dbl || HAVE_64BIT_ADDRESSES)
4508                         : HAVE_64BIT_ADDRESSES));
4509       else if (mips_pic == NO_PIC)
4510         {
4511           /* If this is a reference to a GP relative symbol, we want
4512                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4513              Otherwise we want
4514                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4515                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4516              If we have a constant, we need two instructions anyhow,
4517              so we may as well always use the latter form.
4518
4519             With 64bit address space and a usable $at we want
4520               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4521               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4522               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4523               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4524               dsll32    $tempreg,0
4525               dadd      $tempreg,$tempreg,$at
4526
4527             If $at is already in use, we use an path which is suboptimal
4528             on superscalar processors.
4529               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4530               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4531               dsll      $tempreg,16
4532               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4533               dsll      $tempreg,16
4534               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4535           */
4536           char *p = NULL;
4537           if (HAVE_64BIT_ADDRESSES)
4538             {
4539               /* We don't do GP optimization for now because RELAX_ENCODE can't
4540                  hold the data for such large chunks.  */
4541
4542               if (used_at == 0)
4543                 {
4544                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4545                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4546                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4547                                AT, (int) BFD_RELOC_HI16_S);
4548                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4549                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4550                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4551                                AT, AT, (int) BFD_RELOC_LO16);
4552                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4553                                "d,w,<", tempreg, tempreg, 0);
4554                   macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4555                                tempreg, tempreg, AT);
4556                   used_at = 1;
4557                 }
4558               else
4559                 {
4560                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4561                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4562                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4563                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4564                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4565                                tempreg, tempreg, 16);
4566                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4567                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4568                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4569                                tempreg, tempreg, 16);
4570                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4571                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4572                 }
4573             }
4574           else
4575             {
4576               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4577                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4578                 {
4579                   frag_grow (20);
4580                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4581                                "t,r,j", tempreg, mips_gp_register,
4582                                (int) BFD_RELOC_GPREL16);
4583                   p = frag_var (rs_machine_dependent, 8, 0,
4584                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4585                                               mips_opts.warn_about_macros),
4586                                 offset_expr.X_add_symbol, 0, NULL);
4587                 }
4588               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4589               if (p != NULL)
4590                 p += 4;
4591               macro_build (p, &icnt, &offset_expr, "addiu",
4592                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4593             }
4594         }
4595       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4596         {
4597           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4598
4599           /* If this is a reference to an external symbol, and there
4600              is no constant, we want
4601                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4602              or if tempreg is PIC_CALL_REG
4603                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4604              For a local symbol, we want
4605                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4606                nop
4607                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4608
4609              If we have a small constant, and this is a reference to
4610              an external symbol, we want
4611                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4612                nop
4613                addiu    $tempreg,$tempreg,<constant>
4614              For a local symbol, we want the same instruction
4615              sequence, but we output a BFD_RELOC_LO16 reloc on the
4616              addiu instruction.
4617
4618              If we have a large constant, and this is a reference to
4619              an external symbol, we want
4620                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4621                lui      $at,<hiconstant>
4622                addiu    $at,$at,<loconstant>
4623                addu     $tempreg,$tempreg,$at
4624              For a local symbol, we want the same instruction
4625              sequence, but we output a BFD_RELOC_LO16 reloc on the
4626              addiu instruction.  */
4627           expr1.X_add_number = offset_expr.X_add_number;
4628           offset_expr.X_add_number = 0;
4629           frag_grow (32);
4630           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4631             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4632           macro_build ((char *) NULL, &icnt, &offset_expr,
4633                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4634                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4635           if (expr1.X_add_number == 0)
4636             {
4637               int off;
4638               char *p;
4639
4640               if (breg == 0)
4641                 off = 0;
4642               else
4643                 {
4644                   /* We're going to put in an addu instruction using
4645                      tempreg, so we may as well insert the nop right
4646                      now.  */
4647                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4648                                "nop", "");
4649                   off = 4;
4650                 }
4651               p = frag_var (rs_machine_dependent, 8 - off, 0,
4652                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4653                                           (breg == 0
4654                                            ? mips_opts.warn_about_macros
4655                                            : 0)),
4656                             offset_expr.X_add_symbol, 0, NULL);
4657               if (breg == 0)
4658                 {
4659                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4660                   p += 4;
4661                 }
4662               macro_build (p, &icnt, &expr1,
4663                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4664                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4665               /* FIXME: If breg == 0, and the next instruction uses
4666                  $tempreg, then if this variant case is used an extra
4667                  nop will be generated.  */
4668             }
4669           else if (expr1.X_add_number >= -0x8000
4670                    && expr1.X_add_number < 0x8000)
4671             {
4672               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4673                            "nop", "");
4674               macro_build ((char *) NULL, &icnt, &expr1,
4675                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4676                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4677               frag_var (rs_machine_dependent, 0, 0,
4678                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4679                         offset_expr.X_add_symbol, 0, NULL);
4680             }
4681           else
4682             {
4683               int off1;
4684
4685               /* If we are going to add in a base register, and the
4686                  target register and the base register are the same,
4687                  then we are using AT as a temporary register.  Since
4688                  we want to load the constant into AT, we add our
4689                  current AT (from the global offset table) and the
4690                  register into the register now, and pretend we were
4691                  not using a base register.  */
4692               if (breg != treg)
4693                 off1 = 0;
4694               else
4695                 {
4696                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4697                                "nop", "");
4698                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4699                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4700                                "d,v,t", treg, AT, breg);
4701                   breg = 0;
4702                   tempreg = treg;
4703                   off1 = -8;
4704                 }
4705
4706               /* Set mips_optimize around the lui instruction to avoid
4707                  inserting an unnecessary nop after the lw.  */
4708               hold_mips_optimize = mips_optimize;
4709               mips_optimize = 2;
4710               macro_build_lui (NULL, &icnt, &expr1, AT);
4711               mips_optimize = hold_mips_optimize;
4712
4713               macro_build ((char *) NULL, &icnt, &expr1,
4714                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4715                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4716               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4717                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4718                            "d,v,t", tempreg, tempreg, AT);
4719               frag_var (rs_machine_dependent, 0, 0,
4720                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4721                         offset_expr.X_add_symbol, 0, NULL);
4722               used_at = 1;
4723             }
4724         }
4725       else if (mips_pic == SVR4_PIC)
4726         {
4727           int gpdel;
4728           char *p;
4729           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4730           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4731
4732           /* This is the large GOT case.  If this is a reference to an
4733              external symbol, and there is no constant, we want
4734                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4735                addu     $tempreg,$tempreg,$gp
4736                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4737              or if tempreg is PIC_CALL_REG
4738                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4739                addu     $tempreg,$tempreg,$gp
4740                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4741              For a local symbol, we want
4742                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4743                nop
4744                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4745
4746              If we have a small constant, and this is a reference to
4747              an external symbol, we want
4748                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4749                addu     $tempreg,$tempreg,$gp
4750                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4751                nop
4752                addiu    $tempreg,$tempreg,<constant>
4753              For a local symbol, we want
4754                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4755                nop
4756                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4757
4758              If we have a large constant, and this is a reference to
4759              an external symbol, we want
4760                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4761                addu     $tempreg,$tempreg,$gp
4762                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4763                lui      $at,<hiconstant>
4764                addiu    $at,$at,<loconstant>
4765                addu     $tempreg,$tempreg,$at
4766              For a local symbol, we want
4767                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4768                lui      $at,<hiconstant>
4769                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4770                addu     $tempreg,$tempreg,$at
4771
4772              For NewABI, we want for data addresses
4773                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4774              If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4775                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4776            */
4777           if (HAVE_NEWABI)
4778             {
4779               int reloc_type = (tempreg == PIC_CALL_REG
4780                                 ? BFD_RELOC_MIPS_CALL16
4781                                 : BFD_RELOC_MIPS_GOT_DISP);
4782
4783               macro_build ((char *) NULL, &icnt, &offset_expr,
4784                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4785                            "t,o(b)", tempreg, reloc_type, mips_gp_register);
4786
4787               if (breg != 0)
4788                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4789                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4790                              "d,v,t", treg, tempreg, breg);
4791
4792               if (! used_at)
4793                 return;
4794
4795               break;
4796             }
4797           expr1.X_add_number = offset_expr.X_add_number;
4798           offset_expr.X_add_number = 0;
4799           frag_grow (52);
4800           if (reg_needs_delay (mips_gp_register))
4801             gpdel = 4;
4802           else
4803             gpdel = 0;
4804           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4805             {
4806               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4807               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4808             }
4809           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4810                        tempreg, lui_reloc_type);
4811           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4812                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4813                        "d,v,t", tempreg, tempreg, mips_gp_register);
4814           macro_build ((char *) NULL, &icnt, &offset_expr,
4815                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4816                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4817           if (expr1.X_add_number == 0)
4818             {
4819               int off;
4820
4821               if (breg == 0)
4822                 off = 0;
4823               else
4824                 {
4825                   /* We're going to put in an addu instruction using
4826                      tempreg, so we may as well insert the nop right
4827                      now.  */
4828                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4829                                "nop", "");
4830                   off = 4;
4831                 }
4832
4833               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4834                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4835                                           8 + gpdel, 0,
4836                                           (breg == 0
4837                                            ? mips_opts.warn_about_macros
4838                                            : 0)),
4839                             offset_expr.X_add_symbol, 0, NULL);
4840             }
4841           else if (expr1.X_add_number >= -0x8000
4842                    && expr1.X_add_number < 0x8000)
4843             {
4844               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4845                            "nop", "");
4846               macro_build ((char *) NULL, &icnt, &expr1,
4847                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4848                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4849
4850               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4851                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4852                                           (breg == 0
4853                                            ? mips_opts.warn_about_macros
4854                                            : 0)),
4855                             offset_expr.X_add_symbol, 0, NULL);
4856             }
4857           else
4858             {
4859               int adj, dreg;
4860
4861               /* If we are going to add in a base register, and the
4862                  target register and the base register are the same,
4863                  then we are using AT as a temporary register.  Since
4864                  we want to load the constant into AT, we add our
4865                  current AT (from the global offset table) and the
4866                  register into the register now, and pretend we were
4867                  not using a base register.  */
4868               if (breg != treg)
4869                 {
4870                   adj = 0;
4871                   dreg = tempreg;
4872                 }
4873               else
4874                 {
4875                   assert (tempreg == AT);
4876                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4877                                "nop", "");
4878                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4879                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4880                                "d,v,t", treg, AT, breg);
4881                   dreg = treg;
4882                   adj = 8;
4883                 }
4884
4885               /* Set mips_optimize around the lui instruction to avoid
4886                  inserting an unnecessary nop after the lw.  */
4887               hold_mips_optimize = mips_optimize;
4888               mips_optimize = 2;
4889               macro_build_lui (NULL, &icnt, &expr1, AT);
4890               mips_optimize = hold_mips_optimize;
4891
4892               macro_build ((char *) NULL, &icnt, &expr1,
4893                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4894                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4895               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4896                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4897                            "d,v,t", dreg, dreg, AT);
4898
4899               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4900                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4901                                           8 + gpdel, 0,
4902                                           (breg == 0
4903                                            ? mips_opts.warn_about_macros
4904                                            : 0)),
4905                             offset_expr.X_add_symbol, 0, NULL);
4906
4907               used_at = 1;
4908             }
4909
4910           if (gpdel > 0)
4911             {
4912               /* This is needed because this instruction uses $gp, but
4913                  the first instruction on the main stream does not.  */
4914               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4915               p += 4;
4916             }
4917           macro_build (p, &icnt, &offset_expr,
4918                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4919                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4920                        mips_gp_register);
4921           p += 4;
4922           if (expr1.X_add_number >= -0x8000
4923               && expr1.X_add_number < 0x8000)
4924             {
4925               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4926               p += 4;
4927               macro_build (p, &icnt, &expr1,
4928                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4929                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4930               /* FIXME: If add_number is 0, and there was no base
4931                  register, the external symbol case ended with a load,
4932                  so if the symbol turns out to not be external, and
4933                  the next instruction uses tempreg, an unnecessary nop
4934                  will be inserted.  */
4935             }
4936           else
4937             {
4938               if (breg == treg)
4939                 {
4940                   /* We must add in the base register now, as in the
4941                      external symbol case.  */
4942                   assert (tempreg == AT);
4943                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4944                   p += 4;
4945                   macro_build (p, &icnt, (expressionS *) NULL,
4946                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4947                                "d,v,t", treg, AT, breg);
4948                   p += 4;
4949                   tempreg = treg;
4950                   /* We set breg to 0 because we have arranged to add
4951                      it in in both cases.  */
4952                   breg = 0;
4953                 }
4954
4955               macro_build_lui (p, &icnt, &expr1, AT);
4956               p += 4;
4957               macro_build (p, &icnt, &expr1,
4958                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4959                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4960               p += 4;
4961               macro_build (p, &icnt, (expressionS *) NULL,
4962                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4963                            "d,v,t", tempreg, tempreg, AT);
4964               p += 4;
4965             }
4966         }
4967       else if (mips_pic == EMBEDDED_PIC)
4968         {
4969           /* We use
4970                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4971              */
4972           macro_build ((char *) NULL, &icnt, &offset_expr,
4973                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4974                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4975         }
4976       else
4977         abort ();
4978
4979       if (breg != 0)
4980         {
4981           char *s;
4982
4983           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4984             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4985           else
4986             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
4987
4988           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4989                        "d,v,t", treg, tempreg, breg);
4990         }
4991
4992       if (! used_at)
4993         return;
4994
4995       break;
4996
4997     case M_J_A:
4998       /* The j instruction may not be used in PIC code, since it
4999          requires an absolute address.  We convert it to a b
5000          instruction.  */
5001       if (mips_pic == NO_PIC)
5002         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5003       else
5004         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5005       return;
5006
5007       /* The jal instructions must be handled as macros because when
5008          generating PIC code they expand to multi-instruction
5009          sequences.  Normally they are simple instructions.  */
5010     case M_JAL_1:
5011       dreg = RA;
5012       /* Fall through.  */
5013     case M_JAL_2:
5014       if (mips_pic == NO_PIC
5015           || mips_pic == EMBEDDED_PIC)
5016         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5017                      "d,s", dreg, sreg);
5018       else if (mips_pic == SVR4_PIC)
5019         {
5020           if (sreg != PIC_CALL_REG)
5021             as_warn (_("MIPS PIC call to register other than $25"));
5022
5023           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5024                        "d,s", dreg, sreg);
5025           if (! HAVE_NEWABI)
5026             {
5027               if (mips_cprestore_offset < 0)
5028                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5029               else
5030                 {
5031                   if (! mips_frame_reg_valid)
5032                     {
5033                       as_warn (_("No .frame pseudo-op used in PIC code"));
5034                       /* Quiet this warning.  */
5035                       mips_frame_reg_valid = 1;
5036                     }
5037                   if (! mips_cprestore_valid)
5038                     {
5039                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5040                       /* Quiet this warning.  */
5041                       mips_cprestore_valid = 1;
5042                     }
5043                   expr1.X_add_number = mips_cprestore_offset;
5044                   macro_build ((char *) NULL, &icnt, &expr1,
5045                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5046                                mips_gp_register, (int) BFD_RELOC_LO16,
5047                                mips_frame_reg);
5048                 }
5049             }
5050         }
5051       else
5052         abort ();
5053
5054       return;
5055
5056     case M_JAL_A:
5057       if (mips_pic == NO_PIC)
5058         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5059       else if (mips_pic == SVR4_PIC)
5060         {
5061           char *p;
5062
5063           /* If this is a reference to an external symbol, and we are
5064              using a small GOT, we want
5065                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5066                nop
5067                jalr     $ra,$25
5068                nop
5069                lw       $gp,cprestore($sp)
5070              The cprestore value is set using the .cprestore
5071              pseudo-op.  If we are using a big GOT, we want
5072                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5073                addu     $25,$25,$gp
5074                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5075                nop
5076                jalr     $ra,$25
5077                nop
5078                lw       $gp,cprestore($sp)
5079              If the symbol is not external, we want
5080                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5081                nop
5082                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5083                jalr     $ra,$25
5084                nop
5085                lw $gp,cprestore($sp)
5086              For NewABI, we want
5087                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5088                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5089            */
5090           if (HAVE_NEWABI)
5091             {
5092               macro_build ((char *) NULL, &icnt, &offset_expr,
5093                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5094                            "t,o(b)", PIC_CALL_REG,
5095                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5096               macro_build_jalr (icnt, &offset_expr);
5097             }
5098           else
5099             {
5100               frag_grow (40);
5101               if (! mips_big_got)
5102                 {
5103                   macro_build ((char *) NULL, &icnt, &offset_expr,
5104                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5105                                "t,o(b)", PIC_CALL_REG,
5106                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5107                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5108                                "nop", "");
5109                   p = frag_var (rs_machine_dependent, 4, 0,
5110                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5111                                 offset_expr.X_add_symbol, 0, NULL);
5112                 }
5113               else
5114                 {
5115                   int gpdel;
5116
5117                   if (reg_needs_delay (mips_gp_register))
5118                     gpdel = 4;
5119                   else
5120                     gpdel = 0;
5121                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5122                                "t,u", PIC_CALL_REG,
5123                                (int) BFD_RELOC_MIPS_CALL_HI16);
5124                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5125                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5126                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5127                                mips_gp_register);
5128                   macro_build ((char *) NULL, &icnt, &offset_expr,
5129                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5130                                "t,o(b)", PIC_CALL_REG,
5131                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5132                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5133                                "nop", "");
5134                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5135                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5136                                               8 + gpdel, 0, 0),
5137                                 offset_expr.X_add_symbol, 0, NULL);
5138                   if (gpdel > 0)
5139                     {
5140                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5141                       p += 4;
5142                     }
5143                   macro_build (p, &icnt, &offset_expr,
5144                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5145                                "t,o(b)", PIC_CALL_REG,
5146                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5147                   p += 4;
5148                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5149                   p += 4;
5150                 }
5151               macro_build (p, &icnt, &offset_expr,
5152                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5153                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5154                            (int) BFD_RELOC_LO16);
5155               macro_build_jalr (icnt, &offset_expr);
5156
5157               if (mips_cprestore_offset < 0)
5158                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5159               else
5160                 {
5161                   if (! mips_frame_reg_valid)
5162                     {
5163                       as_warn (_("No .frame pseudo-op used in PIC code"));
5164                       /* Quiet this warning.  */
5165                       mips_frame_reg_valid = 1;
5166                     }
5167                   if (! mips_cprestore_valid)
5168                     {
5169                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5170                       /* Quiet this warning.  */
5171                       mips_cprestore_valid = 1;
5172                     }
5173                   if (mips_opts.noreorder)
5174                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5175                                  "nop", "");
5176                   expr1.X_add_number = mips_cprestore_offset;
5177                   macro_build ((char *) NULL, &icnt, &expr1,
5178                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5179                                mips_gp_register, (int) BFD_RELOC_LO16,
5180                                mips_frame_reg);
5181                 }
5182             }
5183         }
5184       else if (mips_pic == EMBEDDED_PIC)
5185         {
5186           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5187           /* The linker may expand the call to a longer sequence which
5188              uses $at, so we must break rather than return.  */
5189           break;
5190         }
5191       else
5192         abort ();
5193
5194       return;
5195
5196     case M_LB_AB:
5197       s = "lb";
5198       goto ld;
5199     case M_LBU_AB:
5200       s = "lbu";
5201       goto ld;
5202     case M_LH_AB:
5203       s = "lh";
5204       goto ld;
5205     case M_LHU_AB:
5206       s = "lhu";
5207       goto ld;
5208     case M_LW_AB:
5209       s = "lw";
5210       goto ld;
5211     case M_LWC0_AB:
5212       s = "lwc0";
5213       /* Itbl support may require additional care here.  */
5214       coproc = 1;
5215       goto ld;
5216     case M_LWC1_AB:
5217       s = "lwc1";
5218       /* Itbl support may require additional care here.  */
5219       coproc = 1;
5220       goto ld;
5221     case M_LWC2_AB:
5222       s = "lwc2";
5223       /* Itbl support may require additional care here.  */
5224       coproc = 1;
5225       goto ld;
5226     case M_LWC3_AB:
5227       s = "lwc3";
5228       /* Itbl support may require additional care here.  */
5229       coproc = 1;
5230       goto ld;
5231     case M_LWL_AB:
5232       s = "lwl";
5233       lr = 1;
5234       goto ld;
5235     case M_LWR_AB:
5236       s = "lwr";
5237       lr = 1;
5238       goto ld;
5239     case M_LDC1_AB:
5240       if (mips_arch == CPU_R4650)
5241         {
5242           as_bad (_("opcode not supported on this processor"));
5243           return;
5244         }
5245       s = "ldc1";
5246       /* Itbl support may require additional care here.  */
5247       coproc = 1;
5248       goto ld;
5249     case M_LDC2_AB:
5250       s = "ldc2";
5251       /* Itbl support may require additional care here.  */
5252       coproc = 1;
5253       goto ld;
5254     case M_LDC3_AB:
5255       s = "ldc3";
5256       /* Itbl support may require additional care here.  */
5257       coproc = 1;
5258       goto ld;
5259     case M_LDL_AB:
5260       s = "ldl";
5261       lr = 1;
5262       goto ld;
5263     case M_LDR_AB:
5264       s = "ldr";
5265       lr = 1;
5266       goto ld;
5267     case M_LL_AB:
5268       s = "ll";
5269       goto ld;
5270     case M_LLD_AB:
5271       s = "lld";
5272       goto ld;
5273     case M_LWU_AB:
5274       s = "lwu";
5275     ld:
5276       if (breg == treg || coproc || lr)
5277         {
5278           tempreg = AT;
5279           used_at = 1;
5280         }
5281       else
5282         {
5283           tempreg = treg;
5284           used_at = 0;
5285         }
5286       goto ld_st;
5287     case M_SB_AB:
5288       s = "sb";
5289       goto st;
5290     case M_SH_AB:
5291       s = "sh";
5292       goto st;
5293     case M_SW_AB:
5294       s = "sw";
5295       goto st;
5296     case M_SWC0_AB:
5297       s = "swc0";
5298       /* Itbl support may require additional care here.  */
5299       coproc = 1;
5300       goto st;
5301     case M_SWC1_AB:
5302       s = "swc1";
5303       /* Itbl support may require additional care here.  */
5304       coproc = 1;
5305       goto st;
5306     case M_SWC2_AB:
5307       s = "swc2";
5308       /* Itbl support may require additional care here.  */
5309       coproc = 1;
5310       goto st;
5311     case M_SWC3_AB:
5312       s = "swc3";
5313       /* Itbl support may require additional care here.  */
5314       coproc = 1;
5315       goto st;
5316     case M_SWL_AB:
5317       s = "swl";
5318       goto st;
5319     case M_SWR_AB:
5320       s = "swr";
5321       goto st;
5322     case M_SC_AB:
5323       s = "sc";
5324       goto st;
5325     case M_SCD_AB:
5326       s = "scd";
5327       goto st;
5328     case M_SDC1_AB:
5329       if (mips_arch == CPU_R4650)
5330         {
5331           as_bad (_("opcode not supported on this processor"));
5332           return;
5333         }
5334       s = "sdc1";
5335       coproc = 1;
5336       /* Itbl support may require additional care here.  */
5337       goto st;
5338     case M_SDC2_AB:
5339       s = "sdc2";
5340       /* Itbl support may require additional care here.  */
5341       coproc = 1;
5342       goto st;
5343     case M_SDC3_AB:
5344       s = "sdc3";
5345       /* Itbl support may require additional care here.  */
5346       coproc = 1;
5347       goto st;
5348     case M_SDL_AB:
5349       s = "sdl";
5350       goto st;
5351     case M_SDR_AB:
5352       s = "sdr";
5353     st:
5354       tempreg = AT;
5355       used_at = 1;
5356     ld_st:
5357       /* Itbl support may require additional care here.  */
5358       if (mask == M_LWC1_AB
5359           || mask == M_SWC1_AB
5360           || mask == M_LDC1_AB
5361           || mask == M_SDC1_AB
5362           || mask == M_L_DAB
5363           || mask == M_S_DAB)
5364         fmt = "T,o(b)";
5365       else if (coproc)
5366         fmt = "E,o(b)";
5367       else
5368         fmt = "t,o(b)";
5369
5370       /* For embedded PIC, we allow loads where the offset is calculated
5371          by subtracting a symbol in the current segment from an unknown
5372          symbol, relative to a base register, e.g.:
5373                 <op>    $treg, <sym>-<localsym>($breg)
5374          This is used by the compiler for switch statements.  */
5375       if (mips_pic == EMBEDDED_PIC
5376           && offset_expr.X_op == O_subtract
5377           && (symbol_constant_p (offset_expr.X_op_symbol)
5378               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5379               : (symbol_equated_p (offset_expr.X_op_symbol)
5380                  && (S_GET_SEGMENT
5381                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5382                       ->X_add_symbol)
5383                      == now_seg)))
5384           && breg != 0
5385           && (offset_expr.X_add_number == 0
5386               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5387         {
5388           /* For this case, we output the instructions:
5389                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5390                 addiu   $tempreg,$tempreg,$breg
5391                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5392              If the relocation would fit entirely in 16 bits, it would be
5393              nice to emit:
5394                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5395              instead, but that seems quite difficult.  */
5396           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5397                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5398           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5399                        ((bfd_arch_bits_per_address (stdoutput) == 32
5400                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5401                         ? "addu" : "daddu"),
5402                        "d,v,t", tempreg, tempreg, breg);
5403           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5404                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5405           if (! used_at)
5406             return;
5407           break;
5408         }
5409
5410       if (offset_expr.X_op != O_constant
5411           && offset_expr.X_op != O_symbol)
5412         {
5413           as_bad (_("expression too complex"));
5414           offset_expr.X_op = O_constant;
5415         }
5416
5417       /* A constant expression in PIC code can be handled just as it
5418          is in non PIC code.  */
5419       if (mips_pic == NO_PIC
5420           || offset_expr.X_op == O_constant)
5421         {
5422           char *p;
5423
5424           /* If this is a reference to a GP relative symbol, and there
5425              is no base register, we want
5426                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5427              Otherwise, if there is no base register, we want
5428                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5429                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5430              If we have a constant, we need two instructions anyhow,
5431              so we always use the latter form.
5432
5433              If we have a base register, and this is a reference to a
5434              GP relative symbol, we want
5435                addu     $tempreg,$breg,$gp
5436                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5437              Otherwise we want
5438                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5439                addu     $tempreg,$tempreg,$breg
5440                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5441              With a constant we always use the latter case.
5442
5443              With 64bit address space and no base register and $at usable,
5444              we want
5445                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5446                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5447                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5448                dsll32   $tempreg,0
5449                daddu    $tempreg,$at
5450                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5451              If we have a base register, we want
5452                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5453                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5454                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5455                daddu    $at,$breg
5456                dsll32   $tempreg,0
5457                daddu    $tempreg,$at
5458                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5459
5460              Without $at we can't generate the optimal path for superscalar
5461              processors here since this would require two temporary registers.
5462                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5463                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5464                dsll     $tempreg,16
5465                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5466                dsll     $tempreg,16
5467                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5468              If we have a base register, we want
5469                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5470                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5471                dsll     $tempreg,16
5472                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5473                dsll     $tempreg,16
5474                daddu    $tempreg,$tempreg,$breg
5475                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5476
5477              If we have 64-bit addresses, as an optimization, for
5478              addresses which are 32-bit constants (e.g. kseg0/kseg1
5479              addresses) we fall back to the 32-bit address generation
5480              mechanism since it is more efficient.  This code should
5481              probably attempt to generate 64-bit constants more
5482              efficiently in general.
5483            */
5484           if (HAVE_64BIT_ADDRESSES
5485               && !(offset_expr.X_op == O_constant
5486                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
5487             {
5488               p = NULL;
5489
5490               /* We don't do GP optimization for now because RELAX_ENCODE can't
5491                  hold the data for such large chunks.  */
5492
5493               if (used_at == 0)
5494                 {
5495                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5496                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5497                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5498                                AT, (int) BFD_RELOC_HI16_S);
5499                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5500                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5501                   if (breg != 0)
5502                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5503                                  "d,v,t", AT, AT, breg);
5504                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5505                                "d,w,<", tempreg, tempreg, 0);
5506                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5507                                "d,v,t", tempreg, tempreg, AT);
5508                   macro_build (p, &icnt, &offset_expr, s,
5509                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5510                   used_at = 1;
5511                 }
5512               else
5513                 {
5514                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5515                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5516                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5517                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5518                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5519                                "d,w,<", tempreg, tempreg, 16);
5520                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5521                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5522                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5523                                "d,w,<", tempreg, tempreg, 16);
5524                   if (breg != 0)
5525                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5526                                  "d,v,t", tempreg, tempreg, breg);
5527                   macro_build (p, &icnt, &offset_expr, s,
5528                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5529                 }
5530
5531               return;
5532             }
5533
5534           if (breg == 0)
5535             {
5536               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5537                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5538                 p = NULL;
5539               else
5540                 {
5541                   frag_grow (20);
5542                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5543                                treg, (int) BFD_RELOC_GPREL16,
5544                                mips_gp_register);
5545                   p = frag_var (rs_machine_dependent, 8, 0,
5546                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5547                                               (mips_opts.warn_about_macros
5548                                                || (used_at
5549                                                    && mips_opts.noat))),
5550                                 offset_expr.X_add_symbol, 0, NULL);
5551                   used_at = 0;
5552                 }
5553               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5554               if (p != NULL)
5555                 p += 4;
5556               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5557                            (int) BFD_RELOC_LO16, tempreg);
5558             }
5559           else
5560             {
5561               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5562                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5563                 p = NULL;
5564               else
5565                 {
5566                   frag_grow (28);
5567                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5568                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5569                                "d,v,t", tempreg, breg, mips_gp_register);
5570                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5571                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5572                   p = frag_var (rs_machine_dependent, 12, 0,
5573                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5574                                 offset_expr.X_add_symbol, 0, NULL);
5575                 }
5576               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5577               if (p != NULL)
5578                 p += 4;
5579               macro_build (p, &icnt, (expressionS *) NULL,
5580                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5581                            "d,v,t", tempreg, tempreg, breg);
5582               if (p != NULL)
5583                 p += 4;
5584               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5585                            (int) BFD_RELOC_LO16, tempreg);
5586             }
5587         }
5588       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5589         {
5590           char *p;
5591
5592           /* If this is a reference to an external symbol, we want
5593                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5594                nop
5595                <op>     $treg,0($tempreg)
5596              Otherwise we want
5597                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5598                nop
5599                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5600                <op>     $treg,0($tempreg)
5601              If there is a base register, we add it to $tempreg before
5602              the <op>.  If there is a constant, we stick it in the
5603              <op> instruction.  We don't handle constants larger than
5604              16 bits, because we have no way to load the upper 16 bits
5605              (actually, we could handle them for the subset of cases
5606              in which we are not using $at).  */
5607           assert (offset_expr.X_op == O_symbol);
5608           expr1.X_add_number = offset_expr.X_add_number;
5609           offset_expr.X_add_number = 0;
5610           if (expr1.X_add_number < -0x8000
5611               || expr1.X_add_number >= 0x8000)
5612             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5613           frag_grow (20);
5614           macro_build ((char *) NULL, &icnt, &offset_expr,
5615                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5616                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5617           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5618           p = frag_var (rs_machine_dependent, 4, 0,
5619                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5620                         offset_expr.X_add_symbol, 0, NULL);
5621           macro_build (p, &icnt, &offset_expr,
5622                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5623                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5624           if (breg != 0)
5625             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5626                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5627                          "d,v,t", tempreg, tempreg, breg);
5628           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5629                        (int) BFD_RELOC_LO16, tempreg);
5630         }
5631       else if (mips_pic == SVR4_PIC)
5632         {
5633           int gpdel;
5634           char *p;
5635
5636           /* If this is a reference to an external symbol, we want
5637                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5638                addu     $tempreg,$tempreg,$gp
5639                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5640                <op>     $treg,0($tempreg)
5641              Otherwise we want
5642                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5643                nop
5644                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5645                <op>     $treg,0($tempreg)
5646              If there is a base register, we add it to $tempreg before
5647              the <op>.  If there is a constant, we stick it in the
5648              <op> instruction.  We don't handle constants larger than
5649              16 bits, because we have no way to load the upper 16 bits
5650              (actually, we could handle them for the subset of cases
5651              in which we are not using $at).
5652
5653              For NewABI, we want
5654                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5655                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5656                <op>     $treg,0($tempreg)
5657            */
5658           assert (offset_expr.X_op == O_symbol);
5659           expr1.X_add_number = offset_expr.X_add_number;
5660           offset_expr.X_add_number = 0;
5661           if (expr1.X_add_number < -0x8000
5662               || expr1.X_add_number >= 0x8000)
5663             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5664           if (HAVE_NEWABI)
5665             {
5666               macro_build ((char *) NULL, &icnt, &offset_expr,
5667                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5668                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5669                            mips_gp_register);
5670               macro_build ((char *) NULL, &icnt, &offset_expr,
5671                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5672                            "t,r,j", tempreg, tempreg,
5673                            BFD_RELOC_MIPS_GOT_OFST);
5674               if (breg != 0)
5675                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5676                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5677                              "d,v,t", tempreg, tempreg, breg);
5678               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5679                            (int) BFD_RELOC_LO16, tempreg);
5680
5681               if (! used_at)
5682                 return;
5683
5684               break;
5685             }
5686           if (reg_needs_delay (mips_gp_register))
5687             gpdel = 4;
5688           else
5689             gpdel = 0;
5690           frag_grow (36);
5691           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5692                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5693           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5694                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5695                        "d,v,t", tempreg, tempreg, mips_gp_register);
5696           macro_build ((char *) NULL, &icnt, &offset_expr,
5697                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5698                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5699                        tempreg);
5700           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5701                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5702                         offset_expr.X_add_symbol, 0, NULL);
5703           if (gpdel > 0)
5704             {
5705               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5706               p += 4;
5707             }
5708           macro_build (p, &icnt, &offset_expr,
5709                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5710                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5711                        mips_gp_register);
5712           p += 4;
5713           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5714           p += 4;
5715           macro_build (p, &icnt, &offset_expr,
5716                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5717                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5718           if (breg != 0)
5719             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5720                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5721                          "d,v,t", tempreg, tempreg, breg);
5722           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5723                        (int) BFD_RELOC_LO16, tempreg);
5724         }
5725       else if (mips_pic == EMBEDDED_PIC)
5726         {
5727           /* If there is no base register, we want
5728                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5729              If there is a base register, we want
5730                addu     $tempreg,$breg,$gp
5731                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5732              */
5733           assert (offset_expr.X_op == O_symbol);
5734           if (breg == 0)
5735             {
5736               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5737                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5738               used_at = 0;
5739             }
5740           else
5741             {
5742               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5743                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5744                            "d,v,t", tempreg, breg, mips_gp_register);
5745               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5746                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5747             }
5748         }
5749       else
5750         abort ();
5751
5752       if (! used_at)
5753         return;
5754
5755       break;
5756
5757     case M_LI:
5758     case M_LI_S:
5759       load_register (&icnt, treg, &imm_expr, 0);
5760       return;
5761
5762     case M_DLI:
5763       load_register (&icnt, treg, &imm_expr, 1);
5764       return;
5765
5766     case M_LI_SS:
5767       if (imm_expr.X_op == O_constant)
5768         {
5769           load_register (&icnt, AT, &imm_expr, 0);
5770           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5771                        "mtc1", "t,G", AT, treg);
5772           break;
5773         }
5774       else
5775         {
5776           assert (offset_expr.X_op == O_symbol
5777                   && strcmp (segment_name (S_GET_SEGMENT
5778                                            (offset_expr.X_add_symbol)),
5779                              ".lit4") == 0
5780                   && offset_expr.X_add_number == 0);
5781           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5782                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
5783           return;
5784         }
5785
5786     case M_LI_D:
5787       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5788          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5789          order 32 bits of the value and the low order 32 bits are either
5790          zero or in OFFSET_EXPR.  */
5791       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5792         {
5793           if (HAVE_64BIT_GPRS)
5794             load_register (&icnt, treg, &imm_expr, 1);
5795           else
5796             {
5797               int hreg, lreg;
5798
5799               if (target_big_endian)
5800                 {
5801                   hreg = treg;
5802                   lreg = treg + 1;
5803                 }
5804               else
5805                 {
5806                   hreg = treg + 1;
5807                   lreg = treg;
5808                 }
5809
5810               if (hreg <= 31)
5811                 load_register (&icnt, hreg, &imm_expr, 0);
5812               if (lreg <= 31)
5813                 {
5814                   if (offset_expr.X_op == O_absent)
5815                     move_register (&icnt, lreg, 0);
5816                   else
5817                     {
5818                       assert (offset_expr.X_op == O_constant);
5819                       load_register (&icnt, lreg, &offset_expr, 0);
5820                     }
5821                 }
5822             }
5823           return;
5824         }
5825
5826       /* We know that sym is in the .rdata section.  First we get the
5827          upper 16 bits of the address.  */
5828       if (mips_pic == NO_PIC)
5829         {
5830           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5831         }
5832       else if (mips_pic == SVR4_PIC)
5833         {
5834           macro_build ((char *) NULL, &icnt, &offset_expr,
5835                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5836                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5837                        mips_gp_register);
5838         }
5839       else if (mips_pic == EMBEDDED_PIC)
5840         {
5841           /* For embedded PIC we pick up the entire address off $gp in
5842              a single instruction.  */
5843           macro_build ((char *) NULL, &icnt, &offset_expr,
5844                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5845                        mips_gp_register, (int) BFD_RELOC_GPREL16);
5846           offset_expr.X_op = O_constant;
5847           offset_expr.X_add_number = 0;
5848         }
5849       else
5850         abort ();
5851
5852       /* Now we load the register(s).  */
5853       if (HAVE_64BIT_GPRS)
5854         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5855                      treg, (int) BFD_RELOC_LO16, AT);
5856       else
5857         {
5858           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5859                        treg, (int) BFD_RELOC_LO16, AT);
5860           if (treg != RA)
5861             {
5862               /* FIXME: How in the world do we deal with the possible
5863                  overflow here?  */
5864               offset_expr.X_add_number += 4;
5865               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5866                            treg + 1, (int) BFD_RELOC_LO16, AT);
5867             }
5868         }
5869
5870       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5871          does not become a variant frag.  */
5872       frag_wane (frag_now);
5873       frag_new (0);
5874
5875       break;
5876
5877     case M_LI_DD:
5878       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5879          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5880          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5881          the value and the low order 32 bits are either zero or in
5882          OFFSET_EXPR.  */
5883       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5884         {
5885           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5886           if (HAVE_64BIT_FPRS)
5887             {
5888               assert (HAVE_64BIT_GPRS);
5889               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5890                            "dmtc1", "t,S", AT, treg);
5891             }
5892           else
5893             {
5894               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5895                            "mtc1", "t,G", AT, treg + 1);
5896               if (offset_expr.X_op == O_absent)
5897                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5898                              "mtc1", "t,G", 0, treg);
5899               else
5900                 {
5901                   assert (offset_expr.X_op == O_constant);
5902                   load_register (&icnt, AT, &offset_expr, 0);
5903                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5904                                "mtc1", "t,G", AT, treg);
5905                 }
5906             }
5907           break;
5908         }
5909
5910       assert (offset_expr.X_op == O_symbol
5911               && offset_expr.X_add_number == 0);
5912       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5913       if (strcmp (s, ".lit8") == 0)
5914         {
5915           if (mips_opts.isa != ISA_MIPS1)
5916             {
5917               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5918                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5919                            mips_gp_register);
5920               return;
5921             }
5922           breg = mips_gp_register;
5923           r = BFD_RELOC_MIPS_LITERAL;
5924           goto dob;
5925         }
5926       else
5927         {
5928           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5929           if (mips_pic == SVR4_PIC)
5930             macro_build ((char *) NULL, &icnt, &offset_expr,
5931                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5932                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5933                          mips_gp_register);
5934           else
5935             {
5936               /* FIXME: This won't work for a 64 bit address.  */
5937               macro_build_lui (NULL, &icnt, &offset_expr, AT);
5938             }
5939
5940           if (mips_opts.isa != ISA_MIPS1)
5941             {
5942               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5943                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5944
5945               /* To avoid confusion in tc_gen_reloc, we must ensure
5946                  that this does not become a variant frag.  */
5947               frag_wane (frag_now);
5948               frag_new (0);
5949
5950               break;
5951             }
5952           breg = AT;
5953           r = BFD_RELOC_LO16;
5954           goto dob;
5955         }
5956
5957     case M_L_DOB:
5958       if (mips_arch == CPU_R4650)
5959         {
5960           as_bad (_("opcode not supported on this processor"));
5961           return;
5962         }
5963       /* Even on a big endian machine $fn comes before $fn+1.  We have
5964          to adjust when loading from memory.  */
5965       r = BFD_RELOC_LO16;
5966     dob:
5967       assert (mips_opts.isa == ISA_MIPS1);
5968       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5969                    target_big_endian ? treg + 1 : treg,
5970                    (int) r, breg);
5971       /* FIXME: A possible overflow which I don't know how to deal
5972          with.  */
5973       offset_expr.X_add_number += 4;
5974       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5975                    target_big_endian ? treg : treg + 1,
5976                    (int) r, breg);
5977
5978       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5979          does not become a variant frag.  */
5980       frag_wane (frag_now);
5981       frag_new (0);
5982
5983       if (breg != AT)
5984         return;
5985       break;
5986
5987     case M_L_DAB:
5988       /*
5989        * The MIPS assembler seems to check for X_add_number not
5990        * being double aligned and generating:
5991        *        lui     at,%hi(foo+1)
5992        *        addu    at,at,v1
5993        *        addiu   at,at,%lo(foo+1)
5994        *        lwc1    f2,0(at)
5995        *        lwc1    f3,4(at)
5996        * But, the resulting address is the same after relocation so why
5997        * generate the extra instruction?
5998        */
5999       if (mips_arch == CPU_R4650)
6000         {
6001           as_bad (_("opcode not supported on this processor"));
6002           return;
6003         }
6004       /* Itbl support may require additional care here.  */
6005       coproc = 1;
6006       if (mips_opts.isa != ISA_MIPS1)
6007         {
6008           s = "ldc1";
6009           goto ld;
6010         }
6011
6012       s = "lwc1";
6013       fmt = "T,o(b)";
6014       goto ldd_std;
6015
6016     case M_S_DAB:
6017       if (mips_arch == CPU_R4650)
6018         {
6019           as_bad (_("opcode not supported on this processor"));
6020           return;
6021         }
6022
6023       if (mips_opts.isa != ISA_MIPS1)
6024         {
6025           s = "sdc1";
6026           goto st;
6027         }
6028
6029       s = "swc1";
6030       fmt = "T,o(b)";
6031       /* Itbl support may require additional care here.  */
6032       coproc = 1;
6033       goto ldd_std;
6034
6035     case M_LD_AB:
6036       if (HAVE_64BIT_GPRS)
6037         {
6038           s = "ld";
6039           goto ld;
6040         }
6041
6042       s = "lw";
6043       fmt = "t,o(b)";
6044       goto ldd_std;
6045
6046     case M_SD_AB:
6047       if (HAVE_64BIT_GPRS)
6048         {
6049           s = "sd";
6050           goto st;
6051         }
6052
6053       s = "sw";
6054       fmt = "t,o(b)";
6055
6056     ldd_std:
6057       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6058          loads for the case of doing a pair of loads to simulate an 'ld'.
6059          This is not currently done by the compiler, and assembly coders
6060          writing embedded-pic code can cope.  */
6061
6062       if (offset_expr.X_op != O_symbol
6063           && offset_expr.X_op != O_constant)
6064         {
6065           as_bad (_("expression too complex"));
6066           offset_expr.X_op = O_constant;
6067         }
6068
6069       /* Even on a big endian machine $fn comes before $fn+1.  We have
6070          to adjust when loading from memory.  We set coproc if we must
6071          load $fn+1 first.  */
6072       /* Itbl support may require additional care here.  */
6073       if (! target_big_endian)
6074         coproc = 0;
6075
6076       if (mips_pic == NO_PIC
6077           || offset_expr.X_op == O_constant)
6078         {
6079           char *p;
6080
6081           /* If this is a reference to a GP relative symbol, we want
6082                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6083                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6084              If we have a base register, we use this
6085                addu     $at,$breg,$gp
6086                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6087                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6088              If this is not a GP relative symbol, we want
6089                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6090                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6091                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6092              If there is a base register, we add it to $at after the
6093              lui instruction.  If there is a constant, we always use
6094              the last case.  */
6095           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6096               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6097             {
6098               p = NULL;
6099               used_at = 1;
6100             }
6101           else
6102             {
6103               int off;
6104
6105               if (breg == 0)
6106                 {
6107                   frag_grow (28);
6108                   tempreg = mips_gp_register;
6109                   off = 0;
6110                   used_at = 0;
6111                 }
6112               else
6113                 {
6114                   frag_grow (36);
6115                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6116                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6117                                "d,v,t", AT, breg, mips_gp_register);
6118                   tempreg = AT;
6119                   off = 4;
6120                   used_at = 1;
6121                 }
6122
6123               /* Itbl support may require additional care here.  */
6124               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6125                            coproc ? treg + 1 : treg,
6126                            (int) BFD_RELOC_GPREL16, tempreg);
6127               offset_expr.X_add_number += 4;
6128
6129               /* Set mips_optimize to 2 to avoid inserting an
6130                  undesired nop.  */
6131               hold_mips_optimize = mips_optimize;
6132               mips_optimize = 2;
6133               /* Itbl support may require additional care here.  */
6134               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6135                            coproc ? treg : treg + 1,
6136                            (int) BFD_RELOC_GPREL16, tempreg);
6137               mips_optimize = hold_mips_optimize;
6138
6139               p = frag_var (rs_machine_dependent, 12 + off, 0,
6140                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6141                                           used_at && mips_opts.noat),
6142                             offset_expr.X_add_symbol, 0, NULL);
6143
6144               /* We just generated two relocs.  When tc_gen_reloc
6145                  handles this case, it will skip the first reloc and
6146                  handle the second.  The second reloc already has an
6147                  extra addend of 4, which we added above.  We must
6148                  subtract it out, and then subtract another 4 to make
6149                  the first reloc come out right.  The second reloc
6150                  will come out right because we are going to add 4 to
6151                  offset_expr when we build its instruction below.
6152
6153                  If we have a symbol, then we don't want to include
6154                  the offset, because it will wind up being included
6155                  when we generate the reloc.  */
6156
6157               if (offset_expr.X_op == O_constant)
6158                 offset_expr.X_add_number -= 8;
6159               else
6160                 {
6161                   offset_expr.X_add_number = -4;
6162                   offset_expr.X_op = O_constant;
6163                 }
6164             }
6165           macro_build_lui (p, &icnt, &offset_expr, AT);
6166           if (p != NULL)
6167             p += 4;
6168           if (breg != 0)
6169             {
6170               macro_build (p, &icnt, (expressionS *) NULL,
6171                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6172                            "d,v,t", AT, breg, AT);
6173               if (p != NULL)
6174                 p += 4;
6175             }
6176           /* Itbl support may require additional care here.  */
6177           macro_build (p, &icnt, &offset_expr, s, fmt,
6178                        coproc ? treg + 1 : treg,
6179                        (int) BFD_RELOC_LO16, AT);
6180           if (p != NULL)
6181             p += 4;
6182           /* FIXME: How do we handle overflow here?  */
6183           offset_expr.X_add_number += 4;
6184           /* Itbl support may require additional care here.  */
6185           macro_build (p, &icnt, &offset_expr, s, fmt,
6186                        coproc ? treg : treg + 1,
6187                        (int) BFD_RELOC_LO16, AT);
6188         }
6189       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6190         {
6191           int off;
6192
6193           /* If this is a reference to an external symbol, we want
6194                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6195                nop
6196                <op>     $treg,0($at)
6197                <op>     $treg+1,4($at)
6198              Otherwise we want
6199                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6200                nop
6201                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6202                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6203              If there is a base register we add it to $at before the
6204              lwc1 instructions.  If there is a constant we include it
6205              in the lwc1 instructions.  */
6206           used_at = 1;
6207           expr1.X_add_number = offset_expr.X_add_number;
6208           offset_expr.X_add_number = 0;
6209           if (expr1.X_add_number < -0x8000
6210               || expr1.X_add_number >= 0x8000 - 4)
6211             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6212           if (breg == 0)
6213             off = 0;
6214           else
6215             off = 4;
6216           frag_grow (24 + off);
6217           macro_build ((char *) NULL, &icnt, &offset_expr,
6218                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6219                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6220           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6221           if (breg != 0)
6222             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6223                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6224                          "d,v,t", AT, breg, AT);
6225           /* Itbl support may require additional care here.  */
6226           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6227                        coproc ? treg + 1 : treg,
6228                        (int) BFD_RELOC_LO16, AT);
6229           expr1.X_add_number += 4;
6230
6231           /* Set mips_optimize to 2 to avoid inserting an undesired
6232              nop.  */
6233           hold_mips_optimize = mips_optimize;
6234           mips_optimize = 2;
6235           /* Itbl support may require additional care here.  */
6236           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6237                        coproc ? treg : treg + 1,
6238                        (int) BFD_RELOC_LO16, AT);
6239           mips_optimize = hold_mips_optimize;
6240
6241           (void) frag_var (rs_machine_dependent, 0, 0,
6242                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6243                            offset_expr.X_add_symbol, 0, NULL);
6244         }
6245       else if (mips_pic == SVR4_PIC)
6246         {
6247           int gpdel, off;
6248           char *p;
6249
6250           /* If this is a reference to an external symbol, we want
6251                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6252                addu     $at,$at,$gp
6253                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6254                nop
6255                <op>     $treg,0($at)
6256                <op>     $treg+1,4($at)
6257              Otherwise we want
6258                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6259                nop
6260                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6261                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6262              If there is a base register we add it to $at before the
6263              lwc1 instructions.  If there is a constant we include it
6264              in the lwc1 instructions.  */
6265           used_at = 1;
6266           expr1.X_add_number = offset_expr.X_add_number;
6267           offset_expr.X_add_number = 0;
6268           if (expr1.X_add_number < -0x8000
6269               || expr1.X_add_number >= 0x8000 - 4)
6270             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6271           if (reg_needs_delay (mips_gp_register))
6272             gpdel = 4;
6273           else
6274             gpdel = 0;
6275           if (breg == 0)
6276             off = 0;
6277           else
6278             off = 4;
6279           frag_grow (56);
6280           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6281                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6282           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6283                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6284                        "d,v,t", AT, AT, mips_gp_register);
6285           macro_build ((char *) NULL, &icnt, &offset_expr,
6286                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6287                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6288           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6289           if (breg != 0)
6290             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6291                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6292                          "d,v,t", AT, breg, AT);
6293           /* Itbl support may require additional care here.  */
6294           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6295                        coproc ? treg + 1 : treg,
6296                        (int) BFD_RELOC_LO16, AT);
6297           expr1.X_add_number += 4;
6298
6299           /* Set mips_optimize to 2 to avoid inserting an undesired
6300              nop.  */
6301           hold_mips_optimize = mips_optimize;
6302           mips_optimize = 2;
6303           /* Itbl support may require additional care here.  */
6304           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6305                        coproc ? treg : treg + 1,
6306                        (int) BFD_RELOC_LO16, AT);
6307           mips_optimize = hold_mips_optimize;
6308           expr1.X_add_number -= 4;
6309
6310           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6311                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6312                                       8 + gpdel + off, 1, 0),
6313                         offset_expr.X_add_symbol, 0, NULL);
6314           if (gpdel > 0)
6315             {
6316               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6317               p += 4;
6318             }
6319           macro_build (p, &icnt, &offset_expr,
6320                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6321                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6322                        mips_gp_register);
6323           p += 4;
6324           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6325           p += 4;
6326           if (breg != 0)
6327             {
6328               macro_build (p, &icnt, (expressionS *) NULL,
6329                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6330                            "d,v,t", AT, breg, AT);
6331               p += 4;
6332             }
6333           /* Itbl support may require additional care here.  */
6334           macro_build (p, &icnt, &expr1, s, fmt,
6335                        coproc ? treg + 1 : treg,
6336                        (int) BFD_RELOC_LO16, AT);
6337           p += 4;
6338           expr1.X_add_number += 4;
6339
6340           /* Set mips_optimize to 2 to avoid inserting an undesired
6341              nop.  */
6342           hold_mips_optimize = mips_optimize;
6343           mips_optimize = 2;
6344           /* Itbl support may require additional care here.  */
6345           macro_build (p, &icnt, &expr1, s, fmt,
6346                        coproc ? treg : treg + 1,
6347                        (int) BFD_RELOC_LO16, AT);
6348           mips_optimize = hold_mips_optimize;
6349         }
6350       else if (mips_pic == EMBEDDED_PIC)
6351         {
6352           /* If there is no base register, we use
6353                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6354                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6355              If we have a base register, we use
6356                addu     $at,$breg,$gp
6357                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6358                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6359              */
6360           if (breg == 0)
6361             {
6362               tempreg = mips_gp_register;
6363               used_at = 0;
6364             }
6365           else
6366             {
6367               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6368                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6369                            "d,v,t", AT, breg, mips_gp_register);
6370               tempreg = AT;
6371               used_at = 1;
6372             }
6373
6374           /* Itbl support may require additional care here.  */
6375           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6376                        coproc ? treg + 1 : treg,
6377                        (int) BFD_RELOC_GPREL16, tempreg);
6378           offset_expr.X_add_number += 4;
6379           /* Itbl support may require additional care here.  */
6380           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6381                        coproc ? treg : treg + 1,
6382                        (int) BFD_RELOC_GPREL16, tempreg);
6383         }
6384       else
6385         abort ();
6386
6387       if (! used_at)
6388         return;
6389
6390       break;
6391
6392     case M_LD_OB:
6393       s = "lw";
6394       goto sd_ob;
6395     case M_SD_OB:
6396       s = "sw";
6397     sd_ob:
6398       assert (HAVE_32BIT_ADDRESSES);
6399       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6400                    (int) BFD_RELOC_LO16, breg);
6401       offset_expr.X_add_number += 4;
6402       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6403                    (int) BFD_RELOC_LO16, breg);
6404       return;
6405
6406    /* New code added to support COPZ instructions.
6407       This code builds table entries out of the macros in mip_opcodes.
6408       R4000 uses interlocks to handle coproc delays.
6409       Other chips (like the R3000) require nops to be inserted for delays.
6410
6411       FIXME: Currently, we require that the user handle delays.
6412       In order to fill delay slots for non-interlocked chips,
6413       we must have a way to specify delays based on the coprocessor.
6414       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6415       What are the side-effects of the cop instruction?
6416       What cache support might we have and what are its effects?
6417       Both coprocessor & memory require delays. how long???
6418       What registers are read/set/modified?
6419
6420       If an itbl is provided to interpret cop instructions,
6421       this knowledge can be encoded in the itbl spec.  */
6422
6423     case M_COP0:
6424       s = "c0";
6425       goto copz;
6426     case M_COP1:
6427       s = "c1";
6428       goto copz;
6429     case M_COP2:
6430       s = "c2";
6431       goto copz;
6432     case M_COP3:
6433       s = "c3";
6434     copz:
6435       /* For now we just do C (same as Cz).  The parameter will be
6436          stored in insn_opcode by mips_ip.  */
6437       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6438                    ip->insn_opcode);
6439       return;
6440
6441     case M_MOVE:
6442       move_register (&icnt, dreg, sreg);
6443       return;
6444
6445 #ifdef LOSING_COMPILER
6446     default:
6447       /* Try and see if this is a new itbl instruction.
6448          This code builds table entries out of the macros in mip_opcodes.
6449          FIXME: For now we just assemble the expression and pass it's
6450          value along as a 32-bit immediate.
6451          We may want to have the assembler assemble this value,
6452          so that we gain the assembler's knowledge of delay slots,
6453          symbols, etc.
6454          Would it be more efficient to use mask (id) here? */
6455       if (itbl_have_entries
6456           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6457         {
6458           s = ip->insn_mo->name;
6459           s2 = "cop3";
6460           coproc = ITBL_DECODE_PNUM (immed_expr);;
6461           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6462           return;
6463         }
6464       macro2 (ip);
6465       return;
6466     }
6467   if (mips_opts.noat)
6468     as_warn (_("Macro used $at after \".set noat\""));
6469 }
6470
6471 static void
6472 macro2 (ip)
6473      struct mips_cl_insn *ip;
6474 {
6475   register int treg, sreg, dreg, breg;
6476   int tempreg;
6477   int mask;
6478   int icnt = 0;
6479   int used_at;
6480   expressionS expr1;
6481   const char *s;
6482   const char *s2;
6483   const char *fmt;
6484   int likely = 0;
6485   int dbl = 0;
6486   int coproc = 0;
6487   int lr = 0;
6488   int imm = 0;
6489   int off;
6490   offsetT maxnum;
6491   bfd_reloc_code_real_type r;
6492   char *p;
6493
6494   treg = (ip->insn_opcode >> 16) & 0x1f;
6495   dreg = (ip->insn_opcode >> 11) & 0x1f;
6496   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6497   mask = ip->insn_mo->mask;
6498
6499   expr1.X_op = O_constant;
6500   expr1.X_op_symbol = NULL;
6501   expr1.X_add_symbol = NULL;
6502   expr1.X_add_number = 1;
6503
6504   switch (mask)
6505     {
6506 #endif /* LOSING_COMPILER */
6507
6508     case M_DMUL:
6509       dbl = 1;
6510     case M_MUL:
6511       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6512                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6513       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6514                    dreg);
6515       return;
6516
6517     case M_DMUL_I:
6518       dbl = 1;
6519     case M_MUL_I:
6520       /* The MIPS assembler some times generates shifts and adds.  I'm
6521          not trying to be that fancy. GCC should do this for us
6522          anyway.  */
6523       load_register (&icnt, AT, &imm_expr, dbl);
6524       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6525                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6526       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6527                    dreg);
6528       break;
6529
6530     case M_DMULO_I:
6531       dbl = 1;
6532     case M_MULO_I:
6533       imm = 1;
6534       goto do_mulo;
6535
6536     case M_DMULO:
6537       dbl = 1;
6538     case M_MULO:
6539     do_mulo:
6540       mips_emit_delays (true);
6541       ++mips_opts.noreorder;
6542       mips_any_noreorder = 1;
6543       if (imm)
6544         load_register (&icnt, AT, &imm_expr, dbl);
6545       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6546                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6547       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6548                    dreg);
6549       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6550                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6551       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6552                    AT);
6553       if (mips_trap)
6554         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6555                      dreg, AT);
6556       else
6557         {
6558           expr1.X_add_number = 8;
6559           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6560                        AT);
6561           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6562                        0);
6563           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6564                        "c", 6);
6565         }
6566       --mips_opts.noreorder;
6567       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6568       break;
6569
6570     case M_DMULOU_I:
6571       dbl = 1;
6572     case M_MULOU_I:
6573       imm = 1;
6574       goto do_mulou;
6575
6576     case M_DMULOU:
6577       dbl = 1;
6578     case M_MULOU:
6579     do_mulou:
6580       mips_emit_delays (true);
6581       ++mips_opts.noreorder;
6582       mips_any_noreorder = 1;
6583       if (imm)
6584         load_register (&icnt, AT, &imm_expr, dbl);
6585       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6586                    dbl ? "dmultu" : "multu",
6587                    "s,t", sreg, imm ? AT : treg);
6588       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6589                    AT);
6590       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6591                    dreg);
6592       if (mips_trap)
6593         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6594                      AT, 0);
6595       else
6596         {
6597           expr1.X_add_number = 8;
6598           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6599           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6600                        0);
6601           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6602                        "c", 6);
6603         }
6604       --mips_opts.noreorder;
6605       break;
6606
6607     case M_DROL:
6608       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6609                    "d,v,t", AT, 0, treg);
6610       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6611                    "d,t,s", AT, sreg, AT);
6612       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6613                    "d,t,s", dreg, sreg, treg);
6614       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6615                    "d,v,t", dreg, dreg, AT);
6616       break;
6617
6618     case M_ROL:
6619       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6620                    "d,v,t", AT, 0, treg);
6621       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6622                    "d,t,s", AT, sreg, AT);
6623       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6624                    "d,t,s", dreg, sreg, treg);
6625       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6626                    "d,v,t", dreg, dreg, AT);
6627       break;
6628
6629     case M_DROL_I:
6630       {
6631         unsigned int rot;
6632         char *l, *r;
6633
6634         if (imm_expr.X_op != O_constant)
6635           as_bad (_("rotate count too large"));
6636         rot = imm_expr.X_add_number & 0x3f;
6637         if (! rot)
6638           break;
6639         l = (rot < 0x20) ? "dsll" : "dsll32";
6640         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6641         rot &= 0x1f;
6642         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6643                      "d,w,<", AT, sreg, rot);
6644         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6645                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6646         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6647                      "d,v,t", dreg, dreg, AT);
6648       }
6649       break;
6650
6651     case M_ROL_I:
6652       {
6653         unsigned int rot;
6654
6655         if (imm_expr.X_op != O_constant)
6656           as_bad (_("rotate count too large"));
6657         rot = imm_expr.X_add_number & 0x1f;
6658         if (! rot)
6659           break;
6660         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6661                      "d,w,<", AT, sreg, rot);
6662         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6663                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6664         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6665                      "d,v,t", dreg, dreg, AT);
6666       }
6667       break;
6668
6669     case M_DROR:
6670       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6671                    "d,v,t", AT, 0, treg);
6672       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6673                    "d,t,s", AT, sreg, AT);
6674       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6675                    "d,t,s", dreg, sreg, treg);
6676       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6677                    "d,v,t", dreg, dreg, AT);
6678       break;
6679
6680     case M_ROR:
6681       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6682                    "d,v,t", AT, 0, treg);
6683       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6684                    "d,t,s", AT, sreg, AT);
6685       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6686                    "d,t,s", dreg, sreg, treg);
6687       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6688                    "d,v,t", dreg, dreg, AT);
6689       break;
6690
6691     case M_DROR_I:
6692       {
6693         unsigned int rot;
6694         char *l, *r;
6695
6696         if (imm_expr.X_op != O_constant)
6697           as_bad (_("rotate count too large"));
6698         rot = imm_expr.X_add_number & 0x3f;
6699         if (! rot)
6700           break;
6701         r = (rot < 0x20) ? "dsrl" : "dsrl32";
6702         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6703         rot &= 0x1f;
6704         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6705                      "d,w,<", AT, sreg, rot);
6706         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6707                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6708         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6709                      "d,v,t", dreg, dreg, AT);
6710       }
6711       break;
6712
6713     case M_ROR_I:
6714       {
6715         unsigned int rot;
6716
6717         if (imm_expr.X_op != O_constant)
6718           as_bad (_("rotate count too large"));
6719         rot = imm_expr.X_add_number & 0x1f;
6720         if (! rot)
6721           break;
6722         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6723                      "d,w,<", AT, sreg, rot);
6724         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6725                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6726         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6727                      "d,v,t", dreg, dreg, AT);
6728       }
6729       break;
6730
6731     case M_S_DOB:
6732       if (mips_arch == CPU_R4650)
6733         {
6734           as_bad (_("opcode not supported on this processor"));
6735           return;
6736         }
6737       assert (mips_opts.isa == ISA_MIPS1);
6738       /* Even on a big endian machine $fn comes before $fn+1.  We have
6739          to adjust when storing to memory.  */
6740       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6741                    target_big_endian ? treg + 1 : treg,
6742                    (int) BFD_RELOC_LO16, breg);
6743       offset_expr.X_add_number += 4;
6744       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6745                    target_big_endian ? treg : treg + 1,
6746                    (int) BFD_RELOC_LO16, breg);
6747       return;
6748
6749     case M_SEQ:
6750       if (sreg == 0)
6751         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6752                      treg, (int) BFD_RELOC_LO16);
6753       else if (treg == 0)
6754         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6755                      sreg, (int) BFD_RELOC_LO16);
6756       else
6757         {
6758           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6759                        "d,v,t", dreg, sreg, treg);
6760           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6761                        dreg, (int) BFD_RELOC_LO16);
6762         }
6763       return;
6764
6765     case M_SEQ_I:
6766       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6767         {
6768           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6769                        sreg, (int) BFD_RELOC_LO16);
6770           return;
6771         }
6772       if (sreg == 0)
6773         {
6774           as_warn (_("Instruction %s: result is always false"),
6775                    ip->insn_mo->name);
6776           move_register (&icnt, dreg, 0);
6777           return;
6778         }
6779       if (imm_expr.X_op == O_constant
6780           && imm_expr.X_add_number >= 0
6781           && imm_expr.X_add_number < 0x10000)
6782         {
6783           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6784                        sreg, (int) BFD_RELOC_LO16);
6785           used_at = 0;
6786         }
6787       else if (imm_expr.X_op == O_constant
6788                && imm_expr.X_add_number > -0x8000
6789                && imm_expr.X_add_number < 0)
6790         {
6791           imm_expr.X_add_number = -imm_expr.X_add_number;
6792           macro_build ((char *) NULL, &icnt, &imm_expr,
6793                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6794                        "t,r,j", dreg, sreg,
6795                        (int) BFD_RELOC_LO16);
6796           used_at = 0;
6797         }
6798       else
6799         {
6800           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6801           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6802                        "d,v,t", dreg, sreg, AT);
6803           used_at = 1;
6804         }
6805       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6806                    (int) BFD_RELOC_LO16);
6807       if (used_at)
6808         break;
6809       return;
6810
6811     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6812       s = "slt";
6813       goto sge;
6814     case M_SGEU:
6815       s = "sltu";
6816     sge:
6817       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6818                    dreg, sreg, treg);
6819       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6820                    (int) BFD_RELOC_LO16);
6821       return;
6822
6823     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6824     case M_SGEU_I:
6825       if (imm_expr.X_op == O_constant
6826           && imm_expr.X_add_number >= -0x8000
6827           && imm_expr.X_add_number < 0x8000)
6828         {
6829           macro_build ((char *) NULL, &icnt, &imm_expr,
6830                        mask == M_SGE_I ? "slti" : "sltiu",
6831                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6832           used_at = 0;
6833         }
6834       else
6835         {
6836           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6837           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6838                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6839                        AT);
6840           used_at = 1;
6841         }
6842       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6843                    (int) BFD_RELOC_LO16);
6844       if (used_at)
6845         break;
6846       return;
6847
6848     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6849       s = "slt";
6850       goto sgt;
6851     case M_SGTU:
6852       s = "sltu";
6853     sgt:
6854       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6855                    dreg, treg, sreg);
6856       return;
6857
6858     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6859       s = "slt";
6860       goto sgti;
6861     case M_SGTU_I:
6862       s = "sltu";
6863     sgti:
6864       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6865       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6866                    dreg, AT, sreg);
6867       break;
6868
6869     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6870       s = "slt";
6871       goto sle;
6872     case M_SLEU:
6873       s = "sltu";
6874     sle:
6875       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6876                    dreg, treg, sreg);
6877       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6878                    (int) BFD_RELOC_LO16);
6879       return;
6880
6881     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6882       s = "slt";
6883       goto slei;
6884     case M_SLEU_I:
6885       s = "sltu";
6886     slei:
6887       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6888       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6889                    dreg, AT, sreg);
6890       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6891                    (int) BFD_RELOC_LO16);
6892       break;
6893
6894     case M_SLT_I:
6895       if (imm_expr.X_op == O_constant
6896           && imm_expr.X_add_number >= -0x8000
6897           && imm_expr.X_add_number < 0x8000)
6898         {
6899           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6900                        dreg, sreg, (int) BFD_RELOC_LO16);
6901           return;
6902         }
6903       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6904       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6905                    dreg, sreg, AT);
6906       break;
6907
6908     case M_SLTU_I:
6909       if (imm_expr.X_op == O_constant
6910           && imm_expr.X_add_number >= -0x8000
6911           && imm_expr.X_add_number < 0x8000)
6912         {
6913           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6914                        dreg, sreg, (int) BFD_RELOC_LO16);
6915           return;
6916         }
6917       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6918       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6919                    "d,v,t", dreg, sreg, AT);
6920       break;
6921
6922     case M_SNE:
6923       if (sreg == 0)
6924         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6925                      "d,v,t", dreg, 0, treg);
6926       else if (treg == 0)
6927         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6928                      "d,v,t", dreg, 0, sreg);
6929       else
6930         {
6931           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6932                        "d,v,t", dreg, sreg, treg);
6933           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6934                        "d,v,t", dreg, 0, dreg);
6935         }
6936       return;
6937
6938     case M_SNE_I:
6939       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6940         {
6941           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6942                        "d,v,t", dreg, 0, sreg);
6943           return;
6944         }
6945       if (sreg == 0)
6946         {
6947           as_warn (_("Instruction %s: result is always true"),
6948                    ip->insn_mo->name);
6949           macro_build ((char *) NULL, &icnt, &expr1,
6950                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6951                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6952           return;
6953         }
6954       if (imm_expr.X_op == O_constant
6955           && imm_expr.X_add_number >= 0
6956           && imm_expr.X_add_number < 0x10000)
6957         {
6958           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6959                        dreg, sreg, (int) BFD_RELOC_LO16);
6960           used_at = 0;
6961         }
6962       else if (imm_expr.X_op == O_constant
6963                && imm_expr.X_add_number > -0x8000
6964                && imm_expr.X_add_number < 0)
6965         {
6966           imm_expr.X_add_number = -imm_expr.X_add_number;
6967           macro_build ((char *) NULL, &icnt, &imm_expr,
6968                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6969                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6970           used_at = 0;
6971         }
6972       else
6973         {
6974           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6975           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6976                        "d,v,t", dreg, sreg, AT);
6977           used_at = 1;
6978         }
6979       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6980                    "d,v,t", dreg, 0, dreg);
6981       if (used_at)
6982         break;
6983       return;
6984
6985     case M_DSUB_I:
6986       dbl = 1;
6987     case M_SUB_I:
6988       if (imm_expr.X_op == O_constant
6989           && imm_expr.X_add_number > -0x8000
6990           && imm_expr.X_add_number <= 0x8000)
6991         {
6992           imm_expr.X_add_number = -imm_expr.X_add_number;
6993           macro_build ((char *) NULL, &icnt, &imm_expr,
6994                        dbl ? "daddi" : "addi",
6995                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6996           return;
6997         }
6998       load_register (&icnt, AT, &imm_expr, dbl);
6999       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7000                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7001       break;
7002
7003     case M_DSUBU_I:
7004       dbl = 1;
7005     case M_SUBU_I:
7006       if (imm_expr.X_op == O_constant
7007           && imm_expr.X_add_number > -0x8000
7008           && imm_expr.X_add_number <= 0x8000)
7009         {
7010           imm_expr.X_add_number = -imm_expr.X_add_number;
7011           macro_build ((char *) NULL, &icnt, &imm_expr,
7012                        dbl ? "daddiu" : "addiu",
7013                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7014           return;
7015         }
7016       load_register (&icnt, AT, &imm_expr, dbl);
7017       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7018                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7019       break;
7020
7021     case M_TEQ_I:
7022       s = "teq";
7023       goto trap;
7024     case M_TGE_I:
7025       s = "tge";
7026       goto trap;
7027     case M_TGEU_I:
7028       s = "tgeu";
7029       goto trap;
7030     case M_TLT_I:
7031       s = "tlt";
7032       goto trap;
7033     case M_TLTU_I:
7034       s = "tltu";
7035       goto trap;
7036     case M_TNE_I:
7037       s = "tne";
7038     trap:
7039       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7040       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7041                    AT);
7042       break;
7043
7044     case M_TRUNCWS:
7045     case M_TRUNCWD:
7046       assert (mips_opts.isa == ISA_MIPS1);
7047       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7048       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7049
7050       /*
7051        * Is the double cfc1 instruction a bug in the mips assembler;
7052        * or is there a reason for it?
7053        */
7054       mips_emit_delays (true);
7055       ++mips_opts.noreorder;
7056       mips_any_noreorder = 1;
7057       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7058                    treg, RA);
7059       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7060                    treg, RA);
7061       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7062       expr1.X_add_number = 3;
7063       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7064                    (int) BFD_RELOC_LO16);
7065       expr1.X_add_number = 2;
7066       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7067                      (int) BFD_RELOC_LO16);
7068       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7069                    AT, RA);
7070       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7071       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7072               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7073       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7074                    treg, RA);
7075       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7076       --mips_opts.noreorder;
7077       break;
7078
7079     case M_ULH:
7080       s = "lb";
7081       goto ulh;
7082     case M_ULHU:
7083       s = "lbu";
7084     ulh:
7085       if (offset_expr.X_add_number >= 0x7fff)
7086         as_bad (_("operand overflow"));
7087       /* avoid load delay */
7088       if (! target_big_endian)
7089         ++offset_expr.X_add_number;
7090       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7091                    (int) BFD_RELOC_LO16, breg);
7092       if (! target_big_endian)
7093         --offset_expr.X_add_number;
7094       else
7095         ++offset_expr.X_add_number;
7096       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7097                    (int) BFD_RELOC_LO16, breg);
7098       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7099                    treg, treg, 8);
7100       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7101                    treg, treg, AT);
7102       break;
7103
7104     case M_ULD:
7105       s = "ldl";
7106       s2 = "ldr";
7107       off = 7;
7108       goto ulw;
7109     case M_ULW:
7110       s = "lwl";
7111       s2 = "lwr";
7112       off = 3;
7113     ulw:
7114       if (offset_expr.X_add_number >= 0x8000 - off)
7115         as_bad (_("operand overflow"));
7116       if (! target_big_endian)
7117         offset_expr.X_add_number += off;
7118       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7119                    (int) BFD_RELOC_LO16, breg);
7120       if (! target_big_endian)
7121         offset_expr.X_add_number -= off;
7122       else
7123         offset_expr.X_add_number += off;
7124       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7125                    (int) BFD_RELOC_LO16, breg);
7126       return;
7127
7128     case M_ULD_A:
7129       s = "ldl";
7130       s2 = "ldr";
7131       off = 7;
7132       goto ulwa;
7133     case M_ULW_A:
7134       s = "lwl";
7135       s2 = "lwr";
7136       off = 3;
7137     ulwa:
7138       used_at = 1;
7139       load_address (&icnt, AT, &offset_expr, &used_at);
7140       if (breg != 0)
7141         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7142                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7143                      "d,v,t", AT, AT, breg);
7144       if (! target_big_endian)
7145         expr1.X_add_number = off;
7146       else
7147         expr1.X_add_number = 0;
7148       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7149                    (int) BFD_RELOC_LO16, AT);
7150       if (! target_big_endian)
7151         expr1.X_add_number = 0;
7152       else
7153         expr1.X_add_number = off;
7154       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7155                    (int) BFD_RELOC_LO16, AT);
7156       break;
7157
7158     case M_ULH_A:
7159     case M_ULHU_A:
7160       used_at = 1;
7161       load_address (&icnt, AT, &offset_expr, &used_at);
7162       if (breg != 0)
7163         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7164                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7165                      "d,v,t", AT, AT, breg);
7166       if (target_big_endian)
7167         expr1.X_add_number = 0;
7168       macro_build ((char *) NULL, &icnt, &expr1,
7169                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7170                    (int) BFD_RELOC_LO16, AT);
7171       if (target_big_endian)
7172         expr1.X_add_number = 1;
7173       else
7174         expr1.X_add_number = 0;
7175       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7176                    (int) BFD_RELOC_LO16, AT);
7177       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7178                    treg, treg, 8);
7179       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7180                    treg, treg, AT);
7181       break;
7182
7183     case M_USH:
7184       if (offset_expr.X_add_number >= 0x7fff)
7185         as_bad (_("operand overflow"));
7186       if (target_big_endian)
7187         ++offset_expr.X_add_number;
7188       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7189                    (int) BFD_RELOC_LO16, breg);
7190       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7191                    AT, treg, 8);
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, "sb", "t,o(b)", AT,
7197                    (int) BFD_RELOC_LO16, breg);
7198       break;
7199
7200     case M_USD:
7201       s = "sdl";
7202       s2 = "sdr";
7203       off = 7;
7204       goto usw;
7205     case M_USW:
7206       s = "swl";
7207       s2 = "swr";
7208       off = 3;
7209     usw:
7210       if (offset_expr.X_add_number >= 0x8000 - off)
7211         as_bad (_("operand overflow"));
7212       if (! target_big_endian)
7213         offset_expr.X_add_number += off;
7214       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7215                    (int) BFD_RELOC_LO16, breg);
7216       if (! target_big_endian)
7217         offset_expr.X_add_number -= off;
7218       else
7219         offset_expr.X_add_number += off;
7220       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7221                    (int) BFD_RELOC_LO16, breg);
7222       return;
7223
7224     case M_USD_A:
7225       s = "sdl";
7226       s2 = "sdr";
7227       off = 7;
7228       goto uswa;
7229     case M_USW_A:
7230       s = "swl";
7231       s2 = "swr";
7232       off = 3;
7233     uswa:
7234       used_at = 1;
7235       load_address (&icnt, AT, &offset_expr, &used_at);
7236       if (breg != 0)
7237         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7238                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7239                      "d,v,t", AT, AT, breg);
7240       if (! target_big_endian)
7241         expr1.X_add_number = off;
7242       else
7243         expr1.X_add_number = 0;
7244       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7245                    (int) BFD_RELOC_LO16, AT);
7246       if (! target_big_endian)
7247         expr1.X_add_number = 0;
7248       else
7249         expr1.X_add_number = off;
7250       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7251                    (int) BFD_RELOC_LO16, AT);
7252       break;
7253
7254     case M_USH_A:
7255       used_at = 1;
7256       load_address (&icnt, AT, &offset_expr, &used_at);
7257       if (breg != 0)
7258         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7259                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7260                      "d,v,t", AT, AT, breg);
7261       if (! target_big_endian)
7262         expr1.X_add_number = 0;
7263       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7264                    (int) BFD_RELOC_LO16, AT);
7265       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7266                    treg, treg, 8);
7267       if (! target_big_endian)
7268         expr1.X_add_number = 1;
7269       else
7270         expr1.X_add_number = 0;
7271       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7272                    (int) BFD_RELOC_LO16, AT);
7273       if (! target_big_endian)
7274         expr1.X_add_number = 0;
7275       else
7276         expr1.X_add_number = 1;
7277       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7278                    (int) BFD_RELOC_LO16, AT);
7279       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7280                    treg, treg, 8);
7281       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7282                    treg, treg, AT);
7283       break;
7284
7285     default:
7286       /* FIXME: Check if this is one of the itbl macros, since they
7287          are added dynamically.  */
7288       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7289       break;
7290     }
7291   if (mips_opts.noat)
7292     as_warn (_("Macro used $at after \".set noat\""));
7293 }
7294
7295 /* Implement macros in mips16 mode.  */
7296
7297 static void
7298 mips16_macro (ip)
7299      struct mips_cl_insn *ip;
7300 {
7301   int mask;
7302   int xreg, yreg, zreg, tmp;
7303   int icnt;
7304   expressionS expr1;
7305   int dbl;
7306   const char *s, *s2, *s3;
7307
7308   mask = ip->insn_mo->mask;
7309
7310   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7311   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7312   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7313
7314   icnt = 0;
7315
7316   expr1.X_op = O_constant;
7317   expr1.X_op_symbol = NULL;
7318   expr1.X_add_symbol = NULL;
7319   expr1.X_add_number = 1;
7320
7321   dbl = 0;
7322
7323   switch (mask)
7324     {
7325     default:
7326       internalError ();
7327
7328     case M_DDIV_3:
7329       dbl = 1;
7330     case M_DIV_3:
7331       s = "mflo";
7332       goto do_div3;
7333     case M_DREM_3:
7334       dbl = 1;
7335     case M_REM_3:
7336       s = "mfhi";
7337     do_div3:
7338       mips_emit_delays (true);
7339       ++mips_opts.noreorder;
7340       mips_any_noreorder = 1;
7341       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7342                    dbl ? "ddiv" : "div",
7343                    "0,x,y", xreg, yreg);
7344       expr1.X_add_number = 2;
7345       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7346       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7347                    7);
7348
7349       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7350          since that causes an overflow.  We should do that as well,
7351          but I don't see how to do the comparisons without a temporary
7352          register.  */
7353       --mips_opts.noreorder;
7354       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7355       break;
7356
7357     case M_DIVU_3:
7358       s = "divu";
7359       s2 = "mflo";
7360       goto do_divu3;
7361     case M_REMU_3:
7362       s = "divu";
7363       s2 = "mfhi";
7364       goto do_divu3;
7365     case M_DDIVU_3:
7366       s = "ddivu";
7367       s2 = "mflo";
7368       goto do_divu3;
7369     case M_DREMU_3:
7370       s = "ddivu";
7371       s2 = "mfhi";
7372     do_divu3:
7373       mips_emit_delays (true);
7374       ++mips_opts.noreorder;
7375       mips_any_noreorder = 1;
7376       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7377                    xreg, yreg);
7378       expr1.X_add_number = 2;
7379       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7380       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7381                    "6", 7);
7382       --mips_opts.noreorder;
7383       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7384       break;
7385
7386     case M_DMUL:
7387       dbl = 1;
7388     case M_MUL:
7389       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7390                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7391       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7392                    zreg);
7393       return;
7394
7395     case M_DSUBU_I:
7396       dbl = 1;
7397       goto do_subu;
7398     case M_SUBU_I:
7399     do_subu:
7400       if (imm_expr.X_op != O_constant)
7401         as_bad (_("Unsupported large constant"));
7402       imm_expr.X_add_number = -imm_expr.X_add_number;
7403       macro_build ((char *) NULL, &icnt, &imm_expr,
7404                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7405       break;
7406
7407     case M_SUBU_I_2:
7408       if (imm_expr.X_op != O_constant)
7409         as_bad (_("Unsupported large constant"));
7410       imm_expr.X_add_number = -imm_expr.X_add_number;
7411       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7412                    "x,k", xreg);
7413       break;
7414
7415     case M_DSUBU_I_2:
7416       if (imm_expr.X_op != O_constant)
7417         as_bad (_("Unsupported large constant"));
7418       imm_expr.X_add_number = -imm_expr.X_add_number;
7419       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7420                    "y,j", yreg);
7421       break;
7422
7423     case M_BEQ:
7424       s = "cmp";
7425       s2 = "bteqz";
7426       goto do_branch;
7427     case M_BNE:
7428       s = "cmp";
7429       s2 = "btnez";
7430       goto do_branch;
7431     case M_BLT:
7432       s = "slt";
7433       s2 = "btnez";
7434       goto do_branch;
7435     case M_BLTU:
7436       s = "sltu";
7437       s2 = "btnez";
7438       goto do_branch;
7439     case M_BLE:
7440       s = "slt";
7441       s2 = "bteqz";
7442       goto do_reverse_branch;
7443     case M_BLEU:
7444       s = "sltu";
7445       s2 = "bteqz";
7446       goto do_reverse_branch;
7447     case M_BGE:
7448       s = "slt";
7449       s2 = "bteqz";
7450       goto do_branch;
7451     case M_BGEU:
7452       s = "sltu";
7453       s2 = "bteqz";
7454       goto do_branch;
7455     case M_BGT:
7456       s = "slt";
7457       s2 = "btnez";
7458       goto do_reverse_branch;
7459     case M_BGTU:
7460       s = "sltu";
7461       s2 = "btnez";
7462
7463     do_reverse_branch:
7464       tmp = xreg;
7465       xreg = yreg;
7466       yreg = tmp;
7467
7468     do_branch:
7469       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7470                    xreg, yreg);
7471       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7472       break;
7473
7474     case M_BEQ_I:
7475       s = "cmpi";
7476       s2 = "bteqz";
7477       s3 = "x,U";
7478       goto do_branch_i;
7479     case M_BNE_I:
7480       s = "cmpi";
7481       s2 = "btnez";
7482       s3 = "x,U";
7483       goto do_branch_i;
7484     case M_BLT_I:
7485       s = "slti";
7486       s2 = "btnez";
7487       s3 = "x,8";
7488       goto do_branch_i;
7489     case M_BLTU_I:
7490       s = "sltiu";
7491       s2 = "btnez";
7492       s3 = "x,8";
7493       goto do_branch_i;
7494     case M_BLE_I:
7495       s = "slti";
7496       s2 = "btnez";
7497       s3 = "x,8";
7498       goto do_addone_branch_i;
7499     case M_BLEU_I:
7500       s = "sltiu";
7501       s2 = "btnez";
7502       s3 = "x,8";
7503       goto do_addone_branch_i;
7504     case M_BGE_I:
7505       s = "slti";
7506       s2 = "bteqz";
7507       s3 = "x,8";
7508       goto do_branch_i;
7509     case M_BGEU_I:
7510       s = "sltiu";
7511       s2 = "bteqz";
7512       s3 = "x,8";
7513       goto do_branch_i;
7514     case M_BGT_I:
7515       s = "slti";
7516       s2 = "bteqz";
7517       s3 = "x,8";
7518       goto do_addone_branch_i;
7519     case M_BGTU_I:
7520       s = "sltiu";
7521       s2 = "bteqz";
7522       s3 = "x,8";
7523
7524     do_addone_branch_i:
7525       if (imm_expr.X_op != O_constant)
7526         as_bad (_("Unsupported large constant"));
7527       ++imm_expr.X_add_number;
7528
7529     do_branch_i:
7530       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7531       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7532       break;
7533
7534     case M_ABS:
7535       expr1.X_add_number = 0;
7536       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7537       if (xreg != yreg)
7538         move_register (&icnt, xreg, yreg);
7539       expr1.X_add_number = 2;
7540       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7541       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7542                    "neg", "x,w", xreg, xreg);
7543     }
7544 }
7545
7546 /* For consistency checking, verify that all bits are specified either
7547    by the match/mask part of the instruction definition, or by the
7548    operand list.  */
7549 static int
7550 validate_mips_insn (opc)
7551      const struct mips_opcode *opc;
7552 {
7553   const char *p = opc->args;
7554   char c;
7555   unsigned long used_bits = opc->mask;
7556
7557   if ((used_bits & opc->match) != opc->match)
7558     {
7559       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7560               opc->name, opc->args);
7561       return 0;
7562     }
7563 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7564   while (*p)
7565     switch (c = *p++)
7566       {
7567       case ',': break;
7568       case '(': break;
7569       case ')': break;
7570       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7571       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7572       case 'A': break;
7573       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7574       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7575       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7576       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7577       case 'F': break;
7578       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7579       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7580       case 'I': break;
7581       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7582       case 'L': break;
7583       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7584       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7585       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7586       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7587                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7588       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7589       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7590       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7591       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7592       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7593       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7594       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7595       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7596       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7597       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7598       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7599       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7600       case 'f': break;
7601       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7602       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7603       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7604       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7605       case 'l': break;
7606       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7607       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7608       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7609       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7610       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7611       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7612       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7613       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7614       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7615       case 'x': break;
7616       case 'z': break;
7617       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7618       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7619                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7620       default:
7621         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7622                 c, opc->name, opc->args);
7623         return 0;
7624       }
7625 #undef USE_BITS
7626   if (used_bits != 0xffffffff)
7627     {
7628       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7629               ~used_bits & 0xffffffff, opc->name, opc->args);
7630       return 0;
7631     }
7632   return 1;
7633 }
7634
7635 /* This routine assembles an instruction into its binary format.  As a
7636    side effect, it sets one of the global variables imm_reloc or
7637    offset_reloc to the type of relocation to do if one of the operands
7638    is an address expression.  */
7639
7640 static void
7641 mips_ip (str, ip)
7642      char *str;
7643      struct mips_cl_insn *ip;
7644 {
7645   char *s;
7646   const char *args;
7647   char c = 0;
7648   struct mips_opcode *insn;
7649   char *argsStart;
7650   unsigned int regno;
7651   unsigned int lastregno = 0;
7652   char *s_reset;
7653   char save_c = 0;
7654
7655   insn_error = NULL;
7656
7657   /* If the instruction contains a '.', we first try to match an instruction
7658      including the '.'.  Then we try again without the '.'.  */
7659   insn = NULL;
7660   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7661     continue;
7662
7663   /* If we stopped on whitespace, then replace the whitespace with null for
7664      the call to hash_find.  Save the character we replaced just in case we
7665      have to re-parse the instruction.  */
7666   if (ISSPACE (*s))
7667     {
7668       save_c = *s;
7669       *s++ = '\0';
7670     }
7671
7672   insn = (struct mips_opcode *) hash_find (op_hash, str);
7673
7674   /* If we didn't find the instruction in the opcode table, try again, but
7675      this time with just the instruction up to, but not including the
7676      first '.'.  */
7677   if (insn == NULL)
7678     {
7679       /* Restore the character we overwrite above (if any).  */
7680       if (save_c)
7681         *(--s) = save_c;
7682
7683       /* Scan up to the first '.' or whitespace.  */
7684       for (s = str;
7685            *s != '\0' && *s != '.' && !ISSPACE (*s);
7686            ++s)
7687         continue;
7688
7689       /* If we did not find a '.', then we can quit now.  */
7690       if (*s != '.')
7691         {
7692           insn_error = "unrecognized opcode";
7693           return;
7694         }
7695
7696       /* Lookup the instruction in the hash table.  */
7697       *s++ = '\0';
7698       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7699         {
7700           insn_error = "unrecognized opcode";
7701           return;
7702         }
7703     }
7704
7705   argsStart = s;
7706   for (;;)
7707     {
7708       boolean ok;
7709
7710       assert (strcmp (insn->name, str) == 0);
7711
7712       if (OPCODE_IS_MEMBER (insn,
7713                             (mips_opts.isa
7714                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7715                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7716                             mips_arch))
7717         ok = true;
7718       else
7719         ok = false;
7720
7721       if (insn->pinfo != INSN_MACRO)
7722         {
7723           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7724             ok = false;
7725         }
7726
7727       if (! ok)
7728         {
7729           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7730               && strcmp (insn->name, insn[1].name) == 0)
7731             {
7732               ++insn;
7733               continue;
7734             }
7735           else
7736             {
7737               if (!insn_error)
7738                 {
7739                   static char buf[100];
7740                   sprintf (buf,
7741                            _("opcode not supported on this processor: %s (%s)"),
7742                            mips_cpu_to_str (mips_arch),
7743                            mips_isa_to_str (mips_opts.isa));
7744
7745                   insn_error = buf;
7746                 }
7747               if (save_c)
7748                 *(--s) = save_c;
7749               return;
7750             }
7751         }
7752
7753       ip->insn_mo = insn;
7754       ip->insn_opcode = insn->match;
7755       insn_error = NULL;
7756       for (args = insn->args;; ++args)
7757         {
7758           int is_mdmx;
7759
7760           s += strspn (s, " \t");
7761           is_mdmx = 0;
7762           switch (*args)
7763             {
7764             case '\0':          /* end of args */
7765               if (*s == '\0')
7766                 return;
7767               break;
7768
7769             case ',':
7770               if (*s++ == *args)
7771                 continue;
7772               s--;
7773               switch (*++args)
7774                 {
7775                 case 'r':
7776                 case 'v':
7777                   ip->insn_opcode |= lastregno << OP_SH_RS;
7778                   continue;
7779
7780                 case 'w':
7781                   ip->insn_opcode |= lastregno << OP_SH_RT;
7782                   continue;
7783
7784                 case 'W':
7785                   ip->insn_opcode |= lastregno << OP_SH_FT;
7786                   continue;
7787
7788                 case 'V':
7789                   ip->insn_opcode |= lastregno << OP_SH_FS;
7790                   continue;
7791                 }
7792               break;
7793
7794             case '(':
7795               /* Handle optional base register.
7796                  Either the base register is omitted or
7797                  we must have a left paren.  */
7798               /* This is dependent on the next operand specifier
7799                  is a base register specification.  */
7800               assert (args[1] == 'b' || args[1] == '5'
7801                       || args[1] == '-' || args[1] == '4');
7802               if (*s == '\0')
7803                 return;
7804
7805             case ')':           /* these must match exactly */
7806               if (*s++ == *args)
7807                 continue;
7808               break;
7809
7810             case '<':           /* must be at least one digit */
7811               /*
7812                * According to the manual, if the shift amount is greater
7813                * than 31 or less than 0, then the shift amount should be
7814                * mod 32.  In reality the mips assembler issues an error.
7815                * We issue a warning and mask out all but the low 5 bits.
7816                */
7817               my_getExpression (&imm_expr, s);
7818               check_absolute_expr (ip, &imm_expr);
7819               if ((unsigned long) imm_expr.X_add_number > 31)
7820                 {
7821                   as_warn (_("Improper shift amount (%lu)"),
7822                            (unsigned long) imm_expr.X_add_number);
7823                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7824                 }
7825               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7826               imm_expr.X_op = O_absent;
7827               s = expr_end;
7828               continue;
7829
7830             case '>':           /* shift amount minus 32 */
7831               my_getExpression (&imm_expr, s);
7832               check_absolute_expr (ip, &imm_expr);
7833               if ((unsigned long) imm_expr.X_add_number < 32
7834                   || (unsigned long) imm_expr.X_add_number > 63)
7835                 break;
7836               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7837               imm_expr.X_op = O_absent;
7838               s = expr_end;
7839               continue;
7840
7841             case 'k':           /* cache code */
7842             case 'h':           /* prefx code */
7843               my_getExpression (&imm_expr, s);
7844               check_absolute_expr (ip, &imm_expr);
7845               if ((unsigned long) imm_expr.X_add_number > 31)
7846                 {
7847                   as_warn (_("Invalid value for `%s' (%lu)"),
7848                            ip->insn_mo->name,
7849                            (unsigned long) imm_expr.X_add_number);
7850                   imm_expr.X_add_number &= 0x1f;
7851                 }
7852               if (*args == 'k')
7853                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7854               else
7855                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7856               imm_expr.X_op = O_absent;
7857               s = expr_end;
7858               continue;
7859
7860             case 'c':           /* break code */
7861               my_getExpression (&imm_expr, s);
7862               check_absolute_expr (ip, &imm_expr);
7863               if ((unsigned long) imm_expr.X_add_number > 1023)
7864                 {
7865                   as_warn (_("Illegal break code (%lu)"),
7866                            (unsigned long) imm_expr.X_add_number);
7867                   imm_expr.X_add_number &= OP_MASK_CODE;
7868                 }
7869               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7870               imm_expr.X_op = O_absent;
7871               s = expr_end;
7872               continue;
7873
7874             case 'q':           /* lower break code */
7875               my_getExpression (&imm_expr, s);
7876               check_absolute_expr (ip, &imm_expr);
7877               if ((unsigned long) imm_expr.X_add_number > 1023)
7878                 {
7879                   as_warn (_("Illegal lower break code (%lu)"),
7880                            (unsigned long) imm_expr.X_add_number);
7881                   imm_expr.X_add_number &= OP_MASK_CODE2;
7882                 }
7883               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7884               imm_expr.X_op = O_absent;
7885               s = expr_end;
7886               continue;
7887
7888             case 'B':           /* 20-bit syscall/break code.  */
7889               my_getExpression (&imm_expr, s);
7890               check_absolute_expr (ip, &imm_expr);
7891               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7892                 as_warn (_("Illegal 20-bit code (%lu)"),
7893                          (unsigned long) imm_expr.X_add_number);
7894               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7895               imm_expr.X_op = O_absent;
7896               s = expr_end;
7897               continue;
7898
7899             case 'C':           /* Coprocessor code */
7900               my_getExpression (&imm_expr, s);
7901               check_absolute_expr (ip, &imm_expr);
7902               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
7903                 {
7904                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
7905                            (unsigned long) imm_expr.X_add_number);
7906                   imm_expr.X_add_number &= ((1 << 25) - 1);
7907                 }
7908               ip->insn_opcode |= imm_expr.X_add_number;
7909               imm_expr.X_op = O_absent;
7910               s = expr_end;
7911               continue;
7912
7913             case 'J':           /* 19-bit wait code.  */
7914               my_getExpression (&imm_expr, s);
7915               check_absolute_expr (ip, &imm_expr);
7916               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7917                 as_warn (_("Illegal 19-bit code (%lu)"),
7918                          (unsigned long) imm_expr.X_add_number);
7919               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7920               imm_expr.X_op = O_absent;
7921               s = expr_end;
7922               continue;
7923
7924             case 'P':           /* Performance register */
7925               my_getExpression (&imm_expr, s);
7926               check_absolute_expr (ip, &imm_expr);
7927               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7928                 {
7929                   as_warn (_("Invalid performance register (%lu)"),
7930                            (unsigned long) imm_expr.X_add_number);
7931                   imm_expr.X_add_number &= OP_MASK_PERFREG;
7932                 }
7933               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7934               imm_expr.X_op = O_absent;
7935               s = expr_end;
7936               continue;
7937
7938             case 'b':           /* base register */
7939             case 'd':           /* destination register */
7940             case 's':           /* source register */
7941             case 't':           /* target register */
7942             case 'r':           /* both target and source */
7943             case 'v':           /* both dest and source */
7944             case 'w':           /* both dest and target */
7945             case 'E':           /* coprocessor target register */
7946             case 'G':           /* coprocessor destination register */
7947             case 'x':           /* ignore register name */
7948             case 'z':           /* must be zero register */
7949             case 'U':           /* destination register (clo/clz).  */
7950               s_reset = s;
7951               if (s[0] == '$')
7952                 {
7953
7954                   if (ISDIGIT (s[1]))
7955                     {
7956                       ++s;
7957                       regno = 0;
7958                       do
7959                         {
7960                           regno *= 10;
7961                           regno += *s - '0';
7962                           ++s;
7963                         }
7964                       while (ISDIGIT (*s));
7965                       if (regno > 31)
7966                         as_bad (_("Invalid register number (%d)"), regno);
7967                     }
7968                   else if (*args == 'E' || *args == 'G')
7969                     goto notreg;
7970                   else
7971                     {
7972                       if (s[1] == 'r' && s[2] == 'a')
7973                         {
7974                           s += 3;
7975                           regno = RA;
7976                         }
7977                       else if (s[1] == 'f' && s[2] == 'p')
7978                         {
7979                           s += 3;
7980                           regno = FP;
7981                         }
7982                       else if (s[1] == 's' && s[2] == 'p')
7983                         {
7984                           s += 3;
7985                           regno = SP;
7986                         }
7987                       else if (s[1] == 'g' && s[2] == 'p')
7988                         {
7989                           s += 3;
7990                           regno = GP;
7991                         }
7992                       else if (s[1] == 'a' && s[2] == 't')
7993                         {
7994                           s += 3;
7995                           regno = AT;
7996                         }
7997                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7998                         {
7999                           s += 4;
8000                           regno = KT0;
8001                         }
8002                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8003                         {
8004                           s += 4;
8005                           regno = KT1;
8006                         }
8007                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8008                         {
8009                           s += 5;
8010                           regno = ZERO;
8011                         }
8012                       else if (itbl_have_entries)
8013                         {
8014                           char *p, *n;
8015                           unsigned long r;
8016
8017                           p = s + 1;    /* advance past '$' */
8018                           n = itbl_get_field (&p);  /* n is name */
8019
8020                           /* See if this is a register defined in an
8021                              itbl entry.  */
8022                           if (itbl_get_reg_val (n, &r))
8023                             {
8024                               /* Get_field advances to the start of
8025                                  the next field, so we need to back
8026                                  rack to the end of the last field.  */
8027                               if (p)
8028                                 s = p - 1;
8029                               else
8030                                 s = strchr (s, '\0');
8031                               regno = r;
8032                             }
8033                           else
8034                             goto notreg;
8035                         }
8036                       else
8037                         goto notreg;
8038                     }
8039                   if (regno == AT
8040                       && ! mips_opts.noat
8041                       && *args != 'E'
8042                       && *args != 'G')
8043                     as_warn (_("Used $at without \".set noat\""));
8044                   c = *args;
8045                   if (*s == ' ')
8046                     ++s;
8047                   if (args[1] != *s)
8048                     {
8049                       if (c == 'r' || c == 'v' || c == 'w')
8050                         {
8051                           regno = lastregno;
8052                           s = s_reset;
8053                           ++args;
8054                         }
8055                     }
8056                   /* 'z' only matches $0.  */
8057                   if (c == 'z' && regno != 0)
8058                     break;
8059
8060         /* Now that we have assembled one operand, we use the args string
8061          * to figure out where it goes in the instruction.  */
8062                   switch (c)
8063                     {
8064                     case 'r':
8065                     case 's':
8066                     case 'v':
8067                     case 'b':
8068                       ip->insn_opcode |= regno << OP_SH_RS;
8069                       break;
8070                     case 'd':
8071                     case 'G':
8072                       ip->insn_opcode |= regno << OP_SH_RD;
8073                       break;
8074                     case 'U':
8075                       ip->insn_opcode |= regno << OP_SH_RD;
8076                       ip->insn_opcode |= regno << OP_SH_RT;
8077                       break;
8078                     case 'w':
8079                     case 't':
8080                     case 'E':
8081                       ip->insn_opcode |= regno << OP_SH_RT;
8082                       break;
8083                     case 'x':
8084                       /* This case exists because on the r3000 trunc
8085                          expands into a macro which requires a gp
8086                          register.  On the r6000 or r4000 it is
8087                          assembled into a single instruction which
8088                          ignores the register.  Thus the insn version
8089                          is MIPS_ISA2 and uses 'x', and the macro
8090                          version is MIPS_ISA1 and uses 't'.  */
8091                       break;
8092                     case 'z':
8093                       /* This case is for the div instruction, which
8094                          acts differently if the destination argument
8095                          is $0.  This only matches $0, and is checked
8096                          outside the switch.  */
8097                       break;
8098                     case 'D':
8099                       /* Itbl operand; not yet implemented. FIXME ?? */
8100                       break;
8101                       /* What about all other operands like 'i', which
8102                          can be specified in the opcode table? */
8103                     }
8104                   lastregno = regno;
8105                   continue;
8106                 }
8107             notreg:
8108               switch (*args++)
8109                 {
8110                 case 'r':
8111                 case 'v':
8112                   ip->insn_opcode |= lastregno << OP_SH_RS;
8113                   continue;
8114                 case 'w':
8115                   ip->insn_opcode |= lastregno << OP_SH_RT;
8116                   continue;
8117                 }
8118               break;
8119
8120             case 'O':           /* MDMX alignment immediate constant.  */
8121               my_getExpression (&imm_expr, s);
8122               check_absolute_expr (ip, &imm_expr);
8123               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8124                 {
8125                   as_warn ("Improper align amount (%ld), using low bits",
8126                            (long) imm_expr.X_add_number);
8127                   imm_expr.X_add_number &= OP_MASK_ALN;
8128                 }
8129               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8130               imm_expr.X_op = O_absent;
8131               s = expr_end;
8132               continue;
8133
8134             case 'Q':           /* MDMX vector, element sel, or const.  */
8135               if (s[0] != '$')
8136                 {
8137                   /* MDMX Immediate.  */
8138                   my_getExpression (&imm_expr, s);
8139                   check_absolute_expr (ip, &imm_expr);
8140                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8141                     {
8142                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8143                                (long) imm_expr.X_add_number);
8144                       imm_expr.X_add_number &= OP_MASK_FT;
8145                     }
8146                   imm_expr.X_add_number &= OP_MASK_FT;
8147                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8148                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8149                   else
8150                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8151                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8152                   imm_expr.X_op = O_absent;
8153                   s = expr_end;
8154                   continue;
8155                 }
8156               /* Not MDMX Immediate.  Fall through.  */
8157             case 'X':           /* MDMX destination register.  */
8158             case 'Y':           /* MDMX source register.  */
8159             case 'Z':           /* MDMX target register.  */
8160               is_mdmx = 1;
8161             case 'D':           /* floating point destination register */
8162             case 'S':           /* floating point source register */
8163             case 'T':           /* floating point target register */
8164             case 'R':           /* floating point source register */
8165             case 'V':
8166             case 'W':
8167               s_reset = s;
8168               /* Accept $fN for FP and MDMX register numbers, and in
8169                  addition accept $vN for MDMX register numbers.  */
8170               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8171                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8172                       && ISDIGIT (s[2])))
8173                 {
8174                   s += 2;
8175                   regno = 0;
8176                   do
8177                     {
8178                       regno *= 10;
8179                       regno += *s - '0';
8180                       ++s;
8181                     }
8182                   while (ISDIGIT (*s));
8183
8184                   if (regno > 31)
8185                     as_bad (_("Invalid float register number (%d)"), regno);
8186
8187                   if ((regno & 1) != 0
8188                       && HAVE_32BIT_FPRS
8189                       && ! (strcmp (str, "mtc1") == 0
8190                             || strcmp (str, "mfc1") == 0
8191                             || strcmp (str, "lwc1") == 0
8192                             || strcmp (str, "swc1") == 0
8193                             || strcmp (str, "l.s") == 0
8194                             || strcmp (str, "s.s") == 0))
8195                     as_warn (_("Float register should be even, was %d"),
8196                              regno);
8197
8198                   c = *args;
8199                   if (*s == ' ')
8200                     ++s;
8201                   if (args[1] != *s)
8202                     {
8203                       if (c == 'V' || c == 'W')
8204                         {
8205                           regno = lastregno;
8206                           s = s_reset;
8207                           ++args;
8208                         }
8209                     }
8210                   switch (c)
8211                     {
8212                     case 'D':
8213                     case 'X':
8214                       ip->insn_opcode |= regno << OP_SH_FD;
8215                       break;
8216                     case 'V':
8217                     case 'S':
8218                     case 'Y':
8219                       ip->insn_opcode |= regno << OP_SH_FS;
8220                       break;
8221                     case 'Q':
8222                       /* This is like 'Z', but also needs to fix the MDMX
8223                          vector/scalar select bits.  Note that the
8224                          scalar immediate case is handled above.  */
8225                       if (*s == '[')
8226                         {
8227                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8228                           int max_el = (is_qh ? 3 : 7);
8229                           s++;
8230                           my_getExpression(&imm_expr, s);
8231                           check_absolute_expr (ip, &imm_expr);
8232                           s = expr_end;
8233                           if (imm_expr.X_add_number > max_el)
8234                             as_bad(_("Bad element selector %ld"),
8235                                    (long) imm_expr.X_add_number);
8236                           imm_expr.X_add_number &= max_el;
8237                           ip->insn_opcode |= (imm_expr.X_add_number
8238                                               << (OP_SH_VSEL +
8239                                                   (is_qh ? 2 : 1)));
8240                           if (*s != ']')
8241                             as_warn(_("Expecting ']' found '%s'"), s);
8242                           else
8243                             s++;
8244                         }
8245                       else
8246                         {
8247                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8248                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8249                                                 << OP_SH_VSEL);
8250                           else
8251                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8252                                                 OP_SH_VSEL);
8253                         }
8254                       /* Fall through */
8255                     case 'W':
8256                     case 'T':
8257                     case 'Z':
8258                       ip->insn_opcode |= regno << OP_SH_FT;
8259                       break;
8260                     case 'R':
8261                       ip->insn_opcode |= regno << OP_SH_FR;
8262                       break;
8263                     }
8264                   lastregno = regno;
8265                   continue;
8266                 }
8267
8268               switch (*args++)
8269                 {
8270                 case 'V':
8271                   ip->insn_opcode |= lastregno << OP_SH_FS;
8272                   continue;
8273                 case 'W':
8274                   ip->insn_opcode |= lastregno << OP_SH_FT;
8275                   continue;
8276                 }
8277               break;
8278
8279             case 'I':
8280               my_getExpression (&imm_expr, s);
8281               if (imm_expr.X_op != O_big
8282                   && imm_expr.X_op != O_constant)
8283                 insn_error = _("absolute expression required");
8284               s = expr_end;
8285               continue;
8286
8287             case 'A':
8288               my_getExpression (&offset_expr, s);
8289               *imm_reloc = BFD_RELOC_32;
8290               s = expr_end;
8291               continue;
8292
8293             case 'F':
8294             case 'L':
8295             case 'f':
8296             case 'l':
8297               {
8298                 int f64;
8299                 int using_gprs;
8300                 char *save_in;
8301                 char *err;
8302                 unsigned char temp[8];
8303                 int len;
8304                 unsigned int length;
8305                 segT seg;
8306                 subsegT subseg;
8307                 char *p;
8308
8309                 /* These only appear as the last operand in an
8310                    instruction, and every instruction that accepts
8311                    them in any variant accepts them in all variants.
8312                    This means we don't have to worry about backing out
8313                    any changes if the instruction does not match.
8314
8315                    The difference between them is the size of the
8316                    floating point constant and where it goes.  For 'F'
8317                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8318                    is 32 bits.  Where the constant is placed is based
8319                    on how the MIPS assembler does things:
8320                     F -- .rdata
8321                     L -- .lit8
8322                     f -- immediate value
8323                     l -- .lit4
8324
8325                     The .lit4 and .lit8 sections are only used if
8326                     permitted by the -G argument.
8327
8328                     When generating embedded PIC code, we use the
8329                     .lit8 section but not the .lit4 section (we can do
8330                     .lit4 inline easily; we need to put .lit8
8331                     somewhere in the data segment, and using .lit8
8332                     permits the linker to eventually combine identical
8333                     .lit8 entries).
8334
8335                     The code below needs to know whether the target register
8336                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8337                     'F' are used with GPR-based instructions and 'l' and
8338                     'L' are used with FPR-based instructions.  */
8339
8340                 f64 = *args == 'F' || *args == 'L';
8341                 using_gprs = *args == 'F' || *args == 'f';
8342
8343                 save_in = input_line_pointer;
8344                 input_line_pointer = s;
8345                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8346                 length = len;
8347                 s = input_line_pointer;
8348                 input_line_pointer = save_in;
8349                 if (err != NULL && *err != '\0')
8350                   {
8351                     as_bad (_("Bad floating point constant: %s"), err);
8352                     memset (temp, '\0', sizeof temp);
8353                     length = f64 ? 8 : 4;
8354                   }
8355
8356                 assert (length == (unsigned) (f64 ? 8 : 4));
8357
8358                 if (*args == 'f'
8359                     || (*args == 'l'
8360                         && (! USE_GLOBAL_POINTER_OPT
8361                             || mips_pic == EMBEDDED_PIC
8362                             || g_switch_value < 4
8363                             || (temp[0] == 0 && temp[1] == 0)
8364                             || (temp[2] == 0 && temp[3] == 0))))
8365                   {
8366                     imm_expr.X_op = O_constant;
8367                     if (! target_big_endian)
8368                       imm_expr.X_add_number = bfd_getl32 (temp);
8369                     else
8370                       imm_expr.X_add_number = bfd_getb32 (temp);
8371                   }
8372                 else if (length > 4
8373                          && ! mips_disable_float_construction
8374                          /* Constants can only be constructed in GPRs and
8375                             copied to FPRs if the GPRs are at least as wide
8376                             as the FPRs.  Force the constant into memory if
8377                             we are using 64-bit FPRs but the GPRs are only
8378                             32 bits wide.  */
8379                          && (using_gprs
8380                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8381                          && ((temp[0] == 0 && temp[1] == 0)
8382                              || (temp[2] == 0 && temp[3] == 0))
8383                          && ((temp[4] == 0 && temp[5] == 0)
8384                              || (temp[6] == 0 && temp[7] == 0)))
8385                   {
8386                     /* The value is simple enough to load with a couple of
8387                        instructions.  If using 32-bit registers, set
8388                        imm_expr to the high order 32 bits and offset_expr to
8389                        the low order 32 bits.  Otherwise, set imm_expr to
8390                        the entire 64 bit constant.  */
8391                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8392                       {
8393                         imm_expr.X_op = O_constant;
8394                         offset_expr.X_op = O_constant;
8395                         if (! target_big_endian)
8396                           {
8397                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8398                             offset_expr.X_add_number = bfd_getl32 (temp);
8399                           }
8400                         else
8401                           {
8402                             imm_expr.X_add_number = bfd_getb32 (temp);
8403                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8404                           }
8405                         if (offset_expr.X_add_number == 0)
8406                           offset_expr.X_op = O_absent;
8407                       }
8408                     else if (sizeof (imm_expr.X_add_number) > 4)
8409                       {
8410                         imm_expr.X_op = O_constant;
8411                         if (! target_big_endian)
8412                           imm_expr.X_add_number = bfd_getl64 (temp);
8413                         else
8414                           imm_expr.X_add_number = bfd_getb64 (temp);
8415                       }
8416                     else
8417                       {
8418                         imm_expr.X_op = O_big;
8419                         imm_expr.X_add_number = 4;
8420                         if (! target_big_endian)
8421                           {
8422                             generic_bignum[0] = bfd_getl16 (temp);
8423                             generic_bignum[1] = bfd_getl16 (temp + 2);
8424                             generic_bignum[2] = bfd_getl16 (temp + 4);
8425                             generic_bignum[3] = bfd_getl16 (temp + 6);
8426                           }
8427                         else
8428                           {
8429                             generic_bignum[0] = bfd_getb16 (temp + 6);
8430                             generic_bignum[1] = bfd_getb16 (temp + 4);
8431                             generic_bignum[2] = bfd_getb16 (temp + 2);
8432                             generic_bignum[3] = bfd_getb16 (temp);
8433                           }
8434                       }
8435                   }
8436                 else
8437                   {
8438                     const char *newname;
8439                     segT new_seg;
8440
8441                     /* Switch to the right section.  */
8442                     seg = now_seg;
8443                     subseg = now_subseg;
8444                     switch (*args)
8445                       {
8446                       default: /* unused default case avoids warnings.  */
8447                       case 'L':
8448                         newname = RDATA_SECTION_NAME;
8449                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8450                             || mips_pic == EMBEDDED_PIC)
8451                           newname = ".lit8";
8452                         break;
8453                       case 'F':
8454                         if (mips_pic == EMBEDDED_PIC)
8455                           newname = ".lit8";
8456                         else
8457                           newname = RDATA_SECTION_NAME;
8458                         break;
8459                       case 'l':
8460                         assert (!USE_GLOBAL_POINTER_OPT
8461                                 || g_switch_value >= 4);
8462                         newname = ".lit4";
8463                         break;
8464                       }
8465                     new_seg = subseg_new (newname, (subsegT) 0);
8466                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8467                       bfd_set_section_flags (stdoutput, new_seg,
8468                                              (SEC_ALLOC
8469                                               | SEC_LOAD
8470                                               | SEC_READONLY
8471                                               | SEC_DATA));
8472                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8473                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8474                         && strcmp (TARGET_OS, "elf") != 0)
8475                       record_alignment (new_seg, 4);
8476                     else
8477                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8478                     if (seg == now_seg)
8479                       as_bad (_("Can't use floating point insn in this section"));
8480
8481                     /* Set the argument to the current address in the
8482                        section.  */
8483                     offset_expr.X_op = O_symbol;
8484                     offset_expr.X_add_symbol =
8485                       symbol_new ("L0\001", now_seg,
8486                                   (valueT) frag_now_fix (), frag_now);
8487                     offset_expr.X_add_number = 0;
8488
8489                     /* Put the floating point number into the section.  */
8490                     p = frag_more ((int) length);
8491                     memcpy (p, temp, length);
8492
8493                     /* Switch back to the original section.  */
8494                     subseg_set (seg, subseg);
8495                   }
8496               }
8497               continue;
8498
8499             case 'i':           /* 16 bit unsigned immediate */
8500             case 'j':           /* 16 bit signed immediate */
8501               *imm_reloc = BFD_RELOC_LO16;
8502               c = my_getSmallExpression (&imm_expr, s);
8503               if (c != S_EX_NONE)
8504                 {
8505                   if (c != S_EX_LO)
8506                     {
8507                       if (imm_expr.X_op == O_constant)
8508                         imm_expr.X_add_number =
8509                           (imm_expr.X_add_number >> 16) & 0xffff;
8510 #ifdef OBJ_ELF
8511                       else if (c == S_EX_HIGHEST)
8512                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8513                       else if (c == S_EX_HIGHER)
8514                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8515                       else if (c == S_EX_GP_REL)
8516                         {
8517                           /* This occurs in NewABI only.  */
8518                           c = my_getSmallExpression (&imm_expr, s);
8519                           if (c != S_EX_NEG)
8520                             as_bad (_("bad composition of relocations"));
8521                           else
8522                             {
8523                               c = my_getSmallExpression (&imm_expr, s);
8524                               if (c != S_EX_LO)
8525                                 as_bad (_("bad composition of relocations"));
8526                               else
8527                                 {
8528                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8529                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8530                                   imm_reloc[2] = BFD_RELOC_LO16;
8531                                 }
8532                             }
8533                         }
8534 #endif
8535                       else if (c == S_EX_HI)
8536                         {
8537                           *imm_reloc = BFD_RELOC_HI16_S;
8538                           imm_unmatched_hi = true;
8539                         }
8540                       else
8541                         *imm_reloc = BFD_RELOC_HI16;
8542                     }
8543                   else if (imm_expr.X_op == O_constant)
8544                     imm_expr.X_add_number &= 0xffff;
8545                 }
8546               if (*args == 'i')
8547                 {
8548                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8549                       || ((imm_expr.X_add_number < 0
8550                            || imm_expr.X_add_number >= 0x10000)
8551                           && imm_expr.X_op == O_constant))
8552                     {
8553                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8554                           !strcmp (insn->name, insn[1].name))
8555                         break;
8556                       if (imm_expr.X_op == O_constant
8557                           || imm_expr.X_op == O_big)
8558                         as_bad (_("16 bit expression not in range 0..65535"));
8559                     }
8560                 }
8561               else
8562                 {
8563                   int more;
8564                   offsetT max;
8565
8566                   /* The upper bound should be 0x8000, but
8567                      unfortunately the MIPS assembler accepts numbers
8568                      from 0x8000 to 0xffff and sign extends them, and
8569                      we want to be compatible.  We only permit this
8570                      extended range for an instruction which does not
8571                      provide any further alternates, since those
8572                      alternates may handle other cases.  People should
8573                      use the numbers they mean, rather than relying on
8574                      a mysterious sign extension.  */
8575                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8576                           strcmp (insn->name, insn[1].name) == 0);
8577                   if (more)
8578                     max = 0x8000;
8579                   else
8580                     max = 0x10000;
8581                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8582                       || ((imm_expr.X_add_number < -0x8000
8583                            || imm_expr.X_add_number >= max)
8584                           && imm_expr.X_op == O_constant)
8585                       || (more
8586                           && imm_expr.X_add_number < 0
8587                           && HAVE_64BIT_GPRS
8588                           && imm_expr.X_unsigned
8589                           && sizeof (imm_expr.X_add_number) <= 4))
8590                     {
8591                       if (more)
8592                         break;
8593                       if (imm_expr.X_op == O_constant
8594                           || imm_expr.X_op == O_big)
8595                         as_bad (_("16 bit expression not in range -32768..32767"));
8596                     }
8597                 }
8598               s = expr_end;
8599               continue;
8600
8601             case 'o':           /* 16 bit offset */
8602               c = my_getSmallExpression (&offset_expr, s);
8603
8604               /* If this value won't fit into a 16 bit offset, then go
8605                  find a macro that will generate the 32 bit offset
8606                  code pattern.  */
8607               if (c == S_EX_NONE
8608                   && (offset_expr.X_op != O_constant
8609                       || offset_expr.X_add_number >= 0x8000
8610                       || offset_expr.X_add_number < -0x8000))
8611                 break;
8612
8613               if (c == S_EX_HI)
8614                 {
8615                   if (offset_expr.X_op != O_constant)
8616                     break;
8617                   offset_expr.X_add_number =
8618                     (offset_expr.X_add_number >> 16) & 0xffff;
8619                 }
8620               *offset_reloc = BFD_RELOC_LO16;
8621               s = expr_end;
8622               continue;
8623
8624             case 'p':           /* pc relative offset */
8625               if (mips_pic == EMBEDDED_PIC)
8626                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8627               else
8628                 *offset_reloc = BFD_RELOC_16_PCREL;
8629               my_getExpression (&offset_expr, s);
8630               s = expr_end;
8631               continue;
8632
8633             case 'u':           /* upper 16 bits */
8634               c = my_getSmallExpression (&imm_expr, s);
8635               *imm_reloc = BFD_RELOC_LO16;
8636               if (c != S_EX_NONE)
8637                 {
8638                   if (c != S_EX_LO)
8639                     {
8640                       if (imm_expr.X_op == O_constant)
8641                         imm_expr.X_add_number =
8642                           (imm_expr.X_add_number >> 16) & 0xffff;
8643                       else if (c == S_EX_HI)
8644                         {
8645                           *imm_reloc = BFD_RELOC_HI16_S;
8646                           imm_unmatched_hi = true;
8647                         }
8648 #ifdef OBJ_ELF
8649                       else if (c == S_EX_HIGHEST)
8650                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8651                       else if (c == S_EX_GP_REL)
8652                         {
8653                           /* This occurs in NewABI only.  */
8654                           c = my_getSmallExpression (&imm_expr, s);
8655                           if (c != S_EX_NEG)
8656                             as_bad (_("bad composition of relocations"));
8657                           else
8658                             {
8659                               c = my_getSmallExpression (&imm_expr, s);
8660                               if (c != S_EX_HI)
8661                                 as_bad (_("bad composition of relocations"));
8662                               else
8663                                 {
8664                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8665                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8666                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8667                                 }
8668                             }
8669                         }
8670 #endif
8671                       else
8672                         *imm_reloc = BFD_RELOC_HI16;
8673                     }
8674                   else if (imm_expr.X_op == O_constant)
8675                     imm_expr.X_add_number &= 0xffff;
8676                 }
8677               if (imm_expr.X_op == O_constant
8678                   && (imm_expr.X_add_number < 0
8679                       || imm_expr.X_add_number >= 0x10000))
8680                 as_bad (_("lui expression not in range 0..65535"));
8681               s = expr_end;
8682               continue;
8683
8684             case 'a':           /* 26 bit address */
8685               my_getExpression (&offset_expr, s);
8686               s = expr_end;
8687               *offset_reloc = BFD_RELOC_MIPS_JMP;
8688               continue;
8689
8690             case 'N':           /* 3 bit branch condition code */
8691             case 'M':           /* 3 bit compare condition code */
8692               if (strncmp (s, "$fcc", 4) != 0)
8693                 break;
8694               s += 4;
8695               regno = 0;
8696               do
8697                 {
8698                   regno *= 10;
8699                   regno += *s - '0';
8700                   ++s;
8701                 }
8702               while (ISDIGIT (*s));
8703               if (regno > 7)
8704                 as_bad (_("invalid condition code register $fcc%d"), regno);
8705               if (*args == 'N')
8706                 ip->insn_opcode |= regno << OP_SH_BCC;
8707               else
8708                 ip->insn_opcode |= regno << OP_SH_CCC;
8709               continue;
8710
8711             case 'H':
8712               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8713                 s += 2;
8714               if (ISDIGIT (*s))
8715                 {
8716                   c = 0;
8717                   do
8718                     {
8719                       c *= 10;
8720                       c += *s - '0';
8721                       ++s;
8722                     }
8723                   while (ISDIGIT (*s));
8724                 }
8725               else
8726                 c = 8; /* Invalid sel value.  */
8727
8728               if (c > 7)
8729                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8730               ip->insn_opcode |= c;
8731               continue;
8732
8733             default:
8734               as_bad (_("bad char = '%c'\n"), *args);
8735               internalError ();
8736             }
8737           break;
8738         }
8739       /* Args don't match.  */
8740       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8741           !strcmp (insn->name, insn[1].name))
8742         {
8743           ++insn;
8744           s = argsStart;
8745           insn_error = _("illegal operands");
8746           continue;
8747         }
8748       if (save_c)
8749         *(--s) = save_c;
8750       insn_error = _("illegal operands");
8751       return;
8752     }
8753 }
8754
8755 /* This routine assembles an instruction into its binary format when
8756    assembling for the mips16.  As a side effect, it sets one of the
8757    global variables imm_reloc or offset_reloc to the type of
8758    relocation to do if one of the operands is an address expression.
8759    It also sets mips16_small and mips16_ext if the user explicitly
8760    requested a small or extended instruction.  */
8761
8762 static void
8763 mips16_ip (str, ip)
8764      char *str;
8765      struct mips_cl_insn *ip;
8766 {
8767   char *s;
8768   const char *args;
8769   struct mips_opcode *insn;
8770   char *argsstart;
8771   unsigned int regno;
8772   unsigned int lastregno = 0;
8773   char *s_reset;
8774
8775   insn_error = NULL;
8776
8777   mips16_small = false;
8778   mips16_ext = false;
8779
8780   for (s = str; ISLOWER (*s); ++s)
8781     ;
8782   switch (*s)
8783     {
8784     case '\0':
8785       break;
8786
8787     case ' ':
8788       *s++ = '\0';
8789       break;
8790
8791     case '.':
8792       if (s[1] == 't' && s[2] == ' ')
8793         {
8794           *s = '\0';
8795           mips16_small = true;
8796           s += 3;
8797           break;
8798         }
8799       else if (s[1] == 'e' && s[2] == ' ')
8800         {
8801           *s = '\0';
8802           mips16_ext = true;
8803           s += 3;
8804           break;
8805         }
8806       /* Fall through.  */
8807     default:
8808       insn_error = _("unknown opcode");
8809       return;
8810     }
8811
8812   if (mips_opts.noautoextend && ! mips16_ext)
8813     mips16_small = true;
8814
8815   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8816     {
8817       insn_error = _("unrecognized opcode");
8818       return;
8819     }
8820
8821   argsstart = s;
8822   for (;;)
8823     {
8824       assert (strcmp (insn->name, str) == 0);
8825
8826       ip->insn_mo = insn;
8827       ip->insn_opcode = insn->match;
8828       ip->use_extend = false;
8829       imm_expr.X_op = O_absent;
8830       imm_reloc[0] = BFD_RELOC_UNUSED;
8831       imm_reloc[1] = BFD_RELOC_UNUSED;
8832       imm_reloc[2] = BFD_RELOC_UNUSED;
8833       offset_expr.X_op = O_absent;
8834       offset_reloc[0] = BFD_RELOC_UNUSED;
8835       offset_reloc[1] = BFD_RELOC_UNUSED;
8836       offset_reloc[2] = BFD_RELOC_UNUSED;
8837       for (args = insn->args; 1; ++args)
8838         {
8839           int c;
8840
8841           if (*s == ' ')
8842             ++s;
8843
8844           /* In this switch statement we call break if we did not find
8845              a match, continue if we did find a match, or return if we
8846              are done.  */
8847
8848           c = *args;
8849           switch (c)
8850             {
8851             case '\0':
8852               if (*s == '\0')
8853                 {
8854                   /* Stuff the immediate value in now, if we can.  */
8855                   if (imm_expr.X_op == O_constant
8856                       && *imm_reloc > BFD_RELOC_UNUSED
8857                       && insn->pinfo != INSN_MACRO)
8858                     {
8859                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8860                                     imm_expr.X_add_number, true, mips16_small,
8861                                     mips16_ext, &ip->insn_opcode,
8862                                     &ip->use_extend, &ip->extend);
8863                       imm_expr.X_op = O_absent;
8864                       *imm_reloc = BFD_RELOC_UNUSED;
8865                     }
8866
8867                   return;
8868                 }
8869               break;
8870
8871             case ',':
8872               if (*s++ == c)
8873                 continue;
8874               s--;
8875               switch (*++args)
8876                 {
8877                 case 'v':
8878                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8879                   continue;
8880                 case 'w':
8881                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8882                   continue;
8883                 }
8884               break;
8885
8886             case '(':
8887             case ')':
8888               if (*s++ == c)
8889                 continue;
8890               break;
8891
8892             case 'v':
8893             case 'w':
8894               if (s[0] != '$')
8895                 {
8896                   if (c == 'v')
8897                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8898                   else
8899                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8900                   ++args;
8901                   continue;
8902                 }
8903               /* Fall through.  */
8904             case 'x':
8905             case 'y':
8906             case 'z':
8907             case 'Z':
8908             case '0':
8909             case 'S':
8910             case 'R':
8911             case 'X':
8912             case 'Y':
8913               if (s[0] != '$')
8914                 break;
8915               s_reset = s;
8916               if (ISDIGIT (s[1]))
8917                 {
8918                   ++s;
8919                   regno = 0;
8920                   do
8921                     {
8922                       regno *= 10;
8923                       regno += *s - '0';
8924                       ++s;
8925                     }
8926                   while (ISDIGIT (*s));
8927                   if (regno > 31)
8928                     {
8929                       as_bad (_("invalid register number (%d)"), regno);
8930                       regno = 2;
8931                     }
8932                 }
8933               else
8934                 {
8935                   if (s[1] == 'r' && s[2] == 'a')
8936                     {
8937                       s += 3;
8938                       regno = RA;
8939                     }
8940                   else if (s[1] == 'f' && s[2] == 'p')
8941                     {
8942                       s += 3;
8943                       regno = FP;
8944                     }
8945                   else if (s[1] == 's' && s[2] == 'p')
8946                     {
8947                       s += 3;
8948                       regno = SP;
8949                     }
8950                   else if (s[1] == 'g' && s[2] == 'p')
8951                     {
8952                       s += 3;
8953                       regno = GP;
8954                     }
8955                   else if (s[1] == 'a' && s[2] == 't')
8956                     {
8957                       s += 3;
8958                       regno = AT;
8959                     }
8960                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8961                     {
8962                       s += 4;
8963                       regno = KT0;
8964                     }
8965                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8966                     {
8967                       s += 4;
8968                       regno = KT1;
8969                     }
8970                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8971                     {
8972                       s += 5;
8973                       regno = ZERO;
8974                     }
8975                   else
8976                     break;
8977                 }
8978
8979               if (*s == ' ')
8980                 ++s;
8981               if (args[1] != *s)
8982                 {
8983                   if (c == 'v' || c == 'w')
8984                     {
8985                       regno = mips16_to_32_reg_map[lastregno];
8986                       s = s_reset;
8987                       ++args;
8988                     }
8989                 }
8990
8991               switch (c)
8992                 {
8993                 case 'x':
8994                 case 'y':
8995                 case 'z':
8996                 case 'v':
8997                 case 'w':
8998                 case 'Z':
8999                   regno = mips32_to_16_reg_map[regno];
9000                   break;
9001
9002                 case '0':
9003                   if (regno != 0)
9004                     regno = ILLEGAL_REG;
9005                   break;
9006
9007                 case 'S':
9008                   if (regno != SP)
9009                     regno = ILLEGAL_REG;
9010                   break;
9011
9012                 case 'R':
9013                   if (regno != RA)
9014                     regno = ILLEGAL_REG;
9015                   break;
9016
9017                 case 'X':
9018                 case 'Y':
9019                   if (regno == AT && ! mips_opts.noat)
9020                     as_warn (_("used $at without \".set noat\""));
9021                   break;
9022
9023                 default:
9024                   internalError ();
9025                 }
9026
9027               if (regno == ILLEGAL_REG)
9028                 break;
9029
9030               switch (c)
9031                 {
9032                 case 'x':
9033                 case 'v':
9034                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9035                   break;
9036                 case 'y':
9037                 case 'w':
9038                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9039                   break;
9040                 case 'z':
9041                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9042                   break;
9043                 case 'Z':
9044                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9045                 case '0':
9046                 case 'S':
9047                 case 'R':
9048                   break;
9049                 case 'X':
9050                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9051                   break;
9052                 case 'Y':
9053                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9054                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9055                   break;
9056                 default:
9057                   internalError ();
9058                 }
9059
9060               lastregno = regno;
9061               continue;
9062
9063             case 'P':
9064               if (strncmp (s, "$pc", 3) == 0)
9065                 {
9066                   s += 3;
9067                   continue;
9068                 }
9069               break;
9070
9071             case '<':
9072             case '>':
9073             case '[':
9074             case ']':
9075             case '4':
9076             case '5':
9077             case 'H':
9078             case 'W':
9079             case 'D':
9080             case 'j':
9081             case '8':
9082             case 'V':
9083             case 'C':
9084             case 'U':
9085             case 'k':
9086             case 'K':
9087               if (s[0] == '%'
9088                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9089                 {
9090                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9091                      and generate the appropriate reloc.  If the text
9092                      inside %gprel is not a symbol name with an
9093                      optional offset, then we generate a normal reloc
9094                      and will probably fail later.  */
9095                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9096                   if (imm_expr.X_op == O_symbol)
9097                     {
9098                       mips16_ext = true;
9099                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9100                       s = expr_end;
9101                       ip->use_extend = true;
9102                       ip->extend = 0;
9103                       continue;
9104                     }
9105                 }
9106               else
9107                 {
9108                   /* Just pick up a normal expression.  */
9109                   my_getExpression (&imm_expr, s);
9110                 }
9111
9112               if (imm_expr.X_op == O_register)
9113                 {
9114                   /* What we thought was an expression turned out to
9115                      be a register.  */
9116
9117                   if (s[0] == '(' && args[1] == '(')
9118                     {
9119                       /* It looks like the expression was omitted
9120                          before a register indirection, which means
9121                          that the expression is implicitly zero.  We
9122                          still set up imm_expr, so that we handle
9123                          explicit extensions correctly.  */
9124                       imm_expr.X_op = O_constant;
9125                       imm_expr.X_add_number = 0;
9126                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9127                       continue;
9128                     }
9129
9130                   break;
9131                 }
9132
9133               /* We need to relax this instruction.  */
9134               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9135               s = expr_end;
9136               continue;
9137
9138             case 'p':
9139             case 'q':
9140             case 'A':
9141             case 'B':
9142             case 'E':
9143               /* We use offset_reloc rather than imm_reloc for the PC
9144                  relative operands.  This lets macros with both
9145                  immediate and address operands work correctly.  */
9146               my_getExpression (&offset_expr, s);
9147
9148               if (offset_expr.X_op == O_register)
9149                 break;
9150
9151               /* We need to relax this instruction.  */
9152               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9153               s = expr_end;
9154               continue;
9155
9156             case '6':           /* break code */
9157               my_getExpression (&imm_expr, s);
9158               check_absolute_expr (ip, &imm_expr);
9159               if ((unsigned long) imm_expr.X_add_number > 63)
9160                 {
9161                   as_warn (_("Invalid value for `%s' (%lu)"),
9162                            ip->insn_mo->name,
9163                            (unsigned long) imm_expr.X_add_number);
9164                   imm_expr.X_add_number &= 0x3f;
9165                 }
9166               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9167               imm_expr.X_op = O_absent;
9168               s = expr_end;
9169               continue;
9170
9171             case 'a':           /* 26 bit address */
9172               my_getExpression (&offset_expr, s);
9173               s = expr_end;
9174               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9175               ip->insn_opcode <<= 16;
9176               continue;
9177
9178             case 'l':           /* register list for entry macro */
9179             case 'L':           /* register list for exit macro */
9180               {
9181                 int mask;
9182
9183                 if (c == 'l')
9184                   mask = 0;
9185                 else
9186                   mask = 7 << 3;
9187                 while (*s != '\0')
9188                   {
9189                     int freg, reg1, reg2;
9190
9191                     while (*s == ' ' || *s == ',')
9192                       ++s;
9193                     if (*s != '$')
9194                       {
9195                         as_bad (_("can't parse register list"));
9196                         break;
9197                       }
9198                     ++s;
9199                     if (*s != 'f')
9200                       freg = 0;
9201                     else
9202                       {
9203                         freg = 1;
9204                         ++s;
9205                       }
9206                     reg1 = 0;
9207                     while (ISDIGIT (*s))
9208                       {
9209                         reg1 *= 10;
9210                         reg1 += *s - '0';
9211                         ++s;
9212                       }
9213                     if (*s == ' ')
9214                       ++s;
9215                     if (*s != '-')
9216                       reg2 = reg1;
9217                     else
9218                       {
9219                         ++s;
9220                         if (*s != '$')
9221                           break;
9222                         ++s;
9223                         if (freg)
9224                           {
9225                             if (*s == 'f')
9226                               ++s;
9227                             else
9228                               {
9229                                 as_bad (_("invalid register list"));
9230                                 break;
9231                               }
9232                           }
9233                         reg2 = 0;
9234                         while (ISDIGIT (*s))
9235                           {
9236                             reg2 *= 10;
9237                             reg2 += *s - '0';
9238                             ++s;
9239                           }
9240                       }
9241                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9242                       {
9243                         mask &= ~ (7 << 3);
9244                         mask |= 5 << 3;
9245                       }
9246                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9247                       {
9248                         mask &= ~ (7 << 3);
9249                         mask |= 6 << 3;
9250                       }
9251                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9252                       mask |= (reg2 - 3) << 3;
9253                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9254                       mask |= (reg2 - 15) << 1;
9255                     else if (reg1 == RA && reg2 == RA)
9256                       mask |= 1;
9257                     else
9258                       {
9259                         as_bad (_("invalid register list"));
9260                         break;
9261                       }
9262                   }
9263                 /* The mask is filled in in the opcode table for the
9264                    benefit of the disassembler.  We remove it before
9265                    applying the actual mask.  */
9266                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9267                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9268               }
9269             continue;
9270
9271             case 'e':           /* extend code */
9272               my_getExpression (&imm_expr, s);
9273               check_absolute_expr (ip, &imm_expr);
9274               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9275                 {
9276                   as_warn (_("Invalid value for `%s' (%lu)"),
9277                            ip->insn_mo->name,
9278                            (unsigned long) imm_expr.X_add_number);
9279                   imm_expr.X_add_number &= 0x7ff;
9280                 }
9281               ip->insn_opcode |= imm_expr.X_add_number;
9282               imm_expr.X_op = O_absent;
9283               s = expr_end;
9284               continue;
9285
9286             default:
9287               internalError ();
9288             }
9289           break;
9290         }
9291
9292       /* Args don't match.  */
9293       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9294           strcmp (insn->name, insn[1].name) == 0)
9295         {
9296           ++insn;
9297           s = argsstart;
9298           continue;
9299         }
9300
9301       insn_error = _("illegal operands");
9302
9303       return;
9304     }
9305 }
9306
9307 /* This structure holds information we know about a mips16 immediate
9308    argument type.  */
9309
9310 struct mips16_immed_operand
9311 {
9312   /* The type code used in the argument string in the opcode table.  */
9313   int type;
9314   /* The number of bits in the short form of the opcode.  */
9315   int nbits;
9316   /* The number of bits in the extended form of the opcode.  */
9317   int extbits;
9318   /* The amount by which the short form is shifted when it is used;
9319      for example, the sw instruction has a shift count of 2.  */
9320   int shift;
9321   /* The amount by which the short form is shifted when it is stored
9322      into the instruction code.  */
9323   int op_shift;
9324   /* Non-zero if the short form is unsigned.  */
9325   int unsp;
9326   /* Non-zero if the extended form is unsigned.  */
9327   int extu;
9328   /* Non-zero if the value is PC relative.  */
9329   int pcrel;
9330 };
9331
9332 /* The mips16 immediate operand types.  */
9333
9334 static const struct mips16_immed_operand mips16_immed_operands[] =
9335 {
9336   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9337   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9338   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9339   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9340   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9341   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9342   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9343   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9344   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9345   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9346   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9347   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9348   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9349   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9350   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9351   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9352   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9353   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9354   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9355   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9356   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9357 };
9358
9359 #define MIPS16_NUM_IMMED \
9360   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9361
9362 /* Handle a mips16 instruction with an immediate value.  This or's the
9363    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9364    whether an extended value is needed; if one is needed, it sets
9365    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9366    If SMALL is true, an unextended opcode was explicitly requested.
9367    If EXT is true, an extended opcode was explicitly requested.  If
9368    WARN is true, warn if EXT does not match reality.  */
9369
9370 static void
9371 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9372               extend)
9373      char *file;
9374      unsigned int line;
9375      int type;
9376      offsetT val;
9377      boolean warn;
9378      boolean small;
9379      boolean ext;
9380      unsigned long *insn;
9381      boolean *use_extend;
9382      unsigned short *extend;
9383 {
9384   register const struct mips16_immed_operand *op;
9385   int mintiny, maxtiny;
9386   boolean needext;
9387
9388   op = mips16_immed_operands;
9389   while (op->type != type)
9390     {
9391       ++op;
9392       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9393     }
9394
9395   if (op->unsp)
9396     {
9397       if (type == '<' || type == '>' || type == '[' || type == ']')
9398         {
9399           mintiny = 1;
9400           maxtiny = 1 << op->nbits;
9401         }
9402       else
9403         {
9404           mintiny = 0;
9405           maxtiny = (1 << op->nbits) - 1;
9406         }
9407     }
9408   else
9409     {
9410       mintiny = - (1 << (op->nbits - 1));
9411       maxtiny = (1 << (op->nbits - 1)) - 1;
9412     }
9413
9414   /* Branch offsets have an implicit 0 in the lowest bit.  */
9415   if (type == 'p' || type == 'q')
9416     val /= 2;
9417
9418   if ((val & ((1 << op->shift) - 1)) != 0
9419       || val < (mintiny << op->shift)
9420       || val > (maxtiny << op->shift))
9421     needext = true;
9422   else
9423     needext = false;
9424
9425   if (warn && ext && ! needext)
9426     as_warn_where (file, line,
9427                    _("extended operand requested but not required"));
9428   if (small && needext)
9429     as_bad_where (file, line, _("invalid unextended operand value"));
9430
9431   if (small || (! ext && ! needext))
9432     {
9433       int insnval;
9434
9435       *use_extend = false;
9436       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9437       insnval <<= op->op_shift;
9438       *insn |= insnval;
9439     }
9440   else
9441     {
9442       long minext, maxext;
9443       int extval;
9444
9445       if (op->extu)
9446         {
9447           minext = 0;
9448           maxext = (1 << op->extbits) - 1;
9449         }
9450       else
9451         {
9452           minext = - (1 << (op->extbits - 1));
9453           maxext = (1 << (op->extbits - 1)) - 1;
9454         }
9455       if (val < minext || val > maxext)
9456         as_bad_where (file, line,
9457                       _("operand value out of range for instruction"));
9458
9459       *use_extend = true;
9460       if (op->extbits == 16)
9461         {
9462           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9463           val &= 0x1f;
9464         }
9465       else if (op->extbits == 15)
9466         {
9467           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9468           val &= 0xf;
9469         }
9470       else
9471         {
9472           extval = ((val & 0x1f) << 6) | (val & 0x20);
9473           val = 0;
9474         }
9475
9476       *extend = (unsigned short) extval;
9477       *insn |= val;
9478     }
9479 }
9480 \f
9481 static struct percent_op_match
9482 {
9483    const char *str;
9484    const enum small_ex_type type;
9485 } percent_op[] =
9486 {
9487   {"%lo", S_EX_LO},
9488 #ifdef OBJ_ELF
9489   {"%call_hi", S_EX_CALL_HI},
9490   {"%call_lo", S_EX_CALL_LO},
9491   {"%call16", S_EX_CALL16},
9492   {"%got_disp", S_EX_GOT_DISP},
9493   {"%got_page", S_EX_GOT_PAGE},
9494   {"%got_ofst", S_EX_GOT_OFST},
9495   {"%got_hi", S_EX_GOT_HI},
9496   {"%got_lo", S_EX_GOT_LO},
9497   {"%got", S_EX_GOT},
9498   {"%gp_rel", S_EX_GP_REL},
9499   {"%half", S_EX_HALF},
9500   {"%highest", S_EX_HIGHEST},
9501   {"%higher", S_EX_HIGHER},
9502   {"%neg", S_EX_NEG},
9503 #endif
9504   {"%hi", S_EX_HI}
9505 };
9506
9507 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9508    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9509    can be nested, this is handled by blanking the innermost, parsing the
9510    rest by subsequent calls.  */
9511
9512 static int
9513 my_getSmallParser (str, len, nestlevel)
9514      char **str;
9515      unsigned int *len;
9516      int *nestlevel;
9517 {
9518   *len = 0;
9519   *str += strspn (*str, " \t");
9520   /* Check for expression in parentheses.  */
9521   if (**str == '(')
9522     {
9523       char *b = *str + 1 + strspn (*str + 1, " \t");
9524       char *e;
9525
9526       /* Check for base register.  */
9527       if (b[0] == '$')
9528         {
9529           if (strchr (b, ')')
9530               && (e = b + strcspn (b, ") \t"))
9531               && e - b > 1 && e - b < 4)
9532             {
9533               if ((e - b == 3
9534                    && ((b[1] == 'f' && b[2] == 'p')
9535                        || (b[1] == 's' && b[2] == 'p')
9536                        || (b[1] == 'g' && b[2] == 'p')
9537                        || (b[1] == 'a' && b[2] == 't')
9538                        || (ISDIGIT (b[1])
9539                            && ISDIGIT (b[2]))))
9540                   || (ISDIGIT (b[1])))
9541                 {
9542                   *len = strcspn (*str, ")") + 1;
9543                   return S_EX_REGISTER;
9544                 }
9545             }
9546         }
9547       /* Check for percent_op (in parentheses).  */
9548       else if (b[0] == '%')
9549         {
9550           *str = b;
9551           return my_getPercentOp (str, len, nestlevel);
9552         }
9553
9554       /* Some other expression in the parentheses, which can contain
9555          parentheses itself. Attempt to find the matching one.  */
9556       {
9557         int pcnt = 1;
9558         char *s;
9559
9560         *len = 1;
9561         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9562           {
9563             if (*s == '(')
9564               ++pcnt;
9565             else if (*s == ')')
9566               --pcnt;
9567           }
9568       }
9569     }
9570   /* Check for percent_op (outside of parentheses).  */
9571   else if (*str[0] == '%')
9572     return my_getPercentOp (str, len, nestlevel);
9573
9574   /* Any other expression.  */
9575   return S_EX_NONE;
9576 }
9577
9578 static int
9579 my_getPercentOp (str, len, nestlevel)
9580      char **str;
9581      unsigned int *len;
9582      int *nestlevel;
9583 {
9584   char *tmp = *str + 1;
9585   unsigned int i = 0;
9586
9587   while (ISALPHA (*tmp) || *tmp == '_')
9588     {
9589       *tmp = TOLOWER (*tmp);
9590       tmp++;
9591     }
9592   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9593     {
9594       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9595         i++;
9596       else
9597         {
9598           int type = percent_op[i].type;
9599
9600           /* Only %hi and %lo are allowed for OldABI.  */
9601           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9602             return S_EX_NONE;
9603
9604           *len = strlen (percent_op[i].str);
9605           ++(*nestlevel);
9606           return type;
9607         }
9608     }
9609   return S_EX_NONE;
9610 }
9611
9612 static int
9613 my_getSmallExpression (ep, str)
9614      expressionS *ep;
9615      char *str;
9616 {
9617   static char *oldstr = NULL;
9618   int c = S_EX_NONE;
9619   int oldc;
9620   int nestlevel = -1;
9621   unsigned int len;
9622
9623   /* Don't update oldstr if the last call had nested percent_op's. We need
9624      it to parse the outer ones later.  */
9625   if (! oldstr)
9626     oldstr = str;
9627
9628   do
9629     {
9630       oldc = c;
9631       c = my_getSmallParser (&str, &len, &nestlevel);
9632       if (c != S_EX_NONE && c != S_EX_REGISTER)
9633         str += len;
9634     }
9635   while (c != S_EX_NONE && c != S_EX_REGISTER);
9636
9637   if (nestlevel >= 0)
9638     {
9639       /* A percent_op was encountered.  Don't try to get an expression if
9640          it is already blanked out.  */
9641       if (*(str + strspn (str + 1, " )")) != ')')
9642         {
9643           char save;
9644
9645           /* Let my_getExpression() stop at the closing parenthesis.  */
9646           save = *(str + len);
9647           *(str + len) = '\0';
9648           my_getExpression (ep, str);
9649           *(str + len) = save;
9650         }
9651       if (nestlevel > 0)
9652         {
9653           /* Blank out including the % sign and the proper matching
9654              parenthesis.  */
9655           int pcnt = 1;
9656           char *s = strrchr (oldstr, '%');
9657           char *end;
9658
9659           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9660             {
9661               if (*end == '(')
9662                 ++pcnt;
9663               else if (*end == ')')
9664                 --pcnt;
9665             }
9666
9667           memset (s, ' ', end - s);
9668           str = oldstr;
9669         }
9670       else
9671         expr_end = str + len;
9672
9673       c = oldc;
9674     }
9675   else if (c == S_EX_NONE)
9676     {
9677       my_getExpression (ep, str);
9678     }
9679   else if (c == S_EX_REGISTER)
9680     {
9681       ep->X_op = O_constant;
9682       expr_end = str;
9683       ep->X_add_symbol = NULL;
9684       ep->X_op_symbol = NULL;
9685       ep->X_add_number = 0;
9686     }
9687   else
9688     {
9689       as_fatal (_("internal error"));
9690     }
9691
9692   if (nestlevel <= 0)
9693     /* All percent_op's have been handled.  */
9694     oldstr = NULL;
9695
9696   return c;
9697 }
9698
9699 static void
9700 my_getExpression (ep, str)
9701      expressionS *ep;
9702      char *str;
9703 {
9704   char *save_in;
9705   valueT val;
9706
9707   save_in = input_line_pointer;
9708   input_line_pointer = str;
9709   expression (ep);
9710   expr_end = input_line_pointer;
9711   input_line_pointer = save_in;
9712
9713   /* If we are in mips16 mode, and this is an expression based on `.',
9714      then we bump the value of the symbol by 1 since that is how other
9715      text symbols are handled.  We don't bother to handle complex
9716      expressions, just `.' plus or minus a constant.  */
9717   if (mips_opts.mips16
9718       && ep->X_op == O_symbol
9719       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9720       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9721       && symbol_get_frag (ep->X_add_symbol) == frag_now
9722       && symbol_constant_p (ep->X_add_symbol)
9723       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9724     S_SET_VALUE (ep->X_add_symbol, val + 1);
9725 }
9726
9727 /* Turn a string in input_line_pointer into a floating point constant
9728    of type TYPE, and store the appropriate bytes in *LITP.  The number
9729    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9730    returned, or NULL on OK.  */
9731
9732 char *
9733 md_atof (type, litP, sizeP)
9734      int type;
9735      char *litP;
9736      int *sizeP;
9737 {
9738   int prec;
9739   LITTLENUM_TYPE words[4];
9740   char *t;
9741   int i;
9742
9743   switch (type)
9744     {
9745     case 'f':
9746       prec = 2;
9747       break;
9748
9749     case 'd':
9750       prec = 4;
9751       break;
9752
9753     default:
9754       *sizeP = 0;
9755       return _("bad call to md_atof");
9756     }
9757
9758   t = atof_ieee (input_line_pointer, type, words);
9759   if (t)
9760     input_line_pointer = t;
9761
9762   *sizeP = prec * 2;
9763
9764   if (! target_big_endian)
9765     {
9766       for (i = prec - 1; i >= 0; i--)
9767         {
9768           md_number_to_chars (litP, (valueT) words[i], 2);
9769           litP += 2;
9770         }
9771     }
9772   else
9773     {
9774       for (i = 0; i < prec; i++)
9775         {
9776           md_number_to_chars (litP, (valueT) words[i], 2);
9777           litP += 2;
9778         }
9779     }
9780
9781   return NULL;
9782 }
9783
9784 void
9785 md_number_to_chars (buf, val, n)
9786      char *buf;
9787      valueT val;
9788      int n;
9789 {
9790   if (target_big_endian)
9791     number_to_chars_bigendian (buf, val, n);
9792   else
9793     number_to_chars_littleendian (buf, val, n);
9794 }
9795 \f
9796 #ifdef OBJ_ELF
9797 static int support_64bit_objects(void)
9798 {
9799   const char **list, **l;
9800
9801   list = bfd_target_list ();
9802   for (l = list; *l != NULL; l++)
9803 #ifdef TE_TMIPS
9804     /* This is traditional mips */
9805     if (strcmp (*l, "elf64-tradbigmips") == 0
9806         || strcmp (*l, "elf64-tradlittlemips") == 0)
9807 #else
9808     if (strcmp (*l, "elf64-bigmips") == 0
9809         || strcmp (*l, "elf64-littlemips") == 0)
9810 #endif
9811       break;
9812   free (list);
9813   return (*l != NULL);
9814 }
9815 #endif /* OBJ_ELF */
9816
9817 CONST char *md_shortopts = "nO::g::G:";
9818
9819 struct option md_longopts[] =
9820 {
9821 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9822   {"mips0", no_argument, NULL, OPTION_MIPS1},
9823   {"mips1", no_argument, NULL, OPTION_MIPS1},
9824 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9825   {"mips2", no_argument, NULL, OPTION_MIPS2},
9826 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9827   {"mips3", no_argument, NULL, OPTION_MIPS3},
9828 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9829   {"mips4", no_argument, NULL, OPTION_MIPS4},
9830 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9831   {"mips5", no_argument, NULL, OPTION_MIPS5},
9832 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9833   {"mips32", no_argument, NULL, OPTION_MIPS32},
9834 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9835   {"mips64", no_argument, NULL, OPTION_MIPS64},
9836 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9837   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9838 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9839   {"trap", no_argument, NULL, OPTION_TRAP},
9840   {"no-break", no_argument, NULL, OPTION_TRAP},
9841 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9842   {"break", no_argument, NULL, OPTION_BREAK},
9843   {"no-trap", no_argument, NULL, OPTION_BREAK},
9844 #define OPTION_EB (OPTION_MD_BASE + 11)
9845   {"EB", no_argument, NULL, OPTION_EB},
9846 #define OPTION_EL (OPTION_MD_BASE + 12)
9847   {"EL", no_argument, NULL, OPTION_EL},
9848 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9849   {"mips16", no_argument, NULL, OPTION_MIPS16},
9850 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9851   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9852 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9853   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9854 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9855   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9856   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9857 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9858   {"mfp32", no_argument, NULL, OPTION_FP32},
9859 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9860   {"mgp32", no_argument, NULL, OPTION_GP32},
9861 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9862   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9863 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9864   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9865 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9866   {"march", required_argument, NULL, OPTION_MARCH},
9867 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9868   {"mtune", required_argument, NULL, OPTION_MTUNE},
9869 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9870   {"mcpu", required_argument, NULL, OPTION_MCPU},
9871 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9872   {"m4650", no_argument, NULL, OPTION_M4650},
9873 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9874   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9875 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9876   {"m4010", no_argument, NULL, OPTION_M4010},
9877 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9878   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9879 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9880   {"m4100", no_argument, NULL, OPTION_M4100},
9881 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9882   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9883 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9884   {"m3900", no_argument, NULL, OPTION_M3900},
9885 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9886   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9887 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9888   {"mgp64", no_argument, NULL, OPTION_GP64},
9889 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9890   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9891 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9892   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9893 #define OPTION_MDMX (OPTION_MD_BASE + 35)
9894   {"mdmx", no_argument, NULL, OPTION_MDMX},
9895 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9896   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9897 #ifdef OBJ_ELF
9898 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 37)
9899 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9900   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9901   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9902 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
9903   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
9904 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
9905   {"xgot",        no_argument, NULL, OPTION_XGOT},
9906 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
9907   {"mabi", required_argument, NULL, OPTION_MABI},
9908 #define OPTION_32          (OPTION_ELF_BASE + 4)
9909   {"32",          no_argument, NULL, OPTION_32},
9910 #define OPTION_N32         (OPTION_ELF_BASE + 5)
9911   {"n32",         no_argument, NULL, OPTION_N32},
9912 #define OPTION_64          (OPTION_ELF_BASE + 6)
9913   {"64",          no_argument, NULL, OPTION_64},
9914 #endif /* OBJ_ELF */
9915   {NULL, no_argument, NULL, 0}
9916 };
9917 size_t md_longopts_size = sizeof (md_longopts);
9918
9919 int
9920 md_parse_option (c, arg)
9921      int c;
9922      char *arg;
9923 {
9924   switch (c)
9925     {
9926     case OPTION_CONSTRUCT_FLOATS:
9927       mips_disable_float_construction = 0;
9928       break;
9929
9930     case OPTION_NO_CONSTRUCT_FLOATS:
9931       mips_disable_float_construction = 1;
9932       break;
9933
9934     case OPTION_TRAP:
9935       mips_trap = 1;
9936       break;
9937
9938     case OPTION_BREAK:
9939       mips_trap = 0;
9940       break;
9941
9942     case OPTION_EB:
9943       target_big_endian = 1;
9944       break;
9945
9946     case OPTION_EL:
9947       target_big_endian = 0;
9948       break;
9949
9950     case 'n':
9951       warn_nops = 1;
9952       break;
9953
9954     case 'O':
9955       if (arg && arg[1] == '0')
9956         mips_optimize = 1;
9957       else
9958         mips_optimize = 2;
9959       break;
9960
9961     case 'g':
9962       if (arg == NULL)
9963         mips_debug = 2;
9964       else
9965         mips_debug = atoi (arg);
9966       /* When the MIPS assembler sees -g or -g2, it does not do
9967          optimizations which limit full symbolic debugging.  We take
9968          that to be equivalent to -O0.  */
9969       if (mips_debug == 2)
9970         mips_optimize = 1;
9971       break;
9972
9973     case OPTION_MIPS1:
9974       mips_opts.isa = ISA_MIPS1;
9975       break;
9976
9977     case OPTION_MIPS2:
9978       mips_opts.isa = ISA_MIPS2;
9979       break;
9980
9981     case OPTION_MIPS3:
9982       mips_opts.isa = ISA_MIPS3;
9983       break;
9984
9985     case OPTION_MIPS4:
9986       mips_opts.isa = ISA_MIPS4;
9987       break;
9988
9989     case OPTION_MIPS5:
9990       mips_opts.isa = ISA_MIPS5;
9991       break;
9992
9993     case OPTION_MIPS32:
9994       mips_opts.isa = ISA_MIPS32;
9995       break;
9996
9997     case OPTION_MIPS64:
9998       mips_opts.isa = ISA_MIPS64;
9999       break;
10000
10001     case OPTION_MTUNE:
10002     case OPTION_MARCH:
10003     case OPTION_MCPU:
10004       {
10005         int cpu = CPU_UNKNOWN;
10006
10007         /* Identify the processor type.  */
10008         if (strcasecmp (arg, "default") != 0)
10009           {
10010             const struct mips_cpu_info *ci;
10011
10012             ci = mips_cpu_info_from_name (arg);
10013             if (ci == NULL || ci->is_isa)
10014               {
10015                 switch (c)
10016                   {
10017                   case OPTION_MTUNE:
10018                     as_fatal (_("invalid architecture -mtune=%s"), arg);
10019                     break;
10020                   case OPTION_MARCH:
10021                     as_fatal (_("invalid architecture -march=%s"), arg);
10022                     break;
10023                   case OPTION_MCPU:
10024                     as_fatal (_("invalid architecture -mcpu=%s"), arg);
10025                     break;
10026                   }
10027               }
10028             else
10029               cpu = ci->cpu;
10030           }
10031
10032         switch (c)
10033           {
10034           case OPTION_MTUNE:
10035             if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
10036               as_warn (_("A different -mtune= was already specified, is now "
10037                          "-mtune=%s"), arg);
10038             mips_tune = cpu;
10039             break;
10040           case OPTION_MARCH:
10041             if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
10042               as_warn (_("A different -march= was already specified, is now "
10043                          "-march=%s"), arg);
10044             mips_arch = cpu;
10045             break;
10046           case OPTION_MCPU:
10047             if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
10048               as_warn (_("A different -mcpu= was already specified, is now "
10049                          "-mcpu=%s"), arg);
10050             mips_cpu = cpu;
10051           }
10052       }
10053       break;
10054
10055     case OPTION_M4650:
10056       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
10057           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
10058         as_warn (_("A different -march= or -mtune= was already specified, "
10059                    "is now -m4650"));
10060       mips_arch = CPU_R4650;
10061       mips_tune = CPU_R4650;
10062       break;
10063
10064     case OPTION_NO_M4650:
10065       break;
10066
10067     case OPTION_M4010:
10068       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
10069           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
10070         as_warn (_("A different -march= or -mtune= was already specified, "
10071                    "is now -m4010"));
10072       mips_arch = CPU_R4010;
10073       mips_tune = CPU_R4010;
10074       break;
10075
10076     case OPTION_NO_M4010:
10077       break;
10078
10079     case OPTION_M4100:
10080       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
10081           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
10082         as_warn (_("A different -march= or -mtune= was already specified, "
10083                    "is now -m4100"));
10084       mips_arch = CPU_VR4100;
10085       mips_tune = CPU_VR4100;
10086       break;
10087
10088     case OPTION_NO_M4100:
10089       break;
10090
10091     case OPTION_M3900:
10092       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
10093           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
10094         as_warn (_("A different -march= or -mtune= was already specified, "
10095                    "is now -m3900"));
10096       mips_arch = CPU_R3900;
10097       mips_tune = CPU_R3900;
10098       break;
10099
10100     case OPTION_NO_M3900:
10101       break;
10102
10103     case OPTION_MDMX:
10104       mips_opts.ase_mdmx = 1;
10105       break;
10106
10107     case OPTION_NO_MDMX:
10108       mips_opts.ase_mdmx = 0;
10109       break;
10110
10111     case OPTION_MIPS16:
10112       mips_opts.mips16 = 1;
10113       mips_no_prev_insn (false);
10114       break;
10115
10116     case OPTION_NO_MIPS16:
10117       mips_opts.mips16 = 0;
10118       mips_no_prev_insn (false);
10119       break;
10120
10121     case OPTION_MIPS3D:
10122       mips_opts.ase_mips3d = 1;
10123       break;
10124
10125     case OPTION_NO_MIPS3D:
10126       mips_opts.ase_mips3d = 0;
10127       break;
10128
10129     case OPTION_MEMBEDDED_PIC:
10130       mips_pic = EMBEDDED_PIC;
10131       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10132         {
10133           as_bad (_("-G may not be used with embedded PIC code"));
10134           return 0;
10135         }
10136       g_switch_value = 0x7fffffff;
10137       break;
10138
10139 #ifdef OBJ_ELF
10140       /* When generating ELF code, we permit -KPIC and -call_shared to
10141          select SVR4_PIC, and -non_shared to select no PIC.  This is
10142          intended to be compatible with Irix 5.  */
10143     case OPTION_CALL_SHARED:
10144       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10145         {
10146           as_bad (_("-call_shared is supported only for ELF format"));
10147           return 0;
10148         }
10149       mips_pic = SVR4_PIC;
10150       if (g_switch_seen && g_switch_value != 0)
10151         {
10152           as_bad (_("-G may not be used with SVR4 PIC code"));
10153           return 0;
10154         }
10155       g_switch_value = 0;
10156       break;
10157
10158     case OPTION_NON_SHARED:
10159       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10160         {
10161           as_bad (_("-non_shared is supported only for ELF format"));
10162           return 0;
10163         }
10164       mips_pic = NO_PIC;
10165       break;
10166
10167       /* The -xgot option tells the assembler to use 32 offsets when
10168          accessing the got in SVR4_PIC mode.  It is for Irix
10169          compatibility.  */
10170     case OPTION_XGOT:
10171       mips_big_got = 1;
10172       break;
10173 #endif /* OBJ_ELF */
10174
10175     case 'G':
10176       if (! USE_GLOBAL_POINTER_OPT)
10177         {
10178           as_bad (_("-G is not supported for this configuration"));
10179           return 0;
10180         }
10181       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10182         {
10183           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10184           return 0;
10185         }
10186       else
10187         g_switch_value = atoi (arg);
10188       g_switch_seen = 1;
10189       break;
10190
10191 #ifdef OBJ_ELF
10192       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10193          and -mabi=64.  */
10194     case OPTION_32:
10195       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10196         {
10197           as_bad (_("-32 is supported for ELF format only"));
10198           return 0;
10199         }
10200       mips_opts.abi = O32_ABI;
10201       break;
10202
10203     case OPTION_N32:
10204       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10205         {
10206           as_bad (_("-n32 is supported for ELF format only"));
10207           return 0;
10208         }
10209       mips_opts.abi = N32_ABI;
10210       break;
10211
10212     case OPTION_64:
10213       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10214         {
10215           as_bad (_("-64 is supported for ELF format only"));
10216           return 0;
10217         }
10218       mips_opts.abi = N64_ABI;
10219       if (! support_64bit_objects())
10220         as_fatal (_("No compiled in support for 64 bit object file format"));
10221       break;
10222 #endif /* OBJ_ELF */
10223
10224     case OPTION_GP32:
10225       file_mips_gp32 = 1;
10226       if (mips_opts.abi != O32_ABI)
10227         mips_opts.abi = NO_ABI;
10228       break;
10229
10230     case OPTION_GP64:
10231       file_mips_gp32 = 0;
10232       if (mips_opts.abi == O32_ABI)
10233         mips_opts.abi = NO_ABI;
10234       break;
10235
10236     case OPTION_FP32:
10237       file_mips_fp32 = 1;
10238       if (mips_opts.abi != O32_ABI)
10239         mips_opts.abi = NO_ABI;
10240       break;
10241
10242 #ifdef OBJ_ELF
10243     case OPTION_MABI:
10244       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10245         {
10246           as_bad (_("-mabi is supported for ELF format only"));
10247           return 0;
10248         }
10249       if (strcmp (arg, "32") == 0)
10250         mips_opts.abi = O32_ABI;
10251       else if (strcmp (arg, "o64") == 0)
10252         mips_opts.abi = O64_ABI;
10253       else if (strcmp (arg, "n32") == 0)
10254         mips_opts.abi = N32_ABI;
10255       else if (strcmp (arg, "64") == 0)
10256         {
10257           mips_opts.abi = N64_ABI;
10258           if (! support_64bit_objects())
10259             as_fatal (_("No compiled in support for 64 bit object file "
10260                         "format"));
10261         }
10262       else if (strcmp (arg, "eabi") == 0)
10263         mips_opts.abi = EABI_ABI;
10264       else
10265         {
10266           as_fatal (_("invalid abi -mabi=%s"), arg);
10267           return 0;
10268         }
10269       break;
10270 #endif /* OBJ_ELF */
10271
10272     case OPTION_M7000_HILO_FIX:
10273       mips_7000_hilo_fix = true;
10274       break;
10275
10276     case OPTION_MNO_7000_HILO_FIX:
10277       mips_7000_hilo_fix = false;
10278       break;
10279
10280     default:
10281       return 0;
10282     }
10283
10284   return 1;
10285 }
10286
10287 static void
10288 show (stream, string, col_p, first_p)
10289      FILE *stream;
10290      char *string;
10291      int *col_p;
10292      int *first_p;
10293 {
10294   if (*first_p)
10295     {
10296       fprintf (stream, "%24s", "");
10297       *col_p = 24;
10298     }
10299   else
10300     {
10301       fprintf (stream, ", ");
10302       *col_p += 2;
10303     }
10304
10305   if (*col_p + strlen (string) > 72)
10306     {
10307       fprintf (stream, "\n%24s", "");
10308       *col_p = 24;
10309     }
10310
10311   fprintf (stream, "%s", string);
10312   *col_p += strlen (string);
10313
10314   *first_p = 0;
10315 }
10316
10317 void
10318 md_show_usage (stream)
10319      FILE *stream;
10320 {
10321   int column, first;
10322
10323   fprintf (stream, _("\
10324 MIPS options:\n\
10325 -membedded-pic          generate embedded position independent code\n\
10326 -EB                     generate big endian output\n\
10327 -EL                     generate little endian output\n\
10328 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
10329 -G NUM                  allow referencing objects up to NUM bytes\n\
10330                         implicitly with the gp register [default 8]\n"));
10331   fprintf (stream, _("\
10332 -mips1                  generate MIPS ISA I instructions\n\
10333 -mips2                  generate MIPS ISA II instructions\n\
10334 -mips3                  generate MIPS ISA III instructions\n\
10335 -mips4                  generate MIPS ISA IV instructions\n\
10336 -mips5                  generate MIPS ISA V instructions\n\
10337 -mips32                 generate MIPS32 ISA instructions\n\
10338 -mips64                 generate MIPS64 ISA instructions\n\
10339 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
10340
10341   first = 1;
10342
10343   show (stream, "2000", &column, &first);
10344   show (stream, "3000", &column, &first);
10345   show (stream, "3900", &column, &first);
10346   show (stream, "4000", &column, &first);
10347   show (stream, "4010", &column, &first);
10348   show (stream, "4100", &column, &first);
10349   show (stream, "4111", &column, &first);
10350   show (stream, "4300", &column, &first);
10351   show (stream, "4400", &column, &first);
10352   show (stream, "4600", &column, &first);
10353   show (stream, "4650", &column, &first);
10354   show (stream, "5000", &column, &first);
10355   show (stream, "5200", &column, &first);
10356   show (stream, "5230", &column, &first);
10357   show (stream, "5231", &column, &first);
10358   show (stream, "5261", &column, &first);
10359   show (stream, "5721", &column, &first);
10360   show (stream, "6000", &column, &first);
10361   show (stream, "8000", &column, &first);
10362   show (stream, "10000", &column, &first);
10363   show (stream, "12000", &column, &first);
10364   show (stream, "sb1", &column, &first);
10365   fputc ('\n', stream);
10366
10367   fprintf (stream, _("\
10368 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10369 -no-mCPU                don't generate code specific to CPU.\n\
10370                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
10371
10372   first = 1;
10373
10374   show (stream, "3900", &column, &first);
10375   show (stream, "4010", &column, &first);
10376   show (stream, "4100", &column, &first);
10377   show (stream, "4650", &column, &first);
10378   fputc ('\n', stream);
10379
10380   fprintf (stream, _("\
10381 -mips16                 generate mips16 instructions\n\
10382 -no-mips16              do not generate mips16 instructions\n"));
10383   fprintf (stream, _("\
10384 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
10385 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
10386 -O0                     remove unneeded NOPs, do not swap branches\n\
10387 -O                      remove unneeded NOPs and swap branches\n\
10388 -n                      warn about NOPs generated from macros\n\
10389 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10390 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
10391 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
10392 #ifdef OBJ_ELF
10393   fprintf (stream, _("\
10394 -KPIC, -call_shared     generate SVR4 position independent code\n\
10395 -non_shared             do not generate position independent code\n\
10396 -xgot                   assume a 32 bit GOT\n\
10397 -mabi=ABI               create ABI conformant object file for:\n"));
10398
10399   first = 1;
10400
10401   show (stream, "32", &column, &first);
10402   show (stream, "o64", &column, &first);
10403   show (stream, "n32", &column, &first);
10404   show (stream, "64", &column, &first);
10405   show (stream, "eabi", &column, &first);
10406
10407   fputc ('\n', stream);
10408
10409   fprintf (stream, _("\
10410 -32                     create o32 ABI object file (default)\n\
10411 -n32                    create n32 ABI object file\n\
10412 -64                     create 64 ABI object file\n"));
10413 #endif
10414 }
10415 \f
10416 void
10417 mips_after_parse_args ()
10418 {
10419   const char *cpu;
10420   char *a = NULL;
10421   int mips_isa_from_cpu;
10422   int target_cpu_had_mips16 = 0;
10423   const struct mips_cpu_info *ci;
10424
10425   /* GP relative stuff not working for PE */
10426   if (strncmp (TARGET_OS, "pe", 2) == 0
10427       && g_switch_value != 0)
10428     {
10429       if (g_switch_seen)
10430         as_bad (_("-G not supported in this configuration."));
10431       g_switch_value = 0;
10432     }
10433
10434   cpu = TARGET_CPU;
10435   if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
10436     {
10437       a = xmalloc (sizeof TARGET_CPU);
10438       strcpy (a, TARGET_CPU);
10439       a[(sizeof TARGET_CPU) - 3] = '\0';
10440       cpu = a;
10441     }
10442
10443   if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
10444     {
10445       target_cpu_had_mips16 = 1;
10446       cpu += sizeof "mips16" - 1;
10447     }
10448
10449   if (mips_opts.mips16 < 0)
10450     mips_opts.mips16 = target_cpu_had_mips16;
10451
10452   /* Backward compatibility for historic -mcpu= option.  Check for
10453      incompatible options, warn if -mcpu is used.  */
10454   if (mips_cpu != CPU_UNKNOWN
10455       && mips_arch != CPU_UNKNOWN
10456       && mips_cpu != mips_arch)
10457     {
10458       as_fatal (_("The -mcpu option can't be used together with -march. "
10459                   "Use -mtune instead of -mcpu."));
10460     }
10461
10462   if (mips_cpu != CPU_UNKNOWN
10463       && mips_tune != CPU_UNKNOWN
10464       && mips_cpu != mips_tune)
10465     {
10466       as_fatal (_("The -mcpu option can't be used together with -mtune. "
10467                   "Use -march instead of -mcpu."));
10468     }
10469
10470 #if 1
10471   /* For backward compatibility, let -mipsN set various defaults.  */
10472   /* This code should go away, to be replaced with something rather more
10473      draconian.  Until GCC 3.1 has been released for some reasonable
10474      amount of time, however, we need to support this.  */
10475   if (mips_opts.isa != ISA_UNKNOWN)
10476     {
10477       /* Translate -mipsN to the appropriate settings of file_mips_gp32
10478          and file_mips_fp32.  Tag binaries as using the mipsN ISA.  */
10479       if (file_mips_gp32 < 0)
10480         {
10481           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
10482             file_mips_gp32 = 0;
10483           else
10484             file_mips_gp32 = 1;
10485         }
10486       if (file_mips_fp32 < 0)
10487         {
10488           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
10489             file_mips_fp32 = 0;
10490           else
10491             file_mips_fp32 = 1;
10492         }
10493
10494       ci = mips_cpu_info_from_isa (mips_opts.isa);
10495       assert (ci != NULL);
10496       /* -mipsN has higher priority than -mcpu but lower than -march.  */
10497       if (mips_arch == CPU_UNKNOWN)
10498         mips_arch = ci->cpu;
10499
10500       /* Default mips_abi.  */
10501       if (mips_opts.abi == NO_ABI)
10502         {
10503           if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
10504             mips_opts.abi = O32_ABI;
10505           else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
10506             mips_opts.abi = O64_ABI;
10507         }
10508     }
10509
10510   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10511     {
10512       ci = mips_cpu_info_from_cpu (mips_cpu);
10513       assert (ci != NULL);
10514       mips_arch = ci->cpu;
10515       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
10516                  "-mtune instead."));
10517     }
10518
10519   /* Set tune from -mcpu, not from -mipsN.  */
10520   if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10521     {
10522       ci = mips_cpu_info_from_cpu (mips_cpu);
10523       assert (ci != NULL);
10524       mips_tune = ci->cpu;
10525     }
10526
10527   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
10528      specified on the command line, or some other value if one was.
10529      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
10530      the command line, or will be set otherwise if one was.  */
10531
10532   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10533     /* Handled above.  */;
10534 #else
10535   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10536     {
10537       ci = mips_cpu_info_from_cpu (mips_cpu);
10538       assert (ci != NULL);
10539       mips_arch = ci->cpu;
10540       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
10541                  "-mtune instead."));
10542     }
10543
10544   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
10545      specified on the command line, or some other value if one was.
10546      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
10547      the command line, or will be set otherwise if one was.  */
10548
10549   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10550     {
10551       /* We have to check if the isa is the default isa of arch.  Otherwise
10552          we'll get invalid object file headers.  */
10553       ci = mips_cpu_info_from_cpu (mips_arch);
10554       assert (ci != NULL);
10555       if (mips_opts.isa != ci->isa)
10556         {
10557           /* This really should be an error instead of a warning, but old
10558              compilers only have -mcpu which sets both arch and tune.  For
10559              now, we discard arch and preserve tune.  */
10560           as_warn (_("The -march option is incompatible to -mipsN and "
10561                      "therefore ignored."));
10562           if (mips_tune == CPU_UNKNOWN)
10563             mips_tune = mips_arch;
10564           ci = mips_cpu_info_from_isa (mips_opts.isa);
10565           assert (ci != NULL);
10566           mips_arch = ci->cpu;
10567         }
10568     }
10569 #endif
10570   else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
10571     {
10572       /* We have ARCH, we need ISA.  */
10573       ci = mips_cpu_info_from_cpu (mips_arch);
10574       assert (ci != NULL);
10575       mips_opts.isa = ci->isa;
10576     }
10577   else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10578     {
10579       /* We have ISA, we need default ARCH.  */
10580       ci = mips_cpu_info_from_isa (mips_opts.isa);
10581       assert (ci != NULL);
10582       mips_arch = ci->cpu;
10583     }
10584   else
10585     {
10586       /* We need to set both ISA and ARCH from target cpu.  */
10587       ci = mips_cpu_info_from_name (cpu);
10588       if (ci == NULL)
10589         ci = mips_cpu_info_from_cpu (CPU_R3000);
10590       assert (ci != NULL);
10591       mips_opts.isa = ci->isa;
10592       mips_arch = ci->cpu;
10593     }
10594
10595   if (mips_tune == CPU_UNKNOWN)
10596     mips_tune = mips_arch;
10597
10598   ci = mips_cpu_info_from_cpu (mips_arch);
10599   assert (ci != NULL);
10600   mips_isa_from_cpu = ci->isa;
10601
10602   /* End of TARGET_CPU processing, get rid of malloced memory
10603      if necessary.  */
10604   cpu = NULL;
10605   if (a != NULL)
10606     {
10607       free (a);
10608       a = NULL;
10609     }
10610
10611   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10612     as_bad (_("trap exception not supported at ISA 1"));
10613
10614   /* Set the EABI kind based on the ISA before the user gets
10615      to change the ISA with directives.  This isn't really
10616      the best, but then neither is basing the abi on the isa.  */
10617   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10618       && mips_opts.abi == EABI_ABI)
10619     mips_eabi64 = 1;
10620
10621   /* If they asked for mips1 or mips2 and a cpu that is
10622      mips3 or greater, then mark the object file 32BITMODE.  */
10623   if (mips_isa_from_cpu != ISA_UNKNOWN
10624       && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
10625       && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
10626     mips_32bitmode = 1;
10627
10628   /* If the selected architecture includes support for ASEs, enable
10629      generation of code for them.  */
10630   if (mips_opts.ase_mips3d == -1)
10631     mips_opts.ase_mips3d = CPU_HAS_MIPS3D (mips_arch);
10632   if (mips_opts.ase_mdmx == -1)
10633     mips_opts.ase_mdmx = CPU_HAS_MDMX (mips_arch);
10634
10635   if (file_mips_gp32 < 0)
10636     file_mips_gp32 = 0;
10637   if (file_mips_fp32 < 0)
10638     file_mips_fp32 = 0;
10639
10640   file_mips_isa = mips_opts.isa;
10641   file_mips_abi = mips_opts.abi;
10642   file_ase_mips3d = mips_opts.ase_mips3d;
10643   file_ase_mdmx = mips_opts.ase_mdmx;
10644   mips_opts.gp32 = file_mips_gp32;
10645   mips_opts.fp32 = file_mips_fp32;
10646
10647   if (HAVE_NEWABI)
10648     mips_big_got = 1;
10649 }
10650 \f
10651 void
10652 mips_init_after_args ()
10653 {
10654   /* initialize opcodes */
10655   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10656   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10657 }
10658
10659 long
10660 md_pcrel_from (fixP)
10661      fixS *fixP;
10662 {
10663   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10664       && fixP->fx_addsy != (symbolS *) NULL
10665       && ! S_IS_DEFINED (fixP->fx_addsy))
10666     {
10667       /* This makes a branch to an undefined symbol be a branch to the
10668          current location.  */
10669       if (mips_pic == EMBEDDED_PIC)
10670         return 4;
10671       else
10672         return 1;
10673     }
10674
10675   /* Return the address of the delay slot.  */
10676   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10677 }
10678
10679 /* This is called before the symbol table is processed.  In order to
10680    work with gcc when using mips-tfile, we must keep all local labels.
10681    However, in other cases, we want to discard them.  If we were
10682    called with -g, but we didn't see any debugging information, it may
10683    mean that gcc is smuggling debugging information through to
10684    mips-tfile, in which case we must generate all local labels.  */
10685
10686 void
10687 mips_frob_file_before_adjust ()
10688 {
10689 #ifndef NO_ECOFF_DEBUGGING
10690   if (ECOFF_DEBUGGING
10691       && mips_debug != 0
10692       && ! ecoff_debugging_seen)
10693     flag_keep_locals = 1;
10694 #endif
10695 }
10696
10697 /* Sort any unmatched HI16_S relocs so that they immediately precede
10698    the corresponding LO reloc.  This is called before md_apply_fix3 and
10699    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10700    explicit use of the %hi modifier.  */
10701
10702 void
10703 mips_frob_file ()
10704 {
10705   struct mips_hi_fixup *l;
10706
10707   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10708     {
10709       segment_info_type *seginfo;
10710       int pass;
10711
10712       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10713
10714       /* Check quickly whether the next fixup happens to be a matching
10715          %lo.  */
10716       if (l->fixp->fx_next != NULL
10717           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10718           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10719           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10720         continue;
10721
10722       /* Look through the fixups for this segment for a matching %lo.
10723          When we find one, move the %hi just in front of it.  We do
10724          this in two passes.  In the first pass, we try to find a
10725          unique %lo.  In the second pass, we permit multiple %hi
10726          relocs for a single %lo (this is a GNU extension).  */
10727       seginfo = seg_info (l->seg);
10728       for (pass = 0; pass < 2; pass++)
10729         {
10730           fixS *f, *prev;
10731
10732           prev = NULL;
10733           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10734             {
10735               /* Check whether this is a %lo fixup which matches l->fixp.  */
10736               if (f->fx_r_type == BFD_RELOC_LO16
10737                   && f->fx_addsy == l->fixp->fx_addsy
10738                   && f->fx_offset == l->fixp->fx_offset
10739                   && (pass == 1
10740                       || prev == NULL
10741                       || prev->fx_r_type != BFD_RELOC_HI16_S
10742                       || prev->fx_addsy != f->fx_addsy
10743                       || prev->fx_offset !=  f->fx_offset))
10744                 {
10745                   fixS **pf;
10746
10747                   /* Move l->fixp before f.  */
10748                   for (pf = &seginfo->fix_root;
10749                        *pf != l->fixp;
10750                        pf = &(*pf)->fx_next)
10751                     assert (*pf != NULL);
10752
10753                   *pf = l->fixp->fx_next;
10754
10755                   l->fixp->fx_next = f;
10756                   if (prev == NULL)
10757                     seginfo->fix_root = l->fixp;
10758                   else
10759                     prev->fx_next = l->fixp;
10760
10761                   break;
10762                 }
10763
10764               prev = f;
10765             }
10766
10767           if (f != NULL)
10768             break;
10769
10770 #if 0 /* GCC code motion plus incomplete dead code elimination
10771          can leave a %hi without a %lo.  */
10772           if (pass == 1)
10773             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10774                            _("Unmatched %%hi reloc"));
10775 #endif
10776         }
10777     }
10778 }
10779
10780 /* When generating embedded PIC code we need to use a special
10781    relocation to represent the difference of two symbols in the .text
10782    section (switch tables use a difference of this sort).  See
10783    include/coff/mips.h for details.  This macro checks whether this
10784    fixup requires the special reloc.  */
10785 #define SWITCH_TABLE(fixp) \
10786   ((fixp)->fx_r_type == BFD_RELOC_32 \
10787    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10788    && (fixp)->fx_addsy != NULL \
10789    && (fixp)->fx_subsy != NULL \
10790    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10791    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10792
10793 /* When generating embedded PIC code we must keep all PC relative
10794    relocations, in case the linker has to relax a call.  We also need
10795    to keep relocations for switch table entries.
10796
10797    We may have combined relocations without symbols in the N32/N64 ABI.
10798    We have to prevent gas from dropping them.  */
10799
10800 int
10801 mips_force_relocation (fixp)
10802      fixS *fixp;
10803 {
10804   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10805       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10806     return 1;
10807
10808   if (HAVE_NEWABI
10809       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10810       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10811           || fixp->fx_r_type == BFD_RELOC_HI16_S
10812           || fixp->fx_r_type == BFD_RELOC_LO16))
10813     return 1;
10814
10815   return (mips_pic == EMBEDDED_PIC
10816           && (fixp->fx_pcrel
10817               || SWITCH_TABLE (fixp)
10818               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10819               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10820 }
10821
10822 #ifdef OBJ_ELF
10823 static int
10824 mips_need_elf_addend_fixup (fixP)
10825      fixS *fixP;
10826 {
10827   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10828     return 1;
10829   if (mips_pic == EMBEDDED_PIC
10830       && S_IS_WEAK (fixP->fx_addsy))
10831     return 1;
10832   if (mips_pic != EMBEDDED_PIC
10833       && (S_IS_WEAK (fixP->fx_addsy)
10834           || S_IS_EXTERN (fixP->fx_addsy))
10835       && !S_IS_COMMON (fixP->fx_addsy))
10836     return 1;
10837   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10838       && (((bfd_get_section_flags (stdoutput,
10839                                    S_GET_SEGMENT (fixP->fx_addsy))
10840             & SEC_LINK_ONCE) != 0)
10841           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10842                        ".gnu.linkonce",
10843                        sizeof (".gnu.linkonce") - 1)))
10844     return 1;
10845   return 0;
10846 }
10847 #endif
10848
10849 /* Apply a fixup to the object file.  */
10850
10851 void
10852 md_apply_fix3 (fixP, valP, seg)
10853      fixS *fixP;
10854      valueT *valP;
10855      segT seg ATTRIBUTE_UNUSED;
10856 {
10857   bfd_byte *buf;
10858   long insn;
10859   valueT value;
10860
10861   assert (fixP->fx_size == 4
10862           || fixP->fx_r_type == BFD_RELOC_16
10863           || fixP->fx_r_type == BFD_RELOC_32
10864           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10865           || fixP->fx_r_type == BFD_RELOC_HI16_S
10866           || fixP->fx_r_type == BFD_RELOC_LO16
10867           || fixP->fx_r_type == BFD_RELOC_GPREL16
10868           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10869           || fixP->fx_r_type == BFD_RELOC_GPREL32
10870           || fixP->fx_r_type == BFD_RELOC_64
10871           || fixP->fx_r_type == BFD_RELOC_CTOR
10872           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10873           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10874           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10875           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10876           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10877           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10878           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10879           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10880           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10881
10882   value = *valP;
10883
10884   /* If we aren't adjusting this fixup to be against the section
10885      symbol, we need to adjust the value.  */
10886 #ifdef OBJ_ELF
10887   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10888     {
10889       if (mips_need_elf_addend_fixup (fixP))
10890         {
10891           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10892
10893           value -= symval;
10894           if (value != 0 && ! fixP->fx_pcrel)
10895             {
10896               /* In this case, the bfd_install_relocation routine will
10897                  incorrectly add the symbol value back in.  We just want
10898                  the addend to appear in the object file.  */
10899               value -= symval;
10900
10901               /* Make sure the addend is still non-zero.  If it became zero
10902                  after the last operation, set it to a spurious value and
10903                  subtract the same value from the object file's contents.  */
10904               if (value == 0)
10905                 {
10906                   value = 8;
10907
10908                   /* The in-place addends for LO16 relocations are signed;
10909                      leave the matching HI16 in-place addends as zero.  */
10910                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10911                     {
10912                       reloc_howto_type *howto;
10913                       bfd_vma contents, mask, field;
10914
10915                       howto = bfd_reloc_type_lookup (stdoutput,
10916                                                      fixP->fx_r_type);
10917
10918                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10919                                                + fixP->fx_where,
10920                                                fixP->fx_size * 8,
10921                                                target_big_endian);
10922
10923                       /* MASK has bits set where the relocation should go.
10924                          FIELD is -value, shifted into the appropriate place
10925                          for this relocation.  */
10926                       mask = 1 << (howto->bitsize - 1);
10927                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10928                       field = (-value >> howto->rightshift) << howto->bitpos;
10929
10930                       bfd_put_bits ((field & mask) | (contents & ~mask),
10931                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10932                                     fixP->fx_size * 8,
10933                                     target_big_endian);
10934                     }
10935                 }
10936             }
10937         }
10938
10939       /* This code was generated using trial and error and so is
10940          fragile and not trustworthy.  If you change it, you should
10941          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10942          they still pass.  */
10943       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10944         {
10945           value += fixP->fx_frag->fr_address + fixP->fx_where;
10946
10947           /* BFD's REL handling, for MIPS, is _very_ weird.
10948              This gives the right results, but it can't possibly
10949              be the way things are supposed to work.  */
10950           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10951                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10952               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10953             value += fixP->fx_frag->fr_address + fixP->fx_where;
10954         }
10955     }
10956 #endif
10957
10958   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10959
10960   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10961     fixP->fx_done = 1;
10962
10963   switch (fixP->fx_r_type)
10964     {
10965     case BFD_RELOC_MIPS_JMP:
10966     case BFD_RELOC_MIPS_SHIFT5:
10967     case BFD_RELOC_MIPS_SHIFT6:
10968     case BFD_RELOC_MIPS_GOT_DISP:
10969     case BFD_RELOC_MIPS_GOT_PAGE:
10970     case BFD_RELOC_MIPS_GOT_OFST:
10971     case BFD_RELOC_MIPS_SUB:
10972     case BFD_RELOC_MIPS_INSERT_A:
10973     case BFD_RELOC_MIPS_INSERT_B:
10974     case BFD_RELOC_MIPS_DELETE:
10975     case BFD_RELOC_MIPS_HIGHEST:
10976     case BFD_RELOC_MIPS_HIGHER:
10977     case BFD_RELOC_MIPS_SCN_DISP:
10978     case BFD_RELOC_MIPS_REL16:
10979     case BFD_RELOC_MIPS_RELGOT:
10980     case BFD_RELOC_MIPS_JALR:
10981     case BFD_RELOC_HI16:
10982     case BFD_RELOC_HI16_S:
10983     case BFD_RELOC_GPREL16:
10984     case BFD_RELOC_MIPS_LITERAL:
10985     case BFD_RELOC_MIPS_CALL16:
10986     case BFD_RELOC_MIPS_GOT16:
10987     case BFD_RELOC_GPREL32:
10988     case BFD_RELOC_MIPS_GOT_HI16:
10989     case BFD_RELOC_MIPS_GOT_LO16:
10990     case BFD_RELOC_MIPS_CALL_HI16:
10991     case BFD_RELOC_MIPS_CALL_LO16:
10992     case BFD_RELOC_MIPS16_GPREL:
10993       if (fixP->fx_pcrel)
10994         as_bad_where (fixP->fx_file, fixP->fx_line,
10995                       _("Invalid PC relative reloc"));
10996       /* Nothing needed to do. The value comes from the reloc entry */
10997       break;
10998
10999     case BFD_RELOC_MIPS16_JMP:
11000       /* We currently always generate a reloc against a symbol, which
11001          means that we don't want an addend even if the symbol is
11002          defined.  */
11003       fixP->fx_addnumber = 0;
11004       break;
11005
11006     case BFD_RELOC_PCREL_HI16_S:
11007       /* The addend for this is tricky if it is internal, so we just
11008          do everything here rather than in bfd_install_relocation.  */
11009       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11010           && !fixP->fx_done
11011           && value != 0)
11012         break;
11013       if (fixP->fx_addsy
11014           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11015         {
11016           /* For an external symbol adjust by the address to make it
11017              pcrel_offset.  We use the address of the RELLO reloc
11018              which follows this one.  */
11019           value += (fixP->fx_next->fx_frag->fr_address
11020                     + fixP->fx_next->fx_where);
11021         }
11022       value = ((value + 0x8000) >> 16) & 0xffff;
11023       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11024       if (target_big_endian)
11025         buf += 2;
11026       md_number_to_chars ((char *) buf, value, 2);
11027       break;
11028
11029     case BFD_RELOC_PCREL_LO16:
11030       /* The addend for this is tricky if it is internal, so we just
11031          do everything here rather than in bfd_install_relocation.  */
11032       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11033           && !fixP->fx_done
11034           && value != 0)
11035         break;
11036       if (fixP->fx_addsy
11037           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11038         value += fixP->fx_frag->fr_address + fixP->fx_where;
11039       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11040       if (target_big_endian)
11041         buf += 2;
11042       md_number_to_chars ((char *) buf, value, 2);
11043       break;
11044
11045     case BFD_RELOC_64:
11046       /* This is handled like BFD_RELOC_32, but we output a sign
11047          extended value if we are only 32 bits.  */
11048       if (fixP->fx_done
11049           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11050         {
11051           if (8 <= sizeof (valueT))
11052             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11053                                 value, 8);
11054           else
11055             {
11056               long w1, w2;
11057               long hiv;
11058
11059               w1 = w2 = fixP->fx_where;
11060               if (target_big_endian)
11061                 w1 += 4;
11062               else
11063                 w2 += 4;
11064               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11065               if ((value & 0x80000000) != 0)
11066                 hiv = 0xffffffff;
11067               else
11068                 hiv = 0;
11069               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11070             }
11071         }
11072       break;
11073
11074     case BFD_RELOC_RVA:
11075     case BFD_RELOC_32:
11076       /* If we are deleting this reloc entry, we must fill in the
11077          value now.  This can happen if we have a .word which is not
11078          resolved when it appears but is later defined.  We also need
11079          to fill in the value if this is an embedded PIC switch table
11080          entry.  */
11081       if (fixP->fx_done
11082           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11083         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11084                             value, 4);
11085       break;
11086
11087     case BFD_RELOC_16:
11088       /* If we are deleting this reloc entry, we must fill in the
11089          value now.  */
11090       assert (fixP->fx_size == 2);
11091       if (fixP->fx_done)
11092         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11093                             value, 2);
11094       break;
11095
11096     case BFD_RELOC_LO16:
11097       /* When handling an embedded PIC switch statement, we can wind
11098          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11099       if (fixP->fx_done)
11100         {
11101           if (value + 0x8000 > 0xffff)
11102             as_bad_where (fixP->fx_file, fixP->fx_line,
11103                           _("relocation overflow"));
11104           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11105           if (target_big_endian)
11106             buf += 2;
11107           md_number_to_chars ((char *) buf, value, 2);
11108         }
11109       break;
11110
11111     case BFD_RELOC_16_PCREL_S2:
11112       if ((value & 0x3) != 0)
11113         as_bad_where (fixP->fx_file, fixP->fx_line,
11114                       _("Branch to odd address (%lx)"), (long) value);
11115
11116       /* Fall through.  */
11117
11118     case BFD_RELOC_16_PCREL:
11119       /*
11120        * We need to save the bits in the instruction since fixup_segment()
11121        * might be deleting the relocation entry (i.e., a branch within
11122        * the current segment).
11123        */
11124       if (!fixP->fx_done && value != 0)
11125         break;
11126       /* If 'value' is zero, the remaining reloc code won't actually
11127          do the store, so it must be done here.  This is probably
11128          a bug somewhere.  */
11129       if (!fixP->fx_done
11130           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11131               || fixP->fx_addsy == NULL                 /* ??? */
11132               || ! S_IS_DEFINED (fixP->fx_addsy)))
11133         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11134
11135       value = (offsetT) value >> 2;
11136
11137       /* update old instruction data */
11138       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11139       if (target_big_endian)
11140         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11141       else
11142         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11143
11144       if (value + 0x8000 <= 0xffff)
11145         insn |= value & 0xffff;
11146       else
11147         {
11148           /* The branch offset is too large.  If this is an
11149              unconditional branch, and we are not generating PIC code,
11150              we can convert it to an absolute jump instruction.  */
11151           if (mips_pic == NO_PIC
11152               && fixP->fx_done
11153               && fixP->fx_frag->fr_address >= text_section->vma
11154               && (fixP->fx_frag->fr_address
11155                   < text_section->vma + text_section->_raw_size)
11156               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11157                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11158                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11159             {
11160               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11161                 insn = 0x0c000000;      /* jal */
11162               else
11163                 insn = 0x08000000;      /* j */
11164               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11165               fixP->fx_done = 0;
11166               fixP->fx_addsy = section_symbol (text_section);
11167               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11168             }
11169           else
11170             {
11171               /* FIXME.  It would be possible in principle to handle
11172                  conditional branches which overflow.  They could be
11173                  transformed into a branch around a jump.  This would
11174                  require setting up variant frags for each different
11175                  branch type.  The native MIPS assembler attempts to
11176                  handle these cases, but it appears to do it
11177                  incorrectly.  */
11178               as_bad_where (fixP->fx_file, fixP->fx_line,
11179                             _("Branch out of range"));
11180             }
11181         }
11182
11183       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11184       break;
11185
11186     case BFD_RELOC_VTABLE_INHERIT:
11187       fixP->fx_done = 0;
11188       if (fixP->fx_addsy
11189           && !S_IS_DEFINED (fixP->fx_addsy)
11190           && !S_IS_WEAK (fixP->fx_addsy))
11191         S_SET_WEAK (fixP->fx_addsy);
11192       break;
11193
11194     case BFD_RELOC_VTABLE_ENTRY:
11195       fixP->fx_done = 0;
11196       break;
11197
11198     default:
11199       internalError ();
11200     }
11201 }
11202
11203 #if 0
11204 void
11205 printInsn (oc)
11206      unsigned long oc;
11207 {
11208   const struct mips_opcode *p;
11209   int treg, sreg, dreg, shamt;
11210   short imm;
11211   const char *args;
11212   int i;
11213
11214   for (i = 0; i < NUMOPCODES; ++i)
11215     {
11216       p = &mips_opcodes[i];
11217       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11218         {
11219           printf ("%08lx %s\t", oc, p->name);
11220           treg = (oc >> 16) & 0x1f;
11221           sreg = (oc >> 21) & 0x1f;
11222           dreg = (oc >> 11) & 0x1f;
11223           shamt = (oc >> 6) & 0x1f;
11224           imm = oc;
11225           for (args = p->args;; ++args)
11226             {
11227               switch (*args)
11228                 {
11229                 case '\0':
11230                   printf ("\n");
11231                   break;
11232
11233                 case ',':
11234                 case '(':
11235                 case ')':
11236                   printf ("%c", *args);
11237                   continue;
11238
11239                 case 'r':
11240                   assert (treg == sreg);
11241                   printf ("$%d,$%d", treg, sreg);
11242                   continue;
11243
11244                 case 'd':
11245                 case 'G':
11246                   printf ("$%d", dreg);
11247                   continue;
11248
11249                 case 't':
11250                 case 'E':
11251                   printf ("$%d", treg);
11252                   continue;
11253
11254                 case 'k':
11255                   printf ("0x%x", treg);
11256                   continue;
11257
11258                 case 'b':
11259                 case 's':
11260                   printf ("$%d", sreg);
11261                   continue;
11262
11263                 case 'a':
11264                   printf ("0x%08lx", oc & 0x1ffffff);
11265                   continue;
11266
11267                 case 'i':
11268                 case 'j':
11269                 case 'o':
11270                 case 'u':
11271                   printf ("%d", imm);
11272                   continue;
11273
11274                 case '<':
11275                 case '>':
11276                   printf ("$%d", shamt);
11277                   continue;
11278
11279                 default:
11280                   internalError ();
11281                 }
11282               break;
11283             }
11284           return;
11285         }
11286     }
11287   printf (_("%08lx  UNDEFINED\n"), oc);
11288 }
11289 #endif
11290
11291 static symbolS *
11292 get_symbol ()
11293 {
11294   int c;
11295   char *name;
11296   symbolS *p;
11297
11298   name = input_line_pointer;
11299   c = get_symbol_end ();
11300   p = (symbolS *) symbol_find_or_make (name);
11301   *input_line_pointer = c;
11302   return p;
11303 }
11304
11305 /* Align the current frag to a given power of two.  The MIPS assembler
11306    also automatically adjusts any preceding label.  */
11307
11308 static void
11309 mips_align (to, fill, label)
11310      int to;
11311      int fill;
11312      symbolS *label;
11313 {
11314   mips_emit_delays (false);
11315   frag_align (to, fill, 0);
11316   record_alignment (now_seg, to);
11317   if (label != NULL)
11318     {
11319       assert (S_GET_SEGMENT (label) == now_seg);
11320       symbol_set_frag (label, frag_now);
11321       S_SET_VALUE (label, (valueT) frag_now_fix ());
11322     }
11323 }
11324
11325 /* Align to a given power of two.  .align 0 turns off the automatic
11326    alignment used by the data creating pseudo-ops.  */
11327
11328 static void
11329 s_align (x)
11330      int x ATTRIBUTE_UNUSED;
11331 {
11332   register int temp;
11333   register long temp_fill;
11334   long max_alignment = 15;
11335
11336   /*
11337
11338     o  Note that the assembler pulls down any immediately preceeding label
11339        to the aligned address.
11340     o  It's not documented but auto alignment is reinstated by
11341        a .align pseudo instruction.
11342     o  Note also that after auto alignment is turned off the mips assembler
11343        issues an error on attempt to assemble an improperly aligned data item.
11344        We don't.
11345
11346     */
11347
11348   temp = get_absolute_expression ();
11349   if (temp > max_alignment)
11350     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11351   else if (temp < 0)
11352     {
11353       as_warn (_("Alignment negative: 0 assumed."));
11354       temp = 0;
11355     }
11356   if (*input_line_pointer == ',')
11357     {
11358       ++input_line_pointer;
11359       temp_fill = get_absolute_expression ();
11360     }
11361   else
11362     temp_fill = 0;
11363   if (temp)
11364     {
11365       auto_align = 1;
11366       mips_align (temp, (int) temp_fill,
11367                   insn_labels != NULL ? insn_labels->label : NULL);
11368     }
11369   else
11370     {
11371       auto_align = 0;
11372     }
11373
11374   demand_empty_rest_of_line ();
11375 }
11376
11377 void
11378 mips_flush_pending_output ()
11379 {
11380   mips_emit_delays (false);
11381   mips_clear_insn_labels ();
11382 }
11383
11384 static void
11385 s_change_sec (sec)
11386      int sec;
11387 {
11388   segT seg;
11389
11390   /* When generating embedded PIC code, we only use the .text, .lit8,
11391      .sdata and .sbss sections.  We change the .data and .rdata
11392      pseudo-ops to use .sdata.  */
11393   if (mips_pic == EMBEDDED_PIC
11394       && (sec == 'd' || sec == 'r'))
11395     sec = 's';
11396
11397 #ifdef OBJ_ELF
11398   /* The ELF backend needs to know that we are changing sections, so
11399      that .previous works correctly.  We could do something like check
11400      for an obj_section_change_hook macro, but that might be confusing
11401      as it would not be appropriate to use it in the section changing
11402      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11403      This should be cleaner, somehow.  */
11404   obj_elf_section_change_hook ();
11405 #endif
11406
11407   mips_emit_delays (false);
11408   switch (sec)
11409     {
11410     case 't':
11411       s_text (0);
11412       break;
11413     case 'd':
11414       s_data (0);
11415       break;
11416     case 'b':
11417       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11418       demand_empty_rest_of_line ();
11419       break;
11420
11421     case 'r':
11422       if (USE_GLOBAL_POINTER_OPT)
11423         {
11424           seg = subseg_new (RDATA_SECTION_NAME,
11425                             (subsegT) get_absolute_expression ());
11426           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11427             {
11428               bfd_set_section_flags (stdoutput, seg,
11429                                      (SEC_ALLOC
11430                                       | SEC_LOAD
11431                                       | SEC_READONLY
11432                                       | SEC_RELOC
11433                                       | SEC_DATA));
11434               if (strcmp (TARGET_OS, "elf") != 0)
11435                 record_alignment (seg, 4);
11436             }
11437           demand_empty_rest_of_line ();
11438         }
11439       else
11440         {
11441           as_bad (_("No read only data section in this object file format"));
11442           demand_empty_rest_of_line ();
11443           return;
11444         }
11445       break;
11446
11447     case 's':
11448       if (USE_GLOBAL_POINTER_OPT)
11449         {
11450           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11451           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11452             {
11453               bfd_set_section_flags (stdoutput, seg,
11454                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11455                                      | SEC_DATA);
11456               if (strcmp (TARGET_OS, "elf") != 0)
11457                 record_alignment (seg, 4);
11458             }
11459           demand_empty_rest_of_line ();
11460           break;
11461         }
11462       else
11463         {
11464           as_bad (_("Global pointers not supported; recompile -G 0"));
11465           demand_empty_rest_of_line ();
11466           return;
11467         }
11468     }
11469
11470   auto_align = 1;
11471 }
11472
11473 void
11474 mips_enable_auto_align ()
11475 {
11476   auto_align = 1;
11477 }
11478
11479 static void
11480 s_cons (log_size)
11481      int log_size;
11482 {
11483   symbolS *label;
11484
11485   label = insn_labels != NULL ? insn_labels->label : NULL;
11486   mips_emit_delays (false);
11487   if (log_size > 0 && auto_align)
11488     mips_align (log_size, 0, label);
11489   mips_clear_insn_labels ();
11490   cons (1 << log_size);
11491 }
11492
11493 static void
11494 s_float_cons (type)
11495      int type;
11496 {
11497   symbolS *label;
11498
11499   label = insn_labels != NULL ? insn_labels->label : NULL;
11500
11501   mips_emit_delays (false);
11502
11503   if (auto_align)
11504     {
11505       if (type == 'd')
11506         mips_align (3, 0, label);
11507       else
11508         mips_align (2, 0, label);
11509     }
11510
11511   mips_clear_insn_labels ();
11512
11513   float_cons (type);
11514 }
11515
11516 /* Handle .globl.  We need to override it because on Irix 5 you are
11517    permitted to say
11518        .globl foo .text
11519    where foo is an undefined symbol, to mean that foo should be
11520    considered to be the address of a function.  */
11521
11522 static void
11523 s_mips_globl (x)
11524      int x ATTRIBUTE_UNUSED;
11525 {
11526   char *name;
11527   int c;
11528   symbolS *symbolP;
11529   flagword flag;
11530
11531   name = input_line_pointer;
11532   c = get_symbol_end ();
11533   symbolP = symbol_find_or_make (name);
11534   *input_line_pointer = c;
11535   SKIP_WHITESPACE ();
11536
11537   /* On Irix 5, every global symbol that is not explicitly labelled as
11538      being a function is apparently labelled as being an object.  */
11539   flag = BSF_OBJECT;
11540
11541   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11542     {
11543       char *secname;
11544       asection *sec;
11545
11546       secname = input_line_pointer;
11547       c = get_symbol_end ();
11548       sec = bfd_get_section_by_name (stdoutput, secname);
11549       if (sec == NULL)
11550         as_bad (_("%s: no such section"), secname);
11551       *input_line_pointer = c;
11552
11553       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11554         flag = BSF_FUNCTION;
11555     }
11556
11557   symbol_get_bfdsym (symbolP)->flags |= flag;
11558
11559   S_SET_EXTERNAL (symbolP);
11560   demand_empty_rest_of_line ();
11561 }
11562
11563 static void
11564 s_option (x)
11565      int x ATTRIBUTE_UNUSED;
11566 {
11567   char *opt;
11568   char c;
11569
11570   opt = input_line_pointer;
11571   c = get_symbol_end ();
11572
11573   if (*opt == 'O')
11574     {
11575       /* FIXME: What does this mean?  */
11576     }
11577   else if (strncmp (opt, "pic", 3) == 0)
11578     {
11579       int i;
11580
11581       i = atoi (opt + 3);
11582       if (i == 0)
11583         mips_pic = NO_PIC;
11584       else if (i == 2)
11585         mips_pic = SVR4_PIC;
11586       else
11587         as_bad (_(".option pic%d not supported"), i);
11588
11589       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11590         {
11591           if (g_switch_seen && g_switch_value != 0)
11592             as_warn (_("-G may not be used with SVR4 PIC code"));
11593           g_switch_value = 0;
11594           bfd_set_gp_size (stdoutput, 0);
11595         }
11596     }
11597   else
11598     as_warn (_("Unrecognized option \"%s\""), opt);
11599
11600   *input_line_pointer = c;
11601   demand_empty_rest_of_line ();
11602 }
11603
11604 /* This structure is used to hold a stack of .set values.  */
11605
11606 struct mips_option_stack
11607 {
11608   struct mips_option_stack *next;
11609   struct mips_set_options options;
11610 };
11611
11612 static struct mips_option_stack *mips_opts_stack;
11613
11614 /* Handle the .set pseudo-op.  */
11615
11616 static void
11617 s_mipsset (x)
11618      int x ATTRIBUTE_UNUSED;
11619 {
11620   char *name = input_line_pointer, ch;
11621
11622   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11623     ++input_line_pointer;
11624   ch = *input_line_pointer;
11625   *input_line_pointer = '\0';
11626
11627   if (strcmp (name, "reorder") == 0)
11628     {
11629       if (mips_opts.noreorder && prev_nop_frag != NULL)
11630         {
11631           /* If we still have pending nops, we can discard them.  The
11632              usual nop handling will insert any that are still
11633              needed.  */
11634           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11635                                     * (mips_opts.mips16 ? 2 : 4));
11636           prev_nop_frag = NULL;
11637         }
11638       mips_opts.noreorder = 0;
11639     }
11640   else if (strcmp (name, "noreorder") == 0)
11641     {
11642       mips_emit_delays (true);
11643       mips_opts.noreorder = 1;
11644       mips_any_noreorder = 1;
11645     }
11646   else if (strcmp (name, "at") == 0)
11647     {
11648       mips_opts.noat = 0;
11649     }
11650   else if (strcmp (name, "noat") == 0)
11651     {
11652       mips_opts.noat = 1;
11653     }
11654   else if (strcmp (name, "macro") == 0)
11655     {
11656       mips_opts.warn_about_macros = 0;
11657     }
11658   else if (strcmp (name, "nomacro") == 0)
11659     {
11660       if (mips_opts.noreorder == 0)
11661         as_bad (_("`noreorder' must be set before `nomacro'"));
11662       mips_opts.warn_about_macros = 1;
11663     }
11664   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11665     {
11666       mips_opts.nomove = 0;
11667     }
11668   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11669     {
11670       mips_opts.nomove = 1;
11671     }
11672   else if (strcmp (name, "bopt") == 0)
11673     {
11674       mips_opts.nobopt = 0;
11675     }
11676   else if (strcmp (name, "nobopt") == 0)
11677     {
11678       mips_opts.nobopt = 1;
11679     }
11680   else if (strcmp (name, "mdmx") == 0)
11681     mips_opts.ase_mdmx = 1;
11682   else if (strcmp (name, "nomdmx") == 0)
11683     mips_opts.ase_mdmx = 0;
11684   else if (strcmp (name, "mips16") == 0
11685            || strcmp (name, "MIPS-16") == 0)
11686     mips_opts.mips16 = 1;
11687   else if (strcmp (name, "nomips16") == 0
11688            || strcmp (name, "noMIPS-16") == 0)
11689     mips_opts.mips16 = 0;
11690   else if (strcmp (name, "mips3d") == 0)
11691     mips_opts.ase_mips3d = 1;
11692   else if (strcmp (name, "nomips3d") == 0)
11693     mips_opts.ase_mips3d = 0;
11694   else if (strncmp (name, "mips", 4) == 0)
11695     {
11696       int isa;
11697
11698       /* Permit the user to change the ISA on the fly.  Needless to
11699          say, misuse can cause serious problems.  */
11700       isa = atoi (name + 4);
11701       switch (isa)
11702         {
11703         case  0:
11704           mips_opts.gp32 = file_mips_gp32;
11705           mips_opts.fp32 = file_mips_fp32;
11706           mips_opts.abi = file_mips_abi;
11707           break;
11708         case  1:
11709         case  2:
11710         case 32:
11711           mips_opts.gp32 = 1;
11712           mips_opts.fp32 = 1;
11713           break;
11714         case  3:
11715         case  4:
11716         case  5:
11717         case 64:
11718           /* Loosen ABI register width restriction.  */
11719           if (mips_opts.abi == O32_ABI)
11720             mips_opts.abi = NO_ABI;
11721           mips_opts.gp32 = 0;
11722           mips_opts.fp32 = 0;
11723           break;
11724         default:
11725           as_bad (_("unknown ISA level %s"), name + 4);
11726           break;
11727         }
11728
11729       switch (isa)
11730         {
11731         case  0: mips_opts.isa = file_mips_isa;   break;
11732         case  1: mips_opts.isa = ISA_MIPS1;       break;
11733         case  2: mips_opts.isa = ISA_MIPS2;       break;
11734         case  3: mips_opts.isa = ISA_MIPS3;       break;
11735         case  4: mips_opts.isa = ISA_MIPS4;       break;
11736         case  5: mips_opts.isa = ISA_MIPS5;       break;
11737         case 32: mips_opts.isa = ISA_MIPS32;      break;
11738         case 64: mips_opts.isa = ISA_MIPS64;      break;
11739         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11740         }
11741     }
11742   else if (strcmp (name, "autoextend") == 0)
11743     mips_opts.noautoextend = 0;
11744   else if (strcmp (name, "noautoextend") == 0)
11745     mips_opts.noautoextend = 1;
11746   else if (strcmp (name, "push") == 0)
11747     {
11748       struct mips_option_stack *s;
11749
11750       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11751       s->next = mips_opts_stack;
11752       s->options = mips_opts;
11753       mips_opts_stack = s;
11754     }
11755   else if (strcmp (name, "pop") == 0)
11756     {
11757       struct mips_option_stack *s;
11758
11759       s = mips_opts_stack;
11760       if (s == NULL)
11761         as_bad (_(".set pop with no .set push"));
11762       else
11763         {
11764           /* If we're changing the reorder mode we need to handle
11765              delay slots correctly.  */
11766           if (s->options.noreorder && ! mips_opts.noreorder)
11767             mips_emit_delays (true);
11768           else if (! s->options.noreorder && mips_opts.noreorder)
11769             {
11770               if (prev_nop_frag != NULL)
11771                 {
11772                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11773                                             * (mips_opts.mips16 ? 2 : 4));
11774                   prev_nop_frag = NULL;
11775                 }
11776             }
11777
11778           mips_opts = s->options;
11779           mips_opts_stack = s->next;
11780           free (s);
11781         }
11782     }
11783   else
11784     {
11785       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11786     }
11787   *input_line_pointer = ch;
11788   demand_empty_rest_of_line ();
11789 }
11790
11791 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11792    .option pic2.  It means to generate SVR4 PIC calls.  */
11793
11794 static void
11795 s_abicalls (ignore)
11796      int ignore ATTRIBUTE_UNUSED;
11797 {
11798   mips_pic = SVR4_PIC;
11799   if (USE_GLOBAL_POINTER_OPT)
11800     {
11801       if (g_switch_seen && g_switch_value != 0)
11802         as_warn (_("-G may not be used with SVR4 PIC code"));
11803       g_switch_value = 0;
11804     }
11805   bfd_set_gp_size (stdoutput, 0);
11806   demand_empty_rest_of_line ();
11807 }
11808
11809 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11810    PIC code.  It sets the $gp register for the function based on the
11811    function address, which is in the register named in the argument.
11812    This uses a relocation against _gp_disp, which is handled specially
11813    by the linker.  The result is:
11814         lui     $gp,%hi(_gp_disp)
11815         addiu   $gp,$gp,%lo(_gp_disp)
11816         addu    $gp,$gp,.cpload argument
11817    The .cpload argument is normally $25 == $t9.  */
11818
11819 static void
11820 s_cpload (ignore)
11821      int ignore ATTRIBUTE_UNUSED;
11822 {
11823   expressionS ex;
11824   int icnt = 0;
11825
11826   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11827      .cpload is ignored.  */
11828   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11829     {
11830       s_ignore (0);
11831       return;
11832     }
11833
11834   /* .cpload should be in a .set noreorder section.  */
11835   if (mips_opts.noreorder == 0)
11836     as_warn (_(".cpload not in noreorder section"));
11837
11838   ex.X_op = O_symbol;
11839   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11840   ex.X_op_symbol = NULL;
11841   ex.X_add_number = 0;
11842
11843   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11844   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11845
11846   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11847   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11848                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11849
11850   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11851                mips_gp_register, mips_gp_register, tc_get_register (0));
11852
11853   demand_empty_rest_of_line ();
11854 }
11855
11856 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11857      .cpsetup $reg1, offset|$reg2, label
11858
11859    If offset is given, this results in:
11860      sd         $gp, offset($sp)
11861      lui        $gp, %hi(%neg(%gp_rel(label)))
11862      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11863      daddu      $gp, $gp, $reg1
11864
11865    If $reg2 is given, this results in:
11866      daddu      $reg2, $gp, $0
11867      lui        $gp, %hi(%neg(%gp_rel(label)))
11868      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11869      daddu      $gp, $gp, $reg1
11870    $reg1 is normally $25 == $t9.  */
11871 static void
11872 s_cpsetup (ignore)
11873      int ignore ATTRIBUTE_UNUSED;
11874 {
11875   expressionS ex_off;
11876   expressionS ex_sym;
11877   int reg1;
11878   int icnt = 0;
11879   char *sym;
11880
11881   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11882      We also need NewABI support.  */
11883   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11884     {
11885       s_ignore (0);
11886       return;
11887     }
11888
11889   reg1 = tc_get_register (0);
11890   SKIP_WHITESPACE ();
11891   if (*input_line_pointer != ',')
11892     {
11893       as_bad (_("missing argument separator ',' for .cpsetup"));
11894       return;
11895     }
11896   else
11897     ++input_line_pointer;
11898   SKIP_WHITESPACE ();
11899   if (*input_line_pointer == '$')
11900     {
11901       mips_cpreturn_register = tc_get_register (0);
11902       mips_cpreturn_offset = -1;
11903     }
11904   else
11905     {
11906       mips_cpreturn_offset = get_absolute_expression ();
11907       mips_cpreturn_register = -1;
11908     }
11909   SKIP_WHITESPACE ();
11910   if (*input_line_pointer != ',')
11911     {
11912       as_bad (_("missing argument separator ',' for .cpsetup"));
11913       return;
11914     }
11915   else
11916     ++input_line_pointer;
11917   SKIP_WHITESPACE ();
11918   sym = input_line_pointer;
11919   while (ISALNUM (*input_line_pointer))
11920     ++input_line_pointer;
11921   *input_line_pointer = 0;
11922
11923   ex_sym.X_op = O_symbol;
11924   ex_sym.X_add_symbol = symbol_find_or_make (sym);
11925   ex_sym.X_op_symbol = NULL;
11926   ex_sym.X_add_number = 0;
11927
11928   if (mips_cpreturn_register == -1)
11929     {
11930       ex_off.X_op = O_constant;
11931       ex_off.X_add_symbol = NULL;
11932       ex_off.X_op_symbol = NULL;
11933       ex_off.X_add_number = mips_cpreturn_offset;
11934
11935       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11936                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11937     }
11938   else
11939     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11940                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11941
11942   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11943                (int) BFD_RELOC_GPREL16);
11944   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11945   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_HI16_S);
11946   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11947                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11948   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11949   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_LO16);
11950   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11951                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11952                mips_gp_register, mips_gp_register, reg1);
11953
11954   demand_empty_rest_of_line ();
11955 }
11956
11957 static void
11958 s_cplocal (ignore)
11959      int ignore ATTRIBUTE_UNUSED;
11960 {
11961   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11962    .cplocal is ignored.  */
11963   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11964     {
11965       s_ignore (0);
11966       return;
11967     }
11968
11969   mips_gp_register = tc_get_register (0);
11970   demand_empty_rest_of_line ();
11971 }
11972
11973 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11974    offset from $sp.  The offset is remembered, and after making a PIC
11975    call $gp is restored from that location.  */
11976
11977 static void
11978 s_cprestore (ignore)
11979      int ignore ATTRIBUTE_UNUSED;
11980 {
11981   expressionS ex;
11982   int icnt = 0;
11983
11984   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11985      .cprestore is ignored.  */
11986   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11987     {
11988       s_ignore (0);
11989       return;
11990     }
11991
11992   mips_cprestore_offset = get_absolute_expression ();
11993   mips_cprestore_valid = 1;
11994
11995   ex.X_op = O_constant;
11996   ex.X_add_symbol = NULL;
11997   ex.X_op_symbol = NULL;
11998   ex.X_add_number = mips_cprestore_offset;
11999
12000   macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12001                "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
12002
12003   demand_empty_rest_of_line ();
12004 }
12005
12006 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12007    was given in the preceeding .gpsetup, it results in:
12008      ld         $gp, offset($sp)
12009
12010    If a register $reg2 was given there, it results in:
12011      daddiu     $gp, $gp, $reg2
12012  */
12013 static void
12014 s_cpreturn (ignore)
12015      int ignore ATTRIBUTE_UNUSED;
12016 {
12017   expressionS ex;
12018   int icnt = 0;
12019
12020   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12021      We also need NewABI support.  */
12022   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12023     {
12024       s_ignore (0);
12025       return;
12026     }
12027
12028   if (mips_cpreturn_register == -1)
12029     {
12030       ex.X_op = O_constant;
12031       ex.X_add_symbol = NULL;
12032       ex.X_op_symbol = NULL;
12033       ex.X_add_number = mips_cpreturn_offset;
12034
12035       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12036                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12037     }
12038   else
12039     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12040                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12041
12042   demand_empty_rest_of_line ();
12043 }
12044
12045 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12046    code.  It sets the offset to use in gp_rel relocations.  */
12047
12048 static void
12049 s_gpvalue (ignore)
12050      int ignore ATTRIBUTE_UNUSED;
12051 {
12052   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12053      We also need NewABI support.  */
12054   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12055     {
12056       s_ignore (0);
12057       return;
12058     }
12059
12060   mips_gprel_offset = get_absolute_expression ();
12061
12062   demand_empty_rest_of_line ();
12063 }
12064
12065 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12066    code.  It generates a 32 bit GP relative reloc.  */
12067
12068 static void
12069 s_gpword (ignore)
12070      int ignore ATTRIBUTE_UNUSED;
12071 {
12072   symbolS *label;
12073   expressionS ex;
12074   char *p;
12075
12076   /* When not generating PIC code, this is treated as .word.  */
12077   if (mips_pic != SVR4_PIC)
12078     {
12079       s_cons (2);
12080       return;
12081     }
12082
12083   label = insn_labels != NULL ? insn_labels->label : NULL;
12084   mips_emit_delays (true);
12085   if (auto_align)
12086     mips_align (2, 0, label);
12087   mips_clear_insn_labels ();
12088
12089   expression (&ex);
12090
12091   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12092     {
12093       as_bad (_("Unsupported use of .gpword"));
12094       ignore_rest_of_line ();
12095     }
12096
12097   p = frag_more (4);
12098   md_number_to_chars (p, (valueT) 0, 4);
12099   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12100                BFD_RELOC_GPREL32);
12101
12102   demand_empty_rest_of_line ();
12103 }
12104
12105 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12106    tables in SVR4 PIC code.  */
12107
12108 static void
12109 s_cpadd (ignore)
12110      int ignore ATTRIBUTE_UNUSED;
12111 {
12112   int icnt = 0;
12113   int reg;
12114
12115   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12116      code.  */
12117   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12118     {
12119       s_ignore (0);
12120       return;
12121     }
12122
12123   /* Add $gp to the register named as an argument.  */
12124   reg = tc_get_register (0);
12125   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12126                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12127                "d,v,t", reg, reg, mips_gp_register);
12128
12129   demand_empty_rest_of_line ();
12130 }
12131
12132 /* Handle the .insn pseudo-op.  This marks instruction labels in
12133    mips16 mode.  This permits the linker to handle them specially,
12134    such as generating jalx instructions when needed.  We also make
12135    them odd for the duration of the assembly, in order to generate the
12136    right sort of code.  We will make them even in the adjust_symtab
12137    routine, while leaving them marked.  This is convenient for the
12138    debugger and the disassembler.  The linker knows to make them odd
12139    again.  */
12140
12141 static void
12142 s_insn (ignore)
12143      int ignore ATTRIBUTE_UNUSED;
12144 {
12145   mips16_mark_labels ();
12146
12147   demand_empty_rest_of_line ();
12148 }
12149
12150 /* Handle a .stabn directive.  We need these in order to mark a label
12151    as being a mips16 text label correctly.  Sometimes the compiler
12152    will emit a label, followed by a .stabn, and then switch sections.
12153    If the label and .stabn are in mips16 mode, then the label is
12154    really a mips16 text label.  */
12155
12156 static void
12157 s_mips_stab (type)
12158      int type;
12159 {
12160   if (type == 'n')
12161     mips16_mark_labels ();
12162
12163   s_stab (type);
12164 }
12165
12166 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12167  */
12168
12169 static void
12170 s_mips_weakext (ignore)
12171      int ignore ATTRIBUTE_UNUSED;
12172 {
12173   char *name;
12174   int c;
12175   symbolS *symbolP;
12176   expressionS exp;
12177
12178   name = input_line_pointer;
12179   c = get_symbol_end ();
12180   symbolP = symbol_find_or_make (name);
12181   S_SET_WEAK (symbolP);
12182   *input_line_pointer = c;
12183
12184   SKIP_WHITESPACE ();
12185
12186   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12187     {
12188       if (S_IS_DEFINED (symbolP))
12189         {
12190           as_bad ("ignoring attempt to redefine symbol %s",
12191                   S_GET_NAME (symbolP));
12192           ignore_rest_of_line ();
12193           return;
12194         }
12195
12196       if (*input_line_pointer == ',')
12197         {
12198           ++input_line_pointer;
12199           SKIP_WHITESPACE ();
12200         }
12201
12202       expression (&exp);
12203       if (exp.X_op != O_symbol)
12204         {
12205           as_bad ("bad .weakext directive");
12206           ignore_rest_of_line ();
12207           return;
12208         }
12209       symbol_set_value_expression (symbolP, &exp);
12210     }
12211
12212   demand_empty_rest_of_line ();
12213 }
12214
12215 /* Parse a register string into a number.  Called from the ECOFF code
12216    to parse .frame.  The argument is non-zero if this is the frame
12217    register, so that we can record it in mips_frame_reg.  */
12218
12219 int
12220 tc_get_register (frame)
12221      int frame;
12222 {
12223   int reg;
12224
12225   SKIP_WHITESPACE ();
12226   if (*input_line_pointer++ != '$')
12227     {
12228       as_warn (_("expected `$'"));
12229       reg = ZERO;
12230     }
12231   else if (ISDIGIT (*input_line_pointer))
12232     {
12233       reg = get_absolute_expression ();
12234       if (reg < 0 || reg >= 32)
12235         {
12236           as_warn (_("Bad register number"));
12237           reg = ZERO;
12238         }
12239     }
12240   else
12241     {
12242       if (strncmp (input_line_pointer, "ra", 2) == 0)
12243         {
12244           reg = RA;
12245           input_line_pointer += 2;
12246         }
12247       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12248         {
12249           reg = FP;
12250           input_line_pointer += 2;
12251         }
12252       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12253         {
12254           reg = SP;
12255           input_line_pointer += 2;
12256         }
12257       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12258         {
12259           reg = GP;
12260           input_line_pointer += 2;
12261         }
12262       else if (strncmp (input_line_pointer, "at", 2) == 0)
12263         {
12264           reg = AT;
12265           input_line_pointer += 2;
12266         }
12267       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12268         {
12269           reg = KT0;
12270           input_line_pointer += 3;
12271         }
12272       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12273         {
12274           reg = KT1;
12275           input_line_pointer += 3;
12276         }
12277       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12278         {
12279           reg = ZERO;
12280           input_line_pointer += 4;
12281         }
12282       else
12283         {
12284           as_warn (_("Unrecognized register name"));
12285           reg = ZERO;
12286           while (ISALNUM(*input_line_pointer))
12287            input_line_pointer++;
12288         }
12289     }
12290   if (frame)
12291     {
12292       mips_frame_reg = reg != 0 ? reg : SP;
12293       mips_frame_reg_valid = 1;
12294       mips_cprestore_valid = 0;
12295     }
12296   return reg;
12297 }
12298
12299 valueT
12300 md_section_align (seg, addr)
12301      asection *seg;
12302      valueT addr;
12303 {
12304   int align = bfd_get_section_alignment (stdoutput, seg);
12305
12306 #ifdef OBJ_ELF
12307   /* We don't need to align ELF sections to the full alignment.
12308      However, Irix 5 may prefer that we align them at least to a 16
12309      byte boundary.  We don't bother to align the sections if we are
12310      targeted for an embedded system.  */
12311   if (strcmp (TARGET_OS, "elf") == 0)
12312     return addr;
12313   if (align > 4)
12314     align = 4;
12315 #endif
12316
12317   return ((addr + (1 << align) - 1) & (-1 << align));
12318 }
12319
12320 /* Utility routine, called from above as well.  If called while the
12321    input file is still being read, it's only an approximation.  (For
12322    example, a symbol may later become defined which appeared to be
12323    undefined earlier.)  */
12324
12325 static int
12326 nopic_need_relax (sym, before_relaxing)
12327      symbolS *sym;
12328      int before_relaxing;
12329 {
12330   if (sym == 0)
12331     return 0;
12332
12333   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12334     {
12335       const char *symname;
12336       int change;
12337
12338       /* Find out whether this symbol can be referenced off the $gp
12339          register.  It can be if it is smaller than the -G size or if
12340          it is in the .sdata or .sbss section.  Certain symbols can
12341          not be referenced off the $gp, although it appears as though
12342          they can.  */
12343       symname = S_GET_NAME (sym);
12344       if (symname != (const char *) NULL
12345           && (strcmp (symname, "eprol") == 0
12346               || strcmp (symname, "etext") == 0
12347               || strcmp (symname, "_gp") == 0
12348               || strcmp (symname, "edata") == 0
12349               || strcmp (symname, "_fbss") == 0
12350               || strcmp (symname, "_fdata") == 0
12351               || strcmp (symname, "_ftext") == 0
12352               || strcmp (symname, "end") == 0
12353               || strcmp (symname, "_gp_disp") == 0))
12354         change = 1;
12355       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12356                && (0
12357 #ifndef NO_ECOFF_DEBUGGING
12358                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12359                        && (symbol_get_obj (sym)->ecoff_extern_size
12360                            <= g_switch_value))
12361 #endif
12362                    /* We must defer this decision until after the whole
12363                       file has been read, since there might be a .extern
12364                       after the first use of this symbol.  */
12365                    || (before_relaxing
12366 #ifndef NO_ECOFF_DEBUGGING
12367                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12368 #endif
12369                        && S_GET_VALUE (sym) == 0)
12370                    || (S_GET_VALUE (sym) != 0
12371                        && S_GET_VALUE (sym) <= g_switch_value)))
12372         change = 0;
12373       else
12374         {
12375           const char *segname;
12376
12377           segname = segment_name (S_GET_SEGMENT (sym));
12378           assert (strcmp (segname, ".lit8") != 0
12379                   && strcmp (segname, ".lit4") != 0);
12380           change = (strcmp (segname, ".sdata") != 0
12381                     && strcmp (segname, ".sbss") != 0
12382                     && strncmp (segname, ".sdata.", 7) != 0
12383                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12384         }
12385       return change;
12386     }
12387   else
12388     /* We are not optimizing for the $gp register.  */
12389     return 1;
12390 }
12391
12392 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12393    extended opcode.  SEC is the section the frag is in.  */
12394
12395 static int
12396 mips16_extended_frag (fragp, sec, stretch)
12397      fragS *fragp;
12398      asection *sec;
12399      long stretch;
12400 {
12401   int type;
12402   register const struct mips16_immed_operand *op;
12403   offsetT val;
12404   int mintiny, maxtiny;
12405   segT symsec;
12406   fragS *sym_frag;
12407
12408   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12409     return 0;
12410   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12411     return 1;
12412
12413   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12414   op = mips16_immed_operands;
12415   while (op->type != type)
12416     {
12417       ++op;
12418       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12419     }
12420
12421   if (op->unsp)
12422     {
12423       if (type == '<' || type == '>' || type == '[' || type == ']')
12424         {
12425           mintiny = 1;
12426           maxtiny = 1 << op->nbits;
12427         }
12428       else
12429         {
12430           mintiny = 0;
12431           maxtiny = (1 << op->nbits) - 1;
12432         }
12433     }
12434   else
12435     {
12436       mintiny = - (1 << (op->nbits - 1));
12437       maxtiny = (1 << (op->nbits - 1)) - 1;
12438     }
12439
12440   sym_frag = symbol_get_frag (fragp->fr_symbol);
12441   val = S_GET_VALUE (fragp->fr_symbol);
12442   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12443
12444   if (op->pcrel)
12445     {
12446       addressT addr;
12447
12448       /* We won't have the section when we are called from
12449          mips_relax_frag.  However, we will always have been called
12450          from md_estimate_size_before_relax first.  If this is a
12451          branch to a different section, we mark it as such.  If SEC is
12452          NULL, and the frag is not marked, then it must be a branch to
12453          the same section.  */
12454       if (sec == NULL)
12455         {
12456           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12457             return 1;
12458         }
12459       else
12460         {
12461           /* Must have been called from md_estimate_size_before_relax.  */
12462           if (symsec != sec)
12463             {
12464               fragp->fr_subtype =
12465                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12466
12467               /* FIXME: We should support this, and let the linker
12468                  catch branches and loads that are out of range.  */
12469               as_bad_where (fragp->fr_file, fragp->fr_line,
12470                             _("unsupported PC relative reference to different section"));
12471
12472               return 1;
12473             }
12474           if (fragp != sym_frag && sym_frag->fr_address == 0)
12475             /* Assume non-extended on the first relaxation pass.
12476                The address we have calculated will be bogus if this is
12477                a forward branch to another frag, as the forward frag
12478                will have fr_address == 0.  */
12479             return 0;
12480         }
12481
12482       /* In this case, we know for sure that the symbol fragment is in
12483          the same section.  If the relax_marker of the symbol fragment
12484          differs from the relax_marker of this fragment, we have not
12485          yet adjusted the symbol fragment fr_address.  We want to add
12486          in STRETCH in order to get a better estimate of the address.
12487          This particularly matters because of the shift bits.  */
12488       if (stretch != 0
12489           && sym_frag->relax_marker != fragp->relax_marker)
12490         {
12491           fragS *f;
12492
12493           /* Adjust stretch for any alignment frag.  Note that if have
12494              been expanding the earlier code, the symbol may be
12495              defined in what appears to be an earlier frag.  FIXME:
12496              This doesn't handle the fr_subtype field, which specifies
12497              a maximum number of bytes to skip when doing an
12498              alignment.  */
12499           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12500             {
12501               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12502                 {
12503                   if (stretch < 0)
12504                     stretch = - ((- stretch)
12505                                  & ~ ((1 << (int) f->fr_offset) - 1));
12506                   else
12507                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12508                   if (stretch == 0)
12509                     break;
12510                 }
12511             }
12512           if (f != NULL)
12513             val += stretch;
12514         }
12515
12516       addr = fragp->fr_address + fragp->fr_fix;
12517
12518       /* The base address rules are complicated.  The base address of
12519          a branch is the following instruction.  The base address of a
12520          PC relative load or add is the instruction itself, but if it
12521          is in a delay slot (in which case it can not be extended) use
12522          the address of the instruction whose delay slot it is in.  */
12523       if (type == 'p' || type == 'q')
12524         {
12525           addr += 2;
12526
12527           /* If we are currently assuming that this frag should be
12528              extended, then, the current address is two bytes
12529              higher.  */
12530           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12531             addr += 2;
12532
12533           /* Ignore the low bit in the target, since it will be set
12534              for a text label.  */
12535           if ((val & 1) != 0)
12536             --val;
12537         }
12538       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12539         addr -= 4;
12540       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12541         addr -= 2;
12542
12543       val -= addr & ~ ((1 << op->shift) - 1);
12544
12545       /* Branch offsets have an implicit 0 in the lowest bit.  */
12546       if (type == 'p' || type == 'q')
12547         val /= 2;
12548
12549       /* If any of the shifted bits are set, we must use an extended
12550          opcode.  If the address depends on the size of this
12551          instruction, this can lead to a loop, so we arrange to always
12552          use an extended opcode.  We only check this when we are in
12553          the main relaxation loop, when SEC is NULL.  */
12554       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12555         {
12556           fragp->fr_subtype =
12557             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12558           return 1;
12559         }
12560
12561       /* If we are about to mark a frag as extended because the value
12562          is precisely maxtiny + 1, then there is a chance of an
12563          infinite loop as in the following code:
12564              la $4,foo
12565              .skip      1020
12566              .align     2
12567            foo:
12568          In this case when the la is extended, foo is 0x3fc bytes
12569          away, so the la can be shrunk, but then foo is 0x400 away, so
12570          the la must be extended.  To avoid this loop, we mark the
12571          frag as extended if it was small, and is about to become
12572          extended with a value of maxtiny + 1.  */
12573       if (val == ((maxtiny + 1) << op->shift)
12574           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12575           && sec == NULL)
12576         {
12577           fragp->fr_subtype =
12578             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12579           return 1;
12580         }
12581     }
12582   else if (symsec != absolute_section && sec != NULL)
12583     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12584
12585   if ((val & ((1 << op->shift) - 1)) != 0
12586       || val < (mintiny << op->shift)
12587       || val > (maxtiny << op->shift))
12588     return 1;
12589   else
12590     return 0;
12591 }
12592
12593 /* Estimate the size of a frag before relaxing.  Unless this is the
12594    mips16, we are not really relaxing here, and the final size is
12595    encoded in the subtype information.  For the mips16, we have to
12596    decide whether we are using an extended opcode or not.  */
12597
12598 int
12599 md_estimate_size_before_relax (fragp, segtype)
12600      fragS *fragp;
12601      asection *segtype;
12602 {
12603   int change = 0;
12604   boolean linkonce = false;
12605
12606   if (RELAX_MIPS16_P (fragp->fr_subtype))
12607     /* We don't want to modify the EXTENDED bit here; it might get us
12608        into infinite loops.  We change it only in mips_relax_frag().  */
12609     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12610
12611   if (mips_pic == NO_PIC)
12612     {
12613       change = nopic_need_relax (fragp->fr_symbol, 0);
12614     }
12615   else if (mips_pic == SVR4_PIC)
12616     {
12617       symbolS *sym;
12618       asection *symsec;
12619
12620       sym = fragp->fr_symbol;
12621
12622       /* Handle the case of a symbol equated to another symbol.  */
12623       while (symbol_equated_reloc_p (sym))
12624         {
12625           symbolS *n;
12626
12627           /* It's possible to get a loop here in a badly written
12628              program.  */
12629           n = symbol_get_value_expression (sym)->X_add_symbol;
12630           if (n == sym)
12631             break;
12632           sym = n;
12633         }
12634
12635       symsec = S_GET_SEGMENT (sym);
12636
12637       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12638       if (symsec != segtype && ! S_IS_LOCAL (sym))
12639         {
12640           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12641               != 0)
12642             linkonce = true;
12643
12644           /* The GNU toolchain uses an extension for ELF: a section
12645              beginning with the magic string .gnu.linkonce is a linkonce
12646              section.  */
12647           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12648                        sizeof ".gnu.linkonce" - 1) == 0)
12649             linkonce = true;
12650         }
12651
12652       /* This must duplicate the test in adjust_reloc_syms.  */
12653       change = (symsec != &bfd_und_section
12654                 && symsec != &bfd_abs_section
12655                 && ! bfd_is_com_section (symsec)
12656                 && !linkonce
12657 #ifdef OBJ_ELF
12658                 /* A global or weak symbol is treated as external.  */
12659                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12660                     || (! S_IS_WEAK (sym)
12661                         && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
12662 #endif
12663                 );
12664     }
12665   else
12666     abort ();
12667
12668   if (change)
12669     {
12670       /* Record the offset to the first reloc in the fr_opcode field.
12671          This lets md_convert_frag and tc_gen_reloc know that the code
12672          must be expanded.  */
12673       fragp->fr_opcode = (fragp->fr_literal
12674                           + fragp->fr_fix
12675                           - RELAX_OLD (fragp->fr_subtype)
12676                           + RELAX_RELOC1 (fragp->fr_subtype));
12677       /* FIXME: This really needs as_warn_where.  */
12678       if (RELAX_WARN (fragp->fr_subtype))
12679         as_warn (_("AT used after \".set noat\" or macro used after "
12680                    "\".set nomacro\""));
12681
12682       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12683     }
12684
12685   return 0;
12686 }
12687
12688 /* This is called to see whether a reloc against a defined symbol
12689    should be converted into a reloc against a section.  Don't adjust
12690    MIPS16 jump relocations, so we don't have to worry about the format
12691    of the offset in the .o file.  Don't adjust relocations against
12692    mips16 symbols, so that the linker can find them if it needs to set
12693    up a stub.  */
12694
12695 int
12696 mips_fix_adjustable (fixp)
12697      fixS *fixp;
12698 {
12699 #ifdef OBJ_ELF
12700   /* Prevent all adjustments to global symbols.  */
12701   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12702       && mips_pic != EMBEDDED_PIC
12703       && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12704     return 0;
12705 #endif
12706   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12707     return 0;
12708   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12709       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12710     return 0;
12711   if (fixp->fx_addsy == NULL)
12712     return 1;
12713 #ifdef OBJ_ELF
12714   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12715       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12716       && fixp->fx_subsy == NULL)
12717     return 0;
12718 #endif
12719   return 1;
12720 }
12721
12722 /* Translate internal representation of relocation info to BFD target
12723    format.  */
12724
12725 arelent **
12726 tc_gen_reloc (section, fixp)
12727      asection *section ATTRIBUTE_UNUSED;
12728      fixS *fixp;
12729 {
12730   static arelent *retval[4];
12731   arelent *reloc;
12732   bfd_reloc_code_real_type code;
12733
12734   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12735   retval[1] = NULL;
12736
12737   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12738   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12739   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12740
12741   if (mips_pic == EMBEDDED_PIC
12742       && SWITCH_TABLE (fixp))
12743     {
12744       /* For a switch table entry we use a special reloc.  The addend
12745          is actually the difference between the reloc address and the
12746          subtrahend.  */
12747       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12748       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12749         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12750       fixp->fx_r_type = BFD_RELOC_GPREL32;
12751     }
12752   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12753     {
12754       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12755         reloc->addend = fixp->fx_addnumber;
12756       else
12757         {
12758           /* We use a special addend for an internal RELLO reloc.  */
12759           if (symbol_section_p (fixp->fx_addsy))
12760             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12761           else
12762             reloc->addend = fixp->fx_addnumber + reloc->address;
12763         }
12764     }
12765   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12766     {
12767       assert (fixp->fx_next != NULL
12768               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12769
12770       /* The reloc is relative to the RELLO; adjust the addend
12771          accordingly.  */
12772       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12773         reloc->addend = fixp->fx_next->fx_addnumber;
12774       else
12775         {
12776           /* We use a special addend for an internal RELHI reloc.  */
12777           if (symbol_section_p (fixp->fx_addsy))
12778             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12779                              + fixp->fx_next->fx_where
12780                              - S_GET_VALUE (fixp->fx_subsy));
12781           else
12782             reloc->addend = (fixp->fx_addnumber
12783                              + fixp->fx_next->fx_frag->fr_address
12784                              + fixp->fx_next->fx_where);
12785         }
12786     }
12787   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12788     reloc->addend = fixp->fx_addnumber;
12789   else
12790     {
12791       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12792         /* A gruesome hack which is a result of the gruesome gas reloc
12793            handling.  */
12794         reloc->addend = reloc->address;
12795       else
12796         reloc->addend = -reloc->address;
12797     }
12798
12799   /* If this is a variant frag, we may need to adjust the existing
12800      reloc and generate a new one.  */
12801   if (fixp->fx_frag->fr_opcode != NULL
12802       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12803           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12804           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12805           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12806           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12807           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12808           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12809       && ! HAVE_NEWABI)
12810     {
12811       arelent *reloc2;
12812
12813       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12814
12815       /* If this is not the last reloc in this frag, then we have two
12816          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12817          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12818          the second one handle all of them.  */
12819       if (fixp->fx_next != NULL
12820           && fixp->fx_frag == fixp->fx_next->fx_frag)
12821         {
12822           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12823                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12824                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12825                       && (fixp->fx_next->fx_r_type
12826                           == BFD_RELOC_MIPS_GOT_LO16))
12827                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12828                       && (fixp->fx_next->fx_r_type
12829                           == BFD_RELOC_MIPS_CALL_LO16)));
12830           retval[0] = NULL;
12831           return retval;
12832         }
12833
12834       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12835       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12836       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12837       retval[2] = NULL;
12838       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12839       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12840       reloc2->address = (reloc->address
12841                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12842                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12843       reloc2->addend = fixp->fx_addnumber;
12844       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12845       assert (reloc2->howto != NULL);
12846
12847       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12848         {
12849           arelent *reloc3;
12850
12851           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12852           retval[3] = NULL;
12853           *reloc3 = *reloc2;
12854           reloc3->address += 4;
12855         }
12856
12857       if (mips_pic == NO_PIC)
12858         {
12859           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12860           fixp->fx_r_type = BFD_RELOC_HI16_S;
12861         }
12862       else if (mips_pic == SVR4_PIC)
12863         {
12864           switch (fixp->fx_r_type)
12865             {
12866             default:
12867               abort ();
12868             case BFD_RELOC_MIPS_GOT16:
12869               break;
12870             case BFD_RELOC_MIPS_CALL16:
12871             case BFD_RELOC_MIPS_GOT_LO16:
12872             case BFD_RELOC_MIPS_CALL_LO16:
12873               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12874               break;
12875             }
12876         }
12877       else
12878         abort ();
12879     }
12880
12881   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12882      entry to be used in the relocation's section offset.  */
12883   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12884     {
12885       reloc->address = reloc->addend;
12886       reloc->addend = 0;
12887     }
12888
12889   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12890      fixup_segment converted a non-PC relative reloc into a PC
12891      relative reloc.  In such a case, we need to convert the reloc
12892      code.  */
12893   code = fixp->fx_r_type;
12894   if (fixp->fx_pcrel)
12895     {
12896       switch (code)
12897         {
12898         case BFD_RELOC_8:
12899           code = BFD_RELOC_8_PCREL;
12900           break;
12901         case BFD_RELOC_16:
12902           code = BFD_RELOC_16_PCREL;
12903           break;
12904         case BFD_RELOC_32:
12905           code = BFD_RELOC_32_PCREL;
12906           break;
12907         case BFD_RELOC_64:
12908           code = BFD_RELOC_64_PCREL;
12909           break;
12910         case BFD_RELOC_8_PCREL:
12911         case BFD_RELOC_16_PCREL:
12912         case BFD_RELOC_32_PCREL:
12913         case BFD_RELOC_64_PCREL:
12914         case BFD_RELOC_16_PCREL_S2:
12915         case BFD_RELOC_PCREL_HI16_S:
12916         case BFD_RELOC_PCREL_LO16:
12917           break;
12918         default:
12919           as_bad_where (fixp->fx_file, fixp->fx_line,
12920                         _("Cannot make %s relocation PC relative"),
12921                         bfd_get_reloc_code_name (code));
12922         }
12923     }
12924
12925 #ifdef OBJ_ELF
12926   /* md_apply_fix3 has a double-subtraction hack to get
12927      bfd_install_relocation to behave nicely.  GPREL relocations are
12928      handled correctly without this hack, so undo it here.  We can't
12929      stop md_apply_fix3 from subtracting twice in the first place since
12930      the fake addend is required for variant frags above.  */
12931   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12932       && code == BFD_RELOC_GPREL16
12933       && reloc->addend != 0
12934       && mips_need_elf_addend_fixup (fixp))
12935     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12936 #endif
12937
12938   /* To support a PC relative reloc when generating embedded PIC code
12939      for ECOFF, we use a Cygnus extension.  We check for that here to
12940      make sure that we don't let such a reloc escape normally.  */
12941   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12942        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12943       && code == BFD_RELOC_16_PCREL_S2
12944       && mips_pic != EMBEDDED_PIC)
12945     reloc->howto = NULL;
12946   else
12947     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12948
12949   if (reloc->howto == NULL)
12950     {
12951       as_bad_where (fixp->fx_file, fixp->fx_line,
12952                     _("Can not represent %s relocation in this object file format"),
12953                     bfd_get_reloc_code_name (code));
12954       retval[0] = NULL;
12955     }
12956
12957   return retval;
12958 }
12959
12960 /* Relax a machine dependent frag.  This returns the amount by which
12961    the current size of the frag should change.  */
12962
12963 int
12964 mips_relax_frag (fragp, stretch)
12965      fragS *fragp;
12966      long stretch;
12967 {
12968   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12969     return 0;
12970
12971   if (mips16_extended_frag (fragp, NULL, stretch))
12972     {
12973       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12974         return 0;
12975       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12976       return 2;
12977     }
12978   else
12979     {
12980       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12981         return 0;
12982       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12983       return -2;
12984     }
12985
12986   return 0;
12987 }
12988
12989 /* Convert a machine dependent frag.  */
12990
12991 void
12992 md_convert_frag (abfd, asec, fragp)
12993      bfd *abfd ATTRIBUTE_UNUSED;
12994      segT asec;
12995      fragS *fragp;
12996 {
12997   int old, new;
12998   char *fixptr;
12999
13000   if (RELAX_MIPS16_P (fragp->fr_subtype))
13001     {
13002       int type;
13003       register const struct mips16_immed_operand *op;
13004       boolean small, ext;
13005       offsetT val;
13006       bfd_byte *buf;
13007       unsigned long insn;
13008       boolean use_extend;
13009       unsigned short extend;
13010
13011       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13012       op = mips16_immed_operands;
13013       while (op->type != type)
13014         ++op;
13015
13016       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13017         {
13018           small = false;
13019           ext = true;
13020         }
13021       else
13022         {
13023           small = true;
13024           ext = false;
13025         }
13026
13027       resolve_symbol_value (fragp->fr_symbol);
13028       val = S_GET_VALUE (fragp->fr_symbol);
13029       if (op->pcrel)
13030         {
13031           addressT addr;
13032
13033           addr = fragp->fr_address + fragp->fr_fix;
13034
13035           /* The rules for the base address of a PC relative reloc are
13036              complicated; see mips16_extended_frag.  */
13037           if (type == 'p' || type == 'q')
13038             {
13039               addr += 2;
13040               if (ext)
13041                 addr += 2;
13042               /* Ignore the low bit in the target, since it will be
13043                  set for a text label.  */
13044               if ((val & 1) != 0)
13045                 --val;
13046             }
13047           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13048             addr -= 4;
13049           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13050             addr -= 2;
13051
13052           addr &= ~ (addressT) ((1 << op->shift) - 1);
13053           val -= addr;
13054
13055           /* Make sure the section winds up with the alignment we have
13056              assumed.  */
13057           if (op->shift > 0)
13058             record_alignment (asec, op->shift);
13059         }
13060
13061       if (ext
13062           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13063               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13064         as_warn_where (fragp->fr_file, fragp->fr_line,
13065                        _("extended instruction in delay slot"));
13066
13067       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13068
13069       if (target_big_endian)
13070         insn = bfd_getb16 (buf);
13071       else
13072         insn = bfd_getl16 (buf);
13073
13074       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13075                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13076                     small, ext, &insn, &use_extend, &extend);
13077
13078       if (use_extend)
13079         {
13080           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13081           fragp->fr_fix += 2;
13082           buf += 2;
13083         }
13084
13085       md_number_to_chars ((char *) buf, insn, 2);
13086       fragp->fr_fix += 2;
13087       buf += 2;
13088     }
13089   else
13090     {
13091       if (fragp->fr_opcode == NULL)
13092         return;
13093
13094       old = RELAX_OLD (fragp->fr_subtype);
13095       new = RELAX_NEW (fragp->fr_subtype);
13096       fixptr = fragp->fr_literal + fragp->fr_fix;
13097
13098       if (new > 0)
13099         memcpy (fixptr - old, fixptr, new);
13100
13101       fragp->fr_fix += new - old;
13102     }
13103 }
13104
13105 #ifdef OBJ_ELF
13106
13107 /* This function is called after the relocs have been generated.
13108    We've been storing mips16 text labels as odd.  Here we convert them
13109    back to even for the convenience of the debugger.  */
13110
13111 void
13112 mips_frob_file_after_relocs ()
13113 {
13114   asymbol **syms;
13115   unsigned int count, i;
13116
13117   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13118     return;
13119
13120   syms = bfd_get_outsymbols (stdoutput);
13121   count = bfd_get_symcount (stdoutput);
13122   for (i = 0; i < count; i++, syms++)
13123     {
13124       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13125           && ((*syms)->value & 1) != 0)
13126         {
13127           (*syms)->value &= ~1;
13128           /* If the symbol has an odd size, it was probably computed
13129              incorrectly, so adjust that as well.  */
13130           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13131             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13132         }
13133     }
13134 }
13135
13136 #endif
13137
13138 /* This function is called whenever a label is defined.  It is used
13139    when handling branch delays; if a branch has a label, we assume we
13140    can not move it.  */
13141
13142 void
13143 mips_define_label (sym)
13144      symbolS *sym;
13145 {
13146   struct insn_label_list *l;
13147
13148   if (free_insn_labels == NULL)
13149     l = (struct insn_label_list *) xmalloc (sizeof *l);
13150   else
13151     {
13152       l = free_insn_labels;
13153       free_insn_labels = l->next;
13154     }
13155
13156   l->label = sym;
13157   l->next = insn_labels;
13158   insn_labels = l;
13159 }
13160 \f
13161 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13162
13163 /* Some special processing for a MIPS ELF file.  */
13164
13165 void
13166 mips_elf_final_processing ()
13167 {
13168   /* Write out the register information.  */
13169   if (file_mips_abi != N64_ABI)
13170     {
13171       Elf32_RegInfo s;
13172
13173       s.ri_gprmask = mips_gprmask;
13174       s.ri_cprmask[0] = mips_cprmask[0];
13175       s.ri_cprmask[1] = mips_cprmask[1];
13176       s.ri_cprmask[2] = mips_cprmask[2];
13177       s.ri_cprmask[3] = mips_cprmask[3];
13178       /* The gp_value field is set by the MIPS ELF backend.  */
13179
13180       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13181                                        ((Elf32_External_RegInfo *)
13182                                         mips_regmask_frag));
13183     }
13184   else
13185     {
13186       Elf64_Internal_RegInfo s;
13187
13188       s.ri_gprmask = mips_gprmask;
13189       s.ri_pad = 0;
13190       s.ri_cprmask[0] = mips_cprmask[0];
13191       s.ri_cprmask[1] = mips_cprmask[1];
13192       s.ri_cprmask[2] = mips_cprmask[2];
13193       s.ri_cprmask[3] = mips_cprmask[3];
13194       /* The gp_value field is set by the MIPS ELF backend.  */
13195
13196       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13197                                        ((Elf64_External_RegInfo *)
13198                                         mips_regmask_frag));
13199     }
13200
13201   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13202      sort of BFD interface for this.  */
13203   if (mips_any_noreorder)
13204     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13205   if (mips_pic != NO_PIC)
13206     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13207
13208   /* Set MIPS ELF flags for ASEs.  */
13209 #if 0 /* XXX FIXME */
13210   if (file_ase_mips3d)
13211     elf_elfheader (stdoutput)->e_flags |= ???;
13212 #endif
13213   if (file_ase_mdmx)
13214     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13215
13216   /* Set the MIPS ELF ABI flags.  */
13217   if (file_mips_abi == NO_ABI)
13218     ;
13219   else if (file_mips_abi == O32_ABI)
13220     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13221   else if (file_mips_abi == O64_ABI)
13222     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13223   else if (file_mips_abi == EABI_ABI)
13224     {
13225       if (mips_eabi64)
13226         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13227       else
13228         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13229     }
13230   else if (file_mips_abi == N32_ABI)
13231     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13232
13233   /* Nothing to do for N64_ABI.  */
13234
13235   if (mips_32bitmode)
13236     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13237 }
13238
13239 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13240 \f
13241 typedef struct proc {
13242   symbolS *isym;
13243   unsigned long reg_mask;
13244   unsigned long reg_offset;
13245   unsigned long fpreg_mask;
13246   unsigned long fpreg_offset;
13247   unsigned long frame_offset;
13248   unsigned long frame_reg;
13249   unsigned long pc_reg;
13250 } procS;
13251
13252 static procS cur_proc;
13253 static procS *cur_proc_ptr;
13254 static int numprocs;
13255
13256 /* Fill in an rs_align_code fragment.  */
13257
13258 void
13259 mips_handle_align (fragp)
13260      fragS *fragp;
13261 {
13262   if (fragp->fr_type != rs_align_code)
13263     return;
13264
13265   if (mips_opts.mips16)
13266     {
13267       static const unsigned char be_nop[] = { 0x65, 0x00 };
13268       static const unsigned char le_nop[] = { 0x00, 0x65 };
13269
13270       int bytes;
13271       char *p;
13272
13273       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13274       p = fragp->fr_literal + fragp->fr_fix;
13275
13276       if (bytes & 1)
13277         {
13278           *p++ = 0;
13279           fragp->fr_fix++;
13280         }
13281
13282       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13283       fragp->fr_var = 2;
13284     }
13285
13286   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13287 }
13288
13289 static void
13290 md_obj_begin ()
13291 {
13292 }
13293
13294 static void
13295 md_obj_end ()
13296 {
13297   /* check for premature end, nesting errors, etc */
13298   if (cur_proc_ptr)
13299     as_warn (_("missing .end at end of assembly"));
13300 }
13301
13302 static long
13303 get_number ()
13304 {
13305   int negative = 0;
13306   long val = 0;
13307
13308   if (*input_line_pointer == '-')
13309     {
13310       ++input_line_pointer;
13311       negative = 1;
13312     }
13313   if (!ISDIGIT (*input_line_pointer))
13314     as_bad (_("expected simple number"));
13315   if (input_line_pointer[0] == '0')
13316     {
13317       if (input_line_pointer[1] == 'x')
13318         {
13319           input_line_pointer += 2;
13320           while (ISXDIGIT (*input_line_pointer))
13321             {
13322               val <<= 4;
13323               val |= hex_value (*input_line_pointer++);
13324             }
13325           return negative ? -val : val;
13326         }
13327       else
13328         {
13329           ++input_line_pointer;
13330           while (ISDIGIT (*input_line_pointer))
13331             {
13332               val <<= 3;
13333               val |= *input_line_pointer++ - '0';
13334             }
13335           return negative ? -val : val;
13336         }
13337     }
13338   if (!ISDIGIT (*input_line_pointer))
13339     {
13340       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13341               *input_line_pointer, *input_line_pointer);
13342       as_warn (_("invalid number"));
13343       return -1;
13344     }
13345   while (ISDIGIT (*input_line_pointer))
13346     {
13347       val *= 10;
13348       val += *input_line_pointer++ - '0';
13349     }
13350   return negative ? -val : val;
13351 }
13352
13353 /* The .file directive; just like the usual .file directive, but there
13354    is an initial number which is the ECOFF file index.  */
13355
13356 static void
13357 s_file (x)
13358      int x ATTRIBUTE_UNUSED;
13359 {
13360   get_number ();
13361   s_app_file (0);
13362 }
13363
13364 /* The .end directive.  */
13365
13366 static void
13367 s_mips_end (x)
13368      int x ATTRIBUTE_UNUSED;
13369 {
13370   symbolS *p;
13371   int maybe_text;
13372
13373   /* Following functions need their own .frame and .cprestore directives.  */
13374   mips_frame_reg_valid = 0;
13375   mips_cprestore_valid = 0;
13376
13377   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13378     {
13379       p = get_symbol ();
13380       demand_empty_rest_of_line ();
13381     }
13382   else
13383     p = NULL;
13384
13385 #ifdef BFD_ASSEMBLER
13386   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13387     maybe_text = 1;
13388   else
13389     maybe_text = 0;
13390 #else
13391   if (now_seg != data_section && now_seg != bss_section)
13392     maybe_text = 1;
13393   else
13394     maybe_text = 0;
13395 #endif
13396
13397   if (!maybe_text)
13398     as_warn (_(".end not in text section"));
13399
13400   if (!cur_proc_ptr)
13401     {
13402       as_warn (_(".end directive without a preceding .ent directive."));
13403       demand_empty_rest_of_line ();
13404       return;
13405     }
13406
13407   if (p != NULL)
13408     {
13409       assert (S_GET_NAME (p));
13410       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13411         as_warn (_(".end symbol does not match .ent symbol."));
13412     }
13413   else
13414     as_warn (_(".end directive missing or unknown symbol"));
13415
13416 #ifdef MIPS_STABS_ELF
13417   {
13418     segT saved_seg = now_seg;
13419     subsegT saved_subseg = now_subseg;
13420     valueT dot;
13421     expressionS exp;
13422     char *fragp;
13423
13424     dot = frag_now_fix ();
13425
13426 #ifdef md_flush_pending_output
13427     md_flush_pending_output ();
13428 #endif
13429
13430     assert (pdr_seg);
13431     subseg_set (pdr_seg, 0);
13432
13433     /* Write the symbol.  */
13434     exp.X_op = O_symbol;
13435     exp.X_add_symbol = p;
13436     exp.X_add_number = 0;
13437     emit_expr (&exp, 4);
13438
13439     fragp = frag_more (7 * 4);
13440
13441     md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13442     md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13443     md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13444     md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13445     md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13446     md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13447     md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13448
13449     subseg_set (saved_seg, saved_subseg);
13450   }
13451 #endif /* MIPS_STABS_ELF */
13452
13453   cur_proc_ptr = NULL;
13454 }
13455
13456 /* The .aent and .ent directives.  */
13457
13458 static void
13459 s_mips_ent (aent)
13460      int aent;
13461 {
13462   symbolS *symbolP;
13463   int maybe_text;
13464
13465   symbolP = get_symbol ();
13466   if (*input_line_pointer == ',')
13467     ++input_line_pointer;
13468   SKIP_WHITESPACE ();
13469   if (ISDIGIT (*input_line_pointer)
13470       || *input_line_pointer == '-')
13471     get_number ();
13472
13473 #ifdef BFD_ASSEMBLER
13474   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13475     maybe_text = 1;
13476   else
13477     maybe_text = 0;
13478 #else
13479   if (now_seg != data_section && now_seg != bss_section)
13480     maybe_text = 1;
13481   else
13482     maybe_text = 0;
13483 #endif
13484
13485   if (!maybe_text)
13486     as_warn (_(".ent or .aent not in text section."));
13487
13488   if (!aent && cur_proc_ptr)
13489     as_warn (_("missing .end"));
13490
13491   if (!aent)
13492     {
13493       /* This function needs its own .frame and .cprestore directives.  */
13494       mips_frame_reg_valid = 0;
13495       mips_cprestore_valid = 0;
13496
13497       cur_proc_ptr = &cur_proc;
13498       memset (cur_proc_ptr, '\0', sizeof (procS));
13499
13500       cur_proc_ptr->isym = symbolP;
13501
13502       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13503
13504       ++numprocs;
13505     }
13506
13507   demand_empty_rest_of_line ();
13508 }
13509
13510 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13511    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13512    s_mips_frame is used so that we can set the PDR information correctly.
13513    We can't use the ecoff routines because they make reference to the ecoff
13514    symbol table (in the mdebug section).  */
13515
13516 static void
13517 s_mips_frame (ignore)
13518      int ignore ATTRIBUTE_UNUSED;
13519 {
13520 #ifdef MIPS_STABS_ELF
13521
13522   long val;
13523
13524   if (cur_proc_ptr == (procS *) NULL)
13525     {
13526       as_warn (_(".frame outside of .ent"));
13527       demand_empty_rest_of_line ();
13528       return;
13529     }
13530
13531   cur_proc_ptr->frame_reg = tc_get_register (1);
13532
13533   SKIP_WHITESPACE ();
13534   if (*input_line_pointer++ != ','
13535       || get_absolute_expression_and_terminator (&val) != ',')
13536     {
13537       as_warn (_("Bad .frame directive"));
13538       --input_line_pointer;
13539       demand_empty_rest_of_line ();
13540       return;
13541     }
13542
13543   cur_proc_ptr->frame_offset = val;
13544   cur_proc_ptr->pc_reg = tc_get_register (0);
13545
13546   demand_empty_rest_of_line ();
13547 #else
13548   s_ignore (ignore);
13549 #endif /* MIPS_STABS_ELF */
13550 }
13551
13552 /* The .fmask and .mask directives. If the mdebug section is present
13553    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13554    embedded targets, s_mips_mask is used so that we can set the PDR
13555    information correctly. We can't use the ecoff routines because they
13556    make reference to the ecoff symbol table (in the mdebug section).  */
13557
13558 static void
13559 s_mips_mask (reg_type)
13560      char reg_type;
13561 {
13562 #ifdef MIPS_STABS_ELF
13563   long mask, off;
13564
13565   if (cur_proc_ptr == (procS *) NULL)
13566     {
13567       as_warn (_(".mask/.fmask outside of .ent"));
13568       demand_empty_rest_of_line ();
13569       return;
13570     }
13571
13572   if (get_absolute_expression_and_terminator (&mask) != ',')
13573     {
13574       as_warn (_("Bad .mask/.fmask directive"));
13575       --input_line_pointer;
13576       demand_empty_rest_of_line ();
13577       return;
13578     }
13579
13580   off = get_absolute_expression ();
13581
13582   if (reg_type == 'F')
13583     {
13584       cur_proc_ptr->fpreg_mask = mask;
13585       cur_proc_ptr->fpreg_offset = off;
13586     }
13587   else
13588     {
13589       cur_proc_ptr->reg_mask = mask;
13590       cur_proc_ptr->reg_offset = off;
13591     }
13592
13593   demand_empty_rest_of_line ();
13594 #else
13595   s_ignore (reg_type);
13596 #endif /* MIPS_STABS_ELF */
13597 }
13598
13599 /* The .loc directive.  */
13600
13601 #if 0
13602 static void
13603 s_loc (x)
13604      int x;
13605 {
13606   symbolS *symbolP;
13607   int lineno;
13608   int addroff;
13609
13610   assert (now_seg == text_section);
13611
13612   lineno = get_number ();
13613   addroff = frag_now_fix ();
13614
13615   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13616   S_SET_TYPE (symbolP, N_SLINE);
13617   S_SET_OTHER (symbolP, 0);
13618   S_SET_DESC (symbolP, lineno);
13619   symbolP->sy_segment = now_seg;
13620 }
13621 #endif
13622
13623 /* CPU name/ISA/number mapping table.
13624
13625    Entries are grouped by type.  The first matching CPU or ISA entry
13626    gets chosen by CPU or ISA, so it should be the 'canonical' name
13627    for that type.  Entries after that within the type are sorted
13628    alphabetically.
13629
13630    Case is ignored in comparison, so put the canonical entry in the
13631    appropriate case but everything else in lower case to ease eye pain.  */
13632 static const struct mips_cpu_info mips_cpu_info_table[] =
13633 {
13634   /* MIPS1 ISA */
13635   { "MIPS1",          1,      ISA_MIPS1,      CPU_R3000, },
13636   { "mips",           1,      ISA_MIPS1,      CPU_R3000, },
13637
13638   /* MIPS2 ISA */
13639   { "MIPS2",          1,      ISA_MIPS2,      CPU_R6000, },
13640
13641   /* MIPS3 ISA */
13642   { "MIPS3",          1,      ISA_MIPS3,      CPU_R4000, },
13643
13644   /* MIPS4 ISA */
13645   { "MIPS4",          1,      ISA_MIPS4,      CPU_R8000, },
13646
13647   /* MIPS5 ISA */
13648   { "MIPS5",          1,      ISA_MIPS5,      CPU_MIPS5, },
13649   { "Generic-MIPS5",  0,      ISA_MIPS5,      CPU_MIPS5, },
13650
13651   /* MIPS32 ISA */
13652   { "MIPS32",         1,      ISA_MIPS32,     CPU_MIPS32, },
13653   { "mipsisa32",      0,      ISA_MIPS32,     CPU_MIPS32, },
13654   { "Generic-MIPS32", 0,      ISA_MIPS32,     CPU_MIPS32, },
13655   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13656   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32, },
13657   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32, },
13658
13659   /* For historical reasons.  */
13660   { "MIPS64",         1,      ISA_MIPS3,      CPU_R4000, },
13661
13662   /* MIPS64 ISA */
13663   { "mipsisa64",      1,      ISA_MIPS64,     CPU_MIPS64, },
13664   { "Generic-MIPS64", 0,      ISA_MIPS64,     CPU_MIPS64, },
13665   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64, },
13666   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64, },
13667
13668   /* R2000 CPU */
13669   { "R2000",          0,      ISA_MIPS1,      CPU_R2000, },
13670   { "2000",           0,      ISA_MIPS1,      CPU_R2000, },
13671   { "2k",             0,      ISA_MIPS1,      CPU_R2000, },
13672   { "r2k",            0,      ISA_MIPS1,      CPU_R2000, },
13673
13674   /* R3000 CPU */
13675   { "R3000",          0,      ISA_MIPS1,      CPU_R3000, },
13676   { "3000",           0,      ISA_MIPS1,      CPU_R3000, },
13677   { "3k",             0,      ISA_MIPS1,      CPU_R3000, },
13678   { "r3k",            0,      ISA_MIPS1,      CPU_R3000, },
13679
13680   /* TX3900 CPU */
13681   { "R3900",          0,      ISA_MIPS1,      CPU_R3900, },
13682   { "3900",           0,      ISA_MIPS1,      CPU_R3900, },
13683   { "mipstx39",       0,      ISA_MIPS1,      CPU_R3900, },
13684
13685   /* R4000 CPU */
13686   { "R4000",          0,      ISA_MIPS3,      CPU_R4000, },
13687   { "4000",           0,      ISA_MIPS3,      CPU_R4000, },
13688   { "4k",             0,      ISA_MIPS3,      CPU_R4000, },   /* beware */
13689   { "r4k",            0,      ISA_MIPS3,      CPU_R4000, },
13690
13691   /* R4010 CPU */
13692   { "R4010",          0,      ISA_MIPS2,      CPU_R4010, },
13693   { "4010",           0,      ISA_MIPS2,      CPU_R4010, },
13694
13695   /* R4400 CPU */
13696   { "R4400",          0,      ISA_MIPS3,      CPU_R4400, },
13697   { "4400",           0,      ISA_MIPS3,      CPU_R4400, },
13698
13699   /* R4600 CPU */
13700   { "R4600",          0,      ISA_MIPS3,      CPU_R4600, },
13701   { "4600",           0,      ISA_MIPS3,      CPU_R4600, },
13702   { "mips64orion",    0,      ISA_MIPS3,      CPU_R4600, },
13703   { "orion",          0,      ISA_MIPS3,      CPU_R4600, },
13704
13705   /* R4650 CPU */
13706   { "R4650",          0,      ISA_MIPS3,      CPU_R4650, },
13707   { "4650",           0,      ISA_MIPS3,      CPU_R4650, },
13708
13709   /* R6000 CPU */
13710   { "R6000",          0,      ISA_MIPS2,      CPU_R6000, },
13711   { "6000",           0,      ISA_MIPS2,      CPU_R6000, },
13712   { "6k",             0,      ISA_MIPS2,      CPU_R6000, },
13713   { "r6k",            0,      ISA_MIPS2,      CPU_R6000, },
13714
13715   /* R8000 CPU */
13716   { "R8000",          0,      ISA_MIPS4,      CPU_R8000, },
13717   { "8000",           0,      ISA_MIPS4,      CPU_R8000, },
13718   { "8k",             0,      ISA_MIPS4,      CPU_R8000, },
13719   { "r8k",            0,      ISA_MIPS4,      CPU_R8000, },
13720
13721   /* R10000 CPU */
13722   { "R10000",         0,      ISA_MIPS4,      CPU_R10000, },
13723   { "10000",          0,      ISA_MIPS4,      CPU_R10000, },
13724   { "10k",            0,      ISA_MIPS4,      CPU_R10000, },
13725   { "r10k",           0,      ISA_MIPS4,      CPU_R10000, },
13726
13727   /* R12000 CPU */
13728   { "R12000",         0,      ISA_MIPS4,      CPU_R12000, },
13729   { "12000",          0,      ISA_MIPS4,      CPU_R12000, },
13730   { "12k",            0,      ISA_MIPS4,      CPU_R12000, },
13731   { "r12k",           0,      ISA_MIPS4,      CPU_R12000, },
13732
13733   /* VR4100 CPU */
13734   { "VR4100",         0,      ISA_MIPS3,      CPU_VR4100, },
13735   { "4100",           0,      ISA_MIPS3,      CPU_VR4100, },
13736   { "mips64vr4100",   0,      ISA_MIPS3,      CPU_VR4100, },
13737   { "r4100",          0,      ISA_MIPS3,      CPU_VR4100, },
13738
13739   /* VR4111 CPU */
13740   { "VR4111",         0,      ISA_MIPS3,      CPU_R4111, },
13741   { "4111",           0,      ISA_MIPS3,      CPU_R4111, },
13742   { "mips64vr4111",   0,      ISA_MIPS3,      CPU_R4111, },
13743   { "r4111",          0,      ISA_MIPS3,      CPU_R4111, },
13744
13745   /* VR4300 CPU */
13746   { "VR4300",         0,      ISA_MIPS3,      CPU_R4300, },
13747   { "4300",           0,      ISA_MIPS3,      CPU_R4300, },
13748   { "mips64vr4300",   0,      ISA_MIPS3,      CPU_R4300, },
13749   { "r4300",          0,      ISA_MIPS3,      CPU_R4300, },
13750
13751   /* VR5000 CPU */
13752   { "VR5000",         0,      ISA_MIPS4,      CPU_R5000, },
13753   { "5000",           0,      ISA_MIPS4,      CPU_R5000, },
13754   { "5k",             0,      ISA_MIPS4,      CPU_R5000, },
13755   { "mips64vr5000",   0,      ISA_MIPS4,      CPU_R5000, },
13756   { "r5000",          0,      ISA_MIPS4,      CPU_R5000, },
13757   { "r5200",          0,      ISA_MIPS4,      CPU_R5000, },
13758   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000, },
13759   { "r5230",          0,      ISA_MIPS4,      CPU_R5000, },
13760   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000, },
13761   { "r5231",          0,      ISA_MIPS4,      CPU_R5000, },
13762   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000, },
13763   { "r5261",          0,      ISA_MIPS4,      CPU_R5000, },
13764   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000, },
13765   { "r5721",          0,      ISA_MIPS4,      CPU_R5000, },
13766   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000, },
13767   { "r5k",            0,      ISA_MIPS4,      CPU_R5000, },
13768   { "r7000",          0,      ISA_MIPS4,      CPU_R5000, },
13769
13770   /* Broadcom SB-1 CPU */
13771   { "SB-1",           0,      ISA_MIPS64,     CPU_SB1, },
13772   { "sb-1250",        0,      ISA_MIPS64,     CPU_SB1, },
13773   { "sb1",            0,      ISA_MIPS64,     CPU_SB1, },
13774   { "sb1250",         0,      ISA_MIPS64,     CPU_SB1, },
13775
13776   /* End marker.  */
13777   { NULL, 0, 0, 0, },
13778 };
13779
13780 static const struct mips_cpu_info *
13781 mips_cpu_info_from_name (name)
13782      const char *name;
13783 {
13784   int i;
13785
13786   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13787     if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13788       return (&mips_cpu_info_table[i]);
13789
13790   return NULL;
13791 }
13792
13793 static const struct mips_cpu_info *
13794 mips_cpu_info_from_isa (isa)
13795      int isa;
13796 {
13797   int i;
13798
13799   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13800     if (mips_cpu_info_table[i].is_isa
13801       && isa == mips_cpu_info_table[i].isa)
13802       return (&mips_cpu_info_table[i]);
13803
13804   return NULL;
13805 }
13806
13807 static const struct mips_cpu_info *
13808 mips_cpu_info_from_cpu (cpu)
13809      int cpu;
13810 {
13811   int i;
13812
13813   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13814     if (!mips_cpu_info_table[i].is_isa
13815       && cpu == mips_cpu_info_table[i].cpu)
13816       return (&mips_cpu_info_table[i]);
13817
13818   return NULL;
13819 }
This page took 0.838198 seconds and 2 git commands to generate.