* config/tc-mips.c (append_insn): Fix jump overflow check.
[platform/upstream/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too.  */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT  1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP  28
95 #define SP  29
96 #define FP  30
97 #define RA  31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format.  */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section.  */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111                             ? ".data" \
112                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113                             ? ".rdata" \
114                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115                             ? ".rdata" \
116                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117                             ? ".rodata" \
118                             : (abort (), ""))
119
120 /* The ABI to use.  */
121 enum mips_abi_level
122 {
123   NO_ABI = 0,
124   O32_ABI,
125   O64_ABI,
126   N32_ABI,
127   N64_ABI,
128   EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file.  */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* This is the set of options which may be modified by the .set
135    pseudo-op.  We use a struct so that .set push and .set pop are more
136    reliable.  */
137
138 struct mips_set_options
139 {
140   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
141      if it has not been initialized.  Changed by `.set mipsN', and the
142      -mipsN command line option, and the default CPU.  */
143   int isa;
144   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
145      if they have not been initialized.  Changed by `.set <asename>', by
146      command line options, and based on the default architecture.  */
147   int ase_mips3d;
148   int ase_mdmx;
149   /* Whether we are assembling for the mips16 processor.  0 if we are
150      not, 1 if we are, and -1 if the value has not been initialized.
151      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152      -nomips16 command line options, and the default CPU.  */
153   int mips16;
154   /* Non-zero if we should not reorder instructions.  Changed by `.set
155      reorder' and `.set noreorder'.  */
156   int noreorder;
157   /* Non-zero if we should not permit the $at ($1) register to be used
158      in instructions.  Changed by `.set at' and `.set noat'.  */
159   int noat;
160   /* Non-zero if we should warn when a macro instruction expands into
161      more than one machine instruction.  Changed by `.set nomacro' and
162      `.set macro'.  */
163   int warn_about_macros;
164   /* Non-zero if we should not move instructions.  Changed by `.set
165      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
166   int nomove;
167   /* Non-zero if we should not optimize branches by moving the target
168      of the branch into the delay slot.  Actually, we don't perform
169      this optimization anyhow.  Changed by `.set bopt' and `.set
170      nobopt'.  */
171   int nobopt;
172   /* Non-zero if we should not autoextend mips16 instructions.
173      Changed by `.set autoextend' and `.set noautoextend'.  */
174   int noautoextend;
175   /* Restrict general purpose registers and floating point registers
176      to 32 bit.  This is initially determined when -mgp32 or -mfp32
177      is passed but can changed if the assembler code uses .set mipsN.  */
178   int gp32;
179   int fp32;
180 };
181
182 /* True if -mgp32 was passed.  */
183 static int file_mips_gp32 = -1;
184
185 /* True if -mfp32 was passed.  */
186 static int file_mips_fp32 = -1;
187
188 /* This is the struct we use to hold the current set of options.  Note
189    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190    -1 to indicate that they have not been initialized.  */
191
192 static struct mips_set_options mips_opts =
193 {
194   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
195 };
196
197 /* These variables are filled in with the masks of registers used.
198    The object format code reads them and puts them in the appropriate
199    place.  */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
202
203 /* MIPS ISA we are using for this output file.  */
204 static int file_mips_isa = ISA_UNKNOWN;
205
206 /* True if -mips16 was passed or implied by arguments passed on the
207    command line (e.g., by -march).  */
208 static int file_ase_mips16;
209
210 /* True if -mips3d was passed or implied by arguments passed on the
211    command line (e.g., by -march).  */
212 static int file_ase_mips3d;
213
214 /* True if -mdmx was passed or implied by arguments passed on the
215    command line (e.g., by -march).  */
216 static int file_ase_mdmx;
217
218 /* The argument of the -march= flag.  The architecture we are assembling.  */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
222
223 /* The argument of the -mtune= flag.  The architecture for which we
224    are optimizing.  */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
228
229 /* True when generating 32-bit code for a 64-bit processor.  */
230 static int mips_32bitmode = 0;
231
232 /* Some ISA's have delay slots for instructions which read or write
233    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236    delay slot in this ISA.  The uses of this macro assume that any
237    ISA that has delay slots for one of these, has them for all.  They
238    also assume that ISAs which don't have delays for these insns, don't
239    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
240 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
241    (ISA) == ISA_MIPS1                       \
242    || (ISA) == ISA_MIPS2                    \
243    || (ISA) == ISA_MIPS3                    \
244    )
245
246 /* True if the given ABI requires 32-bit registers.  */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249 /* Likewise 64-bit registers.  */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251   ((ABI) == N32_ABI               \
252    || (ABI) == N64_ABI            \
253    || (ABI) == O64_ABI)
254
255 /*  Return true if ISA supports 64 bit gp register instructions.  */
256 #define ISA_HAS_64BIT_REGS(ISA) (    \
257    (ISA) == ISA_MIPS3                \
258    || (ISA) == ISA_MIPS4             \
259    || (ISA) == ISA_MIPS5             \
260    || (ISA) == ISA_MIPS64            \
261    )
262
263 #define HAVE_32BIT_GPRS                            \
264     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
265
266 #define HAVE_32BIT_FPRS                            \
267     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
268
269 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
271
272 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
273
274 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
275
276 /* We can only have 64bit addresses if the object file format
277    supports it.  */
278 #define HAVE_32BIT_ADDRESSES                           \
279    (HAVE_32BIT_GPRS                                    \
280     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
281          || ! HAVE_64BIT_OBJECTS)                      \
282         && mips_pic != EMBEDDED_PIC))
283
284 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
285
286 /* Return true if the given CPU supports the MIPS16 ASE.  */
287 #define CPU_HAS_MIPS16(cpu)                            \
288    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0)
289
290 /* Return true if the given CPU supports the MIPS3D ASE.  */
291 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
292                                  )
293
294 /* Return true if the given CPU supports the MDMX ASE.  */
295 #define CPU_HAS_MDMX(cpu)       (false                 \
296                                  )
297
298 /* Whether the processor uses hardware interlocks to protect
299    reads from the HI and LO registers, and thus does not
300    require nops to be inserted.  */
301
302 #define hilo_interlocks (mips_arch == CPU_R4010                       \
303                          || mips_arch == CPU_SB1                      \
304                          )
305
306 /* Whether the processor uses hardware interlocks to protect reads
307    from the GPRs, and thus does not require nops to be inserted.  */
308 #define gpr_interlocks \
309   (mips_opts.isa != ISA_MIPS1  \
310    || mips_arch == CPU_R3900)
311
312 /* As with other "interlocks" this is used by hardware that has FP
313    (co-processor) interlocks.  */
314 /* Itbl support may require additional care here.  */
315 #define cop_interlocks (mips_arch == CPU_R4300                        \
316                         || mips_arch == CPU_SB1                       \
317                         )
318
319 /* Is this a mfhi or mflo instruction?  */
320 #define MF_HILO_INSN(PINFO) \
321           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
322
323 /* MIPS PIC level.  */
324
325 enum mips_pic_level mips_pic;
326
327 /* Warn about all NOPS that the assembler generates.  */
328 static int warn_nops = 0;
329
330 /* 1 if we should generate 32 bit offsets from the $gp register in
331    SVR4_PIC mode.  Currently has no meaning in other modes.  */
332 static int mips_big_got = 0;
333
334 /* 1 if trap instructions should used for overflow rather than break
335    instructions.  */
336 static int mips_trap = 0;
337
338 /* 1 if double width floating point constants should not be constructed
339    by assembling two single width halves into two single width floating
340    point registers which just happen to alias the double width destination
341    register.  On some architectures this aliasing can be disabled by a bit
342    in the status register, and the setting of this bit cannot be determined
343    automatically at assemble time.  */
344 static int mips_disable_float_construction;
345
346 /* Non-zero if any .set noreorder directives were used.  */
347
348 static int mips_any_noreorder;
349
350 /* Non-zero if nops should be inserted when the register referenced in
351    an mfhi/mflo instruction is read in the next two instructions.  */
352 static int mips_7000_hilo_fix;
353
354 /* The size of the small data section.  */
355 static unsigned int g_switch_value = 8;
356 /* Whether the -G option was used.  */
357 static int g_switch_seen = 0;
358
359 #define N_RMASK 0xc4
360 #define N_VFP   0xd4
361
362 /* If we can determine in advance that GP optimization won't be
363    possible, we can skip the relaxation stuff that tries to produce
364    GP-relative references.  This makes delay slot optimization work
365    better.
366
367    This function can only provide a guess, but it seems to work for
368    gcc output.  It needs to guess right for gcc, otherwise gcc
369    will put what it thinks is a GP-relative instruction in a branch
370    delay slot.
371
372    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
373    fixed it for the non-PIC mode.  KR 95/04/07  */
374 static int nopic_need_relax PARAMS ((symbolS *, int));
375
376 /* handle of the OPCODE hash table */
377 static struct hash_control *op_hash = NULL;
378
379 /* The opcode hash table we use for the mips16.  */
380 static struct hash_control *mips16_op_hash = NULL;
381
382 /* This array holds the chars that always start a comment.  If the
383     pre-processor is disabled, these aren't very useful */
384 const char comment_chars[] = "#";
385
386 /* This array holds the chars that only start a comment at the beginning of
387    a line.  If the line seems to have the form '# 123 filename'
388    .line and .file directives will appear in the pre-processed output */
389 /* Note that input_file.c hand checks for '#' at the beginning of the
390    first line of the input file.  This is because the compiler outputs
391    #NO_APP at the beginning of its output.  */
392 /* Also note that C style comments are always supported.  */
393 const char line_comment_chars[] = "#";
394
395 /* This array holds machine specific line separator characters.  */
396 const char line_separator_chars[] = ";";
397
398 /* Chars that can be used to separate mant from exp in floating point nums */
399 const char EXP_CHARS[] = "eE";
400
401 /* Chars that mean this number is a floating point constant */
402 /* As in 0f12.456 */
403 /* or    0d1.2345e12 */
404 const char FLT_CHARS[] = "rRsSfFdDxXpP";
405
406 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
407    changed in read.c .  Ideally it shouldn't have to know about it at all,
408    but nothing is ideal around here.
409  */
410
411 static char *insn_error;
412
413 static int auto_align = 1;
414
415 /* When outputting SVR4 PIC code, the assembler needs to know the
416    offset in the stack frame from which to restore the $gp register.
417    This is set by the .cprestore pseudo-op, and saved in this
418    variable.  */
419 static offsetT mips_cprestore_offset = -1;
420
421 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
422    more optimizations, it can use a register value instead of a memory-saved
423    offset and even an other register than $gp as global pointer.  */
424 static offsetT mips_cpreturn_offset = -1;
425 static int mips_cpreturn_register = -1;
426 static int mips_gp_register = GP;
427 static int mips_gprel_offset = 0;
428
429 /* Whether mips_cprestore_offset has been set in the current function
430    (or whether it has already been warned about, if not).  */
431 static int mips_cprestore_valid = 0;
432
433 /* This is the register which holds the stack frame, as set by the
434    .frame pseudo-op.  This is needed to implement .cprestore.  */
435 static int mips_frame_reg = SP;
436
437 /* Whether mips_frame_reg has been set in the current function
438    (or whether it has already been warned about, if not).  */
439 static int mips_frame_reg_valid = 0;
440
441 /* To output NOP instructions correctly, we need to keep information
442    about the previous two instructions.  */
443
444 /* Whether we are optimizing.  The default value of 2 means to remove
445    unneeded NOPs and swap branch instructions when possible.  A value
446    of 1 means to not swap branches.  A value of 0 means to always
447    insert NOPs.  */
448 static int mips_optimize = 2;
449
450 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
451    equivalent to seeing no -g option at all.  */
452 static int mips_debug = 0;
453
454 /* The previous instruction.  */
455 static struct mips_cl_insn prev_insn;
456
457 /* The instruction before prev_insn.  */
458 static struct mips_cl_insn prev_prev_insn;
459
460 /* If we don't want information for prev_insn or prev_prev_insn, we
461    point the insn_mo field at this dummy integer.  */
462 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
463
464 /* Non-zero if prev_insn is valid.  */
465 static int prev_insn_valid;
466
467 /* The frag for the previous instruction.  */
468 static struct frag *prev_insn_frag;
469
470 /* The offset into prev_insn_frag for the previous instruction.  */
471 static long prev_insn_where;
472
473 /* The reloc type for the previous instruction, if any.  */
474 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
475
476 /* The reloc for the previous instruction, if any.  */
477 static fixS *prev_insn_fixp[3];
478
479 /* Non-zero if the previous instruction was in a delay slot.  */
480 static int prev_insn_is_delay_slot;
481
482 /* Non-zero if the previous instruction was in a .set noreorder.  */
483 static int prev_insn_unreordered;
484
485 /* Non-zero if the previous instruction uses an extend opcode (if
486    mips16).  */
487 static int prev_insn_extended;
488
489 /* Non-zero if the previous previous instruction was in a .set
490    noreorder.  */
491 static int prev_prev_insn_unreordered;
492
493 /* If this is set, it points to a frag holding nop instructions which
494    were inserted before the start of a noreorder section.  If those
495    nops turn out to be unnecessary, the size of the frag can be
496    decreased.  */
497 static fragS *prev_nop_frag;
498
499 /* The number of nop instructions we created in prev_nop_frag.  */
500 static int prev_nop_frag_holds;
501
502 /* The number of nop instructions that we know we need in
503    prev_nop_frag.  */
504 static int prev_nop_frag_required;
505
506 /* The number of instructions we've seen since prev_nop_frag.  */
507 static int prev_nop_frag_since;
508
509 /* For ECOFF and ELF, relocations against symbols are done in two
510    parts, with a HI relocation and a LO relocation.  Each relocation
511    has only 16 bits of space to store an addend.  This means that in
512    order for the linker to handle carries correctly, it must be able
513    to locate both the HI and the LO relocation.  This means that the
514    relocations must appear in order in the relocation table.
515
516    In order to implement this, we keep track of each unmatched HI
517    relocation.  We then sort them so that they immediately precede the
518    corresponding LO relocation.  */
519
520 struct mips_hi_fixup
521 {
522   /* Next HI fixup.  */
523   struct mips_hi_fixup *next;
524   /* This fixup.  */
525   fixS *fixp;
526   /* The section this fixup is in.  */
527   segT seg;
528 };
529
530 /* The list of unmatched HI relocs.  */
531
532 static struct mips_hi_fixup *mips_hi_fixup_list;
533
534 /* Map normal MIPS register numbers to mips16 register numbers.  */
535
536 #define X ILLEGAL_REG
537 static const int mips32_to_16_reg_map[] =
538 {
539   X, X, 2, 3, 4, 5, 6, 7,
540   X, X, X, X, X, X, X, X,
541   0, 1, X, X, X, X, X, X,
542   X, X, X, X, X, X, X, X
543 };
544 #undef X
545
546 /* Map mips16 register numbers to normal MIPS register numbers.  */
547
548 static const unsigned int mips16_to_32_reg_map[] =
549 {
550   16, 17, 2, 3, 4, 5, 6, 7
551 };
552 \f
553 /* Since the MIPS does not have multiple forms of PC relative
554    instructions, we do not have to do relaxing as is done on other
555    platforms.  However, we do have to handle GP relative addressing
556    correctly, which turns out to be a similar problem.
557
558    Every macro that refers to a symbol can occur in (at least) two
559    forms, one with GP relative addressing and one without.  For
560    example, loading a global variable into a register generally uses
561    a macro instruction like this:
562      lw $4,i
563    If i can be addressed off the GP register (this is true if it is in
564    the .sbss or .sdata section, or if it is known to be smaller than
565    the -G argument) this will generate the following instruction:
566      lw $4,i($gp)
567    This instruction will use a GPREL reloc.  If i can not be addressed
568    off the GP register, the following instruction sequence will be used:
569      lui $at,i
570      lw $4,i($at)
571    In this case the first instruction will have a HI16 reloc, and the
572    second reloc will have a LO16 reloc.  Both relocs will be against
573    the symbol i.
574
575    The issue here is that we may not know whether i is GP addressable
576    until after we see the instruction that uses it.  Therefore, we
577    want to be able to choose the final instruction sequence only at
578    the end of the assembly.  This is similar to the way other
579    platforms choose the size of a PC relative instruction only at the
580    end of assembly.
581
582    When generating position independent code we do not use GP
583    addressing in quite the same way, but the issue still arises as
584    external symbols and local symbols must be handled differently.
585
586    We handle these issues by actually generating both possible
587    instruction sequences.  The longer one is put in a frag_var with
588    type rs_machine_dependent.  We encode what to do with the frag in
589    the subtype field.  We encode (1) the number of existing bytes to
590    replace, (2) the number of new bytes to use, (3) the offset from
591    the start of the existing bytes to the first reloc we must generate
592    (that is, the offset is applied from the start of the existing
593    bytes after they are replaced by the new bytes, if any), (4) the
594    offset from the start of the existing bytes to the second reloc,
595    (5) whether a third reloc is needed (the third reloc is always four
596    bytes after the second reloc), and (6) whether to warn if this
597    variant is used (this is sometimes needed if .set nomacro or .set
598    noat is in effect).  All these numbers are reasonably small.
599
600    Generating two instruction sequences must be handled carefully to
601    ensure that delay slots are handled correctly.  Fortunately, there
602    are a limited number of cases.  When the second instruction
603    sequence is generated, append_insn is directed to maintain the
604    existing delay slot information, so it continues to apply to any
605    code after the second instruction sequence.  This means that the
606    second instruction sequence must not impose any requirements not
607    required by the first instruction sequence.
608
609    These variant frags are then handled in functions called by the
610    machine independent code.  md_estimate_size_before_relax returns
611    the final size of the frag.  md_convert_frag sets up the final form
612    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
613    one if needed.  */
614 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
615   ((relax_substateT) \
616    (((old) << 23) \
617     | ((new) << 16) \
618     | (((reloc1) + 64) << 9) \
619     | (((reloc2) + 64) << 2) \
620     | ((reloc3) ? (1 << 1) : 0) \
621     | ((warn) ? 1 : 0)))
622 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
623 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
624 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
625 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
626 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
627 #define RELAX_WARN(i) ((i) & 1)
628
629 /* For mips16 code, we use an entirely different form of relaxation.
630    mips16 supports two versions of most instructions which take
631    immediate values: a small one which takes some small value, and a
632    larger one which takes a 16 bit value.  Since branches also follow
633    this pattern, relaxing these values is required.
634
635    We can assemble both mips16 and normal MIPS code in a single
636    object.  Therefore, we need to support this type of relaxation at
637    the same time that we support the relaxation described above.  We
638    use the high bit of the subtype field to distinguish these cases.
639
640    The information we store for this type of relaxation is the
641    argument code found in the opcode file for this relocation, whether
642    the user explicitly requested a small or extended form, and whether
643    the relocation is in a jump or jal delay slot.  That tells us the
644    size of the value, and how it should be stored.  We also store
645    whether the fragment is considered to be extended or not.  We also
646    store whether this is known to be a branch to a different section,
647    whether we have tried to relax this frag yet, and whether we have
648    ever extended a PC relative fragment because of a shift count.  */
649 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
650   (0x80000000                                                   \
651    | ((type) & 0xff)                                            \
652    | ((small) ? 0x100 : 0)                                      \
653    | ((ext) ? 0x200 : 0)                                        \
654    | ((dslot) ? 0x400 : 0)                                      \
655    | ((jal_dslot) ? 0x800 : 0))
656 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
657 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
658 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
659 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
660 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
661 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
662 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
663 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
664 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
665 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
666 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
667 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
668
669 /* Is the given value a sign-extended 32-bit value?  */
670 #define IS_SEXT_32BIT_NUM(x)                                            \
671   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
672    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
673
674 /* Is the given value a sign-extended 16-bit value?  */
675 #define IS_SEXT_16BIT_NUM(x)                                            \
676   (((x) &~ (offsetT) 0x7fff) == 0                                       \
677    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
678
679 \f
680 /* Prototypes for static functions.  */
681
682 #ifdef __STDC__
683 #define internalError() \
684     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
685 #else
686 #define internalError() as_fatal (_("MIPS internal Error"));
687 #endif
688
689 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
690
691 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
692                                   unsigned int reg, enum mips_regclass class));
693 static int reg_needs_delay PARAMS ((unsigned int));
694 static void mips16_mark_labels PARAMS ((void));
695 static void append_insn PARAMS ((char *place,
696                                  struct mips_cl_insn * ip,
697                                  expressionS * p,
698                                  bfd_reloc_code_real_type *r,
699                                  boolean));
700 static void mips_no_prev_insn PARAMS ((int));
701 static void mips_emit_delays PARAMS ((boolean));
702 #ifdef USE_STDARG
703 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
704                                  const char *name, const char *fmt,
705                                  ...));
706 #else
707 static void macro_build ();
708 #endif
709 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
710                                         const char *, const char *,
711                                         va_list));
712 static void macro_build_jalr PARAMS ((int, expressionS *));
713 static void macro_build_lui PARAMS ((char *place, int *counter,
714                                      expressionS * ep, int regnum));
715 static void macro_build_ldst_constoffset PARAMS ((char *place, int *counter,
716                                                   expressionS * ep, const char *op,
717                                                   int valreg, int breg));
718 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
719 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
720                                          expressionS *));
721 static void load_register PARAMS ((int *, int, expressionS *, int));
722 static void load_address PARAMS ((int *, int, expressionS *, int *));
723 static void move_register PARAMS ((int *, int, int));
724 static void macro PARAMS ((struct mips_cl_insn * ip));
725 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
726 #ifdef LOSING_COMPILER
727 static void macro2 PARAMS ((struct mips_cl_insn * ip));
728 #endif
729 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
730 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
731 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
732                                   boolean, boolean, unsigned long *,
733                                   boolean *, unsigned short *));
734 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
735 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
736 static int my_getSmallExpression PARAMS ((expressionS *, char *));
737 static void my_getExpression PARAMS ((expressionS *, char *));
738 #ifdef OBJ_ELF
739 static int support_64bit_objects PARAMS((void));
740 #endif
741 static void mips_set_option_string PARAMS ((const char **, const char *));
742 static symbolS *get_symbol PARAMS ((void));
743 static void mips_align PARAMS ((int to, int fill, symbolS *label));
744 static void s_align PARAMS ((int));
745 static void s_change_sec PARAMS ((int));
746 static void s_change_section PARAMS ((int));
747 static void s_cons PARAMS ((int));
748 static void s_float_cons PARAMS ((int));
749 static void s_mips_globl PARAMS ((int));
750 static void s_option PARAMS ((int));
751 static void s_mipsset PARAMS ((int));
752 static void s_abicalls PARAMS ((int));
753 static void s_cpload PARAMS ((int));
754 static void s_cpsetup PARAMS ((int));
755 static void s_cplocal PARAMS ((int));
756 static void s_cprestore PARAMS ((int));
757 static void s_cpreturn PARAMS ((int));
758 static void s_gpvalue PARAMS ((int));
759 static void s_gpword PARAMS ((int));
760 static void s_cpadd PARAMS ((int));
761 static void s_insn PARAMS ((int));
762 static void md_obj_begin PARAMS ((void));
763 static void md_obj_end PARAMS ((void));
764 static long get_number PARAMS ((void));
765 static void s_mips_ent PARAMS ((int));
766 static void s_mips_end PARAMS ((int));
767 static void s_mips_frame PARAMS ((int));
768 static void s_mips_mask PARAMS ((int));
769 static void s_mips_stab PARAMS ((int));
770 static void s_mips_weakext PARAMS ((int));
771 static void s_mips_file PARAMS ((int));
772 static void s_mips_loc PARAMS ((int));
773 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
774 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
775 static void show PARAMS ((FILE *, const char *, int *, int *));
776 #ifdef OBJ_ELF
777 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
778 #endif
779
780 /* Return values of my_getSmallExpression().  */
781
782 enum small_ex_type
783 {
784   S_EX_NONE = 0,
785   S_EX_REGISTER,
786
787   /* Direct relocation creation by %percent_op().  */
788   S_EX_HALF,
789   S_EX_HI,
790   S_EX_LO,
791   S_EX_GP_REL,
792   S_EX_GOT,
793   S_EX_CALL16,
794   S_EX_GOT_DISP,
795   S_EX_GOT_PAGE,
796   S_EX_GOT_OFST,
797   S_EX_GOT_HI,
798   S_EX_GOT_LO,
799   S_EX_NEG,
800   S_EX_HIGHER,
801   S_EX_HIGHEST,
802   S_EX_CALL_HI,
803   S_EX_CALL_LO
804 };
805
806 /* Table and functions used to map between CPU/ISA names, and
807    ISA levels, and CPU numbers.  */
808
809 struct mips_cpu_info
810 {
811   const char *name;           /* CPU or ISA name.  */
812   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
813   int isa;                    /* ISA level.  */
814   int cpu;                    /* CPU number (default CPU if ISA).  */
815 };
816
817 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
818 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
819 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
820                                                         const char *));
821 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
822 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
823                                                            const char *));
824 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
825 \f
826 /* Pseudo-op table.
827
828    The following pseudo-ops from the Kane and Heinrich MIPS book
829    should be defined here, but are currently unsupported: .alias,
830    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
831
832    The following pseudo-ops from the Kane and Heinrich MIPS book are
833    specific to the type of debugging information being generated, and
834    should be defined by the object format: .aent, .begin, .bend,
835    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
836    .vreg.
837
838    The following pseudo-ops from the Kane and Heinrich MIPS book are
839    not MIPS CPU specific, but are also not specific to the object file
840    format.  This file is probably the best place to define them, but
841    they are not currently supported: .asm0, .endr, .lab, .repeat,
842    .struct.  */
843
844 static const pseudo_typeS mips_pseudo_table[] =
845 {
846   /* MIPS specific pseudo-ops.  */
847   {"option", s_option, 0},
848   {"set", s_mipsset, 0},
849   {"rdata", s_change_sec, 'r'},
850   {"sdata", s_change_sec, 's'},
851   {"livereg", s_ignore, 0},
852   {"abicalls", s_abicalls, 0},
853   {"cpload", s_cpload, 0},
854   {"cpsetup", s_cpsetup, 0},
855   {"cplocal", s_cplocal, 0},
856   {"cprestore", s_cprestore, 0},
857   {"cpreturn", s_cpreturn, 0},
858   {"gpvalue", s_gpvalue, 0},
859   {"gpword", s_gpword, 0},
860   {"cpadd", s_cpadd, 0},
861   {"insn", s_insn, 0},
862
863   /* Relatively generic pseudo-ops that happen to be used on MIPS
864      chips.  */
865   {"asciiz", stringer, 1},
866   {"bss", s_change_sec, 'b'},
867   {"err", s_err, 0},
868   {"half", s_cons, 1},
869   {"dword", s_cons, 3},
870   {"weakext", s_mips_weakext, 0},
871
872   /* These pseudo-ops are defined in read.c, but must be overridden
873      here for one reason or another.  */
874   {"align", s_align, 0},
875   {"byte", s_cons, 0},
876   {"data", s_change_sec, 'd'},
877   {"double", s_float_cons, 'd'},
878   {"float", s_float_cons, 'f'},
879   {"globl", s_mips_globl, 0},
880   {"global", s_mips_globl, 0},
881   {"hword", s_cons, 1},
882   {"int", s_cons, 2},
883   {"long", s_cons, 2},
884   {"octa", s_cons, 4},
885   {"quad", s_cons, 3},
886   {"section", s_change_section, 0},
887   {"short", s_cons, 1},
888   {"single", s_float_cons, 'f'},
889   {"stabn", s_mips_stab, 'n'},
890   {"text", s_change_sec, 't'},
891   {"word", s_cons, 2},
892
893   { "extern", ecoff_directive_extern, 0},
894
895   { NULL, NULL, 0 },
896 };
897
898 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
899 {
900   /* These pseudo-ops should be defined by the object file format.
901      However, a.out doesn't support them, so we have versions here.  */
902   {"aent", s_mips_ent, 1},
903   {"bgnb", s_ignore, 0},
904   {"end", s_mips_end, 0},
905   {"endb", s_ignore, 0},
906   {"ent", s_mips_ent, 0},
907   {"file", s_mips_file, 0},
908   {"fmask", s_mips_mask, 'F'},
909   {"frame", s_mips_frame, 0},
910   {"loc", s_mips_loc, 0},
911   {"mask", s_mips_mask, 'R'},
912   {"verstamp", s_ignore, 0},
913   { NULL, NULL, 0 },
914 };
915
916 extern void pop_insert PARAMS ((const pseudo_typeS *));
917
918 void
919 mips_pop_insert ()
920 {
921   pop_insert (mips_pseudo_table);
922   if (! ECOFF_DEBUGGING)
923     pop_insert (mips_nonecoff_pseudo_table);
924 }
925 \f
926 /* Symbols labelling the current insn.  */
927
928 struct insn_label_list
929 {
930   struct insn_label_list *next;
931   symbolS *label;
932 };
933
934 static struct insn_label_list *insn_labels;
935 static struct insn_label_list *free_insn_labels;
936
937 static void mips_clear_insn_labels PARAMS ((void));
938
939 static inline void
940 mips_clear_insn_labels ()
941 {
942   register struct insn_label_list **pl;
943
944   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
945     ;
946   *pl = insn_labels;
947   insn_labels = NULL;
948 }
949 \f
950 static char *expr_end;
951
952 /* Expressions which appear in instructions.  These are set by
953    mips_ip.  */
954
955 static expressionS imm_expr;
956 static expressionS offset_expr;
957
958 /* Relocs associated with imm_expr and offset_expr.  */
959
960 static bfd_reloc_code_real_type imm_reloc[3]
961   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
962 static bfd_reloc_code_real_type offset_reloc[3]
963   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
964
965 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
966
967 static boolean imm_unmatched_hi;
968
969 /* These are set by mips16_ip if an explicit extension is used.  */
970
971 static boolean mips16_small, mips16_ext;
972
973 #ifdef OBJ_ELF
974 /* The pdr segment for per procedure frame/regmask info.  Not used for
975    ECOFF debugging.  */
976
977 static segT pdr_seg;
978 #endif
979
980 /* The default target format to use.  */
981
982 const char *
983 mips_target_format ()
984 {
985   switch (OUTPUT_FLAVOR)
986     {
987     case bfd_target_aout_flavour:
988       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
989     case bfd_target_ecoff_flavour:
990       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
991     case bfd_target_coff_flavour:
992       return "pe-mips";
993     case bfd_target_elf_flavour:
994 #ifdef TE_TMIPS
995       /* This is traditional mips.  */
996       return (target_big_endian
997               ? (HAVE_64BIT_OBJECTS
998                  ? "elf64-tradbigmips"
999                  : (HAVE_NEWABI
1000                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1001               : (HAVE_64BIT_OBJECTS
1002                  ? "elf64-tradlittlemips"
1003                  : (HAVE_NEWABI
1004                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1005 #else
1006       return (target_big_endian
1007               ? (HAVE_64BIT_OBJECTS
1008                  ? "elf64-bigmips"
1009                  : (HAVE_NEWABI
1010                     ? "elf32-nbigmips" : "elf32-bigmips"))
1011               : (HAVE_64BIT_OBJECTS
1012                  ? "elf64-littlemips"
1013                  : (HAVE_NEWABI
1014                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1015 #endif
1016     default:
1017       abort ();
1018       return NULL;
1019     }
1020 }
1021
1022 /* This function is called once, at assembler startup time.  It should
1023    set up all the tables, etc. that the MD part of the assembler will need.  */
1024
1025 void
1026 md_begin ()
1027 {
1028   register const char *retval = NULL;
1029   int i = 0;
1030   int broken = 0;
1031
1032   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1033     as_warn (_("Could not set architecture and machine"));
1034
1035   op_hash = hash_new ();
1036
1037   for (i = 0; i < NUMOPCODES;)
1038     {
1039       const char *name = mips_opcodes[i].name;
1040
1041       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1042       if (retval != NULL)
1043         {
1044           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1045                    mips_opcodes[i].name, retval);
1046           /* Probably a memory allocation problem?  Give up now.  */
1047           as_fatal (_("Broken assembler.  No assembly attempted."));
1048         }
1049       do
1050         {
1051           if (mips_opcodes[i].pinfo != INSN_MACRO)
1052             {
1053               if (!validate_mips_insn (&mips_opcodes[i]))
1054                 broken = 1;
1055             }
1056           ++i;
1057         }
1058       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1059     }
1060
1061   mips16_op_hash = hash_new ();
1062
1063   i = 0;
1064   while (i < bfd_mips16_num_opcodes)
1065     {
1066       const char *name = mips16_opcodes[i].name;
1067
1068       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1069       if (retval != NULL)
1070         as_fatal (_("internal: can't hash `%s': %s"),
1071                   mips16_opcodes[i].name, retval);
1072       do
1073         {
1074           if (mips16_opcodes[i].pinfo != INSN_MACRO
1075               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1076                   != mips16_opcodes[i].match))
1077             {
1078               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1079                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1080               broken = 1;
1081             }
1082           ++i;
1083         }
1084       while (i < bfd_mips16_num_opcodes
1085              && strcmp (mips16_opcodes[i].name, name) == 0);
1086     }
1087
1088   if (broken)
1089     as_fatal (_("Broken assembler.  No assembly attempted."));
1090
1091   /* We add all the general register names to the symbol table.  This
1092      helps us detect invalid uses of them.  */
1093   for (i = 0; i < 32; i++)
1094     {
1095       char buf[5];
1096
1097       sprintf (buf, "$%d", i);
1098       symbol_table_insert (symbol_new (buf, reg_section, i,
1099                                        &zero_address_frag));
1100     }
1101   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1102                                    &zero_address_frag));
1103   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1104                                    &zero_address_frag));
1105   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1106                                    &zero_address_frag));
1107   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1108                                    &zero_address_frag));
1109   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1110                                    &zero_address_frag));
1111   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1112                                    &zero_address_frag));
1113   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1114                                    &zero_address_frag));
1115   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1116                                    &zero_address_frag));
1117   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1118                                    &zero_address_frag));
1119
1120   mips_no_prev_insn (false);
1121
1122   mips_gprmask = 0;
1123   mips_cprmask[0] = 0;
1124   mips_cprmask[1] = 0;
1125   mips_cprmask[2] = 0;
1126   mips_cprmask[3] = 0;
1127
1128   /* set the default alignment for the text section (2**2) */
1129   record_alignment (text_section, 2);
1130
1131   if (USE_GLOBAL_POINTER_OPT)
1132     bfd_set_gp_size (stdoutput, g_switch_value);
1133
1134   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1135     {
1136       /* On a native system, sections must be aligned to 16 byte
1137          boundaries.  When configured for an embedded ELF target, we
1138          don't bother.  */
1139       if (strcmp (TARGET_OS, "elf") != 0)
1140         {
1141           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1142           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1143           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1144         }
1145
1146       /* Create a .reginfo section for register masks and a .mdebug
1147          section for debugging information.  */
1148       {
1149         segT seg;
1150         subsegT subseg;
1151         flagword flags;
1152         segT sec;
1153
1154         seg = now_seg;
1155         subseg = now_subseg;
1156
1157         /* The ABI says this section should be loaded so that the
1158            running program can access it.  However, we don't load it
1159            if we are configured for an embedded target */
1160         flags = SEC_READONLY | SEC_DATA;
1161         if (strcmp (TARGET_OS, "elf") != 0)
1162           flags |= SEC_ALLOC | SEC_LOAD;
1163
1164         if (mips_abi != N64_ABI)
1165           {
1166             sec = subseg_new (".reginfo", (subsegT) 0);
1167
1168             bfd_set_section_flags (stdoutput, sec, flags);
1169             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1170
1171 #ifdef OBJ_ELF
1172             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1173 #endif
1174           }
1175         else
1176           {
1177             /* The 64-bit ABI uses a .MIPS.options section rather than
1178                .reginfo section.  */
1179             sec = subseg_new (".MIPS.options", (subsegT) 0);
1180             bfd_set_section_flags (stdoutput, sec, flags);
1181             bfd_set_section_alignment (stdoutput, sec, 3);
1182
1183 #ifdef OBJ_ELF
1184             /* Set up the option header.  */
1185             {
1186               Elf_Internal_Options opthdr;
1187               char *f;
1188
1189               opthdr.kind = ODK_REGINFO;
1190               opthdr.size = (sizeof (Elf_External_Options)
1191                              + sizeof (Elf64_External_RegInfo));
1192               opthdr.section = 0;
1193               opthdr.info = 0;
1194               f = frag_more (sizeof (Elf_External_Options));
1195               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1196                                              (Elf_External_Options *) f);
1197
1198               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1199             }
1200 #endif
1201           }
1202
1203         if (ECOFF_DEBUGGING)
1204           {
1205             sec = subseg_new (".mdebug", (subsegT) 0);
1206             (void) bfd_set_section_flags (stdoutput, sec,
1207                                           SEC_HAS_CONTENTS | SEC_READONLY);
1208             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1209           }
1210 #ifdef OBJ_ELF
1211         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1212           {
1213             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1214             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1215                                           SEC_READONLY | SEC_RELOC
1216                                           | SEC_DEBUGGING);
1217             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1218           }
1219 #endif
1220
1221         subseg_set (seg, subseg);
1222       }
1223     }
1224
1225   if (! ECOFF_DEBUGGING)
1226     md_obj_begin ();
1227 }
1228
1229 void
1230 md_mips_end ()
1231 {
1232   if (! ECOFF_DEBUGGING)
1233     md_obj_end ();
1234 }
1235
1236 void
1237 md_assemble (str)
1238      char *str;
1239 {
1240   struct mips_cl_insn insn;
1241   bfd_reloc_code_real_type unused_reloc[3]
1242     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1243
1244   imm_expr.X_op = O_absent;
1245   imm_unmatched_hi = false;
1246   offset_expr.X_op = O_absent;
1247   imm_reloc[0] = BFD_RELOC_UNUSED;
1248   imm_reloc[1] = BFD_RELOC_UNUSED;
1249   imm_reloc[2] = BFD_RELOC_UNUSED;
1250   offset_reloc[0] = BFD_RELOC_UNUSED;
1251   offset_reloc[1] = BFD_RELOC_UNUSED;
1252   offset_reloc[2] = BFD_RELOC_UNUSED;
1253
1254   if (mips_opts.mips16)
1255     mips16_ip (str, &insn);
1256   else
1257     {
1258       mips_ip (str, &insn);
1259       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1260             str, insn.insn_opcode));
1261     }
1262
1263   if (insn_error)
1264     {
1265       as_bad ("%s `%s'", insn_error, str);
1266       return;
1267     }
1268
1269   if (insn.insn_mo->pinfo == INSN_MACRO)
1270     {
1271       if (mips_opts.mips16)
1272         mips16_macro (&insn);
1273       else
1274         macro (&insn);
1275     }
1276   else
1277     {
1278       if (imm_expr.X_op != O_absent)
1279         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1280       else if (offset_expr.X_op != O_absent)
1281         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1282       else
1283         append_insn (NULL, &insn, NULL, unused_reloc, false);
1284     }
1285 }
1286
1287 /* See whether instruction IP reads register REG.  CLASS is the type
1288    of register.  */
1289
1290 static int
1291 insn_uses_reg (ip, reg, class)
1292      struct mips_cl_insn *ip;
1293      unsigned int reg;
1294      enum mips_regclass class;
1295 {
1296   if (class == MIPS16_REG)
1297     {
1298       assert (mips_opts.mips16);
1299       reg = mips16_to_32_reg_map[reg];
1300       class = MIPS_GR_REG;
1301     }
1302
1303   /* Don't report on general register ZERO, since it never changes.  */
1304   if (class == MIPS_GR_REG && reg == ZERO)
1305     return 0;
1306
1307   if (class == MIPS_FP_REG)
1308     {
1309       assert (! mips_opts.mips16);
1310       /* If we are called with either $f0 or $f1, we must check $f0.
1311          This is not optimal, because it will introduce an unnecessary
1312          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1313          need to distinguish reading both $f0 and $f1 or just one of
1314          them.  Note that we don't have to check the other way,
1315          because there is no instruction that sets both $f0 and $f1
1316          and requires a delay.  */
1317       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1318           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1319               == (reg &~ (unsigned) 1)))
1320         return 1;
1321       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1322           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1323               == (reg &~ (unsigned) 1)))
1324         return 1;
1325     }
1326   else if (! mips_opts.mips16)
1327     {
1328       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1329           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1330         return 1;
1331       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1332           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1333         return 1;
1334     }
1335   else
1336     {
1337       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1338           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1339                                     & MIPS16OP_MASK_RX)]
1340               == reg))
1341         return 1;
1342       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1343           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1344                                     & MIPS16OP_MASK_RY)]
1345               == reg))
1346         return 1;
1347       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1348           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1349                                     & MIPS16OP_MASK_MOVE32Z)]
1350               == reg))
1351         return 1;
1352       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1353         return 1;
1354       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1355         return 1;
1356       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1357         return 1;
1358       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1359           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1360               & MIPS16OP_MASK_REGR32) == reg)
1361         return 1;
1362     }
1363
1364   return 0;
1365 }
1366
1367 /* This function returns true if modifying a register requires a
1368    delay.  */
1369
1370 static int
1371 reg_needs_delay (reg)
1372      unsigned int reg;
1373 {
1374   unsigned long prev_pinfo;
1375
1376   prev_pinfo = prev_insn.insn_mo->pinfo;
1377   if (! mips_opts.noreorder
1378       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1379       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1380           || (! gpr_interlocks
1381               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1382     {
1383       /* A load from a coprocessor or from memory.  All load
1384          delays delay the use of general register rt for one
1385          instruction on the r3000.  The r6000 and r4000 use
1386          interlocks.  */
1387       /* Itbl support may require additional care here.  */
1388       know (prev_pinfo & INSN_WRITE_GPR_T);
1389       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1390         return 1;
1391     }
1392
1393   return 0;
1394 }
1395
1396 /* Mark instruction labels in mips16 mode.  This permits the linker to
1397    handle them specially, such as generating jalx instructions when
1398    needed.  We also make them odd for the duration of the assembly, in
1399    order to generate the right sort of code.  We will make them even
1400    in the adjust_symtab routine, while leaving them marked.  This is
1401    convenient for the debugger and the disassembler.  The linker knows
1402    to make them odd again.  */
1403
1404 static void
1405 mips16_mark_labels ()
1406 {
1407   if (mips_opts.mips16)
1408     {
1409       struct insn_label_list *l;
1410       valueT val;
1411
1412       for (l = insn_labels; l != NULL; l = l->next)
1413         {
1414 #ifdef OBJ_ELF
1415           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1416             S_SET_OTHER (l->label, STO_MIPS16);
1417 #endif
1418           val = S_GET_VALUE (l->label);
1419           if ((val & 1) == 0)
1420             S_SET_VALUE (l->label, val + 1);
1421         }
1422     }
1423 }
1424
1425 /* Output an instruction.  PLACE is where to put the instruction; if
1426    it is NULL, this uses frag_more to get room.  IP is the instruction
1427    information.  ADDRESS_EXPR is an operand of the instruction to be
1428    used with RELOC_TYPE.  */
1429
1430 static void
1431 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1432      char *place;
1433      struct mips_cl_insn *ip;
1434      expressionS *address_expr;
1435      bfd_reloc_code_real_type *reloc_type;
1436      boolean unmatched_hi;
1437 {
1438   register unsigned long prev_pinfo, pinfo;
1439   char *f;
1440   fixS *fixp[3];
1441   int nops = 0;
1442
1443   /* Mark instruction labels in mips16 mode.  */
1444   mips16_mark_labels ();
1445
1446   prev_pinfo = prev_insn.insn_mo->pinfo;
1447   pinfo = ip->insn_mo->pinfo;
1448
1449   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1450     {
1451       int prev_prev_nop;
1452
1453       /* If the previous insn required any delay slots, see if we need
1454          to insert a NOP or two.  There are eight kinds of possible
1455          hazards, of which an instruction can have at most one type.
1456          (1) a load from memory delay
1457          (2) a load from a coprocessor delay
1458          (3) an unconditional branch delay
1459          (4) a conditional branch delay
1460          (5) a move to coprocessor register delay
1461          (6) a load coprocessor register from memory delay
1462          (7) a coprocessor condition code delay
1463          (8) a HI/LO special register delay
1464
1465          There are a lot of optimizations we could do that we don't.
1466          In particular, we do not, in general, reorder instructions.
1467          If you use gcc with optimization, it will reorder
1468          instructions and generally do much more optimization then we
1469          do here; repeating all that work in the assembler would only
1470          benefit hand written assembly code, and does not seem worth
1471          it.  */
1472
1473       /* This is how a NOP is emitted.  */
1474 #define emit_nop()                                      \
1475   (mips_opts.mips16                                     \
1476    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1477    : md_number_to_chars (frag_more (4), 0, 4))
1478
1479       /* The previous insn might require a delay slot, depending upon
1480          the contents of the current insn.  */
1481       if (! mips_opts.mips16
1482           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1483           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1484                && ! cop_interlocks)
1485               || (! gpr_interlocks
1486                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1487         {
1488           /* A load from a coprocessor or from memory.  All load
1489              delays delay the use of general register rt for one
1490              instruction on the r3000.  The r6000 and r4000 use
1491              interlocks.  */
1492           /* Itbl support may require additional care here.  */
1493           know (prev_pinfo & INSN_WRITE_GPR_T);
1494           if (mips_optimize == 0
1495               || insn_uses_reg (ip,
1496                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1497                                  & OP_MASK_RT),
1498                                 MIPS_GR_REG))
1499             ++nops;
1500         }
1501       else if (! mips_opts.mips16
1502                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1503                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1504                     && ! cop_interlocks)
1505                    || (mips_opts.isa == ISA_MIPS1
1506                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1507         {
1508           /* A generic coprocessor delay.  The previous instruction
1509              modified a coprocessor general or control register.  If
1510              it modified a control register, we need to avoid any
1511              coprocessor instruction (this is probably not always
1512              required, but it sometimes is).  If it modified a general
1513              register, we avoid using that register.
1514
1515              On the r6000 and r4000 loading a coprocessor register
1516              from memory is interlocked, and does not require a delay.
1517
1518              This case is not handled very well.  There is no special
1519              knowledge of CP0 handling, and the coprocessors other
1520              than the floating point unit are not distinguished at
1521              all.  */
1522           /* Itbl support may require additional care here. FIXME!
1523              Need to modify this to include knowledge about
1524              user specified delays!  */
1525           if (prev_pinfo & INSN_WRITE_FPR_T)
1526             {
1527               if (mips_optimize == 0
1528                   || insn_uses_reg (ip,
1529                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1530                                      & OP_MASK_FT),
1531                                     MIPS_FP_REG))
1532                 ++nops;
1533             }
1534           else if (prev_pinfo & INSN_WRITE_FPR_S)
1535             {
1536               if (mips_optimize == 0
1537                   || insn_uses_reg (ip,
1538                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1539                                      & OP_MASK_FS),
1540                                     MIPS_FP_REG))
1541                 ++nops;
1542             }
1543           else
1544             {
1545               /* We don't know exactly what the previous instruction
1546                  does.  If the current instruction uses a coprocessor
1547                  register, we must insert a NOP.  If previous
1548                  instruction may set the condition codes, and the
1549                  current instruction uses them, we must insert two
1550                  NOPS.  */
1551               /* Itbl support may require additional care here.  */
1552               if (mips_optimize == 0
1553                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1554                       && (pinfo & INSN_READ_COND_CODE)))
1555                 nops += 2;
1556               else if (pinfo & INSN_COP)
1557                 ++nops;
1558             }
1559         }
1560       else if (! mips_opts.mips16
1561                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1562                && (prev_pinfo & INSN_WRITE_COND_CODE)
1563                && ! cop_interlocks)
1564         {
1565           /* The previous instruction sets the coprocessor condition
1566              codes, but does not require a general coprocessor delay
1567              (this means it is a floating point comparison
1568              instruction).  If this instruction uses the condition
1569              codes, we need to insert a single NOP.  */
1570           /* Itbl support may require additional care here.  */
1571           if (mips_optimize == 0
1572               || (pinfo & INSN_READ_COND_CODE))
1573             ++nops;
1574         }
1575
1576       /* If we're fixing up mfhi/mflo for the r7000 and the
1577          previous insn was an mfhi/mflo and the current insn
1578          reads the register that the mfhi/mflo wrote to, then
1579          insert two nops.  */
1580
1581       else if (mips_7000_hilo_fix
1582                && MF_HILO_INSN (prev_pinfo)
1583                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1584                                       & OP_MASK_RD),
1585                                  MIPS_GR_REG))
1586         {
1587           nops += 2;
1588         }
1589
1590       /* If we're fixing up mfhi/mflo for the r7000 and the
1591          2nd previous insn was an mfhi/mflo and the current insn
1592          reads the register that the mfhi/mflo wrote to, then
1593          insert one nop.  */
1594
1595       else if (mips_7000_hilo_fix
1596                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1597                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1598                                        & OP_MASK_RD),
1599                                     MIPS_GR_REG))
1600
1601         {
1602           ++nops;
1603         }
1604
1605       else if (prev_pinfo & INSN_READ_LO)
1606         {
1607           /* The previous instruction reads the LO register; if the
1608              current instruction writes to the LO register, we must
1609              insert two NOPS.  Some newer processors have interlocks.
1610              Also the tx39's multiply instructions can be exectuted
1611              immediatly after a read from HI/LO (without the delay),
1612              though the tx39's divide insns still do require the
1613              delay.  */
1614           if (! (hilo_interlocks
1615                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1616               && (mips_optimize == 0
1617                   || (pinfo & INSN_WRITE_LO)))
1618             nops += 2;
1619           /* Most mips16 branch insns don't have a delay slot.
1620              If a read from LO is immediately followed by a branch
1621              to a write to LO we have a read followed by a write
1622              less than 2 insns away.  We assume the target of
1623              a branch might be a write to LO, and insert a nop
1624              between a read and an immediately following branch.  */
1625           else if (mips_opts.mips16
1626                    && (mips_optimize == 0
1627                        || (pinfo & MIPS16_INSN_BRANCH)))
1628             ++nops;
1629         }
1630       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1631         {
1632           /* The previous instruction reads the HI register; if the
1633              current instruction writes to the HI register, we must
1634              insert a NOP.  Some newer processors have interlocks.
1635              Also the note tx39's multiply above.  */
1636           if (! (hilo_interlocks
1637                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1638               && (mips_optimize == 0
1639                   || (pinfo & INSN_WRITE_HI)))
1640             nops += 2;
1641           /* Most mips16 branch insns don't have a delay slot.
1642              If a read from HI is immediately followed by a branch
1643              to a write to HI we have a read followed by a write
1644              less than 2 insns away.  We assume the target of
1645              a branch might be a write to HI, and insert a nop
1646              between a read and an immediately following branch.  */
1647           else if (mips_opts.mips16
1648                    && (mips_optimize == 0
1649                        || (pinfo & MIPS16_INSN_BRANCH)))
1650             ++nops;
1651         }
1652
1653       /* If the previous instruction was in a noreorder section, then
1654          we don't want to insert the nop after all.  */
1655       /* Itbl support may require additional care here.  */
1656       if (prev_insn_unreordered)
1657         nops = 0;
1658
1659       /* There are two cases which require two intervening
1660          instructions: 1) setting the condition codes using a move to
1661          coprocessor instruction which requires a general coprocessor
1662          delay and then reading the condition codes 2) reading the HI
1663          or LO register and then writing to it (except on processors
1664          which have interlocks).  If we are not already emitting a NOP
1665          instruction, we must check for these cases compared to the
1666          instruction previous to the previous instruction.  */
1667       if ((! mips_opts.mips16
1668            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1669            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1670            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1671            && (pinfo & INSN_READ_COND_CODE)
1672            && ! cop_interlocks)
1673           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1674               && (pinfo & INSN_WRITE_LO)
1675               && ! (hilo_interlocks
1676                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1677           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1678               && (pinfo & INSN_WRITE_HI)
1679               && ! (hilo_interlocks
1680                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1681         prev_prev_nop = 1;
1682       else
1683         prev_prev_nop = 0;
1684
1685       if (prev_prev_insn_unreordered)
1686         prev_prev_nop = 0;
1687
1688       if (prev_prev_nop && nops == 0)
1689         ++nops;
1690
1691       /* If we are being given a nop instruction, don't bother with
1692          one of the nops we would otherwise output.  This will only
1693          happen when a nop instruction is used with mips_optimize set
1694          to 0.  */
1695       if (nops > 0
1696           && ! mips_opts.noreorder
1697           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1698         --nops;
1699
1700       /* Now emit the right number of NOP instructions.  */
1701       if (nops > 0 && ! mips_opts.noreorder)
1702         {
1703           fragS *old_frag;
1704           unsigned long old_frag_offset;
1705           int i;
1706           struct insn_label_list *l;
1707
1708           old_frag = frag_now;
1709           old_frag_offset = frag_now_fix ();
1710
1711           for (i = 0; i < nops; i++)
1712             emit_nop ();
1713
1714           if (listing)
1715             {
1716               listing_prev_line ();
1717               /* We may be at the start of a variant frag.  In case we
1718                  are, make sure there is enough space for the frag
1719                  after the frags created by listing_prev_line.  The
1720                  argument to frag_grow here must be at least as large
1721                  as the argument to all other calls to frag_grow in
1722                  this file.  We don't have to worry about being in the
1723                  middle of a variant frag, because the variants insert
1724                  all needed nop instructions themselves.  */
1725               frag_grow (40);
1726             }
1727
1728           for (l = insn_labels; l != NULL; l = l->next)
1729             {
1730               valueT val;
1731
1732               assert (S_GET_SEGMENT (l->label) == now_seg);
1733               symbol_set_frag (l->label, frag_now);
1734               val = (valueT) frag_now_fix ();
1735               /* mips16 text labels are stored as odd.  */
1736               if (mips_opts.mips16)
1737                 ++val;
1738               S_SET_VALUE (l->label, val);
1739             }
1740
1741 #ifndef NO_ECOFF_DEBUGGING
1742           if (ECOFF_DEBUGGING)
1743             ecoff_fix_loc (old_frag, old_frag_offset);
1744 #endif
1745         }
1746       else if (prev_nop_frag != NULL)
1747         {
1748           /* We have a frag holding nops we may be able to remove.  If
1749              we don't need any nops, we can decrease the size of
1750              prev_nop_frag by the size of one instruction.  If we do
1751              need some nops, we count them in prev_nops_required.  */
1752           if (prev_nop_frag_since == 0)
1753             {
1754               if (nops == 0)
1755                 {
1756                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1757                   --prev_nop_frag_holds;
1758                 }
1759               else
1760                 prev_nop_frag_required += nops;
1761             }
1762           else
1763             {
1764               if (prev_prev_nop == 0)
1765                 {
1766                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1767                   --prev_nop_frag_holds;
1768                 }
1769               else
1770                 ++prev_nop_frag_required;
1771             }
1772
1773           if (prev_nop_frag_holds <= prev_nop_frag_required)
1774             prev_nop_frag = NULL;
1775
1776           ++prev_nop_frag_since;
1777
1778           /* Sanity check: by the time we reach the second instruction
1779              after prev_nop_frag, we should have used up all the nops
1780              one way or another.  */
1781           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1782         }
1783     }
1784
1785   if (*reloc_type > BFD_RELOC_UNUSED)
1786     {
1787       /* We need to set up a variant frag.  */
1788       assert (mips_opts.mips16 && address_expr != NULL);
1789       f = frag_var (rs_machine_dependent, 4, 0,
1790                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1791                                          mips16_small, mips16_ext,
1792                                          (prev_pinfo
1793                                           & INSN_UNCOND_BRANCH_DELAY),
1794                                          (*prev_insn_reloc_type
1795                                           == BFD_RELOC_MIPS16_JMP)),
1796                     make_expr_symbol (address_expr), 0, NULL);
1797     }
1798   else if (place != NULL)
1799     f = place;
1800   else if (mips_opts.mips16
1801            && ! ip->use_extend
1802            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1803     {
1804       /* Make sure there is enough room to swap this instruction with
1805          a following jump instruction.  */
1806       frag_grow (6);
1807       f = frag_more (2);
1808     }
1809   else
1810     {
1811       if (mips_opts.mips16
1812           && mips_opts.noreorder
1813           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1814         as_warn (_("extended instruction in delay slot"));
1815
1816       f = frag_more (4);
1817     }
1818
1819   fixp[0] = fixp[1] = fixp[2] = NULL;
1820   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1821     {
1822       if (address_expr->X_op == O_constant)
1823         {
1824           valueT tmp;
1825
1826           switch (*reloc_type)
1827             {
1828             case BFD_RELOC_32:
1829               ip->insn_opcode |= address_expr->X_add_number;
1830               break;
1831
1832             case BFD_RELOC_MIPS_HIGHEST:
1833               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1834               tmp >>= 16;
1835               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1836               break;
1837
1838             case BFD_RELOC_MIPS_HIGHER:
1839               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1840               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1841               break;
1842
1843             case BFD_RELOC_HI16_S:
1844               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1845                                   >> 16) & 0xffff;
1846               break;
1847
1848             case BFD_RELOC_HI16:
1849               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1850               break;
1851
1852             case BFD_RELOC_LO16:
1853             case BFD_RELOC_MIPS_GOT_DISP:
1854               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1855               break;
1856
1857             case BFD_RELOC_MIPS_JMP:
1858               if ((address_expr->X_add_number & 3) != 0)
1859                 as_bad (_("jump to misaligned address (0x%lx)"),
1860                         (unsigned long) address_expr->X_add_number);
1861               if (address_expr->X_add_number & ~0xfffffff)
1862                 as_bad (_("jump address range overflow (0x%lx)"),
1863                         (unsigned long) address_expr->X_add_number);
1864               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1865               break;
1866
1867             case BFD_RELOC_MIPS16_JMP:
1868               if ((address_expr->X_add_number & 3) != 0)
1869                 as_bad (_("jump to misaligned address (0x%lx)"),
1870                         (unsigned long) address_expr->X_add_number);
1871               if (address_expr->X_add_number & ~0xfffffff)
1872                 as_bad (_("jump address range overflow (0x%lx)"),
1873                         (unsigned long) address_expr->X_add_number);
1874               ip->insn_opcode |=
1875                 (((address_expr->X_add_number & 0x7c0000) << 3)
1876                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1877                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1878               break;
1879
1880             case BFD_RELOC_16_PCREL:
1881               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1882               break;
1883
1884             case BFD_RELOC_16_PCREL_S2:
1885               goto need_reloc;
1886
1887             default:
1888               internalError ();
1889             }
1890         }
1891       else
1892         {
1893         need_reloc:
1894           /* Don't generate a reloc if we are writing into a variant frag.  */
1895           if (place == NULL)
1896             {
1897               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1898                                      address_expr,
1899                                      (*reloc_type == BFD_RELOC_16_PCREL
1900                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
1901                                      reloc_type[0]);
1902
1903               /* These relocations can have an addend that won't fit in
1904                  4 octets for 64bit assembly.  */
1905               if (HAVE_64BIT_GPRS &&
1906                   (*reloc_type == BFD_RELOC_16
1907                    || *reloc_type == BFD_RELOC_32
1908                    || *reloc_type == BFD_RELOC_MIPS_JMP
1909                    || *reloc_type == BFD_RELOC_HI16_S
1910                    || *reloc_type == BFD_RELOC_LO16
1911                    || *reloc_type == BFD_RELOC_GPREL16
1912                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
1913                    || *reloc_type == BFD_RELOC_GPREL32
1914                    || *reloc_type == BFD_RELOC_64
1915                    || *reloc_type == BFD_RELOC_CTOR
1916                    || *reloc_type == BFD_RELOC_MIPS_SUB
1917                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1918                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
1919                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1920                    || *reloc_type == BFD_RELOC_MIPS_REL16
1921                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1922                 fixp[0]->fx_no_overflow = 1;
1923
1924               if (unmatched_hi)
1925                 {
1926                   struct mips_hi_fixup *hi_fixup;
1927
1928                   assert (*reloc_type == BFD_RELOC_HI16_S);
1929                   hi_fixup = ((struct mips_hi_fixup *)
1930                               xmalloc (sizeof (struct mips_hi_fixup)));
1931                   hi_fixup->fixp = fixp[0];
1932                   hi_fixup->seg = now_seg;
1933                   hi_fixup->next = mips_hi_fixup_list;
1934                   mips_hi_fixup_list = hi_fixup;
1935                 }
1936
1937               if (reloc_type[1] != BFD_RELOC_UNUSED)
1938                 {
1939                   /* FIXME: This symbol can be one of
1940                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
1941                   address_expr->X_op = O_absent;
1942                   address_expr->X_add_symbol = 0;
1943                   address_expr->X_add_number = 0;
1944
1945                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1946                                          4, address_expr, false,
1947                                          reloc_type[1]);
1948
1949                   /* These relocations can have an addend that won't fit in
1950                      4 octets for 64bit assembly.  */
1951                   if (HAVE_64BIT_GPRS &&
1952                       (*reloc_type == BFD_RELOC_16
1953                        || *reloc_type == BFD_RELOC_32
1954                        || *reloc_type == BFD_RELOC_MIPS_JMP
1955                        || *reloc_type == BFD_RELOC_HI16_S
1956                        || *reloc_type == BFD_RELOC_LO16
1957                        || *reloc_type == BFD_RELOC_GPREL16
1958                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
1959                        || *reloc_type == BFD_RELOC_GPREL32
1960                        || *reloc_type == BFD_RELOC_64
1961                        || *reloc_type == BFD_RELOC_CTOR
1962                        || *reloc_type == BFD_RELOC_MIPS_SUB
1963                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1964                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
1965                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1966                        || *reloc_type == BFD_RELOC_MIPS_REL16
1967                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1968                     fixp[1]->fx_no_overflow = 1;
1969
1970                   if (reloc_type[2] != BFD_RELOC_UNUSED)
1971                     {
1972                       address_expr->X_op = O_absent;
1973                       address_expr->X_add_symbol = 0;
1974                       address_expr->X_add_number = 0;
1975
1976                       fixp[2] = fix_new_exp (frag_now,
1977                                              f - frag_now->fr_literal, 4,
1978                                              address_expr, false,
1979                                              reloc_type[2]);
1980
1981                       /* These relocations can have an addend that won't fit in
1982                          4 octets for 64bit assembly.  */
1983                       if (HAVE_64BIT_GPRS &&
1984                           (*reloc_type == BFD_RELOC_16
1985                            || *reloc_type == BFD_RELOC_32
1986                            || *reloc_type == BFD_RELOC_MIPS_JMP
1987                            || *reloc_type == BFD_RELOC_HI16_S
1988                            || *reloc_type == BFD_RELOC_LO16
1989                            || *reloc_type == BFD_RELOC_GPREL16
1990                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
1991                            || *reloc_type == BFD_RELOC_GPREL32
1992                            || *reloc_type == BFD_RELOC_64
1993                            || *reloc_type == BFD_RELOC_CTOR
1994                            || *reloc_type == BFD_RELOC_MIPS_SUB
1995                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1996                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
1997                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1998                            || *reloc_type == BFD_RELOC_MIPS_REL16
1999                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2000                         fixp[2]->fx_no_overflow = 1;
2001                     }
2002                 }
2003             }
2004         }
2005     }
2006
2007   if (! mips_opts.mips16)
2008     {
2009       md_number_to_chars (f, ip->insn_opcode, 4);
2010 #ifdef OBJ_ELF
2011       dwarf2_emit_insn (4);
2012 #endif
2013     }
2014   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2015     {
2016       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2017       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2018 #ifdef OBJ_ELF
2019       dwarf2_emit_insn (4);
2020 #endif
2021     }
2022   else
2023     {
2024       if (ip->use_extend)
2025         {
2026           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2027           f += 2;
2028         }
2029       md_number_to_chars (f, ip->insn_opcode, 2);
2030 #ifdef OBJ_ELF
2031       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2032 #endif
2033     }
2034
2035   /* Update the register mask information.  */
2036   if (! mips_opts.mips16)
2037     {
2038       if (pinfo & INSN_WRITE_GPR_D)
2039         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2040       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2041         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2042       if (pinfo & INSN_READ_GPR_S)
2043         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2044       if (pinfo & INSN_WRITE_GPR_31)
2045         mips_gprmask |= 1 << RA;
2046       if (pinfo & INSN_WRITE_FPR_D)
2047         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2048       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2049         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2050       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2051         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2052       if ((pinfo & INSN_READ_FPR_R) != 0)
2053         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2054       if (pinfo & INSN_COP)
2055         {
2056           /* We don't keep enough information to sort these cases out.
2057              The itbl support does keep this information however, although
2058              we currently don't support itbl fprmats as part of the cop
2059              instruction.  May want to add this support in the future.  */
2060         }
2061       /* Never set the bit for $0, which is always zero.  */
2062       mips_gprmask &= ~1 << 0;
2063     }
2064   else
2065     {
2066       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2067         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2068                               & MIPS16OP_MASK_RX);
2069       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2070         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2071                               & MIPS16OP_MASK_RY);
2072       if (pinfo & MIPS16_INSN_WRITE_Z)
2073         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2074                               & MIPS16OP_MASK_RZ);
2075       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2076         mips_gprmask |= 1 << TREG;
2077       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2078         mips_gprmask |= 1 << SP;
2079       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2080         mips_gprmask |= 1 << RA;
2081       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2082         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2083       if (pinfo & MIPS16_INSN_READ_Z)
2084         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2085                               & MIPS16OP_MASK_MOVE32Z);
2086       if (pinfo & MIPS16_INSN_READ_GPR_X)
2087         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2088                               & MIPS16OP_MASK_REGR32);
2089     }
2090
2091   if (place == NULL && ! mips_opts.noreorder)
2092     {
2093       /* Filling the branch delay slot is more complex.  We try to
2094          switch the branch with the previous instruction, which we can
2095          do if the previous instruction does not set up a condition
2096          that the branch tests and if the branch is not itself the
2097          target of any branch.  */
2098       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2099           || (pinfo & INSN_COND_BRANCH_DELAY))
2100         {
2101           if (mips_optimize < 2
2102               /* If we have seen .set volatile or .set nomove, don't
2103                  optimize.  */
2104               || mips_opts.nomove != 0
2105               /* If we had to emit any NOP instructions, then we
2106                  already know we can not swap.  */
2107               || nops != 0
2108               /* If we don't even know the previous insn, we can not
2109                  swap.  */
2110               || ! prev_insn_valid
2111               /* If the previous insn is already in a branch delay
2112                  slot, then we can not swap.  */
2113               || prev_insn_is_delay_slot
2114               /* If the previous previous insn was in a .set
2115                  noreorder, we can't swap.  Actually, the MIPS
2116                  assembler will swap in this situation.  However, gcc
2117                  configured -with-gnu-as will generate code like
2118                    .set noreorder
2119                    lw   $4,XXX
2120                    .set reorder
2121                    INSN
2122                    bne  $4,$0,foo
2123                  in which we can not swap the bne and INSN.  If gcc is
2124                  not configured -with-gnu-as, it does not output the
2125                  .set pseudo-ops.  We don't have to check
2126                  prev_insn_unreordered, because prev_insn_valid will
2127                  be 0 in that case.  We don't want to use
2128                  prev_prev_insn_valid, because we do want to be able
2129                  to swap at the start of a function.  */
2130               || prev_prev_insn_unreordered
2131               /* If the branch is itself the target of a branch, we
2132                  can not swap.  We cheat on this; all we check for is
2133                  whether there is a label on this instruction.  If
2134                  there are any branches to anything other than a
2135                  label, users must use .set noreorder.  */
2136               || insn_labels != NULL
2137               /* If the previous instruction is in a variant frag, we
2138                  can not do the swap.  This does not apply to the
2139                  mips16, which uses variant frags for different
2140                  purposes.  */
2141               || (! mips_opts.mips16
2142                   && prev_insn_frag->fr_type == rs_machine_dependent)
2143               /* If the branch reads the condition codes, we don't
2144                  even try to swap, because in the sequence
2145                    ctc1 $X,$31
2146                    INSN
2147                    INSN
2148                    bc1t LABEL
2149                  we can not swap, and I don't feel like handling that
2150                  case.  */
2151               || (! mips_opts.mips16
2152                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2153                   && (pinfo & INSN_READ_COND_CODE))
2154               /* We can not swap with an instruction that requires a
2155                  delay slot, becase the target of the branch might
2156                  interfere with that instruction.  */
2157               || (! mips_opts.mips16
2158                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2159                   && (prev_pinfo
2160               /* Itbl support may require additional care here.  */
2161                       & (INSN_LOAD_COPROC_DELAY
2162                          | INSN_COPROC_MOVE_DELAY
2163                          | INSN_WRITE_COND_CODE)))
2164               || (! (hilo_interlocks
2165                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2166                   && (prev_pinfo
2167                       & (INSN_READ_LO
2168                          | INSN_READ_HI)))
2169               || (! mips_opts.mips16
2170                   && ! gpr_interlocks
2171                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2172               || (! mips_opts.mips16
2173                   && mips_opts.isa == ISA_MIPS1
2174                   /* Itbl support may require additional care here.  */
2175                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2176               /* We can not swap with a branch instruction.  */
2177               || (prev_pinfo
2178                   & (INSN_UNCOND_BRANCH_DELAY
2179                      | INSN_COND_BRANCH_DELAY
2180                      | INSN_COND_BRANCH_LIKELY))
2181               /* We do not swap with a trap instruction, since it
2182                  complicates trap handlers to have the trap
2183                  instruction be in a delay slot.  */
2184               || (prev_pinfo & INSN_TRAP)
2185               /* If the branch reads a register that the previous
2186                  instruction sets, we can not swap.  */
2187               || (! mips_opts.mips16
2188                   && (prev_pinfo & INSN_WRITE_GPR_T)
2189                   && insn_uses_reg (ip,
2190                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2191                                      & OP_MASK_RT),
2192                                     MIPS_GR_REG))
2193               || (! mips_opts.mips16
2194                   && (prev_pinfo & INSN_WRITE_GPR_D)
2195                   && insn_uses_reg (ip,
2196                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2197                                      & OP_MASK_RD),
2198                                     MIPS_GR_REG))
2199               || (mips_opts.mips16
2200                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2201                        && insn_uses_reg (ip,
2202                                          ((prev_insn.insn_opcode
2203                                            >> MIPS16OP_SH_RX)
2204                                           & MIPS16OP_MASK_RX),
2205                                          MIPS16_REG))
2206                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2207                           && insn_uses_reg (ip,
2208                                             ((prev_insn.insn_opcode
2209                                               >> MIPS16OP_SH_RY)
2210                                              & MIPS16OP_MASK_RY),
2211                                             MIPS16_REG))
2212                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2213                           && insn_uses_reg (ip,
2214                                             ((prev_insn.insn_opcode
2215                                               >> MIPS16OP_SH_RZ)
2216                                              & MIPS16OP_MASK_RZ),
2217                                             MIPS16_REG))
2218                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2219                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2220                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2221                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2222                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2223                           && insn_uses_reg (ip,
2224                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2225                                                                      insn_opcode),
2226                                             MIPS_GR_REG))))
2227               /* If the branch writes a register that the previous
2228                  instruction sets, we can not swap (we know that
2229                  branches write only to RD or to $31).  */
2230               || (! mips_opts.mips16
2231                   && (prev_pinfo & INSN_WRITE_GPR_T)
2232                   && (((pinfo & INSN_WRITE_GPR_D)
2233                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2234                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2235                       || ((pinfo & INSN_WRITE_GPR_31)
2236                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2237                                & OP_MASK_RT)
2238                               == RA))))
2239               || (! mips_opts.mips16
2240                   && (prev_pinfo & INSN_WRITE_GPR_D)
2241                   && (((pinfo & INSN_WRITE_GPR_D)
2242                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2243                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2244                       || ((pinfo & INSN_WRITE_GPR_31)
2245                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2246                                & OP_MASK_RD)
2247                               == RA))))
2248               || (mips_opts.mips16
2249                   && (pinfo & MIPS16_INSN_WRITE_31)
2250                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2251                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2252                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2253                               == RA))))
2254               /* If the branch writes a register that the previous
2255                  instruction reads, we can not swap (we know that
2256                  branches only write to RD or to $31).  */
2257               || (! mips_opts.mips16
2258                   && (pinfo & INSN_WRITE_GPR_D)
2259                   && insn_uses_reg (&prev_insn,
2260                                     ((ip->insn_opcode >> OP_SH_RD)
2261                                      & OP_MASK_RD),
2262                                     MIPS_GR_REG))
2263               || (! mips_opts.mips16
2264                   && (pinfo & INSN_WRITE_GPR_31)
2265                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2266               || (mips_opts.mips16
2267                   && (pinfo & MIPS16_INSN_WRITE_31)
2268                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2269               /* If we are generating embedded PIC code, the branch
2270                  might be expanded into a sequence which uses $at, so
2271                  we can't swap with an instruction which reads it.  */
2272               || (mips_pic == EMBEDDED_PIC
2273                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2274               /* If the previous previous instruction has a load
2275                  delay, and sets a register that the branch reads, we
2276                  can not swap.  */
2277               || (! mips_opts.mips16
2278                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2279               /* Itbl support may require additional care here.  */
2280                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2281                       || (! gpr_interlocks
2282                           && (prev_prev_insn.insn_mo->pinfo
2283                               & INSN_LOAD_MEMORY_DELAY)))
2284                   && insn_uses_reg (ip,
2285                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2286                                      & OP_MASK_RT),
2287                                     MIPS_GR_REG))
2288               /* If one instruction sets a condition code and the
2289                  other one uses a condition code, we can not swap.  */
2290               || ((pinfo & INSN_READ_COND_CODE)
2291                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2292               || ((pinfo & INSN_WRITE_COND_CODE)
2293                   && (prev_pinfo & INSN_READ_COND_CODE))
2294               /* If the previous instruction uses the PC, we can not
2295                  swap.  */
2296               || (mips_opts.mips16
2297                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2298               /* If the previous instruction was extended, we can not
2299                  swap.  */
2300               || (mips_opts.mips16 && prev_insn_extended)
2301               /* If the previous instruction had a fixup in mips16
2302                  mode, we can not swap.  This normally means that the
2303                  previous instruction was a 4 byte branch anyhow.  */
2304               || (mips_opts.mips16 && prev_insn_fixp[0])
2305               /* If the previous instruction is a sync, sync.l, or
2306                  sync.p, we can not swap.  */
2307               || (prev_pinfo & INSN_SYNC))
2308             {
2309               /* We could do even better for unconditional branches to
2310                  portions of this object file; we could pick up the
2311                  instruction at the destination, put it in the delay
2312                  slot, and bump the destination address.  */
2313               emit_nop ();
2314               /* Update the previous insn information.  */
2315               prev_prev_insn = *ip;
2316               prev_insn.insn_mo = &dummy_opcode;
2317             }
2318           else
2319             {
2320               /* It looks like we can actually do the swap.  */
2321               if (! mips_opts.mips16)
2322                 {
2323                   char *prev_f;
2324                   char temp[4];
2325
2326                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2327                   memcpy (temp, prev_f, 4);
2328                   memcpy (prev_f, f, 4);
2329                   memcpy (f, temp, 4);
2330                   if (prev_insn_fixp[0])
2331                     {
2332                       prev_insn_fixp[0]->fx_frag = frag_now;
2333                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2334                     }
2335                   if (prev_insn_fixp[1])
2336                     {
2337                       prev_insn_fixp[1]->fx_frag = frag_now;
2338                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2339                     }
2340                   if (prev_insn_fixp[2])
2341                     {
2342                       prev_insn_fixp[2]->fx_frag = frag_now;
2343                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2344                     }
2345                   if (fixp[0])
2346                     {
2347                       fixp[0]->fx_frag = prev_insn_frag;
2348                       fixp[0]->fx_where = prev_insn_where;
2349                     }
2350                   if (fixp[1])
2351                     {
2352                       fixp[1]->fx_frag = prev_insn_frag;
2353                       fixp[1]->fx_where = prev_insn_where;
2354                     }
2355                   if (fixp[2])
2356                     {
2357                       fixp[2]->fx_frag = prev_insn_frag;
2358                       fixp[2]->fx_where = prev_insn_where;
2359                     }
2360                 }
2361               else
2362                 {
2363                   char *prev_f;
2364                   char temp[2];
2365
2366                   assert (prev_insn_fixp[0] == NULL);
2367                   assert (prev_insn_fixp[1] == NULL);
2368                   assert (prev_insn_fixp[2] == NULL);
2369                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2370                   memcpy (temp, prev_f, 2);
2371                   memcpy (prev_f, f, 2);
2372                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2373                     {
2374                       assert (*reloc_type == BFD_RELOC_UNUSED);
2375                       memcpy (f, temp, 2);
2376                     }
2377                   else
2378                     {
2379                       memcpy (f, f + 2, 2);
2380                       memcpy (f + 2, temp, 2);
2381                     }
2382                   if (fixp[0])
2383                     {
2384                       fixp[0]->fx_frag = prev_insn_frag;
2385                       fixp[0]->fx_where = prev_insn_where;
2386                     }
2387                   if (fixp[1])
2388                     {
2389                       fixp[1]->fx_frag = prev_insn_frag;
2390                       fixp[1]->fx_where = prev_insn_where;
2391                     }
2392                   if (fixp[2])
2393                     {
2394                       fixp[2]->fx_frag = prev_insn_frag;
2395                       fixp[2]->fx_where = prev_insn_where;
2396                     }
2397                 }
2398
2399               /* Update the previous insn information; leave prev_insn
2400                  unchanged.  */
2401               prev_prev_insn = *ip;
2402             }
2403           prev_insn_is_delay_slot = 1;
2404
2405           /* If that was an unconditional branch, forget the previous
2406              insn information.  */
2407           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2408             {
2409               prev_prev_insn.insn_mo = &dummy_opcode;
2410               prev_insn.insn_mo = &dummy_opcode;
2411             }
2412
2413           prev_insn_fixp[0] = NULL;
2414           prev_insn_fixp[1] = NULL;
2415           prev_insn_fixp[2] = NULL;
2416           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2417           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2418           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2419           prev_insn_extended = 0;
2420         }
2421       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2422         {
2423           /* We don't yet optimize a branch likely.  What we should do
2424              is look at the target, copy the instruction found there
2425              into the delay slot, and increment the branch to jump to
2426              the next instruction.  */
2427           emit_nop ();
2428           /* Update the previous insn information.  */
2429           prev_prev_insn = *ip;
2430           prev_insn.insn_mo = &dummy_opcode;
2431           prev_insn_fixp[0] = NULL;
2432           prev_insn_fixp[1] = NULL;
2433           prev_insn_fixp[2] = NULL;
2434           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2435           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2436           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2437           prev_insn_extended = 0;
2438         }
2439       else
2440         {
2441           /* Update the previous insn information.  */
2442           if (nops > 0)
2443             prev_prev_insn.insn_mo = &dummy_opcode;
2444           else
2445             prev_prev_insn = prev_insn;
2446           prev_insn = *ip;
2447
2448           /* Any time we see a branch, we always fill the delay slot
2449              immediately; since this insn is not a branch, we know it
2450              is not in a delay slot.  */
2451           prev_insn_is_delay_slot = 0;
2452
2453           prev_insn_fixp[0] = fixp[0];
2454           prev_insn_fixp[1] = fixp[1];
2455           prev_insn_fixp[2] = fixp[2];
2456           prev_insn_reloc_type[0] = reloc_type[0];
2457           prev_insn_reloc_type[1] = reloc_type[1];
2458           prev_insn_reloc_type[2] = reloc_type[2];
2459           if (mips_opts.mips16)
2460             prev_insn_extended = (ip->use_extend
2461                                   || *reloc_type > BFD_RELOC_UNUSED);
2462         }
2463
2464       prev_prev_insn_unreordered = prev_insn_unreordered;
2465       prev_insn_unreordered = 0;
2466       prev_insn_frag = frag_now;
2467       prev_insn_where = f - frag_now->fr_literal;
2468       prev_insn_valid = 1;
2469     }
2470   else if (place == NULL)
2471     {
2472       /* We need to record a bit of information even when we are not
2473          reordering, in order to determine the base address for mips16
2474          PC relative relocs.  */
2475       prev_prev_insn = prev_insn;
2476       prev_insn = *ip;
2477       prev_insn_reloc_type[0] = reloc_type[0];
2478       prev_insn_reloc_type[1] = reloc_type[1];
2479       prev_insn_reloc_type[2] = reloc_type[2];
2480       prev_prev_insn_unreordered = prev_insn_unreordered;
2481       prev_insn_unreordered = 1;
2482     }
2483
2484   /* We just output an insn, so the next one doesn't have a label.  */
2485   mips_clear_insn_labels ();
2486
2487   /* We must ensure that a fixup associated with an unmatched %hi
2488      reloc does not become a variant frag.  Otherwise, the
2489      rearrangement of %hi relocs in frob_file may confuse
2490      tc_gen_reloc.  */
2491   if (unmatched_hi)
2492     {
2493       frag_wane (frag_now);
2494       frag_new (0);
2495     }
2496 }
2497
2498 /* This function forgets that there was any previous instruction or
2499    label.  If PRESERVE is non-zero, it remembers enough information to
2500    know whether nops are needed before a noreorder section.  */
2501
2502 static void
2503 mips_no_prev_insn (preserve)
2504      int preserve;
2505 {
2506   if (! preserve)
2507     {
2508       prev_insn.insn_mo = &dummy_opcode;
2509       prev_prev_insn.insn_mo = &dummy_opcode;
2510       prev_nop_frag = NULL;
2511       prev_nop_frag_holds = 0;
2512       prev_nop_frag_required = 0;
2513       prev_nop_frag_since = 0;
2514     }
2515   prev_insn_valid = 0;
2516   prev_insn_is_delay_slot = 0;
2517   prev_insn_unreordered = 0;
2518   prev_insn_extended = 0;
2519   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2520   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2521   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2522   prev_prev_insn_unreordered = 0;
2523   mips_clear_insn_labels ();
2524 }
2525
2526 /* This function must be called whenever we turn on noreorder or emit
2527    something other than instructions.  It inserts any NOPS which might
2528    be needed by the previous instruction, and clears the information
2529    kept for the previous instructions.  The INSNS parameter is true if
2530    instructions are to follow.  */
2531
2532 static void
2533 mips_emit_delays (insns)
2534      boolean insns;
2535 {
2536   if (! mips_opts.noreorder)
2537     {
2538       int nops;
2539
2540       nops = 0;
2541       if ((! mips_opts.mips16
2542            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2543            && (! cop_interlocks
2544                && (prev_insn.insn_mo->pinfo
2545                    & (INSN_LOAD_COPROC_DELAY
2546                       | INSN_COPROC_MOVE_DELAY
2547                       | INSN_WRITE_COND_CODE))))
2548           || (! hilo_interlocks
2549               && (prev_insn.insn_mo->pinfo
2550                   & (INSN_READ_LO
2551                      | INSN_READ_HI)))
2552           || (! mips_opts.mips16
2553               && ! gpr_interlocks
2554               && (prev_insn.insn_mo->pinfo
2555                   & INSN_LOAD_MEMORY_DELAY))
2556           || (! mips_opts.mips16
2557               && mips_opts.isa == ISA_MIPS1
2558               && (prev_insn.insn_mo->pinfo
2559                   & INSN_COPROC_MEMORY_DELAY)))
2560         {
2561           /* Itbl support may require additional care here.  */
2562           ++nops;
2563           if ((! mips_opts.mips16
2564                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2565                && (! cop_interlocks
2566                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2567               || (! hilo_interlocks
2568                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2569                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2570             ++nops;
2571
2572           if (prev_insn_unreordered)
2573             nops = 0;
2574         }
2575       else if ((! mips_opts.mips16
2576                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2577                 && (! cop_interlocks
2578                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2579                || (! hilo_interlocks
2580                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2581                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2582         {
2583           /* Itbl support may require additional care here.  */
2584           if (! prev_prev_insn_unreordered)
2585             ++nops;
2586         }
2587
2588       if (nops > 0)
2589         {
2590           struct insn_label_list *l;
2591
2592           if (insns)
2593             {
2594               /* Record the frag which holds the nop instructions, so
2595                  that we can remove them if we don't need them.  */
2596               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2597               prev_nop_frag = frag_now;
2598               prev_nop_frag_holds = nops;
2599               prev_nop_frag_required = 0;
2600               prev_nop_frag_since = 0;
2601             }
2602
2603           for (; nops > 0; --nops)
2604             emit_nop ();
2605
2606           if (insns)
2607             {
2608               /* Move on to a new frag, so that it is safe to simply
2609                  decrease the size of prev_nop_frag.  */
2610               frag_wane (frag_now);
2611               frag_new (0);
2612             }
2613
2614           for (l = insn_labels; l != NULL; l = l->next)
2615             {
2616               valueT val;
2617
2618               assert (S_GET_SEGMENT (l->label) == now_seg);
2619               symbol_set_frag (l->label, frag_now);
2620               val = (valueT) frag_now_fix ();
2621               /* mips16 text labels are stored as odd.  */
2622               if (mips_opts.mips16)
2623                 ++val;
2624               S_SET_VALUE (l->label, val);
2625             }
2626         }
2627     }
2628
2629   /* Mark instruction labels in mips16 mode.  */
2630   if (insns)
2631     mips16_mark_labels ();
2632
2633   mips_no_prev_insn (insns);
2634 }
2635
2636 /* Build an instruction created by a macro expansion.  This is passed
2637    a pointer to the count of instructions created so far, an
2638    expression, the name of the instruction to build, an operand format
2639    string, and corresponding arguments.  */
2640
2641 #ifdef USE_STDARG
2642 static void
2643 macro_build (char *place,
2644              int *counter,
2645              expressionS * ep,
2646              const char *name,
2647              const char *fmt,
2648              ...)
2649 #else
2650 static void
2651 macro_build (place, counter, ep, name, fmt, va_alist)
2652      char *place;
2653      int *counter;
2654      expressionS *ep;
2655      const char *name;
2656      const char *fmt;
2657      va_dcl
2658 #endif
2659 {
2660   struct mips_cl_insn insn;
2661   bfd_reloc_code_real_type r[3];
2662   va_list args;
2663
2664 #ifdef USE_STDARG
2665   va_start (args, fmt);
2666 #else
2667   va_start (args);
2668 #endif
2669
2670   /*
2671    * If the macro is about to expand into a second instruction,
2672    * print a warning if needed. We need to pass ip as a parameter
2673    * to generate a better warning message here...
2674    */
2675   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2676     as_warn (_("Macro instruction expanded into multiple instructions"));
2677
2678   /*
2679    * If the macro is about to expand into a second instruction,
2680    * and it is in a delay slot, print a warning.
2681    */
2682   if (place == NULL
2683       && *counter == 1
2684       && mips_opts.noreorder
2685       && (prev_prev_insn.insn_mo->pinfo
2686           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2687              | INSN_COND_BRANCH_LIKELY)) != 0)
2688     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2689
2690   if (place == NULL)
2691     ++*counter;         /* bump instruction counter */
2692
2693   if (mips_opts.mips16)
2694     {
2695       mips16_macro_build (place, counter, ep, name, fmt, args);
2696       va_end (args);
2697       return;
2698     }
2699
2700   r[0] = BFD_RELOC_UNUSED;
2701   r[1] = BFD_RELOC_UNUSED;
2702   r[2] = BFD_RELOC_UNUSED;
2703   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2704   assert (insn.insn_mo);
2705   assert (strcmp (name, insn.insn_mo->name) == 0);
2706
2707   /* Search until we get a match for NAME.  */
2708   while (1)
2709     {
2710       /* It is assumed here that macros will never generate 
2711          MDMX or MIPS-3D instructions.  */
2712       if (strcmp (fmt, insn.insn_mo->args) == 0
2713           && insn.insn_mo->pinfo != INSN_MACRO
2714           && OPCODE_IS_MEMBER (insn.insn_mo,
2715                                (mips_opts.isa
2716                                 | (mips_opts.mips16 ? INSN_MIPS16 : 0)),
2717                                mips_arch)
2718           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2719         break;
2720
2721       ++insn.insn_mo;
2722       assert (insn.insn_mo->name);
2723       assert (strcmp (name, insn.insn_mo->name) == 0);
2724     }
2725
2726   insn.insn_opcode = insn.insn_mo->match;
2727   for (;;)
2728     {
2729       switch (*fmt++)
2730         {
2731         case '\0':
2732           break;
2733
2734         case ',':
2735         case '(':
2736         case ')':
2737           continue;
2738
2739         case 't':
2740         case 'w':
2741         case 'E':
2742           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2743           continue;
2744
2745         case 'c':
2746           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2747           continue;
2748
2749         case 'T':
2750         case 'W':
2751           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2752           continue;
2753
2754         case 'd':
2755         case 'G':
2756           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2757           continue;
2758
2759         case 'U':
2760           {
2761             int tmp = va_arg (args, int);
2762
2763             insn.insn_opcode |= tmp << OP_SH_RT;
2764             insn.insn_opcode |= tmp << OP_SH_RD;
2765             continue;
2766           }
2767
2768         case 'V':
2769         case 'S':
2770           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2771           continue;
2772
2773         case 'z':
2774           continue;
2775
2776         case '<':
2777           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2778           continue;
2779
2780         case 'D':
2781           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2782           continue;
2783
2784         case 'B':
2785           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2786           continue;
2787
2788         case 'J':
2789           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2790           continue;
2791
2792         case 'q':
2793           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2794           continue;
2795
2796         case 'b':
2797         case 's':
2798         case 'r':
2799         case 'v':
2800           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2801           continue;
2802
2803         case 'i':
2804         case 'j':
2805         case 'o':
2806           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2807           assert (*r == BFD_RELOC_GPREL16
2808                   || *r == BFD_RELOC_MIPS_LITERAL
2809                   || *r == BFD_RELOC_MIPS_HIGHER
2810                   || *r == BFD_RELOC_HI16_S
2811                   || *r == BFD_RELOC_LO16
2812                   || *r == BFD_RELOC_MIPS_GOT16
2813                   || *r == BFD_RELOC_MIPS_CALL16
2814                   || *r == BFD_RELOC_MIPS_GOT_DISP
2815                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2816                   || *r == BFD_RELOC_MIPS_GOT_OFST
2817                   || *r == BFD_RELOC_MIPS_GOT_LO16
2818                   || *r == BFD_RELOC_MIPS_CALL_LO16
2819                   || (ep->X_op == O_subtract
2820                       && *r == BFD_RELOC_PCREL_LO16));
2821           continue;
2822
2823         case 'u':
2824           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2825           assert (ep != NULL
2826                   && (ep->X_op == O_constant
2827                       || (ep->X_op == O_symbol
2828                           && (*r == BFD_RELOC_MIPS_HIGHEST
2829                               || *r == BFD_RELOC_HI16_S
2830                               || *r == BFD_RELOC_HI16
2831                               || *r == BFD_RELOC_GPREL16
2832                               || *r == BFD_RELOC_MIPS_GOT_HI16
2833                               || *r == BFD_RELOC_MIPS_CALL_HI16))
2834                       || (ep->X_op == O_subtract
2835                           && *r == BFD_RELOC_PCREL_HI16_S)));
2836           continue;
2837
2838         case 'p':
2839           assert (ep != NULL);
2840           /*
2841            * This allows macro() to pass an immediate expression for
2842            * creating short branches without creating a symbol.
2843            * Note that the expression still might come from the assembly
2844            * input, in which case the value is not checked for range nor
2845            * is a relocation entry generated (yuck).
2846            */
2847           if (ep->X_op == O_constant)
2848             {
2849               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2850               ep = NULL;
2851             }
2852           else
2853             if (mips_pic == EMBEDDED_PIC)
2854               *r = BFD_RELOC_16_PCREL_S2;
2855             else
2856               *r = BFD_RELOC_16_PCREL;
2857           continue;
2858
2859         case 'a':
2860           assert (ep != NULL);
2861           *r = BFD_RELOC_MIPS_JMP;
2862           continue;
2863
2864         case 'C':
2865           insn.insn_opcode |= va_arg (args, unsigned long);
2866           continue;
2867
2868         default:
2869           internalError ();
2870         }
2871       break;
2872     }
2873   va_end (args);
2874   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2875
2876   append_insn (place, &insn, ep, r, false);
2877 }
2878
2879 static void
2880 mips16_macro_build (place, counter, ep, name, fmt, args)
2881      char *place;
2882      int *counter ATTRIBUTE_UNUSED;
2883      expressionS *ep;
2884      const char *name;
2885      const char *fmt;
2886      va_list args;
2887 {
2888   struct mips_cl_insn insn;
2889   bfd_reloc_code_real_type r[3]
2890     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2891
2892   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2893   assert (insn.insn_mo);
2894   assert (strcmp (name, insn.insn_mo->name) == 0);
2895
2896   while (strcmp (fmt, insn.insn_mo->args) != 0
2897          || insn.insn_mo->pinfo == INSN_MACRO)
2898     {
2899       ++insn.insn_mo;
2900       assert (insn.insn_mo->name);
2901       assert (strcmp (name, insn.insn_mo->name) == 0);
2902     }
2903
2904   insn.insn_opcode = insn.insn_mo->match;
2905   insn.use_extend = false;
2906
2907   for (;;)
2908     {
2909       int c;
2910
2911       c = *fmt++;
2912       switch (c)
2913         {
2914         case '\0':
2915           break;
2916
2917         case ',':
2918         case '(':
2919         case ')':
2920           continue;
2921
2922         case 'y':
2923         case 'w':
2924           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2925           continue;
2926
2927         case 'x':
2928         case 'v':
2929           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2930           continue;
2931
2932         case 'z':
2933           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2934           continue;
2935
2936         case 'Z':
2937           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2938           continue;
2939
2940         case '0':
2941         case 'S':
2942         case 'P':
2943         case 'R':
2944           continue;
2945
2946         case 'X':
2947           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2948           continue;
2949
2950         case 'Y':
2951           {
2952             int regno;
2953
2954             regno = va_arg (args, int);
2955             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2956             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2957           }
2958           continue;
2959
2960         case '<':
2961         case '>':
2962         case '4':
2963         case '5':
2964         case 'H':
2965         case 'W':
2966         case 'D':
2967         case 'j':
2968         case '8':
2969         case 'V':
2970         case 'C':
2971         case 'U':
2972         case 'k':
2973         case 'K':
2974         case 'p':
2975         case 'q':
2976           {
2977             assert (ep != NULL);
2978
2979             if (ep->X_op != O_constant)
2980               *r = (int) BFD_RELOC_UNUSED + c;
2981             else
2982               {
2983                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2984                               false, &insn.insn_opcode, &insn.use_extend,
2985                               &insn.extend);
2986                 ep = NULL;
2987                 *r = BFD_RELOC_UNUSED;
2988               }
2989           }
2990           continue;
2991
2992         case '6':
2993           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2994           continue;
2995         }
2996
2997       break;
2998     }
2999
3000   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3001
3002   append_insn (place, &insn, ep, r, false);
3003 }
3004
3005 /*
3006  * Generate a "jalr" instruction with a relocation hint to the called
3007  * function.  This occurs in NewABI PIC code.
3008  */
3009 static void
3010 macro_build_jalr (icnt, ep)
3011      int icnt;
3012      expressionS *ep;
3013 {
3014   char *f;
3015   
3016   if (HAVE_NEWABI)
3017     {
3018       frag_grow (4);
3019       f = frag_more (0);
3020     }
3021   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3022                RA, PIC_CALL_REG);
3023   if (HAVE_NEWABI)
3024     fix_new_exp (frag_now, f - frag_now->fr_literal,
3025                  0, ep, false, BFD_RELOC_MIPS_JALR);
3026 }
3027
3028 /*
3029  * Generate a "lui" instruction.
3030  */
3031 static void
3032 macro_build_lui (place, counter, ep, regnum)
3033      char *place;
3034      int *counter;
3035      expressionS *ep;
3036      int regnum;
3037 {
3038   expressionS high_expr;
3039   struct mips_cl_insn insn;
3040   bfd_reloc_code_real_type r[3]
3041     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3042   const char *name = "lui";
3043   const char *fmt = "t,u";
3044
3045   assert (! mips_opts.mips16);
3046
3047   if (place == NULL)
3048     high_expr = *ep;
3049   else
3050     {
3051       high_expr.X_op = O_constant;
3052       high_expr.X_add_number = ep->X_add_number;
3053     }
3054
3055   if (high_expr.X_op == O_constant)
3056     {
3057       /* we can compute the instruction now without a relocation entry */
3058       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3059                                 >> 16) & 0xffff;
3060       *r = BFD_RELOC_UNUSED;
3061     }
3062   else if (! HAVE_NEWABI)
3063     {
3064       assert (ep->X_op == O_symbol);
3065       /* _gp_disp is a special case, used from s_cpload.  */
3066       assert (mips_pic == NO_PIC
3067               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3068       *r = BFD_RELOC_HI16_S;
3069     }
3070
3071   /*
3072    * If the macro is about to expand into a second instruction,
3073    * print a warning if needed. We need to pass ip as a parameter
3074    * to generate a better warning message here...
3075    */
3076   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3077     as_warn (_("Macro instruction expanded into multiple instructions"));
3078
3079   if (place == NULL)
3080     ++*counter;         /* bump instruction counter */
3081
3082   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3083   assert (insn.insn_mo);
3084   assert (strcmp (name, insn.insn_mo->name) == 0);
3085   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3086
3087   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3088   if (*r == BFD_RELOC_UNUSED)
3089     {
3090       insn.insn_opcode |= high_expr.X_add_number;
3091       append_insn (place, &insn, NULL, r, false);
3092     }
3093   else
3094     append_insn (place, &insn, &high_expr, r, false);
3095 }
3096
3097 /* Generate a sequence of instructions to do a load or store from a constant
3098    offset off of a base register (breg) into/from a target register (treg),
3099    using AT if necessary.  */
3100 static void
3101 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3102      char *place;
3103      int *counter;
3104      expressionS *ep;
3105      const char *op;
3106      int treg, breg;
3107 {
3108   assert (ep->X_op == O_constant);
3109
3110   /* Right now, this routine can only handle signed 32-bit contants.  */
3111   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3112     as_warn (_("operand overflow"));
3113
3114   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3115     {
3116       /* Signed 16-bit offset will fit in the op.  Easy!  */
3117       macro_build (place, counter, ep, op, "t,o(b)", treg,
3118                    (int) BFD_RELOC_LO16, breg);
3119     }
3120   else
3121     {
3122       /* 32-bit offset, need multiple instructions and AT, like:
3123            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3124            addu     $tempreg,$tempreg,$breg
3125            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3126          to handle the complete offset.  */
3127       macro_build_lui (place, counter, ep, AT);
3128       if (place != NULL)
3129         place += 4;
3130       macro_build (place, counter, (expressionS *) NULL,
3131                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3132                    "d,v,t", AT, AT, breg);
3133       if (place != NULL)
3134         place += 4;
3135       macro_build (place, counter, ep, op, "t,o(b)", treg,
3136                    (int) BFD_RELOC_LO16, AT);
3137
3138       if (mips_opts.noat)
3139         as_warn (_("Macro used $at after \".set noat\""));
3140     }
3141 }
3142
3143 /*                      set_at()
3144  * Generates code to set the $at register to true (one)
3145  * if reg is less than the immediate expression.
3146  */
3147 static void
3148 set_at (counter, reg, unsignedp)
3149      int *counter;
3150      int reg;
3151      int unsignedp;
3152 {
3153   if (imm_expr.X_op == O_constant
3154       && imm_expr.X_add_number >= -0x8000
3155       && imm_expr.X_add_number < 0x8000)
3156     macro_build ((char *) NULL, counter, &imm_expr,
3157                  unsignedp ? "sltiu" : "slti",
3158                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3159   else
3160     {
3161       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3162       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3163                    unsignedp ? "sltu" : "slt",
3164                    "d,v,t", AT, reg, AT);
3165     }
3166 }
3167
3168 /* Warn if an expression is not a constant.  */
3169
3170 static void
3171 check_absolute_expr (ip, ex)
3172      struct mips_cl_insn *ip;
3173      expressionS *ex;
3174 {
3175   if (ex->X_op == O_big)
3176     as_bad (_("unsupported large constant"));
3177   else if (ex->X_op != O_constant)
3178     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3179 }
3180
3181 /* Count the leading zeroes by performing a binary chop. This is a
3182    bulky bit of source, but performance is a LOT better for the
3183    majority of values than a simple loop to count the bits:
3184        for (lcnt = 0; (lcnt < 32); lcnt++)
3185          if ((v) & (1 << (31 - lcnt)))
3186            break;
3187   However it is not code size friendly, and the gain will drop a bit
3188   on certain cached systems.
3189 */
3190 #define COUNT_TOP_ZEROES(v)             \
3191   (((v) & ~0xffff) == 0                 \
3192    ? ((v) & ~0xff) == 0                 \
3193      ? ((v) & ~0xf) == 0                \
3194        ? ((v) & ~0x3) == 0              \
3195          ? ((v) & ~0x1) == 0            \
3196            ? !(v)                       \
3197              ? 32                       \
3198              : 31                       \
3199            : 30                         \
3200          : ((v) & ~0x7) == 0            \
3201            ? 29                         \
3202            : 28                         \
3203        : ((v) & ~0x3f) == 0             \
3204          ? ((v) & ~0x1f) == 0           \
3205            ? 27                         \
3206            : 26                         \
3207          : ((v) & ~0x7f) == 0           \
3208            ? 25                         \
3209            : 24                         \
3210      : ((v) & ~0xfff) == 0              \
3211        ? ((v) & ~0x3ff) == 0            \
3212          ? ((v) & ~0x1ff) == 0          \
3213            ? 23                         \
3214            : 22                         \
3215          : ((v) & ~0x7ff) == 0          \
3216            ? 21                         \
3217            : 20                         \
3218        : ((v) & ~0x3fff) == 0           \
3219          ? ((v) & ~0x1fff) == 0         \
3220            ? 19                         \
3221            : 18                         \
3222          : ((v) & ~0x7fff) == 0         \
3223            ? 17                         \
3224            : 16                         \
3225    : ((v) & ~0xffffff) == 0             \
3226      ? ((v) & ~0xfffff) == 0            \
3227        ? ((v) & ~0x3ffff) == 0          \
3228          ? ((v) & ~0x1ffff) == 0        \
3229            ? 15                         \
3230            : 14                         \
3231          : ((v) & ~0x7ffff) == 0        \
3232            ? 13                         \
3233            : 12                         \
3234        : ((v) & ~0x3fffff) == 0         \
3235          ? ((v) & ~0x1fffff) == 0       \
3236            ? 11                         \
3237            : 10                         \
3238          : ((v) & ~0x7fffff) == 0       \
3239            ? 9                          \
3240            : 8                          \
3241      : ((v) & ~0xfffffff) == 0          \
3242        ? ((v) & ~0x3ffffff) == 0        \
3243          ? ((v) & ~0x1ffffff) == 0      \
3244            ? 7                          \
3245            : 6                          \
3246          : ((v) & ~0x7ffffff) == 0      \
3247            ? 5                          \
3248            : 4                          \
3249        : ((v) & ~0x3fffffff) == 0       \
3250          ? ((v) & ~0x1fffffff) == 0     \
3251            ? 3                          \
3252            : 2                          \
3253          : ((v) & ~0x7fffffff) == 0     \
3254            ? 1                          \
3255            : 0)
3256
3257 /*                      load_register()
3258  *  This routine generates the least number of instructions neccessary to load
3259  *  an absolute expression value into a register.
3260  */
3261 static void
3262 load_register (counter, reg, ep, dbl)
3263      int *counter;
3264      int reg;
3265      expressionS *ep;
3266      int dbl;
3267 {
3268   int freg;
3269   expressionS hi32, lo32;
3270
3271   if (ep->X_op != O_big)
3272     {
3273       assert (ep->X_op == O_constant);
3274       if (ep->X_add_number < 0x8000
3275           && (ep->X_add_number >= 0
3276               || (ep->X_add_number >= -0x8000
3277                   && (! dbl
3278                       || ! ep->X_unsigned
3279                       || sizeof (ep->X_add_number) > 4))))
3280         {
3281           /* We can handle 16 bit signed values with an addiu to
3282              $zero.  No need to ever use daddiu here, since $zero and
3283              the result are always correct in 32 bit mode.  */
3284           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3285                        (int) BFD_RELOC_LO16);
3286           return;
3287         }
3288       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3289         {
3290           /* We can handle 16 bit unsigned values with an ori to
3291              $zero.  */
3292           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3293                        (int) BFD_RELOC_LO16);
3294           return;
3295         }
3296       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3297                 && (! dbl
3298                     || ! ep->X_unsigned
3299                     || sizeof (ep->X_add_number) > 4
3300                     || (ep->X_add_number & 0x80000000) == 0))
3301                || ((HAVE_32BIT_GPRS || ! dbl)
3302                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3303                || (HAVE_32BIT_GPRS
3304                    && ! dbl
3305                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3306                        == ~ (offsetT) 0xffffffff)))
3307         {
3308           /* 32 bit values require an lui.  */
3309           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3310                        (int) BFD_RELOC_HI16);
3311           if ((ep->X_add_number & 0xffff) != 0)
3312             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3313                          (int) BFD_RELOC_LO16);
3314           return;
3315         }
3316     }
3317
3318   /* The value is larger than 32 bits.  */
3319
3320   if (HAVE_32BIT_GPRS)
3321     {
3322       as_bad (_("Number (0x%lx) larger than 32 bits"),
3323               (unsigned long) ep->X_add_number);
3324       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3325                    (int) BFD_RELOC_LO16);
3326       return;
3327     }
3328
3329   if (ep->X_op != O_big)
3330     {
3331       hi32 = *ep;
3332       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3333       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3334       hi32.X_add_number &= 0xffffffff;
3335       lo32 = *ep;
3336       lo32.X_add_number &= 0xffffffff;
3337     }
3338   else
3339     {
3340       assert (ep->X_add_number > 2);
3341       if (ep->X_add_number == 3)
3342         generic_bignum[3] = 0;
3343       else if (ep->X_add_number > 4)
3344         as_bad (_("Number larger than 64 bits"));
3345       lo32.X_op = O_constant;
3346       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3347       hi32.X_op = O_constant;
3348       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3349     }
3350
3351   if (hi32.X_add_number == 0)
3352     freg = 0;
3353   else
3354     {
3355       int shift, bit;
3356       unsigned long hi, lo;
3357
3358       if (hi32.X_add_number == (offsetT) 0xffffffff)
3359         {
3360           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3361             {
3362               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3363                            reg, 0, (int) BFD_RELOC_LO16);
3364               return;
3365             }
3366           if (lo32.X_add_number & 0x80000000)
3367             {
3368               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3369                            (int) BFD_RELOC_HI16);
3370               if (lo32.X_add_number & 0xffff)
3371                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3372                              reg, reg, (int) BFD_RELOC_LO16);
3373               return;
3374             }
3375         }
3376
3377       /* Check for 16bit shifted constant.  We know that hi32 is
3378          non-zero, so start the mask on the first bit of the hi32
3379          value.  */
3380       shift = 17;
3381       do
3382         {
3383           unsigned long himask, lomask;
3384
3385           if (shift < 32)
3386             {
3387               himask = 0xffff >> (32 - shift);
3388               lomask = (0xffff << shift) & 0xffffffff;
3389             }
3390           else
3391             {
3392               himask = 0xffff << (shift - 32);
3393               lomask = 0;
3394             }
3395           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3396               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3397             {
3398               expressionS tmp;
3399
3400               tmp.X_op = O_constant;
3401               if (shift < 32)
3402                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3403                                     | (lo32.X_add_number >> shift));
3404               else
3405                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3406               macro_build ((char *) NULL, counter, &tmp,
3407                            "ori", "t,r,i", reg, 0,
3408                            (int) BFD_RELOC_LO16);
3409               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3410                            (shift >= 32) ? "dsll32" : "dsll",
3411                            "d,w,<", reg, reg,
3412                            (shift >= 32) ? shift - 32 : shift);
3413               return;
3414             }
3415           ++shift;
3416         }
3417       while (shift <= (64 - 16));
3418
3419       /* Find the bit number of the lowest one bit, and store the
3420          shifted value in hi/lo.  */
3421       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3422       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3423       if (lo != 0)
3424         {
3425           bit = 0;
3426           while ((lo & 1) == 0)
3427             {
3428               lo >>= 1;
3429               ++bit;
3430             }
3431           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3432           hi >>= bit;
3433         }
3434       else
3435         {
3436           bit = 32;
3437           while ((hi & 1) == 0)
3438             {
3439               hi >>= 1;
3440               ++bit;
3441             }
3442           lo = hi;
3443           hi = 0;
3444         }
3445
3446       /* Optimize if the shifted value is a (power of 2) - 1.  */
3447       if ((hi == 0 && ((lo + 1) & lo) == 0)
3448           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3449         {
3450           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3451           if (shift != 0)
3452             {
3453               expressionS tmp;
3454
3455               /* This instruction will set the register to be all
3456                  ones.  */
3457               tmp.X_op = O_constant;
3458               tmp.X_add_number = (offsetT) -1;
3459               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3460                            reg, 0, (int) BFD_RELOC_LO16);
3461               if (bit != 0)
3462                 {
3463                   bit += shift;
3464                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3465                                (bit >= 32) ? "dsll32" : "dsll",
3466                                "d,w,<", reg, reg,
3467                                (bit >= 32) ? bit - 32 : bit);
3468                 }
3469               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3470                            (shift >= 32) ? "dsrl32" : "dsrl",
3471                            "d,w,<", reg, reg,
3472                            (shift >= 32) ? shift - 32 : shift);
3473               return;
3474             }
3475         }
3476
3477       /* Sign extend hi32 before calling load_register, because we can
3478          generally get better code when we load a sign extended value.  */
3479       if ((hi32.X_add_number & 0x80000000) != 0)
3480         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3481       load_register (counter, reg, &hi32, 0);
3482       freg = reg;
3483     }
3484   if ((lo32.X_add_number & 0xffff0000) == 0)
3485     {
3486       if (freg != 0)
3487         {
3488           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3489                        "dsll32", "d,w,<", reg, freg, 0);
3490           freg = reg;
3491         }
3492     }
3493   else
3494     {
3495       expressionS mid16;
3496
3497       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3498         {
3499           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3500                        (int) BFD_RELOC_HI16);
3501           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3502                        "dsrl32", "d,w,<", reg, reg, 0);
3503           return;
3504         }
3505
3506       if (freg != 0)
3507         {
3508           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3509                        "d,w,<", reg, freg, 16);
3510           freg = reg;
3511         }
3512       mid16 = lo32;
3513       mid16.X_add_number >>= 16;
3514       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3515                    freg, (int) BFD_RELOC_LO16);
3516       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3517                    "d,w,<", reg, reg, 16);
3518       freg = reg;
3519     }
3520   if ((lo32.X_add_number & 0xffff) != 0)
3521     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3522                  (int) BFD_RELOC_LO16);
3523 }
3524
3525 /* Load an address into a register.  */
3526
3527 static void
3528 load_address (counter, reg, ep, used_at)
3529      int *counter;
3530      int reg;
3531      expressionS *ep;
3532      int *used_at;
3533 {
3534   char *p = NULL;
3535
3536   if (ep->X_op != O_constant
3537       && ep->X_op != O_symbol)
3538     {
3539       as_bad (_("expression too complex"));
3540       ep->X_op = O_constant;
3541     }
3542
3543   if (ep->X_op == O_constant)
3544     {
3545       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3546       return;
3547     }
3548
3549   if (mips_pic == NO_PIC)
3550     {
3551       /* If this is a reference to a GP relative symbol, we want
3552            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3553          Otherwise we want
3554            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3555            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3556          If we have an addend, we always use the latter form.
3557
3558          With 64bit address space and a usable $at we want
3559            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3560            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3561            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3562            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3563            dsll32       $reg,0
3564            daddu        $reg,$reg,$at
3565
3566          If $at is already in use, we use an path which is suboptimal
3567          on superscalar processors.
3568            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3569            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3570            dsll         $reg,16
3571            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3572            dsll         $reg,16
3573            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3574        */
3575       if (HAVE_64BIT_ADDRESSES)
3576         {
3577           /* We don't do GP optimization for now because RELAX_ENCODE can't
3578              hold the data for such large chunks.  */
3579
3580           if (*used_at == 0 && ! mips_opts.noat)
3581             {
3582               macro_build (p, counter, ep, "lui", "t,u",
3583                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3584               macro_build (p, counter, ep, "lui", "t,u",
3585                            AT, (int) BFD_RELOC_HI16_S);
3586               macro_build (p, counter, ep, "daddiu", "t,r,j",
3587                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3588               macro_build (p, counter, ep, "daddiu", "t,r,j",
3589                            AT, AT, (int) BFD_RELOC_LO16);
3590               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3591                            "d,w,<", reg, reg, 0);
3592               macro_build (p, counter, (expressionS *) NULL, "daddu",
3593                            "d,v,t", reg, reg, AT);
3594               *used_at = 1;
3595             }
3596           else
3597             {
3598               macro_build (p, counter, ep, "lui", "t,u",
3599                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3600               macro_build (p, counter, ep, "daddiu", "t,r,j",
3601                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3602               macro_build (p, counter, (expressionS *) NULL, "dsll",
3603                            "d,w,<", reg, reg, 16);
3604               macro_build (p, counter, ep, "daddiu", "t,r,j",
3605                            reg, reg, (int) BFD_RELOC_HI16_S);
3606               macro_build (p, counter, (expressionS *) NULL, "dsll",
3607                            "d,w,<", reg, reg, 16);
3608               macro_build (p, counter, ep, "daddiu", "t,r,j",
3609                            reg, reg, (int) BFD_RELOC_LO16);
3610             }
3611         }
3612       else
3613         {
3614           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3615               && ! nopic_need_relax (ep->X_add_symbol, 1))
3616             {
3617               frag_grow (20);
3618               macro_build ((char *) NULL, counter, ep,
3619                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3620                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3621               p = frag_var (rs_machine_dependent, 8, 0,
3622                             RELAX_ENCODE (4, 8, 0, 4, 0,
3623                                           mips_opts.warn_about_macros),
3624                             ep->X_add_symbol, 0, NULL);
3625             }
3626           macro_build_lui (p, counter, ep, reg);
3627           if (p != NULL)
3628             p += 4;
3629           macro_build (p, counter, ep,
3630                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3631                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3632         }
3633     }
3634   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3635     {
3636       expressionS ex;
3637
3638       /* If this is a reference to an external symbol, we want
3639            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3640          Otherwise we want
3641            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3642            nop
3643            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3644          If we have NewABI, we want
3645            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3646          If there is a constant, it must be added in after.  */
3647       ex.X_add_number = ep->X_add_number;
3648       ep->X_add_number = 0;
3649       frag_grow (20);
3650       if (HAVE_NEWABI)
3651         {
3652           macro_build ((char *) NULL, counter, ep,
3653                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3654                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3655         }
3656       else
3657         {
3658           macro_build ((char *) NULL, counter, ep,
3659                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3660                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3661           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3662           p = frag_var (rs_machine_dependent, 4, 0,
3663                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3664                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3665           macro_build (p, counter, ep,
3666                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3667                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3668         }
3669
3670       if (ex.X_add_number != 0)
3671         {
3672           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3673             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3674           ex.X_op = O_constant;
3675           macro_build ((char *) NULL, counter, &ex,
3676                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3677                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3678         }
3679     }
3680   else if (mips_pic == SVR4_PIC)
3681     {
3682       expressionS ex;
3683       int off;
3684
3685       /* This is the large GOT case.  If this is a reference to an
3686          external symbol, we want
3687            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3688            addu         $reg,$reg,$gp
3689            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3690          Otherwise, for a reference to a local symbol, we want
3691            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3692            nop
3693            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3694          If we have NewABI, we want
3695            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3696            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3697          If there is a constant, it must be added in after.  */
3698       ex.X_add_number = ep->X_add_number;
3699       ep->X_add_number = 0;
3700       if (HAVE_NEWABI)
3701         {
3702           macro_build ((char *) NULL, counter, ep,
3703                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3704                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3705           macro_build (p, counter, ep,
3706                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3707                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3708         }
3709       else
3710         {
3711           if (reg_needs_delay (mips_gp_register))
3712             off = 4;
3713           else
3714             off = 0;
3715           frag_grow (32);
3716           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3717                        (int) BFD_RELOC_MIPS_GOT_HI16);
3718           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3719                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3720                        reg, mips_gp_register);
3721           macro_build ((char *) NULL, counter, ep,
3722                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3723                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3724           p = frag_var (rs_machine_dependent, 12 + off, 0,
3725                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3726                                       mips_opts.warn_about_macros),
3727                         ep->X_add_symbol, 0, NULL);
3728           if (off > 0)
3729             {
3730               /* We need a nop before loading from $gp.  This special
3731                  check is required because the lui which starts the main
3732                  instruction stream does not refer to $gp, and so will not
3733                  insert the nop which may be required.  */
3734               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3735                 p += 4;
3736             }
3737           macro_build (p, counter, ep,
3738                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3739                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3740           p += 4;
3741           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3742           p += 4;
3743           macro_build (p, counter, ep,
3744                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3745                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3746         }
3747
3748       if (ex.X_add_number != 0)
3749         {
3750           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3751             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3752           ex.X_op = O_constant;
3753           macro_build ((char *) NULL, counter, &ex,
3754                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3755                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3756         }
3757     }
3758   else if (mips_pic == EMBEDDED_PIC)
3759     {
3760       /* We always do
3761            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3762        */
3763       macro_build ((char *) NULL, counter, ep,
3764                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3765                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3766     }
3767   else
3768     abort ();
3769 }
3770
3771 /* Move the contents of register SOURCE into register DEST.  */
3772
3773 static void
3774 move_register (counter, dest, source)
3775      int *counter;
3776      int dest;
3777      int source;
3778 {
3779   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3780                HAVE_32BIT_GPRS ? "addu" : "daddu",
3781                "d,v,t", dest, source, 0);
3782 }
3783
3784 /*
3785  *                      Build macros
3786  *   This routine implements the seemingly endless macro or synthesized
3787  * instructions and addressing modes in the mips assembly language. Many
3788  * of these macros are simple and are similar to each other. These could
3789  * probably be handled by some kind of table or grammer aproach instead of
3790  * this verbose method. Others are not simple macros but are more like
3791  * optimizing code generation.
3792  *   One interesting optimization is when several store macros appear
3793  * consecutivly that would load AT with the upper half of the same address.
3794  * The ensuing load upper instructions are ommited. This implies some kind
3795  * of global optimization. We currently only optimize within a single macro.
3796  *   For many of the load and store macros if the address is specified as a
3797  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3798  * first load register 'at' with zero and use it as the base register. The
3799  * mips assembler simply uses register $zero. Just one tiny optimization
3800  * we're missing.
3801  */
3802 static void
3803 macro (ip)
3804      struct mips_cl_insn *ip;
3805 {
3806   register int treg, sreg, dreg, breg;
3807   int tempreg;
3808   int mask;
3809   int icnt = 0;
3810   int used_at = 0;
3811   expressionS expr1;
3812   const char *s;
3813   const char *s2;
3814   const char *fmt;
3815   int likely = 0;
3816   int dbl = 0;
3817   int coproc = 0;
3818   int lr = 0;
3819   int imm = 0;
3820   offsetT maxnum;
3821   int off;
3822   bfd_reloc_code_real_type r;
3823   int hold_mips_optimize;
3824
3825   assert (! mips_opts.mips16);
3826
3827   treg = (ip->insn_opcode >> 16) & 0x1f;
3828   dreg = (ip->insn_opcode >> 11) & 0x1f;
3829   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3830   mask = ip->insn_mo->mask;
3831
3832   expr1.X_op = O_constant;
3833   expr1.X_op_symbol = NULL;
3834   expr1.X_add_symbol = NULL;
3835   expr1.X_add_number = 1;
3836
3837   switch (mask)
3838     {
3839     case M_DABS:
3840       dbl = 1;
3841     case M_ABS:
3842       /* bgez $a0,.+12
3843          move v0,$a0
3844          sub v0,$zero,$a0
3845          */
3846
3847       mips_emit_delays (true);
3848       ++mips_opts.noreorder;
3849       mips_any_noreorder = 1;
3850
3851       expr1.X_add_number = 8;
3852       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3853       if (dreg == sreg)
3854         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3855                      0);
3856       else
3857         move_register (&icnt, dreg, sreg);
3858       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3859                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3860
3861       --mips_opts.noreorder;
3862       return;
3863
3864     case M_ADD_I:
3865       s = "addi";
3866       s2 = "add";
3867       goto do_addi;
3868     case M_ADDU_I:
3869       s = "addiu";
3870       s2 = "addu";
3871       goto do_addi;
3872     case M_DADD_I:
3873       dbl = 1;
3874       s = "daddi";
3875       s2 = "dadd";
3876       goto do_addi;
3877     case M_DADDU_I:
3878       dbl = 1;
3879       s = "daddiu";
3880       s2 = "daddu";
3881     do_addi:
3882       if (imm_expr.X_op == O_constant
3883           && imm_expr.X_add_number >= -0x8000
3884           && imm_expr.X_add_number < 0x8000)
3885         {
3886           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3887                        (int) BFD_RELOC_LO16);
3888           return;
3889         }
3890       load_register (&icnt, AT, &imm_expr, dbl);
3891       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3892                    treg, sreg, AT);
3893       break;
3894
3895     case M_AND_I:
3896       s = "andi";
3897       s2 = "and";
3898       goto do_bit;
3899     case M_OR_I:
3900       s = "ori";
3901       s2 = "or";
3902       goto do_bit;
3903     case M_NOR_I:
3904       s = "";
3905       s2 = "nor";
3906       goto do_bit;
3907     case M_XOR_I:
3908       s = "xori";
3909       s2 = "xor";
3910     do_bit:
3911       if (imm_expr.X_op == O_constant
3912           && imm_expr.X_add_number >= 0
3913           && imm_expr.X_add_number < 0x10000)
3914         {
3915           if (mask != M_NOR_I)
3916             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3917                          sreg, (int) BFD_RELOC_LO16);
3918           else
3919             {
3920               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3921                            treg, sreg, (int) BFD_RELOC_LO16);
3922               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3923                            "d,v,t", treg, treg, 0);
3924             }
3925           return;
3926         }
3927
3928       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3929       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3930                    treg, sreg, AT);
3931       break;
3932
3933     case M_BEQ_I:
3934       s = "beq";
3935       goto beq_i;
3936     case M_BEQL_I:
3937       s = "beql";
3938       likely = 1;
3939       goto beq_i;
3940     case M_BNE_I:
3941       s = "bne";
3942       goto beq_i;
3943     case M_BNEL_I:
3944       s = "bnel";
3945       likely = 1;
3946     beq_i:
3947       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3948         {
3949           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3950                        0);
3951           return;
3952         }
3953       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3954       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3955       break;
3956
3957     case M_BGEL:
3958       likely = 1;
3959     case M_BGE:
3960       if (treg == 0)
3961         {
3962           macro_build ((char *) NULL, &icnt, &offset_expr,
3963                        likely ? "bgezl" : "bgez", "s,p", sreg);
3964           return;
3965         }
3966       if (sreg == 0)
3967         {
3968           macro_build ((char *) NULL, &icnt, &offset_expr,
3969                        likely ? "blezl" : "blez", "s,p", treg);
3970           return;
3971         }
3972       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3973                    AT, sreg, treg);
3974       macro_build ((char *) NULL, &icnt, &offset_expr,
3975                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3976       break;
3977
3978     case M_BGTL_I:
3979       likely = 1;
3980     case M_BGT_I:
3981       /* check for > max integer */
3982       maxnum = 0x7fffffff;
3983       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3984         {
3985           maxnum <<= 16;
3986           maxnum |= 0xffff;
3987           maxnum <<= 16;
3988           maxnum |= 0xffff;
3989         }
3990       if (imm_expr.X_op == O_constant
3991           && imm_expr.X_add_number >= maxnum
3992           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3993         {
3994         do_false:
3995           /* result is always false */
3996           if (! likely)
3997             {
3998               if (warn_nops)
3999                 as_warn (_("Branch %s is always false (nop)"),
4000                          ip->insn_mo->name);
4001               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4002                            "", 0);
4003             }
4004           else
4005             {
4006               if (warn_nops)
4007                 as_warn (_("Branch likely %s is always false"),
4008                          ip->insn_mo->name);
4009               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4010                            "s,t,p", 0, 0);
4011             }
4012           return;
4013         }
4014       if (imm_expr.X_op != O_constant)
4015         as_bad (_("Unsupported large constant"));
4016       ++imm_expr.X_add_number;
4017       /* FALLTHROUGH */
4018     case M_BGE_I:
4019     case M_BGEL_I:
4020       if (mask == M_BGEL_I)
4021         likely = 1;
4022       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4023         {
4024           macro_build ((char *) NULL, &icnt, &offset_expr,
4025                        likely ? "bgezl" : "bgez", "s,p", sreg);
4026           return;
4027         }
4028       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4029         {
4030           macro_build ((char *) NULL, &icnt, &offset_expr,
4031                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4032           return;
4033         }
4034       maxnum = 0x7fffffff;
4035       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4036         {
4037           maxnum <<= 16;
4038           maxnum |= 0xffff;
4039           maxnum <<= 16;
4040           maxnum |= 0xffff;
4041         }
4042       maxnum = - maxnum - 1;
4043       if (imm_expr.X_op == O_constant
4044           && imm_expr.X_add_number <= maxnum
4045           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4046         {
4047         do_true:
4048           /* result is always true */
4049           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4050           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4051           return;
4052         }
4053       set_at (&icnt, sreg, 0);
4054       macro_build ((char *) NULL, &icnt, &offset_expr,
4055                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4056       break;
4057
4058     case M_BGEUL:
4059       likely = 1;
4060     case M_BGEU:
4061       if (treg == 0)
4062         goto do_true;
4063       if (sreg == 0)
4064         {
4065           macro_build ((char *) NULL, &icnt, &offset_expr,
4066                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4067           return;
4068         }
4069       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4070                    "d,v,t", AT, sreg, treg);
4071       macro_build ((char *) NULL, &icnt, &offset_expr,
4072                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4073       break;
4074
4075     case M_BGTUL_I:
4076       likely = 1;
4077     case M_BGTU_I:
4078       if (sreg == 0
4079           || (HAVE_32BIT_GPRS
4080               && imm_expr.X_op == O_constant
4081               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4082         goto do_false;
4083       if (imm_expr.X_op != O_constant)
4084         as_bad (_("Unsupported large constant"));
4085       ++imm_expr.X_add_number;
4086       /* FALLTHROUGH */
4087     case M_BGEU_I:
4088     case M_BGEUL_I:
4089       if (mask == M_BGEUL_I)
4090         likely = 1;
4091       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4092         goto do_true;
4093       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4094         {
4095           macro_build ((char *) NULL, &icnt, &offset_expr,
4096                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4097           return;
4098         }
4099       set_at (&icnt, sreg, 1);
4100       macro_build ((char *) NULL, &icnt, &offset_expr,
4101                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4102       break;
4103
4104     case M_BGTL:
4105       likely = 1;
4106     case M_BGT:
4107       if (treg == 0)
4108         {
4109           macro_build ((char *) NULL, &icnt, &offset_expr,
4110                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4111           return;
4112         }
4113       if (sreg == 0)
4114         {
4115           macro_build ((char *) NULL, &icnt, &offset_expr,
4116                        likely ? "bltzl" : "bltz", "s,p", treg);
4117           return;
4118         }
4119       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4120                    AT, treg, sreg);
4121       macro_build ((char *) NULL, &icnt, &offset_expr,
4122                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4123       break;
4124
4125     case M_BGTUL:
4126       likely = 1;
4127     case M_BGTU:
4128       if (treg == 0)
4129         {
4130           macro_build ((char *) NULL, &icnt, &offset_expr,
4131                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4132           return;
4133         }
4134       if (sreg == 0)
4135         goto do_false;
4136       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4137                    "d,v,t", AT, treg, sreg);
4138       macro_build ((char *) NULL, &icnt, &offset_expr,
4139                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4140       break;
4141
4142     case M_BLEL:
4143       likely = 1;
4144     case M_BLE:
4145       if (treg == 0)
4146         {
4147           macro_build ((char *) NULL, &icnt, &offset_expr,
4148                        likely ? "blezl" : "blez", "s,p", sreg);
4149           return;
4150         }
4151       if (sreg == 0)
4152         {
4153           macro_build ((char *) NULL, &icnt, &offset_expr,
4154                        likely ? "bgezl" : "bgez", "s,p", treg);
4155           return;
4156         }
4157       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4158                    AT, treg, sreg);
4159       macro_build ((char *) NULL, &icnt, &offset_expr,
4160                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4161       break;
4162
4163     case M_BLEL_I:
4164       likely = 1;
4165     case M_BLE_I:
4166       maxnum = 0x7fffffff;
4167       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4168         {
4169           maxnum <<= 16;
4170           maxnum |= 0xffff;
4171           maxnum <<= 16;
4172           maxnum |= 0xffff;
4173         }
4174       if (imm_expr.X_op == O_constant
4175           && imm_expr.X_add_number >= maxnum
4176           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4177         goto do_true;
4178       if (imm_expr.X_op != O_constant)
4179         as_bad (_("Unsupported large constant"));
4180       ++imm_expr.X_add_number;
4181       /* FALLTHROUGH */
4182     case M_BLT_I:
4183     case M_BLTL_I:
4184       if (mask == M_BLTL_I)
4185         likely = 1;
4186       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4187         {
4188           macro_build ((char *) NULL, &icnt, &offset_expr,
4189                        likely ? "bltzl" : "bltz", "s,p", sreg);
4190           return;
4191         }
4192       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4193         {
4194           macro_build ((char *) NULL, &icnt, &offset_expr,
4195                        likely ? "blezl" : "blez", "s,p", sreg);
4196           return;
4197         }
4198       set_at (&icnt, sreg, 0);
4199       macro_build ((char *) NULL, &icnt, &offset_expr,
4200                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4201       break;
4202
4203     case M_BLEUL:
4204       likely = 1;
4205     case M_BLEU:
4206       if (treg == 0)
4207         {
4208           macro_build ((char *) NULL, &icnt, &offset_expr,
4209                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4210           return;
4211         }
4212       if (sreg == 0)
4213         goto do_true;
4214       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4215                    "d,v,t", AT, treg, sreg);
4216       macro_build ((char *) NULL, &icnt, &offset_expr,
4217                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4218       break;
4219
4220     case M_BLEUL_I:
4221       likely = 1;
4222     case M_BLEU_I:
4223       if (sreg == 0
4224           || (HAVE_32BIT_GPRS
4225               && imm_expr.X_op == O_constant
4226               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4227         goto do_true;
4228       if (imm_expr.X_op != O_constant)
4229         as_bad (_("Unsupported large constant"));
4230       ++imm_expr.X_add_number;
4231       /* FALLTHROUGH */
4232     case M_BLTU_I:
4233     case M_BLTUL_I:
4234       if (mask == M_BLTUL_I)
4235         likely = 1;
4236       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4237         goto do_false;
4238       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4239         {
4240           macro_build ((char *) NULL, &icnt, &offset_expr,
4241                        likely ? "beql" : "beq",
4242                        "s,t,p", sreg, 0);
4243           return;
4244         }
4245       set_at (&icnt, sreg, 1);
4246       macro_build ((char *) NULL, &icnt, &offset_expr,
4247                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4248       break;
4249
4250     case M_BLTL:
4251       likely = 1;
4252     case M_BLT:
4253       if (treg == 0)
4254         {
4255           macro_build ((char *) NULL, &icnt, &offset_expr,
4256                        likely ? "bltzl" : "bltz", "s,p", sreg);
4257           return;
4258         }
4259       if (sreg == 0)
4260         {
4261           macro_build ((char *) NULL, &icnt, &offset_expr,
4262                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4263           return;
4264         }
4265       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4266                    AT, sreg, treg);
4267       macro_build ((char *) NULL, &icnt, &offset_expr,
4268                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4269       break;
4270
4271     case M_BLTUL:
4272       likely = 1;
4273     case M_BLTU:
4274       if (treg == 0)
4275         goto do_false;
4276       if (sreg == 0)
4277         {
4278           macro_build ((char *) NULL, &icnt, &offset_expr,
4279                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4280           return;
4281         }
4282       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4283                    "d,v,t", AT, sreg,
4284                    treg);
4285       macro_build ((char *) NULL, &icnt, &offset_expr,
4286                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4287       break;
4288
4289     case M_DDIV_3:
4290       dbl = 1;
4291     case M_DIV_3:
4292       s = "mflo";
4293       goto do_div3;
4294     case M_DREM_3:
4295       dbl = 1;
4296     case M_REM_3:
4297       s = "mfhi";
4298     do_div3:
4299       if (treg == 0)
4300         {
4301           as_warn (_("Divide by zero."));
4302           if (mips_trap)
4303             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4304                          "s,t,q", 0, 0, 7);
4305           else
4306             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4307                          "c", 7);
4308           return;
4309         }
4310
4311       mips_emit_delays (true);
4312       ++mips_opts.noreorder;
4313       mips_any_noreorder = 1;
4314       if (mips_trap)
4315         {
4316           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4317                        "s,t,q", treg, 0, 7);
4318           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4319                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4320         }
4321       else
4322         {
4323           expr1.X_add_number = 8;
4324           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4325           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4326                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4327           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4328                        "c", 7);
4329         }
4330       expr1.X_add_number = -1;
4331       macro_build ((char *) NULL, &icnt, &expr1,
4332                    dbl ? "daddiu" : "addiu",
4333                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4334       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4335       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4336       if (dbl)
4337         {
4338           expr1.X_add_number = 1;
4339           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4340                        (int) BFD_RELOC_LO16);
4341           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4342                        "d,w,<", AT, AT, 31);
4343         }
4344       else
4345         {
4346           expr1.X_add_number = 0x80000000;
4347           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4348                        (int) BFD_RELOC_HI16);
4349         }
4350       if (mips_trap)
4351         {
4352           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4353                        "s,t,q", sreg, AT, 6);
4354           /* We want to close the noreorder block as soon as possible, so
4355              that later insns are available for delay slot filling.  */
4356           --mips_opts.noreorder;
4357         }
4358       else
4359         {
4360           expr1.X_add_number = 8;
4361           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4362           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4363                        0);
4364
4365           /* We want to close the noreorder block as soon as possible, so
4366              that later insns are available for delay slot filling.  */
4367           --mips_opts.noreorder;
4368
4369           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4370                        "c", 6);
4371         }
4372       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4373       break;
4374
4375     case M_DIV_3I:
4376       s = "div";
4377       s2 = "mflo";
4378       goto do_divi;
4379     case M_DIVU_3I:
4380       s = "divu";
4381       s2 = "mflo";
4382       goto do_divi;
4383     case M_REM_3I:
4384       s = "div";
4385       s2 = "mfhi";
4386       goto do_divi;
4387     case M_REMU_3I:
4388       s = "divu";
4389       s2 = "mfhi";
4390       goto do_divi;
4391     case M_DDIV_3I:
4392       dbl = 1;
4393       s = "ddiv";
4394       s2 = "mflo";
4395       goto do_divi;
4396     case M_DDIVU_3I:
4397       dbl = 1;
4398       s = "ddivu";
4399       s2 = "mflo";
4400       goto do_divi;
4401     case M_DREM_3I:
4402       dbl = 1;
4403       s = "ddiv";
4404       s2 = "mfhi";
4405       goto do_divi;
4406     case M_DREMU_3I:
4407       dbl = 1;
4408       s = "ddivu";
4409       s2 = "mfhi";
4410     do_divi:
4411       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4412         {
4413           as_warn (_("Divide by zero."));
4414           if (mips_trap)
4415             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4416                          "s,t,q", 0, 0, 7);
4417           else
4418             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4419                          "c", 7);
4420           return;
4421         }
4422       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4423         {
4424           if (strcmp (s2, "mflo") == 0)
4425             move_register (&icnt, dreg, sreg);
4426           else
4427             move_register (&icnt, dreg, 0);
4428           return;
4429         }
4430       if (imm_expr.X_op == O_constant
4431           && imm_expr.X_add_number == -1
4432           && s[strlen (s) - 1] != 'u')
4433         {
4434           if (strcmp (s2, "mflo") == 0)
4435             {
4436               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4437                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4438             }
4439           else
4440             move_register (&icnt, dreg, 0);
4441           return;
4442         }
4443
4444       load_register (&icnt, AT, &imm_expr, dbl);
4445       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4446                    sreg, AT);
4447       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4448       break;
4449
4450     case M_DIVU_3:
4451       s = "divu";
4452       s2 = "mflo";
4453       goto do_divu3;
4454     case M_REMU_3:
4455       s = "divu";
4456       s2 = "mfhi";
4457       goto do_divu3;
4458     case M_DDIVU_3:
4459       s = "ddivu";
4460       s2 = "mflo";
4461       goto do_divu3;
4462     case M_DREMU_3:
4463       s = "ddivu";
4464       s2 = "mfhi";
4465     do_divu3:
4466       mips_emit_delays (true);
4467       ++mips_opts.noreorder;
4468       mips_any_noreorder = 1;
4469       if (mips_trap)
4470         {
4471           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4472                        "s,t,q", treg, 0, 7);
4473           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4474                        sreg, treg);
4475           /* We want to close the noreorder block as soon as possible, so
4476              that later insns are available for delay slot filling.  */
4477           --mips_opts.noreorder;
4478         }
4479       else
4480         {
4481           expr1.X_add_number = 8;
4482           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4483           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4484                        sreg, treg);
4485
4486           /* We want to close the noreorder block as soon as possible, so
4487              that later insns are available for delay slot filling.  */
4488           --mips_opts.noreorder;
4489           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4490                        "c", 7);
4491         }
4492       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4493       return;
4494
4495     case M_DLA_AB:
4496       dbl = 1;
4497     case M_LA_AB:
4498       /* Load the address of a symbol into a register.  If breg is not
4499          zero, we then add a base register to it.  */
4500
4501       if (dbl && HAVE_32BIT_GPRS)
4502         as_warn (_("dla used to load 32-bit register"));
4503
4504       if (! dbl && HAVE_64BIT_OBJECTS)
4505         as_warn (_("la used to load 64-bit address"));
4506
4507       if (offset_expr.X_op == O_constant
4508           && offset_expr.X_add_number >= -0x8000
4509           && offset_expr.X_add_number < 0x8000)
4510         {
4511           macro_build ((char *) NULL, &icnt, &offset_expr,
4512                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4513                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4514           return;
4515         }
4516
4517       if (treg == breg)
4518         {
4519           tempreg = AT;
4520           used_at = 1;
4521         }
4522       else
4523         {
4524           tempreg = treg;
4525           used_at = 0;
4526         }
4527
4528       /* When generating embedded PIC code, we permit expressions of
4529          the form
4530            la   $treg,foo-bar
4531            la   $treg,foo-bar($breg)
4532          where bar is an address in the current section.  These are used
4533          when getting the addresses of functions.  We don't permit
4534          X_add_number to be non-zero, because if the symbol is
4535          external the relaxing code needs to know that any addend is
4536          purely the offset to X_op_symbol.  */
4537       if (mips_pic == EMBEDDED_PIC
4538           && offset_expr.X_op == O_subtract
4539           && (symbol_constant_p (offset_expr.X_op_symbol)
4540               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4541               : (symbol_equated_p (offset_expr.X_op_symbol)
4542                  && (S_GET_SEGMENT
4543                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4544                       ->X_add_symbol)
4545                      == now_seg)))
4546           && (offset_expr.X_add_number == 0
4547               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4548         {
4549           if (breg == 0)
4550             {
4551               tempreg = treg;
4552               used_at = 0;
4553               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4554                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4555             }
4556           else
4557             {
4558               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4559                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4560               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4561                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4562                            "d,v,t", tempreg, tempreg, breg);
4563             }
4564           macro_build ((char *) NULL, &icnt, &offset_expr,
4565                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4566                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4567           if (! used_at)
4568             return;
4569           break;
4570         }
4571
4572       if (offset_expr.X_op != O_symbol
4573           && offset_expr.X_op != O_constant)
4574         {
4575           as_bad (_("expression too complex"));
4576           offset_expr.X_op = O_constant;
4577         }
4578
4579       if (offset_expr.X_op == O_constant)
4580         load_register (&icnt, tempreg, &offset_expr,
4581                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4582                         ? (dbl || HAVE_64BIT_ADDRESSES)
4583                         : HAVE_64BIT_ADDRESSES));
4584       else if (mips_pic == NO_PIC)
4585         {
4586           /* If this is a reference to a GP relative symbol, we want
4587                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4588              Otherwise we want
4589                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4590                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4591              If we have a constant, we need two instructions anyhow,
4592              so we may as well always use the latter form.
4593
4594             With 64bit address space and a usable $at we want
4595               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4596               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4597               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4598               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4599               dsll32    $tempreg,0
4600               daddu     $tempreg,$tempreg,$at
4601
4602             If $at is already in use, we use an path which is suboptimal
4603             on superscalar processors.
4604               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4605               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4606               dsll      $tempreg,16
4607               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4608               dsll      $tempreg,16
4609               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4610           */
4611           char *p = NULL;
4612           if (HAVE_64BIT_ADDRESSES)
4613             {
4614               /* We don't do GP optimization for now because RELAX_ENCODE can't
4615                  hold the data for such large chunks.  */
4616
4617               if (used_at == 0 && ! mips_opts.noat)
4618                 {
4619                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4620                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4621                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4622                                AT, (int) BFD_RELOC_HI16_S);
4623                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4624                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4625                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4626                                AT, AT, (int) BFD_RELOC_LO16);
4627                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4628                                "d,w,<", tempreg, tempreg, 0);
4629                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4630                                "d,v,t", tempreg, tempreg, AT);
4631                   used_at = 1;
4632                 }
4633               else
4634                 {
4635                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4636                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4637                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4638                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4639                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4640                                tempreg, tempreg, 16);
4641                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4642                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4643                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4644                                tempreg, tempreg, 16);
4645                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4646                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4647                 }
4648             }
4649           else
4650             {
4651               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4652                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4653                 {
4654                   frag_grow (20);
4655                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4656                                "t,r,j", tempreg, mips_gp_register,
4657                                (int) BFD_RELOC_GPREL16);
4658                   p = frag_var (rs_machine_dependent, 8, 0,
4659                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4660                                               mips_opts.warn_about_macros),
4661                                 offset_expr.X_add_symbol, 0, NULL);
4662                 }
4663               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4664               if (p != NULL)
4665                 p += 4;
4666               macro_build (p, &icnt, &offset_expr, "addiu",
4667                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4668             }
4669         }
4670       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4671         {
4672           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4673
4674           /* If this is a reference to an external symbol, and there
4675              is no constant, we want
4676                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4677              or if tempreg is PIC_CALL_REG
4678                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4679              For a local symbol, we want
4680                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4681                nop
4682                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4683
4684              If we have a small constant, and this is a reference to
4685              an external symbol, we want
4686                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4687                nop
4688                addiu    $tempreg,$tempreg,<constant>
4689              For a local symbol, we want the same instruction
4690              sequence, but we output a BFD_RELOC_LO16 reloc on the
4691              addiu instruction.
4692
4693              If we have a large constant, and this is a reference to
4694              an external symbol, we want
4695                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4696                lui      $at,<hiconstant>
4697                addiu    $at,$at,<loconstant>
4698                addu     $tempreg,$tempreg,$at
4699              For a local symbol, we want the same instruction
4700              sequence, but we output a BFD_RELOC_LO16 reloc on the
4701              addiu instruction.
4702
4703              For NewABI, we want for local or external data addresses
4704                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4705              For a local function symbol, we want
4706                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4707                nop
4708                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4709            */
4710
4711           expr1.X_add_number = offset_expr.X_add_number;
4712           offset_expr.X_add_number = 0;
4713           frag_grow (32);
4714           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4715             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4716           else if (HAVE_NEWABI)
4717             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4718           macro_build ((char *) NULL, &icnt, &offset_expr,
4719                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4720                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4721           if (expr1.X_add_number == 0)
4722             {
4723               int off;
4724               char *p;
4725
4726               if (breg == 0)
4727                 off = 0;
4728               else
4729                 {
4730                   /* We're going to put in an addu instruction using
4731                      tempreg, so we may as well insert the nop right
4732                      now.  */
4733                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4734                                "nop", "");
4735                   off = 4;
4736                 }
4737               p = frag_var (rs_machine_dependent, 8 - off, 0,
4738                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4739                                           (breg == 0
4740                                            ? mips_opts.warn_about_macros
4741                                            : 0)),
4742                             offset_expr.X_add_symbol, 0, NULL);
4743               if (breg == 0)
4744                 {
4745                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4746                   p += 4;
4747                 }
4748               macro_build (p, &icnt, &expr1,
4749                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4750                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4751               /* FIXME: If breg == 0, and the next instruction uses
4752                  $tempreg, then if this variant case is used an extra
4753                  nop will be generated.  */
4754             }
4755           else if (expr1.X_add_number >= -0x8000
4756                    && expr1.X_add_number < 0x8000)
4757             {
4758               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4759                            "nop", "");
4760               macro_build ((char *) NULL, &icnt, &expr1,
4761                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4762                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4763               frag_var (rs_machine_dependent, 0, 0,
4764                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4765                         offset_expr.X_add_symbol, 0, NULL);
4766             }
4767           else
4768             {
4769               int off1;
4770
4771               /* If we are going to add in a base register, and the
4772                  target register and the base register are the same,
4773                  then we are using AT as a temporary register.  Since
4774                  we want to load the constant into AT, we add our
4775                  current AT (from the global offset table) and the
4776                  register into the register now, and pretend we were
4777                  not using a base register.  */
4778               if (breg != treg)
4779                 off1 = 0;
4780               else
4781                 {
4782                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4783                                "nop", "");
4784                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4785                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4786                                "d,v,t", treg, AT, breg);
4787                   breg = 0;
4788                   tempreg = treg;
4789                   off1 = -8;
4790                 }
4791
4792               /* Set mips_optimize around the lui instruction to avoid
4793                  inserting an unnecessary nop after the lw.  */
4794               hold_mips_optimize = mips_optimize;
4795               mips_optimize = 2;
4796               macro_build_lui (NULL, &icnt, &expr1, AT);
4797               mips_optimize = hold_mips_optimize;
4798
4799               macro_build ((char *) NULL, &icnt, &expr1,
4800                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4801                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4802               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4803                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4804                            "d,v,t", tempreg, tempreg, AT);
4805               frag_var (rs_machine_dependent, 0, 0,
4806                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4807                         offset_expr.X_add_symbol, 0, NULL);
4808               used_at = 1;
4809             }
4810         }
4811       else if (mips_pic == SVR4_PIC)
4812         {
4813           int gpdel;
4814           char *p;
4815           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4816           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4817           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4818
4819           /* This is the large GOT case.  If this is a reference to an
4820              external symbol, and there is no constant, we want
4821                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4822                addu     $tempreg,$tempreg,$gp
4823                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4824              or if tempreg is PIC_CALL_REG
4825                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4826                addu     $tempreg,$tempreg,$gp
4827                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4828              For a local symbol, we want
4829                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4830                nop
4831                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4832
4833              If we have a small constant, and this is a reference to
4834              an external symbol, we want
4835                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4836                addu     $tempreg,$tempreg,$gp
4837                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4838                nop
4839                addiu    $tempreg,$tempreg,<constant>
4840              For a local symbol, we want
4841                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4842                nop
4843                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4844
4845              If we have a large constant, and this is a reference to
4846              an external symbol, we want
4847                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4848                addu     $tempreg,$tempreg,$gp
4849                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4850                lui      $at,<hiconstant>
4851                addiu    $at,$at,<loconstant>
4852                addu     $tempreg,$tempreg,$at
4853              For a local symbol, we want
4854                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4855                lui      $at,<hiconstant>
4856                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4857                addu     $tempreg,$tempreg,$at
4858
4859              For NewABI, we want for local data addresses
4860               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4861            */
4862
4863           expr1.X_add_number = offset_expr.X_add_number;
4864           offset_expr.X_add_number = 0;
4865           frag_grow (52);
4866           if (reg_needs_delay (mips_gp_register))
4867             gpdel = 4;
4868           else
4869             gpdel = 0;
4870           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4871             {
4872               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4873               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4874             }
4875           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4876                        tempreg, lui_reloc_type);
4877           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4878                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4879                        "d,v,t", tempreg, tempreg, mips_gp_register);
4880           macro_build ((char *) NULL, &icnt, &offset_expr,
4881                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4882                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4883           if (expr1.X_add_number == 0)
4884             {
4885               int off;
4886
4887               if (breg == 0)
4888                 off = 0;
4889               else
4890                 {
4891                   /* We're going to put in an addu instruction using
4892                      tempreg, so we may as well insert the nop right
4893                      now.  */
4894                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4895                                "nop", "");
4896                   off = 4;
4897                 }
4898
4899               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4900                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4901                                           8 + gpdel, 0,
4902                                           (breg == 0
4903                                            ? mips_opts.warn_about_macros
4904                                            : 0)),
4905                             offset_expr.X_add_symbol, 0, NULL);
4906             }
4907           else if (expr1.X_add_number >= -0x8000
4908                    && expr1.X_add_number < 0x8000)
4909             {
4910               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4911                            "nop", "");
4912               macro_build ((char *) NULL, &icnt, &expr1,
4913                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4914                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4915
4916               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4917                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4918                                           (breg == 0
4919                                            ? mips_opts.warn_about_macros
4920                                            : 0)),
4921                             offset_expr.X_add_symbol, 0, NULL);
4922             }
4923           else
4924             {
4925               int adj, dreg;
4926
4927               /* If we are going to add in a base register, and the
4928                  target register and the base register are the same,
4929                  then we are using AT as a temporary register.  Since
4930                  we want to load the constant into AT, we add our
4931                  current AT (from the global offset table) and the
4932                  register into the register now, and pretend we were
4933                  not using a base register.  */
4934               if (breg != treg)
4935                 {
4936                   adj = 0;
4937                   dreg = tempreg;
4938                 }
4939               else
4940                 {
4941                   assert (tempreg == AT);
4942                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4943                                "nop", "");
4944                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4945                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4946                                "d,v,t", treg, AT, breg);
4947                   dreg = treg;
4948                   adj = 8;
4949                 }
4950
4951               /* Set mips_optimize around the lui instruction to avoid
4952                  inserting an unnecessary nop after the lw.  */
4953               hold_mips_optimize = mips_optimize;
4954               mips_optimize = 2;
4955               macro_build_lui (NULL, &icnt, &expr1, AT);
4956               mips_optimize = hold_mips_optimize;
4957
4958               macro_build ((char *) NULL, &icnt, &expr1,
4959                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4960                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4961               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4962                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4963                            "d,v,t", dreg, dreg, AT);
4964
4965               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4966                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4967                                           8 + gpdel, 0,
4968                                           (breg == 0
4969                                            ? mips_opts.warn_about_macros
4970                                            : 0)),
4971                             offset_expr.X_add_symbol, 0, NULL);
4972
4973               used_at = 1;
4974             }
4975
4976           if (gpdel > 0)
4977             {
4978               /* This is needed because this instruction uses $gp, but
4979                  the first instruction on the main stream does not.  */
4980               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4981               p += 4;
4982             }
4983
4984           if (HAVE_NEWABI)
4985             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4986           macro_build (p, &icnt, &offset_expr,
4987                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4988                        "t,o(b)", tempreg,
4989                        local_reloc_type,
4990                        mips_gp_register);
4991           p += 4;
4992           if (expr1.X_add_number == 0 && HAVE_NEWABI)
4993             {
4994               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
4995             }
4996          else
4997            if (expr1.X_add_number >= -0x8000
4998               && expr1.X_add_number < 0x8000)
4999             {
5000               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5001               p += 4;
5002               macro_build (p, &icnt, &expr1,
5003                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5004                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5005               /* FIXME: If add_number is 0, and there was no base
5006                  register, the external symbol case ended with a load,
5007                  so if the symbol turns out to not be external, and
5008                  the next instruction uses tempreg, an unnecessary nop
5009                  will be inserted.  */
5010             }
5011           else
5012             {
5013               if (breg == treg)
5014                 {
5015                   /* We must add in the base register now, as in the
5016                      external symbol case.  */
5017                   assert (tempreg == AT);
5018                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5019                   p += 4;
5020                   macro_build (p, &icnt, (expressionS *) NULL,
5021                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5022                                "d,v,t", treg, AT, breg);
5023                   p += 4;
5024                   tempreg = treg;
5025                   /* We set breg to 0 because we have arranged to add
5026                      it in in both cases.  */
5027                   breg = 0;
5028                 }
5029
5030               macro_build_lui (p, &icnt, &expr1, AT);
5031               p += 4;
5032               macro_build (p, &icnt, &expr1,
5033                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5034                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5035               p += 4;
5036               macro_build (p, &icnt, (expressionS *) NULL,
5037                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5038                            "d,v,t", tempreg, tempreg, AT);
5039               p += 4;
5040             }
5041         }
5042       else if (mips_pic == EMBEDDED_PIC)
5043         {
5044           /* We use
5045                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5046              */
5047           macro_build ((char *) NULL, &icnt, &offset_expr,
5048                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5049                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5050         }
5051       else
5052         abort ();
5053
5054       if (breg != 0)
5055         {
5056           char *s;
5057
5058           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5059             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5060           else
5061             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5062
5063           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5064                        "d,v,t", treg, tempreg, breg);
5065         }
5066
5067       if (! used_at)
5068         return;
5069
5070       break;
5071
5072     case M_J_A:
5073       /* The j instruction may not be used in PIC code, since it
5074          requires an absolute address.  We convert it to a b
5075          instruction.  */
5076       if (mips_pic == NO_PIC)
5077         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5078       else
5079         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5080       return;
5081
5082       /* The jal instructions must be handled as macros because when
5083          generating PIC code they expand to multi-instruction
5084          sequences.  Normally they are simple instructions.  */
5085     case M_JAL_1:
5086       dreg = RA;
5087       /* Fall through.  */
5088     case M_JAL_2:
5089       if (mips_pic == NO_PIC
5090           || mips_pic == EMBEDDED_PIC)
5091         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5092                      "d,s", dreg, sreg);
5093       else if (mips_pic == SVR4_PIC)
5094         {
5095           if (sreg != PIC_CALL_REG)
5096             as_warn (_("MIPS PIC call to register other than $25"));
5097
5098           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5099                        "d,s", dreg, sreg);
5100           if (! HAVE_NEWABI)
5101             {
5102               if (mips_cprestore_offset < 0)
5103                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5104               else
5105                 {
5106                   if (! mips_frame_reg_valid)
5107                     {
5108                       as_warn (_("No .frame pseudo-op used in PIC code"));
5109                       /* Quiet this warning.  */
5110                       mips_frame_reg_valid = 1;
5111                     }
5112                   if (! mips_cprestore_valid)
5113                     {
5114                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5115                       /* Quiet this warning.  */
5116                       mips_cprestore_valid = 1;
5117                     }
5118                   expr1.X_add_number = mips_cprestore_offset;
5119                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5120                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5121                                                 mips_gp_register, mips_frame_reg);
5122                 }
5123             }
5124         }
5125       else
5126         abort ();
5127
5128       return;
5129
5130     case M_JAL_A:
5131       if (mips_pic == NO_PIC)
5132         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5133       else if (mips_pic == SVR4_PIC)
5134         {
5135           char *p;
5136
5137           /* If this is a reference to an external symbol, and we are
5138              using a small GOT, we want
5139                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5140                nop
5141                jalr     $ra,$25
5142                nop
5143                lw       $gp,cprestore($sp)
5144              The cprestore value is set using the .cprestore
5145              pseudo-op.  If we are using a big GOT, we want
5146                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5147                addu     $25,$25,$gp
5148                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5149                nop
5150                jalr     $ra,$25
5151                nop
5152                lw       $gp,cprestore($sp)
5153              If the symbol is not external, we want
5154                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5155                nop
5156                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5157                jalr     $ra,$25
5158                nop
5159                lw $gp,cprestore($sp)
5160              For NewABI, we want
5161                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5162                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5163            */
5164           if (HAVE_NEWABI)
5165             {
5166               macro_build ((char *) NULL, &icnt, &offset_expr,
5167                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5168                            "t,o(b)", PIC_CALL_REG,
5169                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5170               macro_build_jalr (icnt, &offset_expr);
5171             }
5172           else
5173             {
5174               frag_grow (40);
5175               if (! mips_big_got)
5176                 {
5177                   macro_build ((char *) NULL, &icnt, &offset_expr,
5178                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5179                                "t,o(b)", PIC_CALL_REG,
5180                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5181                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5182                                "nop", "");
5183                   p = frag_var (rs_machine_dependent, 4, 0,
5184                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5185                                 offset_expr.X_add_symbol, 0, NULL);
5186                 }
5187               else
5188                 {
5189                   int gpdel;
5190
5191                   if (reg_needs_delay (mips_gp_register))
5192                     gpdel = 4;
5193                   else
5194                     gpdel = 0;
5195                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5196                                "t,u", PIC_CALL_REG,
5197                                (int) BFD_RELOC_MIPS_CALL_HI16);
5198                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5199                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5200                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5201                                mips_gp_register);
5202                   macro_build ((char *) NULL, &icnt, &offset_expr,
5203                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5204                                "t,o(b)", PIC_CALL_REG,
5205                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5206                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5207                                "nop", "");
5208                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5209                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5210                                               8 + gpdel, 0, 0),
5211                                 offset_expr.X_add_symbol, 0, NULL);
5212                   if (gpdel > 0)
5213                     {
5214                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5215                       p += 4;
5216                     }
5217                   macro_build (p, &icnt, &offset_expr,
5218                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5219                                "t,o(b)", PIC_CALL_REG,
5220                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5221                   p += 4;
5222                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5223                   p += 4;
5224                 }
5225               macro_build (p, &icnt, &offset_expr,
5226                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5227                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5228                            (int) BFD_RELOC_LO16);
5229               macro_build_jalr (icnt, &offset_expr);
5230
5231               if (mips_cprestore_offset < 0)
5232                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5233               else
5234                 {
5235                   if (! mips_frame_reg_valid)
5236                     {
5237                       as_warn (_("No .frame pseudo-op used in PIC code"));
5238                       /* Quiet this warning.  */
5239                       mips_frame_reg_valid = 1;
5240                     }
5241                   if (! mips_cprestore_valid)
5242                     {
5243                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5244                       /* Quiet this warning.  */
5245                       mips_cprestore_valid = 1;
5246                     }
5247                   if (mips_opts.noreorder)
5248                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5249                                  "nop", "");
5250                   expr1.X_add_number = mips_cprestore_offset;
5251                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5252                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5253                                                 mips_gp_register, mips_frame_reg);
5254                 }
5255             }
5256         }
5257       else if (mips_pic == EMBEDDED_PIC)
5258         {
5259           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5260           /* The linker may expand the call to a longer sequence which
5261              uses $at, so we must break rather than return.  */
5262           break;
5263         }
5264       else
5265         abort ();
5266
5267       return;
5268
5269     case M_LB_AB:
5270       s = "lb";
5271       goto ld;
5272     case M_LBU_AB:
5273       s = "lbu";
5274       goto ld;
5275     case M_LH_AB:
5276       s = "lh";
5277       goto ld;
5278     case M_LHU_AB:
5279       s = "lhu";
5280       goto ld;
5281     case M_LW_AB:
5282       s = "lw";
5283       goto ld;
5284     case M_LWC0_AB:
5285       s = "lwc0";
5286       /* Itbl support may require additional care here.  */
5287       coproc = 1;
5288       goto ld;
5289     case M_LWC1_AB:
5290       s = "lwc1";
5291       /* Itbl support may require additional care here.  */
5292       coproc = 1;
5293       goto ld;
5294     case M_LWC2_AB:
5295       s = "lwc2";
5296       /* Itbl support may require additional care here.  */
5297       coproc = 1;
5298       goto ld;
5299     case M_LWC3_AB:
5300       s = "lwc3";
5301       /* Itbl support may require additional care here.  */
5302       coproc = 1;
5303       goto ld;
5304     case M_LWL_AB:
5305       s = "lwl";
5306       lr = 1;
5307       goto ld;
5308     case M_LWR_AB:
5309       s = "lwr";
5310       lr = 1;
5311       goto ld;
5312     case M_LDC1_AB:
5313       if (mips_arch == CPU_R4650)
5314         {
5315           as_bad (_("opcode not supported on this processor"));
5316           return;
5317         }
5318       s = "ldc1";
5319       /* Itbl support may require additional care here.  */
5320       coproc = 1;
5321       goto ld;
5322     case M_LDC2_AB:
5323       s = "ldc2";
5324       /* Itbl support may require additional care here.  */
5325       coproc = 1;
5326       goto ld;
5327     case M_LDC3_AB:
5328       s = "ldc3";
5329       /* Itbl support may require additional care here.  */
5330       coproc = 1;
5331       goto ld;
5332     case M_LDL_AB:
5333       s = "ldl";
5334       lr = 1;
5335       goto ld;
5336     case M_LDR_AB:
5337       s = "ldr";
5338       lr = 1;
5339       goto ld;
5340     case M_LL_AB:
5341       s = "ll";
5342       goto ld;
5343     case M_LLD_AB:
5344       s = "lld";
5345       goto ld;
5346     case M_LWU_AB:
5347       s = "lwu";
5348     ld:
5349       if (breg == treg || coproc || lr)
5350         {
5351           tempreg = AT;
5352           used_at = 1;
5353         }
5354       else
5355         {
5356           tempreg = treg;
5357           used_at = 0;
5358         }
5359       goto ld_st;
5360     case M_SB_AB:
5361       s = "sb";
5362       goto st;
5363     case M_SH_AB:
5364       s = "sh";
5365       goto st;
5366     case M_SW_AB:
5367       s = "sw";
5368       goto st;
5369     case M_SWC0_AB:
5370       s = "swc0";
5371       /* Itbl support may require additional care here.  */
5372       coproc = 1;
5373       goto st;
5374     case M_SWC1_AB:
5375       s = "swc1";
5376       /* Itbl support may require additional care here.  */
5377       coproc = 1;
5378       goto st;
5379     case M_SWC2_AB:
5380       s = "swc2";
5381       /* Itbl support may require additional care here.  */
5382       coproc = 1;
5383       goto st;
5384     case M_SWC3_AB:
5385       s = "swc3";
5386       /* Itbl support may require additional care here.  */
5387       coproc = 1;
5388       goto st;
5389     case M_SWL_AB:
5390       s = "swl";
5391       goto st;
5392     case M_SWR_AB:
5393       s = "swr";
5394       goto st;
5395     case M_SC_AB:
5396       s = "sc";
5397       goto st;
5398     case M_SCD_AB:
5399       s = "scd";
5400       goto st;
5401     case M_SDC1_AB:
5402       if (mips_arch == CPU_R4650)
5403         {
5404           as_bad (_("opcode not supported on this processor"));
5405           return;
5406         }
5407       s = "sdc1";
5408       coproc = 1;
5409       /* Itbl support may require additional care here.  */
5410       goto st;
5411     case M_SDC2_AB:
5412       s = "sdc2";
5413       /* Itbl support may require additional care here.  */
5414       coproc = 1;
5415       goto st;
5416     case M_SDC3_AB:
5417       s = "sdc3";
5418       /* Itbl support may require additional care here.  */
5419       coproc = 1;
5420       goto st;
5421     case M_SDL_AB:
5422       s = "sdl";
5423       goto st;
5424     case M_SDR_AB:
5425       s = "sdr";
5426     st:
5427       tempreg = AT;
5428       used_at = 1;
5429     ld_st:
5430       /* Itbl support may require additional care here.  */
5431       if (mask == M_LWC1_AB
5432           || mask == M_SWC1_AB
5433           || mask == M_LDC1_AB
5434           || mask == M_SDC1_AB
5435           || mask == M_L_DAB
5436           || mask == M_S_DAB)
5437         fmt = "T,o(b)";
5438       else if (coproc)
5439         fmt = "E,o(b)";
5440       else
5441         fmt = "t,o(b)";
5442
5443       /* For embedded PIC, we allow loads where the offset is calculated
5444          by subtracting a symbol in the current segment from an unknown
5445          symbol, relative to a base register, e.g.:
5446                 <op>    $treg, <sym>-<localsym>($breg)
5447          This is used by the compiler for switch statements.  */
5448       if (mips_pic == EMBEDDED_PIC
5449           && offset_expr.X_op == O_subtract
5450           && (symbol_constant_p (offset_expr.X_op_symbol)
5451               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5452               : (symbol_equated_p (offset_expr.X_op_symbol)
5453                  && (S_GET_SEGMENT
5454                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5455                       ->X_add_symbol)
5456                      == now_seg)))
5457           && breg != 0
5458           && (offset_expr.X_add_number == 0
5459               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5460         {
5461           /* For this case, we output the instructions:
5462                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5463                 addiu   $tempreg,$tempreg,$breg
5464                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5465              If the relocation would fit entirely in 16 bits, it would be
5466              nice to emit:
5467                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5468              instead, but that seems quite difficult.  */
5469           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5470                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5471           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5472                        ((bfd_arch_bits_per_address (stdoutput) == 32
5473                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5474                         ? "addu" : "daddu"),
5475                        "d,v,t", tempreg, tempreg, breg);
5476           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5477                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5478           if (! used_at)
5479             return;
5480           break;
5481         }
5482
5483       if (offset_expr.X_op != O_constant
5484           && offset_expr.X_op != O_symbol)
5485         {
5486           as_bad (_("expression too complex"));
5487           offset_expr.X_op = O_constant;
5488         }
5489
5490       /* A constant expression in PIC code can be handled just as it
5491          is in non PIC code.  */
5492       if (mips_pic == NO_PIC
5493           || offset_expr.X_op == O_constant)
5494         {
5495           char *p;
5496
5497           /* If this is a reference to a GP relative symbol, and there
5498              is no base register, we want
5499                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5500              Otherwise, if there is no base register, we want
5501                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5502                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5503              If we have a constant, we need two instructions anyhow,
5504              so we always use the latter form.
5505
5506              If we have a base register, and this is a reference to a
5507              GP relative symbol, we want
5508                addu     $tempreg,$breg,$gp
5509                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5510              Otherwise we want
5511                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5512                addu     $tempreg,$tempreg,$breg
5513                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5514              With a constant we always use the latter case.
5515
5516              With 64bit address space and no base register and $at usable,
5517              we want
5518                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5519                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5520                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5521                dsll32   $tempreg,0
5522                daddu    $tempreg,$at
5523                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5524              If we have a base register, we want
5525                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5526                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5527                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5528                daddu    $at,$breg
5529                dsll32   $tempreg,0
5530                daddu    $tempreg,$at
5531                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5532
5533              Without $at we can't generate the optimal path for superscalar
5534              processors here since this would require two temporary registers.
5535                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5536                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5537                dsll     $tempreg,16
5538                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5539                dsll     $tempreg,16
5540                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5541              If we have a base register, we want
5542                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5543                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5544                dsll     $tempreg,16
5545                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5546                dsll     $tempreg,16
5547                daddu    $tempreg,$tempreg,$breg
5548                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5549
5550              If we have 64-bit addresses, as an optimization, for
5551              addresses which are 32-bit constants (e.g. kseg0/kseg1
5552              addresses) we fall back to the 32-bit address generation
5553              mechanism since it is more efficient.  Note that due to
5554              the signed offset used by memory operations, the 32-bit
5555              range is shifted down by 32768 here.  This code should
5556              probably attempt to generate 64-bit constants more
5557              efficiently in general.
5558            */
5559           if (HAVE_64BIT_ADDRESSES
5560               && !(offset_expr.X_op == O_constant
5561                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5562             {
5563               p = NULL;
5564
5565               /* We don't do GP optimization for now because RELAX_ENCODE can't
5566                  hold the data for such large chunks.  */
5567
5568               if (used_at == 0 && ! mips_opts.noat)
5569                 {
5570                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5571                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5572                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5573                                AT, (int) BFD_RELOC_HI16_S);
5574                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5575                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5576                   if (breg != 0)
5577                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5578                                  "d,v,t", AT, AT, breg);
5579                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5580                                "d,w,<", tempreg, tempreg, 0);
5581                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5582                                "d,v,t", tempreg, tempreg, AT);
5583                   macro_build (p, &icnt, &offset_expr, s,
5584                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5585                   used_at = 1;
5586                 }
5587               else
5588                 {
5589                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5590                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5591                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5592                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5593                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5594                                "d,w,<", tempreg, tempreg, 16);
5595                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5596                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5597                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5598                                "d,w,<", tempreg, tempreg, 16);
5599                   if (breg != 0)
5600                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5601                                  "d,v,t", tempreg, tempreg, breg);
5602                   macro_build (p, &icnt, &offset_expr, s,
5603                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5604                 }
5605
5606               return;
5607             }
5608
5609           if (breg == 0)
5610             {
5611               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5612                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5613                 p = NULL;
5614               else
5615                 {
5616                   frag_grow (20);
5617                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5618                                treg, (int) BFD_RELOC_GPREL16,
5619                                mips_gp_register);
5620                   p = frag_var (rs_machine_dependent, 8, 0,
5621                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5622                                               (mips_opts.warn_about_macros
5623                                                || (used_at
5624                                                    && mips_opts.noat))),
5625                                 offset_expr.X_add_symbol, 0, NULL);
5626                   used_at = 0;
5627                 }
5628               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5629               if (p != NULL)
5630                 p += 4;
5631               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5632                            (int) BFD_RELOC_LO16, tempreg);
5633             }
5634           else
5635             {
5636               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5637                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5638                 p = NULL;
5639               else
5640                 {
5641                   frag_grow (28);
5642                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5643                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5644                                "d,v,t", tempreg, breg, mips_gp_register);
5645                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5646                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5647                   p = frag_var (rs_machine_dependent, 12, 0,
5648                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5649                                 offset_expr.X_add_symbol, 0, NULL);
5650                 }
5651               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5652               if (p != NULL)
5653                 p += 4;
5654               macro_build (p, &icnt, (expressionS *) NULL,
5655                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5656                            "d,v,t", tempreg, tempreg, breg);
5657               if (p != NULL)
5658                 p += 4;
5659               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5660                            (int) BFD_RELOC_LO16, tempreg);
5661             }
5662         }
5663       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5664         {
5665           char *p;
5666           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5667
5668           /* If this is a reference to an external symbol, we want
5669                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5670                nop
5671                <op>     $treg,0($tempreg)
5672              Otherwise we want
5673                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5674                nop
5675                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5676                <op>     $treg,0($tempreg)
5677              If we have NewABI, we want
5678                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5679              If there is a base register, we add it to $tempreg before
5680              the <op>.  If there is a constant, we stick it in the
5681              <op> instruction.  We don't handle constants larger than
5682              16 bits, because we have no way to load the upper 16 bits
5683              (actually, we could handle them for the subset of cases
5684              in which we are not using $at).  */
5685           assert (offset_expr.X_op == O_symbol);
5686           expr1.X_add_number = offset_expr.X_add_number;
5687           offset_expr.X_add_number = 0;
5688           if (HAVE_NEWABI)
5689             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5690           if (expr1.X_add_number < -0x8000
5691               || expr1.X_add_number >= 0x8000)
5692             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5693           frag_grow (20);
5694           macro_build ((char *) NULL, &icnt, &offset_expr,
5695                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5696                        (int) lw_reloc_type, mips_gp_register);
5697           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5698           p = frag_var (rs_machine_dependent, 4, 0,
5699                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5700                         offset_expr.X_add_symbol, 0, NULL);
5701           macro_build (p, &icnt, &offset_expr,
5702                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5703                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5704           if (breg != 0)
5705             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5706                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5707                          "d,v,t", tempreg, tempreg, breg);
5708           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5709                        (int) BFD_RELOC_LO16, tempreg);
5710         }
5711       else if (mips_pic == SVR4_PIC)
5712         {
5713           int gpdel;
5714           char *p;
5715
5716           /* If this is a reference to an external symbol, we want
5717                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5718                addu     $tempreg,$tempreg,$gp
5719                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5720                <op>     $treg,0($tempreg)
5721              Otherwise we want
5722                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5723                nop
5724                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5725                <op>     $treg,0($tempreg)
5726              If there is a base register, we add it to $tempreg before
5727              the <op>.  If there is a constant, we stick it in the
5728              <op> instruction.  We don't handle constants larger than
5729              16 bits, because we have no way to load the upper 16 bits
5730              (actually, we could handle them for the subset of cases
5731              in which we are not using $at).
5732
5733              For NewABI, we want
5734                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5735                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5736                <op>     $treg,0($tempreg)
5737            */
5738           assert (offset_expr.X_op == O_symbol);
5739           expr1.X_add_number = offset_expr.X_add_number;
5740           offset_expr.X_add_number = 0;
5741           if (expr1.X_add_number < -0x8000
5742               || expr1.X_add_number >= 0x8000)
5743             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5744           if (HAVE_NEWABI)
5745             {
5746               macro_build ((char *) NULL, &icnt, &offset_expr,
5747                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5748                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5749                            mips_gp_register);
5750               macro_build ((char *) NULL, &icnt, &offset_expr,
5751                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5752                            "t,r,j", tempreg, tempreg,
5753                            BFD_RELOC_MIPS_GOT_OFST);
5754               if (breg != 0)
5755                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5756                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5757                              "d,v,t", tempreg, tempreg, breg);
5758               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5759                            (int) BFD_RELOC_LO16, tempreg);
5760
5761               if (! used_at)
5762                 return;
5763
5764               break;
5765             }
5766           if (reg_needs_delay (mips_gp_register))
5767             gpdel = 4;
5768           else
5769             gpdel = 0;
5770           frag_grow (36);
5771           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5772                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5773           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5774                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5775                        "d,v,t", tempreg, tempreg, mips_gp_register);
5776           macro_build ((char *) NULL, &icnt, &offset_expr,
5777                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5778                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5779                        tempreg);
5780           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5781                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5782                         offset_expr.X_add_symbol, 0, NULL);
5783           if (gpdel > 0)
5784             {
5785               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5786               p += 4;
5787             }
5788           macro_build (p, &icnt, &offset_expr,
5789                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5790                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5791                        mips_gp_register);
5792           p += 4;
5793           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5794           p += 4;
5795           macro_build (p, &icnt, &offset_expr,
5796                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5797                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5798           if (breg != 0)
5799             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5800                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5801                          "d,v,t", tempreg, tempreg, breg);
5802           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5803                        (int) BFD_RELOC_LO16, tempreg);
5804         }
5805       else if (mips_pic == EMBEDDED_PIC)
5806         {
5807           /* If there is no base register, we want
5808                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5809              If there is a base register, we want
5810                addu     $tempreg,$breg,$gp
5811                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5812              */
5813           assert (offset_expr.X_op == O_symbol);
5814           if (breg == 0)
5815             {
5816               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5817                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5818               used_at = 0;
5819             }
5820           else
5821             {
5822               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5823                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5824                            "d,v,t", tempreg, breg, mips_gp_register);
5825               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5826                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5827             }
5828         }
5829       else
5830         abort ();
5831
5832       if (! used_at)
5833         return;
5834
5835       break;
5836
5837     case M_LI:
5838     case M_LI_S:
5839       load_register (&icnt, treg, &imm_expr, 0);
5840       return;
5841
5842     case M_DLI:
5843       load_register (&icnt, treg, &imm_expr, 1);
5844       return;
5845
5846     case M_LI_SS:
5847       if (imm_expr.X_op == O_constant)
5848         {
5849           load_register (&icnt, AT, &imm_expr, 0);
5850           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5851                        "mtc1", "t,G", AT, treg);
5852           break;
5853         }
5854       else
5855         {
5856           assert (offset_expr.X_op == O_symbol
5857                   && strcmp (segment_name (S_GET_SEGMENT
5858                                            (offset_expr.X_add_symbol)),
5859                              ".lit4") == 0
5860                   && offset_expr.X_add_number == 0);
5861           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5862                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
5863           return;
5864         }
5865
5866     case M_LI_D:
5867       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5868          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5869          order 32 bits of the value and the low order 32 bits are either
5870          zero or in OFFSET_EXPR.  */
5871       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5872         {
5873           if (HAVE_64BIT_GPRS)
5874             load_register (&icnt, treg, &imm_expr, 1);
5875           else
5876             {
5877               int hreg, lreg;
5878
5879               if (target_big_endian)
5880                 {
5881                   hreg = treg;
5882                   lreg = treg + 1;
5883                 }
5884               else
5885                 {
5886                   hreg = treg + 1;
5887                   lreg = treg;
5888                 }
5889
5890               if (hreg <= 31)
5891                 load_register (&icnt, hreg, &imm_expr, 0);
5892               if (lreg <= 31)
5893                 {
5894                   if (offset_expr.X_op == O_absent)
5895                     move_register (&icnt, lreg, 0);
5896                   else
5897                     {
5898                       assert (offset_expr.X_op == O_constant);
5899                       load_register (&icnt, lreg, &offset_expr, 0);
5900                     }
5901                 }
5902             }
5903           return;
5904         }
5905
5906       /* We know that sym is in the .rdata section.  First we get the
5907          upper 16 bits of the address.  */
5908       if (mips_pic == NO_PIC)
5909         {
5910           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5911         }
5912       else if (mips_pic == SVR4_PIC)
5913         {
5914           macro_build ((char *) NULL, &icnt, &offset_expr,
5915                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5916                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5917                        mips_gp_register);
5918         }
5919       else if (mips_pic == EMBEDDED_PIC)
5920         {
5921           /* For embedded PIC we pick up the entire address off $gp in
5922              a single instruction.  */
5923           macro_build ((char *) NULL, &icnt, &offset_expr,
5924                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5925                        mips_gp_register, (int) BFD_RELOC_GPREL16);
5926           offset_expr.X_op = O_constant;
5927           offset_expr.X_add_number = 0;
5928         }
5929       else
5930         abort ();
5931
5932       /* Now we load the register(s).  */
5933       if (HAVE_64BIT_GPRS)
5934         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5935                      treg, (int) BFD_RELOC_LO16, AT);
5936       else
5937         {
5938           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5939                        treg, (int) BFD_RELOC_LO16, AT);
5940           if (treg != RA)
5941             {
5942               /* FIXME: How in the world do we deal with the possible
5943                  overflow here?  */
5944               offset_expr.X_add_number += 4;
5945               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5946                            treg + 1, (int) BFD_RELOC_LO16, AT);
5947             }
5948         }
5949
5950       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5951          does not become a variant frag.  */
5952       frag_wane (frag_now);
5953       frag_new (0);
5954
5955       break;
5956
5957     case M_LI_DD:
5958       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5959          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5960          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5961          the value and the low order 32 bits are either zero or in
5962          OFFSET_EXPR.  */
5963       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5964         {
5965           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5966           if (HAVE_64BIT_FPRS)
5967             {
5968               assert (HAVE_64BIT_GPRS);
5969               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5970                            "dmtc1", "t,S", AT, treg);
5971             }
5972           else
5973             {
5974               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5975                            "mtc1", "t,G", AT, treg + 1);
5976               if (offset_expr.X_op == O_absent)
5977                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5978                              "mtc1", "t,G", 0, treg);
5979               else
5980                 {
5981                   assert (offset_expr.X_op == O_constant);
5982                   load_register (&icnt, AT, &offset_expr, 0);
5983                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5984                                "mtc1", "t,G", AT, treg);
5985                 }
5986             }
5987           break;
5988         }
5989
5990       assert (offset_expr.X_op == O_symbol
5991               && offset_expr.X_add_number == 0);
5992       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5993       if (strcmp (s, ".lit8") == 0)
5994         {
5995           if (mips_opts.isa != ISA_MIPS1)
5996             {
5997               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5998                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5999                            mips_gp_register);
6000               return;
6001             }
6002           breg = mips_gp_register;
6003           r = BFD_RELOC_MIPS_LITERAL;
6004           goto dob;
6005         }
6006       else
6007         {
6008           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6009           if (mips_pic == SVR4_PIC)
6010             macro_build ((char *) NULL, &icnt, &offset_expr,
6011                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6012                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6013                          mips_gp_register);
6014           else
6015             {
6016               /* FIXME: This won't work for a 64 bit address.  */
6017               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6018             }
6019
6020           if (mips_opts.isa != ISA_MIPS1)
6021             {
6022               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6023                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6024
6025               /* To avoid confusion in tc_gen_reloc, we must ensure
6026                  that this does not become a variant frag.  */
6027               frag_wane (frag_now);
6028               frag_new (0);
6029
6030               break;
6031             }
6032           breg = AT;
6033           r = BFD_RELOC_LO16;
6034           goto dob;
6035         }
6036
6037     case M_L_DOB:
6038       if (mips_arch == CPU_R4650)
6039         {
6040           as_bad (_("opcode not supported on this processor"));
6041           return;
6042         }
6043       /* Even on a big endian machine $fn comes before $fn+1.  We have
6044          to adjust when loading from memory.  */
6045       r = BFD_RELOC_LO16;
6046     dob:
6047       assert (mips_opts.isa == ISA_MIPS1);
6048       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6049                    target_big_endian ? treg + 1 : treg,
6050                    (int) r, breg);
6051       /* FIXME: A possible overflow which I don't know how to deal
6052          with.  */
6053       offset_expr.X_add_number += 4;
6054       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6055                    target_big_endian ? treg : treg + 1,
6056                    (int) r, breg);
6057
6058       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6059          does not become a variant frag.  */
6060       frag_wane (frag_now);
6061       frag_new (0);
6062
6063       if (breg != AT)
6064         return;
6065       break;
6066
6067     case M_L_DAB:
6068       /*
6069        * The MIPS assembler seems to check for X_add_number not
6070        * being double aligned and generating:
6071        *        lui     at,%hi(foo+1)
6072        *        addu    at,at,v1
6073        *        addiu   at,at,%lo(foo+1)
6074        *        lwc1    f2,0(at)
6075        *        lwc1    f3,4(at)
6076        * But, the resulting address is the same after relocation so why
6077        * generate the extra instruction?
6078        */
6079       if (mips_arch == CPU_R4650)
6080         {
6081           as_bad (_("opcode not supported on this processor"));
6082           return;
6083         }
6084       /* Itbl support may require additional care here.  */
6085       coproc = 1;
6086       if (mips_opts.isa != ISA_MIPS1)
6087         {
6088           s = "ldc1";
6089           goto ld;
6090         }
6091
6092       s = "lwc1";
6093       fmt = "T,o(b)";
6094       goto ldd_std;
6095
6096     case M_S_DAB:
6097       if (mips_arch == CPU_R4650)
6098         {
6099           as_bad (_("opcode not supported on this processor"));
6100           return;
6101         }
6102
6103       if (mips_opts.isa != ISA_MIPS1)
6104         {
6105           s = "sdc1";
6106           goto st;
6107         }
6108
6109       s = "swc1";
6110       fmt = "T,o(b)";
6111       /* Itbl support may require additional care here.  */
6112       coproc = 1;
6113       goto ldd_std;
6114
6115     case M_LD_AB:
6116       if (HAVE_64BIT_GPRS)
6117         {
6118           s = "ld";
6119           goto ld;
6120         }
6121
6122       s = "lw";
6123       fmt = "t,o(b)";
6124       goto ldd_std;
6125
6126     case M_SD_AB:
6127       if (HAVE_64BIT_GPRS)
6128         {
6129           s = "sd";
6130           goto st;
6131         }
6132
6133       s = "sw";
6134       fmt = "t,o(b)";
6135
6136     ldd_std:
6137       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6138          loads for the case of doing a pair of loads to simulate an 'ld'.
6139          This is not currently done by the compiler, and assembly coders
6140          writing embedded-pic code can cope.  */
6141
6142       if (offset_expr.X_op != O_symbol
6143           && offset_expr.X_op != O_constant)
6144         {
6145           as_bad (_("expression too complex"));
6146           offset_expr.X_op = O_constant;
6147         }
6148
6149       /* Even on a big endian machine $fn comes before $fn+1.  We have
6150          to adjust when loading from memory.  We set coproc if we must
6151          load $fn+1 first.  */
6152       /* Itbl support may require additional care here.  */
6153       if (! target_big_endian)
6154         coproc = 0;
6155
6156       if (mips_pic == NO_PIC
6157           || offset_expr.X_op == O_constant)
6158         {
6159           char *p;
6160
6161           /* If this is a reference to a GP relative symbol, we want
6162                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6163                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6164              If we have a base register, we use this
6165                addu     $at,$breg,$gp
6166                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6167                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6168              If this is not a GP relative symbol, we want
6169                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6170                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6171                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6172              If there is a base register, we add it to $at after the
6173              lui instruction.  If there is a constant, we always use
6174              the last case.  */
6175           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6176               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6177             {
6178               p = NULL;
6179               used_at = 1;
6180             }
6181           else
6182             {
6183               int off;
6184
6185               if (breg == 0)
6186                 {
6187                   frag_grow (28);
6188                   tempreg = mips_gp_register;
6189                   off = 0;
6190                   used_at = 0;
6191                 }
6192               else
6193                 {
6194                   frag_grow (36);
6195                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6196                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6197                                "d,v,t", AT, breg, mips_gp_register);
6198                   tempreg = AT;
6199                   off = 4;
6200                   used_at = 1;
6201                 }
6202
6203               /* Itbl support may require additional care here.  */
6204               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6205                            coproc ? treg + 1 : treg,
6206                            (int) BFD_RELOC_GPREL16, tempreg);
6207               offset_expr.X_add_number += 4;
6208
6209               /* Set mips_optimize to 2 to avoid inserting an
6210                  undesired nop.  */
6211               hold_mips_optimize = mips_optimize;
6212               mips_optimize = 2;
6213               /* Itbl support may require additional care here.  */
6214               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6215                            coproc ? treg : treg + 1,
6216                            (int) BFD_RELOC_GPREL16, tempreg);
6217               mips_optimize = hold_mips_optimize;
6218
6219               p = frag_var (rs_machine_dependent, 12 + off, 0,
6220                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6221                                           used_at && mips_opts.noat),
6222                             offset_expr.X_add_symbol, 0, NULL);
6223
6224               /* We just generated two relocs.  When tc_gen_reloc
6225                  handles this case, it will skip the first reloc and
6226                  handle the second.  The second reloc already has an
6227                  extra addend of 4, which we added above.  We must
6228                  subtract it out, and then subtract another 4 to make
6229                  the first reloc come out right.  The second reloc
6230                  will come out right because we are going to add 4 to
6231                  offset_expr when we build its instruction below.
6232
6233                  If we have a symbol, then we don't want to include
6234                  the offset, because it will wind up being included
6235                  when we generate the reloc.  */
6236
6237               if (offset_expr.X_op == O_constant)
6238                 offset_expr.X_add_number -= 8;
6239               else
6240                 {
6241                   offset_expr.X_add_number = -4;
6242                   offset_expr.X_op = O_constant;
6243                 }
6244             }
6245           macro_build_lui (p, &icnt, &offset_expr, AT);
6246           if (p != NULL)
6247             p += 4;
6248           if (breg != 0)
6249             {
6250               macro_build (p, &icnt, (expressionS *) NULL,
6251                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6252                            "d,v,t", AT, breg, AT);
6253               if (p != NULL)
6254                 p += 4;
6255             }
6256           /* Itbl support may require additional care here.  */
6257           macro_build (p, &icnt, &offset_expr, s, fmt,
6258                        coproc ? treg + 1 : treg,
6259                        (int) BFD_RELOC_LO16, AT);
6260           if (p != NULL)
6261             p += 4;
6262           /* FIXME: How do we handle overflow here?  */
6263           offset_expr.X_add_number += 4;
6264           /* Itbl support may require additional care here.  */
6265           macro_build (p, &icnt, &offset_expr, s, fmt,
6266                        coproc ? treg : treg + 1,
6267                        (int) BFD_RELOC_LO16, AT);
6268         }
6269       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6270         {
6271           int off;
6272
6273           /* If this is a reference to an external symbol, we want
6274                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6275                nop
6276                <op>     $treg,0($at)
6277                <op>     $treg+1,4($at)
6278              Otherwise we want
6279                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6280                nop
6281                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6282                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6283              If there is a base register we add it to $at before the
6284              lwc1 instructions.  If there is a constant we include it
6285              in the lwc1 instructions.  */
6286           used_at = 1;
6287           expr1.X_add_number = offset_expr.X_add_number;
6288           offset_expr.X_add_number = 0;
6289           if (expr1.X_add_number < -0x8000
6290               || expr1.X_add_number >= 0x8000 - 4)
6291             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6292           if (breg == 0)
6293             off = 0;
6294           else
6295             off = 4;
6296           frag_grow (24 + off);
6297           macro_build ((char *) NULL, &icnt, &offset_expr,
6298                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6299                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6300           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6301           if (breg != 0)
6302             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6303                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6304                          "d,v,t", AT, breg, AT);
6305           /* Itbl support may require additional care here.  */
6306           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6307                        coproc ? treg + 1 : treg,
6308                        (int) BFD_RELOC_LO16, AT);
6309           expr1.X_add_number += 4;
6310
6311           /* Set mips_optimize to 2 to avoid inserting an undesired
6312              nop.  */
6313           hold_mips_optimize = mips_optimize;
6314           mips_optimize = 2;
6315           /* Itbl support may require additional care here.  */
6316           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6317                        coproc ? treg : treg + 1,
6318                        (int) BFD_RELOC_LO16, AT);
6319           mips_optimize = hold_mips_optimize;
6320
6321           (void) frag_var (rs_machine_dependent, 0, 0,
6322                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6323                            offset_expr.X_add_symbol, 0, NULL);
6324         }
6325       else if (mips_pic == SVR4_PIC)
6326         {
6327           int gpdel, off;
6328           char *p;
6329
6330           /* If this is a reference to an external symbol, we want
6331                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6332                addu     $at,$at,$gp
6333                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6334                nop
6335                <op>     $treg,0($at)
6336                <op>     $treg+1,4($at)
6337              Otherwise we want
6338                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6339                nop
6340                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6341                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6342              If there is a base register we add it to $at before the
6343              lwc1 instructions.  If there is a constant we include it
6344              in the lwc1 instructions.  */
6345           used_at = 1;
6346           expr1.X_add_number = offset_expr.X_add_number;
6347           offset_expr.X_add_number = 0;
6348           if (expr1.X_add_number < -0x8000
6349               || expr1.X_add_number >= 0x8000 - 4)
6350             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6351           if (reg_needs_delay (mips_gp_register))
6352             gpdel = 4;
6353           else
6354             gpdel = 0;
6355           if (breg == 0)
6356             off = 0;
6357           else
6358             off = 4;
6359           frag_grow (56);
6360           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6361                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6362           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6363                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6364                        "d,v,t", AT, AT, mips_gp_register);
6365           macro_build ((char *) NULL, &icnt, &offset_expr,
6366                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6367                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6368           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6369           if (breg != 0)
6370             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6371                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6372                          "d,v,t", AT, breg, AT);
6373           /* Itbl support may require additional care here.  */
6374           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6375                        coproc ? treg + 1 : treg,
6376                        (int) BFD_RELOC_LO16, AT);
6377           expr1.X_add_number += 4;
6378
6379           /* Set mips_optimize to 2 to avoid inserting an undesired
6380              nop.  */
6381           hold_mips_optimize = mips_optimize;
6382           mips_optimize = 2;
6383           /* Itbl support may require additional care here.  */
6384           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6385                        coproc ? treg : treg + 1,
6386                        (int) BFD_RELOC_LO16, AT);
6387           mips_optimize = hold_mips_optimize;
6388           expr1.X_add_number -= 4;
6389
6390           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6391                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6392                                       8 + gpdel + off, 1, 0),
6393                         offset_expr.X_add_symbol, 0, NULL);
6394           if (gpdel > 0)
6395             {
6396               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6397               p += 4;
6398             }
6399           macro_build (p, &icnt, &offset_expr,
6400                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6401                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6402                        mips_gp_register);
6403           p += 4;
6404           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6405           p += 4;
6406           if (breg != 0)
6407             {
6408               macro_build (p, &icnt, (expressionS *) NULL,
6409                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6410                            "d,v,t", AT, breg, AT);
6411               p += 4;
6412             }
6413           /* Itbl support may require additional care here.  */
6414           macro_build (p, &icnt, &expr1, s, fmt,
6415                        coproc ? treg + 1 : treg,
6416                        (int) BFD_RELOC_LO16, AT);
6417           p += 4;
6418           expr1.X_add_number += 4;
6419
6420           /* Set mips_optimize to 2 to avoid inserting an undesired
6421              nop.  */
6422           hold_mips_optimize = mips_optimize;
6423           mips_optimize = 2;
6424           /* Itbl support may require additional care here.  */
6425           macro_build (p, &icnt, &expr1, s, fmt,
6426                        coproc ? treg : treg + 1,
6427                        (int) BFD_RELOC_LO16, AT);
6428           mips_optimize = hold_mips_optimize;
6429         }
6430       else if (mips_pic == EMBEDDED_PIC)
6431         {
6432           /* If there is no base register, we use
6433                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6434                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6435              If we have a base register, we use
6436                addu     $at,$breg,$gp
6437                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6438                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6439              */
6440           if (breg == 0)
6441             {
6442               tempreg = mips_gp_register;
6443               used_at = 0;
6444             }
6445           else
6446             {
6447               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6448                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6449                            "d,v,t", AT, breg, mips_gp_register);
6450               tempreg = AT;
6451               used_at = 1;
6452             }
6453
6454           /* Itbl support may require additional care here.  */
6455           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6456                        coproc ? treg + 1 : treg,
6457                        (int) BFD_RELOC_GPREL16, tempreg);
6458           offset_expr.X_add_number += 4;
6459           /* Itbl support may require additional care here.  */
6460           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6461                        coproc ? treg : treg + 1,
6462                        (int) BFD_RELOC_GPREL16, tempreg);
6463         }
6464       else
6465         abort ();
6466
6467       if (! used_at)
6468         return;
6469
6470       break;
6471
6472     case M_LD_OB:
6473       s = "lw";
6474       goto sd_ob;
6475     case M_SD_OB:
6476       s = "sw";
6477     sd_ob:
6478       assert (HAVE_32BIT_ADDRESSES);
6479       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6480                    (int) BFD_RELOC_LO16, breg);
6481       offset_expr.X_add_number += 4;
6482       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6483                    (int) BFD_RELOC_LO16, breg);
6484       return;
6485
6486    /* New code added to support COPZ instructions.
6487       This code builds table entries out of the macros in mip_opcodes.
6488       R4000 uses interlocks to handle coproc delays.
6489       Other chips (like the R3000) require nops to be inserted for delays.
6490
6491       FIXME: Currently, we require that the user handle delays.
6492       In order to fill delay slots for non-interlocked chips,
6493       we must have a way to specify delays based on the coprocessor.
6494       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6495       What are the side-effects of the cop instruction?
6496       What cache support might we have and what are its effects?
6497       Both coprocessor & memory require delays. how long???
6498       What registers are read/set/modified?
6499
6500       If an itbl is provided to interpret cop instructions,
6501       this knowledge can be encoded in the itbl spec.  */
6502
6503     case M_COP0:
6504       s = "c0";
6505       goto copz;
6506     case M_COP1:
6507       s = "c1";
6508       goto copz;
6509     case M_COP2:
6510       s = "c2";
6511       goto copz;
6512     case M_COP3:
6513       s = "c3";
6514     copz:
6515       /* For now we just do C (same as Cz).  The parameter will be
6516          stored in insn_opcode by mips_ip.  */
6517       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6518                    ip->insn_opcode);
6519       return;
6520
6521     case M_MOVE:
6522       move_register (&icnt, dreg, sreg);
6523       return;
6524
6525 #ifdef LOSING_COMPILER
6526     default:
6527       /* Try and see if this is a new itbl instruction.
6528          This code builds table entries out of the macros in mip_opcodes.
6529          FIXME: For now we just assemble the expression and pass it's
6530          value along as a 32-bit immediate.
6531          We may want to have the assembler assemble this value,
6532          so that we gain the assembler's knowledge of delay slots,
6533          symbols, etc.
6534          Would it be more efficient to use mask (id) here? */
6535       if (itbl_have_entries
6536           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6537         {
6538           s = ip->insn_mo->name;
6539           s2 = "cop3";
6540           coproc = ITBL_DECODE_PNUM (immed_expr);;
6541           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6542           return;
6543         }
6544       macro2 (ip);
6545       return;
6546     }
6547   if (mips_opts.noat)
6548     as_warn (_("Macro used $at after \".set noat\""));
6549 }
6550
6551 static void
6552 macro2 (ip)
6553      struct mips_cl_insn *ip;
6554 {
6555   register int treg, sreg, dreg, breg;
6556   int tempreg;
6557   int mask;
6558   int icnt = 0;
6559   int used_at;
6560   expressionS expr1;
6561   const char *s;
6562   const char *s2;
6563   const char *fmt;
6564   int likely = 0;
6565   int dbl = 0;
6566   int coproc = 0;
6567   int lr = 0;
6568   int imm = 0;
6569   int off;
6570   offsetT maxnum;
6571   bfd_reloc_code_real_type r;
6572   char *p;
6573
6574   treg = (ip->insn_opcode >> 16) & 0x1f;
6575   dreg = (ip->insn_opcode >> 11) & 0x1f;
6576   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6577   mask = ip->insn_mo->mask;
6578
6579   expr1.X_op = O_constant;
6580   expr1.X_op_symbol = NULL;
6581   expr1.X_add_symbol = NULL;
6582   expr1.X_add_number = 1;
6583
6584   switch (mask)
6585     {
6586 #endif /* LOSING_COMPILER */
6587
6588     case M_DMUL:
6589       dbl = 1;
6590     case M_MUL:
6591       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6592                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6593       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6594                    dreg);
6595       return;
6596
6597     case M_DMUL_I:
6598       dbl = 1;
6599     case M_MUL_I:
6600       /* The MIPS assembler some times generates shifts and adds.  I'm
6601          not trying to be that fancy. GCC should do this for us
6602          anyway.  */
6603       load_register (&icnt, AT, &imm_expr, dbl);
6604       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6605                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6606       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6607                    dreg);
6608       break;
6609
6610     case M_DMULO_I:
6611       dbl = 1;
6612     case M_MULO_I:
6613       imm = 1;
6614       goto do_mulo;
6615
6616     case M_DMULO:
6617       dbl = 1;
6618     case M_MULO:
6619     do_mulo:
6620       mips_emit_delays (true);
6621       ++mips_opts.noreorder;
6622       mips_any_noreorder = 1;
6623       if (imm)
6624         load_register (&icnt, AT, &imm_expr, dbl);
6625       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6626                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6627       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6628                    dreg);
6629       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6630                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6631       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6632                    AT);
6633       if (mips_trap)
6634         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6635                      "s,t,q", dreg, AT, 6);
6636       else
6637         {
6638           expr1.X_add_number = 8;
6639           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6640                        AT);
6641           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6642                        0);
6643           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6644                        "c", 6);
6645         }
6646       --mips_opts.noreorder;
6647       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6648       break;
6649
6650     case M_DMULOU_I:
6651       dbl = 1;
6652     case M_MULOU_I:
6653       imm = 1;
6654       goto do_mulou;
6655
6656     case M_DMULOU:
6657       dbl = 1;
6658     case M_MULOU:
6659     do_mulou:
6660       mips_emit_delays (true);
6661       ++mips_opts.noreorder;
6662       mips_any_noreorder = 1;
6663       if (imm)
6664         load_register (&icnt, AT, &imm_expr, dbl);
6665       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6666                    dbl ? "dmultu" : "multu",
6667                    "s,t", sreg, imm ? AT : treg);
6668       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6669                    AT);
6670       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6671                    dreg);
6672       if (mips_trap)
6673         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6674                      "s,t,q", AT, 0, 6);
6675       else
6676         {
6677           expr1.X_add_number = 8;
6678           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6679           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6680                        0);
6681           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6682                        "c", 6);
6683         }
6684       --mips_opts.noreorder;
6685       break;
6686
6687     case M_DROL:
6688       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6689                    "d,v,t", AT, 0, treg);
6690       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6691                    "d,t,s", AT, sreg, AT);
6692       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
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_ROL:
6699       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6700                    "d,v,t", AT, 0, treg);
6701       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6702                    "d,t,s", AT, sreg, AT);
6703       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
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_DROL_I:
6710       {
6711         unsigned int rot;
6712
6713         if (imm_expr.X_op != O_constant)
6714           as_bad (_("rotate count too large"));
6715         rot = imm_expr.X_add_number & 0x3f;
6716         if (rot == 0)
6717           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6718                        "d,w,<", dreg, sreg, 0);
6719         else
6720           {
6721             char *l, *r;
6722
6723             l = (rot < 0x20) ? "dsll" : "dsll32";
6724             r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6725             rot &= 0x1f;
6726             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6727                          "d,w,<", AT, sreg, rot);
6728             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6729                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6730             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6731                          "d,v,t", dreg, dreg, AT);
6732           }
6733       }
6734       break;
6735
6736     case M_ROL_I:
6737       {
6738         unsigned int rot;
6739
6740         if (imm_expr.X_op != O_constant)
6741           as_bad (_("rotate count too large"));
6742         rot = imm_expr.X_add_number & 0x1f;
6743         if (rot == 0)
6744           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6745                        "d,w,<", dreg, sreg, 0);
6746         else
6747           {
6748             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6749                          "d,w,<", AT, sreg, rot);
6750             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6751                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6752             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6753                          "d,v,t", dreg, dreg, AT);
6754           }
6755       }
6756       break;
6757
6758     case M_DROR:
6759       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6760                    "d,v,t", AT, 0, treg);
6761       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6762                    "d,t,s", AT, sreg, AT);
6763       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6764                    "d,t,s", dreg, sreg, treg);
6765       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6766                    "d,v,t", dreg, dreg, AT);
6767       break;
6768
6769     case M_ROR:
6770       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6771                    "d,v,t", AT, 0, treg);
6772       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6773                    "d,t,s", AT, sreg, AT);
6774       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6775                    "d,t,s", dreg, sreg, treg);
6776       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6777                    "d,v,t", dreg, dreg, AT);
6778       break;
6779
6780     case M_DROR_I:
6781       {
6782         unsigned int rot;
6783
6784         if (imm_expr.X_op != O_constant)
6785           as_bad (_("rotate count too large"));
6786         rot = imm_expr.X_add_number & 0x3f;
6787         if (rot == 0)
6788           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6789                        "d,w,<", dreg, sreg, 0);
6790         else
6791           {
6792             char *l, *r;
6793
6794             r = (rot < 0x20) ? "dsrl" : "dsrl32";
6795             l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6796             rot &= 0x1f;
6797             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6798                          "d,w,<", AT, sreg, rot);
6799             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6800                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6801             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6802                          "d,v,t", dreg, dreg, AT);
6803           }
6804       }
6805       break;
6806
6807     case M_ROR_I:
6808       {
6809         unsigned int rot;
6810
6811         if (imm_expr.X_op != O_constant)
6812           as_bad (_("rotate count too large"));
6813         rot = imm_expr.X_add_number & 0x1f;
6814         if (rot == 0)
6815           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6816                        "d,w,<", dreg, sreg, 0);
6817         else
6818           {
6819             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6820                          "d,w,<", AT, sreg, rot);
6821             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6822                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6823             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6824                          "d,v,t", dreg, dreg, AT);
6825           }
6826       }
6827       break;
6828
6829     case M_S_DOB:
6830       if (mips_arch == CPU_R4650)
6831         {
6832           as_bad (_("opcode not supported on this processor"));
6833           return;
6834         }
6835       assert (mips_opts.isa == ISA_MIPS1);
6836       /* Even on a big endian machine $fn comes before $fn+1.  We have
6837          to adjust when storing to memory.  */
6838       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6839                    target_big_endian ? treg + 1 : treg,
6840                    (int) BFD_RELOC_LO16, breg);
6841       offset_expr.X_add_number += 4;
6842       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6843                    target_big_endian ? treg : treg + 1,
6844                    (int) BFD_RELOC_LO16, breg);
6845       return;
6846
6847     case M_SEQ:
6848       if (sreg == 0)
6849         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6850                      treg, (int) BFD_RELOC_LO16);
6851       else if (treg == 0)
6852         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6853                      sreg, (int) BFD_RELOC_LO16);
6854       else
6855         {
6856           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6857                        "d,v,t", dreg, sreg, treg);
6858           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6859                        dreg, (int) BFD_RELOC_LO16);
6860         }
6861       return;
6862
6863     case M_SEQ_I:
6864       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6865         {
6866           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6867                        sreg, (int) BFD_RELOC_LO16);
6868           return;
6869         }
6870       if (sreg == 0)
6871         {
6872           as_warn (_("Instruction %s: result is always false"),
6873                    ip->insn_mo->name);
6874           move_register (&icnt, dreg, 0);
6875           return;
6876         }
6877       if (imm_expr.X_op == O_constant
6878           && imm_expr.X_add_number >= 0
6879           && imm_expr.X_add_number < 0x10000)
6880         {
6881           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6882                        sreg, (int) BFD_RELOC_LO16);
6883           used_at = 0;
6884         }
6885       else if (imm_expr.X_op == O_constant
6886                && imm_expr.X_add_number > -0x8000
6887                && imm_expr.X_add_number < 0)
6888         {
6889           imm_expr.X_add_number = -imm_expr.X_add_number;
6890           macro_build ((char *) NULL, &icnt, &imm_expr,
6891                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6892                        "t,r,j", dreg, sreg,
6893                        (int) BFD_RELOC_LO16);
6894           used_at = 0;
6895         }
6896       else
6897         {
6898           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6899           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6900                        "d,v,t", dreg, sreg, AT);
6901           used_at = 1;
6902         }
6903       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6904                    (int) BFD_RELOC_LO16);
6905       if (used_at)
6906         break;
6907       return;
6908
6909     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6910       s = "slt";
6911       goto sge;
6912     case M_SGEU:
6913       s = "sltu";
6914     sge:
6915       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6916                    dreg, sreg, treg);
6917       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6918                    (int) BFD_RELOC_LO16);
6919       return;
6920
6921     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6922     case M_SGEU_I:
6923       if (imm_expr.X_op == O_constant
6924           && imm_expr.X_add_number >= -0x8000
6925           && imm_expr.X_add_number < 0x8000)
6926         {
6927           macro_build ((char *) NULL, &icnt, &imm_expr,
6928                        mask == M_SGE_I ? "slti" : "sltiu",
6929                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6930           used_at = 0;
6931         }
6932       else
6933         {
6934           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6935           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6936                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6937                        AT);
6938           used_at = 1;
6939         }
6940       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6941                    (int) BFD_RELOC_LO16);
6942       if (used_at)
6943         break;
6944       return;
6945
6946     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6947       s = "slt";
6948       goto sgt;
6949     case M_SGTU:
6950       s = "sltu";
6951     sgt:
6952       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6953                    dreg, treg, sreg);
6954       return;
6955
6956     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6957       s = "slt";
6958       goto sgti;
6959     case M_SGTU_I:
6960       s = "sltu";
6961     sgti:
6962       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6963       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6964                    dreg, AT, sreg);
6965       break;
6966
6967     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6968       s = "slt";
6969       goto sle;
6970     case M_SLEU:
6971       s = "sltu";
6972     sle:
6973       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6974                    dreg, treg, sreg);
6975       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6976                    (int) BFD_RELOC_LO16);
6977       return;
6978
6979     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6980       s = "slt";
6981       goto slei;
6982     case M_SLEU_I:
6983       s = "sltu";
6984     slei:
6985       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6986       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6987                    dreg, AT, sreg);
6988       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6989                    (int) BFD_RELOC_LO16);
6990       break;
6991
6992     case M_SLT_I:
6993       if (imm_expr.X_op == O_constant
6994           && imm_expr.X_add_number >= -0x8000
6995           && imm_expr.X_add_number < 0x8000)
6996         {
6997           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6998                        dreg, sreg, (int) BFD_RELOC_LO16);
6999           return;
7000         }
7001       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7002       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7003                    dreg, sreg, AT);
7004       break;
7005
7006     case M_SLTU_I:
7007       if (imm_expr.X_op == O_constant
7008           && imm_expr.X_add_number >= -0x8000
7009           && imm_expr.X_add_number < 0x8000)
7010         {
7011           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7012                        dreg, sreg, (int) BFD_RELOC_LO16);
7013           return;
7014         }
7015       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7016       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7017                    "d,v,t", dreg, sreg, AT);
7018       break;
7019
7020     case M_SNE:
7021       if (sreg == 0)
7022         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7023                      "d,v,t", dreg, 0, treg);
7024       else if (treg == 0)
7025         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7026                      "d,v,t", dreg, 0, sreg);
7027       else
7028         {
7029           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7030                        "d,v,t", dreg, sreg, treg);
7031           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7032                        "d,v,t", dreg, 0, dreg);
7033         }
7034       return;
7035
7036     case M_SNE_I:
7037       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7038         {
7039           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7040                        "d,v,t", dreg, 0, sreg);
7041           return;
7042         }
7043       if (sreg == 0)
7044         {
7045           as_warn (_("Instruction %s: result is always true"),
7046                    ip->insn_mo->name);
7047           macro_build ((char *) NULL, &icnt, &expr1,
7048                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7049                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7050           return;
7051         }
7052       if (imm_expr.X_op == O_constant
7053           && imm_expr.X_add_number >= 0
7054           && imm_expr.X_add_number < 0x10000)
7055         {
7056           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7057                        dreg, sreg, (int) BFD_RELOC_LO16);
7058           used_at = 0;
7059         }
7060       else if (imm_expr.X_op == O_constant
7061                && imm_expr.X_add_number > -0x8000
7062                && imm_expr.X_add_number < 0)
7063         {
7064           imm_expr.X_add_number = -imm_expr.X_add_number;
7065           macro_build ((char *) NULL, &icnt, &imm_expr,
7066                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7067                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7068           used_at = 0;
7069         }
7070       else
7071         {
7072           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7073           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7074                        "d,v,t", dreg, sreg, AT);
7075           used_at = 1;
7076         }
7077       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7078                    "d,v,t", dreg, 0, dreg);
7079       if (used_at)
7080         break;
7081       return;
7082
7083     case M_DSUB_I:
7084       dbl = 1;
7085     case M_SUB_I:
7086       if (imm_expr.X_op == O_constant
7087           && imm_expr.X_add_number > -0x8000
7088           && imm_expr.X_add_number <= 0x8000)
7089         {
7090           imm_expr.X_add_number = -imm_expr.X_add_number;
7091           macro_build ((char *) NULL, &icnt, &imm_expr,
7092                        dbl ? "daddi" : "addi",
7093                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7094           return;
7095         }
7096       load_register (&icnt, AT, &imm_expr, dbl);
7097       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7098                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7099       break;
7100
7101     case M_DSUBU_I:
7102       dbl = 1;
7103     case M_SUBU_I:
7104       if (imm_expr.X_op == O_constant
7105           && imm_expr.X_add_number > -0x8000
7106           && imm_expr.X_add_number <= 0x8000)
7107         {
7108           imm_expr.X_add_number = -imm_expr.X_add_number;
7109           macro_build ((char *) NULL, &icnt, &imm_expr,
7110                        dbl ? "daddiu" : "addiu",
7111                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7112           return;
7113         }
7114       load_register (&icnt, AT, &imm_expr, dbl);
7115       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7116                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7117       break;
7118
7119     case M_TEQ_I:
7120       s = "teq";
7121       goto trap;
7122     case M_TGE_I:
7123       s = "tge";
7124       goto trap;
7125     case M_TGEU_I:
7126       s = "tgeu";
7127       goto trap;
7128     case M_TLT_I:
7129       s = "tlt";
7130       goto trap;
7131     case M_TLTU_I:
7132       s = "tltu";
7133       goto trap;
7134     case M_TNE_I:
7135       s = "tne";
7136     trap:
7137       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7138       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7139                    AT);
7140       break;
7141
7142     case M_TRUNCWS:
7143     case M_TRUNCWD:
7144       assert (mips_opts.isa == ISA_MIPS1);
7145       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7146       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7147
7148       /*
7149        * Is the double cfc1 instruction a bug in the mips assembler;
7150        * or is there a reason for it?
7151        */
7152       mips_emit_delays (true);
7153       ++mips_opts.noreorder;
7154       mips_any_noreorder = 1;
7155       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7156                    treg, RA);
7157       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7158                    treg, RA);
7159       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7160       expr1.X_add_number = 3;
7161       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7162                    (int) BFD_RELOC_LO16);
7163       expr1.X_add_number = 2;
7164       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7165                      (int) BFD_RELOC_LO16);
7166       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7167                    AT, RA);
7168       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7169       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7170               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7171       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7172                    treg, RA);
7173       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7174       --mips_opts.noreorder;
7175       break;
7176
7177     case M_ULH:
7178       s = "lb";
7179       goto ulh;
7180     case M_ULHU:
7181       s = "lbu";
7182     ulh:
7183       if (offset_expr.X_add_number >= 0x7fff)
7184         as_bad (_("operand overflow"));
7185       /* avoid load delay */
7186       if (! target_big_endian)
7187         ++offset_expr.X_add_number;
7188       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7189                    (int) BFD_RELOC_LO16, breg);
7190       if (! target_big_endian)
7191         --offset_expr.X_add_number;
7192       else
7193         ++offset_expr.X_add_number;
7194       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7195                    (int) BFD_RELOC_LO16, breg);
7196       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7197                    treg, treg, 8);
7198       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7199                    treg, treg, AT);
7200       break;
7201
7202     case M_ULD:
7203       s = "ldl";
7204       s2 = "ldr";
7205       off = 7;
7206       goto ulw;
7207     case M_ULW:
7208       s = "lwl";
7209       s2 = "lwr";
7210       off = 3;
7211     ulw:
7212       if (offset_expr.X_add_number >= 0x8000 - off)
7213         as_bad (_("operand overflow"));
7214       if (! target_big_endian)
7215         offset_expr.X_add_number += off;
7216       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7217                    (int) BFD_RELOC_LO16, breg);
7218       if (! target_big_endian)
7219         offset_expr.X_add_number -= off;
7220       else
7221         offset_expr.X_add_number += off;
7222       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7223                    (int) BFD_RELOC_LO16, breg);
7224       return;
7225
7226     case M_ULD_A:
7227       s = "ldl";
7228       s2 = "ldr";
7229       off = 7;
7230       goto ulwa;
7231     case M_ULW_A:
7232       s = "lwl";
7233       s2 = "lwr";
7234       off = 3;
7235     ulwa:
7236       used_at = 1;
7237       load_address (&icnt, AT, &offset_expr, &used_at);
7238       if (breg != 0)
7239         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7240                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7241                      "d,v,t", AT, AT, breg);
7242       if (! target_big_endian)
7243         expr1.X_add_number = off;
7244       else
7245         expr1.X_add_number = 0;
7246       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7247                    (int) BFD_RELOC_LO16, AT);
7248       if (! target_big_endian)
7249         expr1.X_add_number = 0;
7250       else
7251         expr1.X_add_number = off;
7252       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7253                    (int) BFD_RELOC_LO16, AT);
7254       break;
7255
7256     case M_ULH_A:
7257     case M_ULHU_A:
7258       used_at = 1;
7259       load_address (&icnt, AT, &offset_expr, &used_at);
7260       if (breg != 0)
7261         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7262                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7263                      "d,v,t", AT, AT, breg);
7264       if (target_big_endian)
7265         expr1.X_add_number = 0;
7266       macro_build ((char *) NULL, &icnt, &expr1,
7267                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7268                    (int) BFD_RELOC_LO16, AT);
7269       if (target_big_endian)
7270         expr1.X_add_number = 1;
7271       else
7272         expr1.X_add_number = 0;
7273       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7274                    (int) BFD_RELOC_LO16, AT);
7275       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7276                    treg, treg, 8);
7277       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7278                    treg, treg, AT);
7279       break;
7280
7281     case M_USH:
7282       if (offset_expr.X_add_number >= 0x7fff)
7283         as_bad (_("operand overflow"));
7284       if (target_big_endian)
7285         ++offset_expr.X_add_number;
7286       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7287                    (int) BFD_RELOC_LO16, breg);
7288       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7289                    AT, treg, 8);
7290       if (target_big_endian)
7291         --offset_expr.X_add_number;
7292       else
7293         ++offset_expr.X_add_number;
7294       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7295                    (int) BFD_RELOC_LO16, breg);
7296       break;
7297
7298     case M_USD:
7299       s = "sdl";
7300       s2 = "sdr";
7301       off = 7;
7302       goto usw;
7303     case M_USW:
7304       s = "swl";
7305       s2 = "swr";
7306       off = 3;
7307     usw:
7308       if (offset_expr.X_add_number >= 0x8000 - off)
7309         as_bad (_("operand overflow"));
7310       if (! target_big_endian)
7311         offset_expr.X_add_number += off;
7312       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7313                    (int) BFD_RELOC_LO16, breg);
7314       if (! target_big_endian)
7315         offset_expr.X_add_number -= off;
7316       else
7317         offset_expr.X_add_number += off;
7318       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7319                    (int) BFD_RELOC_LO16, breg);
7320       return;
7321
7322     case M_USD_A:
7323       s = "sdl";
7324       s2 = "sdr";
7325       off = 7;
7326       goto uswa;
7327     case M_USW_A:
7328       s = "swl";
7329       s2 = "swr";
7330       off = 3;
7331     uswa:
7332       used_at = 1;
7333       load_address (&icnt, AT, &offset_expr, &used_at);
7334       if (breg != 0)
7335         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7336                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7337                      "d,v,t", AT, AT, breg);
7338       if (! target_big_endian)
7339         expr1.X_add_number = off;
7340       else
7341         expr1.X_add_number = 0;
7342       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7343                    (int) BFD_RELOC_LO16, AT);
7344       if (! target_big_endian)
7345         expr1.X_add_number = 0;
7346       else
7347         expr1.X_add_number = off;
7348       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7349                    (int) BFD_RELOC_LO16, AT);
7350       break;
7351
7352     case M_USH_A:
7353       used_at = 1;
7354       load_address (&icnt, AT, &offset_expr, &used_at);
7355       if (breg != 0)
7356         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7357                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7358                      "d,v,t", AT, AT, breg);
7359       if (! target_big_endian)
7360         expr1.X_add_number = 0;
7361       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7362                    (int) BFD_RELOC_LO16, AT);
7363       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7364                    treg, treg, 8);
7365       if (! target_big_endian)
7366         expr1.X_add_number = 1;
7367       else
7368         expr1.X_add_number = 0;
7369       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7370                    (int) BFD_RELOC_LO16, AT);
7371       if (! target_big_endian)
7372         expr1.X_add_number = 0;
7373       else
7374         expr1.X_add_number = 1;
7375       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7376                    (int) BFD_RELOC_LO16, AT);
7377       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7378                    treg, treg, 8);
7379       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7380                    treg, treg, AT);
7381       break;
7382
7383     default:
7384       /* FIXME: Check if this is one of the itbl macros, since they
7385          are added dynamically.  */
7386       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7387       break;
7388     }
7389   if (mips_opts.noat)
7390     as_warn (_("Macro used $at after \".set noat\""));
7391 }
7392
7393 /* Implement macros in mips16 mode.  */
7394
7395 static void
7396 mips16_macro (ip)
7397      struct mips_cl_insn *ip;
7398 {
7399   int mask;
7400   int xreg, yreg, zreg, tmp;
7401   int icnt;
7402   expressionS expr1;
7403   int dbl;
7404   const char *s, *s2, *s3;
7405
7406   mask = ip->insn_mo->mask;
7407
7408   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7409   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7410   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7411
7412   icnt = 0;
7413
7414   expr1.X_op = O_constant;
7415   expr1.X_op_symbol = NULL;
7416   expr1.X_add_symbol = NULL;
7417   expr1.X_add_number = 1;
7418
7419   dbl = 0;
7420
7421   switch (mask)
7422     {
7423     default:
7424       internalError ();
7425
7426     case M_DDIV_3:
7427       dbl = 1;
7428     case M_DIV_3:
7429       s = "mflo";
7430       goto do_div3;
7431     case M_DREM_3:
7432       dbl = 1;
7433     case M_REM_3:
7434       s = "mfhi";
7435     do_div3:
7436       mips_emit_delays (true);
7437       ++mips_opts.noreorder;
7438       mips_any_noreorder = 1;
7439       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7440                    dbl ? "ddiv" : "div",
7441                    "0,x,y", xreg, yreg);
7442       expr1.X_add_number = 2;
7443       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7444       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7445                    7);
7446
7447       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7448          since that causes an overflow.  We should do that as well,
7449          but I don't see how to do the comparisons without a temporary
7450          register.  */
7451       --mips_opts.noreorder;
7452       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7453       break;
7454
7455     case M_DIVU_3:
7456       s = "divu";
7457       s2 = "mflo";
7458       goto do_divu3;
7459     case M_REMU_3:
7460       s = "divu";
7461       s2 = "mfhi";
7462       goto do_divu3;
7463     case M_DDIVU_3:
7464       s = "ddivu";
7465       s2 = "mflo";
7466       goto do_divu3;
7467     case M_DREMU_3:
7468       s = "ddivu";
7469       s2 = "mfhi";
7470     do_divu3:
7471       mips_emit_delays (true);
7472       ++mips_opts.noreorder;
7473       mips_any_noreorder = 1;
7474       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7475                    xreg, yreg);
7476       expr1.X_add_number = 2;
7477       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7478       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7479                    "6", 7);
7480       --mips_opts.noreorder;
7481       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7482       break;
7483
7484     case M_DMUL:
7485       dbl = 1;
7486     case M_MUL:
7487       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7488                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7489       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7490                    zreg);
7491       return;
7492
7493     case M_DSUBU_I:
7494       dbl = 1;
7495       goto do_subu;
7496     case M_SUBU_I:
7497     do_subu:
7498       if (imm_expr.X_op != O_constant)
7499         as_bad (_("Unsupported large constant"));
7500       imm_expr.X_add_number = -imm_expr.X_add_number;
7501       macro_build ((char *) NULL, &icnt, &imm_expr,
7502                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7503       break;
7504
7505     case M_SUBU_I_2:
7506       if (imm_expr.X_op != O_constant)
7507         as_bad (_("Unsupported large constant"));
7508       imm_expr.X_add_number = -imm_expr.X_add_number;
7509       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7510                    "x,k", xreg);
7511       break;
7512
7513     case M_DSUBU_I_2:
7514       if (imm_expr.X_op != O_constant)
7515         as_bad (_("Unsupported large constant"));
7516       imm_expr.X_add_number = -imm_expr.X_add_number;
7517       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7518                    "y,j", yreg);
7519       break;
7520
7521     case M_BEQ:
7522       s = "cmp";
7523       s2 = "bteqz";
7524       goto do_branch;
7525     case M_BNE:
7526       s = "cmp";
7527       s2 = "btnez";
7528       goto do_branch;
7529     case M_BLT:
7530       s = "slt";
7531       s2 = "btnez";
7532       goto do_branch;
7533     case M_BLTU:
7534       s = "sltu";
7535       s2 = "btnez";
7536       goto do_branch;
7537     case M_BLE:
7538       s = "slt";
7539       s2 = "bteqz";
7540       goto do_reverse_branch;
7541     case M_BLEU:
7542       s = "sltu";
7543       s2 = "bteqz";
7544       goto do_reverse_branch;
7545     case M_BGE:
7546       s = "slt";
7547       s2 = "bteqz";
7548       goto do_branch;
7549     case M_BGEU:
7550       s = "sltu";
7551       s2 = "bteqz";
7552       goto do_branch;
7553     case M_BGT:
7554       s = "slt";
7555       s2 = "btnez";
7556       goto do_reverse_branch;
7557     case M_BGTU:
7558       s = "sltu";
7559       s2 = "btnez";
7560
7561     do_reverse_branch:
7562       tmp = xreg;
7563       xreg = yreg;
7564       yreg = tmp;
7565
7566     do_branch:
7567       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7568                    xreg, yreg);
7569       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7570       break;
7571
7572     case M_BEQ_I:
7573       s = "cmpi";
7574       s2 = "bteqz";
7575       s3 = "x,U";
7576       goto do_branch_i;
7577     case M_BNE_I:
7578       s = "cmpi";
7579       s2 = "btnez";
7580       s3 = "x,U";
7581       goto do_branch_i;
7582     case M_BLT_I:
7583       s = "slti";
7584       s2 = "btnez";
7585       s3 = "x,8";
7586       goto do_branch_i;
7587     case M_BLTU_I:
7588       s = "sltiu";
7589       s2 = "btnez";
7590       s3 = "x,8";
7591       goto do_branch_i;
7592     case M_BLE_I:
7593       s = "slti";
7594       s2 = "btnez";
7595       s3 = "x,8";
7596       goto do_addone_branch_i;
7597     case M_BLEU_I:
7598       s = "sltiu";
7599       s2 = "btnez";
7600       s3 = "x,8";
7601       goto do_addone_branch_i;
7602     case M_BGE_I:
7603       s = "slti";
7604       s2 = "bteqz";
7605       s3 = "x,8";
7606       goto do_branch_i;
7607     case M_BGEU_I:
7608       s = "sltiu";
7609       s2 = "bteqz";
7610       s3 = "x,8";
7611       goto do_branch_i;
7612     case M_BGT_I:
7613       s = "slti";
7614       s2 = "bteqz";
7615       s3 = "x,8";
7616       goto do_addone_branch_i;
7617     case M_BGTU_I:
7618       s = "sltiu";
7619       s2 = "bteqz";
7620       s3 = "x,8";
7621
7622     do_addone_branch_i:
7623       if (imm_expr.X_op != O_constant)
7624         as_bad (_("Unsupported large constant"));
7625       ++imm_expr.X_add_number;
7626
7627     do_branch_i:
7628       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7629       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7630       break;
7631
7632     case M_ABS:
7633       expr1.X_add_number = 0;
7634       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7635       if (xreg != yreg)
7636         move_register (&icnt, xreg, yreg);
7637       expr1.X_add_number = 2;
7638       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7639       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7640                    "neg", "x,w", xreg, xreg);
7641     }
7642 }
7643
7644 /* For consistency checking, verify that all bits are specified either
7645    by the match/mask part of the instruction definition, or by the
7646    operand list.  */
7647 static int
7648 validate_mips_insn (opc)
7649      const struct mips_opcode *opc;
7650 {
7651   const char *p = opc->args;
7652   char c;
7653   unsigned long used_bits = opc->mask;
7654
7655   if ((used_bits & opc->match) != opc->match)
7656     {
7657       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7658               opc->name, opc->args);
7659       return 0;
7660     }
7661 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7662   while (*p)
7663     switch (c = *p++)
7664       {
7665       case ',': break;
7666       case '(': break;
7667       case ')': break;
7668       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7669       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7670       case 'A': break;
7671       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7672       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7673       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7674       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7675       case 'F': break;
7676       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7677       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7678       case 'I': break;
7679       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7680       case 'L': break;
7681       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7682       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7683       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7684       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7685                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7686       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7687       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7688       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7689       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7690       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7691       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7692       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7693       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7694       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7695       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7696       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7697       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7698       case 'f': break;
7699       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7700       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7701       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7702       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7703       case 'l': break;
7704       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7705       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7706       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7707       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7708       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7709       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7710       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7711       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7712       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7713       case 'x': break;
7714       case 'z': break;
7715       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7716       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7717                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7718       default:
7719         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7720                 c, opc->name, opc->args);
7721         return 0;
7722       }
7723 #undef USE_BITS
7724   if (used_bits != 0xffffffff)
7725     {
7726       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7727               ~used_bits & 0xffffffff, opc->name, opc->args);
7728       return 0;
7729     }
7730   return 1;
7731 }
7732
7733 /* This routine assembles an instruction into its binary format.  As a
7734    side effect, it sets one of the global variables imm_reloc or
7735    offset_reloc to the type of relocation to do if one of the operands
7736    is an address expression.  */
7737
7738 static void
7739 mips_ip (str, ip)
7740      char *str;
7741      struct mips_cl_insn *ip;
7742 {
7743   char *s;
7744   const char *args;
7745   char c = 0;
7746   struct mips_opcode *insn;
7747   char *argsStart;
7748   unsigned int regno;
7749   unsigned int lastregno = 0;
7750   char *s_reset;
7751   char save_c = 0;
7752
7753   insn_error = NULL;
7754
7755   /* If the instruction contains a '.', we first try to match an instruction
7756      including the '.'.  Then we try again without the '.'.  */
7757   insn = NULL;
7758   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7759     continue;
7760
7761   /* If we stopped on whitespace, then replace the whitespace with null for
7762      the call to hash_find.  Save the character we replaced just in case we
7763      have to re-parse the instruction.  */
7764   if (ISSPACE (*s))
7765     {
7766       save_c = *s;
7767       *s++ = '\0';
7768     }
7769
7770   insn = (struct mips_opcode *) hash_find (op_hash, str);
7771
7772   /* If we didn't find the instruction in the opcode table, try again, but
7773      this time with just the instruction up to, but not including the
7774      first '.'.  */
7775   if (insn == NULL)
7776     {
7777       /* Restore the character we overwrite above (if any).  */
7778       if (save_c)
7779         *(--s) = save_c;
7780
7781       /* Scan up to the first '.' or whitespace.  */
7782       for (s = str;
7783            *s != '\0' && *s != '.' && !ISSPACE (*s);
7784            ++s)
7785         continue;
7786
7787       /* If we did not find a '.', then we can quit now.  */
7788       if (*s != '.')
7789         {
7790           insn_error = "unrecognized opcode";
7791           return;
7792         }
7793
7794       /* Lookup the instruction in the hash table.  */
7795       *s++ = '\0';
7796       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7797         {
7798           insn_error = "unrecognized opcode";
7799           return;
7800         }
7801     }
7802
7803   argsStart = s;
7804   for (;;)
7805     {
7806       boolean ok;
7807
7808       assert (strcmp (insn->name, str) == 0);
7809
7810       if (OPCODE_IS_MEMBER (insn,
7811                             (mips_opts.isa
7812                              | (mips_opts.mips16 ? INSN_MIPS16 : 0)
7813                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7814                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7815                             mips_arch))
7816         ok = true;
7817       else
7818         ok = false;
7819
7820       if (insn->pinfo != INSN_MACRO)
7821         {
7822           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7823             ok = false;
7824         }
7825
7826       if (! ok)
7827         {
7828           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7829               && strcmp (insn->name, insn[1].name) == 0)
7830             {
7831               ++insn;
7832               continue;
7833             }
7834           else
7835             {
7836               if (!insn_error)
7837                 {
7838                   static char buf[100];
7839                   if (mips_arch_info->is_isa)
7840                     sprintf (buf,
7841                              _("opcode not supported at this ISA level (%s)"),
7842                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7843                   else
7844                     sprintf (buf,
7845                              _("opcode not supported on this processor: %s (%s)"),
7846                              mips_arch_info->name,
7847                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7848                   insn_error = buf;
7849                 }
7850               if (save_c)
7851                 *(--s) = save_c;
7852               return;
7853             }
7854         }
7855
7856       ip->insn_mo = insn;
7857       ip->insn_opcode = insn->match;
7858       insn_error = NULL;
7859       for (args = insn->args;; ++args)
7860         {
7861           int is_mdmx;
7862
7863           s += strspn (s, " \t");
7864           is_mdmx = 0;
7865           switch (*args)
7866             {
7867             case '\0':          /* end of args */
7868               if (*s == '\0')
7869                 return;
7870               break;
7871
7872             case ',':
7873               if (*s++ == *args)
7874                 continue;
7875               s--;
7876               switch (*++args)
7877                 {
7878                 case 'r':
7879                 case 'v':
7880                   ip->insn_opcode |= lastregno << OP_SH_RS;
7881                   continue;
7882
7883                 case 'w':
7884                   ip->insn_opcode |= lastregno << OP_SH_RT;
7885                   continue;
7886
7887                 case 'W':
7888                   ip->insn_opcode |= lastregno << OP_SH_FT;
7889                   continue;
7890
7891                 case 'V':
7892                   ip->insn_opcode |= lastregno << OP_SH_FS;
7893                   continue;
7894                 }
7895               break;
7896
7897             case '(':
7898               /* Handle optional base register.
7899                  Either the base register is omitted or
7900                  we must have a left paren.  */
7901               /* This is dependent on the next operand specifier
7902                  is a base register specification.  */
7903               assert (args[1] == 'b' || args[1] == '5'
7904                       || args[1] == '-' || args[1] == '4');
7905               if (*s == '\0')
7906                 return;
7907
7908             case ')':           /* these must match exactly */
7909               if (*s++ == *args)
7910                 continue;
7911               break;
7912
7913             case '<':           /* must be at least one digit */
7914               /*
7915                * According to the manual, if the shift amount is greater
7916                * than 31 or less than 0, then the shift amount should be
7917                * mod 32.  In reality the mips assembler issues an error.
7918                * We issue a warning and mask out all but the low 5 bits.
7919                */
7920               my_getExpression (&imm_expr, s);
7921               check_absolute_expr (ip, &imm_expr);
7922               if ((unsigned long) imm_expr.X_add_number > 31)
7923                 {
7924                   as_warn (_("Improper shift amount (%lu)"),
7925                            (unsigned long) imm_expr.X_add_number);
7926                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7927                 }
7928               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7929               imm_expr.X_op = O_absent;
7930               s = expr_end;
7931               continue;
7932
7933             case '>':           /* shift amount minus 32 */
7934               my_getExpression (&imm_expr, s);
7935               check_absolute_expr (ip, &imm_expr);
7936               if ((unsigned long) imm_expr.X_add_number < 32
7937                   || (unsigned long) imm_expr.X_add_number > 63)
7938                 break;
7939               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7940               imm_expr.X_op = O_absent;
7941               s = expr_end;
7942               continue;
7943
7944             case 'k':           /* cache code */
7945             case 'h':           /* prefx code */
7946               my_getExpression (&imm_expr, s);
7947               check_absolute_expr (ip, &imm_expr);
7948               if ((unsigned long) imm_expr.X_add_number > 31)
7949                 {
7950                   as_warn (_("Invalid value for `%s' (%lu)"),
7951                            ip->insn_mo->name,
7952                            (unsigned long) imm_expr.X_add_number);
7953                   imm_expr.X_add_number &= 0x1f;
7954                 }
7955               if (*args == 'k')
7956                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7957               else
7958                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7959               imm_expr.X_op = O_absent;
7960               s = expr_end;
7961               continue;
7962
7963             case 'c':           /* break code */
7964               my_getExpression (&imm_expr, s);
7965               check_absolute_expr (ip, &imm_expr);
7966               if ((unsigned long) imm_expr.X_add_number > 1023)
7967                 {
7968                   as_warn (_("Illegal break code (%lu)"),
7969                            (unsigned long) imm_expr.X_add_number);
7970                   imm_expr.X_add_number &= OP_MASK_CODE;
7971                 }
7972               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7973               imm_expr.X_op = O_absent;
7974               s = expr_end;
7975               continue;
7976
7977             case 'q':           /* lower break code */
7978               my_getExpression (&imm_expr, s);
7979               check_absolute_expr (ip, &imm_expr);
7980               if ((unsigned long) imm_expr.X_add_number > 1023)
7981                 {
7982                   as_warn (_("Illegal lower break code (%lu)"),
7983                            (unsigned long) imm_expr.X_add_number);
7984                   imm_expr.X_add_number &= OP_MASK_CODE2;
7985                 }
7986               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7987               imm_expr.X_op = O_absent;
7988               s = expr_end;
7989               continue;
7990
7991             case 'B':           /* 20-bit syscall/break code.  */
7992               my_getExpression (&imm_expr, s);
7993               check_absolute_expr (ip, &imm_expr);
7994               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7995                 as_warn (_("Illegal 20-bit code (%lu)"),
7996                          (unsigned long) imm_expr.X_add_number);
7997               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7998               imm_expr.X_op = O_absent;
7999               s = expr_end;
8000               continue;
8001
8002             case 'C':           /* Coprocessor code */
8003               my_getExpression (&imm_expr, s);
8004               check_absolute_expr (ip, &imm_expr);
8005               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8006                 {
8007                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8008                            (unsigned long) imm_expr.X_add_number);
8009                   imm_expr.X_add_number &= ((1 << 25) - 1);
8010                 }
8011               ip->insn_opcode |= imm_expr.X_add_number;
8012               imm_expr.X_op = O_absent;
8013               s = expr_end;
8014               continue;
8015
8016             case 'J':           /* 19-bit wait code.  */
8017               my_getExpression (&imm_expr, s);
8018               check_absolute_expr (ip, &imm_expr);
8019               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8020                 as_warn (_("Illegal 19-bit code (%lu)"),
8021                          (unsigned long) imm_expr.X_add_number);
8022               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8023               imm_expr.X_op = O_absent;
8024               s = expr_end;
8025               continue;
8026
8027             case 'P':           /* Performance register */
8028               my_getExpression (&imm_expr, s);
8029               check_absolute_expr (ip, &imm_expr);
8030               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8031                 {
8032                   as_warn (_("Invalid performance register (%lu)"),
8033                            (unsigned long) imm_expr.X_add_number);
8034                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8035                 }
8036               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8037               imm_expr.X_op = O_absent;
8038               s = expr_end;
8039               continue;
8040
8041             case 'b':           /* base register */
8042             case 'd':           /* destination register */
8043             case 's':           /* source register */
8044             case 't':           /* target register */
8045             case 'r':           /* both target and source */
8046             case 'v':           /* both dest and source */
8047             case 'w':           /* both dest and target */
8048             case 'E':           /* coprocessor target register */
8049             case 'G':           /* coprocessor destination register */
8050             case 'x':           /* ignore register name */
8051             case 'z':           /* must be zero register */
8052             case 'U':           /* destination register (clo/clz).  */
8053               s_reset = s;
8054               if (s[0] == '$')
8055                 {
8056
8057                   if (ISDIGIT (s[1]))
8058                     {
8059                       ++s;
8060                       regno = 0;
8061                       do
8062                         {
8063                           regno *= 10;
8064                           regno += *s - '0';
8065                           ++s;
8066                         }
8067                       while (ISDIGIT (*s));
8068                       if (regno > 31)
8069                         as_bad (_("Invalid register number (%d)"), regno);
8070                     }
8071                   else if (*args == 'E' || *args == 'G')
8072                     goto notreg;
8073                   else
8074                     {
8075                       if (s[1] == 'r' && s[2] == 'a')
8076                         {
8077                           s += 3;
8078                           regno = RA;
8079                         }
8080                       else if (s[1] == 'f' && s[2] == 'p')
8081                         {
8082                           s += 3;
8083                           regno = FP;
8084                         }
8085                       else if (s[1] == 's' && s[2] == 'p')
8086                         {
8087                           s += 3;
8088                           regno = SP;
8089                         }
8090                       else if (s[1] == 'g' && s[2] == 'p')
8091                         {
8092                           s += 3;
8093                           regno = GP;
8094                         }
8095                       else if (s[1] == 'a' && s[2] == 't')
8096                         {
8097                           s += 3;
8098                           regno = AT;
8099                         }
8100                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8101                         {
8102                           s += 4;
8103                           regno = KT0;
8104                         }
8105                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8106                         {
8107                           s += 4;
8108                           regno = KT1;
8109                         }
8110                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8111                         {
8112                           s += 5;
8113                           regno = ZERO;
8114                         }
8115                       else if (itbl_have_entries)
8116                         {
8117                           char *p, *n;
8118                           unsigned long r;
8119
8120                           p = s + 1;    /* advance past '$' */
8121                           n = itbl_get_field (&p);  /* n is name */
8122
8123                           /* See if this is a register defined in an
8124                              itbl entry.  */
8125                           if (itbl_get_reg_val (n, &r))
8126                             {
8127                               /* Get_field advances to the start of
8128                                  the next field, so we need to back
8129                                  rack to the end of the last field.  */
8130                               if (p)
8131                                 s = p - 1;
8132                               else
8133                                 s = strchr (s, '\0');
8134                               regno = r;
8135                             }
8136                           else
8137                             goto notreg;
8138                         }
8139                       else
8140                         goto notreg;
8141                     }
8142                   if (regno == AT
8143                       && ! mips_opts.noat
8144                       && *args != 'E'
8145                       && *args != 'G')
8146                     as_warn (_("Used $at without \".set noat\""));
8147                   c = *args;
8148                   if (*s == ' ')
8149                     ++s;
8150                   if (args[1] != *s)
8151                     {
8152                       if (c == 'r' || c == 'v' || c == 'w')
8153                         {
8154                           regno = lastregno;
8155                           s = s_reset;
8156                           ++args;
8157                         }
8158                     }
8159                   /* 'z' only matches $0.  */
8160                   if (c == 'z' && regno != 0)
8161                     break;
8162
8163         /* Now that we have assembled one operand, we use the args string
8164          * to figure out where it goes in the instruction.  */
8165                   switch (c)
8166                     {
8167                     case 'r':
8168                     case 's':
8169                     case 'v':
8170                     case 'b':
8171                       ip->insn_opcode |= regno << OP_SH_RS;
8172                       break;
8173                     case 'd':
8174                     case 'G':
8175                       ip->insn_opcode |= regno << OP_SH_RD;
8176                       break;
8177                     case 'U':
8178                       ip->insn_opcode |= regno << OP_SH_RD;
8179                       ip->insn_opcode |= regno << OP_SH_RT;
8180                       break;
8181                     case 'w':
8182                     case 't':
8183                     case 'E':
8184                       ip->insn_opcode |= regno << OP_SH_RT;
8185                       break;
8186                     case 'x':
8187                       /* This case exists because on the r3000 trunc
8188                          expands into a macro which requires a gp
8189                          register.  On the r6000 or r4000 it is
8190                          assembled into a single instruction which
8191                          ignores the register.  Thus the insn version
8192                          is MIPS_ISA2 and uses 'x', and the macro
8193                          version is MIPS_ISA1 and uses 't'.  */
8194                       break;
8195                     case 'z':
8196                       /* This case is for the div instruction, which
8197                          acts differently if the destination argument
8198                          is $0.  This only matches $0, and is checked
8199                          outside the switch.  */
8200                       break;
8201                     case 'D':
8202                       /* Itbl operand; not yet implemented. FIXME ?? */
8203                       break;
8204                       /* What about all other operands like 'i', which
8205                          can be specified in the opcode table? */
8206                     }
8207                   lastregno = regno;
8208                   continue;
8209                 }
8210             notreg:
8211               switch (*args++)
8212                 {
8213                 case 'r':
8214                 case 'v':
8215                   ip->insn_opcode |= lastregno << OP_SH_RS;
8216                   continue;
8217                 case 'w':
8218                   ip->insn_opcode |= lastregno << OP_SH_RT;
8219                   continue;
8220                 }
8221               break;
8222
8223             case 'O':           /* MDMX alignment immediate constant.  */
8224               my_getExpression (&imm_expr, s);
8225               check_absolute_expr (ip, &imm_expr);
8226               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8227                 {
8228                   as_warn ("Improper align amount (%ld), using low bits",
8229                            (long) imm_expr.X_add_number);
8230                   imm_expr.X_add_number &= OP_MASK_ALN;
8231                 }
8232               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8233               imm_expr.X_op = O_absent;
8234               s = expr_end;
8235               continue;
8236
8237             case 'Q':           /* MDMX vector, element sel, or const.  */
8238               if (s[0] != '$')
8239                 {
8240                   /* MDMX Immediate.  */
8241                   my_getExpression (&imm_expr, s);
8242                   check_absolute_expr (ip, &imm_expr);
8243                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8244                     {
8245                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8246                                (long) imm_expr.X_add_number);
8247                       imm_expr.X_add_number &= OP_MASK_FT;
8248                     }
8249                   imm_expr.X_add_number &= OP_MASK_FT;
8250                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8251                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8252                   else
8253                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8254                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8255                   imm_expr.X_op = O_absent;
8256                   s = expr_end;
8257                   continue;
8258                 }
8259               /* Not MDMX Immediate.  Fall through.  */
8260             case 'X':           /* MDMX destination register.  */
8261             case 'Y':           /* MDMX source register.  */
8262             case 'Z':           /* MDMX target register.  */
8263               is_mdmx = 1;
8264             case 'D':           /* floating point destination register */
8265             case 'S':           /* floating point source register */
8266             case 'T':           /* floating point target register */
8267             case 'R':           /* floating point source register */
8268             case 'V':
8269             case 'W':
8270               s_reset = s;
8271               /* Accept $fN for FP and MDMX register numbers, and in
8272                  addition accept $vN for MDMX register numbers.  */
8273               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8274                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8275                       && ISDIGIT (s[2])))
8276                 {
8277                   s += 2;
8278                   regno = 0;
8279                   do
8280                     {
8281                       regno *= 10;
8282                       regno += *s - '0';
8283                       ++s;
8284                     }
8285                   while (ISDIGIT (*s));
8286
8287                   if (regno > 31)
8288                     as_bad (_("Invalid float register number (%d)"), regno);
8289
8290                   if ((regno & 1) != 0
8291                       && HAVE_32BIT_FPRS
8292                       && ! (strcmp (str, "mtc1") == 0
8293                             || strcmp (str, "mfc1") == 0
8294                             || strcmp (str, "lwc1") == 0
8295                             || strcmp (str, "swc1") == 0
8296                             || strcmp (str, "l.s") == 0
8297                             || strcmp (str, "s.s") == 0))
8298                     as_warn (_("Float register should be even, was %d"),
8299                              regno);
8300
8301                   c = *args;
8302                   if (*s == ' ')
8303                     ++s;
8304                   if (args[1] != *s)
8305                     {
8306                       if (c == 'V' || c == 'W')
8307                         {
8308                           regno = lastregno;
8309                           s = s_reset;
8310                           ++args;
8311                         }
8312                     }
8313                   switch (c)
8314                     {
8315                     case 'D':
8316                     case 'X':
8317                       ip->insn_opcode |= regno << OP_SH_FD;
8318                       break;
8319                     case 'V':
8320                     case 'S':
8321                     case 'Y':
8322                       ip->insn_opcode |= regno << OP_SH_FS;
8323                       break;
8324                     case 'Q':
8325                       /* This is like 'Z', but also needs to fix the MDMX
8326                          vector/scalar select bits.  Note that the
8327                          scalar immediate case is handled above.  */
8328                       if (*s == '[')
8329                         {
8330                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8331                           int max_el = (is_qh ? 3 : 7);
8332                           s++;
8333                           my_getExpression(&imm_expr, s);
8334                           check_absolute_expr (ip, &imm_expr);
8335                           s = expr_end;
8336                           if (imm_expr.X_add_number > max_el)
8337                             as_bad(_("Bad element selector %ld"),
8338                                    (long) imm_expr.X_add_number);
8339                           imm_expr.X_add_number &= max_el;
8340                           ip->insn_opcode |= (imm_expr.X_add_number
8341                                               << (OP_SH_VSEL +
8342                                                   (is_qh ? 2 : 1)));
8343                           if (*s != ']')
8344                             as_warn(_("Expecting ']' found '%s'"), s);
8345                           else
8346                             s++;
8347                         }
8348                       else
8349                         {
8350                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8351                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8352                                                 << OP_SH_VSEL);
8353                           else
8354                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8355                                                 OP_SH_VSEL);
8356                         }
8357                       /* Fall through */
8358                     case 'W':
8359                     case 'T':
8360                     case 'Z':
8361                       ip->insn_opcode |= regno << OP_SH_FT;
8362                       break;
8363                     case 'R':
8364                       ip->insn_opcode |= regno << OP_SH_FR;
8365                       break;
8366                     }
8367                   lastregno = regno;
8368                   continue;
8369                 }
8370
8371               switch (*args++)
8372                 {
8373                 case 'V':
8374                   ip->insn_opcode |= lastregno << OP_SH_FS;
8375                   continue;
8376                 case 'W':
8377                   ip->insn_opcode |= lastregno << OP_SH_FT;
8378                   continue;
8379                 }
8380               break;
8381
8382             case 'I':
8383               my_getExpression (&imm_expr, s);
8384               if (imm_expr.X_op != O_big
8385                   && imm_expr.X_op != O_constant)
8386                 insn_error = _("absolute expression required");
8387               s = expr_end;
8388               continue;
8389
8390             case 'A':
8391               my_getExpression (&offset_expr, s);
8392               *imm_reloc = BFD_RELOC_32;
8393               s = expr_end;
8394               continue;
8395
8396             case 'F':
8397             case 'L':
8398             case 'f':
8399             case 'l':
8400               {
8401                 int f64;
8402                 int using_gprs;
8403                 char *save_in;
8404                 char *err;
8405                 unsigned char temp[8];
8406                 int len;
8407                 unsigned int length;
8408                 segT seg;
8409                 subsegT subseg;
8410                 char *p;
8411
8412                 /* These only appear as the last operand in an
8413                    instruction, and every instruction that accepts
8414                    them in any variant accepts them in all variants.
8415                    This means we don't have to worry about backing out
8416                    any changes if the instruction does not match.
8417
8418                    The difference between them is the size of the
8419                    floating point constant and where it goes.  For 'F'
8420                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8421                    is 32 bits.  Where the constant is placed is based
8422                    on how the MIPS assembler does things:
8423                     F -- .rdata
8424                     L -- .lit8
8425                     f -- immediate value
8426                     l -- .lit4
8427
8428                     The .lit4 and .lit8 sections are only used if
8429                     permitted by the -G argument.
8430
8431                     When generating embedded PIC code, we use the
8432                     .lit8 section but not the .lit4 section (we can do
8433                     .lit4 inline easily; we need to put .lit8
8434                     somewhere in the data segment, and using .lit8
8435                     permits the linker to eventually combine identical
8436                     .lit8 entries).
8437
8438                     The code below needs to know whether the target register
8439                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8440                     'F' are used with GPR-based instructions and 'l' and
8441                     'L' are used with FPR-based instructions.  */
8442
8443                 f64 = *args == 'F' || *args == 'L';
8444                 using_gprs = *args == 'F' || *args == 'f';
8445
8446                 save_in = input_line_pointer;
8447                 input_line_pointer = s;
8448                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8449                 length = len;
8450                 s = input_line_pointer;
8451                 input_line_pointer = save_in;
8452                 if (err != NULL && *err != '\0')
8453                   {
8454                     as_bad (_("Bad floating point constant: %s"), err);
8455                     memset (temp, '\0', sizeof temp);
8456                     length = f64 ? 8 : 4;
8457                   }
8458
8459                 assert (length == (unsigned) (f64 ? 8 : 4));
8460
8461                 if (*args == 'f'
8462                     || (*args == 'l'
8463                         && (! USE_GLOBAL_POINTER_OPT
8464                             || mips_pic == EMBEDDED_PIC
8465                             || g_switch_value < 4
8466                             || (temp[0] == 0 && temp[1] == 0)
8467                             || (temp[2] == 0 && temp[3] == 0))))
8468                   {
8469                     imm_expr.X_op = O_constant;
8470                     if (! target_big_endian)
8471                       imm_expr.X_add_number = bfd_getl32 (temp);
8472                     else
8473                       imm_expr.X_add_number = bfd_getb32 (temp);
8474                   }
8475                 else if (length > 4
8476                          && ! mips_disable_float_construction
8477                          /* Constants can only be constructed in GPRs and
8478                             copied to FPRs if the GPRs are at least as wide
8479                             as the FPRs.  Force the constant into memory if
8480                             we are using 64-bit FPRs but the GPRs are only
8481                             32 bits wide.  */
8482                          && (using_gprs
8483                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8484                          && ((temp[0] == 0 && temp[1] == 0)
8485                              || (temp[2] == 0 && temp[3] == 0))
8486                          && ((temp[4] == 0 && temp[5] == 0)
8487                              || (temp[6] == 0 && temp[7] == 0)))
8488                   {
8489                     /* The value is simple enough to load with a couple of
8490                        instructions.  If using 32-bit registers, set
8491                        imm_expr to the high order 32 bits and offset_expr to
8492                        the low order 32 bits.  Otherwise, set imm_expr to
8493                        the entire 64 bit constant.  */
8494                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8495                       {
8496                         imm_expr.X_op = O_constant;
8497                         offset_expr.X_op = O_constant;
8498                         if (! target_big_endian)
8499                           {
8500                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8501                             offset_expr.X_add_number = bfd_getl32 (temp);
8502                           }
8503                         else
8504                           {
8505                             imm_expr.X_add_number = bfd_getb32 (temp);
8506                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8507                           }
8508                         if (offset_expr.X_add_number == 0)
8509                           offset_expr.X_op = O_absent;
8510                       }
8511                     else if (sizeof (imm_expr.X_add_number) > 4)
8512                       {
8513                         imm_expr.X_op = O_constant;
8514                         if (! target_big_endian)
8515                           imm_expr.X_add_number = bfd_getl64 (temp);
8516                         else
8517                           imm_expr.X_add_number = bfd_getb64 (temp);
8518                       }
8519                     else
8520                       {
8521                         imm_expr.X_op = O_big;
8522                         imm_expr.X_add_number = 4;
8523                         if (! target_big_endian)
8524                           {
8525                             generic_bignum[0] = bfd_getl16 (temp);
8526                             generic_bignum[1] = bfd_getl16 (temp + 2);
8527                             generic_bignum[2] = bfd_getl16 (temp + 4);
8528                             generic_bignum[3] = bfd_getl16 (temp + 6);
8529                           }
8530                         else
8531                           {
8532                             generic_bignum[0] = bfd_getb16 (temp + 6);
8533                             generic_bignum[1] = bfd_getb16 (temp + 4);
8534                             generic_bignum[2] = bfd_getb16 (temp + 2);
8535                             generic_bignum[3] = bfd_getb16 (temp);
8536                           }
8537                       }
8538                   }
8539                 else
8540                   {
8541                     const char *newname;
8542                     segT new_seg;
8543
8544                     /* Switch to the right section.  */
8545                     seg = now_seg;
8546                     subseg = now_subseg;
8547                     switch (*args)
8548                       {
8549                       default: /* unused default case avoids warnings.  */
8550                       case 'L':
8551                         newname = RDATA_SECTION_NAME;
8552                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8553                             || mips_pic == EMBEDDED_PIC)
8554                           newname = ".lit8";
8555                         break;
8556                       case 'F':
8557                         if (mips_pic == EMBEDDED_PIC)
8558                           newname = ".lit8";
8559                         else
8560                           newname = RDATA_SECTION_NAME;
8561                         break;
8562                       case 'l':
8563                         assert (!USE_GLOBAL_POINTER_OPT
8564                                 || g_switch_value >= 4);
8565                         newname = ".lit4";
8566                         break;
8567                       }
8568                     new_seg = subseg_new (newname, (subsegT) 0);
8569                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8570                       bfd_set_section_flags (stdoutput, new_seg,
8571                                              (SEC_ALLOC
8572                                               | SEC_LOAD
8573                                               | SEC_READONLY
8574                                               | SEC_DATA));
8575                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8576                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8577                         && strcmp (TARGET_OS, "elf") != 0)
8578                       record_alignment (new_seg, 4);
8579                     else
8580                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8581                     if (seg == now_seg)
8582                       as_bad (_("Can't use floating point insn in this section"));
8583
8584                     /* Set the argument to the current address in the
8585                        section.  */
8586                     offset_expr.X_op = O_symbol;
8587                     offset_expr.X_add_symbol =
8588                       symbol_new ("L0\001", now_seg,
8589                                   (valueT) frag_now_fix (), frag_now);
8590                     offset_expr.X_add_number = 0;
8591
8592                     /* Put the floating point number into the section.  */
8593                     p = frag_more ((int) length);
8594                     memcpy (p, temp, length);
8595
8596                     /* Switch back to the original section.  */
8597                     subseg_set (seg, subseg);
8598                   }
8599               }
8600               continue;
8601
8602             case 'i':           /* 16 bit unsigned immediate */
8603             case 'j':           /* 16 bit signed immediate */
8604               *imm_reloc = BFD_RELOC_LO16;
8605               c = my_getSmallExpression (&imm_expr, s);
8606               if (c != S_EX_NONE)
8607                 {
8608                   if (c != S_EX_LO)
8609                     {
8610                       if (c == S_EX_HI)
8611                         {
8612                           *imm_reloc = BFD_RELOC_HI16_S;
8613                           imm_unmatched_hi = true;
8614                         }
8615 #ifdef OBJ_ELF
8616                       else if (c == S_EX_HIGHEST)
8617                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8618                       else if (c == S_EX_HIGHER)
8619                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8620                       else if (c == S_EX_GP_REL)
8621                         {
8622                           /* This occurs in NewABI only.  */
8623                           c = my_getSmallExpression (&imm_expr, s);
8624                           if (c != S_EX_NEG)
8625                             as_bad (_("bad composition of relocations"));
8626                           else
8627                             {
8628                               c = my_getSmallExpression (&imm_expr, s);
8629                               if (c != S_EX_LO)
8630                                 as_bad (_("bad composition of relocations"));
8631                               else
8632                                 {
8633                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8634                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8635                                   imm_reloc[2] = BFD_RELOC_LO16;
8636                                 }
8637                             }
8638                         }
8639 #endif
8640                       else
8641                         *imm_reloc = BFD_RELOC_HI16;
8642                     }
8643                   else if (imm_expr.X_op == O_constant)
8644                     imm_expr.X_add_number &= 0xffff;
8645                 }
8646               if (*args == 'i')
8647                 {
8648                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8649                       || ((imm_expr.X_add_number < 0
8650                            || imm_expr.X_add_number >= 0x10000)
8651                           && imm_expr.X_op == O_constant))
8652                     {
8653                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8654                           !strcmp (insn->name, insn[1].name))
8655                         break;
8656                       if (imm_expr.X_op == O_constant
8657                           || imm_expr.X_op == O_big)
8658                         as_bad (_("16 bit expression not in range 0..65535"));
8659                     }
8660                 }
8661               else
8662                 {
8663                   int more;
8664                   offsetT max;
8665
8666                   /* The upper bound should be 0x8000, but
8667                      unfortunately the MIPS assembler accepts numbers
8668                      from 0x8000 to 0xffff and sign extends them, and
8669                      we want to be compatible.  We only permit this
8670                      extended range for an instruction which does not
8671                      provide any further alternates, since those
8672                      alternates may handle other cases.  People should
8673                      use the numbers they mean, rather than relying on
8674                      a mysterious sign extension.  */
8675                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8676                           strcmp (insn->name, insn[1].name) == 0);
8677                   if (more)
8678                     max = 0x8000;
8679                   else
8680                     max = 0x10000;
8681                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8682                       || ((imm_expr.X_add_number < -0x8000
8683                            || imm_expr.X_add_number >= max)
8684                           && imm_expr.X_op == O_constant)
8685                       || (more
8686                           && imm_expr.X_add_number < 0
8687                           && HAVE_64BIT_GPRS
8688                           && imm_expr.X_unsigned
8689                           && sizeof (imm_expr.X_add_number) <= 4))
8690                     {
8691                       if (more)
8692                         break;
8693                       if (imm_expr.X_op == O_constant
8694                           || imm_expr.X_op == O_big)
8695                         as_bad (_("16 bit expression not in range -32768..32767"));
8696                     }
8697                 }
8698               s = expr_end;
8699               continue;
8700
8701             case 'o':           /* 16 bit offset */
8702               c = my_getSmallExpression (&offset_expr, s);
8703
8704               /* If this value won't fit into a 16 bit offset, then go
8705                  find a macro that will generate the 32 bit offset
8706                  code pattern.  */
8707               if (c == S_EX_NONE
8708                   && (offset_expr.X_op != O_constant
8709                       || offset_expr.X_add_number >= 0x8000
8710                       || offset_expr.X_add_number < -0x8000))
8711                 break;
8712
8713               if (c == S_EX_HI)
8714                 {
8715                   if (offset_expr.X_op != O_constant)
8716                     break;
8717                   offset_expr.X_add_number =
8718                     (offset_expr.X_add_number >> 16) & 0xffff;
8719                 }
8720               *offset_reloc = BFD_RELOC_LO16;
8721               s = expr_end;
8722               continue;
8723
8724             case 'p':           /* pc relative offset */
8725               if (mips_pic == EMBEDDED_PIC)
8726                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8727               else
8728                 *offset_reloc = BFD_RELOC_16_PCREL;
8729               my_getExpression (&offset_expr, s);
8730               s = expr_end;
8731               continue;
8732
8733             case 'u':           /* upper 16 bits */
8734               c = my_getSmallExpression (&imm_expr, s);
8735               *imm_reloc = BFD_RELOC_LO16;
8736               if (c != S_EX_NONE)
8737                 {
8738                   if (c != S_EX_LO)
8739                     {
8740                       if (c == S_EX_HI)
8741                         {
8742                           *imm_reloc = BFD_RELOC_HI16_S;
8743                           imm_unmatched_hi = true;
8744                         }
8745 #ifdef OBJ_ELF
8746                       else if (c == S_EX_HIGHEST)
8747                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8748                       else if (c == S_EX_GP_REL)
8749                         {
8750                           /* This occurs in NewABI only.  */
8751                           c = my_getSmallExpression (&imm_expr, s);
8752                           if (c != S_EX_NEG)
8753                             as_bad (_("bad composition of relocations"));
8754                           else
8755                             {
8756                               c = my_getSmallExpression (&imm_expr, s);
8757                               if (c != S_EX_HI)
8758                                 as_bad (_("bad composition of relocations"));
8759                               else
8760                                 {
8761                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8762                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8763                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8764                                 }
8765                             }
8766                         }
8767 #endif
8768                       else
8769                         *imm_reloc = BFD_RELOC_HI16;
8770                     }
8771                   else if (imm_expr.X_op == O_constant)
8772                     imm_expr.X_add_number &= 0xffff;
8773                 }
8774               else if (imm_expr.X_op == O_constant
8775                        && (imm_expr.X_add_number < 0
8776                            || imm_expr.X_add_number >= 0x10000))
8777                 as_bad (_("lui expression not in range 0..65535"));
8778               s = expr_end;
8779               continue;
8780
8781             case 'a':           /* 26 bit address */
8782               my_getExpression (&offset_expr, s);
8783               s = expr_end;
8784               *offset_reloc = BFD_RELOC_MIPS_JMP;
8785               continue;
8786
8787             case 'N':           /* 3 bit branch condition code */
8788             case 'M':           /* 3 bit compare condition code */
8789               if (strncmp (s, "$fcc", 4) != 0)
8790                 break;
8791               s += 4;
8792               regno = 0;
8793               do
8794                 {
8795                   regno *= 10;
8796                   regno += *s - '0';
8797                   ++s;
8798                 }
8799               while (ISDIGIT (*s));
8800               if (regno > 7)
8801                 as_bad (_("invalid condition code register $fcc%d"), regno);
8802               if (*args == 'N')
8803                 ip->insn_opcode |= regno << OP_SH_BCC;
8804               else
8805                 ip->insn_opcode |= regno << OP_SH_CCC;
8806               continue;
8807
8808             case 'H':
8809               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8810                 s += 2;
8811               if (ISDIGIT (*s))
8812                 {
8813                   c = 0;
8814                   do
8815                     {
8816                       c *= 10;
8817                       c += *s - '0';
8818                       ++s;
8819                     }
8820                   while (ISDIGIT (*s));
8821                 }
8822               else
8823                 c = 8; /* Invalid sel value.  */
8824
8825               if (c > 7)
8826                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8827               ip->insn_opcode |= c;
8828               continue;
8829
8830             default:
8831               as_bad (_("bad char = '%c'\n"), *args);
8832               internalError ();
8833             }
8834           break;
8835         }
8836       /* Args don't match.  */
8837       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8838           !strcmp (insn->name, insn[1].name))
8839         {
8840           ++insn;
8841           s = argsStart;
8842           insn_error = _("illegal operands");
8843           continue;
8844         }
8845       if (save_c)
8846         *(--s) = save_c;
8847       insn_error = _("illegal operands");
8848       return;
8849     }
8850 }
8851
8852 /* This routine assembles an instruction into its binary format when
8853    assembling for the mips16.  As a side effect, it sets one of the
8854    global variables imm_reloc or offset_reloc to the type of
8855    relocation to do if one of the operands is an address expression.
8856    It also sets mips16_small and mips16_ext if the user explicitly
8857    requested a small or extended instruction.  */
8858
8859 static void
8860 mips16_ip (str, ip)
8861      char *str;
8862      struct mips_cl_insn *ip;
8863 {
8864   char *s;
8865   const char *args;
8866   struct mips_opcode *insn;
8867   char *argsstart;
8868   unsigned int regno;
8869   unsigned int lastregno = 0;
8870   char *s_reset;
8871
8872   insn_error = NULL;
8873
8874   mips16_small = false;
8875   mips16_ext = false;
8876
8877   for (s = str; ISLOWER (*s); ++s)
8878     ;
8879   switch (*s)
8880     {
8881     case '\0':
8882       break;
8883
8884     case ' ':
8885       *s++ = '\0';
8886       break;
8887
8888     case '.':
8889       if (s[1] == 't' && s[2] == ' ')
8890         {
8891           *s = '\0';
8892           mips16_small = true;
8893           s += 3;
8894           break;
8895         }
8896       else if (s[1] == 'e' && s[2] == ' ')
8897         {
8898           *s = '\0';
8899           mips16_ext = true;
8900           s += 3;
8901           break;
8902         }
8903       /* Fall through.  */
8904     default:
8905       insn_error = _("unknown opcode");
8906       return;
8907     }
8908
8909   if (mips_opts.noautoextend && ! mips16_ext)
8910     mips16_small = true;
8911
8912   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8913     {
8914       insn_error = _("unrecognized opcode");
8915       return;
8916     }
8917
8918   argsstart = s;
8919   for (;;)
8920     {
8921       assert (strcmp (insn->name, str) == 0);
8922
8923       ip->insn_mo = insn;
8924       ip->insn_opcode = insn->match;
8925       ip->use_extend = false;
8926       imm_expr.X_op = O_absent;
8927       imm_reloc[0] = BFD_RELOC_UNUSED;
8928       imm_reloc[1] = BFD_RELOC_UNUSED;
8929       imm_reloc[2] = BFD_RELOC_UNUSED;
8930       offset_expr.X_op = O_absent;
8931       offset_reloc[0] = BFD_RELOC_UNUSED;
8932       offset_reloc[1] = BFD_RELOC_UNUSED;
8933       offset_reloc[2] = BFD_RELOC_UNUSED;
8934       for (args = insn->args; 1; ++args)
8935         {
8936           int c;
8937
8938           if (*s == ' ')
8939             ++s;
8940
8941           /* In this switch statement we call break if we did not find
8942              a match, continue if we did find a match, or return if we
8943              are done.  */
8944
8945           c = *args;
8946           switch (c)
8947             {
8948             case '\0':
8949               if (*s == '\0')
8950                 {
8951                   /* Stuff the immediate value in now, if we can.  */
8952                   if (imm_expr.X_op == O_constant
8953                       && *imm_reloc > BFD_RELOC_UNUSED
8954                       && insn->pinfo != INSN_MACRO)
8955                     {
8956                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8957                                     imm_expr.X_add_number, true, mips16_small,
8958                                     mips16_ext, &ip->insn_opcode,
8959                                     &ip->use_extend, &ip->extend);
8960                       imm_expr.X_op = O_absent;
8961                       *imm_reloc = BFD_RELOC_UNUSED;
8962                     }
8963
8964                   return;
8965                 }
8966               break;
8967
8968             case ',':
8969               if (*s++ == c)
8970                 continue;
8971               s--;
8972               switch (*++args)
8973                 {
8974                 case 'v':
8975                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8976                   continue;
8977                 case 'w':
8978                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8979                   continue;
8980                 }
8981               break;
8982
8983             case '(':
8984             case ')':
8985               if (*s++ == c)
8986                 continue;
8987               break;
8988
8989             case 'v':
8990             case 'w':
8991               if (s[0] != '$')
8992                 {
8993                   if (c == 'v')
8994                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8995                   else
8996                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8997                   ++args;
8998                   continue;
8999                 }
9000               /* Fall through.  */
9001             case 'x':
9002             case 'y':
9003             case 'z':
9004             case 'Z':
9005             case '0':
9006             case 'S':
9007             case 'R':
9008             case 'X':
9009             case 'Y':
9010               if (s[0] != '$')
9011                 break;
9012               s_reset = s;
9013               if (ISDIGIT (s[1]))
9014                 {
9015                   ++s;
9016                   regno = 0;
9017                   do
9018                     {
9019                       regno *= 10;
9020                       regno += *s - '0';
9021                       ++s;
9022                     }
9023                   while (ISDIGIT (*s));
9024                   if (regno > 31)
9025                     {
9026                       as_bad (_("invalid register number (%d)"), regno);
9027                       regno = 2;
9028                     }
9029                 }
9030               else
9031                 {
9032                   if (s[1] == 'r' && s[2] == 'a')
9033                     {
9034                       s += 3;
9035                       regno = RA;
9036                     }
9037                   else if (s[1] == 'f' && s[2] == 'p')
9038                     {
9039                       s += 3;
9040                       regno = FP;
9041                     }
9042                   else if (s[1] == 's' && s[2] == 'p')
9043                     {
9044                       s += 3;
9045                       regno = SP;
9046                     }
9047                   else if (s[1] == 'g' && s[2] == 'p')
9048                     {
9049                       s += 3;
9050                       regno = GP;
9051                     }
9052                   else if (s[1] == 'a' && s[2] == 't')
9053                     {
9054                       s += 3;
9055                       regno = AT;
9056                     }
9057                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9058                     {
9059                       s += 4;
9060                       regno = KT0;
9061                     }
9062                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9063                     {
9064                       s += 4;
9065                       regno = KT1;
9066                     }
9067                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9068                     {
9069                       s += 5;
9070                       regno = ZERO;
9071                     }
9072                   else
9073                     break;
9074                 }
9075
9076               if (*s == ' ')
9077                 ++s;
9078               if (args[1] != *s)
9079                 {
9080                   if (c == 'v' || c == 'w')
9081                     {
9082                       regno = mips16_to_32_reg_map[lastregno];
9083                       s = s_reset;
9084                       ++args;
9085                     }
9086                 }
9087
9088               switch (c)
9089                 {
9090                 case 'x':
9091                 case 'y':
9092                 case 'z':
9093                 case 'v':
9094                 case 'w':
9095                 case 'Z':
9096                   regno = mips32_to_16_reg_map[regno];
9097                   break;
9098
9099                 case '0':
9100                   if (regno != 0)
9101                     regno = ILLEGAL_REG;
9102                   break;
9103
9104                 case 'S':
9105                   if (regno != SP)
9106                     regno = ILLEGAL_REG;
9107                   break;
9108
9109                 case 'R':
9110                   if (regno != RA)
9111                     regno = ILLEGAL_REG;
9112                   break;
9113
9114                 case 'X':
9115                 case 'Y':
9116                   if (regno == AT && ! mips_opts.noat)
9117                     as_warn (_("used $at without \".set noat\""));
9118                   break;
9119
9120                 default:
9121                   internalError ();
9122                 }
9123
9124               if (regno == ILLEGAL_REG)
9125                 break;
9126
9127               switch (c)
9128                 {
9129                 case 'x':
9130                 case 'v':
9131                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9132                   break;
9133                 case 'y':
9134                 case 'w':
9135                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9136                   break;
9137                 case 'z':
9138                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9139                   break;
9140                 case 'Z':
9141                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9142                 case '0':
9143                 case 'S':
9144                 case 'R':
9145                   break;
9146                 case 'X':
9147                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9148                   break;
9149                 case 'Y':
9150                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9151                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9152                   break;
9153                 default:
9154                   internalError ();
9155                 }
9156
9157               lastregno = regno;
9158               continue;
9159
9160             case 'P':
9161               if (strncmp (s, "$pc", 3) == 0)
9162                 {
9163                   s += 3;
9164                   continue;
9165                 }
9166               break;
9167
9168             case '<':
9169             case '>':
9170             case '[':
9171             case ']':
9172             case '4':
9173             case '5':
9174             case 'H':
9175             case 'W':
9176             case 'D':
9177             case 'j':
9178             case '8':
9179             case 'V':
9180             case 'C':
9181             case 'U':
9182             case 'k':
9183             case 'K':
9184               if (s[0] == '%'
9185                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9186                 {
9187                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9188                      and generate the appropriate reloc.  If the text
9189                      inside %gprel is not a symbol name with an
9190                      optional offset, then we generate a normal reloc
9191                      and will probably fail later.  */
9192                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9193                   if (imm_expr.X_op == O_symbol)
9194                     {
9195                       mips16_ext = true;
9196                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9197                       s = expr_end;
9198                       ip->use_extend = true;
9199                       ip->extend = 0;
9200                       continue;
9201                     }
9202                 }
9203               else
9204                 {
9205                   /* Just pick up a normal expression.  */
9206                   my_getExpression (&imm_expr, s);
9207                 }
9208
9209               if (imm_expr.X_op == O_register)
9210                 {
9211                   /* What we thought was an expression turned out to
9212                      be a register.  */
9213
9214                   if (s[0] == '(' && args[1] == '(')
9215                     {
9216                       /* It looks like the expression was omitted
9217                          before a register indirection, which means
9218                          that the expression is implicitly zero.  We
9219                          still set up imm_expr, so that we handle
9220                          explicit extensions correctly.  */
9221                       imm_expr.X_op = O_constant;
9222                       imm_expr.X_add_number = 0;
9223                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9224                       continue;
9225                     }
9226
9227                   break;
9228                 }
9229
9230               /* We need to relax this instruction.  */
9231               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9232               s = expr_end;
9233               continue;
9234
9235             case 'p':
9236             case 'q':
9237             case 'A':
9238             case 'B':
9239             case 'E':
9240               /* We use offset_reloc rather than imm_reloc for the PC
9241                  relative operands.  This lets macros with both
9242                  immediate and address operands work correctly.  */
9243               my_getExpression (&offset_expr, s);
9244
9245               if (offset_expr.X_op == O_register)
9246                 break;
9247
9248               /* We need to relax this instruction.  */
9249               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9250               s = expr_end;
9251               continue;
9252
9253             case '6':           /* break code */
9254               my_getExpression (&imm_expr, s);
9255               check_absolute_expr (ip, &imm_expr);
9256               if ((unsigned long) imm_expr.X_add_number > 63)
9257                 {
9258                   as_warn (_("Invalid value for `%s' (%lu)"),
9259                            ip->insn_mo->name,
9260                            (unsigned long) imm_expr.X_add_number);
9261                   imm_expr.X_add_number &= 0x3f;
9262                 }
9263               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9264               imm_expr.X_op = O_absent;
9265               s = expr_end;
9266               continue;
9267
9268             case 'a':           /* 26 bit address */
9269               my_getExpression (&offset_expr, s);
9270               s = expr_end;
9271               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9272               ip->insn_opcode <<= 16;
9273               continue;
9274
9275             case 'l':           /* register list for entry macro */
9276             case 'L':           /* register list for exit macro */
9277               {
9278                 int mask;
9279
9280                 if (c == 'l')
9281                   mask = 0;
9282                 else
9283                   mask = 7 << 3;
9284                 while (*s != '\0')
9285                   {
9286                     int freg, reg1, reg2;
9287
9288                     while (*s == ' ' || *s == ',')
9289                       ++s;
9290                     if (*s != '$')
9291                       {
9292                         as_bad (_("can't parse register list"));
9293                         break;
9294                       }
9295                     ++s;
9296                     if (*s != 'f')
9297                       freg = 0;
9298                     else
9299                       {
9300                         freg = 1;
9301                         ++s;
9302                       }
9303                     reg1 = 0;
9304                     while (ISDIGIT (*s))
9305                       {
9306                         reg1 *= 10;
9307                         reg1 += *s - '0';
9308                         ++s;
9309                       }
9310                     if (*s == ' ')
9311                       ++s;
9312                     if (*s != '-')
9313                       reg2 = reg1;
9314                     else
9315                       {
9316                         ++s;
9317                         if (*s != '$')
9318                           break;
9319                         ++s;
9320                         if (freg)
9321                           {
9322                             if (*s == 'f')
9323                               ++s;
9324                             else
9325                               {
9326                                 as_bad (_("invalid register list"));
9327                                 break;
9328                               }
9329                           }
9330                         reg2 = 0;
9331                         while (ISDIGIT (*s))
9332                           {
9333                             reg2 *= 10;
9334                             reg2 += *s - '0';
9335                             ++s;
9336                           }
9337                       }
9338                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9339                       {
9340                         mask &= ~ (7 << 3);
9341                         mask |= 5 << 3;
9342                       }
9343                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9344                       {
9345                         mask &= ~ (7 << 3);
9346                         mask |= 6 << 3;
9347                       }
9348                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9349                       mask |= (reg2 - 3) << 3;
9350                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9351                       mask |= (reg2 - 15) << 1;
9352                     else if (reg1 == RA && reg2 == RA)
9353                       mask |= 1;
9354                     else
9355                       {
9356                         as_bad (_("invalid register list"));
9357                         break;
9358                       }
9359                   }
9360                 /* The mask is filled in in the opcode table for the
9361                    benefit of the disassembler.  We remove it before
9362                    applying the actual mask.  */
9363                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9364                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9365               }
9366             continue;
9367
9368             case 'e':           /* extend code */
9369               my_getExpression (&imm_expr, s);
9370               check_absolute_expr (ip, &imm_expr);
9371               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9372                 {
9373                   as_warn (_("Invalid value for `%s' (%lu)"),
9374                            ip->insn_mo->name,
9375                            (unsigned long) imm_expr.X_add_number);
9376                   imm_expr.X_add_number &= 0x7ff;
9377                 }
9378               ip->insn_opcode |= imm_expr.X_add_number;
9379               imm_expr.X_op = O_absent;
9380               s = expr_end;
9381               continue;
9382
9383             default:
9384               internalError ();
9385             }
9386           break;
9387         }
9388
9389       /* Args don't match.  */
9390       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9391           strcmp (insn->name, insn[1].name) == 0)
9392         {
9393           ++insn;
9394           s = argsstart;
9395           continue;
9396         }
9397
9398       insn_error = _("illegal operands");
9399
9400       return;
9401     }
9402 }
9403
9404 /* This structure holds information we know about a mips16 immediate
9405    argument type.  */
9406
9407 struct mips16_immed_operand
9408 {
9409   /* The type code used in the argument string in the opcode table.  */
9410   int type;
9411   /* The number of bits in the short form of the opcode.  */
9412   int nbits;
9413   /* The number of bits in the extended form of the opcode.  */
9414   int extbits;
9415   /* The amount by which the short form is shifted when it is used;
9416      for example, the sw instruction has a shift count of 2.  */
9417   int shift;
9418   /* The amount by which the short form is shifted when it is stored
9419      into the instruction code.  */
9420   int op_shift;
9421   /* Non-zero if the short form is unsigned.  */
9422   int unsp;
9423   /* Non-zero if the extended form is unsigned.  */
9424   int extu;
9425   /* Non-zero if the value is PC relative.  */
9426   int pcrel;
9427 };
9428
9429 /* The mips16 immediate operand types.  */
9430
9431 static const struct mips16_immed_operand mips16_immed_operands[] =
9432 {
9433   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9434   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9435   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9436   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9437   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9438   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9439   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9440   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9441   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9442   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9443   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9444   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9445   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9446   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9447   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9448   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9449   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9450   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9451   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9452   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9453   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9454 };
9455
9456 #define MIPS16_NUM_IMMED \
9457   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9458
9459 /* Handle a mips16 instruction with an immediate value.  This or's the
9460    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9461    whether an extended value is needed; if one is needed, it sets
9462    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9463    If SMALL is true, an unextended opcode was explicitly requested.
9464    If EXT is true, an extended opcode was explicitly requested.  If
9465    WARN is true, warn if EXT does not match reality.  */
9466
9467 static void
9468 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9469               extend)
9470      char *file;
9471      unsigned int line;
9472      int type;
9473      offsetT val;
9474      boolean warn;
9475      boolean small;
9476      boolean ext;
9477      unsigned long *insn;
9478      boolean *use_extend;
9479      unsigned short *extend;
9480 {
9481   register const struct mips16_immed_operand *op;
9482   int mintiny, maxtiny;
9483   boolean needext;
9484
9485   op = mips16_immed_operands;
9486   while (op->type != type)
9487     {
9488       ++op;
9489       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9490     }
9491
9492   if (op->unsp)
9493     {
9494       if (type == '<' || type == '>' || type == '[' || type == ']')
9495         {
9496           mintiny = 1;
9497           maxtiny = 1 << op->nbits;
9498         }
9499       else
9500         {
9501           mintiny = 0;
9502           maxtiny = (1 << op->nbits) - 1;
9503         }
9504     }
9505   else
9506     {
9507       mintiny = - (1 << (op->nbits - 1));
9508       maxtiny = (1 << (op->nbits - 1)) - 1;
9509     }
9510
9511   /* Branch offsets have an implicit 0 in the lowest bit.  */
9512   if (type == 'p' || type == 'q')
9513     val /= 2;
9514
9515   if ((val & ((1 << op->shift) - 1)) != 0
9516       || val < (mintiny << op->shift)
9517       || val > (maxtiny << op->shift))
9518     needext = true;
9519   else
9520     needext = false;
9521
9522   if (warn && ext && ! needext)
9523     as_warn_where (file, line,
9524                    _("extended operand requested but not required"));
9525   if (small && needext)
9526     as_bad_where (file, line, _("invalid unextended operand value"));
9527
9528   if (small || (! ext && ! needext))
9529     {
9530       int insnval;
9531
9532       *use_extend = false;
9533       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9534       insnval <<= op->op_shift;
9535       *insn |= insnval;
9536     }
9537   else
9538     {
9539       long minext, maxext;
9540       int extval;
9541
9542       if (op->extu)
9543         {
9544           minext = 0;
9545           maxext = (1 << op->extbits) - 1;
9546         }
9547       else
9548         {
9549           minext = - (1 << (op->extbits - 1));
9550           maxext = (1 << (op->extbits - 1)) - 1;
9551         }
9552       if (val < minext || val > maxext)
9553         as_bad_where (file, line,
9554                       _("operand value out of range for instruction"));
9555
9556       *use_extend = true;
9557       if (op->extbits == 16)
9558         {
9559           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9560           val &= 0x1f;
9561         }
9562       else if (op->extbits == 15)
9563         {
9564           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9565           val &= 0xf;
9566         }
9567       else
9568         {
9569           extval = ((val & 0x1f) << 6) | (val & 0x20);
9570           val = 0;
9571         }
9572
9573       *extend = (unsigned short) extval;
9574       *insn |= val;
9575     }
9576 }
9577 \f
9578 static struct percent_op_match
9579 {
9580    const char *str;
9581    const enum small_ex_type type;
9582 } percent_op[] =
9583 {
9584   {"%lo", S_EX_LO},
9585 #ifdef OBJ_ELF
9586   {"%call_hi", S_EX_CALL_HI},
9587   {"%call_lo", S_EX_CALL_LO},
9588   {"%call16", S_EX_CALL16},
9589   {"%got_disp", S_EX_GOT_DISP},
9590   {"%got_page", S_EX_GOT_PAGE},
9591   {"%got_ofst", S_EX_GOT_OFST},
9592   {"%got_hi", S_EX_GOT_HI},
9593   {"%got_lo", S_EX_GOT_LO},
9594   {"%got", S_EX_GOT},
9595   {"%gp_rel", S_EX_GP_REL},
9596   {"%half", S_EX_HALF},
9597   {"%highest", S_EX_HIGHEST},
9598   {"%higher", S_EX_HIGHER},
9599   {"%neg", S_EX_NEG},
9600 #endif
9601   {"%hi", S_EX_HI}
9602 };
9603
9604 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9605    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9606    can be nested, this is handled by blanking the innermost, parsing the
9607    rest by subsequent calls.  */
9608
9609 static int
9610 my_getSmallParser (str, len, nestlevel)
9611      char **str;
9612      unsigned int *len;
9613      int *nestlevel;
9614 {
9615   *len = 0;
9616   *str += strspn (*str, " \t");
9617   /* Check for expression in parentheses.  */
9618   if (**str == '(')
9619     {
9620       char *b = *str + 1 + strspn (*str + 1, " \t");
9621       char *e;
9622
9623       /* Check for base register.  */
9624       if (b[0] == '$')
9625         {
9626           if (strchr (b, ')')
9627               && (e = b + strcspn (b, ") \t"))
9628               && e - b > 1 && e - b < 4)
9629             {
9630               if ((e - b == 3
9631                    && ((b[1] == 'f' && b[2] == 'p')
9632                        || (b[1] == 's' && b[2] == 'p')
9633                        || (b[1] == 'g' && b[2] == 'p')
9634                        || (b[1] == 'a' && b[2] == 't')
9635                        || (ISDIGIT (b[1])
9636                            && ISDIGIT (b[2]))))
9637                   || (ISDIGIT (b[1])))
9638                 {
9639                   *len = strcspn (*str, ")") + 1;
9640                   return S_EX_REGISTER;
9641                 }
9642             }
9643         }
9644       /* Check for percent_op (in parentheses).  */
9645       else if (b[0] == '%')
9646         {
9647           *str = b;
9648           return my_getPercentOp (str, len, nestlevel);
9649         }
9650
9651       /* Some other expression in the parentheses, which can contain
9652          parentheses itself. Attempt to find the matching one.  */
9653       {
9654         int pcnt = 1;
9655         char *s;
9656
9657         *len = 1;
9658         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9659           {
9660             if (*s == '(')
9661               ++pcnt;
9662             else if (*s == ')')
9663               --pcnt;
9664           }
9665       }
9666     }
9667   /* Check for percent_op (outside of parentheses).  */
9668   else if (*str[0] == '%')
9669     return my_getPercentOp (str, len, nestlevel);
9670
9671   /* Any other expression.  */
9672   return S_EX_NONE;
9673 }
9674
9675 static int
9676 my_getPercentOp (str, len, nestlevel)
9677      char **str;
9678      unsigned int *len;
9679      int *nestlevel;
9680 {
9681   char *tmp = *str + 1;
9682   unsigned int i = 0;
9683
9684   while (ISALPHA (*tmp) || *tmp == '_')
9685     {
9686       *tmp = TOLOWER (*tmp);
9687       tmp++;
9688     }
9689   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9690     {
9691       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9692         i++;
9693       else
9694         {
9695           int type = percent_op[i].type;
9696
9697           /* Only %hi and %lo are allowed for OldABI.  */
9698           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9699             return S_EX_NONE;
9700
9701           *len = strlen (percent_op[i].str);
9702           ++(*nestlevel);
9703           return type;
9704         }
9705     }
9706   return S_EX_NONE;
9707 }
9708
9709 static int
9710 my_getSmallExpression (ep, str)
9711      expressionS *ep;
9712      char *str;
9713 {
9714   static char *oldstr = NULL;
9715   int c = S_EX_NONE;
9716   int oldc;
9717   int nestlevel = -1;
9718   unsigned int len;
9719
9720   /* Don't update oldstr if the last call had nested percent_op's. We need
9721      it to parse the outer ones later.  */
9722   if (! oldstr)
9723     oldstr = str;
9724
9725   do
9726     {
9727       oldc = c;
9728       c = my_getSmallParser (&str, &len, &nestlevel);
9729       if (c != S_EX_NONE && c != S_EX_REGISTER)
9730         str += len;
9731     }
9732   while (c != S_EX_NONE && c != S_EX_REGISTER);
9733
9734   if (nestlevel >= 0)
9735     {
9736       /* A percent_op was encountered.  Don't try to get an expression if
9737          it is already blanked out.  */
9738       if (*(str + strspn (str + 1, " )")) != ')')
9739         {
9740           char save;
9741
9742           /* Let my_getExpression() stop at the closing parenthesis.  */
9743           save = *(str + len);
9744           *(str + len) = '\0';
9745           my_getExpression (ep, str);
9746           *(str + len) = save;
9747         }
9748       if (nestlevel > 0)
9749         {
9750           /* Blank out including the % sign and the proper matching
9751              parenthesis.  */
9752           int pcnt = 1;
9753           char *s = strrchr (oldstr, '%');
9754           char *end;
9755
9756           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9757             {
9758               if (*end == '(')
9759                 ++pcnt;
9760               else if (*end == ')')
9761                 --pcnt;
9762             }
9763
9764           memset (s, ' ', end - s);
9765           str = oldstr;
9766         }
9767       else
9768         expr_end = str + len;
9769
9770       c = oldc;
9771     }
9772   else if (c == S_EX_NONE)
9773     {
9774       my_getExpression (ep, str);
9775     }
9776   else if (c == S_EX_REGISTER)
9777     {
9778       ep->X_op = O_constant;
9779       expr_end = str;
9780       ep->X_add_symbol = NULL;
9781       ep->X_op_symbol = NULL;
9782       ep->X_add_number = 0;
9783     }
9784   else
9785     {
9786       as_fatal (_("internal error"));
9787     }
9788
9789   if (nestlevel <= 0)
9790     /* All percent_op's have been handled.  */
9791     oldstr = NULL;
9792
9793   return c;
9794 }
9795
9796 static void
9797 my_getExpression (ep, str)
9798      expressionS *ep;
9799      char *str;
9800 {
9801   char *save_in;
9802   valueT val;
9803
9804   save_in = input_line_pointer;
9805   input_line_pointer = str;
9806   expression (ep);
9807   expr_end = input_line_pointer;
9808   input_line_pointer = save_in;
9809
9810   /* If we are in mips16 mode, and this is an expression based on `.',
9811      then we bump the value of the symbol by 1 since that is how other
9812      text symbols are handled.  We don't bother to handle complex
9813      expressions, just `.' plus or minus a constant.  */
9814   if (mips_opts.mips16
9815       && ep->X_op == O_symbol
9816       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9817       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9818       && symbol_get_frag (ep->X_add_symbol) == frag_now
9819       && symbol_constant_p (ep->X_add_symbol)
9820       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9821     S_SET_VALUE (ep->X_add_symbol, val + 1);
9822 }
9823
9824 /* Turn a string in input_line_pointer into a floating point constant
9825    of type TYPE, and store the appropriate bytes in *LITP.  The number
9826    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9827    returned, or NULL on OK.  */
9828
9829 char *
9830 md_atof (type, litP, sizeP)
9831      int type;
9832      char *litP;
9833      int *sizeP;
9834 {
9835   int prec;
9836   LITTLENUM_TYPE words[4];
9837   char *t;
9838   int i;
9839
9840   switch (type)
9841     {
9842     case 'f':
9843       prec = 2;
9844       break;
9845
9846     case 'd':
9847       prec = 4;
9848       break;
9849
9850     default:
9851       *sizeP = 0;
9852       return _("bad call to md_atof");
9853     }
9854
9855   t = atof_ieee (input_line_pointer, type, words);
9856   if (t)
9857     input_line_pointer = t;
9858
9859   *sizeP = prec * 2;
9860
9861   if (! target_big_endian)
9862     {
9863       for (i = prec - 1; i >= 0; i--)
9864         {
9865           md_number_to_chars (litP, (valueT) words[i], 2);
9866           litP += 2;
9867         }
9868     }
9869   else
9870     {
9871       for (i = 0; i < prec; i++)
9872         {
9873           md_number_to_chars (litP, (valueT) words[i], 2);
9874           litP += 2;
9875         }
9876     }
9877
9878   return NULL;
9879 }
9880
9881 void
9882 md_number_to_chars (buf, val, n)
9883      char *buf;
9884      valueT val;
9885      int n;
9886 {
9887   if (target_big_endian)
9888     number_to_chars_bigendian (buf, val, n);
9889   else
9890     number_to_chars_littleendian (buf, val, n);
9891 }
9892 \f
9893 #ifdef OBJ_ELF
9894 static int support_64bit_objects(void)
9895 {
9896   const char **list, **l;
9897
9898   list = bfd_target_list ();
9899   for (l = list; *l != NULL; l++)
9900 #ifdef TE_TMIPS
9901     /* This is traditional mips */
9902     if (strcmp (*l, "elf64-tradbigmips") == 0
9903         || strcmp (*l, "elf64-tradlittlemips") == 0)
9904 #else
9905     if (strcmp (*l, "elf64-bigmips") == 0
9906         || strcmp (*l, "elf64-littlemips") == 0)
9907 #endif
9908       break;
9909   free (list);
9910   return (*l != NULL);
9911 }
9912 #endif /* OBJ_ELF */
9913
9914 const char *md_shortopts = "nO::g::G:";
9915
9916 struct option md_longopts[] =
9917 {
9918 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9919   {"mips0", no_argument, NULL, OPTION_MIPS1},
9920   {"mips1", no_argument, NULL, OPTION_MIPS1},
9921 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9922   {"mips2", no_argument, NULL, OPTION_MIPS2},
9923 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9924   {"mips3", no_argument, NULL, OPTION_MIPS3},
9925 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9926   {"mips4", no_argument, NULL, OPTION_MIPS4},
9927 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9928   {"mips5", no_argument, NULL, OPTION_MIPS5},
9929 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9930   {"mips32", no_argument, NULL, OPTION_MIPS32},
9931 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9932   {"mips64", no_argument, NULL, OPTION_MIPS64},
9933 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9934   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9935 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9936   {"trap", no_argument, NULL, OPTION_TRAP},
9937   {"no-break", no_argument, NULL, OPTION_TRAP},
9938 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9939   {"break", no_argument, NULL, OPTION_BREAK},
9940   {"no-trap", no_argument, NULL, OPTION_BREAK},
9941 #define OPTION_EB (OPTION_MD_BASE + 11)
9942   {"EB", no_argument, NULL, OPTION_EB},
9943 #define OPTION_EL (OPTION_MD_BASE + 12)
9944   {"EL", no_argument, NULL, OPTION_EL},
9945 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9946   {"mips16", no_argument, NULL, OPTION_MIPS16},
9947 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9948   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9949 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9950   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9951 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9952   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9953   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9954 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9955   {"mfp32", no_argument, NULL, OPTION_FP32},
9956 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9957   {"mgp32", no_argument, NULL, OPTION_GP32},
9958 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9959   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9960 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9961   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9962 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9963   {"march", required_argument, NULL, OPTION_MARCH},
9964 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9965   {"mtune", required_argument, NULL, OPTION_MTUNE},
9966 #define OPTION_FP64 (OPTION_MD_BASE + 23)
9967   {"mfp64", no_argument, NULL, OPTION_FP64},
9968 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9969   {"m4650", no_argument, NULL, OPTION_M4650},
9970 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9971   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9972 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9973   {"m4010", no_argument, NULL, OPTION_M4010},
9974 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9975   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9976 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9977   {"m4100", no_argument, NULL, OPTION_M4100},
9978 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9979   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9980 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9981   {"m3900", no_argument, NULL, OPTION_M3900},
9982 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9983   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9984 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9985   {"mgp64", no_argument, NULL, OPTION_GP64},
9986 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9987   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9988 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9989   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9990 #define OPTION_MDMX (OPTION_MD_BASE + 35)
9991   {"mdmx", no_argument, NULL, OPTION_MDMX},
9992 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9993   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9994 #ifdef OBJ_ELF
9995 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 37)
9996 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9997   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9998   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9999 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10000   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10001 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10002   {"xgot",        no_argument, NULL, OPTION_XGOT},
10003 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10004   {"mabi", required_argument, NULL, OPTION_MABI},
10005 #define OPTION_32          (OPTION_ELF_BASE + 4)
10006   {"32",          no_argument, NULL, OPTION_32},
10007 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10008   {"n32",         no_argument, NULL, OPTION_N32},
10009 #define OPTION_64          (OPTION_ELF_BASE + 6)
10010   {"64",          no_argument, NULL, OPTION_64},
10011 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10012   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10013 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10014   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10015 #endif /* OBJ_ELF */
10016   {NULL, no_argument, NULL, 0}
10017 };
10018 size_t md_longopts_size = sizeof (md_longopts);
10019
10020 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10021    NEW_VALUE.  Warn if another value was already specified.  Note:
10022    we have to defer parsing the -march and -mtune arguments in order
10023    to handle 'from-abi' correctly, since the ABI might be specified
10024    in a later argument.  */
10025
10026 static void
10027 mips_set_option_string (string_ptr, new_value)
10028      const char **string_ptr, *new_value;
10029 {
10030   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10031     as_warn (_("A different %s was already specified, is now %s"),
10032              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10033              new_value);
10034
10035   *string_ptr = new_value;
10036 }
10037
10038 int
10039 md_parse_option (c, arg)
10040      int c;
10041      char *arg;
10042 {
10043   switch (c)
10044     {
10045     case OPTION_CONSTRUCT_FLOATS:
10046       mips_disable_float_construction = 0;
10047       break;
10048
10049     case OPTION_NO_CONSTRUCT_FLOATS:
10050       mips_disable_float_construction = 1;
10051       break;
10052
10053     case OPTION_TRAP:
10054       mips_trap = 1;
10055       break;
10056
10057     case OPTION_BREAK:
10058       mips_trap = 0;
10059       break;
10060
10061     case OPTION_EB:
10062       target_big_endian = 1;
10063       break;
10064
10065     case OPTION_EL:
10066       target_big_endian = 0;
10067       break;
10068
10069     case 'n':
10070       warn_nops = 1;
10071       break;
10072
10073     case 'O':
10074       if (arg && arg[1] == '0')
10075         mips_optimize = 1;
10076       else
10077         mips_optimize = 2;
10078       break;
10079
10080     case 'g':
10081       if (arg == NULL)
10082         mips_debug = 2;
10083       else
10084         mips_debug = atoi (arg);
10085       /* When the MIPS assembler sees -g or -g2, it does not do
10086          optimizations which limit full symbolic debugging.  We take
10087          that to be equivalent to -O0.  */
10088       if (mips_debug == 2)
10089         mips_optimize = 1;
10090       break;
10091
10092     case OPTION_MIPS1:
10093       file_mips_isa = ISA_MIPS1;
10094       break;
10095
10096     case OPTION_MIPS2:
10097       file_mips_isa = ISA_MIPS2;
10098       break;
10099
10100     case OPTION_MIPS3:
10101       file_mips_isa = ISA_MIPS3;
10102       break;
10103
10104     case OPTION_MIPS4:
10105       file_mips_isa = ISA_MIPS4;
10106       break;
10107
10108     case OPTION_MIPS5:
10109       file_mips_isa = ISA_MIPS5;
10110       break;
10111
10112     case OPTION_MIPS32:
10113       file_mips_isa = ISA_MIPS32;
10114       break;
10115
10116     case OPTION_MIPS64:
10117       file_mips_isa = ISA_MIPS64;
10118       break;
10119
10120     case OPTION_MTUNE:
10121       mips_set_option_string (&mips_tune_string, arg);
10122       break;
10123
10124     case OPTION_MARCH:
10125       mips_set_option_string (&mips_arch_string, arg);
10126       break;
10127
10128     case OPTION_M4650:
10129       mips_set_option_string (&mips_arch_string, "4650");
10130       mips_set_option_string (&mips_tune_string, "4650");
10131       break;
10132
10133     case OPTION_NO_M4650:
10134       break;
10135
10136     case OPTION_M4010:
10137       mips_set_option_string (&mips_arch_string, "4010");
10138       mips_set_option_string (&mips_tune_string, "4010");
10139       break;
10140
10141     case OPTION_NO_M4010:
10142       break;
10143
10144     case OPTION_M4100:
10145       mips_set_option_string (&mips_arch_string, "4100");
10146       mips_set_option_string (&mips_tune_string, "4100");
10147       break;
10148
10149     case OPTION_NO_M4100:
10150       break;
10151
10152     case OPTION_M3900:
10153       mips_set_option_string (&mips_arch_string, "3900");
10154       mips_set_option_string (&mips_tune_string, "3900");
10155       break;
10156
10157     case OPTION_NO_M3900:
10158       break;
10159
10160     case OPTION_MDMX:
10161       mips_opts.ase_mdmx = 1;
10162       break;
10163
10164     case OPTION_NO_MDMX:
10165       mips_opts.ase_mdmx = 0;
10166       break;
10167
10168     case OPTION_MIPS16:
10169       mips_opts.mips16 = 1;
10170       mips_no_prev_insn (false);
10171       break;
10172
10173     case OPTION_NO_MIPS16:
10174       mips_opts.mips16 = 0;
10175       mips_no_prev_insn (false);
10176       break;
10177
10178     case OPTION_MIPS3D:
10179       mips_opts.ase_mips3d = 1;
10180       break;
10181
10182     case OPTION_NO_MIPS3D:
10183       mips_opts.ase_mips3d = 0;
10184       break;
10185
10186     case OPTION_MEMBEDDED_PIC:
10187       mips_pic = EMBEDDED_PIC;
10188       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10189         {
10190           as_bad (_("-G may not be used with embedded PIC code"));
10191           return 0;
10192         }
10193       g_switch_value = 0x7fffffff;
10194       break;
10195
10196 #ifdef OBJ_ELF
10197       /* When generating ELF code, we permit -KPIC and -call_shared to
10198          select SVR4_PIC, and -non_shared to select no PIC.  This is
10199          intended to be compatible with Irix 5.  */
10200     case OPTION_CALL_SHARED:
10201       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10202         {
10203           as_bad (_("-call_shared is supported only for ELF format"));
10204           return 0;
10205         }
10206       mips_pic = SVR4_PIC;
10207       if (g_switch_seen && g_switch_value != 0)
10208         {
10209           as_bad (_("-G may not be used with SVR4 PIC code"));
10210           return 0;
10211         }
10212       g_switch_value = 0;
10213       break;
10214
10215     case OPTION_NON_SHARED:
10216       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10217         {
10218           as_bad (_("-non_shared is supported only for ELF format"));
10219           return 0;
10220         }
10221       mips_pic = NO_PIC;
10222       break;
10223
10224       /* The -xgot option tells the assembler to use 32 offsets when
10225          accessing the got in SVR4_PIC mode.  It is for Irix
10226          compatibility.  */
10227     case OPTION_XGOT:
10228       mips_big_got = 1;
10229       break;
10230 #endif /* OBJ_ELF */
10231
10232     case 'G':
10233       if (! USE_GLOBAL_POINTER_OPT)
10234         {
10235           as_bad (_("-G is not supported for this configuration"));
10236           return 0;
10237         }
10238       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10239         {
10240           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10241           return 0;
10242         }
10243       else
10244         g_switch_value = atoi (arg);
10245       g_switch_seen = 1;
10246       break;
10247
10248 #ifdef OBJ_ELF
10249       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10250          and -mabi=64.  */
10251     case OPTION_32:
10252       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10253         {
10254           as_bad (_("-32 is supported for ELF format only"));
10255           return 0;
10256         }
10257       mips_abi = O32_ABI;
10258       break;
10259
10260     case OPTION_N32:
10261       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10262         {
10263           as_bad (_("-n32 is supported for ELF format only"));
10264           return 0;
10265         }
10266       mips_abi = N32_ABI;
10267       break;
10268
10269     case OPTION_64:
10270       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10271         {
10272           as_bad (_("-64 is supported for ELF format only"));
10273           return 0;
10274         }
10275       mips_abi = N64_ABI;
10276       if (! support_64bit_objects())
10277         as_fatal (_("No compiled in support for 64 bit object file format"));
10278       break;
10279 #endif /* OBJ_ELF */
10280
10281     case OPTION_GP32:
10282       file_mips_gp32 = 1;
10283       break;
10284
10285     case OPTION_GP64:
10286       file_mips_gp32 = 0;
10287       break;
10288
10289     case OPTION_FP32:
10290       file_mips_fp32 = 1;
10291       break;
10292
10293     case OPTION_FP64:
10294       file_mips_fp32 = 0;
10295       break;
10296
10297 #ifdef OBJ_ELF
10298     case OPTION_MABI:
10299       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10300         {
10301           as_bad (_("-mabi is supported for ELF format only"));
10302           return 0;
10303         }
10304       if (strcmp (arg, "32") == 0)
10305         mips_abi = O32_ABI;
10306       else if (strcmp (arg, "o64") == 0)
10307         mips_abi = O64_ABI;
10308       else if (strcmp (arg, "n32") == 0)
10309         mips_abi = N32_ABI;
10310       else if (strcmp (arg, "64") == 0)
10311         {
10312           mips_abi = N64_ABI;
10313           if (! support_64bit_objects())
10314             as_fatal (_("No compiled in support for 64 bit object file "
10315                         "format"));
10316         }
10317       else if (strcmp (arg, "eabi") == 0)
10318         mips_abi = EABI_ABI;
10319       else
10320         {
10321           as_fatal (_("invalid abi -mabi=%s"), arg);
10322           return 0;
10323         }
10324       break;
10325 #endif /* OBJ_ELF */
10326
10327     case OPTION_M7000_HILO_FIX:
10328       mips_7000_hilo_fix = true;
10329       break;
10330
10331     case OPTION_MNO_7000_HILO_FIX:
10332       mips_7000_hilo_fix = false;
10333       break;
10334
10335 #ifdef OBJ_ELF
10336     case OPTION_MDEBUG:
10337       mips_flag_mdebug = true;
10338       break;
10339
10340     case OPTION_NO_MDEBUG:
10341       mips_flag_mdebug = false;
10342       break;
10343 #endif /* OBJ_ELF */
10344
10345     default:
10346       return 0;
10347     }
10348
10349   return 1;
10350 }
10351 \f
10352 /* Set up globals to generate code for the ISA or processor
10353    described by INFO.  */
10354
10355 static void
10356 mips_set_architecture (info)
10357      const struct mips_cpu_info *info;
10358 {
10359   if (info != 0)
10360     {
10361       mips_arch_info = info;
10362       mips_arch = info->cpu;
10363       mips_opts.isa = info->isa;
10364     }
10365 }
10366
10367
10368 /* Likewise for tuning.  */
10369
10370 static void
10371 mips_set_tune (info)
10372      const struct mips_cpu_info *info;
10373 {
10374   if (info != 0)
10375     {
10376       mips_tune_info = info;
10377       mips_tune = info->cpu;
10378     }
10379 }
10380
10381
10382 void
10383 mips_after_parse_args ()
10384 {
10385   /* GP relative stuff not working for PE */
10386   if (strncmp (TARGET_OS, "pe", 2) == 0
10387       && g_switch_value != 0)
10388     {
10389       if (g_switch_seen)
10390         as_bad (_("-G not supported in this configuration."));
10391       g_switch_value = 0;
10392     }
10393
10394   /* The following code determines the architecture and register size.
10395      Similar code was added to GCC 3.3 (see override_options() in
10396      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10397      as much as possible.  */
10398
10399   if (mips_arch_string != 0)
10400     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10401
10402   if (mips_tune_string != 0)
10403     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10404
10405   if (file_mips_isa != ISA_UNKNOWN)
10406     {
10407       /* Handle -mipsN.  At this point, file_mips_isa contains the
10408          ISA level specified by -mipsN, while mips_opts.isa contains
10409          the -march selection (if any).  */
10410       if (mips_arch_info != 0)
10411         {
10412           /* -march takes precedence over -mipsN, since it is more descriptive.
10413              There's no harm in specifying both as long as the ISA levels
10414              are the same.  */
10415           if (file_mips_isa != mips_opts.isa)
10416             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10417                     mips_cpu_info_from_isa (file_mips_isa)->name,
10418                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10419         }
10420       else
10421         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10422     }
10423
10424   if (mips_arch_info == 0)
10425     mips_set_architecture (mips_parse_cpu ("default CPU",
10426                                            MIPS_CPU_STRING_DEFAULT));
10427
10428   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10429     as_bad ("-march=%s is not compatible with the selected ABI",
10430             mips_arch_info->name);
10431
10432   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10433   if (mips_tune_info == 0)
10434     mips_set_tune (mips_arch_info);
10435
10436   if (file_mips_gp32 >= 0)
10437     {
10438       /* The user specified the size of the integer registers.  Make sure
10439          it agrees with the ABI and ISA.  */
10440       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10441         as_bad (_("-mgp64 used with a 32-bit processor"));
10442       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10443         as_bad (_("-mgp32 used with a 64-bit ABI"));
10444       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10445         as_bad (_("-mgp64 used with a 32-bit ABI"));
10446     }
10447   else
10448     {
10449       /* Infer the integer register size from the ABI and processor.
10450          Restrict ourselves to 32-bit registers if that's all the
10451          processor has, or if the ABI cannot handle 64-bit registers.  */
10452       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10453                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10454     }
10455
10456   /* ??? GAS treats single-float processors as though they had 64-bit
10457      float registers (although it complains when double-precision
10458      instructions are used).  As things stand, saying they have 32-bit
10459      registers would lead to spurious "register must be even" messages.
10460      So here we assume float registers are always the same size as
10461      integer ones, unless the user says otherwise.  */
10462   if (file_mips_fp32 < 0)
10463     file_mips_fp32 = file_mips_gp32;
10464
10465   /* End of GCC-shared inference code.  */
10466
10467   /* ??? When do we want this flag to be set?   Who uses it?  */
10468   if (file_mips_gp32 == 1
10469       && mips_abi == NO_ABI
10470       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10471     mips_32bitmode = 1;
10472
10473   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10474     as_bad (_("trap exception not supported at ISA 1"));
10475
10476   /* If the selected architecture includes support for ASEs, enable
10477      generation of code for them.  */
10478   if (mips_opts.mips16 == -1)
10479     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10480   if (mips_opts.ase_mips3d == -1)
10481     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10482   if (mips_opts.ase_mdmx == -1)
10483     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10484
10485   file_mips_isa = mips_opts.isa;
10486   file_ase_mips16 = mips_opts.mips16;
10487   file_ase_mips3d = mips_opts.ase_mips3d;
10488   file_ase_mdmx = mips_opts.ase_mdmx;
10489   mips_opts.gp32 = file_mips_gp32;
10490   mips_opts.fp32 = file_mips_fp32;
10491
10492   if (mips_flag_mdebug < 0)
10493     {
10494 #ifdef OBJ_MAYBE_ECOFF
10495       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10496         mips_flag_mdebug = 1;
10497       else
10498 #endif /* OBJ_MAYBE_ECOFF */
10499         mips_flag_mdebug = 0;
10500     }
10501 }
10502 \f
10503 void
10504 mips_init_after_args ()
10505 {
10506   /* initialize opcodes */
10507   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10508   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10509 }
10510
10511 long
10512 md_pcrel_from (fixP)
10513      fixS *fixP;
10514 {
10515   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10516       && fixP->fx_addsy != (symbolS *) NULL
10517       && ! S_IS_DEFINED (fixP->fx_addsy))
10518     {
10519       /* This makes a branch to an undefined symbol be a branch to the
10520          current location.  */
10521       if (mips_pic == EMBEDDED_PIC)
10522         return 4;
10523       else
10524         return 1;
10525     }
10526
10527   /* Return the address of the delay slot.  */
10528   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10529 }
10530
10531 /* This is called before the symbol table is processed.  In order to
10532    work with gcc when using mips-tfile, we must keep all local labels.
10533    However, in other cases, we want to discard them.  If we were
10534    called with -g, but we didn't see any debugging information, it may
10535    mean that gcc is smuggling debugging information through to
10536    mips-tfile, in which case we must generate all local labels.  */
10537
10538 void
10539 mips_frob_file_before_adjust ()
10540 {
10541 #ifndef NO_ECOFF_DEBUGGING
10542   if (ECOFF_DEBUGGING
10543       && mips_debug != 0
10544       && ! ecoff_debugging_seen)
10545     flag_keep_locals = 1;
10546 #endif
10547 }
10548
10549 /* Sort any unmatched HI16_S relocs so that they immediately precede
10550    the corresponding LO reloc.  This is called before md_apply_fix3 and
10551    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10552    explicit use of the %hi modifier.  */
10553
10554 void
10555 mips_frob_file ()
10556 {
10557   struct mips_hi_fixup *l;
10558
10559   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10560     {
10561       segment_info_type *seginfo;
10562       int pass;
10563
10564       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10565
10566       /* Check quickly whether the next fixup happens to be a matching
10567          %lo.  */
10568       if (l->fixp->fx_next != NULL
10569           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10570           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10571           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10572         continue;
10573
10574       /* Look through the fixups for this segment for a matching %lo.
10575          When we find one, move the %hi just in front of it.  We do
10576          this in two passes.  In the first pass, we try to find a
10577          unique %lo.  In the second pass, we permit multiple %hi
10578          relocs for a single %lo (this is a GNU extension).  */
10579       seginfo = seg_info (l->seg);
10580       for (pass = 0; pass < 2; pass++)
10581         {
10582           fixS *f, *prev;
10583
10584           prev = NULL;
10585           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10586             {
10587               /* Check whether this is a %lo fixup which matches l->fixp.  */
10588               if (f->fx_r_type == BFD_RELOC_LO16
10589                   && f->fx_addsy == l->fixp->fx_addsy
10590                   && f->fx_offset == l->fixp->fx_offset
10591                   && (pass == 1
10592                       || prev == NULL
10593                       || prev->fx_r_type != BFD_RELOC_HI16_S
10594                       || prev->fx_addsy != f->fx_addsy
10595                       || prev->fx_offset !=  f->fx_offset))
10596                 {
10597                   fixS **pf;
10598
10599                   /* Move l->fixp before f.  */
10600                   for (pf = &seginfo->fix_root;
10601                        *pf != l->fixp;
10602                        pf = &(*pf)->fx_next)
10603                     assert (*pf != NULL);
10604
10605                   *pf = l->fixp->fx_next;
10606
10607                   l->fixp->fx_next = f;
10608                   if (prev == NULL)
10609                     seginfo->fix_root = l->fixp;
10610                   else
10611                     prev->fx_next = l->fixp;
10612
10613                   break;
10614                 }
10615
10616               prev = f;
10617             }
10618
10619           if (f != NULL)
10620             break;
10621
10622 #if 0 /* GCC code motion plus incomplete dead code elimination
10623          can leave a %hi without a %lo.  */
10624           if (pass == 1)
10625             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10626                            _("Unmatched %%hi reloc"));
10627 #endif
10628         }
10629     }
10630 }
10631
10632 /* When generating embedded PIC code we need to use a special
10633    relocation to represent the difference of two symbols in the .text
10634    section (switch tables use a difference of this sort).  See
10635    include/coff/mips.h for details.  This macro checks whether this
10636    fixup requires the special reloc.  */
10637 #define SWITCH_TABLE(fixp) \
10638   ((fixp)->fx_r_type == BFD_RELOC_32 \
10639    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10640    && (fixp)->fx_addsy != NULL \
10641    && (fixp)->fx_subsy != NULL \
10642    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10643    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10644
10645 /* When generating embedded PIC code we must keep all PC relative
10646    relocations, in case the linker has to relax a call.  We also need
10647    to keep relocations for switch table entries.
10648
10649    We may have combined relocations without symbols in the N32/N64 ABI.
10650    We have to prevent gas from dropping them.  */
10651
10652 int
10653 mips_force_relocation (fixp)
10654      fixS *fixp;
10655 {
10656   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10657       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10658       || S_FORCE_RELOC (fixp->fx_addsy))
10659     return 1;
10660
10661   if (HAVE_NEWABI
10662       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10663       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10664           || fixp->fx_r_type == BFD_RELOC_HI16_S
10665           || fixp->fx_r_type == BFD_RELOC_LO16))
10666     return 1;
10667
10668   return (mips_pic == EMBEDDED_PIC
10669           && (fixp->fx_pcrel
10670               || SWITCH_TABLE (fixp)
10671               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10672               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10673 }
10674
10675 #ifdef OBJ_ELF
10676 static int
10677 mips_need_elf_addend_fixup (fixP)
10678      fixS *fixP;
10679 {
10680   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10681     return 1;
10682   if (mips_pic == EMBEDDED_PIC
10683       && S_IS_WEAK (fixP->fx_addsy))
10684     return 1;
10685   if (mips_pic != EMBEDDED_PIC
10686       && (S_IS_WEAK (fixP->fx_addsy)
10687           || S_IS_EXTERNAL (fixP->fx_addsy))
10688       && !S_IS_COMMON (fixP->fx_addsy))
10689     return 1;
10690   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10691       && (((bfd_get_section_flags (stdoutput,
10692                                    S_GET_SEGMENT (fixP->fx_addsy))
10693             & SEC_LINK_ONCE) != 0)
10694           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10695                        ".gnu.linkonce",
10696                        sizeof (".gnu.linkonce") - 1)))
10697     return 1;
10698   return 0;
10699 }
10700 #endif
10701
10702 /* Apply a fixup to the object file.  */
10703
10704 void
10705 md_apply_fix3 (fixP, valP, seg)
10706      fixS *fixP;
10707      valueT *valP;
10708      segT seg ATTRIBUTE_UNUSED;
10709 {
10710   bfd_byte *buf;
10711   long insn;
10712   valueT value;
10713   static int previous_fx_r_type = 0;
10714
10715   /* FIXME: Maybe just return for all reloc types not listed below?
10716      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
10717   if (fixP->fx_r_type == BFD_RELOC_8)
10718       return;
10719
10720   assert (fixP->fx_size == 4
10721           || fixP->fx_r_type == BFD_RELOC_16
10722           || fixP->fx_r_type == BFD_RELOC_32
10723           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10724           || fixP->fx_r_type == BFD_RELOC_HI16_S
10725           || fixP->fx_r_type == BFD_RELOC_LO16
10726           || fixP->fx_r_type == BFD_RELOC_GPREL16
10727           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10728           || fixP->fx_r_type == BFD_RELOC_GPREL32
10729           || fixP->fx_r_type == BFD_RELOC_64
10730           || fixP->fx_r_type == BFD_RELOC_CTOR
10731           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10732           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10733           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10734           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10735           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10736           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10737           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10738           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10739           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10740
10741   value = *valP;
10742
10743   /* If we aren't adjusting this fixup to be against the section
10744      symbol, we need to adjust the value.  */
10745 #ifdef OBJ_ELF
10746   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10747     {
10748       if (mips_need_elf_addend_fixup (fixP))
10749         {
10750           reloc_howto_type *howto;
10751           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10752
10753           value -= symval;
10754
10755           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10756           if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
10757             {
10758               /* In this case, the bfd_install_relocation routine will
10759                  incorrectly add the symbol value back in.  We just want
10760                  the addend to appear in the object file.  */
10761               value -= symval;
10762
10763               /* Make sure the addend is still non-zero.  If it became zero
10764                  after the last operation, set it to a spurious value and
10765                  subtract the same value from the object file's contents.  */
10766               if (value == 0)
10767                 {
10768                   value = 8;
10769
10770                   /* The in-place addends for LO16 relocations are signed;
10771                      leave the matching HI16 in-place addends as zero.  */
10772                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10773                     {
10774                       bfd_vma contents, mask, field;
10775
10776                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10777                                                + fixP->fx_where,
10778                                                fixP->fx_size * 8,
10779                                                target_big_endian);
10780
10781                       /* MASK has bits set where the relocation should go.
10782                          FIELD is -value, shifted into the appropriate place
10783                          for this relocation.  */
10784                       mask = 1 << (howto->bitsize - 1);
10785                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10786                       field = (-value >> howto->rightshift) << howto->bitpos;
10787
10788                       bfd_put_bits ((field & mask) | (contents & ~mask),
10789                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10790                                     fixP->fx_size * 8,
10791                                     target_big_endian);
10792                     }
10793                 }
10794             }
10795         }
10796
10797       /* This code was generated using trial and error and so is
10798          fragile and not trustworthy.  If you change it, you should
10799          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10800          they still pass.  */
10801       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10802         {
10803           value += fixP->fx_frag->fr_address + fixP->fx_where;
10804
10805           /* BFD's REL handling, for MIPS, is _very_ weird.
10806              This gives the right results, but it can't possibly
10807              be the way things are supposed to work.  */
10808           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10809                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10810               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10811             value += fixP->fx_frag->fr_address + fixP->fx_where;
10812         }
10813     }
10814 #endif
10815
10816   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10817
10818   /* We are not done if this is a composite relocation to set up gp.  */
10819   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
10820       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10821            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
10822                && (fixP->fx_r_type == BFD_RELOC_HI16_S
10823                    || fixP->fx_r_type == BFD_RELOC_LO16))))
10824     fixP->fx_done = 1;
10825   previous_fx_r_type = fixP->fx_r_type;
10826
10827   switch (fixP->fx_r_type)
10828     {
10829     case BFD_RELOC_MIPS_JMP:
10830     case BFD_RELOC_MIPS_SHIFT5:
10831     case BFD_RELOC_MIPS_SHIFT6:
10832     case BFD_RELOC_MIPS_GOT_DISP:
10833     case BFD_RELOC_MIPS_GOT_PAGE:
10834     case BFD_RELOC_MIPS_GOT_OFST:
10835     case BFD_RELOC_MIPS_SUB:
10836     case BFD_RELOC_MIPS_INSERT_A:
10837     case BFD_RELOC_MIPS_INSERT_B:
10838     case BFD_RELOC_MIPS_DELETE:
10839     case BFD_RELOC_MIPS_HIGHEST:
10840     case BFD_RELOC_MIPS_HIGHER:
10841     case BFD_RELOC_MIPS_SCN_DISP:
10842     case BFD_RELOC_MIPS_REL16:
10843     case BFD_RELOC_MIPS_RELGOT:
10844     case BFD_RELOC_MIPS_JALR:
10845     case BFD_RELOC_HI16:
10846     case BFD_RELOC_HI16_S:
10847     case BFD_RELOC_GPREL16:
10848     case BFD_RELOC_MIPS_LITERAL:
10849     case BFD_RELOC_MIPS_CALL16:
10850     case BFD_RELOC_MIPS_GOT16:
10851     case BFD_RELOC_GPREL32:
10852     case BFD_RELOC_MIPS_GOT_HI16:
10853     case BFD_RELOC_MIPS_GOT_LO16:
10854     case BFD_RELOC_MIPS_CALL_HI16:
10855     case BFD_RELOC_MIPS_CALL_LO16:
10856     case BFD_RELOC_MIPS16_GPREL:
10857       if (fixP->fx_pcrel)
10858         as_bad_where (fixP->fx_file, fixP->fx_line,
10859                       _("Invalid PC relative reloc"));
10860       /* Nothing needed to do. The value comes from the reloc entry */
10861       break;
10862
10863     case BFD_RELOC_MIPS16_JMP:
10864       /* We currently always generate a reloc against a symbol, which
10865          means that we don't want an addend even if the symbol is
10866          defined.  */
10867       fixP->fx_addnumber = 0;
10868       break;
10869
10870     case BFD_RELOC_PCREL_HI16_S:
10871       /* The addend for this is tricky if it is internal, so we just
10872          do everything here rather than in bfd_install_relocation.  */
10873       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10874           && !fixP->fx_done
10875           && value != 0)
10876         break;
10877       if (fixP->fx_addsy
10878           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10879         {
10880           /* For an external symbol adjust by the address to make it
10881              pcrel_offset.  We use the address of the RELLO reloc
10882              which follows this one.  */
10883           value += (fixP->fx_next->fx_frag->fr_address
10884                     + fixP->fx_next->fx_where);
10885         }
10886       value = ((value + 0x8000) >> 16) & 0xffff;
10887       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10888       if (target_big_endian)
10889         buf += 2;
10890       md_number_to_chars ((char *) buf, value, 2);
10891       break;
10892
10893     case BFD_RELOC_PCREL_LO16:
10894       /* The addend for this is tricky if it is internal, so we just
10895          do everything here rather than in bfd_install_relocation.  */
10896       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10897           && !fixP->fx_done
10898           && value != 0)
10899         break;
10900       if (fixP->fx_addsy
10901           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10902         value += fixP->fx_frag->fr_address + fixP->fx_where;
10903       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10904       if (target_big_endian)
10905         buf += 2;
10906       md_number_to_chars ((char *) buf, value, 2);
10907       break;
10908
10909     case BFD_RELOC_64:
10910       /* This is handled like BFD_RELOC_32, but we output a sign
10911          extended value if we are only 32 bits.  */
10912       if (fixP->fx_done
10913           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10914         {
10915           if (8 <= sizeof (valueT))
10916             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10917                                 value, 8);
10918           else
10919             {
10920               long w1, w2;
10921               long hiv;
10922
10923               w1 = w2 = fixP->fx_where;
10924               if (target_big_endian)
10925                 w1 += 4;
10926               else
10927                 w2 += 4;
10928               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10929               if ((value & 0x80000000) != 0)
10930                 hiv = 0xffffffff;
10931               else
10932                 hiv = 0;
10933               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10934             }
10935         }
10936       break;
10937
10938     case BFD_RELOC_RVA:
10939     case BFD_RELOC_32:
10940       /* If we are deleting this reloc entry, we must fill in the
10941          value now.  This can happen if we have a .word which is not
10942          resolved when it appears but is later defined.  We also need
10943          to fill in the value if this is an embedded PIC switch table
10944          entry.  */
10945       if (fixP->fx_done
10946           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10947         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10948                             value, 4);
10949       break;
10950
10951     case BFD_RELOC_16:
10952       /* If we are deleting this reloc entry, we must fill in the
10953          value now.  */
10954       assert (fixP->fx_size == 2);
10955       if (fixP->fx_done)
10956         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10957                             value, 2);
10958       break;
10959
10960     case BFD_RELOC_LO16:
10961       /* When handling an embedded PIC switch statement, we can wind
10962          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
10963       if (fixP->fx_done)
10964         {
10965           if (value + 0x8000 > 0xffff)
10966             as_bad_where (fixP->fx_file, fixP->fx_line,
10967                           _("relocation overflow"));
10968           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10969           if (target_big_endian)
10970             buf += 2;
10971           md_number_to_chars ((char *) buf, value, 2);
10972         }
10973       break;
10974
10975     case BFD_RELOC_16_PCREL_S2:
10976       if ((value & 0x3) != 0)
10977         as_bad_where (fixP->fx_file, fixP->fx_line,
10978                       _("Branch to odd address (%lx)"), (long) value);
10979
10980       /* Fall through.  */
10981
10982     case BFD_RELOC_16_PCREL:
10983       /*
10984        * We need to save the bits in the instruction since fixup_segment()
10985        * might be deleting the relocation entry (i.e., a branch within
10986        * the current segment).
10987        */
10988       if (!fixP->fx_done && value != 0)
10989         break;
10990       /* If 'value' is zero, the remaining reloc code won't actually
10991          do the store, so it must be done here.  This is probably
10992          a bug somewhere.  */
10993       if (!fixP->fx_done
10994           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10995               || fixP->fx_addsy == NULL                 /* ??? */
10996               || ! S_IS_DEFINED (fixP->fx_addsy)))
10997         value -= fixP->fx_frag->fr_address + fixP->fx_where;
10998
10999       value = (offsetT) value >> 2;
11000
11001       /* update old instruction data */
11002       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11003       if (target_big_endian)
11004         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11005       else
11006         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11007
11008       if (value + 0x8000 <= 0xffff)
11009         insn |= value & 0xffff;
11010       else
11011         {
11012           /* The branch offset is too large.  If this is an
11013              unconditional branch, and we are not generating PIC code,
11014              we can convert it to an absolute jump instruction.  */
11015           if (mips_pic == NO_PIC
11016               && fixP->fx_done
11017               && fixP->fx_frag->fr_address >= text_section->vma
11018               && (fixP->fx_frag->fr_address
11019                   < text_section->vma + text_section->_raw_size)
11020               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11021                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11022                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11023             {
11024               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11025                 insn = 0x0c000000;      /* jal */
11026               else
11027                 insn = 0x08000000;      /* j */
11028               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11029               fixP->fx_done = 0;
11030               fixP->fx_addsy = section_symbol (text_section);
11031               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11032             }
11033           else
11034             {
11035               /* FIXME.  It would be possible in principle to handle
11036                  conditional branches which overflow.  They could be
11037                  transformed into a branch around a jump.  This would
11038                  require setting up variant frags for each different
11039                  branch type.  The native MIPS assembler attempts to
11040                  handle these cases, but it appears to do it
11041                  incorrectly.  */
11042               as_bad_where (fixP->fx_file, fixP->fx_line,
11043                             _("Branch out of range"));
11044             }
11045         }
11046
11047       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11048       break;
11049
11050     case BFD_RELOC_VTABLE_INHERIT:
11051       fixP->fx_done = 0;
11052       if (fixP->fx_addsy
11053           && !S_IS_DEFINED (fixP->fx_addsy)
11054           && !S_IS_WEAK (fixP->fx_addsy))
11055         S_SET_WEAK (fixP->fx_addsy);
11056       break;
11057
11058     case BFD_RELOC_VTABLE_ENTRY:
11059       fixP->fx_done = 0;
11060       break;
11061
11062     default:
11063       internalError ();
11064     }
11065 }
11066
11067 #if 0
11068 void
11069 printInsn (oc)
11070      unsigned long oc;
11071 {
11072   const struct mips_opcode *p;
11073   int treg, sreg, dreg, shamt;
11074   short imm;
11075   const char *args;
11076   int i;
11077
11078   for (i = 0; i < NUMOPCODES; ++i)
11079     {
11080       p = &mips_opcodes[i];
11081       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11082         {
11083           printf ("%08lx %s\t", oc, p->name);
11084           treg = (oc >> 16) & 0x1f;
11085           sreg = (oc >> 21) & 0x1f;
11086           dreg = (oc >> 11) & 0x1f;
11087           shamt = (oc >> 6) & 0x1f;
11088           imm = oc;
11089           for (args = p->args;; ++args)
11090             {
11091               switch (*args)
11092                 {
11093                 case '\0':
11094                   printf ("\n");
11095                   break;
11096
11097                 case ',':
11098                 case '(':
11099                 case ')':
11100                   printf ("%c", *args);
11101                   continue;
11102
11103                 case 'r':
11104                   assert (treg == sreg);
11105                   printf ("$%d,$%d", treg, sreg);
11106                   continue;
11107
11108                 case 'd':
11109                 case 'G':
11110                   printf ("$%d", dreg);
11111                   continue;
11112
11113                 case 't':
11114                 case 'E':
11115                   printf ("$%d", treg);
11116                   continue;
11117
11118                 case 'k':
11119                   printf ("0x%x", treg);
11120                   continue;
11121
11122                 case 'b':
11123                 case 's':
11124                   printf ("$%d", sreg);
11125                   continue;
11126
11127                 case 'a':
11128                   printf ("0x%08lx", oc & 0x1ffffff);
11129                   continue;
11130
11131                 case 'i':
11132                 case 'j':
11133                 case 'o':
11134                 case 'u':
11135                   printf ("%d", imm);
11136                   continue;
11137
11138                 case '<':
11139                 case '>':
11140                   printf ("$%d", shamt);
11141                   continue;
11142
11143                 default:
11144                   internalError ();
11145                 }
11146               break;
11147             }
11148           return;
11149         }
11150     }
11151   printf (_("%08lx  UNDEFINED\n"), oc);
11152 }
11153 #endif
11154
11155 static symbolS *
11156 get_symbol ()
11157 {
11158   int c;
11159   char *name;
11160   symbolS *p;
11161
11162   name = input_line_pointer;
11163   c = get_symbol_end ();
11164   p = (symbolS *) symbol_find_or_make (name);
11165   *input_line_pointer = c;
11166   return p;
11167 }
11168
11169 /* Align the current frag to a given power of two.  The MIPS assembler
11170    also automatically adjusts any preceding label.  */
11171
11172 static void
11173 mips_align (to, fill, label)
11174      int to;
11175      int fill;
11176      symbolS *label;
11177 {
11178   mips_emit_delays (false);
11179   frag_align (to, fill, 0);
11180   record_alignment (now_seg, to);
11181   if (label != NULL)
11182     {
11183       assert (S_GET_SEGMENT (label) == now_seg);
11184       symbol_set_frag (label, frag_now);
11185       S_SET_VALUE (label, (valueT) frag_now_fix ());
11186     }
11187 }
11188
11189 /* Align to a given power of two.  .align 0 turns off the automatic
11190    alignment used by the data creating pseudo-ops.  */
11191
11192 static void
11193 s_align (x)
11194      int x ATTRIBUTE_UNUSED;
11195 {
11196   register int temp;
11197   register long temp_fill;
11198   long max_alignment = 15;
11199
11200   /*
11201
11202     o  Note that the assembler pulls down any immediately preceeding label
11203        to the aligned address.
11204     o  It's not documented but auto alignment is reinstated by
11205        a .align pseudo instruction.
11206     o  Note also that after auto alignment is turned off the mips assembler
11207        issues an error on attempt to assemble an improperly aligned data item.
11208        We don't.
11209
11210     */
11211
11212   temp = get_absolute_expression ();
11213   if (temp > max_alignment)
11214     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11215   else if (temp < 0)
11216     {
11217       as_warn (_("Alignment negative: 0 assumed."));
11218       temp = 0;
11219     }
11220   if (*input_line_pointer == ',')
11221     {
11222       ++input_line_pointer;
11223       temp_fill = get_absolute_expression ();
11224     }
11225   else
11226     temp_fill = 0;
11227   if (temp)
11228     {
11229       auto_align = 1;
11230       mips_align (temp, (int) temp_fill,
11231                   insn_labels != NULL ? insn_labels->label : NULL);
11232     }
11233   else
11234     {
11235       auto_align = 0;
11236     }
11237
11238   demand_empty_rest_of_line ();
11239 }
11240
11241 void
11242 mips_flush_pending_output ()
11243 {
11244   mips_emit_delays (false);
11245   mips_clear_insn_labels ();
11246 }
11247
11248 static void
11249 s_change_sec (sec)
11250      int sec;
11251 {
11252   segT seg;
11253
11254   /* When generating embedded PIC code, we only use the .text, .lit8,
11255      .sdata and .sbss sections.  We change the .data and .rdata
11256      pseudo-ops to use .sdata.  */
11257   if (mips_pic == EMBEDDED_PIC
11258       && (sec == 'd' || sec == 'r'))
11259     sec = 's';
11260
11261 #ifdef OBJ_ELF
11262   /* The ELF backend needs to know that we are changing sections, so
11263      that .previous works correctly.  We could do something like check
11264      for an obj_section_change_hook macro, but that might be confusing
11265      as it would not be appropriate to use it in the section changing
11266      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11267      This should be cleaner, somehow.  */
11268   obj_elf_section_change_hook ();
11269 #endif
11270
11271   mips_emit_delays (false);
11272   switch (sec)
11273     {
11274     case 't':
11275       s_text (0);
11276       break;
11277     case 'd':
11278       s_data (0);
11279       break;
11280     case 'b':
11281       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11282       demand_empty_rest_of_line ();
11283       break;
11284
11285     case 'r':
11286       if (USE_GLOBAL_POINTER_OPT)
11287         {
11288           seg = subseg_new (RDATA_SECTION_NAME,
11289                             (subsegT) get_absolute_expression ());
11290           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11291             {
11292               bfd_set_section_flags (stdoutput, seg,
11293                                      (SEC_ALLOC
11294                                       | SEC_LOAD
11295                                       | SEC_READONLY
11296                                       | SEC_RELOC
11297                                       | SEC_DATA));
11298               if (strcmp (TARGET_OS, "elf") != 0)
11299                 record_alignment (seg, 4);
11300             }
11301           demand_empty_rest_of_line ();
11302         }
11303       else
11304         {
11305           as_bad (_("No read only data section in this object file format"));
11306           demand_empty_rest_of_line ();
11307           return;
11308         }
11309       break;
11310
11311     case 's':
11312       if (USE_GLOBAL_POINTER_OPT)
11313         {
11314           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11315           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11316             {
11317               bfd_set_section_flags (stdoutput, seg,
11318                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11319                                      | SEC_DATA);
11320               if (strcmp (TARGET_OS, "elf") != 0)
11321                 record_alignment (seg, 4);
11322             }
11323           demand_empty_rest_of_line ();
11324           break;
11325         }
11326       else
11327         {
11328           as_bad (_("Global pointers not supported; recompile -G 0"));
11329           demand_empty_rest_of_line ();
11330           return;
11331         }
11332     }
11333
11334   auto_align = 1;
11335 }
11336   
11337 void
11338 s_change_section (ignore)
11339      int ignore ATTRIBUTE_UNUSED;
11340 {
11341 #ifdef OBJ_ELF
11342   char *section_name;
11343   char c;
11344   char next_c;
11345   int section_type;
11346   int section_flag;
11347   int section_entry_size;
11348   int section_alignment;
11349   
11350   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11351     return;
11352
11353   section_name = input_line_pointer;
11354   c = get_symbol_end ();
11355   next_c = *(input_line_pointer + 1);
11356
11357   /* Do we have .section Name<,"flags">?  */
11358   if (c != ',' || (c == ',' && next_c == '"'))
11359     {
11360       /* just after name is now '\0'.  */
11361       *input_line_pointer = c;
11362       input_line_pointer = section_name;
11363       obj_elf_section (ignore);
11364       return;
11365     }
11366   input_line_pointer++;
11367
11368   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11369   if (c == ',')
11370     section_type = get_absolute_expression ();
11371   else
11372     section_type = 0;
11373   if (*input_line_pointer++ == ',')
11374     section_flag = get_absolute_expression ();
11375   else
11376     section_flag = 0;
11377   if (*input_line_pointer++ == ',')
11378     section_entry_size = get_absolute_expression ();
11379   else
11380     section_entry_size = 0;
11381   if (*input_line_pointer++ == ',')
11382     section_alignment = get_absolute_expression ();
11383   else
11384     section_alignment = 0;
11385
11386   obj_elf_change_section (section_name, section_type, section_flag,
11387                           section_entry_size, 0, 0, 0);
11388 #endif /* OBJ_ELF */
11389 }
11390
11391 void
11392 mips_enable_auto_align ()
11393 {
11394   auto_align = 1;
11395 }
11396
11397 static void
11398 s_cons (log_size)
11399      int log_size;
11400 {
11401   symbolS *label;
11402
11403   label = insn_labels != NULL ? insn_labels->label : NULL;
11404   mips_emit_delays (false);
11405   if (log_size > 0 && auto_align)
11406     mips_align (log_size, 0, label);
11407   mips_clear_insn_labels ();
11408   cons (1 << log_size);
11409 }
11410
11411 static void
11412 s_float_cons (type)
11413      int type;
11414 {
11415   symbolS *label;
11416
11417   label = insn_labels != NULL ? insn_labels->label : NULL;
11418
11419   mips_emit_delays (false);
11420
11421   if (auto_align)
11422     {
11423       if (type == 'd')
11424         mips_align (3, 0, label);
11425       else
11426         mips_align (2, 0, label);
11427     }
11428
11429   mips_clear_insn_labels ();
11430
11431   float_cons (type);
11432 }
11433
11434 /* Handle .globl.  We need to override it because on Irix 5 you are
11435    permitted to say
11436        .globl foo .text
11437    where foo is an undefined symbol, to mean that foo should be
11438    considered to be the address of a function.  */
11439
11440 static void
11441 s_mips_globl (x)
11442      int x ATTRIBUTE_UNUSED;
11443 {
11444   char *name;
11445   int c;
11446   symbolS *symbolP;
11447   flagword flag;
11448
11449   name = input_line_pointer;
11450   c = get_symbol_end ();
11451   symbolP = symbol_find_or_make (name);
11452   *input_line_pointer = c;
11453   SKIP_WHITESPACE ();
11454
11455   /* On Irix 5, every global symbol that is not explicitly labelled as
11456      being a function is apparently labelled as being an object.  */
11457   flag = BSF_OBJECT;
11458
11459   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11460     {
11461       char *secname;
11462       asection *sec;
11463
11464       secname = input_line_pointer;
11465       c = get_symbol_end ();
11466       sec = bfd_get_section_by_name (stdoutput, secname);
11467       if (sec == NULL)
11468         as_bad (_("%s: no such section"), secname);
11469       *input_line_pointer = c;
11470
11471       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11472         flag = BSF_FUNCTION;
11473     }
11474
11475   symbol_get_bfdsym (symbolP)->flags |= flag;
11476
11477   S_SET_EXTERNAL (symbolP);
11478   demand_empty_rest_of_line ();
11479 }
11480
11481 static void
11482 s_option (x)
11483      int x ATTRIBUTE_UNUSED;
11484 {
11485   char *opt;
11486   char c;
11487
11488   opt = input_line_pointer;
11489   c = get_symbol_end ();
11490
11491   if (*opt == 'O')
11492     {
11493       /* FIXME: What does this mean?  */
11494     }
11495   else if (strncmp (opt, "pic", 3) == 0)
11496     {
11497       int i;
11498
11499       i = atoi (opt + 3);
11500       if (i == 0)
11501         mips_pic = NO_PIC;
11502       else if (i == 2)
11503         mips_pic = SVR4_PIC;
11504       else
11505         as_bad (_(".option pic%d not supported"), i);
11506
11507       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11508         {
11509           if (g_switch_seen && g_switch_value != 0)
11510             as_warn (_("-G may not be used with SVR4 PIC code"));
11511           g_switch_value = 0;
11512           bfd_set_gp_size (stdoutput, 0);
11513         }
11514     }
11515   else
11516     as_warn (_("Unrecognized option \"%s\""), opt);
11517
11518   *input_line_pointer = c;
11519   demand_empty_rest_of_line ();
11520 }
11521
11522 /* This structure is used to hold a stack of .set values.  */
11523
11524 struct mips_option_stack
11525 {
11526   struct mips_option_stack *next;
11527   struct mips_set_options options;
11528 };
11529
11530 static struct mips_option_stack *mips_opts_stack;
11531
11532 /* Handle the .set pseudo-op.  */
11533
11534 static void
11535 s_mipsset (x)
11536      int x ATTRIBUTE_UNUSED;
11537 {
11538   char *name = input_line_pointer, ch;
11539
11540   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11541     ++input_line_pointer;
11542   ch = *input_line_pointer;
11543   *input_line_pointer = '\0';
11544
11545   if (strcmp (name, "reorder") == 0)
11546     {
11547       if (mips_opts.noreorder && prev_nop_frag != NULL)
11548         {
11549           /* If we still have pending nops, we can discard them.  The
11550              usual nop handling will insert any that are still
11551              needed.  */
11552           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11553                                     * (mips_opts.mips16 ? 2 : 4));
11554           prev_nop_frag = NULL;
11555         }
11556       mips_opts.noreorder = 0;
11557     }
11558   else if (strcmp (name, "noreorder") == 0)
11559     {
11560       mips_emit_delays (true);
11561       mips_opts.noreorder = 1;
11562       mips_any_noreorder = 1;
11563     }
11564   else if (strcmp (name, "at") == 0)
11565     {
11566       mips_opts.noat = 0;
11567     }
11568   else if (strcmp (name, "noat") == 0)
11569     {
11570       mips_opts.noat = 1;
11571     }
11572   else if (strcmp (name, "macro") == 0)
11573     {
11574       mips_opts.warn_about_macros = 0;
11575     }
11576   else if (strcmp (name, "nomacro") == 0)
11577     {
11578       if (mips_opts.noreorder == 0)
11579         as_bad (_("`noreorder' must be set before `nomacro'"));
11580       mips_opts.warn_about_macros = 1;
11581     }
11582   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11583     {
11584       mips_opts.nomove = 0;
11585     }
11586   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11587     {
11588       mips_opts.nomove = 1;
11589     }
11590   else if (strcmp (name, "bopt") == 0)
11591     {
11592       mips_opts.nobopt = 0;
11593     }
11594   else if (strcmp (name, "nobopt") == 0)
11595     {
11596       mips_opts.nobopt = 1;
11597     }
11598   else if (strcmp (name, "mips16") == 0
11599            || strcmp (name, "MIPS-16") == 0)
11600     mips_opts.mips16 = 1;
11601   else if (strcmp (name, "nomips16") == 0
11602            || strcmp (name, "noMIPS-16") == 0)
11603     mips_opts.mips16 = 0;
11604   else if (strcmp (name, "mips3d") == 0)
11605     mips_opts.ase_mips3d = 1;
11606   else if (strcmp (name, "nomips3d") == 0)
11607     mips_opts.ase_mips3d = 0;
11608   else if (strcmp (name, "mdmx") == 0)
11609     mips_opts.ase_mdmx = 1;
11610   else if (strcmp (name, "nomdmx") == 0)
11611     mips_opts.ase_mdmx = 0;
11612   else if (strncmp (name, "mips", 4) == 0)
11613     {
11614       int isa;
11615
11616       /* Permit the user to change the ISA on the fly.  Needless to
11617          say, misuse can cause serious problems.  */
11618       isa = atoi (name + 4);
11619       switch (isa)
11620         {
11621         case  0:
11622           mips_opts.gp32 = file_mips_gp32;
11623           mips_opts.fp32 = file_mips_fp32;
11624           break;
11625         case  1:
11626         case  2:
11627         case 32:
11628           mips_opts.gp32 = 1;
11629           mips_opts.fp32 = 1;
11630           break;
11631         case  3:
11632         case  4:
11633         case  5:
11634         case 64:
11635           mips_opts.gp32 = 0;
11636           mips_opts.fp32 = 0;
11637           break;
11638         default:
11639           as_bad (_("unknown ISA level %s"), name + 4);
11640           break;
11641         }
11642
11643       switch (isa)
11644         {
11645         case  0: mips_opts.isa = file_mips_isa;   break;
11646         case  1: mips_opts.isa = ISA_MIPS1;       break;
11647         case  2: mips_opts.isa = ISA_MIPS2;       break;
11648         case  3: mips_opts.isa = ISA_MIPS3;       break;
11649         case  4: mips_opts.isa = ISA_MIPS4;       break;
11650         case  5: mips_opts.isa = ISA_MIPS5;       break;
11651         case 32: mips_opts.isa = ISA_MIPS32;      break;
11652         case 64: mips_opts.isa = ISA_MIPS64;      break;
11653         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11654         }
11655     }
11656   else if (strcmp (name, "autoextend") == 0)
11657     mips_opts.noautoextend = 0;
11658   else if (strcmp (name, "noautoextend") == 0)
11659     mips_opts.noautoextend = 1;
11660   else if (strcmp (name, "push") == 0)
11661     {
11662       struct mips_option_stack *s;
11663
11664       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11665       s->next = mips_opts_stack;
11666       s->options = mips_opts;
11667       mips_opts_stack = s;
11668     }
11669   else if (strcmp (name, "pop") == 0)
11670     {
11671       struct mips_option_stack *s;
11672
11673       s = mips_opts_stack;
11674       if (s == NULL)
11675         as_bad (_(".set pop with no .set push"));
11676       else
11677         {
11678           /* If we're changing the reorder mode we need to handle
11679              delay slots correctly.  */
11680           if (s->options.noreorder && ! mips_opts.noreorder)
11681             mips_emit_delays (true);
11682           else if (! s->options.noreorder && mips_opts.noreorder)
11683             {
11684               if (prev_nop_frag != NULL)
11685                 {
11686                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11687                                             * (mips_opts.mips16 ? 2 : 4));
11688                   prev_nop_frag = NULL;
11689                 }
11690             }
11691
11692           mips_opts = s->options;
11693           mips_opts_stack = s->next;
11694           free (s);
11695         }
11696     }
11697   else
11698     {
11699       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11700     }
11701   *input_line_pointer = ch;
11702   demand_empty_rest_of_line ();
11703 }
11704
11705 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11706    .option pic2.  It means to generate SVR4 PIC calls.  */
11707
11708 static void
11709 s_abicalls (ignore)
11710      int ignore ATTRIBUTE_UNUSED;
11711 {
11712   mips_pic = SVR4_PIC;
11713   if (USE_GLOBAL_POINTER_OPT)
11714     {
11715       if (g_switch_seen && g_switch_value != 0)
11716         as_warn (_("-G may not be used with SVR4 PIC code"));
11717       g_switch_value = 0;
11718     }
11719   bfd_set_gp_size (stdoutput, 0);
11720   demand_empty_rest_of_line ();
11721 }
11722
11723 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11724    PIC code.  It sets the $gp register for the function based on the
11725    function address, which is in the register named in the argument.
11726    This uses a relocation against _gp_disp, which is handled specially
11727    by the linker.  The result is:
11728         lui     $gp,%hi(_gp_disp)
11729         addiu   $gp,$gp,%lo(_gp_disp)
11730         addu    $gp,$gp,.cpload argument
11731    The .cpload argument is normally $25 == $t9.  */
11732
11733 static void
11734 s_cpload (ignore)
11735      int ignore ATTRIBUTE_UNUSED;
11736 {
11737   expressionS ex;
11738   int icnt = 0;
11739
11740   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11741      .cpload is ignored.  */
11742   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11743     {
11744       s_ignore (0);
11745       return;
11746     }
11747
11748   /* .cpload should be in a .set noreorder section.  */
11749   if (mips_opts.noreorder == 0)
11750     as_warn (_(".cpload not in noreorder section"));
11751
11752   ex.X_op = O_symbol;
11753   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11754   ex.X_op_symbol = NULL;
11755   ex.X_add_number = 0;
11756
11757   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11758   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11759
11760   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11761   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11762                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11763
11764   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11765                mips_gp_register, mips_gp_register, tc_get_register (0));
11766
11767   demand_empty_rest_of_line ();
11768 }
11769
11770 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11771      .cpsetup $reg1, offset|$reg2, label
11772
11773    If offset is given, this results in:
11774      sd         $gp, offset($sp)
11775      lui        $gp, %hi(%neg(%gp_rel(label)))
11776      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11777      daddu      $gp, $gp, $reg1
11778
11779    If $reg2 is given, this results in:
11780      daddu      $reg2, $gp, $0
11781      lui        $gp, %hi(%neg(%gp_rel(label)))
11782      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11783      daddu      $gp, $gp, $reg1
11784    $reg1 is normally $25 == $t9.  */
11785 static void
11786 s_cpsetup (ignore)
11787      int ignore ATTRIBUTE_UNUSED;
11788 {
11789   expressionS ex_off;
11790   expressionS ex_sym;
11791   int reg1;
11792   int icnt = 0;
11793   char *f;
11794
11795   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11796      We also need NewABI support.  */
11797   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11798     {
11799       s_ignore (0);
11800       return;
11801     }
11802
11803   reg1 = tc_get_register (0);
11804   SKIP_WHITESPACE ();
11805   if (*input_line_pointer != ',')
11806     {
11807       as_bad (_("missing argument separator ',' for .cpsetup"));
11808       return;
11809     }
11810   else
11811     ++input_line_pointer;
11812   SKIP_WHITESPACE ();
11813   if (*input_line_pointer == '$')
11814     {
11815       mips_cpreturn_register = tc_get_register (0);
11816       mips_cpreturn_offset = -1;
11817     }
11818   else
11819     {
11820       mips_cpreturn_offset = get_absolute_expression ();
11821       mips_cpreturn_register = -1;
11822     }
11823   SKIP_WHITESPACE ();
11824   if (*input_line_pointer != ',')
11825     {
11826       as_bad (_("missing argument separator ',' for .cpsetup"));
11827       return;
11828     }
11829   else
11830     ++input_line_pointer;
11831   SKIP_WHITESPACE ();
11832   expression (&ex_sym);
11833
11834   if (mips_cpreturn_register == -1)
11835     {
11836       ex_off.X_op = O_constant;
11837       ex_off.X_add_symbol = NULL;
11838       ex_off.X_op_symbol = NULL;
11839       ex_off.X_add_number = mips_cpreturn_offset;
11840
11841       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11842                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11843     }
11844   else
11845     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11846                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11847
11848   /* Ensure there's room for the next two instructions, so that `f'
11849      doesn't end up with an address in the wrong frag.  */
11850   frag_grow (8);
11851   f = frag_more (0);
11852   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11853                (int) BFD_RELOC_GPREL16);
11854   fix_new (frag_now, f - frag_now->fr_literal,
11855            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11856   fix_new (frag_now, f - frag_now->fr_literal,
11857            0, NULL, 0, 0, BFD_RELOC_HI16_S);
11858
11859   f = frag_more (0);
11860   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11861                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11862   fix_new (frag_now, f - frag_now->fr_literal,
11863            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11864   fix_new (frag_now, f - frag_now->fr_literal,
11865            0, NULL, 0, 0, BFD_RELOC_LO16);
11866
11867   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11868                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11869                mips_gp_register, mips_gp_register, reg1);
11870
11871   demand_empty_rest_of_line ();
11872 }
11873
11874 static void
11875 s_cplocal (ignore)
11876      int ignore ATTRIBUTE_UNUSED;
11877 {
11878   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11879    .cplocal is ignored.  */
11880   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11881     {
11882       s_ignore (0);
11883       return;
11884     }
11885
11886   mips_gp_register = tc_get_register (0);
11887   demand_empty_rest_of_line ();
11888 }
11889
11890 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11891    offset from $sp.  The offset is remembered, and after making a PIC
11892    call $gp is restored from that location.  */
11893
11894 static void
11895 s_cprestore (ignore)
11896      int ignore ATTRIBUTE_UNUSED;
11897 {
11898   expressionS ex;
11899   int icnt = 0;
11900
11901   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11902      .cprestore is ignored.  */
11903   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11904     {
11905       s_ignore (0);
11906       return;
11907     }
11908
11909   mips_cprestore_offset = get_absolute_expression ();
11910   mips_cprestore_valid = 1;
11911
11912   ex.X_op = O_constant;
11913   ex.X_add_symbol = NULL;
11914   ex.X_op_symbol = NULL;
11915   ex.X_add_number = mips_cprestore_offset;
11916
11917   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
11918                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11919                                 mips_gp_register, SP);
11920
11921   demand_empty_rest_of_line ();
11922 }
11923
11924 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11925    was given in the preceeding .gpsetup, it results in:
11926      ld         $gp, offset($sp)
11927
11928    If a register $reg2 was given there, it results in:
11929      daddiu     $gp, $gp, $reg2
11930  */
11931 static void
11932 s_cpreturn (ignore)
11933      int ignore ATTRIBUTE_UNUSED;
11934 {
11935   expressionS ex;
11936   int icnt = 0;
11937
11938   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11939      We also need NewABI support.  */
11940   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11941     {
11942       s_ignore (0);
11943       return;
11944     }
11945
11946   if (mips_cpreturn_register == -1)
11947     {
11948       ex.X_op = O_constant;
11949       ex.X_add_symbol = NULL;
11950       ex.X_op_symbol = NULL;
11951       ex.X_add_number = mips_cpreturn_offset;
11952
11953       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11954                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11955     }
11956   else
11957     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11958                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11959
11960   demand_empty_rest_of_line ();
11961 }
11962
11963 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
11964    code.  It sets the offset to use in gp_rel relocations.  */
11965
11966 static void
11967 s_gpvalue (ignore)
11968      int ignore ATTRIBUTE_UNUSED;
11969 {
11970   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11971      We also need NewABI support.  */
11972   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11973     {
11974       s_ignore (0);
11975       return;
11976     }
11977
11978   mips_gprel_offset = get_absolute_expression ();
11979
11980   demand_empty_rest_of_line ();
11981 }
11982
11983 /* Handle the .gpword pseudo-op.  This is used when generating PIC
11984    code.  It generates a 32 bit GP relative reloc.  */
11985
11986 static void
11987 s_gpword (ignore)
11988      int ignore ATTRIBUTE_UNUSED;
11989 {
11990   symbolS *label;
11991   expressionS ex;
11992   char *p;
11993
11994   /* When not generating PIC code, this is treated as .word.  */
11995   if (mips_pic != SVR4_PIC)
11996     {
11997       s_cons (2);
11998       return;
11999     }
12000
12001   label = insn_labels != NULL ? insn_labels->label : NULL;
12002   mips_emit_delays (true);
12003   if (auto_align)
12004     mips_align (2, 0, label);
12005   mips_clear_insn_labels ();
12006
12007   expression (&ex);
12008
12009   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12010     {
12011       as_bad (_("Unsupported use of .gpword"));
12012       ignore_rest_of_line ();
12013     }
12014
12015   p = frag_more (4);
12016   md_number_to_chars (p, (valueT) 0, 4);
12017   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12018                BFD_RELOC_GPREL32);
12019
12020   demand_empty_rest_of_line ();
12021 }
12022
12023 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12024    tables in SVR4 PIC code.  */
12025
12026 static void
12027 s_cpadd (ignore)
12028      int ignore ATTRIBUTE_UNUSED;
12029 {
12030   int icnt = 0;
12031   int reg;
12032
12033   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12034      code.  */
12035   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12036     {
12037       s_ignore (0);
12038       return;
12039     }
12040
12041   /* Add $gp to the register named as an argument.  */
12042   reg = tc_get_register (0);
12043   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12044                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12045                "d,v,t", reg, reg, mips_gp_register);
12046
12047   demand_empty_rest_of_line ();
12048 }
12049
12050 /* Handle the .insn pseudo-op.  This marks instruction labels in
12051    mips16 mode.  This permits the linker to handle them specially,
12052    such as generating jalx instructions when needed.  We also make
12053    them odd for the duration of the assembly, in order to generate the
12054    right sort of code.  We will make them even in the adjust_symtab
12055    routine, while leaving them marked.  This is convenient for the
12056    debugger and the disassembler.  The linker knows to make them odd
12057    again.  */
12058
12059 static void
12060 s_insn (ignore)
12061      int ignore ATTRIBUTE_UNUSED;
12062 {
12063   mips16_mark_labels ();
12064
12065   demand_empty_rest_of_line ();
12066 }
12067
12068 /* Handle a .stabn directive.  We need these in order to mark a label
12069    as being a mips16 text label correctly.  Sometimes the compiler
12070    will emit a label, followed by a .stabn, and then switch sections.
12071    If the label and .stabn are in mips16 mode, then the label is
12072    really a mips16 text label.  */
12073
12074 static void
12075 s_mips_stab (type)
12076      int type;
12077 {
12078   if (type == 'n')
12079     mips16_mark_labels ();
12080
12081   s_stab (type);
12082 }
12083
12084 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12085  */
12086
12087 static void
12088 s_mips_weakext (ignore)
12089      int ignore ATTRIBUTE_UNUSED;
12090 {
12091   char *name;
12092   int c;
12093   symbolS *symbolP;
12094   expressionS exp;
12095
12096   name = input_line_pointer;
12097   c = get_symbol_end ();
12098   symbolP = symbol_find_or_make (name);
12099   S_SET_WEAK (symbolP);
12100   *input_line_pointer = c;
12101
12102   SKIP_WHITESPACE ();
12103
12104   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12105     {
12106       if (S_IS_DEFINED (symbolP))
12107         {
12108           as_bad ("ignoring attempt to redefine symbol %s",
12109                   S_GET_NAME (symbolP));
12110           ignore_rest_of_line ();
12111           return;
12112         }
12113
12114       if (*input_line_pointer == ',')
12115         {
12116           ++input_line_pointer;
12117           SKIP_WHITESPACE ();
12118         }
12119
12120       expression (&exp);
12121       if (exp.X_op != O_symbol)
12122         {
12123           as_bad ("bad .weakext directive");
12124           ignore_rest_of_line ();
12125           return;
12126         }
12127       symbol_set_value_expression (symbolP, &exp);
12128     }
12129
12130   demand_empty_rest_of_line ();
12131 }
12132
12133 /* Parse a register string into a number.  Called from the ECOFF code
12134    to parse .frame.  The argument is non-zero if this is the frame
12135    register, so that we can record it in mips_frame_reg.  */
12136
12137 int
12138 tc_get_register (frame)
12139      int frame;
12140 {
12141   int reg;
12142
12143   SKIP_WHITESPACE ();
12144   if (*input_line_pointer++ != '$')
12145     {
12146       as_warn (_("expected `$'"));
12147       reg = ZERO;
12148     }
12149   else if (ISDIGIT (*input_line_pointer))
12150     {
12151       reg = get_absolute_expression ();
12152       if (reg < 0 || reg >= 32)
12153         {
12154           as_warn (_("Bad register number"));
12155           reg = ZERO;
12156         }
12157     }
12158   else
12159     {
12160       if (strncmp (input_line_pointer, "ra", 2) == 0)
12161         {
12162           reg = RA;
12163           input_line_pointer += 2;
12164         }
12165       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12166         {
12167           reg = FP;
12168           input_line_pointer += 2;
12169         }
12170       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12171         {
12172           reg = SP;
12173           input_line_pointer += 2;
12174         }
12175       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12176         {
12177           reg = GP;
12178           input_line_pointer += 2;
12179         }
12180       else if (strncmp (input_line_pointer, "at", 2) == 0)
12181         {
12182           reg = AT;
12183           input_line_pointer += 2;
12184         }
12185       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12186         {
12187           reg = KT0;
12188           input_line_pointer += 3;
12189         }
12190       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12191         {
12192           reg = KT1;
12193           input_line_pointer += 3;
12194         }
12195       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12196         {
12197           reg = ZERO;
12198           input_line_pointer += 4;
12199         }
12200       else
12201         {
12202           as_warn (_("Unrecognized register name"));
12203           reg = ZERO;
12204           while (ISALNUM(*input_line_pointer))
12205            input_line_pointer++;
12206         }
12207     }
12208   if (frame)
12209     {
12210       mips_frame_reg = reg != 0 ? reg : SP;
12211       mips_frame_reg_valid = 1;
12212       mips_cprestore_valid = 0;
12213     }
12214   return reg;
12215 }
12216
12217 valueT
12218 md_section_align (seg, addr)
12219      asection *seg;
12220      valueT addr;
12221 {
12222   int align = bfd_get_section_alignment (stdoutput, seg);
12223
12224 #ifdef OBJ_ELF
12225   /* We don't need to align ELF sections to the full alignment.
12226      However, Irix 5 may prefer that we align them at least to a 16
12227      byte boundary.  We don't bother to align the sections if we are
12228      targeted for an embedded system.  */
12229   if (strcmp (TARGET_OS, "elf") == 0)
12230     return addr;
12231   if (align > 4)
12232     align = 4;
12233 #endif
12234
12235   return ((addr + (1 << align) - 1) & (-1 << align));
12236 }
12237
12238 /* Utility routine, called from above as well.  If called while the
12239    input file is still being read, it's only an approximation.  (For
12240    example, a symbol may later become defined which appeared to be
12241    undefined earlier.)  */
12242
12243 static int
12244 nopic_need_relax (sym, before_relaxing)
12245      symbolS *sym;
12246      int before_relaxing;
12247 {
12248   if (sym == 0)
12249     return 0;
12250
12251   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12252     {
12253       const char *symname;
12254       int change;
12255
12256       /* Find out whether this symbol can be referenced off the $gp
12257          register.  It can be if it is smaller than the -G size or if
12258          it is in the .sdata or .sbss section.  Certain symbols can
12259          not be referenced off the $gp, although it appears as though
12260          they can.  */
12261       symname = S_GET_NAME (sym);
12262       if (symname != (const char *) NULL
12263           && (strcmp (symname, "eprol") == 0
12264               || strcmp (symname, "etext") == 0
12265               || strcmp (symname, "_gp") == 0
12266               || strcmp (symname, "edata") == 0
12267               || strcmp (symname, "_fbss") == 0
12268               || strcmp (symname, "_fdata") == 0
12269               || strcmp (symname, "_ftext") == 0
12270               || strcmp (symname, "end") == 0
12271               || strcmp (symname, "_gp_disp") == 0))
12272         change = 1;
12273       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12274                && (0
12275 #ifndef NO_ECOFF_DEBUGGING
12276                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12277                        && (symbol_get_obj (sym)->ecoff_extern_size
12278                            <= g_switch_value))
12279 #endif
12280                    /* We must defer this decision until after the whole
12281                       file has been read, since there might be a .extern
12282                       after the first use of this symbol.  */
12283                    || (before_relaxing
12284 #ifndef NO_ECOFF_DEBUGGING
12285                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12286 #endif
12287                        && S_GET_VALUE (sym) == 0)
12288                    || (S_GET_VALUE (sym) != 0
12289                        && S_GET_VALUE (sym) <= g_switch_value)))
12290         change = 0;
12291       else
12292         {
12293           const char *segname;
12294
12295           segname = segment_name (S_GET_SEGMENT (sym));
12296           assert (strcmp (segname, ".lit8") != 0
12297                   && strcmp (segname, ".lit4") != 0);
12298           change = (strcmp (segname, ".sdata") != 0
12299                     && strcmp (segname, ".sbss") != 0
12300                     && strncmp (segname, ".sdata.", 7) != 0
12301                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12302         }
12303       return change;
12304     }
12305   else
12306     /* We are not optimizing for the $gp register.  */
12307     return 1;
12308 }
12309
12310 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12311    extended opcode.  SEC is the section the frag is in.  */
12312
12313 static int
12314 mips16_extended_frag (fragp, sec, stretch)
12315      fragS *fragp;
12316      asection *sec;
12317      long stretch;
12318 {
12319   int type;
12320   register const struct mips16_immed_operand *op;
12321   offsetT val;
12322   int mintiny, maxtiny;
12323   segT symsec;
12324   fragS *sym_frag;
12325
12326   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12327     return 0;
12328   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12329     return 1;
12330
12331   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12332   op = mips16_immed_operands;
12333   while (op->type != type)
12334     {
12335       ++op;
12336       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12337     }
12338
12339   if (op->unsp)
12340     {
12341       if (type == '<' || type == '>' || type == '[' || type == ']')
12342         {
12343           mintiny = 1;
12344           maxtiny = 1 << op->nbits;
12345         }
12346       else
12347         {
12348           mintiny = 0;
12349           maxtiny = (1 << op->nbits) - 1;
12350         }
12351     }
12352   else
12353     {
12354       mintiny = - (1 << (op->nbits - 1));
12355       maxtiny = (1 << (op->nbits - 1)) - 1;
12356     }
12357
12358   sym_frag = symbol_get_frag (fragp->fr_symbol);
12359   val = S_GET_VALUE (fragp->fr_symbol);
12360   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12361
12362   if (op->pcrel)
12363     {
12364       addressT addr;
12365
12366       /* We won't have the section when we are called from
12367          mips_relax_frag.  However, we will always have been called
12368          from md_estimate_size_before_relax first.  If this is a
12369          branch to a different section, we mark it as such.  If SEC is
12370          NULL, and the frag is not marked, then it must be a branch to
12371          the same section.  */
12372       if (sec == NULL)
12373         {
12374           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12375             return 1;
12376         }
12377       else
12378         {
12379           /* Must have been called from md_estimate_size_before_relax.  */
12380           if (symsec != sec)
12381             {
12382               fragp->fr_subtype =
12383                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12384
12385               /* FIXME: We should support this, and let the linker
12386                  catch branches and loads that are out of range.  */
12387               as_bad_where (fragp->fr_file, fragp->fr_line,
12388                             _("unsupported PC relative reference to different section"));
12389
12390               return 1;
12391             }
12392           if (fragp != sym_frag && sym_frag->fr_address == 0)
12393             /* Assume non-extended on the first relaxation pass.
12394                The address we have calculated will be bogus if this is
12395                a forward branch to another frag, as the forward frag
12396                will have fr_address == 0.  */
12397             return 0;
12398         }
12399
12400       /* In this case, we know for sure that the symbol fragment is in
12401          the same section.  If the relax_marker of the symbol fragment
12402          differs from the relax_marker of this fragment, we have not
12403          yet adjusted the symbol fragment fr_address.  We want to add
12404          in STRETCH in order to get a better estimate of the address.
12405          This particularly matters because of the shift bits.  */
12406       if (stretch != 0
12407           && sym_frag->relax_marker != fragp->relax_marker)
12408         {
12409           fragS *f;
12410
12411           /* Adjust stretch for any alignment frag.  Note that if have
12412              been expanding the earlier code, the symbol may be
12413              defined in what appears to be an earlier frag.  FIXME:
12414              This doesn't handle the fr_subtype field, which specifies
12415              a maximum number of bytes to skip when doing an
12416              alignment.  */
12417           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12418             {
12419               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12420                 {
12421                   if (stretch < 0)
12422                     stretch = - ((- stretch)
12423                                  & ~ ((1 << (int) f->fr_offset) - 1));
12424                   else
12425                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12426                   if (stretch == 0)
12427                     break;
12428                 }
12429             }
12430           if (f != NULL)
12431             val += stretch;
12432         }
12433
12434       addr = fragp->fr_address + fragp->fr_fix;
12435
12436       /* The base address rules are complicated.  The base address of
12437          a branch is the following instruction.  The base address of a
12438          PC relative load or add is the instruction itself, but if it
12439          is in a delay slot (in which case it can not be extended) use
12440          the address of the instruction whose delay slot it is in.  */
12441       if (type == 'p' || type == 'q')
12442         {
12443           addr += 2;
12444
12445           /* If we are currently assuming that this frag should be
12446              extended, then, the current address is two bytes
12447              higher.  */
12448           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12449             addr += 2;
12450
12451           /* Ignore the low bit in the target, since it will be set
12452              for a text label.  */
12453           if ((val & 1) != 0)
12454             --val;
12455         }
12456       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12457         addr -= 4;
12458       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12459         addr -= 2;
12460
12461       val -= addr & ~ ((1 << op->shift) - 1);
12462
12463       /* Branch offsets have an implicit 0 in the lowest bit.  */
12464       if (type == 'p' || type == 'q')
12465         val /= 2;
12466
12467       /* If any of the shifted bits are set, we must use an extended
12468          opcode.  If the address depends on the size of this
12469          instruction, this can lead to a loop, so we arrange to always
12470          use an extended opcode.  We only check this when we are in
12471          the main relaxation loop, when SEC is NULL.  */
12472       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12473         {
12474           fragp->fr_subtype =
12475             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12476           return 1;
12477         }
12478
12479       /* If we are about to mark a frag as extended because the value
12480          is precisely maxtiny + 1, then there is a chance of an
12481          infinite loop as in the following code:
12482              la $4,foo
12483              .skip      1020
12484              .align     2
12485            foo:
12486          In this case when the la is extended, foo is 0x3fc bytes
12487          away, so the la can be shrunk, but then foo is 0x400 away, so
12488          the la must be extended.  To avoid this loop, we mark the
12489          frag as extended if it was small, and is about to become
12490          extended with a value of maxtiny + 1.  */
12491       if (val == ((maxtiny + 1) << op->shift)
12492           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12493           && sec == NULL)
12494         {
12495           fragp->fr_subtype =
12496             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12497           return 1;
12498         }
12499     }
12500   else if (symsec != absolute_section && sec != NULL)
12501     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12502
12503   if ((val & ((1 << op->shift) - 1)) != 0
12504       || val < (mintiny << op->shift)
12505       || val > (maxtiny << op->shift))
12506     return 1;
12507   else
12508     return 0;
12509 }
12510
12511 /* Estimate the size of a frag before relaxing.  Unless this is the
12512    mips16, we are not really relaxing here, and the final size is
12513    encoded in the subtype information.  For the mips16, we have to
12514    decide whether we are using an extended opcode or not.  */
12515
12516 int
12517 md_estimate_size_before_relax (fragp, segtype)
12518      fragS *fragp;
12519      asection *segtype;
12520 {
12521   int change = 0;
12522   boolean linkonce = false;
12523
12524   if (RELAX_MIPS16_P (fragp->fr_subtype))
12525     /* We don't want to modify the EXTENDED bit here; it might get us
12526        into infinite loops.  We change it only in mips_relax_frag().  */
12527     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12528
12529   if (mips_pic == NO_PIC)
12530     {
12531       change = nopic_need_relax (fragp->fr_symbol, 0);
12532     }
12533   else if (mips_pic == SVR4_PIC)
12534     {
12535       symbolS *sym;
12536       asection *symsec;
12537
12538       sym = fragp->fr_symbol;
12539
12540       /* Handle the case of a symbol equated to another symbol.  */
12541       while (symbol_equated_reloc_p (sym))
12542         {
12543           symbolS *n;
12544
12545           /* It's possible to get a loop here in a badly written
12546              program.  */
12547           n = symbol_get_value_expression (sym)->X_add_symbol;
12548           if (n == sym)
12549             break;
12550           sym = n;
12551         }
12552
12553       symsec = S_GET_SEGMENT (sym);
12554
12555       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12556       if (symsec != segtype && ! S_IS_LOCAL (sym))
12557         {
12558           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12559               != 0)
12560             linkonce = true;
12561
12562           /* The GNU toolchain uses an extension for ELF: a section
12563              beginning with the magic string .gnu.linkonce is a linkonce
12564              section.  */
12565           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12566                        sizeof ".gnu.linkonce" - 1) == 0)
12567             linkonce = true;
12568         }
12569
12570       /* This must duplicate the test in adjust_reloc_syms.  */
12571       change = (symsec != &bfd_und_section
12572                 && symsec != &bfd_abs_section
12573                 && ! bfd_is_com_section (symsec)
12574                 && !linkonce
12575 #ifdef OBJ_ELF
12576                 /* A global or weak symbol is treated as external.  */
12577                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12578                     || (! S_IS_WEAK (sym)
12579                         && (! S_IS_EXTERNAL (sym)
12580                             || mips_pic == EMBEDDED_PIC)))
12581 #endif
12582                 );
12583     }
12584   else
12585     abort ();
12586
12587   if (change)
12588     {
12589       /* Record the offset to the first reloc in the fr_opcode field.
12590          This lets md_convert_frag and tc_gen_reloc know that the code
12591          must be expanded.  */
12592       fragp->fr_opcode = (fragp->fr_literal
12593                           + fragp->fr_fix
12594                           - RELAX_OLD (fragp->fr_subtype)
12595                           + RELAX_RELOC1 (fragp->fr_subtype));
12596       /* FIXME: This really needs as_warn_where.  */
12597       if (RELAX_WARN (fragp->fr_subtype))
12598         as_warn (_("AT used after \".set noat\" or macro used after "
12599                    "\".set nomacro\""));
12600
12601       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12602     }
12603
12604   return 0;
12605 }
12606
12607 /* This is called to see whether a reloc against a defined symbol
12608    should be converted into a reloc against a section.  Don't adjust
12609    MIPS16 jump relocations, so we don't have to worry about the format
12610    of the offset in the .o file.  Don't adjust relocations against
12611    mips16 symbols, so that the linker can find them if it needs to set
12612    up a stub.  */
12613
12614 int
12615 mips_fix_adjustable (fixp)
12616      fixS *fixp;
12617 {
12618   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12619     return 0;
12620
12621   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12622       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12623     return 0;
12624
12625   if (fixp->fx_addsy == NULL)
12626     return 1;
12627
12628 #ifdef OBJ_ELF
12629   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12630       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12631       && fixp->fx_subsy == NULL)
12632     return 0;
12633 #endif
12634
12635   return 1;
12636 }
12637
12638 /* Translate internal representation of relocation info to BFD target
12639    format.  */
12640
12641 arelent **
12642 tc_gen_reloc (section, fixp)
12643      asection *section ATTRIBUTE_UNUSED;
12644      fixS *fixp;
12645 {
12646   static arelent *retval[4];
12647   arelent *reloc;
12648   bfd_reloc_code_real_type code;
12649
12650   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12651   retval[1] = NULL;
12652
12653   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12654   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12655   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12656
12657   if (mips_pic == EMBEDDED_PIC
12658       && SWITCH_TABLE (fixp))
12659     {
12660       /* For a switch table entry we use a special reloc.  The addend
12661          is actually the difference between the reloc address and the
12662          subtrahend.  */
12663       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12664       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12665         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12666       fixp->fx_r_type = BFD_RELOC_GPREL32;
12667     }
12668   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12669     {
12670       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12671         reloc->addend = fixp->fx_addnumber;
12672       else
12673         {
12674           /* We use a special addend for an internal RELLO reloc.  */
12675           if (symbol_section_p (fixp->fx_addsy))
12676             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12677           else
12678             reloc->addend = fixp->fx_addnumber + reloc->address;
12679         }
12680     }
12681   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12682     {
12683       assert (fixp->fx_next != NULL
12684               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12685
12686       /* The reloc is relative to the RELLO; adjust the addend
12687          accordingly.  */
12688       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12689         reloc->addend = fixp->fx_next->fx_addnumber;
12690       else
12691         {
12692           /* We use a special addend for an internal RELHI reloc.  */
12693           if (symbol_section_p (fixp->fx_addsy))
12694             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12695                              + fixp->fx_next->fx_where
12696                              - S_GET_VALUE (fixp->fx_subsy));
12697           else
12698             reloc->addend = (fixp->fx_addnumber
12699                              + fixp->fx_next->fx_frag->fr_address
12700                              + fixp->fx_next->fx_where);
12701         }
12702     }
12703   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12704     reloc->addend = fixp->fx_addnumber;
12705   else
12706     {
12707       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12708         /* A gruesome hack which is a result of the gruesome gas reloc
12709            handling.  */
12710         reloc->addend = reloc->address;
12711       else
12712         reloc->addend = -reloc->address;
12713     }
12714
12715   /* If this is a variant frag, we may need to adjust the existing
12716      reloc and generate a new one.  */
12717   if (fixp->fx_frag->fr_opcode != NULL
12718       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
12719            && ! HAVE_NEWABI)
12720           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12721           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12722           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12723           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12724           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12725           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12726     )
12727     {
12728       arelent *reloc2;
12729
12730       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12731
12732       /* If this is not the last reloc in this frag, then we have two
12733          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12734          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12735          the second one handle all of them.  */
12736       if (fixp->fx_next != NULL
12737           && fixp->fx_frag == fixp->fx_next->fx_frag)
12738         {
12739           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12740                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12741                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12742                       && (fixp->fx_next->fx_r_type
12743                           == BFD_RELOC_MIPS_GOT_LO16))
12744                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12745                       && (fixp->fx_next->fx_r_type
12746                           == BFD_RELOC_MIPS_CALL_LO16)));
12747           retval[0] = NULL;
12748           return retval;
12749         }
12750
12751       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12752       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12753       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12754       retval[2] = NULL;
12755       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12756       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12757       reloc2->address = (reloc->address
12758                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12759                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12760       reloc2->addend = fixp->fx_addnumber;
12761       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12762       assert (reloc2->howto != NULL);
12763
12764       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12765         {
12766           arelent *reloc3;
12767
12768           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12769           retval[3] = NULL;
12770           *reloc3 = *reloc2;
12771           reloc3->address += 4;
12772         }
12773
12774       if (mips_pic == NO_PIC)
12775         {
12776           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12777           fixp->fx_r_type = BFD_RELOC_HI16_S;
12778         }
12779       else if (mips_pic == SVR4_PIC)
12780         {
12781           switch (fixp->fx_r_type)
12782             {
12783             default:
12784               abort ();
12785             case BFD_RELOC_MIPS_GOT16:
12786               break;
12787             case BFD_RELOC_MIPS_GOT_LO16:
12788             case BFD_RELOC_MIPS_CALL_LO16:
12789               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12790               break;
12791             case BFD_RELOC_MIPS_CALL16:
12792               if (HAVE_NEWABI)
12793                 {
12794                   /* BFD_RELOC_MIPS_GOT16;*/
12795                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
12796                   reloc2->howto = bfd_reloc_type_lookup
12797                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
12798                 }
12799               else
12800                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12801               break;
12802             }
12803         }
12804       else
12805         abort ();
12806
12807       /* newabi uses R_MIPS_GOT_DISP for local symbols */
12808       if (HAVE_NEWABI && BFD_RELOC_MIPS_GOT_LO16)
12809         {
12810           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
12811           retval[1] = NULL;
12812         }
12813     }
12814
12815   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12816      entry to be used in the relocation's section offset.  */
12817   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12818     {
12819       reloc->address = reloc->addend;
12820       reloc->addend = 0;
12821     }
12822
12823   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12824      fixup_segment converted a non-PC relative reloc into a PC
12825      relative reloc.  In such a case, we need to convert the reloc
12826      code.  */
12827   code = fixp->fx_r_type;
12828   if (fixp->fx_pcrel)
12829     {
12830       switch (code)
12831         {
12832         case BFD_RELOC_8:
12833           code = BFD_RELOC_8_PCREL;
12834           break;
12835         case BFD_RELOC_16:
12836           code = BFD_RELOC_16_PCREL;
12837           break;
12838         case BFD_RELOC_32:
12839           code = BFD_RELOC_32_PCREL;
12840           break;
12841         case BFD_RELOC_64:
12842           code = BFD_RELOC_64_PCREL;
12843           break;
12844         case BFD_RELOC_8_PCREL:
12845         case BFD_RELOC_16_PCREL:
12846         case BFD_RELOC_32_PCREL:
12847         case BFD_RELOC_64_PCREL:
12848         case BFD_RELOC_16_PCREL_S2:
12849         case BFD_RELOC_PCREL_HI16_S:
12850         case BFD_RELOC_PCREL_LO16:
12851           break;
12852         default:
12853           as_bad_where (fixp->fx_file, fixp->fx_line,
12854                         _("Cannot make %s relocation PC relative"),
12855                         bfd_get_reloc_code_name (code));
12856         }
12857     }
12858
12859 #ifdef OBJ_ELF
12860   /* md_apply_fix3 has a double-subtraction hack to get
12861      bfd_install_relocation to behave nicely.  GPREL relocations are
12862      handled correctly without this hack, so undo it here.  We can't
12863      stop md_apply_fix3 from subtracting twice in the first place since
12864      the fake addend is required for variant frags above.  */
12865   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12866       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
12867       && reloc->addend != 0
12868       && mips_need_elf_addend_fixup (fixp))
12869     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12870 #endif
12871
12872   /* To support a PC relative reloc when generating embedded PIC code
12873      for ECOFF, we use a Cygnus extension.  We check for that here to
12874      make sure that we don't let such a reloc escape normally.  */
12875   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12876        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12877       && code == BFD_RELOC_16_PCREL_S2
12878       && mips_pic != EMBEDDED_PIC)
12879     reloc->howto = NULL;
12880   else
12881     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12882
12883   if (reloc->howto == NULL)
12884     {
12885       as_bad_where (fixp->fx_file, fixp->fx_line,
12886                     _("Can not represent %s relocation in this object file format"),
12887                     bfd_get_reloc_code_name (code));
12888       retval[0] = NULL;
12889     }
12890
12891   return retval;
12892 }
12893
12894 /* Relax a machine dependent frag.  This returns the amount by which
12895    the current size of the frag should change.  */
12896
12897 int
12898 mips_relax_frag (fragp, stretch)
12899      fragS *fragp;
12900      long stretch;
12901 {
12902   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12903     return 0;
12904
12905   if (mips16_extended_frag (fragp, NULL, stretch))
12906     {
12907       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12908         return 0;
12909       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12910       return 2;
12911     }
12912   else
12913     {
12914       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12915         return 0;
12916       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12917       return -2;
12918     }
12919
12920   return 0;
12921 }
12922
12923 /* Convert a machine dependent frag.  */
12924
12925 void
12926 md_convert_frag (abfd, asec, fragp)
12927      bfd *abfd ATTRIBUTE_UNUSED;
12928      segT asec;
12929      fragS *fragp;
12930 {
12931   int old, new;
12932   char *fixptr;
12933
12934   if (RELAX_MIPS16_P (fragp->fr_subtype))
12935     {
12936       int type;
12937       register const struct mips16_immed_operand *op;
12938       boolean small, ext;
12939       offsetT val;
12940       bfd_byte *buf;
12941       unsigned long insn;
12942       boolean use_extend;
12943       unsigned short extend;
12944
12945       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12946       op = mips16_immed_operands;
12947       while (op->type != type)
12948         ++op;
12949
12950       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12951         {
12952           small = false;
12953           ext = true;
12954         }
12955       else
12956         {
12957           small = true;
12958           ext = false;
12959         }
12960
12961       resolve_symbol_value (fragp->fr_symbol);
12962       val = S_GET_VALUE (fragp->fr_symbol);
12963       if (op->pcrel)
12964         {
12965           addressT addr;
12966
12967           addr = fragp->fr_address + fragp->fr_fix;
12968
12969           /* The rules for the base address of a PC relative reloc are
12970              complicated; see mips16_extended_frag.  */
12971           if (type == 'p' || type == 'q')
12972             {
12973               addr += 2;
12974               if (ext)
12975                 addr += 2;
12976               /* Ignore the low bit in the target, since it will be
12977                  set for a text label.  */
12978               if ((val & 1) != 0)
12979                 --val;
12980             }
12981           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12982             addr -= 4;
12983           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12984             addr -= 2;
12985
12986           addr &= ~ (addressT) ((1 << op->shift) - 1);
12987           val -= addr;
12988
12989           /* Make sure the section winds up with the alignment we have
12990              assumed.  */
12991           if (op->shift > 0)
12992             record_alignment (asec, op->shift);
12993         }
12994
12995       if (ext
12996           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12997               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12998         as_warn_where (fragp->fr_file, fragp->fr_line,
12999                        _("extended instruction in delay slot"));
13000
13001       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13002
13003       if (target_big_endian)
13004         insn = bfd_getb16 (buf);
13005       else
13006         insn = bfd_getl16 (buf);
13007
13008       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13009                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13010                     small, ext, &insn, &use_extend, &extend);
13011
13012       if (use_extend)
13013         {
13014           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13015           fragp->fr_fix += 2;
13016           buf += 2;
13017         }
13018
13019       md_number_to_chars ((char *) buf, insn, 2);
13020       fragp->fr_fix += 2;
13021       buf += 2;
13022     }
13023   else
13024     {
13025       if (fragp->fr_opcode == NULL)
13026         return;
13027
13028       old = RELAX_OLD (fragp->fr_subtype);
13029       new = RELAX_NEW (fragp->fr_subtype);
13030       fixptr = fragp->fr_literal + fragp->fr_fix;
13031
13032       if (new > 0)
13033         memcpy (fixptr - old, fixptr, new);
13034
13035       fragp->fr_fix += new - old;
13036     }
13037 }
13038
13039 #ifdef OBJ_ELF
13040
13041 /* This function is called after the relocs have been generated.
13042    We've been storing mips16 text labels as odd.  Here we convert them
13043    back to even for the convenience of the debugger.  */
13044
13045 void
13046 mips_frob_file_after_relocs ()
13047 {
13048   asymbol **syms;
13049   unsigned int count, i;
13050
13051   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13052     return;
13053
13054   syms = bfd_get_outsymbols (stdoutput);
13055   count = bfd_get_symcount (stdoutput);
13056   for (i = 0; i < count; i++, syms++)
13057     {
13058       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13059           && ((*syms)->value & 1) != 0)
13060         {
13061           (*syms)->value &= ~1;
13062           /* If the symbol has an odd size, it was probably computed
13063              incorrectly, so adjust that as well.  */
13064           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13065             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13066         }
13067     }
13068 }
13069
13070 #endif
13071
13072 /* This function is called whenever a label is defined.  It is used
13073    when handling branch delays; if a branch has a label, we assume we
13074    can not move it.  */
13075
13076 void
13077 mips_define_label (sym)
13078      symbolS *sym;
13079 {
13080   struct insn_label_list *l;
13081
13082   if (free_insn_labels == NULL)
13083     l = (struct insn_label_list *) xmalloc (sizeof *l);
13084   else
13085     {
13086       l = free_insn_labels;
13087       free_insn_labels = l->next;
13088     }
13089
13090   l->label = sym;
13091   l->next = insn_labels;
13092   insn_labels = l;
13093 }
13094 \f
13095 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13096
13097 /* Some special processing for a MIPS ELF file.  */
13098
13099 void
13100 mips_elf_final_processing ()
13101 {
13102   /* Write out the register information.  */
13103   if (mips_abi != N64_ABI)
13104     {
13105       Elf32_RegInfo s;
13106
13107       s.ri_gprmask = mips_gprmask;
13108       s.ri_cprmask[0] = mips_cprmask[0];
13109       s.ri_cprmask[1] = mips_cprmask[1];
13110       s.ri_cprmask[2] = mips_cprmask[2];
13111       s.ri_cprmask[3] = mips_cprmask[3];
13112       /* The gp_value field is set by the MIPS ELF backend.  */
13113
13114       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13115                                        ((Elf32_External_RegInfo *)
13116                                         mips_regmask_frag));
13117     }
13118   else
13119     {
13120       Elf64_Internal_RegInfo s;
13121
13122       s.ri_gprmask = mips_gprmask;
13123       s.ri_pad = 0;
13124       s.ri_cprmask[0] = mips_cprmask[0];
13125       s.ri_cprmask[1] = mips_cprmask[1];
13126       s.ri_cprmask[2] = mips_cprmask[2];
13127       s.ri_cprmask[3] = mips_cprmask[3];
13128       /* The gp_value field is set by the MIPS ELF backend.  */
13129
13130       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13131                                        ((Elf64_External_RegInfo *)
13132                                         mips_regmask_frag));
13133     }
13134
13135   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13136      sort of BFD interface for this.  */
13137   if (mips_any_noreorder)
13138     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13139   if (mips_pic != NO_PIC)
13140     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13141
13142   /* Set MIPS ELF flags for ASEs.  */
13143   if (file_ase_mips16)
13144     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13145 #if 0 /* XXX FIXME */
13146   if (file_ase_mips3d)
13147     elf_elfheader (stdoutput)->e_flags |= ???;
13148 #endif
13149   if (file_ase_mdmx)
13150     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13151
13152   /* Set the MIPS ELF ABI flags.  */
13153   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13154     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13155   else if (mips_abi == O64_ABI)
13156     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13157   else if (mips_abi == EABI_ABI)
13158     {
13159       if (!file_mips_gp32)
13160         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13161       else
13162         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13163     }
13164   else if (mips_abi == N32_ABI)
13165     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13166
13167   /* Nothing to do for N64_ABI.  */
13168
13169   if (mips_32bitmode)
13170     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13171 }
13172
13173 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13174 \f
13175 typedef struct proc {
13176   symbolS *isym;
13177   unsigned long reg_mask;
13178   unsigned long reg_offset;
13179   unsigned long fpreg_mask;
13180   unsigned long fpreg_offset;
13181   unsigned long frame_offset;
13182   unsigned long frame_reg;
13183   unsigned long pc_reg;
13184 } procS;
13185
13186 static procS cur_proc;
13187 static procS *cur_proc_ptr;
13188 static int numprocs;
13189
13190 /* Fill in an rs_align_code fragment.  */
13191
13192 void
13193 mips_handle_align (fragp)
13194      fragS *fragp;
13195 {
13196   if (fragp->fr_type != rs_align_code)
13197     return;
13198
13199   if (mips_opts.mips16)
13200     {
13201       static const unsigned char be_nop[] = { 0x65, 0x00 };
13202       static const unsigned char le_nop[] = { 0x00, 0x65 };
13203
13204       int bytes;
13205       char *p;
13206
13207       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13208       p = fragp->fr_literal + fragp->fr_fix;
13209
13210       if (bytes & 1)
13211         {
13212           *p++ = 0;
13213           fragp->fr_fix++;
13214         }
13215
13216       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13217       fragp->fr_var = 2;
13218     }
13219
13220   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13221 }
13222
13223 static void
13224 md_obj_begin ()
13225 {
13226 }
13227
13228 static void
13229 md_obj_end ()
13230 {
13231   /* check for premature end, nesting errors, etc */
13232   if (cur_proc_ptr)
13233     as_warn (_("missing .end at end of assembly"));
13234 }
13235
13236 static long
13237 get_number ()
13238 {
13239   int negative = 0;
13240   long val = 0;
13241
13242   if (*input_line_pointer == '-')
13243     {
13244       ++input_line_pointer;
13245       negative = 1;
13246     }
13247   if (!ISDIGIT (*input_line_pointer))
13248     as_bad (_("expected simple number"));
13249   if (input_line_pointer[0] == '0')
13250     {
13251       if (input_line_pointer[1] == 'x')
13252         {
13253           input_line_pointer += 2;
13254           while (ISXDIGIT (*input_line_pointer))
13255             {
13256               val <<= 4;
13257               val |= hex_value (*input_line_pointer++);
13258             }
13259           return negative ? -val : val;
13260         }
13261       else
13262         {
13263           ++input_line_pointer;
13264           while (ISDIGIT (*input_line_pointer))
13265             {
13266               val <<= 3;
13267               val |= *input_line_pointer++ - '0';
13268             }
13269           return negative ? -val : val;
13270         }
13271     }
13272   if (!ISDIGIT (*input_line_pointer))
13273     {
13274       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13275               *input_line_pointer, *input_line_pointer);
13276       as_warn (_("invalid number"));
13277       return -1;
13278     }
13279   while (ISDIGIT (*input_line_pointer))
13280     {
13281       val *= 10;
13282       val += *input_line_pointer++ - '0';
13283     }
13284   return negative ? -val : val;
13285 }
13286
13287 /* The .file directive; just like the usual .file directive, but there
13288    is an initial number which is the ECOFF file index.  In the non-ECOFF
13289    case .file implies DWARF-2.  */
13290
13291 static void
13292 s_mips_file (x)
13293      int x ATTRIBUTE_UNUSED;
13294 {
13295   static int first_file_directive = 0;
13296
13297   if (ECOFF_DEBUGGING)
13298     {
13299       get_number ();
13300       s_app_file (0);
13301     }
13302   else
13303     {
13304       char *filename;
13305
13306       filename = dwarf2_directive_file (0);
13307
13308       /* Versions of GCC up to 3.1 start files with a ".file"
13309          directive even for stabs output.  Make sure that this
13310          ".file" is handled.  Note that you need a version of GCC
13311          after 3.1 in order to support DWARF-2 on MIPS.  */
13312       if (filename != NULL && ! first_file_directive)
13313         {
13314           (void) new_logical_line (filename, -1);
13315           s_app_file_string (filename);
13316         }
13317       first_file_directive = 1;
13318     }
13319 }
13320
13321 /* The .loc directive, implying DWARF-2.  */
13322
13323 static void
13324 s_mips_loc (x)
13325      int x ATTRIBUTE_UNUSED;
13326 {
13327   if (!ECOFF_DEBUGGING)
13328     dwarf2_directive_loc (0);
13329 }
13330
13331 /* The .end directive.  */
13332
13333 static void
13334 s_mips_end (x)
13335      int x ATTRIBUTE_UNUSED;
13336 {
13337   symbolS *p;
13338   int maybe_text;
13339
13340   /* Following functions need their own .frame and .cprestore directives.  */
13341   mips_frame_reg_valid = 0;
13342   mips_cprestore_valid = 0;
13343
13344   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13345     {
13346       p = get_symbol ();
13347       demand_empty_rest_of_line ();
13348     }
13349   else
13350     p = NULL;
13351
13352 #ifdef BFD_ASSEMBLER
13353   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13354     maybe_text = 1;
13355   else
13356     maybe_text = 0;
13357 #else
13358   if (now_seg != data_section && now_seg != bss_section)
13359     maybe_text = 1;
13360   else
13361     maybe_text = 0;
13362 #endif
13363
13364   if (!maybe_text)
13365     as_warn (_(".end not in text section"));
13366
13367   if (!cur_proc_ptr)
13368     {
13369       as_warn (_(".end directive without a preceding .ent directive."));
13370       demand_empty_rest_of_line ();
13371       return;
13372     }
13373
13374   if (p != NULL)
13375     {
13376       assert (S_GET_NAME (p));
13377       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13378         as_warn (_(".end symbol does not match .ent symbol."));
13379
13380       if (debug_type == DEBUG_STABS)
13381         stabs_generate_asm_endfunc (S_GET_NAME (p),
13382                                     S_GET_NAME (p));
13383     }
13384   else
13385     as_warn (_(".end directive missing or unknown symbol"));
13386
13387 #ifdef OBJ_ELF
13388   /* Generate a .pdr section.  */
13389   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13390     {
13391       segT saved_seg = now_seg;
13392       subsegT saved_subseg = now_subseg;
13393       valueT dot;
13394       expressionS exp;
13395       char *fragp;
13396
13397       dot = frag_now_fix ();
13398
13399 #ifdef md_flush_pending_output
13400       md_flush_pending_output ();
13401 #endif
13402
13403       assert (pdr_seg);
13404       subseg_set (pdr_seg, 0);
13405
13406       /* Write the symbol.  */
13407       exp.X_op = O_symbol;
13408       exp.X_add_symbol = p;
13409       exp.X_add_number = 0;
13410       emit_expr (&exp, 4);
13411
13412       fragp = frag_more (7 * 4);
13413
13414       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13415       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13416       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13417       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13418       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13419       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13420       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13421
13422       subseg_set (saved_seg, saved_subseg);
13423     }
13424 #endif /* OBJ_ELF */
13425
13426   cur_proc_ptr = NULL;
13427 }
13428
13429 /* The .aent and .ent directives.  */
13430
13431 static void
13432 s_mips_ent (aent)
13433      int aent;
13434 {
13435   symbolS *symbolP;
13436   int maybe_text;
13437
13438   symbolP = get_symbol ();
13439   if (*input_line_pointer == ',')
13440     ++input_line_pointer;
13441   SKIP_WHITESPACE ();
13442   if (ISDIGIT (*input_line_pointer)
13443       || *input_line_pointer == '-')
13444     get_number ();
13445
13446 #ifdef BFD_ASSEMBLER
13447   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13448     maybe_text = 1;
13449   else
13450     maybe_text = 0;
13451 #else
13452   if (now_seg != data_section && now_seg != bss_section)
13453     maybe_text = 1;
13454   else
13455     maybe_text = 0;
13456 #endif
13457
13458   if (!maybe_text)
13459     as_warn (_(".ent or .aent not in text section."));
13460
13461   if (!aent && cur_proc_ptr)
13462     as_warn (_("missing .end"));
13463
13464   if (!aent)
13465     {
13466       /* This function needs its own .frame and .cprestore directives.  */
13467       mips_frame_reg_valid = 0;
13468       mips_cprestore_valid = 0;
13469
13470       cur_proc_ptr = &cur_proc;
13471       memset (cur_proc_ptr, '\0', sizeof (procS));
13472
13473       cur_proc_ptr->isym = symbolP;
13474
13475       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13476
13477       ++numprocs;
13478
13479       if (debug_type == DEBUG_STABS)
13480         stabs_generate_asm_func (S_GET_NAME (symbolP),
13481                                  S_GET_NAME (symbolP));
13482     }
13483
13484   demand_empty_rest_of_line ();
13485 }
13486
13487 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13488    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13489    s_mips_frame is used so that we can set the PDR information correctly.
13490    We can't use the ecoff routines because they make reference to the ecoff
13491    symbol table (in the mdebug section).  */
13492
13493 static void
13494 s_mips_frame (ignore)
13495      int ignore ATTRIBUTE_UNUSED;
13496 {
13497 #ifdef OBJ_ELF
13498   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13499     {
13500       long val;
13501
13502       if (cur_proc_ptr == (procS *) NULL)
13503         {
13504           as_warn (_(".frame outside of .ent"));
13505           demand_empty_rest_of_line ();
13506           return;
13507         }
13508
13509       cur_proc_ptr->frame_reg = tc_get_register (1);
13510
13511       SKIP_WHITESPACE ();
13512       if (*input_line_pointer++ != ','
13513           || get_absolute_expression_and_terminator (&val) != ',')
13514         {
13515           as_warn (_("Bad .frame directive"));
13516           --input_line_pointer;
13517           demand_empty_rest_of_line ();
13518           return;
13519         }
13520
13521       cur_proc_ptr->frame_offset = val;
13522       cur_proc_ptr->pc_reg = tc_get_register (0);
13523
13524       demand_empty_rest_of_line ();
13525     }
13526   else
13527 #endif /* OBJ_ELF */
13528     s_ignore (ignore);
13529 }
13530
13531 /* The .fmask and .mask directives. If the mdebug section is present
13532    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13533    embedded targets, s_mips_mask is used so that we can set the PDR
13534    information correctly. We can't use the ecoff routines because they
13535    make reference to the ecoff symbol table (in the mdebug section).  */
13536
13537 static void
13538 s_mips_mask (reg_type)
13539      char reg_type;
13540 {
13541 #ifdef OBJ_ELF
13542   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13543     {
13544       long mask, off;
13545
13546       if (cur_proc_ptr == (procS *) NULL)
13547         {
13548           as_warn (_(".mask/.fmask outside of .ent"));
13549           demand_empty_rest_of_line ();
13550           return;
13551         }
13552
13553       if (get_absolute_expression_and_terminator (&mask) != ',')
13554         {
13555           as_warn (_("Bad .mask/.fmask directive"));
13556           --input_line_pointer;
13557           demand_empty_rest_of_line ();
13558           return;
13559         }
13560
13561       off = get_absolute_expression ();
13562
13563       if (reg_type == 'F')
13564         {
13565           cur_proc_ptr->fpreg_mask = mask;
13566           cur_proc_ptr->fpreg_offset = off;
13567         }
13568       else
13569         {
13570           cur_proc_ptr->reg_mask = mask;
13571           cur_proc_ptr->reg_offset = off;
13572         }
13573
13574       demand_empty_rest_of_line ();
13575     }
13576   else
13577 #endif /* OBJ_ELF */
13578     s_ignore (reg_type);
13579 }
13580
13581 /* The .loc directive.  */
13582
13583 #if 0
13584 static void
13585 s_loc (x)
13586      int x;
13587 {
13588   symbolS *symbolP;
13589   int lineno;
13590   int addroff;
13591
13592   assert (now_seg == text_section);
13593
13594   lineno = get_number ();
13595   addroff = frag_now_fix ();
13596
13597   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13598   S_SET_TYPE (symbolP, N_SLINE);
13599   S_SET_OTHER (symbolP, 0);
13600   S_SET_DESC (symbolP, lineno);
13601   symbolP->sy_segment = now_seg;
13602 }
13603 #endif
13604
13605 /* A table describing all the processors gas knows about.  Names are
13606    matched in the order listed.
13607
13608    To ease comparison, please keep this table in the same order as
13609    gcc's mips_cpu_info_table[].  */
13610 static const struct mips_cpu_info mips_cpu_info_table[] =
13611 {
13612   /* Entries for generic ISAs */
13613   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
13614   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
13615   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
13616   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
13617   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
13618   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
13619   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
13620
13621   /* MIPS I */
13622   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
13623   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
13624   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
13625
13626   /* MIPS II */
13627   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
13628
13629   /* MIPS III */
13630   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
13631   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
13632   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
13633   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
13634   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
13635   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
13636   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
13637   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
13638   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
13639
13640   /* MIPS IV */
13641   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
13642   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
13643   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
13644   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
13645   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
13646   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
13647   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
13648   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
13649   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
13650   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
13651
13652   /* MIPS 32 */
13653   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13654   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
13655   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
13656
13657   /* MIPS 64 */
13658   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
13659   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
13660
13661   /* Broadcom SB-1 CPU core */
13662   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
13663
13664   /* End marker */
13665   { NULL, 0, 0, 0 }
13666 };
13667
13668
13669 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13670    with a final "000" replaced by "k".  Ignore case.
13671
13672    Note: this function is shared between GCC and GAS.  */
13673
13674 static boolean
13675 mips_strict_matching_cpu_name_p (canonical, given)
13676      const char *canonical, *given;
13677 {
13678   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13679     given++, canonical++;
13680
13681   return ((*given == 0 && *canonical == 0)
13682           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13683 }
13684
13685
13686 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13687    CPU name.  We've traditionally allowed a lot of variation here.
13688
13689    Note: this function is shared between GCC and GAS.  */
13690
13691 static boolean
13692 mips_matching_cpu_name_p (canonical, given)
13693      const char *canonical, *given;
13694 {
13695   /* First see if the name matches exactly, or with a final "000"
13696      turned into "k".  */
13697   if (mips_strict_matching_cpu_name_p (canonical, given))
13698     return true;
13699
13700   /* If not, try comparing based on numerical designation alone.
13701      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
13702   if (TOLOWER (*given) == 'r')
13703     given++;
13704   if (!ISDIGIT (*given))
13705     return false;
13706
13707   /* Skip over some well-known prefixes in the canonical name,
13708      hoping to find a number there too.  */
13709   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13710     canonical += 2;
13711   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13712     canonical += 2;
13713   else if (TOLOWER (canonical[0]) == 'r')
13714     canonical += 1;
13715
13716   return mips_strict_matching_cpu_name_p (canonical, given);
13717 }
13718
13719
13720 /* Parse an option that takes the name of a processor as its argument.
13721    OPTION is the name of the option and CPU_STRING is the argument.
13722    Return the corresponding processor enumeration if the CPU_STRING is
13723    recognized, otherwise report an error and return null.
13724
13725    A similar function exists in GCC.  */
13726
13727 static const struct mips_cpu_info *
13728 mips_parse_cpu (option, cpu_string)
13729      const char *option, *cpu_string;
13730 {
13731   const struct mips_cpu_info *p;
13732
13733   /* 'from-abi' selects the most compatible architecture for the given
13734      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
13735      EABIs, we have to decide whether we're using the 32-bit or 64-bit
13736      version.  Look first at the -mgp options, if given, otherwise base
13737      the choice on MIPS_DEFAULT_64BIT.
13738
13739      Treat NO_ABI like the EABIs.  One reason to do this is that the
13740      plain 'mips' and 'mips64' configs have 'from-abi' as their default
13741      architecture.  This code picks MIPS I for 'mips' and MIPS III for
13742      'mips64', just as we did in the days before 'from-abi'.  */
13743   if (strcasecmp (cpu_string, "from-abi") == 0)
13744     {
13745       if (ABI_NEEDS_32BIT_REGS (mips_abi))
13746         return mips_cpu_info_from_isa (ISA_MIPS1);
13747
13748       if (ABI_NEEDS_64BIT_REGS (mips_abi))
13749         return mips_cpu_info_from_isa (ISA_MIPS3);
13750
13751       if (file_mips_gp32 >= 0)
13752         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13753
13754       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13755                                      ? ISA_MIPS3
13756                                      : ISA_MIPS1);
13757     }
13758
13759   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
13760   if (strcasecmp (cpu_string, "default") == 0)
13761     return 0;
13762
13763   for (p = mips_cpu_info_table; p->name != 0; p++)
13764     if (mips_matching_cpu_name_p (p->name, cpu_string))
13765       return p;
13766
13767   as_bad ("Bad value (%s) for %s", cpu_string, option);
13768   return 0;
13769 }
13770
13771 /* Return the canonical processor information for ISA (a member of the
13772    ISA_MIPS* enumeration).  */
13773
13774 static const struct mips_cpu_info *
13775 mips_cpu_info_from_isa (isa)
13776      int isa;
13777 {
13778   int i;
13779
13780   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13781     if (mips_cpu_info_table[i].is_isa
13782         && isa == mips_cpu_info_table[i].isa)
13783       return (&mips_cpu_info_table[i]);
13784
13785   return NULL;
13786 }
13787 \f
13788 static void
13789 show (stream, string, col_p, first_p)
13790      FILE *stream;
13791      const char *string;
13792      int *col_p;
13793      int *first_p;
13794 {
13795   if (*first_p)
13796     {
13797       fprintf (stream, "%24s", "");
13798       *col_p = 24;
13799     }
13800   else
13801     {
13802       fprintf (stream, ", ");
13803       *col_p += 2;
13804     }
13805
13806   if (*col_p + strlen (string) > 72)
13807     {
13808       fprintf (stream, "\n%24s", "");
13809       *col_p = 24;
13810     }
13811
13812   fprintf (stream, "%s", string);
13813   *col_p += strlen (string);
13814
13815   *first_p = 0;
13816 }
13817
13818 void
13819 md_show_usage (stream)
13820      FILE *stream;
13821 {
13822   int column, first;
13823   size_t i;
13824
13825   fprintf (stream, _("\
13826 MIPS options:\n\
13827 -membedded-pic          generate embedded position independent code\n\
13828 -EB                     generate big endian output\n\
13829 -EL                     generate little endian output\n\
13830 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
13831 -G NUM                  allow referencing objects up to NUM bytes\n\
13832                         implicitly with the gp register [default 8]\n"));
13833   fprintf (stream, _("\
13834 -mips1                  generate MIPS ISA I instructions\n\
13835 -mips2                  generate MIPS ISA II instructions\n\
13836 -mips3                  generate MIPS ISA III instructions\n\
13837 -mips4                  generate MIPS ISA IV instructions\n\
13838 -mips5                  generate MIPS ISA V instructions\n\
13839 -mips32                 generate MIPS32 ISA instructions\n\
13840 -mips64                 generate MIPS64 ISA instructions\n\
13841 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
13842
13843   first = 1;
13844
13845   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13846     show (stream, mips_cpu_info_table[i].name, &column, &first);
13847   show (stream, "from-abi", &column, &first);
13848   fputc ('\n', stream);
13849
13850   fprintf (stream, _("\
13851 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13852 -no-mCPU                don't generate code specific to CPU.\n\
13853                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
13854
13855   first = 1;
13856
13857   show (stream, "3900", &column, &first);
13858   show (stream, "4010", &column, &first);
13859   show (stream, "4100", &column, &first);
13860   show (stream, "4650", &column, &first);
13861   fputc ('\n', stream);
13862
13863   fprintf (stream, _("\
13864 -mips16                 generate mips16 instructions\n\
13865 -no-mips16              do not generate mips16 instructions\n"));
13866   fprintf (stream, _("\
13867 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
13868 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
13869 -O0                     remove unneeded NOPs, do not swap branches\n\
13870 -O                      remove unneeded NOPs and swap branches\n\
13871 -n                      warn about NOPs generated from macros\n\
13872 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
13873 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
13874 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
13875 #ifdef OBJ_ELF
13876   fprintf (stream, _("\
13877 -KPIC, -call_shared     generate SVR4 position independent code\n\
13878 -non_shared             do not generate position independent code\n\
13879 -xgot                   assume a 32 bit GOT\n\
13880 -mabi=ABI               create ABI conformant object file for:\n"));
13881
13882   first = 1;
13883
13884   show (stream, "32", &column, &first);
13885   show (stream, "o64", &column, &first);
13886   show (stream, "n32", &column, &first);
13887   show (stream, "64", &column, &first);
13888   show (stream, "eabi", &column, &first);
13889
13890   fputc ('\n', stream);
13891
13892   fprintf (stream, _("\
13893 -32                     create o32 ABI object file (default)\n\
13894 -n32                    create n32 ABI object file\n\
13895 -64                     create 64 ABI object file\n"));
13896 #endif
13897 }