* as.c: Replace CONST with const.
[external/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too.  */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 #include "ecoff.h"
81
82 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
83 static char *mips_regmask_frag;
84 #endif
85
86 #define ZERO 0
87 #define AT  1
88 #define TREG 24
89 #define PIC_CALL_REG 25
90 #define KT0 26
91 #define KT1 27
92 #define GP  28
93 #define SP  29
94 #define FP  30
95 #define RA  31
96
97 #define ILLEGAL_REG (32)
98
99 /* Allow override of standard little-endian ECOFF format.  */
100
101 #ifndef ECOFF_LITTLE_FORMAT
102 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
103 #endif
104
105 extern int target_big_endian;
106
107 /* The name of the readonly data section.  */
108 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
109                             ? ".data" \
110                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
111                             ? ".rdata" \
112                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
113                             ? ".rdata" \
114                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
115                             ? ".rodata" \
116                             : (abort (), ""))
117
118 /* The ABI to use.  */
119 enum mips_abi_level
120 {
121   NO_ABI = 0,
122   O32_ABI,
123   O64_ABI,
124   N32_ABI,
125   N64_ABI,
126   EABI_ABI
127 };
128
129 /* MIPS ABI we are using for this output file.  */
130 static enum mips_abi_level file_mips_abi = NO_ABI;
131
132 /* This is the set of options which may be modified by the .set
133    pseudo-op.  We use a struct so that .set push and .set pop are more
134    reliable.  */
135
136 struct mips_set_options
137 {
138   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
139      if it has not been initialized.  Changed by `.set mipsN', and the
140      -mipsN command line option, and the default CPU.  */
141   int isa;
142   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
143      if they have not been initialized.  Changed by `.set <asename>', by
144      command line options, and based on the default architecture.  */
145   int ase_mips3d;
146   int ase_mdmx;
147   /* Whether we are assembling for the mips16 processor.  0 if we are
148      not, 1 if we are, and -1 if the value has not been initialized.
149      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
150      -nomips16 command line options, and the default CPU.  */
151   int mips16;
152   /* Non-zero if we should not reorder instructions.  Changed by `.set
153      reorder' and `.set noreorder'.  */
154   int noreorder;
155   /* Non-zero if we should not permit the $at ($1) register to be used
156      in instructions.  Changed by `.set at' and `.set noat'.  */
157   int noat;
158   /* Non-zero if we should warn when a macro instruction expands into
159      more than one machine instruction.  Changed by `.set nomacro' and
160      `.set macro'.  */
161   int warn_about_macros;
162   /* Non-zero if we should not move instructions.  Changed by `.set
163      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
164   int nomove;
165   /* Non-zero if we should not optimize branches by moving the target
166      of the branch into the delay slot.  Actually, we don't perform
167      this optimization anyhow.  Changed by `.set bopt' and `.set
168      nobopt'.  */
169   int nobopt;
170   /* Non-zero if we should not autoextend mips16 instructions.
171      Changed by `.set autoextend' and `.set noautoextend'.  */
172   int noautoextend;
173   /* Restrict general purpose registers and floating point registers
174      to 32 bit.  This is initially determined when -mgp32 or -mfp32
175      is passed but can changed if the assembler code uses .set mipsN.  */
176   int gp32;
177   int fp32;
178   /* The ABI currently in use. This is changed by .set mipsN to loosen
179      restrictions and doesn't affect the whole file.  */
180   enum mips_abi_level abi;
181 };
182
183 /* True if -mgp32 was passed.  */
184 static int file_mips_gp32 = -1;
185
186 /* True if -mfp32 was passed.  */
187 static int file_mips_fp32 = -1;
188
189 /* This is the struct we use to hold the current set of options.  Note
190    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
191    -1 to indicate that they have not been initialized.  */
192
193 static struct mips_set_options mips_opts =
194 {
195   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, NO_ABI
196 };
197
198 /* These variables are filled in with the masks of registers used.
199    The object format code reads them and puts them in the appropriate
200    place.  */
201 unsigned long mips_gprmask;
202 unsigned long mips_cprmask[4];
203
204 /* MIPS ISA we are using for this output file.  */
205 static int file_mips_isa = ISA_UNKNOWN;
206
207 /* True if -mips16 was passed or implied by arguments passed on the
208    command line (e.g., by -march).  */
209 static int file_ase_mips16;
210
211 /* True if -mips3d was passed or implied by arguments passed on the
212    command line (e.g., by -march).  */
213 static int file_ase_mips3d;
214
215 /* True if -mdmx was passed or implied by arguments passed on the
216    command line (e.g., by -march).  */
217 static int file_ase_mdmx;
218
219 /* The argument of the -mcpu= flag.  Historical for code generation.  */
220 static int mips_cpu = CPU_UNKNOWN;
221
222 /* The argument of the -march= flag.  The architecture we are assembling.  */
223 static int mips_arch = CPU_UNKNOWN;
224
225 /* The argument of the -mtune= flag.  The architecture for which we
226    are optimizing.  */
227 static int mips_tune = CPU_UNKNOWN;
228
229 /* If they asked for mips1 or mips2 and a cpu that is
230    mips3 or greater, then mark the object file 32BITMODE.  */
231 static int mips_32bitmode = 0;
232
233 /* Some ISA's have delay slots for instructions which read or write
234    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
235    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
236    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
237    delay slot in this ISA.  The uses of this macro assume that any
238    ISA that has delay slots for one of these, has them for all.  They
239    also assume that ISAs which don't have delays for these insns, don't
240    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
241 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
242    (ISA) == ISA_MIPS1                       \
243    || (ISA) == ISA_MIPS2                    \
244    || (ISA) == ISA_MIPS3                    \
245    )
246
247 /*  Return true if ISA supports 64 bit gp register instructions.  */
248 #define ISA_HAS_64BIT_REGS(ISA) (    \
249    (ISA) == ISA_MIPS3                \
250    || (ISA) == ISA_MIPS4             \
251    || (ISA) == ISA_MIPS5             \
252    || (ISA) == ISA_MIPS64            \
253    )
254
255 #define HAVE_32BIT_GPRS                            \
256     (mips_opts.gp32                                \
257      || mips_opts.abi == O32_ABI                   \
258      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
259
260 #define HAVE_32BIT_FPRS                            \
261     (mips_opts.fp32                                \
262      || mips_opts.abi == O32_ABI                   \
263      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
264
265 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
266 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
267
268 #define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
269
270 #define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
271
272 /* We can only have 64bit addresses if the object file format
273    supports it.  */
274 #define HAVE_32BIT_ADDRESSES                           \
275    (HAVE_32BIT_GPRS                                    \
276     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
277          || ! HAVE_64BIT_OBJECTS)                      \
278         && mips_pic != EMBEDDED_PIC))
279
280 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
281
282 /* Return true if the given CPU supports the MIPS16 ASE.  */
283 #define CPU_HAS_MIPS16(cpu)                            \
284    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0)
285
286 /* Return true if the given CPU supports the MIPS3D ASE.  */
287 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
288                                  )
289
290 /* Return true if the given CPU supports the MDMX ASE.  */
291 #define CPU_HAS_MDMX(cpu)       (false                 \
292                                  )
293
294 /* Whether the processor uses hardware interlocks to protect
295    reads from the HI and LO registers, and thus does not
296    require nops to be inserted.  */
297
298 #define hilo_interlocks (mips_arch == CPU_R4010                       \
299                          || mips_arch == CPU_SB1                      \
300                          )
301
302 /* Whether the processor uses hardware interlocks to protect reads
303    from the GPRs, and thus does not require nops to be inserted.  */
304 #define gpr_interlocks \
305   (mips_opts.isa != ISA_MIPS1  \
306    || mips_arch == CPU_R3900)
307
308 /* As with other "interlocks" this is used by hardware that has FP
309    (co-processor) interlocks.  */
310 /* Itbl support may require additional care here.  */
311 #define cop_interlocks (mips_arch == CPU_R4300                        \
312                         || mips_arch == CPU_SB1                       \
313                         )
314
315 /* Is this a mfhi or mflo instruction?  */
316 #define MF_HILO_INSN(PINFO) \
317           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
318
319 /* MIPS PIC level.  */
320
321 enum mips_pic_level
322 {
323   /* Do not generate PIC code.  */
324   NO_PIC,
325
326   /* Generate PIC code as in Irix 4.  This is not implemented, and I'm
327      not sure what it is supposed to do.  */
328   IRIX4_PIC,
329
330   /* Generate PIC code as in the SVR4 MIPS ABI.  */
331   SVR4_PIC,
332
333   /* Generate PIC code without using a global offset table: the data
334      segment has a maximum size of 64K, all data references are off
335      the $gp register, and all text references are PC relative.  This
336      is used on some embedded systems.  */
337   EMBEDDED_PIC
338 };
339
340 static enum mips_pic_level mips_pic;
341
342 /* Warn about all NOPS that the assembler generates.  */
343 static int warn_nops = 0;
344
345 /* 1 if we should generate 32 bit offsets from the $gp register in
346    SVR4_PIC mode.  Currently has no meaning in other modes.  */
347 static int mips_big_got = 0;
348
349 /* 1 if trap instructions should used for overflow rather than break
350    instructions.  */
351 static int mips_trap = 0;
352
353 /* 1 if double width floating point constants should not be constructed
354    by assembling two single width halves into two single width floating
355    point registers which just happen to alias the double width destination
356    register.  On some architectures this aliasing can be disabled by a bit
357    in the status register, and the setting of this bit cannot be determined
358    automatically at assemble time.  */
359 static int mips_disable_float_construction;
360
361 /* Non-zero if any .set noreorder directives were used.  */
362
363 static int mips_any_noreorder;
364
365 /* Non-zero if nops should be inserted when the register referenced in
366    an mfhi/mflo instruction is read in the next two instructions.  */
367 static int mips_7000_hilo_fix;
368
369 /* The size of the small data section.  */
370 static unsigned int g_switch_value = 8;
371 /* Whether the -G option was used.  */
372 static int g_switch_seen = 0;
373
374 #define N_RMASK 0xc4
375 #define N_VFP   0xd4
376
377 /* If we can determine in advance that GP optimization won't be
378    possible, we can skip the relaxation stuff that tries to produce
379    GP-relative references.  This makes delay slot optimization work
380    better.
381
382    This function can only provide a guess, but it seems to work for
383    gcc output.  It needs to guess right for gcc, otherwise gcc
384    will put what it thinks is a GP-relative instruction in a branch
385    delay slot.
386
387    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
388    fixed it for the non-PIC mode.  KR 95/04/07  */
389 static int nopic_need_relax PARAMS ((symbolS *, int));
390
391 /* handle of the OPCODE hash table */
392 static struct hash_control *op_hash = NULL;
393
394 /* The opcode hash table we use for the mips16.  */
395 static struct hash_control *mips16_op_hash = NULL;
396
397 /* This array holds the chars that always start a comment.  If the
398     pre-processor is disabled, these aren't very useful */
399 const char comment_chars[] = "#";
400
401 /* This array holds the chars that only start a comment at the beginning of
402    a line.  If the line seems to have the form '# 123 filename'
403    .line and .file directives will appear in the pre-processed output */
404 /* Note that input_file.c hand checks for '#' at the beginning of the
405    first line of the input file.  This is because the compiler outputs
406    #NO_APP at the beginning of its output.  */
407 /* Also note that C style comments are always supported.  */
408 const char line_comment_chars[] = "#";
409
410 /* This array holds machine specific line separator characters.  */
411 const char line_separator_chars[] = ";";
412
413 /* Chars that can be used to separate mant from exp in floating point nums */
414 const char EXP_CHARS[] = "eE";
415
416 /* Chars that mean this number is a floating point constant */
417 /* As in 0f12.456 */
418 /* or    0d1.2345e12 */
419 const char FLT_CHARS[] = "rRsSfFdDxXpP";
420
421 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
422    changed in read.c .  Ideally it shouldn't have to know about it at all,
423    but nothing is ideal around here.
424  */
425
426 static char *insn_error;
427
428 static int auto_align = 1;
429
430 /* When outputting SVR4 PIC code, the assembler needs to know the
431    offset in the stack frame from which to restore the $gp register.
432    This is set by the .cprestore pseudo-op, and saved in this
433    variable.  */
434 static offsetT mips_cprestore_offset = -1;
435
436 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
437    more optimizations, it can use a register value instead of a memory-saved
438    offset and even an other register than $gp as global pointer.  */
439 static offsetT mips_cpreturn_offset = -1;
440 static int mips_cpreturn_register = -1;
441 static int mips_gp_register = GP;
442 static int mips_gprel_offset = 0;
443
444 /* Whether mips_cprestore_offset has been set in the current function
445    (or whether it has already been warned about, if not).  */
446 static int mips_cprestore_valid = 0;
447
448 /* This is the register which holds the stack frame, as set by the
449    .frame pseudo-op.  This is needed to implement .cprestore.  */
450 static int mips_frame_reg = SP;
451
452 /* Whether mips_frame_reg has been set in the current function
453    (or whether it has already been warned about, if not).  */
454 static int mips_frame_reg_valid = 0;
455
456 /* To output NOP instructions correctly, we need to keep information
457    about the previous two instructions.  */
458
459 /* Whether we are optimizing.  The default value of 2 means to remove
460    unneeded NOPs and swap branch instructions when possible.  A value
461    of 1 means to not swap branches.  A value of 0 means to always
462    insert NOPs.  */
463 static int mips_optimize = 2;
464
465 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
466    equivalent to seeing no -g option at all.  */
467 static int mips_debug = 0;
468
469 /* The previous instruction.  */
470 static struct mips_cl_insn prev_insn;
471
472 /* The instruction before prev_insn.  */
473 static struct mips_cl_insn prev_prev_insn;
474
475 /* If we don't want information for prev_insn or prev_prev_insn, we
476    point the insn_mo field at this dummy integer.  */
477 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
478
479 /* Non-zero if prev_insn is valid.  */
480 static int prev_insn_valid;
481
482 /* The frag for the previous instruction.  */
483 static struct frag *prev_insn_frag;
484
485 /* The offset into prev_insn_frag for the previous instruction.  */
486 static long prev_insn_where;
487
488 /* The reloc type for the previous instruction, if any.  */
489 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
490
491 /* The reloc for the previous instruction, if any.  */
492 static fixS *prev_insn_fixp[3];
493
494 /* Non-zero if the previous instruction was in a delay slot.  */
495 static int prev_insn_is_delay_slot;
496
497 /* Non-zero if the previous instruction was in a .set noreorder.  */
498 static int prev_insn_unreordered;
499
500 /* Non-zero if the previous instruction uses an extend opcode (if
501    mips16).  */
502 static int prev_insn_extended;
503
504 /* Non-zero if the previous previous instruction was in a .set
505    noreorder.  */
506 static int prev_prev_insn_unreordered;
507
508 /* If this is set, it points to a frag holding nop instructions which
509    were inserted before the start of a noreorder section.  If those
510    nops turn out to be unnecessary, the size of the frag can be
511    decreased.  */
512 static fragS *prev_nop_frag;
513
514 /* The number of nop instructions we created in prev_nop_frag.  */
515 static int prev_nop_frag_holds;
516
517 /* The number of nop instructions that we know we need in
518    prev_nop_frag.  */
519 static int prev_nop_frag_required;
520
521 /* The number of instructions we've seen since prev_nop_frag.  */
522 static int prev_nop_frag_since;
523
524 /* For ECOFF and ELF, relocations against symbols are done in two
525    parts, with a HI relocation and a LO relocation.  Each relocation
526    has only 16 bits of space to store an addend.  This means that in
527    order for the linker to handle carries correctly, it must be able
528    to locate both the HI and the LO relocation.  This means that the
529    relocations must appear in order in the relocation table.
530
531    In order to implement this, we keep track of each unmatched HI
532    relocation.  We then sort them so that they immediately precede the
533    corresponding LO relocation.  */
534
535 struct mips_hi_fixup
536 {
537   /* Next HI fixup.  */
538   struct mips_hi_fixup *next;
539   /* This fixup.  */
540   fixS *fixp;
541   /* The section this fixup is in.  */
542   segT seg;
543 };
544
545 /* The list of unmatched HI relocs.  */
546
547 static struct mips_hi_fixup *mips_hi_fixup_list;
548
549 /* Map normal MIPS register numbers to mips16 register numbers.  */
550
551 #define X ILLEGAL_REG
552 static const int mips32_to_16_reg_map[] =
553 {
554   X, X, 2, 3, 4, 5, 6, 7,
555   X, X, X, X, X, X, X, X,
556   0, 1, X, X, X, X, X, X,
557   X, X, X, X, X, X, X, X
558 };
559 #undef X
560
561 /* Map mips16 register numbers to normal MIPS register numbers.  */
562
563 static const unsigned int mips16_to_32_reg_map[] =
564 {
565   16, 17, 2, 3, 4, 5, 6, 7
566 };
567 \f
568 /* Since the MIPS does not have multiple forms of PC relative
569    instructions, we do not have to do relaxing as is done on other
570    platforms.  However, we do have to handle GP relative addressing
571    correctly, which turns out to be a similar problem.
572
573    Every macro that refers to a symbol can occur in (at least) two
574    forms, one with GP relative addressing and one without.  For
575    example, loading a global variable into a register generally uses
576    a macro instruction like this:
577      lw $4,i
578    If i can be addressed off the GP register (this is true if it is in
579    the .sbss or .sdata section, or if it is known to be smaller than
580    the -G argument) this will generate the following instruction:
581      lw $4,i($gp)
582    This instruction will use a GPREL reloc.  If i can not be addressed
583    off the GP register, the following instruction sequence will be used:
584      lui $at,i
585      lw $4,i($at)
586    In this case the first instruction will have a HI16 reloc, and the
587    second reloc will have a LO16 reloc.  Both relocs will be against
588    the symbol i.
589
590    The issue here is that we may not know whether i is GP addressable
591    until after we see the instruction that uses it.  Therefore, we
592    want to be able to choose the final instruction sequence only at
593    the end of the assembly.  This is similar to the way other
594    platforms choose the size of a PC relative instruction only at the
595    end of assembly.
596
597    When generating position independent code we do not use GP
598    addressing in quite the same way, but the issue still arises as
599    external symbols and local symbols must be handled differently.
600
601    We handle these issues by actually generating both possible
602    instruction sequences.  The longer one is put in a frag_var with
603    type rs_machine_dependent.  We encode what to do with the frag in
604    the subtype field.  We encode (1) the number of existing bytes to
605    replace, (2) the number of new bytes to use, (3) the offset from
606    the start of the existing bytes to the first reloc we must generate
607    (that is, the offset is applied from the start of the existing
608    bytes after they are replaced by the new bytes, if any), (4) the
609    offset from the start of the existing bytes to the second reloc,
610    (5) whether a third reloc is needed (the third reloc is always four
611    bytes after the second reloc), and (6) whether to warn if this
612    variant is used (this is sometimes needed if .set nomacro or .set
613    noat is in effect).  All these numbers are reasonably small.
614
615    Generating two instruction sequences must be handled carefully to
616    ensure that delay slots are handled correctly.  Fortunately, there
617    are a limited number of cases.  When the second instruction
618    sequence is generated, append_insn is directed to maintain the
619    existing delay slot information, so it continues to apply to any
620    code after the second instruction sequence.  This means that the
621    second instruction sequence must not impose any requirements not
622    required by the first instruction sequence.
623
624    These variant frags are then handled in functions called by the
625    machine independent code.  md_estimate_size_before_relax returns
626    the final size of the frag.  md_convert_frag sets up the final form
627    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
628    one if needed.  */
629 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
630   ((relax_substateT) \
631    (((old) << 23) \
632     | ((new) << 16) \
633     | (((reloc1) + 64) << 9) \
634     | (((reloc2) + 64) << 2) \
635     | ((reloc3) ? (1 << 1) : 0) \
636     | ((warn) ? 1 : 0)))
637 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
638 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
639 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
640 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
641 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
642 #define RELAX_WARN(i) ((i) & 1)
643
644 /* For mips16 code, we use an entirely different form of relaxation.
645    mips16 supports two versions of most instructions which take
646    immediate values: a small one which takes some small value, and a
647    larger one which takes a 16 bit value.  Since branches also follow
648    this pattern, relaxing these values is required.
649
650    We can assemble both mips16 and normal MIPS code in a single
651    object.  Therefore, we need to support this type of relaxation at
652    the same time that we support the relaxation described above.  We
653    use the high bit of the subtype field to distinguish these cases.
654
655    The information we store for this type of relaxation is the
656    argument code found in the opcode file for this relocation, whether
657    the user explicitly requested a small or extended form, and whether
658    the relocation is in a jump or jal delay slot.  That tells us the
659    size of the value, and how it should be stored.  We also store
660    whether the fragment is considered to be extended or not.  We also
661    store whether this is known to be a branch to a different section,
662    whether we have tried to relax this frag yet, and whether we have
663    ever extended a PC relative fragment because of a shift count.  */
664 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
665   (0x80000000                                                   \
666    | ((type) & 0xff)                                            \
667    | ((small) ? 0x100 : 0)                                      \
668    | ((ext) ? 0x200 : 0)                                        \
669    | ((dslot) ? 0x400 : 0)                                      \
670    | ((jal_dslot) ? 0x800 : 0))
671 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
672 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
673 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
674 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
675 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
676 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
677 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
678 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
679 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
680 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
681 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
682 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
683 \f
684 /* Prototypes for static functions.  */
685
686 #ifdef __STDC__
687 #define internalError() \
688     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
689 #else
690 #define internalError() as_fatal (_("MIPS internal Error"));
691 #endif
692
693 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
694
695 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
696                                   unsigned int reg, enum mips_regclass class));
697 static int reg_needs_delay PARAMS ((unsigned int));
698 static void mips16_mark_labels PARAMS ((void));
699 static void append_insn PARAMS ((char *place,
700                                  struct mips_cl_insn * ip,
701                                  expressionS * p,
702                                  bfd_reloc_code_real_type *r,
703                                  boolean));
704 static void mips_no_prev_insn PARAMS ((int));
705 static void mips_emit_delays PARAMS ((boolean));
706 #ifdef USE_STDARG
707 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
708                                  const char *name, const char *fmt,
709                                  ...));
710 #else
711 static void macro_build ();
712 #endif
713 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
714                                         const char *, const char *,
715                                         va_list));
716 static void macro_build_jalr PARAMS ((int, expressionS *));
717 static void macro_build_lui PARAMS ((char *place, int *counter,
718                                      expressionS * ep, int regnum));
719 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
720 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
721                                          expressionS *));
722 static void load_register PARAMS ((int *, int, expressionS *, int));
723 static void load_address PARAMS ((int *, int, expressionS *, int *));
724 static void move_register PARAMS ((int *, int, int));
725 static void macro PARAMS ((struct mips_cl_insn * ip));
726 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
727 #ifdef LOSING_COMPILER
728 static void macro2 PARAMS ((struct mips_cl_insn * ip));
729 #endif
730 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
731 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
732 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
733                                   boolean, boolean, unsigned long *,
734                                   boolean *, unsigned short *));
735 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
736 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
737 static int my_getSmallExpression PARAMS ((expressionS *, char *));
738 static void my_getExpression PARAMS ((expressionS *, char *));
739 #ifdef OBJ_ELF
740 static int support_64bit_objects PARAMS((void));
741 #endif
742 static symbolS *get_symbol PARAMS ((void));
743 static void mips_align PARAMS ((int to, int fill, symbolS *label));
744 static void s_align PARAMS ((int));
745 static void s_change_sec PARAMS ((int));
746 static void s_cons PARAMS ((int));
747 static void s_float_cons PARAMS ((int));
748 static void s_mips_globl PARAMS ((int));
749 static void s_option PARAMS ((int));
750 static void s_mipsset PARAMS ((int));
751 static void s_abicalls PARAMS ((int));
752 static void s_cpload PARAMS ((int));
753 static void s_cpsetup PARAMS ((int));
754 static void s_cplocal PARAMS ((int));
755 static void s_cprestore PARAMS ((int));
756 static void s_cpreturn PARAMS ((int));
757 static void s_gpvalue PARAMS ((int));
758 static void s_gpword PARAMS ((int));
759 static void s_cpadd PARAMS ((int));
760 static void s_insn PARAMS ((int));
761 static void md_obj_begin PARAMS ((void));
762 static void md_obj_end PARAMS ((void));
763 static long get_number PARAMS ((void));
764 static void s_mips_ent PARAMS ((int));
765 static void s_mips_end PARAMS ((int));
766 static void s_mips_frame PARAMS ((int));
767 static void s_mips_mask PARAMS ((int));
768 static void s_mips_stab PARAMS ((int));
769 static void s_mips_weakext PARAMS ((int));
770 static void s_mips_file PARAMS ((int));
771 static void s_mips_loc PARAMS ((int));
772 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
773 static const char *mips_isa_to_str PARAMS ((int));
774 static const char *mips_cpu_to_str PARAMS ((int));
775 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
776 static void show PARAMS ((FILE *, char *, int *, int *));
777 #ifdef OBJ_ELF
778 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
779 #endif
780
781 /* Return values of my_getSmallExpression().  */
782
783 enum small_ex_type
784 {
785   S_EX_NONE = 0,
786   S_EX_REGISTER,
787
788   /* Direct relocation creation by %percent_op().  */
789   S_EX_HALF,
790   S_EX_HI,
791   S_EX_LO,
792   S_EX_GP_REL,
793   S_EX_GOT,
794   S_EX_CALL16,
795   S_EX_GOT_DISP,
796   S_EX_GOT_PAGE,
797   S_EX_GOT_OFST,
798   S_EX_GOT_HI,
799   S_EX_GOT_LO,
800   S_EX_NEG,
801   S_EX_HIGHER,
802   S_EX_HIGHEST,
803   S_EX_CALL_HI,
804   S_EX_CALL_LO
805 };
806
807 /* Table and functions used to map between CPU/ISA names, and
808    ISA levels, and CPU numbers.  */
809
810 struct mips_cpu_info
811 {
812   const char *name;           /* CPU or ISA name.  */
813   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
814   int isa;                    /* ISA level.  */
815   int cpu;                    /* CPU number (default CPU if ISA).  */
816 };
817
818 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
819 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
820 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
821 \f
822 /* Pseudo-op table.
823
824    The following pseudo-ops from the Kane and Heinrich MIPS book
825    should be defined here, but are currently unsupported: .alias,
826    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
827
828    The following pseudo-ops from the Kane and Heinrich MIPS book are
829    specific to the type of debugging information being generated, and
830    should be defined by the object format: .aent, .begin, .bend,
831    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
832    .vreg.
833
834    The following pseudo-ops from the Kane and Heinrich MIPS book are
835    not MIPS CPU specific, but are also not specific to the object file
836    format.  This file is probably the best place to define them, but
837    they are not currently supported: .asm0, .endr, .lab, .repeat,
838    .struct.  */
839
840 static const pseudo_typeS mips_pseudo_table[] =
841 {
842   /* MIPS specific pseudo-ops.  */
843   {"option", s_option, 0},
844   {"set", s_mipsset, 0},
845   {"rdata", s_change_sec, 'r'},
846   {"sdata", s_change_sec, 's'},
847   {"livereg", s_ignore, 0},
848   {"abicalls", s_abicalls, 0},
849   {"cpload", s_cpload, 0},
850   {"cpsetup", s_cpsetup, 0},
851   {"cplocal", s_cplocal, 0},
852   {"cprestore", s_cprestore, 0},
853   {"cpreturn", s_cpreturn, 0},
854   {"gpvalue", s_gpvalue, 0},
855   {"gpword", s_gpword, 0},
856   {"cpadd", s_cpadd, 0},
857   {"insn", s_insn, 0},
858
859   /* Relatively generic pseudo-ops that happen to be used on MIPS
860      chips.  */
861   {"asciiz", stringer, 1},
862   {"bss", s_change_sec, 'b'},
863   {"err", s_err, 0},
864   {"half", s_cons, 1},
865   {"dword", s_cons, 3},
866   {"weakext", s_mips_weakext, 0},
867
868   /* These pseudo-ops are defined in read.c, but must be overridden
869      here for one reason or another.  */
870   {"align", s_align, 0},
871   {"byte", s_cons, 0},
872   {"data", s_change_sec, 'd'},
873   {"double", s_float_cons, 'd'},
874   {"float", s_float_cons, 'f'},
875   {"globl", s_mips_globl, 0},
876   {"global", s_mips_globl, 0},
877   {"hword", s_cons, 1},
878   {"int", s_cons, 2},
879   {"long", s_cons, 2},
880   {"octa", s_cons, 4},
881   {"quad", s_cons, 3},
882   {"short", s_cons, 1},
883   {"single", s_float_cons, 'f'},
884   {"stabn", s_mips_stab, 'n'},
885   {"text", s_change_sec, 't'},
886   {"word", s_cons, 2},
887
888 #ifdef MIPS_STABS_ELF
889   { "extern", ecoff_directive_extern, 0},
890 #endif
891
892   { NULL, NULL, 0 },
893 };
894
895 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
896 {
897   /* These pseudo-ops should be defined by the object file format.
898      However, a.out doesn't support them, so we have versions here.  */
899   {"aent", s_mips_ent, 1},
900   {"bgnb", s_ignore, 0},
901   {"end", s_mips_end, 0},
902   {"endb", s_ignore, 0},
903   {"ent", s_mips_ent, 0},
904   {"file", s_mips_file, 0},
905   {"fmask", s_mips_mask, 'F'},
906   {"frame", s_mips_frame, 0},
907   {"loc", s_mips_loc, 0},
908   {"mask", s_mips_mask, 'R'},
909   {"verstamp", s_ignore, 0},
910   { NULL, NULL, 0 },
911 };
912
913 extern void pop_insert PARAMS ((const pseudo_typeS *));
914
915 void
916 mips_pop_insert ()
917 {
918   pop_insert (mips_pseudo_table);
919   if (! ECOFF_DEBUGGING)
920     pop_insert (mips_nonecoff_pseudo_table);
921 }
922 \f
923 /* Symbols labelling the current insn.  */
924
925 struct insn_label_list
926 {
927   struct insn_label_list *next;
928   symbolS *label;
929 };
930
931 static struct insn_label_list *insn_labels;
932 static struct insn_label_list *free_insn_labels;
933
934 static void mips_clear_insn_labels PARAMS ((void));
935
936 static inline void
937 mips_clear_insn_labels ()
938 {
939   register struct insn_label_list **pl;
940
941   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
942     ;
943   *pl = insn_labels;
944   insn_labels = NULL;
945 }
946 \f
947 static char *expr_end;
948
949 /* Expressions which appear in instructions.  These are set by
950    mips_ip.  */
951
952 static expressionS imm_expr;
953 static expressionS offset_expr;
954
955 /* Relocs associated with imm_expr and offset_expr.  */
956
957 static bfd_reloc_code_real_type imm_reloc[3]
958   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
959 static bfd_reloc_code_real_type offset_reloc[3]
960   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
961
962 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
963
964 static boolean imm_unmatched_hi;
965
966 /* These are set by mips16_ip if an explicit extension is used.  */
967
968 static boolean mips16_small, mips16_ext;
969
970 #ifdef MIPS_STABS_ELF
971 /* The pdr segment for per procedure frame/regmask info */
972
973 static segT pdr_seg;
974 #endif
975
976 static const char *
977 mips_isa_to_str (isa)
978      int isa;
979 {
980   const struct mips_cpu_info *ci;
981   static char s[20];
982
983   ci = mips_cpu_info_from_isa (isa);
984   if (ci != NULL)
985     return (ci->name);
986
987   sprintf (s, "ISA#%d", isa);
988   return s;
989 }
990
991 static const char *
992 mips_cpu_to_str (cpu)
993      int cpu;
994 {
995   const struct mips_cpu_info *ci;
996   static char s[16];
997
998   ci = mips_cpu_info_from_cpu (cpu);
999   if (ci != NULL)
1000     return (ci->name);
1001
1002   sprintf (s, "CPU#%d", cpu);
1003   return s;
1004 }
1005
1006 /* The default target format to use.  */
1007
1008 const char *
1009 mips_target_format ()
1010 {
1011   switch (OUTPUT_FLAVOR)
1012     {
1013     case bfd_target_aout_flavour:
1014       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1015     case bfd_target_ecoff_flavour:
1016       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1017     case bfd_target_coff_flavour:
1018       return "pe-mips";
1019     case bfd_target_elf_flavour:
1020 #ifdef TE_TMIPS
1021       /* This is traditional mips */
1022       return (target_big_endian
1023               ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
1024                  : "elf32-tradbigmips")
1025               : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
1026                  : "elf32-tradlittlemips"));
1027 #else
1028       return (target_big_endian
1029               ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
1030               : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1031                  : "elf32-littlemips"));
1032 #endif
1033     default:
1034       abort ();
1035       return NULL;
1036     }
1037 }
1038
1039 /* This function is called once, at assembler startup time.  It should
1040    set up all the tables, etc. that the MD part of the assembler will need.  */
1041
1042 void
1043 md_begin ()
1044 {
1045   register const char *retval = NULL;
1046   int i = 0;
1047   int broken = 0;
1048
1049   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1050     as_warn (_("Could not set architecture and machine"));
1051
1052   op_hash = hash_new ();
1053
1054   for (i = 0; i < NUMOPCODES;)
1055     {
1056       const char *name = mips_opcodes[i].name;
1057
1058       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1059       if (retval != NULL)
1060         {
1061           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1062                    mips_opcodes[i].name, retval);
1063           /* Probably a memory allocation problem?  Give up now.  */
1064           as_fatal (_("Broken assembler.  No assembly attempted."));
1065         }
1066       do
1067         {
1068           if (mips_opcodes[i].pinfo != INSN_MACRO)
1069             {
1070               if (!validate_mips_insn (&mips_opcodes[i]))
1071                 broken = 1;
1072             }
1073           ++i;
1074         }
1075       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1076     }
1077
1078   mips16_op_hash = hash_new ();
1079
1080   i = 0;
1081   while (i < bfd_mips16_num_opcodes)
1082     {
1083       const char *name = mips16_opcodes[i].name;
1084
1085       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1086       if (retval != NULL)
1087         as_fatal (_("internal: can't hash `%s': %s"),
1088                   mips16_opcodes[i].name, retval);
1089       do
1090         {
1091           if (mips16_opcodes[i].pinfo != INSN_MACRO
1092               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1093                   != mips16_opcodes[i].match))
1094             {
1095               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1096                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1097               broken = 1;
1098             }
1099           ++i;
1100         }
1101       while (i < bfd_mips16_num_opcodes
1102              && strcmp (mips16_opcodes[i].name, name) == 0);
1103     }
1104
1105   if (broken)
1106     as_fatal (_("Broken assembler.  No assembly attempted."));
1107
1108   /* We add all the general register names to the symbol table.  This
1109      helps us detect invalid uses of them.  */
1110   for (i = 0; i < 32; i++)
1111     {
1112       char buf[5];
1113
1114       sprintf (buf, "$%d", i);
1115       symbol_table_insert (symbol_new (buf, reg_section, i,
1116                                        &zero_address_frag));
1117     }
1118   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1119                                    &zero_address_frag));
1120   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1121                                    &zero_address_frag));
1122   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1123                                    &zero_address_frag));
1124   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1125                                    &zero_address_frag));
1126   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1127                                    &zero_address_frag));
1128   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1129                                    &zero_address_frag));
1130   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1131                                    &zero_address_frag));
1132   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1133                                    &zero_address_frag));
1134   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1135                                    &zero_address_frag));
1136
1137   mips_no_prev_insn (false);
1138
1139   mips_gprmask = 0;
1140   mips_cprmask[0] = 0;
1141   mips_cprmask[1] = 0;
1142   mips_cprmask[2] = 0;
1143   mips_cprmask[3] = 0;
1144
1145   /* set the default alignment for the text section (2**2) */
1146   record_alignment (text_section, 2);
1147
1148   if (USE_GLOBAL_POINTER_OPT)
1149     bfd_set_gp_size (stdoutput, g_switch_value);
1150
1151   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1152     {
1153       /* On a native system, sections must be aligned to 16 byte
1154          boundaries.  When configured for an embedded ELF target, we
1155          don't bother.  */
1156       if (strcmp (TARGET_OS, "elf") != 0)
1157         {
1158           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1159           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1160           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1161         }
1162
1163       /* Create a .reginfo section for register masks and a .mdebug
1164          section for debugging information.  */
1165       {
1166         segT seg;
1167         subsegT subseg;
1168         flagword flags;
1169         segT sec;
1170
1171         seg = now_seg;
1172         subseg = now_subseg;
1173
1174         /* The ABI says this section should be loaded so that the
1175            running program can access it.  However, we don't load it
1176            if we are configured for an embedded target */
1177         flags = SEC_READONLY | SEC_DATA;
1178         if (strcmp (TARGET_OS, "elf") != 0)
1179           flags |= SEC_ALLOC | SEC_LOAD;
1180
1181         if (file_mips_abi != N64_ABI)
1182           {
1183             sec = subseg_new (".reginfo", (subsegT) 0);
1184
1185             bfd_set_section_flags (stdoutput, sec, flags);
1186             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1187
1188 #ifdef OBJ_ELF
1189             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1190 #endif
1191           }
1192         else
1193           {
1194             /* The 64-bit ABI uses a .MIPS.options section rather than
1195                .reginfo section.  */
1196             sec = subseg_new (".MIPS.options", (subsegT) 0);
1197             bfd_set_section_flags (stdoutput, sec, flags);
1198             bfd_set_section_alignment (stdoutput, sec, 3);
1199
1200 #ifdef OBJ_ELF
1201             /* Set up the option header.  */
1202             {
1203               Elf_Internal_Options opthdr;
1204               char *f;
1205
1206               opthdr.kind = ODK_REGINFO;
1207               opthdr.size = (sizeof (Elf_External_Options)
1208                              + sizeof (Elf64_External_RegInfo));
1209               opthdr.section = 0;
1210               opthdr.info = 0;
1211               f = frag_more (sizeof (Elf_External_Options));
1212               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1213                                              (Elf_External_Options *) f);
1214
1215               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1216             }
1217 #endif
1218           }
1219
1220         if (ECOFF_DEBUGGING)
1221           {
1222             sec = subseg_new (".mdebug", (subsegT) 0);
1223             (void) bfd_set_section_flags (stdoutput, sec,
1224                                           SEC_HAS_CONTENTS | SEC_READONLY);
1225             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1226           }
1227
1228 #ifdef MIPS_STABS_ELF
1229         pdr_seg = subseg_new (".pdr", (subsegT) 0);
1230         (void) bfd_set_section_flags (stdoutput, pdr_seg,
1231                              SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1232         (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1233 #endif
1234
1235         subseg_set (seg, subseg);
1236       }
1237     }
1238
1239   if (! ECOFF_DEBUGGING)
1240     md_obj_begin ();
1241 }
1242
1243 void
1244 md_mips_end ()
1245 {
1246   if (! ECOFF_DEBUGGING)
1247     md_obj_end ();
1248 }
1249
1250 void
1251 md_assemble (str)
1252      char *str;
1253 {
1254   struct mips_cl_insn insn;
1255   bfd_reloc_code_real_type unused_reloc[3]
1256     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1257
1258   imm_expr.X_op = O_absent;
1259   imm_unmatched_hi = false;
1260   offset_expr.X_op = O_absent;
1261   imm_reloc[0] = BFD_RELOC_UNUSED;
1262   imm_reloc[1] = BFD_RELOC_UNUSED;
1263   imm_reloc[2] = BFD_RELOC_UNUSED;
1264   offset_reloc[0] = BFD_RELOC_UNUSED;
1265   offset_reloc[1] = BFD_RELOC_UNUSED;
1266   offset_reloc[2] = BFD_RELOC_UNUSED;
1267
1268   if (mips_opts.mips16)
1269     mips16_ip (str, &insn);
1270   else
1271     {
1272       mips_ip (str, &insn);
1273       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1274             str, insn.insn_opcode));
1275     }
1276
1277   if (insn_error)
1278     {
1279       as_bad ("%s `%s'", insn_error, str);
1280       return;
1281     }
1282
1283   if (insn.insn_mo->pinfo == INSN_MACRO)
1284     {
1285       if (mips_opts.mips16)
1286         mips16_macro (&insn);
1287       else
1288         macro (&insn);
1289     }
1290   else
1291     {
1292       if (imm_expr.X_op != O_absent)
1293         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1294       else if (offset_expr.X_op != O_absent)
1295         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1296       else
1297         append_insn (NULL, &insn, NULL, unused_reloc, false);
1298     }
1299 }
1300
1301 /* See whether instruction IP reads register REG.  CLASS is the type
1302    of register.  */
1303
1304 static int
1305 insn_uses_reg (ip, reg, class)
1306      struct mips_cl_insn *ip;
1307      unsigned int reg;
1308      enum mips_regclass class;
1309 {
1310   if (class == MIPS16_REG)
1311     {
1312       assert (mips_opts.mips16);
1313       reg = mips16_to_32_reg_map[reg];
1314       class = MIPS_GR_REG;
1315     }
1316
1317   /* Don't report on general register ZERO, since it never changes.  */
1318   if (class == MIPS_GR_REG && reg == ZERO)
1319     return 0;
1320
1321   if (class == MIPS_FP_REG)
1322     {
1323       assert (! mips_opts.mips16);
1324       /* If we are called with either $f0 or $f1, we must check $f0.
1325          This is not optimal, because it will introduce an unnecessary
1326          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1327          need to distinguish reading both $f0 and $f1 or just one of
1328          them.  Note that we don't have to check the other way,
1329          because there is no instruction that sets both $f0 and $f1
1330          and requires a delay.  */
1331       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1332           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1333               == (reg &~ (unsigned) 1)))
1334         return 1;
1335       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1336           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1337               == (reg &~ (unsigned) 1)))
1338         return 1;
1339     }
1340   else if (! mips_opts.mips16)
1341     {
1342       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1343           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1344         return 1;
1345       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1346           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1347         return 1;
1348     }
1349   else
1350     {
1351       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1352           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1353                                     & MIPS16OP_MASK_RX)]
1354               == reg))
1355         return 1;
1356       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1357           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1358                                     & MIPS16OP_MASK_RY)]
1359               == reg))
1360         return 1;
1361       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1362           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1363                                     & MIPS16OP_MASK_MOVE32Z)]
1364               == reg))
1365         return 1;
1366       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1367         return 1;
1368       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1369         return 1;
1370       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1371         return 1;
1372       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1373           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1374               & MIPS16OP_MASK_REGR32) == reg)
1375         return 1;
1376     }
1377
1378   return 0;
1379 }
1380
1381 /* This function returns true if modifying a register requires a
1382    delay.  */
1383
1384 static int
1385 reg_needs_delay (reg)
1386      unsigned int reg;
1387 {
1388   unsigned long prev_pinfo;
1389
1390   prev_pinfo = prev_insn.insn_mo->pinfo;
1391   if (! mips_opts.noreorder
1392       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1393       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1394           || (! gpr_interlocks
1395               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1396     {
1397       /* A load from a coprocessor or from memory.  All load
1398          delays delay the use of general register rt for one
1399          instruction on the r3000.  The r6000 and r4000 use
1400          interlocks.  */
1401       /* Itbl support may require additional care here.  */
1402       know (prev_pinfo & INSN_WRITE_GPR_T);
1403       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1404         return 1;
1405     }
1406
1407   return 0;
1408 }
1409
1410 /* Mark instruction labels in mips16 mode.  This permits the linker to
1411    handle them specially, such as generating jalx instructions when
1412    needed.  We also make them odd for the duration of the assembly, in
1413    order to generate the right sort of code.  We will make them even
1414    in the adjust_symtab routine, while leaving them marked.  This is
1415    convenient for the debugger and the disassembler.  The linker knows
1416    to make them odd again.  */
1417
1418 static void
1419 mips16_mark_labels ()
1420 {
1421   if (mips_opts.mips16)
1422     {
1423       struct insn_label_list *l;
1424       valueT val;
1425
1426       for (l = insn_labels; l != NULL; l = l->next)
1427         {
1428 #ifdef OBJ_ELF
1429           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1430             S_SET_OTHER (l->label, STO_MIPS16);
1431 #endif
1432           val = S_GET_VALUE (l->label);
1433           if ((val & 1) == 0)
1434             S_SET_VALUE (l->label, val + 1);
1435         }
1436     }
1437 }
1438
1439 /* Output an instruction.  PLACE is where to put the instruction; if
1440    it is NULL, this uses frag_more to get room.  IP is the instruction
1441    information.  ADDRESS_EXPR is an operand of the instruction to be
1442    used with RELOC_TYPE.  */
1443
1444 static void
1445 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1446      char *place;
1447      struct mips_cl_insn *ip;
1448      expressionS *address_expr;
1449      bfd_reloc_code_real_type *reloc_type;
1450      boolean unmatched_hi;
1451 {
1452   register unsigned long prev_pinfo, pinfo;
1453   char *f;
1454   fixS *fixp[3];
1455   int nops = 0;
1456
1457   /* Mark instruction labels in mips16 mode.  */
1458   mips16_mark_labels ();
1459
1460   prev_pinfo = prev_insn.insn_mo->pinfo;
1461   pinfo = ip->insn_mo->pinfo;
1462
1463   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1464     {
1465       int prev_prev_nop;
1466
1467       /* If the previous insn required any delay slots, see if we need
1468          to insert a NOP or two.  There are eight kinds of possible
1469          hazards, of which an instruction can have at most one type.
1470          (1) a load from memory delay
1471          (2) a load from a coprocessor delay
1472          (3) an unconditional branch delay
1473          (4) a conditional branch delay
1474          (5) a move to coprocessor register delay
1475          (6) a load coprocessor register from memory delay
1476          (7) a coprocessor condition code delay
1477          (8) a HI/LO special register delay
1478
1479          There are a lot of optimizations we could do that we don't.
1480          In particular, we do not, in general, reorder instructions.
1481          If you use gcc with optimization, it will reorder
1482          instructions and generally do much more optimization then we
1483          do here; repeating all that work in the assembler would only
1484          benefit hand written assembly code, and does not seem worth
1485          it.  */
1486
1487       /* This is how a NOP is emitted.  */
1488 #define emit_nop()                                      \
1489   (mips_opts.mips16                                     \
1490    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1491    : md_number_to_chars (frag_more (4), 0, 4))
1492
1493       /* The previous insn might require a delay slot, depending upon
1494          the contents of the current insn.  */
1495       if (! mips_opts.mips16
1496           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1497           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1498                && ! cop_interlocks)
1499               || (! gpr_interlocks
1500                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1501         {
1502           /* A load from a coprocessor or from memory.  All load
1503              delays delay the use of general register rt for one
1504              instruction on the r3000.  The r6000 and r4000 use
1505              interlocks.  */
1506           /* Itbl support may require additional care here.  */
1507           know (prev_pinfo & INSN_WRITE_GPR_T);
1508           if (mips_optimize == 0
1509               || insn_uses_reg (ip,
1510                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1511                                  & OP_MASK_RT),
1512                                 MIPS_GR_REG))
1513             ++nops;
1514         }
1515       else if (! mips_opts.mips16
1516                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1517                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1518                     && ! cop_interlocks)
1519                    || (mips_opts.isa == ISA_MIPS1
1520                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1521         {
1522           /* A generic coprocessor delay.  The previous instruction
1523              modified a coprocessor general or control register.  If
1524              it modified a control register, we need to avoid any
1525              coprocessor instruction (this is probably not always
1526              required, but it sometimes is).  If it modified a general
1527              register, we avoid using that register.
1528
1529              On the r6000 and r4000 loading a coprocessor register
1530              from memory is interlocked, and does not require a delay.
1531
1532              This case is not handled very well.  There is no special
1533              knowledge of CP0 handling, and the coprocessors other
1534              than the floating point unit are not distinguished at
1535              all.  */
1536           /* Itbl support may require additional care here. FIXME!
1537              Need to modify this to include knowledge about
1538              user specified delays!  */
1539           if (prev_pinfo & INSN_WRITE_FPR_T)
1540             {
1541               if (mips_optimize == 0
1542                   || insn_uses_reg (ip,
1543                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1544                                      & OP_MASK_FT),
1545                                     MIPS_FP_REG))
1546                 ++nops;
1547             }
1548           else if (prev_pinfo & INSN_WRITE_FPR_S)
1549             {
1550               if (mips_optimize == 0
1551                   || insn_uses_reg (ip,
1552                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1553                                      & OP_MASK_FS),
1554                                     MIPS_FP_REG))
1555                 ++nops;
1556             }
1557           else
1558             {
1559               /* We don't know exactly what the previous instruction
1560                  does.  If the current instruction uses a coprocessor
1561                  register, we must insert a NOP.  If previous
1562                  instruction may set the condition codes, and the
1563                  current instruction uses them, we must insert two
1564                  NOPS.  */
1565               /* Itbl support may require additional care here.  */
1566               if (mips_optimize == 0
1567                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1568                       && (pinfo & INSN_READ_COND_CODE)))
1569                 nops += 2;
1570               else if (pinfo & INSN_COP)
1571                 ++nops;
1572             }
1573         }
1574       else if (! mips_opts.mips16
1575                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1576                && (prev_pinfo & INSN_WRITE_COND_CODE)
1577                && ! cop_interlocks)
1578         {
1579           /* The previous instruction sets the coprocessor condition
1580              codes, but does not require a general coprocessor delay
1581              (this means it is a floating point comparison
1582              instruction).  If this instruction uses the condition
1583              codes, we need to insert a single NOP.  */
1584           /* Itbl support may require additional care here.  */
1585           if (mips_optimize == 0
1586               || (pinfo & INSN_READ_COND_CODE))
1587             ++nops;
1588         }
1589
1590       /* If we're fixing up mfhi/mflo for the r7000 and the
1591          previous insn was an mfhi/mflo and the current insn
1592          reads the register that the mfhi/mflo wrote to, then
1593          insert two nops.  */
1594
1595       else if (mips_7000_hilo_fix
1596                && MF_HILO_INSN (prev_pinfo)
1597                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1598                                       & OP_MASK_RD),
1599                                  MIPS_GR_REG))
1600         {
1601           nops += 2;
1602         }
1603
1604       /* If we're fixing up mfhi/mflo for the r7000 and the
1605          2nd previous insn was an mfhi/mflo and the current insn
1606          reads the register that the mfhi/mflo wrote to, then
1607          insert one nop.  */
1608
1609       else if (mips_7000_hilo_fix
1610                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1611                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1612                                        & OP_MASK_RD),
1613                                     MIPS_GR_REG))
1614
1615         {
1616           ++nops;
1617         }
1618
1619       else if (prev_pinfo & INSN_READ_LO)
1620         {
1621           /* The previous instruction reads the LO register; if the
1622              current instruction writes to the LO register, we must
1623              insert two NOPS.  Some newer processors have interlocks.
1624              Also the tx39's multiply instructions can be exectuted
1625              immediatly after a read from HI/LO (without the delay),
1626              though the tx39's divide insns still do require the
1627              delay.  */
1628           if (! (hilo_interlocks
1629                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1630               && (mips_optimize == 0
1631                   || (pinfo & INSN_WRITE_LO)))
1632             nops += 2;
1633           /* Most mips16 branch insns don't have a delay slot.
1634              If a read from LO is immediately followed by a branch
1635              to a write to LO we have a read followed by a write
1636              less than 2 insns away.  We assume the target of
1637              a branch might be a write to LO, and insert a nop
1638              between a read and an immediately following branch.  */
1639           else if (mips_opts.mips16
1640                    && (mips_optimize == 0
1641                        || (pinfo & MIPS16_INSN_BRANCH)))
1642             ++nops;
1643         }
1644       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1645         {
1646           /* The previous instruction reads the HI register; if the
1647              current instruction writes to the HI register, we must
1648              insert a NOP.  Some newer processors have interlocks.
1649              Also the note tx39's multiply above.  */
1650           if (! (hilo_interlocks
1651                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1652               && (mips_optimize == 0
1653                   || (pinfo & INSN_WRITE_HI)))
1654             nops += 2;
1655           /* Most mips16 branch insns don't have a delay slot.
1656              If a read from HI is immediately followed by a branch
1657              to a write to HI we have a read followed by a write
1658              less than 2 insns away.  We assume the target of
1659              a branch might be a write to HI, and insert a nop
1660              between a read and an immediately following branch.  */
1661           else if (mips_opts.mips16
1662                    && (mips_optimize == 0
1663                        || (pinfo & MIPS16_INSN_BRANCH)))
1664             ++nops;
1665         }
1666
1667       /* If the previous instruction was in a noreorder section, then
1668          we don't want to insert the nop after all.  */
1669       /* Itbl support may require additional care here.  */
1670       if (prev_insn_unreordered)
1671         nops = 0;
1672
1673       /* There are two cases which require two intervening
1674          instructions: 1) setting the condition codes using a move to
1675          coprocessor instruction which requires a general coprocessor
1676          delay and then reading the condition codes 2) reading the HI
1677          or LO register and then writing to it (except on processors
1678          which have interlocks).  If we are not already emitting a NOP
1679          instruction, we must check for these cases compared to the
1680          instruction previous to the previous instruction.  */
1681       if ((! mips_opts.mips16
1682            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1683            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1684            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1685            && (pinfo & INSN_READ_COND_CODE)
1686            && ! cop_interlocks)
1687           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1688               && (pinfo & INSN_WRITE_LO)
1689               && ! (hilo_interlocks
1690                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1691           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1692               && (pinfo & INSN_WRITE_HI)
1693               && ! (hilo_interlocks
1694                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1695         prev_prev_nop = 1;
1696       else
1697         prev_prev_nop = 0;
1698
1699       if (prev_prev_insn_unreordered)
1700         prev_prev_nop = 0;
1701
1702       if (prev_prev_nop && nops == 0)
1703         ++nops;
1704
1705       /* If we are being given a nop instruction, don't bother with
1706          one of the nops we would otherwise output.  This will only
1707          happen when a nop instruction is used with mips_optimize set
1708          to 0.  */
1709       if (nops > 0
1710           && ! mips_opts.noreorder
1711           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1712         --nops;
1713
1714       /* Now emit the right number of NOP instructions.  */
1715       if (nops > 0 && ! mips_opts.noreorder)
1716         {
1717           fragS *old_frag;
1718           unsigned long old_frag_offset;
1719           int i;
1720           struct insn_label_list *l;
1721
1722           old_frag = frag_now;
1723           old_frag_offset = frag_now_fix ();
1724
1725           for (i = 0; i < nops; i++)
1726             emit_nop ();
1727
1728           if (listing)
1729             {
1730               listing_prev_line ();
1731               /* We may be at the start of a variant frag.  In case we
1732                  are, make sure there is enough space for the frag
1733                  after the frags created by listing_prev_line.  The
1734                  argument to frag_grow here must be at least as large
1735                  as the argument to all other calls to frag_grow in
1736                  this file.  We don't have to worry about being in the
1737                  middle of a variant frag, because the variants insert
1738                  all needed nop instructions themselves.  */
1739               frag_grow (40);
1740             }
1741
1742           for (l = insn_labels; l != NULL; l = l->next)
1743             {
1744               valueT val;
1745
1746               assert (S_GET_SEGMENT (l->label) == now_seg);
1747               symbol_set_frag (l->label, frag_now);
1748               val = (valueT) frag_now_fix ();
1749               /* mips16 text labels are stored as odd.  */
1750               if (mips_opts.mips16)
1751                 ++val;
1752               S_SET_VALUE (l->label, val);
1753             }
1754
1755 #ifndef NO_ECOFF_DEBUGGING
1756           if (ECOFF_DEBUGGING)
1757             ecoff_fix_loc (old_frag, old_frag_offset);
1758 #endif
1759         }
1760       else if (prev_nop_frag != NULL)
1761         {
1762           /* We have a frag holding nops we may be able to remove.  If
1763              we don't need any nops, we can decrease the size of
1764              prev_nop_frag by the size of one instruction.  If we do
1765              need some nops, we count them in prev_nops_required.  */
1766           if (prev_nop_frag_since == 0)
1767             {
1768               if (nops == 0)
1769                 {
1770                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1771                   --prev_nop_frag_holds;
1772                 }
1773               else
1774                 prev_nop_frag_required += nops;
1775             }
1776           else
1777             {
1778               if (prev_prev_nop == 0)
1779                 {
1780                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1781                   --prev_nop_frag_holds;
1782                 }
1783               else
1784                 ++prev_nop_frag_required;
1785             }
1786
1787           if (prev_nop_frag_holds <= prev_nop_frag_required)
1788             prev_nop_frag = NULL;
1789
1790           ++prev_nop_frag_since;
1791
1792           /* Sanity check: by the time we reach the second instruction
1793              after prev_nop_frag, we should have used up all the nops
1794              one way or another.  */
1795           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1796         }
1797     }
1798
1799   if (*reloc_type > BFD_RELOC_UNUSED)
1800     {
1801       /* We need to set up a variant frag.  */
1802       assert (mips_opts.mips16 && address_expr != NULL);
1803       f = frag_var (rs_machine_dependent, 4, 0,
1804                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1805                                          mips16_small, mips16_ext,
1806                                          (prev_pinfo
1807                                           & INSN_UNCOND_BRANCH_DELAY),
1808                                          (*prev_insn_reloc_type
1809                                           == BFD_RELOC_MIPS16_JMP)),
1810                     make_expr_symbol (address_expr), 0, NULL);
1811     }
1812   else if (place != NULL)
1813     f = place;
1814   else if (mips_opts.mips16
1815            && ! ip->use_extend
1816            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1817     {
1818       /* Make sure there is enough room to swap this instruction with
1819          a following jump instruction.  */
1820       frag_grow (6);
1821       f = frag_more (2);
1822     }
1823   else
1824     {
1825       if (mips_opts.mips16
1826           && mips_opts.noreorder
1827           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1828         as_warn (_("extended instruction in delay slot"));
1829
1830       f = frag_more (4);
1831     }
1832
1833   fixp[0] = fixp[1] = fixp[2] = NULL;
1834   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1835     {
1836       if (address_expr->X_op == O_constant)
1837         {
1838           valueT tmp;
1839
1840           switch (*reloc_type)
1841             {
1842             case BFD_RELOC_32:
1843               ip->insn_opcode |= address_expr->X_add_number;
1844               break;
1845
1846             case BFD_RELOC_MIPS_HIGHEST:
1847               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1848               tmp >>= 16;
1849               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1850               break;
1851
1852             case BFD_RELOC_MIPS_HIGHER:
1853               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1854               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1855               break;
1856
1857             case BFD_RELOC_HI16_S:
1858               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1859                                   >> 16) & 0xffff;
1860               break;
1861
1862             case BFD_RELOC_HI16:
1863               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1864               break;
1865
1866             case BFD_RELOC_LO16:
1867               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1868               break;
1869
1870             case BFD_RELOC_MIPS_JMP:
1871               if ((address_expr->X_add_number & 3) != 0)
1872                 as_bad (_("jump to misaligned address (0x%lx)"),
1873                         (unsigned long) address_expr->X_add_number);
1874               if (address_expr->X_add_number & ~0xfffffff
1875                   || address_expr->X_add_number > 0x7fffffc)
1876                 as_bad (_("jump address range overflow (0x%lx)"),
1877                         (unsigned long) address_expr->X_add_number);
1878               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1879               break;
1880
1881             case BFD_RELOC_MIPS16_JMP:
1882               if ((address_expr->X_add_number & 3) != 0)
1883                 as_bad (_("jump to misaligned address (0x%lx)"),
1884                         (unsigned long) address_expr->X_add_number);
1885               if (address_expr->X_add_number & ~0xfffffff
1886                   || address_expr->X_add_number > 0x7fffffc)
1887                 as_bad (_("jump address range overflow (0x%lx)"),
1888                         (unsigned long) address_expr->X_add_number);
1889               ip->insn_opcode |=
1890                 (((address_expr->X_add_number & 0x7c0000) << 3)
1891                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1892                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1893               break;
1894
1895             case BFD_RELOC_16_PCREL:
1896               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1897               break;
1898
1899             case BFD_RELOC_16_PCREL_S2:
1900               goto need_reloc;
1901
1902             default:
1903               internalError ();
1904             }
1905         }
1906       else
1907         {
1908         need_reloc:
1909           /* Don't generate a reloc if we are writing into a variant frag.  */
1910           if (place == NULL)
1911             {
1912               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1913                                      address_expr,
1914                                      (*reloc_type == BFD_RELOC_16_PCREL
1915                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
1916                                      reloc_type[0]);
1917
1918               /* These relocations can have an addend that won't fit in
1919                  4 octets for 64bit assembly.  */
1920               if (HAVE_64BIT_GPRS &&
1921                   (*reloc_type == BFD_RELOC_16
1922                    || *reloc_type == BFD_RELOC_32
1923                    || *reloc_type == BFD_RELOC_MIPS_JMP
1924                    || *reloc_type == BFD_RELOC_HI16_S
1925                    || *reloc_type == BFD_RELOC_LO16
1926                    || *reloc_type == BFD_RELOC_GPREL16
1927                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
1928                    || *reloc_type == BFD_RELOC_GPREL32
1929                    || *reloc_type == BFD_RELOC_64
1930                    || *reloc_type == BFD_RELOC_CTOR
1931                    || *reloc_type == BFD_RELOC_MIPS_SUB
1932                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1933                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
1934                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1935                    || *reloc_type == BFD_RELOC_MIPS_REL16
1936                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1937                 fixp[0]->fx_no_overflow = 1;
1938
1939               if (unmatched_hi)
1940                 {
1941                   struct mips_hi_fixup *hi_fixup;
1942
1943                   assert (*reloc_type == BFD_RELOC_HI16_S);
1944                   hi_fixup = ((struct mips_hi_fixup *)
1945                               xmalloc (sizeof (struct mips_hi_fixup)));
1946                   hi_fixup->fixp = fixp[0];
1947                   hi_fixup->seg = now_seg;
1948                   hi_fixup->next = mips_hi_fixup_list;
1949                   mips_hi_fixup_list = hi_fixup;
1950                 }
1951
1952               if (reloc_type[1] != BFD_RELOC_UNUSED)
1953                 {
1954                   /* FIXME: This symbol can be one of
1955                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
1956                   address_expr->X_op = O_absent;
1957                   address_expr->X_add_symbol = 0;
1958                   address_expr->X_add_number = 0;
1959
1960                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1961                                          4, address_expr, false,
1962                                          reloc_type[1]);
1963
1964                   /* These relocations can have an addend that won't fit in
1965                      4 octets for 64bit assembly.  */
1966                   if (HAVE_64BIT_GPRS &&
1967                       (*reloc_type == BFD_RELOC_16
1968                        || *reloc_type == BFD_RELOC_32
1969                        || *reloc_type == BFD_RELOC_MIPS_JMP
1970                        || *reloc_type == BFD_RELOC_HI16_S
1971                        || *reloc_type == BFD_RELOC_LO16
1972                        || *reloc_type == BFD_RELOC_GPREL16
1973                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
1974                        || *reloc_type == BFD_RELOC_GPREL32
1975                        || *reloc_type == BFD_RELOC_64
1976                        || *reloc_type == BFD_RELOC_CTOR
1977                        || *reloc_type == BFD_RELOC_MIPS_SUB
1978                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1979                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
1980                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1981                        || *reloc_type == BFD_RELOC_MIPS_REL16
1982                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1983                     fixp[1]->fx_no_overflow = 1;
1984
1985                   if (reloc_type[2] != BFD_RELOC_UNUSED)
1986                     {
1987                       address_expr->X_op = O_absent;
1988                       address_expr->X_add_symbol = 0;
1989                       address_expr->X_add_number = 0;
1990
1991                       fixp[2] = fix_new_exp (frag_now,
1992                                              f - frag_now->fr_literal, 4,
1993                                              address_expr, false,
1994                                              reloc_type[2]);
1995
1996                       /* These relocations can have an addend that won't fit in
1997                          4 octets for 64bit assembly.  */
1998                       if (HAVE_64BIT_GPRS &&
1999                           (*reloc_type == BFD_RELOC_16
2000                            || *reloc_type == BFD_RELOC_32
2001                            || *reloc_type == BFD_RELOC_MIPS_JMP
2002                            || *reloc_type == BFD_RELOC_HI16_S
2003                            || *reloc_type == BFD_RELOC_LO16
2004                            || *reloc_type == BFD_RELOC_GPREL16
2005                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2006                            || *reloc_type == BFD_RELOC_GPREL32
2007                            || *reloc_type == BFD_RELOC_64
2008                            || *reloc_type == BFD_RELOC_CTOR
2009                            || *reloc_type == BFD_RELOC_MIPS_SUB
2010                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2011                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2012                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2013                            || *reloc_type == BFD_RELOC_MIPS_REL16
2014                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2015                         fixp[2]->fx_no_overflow = 1;
2016                     }
2017                 }
2018             }
2019         }
2020     }
2021
2022   if (! mips_opts.mips16)
2023     {
2024       md_number_to_chars (f, ip->insn_opcode, 4);
2025 #ifdef OBJ_ELF
2026       dwarf2_emit_insn (4);
2027 #endif
2028     }
2029   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2030     {
2031       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2032       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2033 #ifdef OBJ_ELF
2034       dwarf2_emit_insn (4);
2035 #endif
2036     }
2037   else
2038     {
2039       if (ip->use_extend)
2040         {
2041           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2042           f += 2;
2043         }
2044       md_number_to_chars (f, ip->insn_opcode, 2);
2045 #ifdef OBJ_ELF
2046       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2047 #endif
2048     }
2049
2050   /* Update the register mask information.  */
2051   if (! mips_opts.mips16)
2052     {
2053       if (pinfo & INSN_WRITE_GPR_D)
2054         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2055       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2056         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2057       if (pinfo & INSN_READ_GPR_S)
2058         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2059       if (pinfo & INSN_WRITE_GPR_31)
2060         mips_gprmask |= 1 << RA;
2061       if (pinfo & INSN_WRITE_FPR_D)
2062         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2063       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2064         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2065       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2066         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2067       if ((pinfo & INSN_READ_FPR_R) != 0)
2068         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2069       if (pinfo & INSN_COP)
2070         {
2071           /* We don't keep enough information to sort these cases out.
2072              The itbl support does keep this information however, although
2073              we currently don't support itbl fprmats as part of the cop
2074              instruction.  May want to add this support in the future.  */
2075         }
2076       /* Never set the bit for $0, which is always zero.  */
2077       mips_gprmask &= ~1 << 0;
2078     }
2079   else
2080     {
2081       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2082         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2083                               & MIPS16OP_MASK_RX);
2084       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2085         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2086                               & MIPS16OP_MASK_RY);
2087       if (pinfo & MIPS16_INSN_WRITE_Z)
2088         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2089                               & MIPS16OP_MASK_RZ);
2090       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2091         mips_gprmask |= 1 << TREG;
2092       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2093         mips_gprmask |= 1 << SP;
2094       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2095         mips_gprmask |= 1 << RA;
2096       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2097         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2098       if (pinfo & MIPS16_INSN_READ_Z)
2099         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2100                               & MIPS16OP_MASK_MOVE32Z);
2101       if (pinfo & MIPS16_INSN_READ_GPR_X)
2102         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2103                               & MIPS16OP_MASK_REGR32);
2104     }
2105
2106   if (place == NULL && ! mips_opts.noreorder)
2107     {
2108       /* Filling the branch delay slot is more complex.  We try to
2109          switch the branch with the previous instruction, which we can
2110          do if the previous instruction does not set up a condition
2111          that the branch tests and if the branch is not itself the
2112          target of any branch.  */
2113       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2114           || (pinfo & INSN_COND_BRANCH_DELAY))
2115         {
2116           if (mips_optimize < 2
2117               /* If we have seen .set volatile or .set nomove, don't
2118                  optimize.  */
2119               || mips_opts.nomove != 0
2120               /* If we had to emit any NOP instructions, then we
2121                  already know we can not swap.  */
2122               || nops != 0
2123               /* If we don't even know the previous insn, we can not
2124                  swap.  */
2125               || ! prev_insn_valid
2126               /* If the previous insn is already in a branch delay
2127                  slot, then we can not swap.  */
2128               || prev_insn_is_delay_slot
2129               /* If the previous previous insn was in a .set
2130                  noreorder, we can't swap.  Actually, the MIPS
2131                  assembler will swap in this situation.  However, gcc
2132                  configured -with-gnu-as will generate code like
2133                    .set noreorder
2134                    lw   $4,XXX
2135                    .set reorder
2136                    INSN
2137                    bne  $4,$0,foo
2138                  in which we can not swap the bne and INSN.  If gcc is
2139                  not configured -with-gnu-as, it does not output the
2140                  .set pseudo-ops.  We don't have to check
2141                  prev_insn_unreordered, because prev_insn_valid will
2142                  be 0 in that case.  We don't want to use
2143                  prev_prev_insn_valid, because we do want to be able
2144                  to swap at the start of a function.  */
2145               || prev_prev_insn_unreordered
2146               /* If the branch is itself the target of a branch, we
2147                  can not swap.  We cheat on this; all we check for is
2148                  whether there is a label on this instruction.  If
2149                  there are any branches to anything other than a
2150                  label, users must use .set noreorder.  */
2151               || insn_labels != NULL
2152               /* If the previous instruction is in a variant frag, we
2153                  can not do the swap.  This does not apply to the
2154                  mips16, which uses variant frags for different
2155                  purposes.  */
2156               || (! mips_opts.mips16
2157                   && prev_insn_frag->fr_type == rs_machine_dependent)
2158               /* If the branch reads the condition codes, we don't
2159                  even try to swap, because in the sequence
2160                    ctc1 $X,$31
2161                    INSN
2162                    INSN
2163                    bc1t LABEL
2164                  we can not swap, and I don't feel like handling that
2165                  case.  */
2166               || (! mips_opts.mips16
2167                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2168                   && (pinfo & INSN_READ_COND_CODE))
2169               /* We can not swap with an instruction that requires a
2170                  delay slot, becase the target of the branch might
2171                  interfere with that instruction.  */
2172               || (! mips_opts.mips16
2173                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2174                   && (prev_pinfo
2175               /* Itbl support may require additional care here.  */
2176                       & (INSN_LOAD_COPROC_DELAY
2177                          | INSN_COPROC_MOVE_DELAY
2178                          | INSN_WRITE_COND_CODE)))
2179               || (! (hilo_interlocks
2180                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2181                   && (prev_pinfo
2182                       & (INSN_READ_LO
2183                          | INSN_READ_HI)))
2184               || (! mips_opts.mips16
2185                   && ! gpr_interlocks
2186                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2187               || (! mips_opts.mips16
2188                   && mips_opts.isa == ISA_MIPS1
2189                   /* Itbl support may require additional care here.  */
2190                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2191               /* We can not swap with a branch instruction.  */
2192               || (prev_pinfo
2193                   & (INSN_UNCOND_BRANCH_DELAY
2194                      | INSN_COND_BRANCH_DELAY
2195                      | INSN_COND_BRANCH_LIKELY))
2196               /* We do not swap with a trap instruction, since it
2197                  complicates trap handlers to have the trap
2198                  instruction be in a delay slot.  */
2199               || (prev_pinfo & INSN_TRAP)
2200               /* If the branch reads a register that the previous
2201                  instruction sets, we can not swap.  */
2202               || (! mips_opts.mips16
2203                   && (prev_pinfo & INSN_WRITE_GPR_T)
2204                   && insn_uses_reg (ip,
2205                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2206                                      & OP_MASK_RT),
2207                                     MIPS_GR_REG))
2208               || (! mips_opts.mips16
2209                   && (prev_pinfo & INSN_WRITE_GPR_D)
2210                   && insn_uses_reg (ip,
2211                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2212                                      & OP_MASK_RD),
2213                                     MIPS_GR_REG))
2214               || (mips_opts.mips16
2215                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2216                        && insn_uses_reg (ip,
2217                                          ((prev_insn.insn_opcode
2218                                            >> MIPS16OP_SH_RX)
2219                                           & MIPS16OP_MASK_RX),
2220                                          MIPS16_REG))
2221                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2222                           && insn_uses_reg (ip,
2223                                             ((prev_insn.insn_opcode
2224                                               >> MIPS16OP_SH_RY)
2225                                              & MIPS16OP_MASK_RY),
2226                                             MIPS16_REG))
2227                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2228                           && insn_uses_reg (ip,
2229                                             ((prev_insn.insn_opcode
2230                                               >> MIPS16OP_SH_RZ)
2231                                              & MIPS16OP_MASK_RZ),
2232                                             MIPS16_REG))
2233                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2234                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2235                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2236                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2237                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2238                           && insn_uses_reg (ip,
2239                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2240                                                                      insn_opcode),
2241                                             MIPS_GR_REG))))
2242               /* If the branch writes a register that the previous
2243                  instruction sets, we can not swap (we know that
2244                  branches write only to RD or to $31).  */
2245               || (! mips_opts.mips16
2246                   && (prev_pinfo & INSN_WRITE_GPR_T)
2247                   && (((pinfo & INSN_WRITE_GPR_D)
2248                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2249                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2250                       || ((pinfo & INSN_WRITE_GPR_31)
2251                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2252                                & OP_MASK_RT)
2253                               == RA))))
2254               || (! mips_opts.mips16
2255                   && (prev_pinfo & INSN_WRITE_GPR_D)
2256                   && (((pinfo & INSN_WRITE_GPR_D)
2257                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2258                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2259                       || ((pinfo & INSN_WRITE_GPR_31)
2260                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2261                                & OP_MASK_RD)
2262                               == RA))))
2263               || (mips_opts.mips16
2264                   && (pinfo & MIPS16_INSN_WRITE_31)
2265                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2266                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2267                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2268                               == RA))))
2269               /* If the branch writes a register that the previous
2270                  instruction reads, we can not swap (we know that
2271                  branches only write to RD or to $31).  */
2272               || (! mips_opts.mips16
2273                   && (pinfo & INSN_WRITE_GPR_D)
2274                   && insn_uses_reg (&prev_insn,
2275                                     ((ip->insn_opcode >> OP_SH_RD)
2276                                      & OP_MASK_RD),
2277                                     MIPS_GR_REG))
2278               || (! mips_opts.mips16
2279                   && (pinfo & INSN_WRITE_GPR_31)
2280                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2281               || (mips_opts.mips16
2282                   && (pinfo & MIPS16_INSN_WRITE_31)
2283                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2284               /* If we are generating embedded PIC code, the branch
2285                  might be expanded into a sequence which uses $at, so
2286                  we can't swap with an instruction which reads it.  */
2287               || (mips_pic == EMBEDDED_PIC
2288                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2289               /* If the previous previous instruction has a load
2290                  delay, and sets a register that the branch reads, we
2291                  can not swap.  */
2292               || (! mips_opts.mips16
2293                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2294               /* Itbl support may require additional care here.  */
2295                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2296                       || (! gpr_interlocks
2297                           && (prev_prev_insn.insn_mo->pinfo
2298                               & INSN_LOAD_MEMORY_DELAY)))
2299                   && insn_uses_reg (ip,
2300                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2301                                      & OP_MASK_RT),
2302                                     MIPS_GR_REG))
2303               /* If one instruction sets a condition code and the
2304                  other one uses a condition code, we can not swap.  */
2305               || ((pinfo & INSN_READ_COND_CODE)
2306                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2307               || ((pinfo & INSN_WRITE_COND_CODE)
2308                   && (prev_pinfo & INSN_READ_COND_CODE))
2309               /* If the previous instruction uses the PC, we can not
2310                  swap.  */
2311               || (mips_opts.mips16
2312                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2313               /* If the previous instruction was extended, we can not
2314                  swap.  */
2315               || (mips_opts.mips16 && prev_insn_extended)
2316               /* If the previous instruction had a fixup in mips16
2317                  mode, we can not swap.  This normally means that the
2318                  previous instruction was a 4 byte branch anyhow.  */
2319               || (mips_opts.mips16 && prev_insn_fixp[0])
2320               /* If the previous instruction is a sync, sync.l, or
2321                  sync.p, we can not swap.  */
2322               || (prev_pinfo & INSN_SYNC))
2323             {
2324               /* We could do even better for unconditional branches to
2325                  portions of this object file; we could pick up the
2326                  instruction at the destination, put it in the delay
2327                  slot, and bump the destination address.  */
2328               emit_nop ();
2329               /* Update the previous insn information.  */
2330               prev_prev_insn = *ip;
2331               prev_insn.insn_mo = &dummy_opcode;
2332             }
2333           else
2334             {
2335               /* It looks like we can actually do the swap.  */
2336               if (! mips_opts.mips16)
2337                 {
2338                   char *prev_f;
2339                   char temp[4];
2340
2341                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2342                   memcpy (temp, prev_f, 4);
2343                   memcpy (prev_f, f, 4);
2344                   memcpy (f, temp, 4);
2345                   if (prev_insn_fixp[0])
2346                     {
2347                       prev_insn_fixp[0]->fx_frag = frag_now;
2348                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2349                     }
2350                   if (prev_insn_fixp[1])
2351                     {
2352                       prev_insn_fixp[1]->fx_frag = frag_now;
2353                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2354                     }
2355                   if (prev_insn_fixp[2])
2356                     {
2357                       prev_insn_fixp[2]->fx_frag = frag_now;
2358                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2359                     }
2360                   if (fixp[0])
2361                     {
2362                       fixp[0]->fx_frag = prev_insn_frag;
2363                       fixp[0]->fx_where = prev_insn_where;
2364                     }
2365                   if (fixp[1])
2366                     {
2367                       fixp[1]->fx_frag = prev_insn_frag;
2368                       fixp[1]->fx_where = prev_insn_where;
2369                     }
2370                   if (fixp[2])
2371                     {
2372                       fixp[2]->fx_frag = prev_insn_frag;
2373                       fixp[2]->fx_where = prev_insn_where;
2374                     }
2375                 }
2376               else
2377                 {
2378                   char *prev_f;
2379                   char temp[2];
2380
2381                   assert (prev_insn_fixp[0] == NULL);
2382                   assert (prev_insn_fixp[1] == NULL);
2383                   assert (prev_insn_fixp[2] == NULL);
2384                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2385                   memcpy (temp, prev_f, 2);
2386                   memcpy (prev_f, f, 2);
2387                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2388                     {
2389                       assert (*reloc_type == BFD_RELOC_UNUSED);
2390                       memcpy (f, temp, 2);
2391                     }
2392                   else
2393                     {
2394                       memcpy (f, f + 2, 2);
2395                       memcpy (f + 2, temp, 2);
2396                     }
2397                   if (fixp[0])
2398                     {
2399                       fixp[0]->fx_frag = prev_insn_frag;
2400                       fixp[0]->fx_where = prev_insn_where;
2401                     }
2402                   if (fixp[1])
2403                     {
2404                       fixp[1]->fx_frag = prev_insn_frag;
2405                       fixp[1]->fx_where = prev_insn_where;
2406                     }
2407                   if (fixp[2])
2408                     {
2409                       fixp[2]->fx_frag = prev_insn_frag;
2410                       fixp[2]->fx_where = prev_insn_where;
2411                     }
2412                 }
2413
2414               /* Update the previous insn information; leave prev_insn
2415                  unchanged.  */
2416               prev_prev_insn = *ip;
2417             }
2418           prev_insn_is_delay_slot = 1;
2419
2420           /* If that was an unconditional branch, forget the previous
2421              insn information.  */
2422           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2423             {
2424               prev_prev_insn.insn_mo = &dummy_opcode;
2425               prev_insn.insn_mo = &dummy_opcode;
2426             }
2427
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 if (pinfo & INSN_COND_BRANCH_LIKELY)
2437         {
2438           /* We don't yet optimize a branch likely.  What we should do
2439              is look at the target, copy the instruction found there
2440              into the delay slot, and increment the branch to jump to
2441              the next instruction.  */
2442           emit_nop ();
2443           /* Update the previous insn information.  */
2444           prev_prev_insn = *ip;
2445           prev_insn.insn_mo = &dummy_opcode;
2446           prev_insn_fixp[0] = NULL;
2447           prev_insn_fixp[1] = NULL;
2448           prev_insn_fixp[2] = NULL;
2449           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2450           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2451           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2452           prev_insn_extended = 0;
2453         }
2454       else
2455         {
2456           /* Update the previous insn information.  */
2457           if (nops > 0)
2458             prev_prev_insn.insn_mo = &dummy_opcode;
2459           else
2460             prev_prev_insn = prev_insn;
2461           prev_insn = *ip;
2462
2463           /* Any time we see a branch, we always fill the delay slot
2464              immediately; since this insn is not a branch, we know it
2465              is not in a delay slot.  */
2466           prev_insn_is_delay_slot = 0;
2467
2468           prev_insn_fixp[0] = fixp[0];
2469           prev_insn_fixp[1] = fixp[1];
2470           prev_insn_fixp[2] = fixp[2];
2471           prev_insn_reloc_type[0] = reloc_type[0];
2472           prev_insn_reloc_type[1] = reloc_type[1];
2473           prev_insn_reloc_type[2] = reloc_type[2];
2474           if (mips_opts.mips16)
2475             prev_insn_extended = (ip->use_extend
2476                                   || *reloc_type > BFD_RELOC_UNUSED);
2477         }
2478
2479       prev_prev_insn_unreordered = prev_insn_unreordered;
2480       prev_insn_unreordered = 0;
2481       prev_insn_frag = frag_now;
2482       prev_insn_where = f - frag_now->fr_literal;
2483       prev_insn_valid = 1;
2484     }
2485   else if (place == NULL)
2486     {
2487       /* We need to record a bit of information even when we are not
2488          reordering, in order to determine the base address for mips16
2489          PC relative relocs.  */
2490       prev_prev_insn = prev_insn;
2491       prev_insn = *ip;
2492       prev_insn_reloc_type[0] = reloc_type[0];
2493       prev_insn_reloc_type[1] = reloc_type[1];
2494       prev_insn_reloc_type[2] = reloc_type[2];
2495       prev_prev_insn_unreordered = prev_insn_unreordered;
2496       prev_insn_unreordered = 1;
2497     }
2498
2499   /* We just output an insn, so the next one doesn't have a label.  */
2500   mips_clear_insn_labels ();
2501
2502   /* We must ensure that a fixup associated with an unmatched %hi
2503      reloc does not become a variant frag.  Otherwise, the
2504      rearrangement of %hi relocs in frob_file may confuse
2505      tc_gen_reloc.  */
2506   if (unmatched_hi)
2507     {
2508       frag_wane (frag_now);
2509       frag_new (0);
2510     }
2511 }
2512
2513 /* This function forgets that there was any previous instruction or
2514    label.  If PRESERVE is non-zero, it remembers enough information to
2515    know whether nops are needed before a noreorder section.  */
2516
2517 static void
2518 mips_no_prev_insn (preserve)
2519      int preserve;
2520 {
2521   if (! preserve)
2522     {
2523       prev_insn.insn_mo = &dummy_opcode;
2524       prev_prev_insn.insn_mo = &dummy_opcode;
2525       prev_nop_frag = NULL;
2526       prev_nop_frag_holds = 0;
2527       prev_nop_frag_required = 0;
2528       prev_nop_frag_since = 0;
2529     }
2530   prev_insn_valid = 0;
2531   prev_insn_is_delay_slot = 0;
2532   prev_insn_unreordered = 0;
2533   prev_insn_extended = 0;
2534   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2535   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2536   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2537   prev_prev_insn_unreordered = 0;
2538   mips_clear_insn_labels ();
2539 }
2540
2541 /* This function must be called whenever we turn on noreorder or emit
2542    something other than instructions.  It inserts any NOPS which might
2543    be needed by the previous instruction, and clears the information
2544    kept for the previous instructions.  The INSNS parameter is true if
2545    instructions are to follow.  */
2546
2547 static void
2548 mips_emit_delays (insns)
2549      boolean insns;
2550 {
2551   if (! mips_opts.noreorder)
2552     {
2553       int nops;
2554
2555       nops = 0;
2556       if ((! mips_opts.mips16
2557            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2558            && (! cop_interlocks
2559                && (prev_insn.insn_mo->pinfo
2560                    & (INSN_LOAD_COPROC_DELAY
2561                       | INSN_COPROC_MOVE_DELAY
2562                       | INSN_WRITE_COND_CODE))))
2563           || (! hilo_interlocks
2564               && (prev_insn.insn_mo->pinfo
2565                   & (INSN_READ_LO
2566                      | INSN_READ_HI)))
2567           || (! mips_opts.mips16
2568               && ! gpr_interlocks
2569               && (prev_insn.insn_mo->pinfo
2570                   & INSN_LOAD_MEMORY_DELAY))
2571           || (! mips_opts.mips16
2572               && mips_opts.isa == ISA_MIPS1
2573               && (prev_insn.insn_mo->pinfo
2574                   & INSN_COPROC_MEMORY_DELAY)))
2575         {
2576           /* Itbl support may require additional care here.  */
2577           ++nops;
2578           if ((! mips_opts.mips16
2579                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2580                && (! cop_interlocks
2581                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2582               || (! hilo_interlocks
2583                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2584                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2585             ++nops;
2586
2587           if (prev_insn_unreordered)
2588             nops = 0;
2589         }
2590       else if ((! mips_opts.mips16
2591                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2592                 && (! cop_interlocks
2593                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2594                || (! hilo_interlocks
2595                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2596                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2597         {
2598           /* Itbl support may require additional care here.  */
2599           if (! prev_prev_insn_unreordered)
2600             ++nops;
2601         }
2602
2603       if (nops > 0)
2604         {
2605           struct insn_label_list *l;
2606
2607           if (insns)
2608             {
2609               /* Record the frag which holds the nop instructions, so
2610                  that we can remove them if we don't need them.  */
2611               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2612               prev_nop_frag = frag_now;
2613               prev_nop_frag_holds = nops;
2614               prev_nop_frag_required = 0;
2615               prev_nop_frag_since = 0;
2616             }
2617
2618           for (; nops > 0; --nops)
2619             emit_nop ();
2620
2621           if (insns)
2622             {
2623               /* Move on to a new frag, so that it is safe to simply
2624                  decrease the size of prev_nop_frag.  */
2625               frag_wane (frag_now);
2626               frag_new (0);
2627             }
2628
2629           for (l = insn_labels; l != NULL; l = l->next)
2630             {
2631               valueT val;
2632
2633               assert (S_GET_SEGMENT (l->label) == now_seg);
2634               symbol_set_frag (l->label, frag_now);
2635               val = (valueT) frag_now_fix ();
2636               /* mips16 text labels are stored as odd.  */
2637               if (mips_opts.mips16)
2638                 ++val;
2639               S_SET_VALUE (l->label, val);
2640             }
2641         }
2642     }
2643
2644   /* Mark instruction labels in mips16 mode.  */
2645   if (insns)
2646     mips16_mark_labels ();
2647
2648   mips_no_prev_insn (insns);
2649 }
2650
2651 /* Build an instruction created by a macro expansion.  This is passed
2652    a pointer to the count of instructions created so far, an
2653    expression, the name of the instruction to build, an operand format
2654    string, and corresponding arguments.  */
2655
2656 #ifdef USE_STDARG
2657 static void
2658 macro_build (char *place,
2659              int *counter,
2660              expressionS * ep,
2661              const char *name,
2662              const char *fmt,
2663              ...)
2664 #else
2665 static void
2666 macro_build (place, counter, ep, name, fmt, va_alist)
2667      char *place;
2668      int *counter;
2669      expressionS *ep;
2670      const char *name;
2671      const char *fmt;
2672      va_dcl
2673 #endif
2674 {
2675   struct mips_cl_insn insn;
2676   bfd_reloc_code_real_type r[3];
2677   va_list args;
2678
2679 #ifdef USE_STDARG
2680   va_start (args, fmt);
2681 #else
2682   va_start (args);
2683 #endif
2684
2685   /*
2686    * If the macro is about to expand into a second instruction,
2687    * print a warning if needed. We need to pass ip as a parameter
2688    * to generate a better warning message here...
2689    */
2690   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2691     as_warn (_("Macro instruction expanded into multiple instructions"));
2692
2693   /*
2694    * If the macro is about to expand into a second instruction,
2695    * and it is in a delay slot, print a warning.
2696    */
2697   if (place == NULL
2698       && *counter == 1
2699       && mips_opts.noreorder
2700       && (prev_prev_insn.insn_mo->pinfo
2701           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2702              | INSN_COND_BRANCH_LIKELY)) != 0)
2703     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2704
2705   if (place == NULL)
2706     ++*counter;         /* bump instruction counter */
2707
2708   if (mips_opts.mips16)
2709     {
2710       mips16_macro_build (place, counter, ep, name, fmt, args);
2711       va_end (args);
2712       return;
2713     }
2714
2715   r[0] = BFD_RELOC_UNUSED;
2716   r[1] = BFD_RELOC_UNUSED;
2717   r[2] = BFD_RELOC_UNUSED;
2718   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2719   assert (insn.insn_mo);
2720   assert (strcmp (name, insn.insn_mo->name) == 0);
2721
2722   /* Search until we get a match for NAME.  */
2723   while (1)
2724     {
2725       /* It is assumed here that macros will never generate 
2726          MDMX or MIPS-3D instructions.  */
2727       if (strcmp (fmt, insn.insn_mo->args) == 0
2728           && insn.insn_mo->pinfo != INSN_MACRO
2729           && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2730           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2731         break;
2732
2733       ++insn.insn_mo;
2734       assert (insn.insn_mo->name);
2735       assert (strcmp (name, insn.insn_mo->name) == 0);
2736     }
2737
2738   insn.insn_opcode = insn.insn_mo->match;
2739   for (;;)
2740     {
2741       switch (*fmt++)
2742         {
2743         case '\0':
2744           break;
2745
2746         case ',':
2747         case '(':
2748         case ')':
2749           continue;
2750
2751         case 't':
2752         case 'w':
2753         case 'E':
2754           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2755           continue;
2756
2757         case 'c':
2758           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2759           continue;
2760
2761         case 'T':
2762         case 'W':
2763           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2764           continue;
2765
2766         case 'd':
2767         case 'G':
2768           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2769           continue;
2770
2771         case 'U':
2772           {
2773             int tmp = va_arg (args, int);
2774
2775             insn.insn_opcode |= tmp << OP_SH_RT;
2776             insn.insn_opcode |= tmp << OP_SH_RD;
2777             continue;
2778           }
2779
2780         case 'V':
2781         case 'S':
2782           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2783           continue;
2784
2785         case 'z':
2786           continue;
2787
2788         case '<':
2789           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2790           continue;
2791
2792         case 'D':
2793           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2794           continue;
2795
2796         case 'B':
2797           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2798           continue;
2799
2800         case 'J':
2801           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2802           continue;
2803
2804         case 'q':
2805           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2806           continue;
2807
2808         case 'b':
2809         case 's':
2810         case 'r':
2811         case 'v':
2812           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2813           continue;
2814
2815         case 'i':
2816         case 'j':
2817         case 'o':
2818           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2819           assert (*r == BFD_RELOC_GPREL16
2820                   || *r == BFD_RELOC_MIPS_LITERAL
2821                   || *r == BFD_RELOC_MIPS_HIGHER
2822                   || *r == BFD_RELOC_HI16_S
2823                   || *r == BFD_RELOC_LO16
2824                   || *r == BFD_RELOC_MIPS_GOT16
2825                   || *r == BFD_RELOC_MIPS_CALL16
2826                   || *r == BFD_RELOC_MIPS_GOT_DISP
2827                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2828                   || *r == BFD_RELOC_MIPS_GOT_OFST
2829                   || *r == BFD_RELOC_MIPS_GOT_LO16
2830                   || *r == BFD_RELOC_MIPS_CALL_LO16
2831                   || (ep->X_op == O_subtract
2832                       && *r == BFD_RELOC_PCREL_LO16));
2833           continue;
2834
2835         case 'u':
2836           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2837           assert (ep != NULL
2838                   && (ep->X_op == O_constant
2839                       || (ep->X_op == O_symbol
2840                           && (*r == BFD_RELOC_MIPS_HIGHEST
2841                               || *r == BFD_RELOC_HI16_S
2842                               || *r == BFD_RELOC_HI16
2843                               || *r == BFD_RELOC_GPREL16
2844                               || *r == BFD_RELOC_MIPS_GOT_HI16
2845                               || *r == BFD_RELOC_MIPS_CALL_HI16))
2846                       || (ep->X_op == O_subtract
2847                           && *r == BFD_RELOC_PCREL_HI16_S)));
2848           continue;
2849
2850         case 'p':
2851           assert (ep != NULL);
2852           /*
2853            * This allows macro() to pass an immediate expression for
2854            * creating short branches without creating a symbol.
2855            * Note that the expression still might come from the assembly
2856            * input, in which case the value is not checked for range nor
2857            * is a relocation entry generated (yuck).
2858            */
2859           if (ep->X_op == O_constant)
2860             {
2861               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2862               ep = NULL;
2863             }
2864           else
2865             if (mips_pic == EMBEDDED_PIC)
2866               *r = BFD_RELOC_16_PCREL_S2;
2867             else
2868               *r = BFD_RELOC_16_PCREL;
2869           continue;
2870
2871         case 'a':
2872           assert (ep != NULL);
2873           *r = BFD_RELOC_MIPS_JMP;
2874           continue;
2875
2876         case 'C':
2877           insn.insn_opcode |= va_arg (args, unsigned long);
2878           continue;
2879
2880         default:
2881           internalError ();
2882         }
2883       break;
2884     }
2885   va_end (args);
2886   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2887
2888   append_insn (place, &insn, ep, r, false);
2889 }
2890
2891 static void
2892 mips16_macro_build (place, counter, ep, name, fmt, args)
2893      char *place;
2894      int *counter ATTRIBUTE_UNUSED;
2895      expressionS *ep;
2896      const char *name;
2897      const char *fmt;
2898      va_list args;
2899 {
2900   struct mips_cl_insn insn;
2901   bfd_reloc_code_real_type r[3]
2902     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2903
2904   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2905   assert (insn.insn_mo);
2906   assert (strcmp (name, insn.insn_mo->name) == 0);
2907
2908   while (strcmp (fmt, insn.insn_mo->args) != 0
2909          || insn.insn_mo->pinfo == INSN_MACRO)
2910     {
2911       ++insn.insn_mo;
2912       assert (insn.insn_mo->name);
2913       assert (strcmp (name, insn.insn_mo->name) == 0);
2914     }
2915
2916   insn.insn_opcode = insn.insn_mo->match;
2917   insn.use_extend = false;
2918
2919   for (;;)
2920     {
2921       int c;
2922
2923       c = *fmt++;
2924       switch (c)
2925         {
2926         case '\0':
2927           break;
2928
2929         case ',':
2930         case '(':
2931         case ')':
2932           continue;
2933
2934         case 'y':
2935         case 'w':
2936           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2937           continue;
2938
2939         case 'x':
2940         case 'v':
2941           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2942           continue;
2943
2944         case 'z':
2945           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2946           continue;
2947
2948         case 'Z':
2949           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2950           continue;
2951
2952         case '0':
2953         case 'S':
2954         case 'P':
2955         case 'R':
2956           continue;
2957
2958         case 'X':
2959           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2960           continue;
2961
2962         case 'Y':
2963           {
2964             int regno;
2965
2966             regno = va_arg (args, int);
2967             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2968             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2969           }
2970           continue;
2971
2972         case '<':
2973         case '>':
2974         case '4':
2975         case '5':
2976         case 'H':
2977         case 'W':
2978         case 'D':
2979         case 'j':
2980         case '8':
2981         case 'V':
2982         case 'C':
2983         case 'U':
2984         case 'k':
2985         case 'K':
2986         case 'p':
2987         case 'q':
2988           {
2989             assert (ep != NULL);
2990
2991             if (ep->X_op != O_constant)
2992               *r = (int) BFD_RELOC_UNUSED + c;
2993             else
2994               {
2995                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2996                               false, &insn.insn_opcode, &insn.use_extend,
2997                               &insn.extend);
2998                 ep = NULL;
2999                 *r = BFD_RELOC_UNUSED;
3000               }
3001           }
3002           continue;
3003
3004         case '6':
3005           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3006           continue;
3007         }
3008
3009       break;
3010     }
3011
3012   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3013
3014   append_insn (place, &insn, ep, r, false);
3015 }
3016
3017 /*
3018  * Generate a "jalr" instruction with a relocation hint to the called
3019  * function.  This occurs in NewABI PIC code.
3020  */
3021 static void
3022 macro_build_jalr (icnt, ep)
3023      int icnt;
3024      expressionS *ep;
3025 {
3026   if (HAVE_NEWABI)
3027     frag_more (0);
3028   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3029                RA, PIC_CALL_REG);
3030   if (HAVE_NEWABI)
3031     fix_new_exp (frag_now, 0, 0, ep, false, BFD_RELOC_MIPS_JALR);
3032 }
3033
3034 /*
3035  * Generate a "lui" instruction.
3036  */
3037 static void
3038 macro_build_lui (place, counter, ep, regnum)
3039      char *place;
3040      int *counter;
3041      expressionS *ep;
3042      int regnum;
3043 {
3044   expressionS high_expr;
3045   struct mips_cl_insn insn;
3046   bfd_reloc_code_real_type r[3]
3047     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3048   const char *name = "lui";
3049   const char *fmt = "t,u";
3050
3051   assert (! mips_opts.mips16);
3052
3053   if (place == NULL)
3054     high_expr = *ep;
3055   else
3056     {
3057       high_expr.X_op = O_constant;
3058       high_expr.X_add_number = ep->X_add_number;
3059     }
3060
3061   if (high_expr.X_op == O_constant)
3062     {
3063       /* we can compute the instruction now without a relocation entry */
3064       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3065                                 >> 16) & 0xffff;
3066       *r = BFD_RELOC_UNUSED;
3067     }
3068   else if (! HAVE_NEWABI)
3069     {
3070       assert (ep->X_op == O_symbol);
3071       /* _gp_disp is a special case, used from s_cpload.  */
3072       assert (mips_pic == NO_PIC
3073               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3074       *r = BFD_RELOC_HI16_S;
3075     }
3076
3077   /*
3078    * If the macro is about to expand into a second instruction,
3079    * print a warning if needed. We need to pass ip as a parameter
3080    * to generate a better warning message here...
3081    */
3082   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3083     as_warn (_("Macro instruction expanded into multiple instructions"));
3084
3085   if (place == NULL)
3086     ++*counter;         /* bump instruction counter */
3087
3088   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3089   assert (insn.insn_mo);
3090   assert (strcmp (name, insn.insn_mo->name) == 0);
3091   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3092
3093   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3094   if (*r == BFD_RELOC_UNUSED)
3095     {
3096       insn.insn_opcode |= high_expr.X_add_number;
3097       append_insn (place, &insn, NULL, r, false);
3098     }
3099   else
3100     append_insn (place, &insn, &high_expr, r, false);
3101 }
3102
3103 /*                      set_at()
3104  * Generates code to set the $at register to true (one)
3105  * if reg is less than the immediate expression.
3106  */
3107 static void
3108 set_at (counter, reg, unsignedp)
3109      int *counter;
3110      int reg;
3111      int unsignedp;
3112 {
3113   if (imm_expr.X_op == O_constant
3114       && imm_expr.X_add_number >= -0x8000
3115       && imm_expr.X_add_number < 0x8000)
3116     macro_build ((char *) NULL, counter, &imm_expr,
3117                  unsignedp ? "sltiu" : "slti",
3118                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3119   else
3120     {
3121       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3122       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3123                    unsignedp ? "sltu" : "slt",
3124                    "d,v,t", AT, reg, AT);
3125     }
3126 }
3127
3128 /* Warn if an expression is not a constant.  */
3129
3130 static void
3131 check_absolute_expr (ip, ex)
3132      struct mips_cl_insn *ip;
3133      expressionS *ex;
3134 {
3135   if (ex->X_op == O_big)
3136     as_bad (_("unsupported large constant"));
3137   else if (ex->X_op != O_constant)
3138     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3139 }
3140
3141 /* Count the leading zeroes by performing a binary chop. This is a
3142    bulky bit of source, but performance is a LOT better for the
3143    majority of values than a simple loop to count the bits:
3144        for (lcnt = 0; (lcnt < 32); lcnt++)
3145          if ((v) & (1 << (31 - lcnt)))
3146            break;
3147   However it is not code size friendly, and the gain will drop a bit
3148   on certain cached systems.
3149 */
3150 #define COUNT_TOP_ZEROES(v)             \
3151   (((v) & ~0xffff) == 0                 \
3152    ? ((v) & ~0xff) == 0                 \
3153      ? ((v) & ~0xf) == 0                \
3154        ? ((v) & ~0x3) == 0              \
3155          ? ((v) & ~0x1) == 0            \
3156            ? !(v)                       \
3157              ? 32                       \
3158              : 31                       \
3159            : 30                         \
3160          : ((v) & ~0x7) == 0            \
3161            ? 29                         \
3162            : 28                         \
3163        : ((v) & ~0x3f) == 0             \
3164          ? ((v) & ~0x1f) == 0           \
3165            ? 27                         \
3166            : 26                         \
3167          : ((v) & ~0x7f) == 0           \
3168            ? 25                         \
3169            : 24                         \
3170      : ((v) & ~0xfff) == 0              \
3171        ? ((v) & ~0x3ff) == 0            \
3172          ? ((v) & ~0x1ff) == 0          \
3173            ? 23                         \
3174            : 22                         \
3175          : ((v) & ~0x7ff) == 0          \
3176            ? 21                         \
3177            : 20                         \
3178        : ((v) & ~0x3fff) == 0           \
3179          ? ((v) & ~0x1fff) == 0         \
3180            ? 19                         \
3181            : 18                         \
3182          : ((v) & ~0x7fff) == 0         \
3183            ? 17                         \
3184            : 16                         \
3185    : ((v) & ~0xffffff) == 0             \
3186      ? ((v) & ~0xfffff) == 0            \
3187        ? ((v) & ~0x3ffff) == 0          \
3188          ? ((v) & ~0x1ffff) == 0        \
3189            ? 15                         \
3190            : 14                         \
3191          : ((v) & ~0x7ffff) == 0        \
3192            ? 13                         \
3193            : 12                         \
3194        : ((v) & ~0x3fffff) == 0         \
3195          ? ((v) & ~0x1fffff) == 0       \
3196            ? 11                         \
3197            : 10                         \
3198          : ((v) & ~0x7fffff) == 0       \
3199            ? 9                          \
3200            : 8                          \
3201      : ((v) & ~0xfffffff) == 0          \
3202        ? ((v) & ~0x3ffffff) == 0        \
3203          ? ((v) & ~0x1ffffff) == 0      \
3204            ? 7                          \
3205            : 6                          \
3206          : ((v) & ~0x7ffffff) == 0      \
3207            ? 5                          \
3208            : 4                          \
3209        : ((v) & ~0x3fffffff) == 0       \
3210          ? ((v) & ~0x1fffffff) == 0     \
3211            ? 3                          \
3212            : 2                          \
3213          : ((v) & ~0x7fffffff) == 0     \
3214            ? 1                          \
3215            : 0)
3216
3217 /* Is the given value a sign-extended 32-bit value?  */
3218 #define IS_SEXT_32BIT_NUM(x)                                            \
3219   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
3220    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3221
3222 /*                      load_register()
3223  *  This routine generates the least number of instructions neccessary to load
3224  *  an absolute expression value into a register.
3225  */
3226 static void
3227 load_register (counter, reg, ep, dbl)
3228      int *counter;
3229      int reg;
3230      expressionS *ep;
3231      int dbl;
3232 {
3233   int freg;
3234   expressionS hi32, lo32;
3235
3236   if (ep->X_op != O_big)
3237     {
3238       assert (ep->X_op == O_constant);
3239       if (ep->X_add_number < 0x8000
3240           && (ep->X_add_number >= 0
3241               || (ep->X_add_number >= -0x8000
3242                   && (! dbl
3243                       || ! ep->X_unsigned
3244                       || sizeof (ep->X_add_number) > 4))))
3245         {
3246           /* We can handle 16 bit signed values with an addiu to
3247              $zero.  No need to ever use daddiu here, since $zero and
3248              the result are always correct in 32 bit mode.  */
3249           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3250                        (int) BFD_RELOC_LO16);
3251           return;
3252         }
3253       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3254         {
3255           /* We can handle 16 bit unsigned values with an ori to
3256              $zero.  */
3257           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3258                        (int) BFD_RELOC_LO16);
3259           return;
3260         }
3261       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3262                 && (! dbl
3263                     || ! ep->X_unsigned
3264                     || sizeof (ep->X_add_number) > 4
3265                     || (ep->X_add_number & 0x80000000) == 0))
3266                || ((HAVE_32BIT_GPRS || ! dbl)
3267                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3268                || (HAVE_32BIT_GPRS
3269                    && ! dbl
3270                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3271                        == ~ (offsetT) 0xffffffff)))
3272         {
3273           /* 32 bit values require an lui.  */
3274           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3275                        (int) BFD_RELOC_HI16);
3276           if ((ep->X_add_number & 0xffff) != 0)
3277             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3278                          (int) BFD_RELOC_LO16);
3279           return;
3280         }
3281     }
3282
3283   /* The value is larger than 32 bits.  */
3284
3285   if (HAVE_32BIT_GPRS)
3286     {
3287       as_bad (_("Number (0x%lx) larger than 32 bits"),
3288               (unsigned long) ep->X_add_number);
3289       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3290                    (int) BFD_RELOC_LO16);
3291       return;
3292     }
3293
3294   if (ep->X_op != O_big)
3295     {
3296       hi32 = *ep;
3297       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3298       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3299       hi32.X_add_number &= 0xffffffff;
3300       lo32 = *ep;
3301       lo32.X_add_number &= 0xffffffff;
3302     }
3303   else
3304     {
3305       assert (ep->X_add_number > 2);
3306       if (ep->X_add_number == 3)
3307         generic_bignum[3] = 0;
3308       else if (ep->X_add_number > 4)
3309         as_bad (_("Number larger than 64 bits"));
3310       lo32.X_op = O_constant;
3311       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3312       hi32.X_op = O_constant;
3313       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3314     }
3315
3316   if (hi32.X_add_number == 0)
3317     freg = 0;
3318   else
3319     {
3320       int shift, bit;
3321       unsigned long hi, lo;
3322
3323       if (hi32.X_add_number == (offsetT) 0xffffffff)
3324         {
3325           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3326             {
3327               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3328                            reg, 0, (int) BFD_RELOC_LO16);
3329               return;
3330             }
3331           if (lo32.X_add_number & 0x80000000)
3332             {
3333               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3334                            (int) BFD_RELOC_HI16);
3335               if (lo32.X_add_number & 0xffff)
3336                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3337                              reg, reg, (int) BFD_RELOC_LO16);
3338               return;
3339             }
3340         }
3341
3342       /* Check for 16bit shifted constant.  We know that hi32 is
3343          non-zero, so start the mask on the first bit of the hi32
3344          value.  */
3345       shift = 17;
3346       do
3347         {
3348           unsigned long himask, lomask;
3349
3350           if (shift < 32)
3351             {
3352               himask = 0xffff >> (32 - shift);
3353               lomask = (0xffff << shift) & 0xffffffff;
3354             }
3355           else
3356             {
3357               himask = 0xffff << (shift - 32);
3358               lomask = 0;
3359             }
3360           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3361               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3362             {
3363               expressionS tmp;
3364
3365               tmp.X_op = O_constant;
3366               if (shift < 32)
3367                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3368                                     | (lo32.X_add_number >> shift));
3369               else
3370                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3371               macro_build ((char *) NULL, counter, &tmp,
3372                            "ori", "t,r,i", reg, 0,
3373                            (int) BFD_RELOC_LO16);
3374               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3375                            (shift >= 32) ? "dsll32" : "dsll",
3376                            "d,w,<", reg, reg,
3377                            (shift >= 32) ? shift - 32 : shift);
3378               return;
3379             }
3380           ++shift;
3381         }
3382       while (shift <= (64 - 16));
3383
3384       /* Find the bit number of the lowest one bit, and store the
3385          shifted value in hi/lo.  */
3386       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3387       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3388       if (lo != 0)
3389         {
3390           bit = 0;
3391           while ((lo & 1) == 0)
3392             {
3393               lo >>= 1;
3394               ++bit;
3395             }
3396           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3397           hi >>= bit;
3398         }
3399       else
3400         {
3401           bit = 32;
3402           while ((hi & 1) == 0)
3403             {
3404               hi >>= 1;
3405               ++bit;
3406             }
3407           lo = hi;
3408           hi = 0;
3409         }
3410
3411       /* Optimize if the shifted value is a (power of 2) - 1.  */
3412       if ((hi == 0 && ((lo + 1) & lo) == 0)
3413           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3414         {
3415           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3416           if (shift != 0)
3417             {
3418               expressionS tmp;
3419
3420               /* This instruction will set the register to be all
3421                  ones.  */
3422               tmp.X_op = O_constant;
3423               tmp.X_add_number = (offsetT) -1;
3424               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3425                            reg, 0, (int) BFD_RELOC_LO16);
3426               if (bit != 0)
3427                 {
3428                   bit += shift;
3429                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3430                                (bit >= 32) ? "dsll32" : "dsll",
3431                                "d,w,<", reg, reg,
3432                                (bit >= 32) ? bit - 32 : bit);
3433                 }
3434               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3435                            (shift >= 32) ? "dsrl32" : "dsrl",
3436                            "d,w,<", reg, reg,
3437                            (shift >= 32) ? shift - 32 : shift);
3438               return;
3439             }
3440         }
3441
3442       /* Sign extend hi32 before calling load_register, because we can
3443          generally get better code when we load a sign extended value.  */
3444       if ((hi32.X_add_number & 0x80000000) != 0)
3445         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3446       load_register (counter, reg, &hi32, 0);
3447       freg = reg;
3448     }
3449   if ((lo32.X_add_number & 0xffff0000) == 0)
3450     {
3451       if (freg != 0)
3452         {
3453           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3454                        "dsll32", "d,w,<", reg, freg, 0);
3455           freg = reg;
3456         }
3457     }
3458   else
3459     {
3460       expressionS mid16;
3461
3462       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3463         {
3464           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3465                        (int) BFD_RELOC_HI16);
3466           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3467                        "dsrl32", "d,w,<", reg, reg, 0);
3468           return;
3469         }
3470
3471       if (freg != 0)
3472         {
3473           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3474                        "d,w,<", reg, freg, 16);
3475           freg = reg;
3476         }
3477       mid16 = lo32;
3478       mid16.X_add_number >>= 16;
3479       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3480                    freg, (int) BFD_RELOC_LO16);
3481       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3482                    "d,w,<", reg, reg, 16);
3483       freg = reg;
3484     }
3485   if ((lo32.X_add_number & 0xffff) != 0)
3486     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3487                  (int) BFD_RELOC_LO16);
3488 }
3489
3490 /* Load an address into a register.  */
3491
3492 static void
3493 load_address (counter, reg, ep, used_at)
3494      int *counter;
3495      int reg;
3496      expressionS *ep;
3497      int *used_at;
3498 {
3499   char *p = NULL;
3500
3501   if (ep->X_op != O_constant
3502       && ep->X_op != O_symbol)
3503     {
3504       as_bad (_("expression too complex"));
3505       ep->X_op = O_constant;
3506     }
3507
3508   if (ep->X_op == O_constant)
3509     {
3510       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3511       return;
3512     }
3513
3514   if (mips_pic == NO_PIC)
3515     {
3516       /* If this is a reference to a GP relative symbol, we want
3517            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3518          Otherwise we want
3519            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3520            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3521          If we have an addend, we always use the latter form.
3522
3523          With 64bit address space and a usable $at we want
3524            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3525            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3526            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3527            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3528            dsll32       $reg,0
3529            dadd         $reg,$reg,$at
3530
3531          If $at is already in use, we use an path which is suboptimal
3532          on superscalar processors.
3533            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3534            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3535            dsll         $reg,16
3536            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3537            dsll         $reg,16
3538            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3539        */
3540       if (HAVE_64BIT_ADDRESSES)
3541         {
3542           /* We don't do GP optimization for now because RELAX_ENCODE can't
3543              hold the data for such large chunks.  */
3544
3545           if (*used_at == 0)
3546             {
3547               macro_build (p, counter, ep, "lui", "t,u",
3548                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3549               macro_build (p, counter, ep, "lui", "t,u",
3550                            AT, (int) BFD_RELOC_HI16_S);
3551               macro_build (p, counter, ep, "daddiu", "t,r,j",
3552                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3553               macro_build (p, counter, ep, "daddiu", "t,r,j",
3554                            AT, AT, (int) BFD_RELOC_LO16);
3555               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3556                            "d,w,<", reg, reg, 0);
3557               macro_build (p, counter, (expressionS *) NULL, "dadd",
3558                            "d,v,t", reg, reg, AT);
3559               *used_at = 1;
3560             }
3561           else
3562             {
3563               macro_build (p, counter, ep, "lui", "t,u",
3564                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3565               macro_build (p, counter, ep, "daddiu", "t,r,j",
3566                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3567               macro_build (p, counter, (expressionS *) NULL, "dsll",
3568                            "d,w,<", reg, reg, 16);
3569               macro_build (p, counter, ep, "daddiu", "t,r,j",
3570                            reg, reg, (int) BFD_RELOC_HI16_S);
3571               macro_build (p, counter, (expressionS *) NULL, "dsll",
3572                            "d,w,<", reg, reg, 16);
3573               macro_build (p, counter, ep, "daddiu", "t,r,j",
3574                            reg, reg, (int) BFD_RELOC_LO16);
3575             }
3576         }
3577       else
3578         {
3579           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3580               && ! nopic_need_relax (ep->X_add_symbol, 1))
3581             {
3582               frag_grow (20);
3583               macro_build ((char *) NULL, counter, ep,
3584                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3585                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3586               p = frag_var (rs_machine_dependent, 8, 0,
3587                             RELAX_ENCODE (4, 8, 0, 4, 0,
3588                                           mips_opts.warn_about_macros),
3589                             ep->X_add_symbol, 0, NULL);
3590             }
3591           macro_build_lui (p, counter, ep, reg);
3592           if (p != NULL)
3593             p += 4;
3594           macro_build (p, counter, ep,
3595                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3596                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3597         }
3598     }
3599   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3600     {
3601       expressionS ex;
3602
3603       /* If this is a reference to an external symbol, we want
3604            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3605          Otherwise we want
3606            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3607            nop
3608            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3609          If there is a constant, it must be added in after.  */
3610       ex.X_add_number = ep->X_add_number;
3611       ep->X_add_number = 0;
3612       frag_grow (20);
3613       macro_build ((char *) NULL, counter, ep,
3614                    HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3615                    reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3616       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3617       p = frag_var (rs_machine_dependent, 4, 0,
3618                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3619                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3620       macro_build (p, counter, ep,
3621                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3622                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3623       if (ex.X_add_number != 0)
3624         {
3625           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3626             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3627           ex.X_op = O_constant;
3628           macro_build ((char *) NULL, counter, &ex,
3629                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3630                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3631         }
3632     }
3633   else if (mips_pic == SVR4_PIC)
3634     {
3635       expressionS ex;
3636       int off;
3637
3638       /* This is the large GOT case.  If this is a reference to an
3639          external symbol, we want
3640            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3641            addu         $reg,$reg,$gp
3642            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3643          Otherwise, for a reference to a local symbol, we want
3644            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3645            nop
3646            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3647          If we have NewABI, we want
3648            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3649            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3650          If there is a constant, it must be added in after.  */
3651       ex.X_add_number = ep->X_add_number;
3652       ep->X_add_number = 0;
3653       if (HAVE_NEWABI)
3654         {
3655           macro_build ((char *) NULL, counter, ep,
3656                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3657                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3658           macro_build (p, counter, ep,
3659                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3660                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3661         }
3662       else
3663         {
3664           if (reg_needs_delay (mips_gp_register))
3665             off = 4;
3666           else
3667             off = 0;
3668           frag_grow (32);
3669           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3670                        (int) BFD_RELOC_MIPS_GOT_HI16);
3671           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3672                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3673                        reg, mips_gp_register);
3674           macro_build ((char *) NULL, counter, ep,
3675                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3676                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3677           p = frag_var (rs_machine_dependent, 12 + off, 0,
3678                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3679                                       mips_opts.warn_about_macros),
3680                         ep->X_add_symbol, 0, NULL);
3681           if (off > 0)
3682             {
3683               /* We need a nop before loading from $gp.  This special
3684                  check is required because the lui which starts the main
3685                  instruction stream does not refer to $gp, and so will not
3686                  insert the nop which may be required.  */
3687               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3688                 p += 4;
3689             }
3690           macro_build (p, counter, ep,
3691                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3692                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3693           p += 4;
3694           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3695           p += 4;
3696           macro_build (p, counter, ep,
3697                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3698                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3699         }
3700
3701       if (ex.X_add_number != 0)
3702         {
3703           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3704             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3705           ex.X_op = O_constant;
3706           macro_build ((char *) NULL, counter, &ex,
3707                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3708                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3709         }
3710     }
3711   else if (mips_pic == EMBEDDED_PIC)
3712     {
3713       /* We always do
3714            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3715        */
3716       macro_build ((char *) NULL, counter, ep,
3717                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3718                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3719     }
3720   else
3721     abort ();
3722 }
3723
3724 /* Move the contents of register SOURCE into register DEST.  */
3725
3726 static void
3727 move_register (counter, dest, source)
3728      int *counter;
3729      int dest;
3730      int source;
3731 {
3732   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3733                HAVE_32BIT_GPRS ? "addu" : "daddu",
3734                "d,v,t", dest, source, 0);
3735 }
3736
3737 /*
3738  *                      Build macros
3739  *   This routine implements the seemingly endless macro or synthesized
3740  * instructions and addressing modes in the mips assembly language. Many
3741  * of these macros are simple and are similar to each other. These could
3742  * probably be handled by some kind of table or grammer aproach instead of
3743  * this verbose method. Others are not simple macros but are more like
3744  * optimizing code generation.
3745  *   One interesting optimization is when several store macros appear
3746  * consecutivly that would load AT with the upper half of the same address.
3747  * The ensuing load upper instructions are ommited. This implies some kind
3748  * of global optimization. We currently only optimize within a single macro.
3749  *   For many of the load and store macros if the address is specified as a
3750  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3751  * first load register 'at' with zero and use it as the base register. The
3752  * mips assembler simply uses register $zero. Just one tiny optimization
3753  * we're missing.
3754  */
3755 static void
3756 macro (ip)
3757      struct mips_cl_insn *ip;
3758 {
3759   register int treg, sreg, dreg, breg;
3760   int tempreg;
3761   int mask;
3762   int icnt = 0;
3763   int used_at = 0;
3764   expressionS expr1;
3765   const char *s;
3766   const char *s2;
3767   const char *fmt;
3768   int likely = 0;
3769   int dbl = 0;
3770   int coproc = 0;
3771   int lr = 0;
3772   int imm = 0;
3773   offsetT maxnum;
3774   int off;
3775   bfd_reloc_code_real_type r;
3776   int hold_mips_optimize;
3777
3778   assert (! mips_opts.mips16);
3779
3780   treg = (ip->insn_opcode >> 16) & 0x1f;
3781   dreg = (ip->insn_opcode >> 11) & 0x1f;
3782   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3783   mask = ip->insn_mo->mask;
3784
3785   expr1.X_op = O_constant;
3786   expr1.X_op_symbol = NULL;
3787   expr1.X_add_symbol = NULL;
3788   expr1.X_add_number = 1;
3789
3790   switch (mask)
3791     {
3792     case M_DABS:
3793       dbl = 1;
3794     case M_ABS:
3795       /* bgez $a0,.+12
3796          move v0,$a0
3797          sub v0,$zero,$a0
3798          */
3799
3800       mips_emit_delays (true);
3801       ++mips_opts.noreorder;
3802       mips_any_noreorder = 1;
3803
3804       expr1.X_add_number = 8;
3805       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3806       if (dreg == sreg)
3807         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3808                      0);
3809       else
3810         move_register (&icnt, dreg, sreg);
3811       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3812                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3813
3814       --mips_opts.noreorder;
3815       return;
3816
3817     case M_ADD_I:
3818       s = "addi";
3819       s2 = "add";
3820       goto do_addi;
3821     case M_ADDU_I:
3822       s = "addiu";
3823       s2 = "addu";
3824       goto do_addi;
3825     case M_DADD_I:
3826       dbl = 1;
3827       s = "daddi";
3828       s2 = "dadd";
3829       goto do_addi;
3830     case M_DADDU_I:
3831       dbl = 1;
3832       s = "daddiu";
3833       s2 = "daddu";
3834     do_addi:
3835       if (imm_expr.X_op == O_constant
3836           && imm_expr.X_add_number >= -0x8000
3837           && imm_expr.X_add_number < 0x8000)
3838         {
3839           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3840                        (int) BFD_RELOC_LO16);
3841           return;
3842         }
3843       load_register (&icnt, AT, &imm_expr, dbl);
3844       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3845                    treg, sreg, AT);
3846       break;
3847
3848     case M_AND_I:
3849       s = "andi";
3850       s2 = "and";
3851       goto do_bit;
3852     case M_OR_I:
3853       s = "ori";
3854       s2 = "or";
3855       goto do_bit;
3856     case M_NOR_I:
3857       s = "";
3858       s2 = "nor";
3859       goto do_bit;
3860     case M_XOR_I:
3861       s = "xori";
3862       s2 = "xor";
3863     do_bit:
3864       if (imm_expr.X_op == O_constant
3865           && imm_expr.X_add_number >= 0
3866           && imm_expr.X_add_number < 0x10000)
3867         {
3868           if (mask != M_NOR_I)
3869             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3870                          sreg, (int) BFD_RELOC_LO16);
3871           else
3872             {
3873               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3874                            treg, sreg, (int) BFD_RELOC_LO16);
3875               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3876                            "d,v,t", treg, treg, 0);
3877             }
3878           return;
3879         }
3880
3881       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3882       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3883                    treg, sreg, AT);
3884       break;
3885
3886     case M_BEQ_I:
3887       s = "beq";
3888       goto beq_i;
3889     case M_BEQL_I:
3890       s = "beql";
3891       likely = 1;
3892       goto beq_i;
3893     case M_BNE_I:
3894       s = "bne";
3895       goto beq_i;
3896     case M_BNEL_I:
3897       s = "bnel";
3898       likely = 1;
3899     beq_i:
3900       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3901         {
3902           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3903                        0);
3904           return;
3905         }
3906       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3907       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3908       break;
3909
3910     case M_BGEL:
3911       likely = 1;
3912     case M_BGE:
3913       if (treg == 0)
3914         {
3915           macro_build ((char *) NULL, &icnt, &offset_expr,
3916                        likely ? "bgezl" : "bgez", "s,p", sreg);
3917           return;
3918         }
3919       if (sreg == 0)
3920         {
3921           macro_build ((char *) NULL, &icnt, &offset_expr,
3922                        likely ? "blezl" : "blez", "s,p", treg);
3923           return;
3924         }
3925       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3926                    AT, sreg, treg);
3927       macro_build ((char *) NULL, &icnt, &offset_expr,
3928                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3929       break;
3930
3931     case M_BGTL_I:
3932       likely = 1;
3933     case M_BGT_I:
3934       /* check for > max integer */
3935       maxnum = 0x7fffffff;
3936       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3937         {
3938           maxnum <<= 16;
3939           maxnum |= 0xffff;
3940           maxnum <<= 16;
3941           maxnum |= 0xffff;
3942         }
3943       if (imm_expr.X_op == O_constant
3944           && imm_expr.X_add_number >= maxnum
3945           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3946         {
3947         do_false:
3948           /* result is always false */
3949           if (! likely)
3950             {
3951               if (warn_nops)
3952                 as_warn (_("Branch %s is always false (nop)"),
3953                          ip->insn_mo->name);
3954               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3955                            "", 0);
3956             }
3957           else
3958             {
3959               if (warn_nops)
3960                 as_warn (_("Branch likely %s is always false"),
3961                          ip->insn_mo->name);
3962               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3963                            "s,t,p", 0, 0);
3964             }
3965           return;
3966         }
3967       if (imm_expr.X_op != O_constant)
3968         as_bad (_("Unsupported large constant"));
3969       ++imm_expr.X_add_number;
3970       /* FALLTHROUGH */
3971     case M_BGE_I:
3972     case M_BGEL_I:
3973       if (mask == M_BGEL_I)
3974         likely = 1;
3975       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3976         {
3977           macro_build ((char *) NULL, &icnt, &offset_expr,
3978                        likely ? "bgezl" : "bgez", "s,p", sreg);
3979           return;
3980         }
3981       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3982         {
3983           macro_build ((char *) NULL, &icnt, &offset_expr,
3984                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
3985           return;
3986         }
3987       maxnum = 0x7fffffff;
3988       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3989         {
3990           maxnum <<= 16;
3991           maxnum |= 0xffff;
3992           maxnum <<= 16;
3993           maxnum |= 0xffff;
3994         }
3995       maxnum = - maxnum - 1;
3996       if (imm_expr.X_op == O_constant
3997           && imm_expr.X_add_number <= maxnum
3998           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3999         {
4000         do_true:
4001           /* result is always true */
4002           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4003           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4004           return;
4005         }
4006       set_at (&icnt, sreg, 0);
4007       macro_build ((char *) NULL, &icnt, &offset_expr,
4008                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4009       break;
4010
4011     case M_BGEUL:
4012       likely = 1;
4013     case M_BGEU:
4014       if (treg == 0)
4015         goto do_true;
4016       if (sreg == 0)
4017         {
4018           macro_build ((char *) NULL, &icnt, &offset_expr,
4019                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4020           return;
4021         }
4022       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4023                    "d,v,t", AT, sreg, treg);
4024       macro_build ((char *) NULL, &icnt, &offset_expr,
4025                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4026       break;
4027
4028     case M_BGTUL_I:
4029       likely = 1;
4030     case M_BGTU_I:
4031       if (sreg == 0
4032           || (HAVE_32BIT_GPRS
4033               && imm_expr.X_op == O_constant
4034               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4035         goto do_false;
4036       if (imm_expr.X_op != O_constant)
4037         as_bad (_("Unsupported large constant"));
4038       ++imm_expr.X_add_number;
4039       /* FALLTHROUGH */
4040     case M_BGEU_I:
4041     case M_BGEUL_I:
4042       if (mask == M_BGEUL_I)
4043         likely = 1;
4044       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4045         goto do_true;
4046       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4047         {
4048           macro_build ((char *) NULL, &icnt, &offset_expr,
4049                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4050           return;
4051         }
4052       set_at (&icnt, sreg, 1);
4053       macro_build ((char *) NULL, &icnt, &offset_expr,
4054                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4055       break;
4056
4057     case M_BGTL:
4058       likely = 1;
4059     case M_BGT:
4060       if (treg == 0)
4061         {
4062           macro_build ((char *) NULL, &icnt, &offset_expr,
4063                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4064           return;
4065         }
4066       if (sreg == 0)
4067         {
4068           macro_build ((char *) NULL, &icnt, &offset_expr,
4069                        likely ? "bltzl" : "bltz", "s,p", treg);
4070           return;
4071         }
4072       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4073                    AT, treg, sreg);
4074       macro_build ((char *) NULL, &icnt, &offset_expr,
4075                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4076       break;
4077
4078     case M_BGTUL:
4079       likely = 1;
4080     case M_BGTU:
4081       if (treg == 0)
4082         {
4083           macro_build ((char *) NULL, &icnt, &offset_expr,
4084                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4085           return;
4086         }
4087       if (sreg == 0)
4088         goto do_false;
4089       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4090                    "d,v,t", AT, treg, sreg);
4091       macro_build ((char *) NULL, &icnt, &offset_expr,
4092                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4093       break;
4094
4095     case M_BLEL:
4096       likely = 1;
4097     case M_BLE:
4098       if (treg == 0)
4099         {
4100           macro_build ((char *) NULL, &icnt, &offset_expr,
4101                        likely ? "blezl" : "blez", "s,p", sreg);
4102           return;
4103         }
4104       if (sreg == 0)
4105         {
4106           macro_build ((char *) NULL, &icnt, &offset_expr,
4107                        likely ? "bgezl" : "bgez", "s,p", treg);
4108           return;
4109         }
4110       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4111                    AT, treg, sreg);
4112       macro_build ((char *) NULL, &icnt, &offset_expr,
4113                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4114       break;
4115
4116     case M_BLEL_I:
4117       likely = 1;
4118     case M_BLE_I:
4119       maxnum = 0x7fffffff;
4120       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4121         {
4122           maxnum <<= 16;
4123           maxnum |= 0xffff;
4124           maxnum <<= 16;
4125           maxnum |= 0xffff;
4126         }
4127       if (imm_expr.X_op == O_constant
4128           && imm_expr.X_add_number >= maxnum
4129           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4130         goto do_true;
4131       if (imm_expr.X_op != O_constant)
4132         as_bad (_("Unsupported large constant"));
4133       ++imm_expr.X_add_number;
4134       /* FALLTHROUGH */
4135     case M_BLT_I:
4136     case M_BLTL_I:
4137       if (mask == M_BLTL_I)
4138         likely = 1;
4139       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4140         {
4141           macro_build ((char *) NULL, &icnt, &offset_expr,
4142                        likely ? "bltzl" : "bltz", "s,p", sreg);
4143           return;
4144         }
4145       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4146         {
4147           macro_build ((char *) NULL, &icnt, &offset_expr,
4148                        likely ? "blezl" : "blez", "s,p", sreg);
4149           return;
4150         }
4151       set_at (&icnt, sreg, 0);
4152       macro_build ((char *) NULL, &icnt, &offset_expr,
4153                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4154       break;
4155
4156     case M_BLEUL:
4157       likely = 1;
4158     case M_BLEU:
4159       if (treg == 0)
4160         {
4161           macro_build ((char *) NULL, &icnt, &offset_expr,
4162                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4163           return;
4164         }
4165       if (sreg == 0)
4166         goto do_true;
4167       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4168                    "d,v,t", AT, treg, sreg);
4169       macro_build ((char *) NULL, &icnt, &offset_expr,
4170                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4171       break;
4172
4173     case M_BLEUL_I:
4174       likely = 1;
4175     case M_BLEU_I:
4176       if (sreg == 0
4177           || (HAVE_32BIT_GPRS
4178               && imm_expr.X_op == O_constant
4179               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4180         goto do_true;
4181       if (imm_expr.X_op != O_constant)
4182         as_bad (_("Unsupported large constant"));
4183       ++imm_expr.X_add_number;
4184       /* FALLTHROUGH */
4185     case M_BLTU_I:
4186     case M_BLTUL_I:
4187       if (mask == M_BLTUL_I)
4188         likely = 1;
4189       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4190         goto do_false;
4191       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4192         {
4193           macro_build ((char *) NULL, &icnt, &offset_expr,
4194                        likely ? "beql" : "beq",
4195                        "s,t,p", sreg, 0);
4196           return;
4197         }
4198       set_at (&icnt, sreg, 1);
4199       macro_build ((char *) NULL, &icnt, &offset_expr,
4200                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4201       break;
4202
4203     case M_BLTL:
4204       likely = 1;
4205     case M_BLT:
4206       if (treg == 0)
4207         {
4208           macro_build ((char *) NULL, &icnt, &offset_expr,
4209                        likely ? "bltzl" : "bltz", "s,p", sreg);
4210           return;
4211         }
4212       if (sreg == 0)
4213         {
4214           macro_build ((char *) NULL, &icnt, &offset_expr,
4215                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4216           return;
4217         }
4218       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4219                    AT, sreg, treg);
4220       macro_build ((char *) NULL, &icnt, &offset_expr,
4221                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4222       break;
4223
4224     case M_BLTUL:
4225       likely = 1;
4226     case M_BLTU:
4227       if (treg == 0)
4228         goto do_false;
4229       if (sreg == 0)
4230         {
4231           macro_build ((char *) NULL, &icnt, &offset_expr,
4232                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4233           return;
4234         }
4235       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4236                    "d,v,t", AT, sreg,
4237                    treg);
4238       macro_build ((char *) NULL, &icnt, &offset_expr,
4239                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4240       break;
4241
4242     case M_DDIV_3:
4243       dbl = 1;
4244     case M_DIV_3:
4245       s = "mflo";
4246       goto do_div3;
4247     case M_DREM_3:
4248       dbl = 1;
4249     case M_REM_3:
4250       s = "mfhi";
4251     do_div3:
4252       if (treg == 0)
4253         {
4254           as_warn (_("Divide by zero."));
4255           if (mips_trap)
4256             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4257                          "s,t", 0, 0);
4258           else
4259             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4260                          "c", 7);
4261           return;
4262         }
4263
4264       mips_emit_delays (true);
4265       ++mips_opts.noreorder;
4266       mips_any_noreorder = 1;
4267       if (mips_trap)
4268         {
4269           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4270                        "s,t", treg, 0);
4271           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4272                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4273         }
4274       else
4275         {
4276           expr1.X_add_number = 8;
4277           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4278           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4279                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4280           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4281                        "c", 7);
4282         }
4283       expr1.X_add_number = -1;
4284       macro_build ((char *) NULL, &icnt, &expr1,
4285                    dbl ? "daddiu" : "addiu",
4286                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4287       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4288       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4289       if (dbl)
4290         {
4291           expr1.X_add_number = 1;
4292           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4293                        (int) BFD_RELOC_LO16);
4294           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4295                        "d,w,<", AT, AT, 31);
4296         }
4297       else
4298         {
4299           expr1.X_add_number = 0x80000000;
4300           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4301                        (int) BFD_RELOC_HI16);
4302         }
4303       if (mips_trap)
4304         {
4305           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4306                        "s,t", sreg, AT);
4307           /* We want to close the noreorder block as soon as possible, so
4308              that later insns are available for delay slot filling.  */
4309           --mips_opts.noreorder;
4310         }
4311       else
4312         {
4313           expr1.X_add_number = 8;
4314           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4315           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4316                        0);
4317
4318           /* We want to close the noreorder block as soon as possible, so
4319              that later insns are available for delay slot filling.  */
4320           --mips_opts.noreorder;
4321
4322           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4323                        "c", 6);
4324         }
4325       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4326       break;
4327
4328     case M_DIV_3I:
4329       s = "div";
4330       s2 = "mflo";
4331       goto do_divi;
4332     case M_DIVU_3I:
4333       s = "divu";
4334       s2 = "mflo";
4335       goto do_divi;
4336     case M_REM_3I:
4337       s = "div";
4338       s2 = "mfhi";
4339       goto do_divi;
4340     case M_REMU_3I:
4341       s = "divu";
4342       s2 = "mfhi";
4343       goto do_divi;
4344     case M_DDIV_3I:
4345       dbl = 1;
4346       s = "ddiv";
4347       s2 = "mflo";
4348       goto do_divi;
4349     case M_DDIVU_3I:
4350       dbl = 1;
4351       s = "ddivu";
4352       s2 = "mflo";
4353       goto do_divi;
4354     case M_DREM_3I:
4355       dbl = 1;
4356       s = "ddiv";
4357       s2 = "mfhi";
4358       goto do_divi;
4359     case M_DREMU_3I:
4360       dbl = 1;
4361       s = "ddivu";
4362       s2 = "mfhi";
4363     do_divi:
4364       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4365         {
4366           as_warn (_("Divide by zero."));
4367           if (mips_trap)
4368             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4369                          "s,t", 0, 0);
4370           else
4371             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4372                          "c", 7);
4373           return;
4374         }
4375       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4376         {
4377           if (strcmp (s2, "mflo") == 0)
4378             move_register (&icnt, dreg, sreg);
4379           else
4380             move_register (&icnt, dreg, 0);
4381           return;
4382         }
4383       if (imm_expr.X_op == O_constant
4384           && imm_expr.X_add_number == -1
4385           && s[strlen (s) - 1] != 'u')
4386         {
4387           if (strcmp (s2, "mflo") == 0)
4388             {
4389               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4390                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4391             }
4392           else
4393             move_register (&icnt, dreg, 0);
4394           return;
4395         }
4396
4397       load_register (&icnt, AT, &imm_expr, dbl);
4398       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4399                    sreg, AT);
4400       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4401       break;
4402
4403     case M_DIVU_3:
4404       s = "divu";
4405       s2 = "mflo";
4406       goto do_divu3;
4407     case M_REMU_3:
4408       s = "divu";
4409       s2 = "mfhi";
4410       goto do_divu3;
4411     case M_DDIVU_3:
4412       s = "ddivu";
4413       s2 = "mflo";
4414       goto do_divu3;
4415     case M_DREMU_3:
4416       s = "ddivu";
4417       s2 = "mfhi";
4418     do_divu3:
4419       mips_emit_delays (true);
4420       ++mips_opts.noreorder;
4421       mips_any_noreorder = 1;
4422       if (mips_trap)
4423         {
4424           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4425                        "s,t", treg, 0);
4426           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4427                        sreg, treg);
4428           /* We want to close the noreorder block as soon as possible, so
4429              that later insns are available for delay slot filling.  */
4430           --mips_opts.noreorder;
4431         }
4432       else
4433         {
4434           expr1.X_add_number = 8;
4435           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4436           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4437                        sreg, treg);
4438
4439           /* We want to close the noreorder block as soon as possible, so
4440              that later insns are available for delay slot filling.  */
4441           --mips_opts.noreorder;
4442           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4443                        "c", 7);
4444         }
4445       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4446       return;
4447
4448     case M_DLA_AB:
4449       dbl = 1;
4450     case M_LA_AB:
4451       /* Load the address of a symbol into a register.  If breg is not
4452          zero, we then add a base register to it.  */
4453
4454       if (dbl && HAVE_32BIT_GPRS)
4455         as_warn (_("dla used to load 32-bit register"));
4456
4457       if (! dbl && HAVE_64BIT_OBJECTS)
4458         as_warn (_("la used to load 64-bit address"));
4459
4460       if (treg == breg)
4461         {
4462           tempreg = AT;
4463           used_at = 1;
4464         }
4465       else
4466         {
4467           tempreg = treg;
4468           used_at = 0;
4469         }
4470
4471       /* When generating embedded PIC code, we permit expressions of
4472          the form
4473            la   $treg,foo-bar
4474            la   $treg,foo-bar($breg)
4475          where bar is an address in the current section.  These are used
4476          when getting the addresses of functions.  We don't permit
4477          X_add_number to be non-zero, because if the symbol is
4478          external the relaxing code needs to know that any addend is
4479          purely the offset to X_op_symbol.  */
4480       if (mips_pic == EMBEDDED_PIC
4481           && offset_expr.X_op == O_subtract
4482           && (symbol_constant_p (offset_expr.X_op_symbol)
4483               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4484               : (symbol_equated_p (offset_expr.X_op_symbol)
4485                  && (S_GET_SEGMENT
4486                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4487                       ->X_add_symbol)
4488                      == now_seg)))
4489           && (offset_expr.X_add_number == 0
4490               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4491         {
4492           if (breg == 0)
4493             {
4494               tempreg = treg;
4495               used_at = 0;
4496               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4497                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4498             }
4499           else
4500             {
4501               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4502                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4503               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4504                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4505                            "d,v,t", tempreg, tempreg, breg);
4506             }
4507           macro_build ((char *) NULL, &icnt, &offset_expr,
4508                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4509                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4510           if (! used_at)
4511             return;
4512           break;
4513         }
4514
4515       if (offset_expr.X_op != O_symbol
4516           && offset_expr.X_op != O_constant)
4517         {
4518           as_bad (_("expression too complex"));
4519           offset_expr.X_op = O_constant;
4520         }
4521
4522       if (offset_expr.X_op == O_constant)
4523         load_register (&icnt, tempreg, &offset_expr,
4524                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4525                         ? (dbl || HAVE_64BIT_ADDRESSES)
4526                         : HAVE_64BIT_ADDRESSES));
4527       else if (mips_pic == NO_PIC)
4528         {
4529           /* If this is a reference to a GP relative symbol, we want
4530                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4531              Otherwise we want
4532                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4533                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4534              If we have a constant, we need two instructions anyhow,
4535              so we may as well always use the latter form.
4536
4537             With 64bit address space and a usable $at we want
4538               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4539               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4540               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4541               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4542               dsll32    $tempreg,0
4543               dadd      $tempreg,$tempreg,$at
4544
4545             If $at is already in use, we use an path which is suboptimal
4546             on superscalar processors.
4547               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4548               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4549               dsll      $tempreg,16
4550               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4551               dsll      $tempreg,16
4552               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4553           */
4554           char *p = NULL;
4555           if (HAVE_64BIT_ADDRESSES)
4556             {
4557               /* We don't do GP optimization for now because RELAX_ENCODE can't
4558                  hold the data for such large chunks.  */
4559
4560               if (used_at == 0)
4561                 {
4562                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4563                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4564                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4565                                AT, (int) BFD_RELOC_HI16_S);
4566                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4567                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4568                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4569                                AT, AT, (int) BFD_RELOC_LO16);
4570                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4571                                "d,w,<", tempreg, tempreg, 0);
4572                   macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4573                                tempreg, tempreg, AT);
4574                   used_at = 1;
4575                 }
4576               else
4577                 {
4578                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4579                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4580                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4581                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4582                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4583                                tempreg, tempreg, 16);
4584                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4585                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4586                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4587                                tempreg, tempreg, 16);
4588                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4589                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4590                 }
4591             }
4592           else
4593             {
4594               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4595                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4596                 {
4597                   frag_grow (20);
4598                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4599                                "t,r,j", tempreg, mips_gp_register,
4600                                (int) BFD_RELOC_GPREL16);
4601                   p = frag_var (rs_machine_dependent, 8, 0,
4602                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4603                                               mips_opts.warn_about_macros),
4604                                 offset_expr.X_add_symbol, 0, NULL);
4605                 }
4606               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4607               if (p != NULL)
4608                 p += 4;
4609               macro_build (p, &icnt, &offset_expr, "addiu",
4610                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4611             }
4612         }
4613       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4614         {
4615           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4616
4617           /* If this is a reference to an external symbol, and there
4618              is no constant, we want
4619                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4620              or if tempreg is PIC_CALL_REG
4621                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4622              For a local symbol, we want
4623                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4624                nop
4625                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4626
4627              If we have a small constant, and this is a reference to
4628              an external symbol, we want
4629                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4630                nop
4631                addiu    $tempreg,$tempreg,<constant>
4632              For a local symbol, we want the same instruction
4633              sequence, but we output a BFD_RELOC_LO16 reloc on the
4634              addiu instruction.
4635
4636              If we have a large constant, and this is a reference to
4637              an external symbol, we want
4638                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4639                lui      $at,<hiconstant>
4640                addiu    $at,$at,<loconstant>
4641                addu     $tempreg,$tempreg,$at
4642              For a local symbol, we want the same instruction
4643              sequence, but we output a BFD_RELOC_LO16 reloc on the
4644              addiu instruction.  */
4645           expr1.X_add_number = offset_expr.X_add_number;
4646           offset_expr.X_add_number = 0;
4647           frag_grow (32);
4648           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4649             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4650           macro_build ((char *) NULL, &icnt, &offset_expr,
4651                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4652                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4653           if (expr1.X_add_number == 0)
4654             {
4655               int off;
4656               char *p;
4657
4658               if (breg == 0)
4659                 off = 0;
4660               else
4661                 {
4662                   /* We're going to put in an addu instruction using
4663                      tempreg, so we may as well insert the nop right
4664                      now.  */
4665                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4666                                "nop", "");
4667                   off = 4;
4668                 }
4669               p = frag_var (rs_machine_dependent, 8 - off, 0,
4670                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4671                                           (breg == 0
4672                                            ? mips_opts.warn_about_macros
4673                                            : 0)),
4674                             offset_expr.X_add_symbol, 0, NULL);
4675               if (breg == 0)
4676                 {
4677                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4678                   p += 4;
4679                 }
4680               macro_build (p, &icnt, &expr1,
4681                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4682                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4683               /* FIXME: If breg == 0, and the next instruction uses
4684                  $tempreg, then if this variant case is used an extra
4685                  nop will be generated.  */
4686             }
4687           else if (expr1.X_add_number >= -0x8000
4688                    && expr1.X_add_number < 0x8000)
4689             {
4690               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4691                            "nop", "");
4692               macro_build ((char *) NULL, &icnt, &expr1,
4693                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4694                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4695               frag_var (rs_machine_dependent, 0, 0,
4696                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4697                         offset_expr.X_add_symbol, 0, NULL);
4698             }
4699           else
4700             {
4701               int off1;
4702
4703               /* If we are going to add in a base register, and the
4704                  target register and the base register are the same,
4705                  then we are using AT as a temporary register.  Since
4706                  we want to load the constant into AT, we add our
4707                  current AT (from the global offset table) and the
4708                  register into the register now, and pretend we were
4709                  not using a base register.  */
4710               if (breg != treg)
4711                 off1 = 0;
4712               else
4713                 {
4714                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4715                                "nop", "");
4716                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4717                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4718                                "d,v,t", treg, AT, breg);
4719                   breg = 0;
4720                   tempreg = treg;
4721                   off1 = -8;
4722                 }
4723
4724               /* Set mips_optimize around the lui instruction to avoid
4725                  inserting an unnecessary nop after the lw.  */
4726               hold_mips_optimize = mips_optimize;
4727               mips_optimize = 2;
4728               macro_build_lui (NULL, &icnt, &expr1, AT);
4729               mips_optimize = hold_mips_optimize;
4730
4731               macro_build ((char *) NULL, &icnt, &expr1,
4732                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4733                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4734               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4735                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4736                            "d,v,t", tempreg, tempreg, AT);
4737               frag_var (rs_machine_dependent, 0, 0,
4738                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4739                         offset_expr.X_add_symbol, 0, NULL);
4740               used_at = 1;
4741             }
4742         }
4743       else if (mips_pic == SVR4_PIC)
4744         {
4745           int gpdel;
4746           char *p;
4747           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4748           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4749
4750           /* This is the large GOT case.  If this is a reference to an
4751              external symbol, and there is no constant, we want
4752                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4753                addu     $tempreg,$tempreg,$gp
4754                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4755              or if tempreg is PIC_CALL_REG
4756                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4757                addu     $tempreg,$tempreg,$gp
4758                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4759              For a local symbol, we want
4760                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4761                nop
4762                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4763
4764              If we have a small constant, and this is a reference to
4765              an external symbol, we want
4766                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4767                addu     $tempreg,$tempreg,$gp
4768                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4769                nop
4770                addiu    $tempreg,$tempreg,<constant>
4771              For a local symbol, we want
4772                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4773                nop
4774                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4775
4776              If we have a large constant, and this is a reference to
4777              an external symbol, we want
4778                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4779                addu     $tempreg,$tempreg,$gp
4780                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4781                lui      $at,<hiconstant>
4782                addiu    $at,$at,<loconstant>
4783                addu     $tempreg,$tempreg,$at
4784              For a local symbol, we want
4785                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4786                lui      $at,<hiconstant>
4787                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4788                addu     $tempreg,$tempreg,$at
4789
4790              For NewABI, we want for data addresses
4791                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4792              If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4793                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4794            */
4795           if (HAVE_NEWABI)
4796             {
4797               int reloc_type = (tempreg == PIC_CALL_REG
4798                                 ? BFD_RELOC_MIPS_CALL16
4799                                 : BFD_RELOC_MIPS_GOT_DISP);
4800
4801               macro_build ((char *) NULL, &icnt, &offset_expr,
4802                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4803                            "t,o(b)", tempreg, reloc_type, mips_gp_register);
4804
4805               if (breg != 0)
4806                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4807                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4808                              "d,v,t", treg, tempreg, breg);
4809
4810               if (! used_at)
4811                 return;
4812
4813               break;
4814             }
4815           expr1.X_add_number = offset_expr.X_add_number;
4816           offset_expr.X_add_number = 0;
4817           frag_grow (52);
4818           if (reg_needs_delay (mips_gp_register))
4819             gpdel = 4;
4820           else
4821             gpdel = 0;
4822           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4823             {
4824               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4825               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4826             }
4827           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4828                        tempreg, lui_reloc_type);
4829           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4830                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4831                        "d,v,t", tempreg, tempreg, mips_gp_register);
4832           macro_build ((char *) NULL, &icnt, &offset_expr,
4833                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4834                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4835           if (expr1.X_add_number == 0)
4836             {
4837               int off;
4838
4839               if (breg == 0)
4840                 off = 0;
4841               else
4842                 {
4843                   /* We're going to put in an addu instruction using
4844                      tempreg, so we may as well insert the nop right
4845                      now.  */
4846                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4847                                "nop", "");
4848                   off = 4;
4849                 }
4850
4851               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4852                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4853                                           8 + gpdel, 0,
4854                                           (breg == 0
4855                                            ? mips_opts.warn_about_macros
4856                                            : 0)),
4857                             offset_expr.X_add_symbol, 0, NULL);
4858             }
4859           else if (expr1.X_add_number >= -0x8000
4860                    && expr1.X_add_number < 0x8000)
4861             {
4862               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4863                            "nop", "");
4864               macro_build ((char *) NULL, &icnt, &expr1,
4865                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4866                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4867
4868               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4869                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4870                                           (breg == 0
4871                                            ? mips_opts.warn_about_macros
4872                                            : 0)),
4873                             offset_expr.X_add_symbol, 0, NULL);
4874             }
4875           else
4876             {
4877               int adj, dreg;
4878
4879               /* If we are going to add in a base register, and the
4880                  target register and the base register are the same,
4881                  then we are using AT as a temporary register.  Since
4882                  we want to load the constant into AT, we add our
4883                  current AT (from the global offset table) and the
4884                  register into the register now, and pretend we were
4885                  not using a base register.  */
4886               if (breg != treg)
4887                 {
4888                   adj = 0;
4889                   dreg = tempreg;
4890                 }
4891               else
4892                 {
4893                   assert (tempreg == AT);
4894                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4895                                "nop", "");
4896                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4897                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4898                                "d,v,t", treg, AT, breg);
4899                   dreg = treg;
4900                   adj = 8;
4901                 }
4902
4903               /* Set mips_optimize around the lui instruction to avoid
4904                  inserting an unnecessary nop after the lw.  */
4905               hold_mips_optimize = mips_optimize;
4906               mips_optimize = 2;
4907               macro_build_lui (NULL, &icnt, &expr1, AT);
4908               mips_optimize = hold_mips_optimize;
4909
4910               macro_build ((char *) NULL, &icnt, &expr1,
4911                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4912                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4913               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4914                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4915                            "d,v,t", dreg, dreg, AT);
4916
4917               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4918                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4919                                           8 + gpdel, 0,
4920                                           (breg == 0
4921                                            ? mips_opts.warn_about_macros
4922                                            : 0)),
4923                             offset_expr.X_add_symbol, 0, NULL);
4924
4925               used_at = 1;
4926             }
4927
4928           if (gpdel > 0)
4929             {
4930               /* This is needed because this instruction uses $gp, but
4931                  the first instruction on the main stream does not.  */
4932               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4933               p += 4;
4934             }
4935           macro_build (p, &icnt, &offset_expr,
4936                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4937                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4938                        mips_gp_register);
4939           p += 4;
4940           if (expr1.X_add_number >= -0x8000
4941               && expr1.X_add_number < 0x8000)
4942             {
4943               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4944               p += 4;
4945               macro_build (p, &icnt, &expr1,
4946                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4947                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4948               /* FIXME: If add_number is 0, and there was no base
4949                  register, the external symbol case ended with a load,
4950                  so if the symbol turns out to not be external, and
4951                  the next instruction uses tempreg, an unnecessary nop
4952                  will be inserted.  */
4953             }
4954           else
4955             {
4956               if (breg == treg)
4957                 {
4958                   /* We must add in the base register now, as in the
4959                      external symbol case.  */
4960                   assert (tempreg == AT);
4961                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4962                   p += 4;
4963                   macro_build (p, &icnt, (expressionS *) NULL,
4964                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4965                                "d,v,t", treg, AT, breg);
4966                   p += 4;
4967                   tempreg = treg;
4968                   /* We set breg to 0 because we have arranged to add
4969                      it in in both cases.  */
4970                   breg = 0;
4971                 }
4972
4973               macro_build_lui (p, &icnt, &expr1, AT);
4974               p += 4;
4975               macro_build (p, &icnt, &expr1,
4976                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4977                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4978               p += 4;
4979               macro_build (p, &icnt, (expressionS *) NULL,
4980                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4981                            "d,v,t", tempreg, tempreg, AT);
4982               p += 4;
4983             }
4984         }
4985       else if (mips_pic == EMBEDDED_PIC)
4986         {
4987           /* We use
4988                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4989              */
4990           macro_build ((char *) NULL, &icnt, &offset_expr,
4991                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4992                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4993         }
4994       else
4995         abort ();
4996
4997       if (breg != 0)
4998         {
4999           char *s;
5000
5001           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5002             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5003           else
5004             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5005
5006           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5007                        "d,v,t", treg, tempreg, breg);
5008         }
5009
5010       if (! used_at)
5011         return;
5012
5013       break;
5014
5015     case M_J_A:
5016       /* The j instruction may not be used in PIC code, since it
5017          requires an absolute address.  We convert it to a b
5018          instruction.  */
5019       if (mips_pic == NO_PIC)
5020         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5021       else
5022         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5023       return;
5024
5025       /* The jal instructions must be handled as macros because when
5026          generating PIC code they expand to multi-instruction
5027          sequences.  Normally they are simple instructions.  */
5028     case M_JAL_1:
5029       dreg = RA;
5030       /* Fall through.  */
5031     case M_JAL_2:
5032       if (mips_pic == NO_PIC
5033           || mips_pic == EMBEDDED_PIC)
5034         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5035                      "d,s", dreg, sreg);
5036       else if (mips_pic == SVR4_PIC)
5037         {
5038           if (sreg != PIC_CALL_REG)
5039             as_warn (_("MIPS PIC call to register other than $25"));
5040
5041           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5042                        "d,s", dreg, sreg);
5043           if (! HAVE_NEWABI)
5044             {
5045               if (mips_cprestore_offset < 0)
5046                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5047               else
5048                 {
5049                   if (! mips_frame_reg_valid)
5050                     {
5051                       as_warn (_("No .frame pseudo-op used in PIC code"));
5052                       /* Quiet this warning.  */
5053                       mips_frame_reg_valid = 1;
5054                     }
5055                   if (! mips_cprestore_valid)
5056                     {
5057                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5058                       /* Quiet this warning.  */
5059                       mips_cprestore_valid = 1;
5060                     }
5061                   expr1.X_add_number = mips_cprestore_offset;
5062                   macro_build ((char *) NULL, &icnt, &expr1,
5063                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5064                                mips_gp_register, (int) BFD_RELOC_LO16,
5065                                mips_frame_reg);
5066                 }
5067             }
5068         }
5069       else
5070         abort ();
5071
5072       return;
5073
5074     case M_JAL_A:
5075       if (mips_pic == NO_PIC)
5076         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5077       else if (mips_pic == SVR4_PIC)
5078         {
5079           char *p;
5080
5081           /* If this is a reference to an external symbol, and we are
5082              using a small GOT, we want
5083                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5084                nop
5085                jalr     $ra,$25
5086                nop
5087                lw       $gp,cprestore($sp)
5088              The cprestore value is set using the .cprestore
5089              pseudo-op.  If we are using a big GOT, we want
5090                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5091                addu     $25,$25,$gp
5092                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5093                nop
5094                jalr     $ra,$25
5095                nop
5096                lw       $gp,cprestore($sp)
5097              If the symbol is not external, we want
5098                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5099                nop
5100                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5101                jalr     $ra,$25
5102                nop
5103                lw $gp,cprestore($sp)
5104              For NewABI, we want
5105                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5106                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5107            */
5108           if (HAVE_NEWABI)
5109             {
5110               macro_build ((char *) NULL, &icnt, &offset_expr,
5111                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5112                            "t,o(b)", PIC_CALL_REG,
5113                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5114               macro_build_jalr (icnt, &offset_expr);
5115             }
5116           else
5117             {
5118               frag_grow (40);
5119               if (! mips_big_got)
5120                 {
5121                   macro_build ((char *) NULL, &icnt, &offset_expr,
5122                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5123                                "t,o(b)", PIC_CALL_REG,
5124                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5125                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5126                                "nop", "");
5127                   p = frag_var (rs_machine_dependent, 4, 0,
5128                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5129                                 offset_expr.X_add_symbol, 0, NULL);
5130                 }
5131               else
5132                 {
5133                   int gpdel;
5134
5135                   if (reg_needs_delay (mips_gp_register))
5136                     gpdel = 4;
5137                   else
5138                     gpdel = 0;
5139                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5140                                "t,u", PIC_CALL_REG,
5141                                (int) BFD_RELOC_MIPS_CALL_HI16);
5142                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5143                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5144                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5145                                mips_gp_register);
5146                   macro_build ((char *) NULL, &icnt, &offset_expr,
5147                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5148                                "t,o(b)", PIC_CALL_REG,
5149                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5150                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5151                                "nop", "");
5152                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5153                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5154                                               8 + gpdel, 0, 0),
5155                                 offset_expr.X_add_symbol, 0, NULL);
5156                   if (gpdel > 0)
5157                     {
5158                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5159                       p += 4;
5160                     }
5161                   macro_build (p, &icnt, &offset_expr,
5162                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5163                                "t,o(b)", PIC_CALL_REG,
5164                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5165                   p += 4;
5166                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5167                   p += 4;
5168                 }
5169               macro_build (p, &icnt, &offset_expr,
5170                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5171                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5172                            (int) BFD_RELOC_LO16);
5173               macro_build_jalr (icnt, &offset_expr);
5174
5175               if (mips_cprestore_offset < 0)
5176                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5177               else
5178                 {
5179                   if (! mips_frame_reg_valid)
5180                     {
5181                       as_warn (_("No .frame pseudo-op used in PIC code"));
5182                       /* Quiet this warning.  */
5183                       mips_frame_reg_valid = 1;
5184                     }
5185                   if (! mips_cprestore_valid)
5186                     {
5187                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5188                       /* Quiet this warning.  */
5189                       mips_cprestore_valid = 1;
5190                     }
5191                   if (mips_opts.noreorder)
5192                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5193                                  "nop", "");
5194                   expr1.X_add_number = mips_cprestore_offset;
5195                   macro_build ((char *) NULL, &icnt, &expr1,
5196                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5197                                mips_gp_register, (int) BFD_RELOC_LO16,
5198                                mips_frame_reg);
5199                 }
5200             }
5201         }
5202       else if (mips_pic == EMBEDDED_PIC)
5203         {
5204           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5205           /* The linker may expand the call to a longer sequence which
5206              uses $at, so we must break rather than return.  */
5207           break;
5208         }
5209       else
5210         abort ();
5211
5212       return;
5213
5214     case M_LB_AB:
5215       s = "lb";
5216       goto ld;
5217     case M_LBU_AB:
5218       s = "lbu";
5219       goto ld;
5220     case M_LH_AB:
5221       s = "lh";
5222       goto ld;
5223     case M_LHU_AB:
5224       s = "lhu";
5225       goto ld;
5226     case M_LW_AB:
5227       s = "lw";
5228       goto ld;
5229     case M_LWC0_AB:
5230       s = "lwc0";
5231       /* Itbl support may require additional care here.  */
5232       coproc = 1;
5233       goto ld;
5234     case M_LWC1_AB:
5235       s = "lwc1";
5236       /* Itbl support may require additional care here.  */
5237       coproc = 1;
5238       goto ld;
5239     case M_LWC2_AB:
5240       s = "lwc2";
5241       /* Itbl support may require additional care here.  */
5242       coproc = 1;
5243       goto ld;
5244     case M_LWC3_AB:
5245       s = "lwc3";
5246       /* Itbl support may require additional care here.  */
5247       coproc = 1;
5248       goto ld;
5249     case M_LWL_AB:
5250       s = "lwl";
5251       lr = 1;
5252       goto ld;
5253     case M_LWR_AB:
5254       s = "lwr";
5255       lr = 1;
5256       goto ld;
5257     case M_LDC1_AB:
5258       if (mips_arch == CPU_R4650)
5259         {
5260           as_bad (_("opcode not supported on this processor"));
5261           return;
5262         }
5263       s = "ldc1";
5264       /* Itbl support may require additional care here.  */
5265       coproc = 1;
5266       goto ld;
5267     case M_LDC2_AB:
5268       s = "ldc2";
5269       /* Itbl support may require additional care here.  */
5270       coproc = 1;
5271       goto ld;
5272     case M_LDC3_AB:
5273       s = "ldc3";
5274       /* Itbl support may require additional care here.  */
5275       coproc = 1;
5276       goto ld;
5277     case M_LDL_AB:
5278       s = "ldl";
5279       lr = 1;
5280       goto ld;
5281     case M_LDR_AB:
5282       s = "ldr";
5283       lr = 1;
5284       goto ld;
5285     case M_LL_AB:
5286       s = "ll";
5287       goto ld;
5288     case M_LLD_AB:
5289       s = "lld";
5290       goto ld;
5291     case M_LWU_AB:
5292       s = "lwu";
5293     ld:
5294       if (breg == treg || coproc || lr)
5295         {
5296           tempreg = AT;
5297           used_at = 1;
5298         }
5299       else
5300         {
5301           tempreg = treg;
5302           used_at = 0;
5303         }
5304       goto ld_st;
5305     case M_SB_AB:
5306       s = "sb";
5307       goto st;
5308     case M_SH_AB:
5309       s = "sh";
5310       goto st;
5311     case M_SW_AB:
5312       s = "sw";
5313       goto st;
5314     case M_SWC0_AB:
5315       s = "swc0";
5316       /* Itbl support may require additional care here.  */
5317       coproc = 1;
5318       goto st;
5319     case M_SWC1_AB:
5320       s = "swc1";
5321       /* Itbl support may require additional care here.  */
5322       coproc = 1;
5323       goto st;
5324     case M_SWC2_AB:
5325       s = "swc2";
5326       /* Itbl support may require additional care here.  */
5327       coproc = 1;
5328       goto st;
5329     case M_SWC3_AB:
5330       s = "swc3";
5331       /* Itbl support may require additional care here.  */
5332       coproc = 1;
5333       goto st;
5334     case M_SWL_AB:
5335       s = "swl";
5336       goto st;
5337     case M_SWR_AB:
5338       s = "swr";
5339       goto st;
5340     case M_SC_AB:
5341       s = "sc";
5342       goto st;
5343     case M_SCD_AB:
5344       s = "scd";
5345       goto st;
5346     case M_SDC1_AB:
5347       if (mips_arch == CPU_R4650)
5348         {
5349           as_bad (_("opcode not supported on this processor"));
5350           return;
5351         }
5352       s = "sdc1";
5353       coproc = 1;
5354       /* Itbl support may require additional care here.  */
5355       goto st;
5356     case M_SDC2_AB:
5357       s = "sdc2";
5358       /* Itbl support may require additional care here.  */
5359       coproc = 1;
5360       goto st;
5361     case M_SDC3_AB:
5362       s = "sdc3";
5363       /* Itbl support may require additional care here.  */
5364       coproc = 1;
5365       goto st;
5366     case M_SDL_AB:
5367       s = "sdl";
5368       goto st;
5369     case M_SDR_AB:
5370       s = "sdr";
5371     st:
5372       tempreg = AT;
5373       used_at = 1;
5374     ld_st:
5375       /* Itbl support may require additional care here.  */
5376       if (mask == M_LWC1_AB
5377           || mask == M_SWC1_AB
5378           || mask == M_LDC1_AB
5379           || mask == M_SDC1_AB
5380           || mask == M_L_DAB
5381           || mask == M_S_DAB)
5382         fmt = "T,o(b)";
5383       else if (coproc)
5384         fmt = "E,o(b)";
5385       else
5386         fmt = "t,o(b)";
5387
5388       /* For embedded PIC, we allow loads where the offset is calculated
5389          by subtracting a symbol in the current segment from an unknown
5390          symbol, relative to a base register, e.g.:
5391                 <op>    $treg, <sym>-<localsym>($breg)
5392          This is used by the compiler for switch statements.  */
5393       if (mips_pic == EMBEDDED_PIC
5394           && offset_expr.X_op == O_subtract
5395           && (symbol_constant_p (offset_expr.X_op_symbol)
5396               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5397               : (symbol_equated_p (offset_expr.X_op_symbol)
5398                  && (S_GET_SEGMENT
5399                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5400                       ->X_add_symbol)
5401                      == now_seg)))
5402           && breg != 0
5403           && (offset_expr.X_add_number == 0
5404               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5405         {
5406           /* For this case, we output the instructions:
5407                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5408                 addiu   $tempreg,$tempreg,$breg
5409                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5410              If the relocation would fit entirely in 16 bits, it would be
5411              nice to emit:
5412                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5413              instead, but that seems quite difficult.  */
5414           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5415                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5416           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5417                        ((bfd_arch_bits_per_address (stdoutput) == 32
5418                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5419                         ? "addu" : "daddu"),
5420                        "d,v,t", tempreg, tempreg, breg);
5421           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5422                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5423           if (! used_at)
5424             return;
5425           break;
5426         }
5427
5428       if (offset_expr.X_op != O_constant
5429           && offset_expr.X_op != O_symbol)
5430         {
5431           as_bad (_("expression too complex"));
5432           offset_expr.X_op = O_constant;
5433         }
5434
5435       /* A constant expression in PIC code can be handled just as it
5436          is in non PIC code.  */
5437       if (mips_pic == NO_PIC
5438           || offset_expr.X_op == O_constant)
5439         {
5440           char *p;
5441
5442           /* If this is a reference to a GP relative symbol, and there
5443              is no base register, we want
5444                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5445              Otherwise, if there is no base register, we want
5446                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5447                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5448              If we have a constant, we need two instructions anyhow,
5449              so we always use the latter form.
5450
5451              If we have a base register, and this is a reference to a
5452              GP relative symbol, we want
5453                addu     $tempreg,$breg,$gp
5454                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5455              Otherwise we want
5456                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5457                addu     $tempreg,$tempreg,$breg
5458                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5459              With a constant we always use the latter case.
5460
5461              With 64bit address space and no base register and $at usable,
5462              we want
5463                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5464                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5465                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5466                dsll32   $tempreg,0
5467                daddu    $tempreg,$at
5468                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5469              If we have a base register, we want
5470                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5471                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5472                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5473                daddu    $at,$breg
5474                dsll32   $tempreg,0
5475                daddu    $tempreg,$at
5476                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5477
5478              Without $at we can't generate the optimal path for superscalar
5479              processors here since this would require two temporary registers.
5480                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5481                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5482                dsll     $tempreg,16
5483                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5484                dsll     $tempreg,16
5485                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5486              If we have a base register, we want
5487                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5488                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5489                dsll     $tempreg,16
5490                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5491                dsll     $tempreg,16
5492                daddu    $tempreg,$tempreg,$breg
5493                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5494
5495              If we have 64-bit addresses, as an optimization, for
5496              addresses which are 32-bit constants (e.g. kseg0/kseg1
5497              addresses) we fall back to the 32-bit address generation
5498              mechanism since it is more efficient.  This code should
5499              probably attempt to generate 64-bit constants more
5500              efficiently in general.
5501            */
5502           if (HAVE_64BIT_ADDRESSES
5503               && !(offset_expr.X_op == O_constant
5504                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
5505             {
5506               p = NULL;
5507
5508               /* We don't do GP optimization for now because RELAX_ENCODE can't
5509                  hold the data for such large chunks.  */
5510
5511               if (used_at == 0)
5512                 {
5513                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5514                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5515                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5516                                AT, (int) BFD_RELOC_HI16_S);
5517                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5518                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5519                   if (breg != 0)
5520                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5521                                  "d,v,t", AT, AT, breg);
5522                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5523                                "d,w,<", tempreg, tempreg, 0);
5524                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5525                                "d,v,t", tempreg, tempreg, AT);
5526                   macro_build (p, &icnt, &offset_expr, s,
5527                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5528                   used_at = 1;
5529                 }
5530               else
5531                 {
5532                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5533                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5534                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5535                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5536                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5537                                "d,w,<", tempreg, tempreg, 16);
5538                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5539                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5540                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5541                                "d,w,<", tempreg, tempreg, 16);
5542                   if (breg != 0)
5543                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5544                                  "d,v,t", tempreg, tempreg, breg);
5545                   macro_build (p, &icnt, &offset_expr, s,
5546                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5547                 }
5548
5549               return;
5550             }
5551
5552           if (breg == 0)
5553             {
5554               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5555                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5556                 p = NULL;
5557               else
5558                 {
5559                   frag_grow (20);
5560                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5561                                treg, (int) BFD_RELOC_GPREL16,
5562                                mips_gp_register);
5563                   p = frag_var (rs_machine_dependent, 8, 0,
5564                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5565                                               (mips_opts.warn_about_macros
5566                                                || (used_at
5567                                                    && mips_opts.noat))),
5568                                 offset_expr.X_add_symbol, 0, NULL);
5569                   used_at = 0;
5570                 }
5571               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5572               if (p != NULL)
5573                 p += 4;
5574               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5575                            (int) BFD_RELOC_LO16, tempreg);
5576             }
5577           else
5578             {
5579               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5580                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5581                 p = NULL;
5582               else
5583                 {
5584                   frag_grow (28);
5585                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5586                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5587                                "d,v,t", tempreg, breg, mips_gp_register);
5588                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5589                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5590                   p = frag_var (rs_machine_dependent, 12, 0,
5591                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5592                                 offset_expr.X_add_symbol, 0, NULL);
5593                 }
5594               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5595               if (p != NULL)
5596                 p += 4;
5597               macro_build (p, &icnt, (expressionS *) NULL,
5598                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5599                            "d,v,t", tempreg, tempreg, breg);
5600               if (p != NULL)
5601                 p += 4;
5602               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5603                            (int) BFD_RELOC_LO16, tempreg);
5604             }
5605         }
5606       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5607         {
5608           char *p;
5609
5610           /* If this is a reference to an external symbol, we want
5611                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5612                nop
5613                <op>     $treg,0($tempreg)
5614              Otherwise we want
5615                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5616                nop
5617                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5618                <op>     $treg,0($tempreg)
5619              If there is a base register, we add it to $tempreg before
5620              the <op>.  If there is a constant, we stick it in the
5621              <op> instruction.  We don't handle constants larger than
5622              16 bits, because we have no way to load the upper 16 bits
5623              (actually, we could handle them for the subset of cases
5624              in which we are not using $at).  */
5625           assert (offset_expr.X_op == O_symbol);
5626           expr1.X_add_number = offset_expr.X_add_number;
5627           offset_expr.X_add_number = 0;
5628           if (expr1.X_add_number < -0x8000
5629               || expr1.X_add_number >= 0x8000)
5630             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5631           frag_grow (20);
5632           macro_build ((char *) NULL, &icnt, &offset_expr,
5633                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5634                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5635           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5636           p = frag_var (rs_machine_dependent, 4, 0,
5637                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5638                         offset_expr.X_add_symbol, 0, NULL);
5639           macro_build (p, &icnt, &offset_expr,
5640                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5641                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5642           if (breg != 0)
5643             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5644                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5645                          "d,v,t", tempreg, tempreg, breg);
5646           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5647                        (int) BFD_RELOC_LO16, tempreg);
5648         }
5649       else if (mips_pic == SVR4_PIC)
5650         {
5651           int gpdel;
5652           char *p;
5653
5654           /* If this is a reference to an external symbol, we want
5655                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5656                addu     $tempreg,$tempreg,$gp
5657                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5658                <op>     $treg,0($tempreg)
5659              Otherwise we want
5660                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5661                nop
5662                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5663                <op>     $treg,0($tempreg)
5664              If there is a base register, we add it to $tempreg before
5665              the <op>.  If there is a constant, we stick it in the
5666              <op> instruction.  We don't handle constants larger than
5667              16 bits, because we have no way to load the upper 16 bits
5668              (actually, we could handle them for the subset of cases
5669              in which we are not using $at).
5670
5671              For NewABI, we want
5672                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5673                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5674                <op>     $treg,0($tempreg)
5675            */
5676           assert (offset_expr.X_op == O_symbol);
5677           expr1.X_add_number = offset_expr.X_add_number;
5678           offset_expr.X_add_number = 0;
5679           if (expr1.X_add_number < -0x8000
5680               || expr1.X_add_number >= 0x8000)
5681             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5682           if (HAVE_NEWABI)
5683             {
5684               macro_build ((char *) NULL, &icnt, &offset_expr,
5685                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5686                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5687                            mips_gp_register);
5688               macro_build ((char *) NULL, &icnt, &offset_expr,
5689                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5690                            "t,r,j", tempreg, tempreg,
5691                            BFD_RELOC_MIPS_GOT_OFST);
5692               if (breg != 0)
5693                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5694                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5695                              "d,v,t", tempreg, tempreg, breg);
5696               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5697                            (int) BFD_RELOC_LO16, tempreg);
5698
5699               if (! used_at)
5700                 return;
5701
5702               break;
5703             }
5704           if (reg_needs_delay (mips_gp_register))
5705             gpdel = 4;
5706           else
5707             gpdel = 0;
5708           frag_grow (36);
5709           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5710                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5711           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5712                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5713                        "d,v,t", tempreg, tempreg, mips_gp_register);
5714           macro_build ((char *) NULL, &icnt, &offset_expr,
5715                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5716                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5717                        tempreg);
5718           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5719                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5720                         offset_expr.X_add_symbol, 0, NULL);
5721           if (gpdel > 0)
5722             {
5723               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5724               p += 4;
5725             }
5726           macro_build (p, &icnt, &offset_expr,
5727                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5728                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5729                        mips_gp_register);
5730           p += 4;
5731           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5732           p += 4;
5733           macro_build (p, &icnt, &offset_expr,
5734                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5735                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5736           if (breg != 0)
5737             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5738                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5739                          "d,v,t", tempreg, tempreg, breg);
5740           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5741                        (int) BFD_RELOC_LO16, tempreg);
5742         }
5743       else if (mips_pic == EMBEDDED_PIC)
5744         {
5745           /* If there is no base register, we want
5746                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5747              If there is a base register, we want
5748                addu     $tempreg,$breg,$gp
5749                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5750              */
5751           assert (offset_expr.X_op == O_symbol);
5752           if (breg == 0)
5753             {
5754               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5755                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5756               used_at = 0;
5757             }
5758           else
5759             {
5760               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5761                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5762                            "d,v,t", tempreg, breg, mips_gp_register);
5763               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5764                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5765             }
5766         }
5767       else
5768         abort ();
5769
5770       if (! used_at)
5771         return;
5772
5773       break;
5774
5775     case M_LI:
5776     case M_LI_S:
5777       load_register (&icnt, treg, &imm_expr, 0);
5778       return;
5779
5780     case M_DLI:
5781       load_register (&icnt, treg, &imm_expr, 1);
5782       return;
5783
5784     case M_LI_SS:
5785       if (imm_expr.X_op == O_constant)
5786         {
5787           load_register (&icnt, AT, &imm_expr, 0);
5788           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5789                        "mtc1", "t,G", AT, treg);
5790           break;
5791         }
5792       else
5793         {
5794           assert (offset_expr.X_op == O_symbol
5795                   && strcmp (segment_name (S_GET_SEGMENT
5796                                            (offset_expr.X_add_symbol)),
5797                              ".lit4") == 0
5798                   && offset_expr.X_add_number == 0);
5799           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5800                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
5801           return;
5802         }
5803
5804     case M_LI_D:
5805       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5806          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5807          order 32 bits of the value and the low order 32 bits are either
5808          zero or in OFFSET_EXPR.  */
5809       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5810         {
5811           if (HAVE_64BIT_GPRS)
5812             load_register (&icnt, treg, &imm_expr, 1);
5813           else
5814             {
5815               int hreg, lreg;
5816
5817               if (target_big_endian)
5818                 {
5819                   hreg = treg;
5820                   lreg = treg + 1;
5821                 }
5822               else
5823                 {
5824                   hreg = treg + 1;
5825                   lreg = treg;
5826                 }
5827
5828               if (hreg <= 31)
5829                 load_register (&icnt, hreg, &imm_expr, 0);
5830               if (lreg <= 31)
5831                 {
5832                   if (offset_expr.X_op == O_absent)
5833                     move_register (&icnt, lreg, 0);
5834                   else
5835                     {
5836                       assert (offset_expr.X_op == O_constant);
5837                       load_register (&icnt, lreg, &offset_expr, 0);
5838                     }
5839                 }
5840             }
5841           return;
5842         }
5843
5844       /* We know that sym is in the .rdata section.  First we get the
5845          upper 16 bits of the address.  */
5846       if (mips_pic == NO_PIC)
5847         {
5848           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5849         }
5850       else if (mips_pic == SVR4_PIC)
5851         {
5852           macro_build ((char *) NULL, &icnt, &offset_expr,
5853                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5854                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5855                        mips_gp_register);
5856         }
5857       else if (mips_pic == EMBEDDED_PIC)
5858         {
5859           /* For embedded PIC we pick up the entire address off $gp in
5860              a single instruction.  */
5861           macro_build ((char *) NULL, &icnt, &offset_expr,
5862                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5863                        mips_gp_register, (int) BFD_RELOC_GPREL16);
5864           offset_expr.X_op = O_constant;
5865           offset_expr.X_add_number = 0;
5866         }
5867       else
5868         abort ();
5869
5870       /* Now we load the register(s).  */
5871       if (HAVE_64BIT_GPRS)
5872         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5873                      treg, (int) BFD_RELOC_LO16, AT);
5874       else
5875         {
5876           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5877                        treg, (int) BFD_RELOC_LO16, AT);
5878           if (treg != RA)
5879             {
5880               /* FIXME: How in the world do we deal with the possible
5881                  overflow here?  */
5882               offset_expr.X_add_number += 4;
5883               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5884                            treg + 1, (int) BFD_RELOC_LO16, AT);
5885             }
5886         }
5887
5888       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5889          does not become a variant frag.  */
5890       frag_wane (frag_now);
5891       frag_new (0);
5892
5893       break;
5894
5895     case M_LI_DD:
5896       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5897          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5898          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5899          the value and the low order 32 bits are either zero or in
5900          OFFSET_EXPR.  */
5901       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5902         {
5903           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5904           if (HAVE_64BIT_FPRS)
5905             {
5906               assert (HAVE_64BIT_GPRS);
5907               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5908                            "dmtc1", "t,S", AT, treg);
5909             }
5910           else
5911             {
5912               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5913                            "mtc1", "t,G", AT, treg + 1);
5914               if (offset_expr.X_op == O_absent)
5915                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5916                              "mtc1", "t,G", 0, treg);
5917               else
5918                 {
5919                   assert (offset_expr.X_op == O_constant);
5920                   load_register (&icnt, AT, &offset_expr, 0);
5921                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5922                                "mtc1", "t,G", AT, treg);
5923                 }
5924             }
5925           break;
5926         }
5927
5928       assert (offset_expr.X_op == O_symbol
5929               && offset_expr.X_add_number == 0);
5930       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5931       if (strcmp (s, ".lit8") == 0)
5932         {
5933           if (mips_opts.isa != ISA_MIPS1)
5934             {
5935               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5936                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5937                            mips_gp_register);
5938               return;
5939             }
5940           breg = mips_gp_register;
5941           r = BFD_RELOC_MIPS_LITERAL;
5942           goto dob;
5943         }
5944       else
5945         {
5946           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5947           if (mips_pic == SVR4_PIC)
5948             macro_build ((char *) NULL, &icnt, &offset_expr,
5949                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5950                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5951                          mips_gp_register);
5952           else
5953             {
5954               /* FIXME: This won't work for a 64 bit address.  */
5955               macro_build_lui (NULL, &icnt, &offset_expr, AT);
5956             }
5957
5958           if (mips_opts.isa != ISA_MIPS1)
5959             {
5960               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5961                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5962
5963               /* To avoid confusion in tc_gen_reloc, we must ensure
5964                  that this does not become a variant frag.  */
5965               frag_wane (frag_now);
5966               frag_new (0);
5967
5968               break;
5969             }
5970           breg = AT;
5971           r = BFD_RELOC_LO16;
5972           goto dob;
5973         }
5974
5975     case M_L_DOB:
5976       if (mips_arch == CPU_R4650)
5977         {
5978           as_bad (_("opcode not supported on this processor"));
5979           return;
5980         }
5981       /* Even on a big endian machine $fn comes before $fn+1.  We have
5982          to adjust when loading from memory.  */
5983       r = BFD_RELOC_LO16;
5984     dob:
5985       assert (mips_opts.isa == ISA_MIPS1);
5986       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5987                    target_big_endian ? treg + 1 : treg,
5988                    (int) r, breg);
5989       /* FIXME: A possible overflow which I don't know how to deal
5990          with.  */
5991       offset_expr.X_add_number += 4;
5992       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5993                    target_big_endian ? treg : treg + 1,
5994                    (int) r, breg);
5995
5996       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5997          does not become a variant frag.  */
5998       frag_wane (frag_now);
5999       frag_new (0);
6000
6001       if (breg != AT)
6002         return;
6003       break;
6004
6005     case M_L_DAB:
6006       /*
6007        * The MIPS assembler seems to check for X_add_number not
6008        * being double aligned and generating:
6009        *        lui     at,%hi(foo+1)
6010        *        addu    at,at,v1
6011        *        addiu   at,at,%lo(foo+1)
6012        *        lwc1    f2,0(at)
6013        *        lwc1    f3,4(at)
6014        * But, the resulting address is the same after relocation so why
6015        * generate the extra instruction?
6016        */
6017       if (mips_arch == CPU_R4650)
6018         {
6019           as_bad (_("opcode not supported on this processor"));
6020           return;
6021         }
6022       /* Itbl support may require additional care here.  */
6023       coproc = 1;
6024       if (mips_opts.isa != ISA_MIPS1)
6025         {
6026           s = "ldc1";
6027           goto ld;
6028         }
6029
6030       s = "lwc1";
6031       fmt = "T,o(b)";
6032       goto ldd_std;
6033
6034     case M_S_DAB:
6035       if (mips_arch == CPU_R4650)
6036         {
6037           as_bad (_("opcode not supported on this processor"));
6038           return;
6039         }
6040
6041       if (mips_opts.isa != ISA_MIPS1)
6042         {
6043           s = "sdc1";
6044           goto st;
6045         }
6046
6047       s = "swc1";
6048       fmt = "T,o(b)";
6049       /* Itbl support may require additional care here.  */
6050       coproc = 1;
6051       goto ldd_std;
6052
6053     case M_LD_AB:
6054       if (HAVE_64BIT_GPRS)
6055         {
6056           s = "ld";
6057           goto ld;
6058         }
6059
6060       s = "lw";
6061       fmt = "t,o(b)";
6062       goto ldd_std;
6063
6064     case M_SD_AB:
6065       if (HAVE_64BIT_GPRS)
6066         {
6067           s = "sd";
6068           goto st;
6069         }
6070
6071       s = "sw";
6072       fmt = "t,o(b)";
6073
6074     ldd_std:
6075       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6076          loads for the case of doing a pair of loads to simulate an 'ld'.
6077          This is not currently done by the compiler, and assembly coders
6078          writing embedded-pic code can cope.  */
6079
6080       if (offset_expr.X_op != O_symbol
6081           && offset_expr.X_op != O_constant)
6082         {
6083           as_bad (_("expression too complex"));
6084           offset_expr.X_op = O_constant;
6085         }
6086
6087       /* Even on a big endian machine $fn comes before $fn+1.  We have
6088          to adjust when loading from memory.  We set coproc if we must
6089          load $fn+1 first.  */
6090       /* Itbl support may require additional care here.  */
6091       if (! target_big_endian)
6092         coproc = 0;
6093
6094       if (mips_pic == NO_PIC
6095           || offset_expr.X_op == O_constant)
6096         {
6097           char *p;
6098
6099           /* If this is a reference to a GP relative symbol, we want
6100                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6101                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6102              If we have a base register, we use this
6103                addu     $at,$breg,$gp
6104                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6105                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6106              If this is not a GP relative symbol, we want
6107                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6108                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6109                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6110              If there is a base register, we add it to $at after the
6111              lui instruction.  If there is a constant, we always use
6112              the last case.  */
6113           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6114               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6115             {
6116               p = NULL;
6117               used_at = 1;
6118             }
6119           else
6120             {
6121               int off;
6122
6123               if (breg == 0)
6124                 {
6125                   frag_grow (28);
6126                   tempreg = mips_gp_register;
6127                   off = 0;
6128                   used_at = 0;
6129                 }
6130               else
6131                 {
6132                   frag_grow (36);
6133                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6134                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6135                                "d,v,t", AT, breg, mips_gp_register);
6136                   tempreg = AT;
6137                   off = 4;
6138                   used_at = 1;
6139                 }
6140
6141               /* Itbl support may require additional care here.  */
6142               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6143                            coproc ? treg + 1 : treg,
6144                            (int) BFD_RELOC_GPREL16, tempreg);
6145               offset_expr.X_add_number += 4;
6146
6147               /* Set mips_optimize to 2 to avoid inserting an
6148                  undesired nop.  */
6149               hold_mips_optimize = mips_optimize;
6150               mips_optimize = 2;
6151               /* Itbl support may require additional care here.  */
6152               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6153                            coproc ? treg : treg + 1,
6154                            (int) BFD_RELOC_GPREL16, tempreg);
6155               mips_optimize = hold_mips_optimize;
6156
6157               p = frag_var (rs_machine_dependent, 12 + off, 0,
6158                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6159                                           used_at && mips_opts.noat),
6160                             offset_expr.X_add_symbol, 0, NULL);
6161
6162               /* We just generated two relocs.  When tc_gen_reloc
6163                  handles this case, it will skip the first reloc and
6164                  handle the second.  The second reloc already has an
6165                  extra addend of 4, which we added above.  We must
6166                  subtract it out, and then subtract another 4 to make
6167                  the first reloc come out right.  The second reloc
6168                  will come out right because we are going to add 4 to
6169                  offset_expr when we build its instruction below.
6170
6171                  If we have a symbol, then we don't want to include
6172                  the offset, because it will wind up being included
6173                  when we generate the reloc.  */
6174
6175               if (offset_expr.X_op == O_constant)
6176                 offset_expr.X_add_number -= 8;
6177               else
6178                 {
6179                   offset_expr.X_add_number = -4;
6180                   offset_expr.X_op = O_constant;
6181                 }
6182             }
6183           macro_build_lui (p, &icnt, &offset_expr, AT);
6184           if (p != NULL)
6185             p += 4;
6186           if (breg != 0)
6187             {
6188               macro_build (p, &icnt, (expressionS *) NULL,
6189                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6190                            "d,v,t", AT, breg, AT);
6191               if (p != NULL)
6192                 p += 4;
6193             }
6194           /* Itbl support may require additional care here.  */
6195           macro_build (p, &icnt, &offset_expr, s, fmt,
6196                        coproc ? treg + 1 : treg,
6197                        (int) BFD_RELOC_LO16, AT);
6198           if (p != NULL)
6199             p += 4;
6200           /* FIXME: How do we handle overflow here?  */
6201           offset_expr.X_add_number += 4;
6202           /* Itbl support may require additional care here.  */
6203           macro_build (p, &icnt, &offset_expr, s, fmt,
6204                        coproc ? treg : treg + 1,
6205                        (int) BFD_RELOC_LO16, AT);
6206         }
6207       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6208         {
6209           int off;
6210
6211           /* If this is a reference to an external symbol, we want
6212                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6213                nop
6214                <op>     $treg,0($at)
6215                <op>     $treg+1,4($at)
6216              Otherwise we want
6217                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6218                nop
6219                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6220                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6221              If there is a base register we add it to $at before the
6222              lwc1 instructions.  If there is a constant we include it
6223              in the lwc1 instructions.  */
6224           used_at = 1;
6225           expr1.X_add_number = offset_expr.X_add_number;
6226           offset_expr.X_add_number = 0;
6227           if (expr1.X_add_number < -0x8000
6228               || expr1.X_add_number >= 0x8000 - 4)
6229             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6230           if (breg == 0)
6231             off = 0;
6232           else
6233             off = 4;
6234           frag_grow (24 + off);
6235           macro_build ((char *) NULL, &icnt, &offset_expr,
6236                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6237                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6238           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6239           if (breg != 0)
6240             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6241                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6242                          "d,v,t", AT, breg, AT);
6243           /* Itbl support may require additional care here.  */
6244           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6245                        coproc ? treg + 1 : treg,
6246                        (int) BFD_RELOC_LO16, AT);
6247           expr1.X_add_number += 4;
6248
6249           /* Set mips_optimize to 2 to avoid inserting an undesired
6250              nop.  */
6251           hold_mips_optimize = mips_optimize;
6252           mips_optimize = 2;
6253           /* Itbl support may require additional care here.  */
6254           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6255                        coproc ? treg : treg + 1,
6256                        (int) BFD_RELOC_LO16, AT);
6257           mips_optimize = hold_mips_optimize;
6258
6259           (void) frag_var (rs_machine_dependent, 0, 0,
6260                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6261                            offset_expr.X_add_symbol, 0, NULL);
6262         }
6263       else if (mips_pic == SVR4_PIC)
6264         {
6265           int gpdel, off;
6266           char *p;
6267
6268           /* If this is a reference to an external symbol, we want
6269                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6270                addu     $at,$at,$gp
6271                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6272                nop
6273                <op>     $treg,0($at)
6274                <op>     $treg+1,4($at)
6275              Otherwise we want
6276                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6277                nop
6278                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6279                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6280              If there is a base register we add it to $at before the
6281              lwc1 instructions.  If there is a constant we include it
6282              in the lwc1 instructions.  */
6283           used_at = 1;
6284           expr1.X_add_number = offset_expr.X_add_number;
6285           offset_expr.X_add_number = 0;
6286           if (expr1.X_add_number < -0x8000
6287               || expr1.X_add_number >= 0x8000 - 4)
6288             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6289           if (reg_needs_delay (mips_gp_register))
6290             gpdel = 4;
6291           else
6292             gpdel = 0;
6293           if (breg == 0)
6294             off = 0;
6295           else
6296             off = 4;
6297           frag_grow (56);
6298           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6299                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6300           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6301                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6302                        "d,v,t", AT, AT, mips_gp_register);
6303           macro_build ((char *) NULL, &icnt, &offset_expr,
6304                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6305                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6306           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6307           if (breg != 0)
6308             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6309                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6310                          "d,v,t", AT, breg, AT);
6311           /* Itbl support may require additional care here.  */
6312           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6313                        coproc ? treg + 1 : treg,
6314                        (int) BFD_RELOC_LO16, AT);
6315           expr1.X_add_number += 4;
6316
6317           /* Set mips_optimize to 2 to avoid inserting an undesired
6318              nop.  */
6319           hold_mips_optimize = mips_optimize;
6320           mips_optimize = 2;
6321           /* Itbl support may require additional care here.  */
6322           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6323                        coproc ? treg : treg + 1,
6324                        (int) BFD_RELOC_LO16, AT);
6325           mips_optimize = hold_mips_optimize;
6326           expr1.X_add_number -= 4;
6327
6328           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6329                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6330                                       8 + gpdel + off, 1, 0),
6331                         offset_expr.X_add_symbol, 0, NULL);
6332           if (gpdel > 0)
6333             {
6334               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6335               p += 4;
6336             }
6337           macro_build (p, &icnt, &offset_expr,
6338                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6339                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6340                        mips_gp_register);
6341           p += 4;
6342           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6343           p += 4;
6344           if (breg != 0)
6345             {
6346               macro_build (p, &icnt, (expressionS *) NULL,
6347                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6348                            "d,v,t", AT, breg, AT);
6349               p += 4;
6350             }
6351           /* Itbl support may require additional care here.  */
6352           macro_build (p, &icnt, &expr1, s, fmt,
6353                        coproc ? treg + 1 : treg,
6354                        (int) BFD_RELOC_LO16, AT);
6355           p += 4;
6356           expr1.X_add_number += 4;
6357
6358           /* Set mips_optimize to 2 to avoid inserting an undesired
6359              nop.  */
6360           hold_mips_optimize = mips_optimize;
6361           mips_optimize = 2;
6362           /* Itbl support may require additional care here.  */
6363           macro_build (p, &icnt, &expr1, s, fmt,
6364                        coproc ? treg : treg + 1,
6365                        (int) BFD_RELOC_LO16, AT);
6366           mips_optimize = hold_mips_optimize;
6367         }
6368       else if (mips_pic == EMBEDDED_PIC)
6369         {
6370           /* If there is no base register, we use
6371                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6372                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6373              If we have a base register, we use
6374                addu     $at,$breg,$gp
6375                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6376                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6377              */
6378           if (breg == 0)
6379             {
6380               tempreg = mips_gp_register;
6381               used_at = 0;
6382             }
6383           else
6384             {
6385               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6386                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6387                            "d,v,t", AT, breg, mips_gp_register);
6388               tempreg = AT;
6389               used_at = 1;
6390             }
6391
6392           /* Itbl support may require additional care here.  */
6393           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6394                        coproc ? treg + 1 : treg,
6395                        (int) BFD_RELOC_GPREL16, tempreg);
6396           offset_expr.X_add_number += 4;
6397           /* Itbl support may require additional care here.  */
6398           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6399                        coproc ? treg : treg + 1,
6400                        (int) BFD_RELOC_GPREL16, tempreg);
6401         }
6402       else
6403         abort ();
6404
6405       if (! used_at)
6406         return;
6407
6408       break;
6409
6410     case M_LD_OB:
6411       s = "lw";
6412       goto sd_ob;
6413     case M_SD_OB:
6414       s = "sw";
6415     sd_ob:
6416       assert (HAVE_32BIT_ADDRESSES);
6417       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6418                    (int) BFD_RELOC_LO16, breg);
6419       offset_expr.X_add_number += 4;
6420       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6421                    (int) BFD_RELOC_LO16, breg);
6422       return;
6423
6424    /* New code added to support COPZ instructions.
6425       This code builds table entries out of the macros in mip_opcodes.
6426       R4000 uses interlocks to handle coproc delays.
6427       Other chips (like the R3000) require nops to be inserted for delays.
6428
6429       FIXME: Currently, we require that the user handle delays.
6430       In order to fill delay slots for non-interlocked chips,
6431       we must have a way to specify delays based on the coprocessor.
6432       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6433       What are the side-effects of the cop instruction?
6434       What cache support might we have and what are its effects?
6435       Both coprocessor & memory require delays. how long???
6436       What registers are read/set/modified?
6437
6438       If an itbl is provided to interpret cop instructions,
6439       this knowledge can be encoded in the itbl spec.  */
6440
6441     case M_COP0:
6442       s = "c0";
6443       goto copz;
6444     case M_COP1:
6445       s = "c1";
6446       goto copz;
6447     case M_COP2:
6448       s = "c2";
6449       goto copz;
6450     case M_COP3:
6451       s = "c3";
6452     copz:
6453       /* For now we just do C (same as Cz).  The parameter will be
6454          stored in insn_opcode by mips_ip.  */
6455       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6456                    ip->insn_opcode);
6457       return;
6458
6459     case M_MOVE:
6460       move_register (&icnt, dreg, sreg);
6461       return;
6462
6463 #ifdef LOSING_COMPILER
6464     default:
6465       /* Try and see if this is a new itbl instruction.
6466          This code builds table entries out of the macros in mip_opcodes.
6467          FIXME: For now we just assemble the expression and pass it's
6468          value along as a 32-bit immediate.
6469          We may want to have the assembler assemble this value,
6470          so that we gain the assembler's knowledge of delay slots,
6471          symbols, etc.
6472          Would it be more efficient to use mask (id) here? */
6473       if (itbl_have_entries
6474           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6475         {
6476           s = ip->insn_mo->name;
6477           s2 = "cop3";
6478           coproc = ITBL_DECODE_PNUM (immed_expr);;
6479           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6480           return;
6481         }
6482       macro2 (ip);
6483       return;
6484     }
6485   if (mips_opts.noat)
6486     as_warn (_("Macro used $at after \".set noat\""));
6487 }
6488
6489 static void
6490 macro2 (ip)
6491      struct mips_cl_insn *ip;
6492 {
6493   register int treg, sreg, dreg, breg;
6494   int tempreg;
6495   int mask;
6496   int icnt = 0;
6497   int used_at;
6498   expressionS expr1;
6499   const char *s;
6500   const char *s2;
6501   const char *fmt;
6502   int likely = 0;
6503   int dbl = 0;
6504   int coproc = 0;
6505   int lr = 0;
6506   int imm = 0;
6507   int off;
6508   offsetT maxnum;
6509   bfd_reloc_code_real_type r;
6510   char *p;
6511
6512   treg = (ip->insn_opcode >> 16) & 0x1f;
6513   dreg = (ip->insn_opcode >> 11) & 0x1f;
6514   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6515   mask = ip->insn_mo->mask;
6516
6517   expr1.X_op = O_constant;
6518   expr1.X_op_symbol = NULL;
6519   expr1.X_add_symbol = NULL;
6520   expr1.X_add_number = 1;
6521
6522   switch (mask)
6523     {
6524 #endif /* LOSING_COMPILER */
6525
6526     case M_DMUL:
6527       dbl = 1;
6528     case M_MUL:
6529       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6530                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6531       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6532                    dreg);
6533       return;
6534
6535     case M_DMUL_I:
6536       dbl = 1;
6537     case M_MUL_I:
6538       /* The MIPS assembler some times generates shifts and adds.  I'm
6539          not trying to be that fancy. GCC should do this for us
6540          anyway.  */
6541       load_register (&icnt, AT, &imm_expr, dbl);
6542       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6543                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6544       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6545                    dreg);
6546       break;
6547
6548     case M_DMULO_I:
6549       dbl = 1;
6550     case M_MULO_I:
6551       imm = 1;
6552       goto do_mulo;
6553
6554     case M_DMULO:
6555       dbl = 1;
6556     case M_MULO:
6557     do_mulo:
6558       mips_emit_delays (true);
6559       ++mips_opts.noreorder;
6560       mips_any_noreorder = 1;
6561       if (imm)
6562         load_register (&icnt, AT, &imm_expr, dbl);
6563       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6564                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6565       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6566                    dreg);
6567       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6568                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6569       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6570                    AT);
6571       if (mips_trap)
6572         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6573                      dreg, AT);
6574       else
6575         {
6576           expr1.X_add_number = 8;
6577           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6578                        AT);
6579           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6580                        0);
6581           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6582                        "c", 6);
6583         }
6584       --mips_opts.noreorder;
6585       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6586       break;
6587
6588     case M_DMULOU_I:
6589       dbl = 1;
6590     case M_MULOU_I:
6591       imm = 1;
6592       goto do_mulou;
6593
6594     case M_DMULOU:
6595       dbl = 1;
6596     case M_MULOU:
6597     do_mulou:
6598       mips_emit_delays (true);
6599       ++mips_opts.noreorder;
6600       mips_any_noreorder = 1;
6601       if (imm)
6602         load_register (&icnt, AT, &imm_expr, dbl);
6603       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6604                    dbl ? "dmultu" : "multu",
6605                    "s,t", sreg, imm ? AT : treg);
6606       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6607                    AT);
6608       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6609                    dreg);
6610       if (mips_trap)
6611         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6612                      AT, 0);
6613       else
6614         {
6615           expr1.X_add_number = 8;
6616           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6617           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6618                        0);
6619           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6620                        "c", 6);
6621         }
6622       --mips_opts.noreorder;
6623       break;
6624
6625     case M_DROL:
6626       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6627                    "d,v,t", AT, 0, treg);
6628       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6629                    "d,t,s", AT, sreg, AT);
6630       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6631                    "d,t,s", dreg, sreg, treg);
6632       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6633                    "d,v,t", dreg, dreg, AT);
6634       break;
6635
6636     case M_ROL:
6637       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6638                    "d,v,t", AT, 0, treg);
6639       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6640                    "d,t,s", AT, sreg, AT);
6641       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6642                    "d,t,s", dreg, sreg, treg);
6643       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6644                    "d,v,t", dreg, dreg, AT);
6645       break;
6646
6647     case M_DROL_I:
6648       {
6649         unsigned int rot;
6650         char *l, *r;
6651
6652         if (imm_expr.X_op != O_constant)
6653           as_bad (_("rotate count too large"));
6654         rot = imm_expr.X_add_number & 0x3f;
6655         if (! rot)
6656           break;
6657         l = (rot < 0x20) ? "dsll" : "dsll32";
6658         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6659         rot &= 0x1f;
6660         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6661                      "d,w,<", AT, sreg, rot);
6662         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
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_ROL_I:
6670       {
6671         unsigned int rot;
6672
6673         if (imm_expr.X_op != O_constant)
6674           as_bad (_("rotate count too large"));
6675         rot = imm_expr.X_add_number & 0x1f;
6676         if (! rot)
6677           break;
6678         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6679                      "d,w,<", AT, sreg, rot);
6680         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6681                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6682         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6683                      "d,v,t", dreg, dreg, AT);
6684       }
6685       break;
6686
6687     case M_DROR:
6688       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6689                    "d,v,t", AT, 0, treg);
6690       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6691                    "d,t,s", AT, sreg, AT);
6692       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6693                    "d,t,s", dreg, sreg, treg);
6694       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6695                    "d,v,t", dreg, dreg, AT);
6696       break;
6697
6698     case M_ROR:
6699       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6700                    "d,v,t", AT, 0, treg);
6701       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6702                    "d,t,s", AT, sreg, AT);
6703       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6704                    "d,t,s", dreg, sreg, treg);
6705       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6706                    "d,v,t", dreg, dreg, AT);
6707       break;
6708
6709     case M_DROR_I:
6710       {
6711         unsigned int rot;
6712         char *l, *r;
6713
6714         if (imm_expr.X_op != O_constant)
6715           as_bad (_("rotate count too large"));
6716         rot = imm_expr.X_add_number & 0x3f;
6717         if (! rot)
6718           break;
6719         r = (rot < 0x20) ? "dsrl" : "dsrl32";
6720         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6721         rot &= 0x1f;
6722         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6723                      "d,w,<", AT, sreg, rot);
6724         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
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_ROR_I:
6732       {
6733         unsigned int rot;
6734
6735         if (imm_expr.X_op != O_constant)
6736           as_bad (_("rotate count too large"));
6737         rot = imm_expr.X_add_number & 0x1f;
6738         if (! rot)
6739           break;
6740         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6741                      "d,w,<", AT, sreg, rot);
6742         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6743                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6744         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6745                      "d,v,t", dreg, dreg, AT);
6746       }
6747       break;
6748
6749     case M_S_DOB:
6750       if (mips_arch == CPU_R4650)
6751         {
6752           as_bad (_("opcode not supported on this processor"));
6753           return;
6754         }
6755       assert (mips_opts.isa == ISA_MIPS1);
6756       /* Even on a big endian machine $fn comes before $fn+1.  We have
6757          to adjust when storing to memory.  */
6758       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6759                    target_big_endian ? treg + 1 : treg,
6760                    (int) BFD_RELOC_LO16, breg);
6761       offset_expr.X_add_number += 4;
6762       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6763                    target_big_endian ? treg : treg + 1,
6764                    (int) BFD_RELOC_LO16, breg);
6765       return;
6766
6767     case M_SEQ:
6768       if (sreg == 0)
6769         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6770                      treg, (int) BFD_RELOC_LO16);
6771       else if (treg == 0)
6772         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6773                      sreg, (int) BFD_RELOC_LO16);
6774       else
6775         {
6776           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6777                        "d,v,t", dreg, sreg, treg);
6778           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6779                        dreg, (int) BFD_RELOC_LO16);
6780         }
6781       return;
6782
6783     case M_SEQ_I:
6784       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6785         {
6786           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6787                        sreg, (int) BFD_RELOC_LO16);
6788           return;
6789         }
6790       if (sreg == 0)
6791         {
6792           as_warn (_("Instruction %s: result is always false"),
6793                    ip->insn_mo->name);
6794           move_register (&icnt, dreg, 0);
6795           return;
6796         }
6797       if (imm_expr.X_op == O_constant
6798           && imm_expr.X_add_number >= 0
6799           && imm_expr.X_add_number < 0x10000)
6800         {
6801           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6802                        sreg, (int) BFD_RELOC_LO16);
6803           used_at = 0;
6804         }
6805       else if (imm_expr.X_op == O_constant
6806                && imm_expr.X_add_number > -0x8000
6807                && imm_expr.X_add_number < 0)
6808         {
6809           imm_expr.X_add_number = -imm_expr.X_add_number;
6810           macro_build ((char *) NULL, &icnt, &imm_expr,
6811                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6812                        "t,r,j", dreg, sreg,
6813                        (int) BFD_RELOC_LO16);
6814           used_at = 0;
6815         }
6816       else
6817         {
6818           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6819           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6820                        "d,v,t", dreg, sreg, AT);
6821           used_at = 1;
6822         }
6823       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6824                    (int) BFD_RELOC_LO16);
6825       if (used_at)
6826         break;
6827       return;
6828
6829     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6830       s = "slt";
6831       goto sge;
6832     case M_SGEU:
6833       s = "sltu";
6834     sge:
6835       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6836                    dreg, sreg, treg);
6837       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6838                    (int) BFD_RELOC_LO16);
6839       return;
6840
6841     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6842     case M_SGEU_I:
6843       if (imm_expr.X_op == O_constant
6844           && imm_expr.X_add_number >= -0x8000
6845           && imm_expr.X_add_number < 0x8000)
6846         {
6847           macro_build ((char *) NULL, &icnt, &imm_expr,
6848                        mask == M_SGE_I ? "slti" : "sltiu",
6849                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6850           used_at = 0;
6851         }
6852       else
6853         {
6854           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6855           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6856                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6857                        AT);
6858           used_at = 1;
6859         }
6860       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6861                    (int) BFD_RELOC_LO16);
6862       if (used_at)
6863         break;
6864       return;
6865
6866     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6867       s = "slt";
6868       goto sgt;
6869     case M_SGTU:
6870       s = "sltu";
6871     sgt:
6872       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6873                    dreg, treg, sreg);
6874       return;
6875
6876     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6877       s = "slt";
6878       goto sgti;
6879     case M_SGTU_I:
6880       s = "sltu";
6881     sgti:
6882       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6883       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6884                    dreg, AT, sreg);
6885       break;
6886
6887     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6888       s = "slt";
6889       goto sle;
6890     case M_SLEU:
6891       s = "sltu";
6892     sle:
6893       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6894                    dreg, treg, sreg);
6895       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6896                    (int) BFD_RELOC_LO16);
6897       return;
6898
6899     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6900       s = "slt";
6901       goto slei;
6902     case M_SLEU_I:
6903       s = "sltu";
6904     slei:
6905       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6906       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6907                    dreg, AT, sreg);
6908       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6909                    (int) BFD_RELOC_LO16);
6910       break;
6911
6912     case M_SLT_I:
6913       if (imm_expr.X_op == O_constant
6914           && imm_expr.X_add_number >= -0x8000
6915           && imm_expr.X_add_number < 0x8000)
6916         {
6917           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6918                        dreg, sreg, (int) BFD_RELOC_LO16);
6919           return;
6920         }
6921       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6922       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6923                    dreg, sreg, AT);
6924       break;
6925
6926     case M_SLTU_I:
6927       if (imm_expr.X_op == O_constant
6928           && imm_expr.X_add_number >= -0x8000
6929           && imm_expr.X_add_number < 0x8000)
6930         {
6931           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6932                        dreg, sreg, (int) BFD_RELOC_LO16);
6933           return;
6934         }
6935       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6936       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6937                    "d,v,t", dreg, sreg, AT);
6938       break;
6939
6940     case M_SNE:
6941       if (sreg == 0)
6942         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6943                      "d,v,t", dreg, 0, treg);
6944       else if (treg == 0)
6945         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6946                      "d,v,t", dreg, 0, sreg);
6947       else
6948         {
6949           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6950                        "d,v,t", dreg, sreg, treg);
6951           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6952                        "d,v,t", dreg, 0, dreg);
6953         }
6954       return;
6955
6956     case M_SNE_I:
6957       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6958         {
6959           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6960                        "d,v,t", dreg, 0, sreg);
6961           return;
6962         }
6963       if (sreg == 0)
6964         {
6965           as_warn (_("Instruction %s: result is always true"),
6966                    ip->insn_mo->name);
6967           macro_build ((char *) NULL, &icnt, &expr1,
6968                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6969                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6970           return;
6971         }
6972       if (imm_expr.X_op == O_constant
6973           && imm_expr.X_add_number >= 0
6974           && imm_expr.X_add_number < 0x10000)
6975         {
6976           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6977                        dreg, sreg, (int) BFD_RELOC_LO16);
6978           used_at = 0;
6979         }
6980       else if (imm_expr.X_op == O_constant
6981                && imm_expr.X_add_number > -0x8000
6982                && imm_expr.X_add_number < 0)
6983         {
6984           imm_expr.X_add_number = -imm_expr.X_add_number;
6985           macro_build ((char *) NULL, &icnt, &imm_expr,
6986                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6987                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6988           used_at = 0;
6989         }
6990       else
6991         {
6992           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6993           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6994                        "d,v,t", dreg, sreg, AT);
6995           used_at = 1;
6996         }
6997       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6998                    "d,v,t", dreg, 0, dreg);
6999       if (used_at)
7000         break;
7001       return;
7002
7003     case M_DSUB_I:
7004       dbl = 1;
7005     case M_SUB_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 ? "daddi" : "addi",
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 ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7019       break;
7020
7021     case M_DSUBU_I:
7022       dbl = 1;
7023     case M_SUBU_I:
7024       if (imm_expr.X_op == O_constant
7025           && imm_expr.X_add_number > -0x8000
7026           && imm_expr.X_add_number <= 0x8000)
7027         {
7028           imm_expr.X_add_number = -imm_expr.X_add_number;
7029           macro_build ((char *) NULL, &icnt, &imm_expr,
7030                        dbl ? "daddiu" : "addiu",
7031                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7032           return;
7033         }
7034       load_register (&icnt, AT, &imm_expr, dbl);
7035       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7036                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7037       break;
7038
7039     case M_TEQ_I:
7040       s = "teq";
7041       goto trap;
7042     case M_TGE_I:
7043       s = "tge";
7044       goto trap;
7045     case M_TGEU_I:
7046       s = "tgeu";
7047       goto trap;
7048     case M_TLT_I:
7049       s = "tlt";
7050       goto trap;
7051     case M_TLTU_I:
7052       s = "tltu";
7053       goto trap;
7054     case M_TNE_I:
7055       s = "tne";
7056     trap:
7057       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7058       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7059                    AT);
7060       break;
7061
7062     case M_TRUNCWS:
7063     case M_TRUNCWD:
7064       assert (mips_opts.isa == ISA_MIPS1);
7065       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7066       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7067
7068       /*
7069        * Is the double cfc1 instruction a bug in the mips assembler;
7070        * or is there a reason for it?
7071        */
7072       mips_emit_delays (true);
7073       ++mips_opts.noreorder;
7074       mips_any_noreorder = 1;
7075       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7076                    treg, RA);
7077       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7078                    treg, RA);
7079       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7080       expr1.X_add_number = 3;
7081       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7082                    (int) BFD_RELOC_LO16);
7083       expr1.X_add_number = 2;
7084       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7085                      (int) BFD_RELOC_LO16);
7086       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7087                    AT, RA);
7088       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7089       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7090               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7091       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7092                    treg, RA);
7093       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7094       --mips_opts.noreorder;
7095       break;
7096
7097     case M_ULH:
7098       s = "lb";
7099       goto ulh;
7100     case M_ULHU:
7101       s = "lbu";
7102     ulh:
7103       if (offset_expr.X_add_number >= 0x7fff)
7104         as_bad (_("operand overflow"));
7105       /* avoid load delay */
7106       if (! target_big_endian)
7107         ++offset_expr.X_add_number;
7108       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7109                    (int) BFD_RELOC_LO16, breg);
7110       if (! target_big_endian)
7111         --offset_expr.X_add_number;
7112       else
7113         ++offset_expr.X_add_number;
7114       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7115                    (int) BFD_RELOC_LO16, breg);
7116       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7117                    treg, treg, 8);
7118       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7119                    treg, treg, AT);
7120       break;
7121
7122     case M_ULD:
7123       s = "ldl";
7124       s2 = "ldr";
7125       off = 7;
7126       goto ulw;
7127     case M_ULW:
7128       s = "lwl";
7129       s2 = "lwr";
7130       off = 3;
7131     ulw:
7132       if (offset_expr.X_add_number >= 0x8000 - off)
7133         as_bad (_("operand overflow"));
7134       if (! target_big_endian)
7135         offset_expr.X_add_number += off;
7136       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7137                    (int) BFD_RELOC_LO16, breg);
7138       if (! target_big_endian)
7139         offset_expr.X_add_number -= off;
7140       else
7141         offset_expr.X_add_number += off;
7142       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7143                    (int) BFD_RELOC_LO16, breg);
7144       return;
7145
7146     case M_ULD_A:
7147       s = "ldl";
7148       s2 = "ldr";
7149       off = 7;
7150       goto ulwa;
7151     case M_ULW_A:
7152       s = "lwl";
7153       s2 = "lwr";
7154       off = 3;
7155     ulwa:
7156       used_at = 1;
7157       load_address (&icnt, AT, &offset_expr, &used_at);
7158       if (breg != 0)
7159         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7160                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7161                      "d,v,t", AT, AT, breg);
7162       if (! target_big_endian)
7163         expr1.X_add_number = off;
7164       else
7165         expr1.X_add_number = 0;
7166       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7167                    (int) BFD_RELOC_LO16, AT);
7168       if (! target_big_endian)
7169         expr1.X_add_number = 0;
7170       else
7171         expr1.X_add_number = off;
7172       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7173                    (int) BFD_RELOC_LO16, AT);
7174       break;
7175
7176     case M_ULH_A:
7177     case M_ULHU_A:
7178       used_at = 1;
7179       load_address (&icnt, AT, &offset_expr, &used_at);
7180       if (breg != 0)
7181         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7182                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7183                      "d,v,t", AT, AT, breg);
7184       if (target_big_endian)
7185         expr1.X_add_number = 0;
7186       macro_build ((char *) NULL, &icnt, &expr1,
7187                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7188                    (int) BFD_RELOC_LO16, AT);
7189       if (target_big_endian)
7190         expr1.X_add_number = 1;
7191       else
7192         expr1.X_add_number = 0;
7193       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7194                    (int) BFD_RELOC_LO16, AT);
7195       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7196                    treg, treg, 8);
7197       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7198                    treg, treg, AT);
7199       break;
7200
7201     case M_USH:
7202       if (offset_expr.X_add_number >= 0x7fff)
7203         as_bad (_("operand overflow"));
7204       if (target_big_endian)
7205         ++offset_expr.X_add_number;
7206       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7207                    (int) BFD_RELOC_LO16, breg);
7208       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7209                    AT, treg, 8);
7210       if (target_big_endian)
7211         --offset_expr.X_add_number;
7212       else
7213         ++offset_expr.X_add_number;
7214       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7215                    (int) BFD_RELOC_LO16, breg);
7216       break;
7217
7218     case M_USD:
7219       s = "sdl";
7220       s2 = "sdr";
7221       off = 7;
7222       goto usw;
7223     case M_USW:
7224       s = "swl";
7225       s2 = "swr";
7226       off = 3;
7227     usw:
7228       if (offset_expr.X_add_number >= 0x8000 - off)
7229         as_bad (_("operand overflow"));
7230       if (! target_big_endian)
7231         offset_expr.X_add_number += off;
7232       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7233                    (int) BFD_RELOC_LO16, breg);
7234       if (! target_big_endian)
7235         offset_expr.X_add_number -= off;
7236       else
7237         offset_expr.X_add_number += off;
7238       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7239                    (int) BFD_RELOC_LO16, breg);
7240       return;
7241
7242     case M_USD_A:
7243       s = "sdl";
7244       s2 = "sdr";
7245       off = 7;
7246       goto uswa;
7247     case M_USW_A:
7248       s = "swl";
7249       s2 = "swr";
7250       off = 3;
7251     uswa:
7252       used_at = 1;
7253       load_address (&icnt, AT, &offset_expr, &used_at);
7254       if (breg != 0)
7255         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7256                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7257                      "d,v,t", AT, AT, breg);
7258       if (! target_big_endian)
7259         expr1.X_add_number = off;
7260       else
7261         expr1.X_add_number = 0;
7262       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7263                    (int) BFD_RELOC_LO16, AT);
7264       if (! target_big_endian)
7265         expr1.X_add_number = 0;
7266       else
7267         expr1.X_add_number = off;
7268       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7269                    (int) BFD_RELOC_LO16, AT);
7270       break;
7271
7272     case M_USH_A:
7273       used_at = 1;
7274       load_address (&icnt, AT, &offset_expr, &used_at);
7275       if (breg != 0)
7276         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7277                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7278                      "d,v,t", AT, AT, breg);
7279       if (! target_big_endian)
7280         expr1.X_add_number = 0;
7281       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7282                    (int) BFD_RELOC_LO16, AT);
7283       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7284                    treg, treg, 8);
7285       if (! target_big_endian)
7286         expr1.X_add_number = 1;
7287       else
7288         expr1.X_add_number = 0;
7289       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7290                    (int) BFD_RELOC_LO16, AT);
7291       if (! target_big_endian)
7292         expr1.X_add_number = 0;
7293       else
7294         expr1.X_add_number = 1;
7295       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7296                    (int) BFD_RELOC_LO16, AT);
7297       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7298                    treg, treg, 8);
7299       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7300                    treg, treg, AT);
7301       break;
7302
7303     default:
7304       /* FIXME: Check if this is one of the itbl macros, since they
7305          are added dynamically.  */
7306       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7307       break;
7308     }
7309   if (mips_opts.noat)
7310     as_warn (_("Macro used $at after \".set noat\""));
7311 }
7312
7313 /* Implement macros in mips16 mode.  */
7314
7315 static void
7316 mips16_macro (ip)
7317      struct mips_cl_insn *ip;
7318 {
7319   int mask;
7320   int xreg, yreg, zreg, tmp;
7321   int icnt;
7322   expressionS expr1;
7323   int dbl;
7324   const char *s, *s2, *s3;
7325
7326   mask = ip->insn_mo->mask;
7327
7328   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7329   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7330   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7331
7332   icnt = 0;
7333
7334   expr1.X_op = O_constant;
7335   expr1.X_op_symbol = NULL;
7336   expr1.X_add_symbol = NULL;
7337   expr1.X_add_number = 1;
7338
7339   dbl = 0;
7340
7341   switch (mask)
7342     {
7343     default:
7344       internalError ();
7345
7346     case M_DDIV_3:
7347       dbl = 1;
7348     case M_DIV_3:
7349       s = "mflo";
7350       goto do_div3;
7351     case M_DREM_3:
7352       dbl = 1;
7353     case M_REM_3:
7354       s = "mfhi";
7355     do_div3:
7356       mips_emit_delays (true);
7357       ++mips_opts.noreorder;
7358       mips_any_noreorder = 1;
7359       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7360                    dbl ? "ddiv" : "div",
7361                    "0,x,y", xreg, yreg);
7362       expr1.X_add_number = 2;
7363       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7364       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7365                    7);
7366
7367       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7368          since that causes an overflow.  We should do that as well,
7369          but I don't see how to do the comparisons without a temporary
7370          register.  */
7371       --mips_opts.noreorder;
7372       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7373       break;
7374
7375     case M_DIVU_3:
7376       s = "divu";
7377       s2 = "mflo";
7378       goto do_divu3;
7379     case M_REMU_3:
7380       s = "divu";
7381       s2 = "mfhi";
7382       goto do_divu3;
7383     case M_DDIVU_3:
7384       s = "ddivu";
7385       s2 = "mflo";
7386       goto do_divu3;
7387     case M_DREMU_3:
7388       s = "ddivu";
7389       s2 = "mfhi";
7390     do_divu3:
7391       mips_emit_delays (true);
7392       ++mips_opts.noreorder;
7393       mips_any_noreorder = 1;
7394       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7395                    xreg, yreg);
7396       expr1.X_add_number = 2;
7397       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7398       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7399                    "6", 7);
7400       --mips_opts.noreorder;
7401       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7402       break;
7403
7404     case M_DMUL:
7405       dbl = 1;
7406     case M_MUL:
7407       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7408                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7409       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7410                    zreg);
7411       return;
7412
7413     case M_DSUBU_I:
7414       dbl = 1;
7415       goto do_subu;
7416     case M_SUBU_I:
7417     do_subu:
7418       if (imm_expr.X_op != O_constant)
7419         as_bad (_("Unsupported large constant"));
7420       imm_expr.X_add_number = -imm_expr.X_add_number;
7421       macro_build ((char *) NULL, &icnt, &imm_expr,
7422                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7423       break;
7424
7425     case M_SUBU_I_2:
7426       if (imm_expr.X_op != O_constant)
7427         as_bad (_("Unsupported large constant"));
7428       imm_expr.X_add_number = -imm_expr.X_add_number;
7429       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7430                    "x,k", xreg);
7431       break;
7432
7433     case M_DSUBU_I_2:
7434       if (imm_expr.X_op != O_constant)
7435         as_bad (_("Unsupported large constant"));
7436       imm_expr.X_add_number = -imm_expr.X_add_number;
7437       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7438                    "y,j", yreg);
7439       break;
7440
7441     case M_BEQ:
7442       s = "cmp";
7443       s2 = "bteqz";
7444       goto do_branch;
7445     case M_BNE:
7446       s = "cmp";
7447       s2 = "btnez";
7448       goto do_branch;
7449     case M_BLT:
7450       s = "slt";
7451       s2 = "btnez";
7452       goto do_branch;
7453     case M_BLTU:
7454       s = "sltu";
7455       s2 = "btnez";
7456       goto do_branch;
7457     case M_BLE:
7458       s = "slt";
7459       s2 = "bteqz";
7460       goto do_reverse_branch;
7461     case M_BLEU:
7462       s = "sltu";
7463       s2 = "bteqz";
7464       goto do_reverse_branch;
7465     case M_BGE:
7466       s = "slt";
7467       s2 = "bteqz";
7468       goto do_branch;
7469     case M_BGEU:
7470       s = "sltu";
7471       s2 = "bteqz";
7472       goto do_branch;
7473     case M_BGT:
7474       s = "slt";
7475       s2 = "btnez";
7476       goto do_reverse_branch;
7477     case M_BGTU:
7478       s = "sltu";
7479       s2 = "btnez";
7480
7481     do_reverse_branch:
7482       tmp = xreg;
7483       xreg = yreg;
7484       yreg = tmp;
7485
7486     do_branch:
7487       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7488                    xreg, yreg);
7489       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7490       break;
7491
7492     case M_BEQ_I:
7493       s = "cmpi";
7494       s2 = "bteqz";
7495       s3 = "x,U";
7496       goto do_branch_i;
7497     case M_BNE_I:
7498       s = "cmpi";
7499       s2 = "btnez";
7500       s3 = "x,U";
7501       goto do_branch_i;
7502     case M_BLT_I:
7503       s = "slti";
7504       s2 = "btnez";
7505       s3 = "x,8";
7506       goto do_branch_i;
7507     case M_BLTU_I:
7508       s = "sltiu";
7509       s2 = "btnez";
7510       s3 = "x,8";
7511       goto do_branch_i;
7512     case M_BLE_I:
7513       s = "slti";
7514       s2 = "btnez";
7515       s3 = "x,8";
7516       goto do_addone_branch_i;
7517     case M_BLEU_I:
7518       s = "sltiu";
7519       s2 = "btnez";
7520       s3 = "x,8";
7521       goto do_addone_branch_i;
7522     case M_BGE_I:
7523       s = "slti";
7524       s2 = "bteqz";
7525       s3 = "x,8";
7526       goto do_branch_i;
7527     case M_BGEU_I:
7528       s = "sltiu";
7529       s2 = "bteqz";
7530       s3 = "x,8";
7531       goto do_branch_i;
7532     case M_BGT_I:
7533       s = "slti";
7534       s2 = "bteqz";
7535       s3 = "x,8";
7536       goto do_addone_branch_i;
7537     case M_BGTU_I:
7538       s = "sltiu";
7539       s2 = "bteqz";
7540       s3 = "x,8";
7541
7542     do_addone_branch_i:
7543       if (imm_expr.X_op != O_constant)
7544         as_bad (_("Unsupported large constant"));
7545       ++imm_expr.X_add_number;
7546
7547     do_branch_i:
7548       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7549       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7550       break;
7551
7552     case M_ABS:
7553       expr1.X_add_number = 0;
7554       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7555       if (xreg != yreg)
7556         move_register (&icnt, xreg, yreg);
7557       expr1.X_add_number = 2;
7558       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7559       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7560                    "neg", "x,w", xreg, xreg);
7561     }
7562 }
7563
7564 /* For consistency checking, verify that all bits are specified either
7565    by the match/mask part of the instruction definition, or by the
7566    operand list.  */
7567 static int
7568 validate_mips_insn (opc)
7569      const struct mips_opcode *opc;
7570 {
7571   const char *p = opc->args;
7572   char c;
7573   unsigned long used_bits = opc->mask;
7574
7575   if ((used_bits & opc->match) != opc->match)
7576     {
7577       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7578               opc->name, opc->args);
7579       return 0;
7580     }
7581 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7582   while (*p)
7583     switch (c = *p++)
7584       {
7585       case ',': break;
7586       case '(': break;
7587       case ')': break;
7588       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7589       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7590       case 'A': break;
7591       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7592       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7593       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7594       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7595       case 'F': break;
7596       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7597       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7598       case 'I': break;
7599       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7600       case 'L': break;
7601       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7602       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7603       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7604       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7605                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7606       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7607       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7608       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7609       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7610       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7611       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7612       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7613       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7614       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7615       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7616       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7617       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7618       case 'f': break;
7619       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7620       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7621       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7622       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7623       case 'l': break;
7624       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7625       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7626       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7627       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7628       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7629       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7630       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7631       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7632       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7633       case 'x': break;
7634       case 'z': break;
7635       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7636       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7637                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7638       default:
7639         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7640                 c, opc->name, opc->args);
7641         return 0;
7642       }
7643 #undef USE_BITS
7644   if (used_bits != 0xffffffff)
7645     {
7646       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7647               ~used_bits & 0xffffffff, opc->name, opc->args);
7648       return 0;
7649     }
7650   return 1;
7651 }
7652
7653 /* This routine assembles an instruction into its binary format.  As a
7654    side effect, it sets one of the global variables imm_reloc or
7655    offset_reloc to the type of relocation to do if one of the operands
7656    is an address expression.  */
7657
7658 static void
7659 mips_ip (str, ip)
7660      char *str;
7661      struct mips_cl_insn *ip;
7662 {
7663   char *s;
7664   const char *args;
7665   char c = 0;
7666   struct mips_opcode *insn;
7667   char *argsStart;
7668   unsigned int regno;
7669   unsigned int lastregno = 0;
7670   char *s_reset;
7671   char save_c = 0;
7672
7673   insn_error = NULL;
7674
7675   /* If the instruction contains a '.', we first try to match an instruction
7676      including the '.'.  Then we try again without the '.'.  */
7677   insn = NULL;
7678   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7679     continue;
7680
7681   /* If we stopped on whitespace, then replace the whitespace with null for
7682      the call to hash_find.  Save the character we replaced just in case we
7683      have to re-parse the instruction.  */
7684   if (ISSPACE (*s))
7685     {
7686       save_c = *s;
7687       *s++ = '\0';
7688     }
7689
7690   insn = (struct mips_opcode *) hash_find (op_hash, str);
7691
7692   /* If we didn't find the instruction in the opcode table, try again, but
7693      this time with just the instruction up to, but not including the
7694      first '.'.  */
7695   if (insn == NULL)
7696     {
7697       /* Restore the character we overwrite above (if any).  */
7698       if (save_c)
7699         *(--s) = save_c;
7700
7701       /* Scan up to the first '.' or whitespace.  */
7702       for (s = str;
7703            *s != '\0' && *s != '.' && !ISSPACE (*s);
7704            ++s)
7705         continue;
7706
7707       /* If we did not find a '.', then we can quit now.  */
7708       if (*s != '.')
7709         {
7710           insn_error = "unrecognized opcode";
7711           return;
7712         }
7713
7714       /* Lookup the instruction in the hash table.  */
7715       *s++ = '\0';
7716       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7717         {
7718           insn_error = "unrecognized opcode";
7719           return;
7720         }
7721     }
7722
7723   argsStart = s;
7724   for (;;)
7725     {
7726       boolean ok;
7727
7728       assert (strcmp (insn->name, str) == 0);
7729
7730       if (OPCODE_IS_MEMBER (insn,
7731                             (mips_opts.isa
7732                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7733                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7734                             mips_arch))
7735         ok = true;
7736       else
7737         ok = false;
7738
7739       if (insn->pinfo != INSN_MACRO)
7740         {
7741           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7742             ok = false;
7743         }
7744
7745       if (! ok)
7746         {
7747           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7748               && strcmp (insn->name, insn[1].name) == 0)
7749             {
7750               ++insn;
7751               continue;
7752             }
7753           else
7754             {
7755               if (!insn_error)
7756                 {
7757                   static char buf[100];
7758                   sprintf (buf,
7759                            _("opcode not supported on this processor: %s (%s)"),
7760                            mips_cpu_to_str (mips_arch),
7761                            mips_isa_to_str (mips_opts.isa));
7762
7763                   insn_error = buf;
7764                 }
7765               if (save_c)
7766                 *(--s) = save_c;
7767               return;
7768             }
7769         }
7770
7771       ip->insn_mo = insn;
7772       ip->insn_opcode = insn->match;
7773       insn_error = NULL;
7774       for (args = insn->args;; ++args)
7775         {
7776           int is_mdmx;
7777
7778           s += strspn (s, " \t");
7779           is_mdmx = 0;
7780           switch (*args)
7781             {
7782             case '\0':          /* end of args */
7783               if (*s == '\0')
7784                 return;
7785               break;
7786
7787             case ',':
7788               if (*s++ == *args)
7789                 continue;
7790               s--;
7791               switch (*++args)
7792                 {
7793                 case 'r':
7794                 case 'v':
7795                   ip->insn_opcode |= lastregno << OP_SH_RS;
7796                   continue;
7797
7798                 case 'w':
7799                   ip->insn_opcode |= lastregno << OP_SH_RT;
7800                   continue;
7801
7802                 case 'W':
7803                   ip->insn_opcode |= lastregno << OP_SH_FT;
7804                   continue;
7805
7806                 case 'V':
7807                   ip->insn_opcode |= lastregno << OP_SH_FS;
7808                   continue;
7809                 }
7810               break;
7811
7812             case '(':
7813               /* Handle optional base register.
7814                  Either the base register is omitted or
7815                  we must have a left paren.  */
7816               /* This is dependent on the next operand specifier
7817                  is a base register specification.  */
7818               assert (args[1] == 'b' || args[1] == '5'
7819                       || args[1] == '-' || args[1] == '4');
7820               if (*s == '\0')
7821                 return;
7822
7823             case ')':           /* these must match exactly */
7824               if (*s++ == *args)
7825                 continue;
7826               break;
7827
7828             case '<':           /* must be at least one digit */
7829               /*
7830                * According to the manual, if the shift amount is greater
7831                * than 31 or less than 0, then the shift amount should be
7832                * mod 32.  In reality the mips assembler issues an error.
7833                * We issue a warning and mask out all but the low 5 bits.
7834                */
7835               my_getExpression (&imm_expr, s);
7836               check_absolute_expr (ip, &imm_expr);
7837               if ((unsigned long) imm_expr.X_add_number > 31)
7838                 {
7839                   as_warn (_("Improper shift amount (%lu)"),
7840                            (unsigned long) imm_expr.X_add_number);
7841                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7842                 }
7843               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7844               imm_expr.X_op = O_absent;
7845               s = expr_end;
7846               continue;
7847
7848             case '>':           /* shift amount minus 32 */
7849               my_getExpression (&imm_expr, s);
7850               check_absolute_expr (ip, &imm_expr);
7851               if ((unsigned long) imm_expr.X_add_number < 32
7852                   || (unsigned long) imm_expr.X_add_number > 63)
7853                 break;
7854               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7855               imm_expr.X_op = O_absent;
7856               s = expr_end;
7857               continue;
7858
7859             case 'k':           /* cache code */
7860             case 'h':           /* prefx code */
7861               my_getExpression (&imm_expr, s);
7862               check_absolute_expr (ip, &imm_expr);
7863               if ((unsigned long) imm_expr.X_add_number > 31)
7864                 {
7865                   as_warn (_("Invalid value for `%s' (%lu)"),
7866                            ip->insn_mo->name,
7867                            (unsigned long) imm_expr.X_add_number);
7868                   imm_expr.X_add_number &= 0x1f;
7869                 }
7870               if (*args == 'k')
7871                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7872               else
7873                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7874               imm_expr.X_op = O_absent;
7875               s = expr_end;
7876               continue;
7877
7878             case 'c':           /* break code */
7879               my_getExpression (&imm_expr, s);
7880               check_absolute_expr (ip, &imm_expr);
7881               if ((unsigned long) imm_expr.X_add_number > 1023)
7882                 {
7883                   as_warn (_("Illegal break code (%lu)"),
7884                            (unsigned long) imm_expr.X_add_number);
7885                   imm_expr.X_add_number &= OP_MASK_CODE;
7886                 }
7887               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7888               imm_expr.X_op = O_absent;
7889               s = expr_end;
7890               continue;
7891
7892             case 'q':           /* lower break code */
7893               my_getExpression (&imm_expr, s);
7894               check_absolute_expr (ip, &imm_expr);
7895               if ((unsigned long) imm_expr.X_add_number > 1023)
7896                 {
7897                   as_warn (_("Illegal lower break code (%lu)"),
7898                            (unsigned long) imm_expr.X_add_number);
7899                   imm_expr.X_add_number &= OP_MASK_CODE2;
7900                 }
7901               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7902               imm_expr.X_op = O_absent;
7903               s = expr_end;
7904               continue;
7905
7906             case 'B':           /* 20-bit syscall/break code.  */
7907               my_getExpression (&imm_expr, s);
7908               check_absolute_expr (ip, &imm_expr);
7909               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7910                 as_warn (_("Illegal 20-bit code (%lu)"),
7911                          (unsigned long) imm_expr.X_add_number);
7912               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7913               imm_expr.X_op = O_absent;
7914               s = expr_end;
7915               continue;
7916
7917             case 'C':           /* Coprocessor code */
7918               my_getExpression (&imm_expr, s);
7919               check_absolute_expr (ip, &imm_expr);
7920               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
7921                 {
7922                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
7923                            (unsigned long) imm_expr.X_add_number);
7924                   imm_expr.X_add_number &= ((1 << 25) - 1);
7925                 }
7926               ip->insn_opcode |= imm_expr.X_add_number;
7927               imm_expr.X_op = O_absent;
7928               s = expr_end;
7929               continue;
7930
7931             case 'J':           /* 19-bit wait code.  */
7932               my_getExpression (&imm_expr, s);
7933               check_absolute_expr (ip, &imm_expr);
7934               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7935                 as_warn (_("Illegal 19-bit code (%lu)"),
7936                          (unsigned long) imm_expr.X_add_number);
7937               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7938               imm_expr.X_op = O_absent;
7939               s = expr_end;
7940               continue;
7941
7942             case 'P':           /* Performance register */
7943               my_getExpression (&imm_expr, s);
7944               check_absolute_expr (ip, &imm_expr);
7945               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7946                 {
7947                   as_warn (_("Invalid performance register (%lu)"),
7948                            (unsigned long) imm_expr.X_add_number);
7949                   imm_expr.X_add_number &= OP_MASK_PERFREG;
7950                 }
7951               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7952               imm_expr.X_op = O_absent;
7953               s = expr_end;
7954               continue;
7955
7956             case 'b':           /* base register */
7957             case 'd':           /* destination register */
7958             case 's':           /* source register */
7959             case 't':           /* target register */
7960             case 'r':           /* both target and source */
7961             case 'v':           /* both dest and source */
7962             case 'w':           /* both dest and target */
7963             case 'E':           /* coprocessor target register */
7964             case 'G':           /* coprocessor destination register */
7965             case 'x':           /* ignore register name */
7966             case 'z':           /* must be zero register */
7967             case 'U':           /* destination register (clo/clz).  */
7968               s_reset = s;
7969               if (s[0] == '$')
7970                 {
7971
7972                   if (ISDIGIT (s[1]))
7973                     {
7974                       ++s;
7975                       regno = 0;
7976                       do
7977                         {
7978                           regno *= 10;
7979                           regno += *s - '0';
7980                           ++s;
7981                         }
7982                       while (ISDIGIT (*s));
7983                       if (regno > 31)
7984                         as_bad (_("Invalid register number (%d)"), regno);
7985                     }
7986                   else if (*args == 'E' || *args == 'G')
7987                     goto notreg;
7988                   else
7989                     {
7990                       if (s[1] == 'r' && s[2] == 'a')
7991                         {
7992                           s += 3;
7993                           regno = RA;
7994                         }
7995                       else if (s[1] == 'f' && s[2] == 'p')
7996                         {
7997                           s += 3;
7998                           regno = FP;
7999                         }
8000                       else if (s[1] == 's' && s[2] == 'p')
8001                         {
8002                           s += 3;
8003                           regno = SP;
8004                         }
8005                       else if (s[1] == 'g' && s[2] == 'p')
8006                         {
8007                           s += 3;
8008                           regno = GP;
8009                         }
8010                       else if (s[1] == 'a' && s[2] == 't')
8011                         {
8012                           s += 3;
8013                           regno = AT;
8014                         }
8015                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8016                         {
8017                           s += 4;
8018                           regno = KT0;
8019                         }
8020                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8021                         {
8022                           s += 4;
8023                           regno = KT1;
8024                         }
8025                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8026                         {
8027                           s += 5;
8028                           regno = ZERO;
8029                         }
8030                       else if (itbl_have_entries)
8031                         {
8032                           char *p, *n;
8033                           unsigned long r;
8034
8035                           p = s + 1;    /* advance past '$' */
8036                           n = itbl_get_field (&p);  /* n is name */
8037
8038                           /* See if this is a register defined in an
8039                              itbl entry.  */
8040                           if (itbl_get_reg_val (n, &r))
8041                             {
8042                               /* Get_field advances to the start of
8043                                  the next field, so we need to back
8044                                  rack to the end of the last field.  */
8045                               if (p)
8046                                 s = p - 1;
8047                               else
8048                                 s = strchr (s, '\0');
8049                               regno = r;
8050                             }
8051                           else
8052                             goto notreg;
8053                         }
8054                       else
8055                         goto notreg;
8056                     }
8057                   if (regno == AT
8058                       && ! mips_opts.noat
8059                       && *args != 'E'
8060                       && *args != 'G')
8061                     as_warn (_("Used $at without \".set noat\""));
8062                   c = *args;
8063                   if (*s == ' ')
8064                     ++s;
8065                   if (args[1] != *s)
8066                     {
8067                       if (c == 'r' || c == 'v' || c == 'w')
8068                         {
8069                           regno = lastregno;
8070                           s = s_reset;
8071                           ++args;
8072                         }
8073                     }
8074                   /* 'z' only matches $0.  */
8075                   if (c == 'z' && regno != 0)
8076                     break;
8077
8078         /* Now that we have assembled one operand, we use the args string
8079          * to figure out where it goes in the instruction.  */
8080                   switch (c)
8081                     {
8082                     case 'r':
8083                     case 's':
8084                     case 'v':
8085                     case 'b':
8086                       ip->insn_opcode |= regno << OP_SH_RS;
8087                       break;
8088                     case 'd':
8089                     case 'G':
8090                       ip->insn_opcode |= regno << OP_SH_RD;
8091                       break;
8092                     case 'U':
8093                       ip->insn_opcode |= regno << OP_SH_RD;
8094                       ip->insn_opcode |= regno << OP_SH_RT;
8095                       break;
8096                     case 'w':
8097                     case 't':
8098                     case 'E':
8099                       ip->insn_opcode |= regno << OP_SH_RT;
8100                       break;
8101                     case 'x':
8102                       /* This case exists because on the r3000 trunc
8103                          expands into a macro which requires a gp
8104                          register.  On the r6000 or r4000 it is
8105                          assembled into a single instruction which
8106                          ignores the register.  Thus the insn version
8107                          is MIPS_ISA2 and uses 'x', and the macro
8108                          version is MIPS_ISA1 and uses 't'.  */
8109                       break;
8110                     case 'z':
8111                       /* This case is for the div instruction, which
8112                          acts differently if the destination argument
8113                          is $0.  This only matches $0, and is checked
8114                          outside the switch.  */
8115                       break;
8116                     case 'D':
8117                       /* Itbl operand; not yet implemented. FIXME ?? */
8118                       break;
8119                       /* What about all other operands like 'i', which
8120                          can be specified in the opcode table? */
8121                     }
8122                   lastregno = regno;
8123                   continue;
8124                 }
8125             notreg:
8126               switch (*args++)
8127                 {
8128                 case 'r':
8129                 case 'v':
8130                   ip->insn_opcode |= lastregno << OP_SH_RS;
8131                   continue;
8132                 case 'w':
8133                   ip->insn_opcode |= lastregno << OP_SH_RT;
8134                   continue;
8135                 }
8136               break;
8137
8138             case 'O':           /* MDMX alignment immediate constant.  */
8139               my_getExpression (&imm_expr, s);
8140               check_absolute_expr (ip, &imm_expr);
8141               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8142                 {
8143                   as_warn ("Improper align amount (%ld), using low bits",
8144                            (long) imm_expr.X_add_number);
8145                   imm_expr.X_add_number &= OP_MASK_ALN;
8146                 }
8147               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8148               imm_expr.X_op = O_absent;
8149               s = expr_end;
8150               continue;
8151
8152             case 'Q':           /* MDMX vector, element sel, or const.  */
8153               if (s[0] != '$')
8154                 {
8155                   /* MDMX Immediate.  */
8156                   my_getExpression (&imm_expr, s);
8157                   check_absolute_expr (ip, &imm_expr);
8158                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8159                     {
8160                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8161                                (long) imm_expr.X_add_number);
8162                       imm_expr.X_add_number &= OP_MASK_FT;
8163                     }
8164                   imm_expr.X_add_number &= OP_MASK_FT;
8165                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8166                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8167                   else
8168                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8169                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8170                   imm_expr.X_op = O_absent;
8171                   s = expr_end;
8172                   continue;
8173                 }
8174               /* Not MDMX Immediate.  Fall through.  */
8175             case 'X':           /* MDMX destination register.  */
8176             case 'Y':           /* MDMX source register.  */
8177             case 'Z':           /* MDMX target register.  */
8178               is_mdmx = 1;
8179             case 'D':           /* floating point destination register */
8180             case 'S':           /* floating point source register */
8181             case 'T':           /* floating point target register */
8182             case 'R':           /* floating point source register */
8183             case 'V':
8184             case 'W':
8185               s_reset = s;
8186               /* Accept $fN for FP and MDMX register numbers, and in
8187                  addition accept $vN for MDMX register numbers.  */
8188               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8189                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8190                       && ISDIGIT (s[2])))
8191                 {
8192                   s += 2;
8193                   regno = 0;
8194                   do
8195                     {
8196                       regno *= 10;
8197                       regno += *s - '0';
8198                       ++s;
8199                     }
8200                   while (ISDIGIT (*s));
8201
8202                   if (regno > 31)
8203                     as_bad (_("Invalid float register number (%d)"), regno);
8204
8205                   if ((regno & 1) != 0
8206                       && HAVE_32BIT_FPRS
8207                       && ! (strcmp (str, "mtc1") == 0
8208                             || strcmp (str, "mfc1") == 0
8209                             || strcmp (str, "lwc1") == 0
8210                             || strcmp (str, "swc1") == 0
8211                             || strcmp (str, "l.s") == 0
8212                             || strcmp (str, "s.s") == 0))
8213                     as_warn (_("Float register should be even, was %d"),
8214                              regno);
8215
8216                   c = *args;
8217                   if (*s == ' ')
8218                     ++s;
8219                   if (args[1] != *s)
8220                     {
8221                       if (c == 'V' || c == 'W')
8222                         {
8223                           regno = lastregno;
8224                           s = s_reset;
8225                           ++args;
8226                         }
8227                     }
8228                   switch (c)
8229                     {
8230                     case 'D':
8231                     case 'X':
8232                       ip->insn_opcode |= regno << OP_SH_FD;
8233                       break;
8234                     case 'V':
8235                     case 'S':
8236                     case 'Y':
8237                       ip->insn_opcode |= regno << OP_SH_FS;
8238                       break;
8239                     case 'Q':
8240                       /* This is like 'Z', but also needs to fix the MDMX
8241                          vector/scalar select bits.  Note that the
8242                          scalar immediate case is handled above.  */
8243                       if (*s == '[')
8244                         {
8245                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8246                           int max_el = (is_qh ? 3 : 7);
8247                           s++;
8248                           my_getExpression(&imm_expr, s);
8249                           check_absolute_expr (ip, &imm_expr);
8250                           s = expr_end;
8251                           if (imm_expr.X_add_number > max_el)
8252                             as_bad(_("Bad element selector %ld"),
8253                                    (long) imm_expr.X_add_number);
8254                           imm_expr.X_add_number &= max_el;
8255                           ip->insn_opcode |= (imm_expr.X_add_number
8256                                               << (OP_SH_VSEL +
8257                                                   (is_qh ? 2 : 1)));
8258                           if (*s != ']')
8259                             as_warn(_("Expecting ']' found '%s'"), s);
8260                           else
8261                             s++;
8262                         }
8263                       else
8264                         {
8265                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8266                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8267                                                 << OP_SH_VSEL);
8268                           else
8269                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8270                                                 OP_SH_VSEL);
8271                         }
8272                       /* Fall through */
8273                     case 'W':
8274                     case 'T':
8275                     case 'Z':
8276                       ip->insn_opcode |= regno << OP_SH_FT;
8277                       break;
8278                     case 'R':
8279                       ip->insn_opcode |= regno << OP_SH_FR;
8280                       break;
8281                     }
8282                   lastregno = regno;
8283                   continue;
8284                 }
8285
8286               switch (*args++)
8287                 {
8288                 case 'V':
8289                   ip->insn_opcode |= lastregno << OP_SH_FS;
8290                   continue;
8291                 case 'W':
8292                   ip->insn_opcode |= lastregno << OP_SH_FT;
8293                   continue;
8294                 }
8295               break;
8296
8297             case 'I':
8298               my_getExpression (&imm_expr, s);
8299               if (imm_expr.X_op != O_big
8300                   && imm_expr.X_op != O_constant)
8301                 insn_error = _("absolute expression required");
8302               s = expr_end;
8303               continue;
8304
8305             case 'A':
8306               my_getExpression (&offset_expr, s);
8307               *imm_reloc = BFD_RELOC_32;
8308               s = expr_end;
8309               continue;
8310
8311             case 'F':
8312             case 'L':
8313             case 'f':
8314             case 'l':
8315               {
8316                 int f64;
8317                 int using_gprs;
8318                 char *save_in;
8319                 char *err;
8320                 unsigned char temp[8];
8321                 int len;
8322                 unsigned int length;
8323                 segT seg;
8324                 subsegT subseg;
8325                 char *p;
8326
8327                 /* These only appear as the last operand in an
8328                    instruction, and every instruction that accepts
8329                    them in any variant accepts them in all variants.
8330                    This means we don't have to worry about backing out
8331                    any changes if the instruction does not match.
8332
8333                    The difference between them is the size of the
8334                    floating point constant and where it goes.  For 'F'
8335                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8336                    is 32 bits.  Where the constant is placed is based
8337                    on how the MIPS assembler does things:
8338                     F -- .rdata
8339                     L -- .lit8
8340                     f -- immediate value
8341                     l -- .lit4
8342
8343                     The .lit4 and .lit8 sections are only used if
8344                     permitted by the -G argument.
8345
8346                     When generating embedded PIC code, we use the
8347                     .lit8 section but not the .lit4 section (we can do
8348                     .lit4 inline easily; we need to put .lit8
8349                     somewhere in the data segment, and using .lit8
8350                     permits the linker to eventually combine identical
8351                     .lit8 entries).
8352
8353                     The code below needs to know whether the target register
8354                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8355                     'F' are used with GPR-based instructions and 'l' and
8356                     'L' are used with FPR-based instructions.  */
8357
8358                 f64 = *args == 'F' || *args == 'L';
8359                 using_gprs = *args == 'F' || *args == 'f';
8360
8361                 save_in = input_line_pointer;
8362                 input_line_pointer = s;
8363                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8364                 length = len;
8365                 s = input_line_pointer;
8366                 input_line_pointer = save_in;
8367                 if (err != NULL && *err != '\0')
8368                   {
8369                     as_bad (_("Bad floating point constant: %s"), err);
8370                     memset (temp, '\0', sizeof temp);
8371                     length = f64 ? 8 : 4;
8372                   }
8373
8374                 assert (length == (unsigned) (f64 ? 8 : 4));
8375
8376                 if (*args == 'f'
8377                     || (*args == 'l'
8378                         && (! USE_GLOBAL_POINTER_OPT
8379                             || mips_pic == EMBEDDED_PIC
8380                             || g_switch_value < 4
8381                             || (temp[0] == 0 && temp[1] == 0)
8382                             || (temp[2] == 0 && temp[3] == 0))))
8383                   {
8384                     imm_expr.X_op = O_constant;
8385                     if (! target_big_endian)
8386                       imm_expr.X_add_number = bfd_getl32 (temp);
8387                     else
8388                       imm_expr.X_add_number = bfd_getb32 (temp);
8389                   }
8390                 else if (length > 4
8391                          && ! mips_disable_float_construction
8392                          /* Constants can only be constructed in GPRs and
8393                             copied to FPRs if the GPRs are at least as wide
8394                             as the FPRs.  Force the constant into memory if
8395                             we are using 64-bit FPRs but the GPRs are only
8396                             32 bits wide.  */
8397                          && (using_gprs
8398                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8399                          && ((temp[0] == 0 && temp[1] == 0)
8400                              || (temp[2] == 0 && temp[3] == 0))
8401                          && ((temp[4] == 0 && temp[5] == 0)
8402                              || (temp[6] == 0 && temp[7] == 0)))
8403                   {
8404                     /* The value is simple enough to load with a couple of
8405                        instructions.  If using 32-bit registers, set
8406                        imm_expr to the high order 32 bits and offset_expr to
8407                        the low order 32 bits.  Otherwise, set imm_expr to
8408                        the entire 64 bit constant.  */
8409                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8410                       {
8411                         imm_expr.X_op = O_constant;
8412                         offset_expr.X_op = O_constant;
8413                         if (! target_big_endian)
8414                           {
8415                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8416                             offset_expr.X_add_number = bfd_getl32 (temp);
8417                           }
8418                         else
8419                           {
8420                             imm_expr.X_add_number = bfd_getb32 (temp);
8421                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8422                           }
8423                         if (offset_expr.X_add_number == 0)
8424                           offset_expr.X_op = O_absent;
8425                       }
8426                     else if (sizeof (imm_expr.X_add_number) > 4)
8427                       {
8428                         imm_expr.X_op = O_constant;
8429                         if (! target_big_endian)
8430                           imm_expr.X_add_number = bfd_getl64 (temp);
8431                         else
8432                           imm_expr.X_add_number = bfd_getb64 (temp);
8433                       }
8434                     else
8435                       {
8436                         imm_expr.X_op = O_big;
8437                         imm_expr.X_add_number = 4;
8438                         if (! target_big_endian)
8439                           {
8440                             generic_bignum[0] = bfd_getl16 (temp);
8441                             generic_bignum[1] = bfd_getl16 (temp + 2);
8442                             generic_bignum[2] = bfd_getl16 (temp + 4);
8443                             generic_bignum[3] = bfd_getl16 (temp + 6);
8444                           }
8445                         else
8446                           {
8447                             generic_bignum[0] = bfd_getb16 (temp + 6);
8448                             generic_bignum[1] = bfd_getb16 (temp + 4);
8449                             generic_bignum[2] = bfd_getb16 (temp + 2);
8450                             generic_bignum[3] = bfd_getb16 (temp);
8451                           }
8452                       }
8453                   }
8454                 else
8455                   {
8456                     const char *newname;
8457                     segT new_seg;
8458
8459                     /* Switch to the right section.  */
8460                     seg = now_seg;
8461                     subseg = now_subseg;
8462                     switch (*args)
8463                       {
8464                       default: /* unused default case avoids warnings.  */
8465                       case 'L':
8466                         newname = RDATA_SECTION_NAME;
8467                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8468                             || mips_pic == EMBEDDED_PIC)
8469                           newname = ".lit8";
8470                         break;
8471                       case 'F':
8472                         if (mips_pic == EMBEDDED_PIC)
8473                           newname = ".lit8";
8474                         else
8475                           newname = RDATA_SECTION_NAME;
8476                         break;
8477                       case 'l':
8478                         assert (!USE_GLOBAL_POINTER_OPT
8479                                 || g_switch_value >= 4);
8480                         newname = ".lit4";
8481                         break;
8482                       }
8483                     new_seg = subseg_new (newname, (subsegT) 0);
8484                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8485                       bfd_set_section_flags (stdoutput, new_seg,
8486                                              (SEC_ALLOC
8487                                               | SEC_LOAD
8488                                               | SEC_READONLY
8489                                               | SEC_DATA));
8490                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8491                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8492                         && strcmp (TARGET_OS, "elf") != 0)
8493                       record_alignment (new_seg, 4);
8494                     else
8495                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8496                     if (seg == now_seg)
8497                       as_bad (_("Can't use floating point insn in this section"));
8498
8499                     /* Set the argument to the current address in the
8500                        section.  */
8501                     offset_expr.X_op = O_symbol;
8502                     offset_expr.X_add_symbol =
8503                       symbol_new ("L0\001", now_seg,
8504                                   (valueT) frag_now_fix (), frag_now);
8505                     offset_expr.X_add_number = 0;
8506
8507                     /* Put the floating point number into the section.  */
8508                     p = frag_more ((int) length);
8509                     memcpy (p, temp, length);
8510
8511                     /* Switch back to the original section.  */
8512                     subseg_set (seg, subseg);
8513                   }
8514               }
8515               continue;
8516
8517             case 'i':           /* 16 bit unsigned immediate */
8518             case 'j':           /* 16 bit signed immediate */
8519               *imm_reloc = BFD_RELOC_LO16;
8520               c = my_getSmallExpression (&imm_expr, s);
8521               if (c != S_EX_NONE)
8522                 {
8523                   if (c != S_EX_LO)
8524                     {
8525                       if (imm_expr.X_op == O_constant)
8526                         imm_expr.X_add_number =
8527                           (imm_expr.X_add_number >> 16) & 0xffff;
8528 #ifdef OBJ_ELF
8529                       else if (c == S_EX_HIGHEST)
8530                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8531                       else if (c == S_EX_HIGHER)
8532                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8533                       else if (c == S_EX_GP_REL)
8534                         {
8535                           /* This occurs in NewABI only.  */
8536                           c = my_getSmallExpression (&imm_expr, s);
8537                           if (c != S_EX_NEG)
8538                             as_bad (_("bad composition of relocations"));
8539                           else
8540                             {
8541                               c = my_getSmallExpression (&imm_expr, s);
8542                               if (c != S_EX_LO)
8543                                 as_bad (_("bad composition of relocations"));
8544                               else
8545                                 {
8546                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8547                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8548                                   imm_reloc[2] = BFD_RELOC_LO16;
8549                                 }
8550                             }
8551                         }
8552 #endif
8553                       else if (c == S_EX_HI)
8554                         {
8555                           *imm_reloc = BFD_RELOC_HI16_S;
8556                           imm_unmatched_hi = true;
8557                         }
8558                       else
8559                         *imm_reloc = BFD_RELOC_HI16;
8560                     }
8561                   else if (imm_expr.X_op == O_constant)
8562                     imm_expr.X_add_number &= 0xffff;
8563                 }
8564               if (*args == 'i')
8565                 {
8566                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8567                       || ((imm_expr.X_add_number < 0
8568                            || imm_expr.X_add_number >= 0x10000)
8569                           && imm_expr.X_op == O_constant))
8570                     {
8571                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8572                           !strcmp (insn->name, insn[1].name))
8573                         break;
8574                       if (imm_expr.X_op == O_constant
8575                           || imm_expr.X_op == O_big)
8576                         as_bad (_("16 bit expression not in range 0..65535"));
8577                     }
8578                 }
8579               else
8580                 {
8581                   int more;
8582                   offsetT max;
8583
8584                   /* The upper bound should be 0x8000, but
8585                      unfortunately the MIPS assembler accepts numbers
8586                      from 0x8000 to 0xffff and sign extends them, and
8587                      we want to be compatible.  We only permit this
8588                      extended range for an instruction which does not
8589                      provide any further alternates, since those
8590                      alternates may handle other cases.  People should
8591                      use the numbers they mean, rather than relying on
8592                      a mysterious sign extension.  */
8593                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8594                           strcmp (insn->name, insn[1].name) == 0);
8595                   if (more)
8596                     max = 0x8000;
8597                   else
8598                     max = 0x10000;
8599                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8600                       || ((imm_expr.X_add_number < -0x8000
8601                            || imm_expr.X_add_number >= max)
8602                           && imm_expr.X_op == O_constant)
8603                       || (more
8604                           && imm_expr.X_add_number < 0
8605                           && HAVE_64BIT_GPRS
8606                           && imm_expr.X_unsigned
8607                           && sizeof (imm_expr.X_add_number) <= 4))
8608                     {
8609                       if (more)
8610                         break;
8611                       if (imm_expr.X_op == O_constant
8612                           || imm_expr.X_op == O_big)
8613                         as_bad (_("16 bit expression not in range -32768..32767"));
8614                     }
8615                 }
8616               s = expr_end;
8617               continue;
8618
8619             case 'o':           /* 16 bit offset */
8620               c = my_getSmallExpression (&offset_expr, s);
8621
8622               /* If this value won't fit into a 16 bit offset, then go
8623                  find a macro that will generate the 32 bit offset
8624                  code pattern.  */
8625               if (c == S_EX_NONE
8626                   && (offset_expr.X_op != O_constant
8627                       || offset_expr.X_add_number >= 0x8000
8628                       || offset_expr.X_add_number < -0x8000))
8629                 break;
8630
8631               if (c == S_EX_HI)
8632                 {
8633                   if (offset_expr.X_op != O_constant)
8634                     break;
8635                   offset_expr.X_add_number =
8636                     (offset_expr.X_add_number >> 16) & 0xffff;
8637                 }
8638               *offset_reloc = BFD_RELOC_LO16;
8639               s = expr_end;
8640               continue;
8641
8642             case 'p':           /* pc relative offset */
8643               if (mips_pic == EMBEDDED_PIC)
8644                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8645               else
8646                 *offset_reloc = BFD_RELOC_16_PCREL;
8647               my_getExpression (&offset_expr, s);
8648               s = expr_end;
8649               continue;
8650
8651             case 'u':           /* upper 16 bits */
8652               c = my_getSmallExpression (&imm_expr, s);
8653               *imm_reloc = BFD_RELOC_LO16;
8654               if (c != S_EX_NONE)
8655                 {
8656                   if (c != S_EX_LO)
8657                     {
8658                       if (imm_expr.X_op == O_constant)
8659                         imm_expr.X_add_number =
8660                           (imm_expr.X_add_number >> 16) & 0xffff;
8661                       else if (c == S_EX_HI)
8662                         {
8663                           *imm_reloc = BFD_RELOC_HI16_S;
8664                           imm_unmatched_hi = true;
8665                         }
8666 #ifdef OBJ_ELF
8667                       else if (c == S_EX_HIGHEST)
8668                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8669                       else if (c == S_EX_GP_REL)
8670                         {
8671                           /* This occurs in NewABI only.  */
8672                           c = my_getSmallExpression (&imm_expr, s);
8673                           if (c != S_EX_NEG)
8674                             as_bad (_("bad composition of relocations"));
8675                           else
8676                             {
8677                               c = my_getSmallExpression (&imm_expr, s);
8678                               if (c != S_EX_HI)
8679                                 as_bad (_("bad composition of relocations"));
8680                               else
8681                                 {
8682                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8683                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8684                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8685                                 }
8686                             }
8687                         }
8688 #endif
8689                       else
8690                         *imm_reloc = BFD_RELOC_HI16;
8691                     }
8692                   else if (imm_expr.X_op == O_constant)
8693                     imm_expr.X_add_number &= 0xffff;
8694                 }
8695               if (imm_expr.X_op == O_constant
8696                   && (imm_expr.X_add_number < 0
8697                       || imm_expr.X_add_number >= 0x10000))
8698                 as_bad (_("lui expression not in range 0..65535"));
8699               s = expr_end;
8700               continue;
8701
8702             case 'a':           /* 26 bit address */
8703               my_getExpression (&offset_expr, s);
8704               s = expr_end;
8705               *offset_reloc = BFD_RELOC_MIPS_JMP;
8706               continue;
8707
8708             case 'N':           /* 3 bit branch condition code */
8709             case 'M':           /* 3 bit compare condition code */
8710               if (strncmp (s, "$fcc", 4) != 0)
8711                 break;
8712               s += 4;
8713               regno = 0;
8714               do
8715                 {
8716                   regno *= 10;
8717                   regno += *s - '0';
8718                   ++s;
8719                 }
8720               while (ISDIGIT (*s));
8721               if (regno > 7)
8722                 as_bad (_("invalid condition code register $fcc%d"), regno);
8723               if (*args == 'N')
8724                 ip->insn_opcode |= regno << OP_SH_BCC;
8725               else
8726                 ip->insn_opcode |= regno << OP_SH_CCC;
8727               continue;
8728
8729             case 'H':
8730               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8731                 s += 2;
8732               if (ISDIGIT (*s))
8733                 {
8734                   c = 0;
8735                   do
8736                     {
8737                       c *= 10;
8738                       c += *s - '0';
8739                       ++s;
8740                     }
8741                   while (ISDIGIT (*s));
8742                 }
8743               else
8744                 c = 8; /* Invalid sel value.  */
8745
8746               if (c > 7)
8747                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8748               ip->insn_opcode |= c;
8749               continue;
8750
8751             default:
8752               as_bad (_("bad char = '%c'\n"), *args);
8753               internalError ();
8754             }
8755           break;
8756         }
8757       /* Args don't match.  */
8758       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8759           !strcmp (insn->name, insn[1].name))
8760         {
8761           ++insn;
8762           s = argsStart;
8763           insn_error = _("illegal operands");
8764           continue;
8765         }
8766       if (save_c)
8767         *(--s) = save_c;
8768       insn_error = _("illegal operands");
8769       return;
8770     }
8771 }
8772
8773 /* This routine assembles an instruction into its binary format when
8774    assembling for the mips16.  As a side effect, it sets one of the
8775    global variables imm_reloc or offset_reloc to the type of
8776    relocation to do if one of the operands is an address expression.
8777    It also sets mips16_small and mips16_ext if the user explicitly
8778    requested a small or extended instruction.  */
8779
8780 static void
8781 mips16_ip (str, ip)
8782      char *str;
8783      struct mips_cl_insn *ip;
8784 {
8785   char *s;
8786   const char *args;
8787   struct mips_opcode *insn;
8788   char *argsstart;
8789   unsigned int regno;
8790   unsigned int lastregno = 0;
8791   char *s_reset;
8792
8793   insn_error = NULL;
8794
8795   mips16_small = false;
8796   mips16_ext = false;
8797
8798   for (s = str; ISLOWER (*s); ++s)
8799     ;
8800   switch (*s)
8801     {
8802     case '\0':
8803       break;
8804
8805     case ' ':
8806       *s++ = '\0';
8807       break;
8808
8809     case '.':
8810       if (s[1] == 't' && s[2] == ' ')
8811         {
8812           *s = '\0';
8813           mips16_small = true;
8814           s += 3;
8815           break;
8816         }
8817       else if (s[1] == 'e' && s[2] == ' ')
8818         {
8819           *s = '\0';
8820           mips16_ext = true;
8821           s += 3;
8822           break;
8823         }
8824       /* Fall through.  */
8825     default:
8826       insn_error = _("unknown opcode");
8827       return;
8828     }
8829
8830   if (mips_opts.noautoextend && ! mips16_ext)
8831     mips16_small = true;
8832
8833   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8834     {
8835       insn_error = _("unrecognized opcode");
8836       return;
8837     }
8838
8839   argsstart = s;
8840   for (;;)
8841     {
8842       assert (strcmp (insn->name, str) == 0);
8843
8844       ip->insn_mo = insn;
8845       ip->insn_opcode = insn->match;
8846       ip->use_extend = false;
8847       imm_expr.X_op = O_absent;
8848       imm_reloc[0] = BFD_RELOC_UNUSED;
8849       imm_reloc[1] = BFD_RELOC_UNUSED;
8850       imm_reloc[2] = BFD_RELOC_UNUSED;
8851       offset_expr.X_op = O_absent;
8852       offset_reloc[0] = BFD_RELOC_UNUSED;
8853       offset_reloc[1] = BFD_RELOC_UNUSED;
8854       offset_reloc[2] = BFD_RELOC_UNUSED;
8855       for (args = insn->args; 1; ++args)
8856         {
8857           int c;
8858
8859           if (*s == ' ')
8860             ++s;
8861
8862           /* In this switch statement we call break if we did not find
8863              a match, continue if we did find a match, or return if we
8864              are done.  */
8865
8866           c = *args;
8867           switch (c)
8868             {
8869             case '\0':
8870               if (*s == '\0')
8871                 {
8872                   /* Stuff the immediate value in now, if we can.  */
8873                   if (imm_expr.X_op == O_constant
8874                       && *imm_reloc > BFD_RELOC_UNUSED
8875                       && insn->pinfo != INSN_MACRO)
8876                     {
8877                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8878                                     imm_expr.X_add_number, true, mips16_small,
8879                                     mips16_ext, &ip->insn_opcode,
8880                                     &ip->use_extend, &ip->extend);
8881                       imm_expr.X_op = O_absent;
8882                       *imm_reloc = BFD_RELOC_UNUSED;
8883                     }
8884
8885                   return;
8886                 }
8887               break;
8888
8889             case ',':
8890               if (*s++ == c)
8891                 continue;
8892               s--;
8893               switch (*++args)
8894                 {
8895                 case 'v':
8896                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8897                   continue;
8898                 case 'w':
8899                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8900                   continue;
8901                 }
8902               break;
8903
8904             case '(':
8905             case ')':
8906               if (*s++ == c)
8907                 continue;
8908               break;
8909
8910             case 'v':
8911             case 'w':
8912               if (s[0] != '$')
8913                 {
8914                   if (c == 'v')
8915                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8916                   else
8917                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8918                   ++args;
8919                   continue;
8920                 }
8921               /* Fall through.  */
8922             case 'x':
8923             case 'y':
8924             case 'z':
8925             case 'Z':
8926             case '0':
8927             case 'S':
8928             case 'R':
8929             case 'X':
8930             case 'Y':
8931               if (s[0] != '$')
8932                 break;
8933               s_reset = s;
8934               if (ISDIGIT (s[1]))
8935                 {
8936                   ++s;
8937                   regno = 0;
8938                   do
8939                     {
8940                       regno *= 10;
8941                       regno += *s - '0';
8942                       ++s;
8943                     }
8944                   while (ISDIGIT (*s));
8945                   if (regno > 31)
8946                     {
8947                       as_bad (_("invalid register number (%d)"), regno);
8948                       regno = 2;
8949                     }
8950                 }
8951               else
8952                 {
8953                   if (s[1] == 'r' && s[2] == 'a')
8954                     {
8955                       s += 3;
8956                       regno = RA;
8957                     }
8958                   else if (s[1] == 'f' && s[2] == 'p')
8959                     {
8960                       s += 3;
8961                       regno = FP;
8962                     }
8963                   else if (s[1] == 's' && s[2] == 'p')
8964                     {
8965                       s += 3;
8966                       regno = SP;
8967                     }
8968                   else if (s[1] == 'g' && s[2] == 'p')
8969                     {
8970                       s += 3;
8971                       regno = GP;
8972                     }
8973                   else if (s[1] == 'a' && s[2] == 't')
8974                     {
8975                       s += 3;
8976                       regno = AT;
8977                     }
8978                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8979                     {
8980                       s += 4;
8981                       regno = KT0;
8982                     }
8983                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8984                     {
8985                       s += 4;
8986                       regno = KT1;
8987                     }
8988                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8989                     {
8990                       s += 5;
8991                       regno = ZERO;
8992                     }
8993                   else
8994                     break;
8995                 }
8996
8997               if (*s == ' ')
8998                 ++s;
8999               if (args[1] != *s)
9000                 {
9001                   if (c == 'v' || c == 'w')
9002                     {
9003                       regno = mips16_to_32_reg_map[lastregno];
9004                       s = s_reset;
9005                       ++args;
9006                     }
9007                 }
9008
9009               switch (c)
9010                 {
9011                 case 'x':
9012                 case 'y':
9013                 case 'z':
9014                 case 'v':
9015                 case 'w':
9016                 case 'Z':
9017                   regno = mips32_to_16_reg_map[regno];
9018                   break;
9019
9020                 case '0':
9021                   if (regno != 0)
9022                     regno = ILLEGAL_REG;
9023                   break;
9024
9025                 case 'S':
9026                   if (regno != SP)
9027                     regno = ILLEGAL_REG;
9028                   break;
9029
9030                 case 'R':
9031                   if (regno != RA)
9032                     regno = ILLEGAL_REG;
9033                   break;
9034
9035                 case 'X':
9036                 case 'Y':
9037                   if (regno == AT && ! mips_opts.noat)
9038                     as_warn (_("used $at without \".set noat\""));
9039                   break;
9040
9041                 default:
9042                   internalError ();
9043                 }
9044
9045               if (regno == ILLEGAL_REG)
9046                 break;
9047
9048               switch (c)
9049                 {
9050                 case 'x':
9051                 case 'v':
9052                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9053                   break;
9054                 case 'y':
9055                 case 'w':
9056                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9057                   break;
9058                 case 'z':
9059                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9060                   break;
9061                 case 'Z':
9062                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9063                 case '0':
9064                 case 'S':
9065                 case 'R':
9066                   break;
9067                 case 'X':
9068                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9069                   break;
9070                 case 'Y':
9071                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9072                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9073                   break;
9074                 default:
9075                   internalError ();
9076                 }
9077
9078               lastregno = regno;
9079               continue;
9080
9081             case 'P':
9082               if (strncmp (s, "$pc", 3) == 0)
9083                 {
9084                   s += 3;
9085                   continue;
9086                 }
9087               break;
9088
9089             case '<':
9090             case '>':
9091             case '[':
9092             case ']':
9093             case '4':
9094             case '5':
9095             case 'H':
9096             case 'W':
9097             case 'D':
9098             case 'j':
9099             case '8':
9100             case 'V':
9101             case 'C':
9102             case 'U':
9103             case 'k':
9104             case 'K':
9105               if (s[0] == '%'
9106                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9107                 {
9108                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9109                      and generate the appropriate reloc.  If the text
9110                      inside %gprel is not a symbol name with an
9111                      optional offset, then we generate a normal reloc
9112                      and will probably fail later.  */
9113                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9114                   if (imm_expr.X_op == O_symbol)
9115                     {
9116                       mips16_ext = true;
9117                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9118                       s = expr_end;
9119                       ip->use_extend = true;
9120                       ip->extend = 0;
9121                       continue;
9122                     }
9123                 }
9124               else
9125                 {
9126                   /* Just pick up a normal expression.  */
9127                   my_getExpression (&imm_expr, s);
9128                 }
9129
9130               if (imm_expr.X_op == O_register)
9131                 {
9132                   /* What we thought was an expression turned out to
9133                      be a register.  */
9134
9135                   if (s[0] == '(' && args[1] == '(')
9136                     {
9137                       /* It looks like the expression was omitted
9138                          before a register indirection, which means
9139                          that the expression is implicitly zero.  We
9140                          still set up imm_expr, so that we handle
9141                          explicit extensions correctly.  */
9142                       imm_expr.X_op = O_constant;
9143                       imm_expr.X_add_number = 0;
9144                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9145                       continue;
9146                     }
9147
9148                   break;
9149                 }
9150
9151               /* We need to relax this instruction.  */
9152               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9153               s = expr_end;
9154               continue;
9155
9156             case 'p':
9157             case 'q':
9158             case 'A':
9159             case 'B':
9160             case 'E':
9161               /* We use offset_reloc rather than imm_reloc for the PC
9162                  relative operands.  This lets macros with both
9163                  immediate and address operands work correctly.  */
9164               my_getExpression (&offset_expr, s);
9165
9166               if (offset_expr.X_op == O_register)
9167                 break;
9168
9169               /* We need to relax this instruction.  */
9170               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9171               s = expr_end;
9172               continue;
9173
9174             case '6':           /* break code */
9175               my_getExpression (&imm_expr, s);
9176               check_absolute_expr (ip, &imm_expr);
9177               if ((unsigned long) imm_expr.X_add_number > 63)
9178                 {
9179                   as_warn (_("Invalid value for `%s' (%lu)"),
9180                            ip->insn_mo->name,
9181                            (unsigned long) imm_expr.X_add_number);
9182                   imm_expr.X_add_number &= 0x3f;
9183                 }
9184               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9185               imm_expr.X_op = O_absent;
9186               s = expr_end;
9187               continue;
9188
9189             case 'a':           /* 26 bit address */
9190               my_getExpression (&offset_expr, s);
9191               s = expr_end;
9192               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9193               ip->insn_opcode <<= 16;
9194               continue;
9195
9196             case 'l':           /* register list for entry macro */
9197             case 'L':           /* register list for exit macro */
9198               {
9199                 int mask;
9200
9201                 if (c == 'l')
9202                   mask = 0;
9203                 else
9204                   mask = 7 << 3;
9205                 while (*s != '\0')
9206                   {
9207                     int freg, reg1, reg2;
9208
9209                     while (*s == ' ' || *s == ',')
9210                       ++s;
9211                     if (*s != '$')
9212                       {
9213                         as_bad (_("can't parse register list"));
9214                         break;
9215                       }
9216                     ++s;
9217                     if (*s != 'f')
9218                       freg = 0;
9219                     else
9220                       {
9221                         freg = 1;
9222                         ++s;
9223                       }
9224                     reg1 = 0;
9225                     while (ISDIGIT (*s))
9226                       {
9227                         reg1 *= 10;
9228                         reg1 += *s - '0';
9229                         ++s;
9230                       }
9231                     if (*s == ' ')
9232                       ++s;
9233                     if (*s != '-')
9234                       reg2 = reg1;
9235                     else
9236                       {
9237                         ++s;
9238                         if (*s != '$')
9239                           break;
9240                         ++s;
9241                         if (freg)
9242                           {
9243                             if (*s == 'f')
9244                               ++s;
9245                             else
9246                               {
9247                                 as_bad (_("invalid register list"));
9248                                 break;
9249                               }
9250                           }
9251                         reg2 = 0;
9252                         while (ISDIGIT (*s))
9253                           {
9254                             reg2 *= 10;
9255                             reg2 += *s - '0';
9256                             ++s;
9257                           }
9258                       }
9259                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9260                       {
9261                         mask &= ~ (7 << 3);
9262                         mask |= 5 << 3;
9263                       }
9264                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9265                       {
9266                         mask &= ~ (7 << 3);
9267                         mask |= 6 << 3;
9268                       }
9269                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9270                       mask |= (reg2 - 3) << 3;
9271                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9272                       mask |= (reg2 - 15) << 1;
9273                     else if (reg1 == RA && reg2 == RA)
9274                       mask |= 1;
9275                     else
9276                       {
9277                         as_bad (_("invalid register list"));
9278                         break;
9279                       }
9280                   }
9281                 /* The mask is filled in in the opcode table for the
9282                    benefit of the disassembler.  We remove it before
9283                    applying the actual mask.  */
9284                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9285                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9286               }
9287             continue;
9288
9289             case 'e':           /* extend code */
9290               my_getExpression (&imm_expr, s);
9291               check_absolute_expr (ip, &imm_expr);
9292               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9293                 {
9294                   as_warn (_("Invalid value for `%s' (%lu)"),
9295                            ip->insn_mo->name,
9296                            (unsigned long) imm_expr.X_add_number);
9297                   imm_expr.X_add_number &= 0x7ff;
9298                 }
9299               ip->insn_opcode |= imm_expr.X_add_number;
9300               imm_expr.X_op = O_absent;
9301               s = expr_end;
9302               continue;
9303
9304             default:
9305               internalError ();
9306             }
9307           break;
9308         }
9309
9310       /* Args don't match.  */
9311       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9312           strcmp (insn->name, insn[1].name) == 0)
9313         {
9314           ++insn;
9315           s = argsstart;
9316           continue;
9317         }
9318
9319       insn_error = _("illegal operands");
9320
9321       return;
9322     }
9323 }
9324
9325 /* This structure holds information we know about a mips16 immediate
9326    argument type.  */
9327
9328 struct mips16_immed_operand
9329 {
9330   /* The type code used in the argument string in the opcode table.  */
9331   int type;
9332   /* The number of bits in the short form of the opcode.  */
9333   int nbits;
9334   /* The number of bits in the extended form of the opcode.  */
9335   int extbits;
9336   /* The amount by which the short form is shifted when it is used;
9337      for example, the sw instruction has a shift count of 2.  */
9338   int shift;
9339   /* The amount by which the short form is shifted when it is stored
9340      into the instruction code.  */
9341   int op_shift;
9342   /* Non-zero if the short form is unsigned.  */
9343   int unsp;
9344   /* Non-zero if the extended form is unsigned.  */
9345   int extu;
9346   /* Non-zero if the value is PC relative.  */
9347   int pcrel;
9348 };
9349
9350 /* The mips16 immediate operand types.  */
9351
9352 static const struct mips16_immed_operand mips16_immed_operands[] =
9353 {
9354   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9355   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9356   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9357   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9358   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9359   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9360   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9361   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9362   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9363   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9364   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9365   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9366   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9367   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9368   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9369   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9370   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9371   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9372   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9373   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9374   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9375 };
9376
9377 #define MIPS16_NUM_IMMED \
9378   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9379
9380 /* Handle a mips16 instruction with an immediate value.  This or's the
9381    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9382    whether an extended value is needed; if one is needed, it sets
9383    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9384    If SMALL is true, an unextended opcode was explicitly requested.
9385    If EXT is true, an extended opcode was explicitly requested.  If
9386    WARN is true, warn if EXT does not match reality.  */
9387
9388 static void
9389 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9390               extend)
9391      char *file;
9392      unsigned int line;
9393      int type;
9394      offsetT val;
9395      boolean warn;
9396      boolean small;
9397      boolean ext;
9398      unsigned long *insn;
9399      boolean *use_extend;
9400      unsigned short *extend;
9401 {
9402   register const struct mips16_immed_operand *op;
9403   int mintiny, maxtiny;
9404   boolean needext;
9405
9406   op = mips16_immed_operands;
9407   while (op->type != type)
9408     {
9409       ++op;
9410       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9411     }
9412
9413   if (op->unsp)
9414     {
9415       if (type == '<' || type == '>' || type == '[' || type == ']')
9416         {
9417           mintiny = 1;
9418           maxtiny = 1 << op->nbits;
9419         }
9420       else
9421         {
9422           mintiny = 0;
9423           maxtiny = (1 << op->nbits) - 1;
9424         }
9425     }
9426   else
9427     {
9428       mintiny = - (1 << (op->nbits - 1));
9429       maxtiny = (1 << (op->nbits - 1)) - 1;
9430     }
9431
9432   /* Branch offsets have an implicit 0 in the lowest bit.  */
9433   if (type == 'p' || type == 'q')
9434     val /= 2;
9435
9436   if ((val & ((1 << op->shift) - 1)) != 0
9437       || val < (mintiny << op->shift)
9438       || val > (maxtiny << op->shift))
9439     needext = true;
9440   else
9441     needext = false;
9442
9443   if (warn && ext && ! needext)
9444     as_warn_where (file, line,
9445                    _("extended operand requested but not required"));
9446   if (small && needext)
9447     as_bad_where (file, line, _("invalid unextended operand value"));
9448
9449   if (small || (! ext && ! needext))
9450     {
9451       int insnval;
9452
9453       *use_extend = false;
9454       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9455       insnval <<= op->op_shift;
9456       *insn |= insnval;
9457     }
9458   else
9459     {
9460       long minext, maxext;
9461       int extval;
9462
9463       if (op->extu)
9464         {
9465           minext = 0;
9466           maxext = (1 << op->extbits) - 1;
9467         }
9468       else
9469         {
9470           minext = - (1 << (op->extbits - 1));
9471           maxext = (1 << (op->extbits - 1)) - 1;
9472         }
9473       if (val < minext || val > maxext)
9474         as_bad_where (file, line,
9475                       _("operand value out of range for instruction"));
9476
9477       *use_extend = true;
9478       if (op->extbits == 16)
9479         {
9480           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9481           val &= 0x1f;
9482         }
9483       else if (op->extbits == 15)
9484         {
9485           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9486           val &= 0xf;
9487         }
9488       else
9489         {
9490           extval = ((val & 0x1f) << 6) | (val & 0x20);
9491           val = 0;
9492         }
9493
9494       *extend = (unsigned short) extval;
9495       *insn |= val;
9496     }
9497 }
9498 \f
9499 static struct percent_op_match
9500 {
9501    const char *str;
9502    const enum small_ex_type type;
9503 } percent_op[] =
9504 {
9505   {"%lo", S_EX_LO},
9506 #ifdef OBJ_ELF
9507   {"%call_hi", S_EX_CALL_HI},
9508   {"%call_lo", S_EX_CALL_LO},
9509   {"%call16", S_EX_CALL16},
9510   {"%got_disp", S_EX_GOT_DISP},
9511   {"%got_page", S_EX_GOT_PAGE},
9512   {"%got_ofst", S_EX_GOT_OFST},
9513   {"%got_hi", S_EX_GOT_HI},
9514   {"%got_lo", S_EX_GOT_LO},
9515   {"%got", S_EX_GOT},
9516   {"%gp_rel", S_EX_GP_REL},
9517   {"%half", S_EX_HALF},
9518   {"%highest", S_EX_HIGHEST},
9519   {"%higher", S_EX_HIGHER},
9520   {"%neg", S_EX_NEG},
9521 #endif
9522   {"%hi", S_EX_HI}
9523 };
9524
9525 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9526    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9527    can be nested, this is handled by blanking the innermost, parsing the
9528    rest by subsequent calls.  */
9529
9530 static int
9531 my_getSmallParser (str, len, nestlevel)
9532      char **str;
9533      unsigned int *len;
9534      int *nestlevel;
9535 {
9536   *len = 0;
9537   *str += strspn (*str, " \t");
9538   /* Check for expression in parentheses.  */
9539   if (**str == '(')
9540     {
9541       char *b = *str + 1 + strspn (*str + 1, " \t");
9542       char *e;
9543
9544       /* Check for base register.  */
9545       if (b[0] == '$')
9546         {
9547           if (strchr (b, ')')
9548               && (e = b + strcspn (b, ") \t"))
9549               && e - b > 1 && e - b < 4)
9550             {
9551               if ((e - b == 3
9552                    && ((b[1] == 'f' && b[2] == 'p')
9553                        || (b[1] == 's' && b[2] == 'p')
9554                        || (b[1] == 'g' && b[2] == 'p')
9555                        || (b[1] == 'a' && b[2] == 't')
9556                        || (ISDIGIT (b[1])
9557                            && ISDIGIT (b[2]))))
9558                   || (ISDIGIT (b[1])))
9559                 {
9560                   *len = strcspn (*str, ")") + 1;
9561                   return S_EX_REGISTER;
9562                 }
9563             }
9564         }
9565       /* Check for percent_op (in parentheses).  */
9566       else if (b[0] == '%')
9567         {
9568           *str = b;
9569           return my_getPercentOp (str, len, nestlevel);
9570         }
9571
9572       /* Some other expression in the parentheses, which can contain
9573          parentheses itself. Attempt to find the matching one.  */
9574       {
9575         int pcnt = 1;
9576         char *s;
9577
9578         *len = 1;
9579         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9580           {
9581             if (*s == '(')
9582               ++pcnt;
9583             else if (*s == ')')
9584               --pcnt;
9585           }
9586       }
9587     }
9588   /* Check for percent_op (outside of parentheses).  */
9589   else if (*str[0] == '%')
9590     return my_getPercentOp (str, len, nestlevel);
9591
9592   /* Any other expression.  */
9593   return S_EX_NONE;
9594 }
9595
9596 static int
9597 my_getPercentOp (str, len, nestlevel)
9598      char **str;
9599      unsigned int *len;
9600      int *nestlevel;
9601 {
9602   char *tmp = *str + 1;
9603   unsigned int i = 0;
9604
9605   while (ISALPHA (*tmp) || *tmp == '_')
9606     {
9607       *tmp = TOLOWER (*tmp);
9608       tmp++;
9609     }
9610   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9611     {
9612       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9613         i++;
9614       else
9615         {
9616           int type = percent_op[i].type;
9617
9618           /* Only %hi and %lo are allowed for OldABI.  */
9619           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9620             return S_EX_NONE;
9621
9622           *len = strlen (percent_op[i].str);
9623           ++(*nestlevel);
9624           return type;
9625         }
9626     }
9627   return S_EX_NONE;
9628 }
9629
9630 static int
9631 my_getSmallExpression (ep, str)
9632      expressionS *ep;
9633      char *str;
9634 {
9635   static char *oldstr = NULL;
9636   int c = S_EX_NONE;
9637   int oldc;
9638   int nestlevel = -1;
9639   unsigned int len;
9640
9641   /* Don't update oldstr if the last call had nested percent_op's. We need
9642      it to parse the outer ones later.  */
9643   if (! oldstr)
9644     oldstr = str;
9645
9646   do
9647     {
9648       oldc = c;
9649       c = my_getSmallParser (&str, &len, &nestlevel);
9650       if (c != S_EX_NONE && c != S_EX_REGISTER)
9651         str += len;
9652     }
9653   while (c != S_EX_NONE && c != S_EX_REGISTER);
9654
9655   if (nestlevel >= 0)
9656     {
9657       /* A percent_op was encountered.  Don't try to get an expression if
9658          it is already blanked out.  */
9659       if (*(str + strspn (str + 1, " )")) != ')')
9660         {
9661           char save;
9662
9663           /* Let my_getExpression() stop at the closing parenthesis.  */
9664           save = *(str + len);
9665           *(str + len) = '\0';
9666           my_getExpression (ep, str);
9667           *(str + len) = save;
9668         }
9669       if (nestlevel > 0)
9670         {
9671           /* Blank out including the % sign and the proper matching
9672              parenthesis.  */
9673           int pcnt = 1;
9674           char *s = strrchr (oldstr, '%');
9675           char *end;
9676
9677           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9678             {
9679               if (*end == '(')
9680                 ++pcnt;
9681               else if (*end == ')')
9682                 --pcnt;
9683             }
9684
9685           memset (s, ' ', end - s);
9686           str = oldstr;
9687         }
9688       else
9689         expr_end = str + len;
9690
9691       c = oldc;
9692     }
9693   else if (c == S_EX_NONE)
9694     {
9695       my_getExpression (ep, str);
9696     }
9697   else if (c == S_EX_REGISTER)
9698     {
9699       ep->X_op = O_constant;
9700       expr_end = str;
9701       ep->X_add_symbol = NULL;
9702       ep->X_op_symbol = NULL;
9703       ep->X_add_number = 0;
9704     }
9705   else
9706     {
9707       as_fatal (_("internal error"));
9708     }
9709
9710   if (nestlevel <= 0)
9711     /* All percent_op's have been handled.  */
9712     oldstr = NULL;
9713
9714   return c;
9715 }
9716
9717 static void
9718 my_getExpression (ep, str)
9719      expressionS *ep;
9720      char *str;
9721 {
9722   char *save_in;
9723   valueT val;
9724
9725   save_in = input_line_pointer;
9726   input_line_pointer = str;
9727   expression (ep);
9728   expr_end = input_line_pointer;
9729   input_line_pointer = save_in;
9730
9731   /* If we are in mips16 mode, and this is an expression based on `.',
9732      then we bump the value of the symbol by 1 since that is how other
9733      text symbols are handled.  We don't bother to handle complex
9734      expressions, just `.' plus or minus a constant.  */
9735   if (mips_opts.mips16
9736       && ep->X_op == O_symbol
9737       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9738       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9739       && symbol_get_frag (ep->X_add_symbol) == frag_now
9740       && symbol_constant_p (ep->X_add_symbol)
9741       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9742     S_SET_VALUE (ep->X_add_symbol, val + 1);
9743 }
9744
9745 /* Turn a string in input_line_pointer into a floating point constant
9746    of type TYPE, and store the appropriate bytes in *LITP.  The number
9747    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9748    returned, or NULL on OK.  */
9749
9750 char *
9751 md_atof (type, litP, sizeP)
9752      int type;
9753      char *litP;
9754      int *sizeP;
9755 {
9756   int prec;
9757   LITTLENUM_TYPE words[4];
9758   char *t;
9759   int i;
9760
9761   switch (type)
9762     {
9763     case 'f':
9764       prec = 2;
9765       break;
9766
9767     case 'd':
9768       prec = 4;
9769       break;
9770
9771     default:
9772       *sizeP = 0;
9773       return _("bad call to md_atof");
9774     }
9775
9776   t = atof_ieee (input_line_pointer, type, words);
9777   if (t)
9778     input_line_pointer = t;
9779
9780   *sizeP = prec * 2;
9781
9782   if (! target_big_endian)
9783     {
9784       for (i = prec - 1; i >= 0; i--)
9785         {
9786           md_number_to_chars (litP, (valueT) words[i], 2);
9787           litP += 2;
9788         }
9789     }
9790   else
9791     {
9792       for (i = 0; i < prec; i++)
9793         {
9794           md_number_to_chars (litP, (valueT) words[i], 2);
9795           litP += 2;
9796         }
9797     }
9798
9799   return NULL;
9800 }
9801
9802 void
9803 md_number_to_chars (buf, val, n)
9804      char *buf;
9805      valueT val;
9806      int n;
9807 {
9808   if (target_big_endian)
9809     number_to_chars_bigendian (buf, val, n);
9810   else
9811     number_to_chars_littleendian (buf, val, n);
9812 }
9813 \f
9814 #ifdef OBJ_ELF
9815 static int support_64bit_objects(void)
9816 {
9817   const char **list, **l;
9818
9819   list = bfd_target_list ();
9820   for (l = list; *l != NULL; l++)
9821 #ifdef TE_TMIPS
9822     /* This is traditional mips */
9823     if (strcmp (*l, "elf64-tradbigmips") == 0
9824         || strcmp (*l, "elf64-tradlittlemips") == 0)
9825 #else
9826     if (strcmp (*l, "elf64-bigmips") == 0
9827         || strcmp (*l, "elf64-littlemips") == 0)
9828 #endif
9829       break;
9830   free (list);
9831   return (*l != NULL);
9832 }
9833 #endif /* OBJ_ELF */
9834
9835 const char *md_shortopts = "nO::g::G:";
9836
9837 struct option md_longopts[] =
9838 {
9839 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9840   {"mips0", no_argument, NULL, OPTION_MIPS1},
9841   {"mips1", no_argument, NULL, OPTION_MIPS1},
9842 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9843   {"mips2", no_argument, NULL, OPTION_MIPS2},
9844 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9845   {"mips3", no_argument, NULL, OPTION_MIPS3},
9846 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9847   {"mips4", no_argument, NULL, OPTION_MIPS4},
9848 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9849   {"mips5", no_argument, NULL, OPTION_MIPS5},
9850 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9851   {"mips32", no_argument, NULL, OPTION_MIPS32},
9852 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9853   {"mips64", no_argument, NULL, OPTION_MIPS64},
9854 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9855   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9856 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9857   {"trap", no_argument, NULL, OPTION_TRAP},
9858   {"no-break", no_argument, NULL, OPTION_TRAP},
9859 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9860   {"break", no_argument, NULL, OPTION_BREAK},
9861   {"no-trap", no_argument, NULL, OPTION_BREAK},
9862 #define OPTION_EB (OPTION_MD_BASE + 11)
9863   {"EB", no_argument, NULL, OPTION_EB},
9864 #define OPTION_EL (OPTION_MD_BASE + 12)
9865   {"EL", no_argument, NULL, OPTION_EL},
9866 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9867   {"mips16", no_argument, NULL, OPTION_MIPS16},
9868 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9869   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9870 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9871   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9872 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9873   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9874   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9875 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9876   {"mfp32", no_argument, NULL, OPTION_FP32},
9877 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9878   {"mgp32", no_argument, NULL, OPTION_GP32},
9879 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9880   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9881 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9882   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9883 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9884   {"march", required_argument, NULL, OPTION_MARCH},
9885 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9886   {"mtune", required_argument, NULL, OPTION_MTUNE},
9887 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9888   {"mcpu", required_argument, NULL, OPTION_MCPU},
9889 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9890   {"m4650", no_argument, NULL, OPTION_M4650},
9891 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9892   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9893 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9894   {"m4010", no_argument, NULL, OPTION_M4010},
9895 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9896   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9897 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9898   {"m4100", no_argument, NULL, OPTION_M4100},
9899 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9900   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9901 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9902   {"m3900", no_argument, NULL, OPTION_M3900},
9903 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9904   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9905 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9906   {"mgp64", no_argument, NULL, OPTION_GP64},
9907 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9908   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9909 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9910   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9911 #define OPTION_MDMX (OPTION_MD_BASE + 35)
9912   {"mdmx", no_argument, NULL, OPTION_MDMX},
9913 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9914   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9915 #ifdef OBJ_ELF
9916 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 37)
9917 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9918   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9919   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9920 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
9921   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
9922 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
9923   {"xgot",        no_argument, NULL, OPTION_XGOT},
9924 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
9925   {"mabi", required_argument, NULL, OPTION_MABI},
9926 #define OPTION_32          (OPTION_ELF_BASE + 4)
9927   {"32",          no_argument, NULL, OPTION_32},
9928 #define OPTION_N32         (OPTION_ELF_BASE + 5)
9929   {"n32",         no_argument, NULL, OPTION_N32},
9930 #define OPTION_64          (OPTION_ELF_BASE + 6)
9931   {"64",          no_argument, NULL, OPTION_64},
9932 #endif /* OBJ_ELF */
9933   {NULL, no_argument, NULL, 0}
9934 };
9935 size_t md_longopts_size = sizeof (md_longopts);
9936
9937 int
9938 md_parse_option (c, arg)
9939      int c;
9940      char *arg;
9941 {
9942   switch (c)
9943     {
9944     case OPTION_CONSTRUCT_FLOATS:
9945       mips_disable_float_construction = 0;
9946       break;
9947
9948     case OPTION_NO_CONSTRUCT_FLOATS:
9949       mips_disable_float_construction = 1;
9950       break;
9951
9952     case OPTION_TRAP:
9953       mips_trap = 1;
9954       break;
9955
9956     case OPTION_BREAK:
9957       mips_trap = 0;
9958       break;
9959
9960     case OPTION_EB:
9961       target_big_endian = 1;
9962       break;
9963
9964     case OPTION_EL:
9965       target_big_endian = 0;
9966       break;
9967
9968     case 'n':
9969       warn_nops = 1;
9970       break;
9971
9972     case 'O':
9973       if (arg && arg[1] == '0')
9974         mips_optimize = 1;
9975       else
9976         mips_optimize = 2;
9977       break;
9978
9979     case 'g':
9980       if (arg == NULL)
9981         mips_debug = 2;
9982       else
9983         mips_debug = atoi (arg);
9984       /* When the MIPS assembler sees -g or -g2, it does not do
9985          optimizations which limit full symbolic debugging.  We take
9986          that to be equivalent to -O0.  */
9987       if (mips_debug == 2)
9988         mips_optimize = 1;
9989       break;
9990
9991     case OPTION_MIPS1:
9992       mips_opts.isa = ISA_MIPS1;
9993       break;
9994
9995     case OPTION_MIPS2:
9996       mips_opts.isa = ISA_MIPS2;
9997       break;
9998
9999     case OPTION_MIPS3:
10000       mips_opts.isa = ISA_MIPS3;
10001       break;
10002
10003     case OPTION_MIPS4:
10004       mips_opts.isa = ISA_MIPS4;
10005       break;
10006
10007     case OPTION_MIPS5:
10008       mips_opts.isa = ISA_MIPS5;
10009       break;
10010
10011     case OPTION_MIPS32:
10012       mips_opts.isa = ISA_MIPS32;
10013       break;
10014
10015     case OPTION_MIPS64:
10016       mips_opts.isa = ISA_MIPS64;
10017       break;
10018
10019     case OPTION_MTUNE:
10020     case OPTION_MARCH:
10021     case OPTION_MCPU:
10022       {
10023         int cpu = CPU_UNKNOWN;
10024
10025         /* Identify the processor type.  */
10026         if (strcasecmp (arg, "default") != 0)
10027           {
10028             const struct mips_cpu_info *ci;
10029
10030             ci = mips_cpu_info_from_name (arg);
10031             if (ci == NULL || ci->is_isa)
10032               {
10033                 switch (c)
10034                   {
10035                   case OPTION_MTUNE:
10036                     as_fatal (_("invalid architecture -mtune=%s"), arg);
10037                     break;
10038                   case OPTION_MARCH:
10039                     as_fatal (_("invalid architecture -march=%s"), arg);
10040                     break;
10041                   case OPTION_MCPU:
10042                     as_fatal (_("invalid architecture -mcpu=%s"), arg);
10043                     break;
10044                   }
10045               }
10046             else
10047               cpu = ci->cpu;
10048           }
10049
10050         switch (c)
10051           {
10052           case OPTION_MTUNE:
10053             if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
10054               as_warn (_("A different -mtune= was already specified, is now "
10055                          "-mtune=%s"), arg);
10056             mips_tune = cpu;
10057             break;
10058           case OPTION_MARCH:
10059             if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
10060               as_warn (_("A different -march= was already specified, is now "
10061                          "-march=%s"), arg);
10062             mips_arch = cpu;
10063             break;
10064           case OPTION_MCPU:
10065             if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
10066               as_warn (_("A different -mcpu= was already specified, is now "
10067                          "-mcpu=%s"), arg);
10068             mips_cpu = cpu;
10069           }
10070       }
10071       break;
10072
10073     case OPTION_M4650:
10074       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
10075           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
10076         as_warn (_("A different -march= or -mtune= was already specified, "
10077                    "is now -m4650"));
10078       mips_arch = CPU_R4650;
10079       mips_tune = CPU_R4650;
10080       break;
10081
10082     case OPTION_NO_M4650:
10083       break;
10084
10085     case OPTION_M4010:
10086       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
10087           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
10088         as_warn (_("A different -march= or -mtune= was already specified, "
10089                    "is now -m4010"));
10090       mips_arch = CPU_R4010;
10091       mips_tune = CPU_R4010;
10092       break;
10093
10094     case OPTION_NO_M4010:
10095       break;
10096
10097     case OPTION_M4100:
10098       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
10099           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
10100         as_warn (_("A different -march= or -mtune= was already specified, "
10101                    "is now -m4100"));
10102       mips_arch = CPU_VR4100;
10103       mips_tune = CPU_VR4100;
10104       break;
10105
10106     case OPTION_NO_M4100:
10107       break;
10108
10109     case OPTION_M3900:
10110       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
10111           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
10112         as_warn (_("A different -march= or -mtune= was already specified, "
10113                    "is now -m3900"));
10114       mips_arch = CPU_R3900;
10115       mips_tune = CPU_R3900;
10116       break;
10117
10118     case OPTION_NO_M3900:
10119       break;
10120
10121     case OPTION_MDMX:
10122       mips_opts.ase_mdmx = 1;
10123       break;
10124
10125     case OPTION_NO_MDMX:
10126       mips_opts.ase_mdmx = 0;
10127       break;
10128
10129     case OPTION_MIPS16:
10130       mips_opts.mips16 = 1;
10131       mips_no_prev_insn (false);
10132       break;
10133
10134     case OPTION_NO_MIPS16:
10135       mips_opts.mips16 = 0;
10136       mips_no_prev_insn (false);
10137       break;
10138
10139     case OPTION_MIPS3D:
10140       mips_opts.ase_mips3d = 1;
10141       break;
10142
10143     case OPTION_NO_MIPS3D:
10144       mips_opts.ase_mips3d = 0;
10145       break;
10146
10147     case OPTION_MEMBEDDED_PIC:
10148       mips_pic = EMBEDDED_PIC;
10149       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10150         {
10151           as_bad (_("-G may not be used with embedded PIC code"));
10152           return 0;
10153         }
10154       g_switch_value = 0x7fffffff;
10155       break;
10156
10157 #ifdef OBJ_ELF
10158       /* When generating ELF code, we permit -KPIC and -call_shared to
10159          select SVR4_PIC, and -non_shared to select no PIC.  This is
10160          intended to be compatible with Irix 5.  */
10161     case OPTION_CALL_SHARED:
10162       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10163         {
10164           as_bad (_("-call_shared is supported only for ELF format"));
10165           return 0;
10166         }
10167       mips_pic = SVR4_PIC;
10168       if (g_switch_seen && g_switch_value != 0)
10169         {
10170           as_bad (_("-G may not be used with SVR4 PIC code"));
10171           return 0;
10172         }
10173       g_switch_value = 0;
10174       break;
10175
10176     case OPTION_NON_SHARED:
10177       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10178         {
10179           as_bad (_("-non_shared is supported only for ELF format"));
10180           return 0;
10181         }
10182       mips_pic = NO_PIC;
10183       break;
10184
10185       /* The -xgot option tells the assembler to use 32 offsets when
10186          accessing the got in SVR4_PIC mode.  It is for Irix
10187          compatibility.  */
10188     case OPTION_XGOT:
10189       mips_big_got = 1;
10190       break;
10191 #endif /* OBJ_ELF */
10192
10193     case 'G':
10194       if (! USE_GLOBAL_POINTER_OPT)
10195         {
10196           as_bad (_("-G is not supported for this configuration"));
10197           return 0;
10198         }
10199       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10200         {
10201           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10202           return 0;
10203         }
10204       else
10205         g_switch_value = atoi (arg);
10206       g_switch_seen = 1;
10207       break;
10208
10209 #ifdef OBJ_ELF
10210       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10211          and -mabi=64.  */
10212     case OPTION_32:
10213       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10214         {
10215           as_bad (_("-32 is supported for ELF format only"));
10216           return 0;
10217         }
10218       mips_opts.abi = O32_ABI;
10219       break;
10220
10221     case OPTION_N32:
10222       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10223         {
10224           as_bad (_("-n32 is supported for ELF format only"));
10225           return 0;
10226         }
10227       mips_opts.abi = N32_ABI;
10228       break;
10229
10230     case OPTION_64:
10231       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10232         {
10233           as_bad (_("-64 is supported for ELF format only"));
10234           return 0;
10235         }
10236       mips_opts.abi = N64_ABI;
10237       if (! support_64bit_objects())
10238         as_fatal (_("No compiled in support for 64 bit object file format"));
10239       break;
10240 #endif /* OBJ_ELF */
10241
10242     case OPTION_GP32:
10243       file_mips_gp32 = 1;
10244       if (mips_opts.abi != O32_ABI)
10245         mips_opts.abi = NO_ABI;
10246       break;
10247
10248     case OPTION_GP64:
10249       file_mips_gp32 = 0;
10250       if (mips_opts.abi == O32_ABI)
10251         mips_opts.abi = NO_ABI;
10252       break;
10253
10254     case OPTION_FP32:
10255       file_mips_fp32 = 1;
10256       if (mips_opts.abi != O32_ABI)
10257         mips_opts.abi = NO_ABI;
10258       break;
10259
10260 #ifdef OBJ_ELF
10261     case OPTION_MABI:
10262       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10263         {
10264           as_bad (_("-mabi is supported for ELF format only"));
10265           return 0;
10266         }
10267       if (strcmp (arg, "32") == 0)
10268         mips_opts.abi = O32_ABI;
10269       else if (strcmp (arg, "o64") == 0)
10270         mips_opts.abi = O64_ABI;
10271       else if (strcmp (arg, "n32") == 0)
10272         mips_opts.abi = N32_ABI;
10273       else if (strcmp (arg, "64") == 0)
10274         {
10275           mips_opts.abi = N64_ABI;
10276           if (! support_64bit_objects())
10277             as_fatal (_("No compiled in support for 64 bit object file "
10278                         "format"));
10279         }
10280       else if (strcmp (arg, "eabi") == 0)
10281         mips_opts.abi = EABI_ABI;
10282       else
10283         {
10284           as_fatal (_("invalid abi -mabi=%s"), arg);
10285           return 0;
10286         }
10287       break;
10288 #endif /* OBJ_ELF */
10289
10290     case OPTION_M7000_HILO_FIX:
10291       mips_7000_hilo_fix = true;
10292       break;
10293
10294     case OPTION_MNO_7000_HILO_FIX:
10295       mips_7000_hilo_fix = false;
10296       break;
10297
10298     default:
10299       return 0;
10300     }
10301
10302   return 1;
10303 }
10304
10305 static void
10306 show (stream, string, col_p, first_p)
10307      FILE *stream;
10308      char *string;
10309      int *col_p;
10310      int *first_p;
10311 {
10312   if (*first_p)
10313     {
10314       fprintf (stream, "%24s", "");
10315       *col_p = 24;
10316     }
10317   else
10318     {
10319       fprintf (stream, ", ");
10320       *col_p += 2;
10321     }
10322
10323   if (*col_p + strlen (string) > 72)
10324     {
10325       fprintf (stream, "\n%24s", "");
10326       *col_p = 24;
10327     }
10328
10329   fprintf (stream, "%s", string);
10330   *col_p += strlen (string);
10331
10332   *first_p = 0;
10333 }
10334
10335 void
10336 md_show_usage (stream)
10337      FILE *stream;
10338 {
10339   int column, first;
10340
10341   fprintf (stream, _("\
10342 MIPS options:\n\
10343 -membedded-pic          generate embedded position independent code\n\
10344 -EB                     generate big endian output\n\
10345 -EL                     generate little endian output\n\
10346 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
10347 -G NUM                  allow referencing objects up to NUM bytes\n\
10348                         implicitly with the gp register [default 8]\n"));
10349   fprintf (stream, _("\
10350 -mips1                  generate MIPS ISA I instructions\n\
10351 -mips2                  generate MIPS ISA II instructions\n\
10352 -mips3                  generate MIPS ISA III instructions\n\
10353 -mips4                  generate MIPS ISA IV instructions\n\
10354 -mips5                  generate MIPS ISA V instructions\n\
10355 -mips32                 generate MIPS32 ISA instructions\n\
10356 -mips64                 generate MIPS64 ISA instructions\n\
10357 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
10358
10359   first = 1;
10360
10361   show (stream, "2000", &column, &first);
10362   show (stream, "3000", &column, &first);
10363   show (stream, "3900", &column, &first);
10364   show (stream, "4000", &column, &first);
10365   show (stream, "4010", &column, &first);
10366   show (stream, "4100", &column, &first);
10367   show (stream, "4111", &column, &first);
10368   show (stream, "4300", &column, &first);
10369   show (stream, "4400", &column, &first);
10370   show (stream, "4600", &column, &first);
10371   show (stream, "4650", &column, &first);
10372   show (stream, "5000", &column, &first);
10373   show (stream, "5200", &column, &first);
10374   show (stream, "5230", &column, &first);
10375   show (stream, "5231", &column, &first);
10376   show (stream, "5261", &column, &first);
10377   show (stream, "5721", &column, &first);
10378   show (stream, "6000", &column, &first);
10379   show (stream, "8000", &column, &first);
10380   show (stream, "10000", &column, &first);
10381   show (stream, "12000", &column, &first);
10382   show (stream, "sb1", &column, &first);
10383   fputc ('\n', stream);
10384
10385   fprintf (stream, _("\
10386 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10387 -no-mCPU                don't generate code specific to CPU.\n\
10388                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
10389
10390   first = 1;
10391
10392   show (stream, "3900", &column, &first);
10393   show (stream, "4010", &column, &first);
10394   show (stream, "4100", &column, &first);
10395   show (stream, "4650", &column, &first);
10396   fputc ('\n', stream);
10397
10398   fprintf (stream, _("\
10399 -mips16                 generate mips16 instructions\n\
10400 -no-mips16              do not generate mips16 instructions\n"));
10401   fprintf (stream, _("\
10402 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
10403 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
10404 -O0                     remove unneeded NOPs, do not swap branches\n\
10405 -O                      remove unneeded NOPs and swap branches\n\
10406 -n                      warn about NOPs generated from macros\n\
10407 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10408 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
10409 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
10410 #ifdef OBJ_ELF
10411   fprintf (stream, _("\
10412 -KPIC, -call_shared     generate SVR4 position independent code\n\
10413 -non_shared             do not generate position independent code\n\
10414 -xgot                   assume a 32 bit GOT\n\
10415 -mabi=ABI               create ABI conformant object file for:\n"));
10416
10417   first = 1;
10418
10419   show (stream, "32", &column, &first);
10420   show (stream, "o64", &column, &first);
10421   show (stream, "n32", &column, &first);
10422   show (stream, "64", &column, &first);
10423   show (stream, "eabi", &column, &first);
10424
10425   fputc ('\n', stream);
10426
10427   fprintf (stream, _("\
10428 -32                     create o32 ABI object file (default)\n\
10429 -n32                    create n32 ABI object file\n\
10430 -64                     create 64 ABI object file\n"));
10431 #endif
10432 }
10433 \f
10434 void
10435 mips_after_parse_args ()
10436 {
10437   const char *cpu;
10438   char *a = NULL;
10439   int mips_isa_from_cpu;
10440   const struct mips_cpu_info *ci;
10441
10442   /* GP relative stuff not working for PE */
10443   if (strncmp (TARGET_OS, "pe", 2) == 0
10444       && g_switch_value != 0)
10445     {
10446       if (g_switch_seen)
10447         as_bad (_("-G not supported in this configuration."));
10448       g_switch_value = 0;
10449     }
10450
10451   cpu = TARGET_CPU;
10452   if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
10453     {
10454       a = xmalloc (sizeof TARGET_CPU);
10455       strcpy (a, TARGET_CPU);
10456       a[(sizeof TARGET_CPU) - 3] = '\0';
10457       cpu = a;
10458     }
10459
10460   /* Backward compatibility for historic -mcpu= option.  Check for
10461      incompatible options, warn if -mcpu is used.  */
10462   if (mips_cpu != CPU_UNKNOWN
10463       && mips_arch != CPU_UNKNOWN
10464       && mips_cpu != mips_arch)
10465     {
10466       as_fatal (_("The -mcpu option can't be used together with -march. "
10467                   "Use -mtune instead of -mcpu."));
10468     }
10469
10470   if (mips_cpu != CPU_UNKNOWN
10471       && mips_tune != CPU_UNKNOWN
10472       && mips_cpu != mips_tune)
10473     {
10474       as_fatal (_("The -mcpu option can't be used together with -mtune. "
10475                   "Use -march instead of -mcpu."));
10476     }
10477
10478 #if 1
10479   /* For backward compatibility, let -mipsN set various defaults.  */
10480   /* This code should go away, to be replaced with something rather more
10481      draconian.  Until GCC 3.1 has been released for some reasonable
10482      amount of time, however, we need to support this.  */
10483   if (mips_opts.isa != ISA_UNKNOWN)
10484     {
10485       /* Translate -mipsN to the appropriate settings of file_mips_gp32
10486          and file_mips_fp32.  Tag binaries as using the mipsN ISA.  */
10487       if (file_mips_gp32 < 0)
10488         {
10489           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
10490             file_mips_gp32 = 0;
10491           else
10492             file_mips_gp32 = 1;
10493         }
10494       if (file_mips_fp32 < 0)
10495         {
10496           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
10497             file_mips_fp32 = 0;
10498           else
10499             file_mips_fp32 = 1;
10500         }
10501
10502       ci = mips_cpu_info_from_isa (mips_opts.isa);
10503       assert (ci != NULL);
10504       /* -mipsN has higher priority than -mcpu but lower than -march.  */
10505       if (mips_arch == CPU_UNKNOWN)
10506         mips_arch = ci->cpu;
10507
10508       /* Default mips_abi.  */
10509       if (mips_opts.abi == NO_ABI)
10510         {
10511           if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
10512             mips_opts.abi = O32_ABI;
10513           else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
10514             mips_opts.abi = O64_ABI;
10515         }
10516     }
10517
10518   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10519     {
10520       ci = mips_cpu_info_from_cpu (mips_cpu);
10521       assert (ci != NULL);
10522       mips_arch = ci->cpu;
10523       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
10524                  "-mtune instead."));
10525     }
10526
10527   /* Set tune from -mcpu, not from -mipsN.  */
10528   if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10529     {
10530       ci = mips_cpu_info_from_cpu (mips_cpu);
10531       assert (ci != NULL);
10532       mips_tune = ci->cpu;
10533     }
10534
10535   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
10536      specified on the command line, or some other value if one was.
10537      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
10538      the command line, or will be set otherwise if one was.  */
10539
10540   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10541     /* Handled above.  */;
10542 #else
10543   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10544     {
10545       ci = mips_cpu_info_from_cpu (mips_cpu);
10546       assert (ci != NULL);
10547       mips_arch = ci->cpu;
10548       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
10549                  "-mtune instead."));
10550     }
10551
10552   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
10553      specified on the command line, or some other value if one was.
10554      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
10555      the command line, or will be set otherwise if one was.  */
10556
10557   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10558     {
10559       /* We have to check if the isa is the default isa of arch.  Otherwise
10560          we'll get invalid object file headers.  */
10561       ci = mips_cpu_info_from_cpu (mips_arch);
10562       assert (ci != NULL);
10563       if (mips_opts.isa != ci->isa)
10564         {
10565           /* This really should be an error instead of a warning, but old
10566              compilers only have -mcpu which sets both arch and tune.  For
10567              now, we discard arch and preserve tune.  */
10568           as_warn (_("The -march option is incompatible to -mipsN and "
10569                      "therefore ignored."));
10570           if (mips_tune == CPU_UNKNOWN)
10571             mips_tune = mips_arch;
10572           ci = mips_cpu_info_from_isa (mips_opts.isa);
10573           assert (ci != NULL);
10574           mips_arch = ci->cpu;
10575         }
10576     }
10577 #endif
10578   else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
10579     {
10580       /* We have ARCH, we need ISA.  */
10581       ci = mips_cpu_info_from_cpu (mips_arch);
10582       assert (ci != NULL);
10583       mips_opts.isa = ci->isa;
10584     }
10585   else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10586     {
10587       /* We have ISA, we need default ARCH.  */
10588       ci = mips_cpu_info_from_isa (mips_opts.isa);
10589       assert (ci != NULL);
10590       mips_arch = ci->cpu;
10591     }
10592   else
10593     {
10594       /* We need to set both ISA and ARCH from target cpu.  */
10595       ci = mips_cpu_info_from_name (cpu);
10596       if (ci == NULL)
10597         ci = mips_cpu_info_from_cpu (CPU_R3000);
10598       assert (ci != NULL);
10599       mips_opts.isa = ci->isa;
10600       mips_arch = ci->cpu;
10601     }
10602
10603   if (mips_tune == CPU_UNKNOWN)
10604     mips_tune = mips_arch;
10605
10606   ci = mips_cpu_info_from_cpu (mips_arch);
10607   assert (ci != NULL);
10608   mips_isa_from_cpu = ci->isa;
10609
10610   /* End of TARGET_CPU processing, get rid of malloced memory
10611      if necessary.  */
10612   cpu = NULL;
10613   if (a != NULL)
10614     {
10615       free (a);
10616       a = NULL;
10617     }
10618
10619   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10620     as_bad (_("trap exception not supported at ISA 1"));
10621
10622   /* If they asked for mips1 or mips2 and a cpu that is
10623      mips3 or greater, then mark the object file 32BITMODE.  */
10624   if (mips_isa_from_cpu != ISA_UNKNOWN
10625       && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
10626       && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
10627     mips_32bitmode = 1;
10628
10629   /* If the selected architecture includes support for ASEs, enable
10630      generation of code for them.  */
10631   if (mips_opts.mips16 == -1)
10632     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10633   if (mips_opts.ase_mips3d == -1)
10634     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10635   if (mips_opts.ase_mdmx == -1)
10636     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10637
10638   if (file_mips_gp32 < 0)
10639     file_mips_gp32 = 0;
10640   if (file_mips_fp32 < 0)
10641     file_mips_fp32 = 0;
10642
10643   file_mips_isa = mips_opts.isa;
10644   file_mips_abi = mips_opts.abi;
10645   file_ase_mips16 = mips_opts.mips16;
10646   file_ase_mips3d = mips_opts.ase_mips3d;
10647   file_ase_mdmx = mips_opts.ase_mdmx;
10648   mips_opts.gp32 = file_mips_gp32;
10649   mips_opts.fp32 = file_mips_fp32;
10650
10651   if (HAVE_NEWABI)
10652     mips_big_got = 1;
10653 }
10654 \f
10655 void
10656 mips_init_after_args ()
10657 {
10658   /* initialize opcodes */
10659   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10660   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10661 }
10662
10663 long
10664 md_pcrel_from (fixP)
10665      fixS *fixP;
10666 {
10667   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10668       && fixP->fx_addsy != (symbolS *) NULL
10669       && ! S_IS_DEFINED (fixP->fx_addsy))
10670     {
10671       /* This makes a branch to an undefined symbol be a branch to the
10672          current location.  */
10673       if (mips_pic == EMBEDDED_PIC)
10674         return 4;
10675       else
10676         return 1;
10677     }
10678
10679   /* Return the address of the delay slot.  */
10680   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10681 }
10682
10683 /* This is called before the symbol table is processed.  In order to
10684    work with gcc when using mips-tfile, we must keep all local labels.
10685    However, in other cases, we want to discard them.  If we were
10686    called with -g, but we didn't see any debugging information, it may
10687    mean that gcc is smuggling debugging information through to
10688    mips-tfile, in which case we must generate all local labels.  */
10689
10690 void
10691 mips_frob_file_before_adjust ()
10692 {
10693 #ifndef NO_ECOFF_DEBUGGING
10694   if (ECOFF_DEBUGGING
10695       && mips_debug != 0
10696       && ! ecoff_debugging_seen)
10697     flag_keep_locals = 1;
10698 #endif
10699 }
10700
10701 /* Sort any unmatched HI16_S relocs so that they immediately precede
10702    the corresponding LO reloc.  This is called before md_apply_fix3 and
10703    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10704    explicit use of the %hi modifier.  */
10705
10706 void
10707 mips_frob_file ()
10708 {
10709   struct mips_hi_fixup *l;
10710
10711   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10712     {
10713       segment_info_type *seginfo;
10714       int pass;
10715
10716       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10717
10718       /* Check quickly whether the next fixup happens to be a matching
10719          %lo.  */
10720       if (l->fixp->fx_next != NULL
10721           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10722           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10723           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10724         continue;
10725
10726       /* Look through the fixups for this segment for a matching %lo.
10727          When we find one, move the %hi just in front of it.  We do
10728          this in two passes.  In the first pass, we try to find a
10729          unique %lo.  In the second pass, we permit multiple %hi
10730          relocs for a single %lo (this is a GNU extension).  */
10731       seginfo = seg_info (l->seg);
10732       for (pass = 0; pass < 2; pass++)
10733         {
10734           fixS *f, *prev;
10735
10736           prev = NULL;
10737           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10738             {
10739               /* Check whether this is a %lo fixup which matches l->fixp.  */
10740               if (f->fx_r_type == BFD_RELOC_LO16
10741                   && f->fx_addsy == l->fixp->fx_addsy
10742                   && f->fx_offset == l->fixp->fx_offset
10743                   && (pass == 1
10744                       || prev == NULL
10745                       || prev->fx_r_type != BFD_RELOC_HI16_S
10746                       || prev->fx_addsy != f->fx_addsy
10747                       || prev->fx_offset !=  f->fx_offset))
10748                 {
10749                   fixS **pf;
10750
10751                   /* Move l->fixp before f.  */
10752                   for (pf = &seginfo->fix_root;
10753                        *pf != l->fixp;
10754                        pf = &(*pf)->fx_next)
10755                     assert (*pf != NULL);
10756
10757                   *pf = l->fixp->fx_next;
10758
10759                   l->fixp->fx_next = f;
10760                   if (prev == NULL)
10761                     seginfo->fix_root = l->fixp;
10762                   else
10763                     prev->fx_next = l->fixp;
10764
10765                   break;
10766                 }
10767
10768               prev = f;
10769             }
10770
10771           if (f != NULL)
10772             break;
10773
10774 #if 0 /* GCC code motion plus incomplete dead code elimination
10775          can leave a %hi without a %lo.  */
10776           if (pass == 1)
10777             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10778                            _("Unmatched %%hi reloc"));
10779 #endif
10780         }
10781     }
10782 }
10783
10784 /* When generating embedded PIC code we need to use a special
10785    relocation to represent the difference of two symbols in the .text
10786    section (switch tables use a difference of this sort).  See
10787    include/coff/mips.h for details.  This macro checks whether this
10788    fixup requires the special reloc.  */
10789 #define SWITCH_TABLE(fixp) \
10790   ((fixp)->fx_r_type == BFD_RELOC_32 \
10791    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10792    && (fixp)->fx_addsy != NULL \
10793    && (fixp)->fx_subsy != NULL \
10794    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10795    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10796
10797 /* When generating embedded PIC code we must keep all PC relative
10798    relocations, in case the linker has to relax a call.  We also need
10799    to keep relocations for switch table entries.
10800
10801    We may have combined relocations without symbols in the N32/N64 ABI.
10802    We have to prevent gas from dropping them.  */
10803
10804 int
10805 mips_force_relocation (fixp)
10806      fixS *fixp;
10807 {
10808   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10809       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10810     return 1;
10811
10812   if (HAVE_NEWABI
10813       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10814       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10815           || fixp->fx_r_type == BFD_RELOC_HI16_S
10816           || fixp->fx_r_type == BFD_RELOC_LO16))
10817     return 1;
10818
10819   return (mips_pic == EMBEDDED_PIC
10820           && (fixp->fx_pcrel
10821               || SWITCH_TABLE (fixp)
10822               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10823               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10824 }
10825
10826 #ifdef OBJ_ELF
10827 static int
10828 mips_need_elf_addend_fixup (fixP)
10829      fixS *fixP;
10830 {
10831   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10832     return 1;
10833   if (mips_pic == EMBEDDED_PIC
10834       && S_IS_WEAK (fixP->fx_addsy))
10835     return 1;
10836   if (mips_pic != EMBEDDED_PIC
10837       && (S_IS_WEAK (fixP->fx_addsy)
10838           || S_IS_EXTERN (fixP->fx_addsy))
10839       && !S_IS_COMMON (fixP->fx_addsy))
10840     return 1;
10841   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10842       && (((bfd_get_section_flags (stdoutput,
10843                                    S_GET_SEGMENT (fixP->fx_addsy))
10844             & SEC_LINK_ONCE) != 0)
10845           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10846                        ".gnu.linkonce",
10847                        sizeof (".gnu.linkonce") - 1)))
10848     return 1;
10849   return 0;
10850 }
10851 #endif
10852
10853 /* Apply a fixup to the object file.  */
10854
10855 void
10856 md_apply_fix3 (fixP, valP, seg)
10857      fixS *fixP;
10858      valueT *valP;
10859      segT seg ATTRIBUTE_UNUSED;
10860 {
10861   bfd_byte *buf;
10862   long insn;
10863   valueT value;
10864
10865   assert (fixP->fx_size == 4
10866           || fixP->fx_r_type == BFD_RELOC_16
10867           || fixP->fx_r_type == BFD_RELOC_32
10868           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10869           || fixP->fx_r_type == BFD_RELOC_HI16_S
10870           || fixP->fx_r_type == BFD_RELOC_LO16
10871           || fixP->fx_r_type == BFD_RELOC_GPREL16
10872           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10873           || fixP->fx_r_type == BFD_RELOC_GPREL32
10874           || fixP->fx_r_type == BFD_RELOC_64
10875           || fixP->fx_r_type == BFD_RELOC_CTOR
10876           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10877           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10878           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10879           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10880           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10881           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10882           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10883           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10884           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10885
10886   value = *valP;
10887
10888   /* If we aren't adjusting this fixup to be against the section
10889      symbol, we need to adjust the value.  */
10890 #ifdef OBJ_ELF
10891   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10892     {
10893       if (mips_need_elf_addend_fixup (fixP))
10894         {
10895           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10896
10897           value -= symval;
10898           if (value != 0 && ! fixP->fx_pcrel)
10899             {
10900               /* In this case, the bfd_install_relocation routine will
10901                  incorrectly add the symbol value back in.  We just want
10902                  the addend to appear in the object file.  */
10903               value -= symval;
10904
10905               /* Make sure the addend is still non-zero.  If it became zero
10906                  after the last operation, set it to a spurious value and
10907                  subtract the same value from the object file's contents.  */
10908               if (value == 0)
10909                 {
10910                   value = 8;
10911
10912                   /* The in-place addends for LO16 relocations are signed;
10913                      leave the matching HI16 in-place addends as zero.  */
10914                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10915                     {
10916                       reloc_howto_type *howto;
10917                       bfd_vma contents, mask, field;
10918
10919                       howto = bfd_reloc_type_lookup (stdoutput,
10920                                                      fixP->fx_r_type);
10921
10922                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10923                                                + fixP->fx_where,
10924                                                fixP->fx_size * 8,
10925                                                target_big_endian);
10926
10927                       /* MASK has bits set where the relocation should go.
10928                          FIELD is -value, shifted into the appropriate place
10929                          for this relocation.  */
10930                       mask = 1 << (howto->bitsize - 1);
10931                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10932                       field = (-value >> howto->rightshift) << howto->bitpos;
10933
10934                       bfd_put_bits ((field & mask) | (contents & ~mask),
10935                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10936                                     fixP->fx_size * 8,
10937                                     target_big_endian);
10938                     }
10939                 }
10940             }
10941         }
10942
10943       /* This code was generated using trial and error and so is
10944          fragile and not trustworthy.  If you change it, you should
10945          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10946          they still pass.  */
10947       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10948         {
10949           value += fixP->fx_frag->fr_address + fixP->fx_where;
10950
10951           /* BFD's REL handling, for MIPS, is _very_ weird.
10952              This gives the right results, but it can't possibly
10953              be the way things are supposed to work.  */
10954           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10955                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10956               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10957             value += fixP->fx_frag->fr_address + fixP->fx_where;
10958         }
10959     }
10960 #endif
10961
10962   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10963
10964   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10965     fixP->fx_done = 1;
10966
10967   switch (fixP->fx_r_type)
10968     {
10969     case BFD_RELOC_MIPS_JMP:
10970     case BFD_RELOC_MIPS_SHIFT5:
10971     case BFD_RELOC_MIPS_SHIFT6:
10972     case BFD_RELOC_MIPS_GOT_DISP:
10973     case BFD_RELOC_MIPS_GOT_PAGE:
10974     case BFD_RELOC_MIPS_GOT_OFST:
10975     case BFD_RELOC_MIPS_SUB:
10976     case BFD_RELOC_MIPS_INSERT_A:
10977     case BFD_RELOC_MIPS_INSERT_B:
10978     case BFD_RELOC_MIPS_DELETE:
10979     case BFD_RELOC_MIPS_HIGHEST:
10980     case BFD_RELOC_MIPS_HIGHER:
10981     case BFD_RELOC_MIPS_SCN_DISP:
10982     case BFD_RELOC_MIPS_REL16:
10983     case BFD_RELOC_MIPS_RELGOT:
10984     case BFD_RELOC_MIPS_JALR:
10985     case BFD_RELOC_HI16:
10986     case BFD_RELOC_HI16_S:
10987     case BFD_RELOC_GPREL16:
10988     case BFD_RELOC_MIPS_LITERAL:
10989     case BFD_RELOC_MIPS_CALL16:
10990     case BFD_RELOC_MIPS_GOT16:
10991     case BFD_RELOC_GPREL32:
10992     case BFD_RELOC_MIPS_GOT_HI16:
10993     case BFD_RELOC_MIPS_GOT_LO16:
10994     case BFD_RELOC_MIPS_CALL_HI16:
10995     case BFD_RELOC_MIPS_CALL_LO16:
10996     case BFD_RELOC_MIPS16_GPREL:
10997       if (fixP->fx_pcrel)
10998         as_bad_where (fixP->fx_file, fixP->fx_line,
10999                       _("Invalid PC relative reloc"));
11000       /* Nothing needed to do. The value comes from the reloc entry */
11001       break;
11002
11003     case BFD_RELOC_MIPS16_JMP:
11004       /* We currently always generate a reloc against a symbol, which
11005          means that we don't want an addend even if the symbol is
11006          defined.  */
11007       fixP->fx_addnumber = 0;
11008       break;
11009
11010     case BFD_RELOC_PCREL_HI16_S:
11011       /* The addend for this is tricky if it is internal, so we just
11012          do everything here rather than in bfd_install_relocation.  */
11013       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11014           && !fixP->fx_done
11015           && value != 0)
11016         break;
11017       if (fixP->fx_addsy
11018           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11019         {
11020           /* For an external symbol adjust by the address to make it
11021              pcrel_offset.  We use the address of the RELLO reloc
11022              which follows this one.  */
11023           value += (fixP->fx_next->fx_frag->fr_address
11024                     + fixP->fx_next->fx_where);
11025         }
11026       value = ((value + 0x8000) >> 16) & 0xffff;
11027       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11028       if (target_big_endian)
11029         buf += 2;
11030       md_number_to_chars ((char *) buf, value, 2);
11031       break;
11032
11033     case BFD_RELOC_PCREL_LO16:
11034       /* The addend for this is tricky if it is internal, so we just
11035          do everything here rather than in bfd_install_relocation.  */
11036       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11037           && !fixP->fx_done
11038           && value != 0)
11039         break;
11040       if (fixP->fx_addsy
11041           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11042         value += fixP->fx_frag->fr_address + fixP->fx_where;
11043       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11044       if (target_big_endian)
11045         buf += 2;
11046       md_number_to_chars ((char *) buf, value, 2);
11047       break;
11048
11049     case BFD_RELOC_64:
11050       /* This is handled like BFD_RELOC_32, but we output a sign
11051          extended value if we are only 32 bits.  */
11052       if (fixP->fx_done
11053           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11054         {
11055           if (8 <= sizeof (valueT))
11056             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11057                                 value, 8);
11058           else
11059             {
11060               long w1, w2;
11061               long hiv;
11062
11063               w1 = w2 = fixP->fx_where;
11064               if (target_big_endian)
11065                 w1 += 4;
11066               else
11067                 w2 += 4;
11068               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11069               if ((value & 0x80000000) != 0)
11070                 hiv = 0xffffffff;
11071               else
11072                 hiv = 0;
11073               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11074             }
11075         }
11076       break;
11077
11078     case BFD_RELOC_RVA:
11079     case BFD_RELOC_32:
11080       /* If we are deleting this reloc entry, we must fill in the
11081          value now.  This can happen if we have a .word which is not
11082          resolved when it appears but is later defined.  We also need
11083          to fill in the value if this is an embedded PIC switch table
11084          entry.  */
11085       if (fixP->fx_done
11086           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11087         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11088                             value, 4);
11089       break;
11090
11091     case BFD_RELOC_16:
11092       /* If we are deleting this reloc entry, we must fill in the
11093          value now.  */
11094       assert (fixP->fx_size == 2);
11095       if (fixP->fx_done)
11096         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11097                             value, 2);
11098       break;
11099
11100     case BFD_RELOC_LO16:
11101       /* When handling an embedded PIC switch statement, we can wind
11102          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11103       if (fixP->fx_done)
11104         {
11105           if (value + 0x8000 > 0xffff)
11106             as_bad_where (fixP->fx_file, fixP->fx_line,
11107                           _("relocation overflow"));
11108           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11109           if (target_big_endian)
11110             buf += 2;
11111           md_number_to_chars ((char *) buf, value, 2);
11112         }
11113       break;
11114
11115     case BFD_RELOC_16_PCREL_S2:
11116       if ((value & 0x3) != 0)
11117         as_bad_where (fixP->fx_file, fixP->fx_line,
11118                       _("Branch to odd address (%lx)"), (long) value);
11119
11120       /* Fall through.  */
11121
11122     case BFD_RELOC_16_PCREL:
11123       /*
11124        * We need to save the bits in the instruction since fixup_segment()
11125        * might be deleting the relocation entry (i.e., a branch within
11126        * the current segment).
11127        */
11128       if (!fixP->fx_done && value != 0)
11129         break;
11130       /* If 'value' is zero, the remaining reloc code won't actually
11131          do the store, so it must be done here.  This is probably
11132          a bug somewhere.  */
11133       if (!fixP->fx_done
11134           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11135               || fixP->fx_addsy == NULL                 /* ??? */
11136               || ! S_IS_DEFINED (fixP->fx_addsy)))
11137         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11138
11139       value = (offsetT) value >> 2;
11140
11141       /* update old instruction data */
11142       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11143       if (target_big_endian)
11144         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11145       else
11146         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11147
11148       if (value + 0x8000 <= 0xffff)
11149         insn |= value & 0xffff;
11150       else
11151         {
11152           /* The branch offset is too large.  If this is an
11153              unconditional branch, and we are not generating PIC code,
11154              we can convert it to an absolute jump instruction.  */
11155           if (mips_pic == NO_PIC
11156               && fixP->fx_done
11157               && fixP->fx_frag->fr_address >= text_section->vma
11158               && (fixP->fx_frag->fr_address
11159                   < text_section->vma + text_section->_raw_size)
11160               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11161                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11162                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11163             {
11164               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11165                 insn = 0x0c000000;      /* jal */
11166               else
11167                 insn = 0x08000000;      /* j */
11168               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11169               fixP->fx_done = 0;
11170               fixP->fx_addsy = section_symbol (text_section);
11171               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11172             }
11173           else
11174             {
11175               /* FIXME.  It would be possible in principle to handle
11176                  conditional branches which overflow.  They could be
11177                  transformed into a branch around a jump.  This would
11178                  require setting up variant frags for each different
11179                  branch type.  The native MIPS assembler attempts to
11180                  handle these cases, but it appears to do it
11181                  incorrectly.  */
11182               as_bad_where (fixP->fx_file, fixP->fx_line,
11183                             _("Branch out of range"));
11184             }
11185         }
11186
11187       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11188       break;
11189
11190     case BFD_RELOC_VTABLE_INHERIT:
11191       fixP->fx_done = 0;
11192       if (fixP->fx_addsy
11193           && !S_IS_DEFINED (fixP->fx_addsy)
11194           && !S_IS_WEAK (fixP->fx_addsy))
11195         S_SET_WEAK (fixP->fx_addsy);
11196       break;
11197
11198     case BFD_RELOC_VTABLE_ENTRY:
11199       fixP->fx_done = 0;
11200       break;
11201
11202     default:
11203       internalError ();
11204     }
11205 }
11206
11207 #if 0
11208 void
11209 printInsn (oc)
11210      unsigned long oc;
11211 {
11212   const struct mips_opcode *p;
11213   int treg, sreg, dreg, shamt;
11214   short imm;
11215   const char *args;
11216   int i;
11217
11218   for (i = 0; i < NUMOPCODES; ++i)
11219     {
11220       p = &mips_opcodes[i];
11221       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11222         {
11223           printf ("%08lx %s\t", oc, p->name);
11224           treg = (oc >> 16) & 0x1f;
11225           sreg = (oc >> 21) & 0x1f;
11226           dreg = (oc >> 11) & 0x1f;
11227           shamt = (oc >> 6) & 0x1f;
11228           imm = oc;
11229           for (args = p->args;; ++args)
11230             {
11231               switch (*args)
11232                 {
11233                 case '\0':
11234                   printf ("\n");
11235                   break;
11236
11237                 case ',':
11238                 case '(':
11239                 case ')':
11240                   printf ("%c", *args);
11241                   continue;
11242
11243                 case 'r':
11244                   assert (treg == sreg);
11245                   printf ("$%d,$%d", treg, sreg);
11246                   continue;
11247
11248                 case 'd':
11249                 case 'G':
11250                   printf ("$%d", dreg);
11251                   continue;
11252
11253                 case 't':
11254                 case 'E':
11255                   printf ("$%d", treg);
11256                   continue;
11257
11258                 case 'k':
11259                   printf ("0x%x", treg);
11260                   continue;
11261
11262                 case 'b':
11263                 case 's':
11264                   printf ("$%d", sreg);
11265                   continue;
11266
11267                 case 'a':
11268                   printf ("0x%08lx", oc & 0x1ffffff);
11269                   continue;
11270
11271                 case 'i':
11272                 case 'j':
11273                 case 'o':
11274                 case 'u':
11275                   printf ("%d", imm);
11276                   continue;
11277
11278                 case '<':
11279                 case '>':
11280                   printf ("$%d", shamt);
11281                   continue;
11282
11283                 default:
11284                   internalError ();
11285                 }
11286               break;
11287             }
11288           return;
11289         }
11290     }
11291   printf (_("%08lx  UNDEFINED\n"), oc);
11292 }
11293 #endif
11294
11295 static symbolS *
11296 get_symbol ()
11297 {
11298   int c;
11299   char *name;
11300   symbolS *p;
11301
11302   name = input_line_pointer;
11303   c = get_symbol_end ();
11304   p = (symbolS *) symbol_find_or_make (name);
11305   *input_line_pointer = c;
11306   return p;
11307 }
11308
11309 /* Align the current frag to a given power of two.  The MIPS assembler
11310    also automatically adjusts any preceding label.  */
11311
11312 static void
11313 mips_align (to, fill, label)
11314      int to;
11315      int fill;
11316      symbolS *label;
11317 {
11318   mips_emit_delays (false);
11319   frag_align (to, fill, 0);
11320   record_alignment (now_seg, to);
11321   if (label != NULL)
11322     {
11323       assert (S_GET_SEGMENT (label) == now_seg);
11324       symbol_set_frag (label, frag_now);
11325       S_SET_VALUE (label, (valueT) frag_now_fix ());
11326     }
11327 }
11328
11329 /* Align to a given power of two.  .align 0 turns off the automatic
11330    alignment used by the data creating pseudo-ops.  */
11331
11332 static void
11333 s_align (x)
11334      int x ATTRIBUTE_UNUSED;
11335 {
11336   register int temp;
11337   register long temp_fill;
11338   long max_alignment = 15;
11339
11340   /*
11341
11342     o  Note that the assembler pulls down any immediately preceeding label
11343        to the aligned address.
11344     o  It's not documented but auto alignment is reinstated by
11345        a .align pseudo instruction.
11346     o  Note also that after auto alignment is turned off the mips assembler
11347        issues an error on attempt to assemble an improperly aligned data item.
11348        We don't.
11349
11350     */
11351
11352   temp = get_absolute_expression ();
11353   if (temp > max_alignment)
11354     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11355   else if (temp < 0)
11356     {
11357       as_warn (_("Alignment negative: 0 assumed."));
11358       temp = 0;
11359     }
11360   if (*input_line_pointer == ',')
11361     {
11362       ++input_line_pointer;
11363       temp_fill = get_absolute_expression ();
11364     }
11365   else
11366     temp_fill = 0;
11367   if (temp)
11368     {
11369       auto_align = 1;
11370       mips_align (temp, (int) temp_fill,
11371                   insn_labels != NULL ? insn_labels->label : NULL);
11372     }
11373   else
11374     {
11375       auto_align = 0;
11376     }
11377
11378   demand_empty_rest_of_line ();
11379 }
11380
11381 void
11382 mips_flush_pending_output ()
11383 {
11384   mips_emit_delays (false);
11385   mips_clear_insn_labels ();
11386 }
11387
11388 static void
11389 s_change_sec (sec)
11390      int sec;
11391 {
11392   segT seg;
11393
11394   /* When generating embedded PIC code, we only use the .text, .lit8,
11395      .sdata and .sbss sections.  We change the .data and .rdata
11396      pseudo-ops to use .sdata.  */
11397   if (mips_pic == EMBEDDED_PIC
11398       && (sec == 'd' || sec == 'r'))
11399     sec = 's';
11400
11401 #ifdef OBJ_ELF
11402   /* The ELF backend needs to know that we are changing sections, so
11403      that .previous works correctly.  We could do something like check
11404      for an obj_section_change_hook macro, but that might be confusing
11405      as it would not be appropriate to use it in the section changing
11406      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11407      This should be cleaner, somehow.  */
11408   obj_elf_section_change_hook ();
11409 #endif
11410
11411   mips_emit_delays (false);
11412   switch (sec)
11413     {
11414     case 't':
11415       s_text (0);
11416       break;
11417     case 'd':
11418       s_data (0);
11419       break;
11420     case 'b':
11421       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11422       demand_empty_rest_of_line ();
11423       break;
11424
11425     case 'r':
11426       if (USE_GLOBAL_POINTER_OPT)
11427         {
11428           seg = subseg_new (RDATA_SECTION_NAME,
11429                             (subsegT) get_absolute_expression ());
11430           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11431             {
11432               bfd_set_section_flags (stdoutput, seg,
11433                                      (SEC_ALLOC
11434                                       | SEC_LOAD
11435                                       | SEC_READONLY
11436                                       | SEC_RELOC
11437                                       | SEC_DATA));
11438               if (strcmp (TARGET_OS, "elf") != 0)
11439                 record_alignment (seg, 4);
11440             }
11441           demand_empty_rest_of_line ();
11442         }
11443       else
11444         {
11445           as_bad (_("No read only data section in this object file format"));
11446           demand_empty_rest_of_line ();
11447           return;
11448         }
11449       break;
11450
11451     case 's':
11452       if (USE_GLOBAL_POINTER_OPT)
11453         {
11454           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11455           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11456             {
11457               bfd_set_section_flags (stdoutput, seg,
11458                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11459                                      | SEC_DATA);
11460               if (strcmp (TARGET_OS, "elf") != 0)
11461                 record_alignment (seg, 4);
11462             }
11463           demand_empty_rest_of_line ();
11464           break;
11465         }
11466       else
11467         {
11468           as_bad (_("Global pointers not supported; recompile -G 0"));
11469           demand_empty_rest_of_line ();
11470           return;
11471         }
11472     }
11473
11474   auto_align = 1;
11475 }
11476
11477 void
11478 mips_enable_auto_align ()
11479 {
11480   auto_align = 1;
11481 }
11482
11483 static void
11484 s_cons (log_size)
11485      int log_size;
11486 {
11487   symbolS *label;
11488
11489   label = insn_labels != NULL ? insn_labels->label : NULL;
11490   mips_emit_delays (false);
11491   if (log_size > 0 && auto_align)
11492     mips_align (log_size, 0, label);
11493   mips_clear_insn_labels ();
11494   cons (1 << log_size);
11495 }
11496
11497 static void
11498 s_float_cons (type)
11499      int type;
11500 {
11501   symbolS *label;
11502
11503   label = insn_labels != NULL ? insn_labels->label : NULL;
11504
11505   mips_emit_delays (false);
11506
11507   if (auto_align)
11508     {
11509       if (type == 'd')
11510         mips_align (3, 0, label);
11511       else
11512         mips_align (2, 0, label);
11513     }
11514
11515   mips_clear_insn_labels ();
11516
11517   float_cons (type);
11518 }
11519
11520 /* Handle .globl.  We need to override it because on Irix 5 you are
11521    permitted to say
11522        .globl foo .text
11523    where foo is an undefined symbol, to mean that foo should be
11524    considered to be the address of a function.  */
11525
11526 static void
11527 s_mips_globl (x)
11528      int x ATTRIBUTE_UNUSED;
11529 {
11530   char *name;
11531   int c;
11532   symbolS *symbolP;
11533   flagword flag;
11534
11535   name = input_line_pointer;
11536   c = get_symbol_end ();
11537   symbolP = symbol_find_or_make (name);
11538   *input_line_pointer = c;
11539   SKIP_WHITESPACE ();
11540
11541   /* On Irix 5, every global symbol that is not explicitly labelled as
11542      being a function is apparently labelled as being an object.  */
11543   flag = BSF_OBJECT;
11544
11545   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11546     {
11547       char *secname;
11548       asection *sec;
11549
11550       secname = input_line_pointer;
11551       c = get_symbol_end ();
11552       sec = bfd_get_section_by_name (stdoutput, secname);
11553       if (sec == NULL)
11554         as_bad (_("%s: no such section"), secname);
11555       *input_line_pointer = c;
11556
11557       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11558         flag = BSF_FUNCTION;
11559     }
11560
11561   symbol_get_bfdsym (symbolP)->flags |= flag;
11562
11563   S_SET_EXTERNAL (symbolP);
11564   demand_empty_rest_of_line ();
11565 }
11566
11567 static void
11568 s_option (x)
11569      int x ATTRIBUTE_UNUSED;
11570 {
11571   char *opt;
11572   char c;
11573
11574   opt = input_line_pointer;
11575   c = get_symbol_end ();
11576
11577   if (*opt == 'O')
11578     {
11579       /* FIXME: What does this mean?  */
11580     }
11581   else if (strncmp (opt, "pic", 3) == 0)
11582     {
11583       int i;
11584
11585       i = atoi (opt + 3);
11586       if (i == 0)
11587         mips_pic = NO_PIC;
11588       else if (i == 2)
11589         mips_pic = SVR4_PIC;
11590       else
11591         as_bad (_(".option pic%d not supported"), i);
11592
11593       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11594         {
11595           if (g_switch_seen && g_switch_value != 0)
11596             as_warn (_("-G may not be used with SVR4 PIC code"));
11597           g_switch_value = 0;
11598           bfd_set_gp_size (stdoutput, 0);
11599         }
11600     }
11601   else
11602     as_warn (_("Unrecognized option \"%s\""), opt);
11603
11604   *input_line_pointer = c;
11605   demand_empty_rest_of_line ();
11606 }
11607
11608 /* This structure is used to hold a stack of .set values.  */
11609
11610 struct mips_option_stack
11611 {
11612   struct mips_option_stack *next;
11613   struct mips_set_options options;
11614 };
11615
11616 static struct mips_option_stack *mips_opts_stack;
11617
11618 /* Handle the .set pseudo-op.  */
11619
11620 static void
11621 s_mipsset (x)
11622      int x ATTRIBUTE_UNUSED;
11623 {
11624   char *name = input_line_pointer, ch;
11625
11626   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11627     ++input_line_pointer;
11628   ch = *input_line_pointer;
11629   *input_line_pointer = '\0';
11630
11631   if (strcmp (name, "reorder") == 0)
11632     {
11633       if (mips_opts.noreorder && prev_nop_frag != NULL)
11634         {
11635           /* If we still have pending nops, we can discard them.  The
11636              usual nop handling will insert any that are still
11637              needed.  */
11638           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11639                                     * (mips_opts.mips16 ? 2 : 4));
11640           prev_nop_frag = NULL;
11641         }
11642       mips_opts.noreorder = 0;
11643     }
11644   else if (strcmp (name, "noreorder") == 0)
11645     {
11646       mips_emit_delays (true);
11647       mips_opts.noreorder = 1;
11648       mips_any_noreorder = 1;
11649     }
11650   else if (strcmp (name, "at") == 0)
11651     {
11652       mips_opts.noat = 0;
11653     }
11654   else if (strcmp (name, "noat") == 0)
11655     {
11656       mips_opts.noat = 1;
11657     }
11658   else if (strcmp (name, "macro") == 0)
11659     {
11660       mips_opts.warn_about_macros = 0;
11661     }
11662   else if (strcmp (name, "nomacro") == 0)
11663     {
11664       if (mips_opts.noreorder == 0)
11665         as_bad (_("`noreorder' must be set before `nomacro'"));
11666       mips_opts.warn_about_macros = 1;
11667     }
11668   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11669     {
11670       mips_opts.nomove = 0;
11671     }
11672   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11673     {
11674       mips_opts.nomove = 1;
11675     }
11676   else if (strcmp (name, "bopt") == 0)
11677     {
11678       mips_opts.nobopt = 0;
11679     }
11680   else if (strcmp (name, "nobopt") == 0)
11681     {
11682       mips_opts.nobopt = 1;
11683     }
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 (strcmp (name, "mdmx") == 0)
11695     mips_opts.ase_mdmx = 1;
11696   else if (strcmp (name, "nomdmx") == 0)
11697     mips_opts.ase_mdmx = 0;
11698   else if (strncmp (name, "mips", 4) == 0)
11699     {
11700       int isa;
11701
11702       /* Permit the user to change the ISA on the fly.  Needless to
11703          say, misuse can cause serious problems.  */
11704       isa = atoi (name + 4);
11705       switch (isa)
11706         {
11707         case  0:
11708           mips_opts.gp32 = file_mips_gp32;
11709           mips_opts.fp32 = file_mips_fp32;
11710           mips_opts.abi = file_mips_abi;
11711           break;
11712         case  1:
11713         case  2:
11714         case 32:
11715           mips_opts.gp32 = 1;
11716           mips_opts.fp32 = 1;
11717           break;
11718         case  3:
11719         case  4:
11720         case  5:
11721         case 64:
11722           /* Loosen ABI register width restriction.  */
11723           if (mips_opts.abi == O32_ABI)
11724             mips_opts.abi = NO_ABI;
11725           mips_opts.gp32 = 0;
11726           mips_opts.fp32 = 0;
11727           break;
11728         default:
11729           as_bad (_("unknown ISA level %s"), name + 4);
11730           break;
11731         }
11732
11733       switch (isa)
11734         {
11735         case  0: mips_opts.isa = file_mips_isa;   break;
11736         case  1: mips_opts.isa = ISA_MIPS1;       break;
11737         case  2: mips_opts.isa = ISA_MIPS2;       break;
11738         case  3: mips_opts.isa = ISA_MIPS3;       break;
11739         case  4: mips_opts.isa = ISA_MIPS4;       break;
11740         case  5: mips_opts.isa = ISA_MIPS5;       break;
11741         case 32: mips_opts.isa = ISA_MIPS32;      break;
11742         case 64: mips_opts.isa = ISA_MIPS64;      break;
11743         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11744         }
11745     }
11746   else if (strcmp (name, "autoextend") == 0)
11747     mips_opts.noautoextend = 0;
11748   else if (strcmp (name, "noautoextend") == 0)
11749     mips_opts.noautoextend = 1;
11750   else if (strcmp (name, "push") == 0)
11751     {
11752       struct mips_option_stack *s;
11753
11754       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11755       s->next = mips_opts_stack;
11756       s->options = mips_opts;
11757       mips_opts_stack = s;
11758     }
11759   else if (strcmp (name, "pop") == 0)
11760     {
11761       struct mips_option_stack *s;
11762
11763       s = mips_opts_stack;
11764       if (s == NULL)
11765         as_bad (_(".set pop with no .set push"));
11766       else
11767         {
11768           /* If we're changing the reorder mode we need to handle
11769              delay slots correctly.  */
11770           if (s->options.noreorder && ! mips_opts.noreorder)
11771             mips_emit_delays (true);
11772           else if (! s->options.noreorder && mips_opts.noreorder)
11773             {
11774               if (prev_nop_frag != NULL)
11775                 {
11776                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11777                                             * (mips_opts.mips16 ? 2 : 4));
11778                   prev_nop_frag = NULL;
11779                 }
11780             }
11781
11782           mips_opts = s->options;
11783           mips_opts_stack = s->next;
11784           free (s);
11785         }
11786     }
11787   else
11788     {
11789       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11790     }
11791   *input_line_pointer = ch;
11792   demand_empty_rest_of_line ();
11793 }
11794
11795 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11796    .option pic2.  It means to generate SVR4 PIC calls.  */
11797
11798 static void
11799 s_abicalls (ignore)
11800      int ignore ATTRIBUTE_UNUSED;
11801 {
11802   mips_pic = SVR4_PIC;
11803   if (USE_GLOBAL_POINTER_OPT)
11804     {
11805       if (g_switch_seen && g_switch_value != 0)
11806         as_warn (_("-G may not be used with SVR4 PIC code"));
11807       g_switch_value = 0;
11808     }
11809   bfd_set_gp_size (stdoutput, 0);
11810   demand_empty_rest_of_line ();
11811 }
11812
11813 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11814    PIC code.  It sets the $gp register for the function based on the
11815    function address, which is in the register named in the argument.
11816    This uses a relocation against _gp_disp, which is handled specially
11817    by the linker.  The result is:
11818         lui     $gp,%hi(_gp_disp)
11819         addiu   $gp,$gp,%lo(_gp_disp)
11820         addu    $gp,$gp,.cpload argument
11821    The .cpload argument is normally $25 == $t9.  */
11822
11823 static void
11824 s_cpload (ignore)
11825      int ignore ATTRIBUTE_UNUSED;
11826 {
11827   expressionS ex;
11828   int icnt = 0;
11829
11830   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11831      .cpload is ignored.  */
11832   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11833     {
11834       s_ignore (0);
11835       return;
11836     }
11837
11838   /* .cpload should be in a .set noreorder section.  */
11839   if (mips_opts.noreorder == 0)
11840     as_warn (_(".cpload not in noreorder section"));
11841
11842   ex.X_op = O_symbol;
11843   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11844   ex.X_op_symbol = NULL;
11845   ex.X_add_number = 0;
11846
11847   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11848   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11849
11850   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11851   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11852                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11853
11854   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11855                mips_gp_register, mips_gp_register, tc_get_register (0));
11856
11857   demand_empty_rest_of_line ();
11858 }
11859
11860 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11861      .cpsetup $reg1, offset|$reg2, label
11862
11863    If offset is given, this results in:
11864      sd         $gp, offset($sp)
11865      lui        $gp, %hi(%neg(%gp_rel(label)))
11866      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11867      daddu      $gp, $gp, $reg1
11868
11869    If $reg2 is given, this results in:
11870      daddu      $reg2, $gp, $0
11871      lui        $gp, %hi(%neg(%gp_rel(label)))
11872      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11873      daddu      $gp, $gp, $reg1
11874    $reg1 is normally $25 == $t9.  */
11875 static void
11876 s_cpsetup (ignore)
11877      int ignore ATTRIBUTE_UNUSED;
11878 {
11879   expressionS ex_off;
11880   expressionS ex_sym;
11881   int reg1;
11882   int icnt = 0;
11883   char *sym;
11884
11885   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11886      We also need NewABI support.  */
11887   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11888     {
11889       s_ignore (0);
11890       return;
11891     }
11892
11893   reg1 = tc_get_register (0);
11894   SKIP_WHITESPACE ();
11895   if (*input_line_pointer != ',')
11896     {
11897       as_bad (_("missing argument separator ',' for .cpsetup"));
11898       return;
11899     }
11900   else
11901     ++input_line_pointer;
11902   SKIP_WHITESPACE ();
11903   if (*input_line_pointer == '$')
11904     {
11905       mips_cpreturn_register = tc_get_register (0);
11906       mips_cpreturn_offset = -1;
11907     }
11908   else
11909     {
11910       mips_cpreturn_offset = get_absolute_expression ();
11911       mips_cpreturn_register = -1;
11912     }
11913   SKIP_WHITESPACE ();
11914   if (*input_line_pointer != ',')
11915     {
11916       as_bad (_("missing argument separator ',' for .cpsetup"));
11917       return;
11918     }
11919   else
11920     ++input_line_pointer;
11921   SKIP_WHITESPACE ();
11922   sym = input_line_pointer;
11923   while (ISALNUM (*input_line_pointer))
11924     ++input_line_pointer;
11925   *input_line_pointer = 0;
11926
11927   ex_sym.X_op = O_symbol;
11928   ex_sym.X_add_symbol = symbol_find_or_make (sym);
11929   ex_sym.X_op_symbol = NULL;
11930   ex_sym.X_add_number = 0;
11931
11932   if (mips_cpreturn_register == -1)
11933     {
11934       ex_off.X_op = O_constant;
11935       ex_off.X_add_symbol = NULL;
11936       ex_off.X_op_symbol = NULL;
11937       ex_off.X_add_number = mips_cpreturn_offset;
11938
11939       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11940                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11941     }
11942   else
11943     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11944                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11945
11946   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11947                (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_HI16_S);
11950   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11951                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11952   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11953   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_LO16);
11954   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11955                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11956                mips_gp_register, mips_gp_register, reg1);
11957
11958   demand_empty_rest_of_line ();
11959 }
11960
11961 static void
11962 s_cplocal (ignore)
11963      int ignore ATTRIBUTE_UNUSED;
11964 {
11965   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11966    .cplocal is ignored.  */
11967   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11968     {
11969       s_ignore (0);
11970       return;
11971     }
11972
11973   mips_gp_register = tc_get_register (0);
11974   demand_empty_rest_of_line ();
11975 }
11976
11977 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11978    offset from $sp.  The offset is remembered, and after making a PIC
11979    call $gp is restored from that location.  */
11980
11981 static void
11982 s_cprestore (ignore)
11983      int ignore ATTRIBUTE_UNUSED;
11984 {
11985   expressionS ex;
11986   int icnt = 0;
11987
11988   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11989      .cprestore is ignored.  */
11990   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11991     {
11992       s_ignore (0);
11993       return;
11994     }
11995
11996   mips_cprestore_offset = get_absolute_expression ();
11997   mips_cprestore_valid = 1;
11998
11999   ex.X_op = O_constant;
12000   ex.X_add_symbol = NULL;
12001   ex.X_op_symbol = NULL;
12002   ex.X_add_number = mips_cprestore_offset;
12003
12004   macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12005                "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
12006
12007   demand_empty_rest_of_line ();
12008 }
12009
12010 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12011    was given in the preceeding .gpsetup, it results in:
12012      ld         $gp, offset($sp)
12013
12014    If a register $reg2 was given there, it results in:
12015      daddiu     $gp, $gp, $reg2
12016  */
12017 static void
12018 s_cpreturn (ignore)
12019      int ignore ATTRIBUTE_UNUSED;
12020 {
12021   expressionS ex;
12022   int icnt = 0;
12023
12024   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12025      We also need NewABI support.  */
12026   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12027     {
12028       s_ignore (0);
12029       return;
12030     }
12031
12032   if (mips_cpreturn_register == -1)
12033     {
12034       ex.X_op = O_constant;
12035       ex.X_add_symbol = NULL;
12036       ex.X_op_symbol = NULL;
12037       ex.X_add_number = mips_cpreturn_offset;
12038
12039       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12040                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12041     }
12042   else
12043     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12044                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12045
12046   demand_empty_rest_of_line ();
12047 }
12048
12049 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12050    code.  It sets the offset to use in gp_rel relocations.  */
12051
12052 static void
12053 s_gpvalue (ignore)
12054      int ignore ATTRIBUTE_UNUSED;
12055 {
12056   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12057      We also need NewABI support.  */
12058   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12059     {
12060       s_ignore (0);
12061       return;
12062     }
12063
12064   mips_gprel_offset = get_absolute_expression ();
12065
12066   demand_empty_rest_of_line ();
12067 }
12068
12069 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12070    code.  It generates a 32 bit GP relative reloc.  */
12071
12072 static void
12073 s_gpword (ignore)
12074      int ignore ATTRIBUTE_UNUSED;
12075 {
12076   symbolS *label;
12077   expressionS ex;
12078   char *p;
12079
12080   /* When not generating PIC code, this is treated as .word.  */
12081   if (mips_pic != SVR4_PIC)
12082     {
12083       s_cons (2);
12084       return;
12085     }
12086
12087   label = insn_labels != NULL ? insn_labels->label : NULL;
12088   mips_emit_delays (true);
12089   if (auto_align)
12090     mips_align (2, 0, label);
12091   mips_clear_insn_labels ();
12092
12093   expression (&ex);
12094
12095   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12096     {
12097       as_bad (_("Unsupported use of .gpword"));
12098       ignore_rest_of_line ();
12099     }
12100
12101   p = frag_more (4);
12102   md_number_to_chars (p, (valueT) 0, 4);
12103   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12104                BFD_RELOC_GPREL32);
12105
12106   demand_empty_rest_of_line ();
12107 }
12108
12109 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12110    tables in SVR4 PIC code.  */
12111
12112 static void
12113 s_cpadd (ignore)
12114      int ignore ATTRIBUTE_UNUSED;
12115 {
12116   int icnt = 0;
12117   int reg;
12118
12119   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12120      code.  */
12121   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12122     {
12123       s_ignore (0);
12124       return;
12125     }
12126
12127   /* Add $gp to the register named as an argument.  */
12128   reg = tc_get_register (0);
12129   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12130                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12131                "d,v,t", reg, reg, mips_gp_register);
12132
12133   demand_empty_rest_of_line ();
12134 }
12135
12136 /* Handle the .insn pseudo-op.  This marks instruction labels in
12137    mips16 mode.  This permits the linker to handle them specially,
12138    such as generating jalx instructions when needed.  We also make
12139    them odd for the duration of the assembly, in order to generate the
12140    right sort of code.  We will make them even in the adjust_symtab
12141    routine, while leaving them marked.  This is convenient for the
12142    debugger and the disassembler.  The linker knows to make them odd
12143    again.  */
12144
12145 static void
12146 s_insn (ignore)
12147      int ignore ATTRIBUTE_UNUSED;
12148 {
12149   mips16_mark_labels ();
12150
12151   demand_empty_rest_of_line ();
12152 }
12153
12154 /* Handle a .stabn directive.  We need these in order to mark a label
12155    as being a mips16 text label correctly.  Sometimes the compiler
12156    will emit a label, followed by a .stabn, and then switch sections.
12157    If the label and .stabn are in mips16 mode, then the label is
12158    really a mips16 text label.  */
12159
12160 static void
12161 s_mips_stab (type)
12162      int type;
12163 {
12164   if (type == 'n')
12165     mips16_mark_labels ();
12166
12167   s_stab (type);
12168 }
12169
12170 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12171  */
12172
12173 static void
12174 s_mips_weakext (ignore)
12175      int ignore ATTRIBUTE_UNUSED;
12176 {
12177   char *name;
12178   int c;
12179   symbolS *symbolP;
12180   expressionS exp;
12181
12182   name = input_line_pointer;
12183   c = get_symbol_end ();
12184   symbolP = symbol_find_or_make (name);
12185   S_SET_WEAK (symbolP);
12186   *input_line_pointer = c;
12187
12188   SKIP_WHITESPACE ();
12189
12190   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12191     {
12192       if (S_IS_DEFINED (symbolP))
12193         {
12194           as_bad ("ignoring attempt to redefine symbol %s",
12195                   S_GET_NAME (symbolP));
12196           ignore_rest_of_line ();
12197           return;
12198         }
12199
12200       if (*input_line_pointer == ',')
12201         {
12202           ++input_line_pointer;
12203           SKIP_WHITESPACE ();
12204         }
12205
12206       expression (&exp);
12207       if (exp.X_op != O_symbol)
12208         {
12209           as_bad ("bad .weakext directive");
12210           ignore_rest_of_line ();
12211           return;
12212         }
12213       symbol_set_value_expression (symbolP, &exp);
12214     }
12215
12216   demand_empty_rest_of_line ();
12217 }
12218
12219 /* Parse a register string into a number.  Called from the ECOFF code
12220    to parse .frame.  The argument is non-zero if this is the frame
12221    register, so that we can record it in mips_frame_reg.  */
12222
12223 int
12224 tc_get_register (frame)
12225      int frame;
12226 {
12227   int reg;
12228
12229   SKIP_WHITESPACE ();
12230   if (*input_line_pointer++ != '$')
12231     {
12232       as_warn (_("expected `$'"));
12233       reg = ZERO;
12234     }
12235   else if (ISDIGIT (*input_line_pointer))
12236     {
12237       reg = get_absolute_expression ();
12238       if (reg < 0 || reg >= 32)
12239         {
12240           as_warn (_("Bad register number"));
12241           reg = ZERO;
12242         }
12243     }
12244   else
12245     {
12246       if (strncmp (input_line_pointer, "ra", 2) == 0)
12247         {
12248           reg = RA;
12249           input_line_pointer += 2;
12250         }
12251       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12252         {
12253           reg = FP;
12254           input_line_pointer += 2;
12255         }
12256       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12257         {
12258           reg = SP;
12259           input_line_pointer += 2;
12260         }
12261       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12262         {
12263           reg = GP;
12264           input_line_pointer += 2;
12265         }
12266       else if (strncmp (input_line_pointer, "at", 2) == 0)
12267         {
12268           reg = AT;
12269           input_line_pointer += 2;
12270         }
12271       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12272         {
12273           reg = KT0;
12274           input_line_pointer += 3;
12275         }
12276       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12277         {
12278           reg = KT1;
12279           input_line_pointer += 3;
12280         }
12281       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12282         {
12283           reg = ZERO;
12284           input_line_pointer += 4;
12285         }
12286       else
12287         {
12288           as_warn (_("Unrecognized register name"));
12289           reg = ZERO;
12290           while (ISALNUM(*input_line_pointer))
12291            input_line_pointer++;
12292         }
12293     }
12294   if (frame)
12295     {
12296       mips_frame_reg = reg != 0 ? reg : SP;
12297       mips_frame_reg_valid = 1;
12298       mips_cprestore_valid = 0;
12299     }
12300   return reg;
12301 }
12302
12303 valueT
12304 md_section_align (seg, addr)
12305      asection *seg;
12306      valueT addr;
12307 {
12308   int align = bfd_get_section_alignment (stdoutput, seg);
12309
12310 #ifdef OBJ_ELF
12311   /* We don't need to align ELF sections to the full alignment.
12312      However, Irix 5 may prefer that we align them at least to a 16
12313      byte boundary.  We don't bother to align the sections if we are
12314      targeted for an embedded system.  */
12315   if (strcmp (TARGET_OS, "elf") == 0)
12316     return addr;
12317   if (align > 4)
12318     align = 4;
12319 #endif
12320
12321   return ((addr + (1 << align) - 1) & (-1 << align));
12322 }
12323
12324 /* Utility routine, called from above as well.  If called while the
12325    input file is still being read, it's only an approximation.  (For
12326    example, a symbol may later become defined which appeared to be
12327    undefined earlier.)  */
12328
12329 static int
12330 nopic_need_relax (sym, before_relaxing)
12331      symbolS *sym;
12332      int before_relaxing;
12333 {
12334   if (sym == 0)
12335     return 0;
12336
12337   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12338     {
12339       const char *symname;
12340       int change;
12341
12342       /* Find out whether this symbol can be referenced off the $gp
12343          register.  It can be if it is smaller than the -G size or if
12344          it is in the .sdata or .sbss section.  Certain symbols can
12345          not be referenced off the $gp, although it appears as though
12346          they can.  */
12347       symname = S_GET_NAME (sym);
12348       if (symname != (const char *) NULL
12349           && (strcmp (symname, "eprol") == 0
12350               || strcmp (symname, "etext") == 0
12351               || strcmp (symname, "_gp") == 0
12352               || strcmp (symname, "edata") == 0
12353               || strcmp (symname, "_fbss") == 0
12354               || strcmp (symname, "_fdata") == 0
12355               || strcmp (symname, "_ftext") == 0
12356               || strcmp (symname, "end") == 0
12357               || strcmp (symname, "_gp_disp") == 0))
12358         change = 1;
12359       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12360                && (0
12361 #ifndef NO_ECOFF_DEBUGGING
12362                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12363                        && (symbol_get_obj (sym)->ecoff_extern_size
12364                            <= g_switch_value))
12365 #endif
12366                    /* We must defer this decision until after the whole
12367                       file has been read, since there might be a .extern
12368                       after the first use of this symbol.  */
12369                    || (before_relaxing
12370 #ifndef NO_ECOFF_DEBUGGING
12371                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12372 #endif
12373                        && S_GET_VALUE (sym) == 0)
12374                    || (S_GET_VALUE (sym) != 0
12375                        && S_GET_VALUE (sym) <= g_switch_value)))
12376         change = 0;
12377       else
12378         {
12379           const char *segname;
12380
12381           segname = segment_name (S_GET_SEGMENT (sym));
12382           assert (strcmp (segname, ".lit8") != 0
12383                   && strcmp (segname, ".lit4") != 0);
12384           change = (strcmp (segname, ".sdata") != 0
12385                     && strcmp (segname, ".sbss") != 0
12386                     && strncmp (segname, ".sdata.", 7) != 0
12387                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12388         }
12389       return change;
12390     }
12391   else
12392     /* We are not optimizing for the $gp register.  */
12393     return 1;
12394 }
12395
12396 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12397    extended opcode.  SEC is the section the frag is in.  */
12398
12399 static int
12400 mips16_extended_frag (fragp, sec, stretch)
12401      fragS *fragp;
12402      asection *sec;
12403      long stretch;
12404 {
12405   int type;
12406   register const struct mips16_immed_operand *op;
12407   offsetT val;
12408   int mintiny, maxtiny;
12409   segT symsec;
12410   fragS *sym_frag;
12411
12412   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12413     return 0;
12414   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12415     return 1;
12416
12417   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12418   op = mips16_immed_operands;
12419   while (op->type != type)
12420     {
12421       ++op;
12422       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12423     }
12424
12425   if (op->unsp)
12426     {
12427       if (type == '<' || type == '>' || type == '[' || type == ']')
12428         {
12429           mintiny = 1;
12430           maxtiny = 1 << op->nbits;
12431         }
12432       else
12433         {
12434           mintiny = 0;
12435           maxtiny = (1 << op->nbits) - 1;
12436         }
12437     }
12438   else
12439     {
12440       mintiny = - (1 << (op->nbits - 1));
12441       maxtiny = (1 << (op->nbits - 1)) - 1;
12442     }
12443
12444   sym_frag = symbol_get_frag (fragp->fr_symbol);
12445   val = S_GET_VALUE (fragp->fr_symbol);
12446   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12447
12448   if (op->pcrel)
12449     {
12450       addressT addr;
12451
12452       /* We won't have the section when we are called from
12453          mips_relax_frag.  However, we will always have been called
12454          from md_estimate_size_before_relax first.  If this is a
12455          branch to a different section, we mark it as such.  If SEC is
12456          NULL, and the frag is not marked, then it must be a branch to
12457          the same section.  */
12458       if (sec == NULL)
12459         {
12460           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12461             return 1;
12462         }
12463       else
12464         {
12465           /* Must have been called from md_estimate_size_before_relax.  */
12466           if (symsec != sec)
12467             {
12468               fragp->fr_subtype =
12469                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12470
12471               /* FIXME: We should support this, and let the linker
12472                  catch branches and loads that are out of range.  */
12473               as_bad_where (fragp->fr_file, fragp->fr_line,
12474                             _("unsupported PC relative reference to different section"));
12475
12476               return 1;
12477             }
12478           if (fragp != sym_frag && sym_frag->fr_address == 0)
12479             /* Assume non-extended on the first relaxation pass.
12480                The address we have calculated will be bogus if this is
12481                a forward branch to another frag, as the forward frag
12482                will have fr_address == 0.  */
12483             return 0;
12484         }
12485
12486       /* In this case, we know for sure that the symbol fragment is in
12487          the same section.  If the relax_marker of the symbol fragment
12488          differs from the relax_marker of this fragment, we have not
12489          yet adjusted the symbol fragment fr_address.  We want to add
12490          in STRETCH in order to get a better estimate of the address.
12491          This particularly matters because of the shift bits.  */
12492       if (stretch != 0
12493           && sym_frag->relax_marker != fragp->relax_marker)
12494         {
12495           fragS *f;
12496
12497           /* Adjust stretch for any alignment frag.  Note that if have
12498              been expanding the earlier code, the symbol may be
12499              defined in what appears to be an earlier frag.  FIXME:
12500              This doesn't handle the fr_subtype field, which specifies
12501              a maximum number of bytes to skip when doing an
12502              alignment.  */
12503           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12504             {
12505               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12506                 {
12507                   if (stretch < 0)
12508                     stretch = - ((- stretch)
12509                                  & ~ ((1 << (int) f->fr_offset) - 1));
12510                   else
12511                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12512                   if (stretch == 0)
12513                     break;
12514                 }
12515             }
12516           if (f != NULL)
12517             val += stretch;
12518         }
12519
12520       addr = fragp->fr_address + fragp->fr_fix;
12521
12522       /* The base address rules are complicated.  The base address of
12523          a branch is the following instruction.  The base address of a
12524          PC relative load or add is the instruction itself, but if it
12525          is in a delay slot (in which case it can not be extended) use
12526          the address of the instruction whose delay slot it is in.  */
12527       if (type == 'p' || type == 'q')
12528         {
12529           addr += 2;
12530
12531           /* If we are currently assuming that this frag should be
12532              extended, then, the current address is two bytes
12533              higher.  */
12534           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12535             addr += 2;
12536
12537           /* Ignore the low bit in the target, since it will be set
12538              for a text label.  */
12539           if ((val & 1) != 0)
12540             --val;
12541         }
12542       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12543         addr -= 4;
12544       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12545         addr -= 2;
12546
12547       val -= addr & ~ ((1 << op->shift) - 1);
12548
12549       /* Branch offsets have an implicit 0 in the lowest bit.  */
12550       if (type == 'p' || type == 'q')
12551         val /= 2;
12552
12553       /* If any of the shifted bits are set, we must use an extended
12554          opcode.  If the address depends on the size of this
12555          instruction, this can lead to a loop, so we arrange to always
12556          use an extended opcode.  We only check this when we are in
12557          the main relaxation loop, when SEC is NULL.  */
12558       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12559         {
12560           fragp->fr_subtype =
12561             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12562           return 1;
12563         }
12564
12565       /* If we are about to mark a frag as extended because the value
12566          is precisely maxtiny + 1, then there is a chance of an
12567          infinite loop as in the following code:
12568              la $4,foo
12569              .skip      1020
12570              .align     2
12571            foo:
12572          In this case when the la is extended, foo is 0x3fc bytes
12573          away, so the la can be shrunk, but then foo is 0x400 away, so
12574          the la must be extended.  To avoid this loop, we mark the
12575          frag as extended if it was small, and is about to become
12576          extended with a value of maxtiny + 1.  */
12577       if (val == ((maxtiny + 1) << op->shift)
12578           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12579           && sec == NULL)
12580         {
12581           fragp->fr_subtype =
12582             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12583           return 1;
12584         }
12585     }
12586   else if (symsec != absolute_section && sec != NULL)
12587     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12588
12589   if ((val & ((1 << op->shift) - 1)) != 0
12590       || val < (mintiny << op->shift)
12591       || val > (maxtiny << op->shift))
12592     return 1;
12593   else
12594     return 0;
12595 }
12596
12597 /* Estimate the size of a frag before relaxing.  Unless this is the
12598    mips16, we are not really relaxing here, and the final size is
12599    encoded in the subtype information.  For the mips16, we have to
12600    decide whether we are using an extended opcode or not.  */
12601
12602 int
12603 md_estimate_size_before_relax (fragp, segtype)
12604      fragS *fragp;
12605      asection *segtype;
12606 {
12607   int change = 0;
12608   boolean linkonce = false;
12609
12610   if (RELAX_MIPS16_P (fragp->fr_subtype))
12611     /* We don't want to modify the EXTENDED bit here; it might get us
12612        into infinite loops.  We change it only in mips_relax_frag().  */
12613     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12614
12615   if (mips_pic == NO_PIC)
12616     {
12617       change = nopic_need_relax (fragp->fr_symbol, 0);
12618     }
12619   else if (mips_pic == SVR4_PIC)
12620     {
12621       symbolS *sym;
12622       asection *symsec;
12623
12624       sym = fragp->fr_symbol;
12625
12626       /* Handle the case of a symbol equated to another symbol.  */
12627       while (symbol_equated_reloc_p (sym))
12628         {
12629           symbolS *n;
12630
12631           /* It's possible to get a loop here in a badly written
12632              program.  */
12633           n = symbol_get_value_expression (sym)->X_add_symbol;
12634           if (n == sym)
12635             break;
12636           sym = n;
12637         }
12638
12639       symsec = S_GET_SEGMENT (sym);
12640
12641       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12642       if (symsec != segtype && ! S_IS_LOCAL (sym))
12643         {
12644           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12645               != 0)
12646             linkonce = true;
12647
12648           /* The GNU toolchain uses an extension for ELF: a section
12649              beginning with the magic string .gnu.linkonce is a linkonce
12650              section.  */
12651           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12652                        sizeof ".gnu.linkonce" - 1) == 0)
12653             linkonce = true;
12654         }
12655
12656       /* This must duplicate the test in adjust_reloc_syms.  */
12657       change = (symsec != &bfd_und_section
12658                 && symsec != &bfd_abs_section
12659                 && ! bfd_is_com_section (symsec)
12660                 && !linkonce
12661 #ifdef OBJ_ELF
12662                 /* A global or weak symbol is treated as external.  */
12663                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12664                     || (! S_IS_WEAK (sym)
12665                         && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
12666 #endif
12667                 );
12668     }
12669   else
12670     abort ();
12671
12672   if (change)
12673     {
12674       /* Record the offset to the first reloc in the fr_opcode field.
12675          This lets md_convert_frag and tc_gen_reloc know that the code
12676          must be expanded.  */
12677       fragp->fr_opcode = (fragp->fr_literal
12678                           + fragp->fr_fix
12679                           - RELAX_OLD (fragp->fr_subtype)
12680                           + RELAX_RELOC1 (fragp->fr_subtype));
12681       /* FIXME: This really needs as_warn_where.  */
12682       if (RELAX_WARN (fragp->fr_subtype))
12683         as_warn (_("AT used after \".set noat\" or macro used after "
12684                    "\".set nomacro\""));
12685
12686       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12687     }
12688
12689   return 0;
12690 }
12691
12692 /* This is called to see whether a reloc against a defined symbol
12693    should be converted into a reloc against a section.  Don't adjust
12694    MIPS16 jump relocations, so we don't have to worry about the format
12695    of the offset in the .o file.  Don't adjust relocations against
12696    mips16 symbols, so that the linker can find them if it needs to set
12697    up a stub.  */
12698
12699 int
12700 mips_fix_adjustable (fixp)
12701      fixS *fixp;
12702 {
12703 #ifdef OBJ_ELF
12704   /* Prevent all adjustments to global symbols.  */
12705   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12706       && mips_pic != EMBEDDED_PIC
12707       && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12708     return 0;
12709 #endif
12710   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12711     return 0;
12712   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12713       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12714     return 0;
12715   if (fixp->fx_addsy == NULL)
12716     return 1;
12717 #ifdef OBJ_ELF
12718   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12719       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12720       && fixp->fx_subsy == NULL)
12721     return 0;
12722 #endif
12723   return 1;
12724 }
12725
12726 /* Translate internal representation of relocation info to BFD target
12727    format.  */
12728
12729 arelent **
12730 tc_gen_reloc (section, fixp)
12731      asection *section ATTRIBUTE_UNUSED;
12732      fixS *fixp;
12733 {
12734   static arelent *retval[4];
12735   arelent *reloc;
12736   bfd_reloc_code_real_type code;
12737
12738   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12739   retval[1] = NULL;
12740
12741   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12742   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12743   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12744
12745   if (mips_pic == EMBEDDED_PIC
12746       && SWITCH_TABLE (fixp))
12747     {
12748       /* For a switch table entry we use a special reloc.  The addend
12749          is actually the difference between the reloc address and the
12750          subtrahend.  */
12751       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12752       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12753         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12754       fixp->fx_r_type = BFD_RELOC_GPREL32;
12755     }
12756   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12757     {
12758       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12759         reloc->addend = fixp->fx_addnumber;
12760       else
12761         {
12762           /* We use a special addend for an internal RELLO reloc.  */
12763           if (symbol_section_p (fixp->fx_addsy))
12764             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12765           else
12766             reloc->addend = fixp->fx_addnumber + reloc->address;
12767         }
12768     }
12769   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12770     {
12771       assert (fixp->fx_next != NULL
12772               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12773
12774       /* The reloc is relative to the RELLO; adjust the addend
12775          accordingly.  */
12776       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12777         reloc->addend = fixp->fx_next->fx_addnumber;
12778       else
12779         {
12780           /* We use a special addend for an internal RELHI reloc.  */
12781           if (symbol_section_p (fixp->fx_addsy))
12782             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12783                              + fixp->fx_next->fx_where
12784                              - S_GET_VALUE (fixp->fx_subsy));
12785           else
12786             reloc->addend = (fixp->fx_addnumber
12787                              + fixp->fx_next->fx_frag->fr_address
12788                              + fixp->fx_next->fx_where);
12789         }
12790     }
12791   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12792     reloc->addend = fixp->fx_addnumber;
12793   else
12794     {
12795       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12796         /* A gruesome hack which is a result of the gruesome gas reloc
12797            handling.  */
12798         reloc->addend = reloc->address;
12799       else
12800         reloc->addend = -reloc->address;
12801     }
12802
12803   /* If this is a variant frag, we may need to adjust the existing
12804      reloc and generate a new one.  */
12805   if (fixp->fx_frag->fr_opcode != NULL
12806       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12807           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12808           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12809           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12810           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12811           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12812           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12813       && ! HAVE_NEWABI)
12814     {
12815       arelent *reloc2;
12816
12817       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12818
12819       /* If this is not the last reloc in this frag, then we have two
12820          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12821          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12822          the second one handle all of them.  */
12823       if (fixp->fx_next != NULL
12824           && fixp->fx_frag == fixp->fx_next->fx_frag)
12825         {
12826           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12827                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12828                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12829                       && (fixp->fx_next->fx_r_type
12830                           == BFD_RELOC_MIPS_GOT_LO16))
12831                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12832                       && (fixp->fx_next->fx_r_type
12833                           == BFD_RELOC_MIPS_CALL_LO16)));
12834           retval[0] = NULL;
12835           return retval;
12836         }
12837
12838       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12839       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12840       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12841       retval[2] = NULL;
12842       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12843       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12844       reloc2->address = (reloc->address
12845                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12846                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12847       reloc2->addend = fixp->fx_addnumber;
12848       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12849       assert (reloc2->howto != NULL);
12850
12851       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12852         {
12853           arelent *reloc3;
12854
12855           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12856           retval[3] = NULL;
12857           *reloc3 = *reloc2;
12858           reloc3->address += 4;
12859         }
12860
12861       if (mips_pic == NO_PIC)
12862         {
12863           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12864           fixp->fx_r_type = BFD_RELOC_HI16_S;
12865         }
12866       else if (mips_pic == SVR4_PIC)
12867         {
12868           switch (fixp->fx_r_type)
12869             {
12870             default:
12871               abort ();
12872             case BFD_RELOC_MIPS_GOT16:
12873               break;
12874             case BFD_RELOC_MIPS_CALL16:
12875             case BFD_RELOC_MIPS_GOT_LO16:
12876             case BFD_RELOC_MIPS_CALL_LO16:
12877               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12878               break;
12879             }
12880         }
12881       else
12882         abort ();
12883     }
12884
12885   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12886      entry to be used in the relocation's section offset.  */
12887   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12888     {
12889       reloc->address = reloc->addend;
12890       reloc->addend = 0;
12891     }
12892
12893   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12894      fixup_segment converted a non-PC relative reloc into a PC
12895      relative reloc.  In such a case, we need to convert the reloc
12896      code.  */
12897   code = fixp->fx_r_type;
12898   if (fixp->fx_pcrel)
12899     {
12900       switch (code)
12901         {
12902         case BFD_RELOC_8:
12903           code = BFD_RELOC_8_PCREL;
12904           break;
12905         case BFD_RELOC_16:
12906           code = BFD_RELOC_16_PCREL;
12907           break;
12908         case BFD_RELOC_32:
12909           code = BFD_RELOC_32_PCREL;
12910           break;
12911         case BFD_RELOC_64:
12912           code = BFD_RELOC_64_PCREL;
12913           break;
12914         case BFD_RELOC_8_PCREL:
12915         case BFD_RELOC_16_PCREL:
12916         case BFD_RELOC_32_PCREL:
12917         case BFD_RELOC_64_PCREL:
12918         case BFD_RELOC_16_PCREL_S2:
12919         case BFD_RELOC_PCREL_HI16_S:
12920         case BFD_RELOC_PCREL_LO16:
12921           break;
12922         default:
12923           as_bad_where (fixp->fx_file, fixp->fx_line,
12924                         _("Cannot make %s relocation PC relative"),
12925                         bfd_get_reloc_code_name (code));
12926         }
12927     }
12928
12929 #ifdef OBJ_ELF
12930   /* md_apply_fix3 has a double-subtraction hack to get
12931      bfd_install_relocation to behave nicely.  GPREL relocations are
12932      handled correctly without this hack, so undo it here.  We can't
12933      stop md_apply_fix3 from subtracting twice in the first place since
12934      the fake addend is required for variant frags above.  */
12935   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12936       && code == BFD_RELOC_GPREL16
12937       && reloc->addend != 0
12938       && mips_need_elf_addend_fixup (fixp))
12939     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12940 #endif
12941
12942   /* To support a PC relative reloc when generating embedded PIC code
12943      for ECOFF, we use a Cygnus extension.  We check for that here to
12944      make sure that we don't let such a reloc escape normally.  */
12945   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12946        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12947       && code == BFD_RELOC_16_PCREL_S2
12948       && mips_pic != EMBEDDED_PIC)
12949     reloc->howto = NULL;
12950   else
12951     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12952
12953   if (reloc->howto == NULL)
12954     {
12955       as_bad_where (fixp->fx_file, fixp->fx_line,
12956                     _("Can not represent %s relocation in this object file format"),
12957                     bfd_get_reloc_code_name (code));
12958       retval[0] = NULL;
12959     }
12960
12961   return retval;
12962 }
12963
12964 /* Relax a machine dependent frag.  This returns the amount by which
12965    the current size of the frag should change.  */
12966
12967 int
12968 mips_relax_frag (fragp, stretch)
12969      fragS *fragp;
12970      long stretch;
12971 {
12972   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12973     return 0;
12974
12975   if (mips16_extended_frag (fragp, NULL, stretch))
12976     {
12977       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12978         return 0;
12979       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12980       return 2;
12981     }
12982   else
12983     {
12984       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12985         return 0;
12986       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12987       return -2;
12988     }
12989
12990   return 0;
12991 }
12992
12993 /* Convert a machine dependent frag.  */
12994
12995 void
12996 md_convert_frag (abfd, asec, fragp)
12997      bfd *abfd ATTRIBUTE_UNUSED;
12998      segT asec;
12999      fragS *fragp;
13000 {
13001   int old, new;
13002   char *fixptr;
13003
13004   if (RELAX_MIPS16_P (fragp->fr_subtype))
13005     {
13006       int type;
13007       register const struct mips16_immed_operand *op;
13008       boolean small, ext;
13009       offsetT val;
13010       bfd_byte *buf;
13011       unsigned long insn;
13012       boolean use_extend;
13013       unsigned short extend;
13014
13015       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13016       op = mips16_immed_operands;
13017       while (op->type != type)
13018         ++op;
13019
13020       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13021         {
13022           small = false;
13023           ext = true;
13024         }
13025       else
13026         {
13027           small = true;
13028           ext = false;
13029         }
13030
13031       resolve_symbol_value (fragp->fr_symbol);
13032       val = S_GET_VALUE (fragp->fr_symbol);
13033       if (op->pcrel)
13034         {
13035           addressT addr;
13036
13037           addr = fragp->fr_address + fragp->fr_fix;
13038
13039           /* The rules for the base address of a PC relative reloc are
13040              complicated; see mips16_extended_frag.  */
13041           if (type == 'p' || type == 'q')
13042             {
13043               addr += 2;
13044               if (ext)
13045                 addr += 2;
13046               /* Ignore the low bit in the target, since it will be
13047                  set for a text label.  */
13048               if ((val & 1) != 0)
13049                 --val;
13050             }
13051           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13052             addr -= 4;
13053           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13054             addr -= 2;
13055
13056           addr &= ~ (addressT) ((1 << op->shift) - 1);
13057           val -= addr;
13058
13059           /* Make sure the section winds up with the alignment we have
13060              assumed.  */
13061           if (op->shift > 0)
13062             record_alignment (asec, op->shift);
13063         }
13064
13065       if (ext
13066           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13067               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13068         as_warn_where (fragp->fr_file, fragp->fr_line,
13069                        _("extended instruction in delay slot"));
13070
13071       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13072
13073       if (target_big_endian)
13074         insn = bfd_getb16 (buf);
13075       else
13076         insn = bfd_getl16 (buf);
13077
13078       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13079                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13080                     small, ext, &insn, &use_extend, &extend);
13081
13082       if (use_extend)
13083         {
13084           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13085           fragp->fr_fix += 2;
13086           buf += 2;
13087         }
13088
13089       md_number_to_chars ((char *) buf, insn, 2);
13090       fragp->fr_fix += 2;
13091       buf += 2;
13092     }
13093   else
13094     {
13095       if (fragp->fr_opcode == NULL)
13096         return;
13097
13098       old = RELAX_OLD (fragp->fr_subtype);
13099       new = RELAX_NEW (fragp->fr_subtype);
13100       fixptr = fragp->fr_literal + fragp->fr_fix;
13101
13102       if (new > 0)
13103         memcpy (fixptr - old, fixptr, new);
13104
13105       fragp->fr_fix += new - old;
13106     }
13107 }
13108
13109 #ifdef OBJ_ELF
13110
13111 /* This function is called after the relocs have been generated.
13112    We've been storing mips16 text labels as odd.  Here we convert them
13113    back to even for the convenience of the debugger.  */
13114
13115 void
13116 mips_frob_file_after_relocs ()
13117 {
13118   asymbol **syms;
13119   unsigned int count, i;
13120
13121   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13122     return;
13123
13124   syms = bfd_get_outsymbols (stdoutput);
13125   count = bfd_get_symcount (stdoutput);
13126   for (i = 0; i < count; i++, syms++)
13127     {
13128       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13129           && ((*syms)->value & 1) != 0)
13130         {
13131           (*syms)->value &= ~1;
13132           /* If the symbol has an odd size, it was probably computed
13133              incorrectly, so adjust that as well.  */
13134           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13135             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13136         }
13137     }
13138 }
13139
13140 #endif
13141
13142 /* This function is called whenever a label is defined.  It is used
13143    when handling branch delays; if a branch has a label, we assume we
13144    can not move it.  */
13145
13146 void
13147 mips_define_label (sym)
13148      symbolS *sym;
13149 {
13150   struct insn_label_list *l;
13151
13152   if (free_insn_labels == NULL)
13153     l = (struct insn_label_list *) xmalloc (sizeof *l);
13154   else
13155     {
13156       l = free_insn_labels;
13157       free_insn_labels = l->next;
13158     }
13159
13160   l->label = sym;
13161   l->next = insn_labels;
13162   insn_labels = l;
13163 }
13164 \f
13165 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13166
13167 /* Some special processing for a MIPS ELF file.  */
13168
13169 void
13170 mips_elf_final_processing ()
13171 {
13172   /* Write out the register information.  */
13173   if (file_mips_abi != N64_ABI)
13174     {
13175       Elf32_RegInfo s;
13176
13177       s.ri_gprmask = mips_gprmask;
13178       s.ri_cprmask[0] = mips_cprmask[0];
13179       s.ri_cprmask[1] = mips_cprmask[1];
13180       s.ri_cprmask[2] = mips_cprmask[2];
13181       s.ri_cprmask[3] = mips_cprmask[3];
13182       /* The gp_value field is set by the MIPS ELF backend.  */
13183
13184       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13185                                        ((Elf32_External_RegInfo *)
13186                                         mips_regmask_frag));
13187     }
13188   else
13189     {
13190       Elf64_Internal_RegInfo s;
13191
13192       s.ri_gprmask = mips_gprmask;
13193       s.ri_pad = 0;
13194       s.ri_cprmask[0] = mips_cprmask[0];
13195       s.ri_cprmask[1] = mips_cprmask[1];
13196       s.ri_cprmask[2] = mips_cprmask[2];
13197       s.ri_cprmask[3] = mips_cprmask[3];
13198       /* The gp_value field is set by the MIPS ELF backend.  */
13199
13200       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13201                                        ((Elf64_External_RegInfo *)
13202                                         mips_regmask_frag));
13203     }
13204
13205   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13206      sort of BFD interface for this.  */
13207   if (mips_any_noreorder)
13208     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13209   if (mips_pic != NO_PIC)
13210     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13211
13212   /* Set MIPS ELF flags for ASEs.  */
13213   if (file_ase_mips16)
13214     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13215 #if 0 /* XXX FIXME */
13216   if (file_ase_mips3d)
13217     elf_elfheader (stdoutput)->e_flags |= ???;
13218 #endif
13219   if (file_ase_mdmx)
13220     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13221
13222   /* Set the MIPS ELF ABI flags.  */
13223   if (file_mips_abi == NO_ABI)
13224     ;
13225   else if (file_mips_abi == O32_ABI)
13226     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13227   else if (file_mips_abi == O64_ABI)
13228     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13229   else if (file_mips_abi == EABI_ABI)
13230     {
13231       /* Set the EABI kind based on the ISA.  This isn't really
13232          the best, but then neither is basing the abi on the isa.  */
13233       if (ISA_HAS_64BIT_REGS (file_mips_isa))
13234         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13235       else
13236         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13237     }
13238   else if (file_mips_abi == N32_ABI)
13239     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13240
13241   /* Nothing to do for N64_ABI.  */
13242
13243   if (mips_32bitmode)
13244     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13245 }
13246
13247 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13248 \f
13249 typedef struct proc {
13250   symbolS *isym;
13251   unsigned long reg_mask;
13252   unsigned long reg_offset;
13253   unsigned long fpreg_mask;
13254   unsigned long fpreg_offset;
13255   unsigned long frame_offset;
13256   unsigned long frame_reg;
13257   unsigned long pc_reg;
13258 } procS;
13259
13260 static procS cur_proc;
13261 static procS *cur_proc_ptr;
13262 static int numprocs;
13263
13264 /* Fill in an rs_align_code fragment.  */
13265
13266 void
13267 mips_handle_align (fragp)
13268      fragS *fragp;
13269 {
13270   if (fragp->fr_type != rs_align_code)
13271     return;
13272
13273   if (mips_opts.mips16)
13274     {
13275       static const unsigned char be_nop[] = { 0x65, 0x00 };
13276       static const unsigned char le_nop[] = { 0x00, 0x65 };
13277
13278       int bytes;
13279       char *p;
13280
13281       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13282       p = fragp->fr_literal + fragp->fr_fix;
13283
13284       if (bytes & 1)
13285         {
13286           *p++ = 0;
13287           fragp->fr_fix++;
13288         }
13289
13290       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13291       fragp->fr_var = 2;
13292     }
13293
13294   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13295 }
13296
13297 static void
13298 md_obj_begin ()
13299 {
13300 }
13301
13302 static void
13303 md_obj_end ()
13304 {
13305   /* check for premature end, nesting errors, etc */
13306   if (cur_proc_ptr)
13307     as_warn (_("missing .end at end of assembly"));
13308 }
13309
13310 static long
13311 get_number ()
13312 {
13313   int negative = 0;
13314   long val = 0;
13315
13316   if (*input_line_pointer == '-')
13317     {
13318       ++input_line_pointer;
13319       negative = 1;
13320     }
13321   if (!ISDIGIT (*input_line_pointer))
13322     as_bad (_("expected simple number"));
13323   if (input_line_pointer[0] == '0')
13324     {
13325       if (input_line_pointer[1] == 'x')
13326         {
13327           input_line_pointer += 2;
13328           while (ISXDIGIT (*input_line_pointer))
13329             {
13330               val <<= 4;
13331               val |= hex_value (*input_line_pointer++);
13332             }
13333           return negative ? -val : val;
13334         }
13335       else
13336         {
13337           ++input_line_pointer;
13338           while (ISDIGIT (*input_line_pointer))
13339             {
13340               val <<= 3;
13341               val |= *input_line_pointer++ - '0';
13342             }
13343           return negative ? -val : val;
13344         }
13345     }
13346   if (!ISDIGIT (*input_line_pointer))
13347     {
13348       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13349               *input_line_pointer, *input_line_pointer);
13350       as_warn (_("invalid number"));
13351       return -1;
13352     }
13353   while (ISDIGIT (*input_line_pointer))
13354     {
13355       val *= 10;
13356       val += *input_line_pointer++ - '0';
13357     }
13358   return negative ? -val : val;
13359 }
13360
13361 /* The .file directive; just like the usual .file directive, but there
13362    is an initial number which is the ECOFF file index.  In the non-ECOFF
13363    case .file implies DWARF-2.  */
13364
13365 static void
13366 s_mips_file (x)
13367      int x ATTRIBUTE_UNUSED;
13368 {
13369   if (ECOFF_DEBUGGING)
13370     {
13371       get_number ();
13372       s_app_file (0);
13373     }
13374   else
13375     dwarf2_directive_file (0);
13376 }
13377
13378 /* The .loc directive, implying DWARF-2.  */
13379
13380 static void
13381 s_mips_loc (x)
13382      int x ATTRIBUTE_UNUSED;
13383 {
13384   if (!ECOFF_DEBUGGING)
13385     dwarf2_directive_loc (0);
13386 }
13387
13388 /* The .end directive.  */
13389
13390 static void
13391 s_mips_end (x)
13392      int x ATTRIBUTE_UNUSED;
13393 {
13394   symbolS *p;
13395   int maybe_text;
13396
13397   /* Following functions need their own .frame and .cprestore directives.  */
13398   mips_frame_reg_valid = 0;
13399   mips_cprestore_valid = 0;
13400
13401   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13402     {
13403       p = get_symbol ();
13404       demand_empty_rest_of_line ();
13405     }
13406   else
13407     p = NULL;
13408
13409 #ifdef BFD_ASSEMBLER
13410   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13411     maybe_text = 1;
13412   else
13413     maybe_text = 0;
13414 #else
13415   if (now_seg != data_section && now_seg != bss_section)
13416     maybe_text = 1;
13417   else
13418     maybe_text = 0;
13419 #endif
13420
13421   if (!maybe_text)
13422     as_warn (_(".end not in text section"));
13423
13424   if (!cur_proc_ptr)
13425     {
13426       as_warn (_(".end directive without a preceding .ent directive."));
13427       demand_empty_rest_of_line ();
13428       return;
13429     }
13430
13431   if (p != NULL)
13432     {
13433       assert (S_GET_NAME (p));
13434       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13435         as_warn (_(".end symbol does not match .ent symbol."));
13436     }
13437   else
13438     as_warn (_(".end directive missing or unknown symbol"));
13439
13440 #ifdef MIPS_STABS_ELF
13441   {
13442     segT saved_seg = now_seg;
13443     subsegT saved_subseg = now_subseg;
13444     valueT dot;
13445     expressionS exp;
13446     char *fragp;
13447
13448     dot = frag_now_fix ();
13449
13450 #ifdef md_flush_pending_output
13451     md_flush_pending_output ();
13452 #endif
13453
13454     assert (pdr_seg);
13455     subseg_set (pdr_seg, 0);
13456
13457     /* Write the symbol.  */
13458     exp.X_op = O_symbol;
13459     exp.X_add_symbol = p;
13460     exp.X_add_number = 0;
13461     emit_expr (&exp, 4);
13462
13463     fragp = frag_more (7 * 4);
13464
13465     md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13466     md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13467     md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13468     md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13469     md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13470     md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13471     md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13472
13473     subseg_set (saved_seg, saved_subseg);
13474   }
13475 #endif /* MIPS_STABS_ELF */
13476
13477   cur_proc_ptr = NULL;
13478 }
13479
13480 /* The .aent and .ent directives.  */
13481
13482 static void
13483 s_mips_ent (aent)
13484      int aent;
13485 {
13486   symbolS *symbolP;
13487   int maybe_text;
13488
13489   symbolP = get_symbol ();
13490   if (*input_line_pointer == ',')
13491     ++input_line_pointer;
13492   SKIP_WHITESPACE ();
13493   if (ISDIGIT (*input_line_pointer)
13494       || *input_line_pointer == '-')
13495     get_number ();
13496
13497 #ifdef BFD_ASSEMBLER
13498   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13499     maybe_text = 1;
13500   else
13501     maybe_text = 0;
13502 #else
13503   if (now_seg != data_section && now_seg != bss_section)
13504     maybe_text = 1;
13505   else
13506     maybe_text = 0;
13507 #endif
13508
13509   if (!maybe_text)
13510     as_warn (_(".ent or .aent not in text section."));
13511
13512   if (!aent && cur_proc_ptr)
13513     as_warn (_("missing .end"));
13514
13515   if (!aent)
13516     {
13517       /* This function needs its own .frame and .cprestore directives.  */
13518       mips_frame_reg_valid = 0;
13519       mips_cprestore_valid = 0;
13520
13521       cur_proc_ptr = &cur_proc;
13522       memset (cur_proc_ptr, '\0', sizeof (procS));
13523
13524       cur_proc_ptr->isym = symbolP;
13525
13526       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13527
13528       ++numprocs;
13529     }
13530
13531   demand_empty_rest_of_line ();
13532 }
13533
13534 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13535    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13536    s_mips_frame is used so that we can set the PDR information correctly.
13537    We can't use the ecoff routines because they make reference to the ecoff
13538    symbol table (in the mdebug section).  */
13539
13540 static void
13541 s_mips_frame (ignore)
13542      int ignore ATTRIBUTE_UNUSED;
13543 {
13544 #ifdef MIPS_STABS_ELF
13545
13546   long val;
13547
13548   if (cur_proc_ptr == (procS *) NULL)
13549     {
13550       as_warn (_(".frame outside of .ent"));
13551       demand_empty_rest_of_line ();
13552       return;
13553     }
13554
13555   cur_proc_ptr->frame_reg = tc_get_register (1);
13556
13557   SKIP_WHITESPACE ();
13558   if (*input_line_pointer++ != ','
13559       || get_absolute_expression_and_terminator (&val) != ',')
13560     {
13561       as_warn (_("Bad .frame directive"));
13562       --input_line_pointer;
13563       demand_empty_rest_of_line ();
13564       return;
13565     }
13566
13567   cur_proc_ptr->frame_offset = val;
13568   cur_proc_ptr->pc_reg = tc_get_register (0);
13569
13570   demand_empty_rest_of_line ();
13571 #else
13572   s_ignore (ignore);
13573 #endif /* MIPS_STABS_ELF */
13574 }
13575
13576 /* The .fmask and .mask directives. If the mdebug section is present
13577    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13578    embedded targets, s_mips_mask is used so that we can set the PDR
13579    information correctly. We can't use the ecoff routines because they
13580    make reference to the ecoff symbol table (in the mdebug section).  */
13581
13582 static void
13583 s_mips_mask (reg_type)
13584      char reg_type;
13585 {
13586 #ifdef MIPS_STABS_ELF
13587   long mask, off;
13588
13589   if (cur_proc_ptr == (procS *) NULL)
13590     {
13591       as_warn (_(".mask/.fmask outside of .ent"));
13592       demand_empty_rest_of_line ();
13593       return;
13594     }
13595
13596   if (get_absolute_expression_and_terminator (&mask) != ',')
13597     {
13598       as_warn (_("Bad .mask/.fmask directive"));
13599       --input_line_pointer;
13600       demand_empty_rest_of_line ();
13601       return;
13602     }
13603
13604   off = get_absolute_expression ();
13605
13606   if (reg_type == 'F')
13607     {
13608       cur_proc_ptr->fpreg_mask = mask;
13609       cur_proc_ptr->fpreg_offset = off;
13610     }
13611   else
13612     {
13613       cur_proc_ptr->reg_mask = mask;
13614       cur_proc_ptr->reg_offset = off;
13615     }
13616
13617   demand_empty_rest_of_line ();
13618 #else
13619   s_ignore (reg_type);
13620 #endif /* MIPS_STABS_ELF */
13621 }
13622
13623 /* The .loc directive.  */
13624
13625 #if 0
13626 static void
13627 s_loc (x)
13628      int x;
13629 {
13630   symbolS *symbolP;
13631   int lineno;
13632   int addroff;
13633
13634   assert (now_seg == text_section);
13635
13636   lineno = get_number ();
13637   addroff = frag_now_fix ();
13638
13639   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13640   S_SET_TYPE (symbolP, N_SLINE);
13641   S_SET_OTHER (symbolP, 0);
13642   S_SET_DESC (symbolP, lineno);
13643   symbolP->sy_segment = now_seg;
13644 }
13645 #endif
13646
13647 /* CPU name/ISA/number mapping table.
13648
13649    Entries are grouped by type.  The first matching CPU or ISA entry
13650    gets chosen by CPU or ISA, so it should be the 'canonical' name
13651    for that type.  Entries after that within the type are sorted
13652    alphabetically.
13653
13654    Case is ignored in comparison, so put the canonical entry in the
13655    appropriate case but everything else in lower case to ease eye pain.  */
13656 static const struct mips_cpu_info mips_cpu_info_table[] =
13657 {
13658   /* MIPS1 ISA */
13659   { "MIPS1",          1,      ISA_MIPS1,      CPU_R3000, },
13660   { "mips",           1,      ISA_MIPS1,      CPU_R3000, },
13661
13662   /* MIPS2 ISA */
13663   { "MIPS2",          1,      ISA_MIPS2,      CPU_R6000, },
13664
13665   /* MIPS3 ISA */
13666   { "MIPS3",          1,      ISA_MIPS3,      CPU_R4000, },
13667
13668   /* MIPS4 ISA */
13669   { "MIPS4",          1,      ISA_MIPS4,      CPU_R8000, },
13670
13671   /* MIPS5 ISA */
13672   { "MIPS5",          1,      ISA_MIPS5,      CPU_MIPS5, },
13673   { "Generic-MIPS5",  0,      ISA_MIPS5,      CPU_MIPS5, },
13674
13675   /* MIPS32 ISA */
13676   { "MIPS32",         1,      ISA_MIPS32,     CPU_MIPS32, },
13677   { "mipsisa32",      0,      ISA_MIPS32,     CPU_MIPS32, },
13678   { "Generic-MIPS32", 0,      ISA_MIPS32,     CPU_MIPS32, },
13679   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13680   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32, },
13681   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32, },
13682
13683   /* For historical reasons.  */
13684   { "MIPS64",         1,      ISA_MIPS3,      CPU_R4000, },
13685
13686   /* MIPS64 ISA */
13687   { "mipsisa64",      1,      ISA_MIPS64,     CPU_MIPS64, },
13688   { "Generic-MIPS64", 0,      ISA_MIPS64,     CPU_MIPS64, },
13689   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64, },
13690   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64, },
13691
13692   /* R2000 CPU */
13693   { "R2000",          0,      ISA_MIPS1,      CPU_R2000, },
13694   { "2000",           0,      ISA_MIPS1,      CPU_R2000, },
13695   { "2k",             0,      ISA_MIPS1,      CPU_R2000, },
13696   { "r2k",            0,      ISA_MIPS1,      CPU_R2000, },
13697
13698   /* R3000 CPU */
13699   { "R3000",          0,      ISA_MIPS1,      CPU_R3000, },
13700   { "3000",           0,      ISA_MIPS1,      CPU_R3000, },
13701   { "3k",             0,      ISA_MIPS1,      CPU_R3000, },
13702   { "r3k",            0,      ISA_MIPS1,      CPU_R3000, },
13703
13704   /* TX3900 CPU */
13705   { "R3900",          0,      ISA_MIPS1,      CPU_R3900, },
13706   { "3900",           0,      ISA_MIPS1,      CPU_R3900, },
13707   { "mipstx39",       0,      ISA_MIPS1,      CPU_R3900, },
13708
13709   /* R4000 CPU */
13710   { "R4000",          0,      ISA_MIPS3,      CPU_R4000, },
13711   { "4000",           0,      ISA_MIPS3,      CPU_R4000, },
13712   { "4k",             0,      ISA_MIPS3,      CPU_R4000, },   /* beware */
13713   { "r4k",            0,      ISA_MIPS3,      CPU_R4000, },
13714
13715   /* R4010 CPU */
13716   { "R4010",          0,      ISA_MIPS2,      CPU_R4010, },
13717   { "4010",           0,      ISA_MIPS2,      CPU_R4010, },
13718
13719   /* R4400 CPU */
13720   { "R4400",          0,      ISA_MIPS3,      CPU_R4400, },
13721   { "4400",           0,      ISA_MIPS3,      CPU_R4400, },
13722
13723   /* R4600 CPU */
13724   { "R4600",          0,      ISA_MIPS3,      CPU_R4600, },
13725   { "4600",           0,      ISA_MIPS3,      CPU_R4600, },
13726   { "mips64orion",    0,      ISA_MIPS3,      CPU_R4600, },
13727   { "orion",          0,      ISA_MIPS3,      CPU_R4600, },
13728
13729   /* R4650 CPU */
13730   { "R4650",          0,      ISA_MIPS3,      CPU_R4650, },
13731   { "4650",           0,      ISA_MIPS3,      CPU_R4650, },
13732
13733   /* R6000 CPU */
13734   { "R6000",          0,      ISA_MIPS2,      CPU_R6000, },
13735   { "6000",           0,      ISA_MIPS2,      CPU_R6000, },
13736   { "6k",             0,      ISA_MIPS2,      CPU_R6000, },
13737   { "r6k",            0,      ISA_MIPS2,      CPU_R6000, },
13738
13739   /* R8000 CPU */
13740   { "R8000",          0,      ISA_MIPS4,      CPU_R8000, },
13741   { "8000",           0,      ISA_MIPS4,      CPU_R8000, },
13742   { "8k",             0,      ISA_MIPS4,      CPU_R8000, },
13743   { "r8k",            0,      ISA_MIPS4,      CPU_R8000, },
13744
13745   /* R10000 CPU */
13746   { "R10000",         0,      ISA_MIPS4,      CPU_R10000, },
13747   { "10000",          0,      ISA_MIPS4,      CPU_R10000, },
13748   { "10k",            0,      ISA_MIPS4,      CPU_R10000, },
13749   { "r10k",           0,      ISA_MIPS4,      CPU_R10000, },
13750
13751   /* R12000 CPU */
13752   { "R12000",         0,      ISA_MIPS4,      CPU_R12000, },
13753   { "12000",          0,      ISA_MIPS4,      CPU_R12000, },
13754   { "12k",            0,      ISA_MIPS4,      CPU_R12000, },
13755   { "r12k",           0,      ISA_MIPS4,      CPU_R12000, },
13756
13757   /* VR4100 CPU */
13758   { "VR4100",         0,      ISA_MIPS3,      CPU_VR4100, },
13759   { "4100",           0,      ISA_MIPS3,      CPU_VR4100, },
13760   { "mips64vr4100",   0,      ISA_MIPS3,      CPU_VR4100, },
13761   { "r4100",          0,      ISA_MIPS3,      CPU_VR4100, },
13762
13763   /* VR4111 CPU */
13764   { "VR4111",         0,      ISA_MIPS3,      CPU_R4111, },
13765   { "4111",           0,      ISA_MIPS3,      CPU_R4111, },
13766   { "mips64vr4111",   0,      ISA_MIPS3,      CPU_R4111, },
13767   { "r4111",          0,      ISA_MIPS3,      CPU_R4111, },
13768
13769   /* VR4300 CPU */
13770   { "VR4300",         0,      ISA_MIPS3,      CPU_R4300, },
13771   { "4300",           0,      ISA_MIPS3,      CPU_R4300, },
13772   { "mips64vr4300",   0,      ISA_MIPS3,      CPU_R4300, },
13773   { "r4300",          0,      ISA_MIPS3,      CPU_R4300, },
13774
13775   /* VR5000 CPU */
13776   { "VR5000",         0,      ISA_MIPS4,      CPU_R5000, },
13777   { "5000",           0,      ISA_MIPS4,      CPU_R5000, },
13778   { "5k",             0,      ISA_MIPS4,      CPU_R5000, },
13779   { "mips64vr5000",   0,      ISA_MIPS4,      CPU_R5000, },
13780   { "r5000",          0,      ISA_MIPS4,      CPU_R5000, },
13781   { "r5200",          0,      ISA_MIPS4,      CPU_R5000, },
13782   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000, },
13783   { "r5230",          0,      ISA_MIPS4,      CPU_R5000, },
13784   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000, },
13785   { "r5231",          0,      ISA_MIPS4,      CPU_R5000, },
13786   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000, },
13787   { "r5261",          0,      ISA_MIPS4,      CPU_R5000, },
13788   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000, },
13789   { "r5721",          0,      ISA_MIPS4,      CPU_R5000, },
13790   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000, },
13791   { "r5k",            0,      ISA_MIPS4,      CPU_R5000, },
13792   { "r7000",          0,      ISA_MIPS4,      CPU_R5000, },
13793
13794   /* Broadcom SB-1 CPU */
13795   { "SB-1",           0,      ISA_MIPS64,     CPU_SB1, },
13796   { "sb-1250",        0,      ISA_MIPS64,     CPU_SB1, },
13797   { "sb1",            0,      ISA_MIPS64,     CPU_SB1, },
13798   { "sb1250",         0,      ISA_MIPS64,     CPU_SB1, },
13799
13800   /* End marker.  */
13801   { NULL, 0, 0, 0, },
13802 };
13803
13804 static const struct mips_cpu_info *
13805 mips_cpu_info_from_name (name)
13806      const char *name;
13807 {
13808   int i;
13809
13810   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13811     if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13812       return (&mips_cpu_info_table[i]);
13813
13814   return NULL;
13815 }
13816
13817 static const struct mips_cpu_info *
13818 mips_cpu_info_from_isa (isa)
13819      int isa;
13820 {
13821   int i;
13822
13823   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13824     if (mips_cpu_info_table[i].is_isa
13825       && isa == mips_cpu_info_table[i].isa)
13826       return (&mips_cpu_info_table[i]);
13827
13828   return NULL;
13829 }
13830
13831 static const struct mips_cpu_info *
13832 mips_cpu_info_from_cpu (cpu)
13833      int cpu;
13834 {
13835   int i;
13836
13837   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13838     if (!mips_cpu_info_table[i].is_isa
13839       && cpu == mips_cpu_info_table[i].cpu)
13840       return (&mips_cpu_info_table[i]);
13841
13842   return NULL;
13843 }