gas reloc rewrite.
[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 \f
669 /* Prototypes for static functions.  */
670
671 #ifdef __STDC__
672 #define internalError() \
673     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
674 #else
675 #define internalError() as_fatal (_("MIPS internal Error"));
676 #endif
677
678 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
679
680 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
681                                   unsigned int reg, enum mips_regclass class));
682 static int reg_needs_delay PARAMS ((unsigned int));
683 static void mips16_mark_labels PARAMS ((void));
684 static void append_insn PARAMS ((char *place,
685                                  struct mips_cl_insn * ip,
686                                  expressionS * p,
687                                  bfd_reloc_code_real_type *r,
688                                  boolean));
689 static void mips_no_prev_insn PARAMS ((int));
690 static void mips_emit_delays PARAMS ((boolean));
691 #ifdef USE_STDARG
692 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
693                                  const char *name, const char *fmt,
694                                  ...));
695 #else
696 static void macro_build ();
697 #endif
698 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
699                                         const char *, const char *,
700                                         va_list));
701 static void macro_build_jalr PARAMS ((int, expressionS *));
702 static void macro_build_lui PARAMS ((char *place, int *counter,
703                                      expressionS * ep, int regnum));
704 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
705 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
706                                          expressionS *));
707 static void load_register PARAMS ((int *, int, expressionS *, int));
708 static void load_address PARAMS ((int *, int, expressionS *, int *));
709 static void move_register PARAMS ((int *, int, int));
710 static void macro PARAMS ((struct mips_cl_insn * ip));
711 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
712 #ifdef LOSING_COMPILER
713 static void macro2 PARAMS ((struct mips_cl_insn * ip));
714 #endif
715 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
716 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
717 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
718                                   boolean, boolean, unsigned long *,
719                                   boolean *, unsigned short *));
720 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
721 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
722 static int my_getSmallExpression PARAMS ((expressionS *, char *));
723 static void my_getExpression PARAMS ((expressionS *, char *));
724 #ifdef OBJ_ELF
725 static int support_64bit_objects PARAMS((void));
726 #endif
727 static void mips_set_option_string PARAMS ((const char **, const char *));
728 static symbolS *get_symbol PARAMS ((void));
729 static void mips_align PARAMS ((int to, int fill, symbolS *label));
730 static void s_align PARAMS ((int));
731 static void s_change_sec PARAMS ((int));
732 static void s_change_section PARAMS ((int));
733 static void s_cons PARAMS ((int));
734 static void s_float_cons PARAMS ((int));
735 static void s_mips_globl PARAMS ((int));
736 static void s_option PARAMS ((int));
737 static void s_mipsset PARAMS ((int));
738 static void s_abicalls PARAMS ((int));
739 static void s_cpload PARAMS ((int));
740 static void s_cpsetup PARAMS ((int));
741 static void s_cplocal PARAMS ((int));
742 static void s_cprestore PARAMS ((int));
743 static void s_cpreturn PARAMS ((int));
744 static void s_gpvalue PARAMS ((int));
745 static void s_gpword PARAMS ((int));
746 static void s_cpadd PARAMS ((int));
747 static void s_insn PARAMS ((int));
748 static void md_obj_begin PARAMS ((void));
749 static void md_obj_end PARAMS ((void));
750 static long get_number PARAMS ((void));
751 static void s_mips_ent PARAMS ((int));
752 static void s_mips_end PARAMS ((int));
753 static void s_mips_frame PARAMS ((int));
754 static void s_mips_mask PARAMS ((int));
755 static void s_mips_stab PARAMS ((int));
756 static void s_mips_weakext PARAMS ((int));
757 static void s_mips_file PARAMS ((int));
758 static void s_mips_loc PARAMS ((int));
759 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
760 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
761 static void show PARAMS ((FILE *, const char *, int *, int *));
762 #ifdef OBJ_ELF
763 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
764 #endif
765
766 /* Return values of my_getSmallExpression().  */
767
768 enum small_ex_type
769 {
770   S_EX_NONE = 0,
771   S_EX_REGISTER,
772
773   /* Direct relocation creation by %percent_op().  */
774   S_EX_HALF,
775   S_EX_HI,
776   S_EX_LO,
777   S_EX_GP_REL,
778   S_EX_GOT,
779   S_EX_CALL16,
780   S_EX_GOT_DISP,
781   S_EX_GOT_PAGE,
782   S_EX_GOT_OFST,
783   S_EX_GOT_HI,
784   S_EX_GOT_LO,
785   S_EX_NEG,
786   S_EX_HIGHER,
787   S_EX_HIGHEST,
788   S_EX_CALL_HI,
789   S_EX_CALL_LO
790 };
791
792 /* Table and functions used to map between CPU/ISA names, and
793    ISA levels, and CPU numbers.  */
794
795 struct mips_cpu_info
796 {
797   const char *name;           /* CPU or ISA name.  */
798   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
799   int isa;                    /* ISA level.  */
800   int cpu;                    /* CPU number (default CPU if ISA).  */
801 };
802
803 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
804 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
805 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
806                                                         const char *));
807 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
808 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
809                                                            const char *));
810 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
811 \f
812 /* Pseudo-op table.
813
814    The following pseudo-ops from the Kane and Heinrich MIPS book
815    should be defined here, but are currently unsupported: .alias,
816    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
817
818    The following pseudo-ops from the Kane and Heinrich MIPS book are
819    specific to the type of debugging information being generated, and
820    should be defined by the object format: .aent, .begin, .bend,
821    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
822    .vreg.
823
824    The following pseudo-ops from the Kane and Heinrich MIPS book are
825    not MIPS CPU specific, but are also not specific to the object file
826    format.  This file is probably the best place to define them, but
827    they are not currently supported: .asm0, .endr, .lab, .repeat,
828    .struct.  */
829
830 static const pseudo_typeS mips_pseudo_table[] =
831 {
832   /* MIPS specific pseudo-ops.  */
833   {"option", s_option, 0},
834   {"set", s_mipsset, 0},
835   {"rdata", s_change_sec, 'r'},
836   {"sdata", s_change_sec, 's'},
837   {"livereg", s_ignore, 0},
838   {"abicalls", s_abicalls, 0},
839   {"cpload", s_cpload, 0},
840   {"cpsetup", s_cpsetup, 0},
841   {"cplocal", s_cplocal, 0},
842   {"cprestore", s_cprestore, 0},
843   {"cpreturn", s_cpreturn, 0},
844   {"gpvalue", s_gpvalue, 0},
845   {"gpword", s_gpword, 0},
846   {"cpadd", s_cpadd, 0},
847   {"insn", s_insn, 0},
848
849   /* Relatively generic pseudo-ops that happen to be used on MIPS
850      chips.  */
851   {"asciiz", stringer, 1},
852   {"bss", s_change_sec, 'b'},
853   {"err", s_err, 0},
854   {"half", s_cons, 1},
855   {"dword", s_cons, 3},
856   {"weakext", s_mips_weakext, 0},
857
858   /* These pseudo-ops are defined in read.c, but must be overridden
859      here for one reason or another.  */
860   {"align", s_align, 0},
861   {"byte", s_cons, 0},
862   {"data", s_change_sec, 'd'},
863   {"double", s_float_cons, 'd'},
864   {"float", s_float_cons, 'f'},
865   {"globl", s_mips_globl, 0},
866   {"global", s_mips_globl, 0},
867   {"hword", s_cons, 1},
868   {"int", s_cons, 2},
869   {"long", s_cons, 2},
870   {"octa", s_cons, 4},
871   {"quad", s_cons, 3},
872   {"section", s_change_section, 0},
873   {"short", s_cons, 1},
874   {"single", s_float_cons, 'f'},
875   {"stabn", s_mips_stab, 'n'},
876   {"text", s_change_sec, 't'},
877   {"word", s_cons, 2},
878
879   { "extern", ecoff_directive_extern, 0},
880
881   { NULL, NULL, 0 },
882 };
883
884 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
885 {
886   /* These pseudo-ops should be defined by the object file format.
887      However, a.out doesn't support them, so we have versions here.  */
888   {"aent", s_mips_ent, 1},
889   {"bgnb", s_ignore, 0},
890   {"end", s_mips_end, 0},
891   {"endb", s_ignore, 0},
892   {"ent", s_mips_ent, 0},
893   {"file", s_mips_file, 0},
894   {"fmask", s_mips_mask, 'F'},
895   {"frame", s_mips_frame, 0},
896   {"loc", s_mips_loc, 0},
897   {"mask", s_mips_mask, 'R'},
898   {"verstamp", s_ignore, 0},
899   { NULL, NULL, 0 },
900 };
901
902 extern void pop_insert PARAMS ((const pseudo_typeS *));
903
904 void
905 mips_pop_insert ()
906 {
907   pop_insert (mips_pseudo_table);
908   if (! ECOFF_DEBUGGING)
909     pop_insert (mips_nonecoff_pseudo_table);
910 }
911 \f
912 /* Symbols labelling the current insn.  */
913
914 struct insn_label_list
915 {
916   struct insn_label_list *next;
917   symbolS *label;
918 };
919
920 static struct insn_label_list *insn_labels;
921 static struct insn_label_list *free_insn_labels;
922
923 static void mips_clear_insn_labels PARAMS ((void));
924
925 static inline void
926 mips_clear_insn_labels ()
927 {
928   register struct insn_label_list **pl;
929
930   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
931     ;
932   *pl = insn_labels;
933   insn_labels = NULL;
934 }
935 \f
936 static char *expr_end;
937
938 /* Expressions which appear in instructions.  These are set by
939    mips_ip.  */
940
941 static expressionS imm_expr;
942 static expressionS offset_expr;
943
944 /* Relocs associated with imm_expr and offset_expr.  */
945
946 static bfd_reloc_code_real_type imm_reloc[3]
947   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
948 static bfd_reloc_code_real_type offset_reloc[3]
949   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
950
951 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
952
953 static boolean imm_unmatched_hi;
954
955 /* These are set by mips16_ip if an explicit extension is used.  */
956
957 static boolean mips16_small, mips16_ext;
958
959 /* The pdr segment for per procedure frame/regmask info.  Not used for
960    ECOFF debugging.  */
961
962 static segT pdr_seg;
963
964 /* The default target format to use.  */
965
966 const char *
967 mips_target_format ()
968 {
969   switch (OUTPUT_FLAVOR)
970     {
971     case bfd_target_aout_flavour:
972       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
973     case bfd_target_ecoff_flavour:
974       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
975     case bfd_target_coff_flavour:
976       return "pe-mips";
977     case bfd_target_elf_flavour:
978 #ifdef TE_TMIPS
979       /* This is traditional mips.  */
980       return (target_big_endian
981               ? (HAVE_64BIT_OBJECTS
982                  ? "elf64-tradbigmips"
983                  : (HAVE_NEWABI
984                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
985               : (HAVE_64BIT_OBJECTS
986                  ? "elf64-tradlittlemips"
987                  : (HAVE_NEWABI
988                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
989 #else
990       return (target_big_endian
991               ? (HAVE_64BIT_OBJECTS
992                  ? "elf64-bigmips"
993                  : (HAVE_NEWABI
994                     ? "elf32-nbigmips" : "elf32-bigmips"))
995               : (HAVE_64BIT_OBJECTS
996                  ? "elf64-littlemips"
997                  : (HAVE_NEWABI
998                     ? "elf32-nlittlemips" : "elf32-littlemips")));
999 #endif
1000     default:
1001       abort ();
1002       return NULL;
1003     }
1004 }
1005
1006 /* This function is called once, at assembler startup time.  It should
1007    set up all the tables, etc. that the MD part of the assembler will need.  */
1008
1009 void
1010 md_begin ()
1011 {
1012   register const char *retval = NULL;
1013   int i = 0;
1014   int broken = 0;
1015
1016   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1017     as_warn (_("Could not set architecture and machine"));
1018
1019   op_hash = hash_new ();
1020
1021   for (i = 0; i < NUMOPCODES;)
1022     {
1023       const char *name = mips_opcodes[i].name;
1024
1025       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1026       if (retval != NULL)
1027         {
1028           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1029                    mips_opcodes[i].name, retval);
1030           /* Probably a memory allocation problem?  Give up now.  */
1031           as_fatal (_("Broken assembler.  No assembly attempted."));
1032         }
1033       do
1034         {
1035           if (mips_opcodes[i].pinfo != INSN_MACRO)
1036             {
1037               if (!validate_mips_insn (&mips_opcodes[i]))
1038                 broken = 1;
1039             }
1040           ++i;
1041         }
1042       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1043     }
1044
1045   mips16_op_hash = hash_new ();
1046
1047   i = 0;
1048   while (i < bfd_mips16_num_opcodes)
1049     {
1050       const char *name = mips16_opcodes[i].name;
1051
1052       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1053       if (retval != NULL)
1054         as_fatal (_("internal: can't hash `%s': %s"),
1055                   mips16_opcodes[i].name, retval);
1056       do
1057         {
1058           if (mips16_opcodes[i].pinfo != INSN_MACRO
1059               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1060                   != mips16_opcodes[i].match))
1061             {
1062               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1063                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1064               broken = 1;
1065             }
1066           ++i;
1067         }
1068       while (i < bfd_mips16_num_opcodes
1069              && strcmp (mips16_opcodes[i].name, name) == 0);
1070     }
1071
1072   if (broken)
1073     as_fatal (_("Broken assembler.  No assembly attempted."));
1074
1075   /* We add all the general register names to the symbol table.  This
1076      helps us detect invalid uses of them.  */
1077   for (i = 0; i < 32; i++)
1078     {
1079       char buf[5];
1080
1081       sprintf (buf, "$%d", i);
1082       symbol_table_insert (symbol_new (buf, reg_section, i,
1083                                        &zero_address_frag));
1084     }
1085   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1086                                    &zero_address_frag));
1087   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1088                                    &zero_address_frag));
1089   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1090                                    &zero_address_frag));
1091   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1092                                    &zero_address_frag));
1093   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1094                                    &zero_address_frag));
1095   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1096                                    &zero_address_frag));
1097   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1098                                    &zero_address_frag));
1099   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1100                                    &zero_address_frag));
1101   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1102                                    &zero_address_frag));
1103
1104   mips_no_prev_insn (false);
1105
1106   mips_gprmask = 0;
1107   mips_cprmask[0] = 0;
1108   mips_cprmask[1] = 0;
1109   mips_cprmask[2] = 0;
1110   mips_cprmask[3] = 0;
1111
1112   /* set the default alignment for the text section (2**2) */
1113   record_alignment (text_section, 2);
1114
1115   if (USE_GLOBAL_POINTER_OPT)
1116     bfd_set_gp_size (stdoutput, g_switch_value);
1117
1118   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1119     {
1120       /* On a native system, sections must be aligned to 16 byte
1121          boundaries.  When configured for an embedded ELF target, we
1122          don't bother.  */
1123       if (strcmp (TARGET_OS, "elf") != 0)
1124         {
1125           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1126           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1127           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1128         }
1129
1130       /* Create a .reginfo section for register masks and a .mdebug
1131          section for debugging information.  */
1132       {
1133         segT seg;
1134         subsegT subseg;
1135         flagword flags;
1136         segT sec;
1137
1138         seg = now_seg;
1139         subseg = now_subseg;
1140
1141         /* The ABI says this section should be loaded so that the
1142            running program can access it.  However, we don't load it
1143            if we are configured for an embedded target */
1144         flags = SEC_READONLY | SEC_DATA;
1145         if (strcmp (TARGET_OS, "elf") != 0)
1146           flags |= SEC_ALLOC | SEC_LOAD;
1147
1148         if (mips_abi != N64_ABI)
1149           {
1150             sec = subseg_new (".reginfo", (subsegT) 0);
1151
1152             bfd_set_section_flags (stdoutput, sec, flags);
1153             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1154
1155 #ifdef OBJ_ELF
1156             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1157 #endif
1158           }
1159         else
1160           {
1161             /* The 64-bit ABI uses a .MIPS.options section rather than
1162                .reginfo section.  */
1163             sec = subseg_new (".MIPS.options", (subsegT) 0);
1164             bfd_set_section_flags (stdoutput, sec, flags);
1165             bfd_set_section_alignment (stdoutput, sec, 3);
1166
1167 #ifdef OBJ_ELF
1168             /* Set up the option header.  */
1169             {
1170               Elf_Internal_Options opthdr;
1171               char *f;
1172
1173               opthdr.kind = ODK_REGINFO;
1174               opthdr.size = (sizeof (Elf_External_Options)
1175                              + sizeof (Elf64_External_RegInfo));
1176               opthdr.section = 0;
1177               opthdr.info = 0;
1178               f = frag_more (sizeof (Elf_External_Options));
1179               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1180                                              (Elf_External_Options *) f);
1181
1182               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1183             }
1184 #endif
1185           }
1186
1187         if (ECOFF_DEBUGGING)
1188           {
1189             sec = subseg_new (".mdebug", (subsegT) 0);
1190             (void) bfd_set_section_flags (stdoutput, sec,
1191                                           SEC_HAS_CONTENTS | SEC_READONLY);
1192             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1193           }
1194 #ifdef OBJ_ELF
1195         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1196           {
1197             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1198             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1199                                           SEC_READONLY | SEC_RELOC
1200                                           | SEC_DEBUGGING);
1201             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1202           }
1203 #endif
1204
1205         subseg_set (seg, subseg);
1206       }
1207     }
1208
1209   if (! ECOFF_DEBUGGING)
1210     md_obj_begin ();
1211 }
1212
1213 void
1214 md_mips_end ()
1215 {
1216   if (! ECOFF_DEBUGGING)
1217     md_obj_end ();
1218 }
1219
1220 void
1221 md_assemble (str)
1222      char *str;
1223 {
1224   struct mips_cl_insn insn;
1225   bfd_reloc_code_real_type unused_reloc[3]
1226     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1227
1228   imm_expr.X_op = O_absent;
1229   imm_unmatched_hi = false;
1230   offset_expr.X_op = O_absent;
1231   imm_reloc[0] = BFD_RELOC_UNUSED;
1232   imm_reloc[1] = BFD_RELOC_UNUSED;
1233   imm_reloc[2] = BFD_RELOC_UNUSED;
1234   offset_reloc[0] = BFD_RELOC_UNUSED;
1235   offset_reloc[1] = BFD_RELOC_UNUSED;
1236   offset_reloc[2] = BFD_RELOC_UNUSED;
1237
1238   if (mips_opts.mips16)
1239     mips16_ip (str, &insn);
1240   else
1241     {
1242       mips_ip (str, &insn);
1243       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1244             str, insn.insn_opcode));
1245     }
1246
1247   if (insn_error)
1248     {
1249       as_bad ("%s `%s'", insn_error, str);
1250       return;
1251     }
1252
1253   if (insn.insn_mo->pinfo == INSN_MACRO)
1254     {
1255       if (mips_opts.mips16)
1256         mips16_macro (&insn);
1257       else
1258         macro (&insn);
1259     }
1260   else
1261     {
1262       if (imm_expr.X_op != O_absent)
1263         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1264       else if (offset_expr.X_op != O_absent)
1265         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1266       else
1267         append_insn (NULL, &insn, NULL, unused_reloc, false);
1268     }
1269 }
1270
1271 /* See whether instruction IP reads register REG.  CLASS is the type
1272    of register.  */
1273
1274 static int
1275 insn_uses_reg (ip, reg, class)
1276      struct mips_cl_insn *ip;
1277      unsigned int reg;
1278      enum mips_regclass class;
1279 {
1280   if (class == MIPS16_REG)
1281     {
1282       assert (mips_opts.mips16);
1283       reg = mips16_to_32_reg_map[reg];
1284       class = MIPS_GR_REG;
1285     }
1286
1287   /* Don't report on general register ZERO, since it never changes.  */
1288   if (class == MIPS_GR_REG && reg == ZERO)
1289     return 0;
1290
1291   if (class == MIPS_FP_REG)
1292     {
1293       assert (! mips_opts.mips16);
1294       /* If we are called with either $f0 or $f1, we must check $f0.
1295          This is not optimal, because it will introduce an unnecessary
1296          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1297          need to distinguish reading both $f0 and $f1 or just one of
1298          them.  Note that we don't have to check the other way,
1299          because there is no instruction that sets both $f0 and $f1
1300          and requires a delay.  */
1301       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1302           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1303               == (reg &~ (unsigned) 1)))
1304         return 1;
1305       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1306           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1307               == (reg &~ (unsigned) 1)))
1308         return 1;
1309     }
1310   else if (! mips_opts.mips16)
1311     {
1312       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1313           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1314         return 1;
1315       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1316           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1317         return 1;
1318     }
1319   else
1320     {
1321       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1322           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1323                                     & MIPS16OP_MASK_RX)]
1324               == reg))
1325         return 1;
1326       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1327           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1328                                     & MIPS16OP_MASK_RY)]
1329               == reg))
1330         return 1;
1331       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1332           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1333                                     & MIPS16OP_MASK_MOVE32Z)]
1334               == reg))
1335         return 1;
1336       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1337         return 1;
1338       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1339         return 1;
1340       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1341         return 1;
1342       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1343           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1344               & MIPS16OP_MASK_REGR32) == reg)
1345         return 1;
1346     }
1347
1348   return 0;
1349 }
1350
1351 /* This function returns true if modifying a register requires a
1352    delay.  */
1353
1354 static int
1355 reg_needs_delay (reg)
1356      unsigned int reg;
1357 {
1358   unsigned long prev_pinfo;
1359
1360   prev_pinfo = prev_insn.insn_mo->pinfo;
1361   if (! mips_opts.noreorder
1362       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1363       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1364           || (! gpr_interlocks
1365               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1366     {
1367       /* A load from a coprocessor or from memory.  All load
1368          delays delay the use of general register rt for one
1369          instruction on the r3000.  The r6000 and r4000 use
1370          interlocks.  */
1371       /* Itbl support may require additional care here.  */
1372       know (prev_pinfo & INSN_WRITE_GPR_T);
1373       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1374         return 1;
1375     }
1376
1377   return 0;
1378 }
1379
1380 /* Mark instruction labels in mips16 mode.  This permits the linker to
1381    handle them specially, such as generating jalx instructions when
1382    needed.  We also make them odd for the duration of the assembly, in
1383    order to generate the right sort of code.  We will make them even
1384    in the adjust_symtab routine, while leaving them marked.  This is
1385    convenient for the debugger and the disassembler.  The linker knows
1386    to make them odd again.  */
1387
1388 static void
1389 mips16_mark_labels ()
1390 {
1391   if (mips_opts.mips16)
1392     {
1393       struct insn_label_list *l;
1394       valueT val;
1395
1396       for (l = insn_labels; l != NULL; l = l->next)
1397         {
1398 #ifdef OBJ_ELF
1399           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1400             S_SET_OTHER (l->label, STO_MIPS16);
1401 #endif
1402           val = S_GET_VALUE (l->label);
1403           if ((val & 1) == 0)
1404             S_SET_VALUE (l->label, val + 1);
1405         }
1406     }
1407 }
1408
1409 /* Output an instruction.  PLACE is where to put the instruction; if
1410    it is NULL, this uses frag_more to get room.  IP is the instruction
1411    information.  ADDRESS_EXPR is an operand of the instruction to be
1412    used with RELOC_TYPE.  */
1413
1414 static void
1415 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1416      char *place;
1417      struct mips_cl_insn *ip;
1418      expressionS *address_expr;
1419      bfd_reloc_code_real_type *reloc_type;
1420      boolean unmatched_hi;
1421 {
1422   register unsigned long prev_pinfo, pinfo;
1423   char *f;
1424   fixS *fixp[3];
1425   int nops = 0;
1426
1427   /* Mark instruction labels in mips16 mode.  */
1428   mips16_mark_labels ();
1429
1430   prev_pinfo = prev_insn.insn_mo->pinfo;
1431   pinfo = ip->insn_mo->pinfo;
1432
1433   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1434     {
1435       int prev_prev_nop;
1436
1437       /* If the previous insn required any delay slots, see if we need
1438          to insert a NOP or two.  There are eight kinds of possible
1439          hazards, of which an instruction can have at most one type.
1440          (1) a load from memory delay
1441          (2) a load from a coprocessor delay
1442          (3) an unconditional branch delay
1443          (4) a conditional branch delay
1444          (5) a move to coprocessor register delay
1445          (6) a load coprocessor register from memory delay
1446          (7) a coprocessor condition code delay
1447          (8) a HI/LO special register delay
1448
1449          There are a lot of optimizations we could do that we don't.
1450          In particular, we do not, in general, reorder instructions.
1451          If you use gcc with optimization, it will reorder
1452          instructions and generally do much more optimization then we
1453          do here; repeating all that work in the assembler would only
1454          benefit hand written assembly code, and does not seem worth
1455          it.  */
1456
1457       /* This is how a NOP is emitted.  */
1458 #define emit_nop()                                      \
1459   (mips_opts.mips16                                     \
1460    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1461    : md_number_to_chars (frag_more (4), 0, 4))
1462
1463       /* The previous insn might require a delay slot, depending upon
1464          the contents of the current insn.  */
1465       if (! mips_opts.mips16
1466           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1467           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1468                && ! cop_interlocks)
1469               || (! gpr_interlocks
1470                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1471         {
1472           /* A load from a coprocessor or from memory.  All load
1473              delays delay the use of general register rt for one
1474              instruction on the r3000.  The r6000 and r4000 use
1475              interlocks.  */
1476           /* Itbl support may require additional care here.  */
1477           know (prev_pinfo & INSN_WRITE_GPR_T);
1478           if (mips_optimize == 0
1479               || insn_uses_reg (ip,
1480                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1481                                  & OP_MASK_RT),
1482                                 MIPS_GR_REG))
1483             ++nops;
1484         }
1485       else if (! mips_opts.mips16
1486                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1487                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1488                     && ! cop_interlocks)
1489                    || (mips_opts.isa == ISA_MIPS1
1490                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1491         {
1492           /* A generic coprocessor delay.  The previous instruction
1493              modified a coprocessor general or control register.  If
1494              it modified a control register, we need to avoid any
1495              coprocessor instruction (this is probably not always
1496              required, but it sometimes is).  If it modified a general
1497              register, we avoid using that register.
1498
1499              On the r6000 and r4000 loading a coprocessor register
1500              from memory is interlocked, and does not require a delay.
1501
1502              This case is not handled very well.  There is no special
1503              knowledge of CP0 handling, and the coprocessors other
1504              than the floating point unit are not distinguished at
1505              all.  */
1506           /* Itbl support may require additional care here. FIXME!
1507              Need to modify this to include knowledge about
1508              user specified delays!  */
1509           if (prev_pinfo & INSN_WRITE_FPR_T)
1510             {
1511               if (mips_optimize == 0
1512                   || insn_uses_reg (ip,
1513                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1514                                      & OP_MASK_FT),
1515                                     MIPS_FP_REG))
1516                 ++nops;
1517             }
1518           else if (prev_pinfo & INSN_WRITE_FPR_S)
1519             {
1520               if (mips_optimize == 0
1521                   || insn_uses_reg (ip,
1522                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1523                                      & OP_MASK_FS),
1524                                     MIPS_FP_REG))
1525                 ++nops;
1526             }
1527           else
1528             {
1529               /* We don't know exactly what the previous instruction
1530                  does.  If the current instruction uses a coprocessor
1531                  register, we must insert a NOP.  If previous
1532                  instruction may set the condition codes, and the
1533                  current instruction uses them, we must insert two
1534                  NOPS.  */
1535               /* Itbl support may require additional care here.  */
1536               if (mips_optimize == 0
1537                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1538                       && (pinfo & INSN_READ_COND_CODE)))
1539                 nops += 2;
1540               else if (pinfo & INSN_COP)
1541                 ++nops;
1542             }
1543         }
1544       else if (! mips_opts.mips16
1545                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1546                && (prev_pinfo & INSN_WRITE_COND_CODE)
1547                && ! cop_interlocks)
1548         {
1549           /* The previous instruction sets the coprocessor condition
1550              codes, but does not require a general coprocessor delay
1551              (this means it is a floating point comparison
1552              instruction).  If this instruction uses the condition
1553              codes, we need to insert a single NOP.  */
1554           /* Itbl support may require additional care here.  */
1555           if (mips_optimize == 0
1556               || (pinfo & INSN_READ_COND_CODE))
1557             ++nops;
1558         }
1559
1560       /* If we're fixing up mfhi/mflo for the r7000 and the
1561          previous insn was an mfhi/mflo and the current insn
1562          reads the register that the mfhi/mflo wrote to, then
1563          insert two nops.  */
1564
1565       else if (mips_7000_hilo_fix
1566                && MF_HILO_INSN (prev_pinfo)
1567                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1568                                       & OP_MASK_RD),
1569                                  MIPS_GR_REG))
1570         {
1571           nops += 2;
1572         }
1573
1574       /* If we're fixing up mfhi/mflo for the r7000 and the
1575          2nd previous insn was an mfhi/mflo and the current insn
1576          reads the register that the mfhi/mflo wrote to, then
1577          insert one nop.  */
1578
1579       else if (mips_7000_hilo_fix
1580                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1581                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1582                                        & OP_MASK_RD),
1583                                     MIPS_GR_REG))
1584
1585         {
1586           ++nops;
1587         }
1588
1589       else if (prev_pinfo & INSN_READ_LO)
1590         {
1591           /* The previous instruction reads the LO register; if the
1592              current instruction writes to the LO register, we must
1593              insert two NOPS.  Some newer processors have interlocks.
1594              Also the tx39's multiply instructions can be exectuted
1595              immediatly after a read from HI/LO (without the delay),
1596              though the tx39's divide insns still do require the
1597              delay.  */
1598           if (! (hilo_interlocks
1599                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1600               && (mips_optimize == 0
1601                   || (pinfo & INSN_WRITE_LO)))
1602             nops += 2;
1603           /* Most mips16 branch insns don't have a delay slot.
1604              If a read from LO is immediately followed by a branch
1605              to a write to LO we have a read followed by a write
1606              less than 2 insns away.  We assume the target of
1607              a branch might be a write to LO, and insert a nop
1608              between a read and an immediately following branch.  */
1609           else if (mips_opts.mips16
1610                    && (mips_optimize == 0
1611                        || (pinfo & MIPS16_INSN_BRANCH)))
1612             ++nops;
1613         }
1614       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1615         {
1616           /* The previous instruction reads the HI register; if the
1617              current instruction writes to the HI register, we must
1618              insert a NOP.  Some newer processors have interlocks.
1619              Also the note tx39's multiply above.  */
1620           if (! (hilo_interlocks
1621                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1622               && (mips_optimize == 0
1623                   || (pinfo & INSN_WRITE_HI)))
1624             nops += 2;
1625           /* Most mips16 branch insns don't have a delay slot.
1626              If a read from HI is immediately followed by a branch
1627              to a write to HI we have a read followed by a write
1628              less than 2 insns away.  We assume the target of
1629              a branch might be a write to HI, and insert a nop
1630              between a read and an immediately following branch.  */
1631           else if (mips_opts.mips16
1632                    && (mips_optimize == 0
1633                        || (pinfo & MIPS16_INSN_BRANCH)))
1634             ++nops;
1635         }
1636
1637       /* If the previous instruction was in a noreorder section, then
1638          we don't want to insert the nop after all.  */
1639       /* Itbl support may require additional care here.  */
1640       if (prev_insn_unreordered)
1641         nops = 0;
1642
1643       /* There are two cases which require two intervening
1644          instructions: 1) setting the condition codes using a move to
1645          coprocessor instruction which requires a general coprocessor
1646          delay and then reading the condition codes 2) reading the HI
1647          or LO register and then writing to it (except on processors
1648          which have interlocks).  If we are not already emitting a NOP
1649          instruction, we must check for these cases compared to the
1650          instruction previous to the previous instruction.  */
1651       if ((! mips_opts.mips16
1652            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1653            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1654            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1655            && (pinfo & INSN_READ_COND_CODE)
1656            && ! cop_interlocks)
1657           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1658               && (pinfo & INSN_WRITE_LO)
1659               && ! (hilo_interlocks
1660                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1661           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1662               && (pinfo & INSN_WRITE_HI)
1663               && ! (hilo_interlocks
1664                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1665         prev_prev_nop = 1;
1666       else
1667         prev_prev_nop = 0;
1668
1669       if (prev_prev_insn_unreordered)
1670         prev_prev_nop = 0;
1671
1672       if (prev_prev_nop && nops == 0)
1673         ++nops;
1674
1675       /* If we are being given a nop instruction, don't bother with
1676          one of the nops we would otherwise output.  This will only
1677          happen when a nop instruction is used with mips_optimize set
1678          to 0.  */
1679       if (nops > 0
1680           && ! mips_opts.noreorder
1681           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1682         --nops;
1683
1684       /* Now emit the right number of NOP instructions.  */
1685       if (nops > 0 && ! mips_opts.noreorder)
1686         {
1687           fragS *old_frag;
1688           unsigned long old_frag_offset;
1689           int i;
1690           struct insn_label_list *l;
1691
1692           old_frag = frag_now;
1693           old_frag_offset = frag_now_fix ();
1694
1695           for (i = 0; i < nops; i++)
1696             emit_nop ();
1697
1698           if (listing)
1699             {
1700               listing_prev_line ();
1701               /* We may be at the start of a variant frag.  In case we
1702                  are, make sure there is enough space for the frag
1703                  after the frags created by listing_prev_line.  The
1704                  argument to frag_grow here must be at least as large
1705                  as the argument to all other calls to frag_grow in
1706                  this file.  We don't have to worry about being in the
1707                  middle of a variant frag, because the variants insert
1708                  all needed nop instructions themselves.  */
1709               frag_grow (40);
1710             }
1711
1712           for (l = insn_labels; l != NULL; l = l->next)
1713             {
1714               valueT val;
1715
1716               assert (S_GET_SEGMENT (l->label) == now_seg);
1717               symbol_set_frag (l->label, frag_now);
1718               val = (valueT) frag_now_fix ();
1719               /* mips16 text labels are stored as odd.  */
1720               if (mips_opts.mips16)
1721                 ++val;
1722               S_SET_VALUE (l->label, val);
1723             }
1724
1725 #ifndef NO_ECOFF_DEBUGGING
1726           if (ECOFF_DEBUGGING)
1727             ecoff_fix_loc (old_frag, old_frag_offset);
1728 #endif
1729         }
1730       else if (prev_nop_frag != NULL)
1731         {
1732           /* We have a frag holding nops we may be able to remove.  If
1733              we don't need any nops, we can decrease the size of
1734              prev_nop_frag by the size of one instruction.  If we do
1735              need some nops, we count them in prev_nops_required.  */
1736           if (prev_nop_frag_since == 0)
1737             {
1738               if (nops == 0)
1739                 {
1740                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1741                   --prev_nop_frag_holds;
1742                 }
1743               else
1744                 prev_nop_frag_required += nops;
1745             }
1746           else
1747             {
1748               if (prev_prev_nop == 0)
1749                 {
1750                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1751                   --prev_nop_frag_holds;
1752                 }
1753               else
1754                 ++prev_nop_frag_required;
1755             }
1756
1757           if (prev_nop_frag_holds <= prev_nop_frag_required)
1758             prev_nop_frag = NULL;
1759
1760           ++prev_nop_frag_since;
1761
1762           /* Sanity check: by the time we reach the second instruction
1763              after prev_nop_frag, we should have used up all the nops
1764              one way or another.  */
1765           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1766         }
1767     }
1768
1769   if (*reloc_type > BFD_RELOC_UNUSED)
1770     {
1771       /* We need to set up a variant frag.  */
1772       assert (mips_opts.mips16 && address_expr != NULL);
1773       f = frag_var (rs_machine_dependent, 4, 0,
1774                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1775                                          mips16_small, mips16_ext,
1776                                          (prev_pinfo
1777                                           & INSN_UNCOND_BRANCH_DELAY),
1778                                          (*prev_insn_reloc_type
1779                                           == BFD_RELOC_MIPS16_JMP)),
1780                     make_expr_symbol (address_expr), 0, NULL);
1781     }
1782   else if (place != NULL)
1783     f = place;
1784   else if (mips_opts.mips16
1785            && ! ip->use_extend
1786            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1787     {
1788       /* Make sure there is enough room to swap this instruction with
1789          a following jump instruction.  */
1790       frag_grow (6);
1791       f = frag_more (2);
1792     }
1793   else
1794     {
1795       if (mips_opts.mips16
1796           && mips_opts.noreorder
1797           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1798         as_warn (_("extended instruction in delay slot"));
1799
1800       f = frag_more (4);
1801     }
1802
1803   fixp[0] = fixp[1] = fixp[2] = NULL;
1804   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1805     {
1806       if (address_expr->X_op == O_constant)
1807         {
1808           valueT tmp;
1809
1810           switch (*reloc_type)
1811             {
1812             case BFD_RELOC_32:
1813               ip->insn_opcode |= address_expr->X_add_number;
1814               break;
1815
1816             case BFD_RELOC_MIPS_HIGHEST:
1817               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1818               tmp >>= 16;
1819               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1820               break;
1821
1822             case BFD_RELOC_MIPS_HIGHER:
1823               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1824               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1825               break;
1826
1827             case BFD_RELOC_HI16_S:
1828               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1829                                   >> 16) & 0xffff;
1830               break;
1831
1832             case BFD_RELOC_HI16:
1833               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1834               break;
1835
1836             case BFD_RELOC_LO16:
1837               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1838               break;
1839
1840             case BFD_RELOC_MIPS_JMP:
1841               if ((address_expr->X_add_number & 3) != 0)
1842                 as_bad (_("jump to misaligned address (0x%lx)"),
1843                         (unsigned long) address_expr->X_add_number);
1844               if (address_expr->X_add_number & ~0xfffffff
1845                   || address_expr->X_add_number > 0x7fffffc)
1846                 as_bad (_("jump address range overflow (0x%lx)"),
1847                         (unsigned long) address_expr->X_add_number);
1848               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1849               break;
1850
1851             case BFD_RELOC_MIPS16_JMP:
1852               if ((address_expr->X_add_number & 3) != 0)
1853                 as_bad (_("jump to misaligned address (0x%lx)"),
1854                         (unsigned long) address_expr->X_add_number);
1855               if (address_expr->X_add_number & ~0xfffffff
1856                   || address_expr->X_add_number > 0x7fffffc)
1857                 as_bad (_("jump address range overflow (0x%lx)"),
1858                         (unsigned long) address_expr->X_add_number);
1859               ip->insn_opcode |=
1860                 (((address_expr->X_add_number & 0x7c0000) << 3)
1861                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1862                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1863               break;
1864
1865             case BFD_RELOC_16_PCREL:
1866               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1867               break;
1868
1869             case BFD_RELOC_16_PCREL_S2:
1870               goto need_reloc;
1871
1872             default:
1873               internalError ();
1874             }
1875         }
1876       else
1877         {
1878         need_reloc:
1879           /* Don't generate a reloc if we are writing into a variant frag.  */
1880           if (place == NULL)
1881             {
1882               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1883                                      address_expr,
1884                                      (*reloc_type == BFD_RELOC_16_PCREL
1885                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
1886                                      reloc_type[0]);
1887
1888               /* These relocations can have an addend that won't fit in
1889                  4 octets for 64bit assembly.  */
1890               if (HAVE_64BIT_GPRS &&
1891                   (*reloc_type == BFD_RELOC_16
1892                    || *reloc_type == BFD_RELOC_32
1893                    || *reloc_type == BFD_RELOC_MIPS_JMP
1894                    || *reloc_type == BFD_RELOC_HI16_S
1895                    || *reloc_type == BFD_RELOC_LO16
1896                    || *reloc_type == BFD_RELOC_GPREL16
1897                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
1898                    || *reloc_type == BFD_RELOC_GPREL32
1899                    || *reloc_type == BFD_RELOC_64
1900                    || *reloc_type == BFD_RELOC_CTOR
1901                    || *reloc_type == BFD_RELOC_MIPS_SUB
1902                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1903                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
1904                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1905                    || *reloc_type == BFD_RELOC_MIPS_REL16
1906                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1907                 fixp[0]->fx_no_overflow = 1;
1908
1909               if (unmatched_hi)
1910                 {
1911                   struct mips_hi_fixup *hi_fixup;
1912
1913                   assert (*reloc_type == BFD_RELOC_HI16_S);
1914                   hi_fixup = ((struct mips_hi_fixup *)
1915                               xmalloc (sizeof (struct mips_hi_fixup)));
1916                   hi_fixup->fixp = fixp[0];
1917                   hi_fixup->seg = now_seg;
1918                   hi_fixup->next = mips_hi_fixup_list;
1919                   mips_hi_fixup_list = hi_fixup;
1920                 }
1921
1922               if (reloc_type[1] != BFD_RELOC_UNUSED)
1923                 {
1924                   /* FIXME: This symbol can be one of
1925                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
1926                   address_expr->X_op = O_absent;
1927                   address_expr->X_add_symbol = 0;
1928                   address_expr->X_add_number = 0;
1929
1930                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1931                                          4, address_expr, false,
1932                                          reloc_type[1]);
1933
1934                   /* These relocations can have an addend that won't fit in
1935                      4 octets for 64bit assembly.  */
1936                   if (HAVE_64BIT_GPRS &&
1937                       (*reloc_type == BFD_RELOC_16
1938                        || *reloc_type == BFD_RELOC_32
1939                        || *reloc_type == BFD_RELOC_MIPS_JMP
1940                        || *reloc_type == BFD_RELOC_HI16_S
1941                        || *reloc_type == BFD_RELOC_LO16
1942                        || *reloc_type == BFD_RELOC_GPREL16
1943                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
1944                        || *reloc_type == BFD_RELOC_GPREL32
1945                        || *reloc_type == BFD_RELOC_64
1946                        || *reloc_type == BFD_RELOC_CTOR
1947                        || *reloc_type == BFD_RELOC_MIPS_SUB
1948                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1949                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
1950                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1951                        || *reloc_type == BFD_RELOC_MIPS_REL16
1952                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1953                     fixp[1]->fx_no_overflow = 1;
1954
1955                   if (reloc_type[2] != BFD_RELOC_UNUSED)
1956                     {
1957                       address_expr->X_op = O_absent;
1958                       address_expr->X_add_symbol = 0;
1959                       address_expr->X_add_number = 0;
1960
1961                       fixp[2] = fix_new_exp (frag_now,
1962                                              f - frag_now->fr_literal, 4,
1963                                              address_expr, false,
1964                                              reloc_type[2]);
1965
1966                       /* These relocations can have an addend that won't fit in
1967                          4 octets for 64bit assembly.  */
1968                       if (HAVE_64BIT_GPRS &&
1969                           (*reloc_type == BFD_RELOC_16
1970                            || *reloc_type == BFD_RELOC_32
1971                            || *reloc_type == BFD_RELOC_MIPS_JMP
1972                            || *reloc_type == BFD_RELOC_HI16_S
1973                            || *reloc_type == BFD_RELOC_LO16
1974                            || *reloc_type == BFD_RELOC_GPREL16
1975                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
1976                            || *reloc_type == BFD_RELOC_GPREL32
1977                            || *reloc_type == BFD_RELOC_64
1978                            || *reloc_type == BFD_RELOC_CTOR
1979                            || *reloc_type == BFD_RELOC_MIPS_SUB
1980                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1981                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
1982                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1983                            || *reloc_type == BFD_RELOC_MIPS_REL16
1984                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1985                         fixp[2]->fx_no_overflow = 1;
1986                     }
1987                 }
1988             }
1989         }
1990     }
1991
1992   if (! mips_opts.mips16)
1993     {
1994       md_number_to_chars (f, ip->insn_opcode, 4);
1995 #ifdef OBJ_ELF
1996       dwarf2_emit_insn (4);
1997 #endif
1998     }
1999   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2000     {
2001       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2002       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2003 #ifdef OBJ_ELF
2004       dwarf2_emit_insn (4);
2005 #endif
2006     }
2007   else
2008     {
2009       if (ip->use_extend)
2010         {
2011           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2012           f += 2;
2013         }
2014       md_number_to_chars (f, ip->insn_opcode, 2);
2015 #ifdef OBJ_ELF
2016       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2017 #endif
2018     }
2019
2020   /* Update the register mask information.  */
2021   if (! mips_opts.mips16)
2022     {
2023       if (pinfo & INSN_WRITE_GPR_D)
2024         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2025       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2026         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2027       if (pinfo & INSN_READ_GPR_S)
2028         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2029       if (pinfo & INSN_WRITE_GPR_31)
2030         mips_gprmask |= 1 << RA;
2031       if (pinfo & INSN_WRITE_FPR_D)
2032         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2033       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2034         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2035       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2036         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2037       if ((pinfo & INSN_READ_FPR_R) != 0)
2038         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2039       if (pinfo & INSN_COP)
2040         {
2041           /* We don't keep enough information to sort these cases out.
2042              The itbl support does keep this information however, although
2043              we currently don't support itbl fprmats as part of the cop
2044              instruction.  May want to add this support in the future.  */
2045         }
2046       /* Never set the bit for $0, which is always zero.  */
2047       mips_gprmask &= ~1 << 0;
2048     }
2049   else
2050     {
2051       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2052         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2053                               & MIPS16OP_MASK_RX);
2054       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2055         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2056                               & MIPS16OP_MASK_RY);
2057       if (pinfo & MIPS16_INSN_WRITE_Z)
2058         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2059                               & MIPS16OP_MASK_RZ);
2060       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2061         mips_gprmask |= 1 << TREG;
2062       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2063         mips_gprmask |= 1 << SP;
2064       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2065         mips_gprmask |= 1 << RA;
2066       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2067         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2068       if (pinfo & MIPS16_INSN_READ_Z)
2069         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2070                               & MIPS16OP_MASK_MOVE32Z);
2071       if (pinfo & MIPS16_INSN_READ_GPR_X)
2072         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2073                               & MIPS16OP_MASK_REGR32);
2074     }
2075
2076   if (place == NULL && ! mips_opts.noreorder)
2077     {
2078       /* Filling the branch delay slot is more complex.  We try to
2079          switch the branch with the previous instruction, which we can
2080          do if the previous instruction does not set up a condition
2081          that the branch tests and if the branch is not itself the
2082          target of any branch.  */
2083       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2084           || (pinfo & INSN_COND_BRANCH_DELAY))
2085         {
2086           if (mips_optimize < 2
2087               /* If we have seen .set volatile or .set nomove, don't
2088                  optimize.  */
2089               || mips_opts.nomove != 0
2090               /* If we had to emit any NOP instructions, then we
2091                  already know we can not swap.  */
2092               || nops != 0
2093               /* If we don't even know the previous insn, we can not
2094                  swap.  */
2095               || ! prev_insn_valid
2096               /* If the previous insn is already in a branch delay
2097                  slot, then we can not swap.  */
2098               || prev_insn_is_delay_slot
2099               /* If the previous previous insn was in a .set
2100                  noreorder, we can't swap.  Actually, the MIPS
2101                  assembler will swap in this situation.  However, gcc
2102                  configured -with-gnu-as will generate code like
2103                    .set noreorder
2104                    lw   $4,XXX
2105                    .set reorder
2106                    INSN
2107                    bne  $4,$0,foo
2108                  in which we can not swap the bne and INSN.  If gcc is
2109                  not configured -with-gnu-as, it does not output the
2110                  .set pseudo-ops.  We don't have to check
2111                  prev_insn_unreordered, because prev_insn_valid will
2112                  be 0 in that case.  We don't want to use
2113                  prev_prev_insn_valid, because we do want to be able
2114                  to swap at the start of a function.  */
2115               || prev_prev_insn_unreordered
2116               /* If the branch is itself the target of a branch, we
2117                  can not swap.  We cheat on this; all we check for is
2118                  whether there is a label on this instruction.  If
2119                  there are any branches to anything other than a
2120                  label, users must use .set noreorder.  */
2121               || insn_labels != NULL
2122               /* If the previous instruction is in a variant frag, we
2123                  can not do the swap.  This does not apply to the
2124                  mips16, which uses variant frags for different
2125                  purposes.  */
2126               || (! mips_opts.mips16
2127                   && prev_insn_frag->fr_type == rs_machine_dependent)
2128               /* If the branch reads the condition codes, we don't
2129                  even try to swap, because in the sequence
2130                    ctc1 $X,$31
2131                    INSN
2132                    INSN
2133                    bc1t LABEL
2134                  we can not swap, and I don't feel like handling that
2135                  case.  */
2136               || (! mips_opts.mips16
2137                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2138                   && (pinfo & INSN_READ_COND_CODE))
2139               /* We can not swap with an instruction that requires a
2140                  delay slot, becase the target of the branch might
2141                  interfere with that instruction.  */
2142               || (! mips_opts.mips16
2143                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2144                   && (prev_pinfo
2145               /* Itbl support may require additional care here.  */
2146                       & (INSN_LOAD_COPROC_DELAY
2147                          | INSN_COPROC_MOVE_DELAY
2148                          | INSN_WRITE_COND_CODE)))
2149               || (! (hilo_interlocks
2150                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2151                   && (prev_pinfo
2152                       & (INSN_READ_LO
2153                          | INSN_READ_HI)))
2154               || (! mips_opts.mips16
2155                   && ! gpr_interlocks
2156                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2157               || (! mips_opts.mips16
2158                   && mips_opts.isa == ISA_MIPS1
2159                   /* Itbl support may require additional care here.  */
2160                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2161               /* We can not swap with a branch instruction.  */
2162               || (prev_pinfo
2163                   & (INSN_UNCOND_BRANCH_DELAY
2164                      | INSN_COND_BRANCH_DELAY
2165                      | INSN_COND_BRANCH_LIKELY))
2166               /* We do not swap with a trap instruction, since it
2167                  complicates trap handlers to have the trap
2168                  instruction be in a delay slot.  */
2169               || (prev_pinfo & INSN_TRAP)
2170               /* If the branch reads a register that the previous
2171                  instruction sets, we can not swap.  */
2172               || (! mips_opts.mips16
2173                   && (prev_pinfo & INSN_WRITE_GPR_T)
2174                   && insn_uses_reg (ip,
2175                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2176                                      & OP_MASK_RT),
2177                                     MIPS_GR_REG))
2178               || (! mips_opts.mips16
2179                   && (prev_pinfo & INSN_WRITE_GPR_D)
2180                   && insn_uses_reg (ip,
2181                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2182                                      & OP_MASK_RD),
2183                                     MIPS_GR_REG))
2184               || (mips_opts.mips16
2185                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2186                        && insn_uses_reg (ip,
2187                                          ((prev_insn.insn_opcode
2188                                            >> MIPS16OP_SH_RX)
2189                                           & MIPS16OP_MASK_RX),
2190                                          MIPS16_REG))
2191                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2192                           && insn_uses_reg (ip,
2193                                             ((prev_insn.insn_opcode
2194                                               >> MIPS16OP_SH_RY)
2195                                              & MIPS16OP_MASK_RY),
2196                                             MIPS16_REG))
2197                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2198                           && insn_uses_reg (ip,
2199                                             ((prev_insn.insn_opcode
2200                                               >> MIPS16OP_SH_RZ)
2201                                              & MIPS16OP_MASK_RZ),
2202                                             MIPS16_REG))
2203                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2204                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2205                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2206                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2207                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2208                           && insn_uses_reg (ip,
2209                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2210                                                                      insn_opcode),
2211                                             MIPS_GR_REG))))
2212               /* If the branch writes a register that the previous
2213                  instruction sets, we can not swap (we know that
2214                  branches write only to RD or to $31).  */
2215               || (! mips_opts.mips16
2216                   && (prev_pinfo & INSN_WRITE_GPR_T)
2217                   && (((pinfo & INSN_WRITE_GPR_D)
2218                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2219                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2220                       || ((pinfo & INSN_WRITE_GPR_31)
2221                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2222                                & OP_MASK_RT)
2223                               == RA))))
2224               || (! mips_opts.mips16
2225                   && (prev_pinfo & INSN_WRITE_GPR_D)
2226                   && (((pinfo & INSN_WRITE_GPR_D)
2227                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2228                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2229                       || ((pinfo & INSN_WRITE_GPR_31)
2230                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2231                                & OP_MASK_RD)
2232                               == RA))))
2233               || (mips_opts.mips16
2234                   && (pinfo & MIPS16_INSN_WRITE_31)
2235                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2236                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2237                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2238                               == RA))))
2239               /* If the branch writes a register that the previous
2240                  instruction reads, we can not swap (we know that
2241                  branches only write to RD or to $31).  */
2242               || (! mips_opts.mips16
2243                   && (pinfo & INSN_WRITE_GPR_D)
2244                   && insn_uses_reg (&prev_insn,
2245                                     ((ip->insn_opcode >> OP_SH_RD)
2246                                      & OP_MASK_RD),
2247                                     MIPS_GR_REG))
2248               || (! mips_opts.mips16
2249                   && (pinfo & INSN_WRITE_GPR_31)
2250                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2251               || (mips_opts.mips16
2252                   && (pinfo & MIPS16_INSN_WRITE_31)
2253                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2254               /* If we are generating embedded PIC code, the branch
2255                  might be expanded into a sequence which uses $at, so
2256                  we can't swap with an instruction which reads it.  */
2257               || (mips_pic == EMBEDDED_PIC
2258                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2259               /* If the previous previous instruction has a load
2260                  delay, and sets a register that the branch reads, we
2261                  can not swap.  */
2262               || (! mips_opts.mips16
2263                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2264               /* Itbl support may require additional care here.  */
2265                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2266                       || (! gpr_interlocks
2267                           && (prev_prev_insn.insn_mo->pinfo
2268                               & INSN_LOAD_MEMORY_DELAY)))
2269                   && insn_uses_reg (ip,
2270                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2271                                      & OP_MASK_RT),
2272                                     MIPS_GR_REG))
2273               /* If one instruction sets a condition code and the
2274                  other one uses a condition code, we can not swap.  */
2275               || ((pinfo & INSN_READ_COND_CODE)
2276                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2277               || ((pinfo & INSN_WRITE_COND_CODE)
2278                   && (prev_pinfo & INSN_READ_COND_CODE))
2279               /* If the previous instruction uses the PC, we can not
2280                  swap.  */
2281               || (mips_opts.mips16
2282                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2283               /* If the previous instruction was extended, we can not
2284                  swap.  */
2285               || (mips_opts.mips16 && prev_insn_extended)
2286               /* If the previous instruction had a fixup in mips16
2287                  mode, we can not swap.  This normally means that the
2288                  previous instruction was a 4 byte branch anyhow.  */
2289               || (mips_opts.mips16 && prev_insn_fixp[0])
2290               /* If the previous instruction is a sync, sync.l, or
2291                  sync.p, we can not swap.  */
2292               || (prev_pinfo & INSN_SYNC))
2293             {
2294               /* We could do even better for unconditional branches to
2295                  portions of this object file; we could pick up the
2296                  instruction at the destination, put it in the delay
2297                  slot, and bump the destination address.  */
2298               emit_nop ();
2299               /* Update the previous insn information.  */
2300               prev_prev_insn = *ip;
2301               prev_insn.insn_mo = &dummy_opcode;
2302             }
2303           else
2304             {
2305               /* It looks like we can actually do the swap.  */
2306               if (! mips_opts.mips16)
2307                 {
2308                   char *prev_f;
2309                   char temp[4];
2310
2311                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2312                   memcpy (temp, prev_f, 4);
2313                   memcpy (prev_f, f, 4);
2314                   memcpy (f, temp, 4);
2315                   if (prev_insn_fixp[0])
2316                     {
2317                       prev_insn_fixp[0]->fx_frag = frag_now;
2318                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2319                     }
2320                   if (prev_insn_fixp[1])
2321                     {
2322                       prev_insn_fixp[1]->fx_frag = frag_now;
2323                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2324                     }
2325                   if (prev_insn_fixp[2])
2326                     {
2327                       prev_insn_fixp[2]->fx_frag = frag_now;
2328                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2329                     }
2330                   if (fixp[0])
2331                     {
2332                       fixp[0]->fx_frag = prev_insn_frag;
2333                       fixp[0]->fx_where = prev_insn_where;
2334                     }
2335                   if (fixp[1])
2336                     {
2337                       fixp[1]->fx_frag = prev_insn_frag;
2338                       fixp[1]->fx_where = prev_insn_where;
2339                     }
2340                   if (fixp[2])
2341                     {
2342                       fixp[2]->fx_frag = prev_insn_frag;
2343                       fixp[2]->fx_where = prev_insn_where;
2344                     }
2345                 }
2346               else
2347                 {
2348                   char *prev_f;
2349                   char temp[2];
2350
2351                   assert (prev_insn_fixp[0] == NULL);
2352                   assert (prev_insn_fixp[1] == NULL);
2353                   assert (prev_insn_fixp[2] == NULL);
2354                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2355                   memcpy (temp, prev_f, 2);
2356                   memcpy (prev_f, f, 2);
2357                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2358                     {
2359                       assert (*reloc_type == BFD_RELOC_UNUSED);
2360                       memcpy (f, temp, 2);
2361                     }
2362                   else
2363                     {
2364                       memcpy (f, f + 2, 2);
2365                       memcpy (f + 2, temp, 2);
2366                     }
2367                   if (fixp[0])
2368                     {
2369                       fixp[0]->fx_frag = prev_insn_frag;
2370                       fixp[0]->fx_where = prev_insn_where;
2371                     }
2372                   if (fixp[1])
2373                     {
2374                       fixp[1]->fx_frag = prev_insn_frag;
2375                       fixp[1]->fx_where = prev_insn_where;
2376                     }
2377                   if (fixp[2])
2378                     {
2379                       fixp[2]->fx_frag = prev_insn_frag;
2380                       fixp[2]->fx_where = prev_insn_where;
2381                     }
2382                 }
2383
2384               /* Update the previous insn information; leave prev_insn
2385                  unchanged.  */
2386               prev_prev_insn = *ip;
2387             }
2388           prev_insn_is_delay_slot = 1;
2389
2390           /* If that was an unconditional branch, forget the previous
2391              insn information.  */
2392           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2393             {
2394               prev_prev_insn.insn_mo = &dummy_opcode;
2395               prev_insn.insn_mo = &dummy_opcode;
2396             }
2397
2398           prev_insn_fixp[0] = NULL;
2399           prev_insn_fixp[1] = NULL;
2400           prev_insn_fixp[2] = NULL;
2401           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2402           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2403           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2404           prev_insn_extended = 0;
2405         }
2406       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2407         {
2408           /* We don't yet optimize a branch likely.  What we should do
2409              is look at the target, copy the instruction found there
2410              into the delay slot, and increment the branch to jump to
2411              the next instruction.  */
2412           emit_nop ();
2413           /* Update the previous insn information.  */
2414           prev_prev_insn = *ip;
2415           prev_insn.insn_mo = &dummy_opcode;
2416           prev_insn_fixp[0] = NULL;
2417           prev_insn_fixp[1] = NULL;
2418           prev_insn_fixp[2] = NULL;
2419           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2420           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2421           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2422           prev_insn_extended = 0;
2423         }
2424       else
2425         {
2426           /* Update the previous insn information.  */
2427           if (nops > 0)
2428             prev_prev_insn.insn_mo = &dummy_opcode;
2429           else
2430             prev_prev_insn = prev_insn;
2431           prev_insn = *ip;
2432
2433           /* Any time we see a branch, we always fill the delay slot
2434              immediately; since this insn is not a branch, we know it
2435              is not in a delay slot.  */
2436           prev_insn_is_delay_slot = 0;
2437
2438           prev_insn_fixp[0] = fixp[0];
2439           prev_insn_fixp[1] = fixp[1];
2440           prev_insn_fixp[2] = fixp[2];
2441           prev_insn_reloc_type[0] = reloc_type[0];
2442           prev_insn_reloc_type[1] = reloc_type[1];
2443           prev_insn_reloc_type[2] = reloc_type[2];
2444           if (mips_opts.mips16)
2445             prev_insn_extended = (ip->use_extend
2446                                   || *reloc_type > BFD_RELOC_UNUSED);
2447         }
2448
2449       prev_prev_insn_unreordered = prev_insn_unreordered;
2450       prev_insn_unreordered = 0;
2451       prev_insn_frag = frag_now;
2452       prev_insn_where = f - frag_now->fr_literal;
2453       prev_insn_valid = 1;
2454     }
2455   else if (place == NULL)
2456     {
2457       /* We need to record a bit of information even when we are not
2458          reordering, in order to determine the base address for mips16
2459          PC relative relocs.  */
2460       prev_prev_insn = prev_insn;
2461       prev_insn = *ip;
2462       prev_insn_reloc_type[0] = reloc_type[0];
2463       prev_insn_reloc_type[1] = reloc_type[1];
2464       prev_insn_reloc_type[2] = reloc_type[2];
2465       prev_prev_insn_unreordered = prev_insn_unreordered;
2466       prev_insn_unreordered = 1;
2467     }
2468
2469   /* We just output an insn, so the next one doesn't have a label.  */
2470   mips_clear_insn_labels ();
2471
2472   /* We must ensure that a fixup associated with an unmatched %hi
2473      reloc does not become a variant frag.  Otherwise, the
2474      rearrangement of %hi relocs in frob_file may confuse
2475      tc_gen_reloc.  */
2476   if (unmatched_hi)
2477     {
2478       frag_wane (frag_now);
2479       frag_new (0);
2480     }
2481 }
2482
2483 /* This function forgets that there was any previous instruction or
2484    label.  If PRESERVE is non-zero, it remembers enough information to
2485    know whether nops are needed before a noreorder section.  */
2486
2487 static void
2488 mips_no_prev_insn (preserve)
2489      int preserve;
2490 {
2491   if (! preserve)
2492     {
2493       prev_insn.insn_mo = &dummy_opcode;
2494       prev_prev_insn.insn_mo = &dummy_opcode;
2495       prev_nop_frag = NULL;
2496       prev_nop_frag_holds = 0;
2497       prev_nop_frag_required = 0;
2498       prev_nop_frag_since = 0;
2499     }
2500   prev_insn_valid = 0;
2501   prev_insn_is_delay_slot = 0;
2502   prev_insn_unreordered = 0;
2503   prev_insn_extended = 0;
2504   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2505   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2506   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2507   prev_prev_insn_unreordered = 0;
2508   mips_clear_insn_labels ();
2509 }
2510
2511 /* This function must be called whenever we turn on noreorder or emit
2512    something other than instructions.  It inserts any NOPS which might
2513    be needed by the previous instruction, and clears the information
2514    kept for the previous instructions.  The INSNS parameter is true if
2515    instructions are to follow.  */
2516
2517 static void
2518 mips_emit_delays (insns)
2519      boolean insns;
2520 {
2521   if (! mips_opts.noreorder)
2522     {
2523       int nops;
2524
2525       nops = 0;
2526       if ((! mips_opts.mips16
2527            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2528            && (! cop_interlocks
2529                && (prev_insn.insn_mo->pinfo
2530                    & (INSN_LOAD_COPROC_DELAY
2531                       | INSN_COPROC_MOVE_DELAY
2532                       | INSN_WRITE_COND_CODE))))
2533           || (! hilo_interlocks
2534               && (prev_insn.insn_mo->pinfo
2535                   & (INSN_READ_LO
2536                      | INSN_READ_HI)))
2537           || (! mips_opts.mips16
2538               && ! gpr_interlocks
2539               && (prev_insn.insn_mo->pinfo
2540                   & INSN_LOAD_MEMORY_DELAY))
2541           || (! mips_opts.mips16
2542               && mips_opts.isa == ISA_MIPS1
2543               && (prev_insn.insn_mo->pinfo
2544                   & INSN_COPROC_MEMORY_DELAY)))
2545         {
2546           /* Itbl support may require additional care here.  */
2547           ++nops;
2548           if ((! mips_opts.mips16
2549                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2550                && (! cop_interlocks
2551                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2552               || (! hilo_interlocks
2553                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2554                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2555             ++nops;
2556
2557           if (prev_insn_unreordered)
2558             nops = 0;
2559         }
2560       else if ((! mips_opts.mips16
2561                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2562                 && (! cop_interlocks
2563                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2564                || (! hilo_interlocks
2565                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2566                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2567         {
2568           /* Itbl support may require additional care here.  */
2569           if (! prev_prev_insn_unreordered)
2570             ++nops;
2571         }
2572
2573       if (nops > 0)
2574         {
2575           struct insn_label_list *l;
2576
2577           if (insns)
2578             {
2579               /* Record the frag which holds the nop instructions, so
2580                  that we can remove them if we don't need them.  */
2581               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2582               prev_nop_frag = frag_now;
2583               prev_nop_frag_holds = nops;
2584               prev_nop_frag_required = 0;
2585               prev_nop_frag_since = 0;
2586             }
2587
2588           for (; nops > 0; --nops)
2589             emit_nop ();
2590
2591           if (insns)
2592             {
2593               /* Move on to a new frag, so that it is safe to simply
2594                  decrease the size of prev_nop_frag.  */
2595               frag_wane (frag_now);
2596               frag_new (0);
2597             }
2598
2599           for (l = insn_labels; l != NULL; l = l->next)
2600             {
2601               valueT val;
2602
2603               assert (S_GET_SEGMENT (l->label) == now_seg);
2604               symbol_set_frag (l->label, frag_now);
2605               val = (valueT) frag_now_fix ();
2606               /* mips16 text labels are stored as odd.  */
2607               if (mips_opts.mips16)
2608                 ++val;
2609               S_SET_VALUE (l->label, val);
2610             }
2611         }
2612     }
2613
2614   /* Mark instruction labels in mips16 mode.  */
2615   if (insns)
2616     mips16_mark_labels ();
2617
2618   mips_no_prev_insn (insns);
2619 }
2620
2621 /* Build an instruction created by a macro expansion.  This is passed
2622    a pointer to the count of instructions created so far, an
2623    expression, the name of the instruction to build, an operand format
2624    string, and corresponding arguments.  */
2625
2626 #ifdef USE_STDARG
2627 static void
2628 macro_build (char *place,
2629              int *counter,
2630              expressionS * ep,
2631              const char *name,
2632              const char *fmt,
2633              ...)
2634 #else
2635 static void
2636 macro_build (place, counter, ep, name, fmt, va_alist)
2637      char *place;
2638      int *counter;
2639      expressionS *ep;
2640      const char *name;
2641      const char *fmt;
2642      va_dcl
2643 #endif
2644 {
2645   struct mips_cl_insn insn;
2646   bfd_reloc_code_real_type r[3];
2647   va_list args;
2648
2649 #ifdef USE_STDARG
2650   va_start (args, fmt);
2651 #else
2652   va_start (args);
2653 #endif
2654
2655   /*
2656    * If the macro is about to expand into a second instruction,
2657    * print a warning if needed. We need to pass ip as a parameter
2658    * to generate a better warning message here...
2659    */
2660   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2661     as_warn (_("Macro instruction expanded into multiple instructions"));
2662
2663   /*
2664    * If the macro is about to expand into a second instruction,
2665    * and it is in a delay slot, print a warning.
2666    */
2667   if (place == NULL
2668       && *counter == 1
2669       && mips_opts.noreorder
2670       && (prev_prev_insn.insn_mo->pinfo
2671           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2672              | INSN_COND_BRANCH_LIKELY)) != 0)
2673     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2674
2675   if (place == NULL)
2676     ++*counter;         /* bump instruction counter */
2677
2678   if (mips_opts.mips16)
2679     {
2680       mips16_macro_build (place, counter, ep, name, fmt, args);
2681       va_end (args);
2682       return;
2683     }
2684
2685   r[0] = BFD_RELOC_UNUSED;
2686   r[1] = BFD_RELOC_UNUSED;
2687   r[2] = BFD_RELOC_UNUSED;
2688   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2689   assert (insn.insn_mo);
2690   assert (strcmp (name, insn.insn_mo->name) == 0);
2691
2692   /* Search until we get a match for NAME.  */
2693   while (1)
2694     {
2695       /* It is assumed here that macros will never generate 
2696          MDMX or MIPS-3D instructions.  */
2697       if (strcmp (fmt, insn.insn_mo->args) == 0
2698           && insn.insn_mo->pinfo != INSN_MACRO
2699           && OPCODE_IS_MEMBER (insn.insn_mo,
2700                                (mips_opts.isa
2701                                 | (mips_opts.mips16 ? INSN_MIPS16 : 0)),
2702                                mips_arch)
2703           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2704         break;
2705
2706       ++insn.insn_mo;
2707       assert (insn.insn_mo->name);
2708       assert (strcmp (name, insn.insn_mo->name) == 0);
2709     }
2710
2711   insn.insn_opcode = insn.insn_mo->match;
2712   for (;;)
2713     {
2714       switch (*fmt++)
2715         {
2716         case '\0':
2717           break;
2718
2719         case ',':
2720         case '(':
2721         case ')':
2722           continue;
2723
2724         case 't':
2725         case 'w':
2726         case 'E':
2727           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2728           continue;
2729
2730         case 'c':
2731           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2732           continue;
2733
2734         case 'T':
2735         case 'W':
2736           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2737           continue;
2738
2739         case 'd':
2740         case 'G':
2741           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2742           continue;
2743
2744         case 'U':
2745           {
2746             int tmp = va_arg (args, int);
2747
2748             insn.insn_opcode |= tmp << OP_SH_RT;
2749             insn.insn_opcode |= tmp << OP_SH_RD;
2750             continue;
2751           }
2752
2753         case 'V':
2754         case 'S':
2755           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2756           continue;
2757
2758         case 'z':
2759           continue;
2760
2761         case '<':
2762           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2763           continue;
2764
2765         case 'D':
2766           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2767           continue;
2768
2769         case 'B':
2770           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2771           continue;
2772
2773         case 'J':
2774           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2775           continue;
2776
2777         case 'q':
2778           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2779           continue;
2780
2781         case 'b':
2782         case 's':
2783         case 'r':
2784         case 'v':
2785           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2786           continue;
2787
2788         case 'i':
2789         case 'j':
2790         case 'o':
2791           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2792           assert (*r == BFD_RELOC_GPREL16
2793                   || *r == BFD_RELOC_MIPS_LITERAL
2794                   || *r == BFD_RELOC_MIPS_HIGHER
2795                   || *r == BFD_RELOC_HI16_S
2796                   || *r == BFD_RELOC_LO16
2797                   || *r == BFD_RELOC_MIPS_GOT16
2798                   || *r == BFD_RELOC_MIPS_CALL16
2799                   || *r == BFD_RELOC_MIPS_GOT_DISP
2800                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2801                   || *r == BFD_RELOC_MIPS_GOT_OFST
2802                   || *r == BFD_RELOC_MIPS_GOT_LO16
2803                   || *r == BFD_RELOC_MIPS_CALL_LO16
2804                   || (ep->X_op == O_subtract
2805                       && *r == BFD_RELOC_PCREL_LO16));
2806           continue;
2807
2808         case 'u':
2809           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2810           assert (ep != NULL
2811                   && (ep->X_op == O_constant
2812                       || (ep->X_op == O_symbol
2813                           && (*r == BFD_RELOC_MIPS_HIGHEST
2814                               || *r == BFD_RELOC_HI16_S
2815                               || *r == BFD_RELOC_HI16
2816                               || *r == BFD_RELOC_GPREL16
2817                               || *r == BFD_RELOC_MIPS_GOT_HI16
2818                               || *r == BFD_RELOC_MIPS_CALL_HI16))
2819                       || (ep->X_op == O_subtract
2820                           && *r == BFD_RELOC_PCREL_HI16_S)));
2821           continue;
2822
2823         case 'p':
2824           assert (ep != NULL);
2825           /*
2826            * This allows macro() to pass an immediate expression for
2827            * creating short branches without creating a symbol.
2828            * Note that the expression still might come from the assembly
2829            * input, in which case the value is not checked for range nor
2830            * is a relocation entry generated (yuck).
2831            */
2832           if (ep->X_op == O_constant)
2833             {
2834               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2835               ep = NULL;
2836             }
2837           else
2838             if (mips_pic == EMBEDDED_PIC)
2839               *r = BFD_RELOC_16_PCREL_S2;
2840             else
2841               *r = BFD_RELOC_16_PCREL;
2842           continue;
2843
2844         case 'a':
2845           assert (ep != NULL);
2846           *r = BFD_RELOC_MIPS_JMP;
2847           continue;
2848
2849         case 'C':
2850           insn.insn_opcode |= va_arg (args, unsigned long);
2851           continue;
2852
2853         default:
2854           internalError ();
2855         }
2856       break;
2857     }
2858   va_end (args);
2859   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2860
2861   append_insn (place, &insn, ep, r, false);
2862 }
2863
2864 static void
2865 mips16_macro_build (place, counter, ep, name, fmt, args)
2866      char *place;
2867      int *counter ATTRIBUTE_UNUSED;
2868      expressionS *ep;
2869      const char *name;
2870      const char *fmt;
2871      va_list args;
2872 {
2873   struct mips_cl_insn insn;
2874   bfd_reloc_code_real_type r[3]
2875     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2876
2877   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2878   assert (insn.insn_mo);
2879   assert (strcmp (name, insn.insn_mo->name) == 0);
2880
2881   while (strcmp (fmt, insn.insn_mo->args) != 0
2882          || insn.insn_mo->pinfo == INSN_MACRO)
2883     {
2884       ++insn.insn_mo;
2885       assert (insn.insn_mo->name);
2886       assert (strcmp (name, insn.insn_mo->name) == 0);
2887     }
2888
2889   insn.insn_opcode = insn.insn_mo->match;
2890   insn.use_extend = false;
2891
2892   for (;;)
2893     {
2894       int c;
2895
2896       c = *fmt++;
2897       switch (c)
2898         {
2899         case '\0':
2900           break;
2901
2902         case ',':
2903         case '(':
2904         case ')':
2905           continue;
2906
2907         case 'y':
2908         case 'w':
2909           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2910           continue;
2911
2912         case 'x':
2913         case 'v':
2914           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2915           continue;
2916
2917         case 'z':
2918           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2919           continue;
2920
2921         case 'Z':
2922           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2923           continue;
2924
2925         case '0':
2926         case 'S':
2927         case 'P':
2928         case 'R':
2929           continue;
2930
2931         case 'X':
2932           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2933           continue;
2934
2935         case 'Y':
2936           {
2937             int regno;
2938
2939             regno = va_arg (args, int);
2940             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2941             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2942           }
2943           continue;
2944
2945         case '<':
2946         case '>':
2947         case '4':
2948         case '5':
2949         case 'H':
2950         case 'W':
2951         case 'D':
2952         case 'j':
2953         case '8':
2954         case 'V':
2955         case 'C':
2956         case 'U':
2957         case 'k':
2958         case 'K':
2959         case 'p':
2960         case 'q':
2961           {
2962             assert (ep != NULL);
2963
2964             if (ep->X_op != O_constant)
2965               *r = (int) BFD_RELOC_UNUSED + c;
2966             else
2967               {
2968                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2969                               false, &insn.insn_opcode, &insn.use_extend,
2970                               &insn.extend);
2971                 ep = NULL;
2972                 *r = BFD_RELOC_UNUSED;
2973               }
2974           }
2975           continue;
2976
2977         case '6':
2978           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2979           continue;
2980         }
2981
2982       break;
2983     }
2984
2985   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2986
2987   append_insn (place, &insn, ep, r, false);
2988 }
2989
2990 /*
2991  * Generate a "jalr" instruction with a relocation hint to the called
2992  * function.  This occurs in NewABI PIC code.
2993  */
2994 static void
2995 macro_build_jalr (icnt, ep)
2996      int icnt;
2997      expressionS *ep;
2998 {
2999   char *f;
3000   
3001   if (HAVE_NEWABI)
3002     {
3003       frag_grow (4);
3004       f = frag_more (0);
3005     }
3006   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3007                RA, PIC_CALL_REG);
3008   if (HAVE_NEWABI)
3009     fix_new_exp (frag_now, f - frag_now->fr_literal,
3010                  0, ep, false, BFD_RELOC_MIPS_JALR);
3011 }
3012
3013 /*
3014  * Generate a "lui" instruction.
3015  */
3016 static void
3017 macro_build_lui (place, counter, ep, regnum)
3018      char *place;
3019      int *counter;
3020      expressionS *ep;
3021      int regnum;
3022 {
3023   expressionS high_expr;
3024   struct mips_cl_insn insn;
3025   bfd_reloc_code_real_type r[3]
3026     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3027   const char *name = "lui";
3028   const char *fmt = "t,u";
3029
3030   assert (! mips_opts.mips16);
3031
3032   if (place == NULL)
3033     high_expr = *ep;
3034   else
3035     {
3036       high_expr.X_op = O_constant;
3037       high_expr.X_add_number = ep->X_add_number;
3038     }
3039
3040   if (high_expr.X_op == O_constant)
3041     {
3042       /* we can compute the instruction now without a relocation entry */
3043       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3044                                 >> 16) & 0xffff;
3045       *r = BFD_RELOC_UNUSED;
3046     }
3047   else if (! HAVE_NEWABI)
3048     {
3049       assert (ep->X_op == O_symbol);
3050       /* _gp_disp is a special case, used from s_cpload.  */
3051       assert (mips_pic == NO_PIC
3052               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3053       *r = BFD_RELOC_HI16_S;
3054     }
3055
3056   /*
3057    * If the macro is about to expand into a second instruction,
3058    * print a warning if needed. We need to pass ip as a parameter
3059    * to generate a better warning message here...
3060    */
3061   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3062     as_warn (_("Macro instruction expanded into multiple instructions"));
3063
3064   if (place == NULL)
3065     ++*counter;         /* bump instruction counter */
3066
3067   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3068   assert (insn.insn_mo);
3069   assert (strcmp (name, insn.insn_mo->name) == 0);
3070   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3071
3072   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3073   if (*r == BFD_RELOC_UNUSED)
3074     {
3075       insn.insn_opcode |= high_expr.X_add_number;
3076       append_insn (place, &insn, NULL, r, false);
3077     }
3078   else
3079     append_insn (place, &insn, &high_expr, r, false);
3080 }
3081
3082 /*                      set_at()
3083  * Generates code to set the $at register to true (one)
3084  * if reg is less than the immediate expression.
3085  */
3086 static void
3087 set_at (counter, reg, unsignedp)
3088      int *counter;
3089      int reg;
3090      int unsignedp;
3091 {
3092   if (imm_expr.X_op == O_constant
3093       && imm_expr.X_add_number >= -0x8000
3094       && imm_expr.X_add_number < 0x8000)
3095     macro_build ((char *) NULL, counter, &imm_expr,
3096                  unsignedp ? "sltiu" : "slti",
3097                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3098   else
3099     {
3100       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3101       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3102                    unsignedp ? "sltu" : "slt",
3103                    "d,v,t", AT, reg, AT);
3104     }
3105 }
3106
3107 /* Warn if an expression is not a constant.  */
3108
3109 static void
3110 check_absolute_expr (ip, ex)
3111      struct mips_cl_insn *ip;
3112      expressionS *ex;
3113 {
3114   if (ex->X_op == O_big)
3115     as_bad (_("unsupported large constant"));
3116   else if (ex->X_op != O_constant)
3117     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3118 }
3119
3120 /* Count the leading zeroes by performing a binary chop. This is a
3121    bulky bit of source, but performance is a LOT better for the
3122    majority of values than a simple loop to count the bits:
3123        for (lcnt = 0; (lcnt < 32); lcnt++)
3124          if ((v) & (1 << (31 - lcnt)))
3125            break;
3126   However it is not code size friendly, and the gain will drop a bit
3127   on certain cached systems.
3128 */
3129 #define COUNT_TOP_ZEROES(v)             \
3130   (((v) & ~0xffff) == 0                 \
3131    ? ((v) & ~0xff) == 0                 \
3132      ? ((v) & ~0xf) == 0                \
3133        ? ((v) & ~0x3) == 0              \
3134          ? ((v) & ~0x1) == 0            \
3135            ? !(v)                       \
3136              ? 32                       \
3137              : 31                       \
3138            : 30                         \
3139          : ((v) & ~0x7) == 0            \
3140            ? 29                         \
3141            : 28                         \
3142        : ((v) & ~0x3f) == 0             \
3143          ? ((v) & ~0x1f) == 0           \
3144            ? 27                         \
3145            : 26                         \
3146          : ((v) & ~0x7f) == 0           \
3147            ? 25                         \
3148            : 24                         \
3149      : ((v) & ~0xfff) == 0              \
3150        ? ((v) & ~0x3ff) == 0            \
3151          ? ((v) & ~0x1ff) == 0          \
3152            ? 23                         \
3153            : 22                         \
3154          : ((v) & ~0x7ff) == 0          \
3155            ? 21                         \
3156            : 20                         \
3157        : ((v) & ~0x3fff) == 0           \
3158          ? ((v) & ~0x1fff) == 0         \
3159            ? 19                         \
3160            : 18                         \
3161          : ((v) & ~0x7fff) == 0         \
3162            ? 17                         \
3163            : 16                         \
3164    : ((v) & ~0xffffff) == 0             \
3165      ? ((v) & ~0xfffff) == 0            \
3166        ? ((v) & ~0x3ffff) == 0          \
3167          ? ((v) & ~0x1ffff) == 0        \
3168            ? 15                         \
3169            : 14                         \
3170          : ((v) & ~0x7ffff) == 0        \
3171            ? 13                         \
3172            : 12                         \
3173        : ((v) & ~0x3fffff) == 0         \
3174          ? ((v) & ~0x1fffff) == 0       \
3175            ? 11                         \
3176            : 10                         \
3177          : ((v) & ~0x7fffff) == 0       \
3178            ? 9                          \
3179            : 8                          \
3180      : ((v) & ~0xfffffff) == 0          \
3181        ? ((v) & ~0x3ffffff) == 0        \
3182          ? ((v) & ~0x1ffffff) == 0      \
3183            ? 7                          \
3184            : 6                          \
3185          : ((v) & ~0x7ffffff) == 0      \
3186            ? 5                          \
3187            : 4                          \
3188        : ((v) & ~0x3fffffff) == 0       \
3189          ? ((v) & ~0x1fffffff) == 0     \
3190            ? 3                          \
3191            : 2                          \
3192          : ((v) & ~0x7fffffff) == 0     \
3193            ? 1                          \
3194            : 0)
3195
3196 /* Is the given value a sign-extended 32-bit value?  */
3197 #define IS_SEXT_32BIT_NUM(x)                                            \
3198   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
3199    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3200
3201 /*                      load_register()
3202  *  This routine generates the least number of instructions neccessary to load
3203  *  an absolute expression value into a register.
3204  */
3205 static void
3206 load_register (counter, reg, ep, dbl)
3207      int *counter;
3208      int reg;
3209      expressionS *ep;
3210      int dbl;
3211 {
3212   int freg;
3213   expressionS hi32, lo32;
3214
3215   if (ep->X_op != O_big)
3216     {
3217       assert (ep->X_op == O_constant);
3218       if (ep->X_add_number < 0x8000
3219           && (ep->X_add_number >= 0
3220               || (ep->X_add_number >= -0x8000
3221                   && (! dbl
3222                       || ! ep->X_unsigned
3223                       || sizeof (ep->X_add_number) > 4))))
3224         {
3225           /* We can handle 16 bit signed values with an addiu to
3226              $zero.  No need to ever use daddiu here, since $zero and
3227              the result are always correct in 32 bit mode.  */
3228           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3229                        (int) BFD_RELOC_LO16);
3230           return;
3231         }
3232       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3233         {
3234           /* We can handle 16 bit unsigned values with an ori to
3235              $zero.  */
3236           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3237                        (int) BFD_RELOC_LO16);
3238           return;
3239         }
3240       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3241                 && (! dbl
3242                     || ! ep->X_unsigned
3243                     || sizeof (ep->X_add_number) > 4
3244                     || (ep->X_add_number & 0x80000000) == 0))
3245                || ((HAVE_32BIT_GPRS || ! dbl)
3246                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3247                || (HAVE_32BIT_GPRS
3248                    && ! dbl
3249                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3250                        == ~ (offsetT) 0xffffffff)))
3251         {
3252           /* 32 bit values require an lui.  */
3253           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3254                        (int) BFD_RELOC_HI16);
3255           if ((ep->X_add_number & 0xffff) != 0)
3256             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3257                          (int) BFD_RELOC_LO16);
3258           return;
3259         }
3260     }
3261
3262   /* The value is larger than 32 bits.  */
3263
3264   if (HAVE_32BIT_GPRS)
3265     {
3266       as_bad (_("Number (0x%lx) larger than 32 bits"),
3267               (unsigned long) ep->X_add_number);
3268       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3269                    (int) BFD_RELOC_LO16);
3270       return;
3271     }
3272
3273   if (ep->X_op != O_big)
3274     {
3275       hi32 = *ep;
3276       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3277       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3278       hi32.X_add_number &= 0xffffffff;
3279       lo32 = *ep;
3280       lo32.X_add_number &= 0xffffffff;
3281     }
3282   else
3283     {
3284       assert (ep->X_add_number > 2);
3285       if (ep->X_add_number == 3)
3286         generic_bignum[3] = 0;
3287       else if (ep->X_add_number > 4)
3288         as_bad (_("Number larger than 64 bits"));
3289       lo32.X_op = O_constant;
3290       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3291       hi32.X_op = O_constant;
3292       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3293     }
3294
3295   if (hi32.X_add_number == 0)
3296     freg = 0;
3297   else
3298     {
3299       int shift, bit;
3300       unsigned long hi, lo;
3301
3302       if (hi32.X_add_number == (offsetT) 0xffffffff)
3303         {
3304           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3305             {
3306               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3307                            reg, 0, (int) BFD_RELOC_LO16);
3308               return;
3309             }
3310           if (lo32.X_add_number & 0x80000000)
3311             {
3312               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3313                            (int) BFD_RELOC_HI16);
3314               if (lo32.X_add_number & 0xffff)
3315                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3316                              reg, reg, (int) BFD_RELOC_LO16);
3317               return;
3318             }
3319         }
3320
3321       /* Check for 16bit shifted constant.  We know that hi32 is
3322          non-zero, so start the mask on the first bit of the hi32
3323          value.  */
3324       shift = 17;
3325       do
3326         {
3327           unsigned long himask, lomask;
3328
3329           if (shift < 32)
3330             {
3331               himask = 0xffff >> (32 - shift);
3332               lomask = (0xffff << shift) & 0xffffffff;
3333             }
3334           else
3335             {
3336               himask = 0xffff << (shift - 32);
3337               lomask = 0;
3338             }
3339           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3340               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3341             {
3342               expressionS tmp;
3343
3344               tmp.X_op = O_constant;
3345               if (shift < 32)
3346                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3347                                     | (lo32.X_add_number >> shift));
3348               else
3349                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3350               macro_build ((char *) NULL, counter, &tmp,
3351                            "ori", "t,r,i", reg, 0,
3352                            (int) BFD_RELOC_LO16);
3353               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3354                            (shift >= 32) ? "dsll32" : "dsll",
3355                            "d,w,<", reg, reg,
3356                            (shift >= 32) ? shift - 32 : shift);
3357               return;
3358             }
3359           ++shift;
3360         }
3361       while (shift <= (64 - 16));
3362
3363       /* Find the bit number of the lowest one bit, and store the
3364          shifted value in hi/lo.  */
3365       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3366       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3367       if (lo != 0)
3368         {
3369           bit = 0;
3370           while ((lo & 1) == 0)
3371             {
3372               lo >>= 1;
3373               ++bit;
3374             }
3375           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3376           hi >>= bit;
3377         }
3378       else
3379         {
3380           bit = 32;
3381           while ((hi & 1) == 0)
3382             {
3383               hi >>= 1;
3384               ++bit;
3385             }
3386           lo = hi;
3387           hi = 0;
3388         }
3389
3390       /* Optimize if the shifted value is a (power of 2) - 1.  */
3391       if ((hi == 0 && ((lo + 1) & lo) == 0)
3392           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3393         {
3394           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3395           if (shift != 0)
3396             {
3397               expressionS tmp;
3398
3399               /* This instruction will set the register to be all
3400                  ones.  */
3401               tmp.X_op = O_constant;
3402               tmp.X_add_number = (offsetT) -1;
3403               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3404                            reg, 0, (int) BFD_RELOC_LO16);
3405               if (bit != 0)
3406                 {
3407                   bit += shift;
3408                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3409                                (bit >= 32) ? "dsll32" : "dsll",
3410                                "d,w,<", reg, reg,
3411                                (bit >= 32) ? bit - 32 : bit);
3412                 }
3413               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3414                            (shift >= 32) ? "dsrl32" : "dsrl",
3415                            "d,w,<", reg, reg,
3416                            (shift >= 32) ? shift - 32 : shift);
3417               return;
3418             }
3419         }
3420
3421       /* Sign extend hi32 before calling load_register, because we can
3422          generally get better code when we load a sign extended value.  */
3423       if ((hi32.X_add_number & 0x80000000) != 0)
3424         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3425       load_register (counter, reg, &hi32, 0);
3426       freg = reg;
3427     }
3428   if ((lo32.X_add_number & 0xffff0000) == 0)
3429     {
3430       if (freg != 0)
3431         {
3432           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3433                        "dsll32", "d,w,<", reg, freg, 0);
3434           freg = reg;
3435         }
3436     }
3437   else
3438     {
3439       expressionS mid16;
3440
3441       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3442         {
3443           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3444                        (int) BFD_RELOC_HI16);
3445           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3446                        "dsrl32", "d,w,<", reg, reg, 0);
3447           return;
3448         }
3449
3450       if (freg != 0)
3451         {
3452           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3453                        "d,w,<", reg, freg, 16);
3454           freg = reg;
3455         }
3456       mid16 = lo32;
3457       mid16.X_add_number >>= 16;
3458       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3459                    freg, (int) BFD_RELOC_LO16);
3460       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3461                    "d,w,<", reg, reg, 16);
3462       freg = reg;
3463     }
3464   if ((lo32.X_add_number & 0xffff) != 0)
3465     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3466                  (int) BFD_RELOC_LO16);
3467 }
3468
3469 /* Load an address into a register.  */
3470
3471 static void
3472 load_address (counter, reg, ep, used_at)
3473      int *counter;
3474      int reg;
3475      expressionS *ep;
3476      int *used_at;
3477 {
3478   char *p = NULL;
3479
3480   if (ep->X_op != O_constant
3481       && ep->X_op != O_symbol)
3482     {
3483       as_bad (_("expression too complex"));
3484       ep->X_op = O_constant;
3485     }
3486
3487   if (ep->X_op == O_constant)
3488     {
3489       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3490       return;
3491     }
3492
3493   if (mips_pic == NO_PIC)
3494     {
3495       /* If this is a reference to a GP relative symbol, we want
3496            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3497          Otherwise we want
3498            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3499            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3500          If we have an addend, we always use the latter form.
3501
3502          With 64bit address space and a usable $at we want
3503            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3504            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3505            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3506            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3507            dsll32       $reg,0
3508            daddu        $reg,$reg,$at
3509
3510          If $at is already in use, we use an path which is suboptimal
3511          on superscalar processors.
3512            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3513            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3514            dsll         $reg,16
3515            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3516            dsll         $reg,16
3517            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3518        */
3519       if (HAVE_64BIT_ADDRESSES)
3520         {
3521           /* We don't do GP optimization for now because RELAX_ENCODE can't
3522              hold the data for such large chunks.  */
3523
3524           if (*used_at == 0 && ! mips_opts.noat)
3525             {
3526               macro_build (p, counter, ep, "lui", "t,u",
3527                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3528               macro_build (p, counter, ep, "lui", "t,u",
3529                            AT, (int) BFD_RELOC_HI16_S);
3530               macro_build (p, counter, ep, "daddiu", "t,r,j",
3531                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3532               macro_build (p, counter, ep, "daddiu", "t,r,j",
3533                            AT, AT, (int) BFD_RELOC_LO16);
3534               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3535                            "d,w,<", reg, reg, 0);
3536               macro_build (p, counter, (expressionS *) NULL, "daddu",
3537                            "d,v,t", reg, reg, AT);
3538               *used_at = 1;
3539             }
3540           else
3541             {
3542               macro_build (p, counter, ep, "lui", "t,u",
3543                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3544               macro_build (p, counter, ep, "daddiu", "t,r,j",
3545                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3546               macro_build (p, counter, (expressionS *) NULL, "dsll",
3547                            "d,w,<", reg, reg, 16);
3548               macro_build (p, counter, ep, "daddiu", "t,r,j",
3549                            reg, reg, (int) BFD_RELOC_HI16_S);
3550               macro_build (p, counter, (expressionS *) NULL, "dsll",
3551                            "d,w,<", reg, reg, 16);
3552               macro_build (p, counter, ep, "daddiu", "t,r,j",
3553                            reg, reg, (int) BFD_RELOC_LO16);
3554             }
3555         }
3556       else
3557         {
3558           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3559               && ! nopic_need_relax (ep->X_add_symbol, 1))
3560             {
3561               frag_grow (20);
3562               macro_build ((char *) NULL, counter, ep,
3563                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3564                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3565               p = frag_var (rs_machine_dependent, 8, 0,
3566                             RELAX_ENCODE (4, 8, 0, 4, 0,
3567                                           mips_opts.warn_about_macros),
3568                             ep->X_add_symbol, 0, NULL);
3569             }
3570           macro_build_lui (p, counter, ep, reg);
3571           if (p != NULL)
3572             p += 4;
3573           macro_build (p, counter, ep,
3574                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3575                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3576         }
3577     }
3578   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3579     {
3580       expressionS ex;
3581
3582       /* If this is a reference to an external symbol, we want
3583            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3584          Otherwise we want
3585            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3586            nop
3587            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3588          If there is a constant, it must be added in after.  */
3589       ex.X_add_number = ep->X_add_number;
3590       ep->X_add_number = 0;
3591       frag_grow (20);
3592       macro_build ((char *) NULL, counter, ep,
3593                    HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3594                    reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3595       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3596       p = frag_var (rs_machine_dependent, 4, 0,
3597                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3598                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3599       macro_build (p, counter, ep,
3600                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3601                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3602       if (ex.X_add_number != 0)
3603         {
3604           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3605             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3606           ex.X_op = O_constant;
3607           macro_build ((char *) NULL, counter, &ex,
3608                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3609                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3610         }
3611     }
3612   else if (mips_pic == SVR4_PIC)
3613     {
3614       expressionS ex;
3615       int off;
3616
3617       /* This is the large GOT case.  If this is a reference to an
3618          external symbol, we want
3619            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3620            addu         $reg,$reg,$gp
3621            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3622          Otherwise, for a reference to a local symbol, we want
3623            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3624            nop
3625            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3626          If we have NewABI, we want
3627            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3628            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3629          If there is a constant, it must be added in after.  */
3630       ex.X_add_number = ep->X_add_number;
3631       ep->X_add_number = 0;
3632       if (HAVE_NEWABI)
3633         {
3634           macro_build ((char *) NULL, counter, ep,
3635                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3636                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3637           macro_build (p, counter, ep,
3638                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3639                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3640         }
3641       else
3642         {
3643           if (reg_needs_delay (mips_gp_register))
3644             off = 4;
3645           else
3646             off = 0;
3647           frag_grow (32);
3648           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3649                        (int) BFD_RELOC_MIPS_GOT_HI16);
3650           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3651                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3652                        reg, mips_gp_register);
3653           macro_build ((char *) NULL, counter, ep,
3654                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3655                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3656           p = frag_var (rs_machine_dependent, 12 + off, 0,
3657                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3658                                       mips_opts.warn_about_macros),
3659                         ep->X_add_symbol, 0, NULL);
3660           if (off > 0)
3661             {
3662               /* We need a nop before loading from $gp.  This special
3663                  check is required because the lui which starts the main
3664                  instruction stream does not refer to $gp, and so will not
3665                  insert the nop which may be required.  */
3666               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3667                 p += 4;
3668             }
3669           macro_build (p, counter, ep,
3670                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3671                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3672           p += 4;
3673           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3674           p += 4;
3675           macro_build (p, counter, ep,
3676                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3677                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3678         }
3679
3680       if (ex.X_add_number != 0)
3681         {
3682           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3683             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3684           ex.X_op = O_constant;
3685           macro_build ((char *) NULL, counter, &ex,
3686                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3687                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3688         }
3689     }
3690   else if (mips_pic == EMBEDDED_PIC)
3691     {
3692       /* We always do
3693            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3694        */
3695       macro_build ((char *) NULL, counter, ep,
3696                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3697                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3698     }
3699   else
3700     abort ();
3701 }
3702
3703 /* Move the contents of register SOURCE into register DEST.  */
3704
3705 static void
3706 move_register (counter, dest, source)
3707      int *counter;
3708      int dest;
3709      int source;
3710 {
3711   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3712                HAVE_32BIT_GPRS ? "addu" : "daddu",
3713                "d,v,t", dest, source, 0);
3714 }
3715
3716 /*
3717  *                      Build macros
3718  *   This routine implements the seemingly endless macro or synthesized
3719  * instructions and addressing modes in the mips assembly language. Many
3720  * of these macros are simple and are similar to each other. These could
3721  * probably be handled by some kind of table or grammer aproach instead of
3722  * this verbose method. Others are not simple macros but are more like
3723  * optimizing code generation.
3724  *   One interesting optimization is when several store macros appear
3725  * consecutivly that would load AT with the upper half of the same address.
3726  * The ensuing load upper instructions are ommited. This implies some kind
3727  * of global optimization. We currently only optimize within a single macro.
3728  *   For many of the load and store macros if the address is specified as a
3729  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3730  * first load register 'at' with zero and use it as the base register. The
3731  * mips assembler simply uses register $zero. Just one tiny optimization
3732  * we're missing.
3733  */
3734 static void
3735 macro (ip)
3736      struct mips_cl_insn *ip;
3737 {
3738   register int treg, sreg, dreg, breg;
3739   int tempreg;
3740   int mask;
3741   int icnt = 0;
3742   int used_at = 0;
3743   expressionS expr1;
3744   const char *s;
3745   const char *s2;
3746   const char *fmt;
3747   int likely = 0;
3748   int dbl = 0;
3749   int coproc = 0;
3750   int lr = 0;
3751   int imm = 0;
3752   offsetT maxnum;
3753   int off;
3754   bfd_reloc_code_real_type r;
3755   int hold_mips_optimize;
3756
3757   assert (! mips_opts.mips16);
3758
3759   treg = (ip->insn_opcode >> 16) & 0x1f;
3760   dreg = (ip->insn_opcode >> 11) & 0x1f;
3761   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3762   mask = ip->insn_mo->mask;
3763
3764   expr1.X_op = O_constant;
3765   expr1.X_op_symbol = NULL;
3766   expr1.X_add_symbol = NULL;
3767   expr1.X_add_number = 1;
3768
3769   switch (mask)
3770     {
3771     case M_DABS:
3772       dbl = 1;
3773     case M_ABS:
3774       /* bgez $a0,.+12
3775          move v0,$a0
3776          sub v0,$zero,$a0
3777          */
3778
3779       mips_emit_delays (true);
3780       ++mips_opts.noreorder;
3781       mips_any_noreorder = 1;
3782
3783       expr1.X_add_number = 8;
3784       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3785       if (dreg == sreg)
3786         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3787                      0);
3788       else
3789         move_register (&icnt, dreg, sreg);
3790       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3791                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3792
3793       --mips_opts.noreorder;
3794       return;
3795
3796     case M_ADD_I:
3797       s = "addi";
3798       s2 = "add";
3799       goto do_addi;
3800     case M_ADDU_I:
3801       s = "addiu";
3802       s2 = "addu";
3803       goto do_addi;
3804     case M_DADD_I:
3805       dbl = 1;
3806       s = "daddi";
3807       s2 = "dadd";
3808       goto do_addi;
3809     case M_DADDU_I:
3810       dbl = 1;
3811       s = "daddiu";
3812       s2 = "daddu";
3813     do_addi:
3814       if (imm_expr.X_op == O_constant
3815           && imm_expr.X_add_number >= -0x8000
3816           && imm_expr.X_add_number < 0x8000)
3817         {
3818           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3819                        (int) BFD_RELOC_LO16);
3820           return;
3821         }
3822       load_register (&icnt, AT, &imm_expr, dbl);
3823       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3824                    treg, sreg, AT);
3825       break;
3826
3827     case M_AND_I:
3828       s = "andi";
3829       s2 = "and";
3830       goto do_bit;
3831     case M_OR_I:
3832       s = "ori";
3833       s2 = "or";
3834       goto do_bit;
3835     case M_NOR_I:
3836       s = "";
3837       s2 = "nor";
3838       goto do_bit;
3839     case M_XOR_I:
3840       s = "xori";
3841       s2 = "xor";
3842     do_bit:
3843       if (imm_expr.X_op == O_constant
3844           && imm_expr.X_add_number >= 0
3845           && imm_expr.X_add_number < 0x10000)
3846         {
3847           if (mask != M_NOR_I)
3848             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3849                          sreg, (int) BFD_RELOC_LO16);
3850           else
3851             {
3852               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3853                            treg, sreg, (int) BFD_RELOC_LO16);
3854               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3855                            "d,v,t", treg, treg, 0);
3856             }
3857           return;
3858         }
3859
3860       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3861       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3862                    treg, sreg, AT);
3863       break;
3864
3865     case M_BEQ_I:
3866       s = "beq";
3867       goto beq_i;
3868     case M_BEQL_I:
3869       s = "beql";
3870       likely = 1;
3871       goto beq_i;
3872     case M_BNE_I:
3873       s = "bne";
3874       goto beq_i;
3875     case M_BNEL_I:
3876       s = "bnel";
3877       likely = 1;
3878     beq_i:
3879       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3880         {
3881           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3882                        0);
3883           return;
3884         }
3885       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3886       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3887       break;
3888
3889     case M_BGEL:
3890       likely = 1;
3891     case M_BGE:
3892       if (treg == 0)
3893         {
3894           macro_build ((char *) NULL, &icnt, &offset_expr,
3895                        likely ? "bgezl" : "bgez", "s,p", sreg);
3896           return;
3897         }
3898       if (sreg == 0)
3899         {
3900           macro_build ((char *) NULL, &icnt, &offset_expr,
3901                        likely ? "blezl" : "blez", "s,p", treg);
3902           return;
3903         }
3904       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3905                    AT, sreg, treg);
3906       macro_build ((char *) NULL, &icnt, &offset_expr,
3907                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3908       break;
3909
3910     case M_BGTL_I:
3911       likely = 1;
3912     case M_BGT_I:
3913       /* check for > max integer */
3914       maxnum = 0x7fffffff;
3915       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3916         {
3917           maxnum <<= 16;
3918           maxnum |= 0xffff;
3919           maxnum <<= 16;
3920           maxnum |= 0xffff;
3921         }
3922       if (imm_expr.X_op == O_constant
3923           && imm_expr.X_add_number >= maxnum
3924           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3925         {
3926         do_false:
3927           /* result is always false */
3928           if (! likely)
3929             {
3930               if (warn_nops)
3931                 as_warn (_("Branch %s is always false (nop)"),
3932                          ip->insn_mo->name);
3933               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3934                            "", 0);
3935             }
3936           else
3937             {
3938               if (warn_nops)
3939                 as_warn (_("Branch likely %s is always false"),
3940                          ip->insn_mo->name);
3941               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3942                            "s,t,p", 0, 0);
3943             }
3944           return;
3945         }
3946       if (imm_expr.X_op != O_constant)
3947         as_bad (_("Unsupported large constant"));
3948       ++imm_expr.X_add_number;
3949       /* FALLTHROUGH */
3950     case M_BGE_I:
3951     case M_BGEL_I:
3952       if (mask == M_BGEL_I)
3953         likely = 1;
3954       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3955         {
3956           macro_build ((char *) NULL, &icnt, &offset_expr,
3957                        likely ? "bgezl" : "bgez", "s,p", sreg);
3958           return;
3959         }
3960       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3961         {
3962           macro_build ((char *) NULL, &icnt, &offset_expr,
3963                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
3964           return;
3965         }
3966       maxnum = 0x7fffffff;
3967       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3968         {
3969           maxnum <<= 16;
3970           maxnum |= 0xffff;
3971           maxnum <<= 16;
3972           maxnum |= 0xffff;
3973         }
3974       maxnum = - maxnum - 1;
3975       if (imm_expr.X_op == O_constant
3976           && imm_expr.X_add_number <= maxnum
3977           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3978         {
3979         do_true:
3980           /* result is always true */
3981           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3982           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3983           return;
3984         }
3985       set_at (&icnt, sreg, 0);
3986       macro_build ((char *) NULL, &icnt, &offset_expr,
3987                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3988       break;
3989
3990     case M_BGEUL:
3991       likely = 1;
3992     case M_BGEU:
3993       if (treg == 0)
3994         goto do_true;
3995       if (sreg == 0)
3996         {
3997           macro_build ((char *) NULL, &icnt, &offset_expr,
3998                        likely ? "beql" : "beq", "s,t,p", 0, treg);
3999           return;
4000         }
4001       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4002                    "d,v,t", AT, sreg, treg);
4003       macro_build ((char *) NULL, &icnt, &offset_expr,
4004                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4005       break;
4006
4007     case M_BGTUL_I:
4008       likely = 1;
4009     case M_BGTU_I:
4010       if (sreg == 0
4011           || (HAVE_32BIT_GPRS
4012               && imm_expr.X_op == O_constant
4013               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4014         goto do_false;
4015       if (imm_expr.X_op != O_constant)
4016         as_bad (_("Unsupported large constant"));
4017       ++imm_expr.X_add_number;
4018       /* FALLTHROUGH */
4019     case M_BGEU_I:
4020     case M_BGEUL_I:
4021       if (mask == M_BGEUL_I)
4022         likely = 1;
4023       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4024         goto do_true;
4025       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4026         {
4027           macro_build ((char *) NULL, &icnt, &offset_expr,
4028                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4029           return;
4030         }
4031       set_at (&icnt, sreg, 1);
4032       macro_build ((char *) NULL, &icnt, &offset_expr,
4033                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4034       break;
4035
4036     case M_BGTL:
4037       likely = 1;
4038     case M_BGT:
4039       if (treg == 0)
4040         {
4041           macro_build ((char *) NULL, &icnt, &offset_expr,
4042                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4043           return;
4044         }
4045       if (sreg == 0)
4046         {
4047           macro_build ((char *) NULL, &icnt, &offset_expr,
4048                        likely ? "bltzl" : "bltz", "s,p", treg);
4049           return;
4050         }
4051       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4052                    AT, treg, sreg);
4053       macro_build ((char *) NULL, &icnt, &offset_expr,
4054                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4055       break;
4056
4057     case M_BGTUL:
4058       likely = 1;
4059     case M_BGTU:
4060       if (treg == 0)
4061         {
4062           macro_build ((char *) NULL, &icnt, &offset_expr,
4063                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4064           return;
4065         }
4066       if (sreg == 0)
4067         goto do_false;
4068       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4069                    "d,v,t", AT, treg, sreg);
4070       macro_build ((char *) NULL, &icnt, &offset_expr,
4071                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4072       break;
4073
4074     case M_BLEL:
4075       likely = 1;
4076     case M_BLE:
4077       if (treg == 0)
4078         {
4079           macro_build ((char *) NULL, &icnt, &offset_expr,
4080                        likely ? "blezl" : "blez", "s,p", sreg);
4081           return;
4082         }
4083       if (sreg == 0)
4084         {
4085           macro_build ((char *) NULL, &icnt, &offset_expr,
4086                        likely ? "bgezl" : "bgez", "s,p", treg);
4087           return;
4088         }
4089       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4090                    AT, treg, sreg);
4091       macro_build ((char *) NULL, &icnt, &offset_expr,
4092                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4093       break;
4094
4095     case M_BLEL_I:
4096       likely = 1;
4097     case M_BLE_I:
4098       maxnum = 0x7fffffff;
4099       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4100         {
4101           maxnum <<= 16;
4102           maxnum |= 0xffff;
4103           maxnum <<= 16;
4104           maxnum |= 0xffff;
4105         }
4106       if (imm_expr.X_op == O_constant
4107           && imm_expr.X_add_number >= maxnum
4108           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4109         goto do_true;
4110       if (imm_expr.X_op != O_constant)
4111         as_bad (_("Unsupported large constant"));
4112       ++imm_expr.X_add_number;
4113       /* FALLTHROUGH */
4114     case M_BLT_I:
4115     case M_BLTL_I:
4116       if (mask == M_BLTL_I)
4117         likely = 1;
4118       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4119         {
4120           macro_build ((char *) NULL, &icnt, &offset_expr,
4121                        likely ? "bltzl" : "bltz", "s,p", sreg);
4122           return;
4123         }
4124       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4125         {
4126           macro_build ((char *) NULL, &icnt, &offset_expr,
4127                        likely ? "blezl" : "blez", "s,p", sreg);
4128           return;
4129         }
4130       set_at (&icnt, sreg, 0);
4131       macro_build ((char *) NULL, &icnt, &offset_expr,
4132                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4133       break;
4134
4135     case M_BLEUL:
4136       likely = 1;
4137     case M_BLEU:
4138       if (treg == 0)
4139         {
4140           macro_build ((char *) NULL, &icnt, &offset_expr,
4141                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4142           return;
4143         }
4144       if (sreg == 0)
4145         goto do_true;
4146       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4147                    "d,v,t", AT, treg, sreg);
4148       macro_build ((char *) NULL, &icnt, &offset_expr,
4149                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4150       break;
4151
4152     case M_BLEUL_I:
4153       likely = 1;
4154     case M_BLEU_I:
4155       if (sreg == 0
4156           || (HAVE_32BIT_GPRS
4157               && imm_expr.X_op == O_constant
4158               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4159         goto do_true;
4160       if (imm_expr.X_op != O_constant)
4161         as_bad (_("Unsupported large constant"));
4162       ++imm_expr.X_add_number;
4163       /* FALLTHROUGH */
4164     case M_BLTU_I:
4165     case M_BLTUL_I:
4166       if (mask == M_BLTUL_I)
4167         likely = 1;
4168       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4169         goto do_false;
4170       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4171         {
4172           macro_build ((char *) NULL, &icnt, &offset_expr,
4173                        likely ? "beql" : "beq",
4174                        "s,t,p", sreg, 0);
4175           return;
4176         }
4177       set_at (&icnt, sreg, 1);
4178       macro_build ((char *) NULL, &icnt, &offset_expr,
4179                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4180       break;
4181
4182     case M_BLTL:
4183       likely = 1;
4184     case M_BLT:
4185       if (treg == 0)
4186         {
4187           macro_build ((char *) NULL, &icnt, &offset_expr,
4188                        likely ? "bltzl" : "bltz", "s,p", sreg);
4189           return;
4190         }
4191       if (sreg == 0)
4192         {
4193           macro_build ((char *) NULL, &icnt, &offset_expr,
4194                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4195           return;
4196         }
4197       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4198                    AT, sreg, treg);
4199       macro_build ((char *) NULL, &icnt, &offset_expr,
4200                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4201       break;
4202
4203     case M_BLTUL:
4204       likely = 1;
4205     case M_BLTU:
4206       if (treg == 0)
4207         goto do_false;
4208       if (sreg == 0)
4209         {
4210           macro_build ((char *) NULL, &icnt, &offset_expr,
4211                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4212           return;
4213         }
4214       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4215                    "d,v,t", AT, sreg,
4216                    treg);
4217       macro_build ((char *) NULL, &icnt, &offset_expr,
4218                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4219       break;
4220
4221     case M_DDIV_3:
4222       dbl = 1;
4223     case M_DIV_3:
4224       s = "mflo";
4225       goto do_div3;
4226     case M_DREM_3:
4227       dbl = 1;
4228     case M_REM_3:
4229       s = "mfhi";
4230     do_div3:
4231       if (treg == 0)
4232         {
4233           as_warn (_("Divide by zero."));
4234           if (mips_trap)
4235             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4236                          "s,t,q", 0, 0, 7);
4237           else
4238             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4239                          "c", 7);
4240           return;
4241         }
4242
4243       mips_emit_delays (true);
4244       ++mips_opts.noreorder;
4245       mips_any_noreorder = 1;
4246       if (mips_trap)
4247         {
4248           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4249                        "s,t,q", treg, 0, 7);
4250           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4251                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4252         }
4253       else
4254         {
4255           expr1.X_add_number = 8;
4256           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4257           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4258                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4259           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4260                        "c", 7);
4261         }
4262       expr1.X_add_number = -1;
4263       macro_build ((char *) NULL, &icnt, &expr1,
4264                    dbl ? "daddiu" : "addiu",
4265                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4266       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4267       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4268       if (dbl)
4269         {
4270           expr1.X_add_number = 1;
4271           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4272                        (int) BFD_RELOC_LO16);
4273           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4274                        "d,w,<", AT, AT, 31);
4275         }
4276       else
4277         {
4278           expr1.X_add_number = 0x80000000;
4279           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4280                        (int) BFD_RELOC_HI16);
4281         }
4282       if (mips_trap)
4283         {
4284           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4285                        "s,t,q", sreg, AT, 6);
4286           /* We want to close the noreorder block as soon as possible, so
4287              that later insns are available for delay slot filling.  */
4288           --mips_opts.noreorder;
4289         }
4290       else
4291         {
4292           expr1.X_add_number = 8;
4293           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4294           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4295                        0);
4296
4297           /* We want to close the noreorder block as soon as possible, so
4298              that later insns are available for delay slot filling.  */
4299           --mips_opts.noreorder;
4300
4301           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4302                        "c", 6);
4303         }
4304       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4305       break;
4306
4307     case M_DIV_3I:
4308       s = "div";
4309       s2 = "mflo";
4310       goto do_divi;
4311     case M_DIVU_3I:
4312       s = "divu";
4313       s2 = "mflo";
4314       goto do_divi;
4315     case M_REM_3I:
4316       s = "div";
4317       s2 = "mfhi";
4318       goto do_divi;
4319     case M_REMU_3I:
4320       s = "divu";
4321       s2 = "mfhi";
4322       goto do_divi;
4323     case M_DDIV_3I:
4324       dbl = 1;
4325       s = "ddiv";
4326       s2 = "mflo";
4327       goto do_divi;
4328     case M_DDIVU_3I:
4329       dbl = 1;
4330       s = "ddivu";
4331       s2 = "mflo";
4332       goto do_divi;
4333     case M_DREM_3I:
4334       dbl = 1;
4335       s = "ddiv";
4336       s2 = "mfhi";
4337       goto do_divi;
4338     case M_DREMU_3I:
4339       dbl = 1;
4340       s = "ddivu";
4341       s2 = "mfhi";
4342     do_divi:
4343       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4344         {
4345           as_warn (_("Divide by zero."));
4346           if (mips_trap)
4347             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4348                          "s,t,q", 0, 0, 7);
4349           else
4350             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4351                          "c", 7);
4352           return;
4353         }
4354       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4355         {
4356           if (strcmp (s2, "mflo") == 0)
4357             move_register (&icnt, dreg, sreg);
4358           else
4359             move_register (&icnt, dreg, 0);
4360           return;
4361         }
4362       if (imm_expr.X_op == O_constant
4363           && imm_expr.X_add_number == -1
4364           && s[strlen (s) - 1] != 'u')
4365         {
4366           if (strcmp (s2, "mflo") == 0)
4367             {
4368               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4369                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4370             }
4371           else
4372             move_register (&icnt, dreg, 0);
4373           return;
4374         }
4375
4376       load_register (&icnt, AT, &imm_expr, dbl);
4377       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4378                    sreg, AT);
4379       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4380       break;
4381
4382     case M_DIVU_3:
4383       s = "divu";
4384       s2 = "mflo";
4385       goto do_divu3;
4386     case M_REMU_3:
4387       s = "divu";
4388       s2 = "mfhi";
4389       goto do_divu3;
4390     case M_DDIVU_3:
4391       s = "ddivu";
4392       s2 = "mflo";
4393       goto do_divu3;
4394     case M_DREMU_3:
4395       s = "ddivu";
4396       s2 = "mfhi";
4397     do_divu3:
4398       mips_emit_delays (true);
4399       ++mips_opts.noreorder;
4400       mips_any_noreorder = 1;
4401       if (mips_trap)
4402         {
4403           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4404                        "s,t,q", treg, 0, 7);
4405           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4406                        sreg, treg);
4407           /* We want to close the noreorder block as soon as possible, so
4408              that later insns are available for delay slot filling.  */
4409           --mips_opts.noreorder;
4410         }
4411       else
4412         {
4413           expr1.X_add_number = 8;
4414           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4415           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4416                        sreg, treg);
4417
4418           /* We want to close the noreorder block as soon as possible, so
4419              that later insns are available for delay slot filling.  */
4420           --mips_opts.noreorder;
4421           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4422                        "c", 7);
4423         }
4424       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4425       return;
4426
4427     case M_DLA_AB:
4428       dbl = 1;
4429     case M_LA_AB:
4430       /* Load the address of a symbol into a register.  If breg is not
4431          zero, we then add a base register to it.  */
4432
4433       if (dbl && HAVE_32BIT_GPRS)
4434         as_warn (_("dla used to load 32-bit register"));
4435
4436       if (! dbl && HAVE_64BIT_OBJECTS)
4437         as_warn (_("la used to load 64-bit address"));
4438
4439       if (offset_expr.X_op == O_constant
4440           && offset_expr.X_add_number >= -0x8000
4441           && offset_expr.X_add_number < 0x8000)
4442         {
4443           macro_build ((char *) NULL, &icnt, &offset_expr,
4444                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4445                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4446           return;
4447         }
4448
4449       if (treg == breg)
4450         {
4451           tempreg = AT;
4452           used_at = 1;
4453         }
4454       else
4455         {
4456           tempreg = treg;
4457           used_at = 0;
4458         }
4459
4460       /* When generating embedded PIC code, we permit expressions of
4461          the form
4462            la   $treg,foo-bar
4463            la   $treg,foo-bar($breg)
4464          where bar is an address in the current section.  These are used
4465          when getting the addresses of functions.  We don't permit
4466          X_add_number to be non-zero, because if the symbol is
4467          external the relaxing code needs to know that any addend is
4468          purely the offset to X_op_symbol.  */
4469       if (mips_pic == EMBEDDED_PIC
4470           && offset_expr.X_op == O_subtract
4471           && (symbol_constant_p (offset_expr.X_op_symbol)
4472               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4473               : (symbol_equated_p (offset_expr.X_op_symbol)
4474                  && (S_GET_SEGMENT
4475                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4476                       ->X_add_symbol)
4477                      == now_seg)))
4478           && (offset_expr.X_add_number == 0
4479               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4480         {
4481           if (breg == 0)
4482             {
4483               tempreg = treg;
4484               used_at = 0;
4485               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4486                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4487             }
4488           else
4489             {
4490               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4491                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4492               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4493                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4494                            "d,v,t", tempreg, tempreg, breg);
4495             }
4496           macro_build ((char *) NULL, &icnt, &offset_expr,
4497                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4498                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4499           if (! used_at)
4500             return;
4501           break;
4502         }
4503
4504       if (offset_expr.X_op != O_symbol
4505           && offset_expr.X_op != O_constant)
4506         {
4507           as_bad (_("expression too complex"));
4508           offset_expr.X_op = O_constant;
4509         }
4510
4511       if (offset_expr.X_op == O_constant)
4512         load_register (&icnt, tempreg, &offset_expr,
4513                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4514                         ? (dbl || HAVE_64BIT_ADDRESSES)
4515                         : HAVE_64BIT_ADDRESSES));
4516       else if (mips_pic == NO_PIC)
4517         {
4518           /* If this is a reference to a GP relative symbol, we want
4519                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4520              Otherwise we want
4521                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4522                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4523              If we have a constant, we need two instructions anyhow,
4524              so we may as well always use the latter form.
4525
4526             With 64bit address space and a usable $at we want
4527               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4528               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4529               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4530               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4531               dsll32    $tempreg,0
4532               daddu     $tempreg,$tempreg,$at
4533
4534             If $at is already in use, we use an path which is suboptimal
4535             on superscalar processors.
4536               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4537               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4538               dsll      $tempreg,16
4539               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4540               dsll      $tempreg,16
4541               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4542           */
4543           char *p = NULL;
4544           if (HAVE_64BIT_ADDRESSES)
4545             {
4546               /* We don't do GP optimization for now because RELAX_ENCODE can't
4547                  hold the data for such large chunks.  */
4548
4549               if (used_at == 0 && ! mips_opts.noat)
4550                 {
4551                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4552                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4553                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4554                                AT, (int) BFD_RELOC_HI16_S);
4555                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4556                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4557                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4558                                AT, AT, (int) BFD_RELOC_LO16);
4559                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4560                                "d,w,<", tempreg, tempreg, 0);
4561                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4562                                "d,v,t", tempreg, tempreg, AT);
4563                   used_at = 1;
4564                 }
4565               else
4566                 {
4567                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4568                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4569                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4570                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4571                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4572                                tempreg, tempreg, 16);
4573                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4574                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4575                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4576                                tempreg, tempreg, 16);
4577                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4578                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4579                 }
4580             }
4581           else
4582             {
4583               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4584                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4585                 {
4586                   frag_grow (20);
4587                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4588                                "t,r,j", tempreg, mips_gp_register,
4589                                (int) BFD_RELOC_GPREL16);
4590                   p = frag_var (rs_machine_dependent, 8, 0,
4591                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4592                                               mips_opts.warn_about_macros),
4593                                 offset_expr.X_add_symbol, 0, NULL);
4594                 }
4595               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4596               if (p != NULL)
4597                 p += 4;
4598               macro_build (p, &icnt, &offset_expr, "addiu",
4599                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4600             }
4601         }
4602       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4603         {
4604           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4605
4606           /* If this is a reference to an external symbol, and there
4607              is no constant, we want
4608                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4609              or if tempreg is PIC_CALL_REG
4610                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4611              For a local symbol, we want
4612                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4613                nop
4614                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4615
4616              If we have a small constant, and this is a reference to
4617              an external symbol, we want
4618                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4619                nop
4620                addiu    $tempreg,$tempreg,<constant>
4621              For a local symbol, we want the same instruction
4622              sequence, but we output a BFD_RELOC_LO16 reloc on the
4623              addiu instruction.
4624
4625              If we have a large constant, and this is a reference to
4626              an external symbol, we want
4627                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4628                lui      $at,<hiconstant>
4629                addiu    $at,$at,<loconstant>
4630                addu     $tempreg,$tempreg,$at
4631              For a local symbol, we want the same instruction
4632              sequence, but we output a BFD_RELOC_LO16 reloc on the
4633              addiu instruction.  */
4634           expr1.X_add_number = offset_expr.X_add_number;
4635           offset_expr.X_add_number = 0;
4636           frag_grow (32);
4637           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4638             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4639           macro_build ((char *) NULL, &icnt, &offset_expr,
4640                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4641                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4642           if (expr1.X_add_number == 0)
4643             {
4644               int off;
4645               char *p;
4646
4647               if (breg == 0)
4648                 off = 0;
4649               else
4650                 {
4651                   /* We're going to put in an addu instruction using
4652                      tempreg, so we may as well insert the nop right
4653                      now.  */
4654                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4655                                "nop", "");
4656                   off = 4;
4657                 }
4658               p = frag_var (rs_machine_dependent, 8 - off, 0,
4659                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4660                                           (breg == 0
4661                                            ? mips_opts.warn_about_macros
4662                                            : 0)),
4663                             offset_expr.X_add_symbol, 0, NULL);
4664               if (breg == 0)
4665                 {
4666                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4667                   p += 4;
4668                 }
4669               macro_build (p, &icnt, &expr1,
4670                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4671                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4672               /* FIXME: If breg == 0, and the next instruction uses
4673                  $tempreg, then if this variant case is used an extra
4674                  nop will be generated.  */
4675             }
4676           else if (expr1.X_add_number >= -0x8000
4677                    && expr1.X_add_number < 0x8000)
4678             {
4679               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4680                            "nop", "");
4681               macro_build ((char *) NULL, &icnt, &expr1,
4682                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4683                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4684               frag_var (rs_machine_dependent, 0, 0,
4685                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4686                         offset_expr.X_add_symbol, 0, NULL);
4687             }
4688           else
4689             {
4690               int off1;
4691
4692               /* If we are going to add in a base register, and the
4693                  target register and the base register are the same,
4694                  then we are using AT as a temporary register.  Since
4695                  we want to load the constant into AT, we add our
4696                  current AT (from the global offset table) and the
4697                  register into the register now, and pretend we were
4698                  not using a base register.  */
4699               if (breg != treg)
4700                 off1 = 0;
4701               else
4702                 {
4703                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4704                                "nop", "");
4705                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4706                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4707                                "d,v,t", treg, AT, breg);
4708                   breg = 0;
4709                   tempreg = treg;
4710                   off1 = -8;
4711                 }
4712
4713               /* Set mips_optimize around the lui instruction to avoid
4714                  inserting an unnecessary nop after the lw.  */
4715               hold_mips_optimize = mips_optimize;
4716               mips_optimize = 2;
4717               macro_build_lui (NULL, &icnt, &expr1, AT);
4718               mips_optimize = hold_mips_optimize;
4719
4720               macro_build ((char *) NULL, &icnt, &expr1,
4721                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4722                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4723               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4724                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4725                            "d,v,t", tempreg, tempreg, AT);
4726               frag_var (rs_machine_dependent, 0, 0,
4727                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4728                         offset_expr.X_add_symbol, 0, NULL);
4729               used_at = 1;
4730             }
4731         }
4732       else if (mips_pic == SVR4_PIC)
4733         {
4734           int gpdel;
4735           char *p;
4736           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4737           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4738
4739           /* This is the large GOT case.  If this is a reference to an
4740              external symbol, and there is no constant, we want
4741                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4742                addu     $tempreg,$tempreg,$gp
4743                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4744              or if tempreg is PIC_CALL_REG
4745                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4746                addu     $tempreg,$tempreg,$gp
4747                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4748              For a local symbol, we want
4749                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4750                nop
4751                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4752
4753              If we have a small constant, and this is a reference to
4754              an external symbol, we want
4755                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4756                addu     $tempreg,$tempreg,$gp
4757                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4758                nop
4759                addiu    $tempreg,$tempreg,<constant>
4760              For a local symbol, we want
4761                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4762                nop
4763                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4764
4765              If we have a large constant, and this is a reference to
4766              an external symbol, we want
4767                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4768                addu     $tempreg,$tempreg,$gp
4769                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4770                lui      $at,<hiconstant>
4771                addiu    $at,$at,<loconstant>
4772                addu     $tempreg,$tempreg,$at
4773              For a local symbol, we want
4774                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4775                lui      $at,<hiconstant>
4776                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4777                addu     $tempreg,$tempreg,$at
4778
4779              For NewABI, we want for data addresses
4780                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4781              If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4782                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4783            */
4784           if (HAVE_NEWABI)
4785             {
4786               int reloc_type = (tempreg == PIC_CALL_REG
4787                                 ? BFD_RELOC_MIPS_CALL16
4788                                 : BFD_RELOC_MIPS_GOT_DISP);
4789
4790               macro_build ((char *) NULL, &icnt, &offset_expr,
4791                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4792                            "t,o(b)", tempreg, reloc_type, mips_gp_register);
4793
4794               if (breg != 0)
4795                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4796                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4797                              "d,v,t", treg, tempreg, breg);
4798
4799               if (! used_at)
4800                 return;
4801
4802               break;
4803             }
4804           expr1.X_add_number = offset_expr.X_add_number;
4805           offset_expr.X_add_number = 0;
4806           frag_grow (52);
4807           if (reg_needs_delay (mips_gp_register))
4808             gpdel = 4;
4809           else
4810             gpdel = 0;
4811           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4812             {
4813               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4814               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4815             }
4816           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4817                        tempreg, lui_reloc_type);
4818           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4819                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4820                        "d,v,t", tempreg, tempreg, mips_gp_register);
4821           macro_build ((char *) NULL, &icnt, &offset_expr,
4822                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4823                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4824           if (expr1.X_add_number == 0)
4825             {
4826               int off;
4827
4828               if (breg == 0)
4829                 off = 0;
4830               else
4831                 {
4832                   /* We're going to put in an addu instruction using
4833                      tempreg, so we may as well insert the nop right
4834                      now.  */
4835                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4836                                "nop", "");
4837                   off = 4;
4838                 }
4839
4840               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4841                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4842                                           8 + gpdel, 0,
4843                                           (breg == 0
4844                                            ? mips_opts.warn_about_macros
4845                                            : 0)),
4846                             offset_expr.X_add_symbol, 0, NULL);
4847             }
4848           else if (expr1.X_add_number >= -0x8000
4849                    && expr1.X_add_number < 0x8000)
4850             {
4851               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4852                            "nop", "");
4853               macro_build ((char *) NULL, &icnt, &expr1,
4854                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4855                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4856
4857               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4858                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4859                                           (breg == 0
4860                                            ? mips_opts.warn_about_macros
4861                                            : 0)),
4862                             offset_expr.X_add_symbol, 0, NULL);
4863             }
4864           else
4865             {
4866               int adj, dreg;
4867
4868               /* If we are going to add in a base register, and the
4869                  target register and the base register are the same,
4870                  then we are using AT as a temporary register.  Since
4871                  we want to load the constant into AT, we add our
4872                  current AT (from the global offset table) and the
4873                  register into the register now, and pretend we were
4874                  not using a base register.  */
4875               if (breg != treg)
4876                 {
4877                   adj = 0;
4878                   dreg = tempreg;
4879                 }
4880               else
4881                 {
4882                   assert (tempreg == AT);
4883                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4884                                "nop", "");
4885                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4886                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4887                                "d,v,t", treg, AT, breg);
4888                   dreg = treg;
4889                   adj = 8;
4890                 }
4891
4892               /* Set mips_optimize around the lui instruction to avoid
4893                  inserting an unnecessary nop after the lw.  */
4894               hold_mips_optimize = mips_optimize;
4895               mips_optimize = 2;
4896               macro_build_lui (NULL, &icnt, &expr1, AT);
4897               mips_optimize = hold_mips_optimize;
4898
4899               macro_build ((char *) NULL, &icnt, &expr1,
4900                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4901                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4902               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4903                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4904                            "d,v,t", dreg, dreg, AT);
4905
4906               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4907                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4908                                           8 + gpdel, 0,
4909                                           (breg == 0
4910                                            ? mips_opts.warn_about_macros
4911                                            : 0)),
4912                             offset_expr.X_add_symbol, 0, NULL);
4913
4914               used_at = 1;
4915             }
4916
4917           if (gpdel > 0)
4918             {
4919               /* This is needed because this instruction uses $gp, but
4920                  the first instruction on the main stream does not.  */
4921               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4922               p += 4;
4923             }
4924           macro_build (p, &icnt, &offset_expr,
4925                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4926                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4927                        mips_gp_register);
4928           p += 4;
4929           if (expr1.X_add_number >= -0x8000
4930               && expr1.X_add_number < 0x8000)
4931             {
4932               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4933               p += 4;
4934               macro_build (p, &icnt, &expr1,
4935                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4936                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4937               /* FIXME: If add_number is 0, and there was no base
4938                  register, the external symbol case ended with a load,
4939                  so if the symbol turns out to not be external, and
4940                  the next instruction uses tempreg, an unnecessary nop
4941                  will be inserted.  */
4942             }
4943           else
4944             {
4945               if (breg == treg)
4946                 {
4947                   /* We must add in the base register now, as in the
4948                      external symbol case.  */
4949                   assert (tempreg == AT);
4950                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4951                   p += 4;
4952                   macro_build (p, &icnt, (expressionS *) NULL,
4953                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4954                                "d,v,t", treg, AT, breg);
4955                   p += 4;
4956                   tempreg = treg;
4957                   /* We set breg to 0 because we have arranged to add
4958                      it in in both cases.  */
4959                   breg = 0;
4960                 }
4961
4962               macro_build_lui (p, &icnt, &expr1, AT);
4963               p += 4;
4964               macro_build (p, &icnt, &expr1,
4965                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4966                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4967               p += 4;
4968               macro_build (p, &icnt, (expressionS *) NULL,
4969                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4970                            "d,v,t", tempreg, tempreg, AT);
4971               p += 4;
4972             }
4973         }
4974       else if (mips_pic == EMBEDDED_PIC)
4975         {
4976           /* We use
4977                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4978              */
4979           macro_build ((char *) NULL, &icnt, &offset_expr,
4980                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4981                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4982         }
4983       else
4984         abort ();
4985
4986       if (breg != 0)
4987         {
4988           char *s;
4989
4990           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4991             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4992           else
4993             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
4994
4995           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4996                        "d,v,t", treg, tempreg, breg);
4997         }
4998
4999       if (! used_at)
5000         return;
5001
5002       break;
5003
5004     case M_J_A:
5005       /* The j instruction may not be used in PIC code, since it
5006          requires an absolute address.  We convert it to a b
5007          instruction.  */
5008       if (mips_pic == NO_PIC)
5009         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5010       else
5011         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5012       return;
5013
5014       /* The jal instructions must be handled as macros because when
5015          generating PIC code they expand to multi-instruction
5016          sequences.  Normally they are simple instructions.  */
5017     case M_JAL_1:
5018       dreg = RA;
5019       /* Fall through.  */
5020     case M_JAL_2:
5021       if (mips_pic == NO_PIC
5022           || mips_pic == EMBEDDED_PIC)
5023         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5024                      "d,s", dreg, sreg);
5025       else if (mips_pic == SVR4_PIC)
5026         {
5027           if (sreg != PIC_CALL_REG)
5028             as_warn (_("MIPS PIC call to register other than $25"));
5029
5030           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5031                        "d,s", dreg, sreg);
5032           if (! HAVE_NEWABI)
5033             {
5034               if (mips_cprestore_offset < 0)
5035                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5036               else
5037                 {
5038                   if (! mips_frame_reg_valid)
5039                     {
5040                       as_warn (_("No .frame pseudo-op used in PIC code"));
5041                       /* Quiet this warning.  */
5042                       mips_frame_reg_valid = 1;
5043                     }
5044                   if (! mips_cprestore_valid)
5045                     {
5046                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5047                       /* Quiet this warning.  */
5048                       mips_cprestore_valid = 1;
5049                     }
5050                   expr1.X_add_number = mips_cprestore_offset;
5051                   macro_build ((char *) NULL, &icnt, &expr1,
5052                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5053                                mips_gp_register, (int) BFD_RELOC_LO16,
5054                                mips_frame_reg);
5055                 }
5056             }
5057         }
5058       else
5059         abort ();
5060
5061       return;
5062
5063     case M_JAL_A:
5064       if (mips_pic == NO_PIC)
5065         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5066       else if (mips_pic == SVR4_PIC)
5067         {
5068           char *p;
5069
5070           /* If this is a reference to an external symbol, and we are
5071              using a small GOT, we want
5072                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5073                nop
5074                jalr     $ra,$25
5075                nop
5076                lw       $gp,cprestore($sp)
5077              The cprestore value is set using the .cprestore
5078              pseudo-op.  If we are using a big GOT, we want
5079                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5080                addu     $25,$25,$gp
5081                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5082                nop
5083                jalr     $ra,$25
5084                nop
5085                lw       $gp,cprestore($sp)
5086              If the symbol is not external, we want
5087                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5088                nop
5089                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5090                jalr     $ra,$25
5091                nop
5092                lw $gp,cprestore($sp)
5093              For NewABI, we want
5094                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5095                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5096            */
5097           if (HAVE_NEWABI)
5098             {
5099               macro_build ((char *) NULL, &icnt, &offset_expr,
5100                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5101                            "t,o(b)", PIC_CALL_REG,
5102                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5103               macro_build_jalr (icnt, &offset_expr);
5104             }
5105           else
5106             {
5107               frag_grow (40);
5108               if (! mips_big_got)
5109                 {
5110                   macro_build ((char *) NULL, &icnt, &offset_expr,
5111                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5112                                "t,o(b)", PIC_CALL_REG,
5113                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5114                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5115                                "nop", "");
5116                   p = frag_var (rs_machine_dependent, 4, 0,
5117                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5118                                 offset_expr.X_add_symbol, 0, NULL);
5119                 }
5120               else
5121                 {
5122                   int gpdel;
5123
5124                   if (reg_needs_delay (mips_gp_register))
5125                     gpdel = 4;
5126                   else
5127                     gpdel = 0;
5128                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5129                                "t,u", PIC_CALL_REG,
5130                                (int) BFD_RELOC_MIPS_CALL_HI16);
5131                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5132                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5133                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5134                                mips_gp_register);
5135                   macro_build ((char *) NULL, &icnt, &offset_expr,
5136                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5137                                "t,o(b)", PIC_CALL_REG,
5138                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5139                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5140                                "nop", "");
5141                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5142                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5143                                               8 + gpdel, 0, 0),
5144                                 offset_expr.X_add_symbol, 0, NULL);
5145                   if (gpdel > 0)
5146                     {
5147                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5148                       p += 4;
5149                     }
5150                   macro_build (p, &icnt, &offset_expr,
5151                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5152                                "t,o(b)", PIC_CALL_REG,
5153                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5154                   p += 4;
5155                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5156                   p += 4;
5157                 }
5158               macro_build (p, &icnt, &offset_expr,
5159                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5160                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5161                            (int) BFD_RELOC_LO16);
5162               macro_build_jalr (icnt, &offset_expr);
5163
5164               if (mips_cprestore_offset < 0)
5165                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5166               else
5167                 {
5168                   if (! mips_frame_reg_valid)
5169                     {
5170                       as_warn (_("No .frame pseudo-op used in PIC code"));
5171                       /* Quiet this warning.  */
5172                       mips_frame_reg_valid = 1;
5173                     }
5174                   if (! mips_cprestore_valid)
5175                     {
5176                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5177                       /* Quiet this warning.  */
5178                       mips_cprestore_valid = 1;
5179                     }
5180                   if (mips_opts.noreorder)
5181                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5182                                  "nop", "");
5183                   expr1.X_add_number = mips_cprestore_offset;
5184                   macro_build ((char *) NULL, &icnt, &expr1,
5185                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5186                                mips_gp_register, (int) BFD_RELOC_LO16,
5187                                mips_frame_reg);
5188                 }
5189             }
5190         }
5191       else if (mips_pic == EMBEDDED_PIC)
5192         {
5193           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5194           /* The linker may expand the call to a longer sequence which
5195              uses $at, so we must break rather than return.  */
5196           break;
5197         }
5198       else
5199         abort ();
5200
5201       return;
5202
5203     case M_LB_AB:
5204       s = "lb";
5205       goto ld;
5206     case M_LBU_AB:
5207       s = "lbu";
5208       goto ld;
5209     case M_LH_AB:
5210       s = "lh";
5211       goto ld;
5212     case M_LHU_AB:
5213       s = "lhu";
5214       goto ld;
5215     case M_LW_AB:
5216       s = "lw";
5217       goto ld;
5218     case M_LWC0_AB:
5219       s = "lwc0";
5220       /* Itbl support may require additional care here.  */
5221       coproc = 1;
5222       goto ld;
5223     case M_LWC1_AB:
5224       s = "lwc1";
5225       /* Itbl support may require additional care here.  */
5226       coproc = 1;
5227       goto ld;
5228     case M_LWC2_AB:
5229       s = "lwc2";
5230       /* Itbl support may require additional care here.  */
5231       coproc = 1;
5232       goto ld;
5233     case M_LWC3_AB:
5234       s = "lwc3";
5235       /* Itbl support may require additional care here.  */
5236       coproc = 1;
5237       goto ld;
5238     case M_LWL_AB:
5239       s = "lwl";
5240       lr = 1;
5241       goto ld;
5242     case M_LWR_AB:
5243       s = "lwr";
5244       lr = 1;
5245       goto ld;
5246     case M_LDC1_AB:
5247       if (mips_arch == CPU_R4650)
5248         {
5249           as_bad (_("opcode not supported on this processor"));
5250           return;
5251         }
5252       s = "ldc1";
5253       /* Itbl support may require additional care here.  */
5254       coproc = 1;
5255       goto ld;
5256     case M_LDC2_AB:
5257       s = "ldc2";
5258       /* Itbl support may require additional care here.  */
5259       coproc = 1;
5260       goto ld;
5261     case M_LDC3_AB:
5262       s = "ldc3";
5263       /* Itbl support may require additional care here.  */
5264       coproc = 1;
5265       goto ld;
5266     case M_LDL_AB:
5267       s = "ldl";
5268       lr = 1;
5269       goto ld;
5270     case M_LDR_AB:
5271       s = "ldr";
5272       lr = 1;
5273       goto ld;
5274     case M_LL_AB:
5275       s = "ll";
5276       goto ld;
5277     case M_LLD_AB:
5278       s = "lld";
5279       goto ld;
5280     case M_LWU_AB:
5281       s = "lwu";
5282     ld:
5283       if (breg == treg || coproc || lr)
5284         {
5285           tempreg = AT;
5286           used_at = 1;
5287         }
5288       else
5289         {
5290           tempreg = treg;
5291           used_at = 0;
5292         }
5293       goto ld_st;
5294     case M_SB_AB:
5295       s = "sb";
5296       goto st;
5297     case M_SH_AB:
5298       s = "sh";
5299       goto st;
5300     case M_SW_AB:
5301       s = "sw";
5302       goto st;
5303     case M_SWC0_AB:
5304       s = "swc0";
5305       /* Itbl support may require additional care here.  */
5306       coproc = 1;
5307       goto st;
5308     case M_SWC1_AB:
5309       s = "swc1";
5310       /* Itbl support may require additional care here.  */
5311       coproc = 1;
5312       goto st;
5313     case M_SWC2_AB:
5314       s = "swc2";
5315       /* Itbl support may require additional care here.  */
5316       coproc = 1;
5317       goto st;
5318     case M_SWC3_AB:
5319       s = "swc3";
5320       /* Itbl support may require additional care here.  */
5321       coproc = 1;
5322       goto st;
5323     case M_SWL_AB:
5324       s = "swl";
5325       goto st;
5326     case M_SWR_AB:
5327       s = "swr";
5328       goto st;
5329     case M_SC_AB:
5330       s = "sc";
5331       goto st;
5332     case M_SCD_AB:
5333       s = "scd";
5334       goto st;
5335     case M_SDC1_AB:
5336       if (mips_arch == CPU_R4650)
5337         {
5338           as_bad (_("opcode not supported on this processor"));
5339           return;
5340         }
5341       s = "sdc1";
5342       coproc = 1;
5343       /* Itbl support may require additional care here.  */
5344       goto st;
5345     case M_SDC2_AB:
5346       s = "sdc2";
5347       /* Itbl support may require additional care here.  */
5348       coproc = 1;
5349       goto st;
5350     case M_SDC3_AB:
5351       s = "sdc3";
5352       /* Itbl support may require additional care here.  */
5353       coproc = 1;
5354       goto st;
5355     case M_SDL_AB:
5356       s = "sdl";
5357       goto st;
5358     case M_SDR_AB:
5359       s = "sdr";
5360     st:
5361       tempreg = AT;
5362       used_at = 1;
5363     ld_st:
5364       /* Itbl support may require additional care here.  */
5365       if (mask == M_LWC1_AB
5366           || mask == M_SWC1_AB
5367           || mask == M_LDC1_AB
5368           || mask == M_SDC1_AB
5369           || mask == M_L_DAB
5370           || mask == M_S_DAB)
5371         fmt = "T,o(b)";
5372       else if (coproc)
5373         fmt = "E,o(b)";
5374       else
5375         fmt = "t,o(b)";
5376
5377       /* For embedded PIC, we allow loads where the offset is calculated
5378          by subtracting a symbol in the current segment from an unknown
5379          symbol, relative to a base register, e.g.:
5380                 <op>    $treg, <sym>-<localsym>($breg)
5381          This is used by the compiler for switch statements.  */
5382       if (mips_pic == EMBEDDED_PIC
5383           && offset_expr.X_op == O_subtract
5384           && (symbol_constant_p (offset_expr.X_op_symbol)
5385               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5386               : (symbol_equated_p (offset_expr.X_op_symbol)
5387                  && (S_GET_SEGMENT
5388                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5389                       ->X_add_symbol)
5390                      == now_seg)))
5391           && breg != 0
5392           && (offset_expr.X_add_number == 0
5393               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5394         {
5395           /* For this case, we output the instructions:
5396                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5397                 addiu   $tempreg,$tempreg,$breg
5398                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5399              If the relocation would fit entirely in 16 bits, it would be
5400              nice to emit:
5401                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5402              instead, but that seems quite difficult.  */
5403           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5404                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5405           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5406                        ((bfd_arch_bits_per_address (stdoutput) == 32
5407                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5408                         ? "addu" : "daddu"),
5409                        "d,v,t", tempreg, tempreg, breg);
5410           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5411                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5412           if (! used_at)
5413             return;
5414           break;
5415         }
5416
5417       if (offset_expr.X_op != O_constant
5418           && offset_expr.X_op != O_symbol)
5419         {
5420           as_bad (_("expression too complex"));
5421           offset_expr.X_op = O_constant;
5422         }
5423
5424       /* A constant expression in PIC code can be handled just as it
5425          is in non PIC code.  */
5426       if (mips_pic == NO_PIC
5427           || offset_expr.X_op == O_constant)
5428         {
5429           char *p;
5430
5431           /* If this is a reference to a GP relative symbol, and there
5432              is no base register, we want
5433                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5434              Otherwise, if there is no base register, we want
5435                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5436                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5437              If we have a constant, we need two instructions anyhow,
5438              so we always use the latter form.
5439
5440              If we have a base register, and this is a reference to a
5441              GP relative symbol, we want
5442                addu     $tempreg,$breg,$gp
5443                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5444              Otherwise we want
5445                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5446                addu     $tempreg,$tempreg,$breg
5447                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5448              With a constant we always use the latter case.
5449
5450              With 64bit address space and no base register and $at usable,
5451              we want
5452                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5453                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5454                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5455                dsll32   $tempreg,0
5456                daddu    $tempreg,$at
5457                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5458              If we have a base register, we want
5459                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5460                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5461                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5462                daddu    $at,$breg
5463                dsll32   $tempreg,0
5464                daddu    $tempreg,$at
5465                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5466
5467              Without $at we can't generate the optimal path for superscalar
5468              processors here since this would require two temporary registers.
5469                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5470                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5471                dsll     $tempreg,16
5472                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5473                dsll     $tempreg,16
5474                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5475              If we have a base register, we want
5476                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5477                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5478                dsll     $tempreg,16
5479                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5480                dsll     $tempreg,16
5481                daddu    $tempreg,$tempreg,$breg
5482                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5483
5484              If we have 64-bit addresses, as an optimization, for
5485              addresses which are 32-bit constants (e.g. kseg0/kseg1
5486              addresses) we fall back to the 32-bit address generation
5487              mechanism since it is more efficient.  Note that due to
5488              the signed offset used by memory operations, the 32-bit
5489              range is shifted down by 32768 here.  This code should
5490              probably attempt to generate 64-bit constants more
5491              efficiently in general.
5492            */
5493           if (HAVE_64BIT_ADDRESSES
5494               && !(offset_expr.X_op == O_constant
5495                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5496             {
5497               p = NULL;
5498
5499               /* We don't do GP optimization for now because RELAX_ENCODE can't
5500                  hold the data for such large chunks.  */
5501
5502               if (used_at == 0 && ! mips_opts.noat)
5503                 {
5504                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5505                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5506                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5507                                AT, (int) BFD_RELOC_HI16_S);
5508                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5509                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5510                   if (breg != 0)
5511                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5512                                  "d,v,t", AT, AT, breg);
5513                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5514                                "d,w,<", tempreg, tempreg, 0);
5515                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5516                                "d,v,t", tempreg, tempreg, AT);
5517                   macro_build (p, &icnt, &offset_expr, s,
5518                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5519                   used_at = 1;
5520                 }
5521               else
5522                 {
5523                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5524                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5525                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5526                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5527                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5528                                "d,w,<", tempreg, tempreg, 16);
5529                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5530                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5531                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5532                                "d,w,<", tempreg, tempreg, 16);
5533                   if (breg != 0)
5534                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5535                                  "d,v,t", tempreg, tempreg, breg);
5536                   macro_build (p, &icnt, &offset_expr, s,
5537                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5538                 }
5539
5540               return;
5541             }
5542
5543           if (breg == 0)
5544             {
5545               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5546                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5547                 p = NULL;
5548               else
5549                 {
5550                   frag_grow (20);
5551                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5552                                treg, (int) BFD_RELOC_GPREL16,
5553                                mips_gp_register);
5554                   p = frag_var (rs_machine_dependent, 8, 0,
5555                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5556                                               (mips_opts.warn_about_macros
5557                                                || (used_at
5558                                                    && mips_opts.noat))),
5559                                 offset_expr.X_add_symbol, 0, NULL);
5560                   used_at = 0;
5561                 }
5562               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5563               if (p != NULL)
5564                 p += 4;
5565               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5566                            (int) BFD_RELOC_LO16, tempreg);
5567             }
5568           else
5569             {
5570               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5571                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5572                 p = NULL;
5573               else
5574                 {
5575                   frag_grow (28);
5576                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5577                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5578                                "d,v,t", tempreg, breg, mips_gp_register);
5579                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5580                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5581                   p = frag_var (rs_machine_dependent, 12, 0,
5582                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5583                                 offset_expr.X_add_symbol, 0, NULL);
5584                 }
5585               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5586               if (p != NULL)
5587                 p += 4;
5588               macro_build (p, &icnt, (expressionS *) NULL,
5589                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5590                            "d,v,t", tempreg, tempreg, breg);
5591               if (p != NULL)
5592                 p += 4;
5593               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5594                            (int) BFD_RELOC_LO16, tempreg);
5595             }
5596         }
5597       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5598         {
5599           char *p;
5600
5601           /* If this is a reference to an external symbol, we want
5602                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5603                nop
5604                <op>     $treg,0($tempreg)
5605              Otherwise we want
5606                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5607                nop
5608                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5609                <op>     $treg,0($tempreg)
5610              If there is a base register, we add it to $tempreg before
5611              the <op>.  If there is a constant, we stick it in the
5612              <op> instruction.  We don't handle constants larger than
5613              16 bits, because we have no way to load the upper 16 bits
5614              (actually, we could handle them for the subset of cases
5615              in which we are not using $at).  */
5616           assert (offset_expr.X_op == O_symbol);
5617           expr1.X_add_number = offset_expr.X_add_number;
5618           offset_expr.X_add_number = 0;
5619           if (expr1.X_add_number < -0x8000
5620               || expr1.X_add_number >= 0x8000)
5621             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5622           frag_grow (20);
5623           macro_build ((char *) NULL, &icnt, &offset_expr,
5624                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5625                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5626           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5627           p = frag_var (rs_machine_dependent, 4, 0,
5628                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5629                         offset_expr.X_add_symbol, 0, NULL);
5630           macro_build (p, &icnt, &offset_expr,
5631                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5632                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5633           if (breg != 0)
5634             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5635                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5636                          "d,v,t", tempreg, tempreg, breg);
5637           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5638                        (int) BFD_RELOC_LO16, tempreg);
5639         }
5640       else if (mips_pic == SVR4_PIC)
5641         {
5642           int gpdel;
5643           char *p;
5644
5645           /* If this is a reference to an external symbol, we want
5646                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5647                addu     $tempreg,$tempreg,$gp
5648                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5649                <op>     $treg,0($tempreg)
5650              Otherwise we want
5651                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5652                nop
5653                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5654                <op>     $treg,0($tempreg)
5655              If there is a base register, we add it to $tempreg before
5656              the <op>.  If there is a constant, we stick it in the
5657              <op> instruction.  We don't handle constants larger than
5658              16 bits, because we have no way to load the upper 16 bits
5659              (actually, we could handle them for the subset of cases
5660              in which we are not using $at).
5661
5662              For NewABI, we want
5663                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5664                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5665                <op>     $treg,0($tempreg)
5666            */
5667           assert (offset_expr.X_op == O_symbol);
5668           expr1.X_add_number = offset_expr.X_add_number;
5669           offset_expr.X_add_number = 0;
5670           if (expr1.X_add_number < -0x8000
5671               || expr1.X_add_number >= 0x8000)
5672             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5673           if (HAVE_NEWABI)
5674             {
5675               macro_build ((char *) NULL, &icnt, &offset_expr,
5676                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5677                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5678                            mips_gp_register);
5679               macro_build ((char *) NULL, &icnt, &offset_expr,
5680                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5681                            "t,r,j", tempreg, tempreg,
5682                            BFD_RELOC_MIPS_GOT_OFST);
5683               if (breg != 0)
5684                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5685                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5686                              "d,v,t", tempreg, tempreg, breg);
5687               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5688                            (int) BFD_RELOC_LO16, tempreg);
5689
5690               if (! used_at)
5691                 return;
5692
5693               break;
5694             }
5695           if (reg_needs_delay (mips_gp_register))
5696             gpdel = 4;
5697           else
5698             gpdel = 0;
5699           frag_grow (36);
5700           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5701                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5702           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5703                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5704                        "d,v,t", tempreg, tempreg, mips_gp_register);
5705           macro_build ((char *) NULL, &icnt, &offset_expr,
5706                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5707                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5708                        tempreg);
5709           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5710                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5711                         offset_expr.X_add_symbol, 0, NULL);
5712           if (gpdel > 0)
5713             {
5714               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5715               p += 4;
5716             }
5717           macro_build (p, &icnt, &offset_expr,
5718                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5719                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5720                        mips_gp_register);
5721           p += 4;
5722           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5723           p += 4;
5724           macro_build (p, &icnt, &offset_expr,
5725                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5726                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5727           if (breg != 0)
5728             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5729                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5730                          "d,v,t", tempreg, tempreg, breg);
5731           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5732                        (int) BFD_RELOC_LO16, tempreg);
5733         }
5734       else if (mips_pic == EMBEDDED_PIC)
5735         {
5736           /* If there is no base register, we want
5737                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5738              If there is a base register, we want
5739                addu     $tempreg,$breg,$gp
5740                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5741              */
5742           assert (offset_expr.X_op == O_symbol);
5743           if (breg == 0)
5744             {
5745               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5746                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5747               used_at = 0;
5748             }
5749           else
5750             {
5751               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5752                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5753                            "d,v,t", tempreg, breg, mips_gp_register);
5754               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5755                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5756             }
5757         }
5758       else
5759         abort ();
5760
5761       if (! used_at)
5762         return;
5763
5764       break;
5765
5766     case M_LI:
5767     case M_LI_S:
5768       load_register (&icnt, treg, &imm_expr, 0);
5769       return;
5770
5771     case M_DLI:
5772       load_register (&icnt, treg, &imm_expr, 1);
5773       return;
5774
5775     case M_LI_SS:
5776       if (imm_expr.X_op == O_constant)
5777         {
5778           load_register (&icnt, AT, &imm_expr, 0);
5779           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5780                        "mtc1", "t,G", AT, treg);
5781           break;
5782         }
5783       else
5784         {
5785           assert (offset_expr.X_op == O_symbol
5786                   && strcmp (segment_name (S_GET_SEGMENT
5787                                            (offset_expr.X_add_symbol)),
5788                              ".lit4") == 0
5789                   && offset_expr.X_add_number == 0);
5790           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5791                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
5792           return;
5793         }
5794
5795     case M_LI_D:
5796       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5797          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5798          order 32 bits of the value and the low order 32 bits are either
5799          zero or in OFFSET_EXPR.  */
5800       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5801         {
5802           if (HAVE_64BIT_GPRS)
5803             load_register (&icnt, treg, &imm_expr, 1);
5804           else
5805             {
5806               int hreg, lreg;
5807
5808               if (target_big_endian)
5809                 {
5810                   hreg = treg;
5811                   lreg = treg + 1;
5812                 }
5813               else
5814                 {
5815                   hreg = treg + 1;
5816                   lreg = treg;
5817                 }
5818
5819               if (hreg <= 31)
5820                 load_register (&icnt, hreg, &imm_expr, 0);
5821               if (lreg <= 31)
5822                 {
5823                   if (offset_expr.X_op == O_absent)
5824                     move_register (&icnt, lreg, 0);
5825                   else
5826                     {
5827                       assert (offset_expr.X_op == O_constant);
5828                       load_register (&icnt, lreg, &offset_expr, 0);
5829                     }
5830                 }
5831             }
5832           return;
5833         }
5834
5835       /* We know that sym is in the .rdata section.  First we get the
5836          upper 16 bits of the address.  */
5837       if (mips_pic == NO_PIC)
5838         {
5839           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5840         }
5841       else if (mips_pic == SVR4_PIC)
5842         {
5843           macro_build ((char *) NULL, &icnt, &offset_expr,
5844                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5845                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5846                        mips_gp_register);
5847         }
5848       else if (mips_pic == EMBEDDED_PIC)
5849         {
5850           /* For embedded PIC we pick up the entire address off $gp in
5851              a single instruction.  */
5852           macro_build ((char *) NULL, &icnt, &offset_expr,
5853                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5854                        mips_gp_register, (int) BFD_RELOC_GPREL16);
5855           offset_expr.X_op = O_constant;
5856           offset_expr.X_add_number = 0;
5857         }
5858       else
5859         abort ();
5860
5861       /* Now we load the register(s).  */
5862       if (HAVE_64BIT_GPRS)
5863         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5864                      treg, (int) BFD_RELOC_LO16, AT);
5865       else
5866         {
5867           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5868                        treg, (int) BFD_RELOC_LO16, AT);
5869           if (treg != RA)
5870             {
5871               /* FIXME: How in the world do we deal with the possible
5872                  overflow here?  */
5873               offset_expr.X_add_number += 4;
5874               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5875                            treg + 1, (int) BFD_RELOC_LO16, AT);
5876             }
5877         }
5878
5879       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5880          does not become a variant frag.  */
5881       frag_wane (frag_now);
5882       frag_new (0);
5883
5884       break;
5885
5886     case M_LI_DD:
5887       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5888          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5889          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5890          the value and the low order 32 bits are either zero or in
5891          OFFSET_EXPR.  */
5892       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5893         {
5894           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5895           if (HAVE_64BIT_FPRS)
5896             {
5897               assert (HAVE_64BIT_GPRS);
5898               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5899                            "dmtc1", "t,S", AT, treg);
5900             }
5901           else
5902             {
5903               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5904                            "mtc1", "t,G", AT, treg + 1);
5905               if (offset_expr.X_op == O_absent)
5906                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5907                              "mtc1", "t,G", 0, treg);
5908               else
5909                 {
5910                   assert (offset_expr.X_op == O_constant);
5911                   load_register (&icnt, AT, &offset_expr, 0);
5912                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5913                                "mtc1", "t,G", AT, treg);
5914                 }
5915             }
5916           break;
5917         }
5918
5919       assert (offset_expr.X_op == O_symbol
5920               && offset_expr.X_add_number == 0);
5921       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5922       if (strcmp (s, ".lit8") == 0)
5923         {
5924           if (mips_opts.isa != ISA_MIPS1)
5925             {
5926               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5927                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5928                            mips_gp_register);
5929               return;
5930             }
5931           breg = mips_gp_register;
5932           r = BFD_RELOC_MIPS_LITERAL;
5933           goto dob;
5934         }
5935       else
5936         {
5937           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5938           if (mips_pic == SVR4_PIC)
5939             macro_build ((char *) NULL, &icnt, &offset_expr,
5940                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5941                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5942                          mips_gp_register);
5943           else
5944             {
5945               /* FIXME: This won't work for a 64 bit address.  */
5946               macro_build_lui (NULL, &icnt, &offset_expr, AT);
5947             }
5948
5949           if (mips_opts.isa != ISA_MIPS1)
5950             {
5951               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5952                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5953
5954               /* To avoid confusion in tc_gen_reloc, we must ensure
5955                  that this does not become a variant frag.  */
5956               frag_wane (frag_now);
5957               frag_new (0);
5958
5959               break;
5960             }
5961           breg = AT;
5962           r = BFD_RELOC_LO16;
5963           goto dob;
5964         }
5965
5966     case M_L_DOB:
5967       if (mips_arch == CPU_R4650)
5968         {
5969           as_bad (_("opcode not supported on this processor"));
5970           return;
5971         }
5972       /* Even on a big endian machine $fn comes before $fn+1.  We have
5973          to adjust when loading from memory.  */
5974       r = BFD_RELOC_LO16;
5975     dob:
5976       assert (mips_opts.isa == ISA_MIPS1);
5977       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5978                    target_big_endian ? treg + 1 : treg,
5979                    (int) r, breg);
5980       /* FIXME: A possible overflow which I don't know how to deal
5981          with.  */
5982       offset_expr.X_add_number += 4;
5983       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5984                    target_big_endian ? treg : treg + 1,
5985                    (int) r, breg);
5986
5987       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5988          does not become a variant frag.  */
5989       frag_wane (frag_now);
5990       frag_new (0);
5991
5992       if (breg != AT)
5993         return;
5994       break;
5995
5996     case M_L_DAB:
5997       /*
5998        * The MIPS assembler seems to check for X_add_number not
5999        * being double aligned and generating:
6000        *        lui     at,%hi(foo+1)
6001        *        addu    at,at,v1
6002        *        addiu   at,at,%lo(foo+1)
6003        *        lwc1    f2,0(at)
6004        *        lwc1    f3,4(at)
6005        * But, the resulting address is the same after relocation so why
6006        * generate the extra instruction?
6007        */
6008       if (mips_arch == CPU_R4650)
6009         {
6010           as_bad (_("opcode not supported on this processor"));
6011           return;
6012         }
6013       /* Itbl support may require additional care here.  */
6014       coproc = 1;
6015       if (mips_opts.isa != ISA_MIPS1)
6016         {
6017           s = "ldc1";
6018           goto ld;
6019         }
6020
6021       s = "lwc1";
6022       fmt = "T,o(b)";
6023       goto ldd_std;
6024
6025     case M_S_DAB:
6026       if (mips_arch == CPU_R4650)
6027         {
6028           as_bad (_("opcode not supported on this processor"));
6029           return;
6030         }
6031
6032       if (mips_opts.isa != ISA_MIPS1)
6033         {
6034           s = "sdc1";
6035           goto st;
6036         }
6037
6038       s = "swc1";
6039       fmt = "T,o(b)";
6040       /* Itbl support may require additional care here.  */
6041       coproc = 1;
6042       goto ldd_std;
6043
6044     case M_LD_AB:
6045       if (HAVE_64BIT_GPRS)
6046         {
6047           s = "ld";
6048           goto ld;
6049         }
6050
6051       s = "lw";
6052       fmt = "t,o(b)";
6053       goto ldd_std;
6054
6055     case M_SD_AB:
6056       if (HAVE_64BIT_GPRS)
6057         {
6058           s = "sd";
6059           goto st;
6060         }
6061
6062       s = "sw";
6063       fmt = "t,o(b)";
6064
6065     ldd_std:
6066       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6067          loads for the case of doing a pair of loads to simulate an 'ld'.
6068          This is not currently done by the compiler, and assembly coders
6069          writing embedded-pic code can cope.  */
6070
6071       if (offset_expr.X_op != O_symbol
6072           && offset_expr.X_op != O_constant)
6073         {
6074           as_bad (_("expression too complex"));
6075           offset_expr.X_op = O_constant;
6076         }
6077
6078       /* Even on a big endian machine $fn comes before $fn+1.  We have
6079          to adjust when loading from memory.  We set coproc if we must
6080          load $fn+1 first.  */
6081       /* Itbl support may require additional care here.  */
6082       if (! target_big_endian)
6083         coproc = 0;
6084
6085       if (mips_pic == NO_PIC
6086           || offset_expr.X_op == O_constant)
6087         {
6088           char *p;
6089
6090           /* If this is a reference to a GP relative symbol, we want
6091                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6092                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6093              If we have a base register, we use this
6094                addu     $at,$breg,$gp
6095                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6096                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6097              If this is not a GP relative symbol, we want
6098                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6099                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6100                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6101              If there is a base register, we add it to $at after the
6102              lui instruction.  If there is a constant, we always use
6103              the last case.  */
6104           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6105               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6106             {
6107               p = NULL;
6108               used_at = 1;
6109             }
6110           else
6111             {
6112               int off;
6113
6114               if (breg == 0)
6115                 {
6116                   frag_grow (28);
6117                   tempreg = mips_gp_register;
6118                   off = 0;
6119                   used_at = 0;
6120                 }
6121               else
6122                 {
6123                   frag_grow (36);
6124                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6125                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6126                                "d,v,t", AT, breg, mips_gp_register);
6127                   tempreg = AT;
6128                   off = 4;
6129                   used_at = 1;
6130                 }
6131
6132               /* Itbl support may require additional care here.  */
6133               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6134                            coproc ? treg + 1 : treg,
6135                            (int) BFD_RELOC_GPREL16, tempreg);
6136               offset_expr.X_add_number += 4;
6137
6138               /* Set mips_optimize to 2 to avoid inserting an
6139                  undesired nop.  */
6140               hold_mips_optimize = mips_optimize;
6141               mips_optimize = 2;
6142               /* Itbl support may require additional care here.  */
6143               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6144                            coproc ? treg : treg + 1,
6145                            (int) BFD_RELOC_GPREL16, tempreg);
6146               mips_optimize = hold_mips_optimize;
6147
6148               p = frag_var (rs_machine_dependent, 12 + off, 0,
6149                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6150                                           used_at && mips_opts.noat),
6151                             offset_expr.X_add_symbol, 0, NULL);
6152
6153               /* We just generated two relocs.  When tc_gen_reloc
6154                  handles this case, it will skip the first reloc and
6155                  handle the second.  The second reloc already has an
6156                  extra addend of 4, which we added above.  We must
6157                  subtract it out, and then subtract another 4 to make
6158                  the first reloc come out right.  The second reloc
6159                  will come out right because we are going to add 4 to
6160                  offset_expr when we build its instruction below.
6161
6162                  If we have a symbol, then we don't want to include
6163                  the offset, because it will wind up being included
6164                  when we generate the reloc.  */
6165
6166               if (offset_expr.X_op == O_constant)
6167                 offset_expr.X_add_number -= 8;
6168               else
6169                 {
6170                   offset_expr.X_add_number = -4;
6171                   offset_expr.X_op = O_constant;
6172                 }
6173             }
6174           macro_build_lui (p, &icnt, &offset_expr, AT);
6175           if (p != NULL)
6176             p += 4;
6177           if (breg != 0)
6178             {
6179               macro_build (p, &icnt, (expressionS *) NULL,
6180                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6181                            "d,v,t", AT, breg, AT);
6182               if (p != NULL)
6183                 p += 4;
6184             }
6185           /* Itbl support may require additional care here.  */
6186           macro_build (p, &icnt, &offset_expr, s, fmt,
6187                        coproc ? treg + 1 : treg,
6188                        (int) BFD_RELOC_LO16, AT);
6189           if (p != NULL)
6190             p += 4;
6191           /* FIXME: How do we handle overflow here?  */
6192           offset_expr.X_add_number += 4;
6193           /* Itbl support may require additional care here.  */
6194           macro_build (p, &icnt, &offset_expr, s, fmt,
6195                        coproc ? treg : treg + 1,
6196                        (int) BFD_RELOC_LO16, AT);
6197         }
6198       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6199         {
6200           int off;
6201
6202           /* If this is a reference to an external symbol, we want
6203                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6204                nop
6205                <op>     $treg,0($at)
6206                <op>     $treg+1,4($at)
6207              Otherwise we want
6208                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6209                nop
6210                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6211                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6212              If there is a base register we add it to $at before the
6213              lwc1 instructions.  If there is a constant we include it
6214              in the lwc1 instructions.  */
6215           used_at = 1;
6216           expr1.X_add_number = offset_expr.X_add_number;
6217           offset_expr.X_add_number = 0;
6218           if (expr1.X_add_number < -0x8000
6219               || expr1.X_add_number >= 0x8000 - 4)
6220             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6221           if (breg == 0)
6222             off = 0;
6223           else
6224             off = 4;
6225           frag_grow (24 + off);
6226           macro_build ((char *) NULL, &icnt, &offset_expr,
6227                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6228                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6229           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6230           if (breg != 0)
6231             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6232                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6233                          "d,v,t", AT, breg, AT);
6234           /* Itbl support may require additional care here.  */
6235           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6236                        coproc ? treg + 1 : treg,
6237                        (int) BFD_RELOC_LO16, AT);
6238           expr1.X_add_number += 4;
6239
6240           /* Set mips_optimize to 2 to avoid inserting an undesired
6241              nop.  */
6242           hold_mips_optimize = mips_optimize;
6243           mips_optimize = 2;
6244           /* Itbl support may require additional care here.  */
6245           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6246                        coproc ? treg : treg + 1,
6247                        (int) BFD_RELOC_LO16, AT);
6248           mips_optimize = hold_mips_optimize;
6249
6250           (void) frag_var (rs_machine_dependent, 0, 0,
6251                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6252                            offset_expr.X_add_symbol, 0, NULL);
6253         }
6254       else if (mips_pic == SVR4_PIC)
6255         {
6256           int gpdel, off;
6257           char *p;
6258
6259           /* If this is a reference to an external symbol, we want
6260                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6261                addu     $at,$at,$gp
6262                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6263                nop
6264                <op>     $treg,0($at)
6265                <op>     $treg+1,4($at)
6266              Otherwise we want
6267                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6268                nop
6269                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6270                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6271              If there is a base register we add it to $at before the
6272              lwc1 instructions.  If there is a constant we include it
6273              in the lwc1 instructions.  */
6274           used_at = 1;
6275           expr1.X_add_number = offset_expr.X_add_number;
6276           offset_expr.X_add_number = 0;
6277           if (expr1.X_add_number < -0x8000
6278               || expr1.X_add_number >= 0x8000 - 4)
6279             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6280           if (reg_needs_delay (mips_gp_register))
6281             gpdel = 4;
6282           else
6283             gpdel = 0;
6284           if (breg == 0)
6285             off = 0;
6286           else
6287             off = 4;
6288           frag_grow (56);
6289           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6290                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6291           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6292                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6293                        "d,v,t", AT, AT, mips_gp_register);
6294           macro_build ((char *) NULL, &icnt, &offset_expr,
6295                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6296                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6297           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6298           if (breg != 0)
6299             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6300                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6301                          "d,v,t", AT, breg, AT);
6302           /* Itbl support may require additional care here.  */
6303           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6304                        coproc ? treg + 1 : treg,
6305                        (int) BFD_RELOC_LO16, AT);
6306           expr1.X_add_number += 4;
6307
6308           /* Set mips_optimize to 2 to avoid inserting an undesired
6309              nop.  */
6310           hold_mips_optimize = mips_optimize;
6311           mips_optimize = 2;
6312           /* Itbl support may require additional care here.  */
6313           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6314                        coproc ? treg : treg + 1,
6315                        (int) BFD_RELOC_LO16, AT);
6316           mips_optimize = hold_mips_optimize;
6317           expr1.X_add_number -= 4;
6318
6319           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6320                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6321                                       8 + gpdel + off, 1, 0),
6322                         offset_expr.X_add_symbol, 0, NULL);
6323           if (gpdel > 0)
6324             {
6325               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6326               p += 4;
6327             }
6328           macro_build (p, &icnt, &offset_expr,
6329                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6330                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6331                        mips_gp_register);
6332           p += 4;
6333           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6334           p += 4;
6335           if (breg != 0)
6336             {
6337               macro_build (p, &icnt, (expressionS *) NULL,
6338                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6339                            "d,v,t", AT, breg, AT);
6340               p += 4;
6341             }
6342           /* Itbl support may require additional care here.  */
6343           macro_build (p, &icnt, &expr1, s, fmt,
6344                        coproc ? treg + 1 : treg,
6345                        (int) BFD_RELOC_LO16, AT);
6346           p += 4;
6347           expr1.X_add_number += 4;
6348
6349           /* Set mips_optimize to 2 to avoid inserting an undesired
6350              nop.  */
6351           hold_mips_optimize = mips_optimize;
6352           mips_optimize = 2;
6353           /* Itbl support may require additional care here.  */
6354           macro_build (p, &icnt, &expr1, s, fmt,
6355                        coproc ? treg : treg + 1,
6356                        (int) BFD_RELOC_LO16, AT);
6357           mips_optimize = hold_mips_optimize;
6358         }
6359       else if (mips_pic == EMBEDDED_PIC)
6360         {
6361           /* If there is no base register, we use
6362                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6363                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6364              If we have a base register, we use
6365                addu     $at,$breg,$gp
6366                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6367                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6368              */
6369           if (breg == 0)
6370             {
6371               tempreg = mips_gp_register;
6372               used_at = 0;
6373             }
6374           else
6375             {
6376               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6377                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6378                            "d,v,t", AT, breg, mips_gp_register);
6379               tempreg = AT;
6380               used_at = 1;
6381             }
6382
6383           /* Itbl support may require additional care here.  */
6384           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6385                        coproc ? treg + 1 : treg,
6386                        (int) BFD_RELOC_GPREL16, tempreg);
6387           offset_expr.X_add_number += 4;
6388           /* Itbl support may require additional care here.  */
6389           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6390                        coproc ? treg : treg + 1,
6391                        (int) BFD_RELOC_GPREL16, tempreg);
6392         }
6393       else
6394         abort ();
6395
6396       if (! used_at)
6397         return;
6398
6399       break;
6400
6401     case M_LD_OB:
6402       s = "lw";
6403       goto sd_ob;
6404     case M_SD_OB:
6405       s = "sw";
6406     sd_ob:
6407       assert (HAVE_32BIT_ADDRESSES);
6408       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6409                    (int) BFD_RELOC_LO16, breg);
6410       offset_expr.X_add_number += 4;
6411       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6412                    (int) BFD_RELOC_LO16, breg);
6413       return;
6414
6415    /* New code added to support COPZ instructions.
6416       This code builds table entries out of the macros in mip_opcodes.
6417       R4000 uses interlocks to handle coproc delays.
6418       Other chips (like the R3000) require nops to be inserted for delays.
6419
6420       FIXME: Currently, we require that the user handle delays.
6421       In order to fill delay slots for non-interlocked chips,
6422       we must have a way to specify delays based on the coprocessor.
6423       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6424       What are the side-effects of the cop instruction?
6425       What cache support might we have and what are its effects?
6426       Both coprocessor & memory require delays. how long???
6427       What registers are read/set/modified?
6428
6429       If an itbl is provided to interpret cop instructions,
6430       this knowledge can be encoded in the itbl spec.  */
6431
6432     case M_COP0:
6433       s = "c0";
6434       goto copz;
6435     case M_COP1:
6436       s = "c1";
6437       goto copz;
6438     case M_COP2:
6439       s = "c2";
6440       goto copz;
6441     case M_COP3:
6442       s = "c3";
6443     copz:
6444       /* For now we just do C (same as Cz).  The parameter will be
6445          stored in insn_opcode by mips_ip.  */
6446       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6447                    ip->insn_opcode);
6448       return;
6449
6450     case M_MOVE:
6451       move_register (&icnt, dreg, sreg);
6452       return;
6453
6454 #ifdef LOSING_COMPILER
6455     default:
6456       /* Try and see if this is a new itbl instruction.
6457          This code builds table entries out of the macros in mip_opcodes.
6458          FIXME: For now we just assemble the expression and pass it's
6459          value along as a 32-bit immediate.
6460          We may want to have the assembler assemble this value,
6461          so that we gain the assembler's knowledge of delay slots,
6462          symbols, etc.
6463          Would it be more efficient to use mask (id) here? */
6464       if (itbl_have_entries
6465           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6466         {
6467           s = ip->insn_mo->name;
6468           s2 = "cop3";
6469           coproc = ITBL_DECODE_PNUM (immed_expr);;
6470           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6471           return;
6472         }
6473       macro2 (ip);
6474       return;
6475     }
6476   if (mips_opts.noat)
6477     as_warn (_("Macro used $at after \".set noat\""));
6478 }
6479
6480 static void
6481 macro2 (ip)
6482      struct mips_cl_insn *ip;
6483 {
6484   register int treg, sreg, dreg, breg;
6485   int tempreg;
6486   int mask;
6487   int icnt = 0;
6488   int used_at;
6489   expressionS expr1;
6490   const char *s;
6491   const char *s2;
6492   const char *fmt;
6493   int likely = 0;
6494   int dbl = 0;
6495   int coproc = 0;
6496   int lr = 0;
6497   int imm = 0;
6498   int off;
6499   offsetT maxnum;
6500   bfd_reloc_code_real_type r;
6501   char *p;
6502
6503   treg = (ip->insn_opcode >> 16) & 0x1f;
6504   dreg = (ip->insn_opcode >> 11) & 0x1f;
6505   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6506   mask = ip->insn_mo->mask;
6507
6508   expr1.X_op = O_constant;
6509   expr1.X_op_symbol = NULL;
6510   expr1.X_add_symbol = NULL;
6511   expr1.X_add_number = 1;
6512
6513   switch (mask)
6514     {
6515 #endif /* LOSING_COMPILER */
6516
6517     case M_DMUL:
6518       dbl = 1;
6519     case M_MUL:
6520       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6521                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6522       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6523                    dreg);
6524       return;
6525
6526     case M_DMUL_I:
6527       dbl = 1;
6528     case M_MUL_I:
6529       /* The MIPS assembler some times generates shifts and adds.  I'm
6530          not trying to be that fancy. GCC should do this for us
6531          anyway.  */
6532       load_register (&icnt, AT, &imm_expr, dbl);
6533       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6534                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6535       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6536                    dreg);
6537       break;
6538
6539     case M_DMULO_I:
6540       dbl = 1;
6541     case M_MULO_I:
6542       imm = 1;
6543       goto do_mulo;
6544
6545     case M_DMULO:
6546       dbl = 1;
6547     case M_MULO:
6548     do_mulo:
6549       mips_emit_delays (true);
6550       ++mips_opts.noreorder;
6551       mips_any_noreorder = 1;
6552       if (imm)
6553         load_register (&icnt, AT, &imm_expr, dbl);
6554       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6555                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6556       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6557                    dreg);
6558       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6559                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6560       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6561                    AT);
6562       if (mips_trap)
6563         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6564                      "s,t,q", dreg, AT, 6);
6565       else
6566         {
6567           expr1.X_add_number = 8;
6568           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6569                        AT);
6570           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6571                        0);
6572           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6573                        "c", 6);
6574         }
6575       --mips_opts.noreorder;
6576       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6577       break;
6578
6579     case M_DMULOU_I:
6580       dbl = 1;
6581     case M_MULOU_I:
6582       imm = 1;
6583       goto do_mulou;
6584
6585     case M_DMULOU:
6586       dbl = 1;
6587     case M_MULOU:
6588     do_mulou:
6589       mips_emit_delays (true);
6590       ++mips_opts.noreorder;
6591       mips_any_noreorder = 1;
6592       if (imm)
6593         load_register (&icnt, AT, &imm_expr, dbl);
6594       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6595                    dbl ? "dmultu" : "multu",
6596                    "s,t", sreg, imm ? AT : treg);
6597       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6598                    AT);
6599       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6600                    dreg);
6601       if (mips_trap)
6602         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6603                      "s,t,q", AT, 0, 6);
6604       else
6605         {
6606           expr1.X_add_number = 8;
6607           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6608           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6609                        0);
6610           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6611                        "c", 6);
6612         }
6613       --mips_opts.noreorder;
6614       break;
6615
6616     case M_DROL:
6617       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6618                    "d,v,t", AT, 0, treg);
6619       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6620                    "d,t,s", AT, sreg, AT);
6621       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6622                    "d,t,s", dreg, sreg, treg);
6623       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6624                    "d,v,t", dreg, dreg, AT);
6625       break;
6626
6627     case M_ROL:
6628       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6629                    "d,v,t", AT, 0, treg);
6630       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6631                    "d,t,s", AT, sreg, AT);
6632       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6633                    "d,t,s", dreg, sreg, treg);
6634       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6635                    "d,v,t", dreg, dreg, AT);
6636       break;
6637
6638     case M_DROL_I:
6639       {
6640         unsigned int rot;
6641
6642         if (imm_expr.X_op != O_constant)
6643           as_bad (_("rotate count too large"));
6644         rot = imm_expr.X_add_number & 0x3f;
6645         if (rot == 0)
6646           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6647                        "d,w,<", dreg, sreg, 0);
6648         else
6649           {
6650             char *l, *r;
6651
6652             l = (rot < 0x20) ? "dsll" : "dsll32";
6653             r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6654             rot &= 0x1f;
6655             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6656                          "d,w,<", AT, sreg, rot);
6657             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6658                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6659             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6660                          "d,v,t", dreg, dreg, AT);
6661           }
6662       }
6663       break;
6664
6665     case M_ROL_I:
6666       {
6667         unsigned int rot;
6668
6669         if (imm_expr.X_op != O_constant)
6670           as_bad (_("rotate count too large"));
6671         rot = imm_expr.X_add_number & 0x1f;
6672         if (rot == 0)
6673           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6674                        "d,w,<", dreg, sreg, 0);
6675         else
6676           {
6677             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6678                          "d,w,<", AT, sreg, rot);
6679             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6680                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6681             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6682                          "d,v,t", dreg, dreg, AT);
6683           }
6684       }
6685       break;
6686
6687     case M_DROR:
6688       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6689                    "d,v,t", AT, 0, treg);
6690       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6691                    "d,t,s", AT, sreg, AT);
6692       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6693                    "d,t,s", dreg, sreg, treg);
6694       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6695                    "d,v,t", dreg, dreg, AT);
6696       break;
6697
6698     case M_ROR:
6699       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6700                    "d,v,t", AT, 0, treg);
6701       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6702                    "d,t,s", AT, sreg, AT);
6703       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6704                    "d,t,s", dreg, sreg, treg);
6705       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6706                    "d,v,t", dreg, dreg, AT);
6707       break;
6708
6709     case M_DROR_I:
6710       {
6711         unsigned int rot;
6712
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             r = (rot < 0x20) ? "dsrl" : "dsrl32";
6724             l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6725             rot &= 0x1f;
6726             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6727                          "d,w,<", AT, sreg, rot);
6728             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
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_ROR_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, "srl",
6749                          "d,w,<", AT, sreg, rot);
6750             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
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_S_DOB:
6759       if (mips_arch == CPU_R4650)
6760         {
6761           as_bad (_("opcode not supported on this processor"));
6762           return;
6763         }
6764       assert (mips_opts.isa == ISA_MIPS1);
6765       /* Even on a big endian machine $fn comes before $fn+1.  We have
6766          to adjust when storing to memory.  */
6767       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6768                    target_big_endian ? treg + 1 : treg,
6769                    (int) BFD_RELOC_LO16, breg);
6770       offset_expr.X_add_number += 4;
6771       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6772                    target_big_endian ? treg : treg + 1,
6773                    (int) BFD_RELOC_LO16, breg);
6774       return;
6775
6776     case M_SEQ:
6777       if (sreg == 0)
6778         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6779                      treg, (int) BFD_RELOC_LO16);
6780       else if (treg == 0)
6781         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6782                      sreg, (int) BFD_RELOC_LO16);
6783       else
6784         {
6785           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6786                        "d,v,t", dreg, sreg, treg);
6787           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6788                        dreg, (int) BFD_RELOC_LO16);
6789         }
6790       return;
6791
6792     case M_SEQ_I:
6793       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6794         {
6795           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6796                        sreg, (int) BFD_RELOC_LO16);
6797           return;
6798         }
6799       if (sreg == 0)
6800         {
6801           as_warn (_("Instruction %s: result is always false"),
6802                    ip->insn_mo->name);
6803           move_register (&icnt, dreg, 0);
6804           return;
6805         }
6806       if (imm_expr.X_op == O_constant
6807           && imm_expr.X_add_number >= 0
6808           && imm_expr.X_add_number < 0x10000)
6809         {
6810           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6811                        sreg, (int) BFD_RELOC_LO16);
6812           used_at = 0;
6813         }
6814       else if (imm_expr.X_op == O_constant
6815                && imm_expr.X_add_number > -0x8000
6816                && imm_expr.X_add_number < 0)
6817         {
6818           imm_expr.X_add_number = -imm_expr.X_add_number;
6819           macro_build ((char *) NULL, &icnt, &imm_expr,
6820                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6821                        "t,r,j", dreg, sreg,
6822                        (int) BFD_RELOC_LO16);
6823           used_at = 0;
6824         }
6825       else
6826         {
6827           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6828           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6829                        "d,v,t", dreg, sreg, AT);
6830           used_at = 1;
6831         }
6832       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6833                    (int) BFD_RELOC_LO16);
6834       if (used_at)
6835         break;
6836       return;
6837
6838     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6839       s = "slt";
6840       goto sge;
6841     case M_SGEU:
6842       s = "sltu";
6843     sge:
6844       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6845                    dreg, sreg, treg);
6846       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6847                    (int) BFD_RELOC_LO16);
6848       return;
6849
6850     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6851     case M_SGEU_I:
6852       if (imm_expr.X_op == O_constant
6853           && imm_expr.X_add_number >= -0x8000
6854           && imm_expr.X_add_number < 0x8000)
6855         {
6856           macro_build ((char *) NULL, &icnt, &imm_expr,
6857                        mask == M_SGE_I ? "slti" : "sltiu",
6858                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6859           used_at = 0;
6860         }
6861       else
6862         {
6863           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6864           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6865                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6866                        AT);
6867           used_at = 1;
6868         }
6869       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6870                    (int) BFD_RELOC_LO16);
6871       if (used_at)
6872         break;
6873       return;
6874
6875     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6876       s = "slt";
6877       goto sgt;
6878     case M_SGTU:
6879       s = "sltu";
6880     sgt:
6881       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6882                    dreg, treg, sreg);
6883       return;
6884
6885     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6886       s = "slt";
6887       goto sgti;
6888     case M_SGTU_I:
6889       s = "sltu";
6890     sgti:
6891       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6892       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6893                    dreg, AT, sreg);
6894       break;
6895
6896     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6897       s = "slt";
6898       goto sle;
6899     case M_SLEU:
6900       s = "sltu";
6901     sle:
6902       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6903                    dreg, treg, sreg);
6904       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6905                    (int) BFD_RELOC_LO16);
6906       return;
6907
6908     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6909       s = "slt";
6910       goto slei;
6911     case M_SLEU_I:
6912       s = "sltu";
6913     slei:
6914       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6915       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6916                    dreg, AT, sreg);
6917       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6918                    (int) BFD_RELOC_LO16);
6919       break;
6920
6921     case M_SLT_I:
6922       if (imm_expr.X_op == O_constant
6923           && imm_expr.X_add_number >= -0x8000
6924           && imm_expr.X_add_number < 0x8000)
6925         {
6926           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6927                        dreg, sreg, (int) BFD_RELOC_LO16);
6928           return;
6929         }
6930       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6931       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6932                    dreg, sreg, AT);
6933       break;
6934
6935     case M_SLTU_I:
6936       if (imm_expr.X_op == O_constant
6937           && imm_expr.X_add_number >= -0x8000
6938           && imm_expr.X_add_number < 0x8000)
6939         {
6940           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6941                        dreg, sreg, (int) BFD_RELOC_LO16);
6942           return;
6943         }
6944       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6945       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6946                    "d,v,t", dreg, sreg, AT);
6947       break;
6948
6949     case M_SNE:
6950       if (sreg == 0)
6951         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6952                      "d,v,t", dreg, 0, treg);
6953       else if (treg == 0)
6954         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6955                      "d,v,t", dreg, 0, sreg);
6956       else
6957         {
6958           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6959                        "d,v,t", dreg, sreg, treg);
6960           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6961                        "d,v,t", dreg, 0, dreg);
6962         }
6963       return;
6964
6965     case M_SNE_I:
6966       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6967         {
6968           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6969                        "d,v,t", dreg, 0, sreg);
6970           return;
6971         }
6972       if (sreg == 0)
6973         {
6974           as_warn (_("Instruction %s: result is always true"),
6975                    ip->insn_mo->name);
6976           macro_build ((char *) NULL, &icnt, &expr1,
6977                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6978                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6979           return;
6980         }
6981       if (imm_expr.X_op == O_constant
6982           && imm_expr.X_add_number >= 0
6983           && imm_expr.X_add_number < 0x10000)
6984         {
6985           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6986                        dreg, sreg, (int) BFD_RELOC_LO16);
6987           used_at = 0;
6988         }
6989       else if (imm_expr.X_op == O_constant
6990                && imm_expr.X_add_number > -0x8000
6991                && imm_expr.X_add_number < 0)
6992         {
6993           imm_expr.X_add_number = -imm_expr.X_add_number;
6994           macro_build ((char *) NULL, &icnt, &imm_expr,
6995                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6996                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6997           used_at = 0;
6998         }
6999       else
7000         {
7001           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7002           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7003                        "d,v,t", dreg, sreg, AT);
7004           used_at = 1;
7005         }
7006       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7007                    "d,v,t", dreg, 0, dreg);
7008       if (used_at)
7009         break;
7010       return;
7011
7012     case M_DSUB_I:
7013       dbl = 1;
7014     case M_SUB_I:
7015       if (imm_expr.X_op == O_constant
7016           && imm_expr.X_add_number > -0x8000
7017           && imm_expr.X_add_number <= 0x8000)
7018         {
7019           imm_expr.X_add_number = -imm_expr.X_add_number;
7020           macro_build ((char *) NULL, &icnt, &imm_expr,
7021                        dbl ? "daddi" : "addi",
7022                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7023           return;
7024         }
7025       load_register (&icnt, AT, &imm_expr, dbl);
7026       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7027                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7028       break;
7029
7030     case M_DSUBU_I:
7031       dbl = 1;
7032     case M_SUBU_I:
7033       if (imm_expr.X_op == O_constant
7034           && imm_expr.X_add_number > -0x8000
7035           && imm_expr.X_add_number <= 0x8000)
7036         {
7037           imm_expr.X_add_number = -imm_expr.X_add_number;
7038           macro_build ((char *) NULL, &icnt, &imm_expr,
7039                        dbl ? "daddiu" : "addiu",
7040                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7041           return;
7042         }
7043       load_register (&icnt, AT, &imm_expr, dbl);
7044       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7045                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7046       break;
7047
7048     case M_TEQ_I:
7049       s = "teq";
7050       goto trap;
7051     case M_TGE_I:
7052       s = "tge";
7053       goto trap;
7054     case M_TGEU_I:
7055       s = "tgeu";
7056       goto trap;
7057     case M_TLT_I:
7058       s = "tlt";
7059       goto trap;
7060     case M_TLTU_I:
7061       s = "tltu";
7062       goto trap;
7063     case M_TNE_I:
7064       s = "tne";
7065     trap:
7066       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7067       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7068                    AT);
7069       break;
7070
7071     case M_TRUNCWS:
7072     case M_TRUNCWD:
7073       assert (mips_opts.isa == ISA_MIPS1);
7074       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7075       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7076
7077       /*
7078        * Is the double cfc1 instruction a bug in the mips assembler;
7079        * or is there a reason for it?
7080        */
7081       mips_emit_delays (true);
7082       ++mips_opts.noreorder;
7083       mips_any_noreorder = 1;
7084       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7085                    treg, RA);
7086       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7087                    treg, RA);
7088       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7089       expr1.X_add_number = 3;
7090       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7091                    (int) BFD_RELOC_LO16);
7092       expr1.X_add_number = 2;
7093       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7094                      (int) BFD_RELOC_LO16);
7095       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7096                    AT, RA);
7097       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7098       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7099               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7100       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7101                    treg, RA);
7102       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7103       --mips_opts.noreorder;
7104       break;
7105
7106     case M_ULH:
7107       s = "lb";
7108       goto ulh;
7109     case M_ULHU:
7110       s = "lbu";
7111     ulh:
7112       if (offset_expr.X_add_number >= 0x7fff)
7113         as_bad (_("operand overflow"));
7114       /* avoid load delay */
7115       if (! target_big_endian)
7116         ++offset_expr.X_add_number;
7117       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7118                    (int) BFD_RELOC_LO16, breg);
7119       if (! target_big_endian)
7120         --offset_expr.X_add_number;
7121       else
7122         ++offset_expr.X_add_number;
7123       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7124                    (int) BFD_RELOC_LO16, breg);
7125       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7126                    treg, treg, 8);
7127       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7128                    treg, treg, AT);
7129       break;
7130
7131     case M_ULD:
7132       s = "ldl";
7133       s2 = "ldr";
7134       off = 7;
7135       goto ulw;
7136     case M_ULW:
7137       s = "lwl";
7138       s2 = "lwr";
7139       off = 3;
7140     ulw:
7141       if (offset_expr.X_add_number >= 0x8000 - off)
7142         as_bad (_("operand overflow"));
7143       if (! target_big_endian)
7144         offset_expr.X_add_number += off;
7145       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7146                    (int) BFD_RELOC_LO16, breg);
7147       if (! target_big_endian)
7148         offset_expr.X_add_number -= off;
7149       else
7150         offset_expr.X_add_number += off;
7151       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7152                    (int) BFD_RELOC_LO16, breg);
7153       return;
7154
7155     case M_ULD_A:
7156       s = "ldl";
7157       s2 = "ldr";
7158       off = 7;
7159       goto ulwa;
7160     case M_ULW_A:
7161       s = "lwl";
7162       s2 = "lwr";
7163       off = 3;
7164     ulwa:
7165       used_at = 1;
7166       load_address (&icnt, AT, &offset_expr, &used_at);
7167       if (breg != 0)
7168         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7169                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7170                      "d,v,t", AT, AT, breg);
7171       if (! target_big_endian)
7172         expr1.X_add_number = off;
7173       else
7174         expr1.X_add_number = 0;
7175       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7176                    (int) BFD_RELOC_LO16, AT);
7177       if (! target_big_endian)
7178         expr1.X_add_number = 0;
7179       else
7180         expr1.X_add_number = off;
7181       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7182                    (int) BFD_RELOC_LO16, AT);
7183       break;
7184
7185     case M_ULH_A:
7186     case M_ULHU_A:
7187       used_at = 1;
7188       load_address (&icnt, AT, &offset_expr, &used_at);
7189       if (breg != 0)
7190         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7191                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7192                      "d,v,t", AT, AT, breg);
7193       if (target_big_endian)
7194         expr1.X_add_number = 0;
7195       macro_build ((char *) NULL, &icnt, &expr1,
7196                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7197                    (int) BFD_RELOC_LO16, AT);
7198       if (target_big_endian)
7199         expr1.X_add_number = 1;
7200       else
7201         expr1.X_add_number = 0;
7202       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7203                    (int) BFD_RELOC_LO16, AT);
7204       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7205                    treg, treg, 8);
7206       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7207                    treg, treg, AT);
7208       break;
7209
7210     case M_USH:
7211       if (offset_expr.X_add_number >= 0x7fff)
7212         as_bad (_("operand overflow"));
7213       if (target_big_endian)
7214         ++offset_expr.X_add_number;
7215       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7216                    (int) BFD_RELOC_LO16, breg);
7217       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7218                    AT, treg, 8);
7219       if (target_big_endian)
7220         --offset_expr.X_add_number;
7221       else
7222         ++offset_expr.X_add_number;
7223       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7224                    (int) BFD_RELOC_LO16, breg);
7225       break;
7226
7227     case M_USD:
7228       s = "sdl";
7229       s2 = "sdr";
7230       off = 7;
7231       goto usw;
7232     case M_USW:
7233       s = "swl";
7234       s2 = "swr";
7235       off = 3;
7236     usw:
7237       if (offset_expr.X_add_number >= 0x8000 - off)
7238         as_bad (_("operand overflow"));
7239       if (! target_big_endian)
7240         offset_expr.X_add_number += off;
7241       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7242                    (int) BFD_RELOC_LO16, breg);
7243       if (! target_big_endian)
7244         offset_expr.X_add_number -= off;
7245       else
7246         offset_expr.X_add_number += off;
7247       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7248                    (int) BFD_RELOC_LO16, breg);
7249       return;
7250
7251     case M_USD_A:
7252       s = "sdl";
7253       s2 = "sdr";
7254       off = 7;
7255       goto uswa;
7256     case M_USW_A:
7257       s = "swl";
7258       s2 = "swr";
7259       off = 3;
7260     uswa:
7261       used_at = 1;
7262       load_address (&icnt, AT, &offset_expr, &used_at);
7263       if (breg != 0)
7264         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7265                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7266                      "d,v,t", AT, AT, breg);
7267       if (! target_big_endian)
7268         expr1.X_add_number = off;
7269       else
7270         expr1.X_add_number = 0;
7271       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7272                    (int) BFD_RELOC_LO16, AT);
7273       if (! target_big_endian)
7274         expr1.X_add_number = 0;
7275       else
7276         expr1.X_add_number = off;
7277       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7278                    (int) BFD_RELOC_LO16, AT);
7279       break;
7280
7281     case M_USH_A:
7282       used_at = 1;
7283       load_address (&icnt, AT, &offset_expr, &used_at);
7284       if (breg != 0)
7285         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7286                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7287                      "d,v,t", AT, AT, breg);
7288       if (! target_big_endian)
7289         expr1.X_add_number = 0;
7290       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7291                    (int) BFD_RELOC_LO16, AT);
7292       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7293                    treg, treg, 8);
7294       if (! target_big_endian)
7295         expr1.X_add_number = 1;
7296       else
7297         expr1.X_add_number = 0;
7298       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7299                    (int) BFD_RELOC_LO16, AT);
7300       if (! target_big_endian)
7301         expr1.X_add_number = 0;
7302       else
7303         expr1.X_add_number = 1;
7304       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7305                    (int) BFD_RELOC_LO16, AT);
7306       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7307                    treg, treg, 8);
7308       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7309                    treg, treg, AT);
7310       break;
7311
7312     default:
7313       /* FIXME: Check if this is one of the itbl macros, since they
7314          are added dynamically.  */
7315       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7316       break;
7317     }
7318   if (mips_opts.noat)
7319     as_warn (_("Macro used $at after \".set noat\""));
7320 }
7321
7322 /* Implement macros in mips16 mode.  */
7323
7324 static void
7325 mips16_macro (ip)
7326      struct mips_cl_insn *ip;
7327 {
7328   int mask;
7329   int xreg, yreg, zreg, tmp;
7330   int icnt;
7331   expressionS expr1;
7332   int dbl;
7333   const char *s, *s2, *s3;
7334
7335   mask = ip->insn_mo->mask;
7336
7337   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7338   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7339   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7340
7341   icnt = 0;
7342
7343   expr1.X_op = O_constant;
7344   expr1.X_op_symbol = NULL;
7345   expr1.X_add_symbol = NULL;
7346   expr1.X_add_number = 1;
7347
7348   dbl = 0;
7349
7350   switch (mask)
7351     {
7352     default:
7353       internalError ();
7354
7355     case M_DDIV_3:
7356       dbl = 1;
7357     case M_DIV_3:
7358       s = "mflo";
7359       goto do_div3;
7360     case M_DREM_3:
7361       dbl = 1;
7362     case M_REM_3:
7363       s = "mfhi";
7364     do_div3:
7365       mips_emit_delays (true);
7366       ++mips_opts.noreorder;
7367       mips_any_noreorder = 1;
7368       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7369                    dbl ? "ddiv" : "div",
7370                    "0,x,y", xreg, yreg);
7371       expr1.X_add_number = 2;
7372       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7373       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7374                    7);
7375
7376       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7377          since that causes an overflow.  We should do that as well,
7378          but I don't see how to do the comparisons without a temporary
7379          register.  */
7380       --mips_opts.noreorder;
7381       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7382       break;
7383
7384     case M_DIVU_3:
7385       s = "divu";
7386       s2 = "mflo";
7387       goto do_divu3;
7388     case M_REMU_3:
7389       s = "divu";
7390       s2 = "mfhi";
7391       goto do_divu3;
7392     case M_DDIVU_3:
7393       s = "ddivu";
7394       s2 = "mflo";
7395       goto do_divu3;
7396     case M_DREMU_3:
7397       s = "ddivu";
7398       s2 = "mfhi";
7399     do_divu3:
7400       mips_emit_delays (true);
7401       ++mips_opts.noreorder;
7402       mips_any_noreorder = 1;
7403       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7404                    xreg, yreg);
7405       expr1.X_add_number = 2;
7406       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7407       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7408                    "6", 7);
7409       --mips_opts.noreorder;
7410       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7411       break;
7412
7413     case M_DMUL:
7414       dbl = 1;
7415     case M_MUL:
7416       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7417                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7418       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7419                    zreg);
7420       return;
7421
7422     case M_DSUBU_I:
7423       dbl = 1;
7424       goto do_subu;
7425     case M_SUBU_I:
7426     do_subu:
7427       if (imm_expr.X_op != O_constant)
7428         as_bad (_("Unsupported large constant"));
7429       imm_expr.X_add_number = -imm_expr.X_add_number;
7430       macro_build ((char *) NULL, &icnt, &imm_expr,
7431                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7432       break;
7433
7434     case M_SUBU_I_2:
7435       if (imm_expr.X_op != O_constant)
7436         as_bad (_("Unsupported large constant"));
7437       imm_expr.X_add_number = -imm_expr.X_add_number;
7438       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7439                    "x,k", xreg);
7440       break;
7441
7442     case M_DSUBU_I_2:
7443       if (imm_expr.X_op != O_constant)
7444         as_bad (_("Unsupported large constant"));
7445       imm_expr.X_add_number = -imm_expr.X_add_number;
7446       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7447                    "y,j", yreg);
7448       break;
7449
7450     case M_BEQ:
7451       s = "cmp";
7452       s2 = "bteqz";
7453       goto do_branch;
7454     case M_BNE:
7455       s = "cmp";
7456       s2 = "btnez";
7457       goto do_branch;
7458     case M_BLT:
7459       s = "slt";
7460       s2 = "btnez";
7461       goto do_branch;
7462     case M_BLTU:
7463       s = "sltu";
7464       s2 = "btnez";
7465       goto do_branch;
7466     case M_BLE:
7467       s = "slt";
7468       s2 = "bteqz";
7469       goto do_reverse_branch;
7470     case M_BLEU:
7471       s = "sltu";
7472       s2 = "bteqz";
7473       goto do_reverse_branch;
7474     case M_BGE:
7475       s = "slt";
7476       s2 = "bteqz";
7477       goto do_branch;
7478     case M_BGEU:
7479       s = "sltu";
7480       s2 = "bteqz";
7481       goto do_branch;
7482     case M_BGT:
7483       s = "slt";
7484       s2 = "btnez";
7485       goto do_reverse_branch;
7486     case M_BGTU:
7487       s = "sltu";
7488       s2 = "btnez";
7489
7490     do_reverse_branch:
7491       tmp = xreg;
7492       xreg = yreg;
7493       yreg = tmp;
7494
7495     do_branch:
7496       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7497                    xreg, yreg);
7498       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7499       break;
7500
7501     case M_BEQ_I:
7502       s = "cmpi";
7503       s2 = "bteqz";
7504       s3 = "x,U";
7505       goto do_branch_i;
7506     case M_BNE_I:
7507       s = "cmpi";
7508       s2 = "btnez";
7509       s3 = "x,U";
7510       goto do_branch_i;
7511     case M_BLT_I:
7512       s = "slti";
7513       s2 = "btnez";
7514       s3 = "x,8";
7515       goto do_branch_i;
7516     case M_BLTU_I:
7517       s = "sltiu";
7518       s2 = "btnez";
7519       s3 = "x,8";
7520       goto do_branch_i;
7521     case M_BLE_I:
7522       s = "slti";
7523       s2 = "btnez";
7524       s3 = "x,8";
7525       goto do_addone_branch_i;
7526     case M_BLEU_I:
7527       s = "sltiu";
7528       s2 = "btnez";
7529       s3 = "x,8";
7530       goto do_addone_branch_i;
7531     case M_BGE_I:
7532       s = "slti";
7533       s2 = "bteqz";
7534       s3 = "x,8";
7535       goto do_branch_i;
7536     case M_BGEU_I:
7537       s = "sltiu";
7538       s2 = "bteqz";
7539       s3 = "x,8";
7540       goto do_branch_i;
7541     case M_BGT_I:
7542       s = "slti";
7543       s2 = "bteqz";
7544       s3 = "x,8";
7545       goto do_addone_branch_i;
7546     case M_BGTU_I:
7547       s = "sltiu";
7548       s2 = "bteqz";
7549       s3 = "x,8";
7550
7551     do_addone_branch_i:
7552       if (imm_expr.X_op != O_constant)
7553         as_bad (_("Unsupported large constant"));
7554       ++imm_expr.X_add_number;
7555
7556     do_branch_i:
7557       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7558       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7559       break;
7560
7561     case M_ABS:
7562       expr1.X_add_number = 0;
7563       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7564       if (xreg != yreg)
7565         move_register (&icnt, xreg, yreg);
7566       expr1.X_add_number = 2;
7567       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7568       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7569                    "neg", "x,w", xreg, xreg);
7570     }
7571 }
7572
7573 /* For consistency checking, verify that all bits are specified either
7574    by the match/mask part of the instruction definition, or by the
7575    operand list.  */
7576 static int
7577 validate_mips_insn (opc)
7578      const struct mips_opcode *opc;
7579 {
7580   const char *p = opc->args;
7581   char c;
7582   unsigned long used_bits = opc->mask;
7583
7584   if ((used_bits & opc->match) != opc->match)
7585     {
7586       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7587               opc->name, opc->args);
7588       return 0;
7589     }
7590 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7591   while (*p)
7592     switch (c = *p++)
7593       {
7594       case ',': break;
7595       case '(': break;
7596       case ')': break;
7597       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7598       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7599       case 'A': break;
7600       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7601       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7602       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7603       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7604       case 'F': break;
7605       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7606       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7607       case 'I': break;
7608       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7609       case 'L': break;
7610       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7611       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7612       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7613       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7614                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7615       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7616       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7617       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7618       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7619       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7620       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7621       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7622       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7623       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7624       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7625       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7626       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7627       case 'f': break;
7628       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7629       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7630       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7631       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7632       case 'l': break;
7633       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7634       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7635       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7636       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7637       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7638       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7639       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7640       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7641       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7642       case 'x': break;
7643       case 'z': break;
7644       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7645       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7646                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7647       default:
7648         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7649                 c, opc->name, opc->args);
7650         return 0;
7651       }
7652 #undef USE_BITS
7653   if (used_bits != 0xffffffff)
7654     {
7655       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7656               ~used_bits & 0xffffffff, opc->name, opc->args);
7657       return 0;
7658     }
7659   return 1;
7660 }
7661
7662 /* This routine assembles an instruction into its binary format.  As a
7663    side effect, it sets one of the global variables imm_reloc or
7664    offset_reloc to the type of relocation to do if one of the operands
7665    is an address expression.  */
7666
7667 static void
7668 mips_ip (str, ip)
7669      char *str;
7670      struct mips_cl_insn *ip;
7671 {
7672   char *s;
7673   const char *args;
7674   char c = 0;
7675   struct mips_opcode *insn;
7676   char *argsStart;
7677   unsigned int regno;
7678   unsigned int lastregno = 0;
7679   char *s_reset;
7680   char save_c = 0;
7681
7682   insn_error = NULL;
7683
7684   /* If the instruction contains a '.', we first try to match an instruction
7685      including the '.'.  Then we try again without the '.'.  */
7686   insn = NULL;
7687   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7688     continue;
7689
7690   /* If we stopped on whitespace, then replace the whitespace with null for
7691      the call to hash_find.  Save the character we replaced just in case we
7692      have to re-parse the instruction.  */
7693   if (ISSPACE (*s))
7694     {
7695       save_c = *s;
7696       *s++ = '\0';
7697     }
7698
7699   insn = (struct mips_opcode *) hash_find (op_hash, str);
7700
7701   /* If we didn't find the instruction in the opcode table, try again, but
7702      this time with just the instruction up to, but not including the
7703      first '.'.  */
7704   if (insn == NULL)
7705     {
7706       /* Restore the character we overwrite above (if any).  */
7707       if (save_c)
7708         *(--s) = save_c;
7709
7710       /* Scan up to the first '.' or whitespace.  */
7711       for (s = str;
7712            *s != '\0' && *s != '.' && !ISSPACE (*s);
7713            ++s)
7714         continue;
7715
7716       /* If we did not find a '.', then we can quit now.  */
7717       if (*s != '.')
7718         {
7719           insn_error = "unrecognized opcode";
7720           return;
7721         }
7722
7723       /* Lookup the instruction in the hash table.  */
7724       *s++ = '\0';
7725       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7726         {
7727           insn_error = "unrecognized opcode";
7728           return;
7729         }
7730     }
7731
7732   argsStart = s;
7733   for (;;)
7734     {
7735       boolean ok;
7736
7737       assert (strcmp (insn->name, str) == 0);
7738
7739       if (OPCODE_IS_MEMBER (insn,
7740                             (mips_opts.isa
7741                              | (mips_opts.mips16 ? INSN_MIPS16 : 0)
7742                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7743                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7744                             mips_arch))
7745         ok = true;
7746       else
7747         ok = false;
7748
7749       if (insn->pinfo != INSN_MACRO)
7750         {
7751           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7752             ok = false;
7753         }
7754
7755       if (! ok)
7756         {
7757           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7758               && strcmp (insn->name, insn[1].name) == 0)
7759             {
7760               ++insn;
7761               continue;
7762             }
7763           else
7764             {
7765               if (!insn_error)
7766                 {
7767                   static char buf[100];
7768                   if (mips_arch_info->is_isa)
7769                     sprintf (buf,
7770                              _("opcode not supported at this ISA level (%s)"),
7771                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7772                   else
7773                     sprintf (buf,
7774                              _("opcode not supported on this processor: %s (%s)"),
7775                              mips_arch_info->name,
7776                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7777                   insn_error = buf;
7778                 }
7779               if (save_c)
7780                 *(--s) = save_c;
7781               return;
7782             }
7783         }
7784
7785       ip->insn_mo = insn;
7786       ip->insn_opcode = insn->match;
7787       insn_error = NULL;
7788       for (args = insn->args;; ++args)
7789         {
7790           int is_mdmx;
7791
7792           s += strspn (s, " \t");
7793           is_mdmx = 0;
7794           switch (*args)
7795             {
7796             case '\0':          /* end of args */
7797               if (*s == '\0')
7798                 return;
7799               break;
7800
7801             case ',':
7802               if (*s++ == *args)
7803                 continue;
7804               s--;
7805               switch (*++args)
7806                 {
7807                 case 'r':
7808                 case 'v':
7809                   ip->insn_opcode |= lastregno << OP_SH_RS;
7810                   continue;
7811
7812                 case 'w':
7813                   ip->insn_opcode |= lastregno << OP_SH_RT;
7814                   continue;
7815
7816                 case 'W':
7817                   ip->insn_opcode |= lastregno << OP_SH_FT;
7818                   continue;
7819
7820                 case 'V':
7821                   ip->insn_opcode |= lastregno << OP_SH_FS;
7822                   continue;
7823                 }
7824               break;
7825
7826             case '(':
7827               /* Handle optional base register.
7828                  Either the base register is omitted or
7829                  we must have a left paren.  */
7830               /* This is dependent on the next operand specifier
7831                  is a base register specification.  */
7832               assert (args[1] == 'b' || args[1] == '5'
7833                       || args[1] == '-' || args[1] == '4');
7834               if (*s == '\0')
7835                 return;
7836
7837             case ')':           /* these must match exactly */
7838               if (*s++ == *args)
7839                 continue;
7840               break;
7841
7842             case '<':           /* must be at least one digit */
7843               /*
7844                * According to the manual, if the shift amount is greater
7845                * than 31 or less than 0, then the shift amount should be
7846                * mod 32.  In reality the mips assembler issues an error.
7847                * We issue a warning and mask out all but the low 5 bits.
7848                */
7849               my_getExpression (&imm_expr, s);
7850               check_absolute_expr (ip, &imm_expr);
7851               if ((unsigned long) imm_expr.X_add_number > 31)
7852                 {
7853                   as_warn (_("Improper shift amount (%lu)"),
7854                            (unsigned long) imm_expr.X_add_number);
7855                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7856                 }
7857               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7858               imm_expr.X_op = O_absent;
7859               s = expr_end;
7860               continue;
7861
7862             case '>':           /* shift amount minus 32 */
7863               my_getExpression (&imm_expr, s);
7864               check_absolute_expr (ip, &imm_expr);
7865               if ((unsigned long) imm_expr.X_add_number < 32
7866                   || (unsigned long) imm_expr.X_add_number > 63)
7867                 break;
7868               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7869               imm_expr.X_op = O_absent;
7870               s = expr_end;
7871               continue;
7872
7873             case 'k':           /* cache code */
7874             case 'h':           /* prefx code */
7875               my_getExpression (&imm_expr, s);
7876               check_absolute_expr (ip, &imm_expr);
7877               if ((unsigned long) imm_expr.X_add_number > 31)
7878                 {
7879                   as_warn (_("Invalid value for `%s' (%lu)"),
7880                            ip->insn_mo->name,
7881                            (unsigned long) imm_expr.X_add_number);
7882                   imm_expr.X_add_number &= 0x1f;
7883                 }
7884               if (*args == 'k')
7885                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7886               else
7887                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7888               imm_expr.X_op = O_absent;
7889               s = expr_end;
7890               continue;
7891
7892             case 'c':           /* break code */
7893               my_getExpression (&imm_expr, s);
7894               check_absolute_expr (ip, &imm_expr);
7895               if ((unsigned long) imm_expr.X_add_number > 1023)
7896                 {
7897                   as_warn (_("Illegal break code (%lu)"),
7898                            (unsigned long) imm_expr.X_add_number);
7899                   imm_expr.X_add_number &= OP_MASK_CODE;
7900                 }
7901               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7902               imm_expr.X_op = O_absent;
7903               s = expr_end;
7904               continue;
7905
7906             case 'q':           /* lower break code */
7907               my_getExpression (&imm_expr, s);
7908               check_absolute_expr (ip, &imm_expr);
7909               if ((unsigned long) imm_expr.X_add_number > 1023)
7910                 {
7911                   as_warn (_("Illegal lower break code (%lu)"),
7912                            (unsigned long) imm_expr.X_add_number);
7913                   imm_expr.X_add_number &= OP_MASK_CODE2;
7914                 }
7915               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7916               imm_expr.X_op = O_absent;
7917               s = expr_end;
7918               continue;
7919
7920             case 'B':           /* 20-bit syscall/break code.  */
7921               my_getExpression (&imm_expr, s);
7922               check_absolute_expr (ip, &imm_expr);
7923               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7924                 as_warn (_("Illegal 20-bit code (%lu)"),
7925                          (unsigned long) imm_expr.X_add_number);
7926               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7927               imm_expr.X_op = O_absent;
7928               s = expr_end;
7929               continue;
7930
7931             case 'C':           /* Coprocessor code */
7932               my_getExpression (&imm_expr, s);
7933               check_absolute_expr (ip, &imm_expr);
7934               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
7935                 {
7936                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
7937                            (unsigned long) imm_expr.X_add_number);
7938                   imm_expr.X_add_number &= ((1 << 25) - 1);
7939                 }
7940               ip->insn_opcode |= imm_expr.X_add_number;
7941               imm_expr.X_op = O_absent;
7942               s = expr_end;
7943               continue;
7944
7945             case 'J':           /* 19-bit wait code.  */
7946               my_getExpression (&imm_expr, s);
7947               check_absolute_expr (ip, &imm_expr);
7948               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7949                 as_warn (_("Illegal 19-bit code (%lu)"),
7950                          (unsigned long) imm_expr.X_add_number);
7951               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7952               imm_expr.X_op = O_absent;
7953               s = expr_end;
7954               continue;
7955
7956             case 'P':           /* Performance register */
7957               my_getExpression (&imm_expr, s);
7958               check_absolute_expr (ip, &imm_expr);
7959               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7960                 {
7961                   as_warn (_("Invalid performance register (%lu)"),
7962                            (unsigned long) imm_expr.X_add_number);
7963                   imm_expr.X_add_number &= OP_MASK_PERFREG;
7964                 }
7965               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7966               imm_expr.X_op = O_absent;
7967               s = expr_end;
7968               continue;
7969
7970             case 'b':           /* base register */
7971             case 'd':           /* destination register */
7972             case 's':           /* source register */
7973             case 't':           /* target register */
7974             case 'r':           /* both target and source */
7975             case 'v':           /* both dest and source */
7976             case 'w':           /* both dest and target */
7977             case 'E':           /* coprocessor target register */
7978             case 'G':           /* coprocessor destination register */
7979             case 'x':           /* ignore register name */
7980             case 'z':           /* must be zero register */
7981             case 'U':           /* destination register (clo/clz).  */
7982               s_reset = s;
7983               if (s[0] == '$')
7984                 {
7985
7986                   if (ISDIGIT (s[1]))
7987                     {
7988                       ++s;
7989                       regno = 0;
7990                       do
7991                         {
7992                           regno *= 10;
7993                           regno += *s - '0';
7994                           ++s;
7995                         }
7996                       while (ISDIGIT (*s));
7997                       if (regno > 31)
7998                         as_bad (_("Invalid register number (%d)"), regno);
7999                     }
8000                   else if (*args == 'E' || *args == 'G')
8001                     goto notreg;
8002                   else
8003                     {
8004                       if (s[1] == 'r' && s[2] == 'a')
8005                         {
8006                           s += 3;
8007                           regno = RA;
8008                         }
8009                       else if (s[1] == 'f' && s[2] == 'p')
8010                         {
8011                           s += 3;
8012                           regno = FP;
8013                         }
8014                       else if (s[1] == 's' && s[2] == 'p')
8015                         {
8016                           s += 3;
8017                           regno = SP;
8018                         }
8019                       else if (s[1] == 'g' && s[2] == 'p')
8020                         {
8021                           s += 3;
8022                           regno = GP;
8023                         }
8024                       else if (s[1] == 'a' && s[2] == 't')
8025                         {
8026                           s += 3;
8027                           regno = AT;
8028                         }
8029                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8030                         {
8031                           s += 4;
8032                           regno = KT0;
8033                         }
8034                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8035                         {
8036                           s += 4;
8037                           regno = KT1;
8038                         }
8039                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8040                         {
8041                           s += 5;
8042                           regno = ZERO;
8043                         }
8044                       else if (itbl_have_entries)
8045                         {
8046                           char *p, *n;
8047                           unsigned long r;
8048
8049                           p = s + 1;    /* advance past '$' */
8050                           n = itbl_get_field (&p);  /* n is name */
8051
8052                           /* See if this is a register defined in an
8053                              itbl entry.  */
8054                           if (itbl_get_reg_val (n, &r))
8055                             {
8056                               /* Get_field advances to the start of
8057                                  the next field, so we need to back
8058                                  rack to the end of the last field.  */
8059                               if (p)
8060                                 s = p - 1;
8061                               else
8062                                 s = strchr (s, '\0');
8063                               regno = r;
8064                             }
8065                           else
8066                             goto notreg;
8067                         }
8068                       else
8069                         goto notreg;
8070                     }
8071                   if (regno == AT
8072                       && ! mips_opts.noat
8073                       && *args != 'E'
8074                       && *args != 'G')
8075                     as_warn (_("Used $at without \".set noat\""));
8076                   c = *args;
8077                   if (*s == ' ')
8078                     ++s;
8079                   if (args[1] != *s)
8080                     {
8081                       if (c == 'r' || c == 'v' || c == 'w')
8082                         {
8083                           regno = lastregno;
8084                           s = s_reset;
8085                           ++args;
8086                         }
8087                     }
8088                   /* 'z' only matches $0.  */
8089                   if (c == 'z' && regno != 0)
8090                     break;
8091
8092         /* Now that we have assembled one operand, we use the args string
8093          * to figure out where it goes in the instruction.  */
8094                   switch (c)
8095                     {
8096                     case 'r':
8097                     case 's':
8098                     case 'v':
8099                     case 'b':
8100                       ip->insn_opcode |= regno << OP_SH_RS;
8101                       break;
8102                     case 'd':
8103                     case 'G':
8104                       ip->insn_opcode |= regno << OP_SH_RD;
8105                       break;
8106                     case 'U':
8107                       ip->insn_opcode |= regno << OP_SH_RD;
8108                       ip->insn_opcode |= regno << OP_SH_RT;
8109                       break;
8110                     case 'w':
8111                     case 't':
8112                     case 'E':
8113                       ip->insn_opcode |= regno << OP_SH_RT;
8114                       break;
8115                     case 'x':
8116                       /* This case exists because on the r3000 trunc
8117                          expands into a macro which requires a gp
8118                          register.  On the r6000 or r4000 it is
8119                          assembled into a single instruction which
8120                          ignores the register.  Thus the insn version
8121                          is MIPS_ISA2 and uses 'x', and the macro
8122                          version is MIPS_ISA1 and uses 't'.  */
8123                       break;
8124                     case 'z':
8125                       /* This case is for the div instruction, which
8126                          acts differently if the destination argument
8127                          is $0.  This only matches $0, and is checked
8128                          outside the switch.  */
8129                       break;
8130                     case 'D':
8131                       /* Itbl operand; not yet implemented. FIXME ?? */
8132                       break;
8133                       /* What about all other operands like 'i', which
8134                          can be specified in the opcode table? */
8135                     }
8136                   lastregno = regno;
8137                   continue;
8138                 }
8139             notreg:
8140               switch (*args++)
8141                 {
8142                 case 'r':
8143                 case 'v':
8144                   ip->insn_opcode |= lastregno << OP_SH_RS;
8145                   continue;
8146                 case 'w':
8147                   ip->insn_opcode |= lastregno << OP_SH_RT;
8148                   continue;
8149                 }
8150               break;
8151
8152             case 'O':           /* MDMX alignment immediate constant.  */
8153               my_getExpression (&imm_expr, s);
8154               check_absolute_expr (ip, &imm_expr);
8155               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8156                 {
8157                   as_warn ("Improper align amount (%ld), using low bits",
8158                            (long) imm_expr.X_add_number);
8159                   imm_expr.X_add_number &= OP_MASK_ALN;
8160                 }
8161               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8162               imm_expr.X_op = O_absent;
8163               s = expr_end;
8164               continue;
8165
8166             case 'Q':           /* MDMX vector, element sel, or const.  */
8167               if (s[0] != '$')
8168                 {
8169                   /* MDMX Immediate.  */
8170                   my_getExpression (&imm_expr, s);
8171                   check_absolute_expr (ip, &imm_expr);
8172                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8173                     {
8174                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8175                                (long) imm_expr.X_add_number);
8176                       imm_expr.X_add_number &= OP_MASK_FT;
8177                     }
8178                   imm_expr.X_add_number &= OP_MASK_FT;
8179                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8180                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8181                   else
8182                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8183                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8184                   imm_expr.X_op = O_absent;
8185                   s = expr_end;
8186                   continue;
8187                 }
8188               /* Not MDMX Immediate.  Fall through.  */
8189             case 'X':           /* MDMX destination register.  */
8190             case 'Y':           /* MDMX source register.  */
8191             case 'Z':           /* MDMX target register.  */
8192               is_mdmx = 1;
8193             case 'D':           /* floating point destination register */
8194             case 'S':           /* floating point source register */
8195             case 'T':           /* floating point target register */
8196             case 'R':           /* floating point source register */
8197             case 'V':
8198             case 'W':
8199               s_reset = s;
8200               /* Accept $fN for FP and MDMX register numbers, and in
8201                  addition accept $vN for MDMX register numbers.  */
8202               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8203                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8204                       && ISDIGIT (s[2])))
8205                 {
8206                   s += 2;
8207                   regno = 0;
8208                   do
8209                     {
8210                       regno *= 10;
8211                       regno += *s - '0';
8212                       ++s;
8213                     }
8214                   while (ISDIGIT (*s));
8215
8216                   if (regno > 31)
8217                     as_bad (_("Invalid float register number (%d)"), regno);
8218
8219                   if ((regno & 1) != 0
8220                       && HAVE_32BIT_FPRS
8221                       && ! (strcmp (str, "mtc1") == 0
8222                             || strcmp (str, "mfc1") == 0
8223                             || strcmp (str, "lwc1") == 0
8224                             || strcmp (str, "swc1") == 0
8225                             || strcmp (str, "l.s") == 0
8226                             || strcmp (str, "s.s") == 0))
8227                     as_warn (_("Float register should be even, was %d"),
8228                              regno);
8229
8230                   c = *args;
8231                   if (*s == ' ')
8232                     ++s;
8233                   if (args[1] != *s)
8234                     {
8235                       if (c == 'V' || c == 'W')
8236                         {
8237                           regno = lastregno;
8238                           s = s_reset;
8239                           ++args;
8240                         }
8241                     }
8242                   switch (c)
8243                     {
8244                     case 'D':
8245                     case 'X':
8246                       ip->insn_opcode |= regno << OP_SH_FD;
8247                       break;
8248                     case 'V':
8249                     case 'S':
8250                     case 'Y':
8251                       ip->insn_opcode |= regno << OP_SH_FS;
8252                       break;
8253                     case 'Q':
8254                       /* This is like 'Z', but also needs to fix the MDMX
8255                          vector/scalar select bits.  Note that the
8256                          scalar immediate case is handled above.  */
8257                       if (*s == '[')
8258                         {
8259                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8260                           int max_el = (is_qh ? 3 : 7);
8261                           s++;
8262                           my_getExpression(&imm_expr, s);
8263                           check_absolute_expr (ip, &imm_expr);
8264                           s = expr_end;
8265                           if (imm_expr.X_add_number > max_el)
8266                             as_bad(_("Bad element selector %ld"),
8267                                    (long) imm_expr.X_add_number);
8268                           imm_expr.X_add_number &= max_el;
8269                           ip->insn_opcode |= (imm_expr.X_add_number
8270                                               << (OP_SH_VSEL +
8271                                                   (is_qh ? 2 : 1)));
8272                           if (*s != ']')
8273                             as_warn(_("Expecting ']' found '%s'"), s);
8274                           else
8275                             s++;
8276                         }
8277                       else
8278                         {
8279                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8280                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8281                                                 << OP_SH_VSEL);
8282                           else
8283                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8284                                                 OP_SH_VSEL);
8285                         }
8286                       /* Fall through */
8287                     case 'W':
8288                     case 'T':
8289                     case 'Z':
8290                       ip->insn_opcode |= regno << OP_SH_FT;
8291                       break;
8292                     case 'R':
8293                       ip->insn_opcode |= regno << OP_SH_FR;
8294                       break;
8295                     }
8296                   lastregno = regno;
8297                   continue;
8298                 }
8299
8300               switch (*args++)
8301                 {
8302                 case 'V':
8303                   ip->insn_opcode |= lastregno << OP_SH_FS;
8304                   continue;
8305                 case 'W':
8306                   ip->insn_opcode |= lastregno << OP_SH_FT;
8307                   continue;
8308                 }
8309               break;
8310
8311             case 'I':
8312               my_getExpression (&imm_expr, s);
8313               if (imm_expr.X_op != O_big
8314                   && imm_expr.X_op != O_constant)
8315                 insn_error = _("absolute expression required");
8316               s = expr_end;
8317               continue;
8318
8319             case 'A':
8320               my_getExpression (&offset_expr, s);
8321               *imm_reloc = BFD_RELOC_32;
8322               s = expr_end;
8323               continue;
8324
8325             case 'F':
8326             case 'L':
8327             case 'f':
8328             case 'l':
8329               {
8330                 int f64;
8331                 int using_gprs;
8332                 char *save_in;
8333                 char *err;
8334                 unsigned char temp[8];
8335                 int len;
8336                 unsigned int length;
8337                 segT seg;
8338                 subsegT subseg;
8339                 char *p;
8340
8341                 /* These only appear as the last operand in an
8342                    instruction, and every instruction that accepts
8343                    them in any variant accepts them in all variants.
8344                    This means we don't have to worry about backing out
8345                    any changes if the instruction does not match.
8346
8347                    The difference between them is the size of the
8348                    floating point constant and where it goes.  For 'F'
8349                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8350                    is 32 bits.  Where the constant is placed is based
8351                    on how the MIPS assembler does things:
8352                     F -- .rdata
8353                     L -- .lit8
8354                     f -- immediate value
8355                     l -- .lit4
8356
8357                     The .lit4 and .lit8 sections are only used if
8358                     permitted by the -G argument.
8359
8360                     When generating embedded PIC code, we use the
8361                     .lit8 section but not the .lit4 section (we can do
8362                     .lit4 inline easily; we need to put .lit8
8363                     somewhere in the data segment, and using .lit8
8364                     permits the linker to eventually combine identical
8365                     .lit8 entries).
8366
8367                     The code below needs to know whether the target register
8368                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8369                     'F' are used with GPR-based instructions and 'l' and
8370                     'L' are used with FPR-based instructions.  */
8371
8372                 f64 = *args == 'F' || *args == 'L';
8373                 using_gprs = *args == 'F' || *args == 'f';
8374
8375                 save_in = input_line_pointer;
8376                 input_line_pointer = s;
8377                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8378                 length = len;
8379                 s = input_line_pointer;
8380                 input_line_pointer = save_in;
8381                 if (err != NULL && *err != '\0')
8382                   {
8383                     as_bad (_("Bad floating point constant: %s"), err);
8384                     memset (temp, '\0', sizeof temp);
8385                     length = f64 ? 8 : 4;
8386                   }
8387
8388                 assert (length == (unsigned) (f64 ? 8 : 4));
8389
8390                 if (*args == 'f'
8391                     || (*args == 'l'
8392                         && (! USE_GLOBAL_POINTER_OPT
8393                             || mips_pic == EMBEDDED_PIC
8394                             || g_switch_value < 4
8395                             || (temp[0] == 0 && temp[1] == 0)
8396                             || (temp[2] == 0 && temp[3] == 0))))
8397                   {
8398                     imm_expr.X_op = O_constant;
8399                     if (! target_big_endian)
8400                       imm_expr.X_add_number = bfd_getl32 (temp);
8401                     else
8402                       imm_expr.X_add_number = bfd_getb32 (temp);
8403                   }
8404                 else if (length > 4
8405                          && ! mips_disable_float_construction
8406                          /* Constants can only be constructed in GPRs and
8407                             copied to FPRs if the GPRs are at least as wide
8408                             as the FPRs.  Force the constant into memory if
8409                             we are using 64-bit FPRs but the GPRs are only
8410                             32 bits wide.  */
8411                          && (using_gprs
8412                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8413                          && ((temp[0] == 0 && temp[1] == 0)
8414                              || (temp[2] == 0 && temp[3] == 0))
8415                          && ((temp[4] == 0 && temp[5] == 0)
8416                              || (temp[6] == 0 && temp[7] == 0)))
8417                   {
8418                     /* The value is simple enough to load with a couple of
8419                        instructions.  If using 32-bit registers, set
8420                        imm_expr to the high order 32 bits and offset_expr to
8421                        the low order 32 bits.  Otherwise, set imm_expr to
8422                        the entire 64 bit constant.  */
8423                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8424                       {
8425                         imm_expr.X_op = O_constant;
8426                         offset_expr.X_op = O_constant;
8427                         if (! target_big_endian)
8428                           {
8429                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8430                             offset_expr.X_add_number = bfd_getl32 (temp);
8431                           }
8432                         else
8433                           {
8434                             imm_expr.X_add_number = bfd_getb32 (temp);
8435                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8436                           }
8437                         if (offset_expr.X_add_number == 0)
8438                           offset_expr.X_op = O_absent;
8439                       }
8440                     else if (sizeof (imm_expr.X_add_number) > 4)
8441                       {
8442                         imm_expr.X_op = O_constant;
8443                         if (! target_big_endian)
8444                           imm_expr.X_add_number = bfd_getl64 (temp);
8445                         else
8446                           imm_expr.X_add_number = bfd_getb64 (temp);
8447                       }
8448                     else
8449                       {
8450                         imm_expr.X_op = O_big;
8451                         imm_expr.X_add_number = 4;
8452                         if (! target_big_endian)
8453                           {
8454                             generic_bignum[0] = bfd_getl16 (temp);
8455                             generic_bignum[1] = bfd_getl16 (temp + 2);
8456                             generic_bignum[2] = bfd_getl16 (temp + 4);
8457                             generic_bignum[3] = bfd_getl16 (temp + 6);
8458                           }
8459                         else
8460                           {
8461                             generic_bignum[0] = bfd_getb16 (temp + 6);
8462                             generic_bignum[1] = bfd_getb16 (temp + 4);
8463                             generic_bignum[2] = bfd_getb16 (temp + 2);
8464                             generic_bignum[3] = bfd_getb16 (temp);
8465                           }
8466                       }
8467                   }
8468                 else
8469                   {
8470                     const char *newname;
8471                     segT new_seg;
8472
8473                     /* Switch to the right section.  */
8474                     seg = now_seg;
8475                     subseg = now_subseg;
8476                     switch (*args)
8477                       {
8478                       default: /* unused default case avoids warnings.  */
8479                       case 'L':
8480                         newname = RDATA_SECTION_NAME;
8481                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8482                             || mips_pic == EMBEDDED_PIC)
8483                           newname = ".lit8";
8484                         break;
8485                       case 'F':
8486                         if (mips_pic == EMBEDDED_PIC)
8487                           newname = ".lit8";
8488                         else
8489                           newname = RDATA_SECTION_NAME;
8490                         break;
8491                       case 'l':
8492                         assert (!USE_GLOBAL_POINTER_OPT
8493                                 || g_switch_value >= 4);
8494                         newname = ".lit4";
8495                         break;
8496                       }
8497                     new_seg = subseg_new (newname, (subsegT) 0);
8498                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8499                       bfd_set_section_flags (stdoutput, new_seg,
8500                                              (SEC_ALLOC
8501                                               | SEC_LOAD
8502                                               | SEC_READONLY
8503                                               | SEC_DATA));
8504                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8505                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8506                         && strcmp (TARGET_OS, "elf") != 0)
8507                       record_alignment (new_seg, 4);
8508                     else
8509                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8510                     if (seg == now_seg)
8511                       as_bad (_("Can't use floating point insn in this section"));
8512
8513                     /* Set the argument to the current address in the
8514                        section.  */
8515                     offset_expr.X_op = O_symbol;
8516                     offset_expr.X_add_symbol =
8517                       symbol_new ("L0\001", now_seg,
8518                                   (valueT) frag_now_fix (), frag_now);
8519                     offset_expr.X_add_number = 0;
8520
8521                     /* Put the floating point number into the section.  */
8522                     p = frag_more ((int) length);
8523                     memcpy (p, temp, length);
8524
8525                     /* Switch back to the original section.  */
8526                     subseg_set (seg, subseg);
8527                   }
8528               }
8529               continue;
8530
8531             case 'i':           /* 16 bit unsigned immediate */
8532             case 'j':           /* 16 bit signed immediate */
8533               *imm_reloc = BFD_RELOC_LO16;
8534               c = my_getSmallExpression (&imm_expr, s);
8535               if (c != S_EX_NONE)
8536                 {
8537                   if (c != S_EX_LO)
8538                     {
8539                       if (c == S_EX_HI)
8540                         {
8541                           *imm_reloc = BFD_RELOC_HI16_S;
8542                           imm_unmatched_hi = true;
8543                         }
8544 #ifdef OBJ_ELF
8545                       else if (c == S_EX_HIGHEST)
8546                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8547                       else if (c == S_EX_HIGHER)
8548                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8549                       else if (c == S_EX_GP_REL)
8550                         {
8551                           /* This occurs in NewABI only.  */
8552                           c = my_getSmallExpression (&imm_expr, s);
8553                           if (c != S_EX_NEG)
8554                             as_bad (_("bad composition of relocations"));
8555                           else
8556                             {
8557                               c = my_getSmallExpression (&imm_expr, s);
8558                               if (c != S_EX_LO)
8559                                 as_bad (_("bad composition of relocations"));
8560                               else
8561                                 {
8562                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8563                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8564                                   imm_reloc[2] = BFD_RELOC_LO16;
8565                                 }
8566                             }
8567                         }
8568 #endif
8569                       else
8570                         *imm_reloc = BFD_RELOC_HI16;
8571                     }
8572                   else if (imm_expr.X_op == O_constant)
8573                     imm_expr.X_add_number &= 0xffff;
8574                 }
8575               if (*args == 'i')
8576                 {
8577                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8578                       || ((imm_expr.X_add_number < 0
8579                            || imm_expr.X_add_number >= 0x10000)
8580                           && imm_expr.X_op == O_constant))
8581                     {
8582                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8583                           !strcmp (insn->name, insn[1].name))
8584                         break;
8585                       if (imm_expr.X_op == O_constant
8586                           || imm_expr.X_op == O_big)
8587                         as_bad (_("16 bit expression not in range 0..65535"));
8588                     }
8589                 }
8590               else
8591                 {
8592                   int more;
8593                   offsetT max;
8594
8595                   /* The upper bound should be 0x8000, but
8596                      unfortunately the MIPS assembler accepts numbers
8597                      from 0x8000 to 0xffff and sign extends them, and
8598                      we want to be compatible.  We only permit this
8599                      extended range for an instruction which does not
8600                      provide any further alternates, since those
8601                      alternates may handle other cases.  People should
8602                      use the numbers they mean, rather than relying on
8603                      a mysterious sign extension.  */
8604                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8605                           strcmp (insn->name, insn[1].name) == 0);
8606                   if (more)
8607                     max = 0x8000;
8608                   else
8609                     max = 0x10000;
8610                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8611                       || ((imm_expr.X_add_number < -0x8000
8612                            || imm_expr.X_add_number >= max)
8613                           && imm_expr.X_op == O_constant)
8614                       || (more
8615                           && imm_expr.X_add_number < 0
8616                           && HAVE_64BIT_GPRS
8617                           && imm_expr.X_unsigned
8618                           && sizeof (imm_expr.X_add_number) <= 4))
8619                     {
8620                       if (more)
8621                         break;
8622                       if (imm_expr.X_op == O_constant
8623                           || imm_expr.X_op == O_big)
8624                         as_bad (_("16 bit expression not in range -32768..32767"));
8625                     }
8626                 }
8627               s = expr_end;
8628               continue;
8629
8630             case 'o':           /* 16 bit offset */
8631               c = my_getSmallExpression (&offset_expr, s);
8632
8633               /* If this value won't fit into a 16 bit offset, then go
8634                  find a macro that will generate the 32 bit offset
8635                  code pattern.  */
8636               if (c == S_EX_NONE
8637                   && (offset_expr.X_op != O_constant
8638                       || offset_expr.X_add_number >= 0x8000
8639                       || offset_expr.X_add_number < -0x8000))
8640                 break;
8641
8642               if (c == S_EX_HI)
8643                 {
8644                   if (offset_expr.X_op != O_constant)
8645                     break;
8646                   offset_expr.X_add_number =
8647                     (offset_expr.X_add_number >> 16) & 0xffff;
8648                 }
8649               *offset_reloc = BFD_RELOC_LO16;
8650               s = expr_end;
8651               continue;
8652
8653             case 'p':           /* pc relative offset */
8654               if (mips_pic == EMBEDDED_PIC)
8655                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8656               else
8657                 *offset_reloc = BFD_RELOC_16_PCREL;
8658               my_getExpression (&offset_expr, s);
8659               s = expr_end;
8660               continue;
8661
8662             case 'u':           /* upper 16 bits */
8663               c = my_getSmallExpression (&imm_expr, s);
8664               *imm_reloc = BFD_RELOC_LO16;
8665               if (c != S_EX_NONE)
8666                 {
8667                   if (c != S_EX_LO)
8668                     {
8669                       if (c == S_EX_HI)
8670                         {
8671                           *imm_reloc = BFD_RELOC_HI16_S;
8672                           imm_unmatched_hi = true;
8673                         }
8674 #ifdef OBJ_ELF
8675                       else if (c == S_EX_HIGHEST)
8676                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8677                       else if (c == S_EX_GP_REL)
8678                         {
8679                           /* This occurs in NewABI only.  */
8680                           c = my_getSmallExpression (&imm_expr, s);
8681                           if (c != S_EX_NEG)
8682                             as_bad (_("bad composition of relocations"));
8683                           else
8684                             {
8685                               c = my_getSmallExpression (&imm_expr, s);
8686                               if (c != S_EX_HI)
8687                                 as_bad (_("bad composition of relocations"));
8688                               else
8689                                 {
8690                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8691                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8692                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8693                                 }
8694                             }
8695                         }
8696 #endif
8697                       else
8698                         *imm_reloc = BFD_RELOC_HI16;
8699                     }
8700                   else if (imm_expr.X_op == O_constant)
8701                     imm_expr.X_add_number &= 0xffff;
8702                 }
8703               else if (imm_expr.X_op == O_constant
8704                        && (imm_expr.X_add_number < 0
8705                            || imm_expr.X_add_number >= 0x10000))
8706                 as_bad (_("lui expression not in range 0..65535"));
8707               s = expr_end;
8708               continue;
8709
8710             case 'a':           /* 26 bit address */
8711               my_getExpression (&offset_expr, s);
8712               s = expr_end;
8713               *offset_reloc = BFD_RELOC_MIPS_JMP;
8714               continue;
8715
8716             case 'N':           /* 3 bit branch condition code */
8717             case 'M':           /* 3 bit compare condition code */
8718               if (strncmp (s, "$fcc", 4) != 0)
8719                 break;
8720               s += 4;
8721               regno = 0;
8722               do
8723                 {
8724                   regno *= 10;
8725                   regno += *s - '0';
8726                   ++s;
8727                 }
8728               while (ISDIGIT (*s));
8729               if (regno > 7)
8730                 as_bad (_("invalid condition code register $fcc%d"), regno);
8731               if (*args == 'N')
8732                 ip->insn_opcode |= regno << OP_SH_BCC;
8733               else
8734                 ip->insn_opcode |= regno << OP_SH_CCC;
8735               continue;
8736
8737             case 'H':
8738               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8739                 s += 2;
8740               if (ISDIGIT (*s))
8741                 {
8742                   c = 0;
8743                   do
8744                     {
8745                       c *= 10;
8746                       c += *s - '0';
8747                       ++s;
8748                     }
8749                   while (ISDIGIT (*s));
8750                 }
8751               else
8752                 c = 8; /* Invalid sel value.  */
8753
8754               if (c > 7)
8755                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8756               ip->insn_opcode |= c;
8757               continue;
8758
8759             default:
8760               as_bad (_("bad char = '%c'\n"), *args);
8761               internalError ();
8762             }
8763           break;
8764         }
8765       /* Args don't match.  */
8766       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8767           !strcmp (insn->name, insn[1].name))
8768         {
8769           ++insn;
8770           s = argsStart;
8771           insn_error = _("illegal operands");
8772           continue;
8773         }
8774       if (save_c)
8775         *(--s) = save_c;
8776       insn_error = _("illegal operands");
8777       return;
8778     }
8779 }
8780
8781 /* This routine assembles an instruction into its binary format when
8782    assembling for the mips16.  As a side effect, it sets one of the
8783    global variables imm_reloc or offset_reloc to the type of
8784    relocation to do if one of the operands is an address expression.
8785    It also sets mips16_small and mips16_ext if the user explicitly
8786    requested a small or extended instruction.  */
8787
8788 static void
8789 mips16_ip (str, ip)
8790      char *str;
8791      struct mips_cl_insn *ip;
8792 {
8793   char *s;
8794   const char *args;
8795   struct mips_opcode *insn;
8796   char *argsstart;
8797   unsigned int regno;
8798   unsigned int lastregno = 0;
8799   char *s_reset;
8800
8801   insn_error = NULL;
8802
8803   mips16_small = false;
8804   mips16_ext = false;
8805
8806   for (s = str; ISLOWER (*s); ++s)
8807     ;
8808   switch (*s)
8809     {
8810     case '\0':
8811       break;
8812
8813     case ' ':
8814       *s++ = '\0';
8815       break;
8816
8817     case '.':
8818       if (s[1] == 't' && s[2] == ' ')
8819         {
8820           *s = '\0';
8821           mips16_small = true;
8822           s += 3;
8823           break;
8824         }
8825       else if (s[1] == 'e' && s[2] == ' ')
8826         {
8827           *s = '\0';
8828           mips16_ext = true;
8829           s += 3;
8830           break;
8831         }
8832       /* Fall through.  */
8833     default:
8834       insn_error = _("unknown opcode");
8835       return;
8836     }
8837
8838   if (mips_opts.noautoextend && ! mips16_ext)
8839     mips16_small = true;
8840
8841   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8842     {
8843       insn_error = _("unrecognized opcode");
8844       return;
8845     }
8846
8847   argsstart = s;
8848   for (;;)
8849     {
8850       assert (strcmp (insn->name, str) == 0);
8851
8852       ip->insn_mo = insn;
8853       ip->insn_opcode = insn->match;
8854       ip->use_extend = false;
8855       imm_expr.X_op = O_absent;
8856       imm_reloc[0] = BFD_RELOC_UNUSED;
8857       imm_reloc[1] = BFD_RELOC_UNUSED;
8858       imm_reloc[2] = BFD_RELOC_UNUSED;
8859       offset_expr.X_op = O_absent;
8860       offset_reloc[0] = BFD_RELOC_UNUSED;
8861       offset_reloc[1] = BFD_RELOC_UNUSED;
8862       offset_reloc[2] = BFD_RELOC_UNUSED;
8863       for (args = insn->args; 1; ++args)
8864         {
8865           int c;
8866
8867           if (*s == ' ')
8868             ++s;
8869
8870           /* In this switch statement we call break if we did not find
8871              a match, continue if we did find a match, or return if we
8872              are done.  */
8873
8874           c = *args;
8875           switch (c)
8876             {
8877             case '\0':
8878               if (*s == '\0')
8879                 {
8880                   /* Stuff the immediate value in now, if we can.  */
8881                   if (imm_expr.X_op == O_constant
8882                       && *imm_reloc > BFD_RELOC_UNUSED
8883                       && insn->pinfo != INSN_MACRO)
8884                     {
8885                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8886                                     imm_expr.X_add_number, true, mips16_small,
8887                                     mips16_ext, &ip->insn_opcode,
8888                                     &ip->use_extend, &ip->extend);
8889                       imm_expr.X_op = O_absent;
8890                       *imm_reloc = BFD_RELOC_UNUSED;
8891                     }
8892
8893                   return;
8894                 }
8895               break;
8896
8897             case ',':
8898               if (*s++ == c)
8899                 continue;
8900               s--;
8901               switch (*++args)
8902                 {
8903                 case 'v':
8904                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8905                   continue;
8906                 case 'w':
8907                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8908                   continue;
8909                 }
8910               break;
8911
8912             case '(':
8913             case ')':
8914               if (*s++ == c)
8915                 continue;
8916               break;
8917
8918             case 'v':
8919             case 'w':
8920               if (s[0] != '$')
8921                 {
8922                   if (c == 'v')
8923                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8924                   else
8925                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8926                   ++args;
8927                   continue;
8928                 }
8929               /* Fall through.  */
8930             case 'x':
8931             case 'y':
8932             case 'z':
8933             case 'Z':
8934             case '0':
8935             case 'S':
8936             case 'R':
8937             case 'X':
8938             case 'Y':
8939               if (s[0] != '$')
8940                 break;
8941               s_reset = s;
8942               if (ISDIGIT (s[1]))
8943                 {
8944                   ++s;
8945                   regno = 0;
8946                   do
8947                     {
8948                       regno *= 10;
8949                       regno += *s - '0';
8950                       ++s;
8951                     }
8952                   while (ISDIGIT (*s));
8953                   if (regno > 31)
8954                     {
8955                       as_bad (_("invalid register number (%d)"), regno);
8956                       regno = 2;
8957                     }
8958                 }
8959               else
8960                 {
8961                   if (s[1] == 'r' && s[2] == 'a')
8962                     {
8963                       s += 3;
8964                       regno = RA;
8965                     }
8966                   else if (s[1] == 'f' && s[2] == 'p')
8967                     {
8968                       s += 3;
8969                       regno = FP;
8970                     }
8971                   else if (s[1] == 's' && s[2] == 'p')
8972                     {
8973                       s += 3;
8974                       regno = SP;
8975                     }
8976                   else if (s[1] == 'g' && s[2] == 'p')
8977                     {
8978                       s += 3;
8979                       regno = GP;
8980                     }
8981                   else if (s[1] == 'a' && s[2] == 't')
8982                     {
8983                       s += 3;
8984                       regno = AT;
8985                     }
8986                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8987                     {
8988                       s += 4;
8989                       regno = KT0;
8990                     }
8991                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8992                     {
8993                       s += 4;
8994                       regno = KT1;
8995                     }
8996                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8997                     {
8998                       s += 5;
8999                       regno = ZERO;
9000                     }
9001                   else
9002                     break;
9003                 }
9004
9005               if (*s == ' ')
9006                 ++s;
9007               if (args[1] != *s)
9008                 {
9009                   if (c == 'v' || c == 'w')
9010                     {
9011                       regno = mips16_to_32_reg_map[lastregno];
9012                       s = s_reset;
9013                       ++args;
9014                     }
9015                 }
9016
9017               switch (c)
9018                 {
9019                 case 'x':
9020                 case 'y':
9021                 case 'z':
9022                 case 'v':
9023                 case 'w':
9024                 case 'Z':
9025                   regno = mips32_to_16_reg_map[regno];
9026                   break;
9027
9028                 case '0':
9029                   if (regno != 0)
9030                     regno = ILLEGAL_REG;
9031                   break;
9032
9033                 case 'S':
9034                   if (regno != SP)
9035                     regno = ILLEGAL_REG;
9036                   break;
9037
9038                 case 'R':
9039                   if (regno != RA)
9040                     regno = ILLEGAL_REG;
9041                   break;
9042
9043                 case 'X':
9044                 case 'Y':
9045                   if (regno == AT && ! mips_opts.noat)
9046                     as_warn (_("used $at without \".set noat\""));
9047                   break;
9048
9049                 default:
9050                   internalError ();
9051                 }
9052
9053               if (regno == ILLEGAL_REG)
9054                 break;
9055
9056               switch (c)
9057                 {
9058                 case 'x':
9059                 case 'v':
9060                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9061                   break;
9062                 case 'y':
9063                 case 'w':
9064                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9065                   break;
9066                 case 'z':
9067                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9068                   break;
9069                 case 'Z':
9070                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9071                 case '0':
9072                 case 'S':
9073                 case 'R':
9074                   break;
9075                 case 'X':
9076                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9077                   break;
9078                 case 'Y':
9079                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9080                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9081                   break;
9082                 default:
9083                   internalError ();
9084                 }
9085
9086               lastregno = regno;
9087               continue;
9088
9089             case 'P':
9090               if (strncmp (s, "$pc", 3) == 0)
9091                 {
9092                   s += 3;
9093                   continue;
9094                 }
9095               break;
9096
9097             case '<':
9098             case '>':
9099             case '[':
9100             case ']':
9101             case '4':
9102             case '5':
9103             case 'H':
9104             case 'W':
9105             case 'D':
9106             case 'j':
9107             case '8':
9108             case 'V':
9109             case 'C':
9110             case 'U':
9111             case 'k':
9112             case 'K':
9113               if (s[0] == '%'
9114                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9115                 {
9116                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9117                      and generate the appropriate reloc.  If the text
9118                      inside %gprel is not a symbol name with an
9119                      optional offset, then we generate a normal reloc
9120                      and will probably fail later.  */
9121                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9122                   if (imm_expr.X_op == O_symbol)
9123                     {
9124                       mips16_ext = true;
9125                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9126                       s = expr_end;
9127                       ip->use_extend = true;
9128                       ip->extend = 0;
9129                       continue;
9130                     }
9131                 }
9132               else
9133                 {
9134                   /* Just pick up a normal expression.  */
9135                   my_getExpression (&imm_expr, s);
9136                 }
9137
9138               if (imm_expr.X_op == O_register)
9139                 {
9140                   /* What we thought was an expression turned out to
9141                      be a register.  */
9142
9143                   if (s[0] == '(' && args[1] == '(')
9144                     {
9145                       /* It looks like the expression was omitted
9146                          before a register indirection, which means
9147                          that the expression is implicitly zero.  We
9148                          still set up imm_expr, so that we handle
9149                          explicit extensions correctly.  */
9150                       imm_expr.X_op = O_constant;
9151                       imm_expr.X_add_number = 0;
9152                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9153                       continue;
9154                     }
9155
9156                   break;
9157                 }
9158
9159               /* We need to relax this instruction.  */
9160               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9161               s = expr_end;
9162               continue;
9163
9164             case 'p':
9165             case 'q':
9166             case 'A':
9167             case 'B':
9168             case 'E':
9169               /* We use offset_reloc rather than imm_reloc for the PC
9170                  relative operands.  This lets macros with both
9171                  immediate and address operands work correctly.  */
9172               my_getExpression (&offset_expr, s);
9173
9174               if (offset_expr.X_op == O_register)
9175                 break;
9176
9177               /* We need to relax this instruction.  */
9178               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9179               s = expr_end;
9180               continue;
9181
9182             case '6':           /* break code */
9183               my_getExpression (&imm_expr, s);
9184               check_absolute_expr (ip, &imm_expr);
9185               if ((unsigned long) imm_expr.X_add_number > 63)
9186                 {
9187                   as_warn (_("Invalid value for `%s' (%lu)"),
9188                            ip->insn_mo->name,
9189                            (unsigned long) imm_expr.X_add_number);
9190                   imm_expr.X_add_number &= 0x3f;
9191                 }
9192               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9193               imm_expr.X_op = O_absent;
9194               s = expr_end;
9195               continue;
9196
9197             case 'a':           /* 26 bit address */
9198               my_getExpression (&offset_expr, s);
9199               s = expr_end;
9200               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9201               ip->insn_opcode <<= 16;
9202               continue;
9203
9204             case 'l':           /* register list for entry macro */
9205             case 'L':           /* register list for exit macro */
9206               {
9207                 int mask;
9208
9209                 if (c == 'l')
9210                   mask = 0;
9211                 else
9212                   mask = 7 << 3;
9213                 while (*s != '\0')
9214                   {
9215                     int freg, reg1, reg2;
9216
9217                     while (*s == ' ' || *s == ',')
9218                       ++s;
9219                     if (*s != '$')
9220                       {
9221                         as_bad (_("can't parse register list"));
9222                         break;
9223                       }
9224                     ++s;
9225                     if (*s != 'f')
9226                       freg = 0;
9227                     else
9228                       {
9229                         freg = 1;
9230                         ++s;
9231                       }
9232                     reg1 = 0;
9233                     while (ISDIGIT (*s))
9234                       {
9235                         reg1 *= 10;
9236                         reg1 += *s - '0';
9237                         ++s;
9238                       }
9239                     if (*s == ' ')
9240                       ++s;
9241                     if (*s != '-')
9242                       reg2 = reg1;
9243                     else
9244                       {
9245                         ++s;
9246                         if (*s != '$')
9247                           break;
9248                         ++s;
9249                         if (freg)
9250                           {
9251                             if (*s == 'f')
9252                               ++s;
9253                             else
9254                               {
9255                                 as_bad (_("invalid register list"));
9256                                 break;
9257                               }
9258                           }
9259                         reg2 = 0;
9260                         while (ISDIGIT (*s))
9261                           {
9262                             reg2 *= 10;
9263                             reg2 += *s - '0';
9264                             ++s;
9265                           }
9266                       }
9267                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9268                       {
9269                         mask &= ~ (7 << 3);
9270                         mask |= 5 << 3;
9271                       }
9272                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9273                       {
9274                         mask &= ~ (7 << 3);
9275                         mask |= 6 << 3;
9276                       }
9277                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9278                       mask |= (reg2 - 3) << 3;
9279                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9280                       mask |= (reg2 - 15) << 1;
9281                     else if (reg1 == RA && reg2 == RA)
9282                       mask |= 1;
9283                     else
9284                       {
9285                         as_bad (_("invalid register list"));
9286                         break;
9287                       }
9288                   }
9289                 /* The mask is filled in in the opcode table for the
9290                    benefit of the disassembler.  We remove it before
9291                    applying the actual mask.  */
9292                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9293                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9294               }
9295             continue;
9296
9297             case 'e':           /* extend code */
9298               my_getExpression (&imm_expr, s);
9299               check_absolute_expr (ip, &imm_expr);
9300               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9301                 {
9302                   as_warn (_("Invalid value for `%s' (%lu)"),
9303                            ip->insn_mo->name,
9304                            (unsigned long) imm_expr.X_add_number);
9305                   imm_expr.X_add_number &= 0x7ff;
9306                 }
9307               ip->insn_opcode |= imm_expr.X_add_number;
9308               imm_expr.X_op = O_absent;
9309               s = expr_end;
9310               continue;
9311
9312             default:
9313               internalError ();
9314             }
9315           break;
9316         }
9317
9318       /* Args don't match.  */
9319       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9320           strcmp (insn->name, insn[1].name) == 0)
9321         {
9322           ++insn;
9323           s = argsstart;
9324           continue;
9325         }
9326
9327       insn_error = _("illegal operands");
9328
9329       return;
9330     }
9331 }
9332
9333 /* This structure holds information we know about a mips16 immediate
9334    argument type.  */
9335
9336 struct mips16_immed_operand
9337 {
9338   /* The type code used in the argument string in the opcode table.  */
9339   int type;
9340   /* The number of bits in the short form of the opcode.  */
9341   int nbits;
9342   /* The number of bits in the extended form of the opcode.  */
9343   int extbits;
9344   /* The amount by which the short form is shifted when it is used;
9345      for example, the sw instruction has a shift count of 2.  */
9346   int shift;
9347   /* The amount by which the short form is shifted when it is stored
9348      into the instruction code.  */
9349   int op_shift;
9350   /* Non-zero if the short form is unsigned.  */
9351   int unsp;
9352   /* Non-zero if the extended form is unsigned.  */
9353   int extu;
9354   /* Non-zero if the value is PC relative.  */
9355   int pcrel;
9356 };
9357
9358 /* The mips16 immediate operand types.  */
9359
9360 static const struct mips16_immed_operand mips16_immed_operands[] =
9361 {
9362   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9363   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9364   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9365   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9366   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9367   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9368   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9369   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9370   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9371   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9372   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9373   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9374   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9375   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9376   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9377   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9378   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9379   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9380   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9381   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9382   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9383 };
9384
9385 #define MIPS16_NUM_IMMED \
9386   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9387
9388 /* Handle a mips16 instruction with an immediate value.  This or's the
9389    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9390    whether an extended value is needed; if one is needed, it sets
9391    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9392    If SMALL is true, an unextended opcode was explicitly requested.
9393    If EXT is true, an extended opcode was explicitly requested.  If
9394    WARN is true, warn if EXT does not match reality.  */
9395
9396 static void
9397 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9398               extend)
9399      char *file;
9400      unsigned int line;
9401      int type;
9402      offsetT val;
9403      boolean warn;
9404      boolean small;
9405      boolean ext;
9406      unsigned long *insn;
9407      boolean *use_extend;
9408      unsigned short *extend;
9409 {
9410   register const struct mips16_immed_operand *op;
9411   int mintiny, maxtiny;
9412   boolean needext;
9413
9414   op = mips16_immed_operands;
9415   while (op->type != type)
9416     {
9417       ++op;
9418       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9419     }
9420
9421   if (op->unsp)
9422     {
9423       if (type == '<' || type == '>' || type == '[' || type == ']')
9424         {
9425           mintiny = 1;
9426           maxtiny = 1 << op->nbits;
9427         }
9428       else
9429         {
9430           mintiny = 0;
9431           maxtiny = (1 << op->nbits) - 1;
9432         }
9433     }
9434   else
9435     {
9436       mintiny = - (1 << (op->nbits - 1));
9437       maxtiny = (1 << (op->nbits - 1)) - 1;
9438     }
9439
9440   /* Branch offsets have an implicit 0 in the lowest bit.  */
9441   if (type == 'p' || type == 'q')
9442     val /= 2;
9443
9444   if ((val & ((1 << op->shift) - 1)) != 0
9445       || val < (mintiny << op->shift)
9446       || val > (maxtiny << op->shift))
9447     needext = true;
9448   else
9449     needext = false;
9450
9451   if (warn && ext && ! needext)
9452     as_warn_where (file, line,
9453                    _("extended operand requested but not required"));
9454   if (small && needext)
9455     as_bad_where (file, line, _("invalid unextended operand value"));
9456
9457   if (small || (! ext && ! needext))
9458     {
9459       int insnval;
9460
9461       *use_extend = false;
9462       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9463       insnval <<= op->op_shift;
9464       *insn |= insnval;
9465     }
9466   else
9467     {
9468       long minext, maxext;
9469       int extval;
9470
9471       if (op->extu)
9472         {
9473           minext = 0;
9474           maxext = (1 << op->extbits) - 1;
9475         }
9476       else
9477         {
9478           minext = - (1 << (op->extbits - 1));
9479           maxext = (1 << (op->extbits - 1)) - 1;
9480         }
9481       if (val < minext || val > maxext)
9482         as_bad_where (file, line,
9483                       _("operand value out of range for instruction"));
9484
9485       *use_extend = true;
9486       if (op->extbits == 16)
9487         {
9488           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9489           val &= 0x1f;
9490         }
9491       else if (op->extbits == 15)
9492         {
9493           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9494           val &= 0xf;
9495         }
9496       else
9497         {
9498           extval = ((val & 0x1f) << 6) | (val & 0x20);
9499           val = 0;
9500         }
9501
9502       *extend = (unsigned short) extval;
9503       *insn |= val;
9504     }
9505 }
9506 \f
9507 static struct percent_op_match
9508 {
9509    const char *str;
9510    const enum small_ex_type type;
9511 } percent_op[] =
9512 {
9513   {"%lo", S_EX_LO},
9514 #ifdef OBJ_ELF
9515   {"%call_hi", S_EX_CALL_HI},
9516   {"%call_lo", S_EX_CALL_LO},
9517   {"%call16", S_EX_CALL16},
9518   {"%got_disp", S_EX_GOT_DISP},
9519   {"%got_page", S_EX_GOT_PAGE},
9520   {"%got_ofst", S_EX_GOT_OFST},
9521   {"%got_hi", S_EX_GOT_HI},
9522   {"%got_lo", S_EX_GOT_LO},
9523   {"%got", S_EX_GOT},
9524   {"%gp_rel", S_EX_GP_REL},
9525   {"%half", S_EX_HALF},
9526   {"%highest", S_EX_HIGHEST},
9527   {"%higher", S_EX_HIGHER},
9528   {"%neg", S_EX_NEG},
9529 #endif
9530   {"%hi", S_EX_HI}
9531 };
9532
9533 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9534    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9535    can be nested, this is handled by blanking the innermost, parsing the
9536    rest by subsequent calls.  */
9537
9538 static int
9539 my_getSmallParser (str, len, nestlevel)
9540      char **str;
9541      unsigned int *len;
9542      int *nestlevel;
9543 {
9544   *len = 0;
9545   *str += strspn (*str, " \t");
9546   /* Check for expression in parentheses.  */
9547   if (**str == '(')
9548     {
9549       char *b = *str + 1 + strspn (*str + 1, " \t");
9550       char *e;
9551
9552       /* Check for base register.  */
9553       if (b[0] == '$')
9554         {
9555           if (strchr (b, ')')
9556               && (e = b + strcspn (b, ") \t"))
9557               && e - b > 1 && e - b < 4)
9558             {
9559               if ((e - b == 3
9560                    && ((b[1] == 'f' && b[2] == 'p')
9561                        || (b[1] == 's' && b[2] == 'p')
9562                        || (b[1] == 'g' && b[2] == 'p')
9563                        || (b[1] == 'a' && b[2] == 't')
9564                        || (ISDIGIT (b[1])
9565                            && ISDIGIT (b[2]))))
9566                   || (ISDIGIT (b[1])))
9567                 {
9568                   *len = strcspn (*str, ")") + 1;
9569                   return S_EX_REGISTER;
9570                 }
9571             }
9572         }
9573       /* Check for percent_op (in parentheses).  */
9574       else if (b[0] == '%')
9575         {
9576           *str = b;
9577           return my_getPercentOp (str, len, nestlevel);
9578         }
9579
9580       /* Some other expression in the parentheses, which can contain
9581          parentheses itself. Attempt to find the matching one.  */
9582       {
9583         int pcnt = 1;
9584         char *s;
9585
9586         *len = 1;
9587         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9588           {
9589             if (*s == '(')
9590               ++pcnt;
9591             else if (*s == ')')
9592               --pcnt;
9593           }
9594       }
9595     }
9596   /* Check for percent_op (outside of parentheses).  */
9597   else if (*str[0] == '%')
9598     return my_getPercentOp (str, len, nestlevel);
9599
9600   /* Any other expression.  */
9601   return S_EX_NONE;
9602 }
9603
9604 static int
9605 my_getPercentOp (str, len, nestlevel)
9606      char **str;
9607      unsigned int *len;
9608      int *nestlevel;
9609 {
9610   char *tmp = *str + 1;
9611   unsigned int i = 0;
9612
9613   while (ISALPHA (*tmp) || *tmp == '_')
9614     {
9615       *tmp = TOLOWER (*tmp);
9616       tmp++;
9617     }
9618   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9619     {
9620       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9621         i++;
9622       else
9623         {
9624           int type = percent_op[i].type;
9625
9626           /* Only %hi and %lo are allowed for OldABI.  */
9627           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9628             return S_EX_NONE;
9629
9630           *len = strlen (percent_op[i].str);
9631           ++(*nestlevel);
9632           return type;
9633         }
9634     }
9635   return S_EX_NONE;
9636 }
9637
9638 static int
9639 my_getSmallExpression (ep, str)
9640      expressionS *ep;
9641      char *str;
9642 {
9643   static char *oldstr = NULL;
9644   int c = S_EX_NONE;
9645   int oldc;
9646   int nestlevel = -1;
9647   unsigned int len;
9648
9649   /* Don't update oldstr if the last call had nested percent_op's. We need
9650      it to parse the outer ones later.  */
9651   if (! oldstr)
9652     oldstr = str;
9653
9654   do
9655     {
9656       oldc = c;
9657       c = my_getSmallParser (&str, &len, &nestlevel);
9658       if (c != S_EX_NONE && c != S_EX_REGISTER)
9659         str += len;
9660     }
9661   while (c != S_EX_NONE && c != S_EX_REGISTER);
9662
9663   if (nestlevel >= 0)
9664     {
9665       /* A percent_op was encountered.  Don't try to get an expression if
9666          it is already blanked out.  */
9667       if (*(str + strspn (str + 1, " )")) != ')')
9668         {
9669           char save;
9670
9671           /* Let my_getExpression() stop at the closing parenthesis.  */
9672           save = *(str + len);
9673           *(str + len) = '\0';
9674           my_getExpression (ep, str);
9675           *(str + len) = save;
9676         }
9677       if (nestlevel > 0)
9678         {
9679           /* Blank out including the % sign and the proper matching
9680              parenthesis.  */
9681           int pcnt = 1;
9682           char *s = strrchr (oldstr, '%');
9683           char *end;
9684
9685           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9686             {
9687               if (*end == '(')
9688                 ++pcnt;
9689               else if (*end == ')')
9690                 --pcnt;
9691             }
9692
9693           memset (s, ' ', end - s);
9694           str = oldstr;
9695         }
9696       else
9697         expr_end = str + len;
9698
9699       c = oldc;
9700     }
9701   else if (c == S_EX_NONE)
9702     {
9703       my_getExpression (ep, str);
9704     }
9705   else if (c == S_EX_REGISTER)
9706     {
9707       ep->X_op = O_constant;
9708       expr_end = str;
9709       ep->X_add_symbol = NULL;
9710       ep->X_op_symbol = NULL;
9711       ep->X_add_number = 0;
9712     }
9713   else
9714     {
9715       as_fatal (_("internal error"));
9716     }
9717
9718   if (nestlevel <= 0)
9719     /* All percent_op's have been handled.  */
9720     oldstr = NULL;
9721
9722   return c;
9723 }
9724
9725 static void
9726 my_getExpression (ep, str)
9727      expressionS *ep;
9728      char *str;
9729 {
9730   char *save_in;
9731   valueT val;
9732
9733   save_in = input_line_pointer;
9734   input_line_pointer = str;
9735   expression (ep);
9736   expr_end = input_line_pointer;
9737   input_line_pointer = save_in;
9738
9739   /* If we are in mips16 mode, and this is an expression based on `.',
9740      then we bump the value of the symbol by 1 since that is how other
9741      text symbols are handled.  We don't bother to handle complex
9742      expressions, just `.' plus or minus a constant.  */
9743   if (mips_opts.mips16
9744       && ep->X_op == O_symbol
9745       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9746       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9747       && symbol_get_frag (ep->X_add_symbol) == frag_now
9748       && symbol_constant_p (ep->X_add_symbol)
9749       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9750     S_SET_VALUE (ep->X_add_symbol, val + 1);
9751 }
9752
9753 /* Turn a string in input_line_pointer into a floating point constant
9754    of type TYPE, and store the appropriate bytes in *LITP.  The number
9755    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9756    returned, or NULL on OK.  */
9757
9758 char *
9759 md_atof (type, litP, sizeP)
9760      int type;
9761      char *litP;
9762      int *sizeP;
9763 {
9764   int prec;
9765   LITTLENUM_TYPE words[4];
9766   char *t;
9767   int i;
9768
9769   switch (type)
9770     {
9771     case 'f':
9772       prec = 2;
9773       break;
9774
9775     case 'd':
9776       prec = 4;
9777       break;
9778
9779     default:
9780       *sizeP = 0;
9781       return _("bad call to md_atof");
9782     }
9783
9784   t = atof_ieee (input_line_pointer, type, words);
9785   if (t)
9786     input_line_pointer = t;
9787
9788   *sizeP = prec * 2;
9789
9790   if (! target_big_endian)
9791     {
9792       for (i = prec - 1; i >= 0; i--)
9793         {
9794           md_number_to_chars (litP, (valueT) words[i], 2);
9795           litP += 2;
9796         }
9797     }
9798   else
9799     {
9800       for (i = 0; i < prec; i++)
9801         {
9802           md_number_to_chars (litP, (valueT) words[i], 2);
9803           litP += 2;
9804         }
9805     }
9806
9807   return NULL;
9808 }
9809
9810 void
9811 md_number_to_chars (buf, val, n)
9812      char *buf;
9813      valueT val;
9814      int n;
9815 {
9816   if (target_big_endian)
9817     number_to_chars_bigendian (buf, val, n);
9818   else
9819     number_to_chars_littleendian (buf, val, n);
9820 }
9821 \f
9822 #ifdef OBJ_ELF
9823 static int support_64bit_objects(void)
9824 {
9825   const char **list, **l;
9826
9827   list = bfd_target_list ();
9828   for (l = list; *l != NULL; l++)
9829 #ifdef TE_TMIPS
9830     /* This is traditional mips */
9831     if (strcmp (*l, "elf64-tradbigmips") == 0
9832         || strcmp (*l, "elf64-tradlittlemips") == 0)
9833 #else
9834     if (strcmp (*l, "elf64-bigmips") == 0
9835         || strcmp (*l, "elf64-littlemips") == 0)
9836 #endif
9837       break;
9838   free (list);
9839   return (*l != NULL);
9840 }
9841 #endif /* OBJ_ELF */
9842
9843 const char *md_shortopts = "nO::g::G:";
9844
9845 struct option md_longopts[] =
9846 {
9847 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9848   {"mips0", no_argument, NULL, OPTION_MIPS1},
9849   {"mips1", no_argument, NULL, OPTION_MIPS1},
9850 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9851   {"mips2", no_argument, NULL, OPTION_MIPS2},
9852 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9853   {"mips3", no_argument, NULL, OPTION_MIPS3},
9854 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9855   {"mips4", no_argument, NULL, OPTION_MIPS4},
9856 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9857   {"mips5", no_argument, NULL, OPTION_MIPS5},
9858 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9859   {"mips32", no_argument, NULL, OPTION_MIPS32},
9860 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9861   {"mips64", no_argument, NULL, OPTION_MIPS64},
9862 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9863   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9864 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9865   {"trap", no_argument, NULL, OPTION_TRAP},
9866   {"no-break", no_argument, NULL, OPTION_TRAP},
9867 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9868   {"break", no_argument, NULL, OPTION_BREAK},
9869   {"no-trap", no_argument, NULL, OPTION_BREAK},
9870 #define OPTION_EB (OPTION_MD_BASE + 11)
9871   {"EB", no_argument, NULL, OPTION_EB},
9872 #define OPTION_EL (OPTION_MD_BASE + 12)
9873   {"EL", no_argument, NULL, OPTION_EL},
9874 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9875   {"mips16", no_argument, NULL, OPTION_MIPS16},
9876 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9877   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9878 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9879   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9880 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9881   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9882   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9883 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9884   {"mfp32", no_argument, NULL, OPTION_FP32},
9885 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9886   {"mgp32", no_argument, NULL, OPTION_GP32},
9887 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9888   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9889 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9890   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9891 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9892   {"march", required_argument, NULL, OPTION_MARCH},
9893 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9894   {"mtune", required_argument, NULL, OPTION_MTUNE},
9895 #define OPTION_FP64 (OPTION_MD_BASE + 23)
9896   {"mfp64", no_argument, NULL, OPTION_FP64},
9897 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9898   {"m4650", no_argument, NULL, OPTION_M4650},
9899 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9900   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9901 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9902   {"m4010", no_argument, NULL, OPTION_M4010},
9903 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9904   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9905 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9906   {"m4100", no_argument, NULL, OPTION_M4100},
9907 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9908   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9909 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9910   {"m3900", no_argument, NULL, OPTION_M3900},
9911 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9912   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9913 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9914   {"mgp64", no_argument, NULL, OPTION_GP64},
9915 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9916   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9917 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9918   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9919 #define OPTION_MDMX (OPTION_MD_BASE + 35)
9920   {"mdmx", no_argument, NULL, OPTION_MDMX},
9921 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9922   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9923 #ifdef OBJ_ELF
9924 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 37)
9925 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9926   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9927   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9928 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
9929   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
9930 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
9931   {"xgot",        no_argument, NULL, OPTION_XGOT},
9932 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
9933   {"mabi", required_argument, NULL, OPTION_MABI},
9934 #define OPTION_32          (OPTION_ELF_BASE + 4)
9935   {"32",          no_argument, NULL, OPTION_32},
9936 #define OPTION_N32         (OPTION_ELF_BASE + 5)
9937   {"n32",         no_argument, NULL, OPTION_N32},
9938 #define OPTION_64          (OPTION_ELF_BASE + 6)
9939   {"64",          no_argument, NULL, OPTION_64},
9940 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
9941   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
9942 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
9943   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
9944 #endif /* OBJ_ELF */
9945   {NULL, no_argument, NULL, 0}
9946 };
9947 size_t md_longopts_size = sizeof (md_longopts);
9948
9949 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
9950    NEW_VALUE.  Warn if another value was already specified.  Note:
9951    we have to defer parsing the -march and -mtune arguments in order
9952    to handle 'from-abi' correctly, since the ABI might be specified
9953    in a later argument.  */
9954
9955 static void
9956 mips_set_option_string (string_ptr, new_value)
9957      const char **string_ptr, *new_value;
9958 {
9959   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
9960     as_warn (_("A different %s was already specified, is now %s"),
9961              string_ptr == &mips_arch_string ? "-march" : "-mtune",
9962              new_value);
9963
9964   *string_ptr = new_value;
9965 }
9966
9967 int
9968 md_parse_option (c, arg)
9969      int c;
9970      char *arg;
9971 {
9972   switch (c)
9973     {
9974     case OPTION_CONSTRUCT_FLOATS:
9975       mips_disable_float_construction = 0;
9976       break;
9977
9978     case OPTION_NO_CONSTRUCT_FLOATS:
9979       mips_disable_float_construction = 1;
9980       break;
9981
9982     case OPTION_TRAP:
9983       mips_trap = 1;
9984       break;
9985
9986     case OPTION_BREAK:
9987       mips_trap = 0;
9988       break;
9989
9990     case OPTION_EB:
9991       target_big_endian = 1;
9992       break;
9993
9994     case OPTION_EL:
9995       target_big_endian = 0;
9996       break;
9997
9998     case 'n':
9999       warn_nops = 1;
10000       break;
10001
10002     case 'O':
10003       if (arg && arg[1] == '0')
10004         mips_optimize = 1;
10005       else
10006         mips_optimize = 2;
10007       break;
10008
10009     case 'g':
10010       if (arg == NULL)
10011         mips_debug = 2;
10012       else
10013         mips_debug = atoi (arg);
10014       /* When the MIPS assembler sees -g or -g2, it does not do
10015          optimizations which limit full symbolic debugging.  We take
10016          that to be equivalent to -O0.  */
10017       if (mips_debug == 2)
10018         mips_optimize = 1;
10019       break;
10020
10021     case OPTION_MIPS1:
10022       file_mips_isa = ISA_MIPS1;
10023       break;
10024
10025     case OPTION_MIPS2:
10026       file_mips_isa = ISA_MIPS2;
10027       break;
10028
10029     case OPTION_MIPS3:
10030       file_mips_isa = ISA_MIPS3;
10031       break;
10032
10033     case OPTION_MIPS4:
10034       file_mips_isa = ISA_MIPS4;
10035       break;
10036
10037     case OPTION_MIPS5:
10038       file_mips_isa = ISA_MIPS5;
10039       break;
10040
10041     case OPTION_MIPS32:
10042       file_mips_isa = ISA_MIPS32;
10043       break;
10044
10045     case OPTION_MIPS64:
10046       file_mips_isa = ISA_MIPS64;
10047       break;
10048
10049     case OPTION_MTUNE:
10050       mips_set_option_string (&mips_tune_string, arg);
10051       break;
10052
10053     case OPTION_MARCH:
10054       mips_set_option_string (&mips_arch_string, arg);
10055       break;
10056
10057     case OPTION_M4650:
10058       mips_set_option_string (&mips_arch_string, "4650");
10059       mips_set_option_string (&mips_tune_string, "4650");
10060       break;
10061
10062     case OPTION_NO_M4650:
10063       break;
10064
10065     case OPTION_M4010:
10066       mips_set_option_string (&mips_arch_string, "4010");
10067       mips_set_option_string (&mips_tune_string, "4010");
10068       break;
10069
10070     case OPTION_NO_M4010:
10071       break;
10072
10073     case OPTION_M4100:
10074       mips_set_option_string (&mips_arch_string, "4100");
10075       mips_set_option_string (&mips_tune_string, "4100");
10076       break;
10077
10078     case OPTION_NO_M4100:
10079       break;
10080
10081     case OPTION_M3900:
10082       mips_set_option_string (&mips_arch_string, "3900");
10083       mips_set_option_string (&mips_tune_string, "3900");
10084       break;
10085
10086     case OPTION_NO_M3900:
10087       break;
10088
10089     case OPTION_MDMX:
10090       mips_opts.ase_mdmx = 1;
10091       break;
10092
10093     case OPTION_NO_MDMX:
10094       mips_opts.ase_mdmx = 0;
10095       break;
10096
10097     case OPTION_MIPS16:
10098       mips_opts.mips16 = 1;
10099       mips_no_prev_insn (false);
10100       break;
10101
10102     case OPTION_NO_MIPS16:
10103       mips_opts.mips16 = 0;
10104       mips_no_prev_insn (false);
10105       break;
10106
10107     case OPTION_MIPS3D:
10108       mips_opts.ase_mips3d = 1;
10109       break;
10110
10111     case OPTION_NO_MIPS3D:
10112       mips_opts.ase_mips3d = 0;
10113       break;
10114
10115     case OPTION_MEMBEDDED_PIC:
10116       mips_pic = EMBEDDED_PIC;
10117       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10118         {
10119           as_bad (_("-G may not be used with embedded PIC code"));
10120           return 0;
10121         }
10122       g_switch_value = 0x7fffffff;
10123       break;
10124
10125 #ifdef OBJ_ELF
10126       /* When generating ELF code, we permit -KPIC and -call_shared to
10127          select SVR4_PIC, and -non_shared to select no PIC.  This is
10128          intended to be compatible with Irix 5.  */
10129     case OPTION_CALL_SHARED:
10130       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10131         {
10132           as_bad (_("-call_shared is supported only for ELF format"));
10133           return 0;
10134         }
10135       mips_pic = SVR4_PIC;
10136       if (g_switch_seen && g_switch_value != 0)
10137         {
10138           as_bad (_("-G may not be used with SVR4 PIC code"));
10139           return 0;
10140         }
10141       g_switch_value = 0;
10142       break;
10143
10144     case OPTION_NON_SHARED:
10145       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10146         {
10147           as_bad (_("-non_shared is supported only for ELF format"));
10148           return 0;
10149         }
10150       mips_pic = NO_PIC;
10151       break;
10152
10153       /* The -xgot option tells the assembler to use 32 offsets when
10154          accessing the got in SVR4_PIC mode.  It is for Irix
10155          compatibility.  */
10156     case OPTION_XGOT:
10157       mips_big_got = 1;
10158       break;
10159 #endif /* OBJ_ELF */
10160
10161     case 'G':
10162       if (! USE_GLOBAL_POINTER_OPT)
10163         {
10164           as_bad (_("-G is not supported for this configuration"));
10165           return 0;
10166         }
10167       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10168         {
10169           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10170           return 0;
10171         }
10172       else
10173         g_switch_value = atoi (arg);
10174       g_switch_seen = 1;
10175       break;
10176
10177 #ifdef OBJ_ELF
10178       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10179          and -mabi=64.  */
10180     case OPTION_32:
10181       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10182         {
10183           as_bad (_("-32 is supported for ELF format only"));
10184           return 0;
10185         }
10186       mips_abi = O32_ABI;
10187       break;
10188
10189     case OPTION_N32:
10190       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10191         {
10192           as_bad (_("-n32 is supported for ELF format only"));
10193           return 0;
10194         }
10195       mips_abi = N32_ABI;
10196       break;
10197
10198     case OPTION_64:
10199       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10200         {
10201           as_bad (_("-64 is supported for ELF format only"));
10202           return 0;
10203         }
10204       mips_abi = N64_ABI;
10205       if (! support_64bit_objects())
10206         as_fatal (_("No compiled in support for 64 bit object file format"));
10207       break;
10208 #endif /* OBJ_ELF */
10209
10210     case OPTION_GP32:
10211       file_mips_gp32 = 1;
10212       break;
10213
10214     case OPTION_GP64:
10215       file_mips_gp32 = 0;
10216       break;
10217
10218     case OPTION_FP32:
10219       file_mips_fp32 = 1;
10220       break;
10221
10222     case OPTION_FP64:
10223       file_mips_fp32 = 0;
10224       break;
10225
10226 #ifdef OBJ_ELF
10227     case OPTION_MABI:
10228       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10229         {
10230           as_bad (_("-mabi is supported for ELF format only"));
10231           return 0;
10232         }
10233       if (strcmp (arg, "32") == 0)
10234         mips_abi = O32_ABI;
10235       else if (strcmp (arg, "o64") == 0)
10236         mips_abi = O64_ABI;
10237       else if (strcmp (arg, "n32") == 0)
10238         mips_abi = N32_ABI;
10239       else if (strcmp (arg, "64") == 0)
10240         {
10241           mips_abi = N64_ABI;
10242           if (! support_64bit_objects())
10243             as_fatal (_("No compiled in support for 64 bit object file "
10244                         "format"));
10245         }
10246       else if (strcmp (arg, "eabi") == 0)
10247         mips_abi = EABI_ABI;
10248       else
10249         {
10250           as_fatal (_("invalid abi -mabi=%s"), arg);
10251           return 0;
10252         }
10253       break;
10254 #endif /* OBJ_ELF */
10255
10256     case OPTION_M7000_HILO_FIX:
10257       mips_7000_hilo_fix = true;
10258       break;
10259
10260     case OPTION_MNO_7000_HILO_FIX:
10261       mips_7000_hilo_fix = false;
10262       break;
10263
10264 #ifdef OBJ_ELF
10265     case OPTION_MDEBUG:
10266       mips_flag_mdebug = true;
10267       break;
10268
10269     case OPTION_NO_MDEBUG:
10270       mips_flag_mdebug = false;
10271       break;
10272 #endif /* OBJ_ELF */
10273
10274     default:
10275       return 0;
10276     }
10277
10278   return 1;
10279 }
10280 \f
10281 /* Set up globals to generate code for the ISA or processor
10282    described by INFO.  */
10283
10284 static void
10285 mips_set_architecture (info)
10286      const struct mips_cpu_info *info;
10287 {
10288   if (info != 0)
10289     {
10290       mips_arch_info = info;
10291       mips_arch = info->cpu;
10292       mips_opts.isa = info->isa;
10293     }
10294 }
10295
10296
10297 /* Likewise for tuning.  */
10298
10299 static void
10300 mips_set_tune (info)
10301      const struct mips_cpu_info *info;
10302 {
10303   if (info != 0)
10304     {
10305       mips_tune_info = info;
10306       mips_tune = info->cpu;
10307     }
10308 }
10309
10310
10311 void
10312 mips_after_parse_args ()
10313 {
10314   /* GP relative stuff not working for PE */
10315   if (strncmp (TARGET_OS, "pe", 2) == 0
10316       && g_switch_value != 0)
10317     {
10318       if (g_switch_seen)
10319         as_bad (_("-G not supported in this configuration."));
10320       g_switch_value = 0;
10321     }
10322
10323   /* The following code determines the architecture and register size.
10324      Similar code was added to GCC 3.3 (see override_options() in
10325      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10326      as much as possible.  */
10327
10328   if (mips_arch_string != 0)
10329     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10330
10331   if (mips_tune_string != 0)
10332     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10333
10334   if (file_mips_isa != ISA_UNKNOWN)
10335     {
10336       /* Handle -mipsN.  At this point, file_mips_isa contains the
10337          ISA level specified by -mipsN, while mips_opts.isa contains
10338          the -march selection (if any).  */
10339       if (mips_arch_info != 0)
10340         {
10341           /* -march takes precedence over -mipsN, since it is more descriptive.
10342              There's no harm in specifying both as long as the ISA levels
10343              are the same.  */
10344           if (file_mips_isa != mips_opts.isa)
10345             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10346                     mips_cpu_info_from_isa (file_mips_isa)->name,
10347                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10348         }
10349       else
10350         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10351     }
10352
10353   if (mips_arch_info == 0)
10354     mips_set_architecture (mips_parse_cpu ("default CPU",
10355                                            MIPS_CPU_STRING_DEFAULT));
10356
10357   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10358     as_bad ("-march=%s is not compatible with the selected ABI",
10359             mips_arch_info->name);
10360
10361   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10362   if (mips_tune_info == 0)
10363     mips_set_tune (mips_arch_info);
10364
10365   if (file_mips_gp32 >= 0)
10366     {
10367       /* The user specified the size of the integer registers.  Make sure
10368          it agrees with the ABI and ISA.  */
10369       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10370         as_bad (_("-mgp64 used with a 32-bit processor"));
10371       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10372         as_bad (_("-mgp32 used with a 64-bit ABI"));
10373       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10374         as_bad (_("-mgp64 used with a 32-bit ABI"));
10375     }
10376   else
10377     {
10378       /* Infer the integer register size from the ABI and processor.
10379          Restrict ourselves to 32-bit registers if that's all the
10380          processor has, or if the ABI cannot handle 64-bit registers.  */
10381       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10382                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10383     }
10384
10385   /* ??? GAS treats single-float processors as though they had 64-bit
10386      float registers (although it complains when double-precision
10387      instructions are used).  As things stand, saying they have 32-bit
10388      registers would lead to spurious "register must be even" messages.
10389      So here we assume float registers are always the same size as
10390      integer ones, unless the user says otherwise.  */
10391   if (file_mips_fp32 < 0)
10392     file_mips_fp32 = file_mips_gp32;
10393
10394   /* End of GCC-shared inference code.  */
10395
10396   /* ??? When do we want this flag to be set?   Who uses it?  */
10397   if (file_mips_gp32 == 1
10398       && mips_abi == NO_ABI
10399       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10400     mips_32bitmode = 1;
10401
10402   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10403     as_bad (_("trap exception not supported at ISA 1"));
10404
10405   /* If the selected architecture includes support for ASEs, enable
10406      generation of code for them.  */
10407   if (mips_opts.mips16 == -1)
10408     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10409   if (mips_opts.ase_mips3d == -1)
10410     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10411   if (mips_opts.ase_mdmx == -1)
10412     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10413
10414   file_mips_isa = mips_opts.isa;
10415   file_ase_mips16 = mips_opts.mips16;
10416   file_ase_mips3d = mips_opts.ase_mips3d;
10417   file_ase_mdmx = mips_opts.ase_mdmx;
10418   mips_opts.gp32 = file_mips_gp32;
10419   mips_opts.fp32 = file_mips_fp32;
10420
10421   if (HAVE_NEWABI)
10422     mips_big_got = 1;
10423
10424   if (mips_flag_mdebug < 0)
10425     {
10426 #ifdef OBJ_MAYBE_ECOFF
10427       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10428         mips_flag_mdebug = 1;
10429       else
10430 #endif /* OBJ_MAYBE_ECOFF */
10431         mips_flag_mdebug = 0;
10432     }
10433 }
10434 \f
10435 void
10436 mips_init_after_args ()
10437 {
10438   /* initialize opcodes */
10439   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10440   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10441 }
10442
10443 long
10444 md_pcrel_from (fixP)
10445      fixS *fixP;
10446 {
10447   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10448       && fixP->fx_addsy != (symbolS *) NULL
10449       && ! S_IS_DEFINED (fixP->fx_addsy))
10450     {
10451       /* This makes a branch to an undefined symbol be a branch to the
10452          current location.  */
10453       if (mips_pic == EMBEDDED_PIC)
10454         return 4;
10455       else
10456         return 1;
10457     }
10458
10459   /* Return the address of the delay slot.  */
10460   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10461 }
10462
10463 /* This is called before the symbol table is processed.  In order to
10464    work with gcc when using mips-tfile, we must keep all local labels.
10465    However, in other cases, we want to discard them.  If we were
10466    called with -g, but we didn't see any debugging information, it may
10467    mean that gcc is smuggling debugging information through to
10468    mips-tfile, in which case we must generate all local labels.  */
10469
10470 void
10471 mips_frob_file_before_adjust ()
10472 {
10473 #ifndef NO_ECOFF_DEBUGGING
10474   if (ECOFF_DEBUGGING
10475       && mips_debug != 0
10476       && ! ecoff_debugging_seen)
10477     flag_keep_locals = 1;
10478 #endif
10479 }
10480
10481 /* Sort any unmatched HI16_S relocs so that they immediately precede
10482    the corresponding LO reloc.  This is called before md_apply_fix3 and
10483    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10484    explicit use of the %hi modifier.  */
10485
10486 void
10487 mips_frob_file ()
10488 {
10489   struct mips_hi_fixup *l;
10490
10491   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10492     {
10493       segment_info_type *seginfo;
10494       int pass;
10495
10496       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10497
10498       /* Check quickly whether the next fixup happens to be a matching
10499          %lo.  */
10500       if (l->fixp->fx_next != NULL
10501           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10502           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10503           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10504         continue;
10505
10506       /* Look through the fixups for this segment for a matching %lo.
10507          When we find one, move the %hi just in front of it.  We do
10508          this in two passes.  In the first pass, we try to find a
10509          unique %lo.  In the second pass, we permit multiple %hi
10510          relocs for a single %lo (this is a GNU extension).  */
10511       seginfo = seg_info (l->seg);
10512       for (pass = 0; pass < 2; pass++)
10513         {
10514           fixS *f, *prev;
10515
10516           prev = NULL;
10517           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10518             {
10519               /* Check whether this is a %lo fixup which matches l->fixp.  */
10520               if (f->fx_r_type == BFD_RELOC_LO16
10521                   && f->fx_addsy == l->fixp->fx_addsy
10522                   && f->fx_offset == l->fixp->fx_offset
10523                   && (pass == 1
10524                       || prev == NULL
10525                       || prev->fx_r_type != BFD_RELOC_HI16_S
10526                       || prev->fx_addsy != f->fx_addsy
10527                       || prev->fx_offset !=  f->fx_offset))
10528                 {
10529                   fixS **pf;
10530
10531                   /* Move l->fixp before f.  */
10532                   for (pf = &seginfo->fix_root;
10533                        *pf != l->fixp;
10534                        pf = &(*pf)->fx_next)
10535                     assert (*pf != NULL);
10536
10537                   *pf = l->fixp->fx_next;
10538
10539                   l->fixp->fx_next = f;
10540                   if (prev == NULL)
10541                     seginfo->fix_root = l->fixp;
10542                   else
10543                     prev->fx_next = l->fixp;
10544
10545                   break;
10546                 }
10547
10548               prev = f;
10549             }
10550
10551           if (f != NULL)
10552             break;
10553
10554 #if 0 /* GCC code motion plus incomplete dead code elimination
10555          can leave a %hi without a %lo.  */
10556           if (pass == 1)
10557             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10558                            _("Unmatched %%hi reloc"));
10559 #endif
10560         }
10561     }
10562 }
10563
10564 /* When generating embedded PIC code we need to use a special
10565    relocation to represent the difference of two symbols in the .text
10566    section (switch tables use a difference of this sort).  See
10567    include/coff/mips.h for details.  This macro checks whether this
10568    fixup requires the special reloc.  */
10569 #define SWITCH_TABLE(fixp) \
10570   ((fixp)->fx_r_type == BFD_RELOC_32 \
10571    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10572    && (fixp)->fx_addsy != NULL \
10573    && (fixp)->fx_subsy != NULL \
10574    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10575    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10576
10577 /* When generating embedded PIC code we must keep all PC relative
10578    relocations, in case the linker has to relax a call.  We also need
10579    to keep relocations for switch table entries.
10580
10581    We may have combined relocations without symbols in the N32/N64 ABI.
10582    We have to prevent gas from dropping them.  */
10583
10584 int
10585 mips_force_relocation (fixp)
10586      fixS *fixp;
10587 {
10588   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10589       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10590       || S_FORCE_RELOC (fixp->fx_addsy))
10591     return 1;
10592
10593   if (HAVE_NEWABI
10594       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10595       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10596           || fixp->fx_r_type == BFD_RELOC_HI16_S
10597           || fixp->fx_r_type == BFD_RELOC_LO16))
10598     return 1;
10599
10600   return (mips_pic == EMBEDDED_PIC
10601           && (fixp->fx_pcrel
10602               || SWITCH_TABLE (fixp)
10603               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10604               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10605 }
10606
10607 #ifdef OBJ_ELF
10608 static int
10609 mips_need_elf_addend_fixup (fixP)
10610      fixS *fixP;
10611 {
10612   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10613     return 1;
10614   if (mips_pic == EMBEDDED_PIC
10615       && S_IS_WEAK (fixP->fx_addsy))
10616     return 1;
10617   if (mips_pic != EMBEDDED_PIC
10618       && (S_IS_WEAK (fixP->fx_addsy)
10619           || S_IS_EXTERNAL (fixP->fx_addsy))
10620       && !S_IS_COMMON (fixP->fx_addsy))
10621     return 1;
10622   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10623       && (((bfd_get_section_flags (stdoutput,
10624                                    S_GET_SEGMENT (fixP->fx_addsy))
10625             & SEC_LINK_ONCE) != 0)
10626           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10627                        ".gnu.linkonce",
10628                        sizeof (".gnu.linkonce") - 1)))
10629     return 1;
10630   return 0;
10631 }
10632 #endif
10633
10634 /* Apply a fixup to the object file.  */
10635
10636 void
10637 md_apply_fix3 (fixP, valP, seg)
10638      fixS *fixP;
10639      valueT *valP;
10640      segT seg ATTRIBUTE_UNUSED;
10641 {
10642   bfd_byte *buf;
10643   long insn;
10644   valueT value;
10645
10646   assert (fixP->fx_size == 4
10647           || fixP->fx_r_type == BFD_RELOC_16
10648           || fixP->fx_r_type == BFD_RELOC_32
10649           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10650           || fixP->fx_r_type == BFD_RELOC_HI16_S
10651           || fixP->fx_r_type == BFD_RELOC_LO16
10652           || fixP->fx_r_type == BFD_RELOC_GPREL16
10653           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10654           || fixP->fx_r_type == BFD_RELOC_GPREL32
10655           || fixP->fx_r_type == BFD_RELOC_64
10656           || fixP->fx_r_type == BFD_RELOC_CTOR
10657           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10658           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10659           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10660           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10661           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10662           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10663           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10664           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10665           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10666
10667   value = *valP;
10668
10669   /* If we aren't adjusting this fixup to be against the section
10670      symbol, we need to adjust the value.  */
10671 #ifdef OBJ_ELF
10672   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10673     {
10674       if (mips_need_elf_addend_fixup (fixP))
10675         {
10676           reloc_howto_type *howto;
10677           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10678
10679           value -= symval;
10680
10681           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10682           if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
10683             {
10684               /* In this case, the bfd_install_relocation routine will
10685                  incorrectly add the symbol value back in.  We just want
10686                  the addend to appear in the object file.  */
10687               value -= symval;
10688
10689               /* Make sure the addend is still non-zero.  If it became zero
10690                  after the last operation, set it to a spurious value and
10691                  subtract the same value from the object file's contents.  */
10692               if (value == 0)
10693                 {
10694                   value = 8;
10695
10696                   /* The in-place addends for LO16 relocations are signed;
10697                      leave the matching HI16 in-place addends as zero.  */
10698                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10699                     {
10700                       bfd_vma contents, mask, field;
10701
10702                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10703                                                + fixP->fx_where,
10704                                                fixP->fx_size * 8,
10705                                                target_big_endian);
10706
10707                       /* MASK has bits set where the relocation should go.
10708                          FIELD is -value, shifted into the appropriate place
10709                          for this relocation.  */
10710                       mask = 1 << (howto->bitsize - 1);
10711                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10712                       field = (-value >> howto->rightshift) << howto->bitpos;
10713
10714                       bfd_put_bits ((field & mask) | (contents & ~mask),
10715                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10716                                     fixP->fx_size * 8,
10717                                     target_big_endian);
10718                     }
10719                 }
10720             }
10721         }
10722
10723       /* This code was generated using trial and error and so is
10724          fragile and not trustworthy.  If you change it, you should
10725          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10726          they still pass.  */
10727       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10728         {
10729           value += fixP->fx_frag->fr_address + fixP->fx_where;
10730
10731           /* BFD's REL handling, for MIPS, is _very_ weird.
10732              This gives the right results, but it can't possibly
10733              be the way things are supposed to work.  */
10734           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10735                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10736               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10737             value += fixP->fx_frag->fr_address + fixP->fx_where;
10738         }
10739     }
10740 #endif
10741
10742   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10743
10744   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10745     fixP->fx_done = 1;
10746
10747   switch (fixP->fx_r_type)
10748     {
10749     case BFD_RELOC_MIPS_JMP:
10750     case BFD_RELOC_MIPS_SHIFT5:
10751     case BFD_RELOC_MIPS_SHIFT6:
10752     case BFD_RELOC_MIPS_GOT_DISP:
10753     case BFD_RELOC_MIPS_GOT_PAGE:
10754     case BFD_RELOC_MIPS_GOT_OFST:
10755     case BFD_RELOC_MIPS_SUB:
10756     case BFD_RELOC_MIPS_INSERT_A:
10757     case BFD_RELOC_MIPS_INSERT_B:
10758     case BFD_RELOC_MIPS_DELETE:
10759     case BFD_RELOC_MIPS_HIGHEST:
10760     case BFD_RELOC_MIPS_HIGHER:
10761     case BFD_RELOC_MIPS_SCN_DISP:
10762     case BFD_RELOC_MIPS_REL16:
10763     case BFD_RELOC_MIPS_RELGOT:
10764     case BFD_RELOC_MIPS_JALR:
10765     case BFD_RELOC_HI16:
10766     case BFD_RELOC_HI16_S:
10767     case BFD_RELOC_GPREL16:
10768     case BFD_RELOC_MIPS_LITERAL:
10769     case BFD_RELOC_MIPS_CALL16:
10770     case BFD_RELOC_MIPS_GOT16:
10771     case BFD_RELOC_GPREL32:
10772     case BFD_RELOC_MIPS_GOT_HI16:
10773     case BFD_RELOC_MIPS_GOT_LO16:
10774     case BFD_RELOC_MIPS_CALL_HI16:
10775     case BFD_RELOC_MIPS_CALL_LO16:
10776     case BFD_RELOC_MIPS16_GPREL:
10777       if (fixP->fx_pcrel)
10778         as_bad_where (fixP->fx_file, fixP->fx_line,
10779                       _("Invalid PC relative reloc"));
10780       /* Nothing needed to do. The value comes from the reloc entry */
10781       break;
10782
10783     case BFD_RELOC_MIPS16_JMP:
10784       /* We currently always generate a reloc against a symbol, which
10785          means that we don't want an addend even if the symbol is
10786          defined.  */
10787       fixP->fx_addnumber = 0;
10788       break;
10789
10790     case BFD_RELOC_PCREL_HI16_S:
10791       /* The addend for this is tricky if it is internal, so we just
10792          do everything here rather than in bfd_install_relocation.  */
10793       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10794           && !fixP->fx_done
10795           && value != 0)
10796         break;
10797       if (fixP->fx_addsy
10798           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10799         {
10800           /* For an external symbol adjust by the address to make it
10801              pcrel_offset.  We use the address of the RELLO reloc
10802              which follows this one.  */
10803           value += (fixP->fx_next->fx_frag->fr_address
10804                     + fixP->fx_next->fx_where);
10805         }
10806       value = ((value + 0x8000) >> 16) & 0xffff;
10807       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10808       if (target_big_endian)
10809         buf += 2;
10810       md_number_to_chars ((char *) buf, value, 2);
10811       break;
10812
10813     case BFD_RELOC_PCREL_LO16:
10814       /* The addend for this is tricky if it is internal, so we just
10815          do everything here rather than in bfd_install_relocation.  */
10816       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10817           && !fixP->fx_done
10818           && value != 0)
10819         break;
10820       if (fixP->fx_addsy
10821           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10822         value += fixP->fx_frag->fr_address + fixP->fx_where;
10823       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10824       if (target_big_endian)
10825         buf += 2;
10826       md_number_to_chars ((char *) buf, value, 2);
10827       break;
10828
10829     case BFD_RELOC_64:
10830       /* This is handled like BFD_RELOC_32, but we output a sign
10831          extended value if we are only 32 bits.  */
10832       if (fixP->fx_done
10833           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10834         {
10835           if (8 <= sizeof (valueT))
10836             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10837                                 value, 8);
10838           else
10839             {
10840               long w1, w2;
10841               long hiv;
10842
10843               w1 = w2 = fixP->fx_where;
10844               if (target_big_endian)
10845                 w1 += 4;
10846               else
10847                 w2 += 4;
10848               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10849               if ((value & 0x80000000) != 0)
10850                 hiv = 0xffffffff;
10851               else
10852                 hiv = 0;
10853               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10854             }
10855         }
10856       break;
10857
10858     case BFD_RELOC_RVA:
10859     case BFD_RELOC_32:
10860       /* If we are deleting this reloc entry, we must fill in the
10861          value now.  This can happen if we have a .word which is not
10862          resolved when it appears but is later defined.  We also need
10863          to fill in the value if this is an embedded PIC switch table
10864          entry.  */
10865       if (fixP->fx_done
10866           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10867         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10868                             value, 4);
10869       break;
10870
10871     case BFD_RELOC_16:
10872       /* If we are deleting this reloc entry, we must fill in the
10873          value now.  */
10874       assert (fixP->fx_size == 2);
10875       if (fixP->fx_done)
10876         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10877                             value, 2);
10878       break;
10879
10880     case BFD_RELOC_LO16:
10881       /* When handling an embedded PIC switch statement, we can wind
10882          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
10883       if (fixP->fx_done)
10884         {
10885           if (value + 0x8000 > 0xffff)
10886             as_bad_where (fixP->fx_file, fixP->fx_line,
10887                           _("relocation overflow"));
10888           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10889           if (target_big_endian)
10890             buf += 2;
10891           md_number_to_chars ((char *) buf, value, 2);
10892         }
10893       break;
10894
10895     case BFD_RELOC_16_PCREL_S2:
10896       if ((value & 0x3) != 0)
10897         as_bad_where (fixP->fx_file, fixP->fx_line,
10898                       _("Branch to odd address (%lx)"), (long) value);
10899
10900       /* Fall through.  */
10901
10902     case BFD_RELOC_16_PCREL:
10903       /*
10904        * We need to save the bits in the instruction since fixup_segment()
10905        * might be deleting the relocation entry (i.e., a branch within
10906        * the current segment).
10907        */
10908       if (!fixP->fx_done && value != 0)
10909         break;
10910       /* If 'value' is zero, the remaining reloc code won't actually
10911          do the store, so it must be done here.  This is probably
10912          a bug somewhere.  */
10913       if (!fixP->fx_done
10914           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10915               || fixP->fx_addsy == NULL                 /* ??? */
10916               || ! S_IS_DEFINED (fixP->fx_addsy)))
10917         value -= fixP->fx_frag->fr_address + fixP->fx_where;
10918
10919       value = (offsetT) value >> 2;
10920
10921       /* update old instruction data */
10922       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10923       if (target_big_endian)
10924         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10925       else
10926         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10927
10928       if (value + 0x8000 <= 0xffff)
10929         insn |= value & 0xffff;
10930       else
10931         {
10932           /* The branch offset is too large.  If this is an
10933              unconditional branch, and we are not generating PIC code,
10934              we can convert it to an absolute jump instruction.  */
10935           if (mips_pic == NO_PIC
10936               && fixP->fx_done
10937               && fixP->fx_frag->fr_address >= text_section->vma
10938               && (fixP->fx_frag->fr_address
10939                   < text_section->vma + text_section->_raw_size)
10940               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
10941                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
10942                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10943             {
10944               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
10945                 insn = 0x0c000000;      /* jal */
10946               else
10947                 insn = 0x08000000;      /* j */
10948               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10949               fixP->fx_done = 0;
10950               fixP->fx_addsy = section_symbol (text_section);
10951               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10952             }
10953           else
10954             {
10955               /* FIXME.  It would be possible in principle to handle
10956                  conditional branches which overflow.  They could be
10957                  transformed into a branch around a jump.  This would
10958                  require setting up variant frags for each different
10959                  branch type.  The native MIPS assembler attempts to
10960                  handle these cases, but it appears to do it
10961                  incorrectly.  */
10962               as_bad_where (fixP->fx_file, fixP->fx_line,
10963                             _("Branch out of range"));
10964             }
10965         }
10966
10967       md_number_to_chars ((char *) buf, (valueT) insn, 4);
10968       break;
10969
10970     case BFD_RELOC_VTABLE_INHERIT:
10971       fixP->fx_done = 0;
10972       if (fixP->fx_addsy
10973           && !S_IS_DEFINED (fixP->fx_addsy)
10974           && !S_IS_WEAK (fixP->fx_addsy))
10975         S_SET_WEAK (fixP->fx_addsy);
10976       break;
10977
10978     case BFD_RELOC_VTABLE_ENTRY:
10979       fixP->fx_done = 0;
10980       break;
10981
10982     default:
10983       internalError ();
10984     }
10985 }
10986
10987 #if 0
10988 void
10989 printInsn (oc)
10990      unsigned long oc;
10991 {
10992   const struct mips_opcode *p;
10993   int treg, sreg, dreg, shamt;
10994   short imm;
10995   const char *args;
10996   int i;
10997
10998   for (i = 0; i < NUMOPCODES; ++i)
10999     {
11000       p = &mips_opcodes[i];
11001       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11002         {
11003           printf ("%08lx %s\t", oc, p->name);
11004           treg = (oc >> 16) & 0x1f;
11005           sreg = (oc >> 21) & 0x1f;
11006           dreg = (oc >> 11) & 0x1f;
11007           shamt = (oc >> 6) & 0x1f;
11008           imm = oc;
11009           for (args = p->args;; ++args)
11010             {
11011               switch (*args)
11012                 {
11013                 case '\0':
11014                   printf ("\n");
11015                   break;
11016
11017                 case ',':
11018                 case '(':
11019                 case ')':
11020                   printf ("%c", *args);
11021                   continue;
11022
11023                 case 'r':
11024                   assert (treg == sreg);
11025                   printf ("$%d,$%d", treg, sreg);
11026                   continue;
11027
11028                 case 'd':
11029                 case 'G':
11030                   printf ("$%d", dreg);
11031                   continue;
11032
11033                 case 't':
11034                 case 'E':
11035                   printf ("$%d", treg);
11036                   continue;
11037
11038                 case 'k':
11039                   printf ("0x%x", treg);
11040                   continue;
11041
11042                 case 'b':
11043                 case 's':
11044                   printf ("$%d", sreg);
11045                   continue;
11046
11047                 case 'a':
11048                   printf ("0x%08lx", oc & 0x1ffffff);
11049                   continue;
11050
11051                 case 'i':
11052                 case 'j':
11053                 case 'o':
11054                 case 'u':
11055                   printf ("%d", imm);
11056                   continue;
11057
11058                 case '<':
11059                 case '>':
11060                   printf ("$%d", shamt);
11061                   continue;
11062
11063                 default:
11064                   internalError ();
11065                 }
11066               break;
11067             }
11068           return;
11069         }
11070     }
11071   printf (_("%08lx  UNDEFINED\n"), oc);
11072 }
11073 #endif
11074
11075 static symbolS *
11076 get_symbol ()
11077 {
11078   int c;
11079   char *name;
11080   symbolS *p;
11081
11082   name = input_line_pointer;
11083   c = get_symbol_end ();
11084   p = (symbolS *) symbol_find_or_make (name);
11085   *input_line_pointer = c;
11086   return p;
11087 }
11088
11089 /* Align the current frag to a given power of two.  The MIPS assembler
11090    also automatically adjusts any preceding label.  */
11091
11092 static void
11093 mips_align (to, fill, label)
11094      int to;
11095      int fill;
11096      symbolS *label;
11097 {
11098   mips_emit_delays (false);
11099   frag_align (to, fill, 0);
11100   record_alignment (now_seg, to);
11101   if (label != NULL)
11102     {
11103       assert (S_GET_SEGMENT (label) == now_seg);
11104       symbol_set_frag (label, frag_now);
11105       S_SET_VALUE (label, (valueT) frag_now_fix ());
11106     }
11107 }
11108
11109 /* Align to a given power of two.  .align 0 turns off the automatic
11110    alignment used by the data creating pseudo-ops.  */
11111
11112 static void
11113 s_align (x)
11114      int x ATTRIBUTE_UNUSED;
11115 {
11116   register int temp;
11117   register long temp_fill;
11118   long max_alignment = 15;
11119
11120   /*
11121
11122     o  Note that the assembler pulls down any immediately preceeding label
11123        to the aligned address.
11124     o  It's not documented but auto alignment is reinstated by
11125        a .align pseudo instruction.
11126     o  Note also that after auto alignment is turned off the mips assembler
11127        issues an error on attempt to assemble an improperly aligned data item.
11128        We don't.
11129
11130     */
11131
11132   temp = get_absolute_expression ();
11133   if (temp > max_alignment)
11134     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11135   else if (temp < 0)
11136     {
11137       as_warn (_("Alignment negative: 0 assumed."));
11138       temp = 0;
11139     }
11140   if (*input_line_pointer == ',')
11141     {
11142       ++input_line_pointer;
11143       temp_fill = get_absolute_expression ();
11144     }
11145   else
11146     temp_fill = 0;
11147   if (temp)
11148     {
11149       auto_align = 1;
11150       mips_align (temp, (int) temp_fill,
11151                   insn_labels != NULL ? insn_labels->label : NULL);
11152     }
11153   else
11154     {
11155       auto_align = 0;
11156     }
11157
11158   demand_empty_rest_of_line ();
11159 }
11160
11161 void
11162 mips_flush_pending_output ()
11163 {
11164   mips_emit_delays (false);
11165   mips_clear_insn_labels ();
11166 }
11167
11168 static void
11169 s_change_sec (sec)
11170      int sec;
11171 {
11172   segT seg;
11173
11174   /* When generating embedded PIC code, we only use the .text, .lit8,
11175      .sdata and .sbss sections.  We change the .data and .rdata
11176      pseudo-ops to use .sdata.  */
11177   if (mips_pic == EMBEDDED_PIC
11178       && (sec == 'd' || sec == 'r'))
11179     sec = 's';
11180
11181 #ifdef OBJ_ELF
11182   /* The ELF backend needs to know that we are changing sections, so
11183      that .previous works correctly.  We could do something like check
11184      for an obj_section_change_hook macro, but that might be confusing
11185      as it would not be appropriate to use it in the section changing
11186      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11187      This should be cleaner, somehow.  */
11188   obj_elf_section_change_hook ();
11189 #endif
11190
11191   mips_emit_delays (false);
11192   switch (sec)
11193     {
11194     case 't':
11195       s_text (0);
11196       break;
11197     case 'd':
11198       s_data (0);
11199       break;
11200     case 'b':
11201       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11202       demand_empty_rest_of_line ();
11203       break;
11204
11205     case 'r':
11206       if (USE_GLOBAL_POINTER_OPT)
11207         {
11208           seg = subseg_new (RDATA_SECTION_NAME,
11209                             (subsegT) get_absolute_expression ());
11210           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11211             {
11212               bfd_set_section_flags (stdoutput, seg,
11213                                      (SEC_ALLOC
11214                                       | SEC_LOAD
11215                                       | SEC_READONLY
11216                                       | SEC_RELOC
11217                                       | SEC_DATA));
11218               if (strcmp (TARGET_OS, "elf") != 0)
11219                 record_alignment (seg, 4);
11220             }
11221           demand_empty_rest_of_line ();
11222         }
11223       else
11224         {
11225           as_bad (_("No read only data section in this object file format"));
11226           demand_empty_rest_of_line ();
11227           return;
11228         }
11229       break;
11230
11231     case 's':
11232       if (USE_GLOBAL_POINTER_OPT)
11233         {
11234           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11235           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11236             {
11237               bfd_set_section_flags (stdoutput, seg,
11238                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11239                                      | SEC_DATA);
11240               if (strcmp (TARGET_OS, "elf") != 0)
11241                 record_alignment (seg, 4);
11242             }
11243           demand_empty_rest_of_line ();
11244           break;
11245         }
11246       else
11247         {
11248           as_bad (_("Global pointers not supported; recompile -G 0"));
11249           demand_empty_rest_of_line ();
11250           return;
11251         }
11252     }
11253
11254   auto_align = 1;
11255 }
11256   
11257 void
11258 s_change_section (ignore)
11259      int ignore ATTRIBUTE_UNUSED;
11260 {
11261   expressionS rep_exp;
11262   
11263   char *section_name;
11264   char c;
11265   char *next_c;
11266   char *p;
11267   int section_type;
11268   int section_flag;
11269   int section_entry_size;
11270   int section_alignment;
11271   int log = -1;
11272   flagword flags;
11273   
11274   section_name = input_line_pointer;
11275   c = get_symbol_end ();
11276   next_c = input_line_pointer + 1;
11277   /* just after name is now '\0' */
11278   p = input_line_pointer;
11279
11280   /* Do we have .section Name<,"flags">  */
11281   if (c == '\n' || (c == ',' && *next_c == '"') || c == '"')
11282     {
11283       *p = c;
11284       input_line_pointer = section_name;
11285       obj_elf_section (ignore);
11286       return;
11287     }
11288   input_line_pointer++;
11289
11290   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11291   if (c == ',')
11292     section_type = get_absolute_expression ();
11293   else
11294     section_type = 0;
11295   if (*input_line_pointer++ == ',')
11296     section_flag = get_absolute_expression ();
11297   else
11298     section_flag = 0;
11299   if (*input_line_pointer++ == ',')
11300     section_entry_size = get_absolute_expression ();
11301   else
11302     section_entry_size = 0;
11303   if (*input_line_pointer++ == ',')
11304     section_alignment = get_absolute_expression ();
11305   else
11306     section_alignment = 0;
11307
11308   obj_elf_change_section (section_name, section_type, section_flag,
11309                           section_entry_size, 0, 0, 0);
11310 }
11311
11312 void
11313 mips_enable_auto_align ()
11314 {
11315   auto_align = 1;
11316 }
11317
11318 static void
11319 s_cons (log_size)
11320      int log_size;
11321 {
11322   symbolS *label;
11323
11324   label = insn_labels != NULL ? insn_labels->label : NULL;
11325   mips_emit_delays (false);
11326   if (log_size > 0 && auto_align)
11327     mips_align (log_size, 0, label);
11328   mips_clear_insn_labels ();
11329   cons (1 << log_size);
11330 }
11331
11332 static void
11333 s_float_cons (type)
11334      int type;
11335 {
11336   symbolS *label;
11337
11338   label = insn_labels != NULL ? insn_labels->label : NULL;
11339
11340   mips_emit_delays (false);
11341
11342   if (auto_align)
11343     {
11344       if (type == 'd')
11345         mips_align (3, 0, label);
11346       else
11347         mips_align (2, 0, label);
11348     }
11349
11350   mips_clear_insn_labels ();
11351
11352   float_cons (type);
11353 }
11354
11355 /* Handle .globl.  We need to override it because on Irix 5 you are
11356    permitted to say
11357        .globl foo .text
11358    where foo is an undefined symbol, to mean that foo should be
11359    considered to be the address of a function.  */
11360
11361 static void
11362 s_mips_globl (x)
11363      int x ATTRIBUTE_UNUSED;
11364 {
11365   char *name;
11366   int c;
11367   symbolS *symbolP;
11368   flagword flag;
11369
11370   name = input_line_pointer;
11371   c = get_symbol_end ();
11372   symbolP = symbol_find_or_make (name);
11373   *input_line_pointer = c;
11374   SKIP_WHITESPACE ();
11375
11376   /* On Irix 5, every global symbol that is not explicitly labelled as
11377      being a function is apparently labelled as being an object.  */
11378   flag = BSF_OBJECT;
11379
11380   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11381     {
11382       char *secname;
11383       asection *sec;
11384
11385       secname = input_line_pointer;
11386       c = get_symbol_end ();
11387       sec = bfd_get_section_by_name (stdoutput, secname);
11388       if (sec == NULL)
11389         as_bad (_("%s: no such section"), secname);
11390       *input_line_pointer = c;
11391
11392       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11393         flag = BSF_FUNCTION;
11394     }
11395
11396   symbol_get_bfdsym (symbolP)->flags |= flag;
11397
11398   S_SET_EXTERNAL (symbolP);
11399   demand_empty_rest_of_line ();
11400 }
11401
11402 static void
11403 s_option (x)
11404      int x ATTRIBUTE_UNUSED;
11405 {
11406   char *opt;
11407   char c;
11408
11409   opt = input_line_pointer;
11410   c = get_symbol_end ();
11411
11412   if (*opt == 'O')
11413     {
11414       /* FIXME: What does this mean?  */
11415     }
11416   else if (strncmp (opt, "pic", 3) == 0)
11417     {
11418       int i;
11419
11420       i = atoi (opt + 3);
11421       if (i == 0)
11422         mips_pic = NO_PIC;
11423       else if (i == 2)
11424         mips_pic = SVR4_PIC;
11425       else
11426         as_bad (_(".option pic%d not supported"), i);
11427
11428       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11429         {
11430           if (g_switch_seen && g_switch_value != 0)
11431             as_warn (_("-G may not be used with SVR4 PIC code"));
11432           g_switch_value = 0;
11433           bfd_set_gp_size (stdoutput, 0);
11434         }
11435     }
11436   else
11437     as_warn (_("Unrecognized option \"%s\""), opt);
11438
11439   *input_line_pointer = c;
11440   demand_empty_rest_of_line ();
11441 }
11442
11443 /* This structure is used to hold a stack of .set values.  */
11444
11445 struct mips_option_stack
11446 {
11447   struct mips_option_stack *next;
11448   struct mips_set_options options;
11449 };
11450
11451 static struct mips_option_stack *mips_opts_stack;
11452
11453 /* Handle the .set pseudo-op.  */
11454
11455 static void
11456 s_mipsset (x)
11457      int x ATTRIBUTE_UNUSED;
11458 {
11459   char *name = input_line_pointer, ch;
11460
11461   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11462     ++input_line_pointer;
11463   ch = *input_line_pointer;
11464   *input_line_pointer = '\0';
11465
11466   if (strcmp (name, "reorder") == 0)
11467     {
11468       if (mips_opts.noreorder && prev_nop_frag != NULL)
11469         {
11470           /* If we still have pending nops, we can discard them.  The
11471              usual nop handling will insert any that are still
11472              needed.  */
11473           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11474                                     * (mips_opts.mips16 ? 2 : 4));
11475           prev_nop_frag = NULL;
11476         }
11477       mips_opts.noreorder = 0;
11478     }
11479   else if (strcmp (name, "noreorder") == 0)
11480     {
11481       mips_emit_delays (true);
11482       mips_opts.noreorder = 1;
11483       mips_any_noreorder = 1;
11484     }
11485   else if (strcmp (name, "at") == 0)
11486     {
11487       mips_opts.noat = 0;
11488     }
11489   else if (strcmp (name, "noat") == 0)
11490     {
11491       mips_opts.noat = 1;
11492     }
11493   else if (strcmp (name, "macro") == 0)
11494     {
11495       mips_opts.warn_about_macros = 0;
11496     }
11497   else if (strcmp (name, "nomacro") == 0)
11498     {
11499       if (mips_opts.noreorder == 0)
11500         as_bad (_("`noreorder' must be set before `nomacro'"));
11501       mips_opts.warn_about_macros = 1;
11502     }
11503   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11504     {
11505       mips_opts.nomove = 0;
11506     }
11507   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11508     {
11509       mips_opts.nomove = 1;
11510     }
11511   else if (strcmp (name, "bopt") == 0)
11512     {
11513       mips_opts.nobopt = 0;
11514     }
11515   else if (strcmp (name, "nobopt") == 0)
11516     {
11517       mips_opts.nobopt = 1;
11518     }
11519   else if (strcmp (name, "mips16") == 0
11520            || strcmp (name, "MIPS-16") == 0)
11521     mips_opts.mips16 = 1;
11522   else if (strcmp (name, "nomips16") == 0
11523            || strcmp (name, "noMIPS-16") == 0)
11524     mips_opts.mips16 = 0;
11525   else if (strcmp (name, "mips3d") == 0)
11526     mips_opts.ase_mips3d = 1;
11527   else if (strcmp (name, "nomips3d") == 0)
11528     mips_opts.ase_mips3d = 0;
11529   else if (strcmp (name, "mdmx") == 0)
11530     mips_opts.ase_mdmx = 1;
11531   else if (strcmp (name, "nomdmx") == 0)
11532     mips_opts.ase_mdmx = 0;
11533   else if (strncmp (name, "mips", 4) == 0)
11534     {
11535       int isa;
11536
11537       /* Permit the user to change the ISA on the fly.  Needless to
11538          say, misuse can cause serious problems.  */
11539       isa = atoi (name + 4);
11540       switch (isa)
11541         {
11542         case  0:
11543           mips_opts.gp32 = file_mips_gp32;
11544           mips_opts.fp32 = file_mips_fp32;
11545           break;
11546         case  1:
11547         case  2:
11548         case 32:
11549           mips_opts.gp32 = 1;
11550           mips_opts.fp32 = 1;
11551           break;
11552         case  3:
11553         case  4:
11554         case  5:
11555         case 64:
11556           mips_opts.gp32 = 0;
11557           mips_opts.fp32 = 0;
11558           break;
11559         default:
11560           as_bad (_("unknown ISA level %s"), name + 4);
11561           break;
11562         }
11563
11564       switch (isa)
11565         {
11566         case  0: mips_opts.isa = file_mips_isa;   break;
11567         case  1: mips_opts.isa = ISA_MIPS1;       break;
11568         case  2: mips_opts.isa = ISA_MIPS2;       break;
11569         case  3: mips_opts.isa = ISA_MIPS3;       break;
11570         case  4: mips_opts.isa = ISA_MIPS4;       break;
11571         case  5: mips_opts.isa = ISA_MIPS5;       break;
11572         case 32: mips_opts.isa = ISA_MIPS32;      break;
11573         case 64: mips_opts.isa = ISA_MIPS64;      break;
11574         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11575         }
11576     }
11577   else if (strcmp (name, "autoextend") == 0)
11578     mips_opts.noautoextend = 0;
11579   else if (strcmp (name, "noautoextend") == 0)
11580     mips_opts.noautoextend = 1;
11581   else if (strcmp (name, "push") == 0)
11582     {
11583       struct mips_option_stack *s;
11584
11585       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11586       s->next = mips_opts_stack;
11587       s->options = mips_opts;
11588       mips_opts_stack = s;
11589     }
11590   else if (strcmp (name, "pop") == 0)
11591     {
11592       struct mips_option_stack *s;
11593
11594       s = mips_opts_stack;
11595       if (s == NULL)
11596         as_bad (_(".set pop with no .set push"));
11597       else
11598         {
11599           /* If we're changing the reorder mode we need to handle
11600              delay slots correctly.  */
11601           if (s->options.noreorder && ! mips_opts.noreorder)
11602             mips_emit_delays (true);
11603           else if (! s->options.noreorder && mips_opts.noreorder)
11604             {
11605               if (prev_nop_frag != NULL)
11606                 {
11607                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11608                                             * (mips_opts.mips16 ? 2 : 4));
11609                   prev_nop_frag = NULL;
11610                 }
11611             }
11612
11613           mips_opts = s->options;
11614           mips_opts_stack = s->next;
11615           free (s);
11616         }
11617     }
11618   else
11619     {
11620       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11621     }
11622   *input_line_pointer = ch;
11623   demand_empty_rest_of_line ();
11624 }
11625
11626 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11627    .option pic2.  It means to generate SVR4 PIC calls.  */
11628
11629 static void
11630 s_abicalls (ignore)
11631      int ignore ATTRIBUTE_UNUSED;
11632 {
11633   mips_pic = SVR4_PIC;
11634   if (USE_GLOBAL_POINTER_OPT)
11635     {
11636       if (g_switch_seen && g_switch_value != 0)
11637         as_warn (_("-G may not be used with SVR4 PIC code"));
11638       g_switch_value = 0;
11639     }
11640   bfd_set_gp_size (stdoutput, 0);
11641   demand_empty_rest_of_line ();
11642 }
11643
11644 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11645    PIC code.  It sets the $gp register for the function based on the
11646    function address, which is in the register named in the argument.
11647    This uses a relocation against _gp_disp, which is handled specially
11648    by the linker.  The result is:
11649         lui     $gp,%hi(_gp_disp)
11650         addiu   $gp,$gp,%lo(_gp_disp)
11651         addu    $gp,$gp,.cpload argument
11652    The .cpload argument is normally $25 == $t9.  */
11653
11654 static void
11655 s_cpload (ignore)
11656      int ignore ATTRIBUTE_UNUSED;
11657 {
11658   expressionS ex;
11659   int icnt = 0;
11660
11661   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11662      .cpload is ignored.  */
11663   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11664     {
11665       s_ignore (0);
11666       return;
11667     }
11668
11669   /* .cpload should be in a .set noreorder section.  */
11670   if (mips_opts.noreorder == 0)
11671     as_warn (_(".cpload not in noreorder section"));
11672
11673   ex.X_op = O_symbol;
11674   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11675   ex.X_op_symbol = NULL;
11676   ex.X_add_number = 0;
11677
11678   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11679   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11680
11681   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11682   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11683                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11684
11685   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11686                mips_gp_register, mips_gp_register, tc_get_register (0));
11687
11688   demand_empty_rest_of_line ();
11689 }
11690
11691 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11692      .cpsetup $reg1, offset|$reg2, label
11693
11694    If offset is given, this results in:
11695      sd         $gp, offset($sp)
11696      lui        $gp, %hi(%neg(%gp_rel(label)))
11697      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11698      daddu      $gp, $gp, $reg1
11699
11700    If $reg2 is given, this results in:
11701      daddu      $reg2, $gp, $0
11702      lui        $gp, %hi(%neg(%gp_rel(label)))
11703      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11704      daddu      $gp, $gp, $reg1
11705    $reg1 is normally $25 == $t9.  */
11706 static void
11707 s_cpsetup (ignore)
11708      int ignore ATTRIBUTE_UNUSED;
11709 {
11710   expressionS ex_off;
11711   expressionS ex_sym;
11712   int reg1;
11713   int icnt = 0;
11714   char *f;
11715
11716   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11717      We also need NewABI support.  */
11718   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11719     {
11720       s_ignore (0);
11721       return;
11722     }
11723
11724   reg1 = tc_get_register (0);
11725   SKIP_WHITESPACE ();
11726   if (*input_line_pointer != ',')
11727     {
11728       as_bad (_("missing argument separator ',' for .cpsetup"));
11729       return;
11730     }
11731   else
11732     ++input_line_pointer;
11733   SKIP_WHITESPACE ();
11734   if (*input_line_pointer == '$')
11735     {
11736       mips_cpreturn_register = tc_get_register (0);
11737       mips_cpreturn_offset = -1;
11738     }
11739   else
11740     {
11741       mips_cpreturn_offset = get_absolute_expression ();
11742       mips_cpreturn_register = -1;
11743     }
11744   SKIP_WHITESPACE ();
11745   if (*input_line_pointer != ',')
11746     {
11747       as_bad (_("missing argument separator ',' for .cpsetup"));
11748       return;
11749     }
11750   else
11751     ++input_line_pointer;
11752   SKIP_WHITESPACE ();
11753   expression (&ex_sym);
11754
11755   if (mips_cpreturn_register == -1)
11756     {
11757       ex_off.X_op = O_constant;
11758       ex_off.X_add_symbol = NULL;
11759       ex_off.X_op_symbol = NULL;
11760       ex_off.X_add_number = mips_cpreturn_offset;
11761
11762       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11763                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11764     }
11765   else
11766     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11767                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11768
11769   /* Ensure there's room for the next two instructions, so that `f'
11770      doesn't end up with an address in the wrong frag.  */
11771   frag_grow (8);
11772   f = frag_more (0);
11773   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11774                (int) BFD_RELOC_GPREL16);
11775   fix_new (frag_now, f - frag_now->fr_literal,
11776            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11777   fix_new (frag_now, f - frag_now->fr_literal,
11778            0, NULL, 0, 0, BFD_RELOC_HI16_S);
11779
11780   f = frag_more (0);
11781   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11782                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11783   fix_new (frag_now, f - frag_now->fr_literal,
11784            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11785   fix_new (frag_now, f - frag_now->fr_literal,
11786            0, NULL, 0, 0, BFD_RELOC_LO16);
11787
11788   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11789                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11790                mips_gp_register, mips_gp_register, reg1);
11791
11792   demand_empty_rest_of_line ();
11793 }
11794
11795 static void
11796 s_cplocal (ignore)
11797      int ignore ATTRIBUTE_UNUSED;
11798 {
11799   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11800    .cplocal is ignored.  */
11801   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11802     {
11803       s_ignore (0);
11804       return;
11805     }
11806
11807   mips_gp_register = tc_get_register (0);
11808   demand_empty_rest_of_line ();
11809 }
11810
11811 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11812    offset from $sp.  The offset is remembered, and after making a PIC
11813    call $gp is restored from that location.  */
11814
11815 static void
11816 s_cprestore (ignore)
11817      int ignore ATTRIBUTE_UNUSED;
11818 {
11819   expressionS ex;
11820   int icnt = 0;
11821
11822   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11823      .cprestore is ignored.  */
11824   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11825     {
11826       s_ignore (0);
11827       return;
11828     }
11829
11830   mips_cprestore_offset = get_absolute_expression ();
11831   mips_cprestore_valid = 1;
11832
11833   ex.X_op = O_constant;
11834   ex.X_add_symbol = NULL;
11835   ex.X_op_symbol = NULL;
11836   ex.X_add_number = mips_cprestore_offset;
11837
11838   macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11839                "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
11840
11841   demand_empty_rest_of_line ();
11842 }
11843
11844 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11845    was given in the preceeding .gpsetup, it results in:
11846      ld         $gp, offset($sp)
11847
11848    If a register $reg2 was given there, it results in:
11849      daddiu     $gp, $gp, $reg2
11850  */
11851 static void
11852 s_cpreturn (ignore)
11853      int ignore ATTRIBUTE_UNUSED;
11854 {
11855   expressionS ex;
11856   int icnt = 0;
11857
11858   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11859      We also need NewABI support.  */
11860   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11861     {
11862       s_ignore (0);
11863       return;
11864     }
11865
11866   if (mips_cpreturn_register == -1)
11867     {
11868       ex.X_op = O_constant;
11869       ex.X_add_symbol = NULL;
11870       ex.X_op_symbol = NULL;
11871       ex.X_add_number = mips_cpreturn_offset;
11872
11873       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11874                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11875     }
11876   else
11877     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11878                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11879
11880   demand_empty_rest_of_line ();
11881 }
11882
11883 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
11884    code.  It sets the offset to use in gp_rel relocations.  */
11885
11886 static void
11887 s_gpvalue (ignore)
11888      int ignore ATTRIBUTE_UNUSED;
11889 {
11890   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11891      We also need NewABI support.  */
11892   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11893     {
11894       s_ignore (0);
11895       return;
11896     }
11897
11898   mips_gprel_offset = get_absolute_expression ();
11899
11900   demand_empty_rest_of_line ();
11901 }
11902
11903 /* Handle the .gpword pseudo-op.  This is used when generating PIC
11904    code.  It generates a 32 bit GP relative reloc.  */
11905
11906 static void
11907 s_gpword (ignore)
11908      int ignore ATTRIBUTE_UNUSED;
11909 {
11910   symbolS *label;
11911   expressionS ex;
11912   char *p;
11913
11914   /* When not generating PIC code, this is treated as .word.  */
11915   if (mips_pic != SVR4_PIC)
11916     {
11917       s_cons (2);
11918       return;
11919     }
11920
11921   label = insn_labels != NULL ? insn_labels->label : NULL;
11922   mips_emit_delays (true);
11923   if (auto_align)
11924     mips_align (2, 0, label);
11925   mips_clear_insn_labels ();
11926
11927   expression (&ex);
11928
11929   if (ex.X_op != O_symbol || ex.X_add_number != 0)
11930     {
11931       as_bad (_("Unsupported use of .gpword"));
11932       ignore_rest_of_line ();
11933     }
11934
11935   p = frag_more (4);
11936   md_number_to_chars (p, (valueT) 0, 4);
11937   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
11938                BFD_RELOC_GPREL32);
11939
11940   demand_empty_rest_of_line ();
11941 }
11942
11943 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
11944    tables in SVR4 PIC code.  */
11945
11946 static void
11947 s_cpadd (ignore)
11948      int ignore ATTRIBUTE_UNUSED;
11949 {
11950   int icnt = 0;
11951   int reg;
11952
11953   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11954      code.  */
11955   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11956     {
11957       s_ignore (0);
11958       return;
11959     }
11960
11961   /* Add $gp to the register named as an argument.  */
11962   reg = tc_get_register (0);
11963   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11964                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11965                "d,v,t", reg, reg, mips_gp_register);
11966
11967   demand_empty_rest_of_line ();
11968 }
11969
11970 /* Handle the .insn pseudo-op.  This marks instruction labels in
11971    mips16 mode.  This permits the linker to handle them specially,
11972    such as generating jalx instructions when needed.  We also make
11973    them odd for the duration of the assembly, in order to generate the
11974    right sort of code.  We will make them even in the adjust_symtab
11975    routine, while leaving them marked.  This is convenient for the
11976    debugger and the disassembler.  The linker knows to make them odd
11977    again.  */
11978
11979 static void
11980 s_insn (ignore)
11981      int ignore ATTRIBUTE_UNUSED;
11982 {
11983   mips16_mark_labels ();
11984
11985   demand_empty_rest_of_line ();
11986 }
11987
11988 /* Handle a .stabn directive.  We need these in order to mark a label
11989    as being a mips16 text label correctly.  Sometimes the compiler
11990    will emit a label, followed by a .stabn, and then switch sections.
11991    If the label and .stabn are in mips16 mode, then the label is
11992    really a mips16 text label.  */
11993
11994 static void
11995 s_mips_stab (type)
11996      int type;
11997 {
11998   if (type == 'n')
11999     mips16_mark_labels ();
12000
12001   s_stab (type);
12002 }
12003
12004 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12005  */
12006
12007 static void
12008 s_mips_weakext (ignore)
12009      int ignore ATTRIBUTE_UNUSED;
12010 {
12011   char *name;
12012   int c;
12013   symbolS *symbolP;
12014   expressionS exp;
12015
12016   name = input_line_pointer;
12017   c = get_symbol_end ();
12018   symbolP = symbol_find_or_make (name);
12019   S_SET_WEAK (symbolP);
12020   *input_line_pointer = c;
12021
12022   SKIP_WHITESPACE ();
12023
12024   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12025     {
12026       if (S_IS_DEFINED (symbolP))
12027         {
12028           as_bad ("ignoring attempt to redefine symbol %s",
12029                   S_GET_NAME (symbolP));
12030           ignore_rest_of_line ();
12031           return;
12032         }
12033
12034       if (*input_line_pointer == ',')
12035         {
12036           ++input_line_pointer;
12037           SKIP_WHITESPACE ();
12038         }
12039
12040       expression (&exp);
12041       if (exp.X_op != O_symbol)
12042         {
12043           as_bad ("bad .weakext directive");
12044           ignore_rest_of_line ();
12045           return;
12046         }
12047       symbol_set_value_expression (symbolP, &exp);
12048     }
12049
12050   demand_empty_rest_of_line ();
12051 }
12052
12053 /* Parse a register string into a number.  Called from the ECOFF code
12054    to parse .frame.  The argument is non-zero if this is the frame
12055    register, so that we can record it in mips_frame_reg.  */
12056
12057 int
12058 tc_get_register (frame)
12059      int frame;
12060 {
12061   int reg;
12062
12063   SKIP_WHITESPACE ();
12064   if (*input_line_pointer++ != '$')
12065     {
12066       as_warn (_("expected `$'"));
12067       reg = ZERO;
12068     }
12069   else if (ISDIGIT (*input_line_pointer))
12070     {
12071       reg = get_absolute_expression ();
12072       if (reg < 0 || reg >= 32)
12073         {
12074           as_warn (_("Bad register number"));
12075           reg = ZERO;
12076         }
12077     }
12078   else
12079     {
12080       if (strncmp (input_line_pointer, "ra", 2) == 0)
12081         {
12082           reg = RA;
12083           input_line_pointer += 2;
12084         }
12085       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12086         {
12087           reg = FP;
12088           input_line_pointer += 2;
12089         }
12090       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12091         {
12092           reg = SP;
12093           input_line_pointer += 2;
12094         }
12095       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12096         {
12097           reg = GP;
12098           input_line_pointer += 2;
12099         }
12100       else if (strncmp (input_line_pointer, "at", 2) == 0)
12101         {
12102           reg = AT;
12103           input_line_pointer += 2;
12104         }
12105       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12106         {
12107           reg = KT0;
12108           input_line_pointer += 3;
12109         }
12110       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12111         {
12112           reg = KT1;
12113           input_line_pointer += 3;
12114         }
12115       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12116         {
12117           reg = ZERO;
12118           input_line_pointer += 4;
12119         }
12120       else
12121         {
12122           as_warn (_("Unrecognized register name"));
12123           reg = ZERO;
12124           while (ISALNUM(*input_line_pointer))
12125            input_line_pointer++;
12126         }
12127     }
12128   if (frame)
12129     {
12130       mips_frame_reg = reg != 0 ? reg : SP;
12131       mips_frame_reg_valid = 1;
12132       mips_cprestore_valid = 0;
12133     }
12134   return reg;
12135 }
12136
12137 valueT
12138 md_section_align (seg, addr)
12139      asection *seg;
12140      valueT addr;
12141 {
12142   int align = bfd_get_section_alignment (stdoutput, seg);
12143
12144 #ifdef OBJ_ELF
12145   /* We don't need to align ELF sections to the full alignment.
12146      However, Irix 5 may prefer that we align them at least to a 16
12147      byte boundary.  We don't bother to align the sections if we are
12148      targeted for an embedded system.  */
12149   if (strcmp (TARGET_OS, "elf") == 0)
12150     return addr;
12151   if (align > 4)
12152     align = 4;
12153 #endif
12154
12155   return ((addr + (1 << align) - 1) & (-1 << align));
12156 }
12157
12158 /* Utility routine, called from above as well.  If called while the
12159    input file is still being read, it's only an approximation.  (For
12160    example, a symbol may later become defined which appeared to be
12161    undefined earlier.)  */
12162
12163 static int
12164 nopic_need_relax (sym, before_relaxing)
12165      symbolS *sym;
12166      int before_relaxing;
12167 {
12168   if (sym == 0)
12169     return 0;
12170
12171   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12172     {
12173       const char *symname;
12174       int change;
12175
12176       /* Find out whether this symbol can be referenced off the $gp
12177          register.  It can be if it is smaller than the -G size or if
12178          it is in the .sdata or .sbss section.  Certain symbols can
12179          not be referenced off the $gp, although it appears as though
12180          they can.  */
12181       symname = S_GET_NAME (sym);
12182       if (symname != (const char *) NULL
12183           && (strcmp (symname, "eprol") == 0
12184               || strcmp (symname, "etext") == 0
12185               || strcmp (symname, "_gp") == 0
12186               || strcmp (symname, "edata") == 0
12187               || strcmp (symname, "_fbss") == 0
12188               || strcmp (symname, "_fdata") == 0
12189               || strcmp (symname, "_ftext") == 0
12190               || strcmp (symname, "end") == 0
12191               || strcmp (symname, "_gp_disp") == 0))
12192         change = 1;
12193       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12194                && (0
12195 #ifndef NO_ECOFF_DEBUGGING
12196                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12197                        && (symbol_get_obj (sym)->ecoff_extern_size
12198                            <= g_switch_value))
12199 #endif
12200                    /* We must defer this decision until after the whole
12201                       file has been read, since there might be a .extern
12202                       after the first use of this symbol.  */
12203                    || (before_relaxing
12204 #ifndef NO_ECOFF_DEBUGGING
12205                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12206 #endif
12207                        && S_GET_VALUE (sym) == 0)
12208                    || (S_GET_VALUE (sym) != 0
12209                        && S_GET_VALUE (sym) <= g_switch_value)))
12210         change = 0;
12211       else
12212         {
12213           const char *segname;
12214
12215           segname = segment_name (S_GET_SEGMENT (sym));
12216           assert (strcmp (segname, ".lit8") != 0
12217                   && strcmp (segname, ".lit4") != 0);
12218           change = (strcmp (segname, ".sdata") != 0
12219                     && strcmp (segname, ".sbss") != 0
12220                     && strncmp (segname, ".sdata.", 7) != 0
12221                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12222         }
12223       return change;
12224     }
12225   else
12226     /* We are not optimizing for the $gp register.  */
12227     return 1;
12228 }
12229
12230 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12231    extended opcode.  SEC is the section the frag is in.  */
12232
12233 static int
12234 mips16_extended_frag (fragp, sec, stretch)
12235      fragS *fragp;
12236      asection *sec;
12237      long stretch;
12238 {
12239   int type;
12240   register const struct mips16_immed_operand *op;
12241   offsetT val;
12242   int mintiny, maxtiny;
12243   segT symsec;
12244   fragS *sym_frag;
12245
12246   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12247     return 0;
12248   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12249     return 1;
12250
12251   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12252   op = mips16_immed_operands;
12253   while (op->type != type)
12254     {
12255       ++op;
12256       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12257     }
12258
12259   if (op->unsp)
12260     {
12261       if (type == '<' || type == '>' || type == '[' || type == ']')
12262         {
12263           mintiny = 1;
12264           maxtiny = 1 << op->nbits;
12265         }
12266       else
12267         {
12268           mintiny = 0;
12269           maxtiny = (1 << op->nbits) - 1;
12270         }
12271     }
12272   else
12273     {
12274       mintiny = - (1 << (op->nbits - 1));
12275       maxtiny = (1 << (op->nbits - 1)) - 1;
12276     }
12277
12278   sym_frag = symbol_get_frag (fragp->fr_symbol);
12279   val = S_GET_VALUE (fragp->fr_symbol);
12280   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12281
12282   if (op->pcrel)
12283     {
12284       addressT addr;
12285
12286       /* We won't have the section when we are called from
12287          mips_relax_frag.  However, we will always have been called
12288          from md_estimate_size_before_relax first.  If this is a
12289          branch to a different section, we mark it as such.  If SEC is
12290          NULL, and the frag is not marked, then it must be a branch to
12291          the same section.  */
12292       if (sec == NULL)
12293         {
12294           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12295             return 1;
12296         }
12297       else
12298         {
12299           /* Must have been called from md_estimate_size_before_relax.  */
12300           if (symsec != sec)
12301             {
12302               fragp->fr_subtype =
12303                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12304
12305               /* FIXME: We should support this, and let the linker
12306                  catch branches and loads that are out of range.  */
12307               as_bad_where (fragp->fr_file, fragp->fr_line,
12308                             _("unsupported PC relative reference to different section"));
12309
12310               return 1;
12311             }
12312           if (fragp != sym_frag && sym_frag->fr_address == 0)
12313             /* Assume non-extended on the first relaxation pass.
12314                The address we have calculated will be bogus if this is
12315                a forward branch to another frag, as the forward frag
12316                will have fr_address == 0.  */
12317             return 0;
12318         }
12319
12320       /* In this case, we know for sure that the symbol fragment is in
12321          the same section.  If the relax_marker of the symbol fragment
12322          differs from the relax_marker of this fragment, we have not
12323          yet adjusted the symbol fragment fr_address.  We want to add
12324          in STRETCH in order to get a better estimate of the address.
12325          This particularly matters because of the shift bits.  */
12326       if (stretch != 0
12327           && sym_frag->relax_marker != fragp->relax_marker)
12328         {
12329           fragS *f;
12330
12331           /* Adjust stretch for any alignment frag.  Note that if have
12332              been expanding the earlier code, the symbol may be
12333              defined in what appears to be an earlier frag.  FIXME:
12334              This doesn't handle the fr_subtype field, which specifies
12335              a maximum number of bytes to skip when doing an
12336              alignment.  */
12337           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12338             {
12339               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12340                 {
12341                   if (stretch < 0)
12342                     stretch = - ((- stretch)
12343                                  & ~ ((1 << (int) f->fr_offset) - 1));
12344                   else
12345                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12346                   if (stretch == 0)
12347                     break;
12348                 }
12349             }
12350           if (f != NULL)
12351             val += stretch;
12352         }
12353
12354       addr = fragp->fr_address + fragp->fr_fix;
12355
12356       /* The base address rules are complicated.  The base address of
12357          a branch is the following instruction.  The base address of a
12358          PC relative load or add is the instruction itself, but if it
12359          is in a delay slot (in which case it can not be extended) use
12360          the address of the instruction whose delay slot it is in.  */
12361       if (type == 'p' || type == 'q')
12362         {
12363           addr += 2;
12364
12365           /* If we are currently assuming that this frag should be
12366              extended, then, the current address is two bytes
12367              higher.  */
12368           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12369             addr += 2;
12370
12371           /* Ignore the low bit in the target, since it will be set
12372              for a text label.  */
12373           if ((val & 1) != 0)
12374             --val;
12375         }
12376       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12377         addr -= 4;
12378       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12379         addr -= 2;
12380
12381       val -= addr & ~ ((1 << op->shift) - 1);
12382
12383       /* Branch offsets have an implicit 0 in the lowest bit.  */
12384       if (type == 'p' || type == 'q')
12385         val /= 2;
12386
12387       /* If any of the shifted bits are set, we must use an extended
12388          opcode.  If the address depends on the size of this
12389          instruction, this can lead to a loop, so we arrange to always
12390          use an extended opcode.  We only check this when we are in
12391          the main relaxation loop, when SEC is NULL.  */
12392       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12393         {
12394           fragp->fr_subtype =
12395             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12396           return 1;
12397         }
12398
12399       /* If we are about to mark a frag as extended because the value
12400          is precisely maxtiny + 1, then there is a chance of an
12401          infinite loop as in the following code:
12402              la $4,foo
12403              .skip      1020
12404              .align     2
12405            foo:
12406          In this case when the la is extended, foo is 0x3fc bytes
12407          away, so the la can be shrunk, but then foo is 0x400 away, so
12408          the la must be extended.  To avoid this loop, we mark the
12409          frag as extended if it was small, and is about to become
12410          extended with a value of maxtiny + 1.  */
12411       if (val == ((maxtiny + 1) << op->shift)
12412           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12413           && sec == NULL)
12414         {
12415           fragp->fr_subtype =
12416             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12417           return 1;
12418         }
12419     }
12420   else if (symsec != absolute_section && sec != NULL)
12421     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12422
12423   if ((val & ((1 << op->shift) - 1)) != 0
12424       || val < (mintiny << op->shift)
12425       || val > (maxtiny << op->shift))
12426     return 1;
12427   else
12428     return 0;
12429 }
12430
12431 /* Estimate the size of a frag before relaxing.  Unless this is the
12432    mips16, we are not really relaxing here, and the final size is
12433    encoded in the subtype information.  For the mips16, we have to
12434    decide whether we are using an extended opcode or not.  */
12435
12436 int
12437 md_estimate_size_before_relax (fragp, segtype)
12438      fragS *fragp;
12439      asection *segtype;
12440 {
12441   int change = 0;
12442   boolean linkonce = false;
12443
12444   if (RELAX_MIPS16_P (fragp->fr_subtype))
12445     /* We don't want to modify the EXTENDED bit here; it might get us
12446        into infinite loops.  We change it only in mips_relax_frag().  */
12447     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12448
12449   if (mips_pic == NO_PIC)
12450     {
12451       change = nopic_need_relax (fragp->fr_symbol, 0);
12452     }
12453   else if (mips_pic == SVR4_PIC)
12454     {
12455       symbolS *sym;
12456       asection *symsec;
12457
12458       sym = fragp->fr_symbol;
12459
12460       /* Handle the case of a symbol equated to another symbol.  */
12461       while (symbol_equated_reloc_p (sym))
12462         {
12463           symbolS *n;
12464
12465           /* It's possible to get a loop here in a badly written
12466              program.  */
12467           n = symbol_get_value_expression (sym)->X_add_symbol;
12468           if (n == sym)
12469             break;
12470           sym = n;
12471         }
12472
12473       symsec = S_GET_SEGMENT (sym);
12474
12475       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12476       if (symsec != segtype && ! S_IS_LOCAL (sym))
12477         {
12478           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12479               != 0)
12480             linkonce = true;
12481
12482           /* The GNU toolchain uses an extension for ELF: a section
12483              beginning with the magic string .gnu.linkonce is a linkonce
12484              section.  */
12485           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12486                        sizeof ".gnu.linkonce" - 1) == 0)
12487             linkonce = true;
12488         }
12489
12490       /* This must duplicate the test in adjust_reloc_syms.  */
12491       change = (symsec != &bfd_und_section
12492                 && symsec != &bfd_abs_section
12493                 && ! bfd_is_com_section (symsec)
12494                 && !linkonce
12495 #ifdef OBJ_ELF
12496                 /* A global or weak symbol is treated as external.  */
12497                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12498                     || (! S_IS_WEAK (sym)
12499                         && (! S_IS_EXTERNAL (sym)
12500                             || mips_pic == EMBEDDED_PIC)))
12501 #endif
12502                 );
12503     }
12504   else
12505     abort ();
12506
12507   if (change)
12508     {
12509       /* Record the offset to the first reloc in the fr_opcode field.
12510          This lets md_convert_frag and tc_gen_reloc know that the code
12511          must be expanded.  */
12512       fragp->fr_opcode = (fragp->fr_literal
12513                           + fragp->fr_fix
12514                           - RELAX_OLD (fragp->fr_subtype)
12515                           + RELAX_RELOC1 (fragp->fr_subtype));
12516       /* FIXME: This really needs as_warn_where.  */
12517       if (RELAX_WARN (fragp->fr_subtype))
12518         as_warn (_("AT used after \".set noat\" or macro used after "
12519                    "\".set nomacro\""));
12520
12521       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12522     }
12523
12524   return 0;
12525 }
12526
12527 /* This is called to see whether a reloc against a defined symbol
12528    should be converted into a reloc against a section.  Don't adjust
12529    MIPS16 jump relocations, so we don't have to worry about the format
12530    of the offset in the .o file.  Don't adjust relocations against
12531    mips16 symbols, so that the linker can find them if it needs to set
12532    up a stub.  */
12533
12534 int
12535 mips_fix_adjustable (fixp)
12536      fixS *fixp;
12537 {
12538   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12539     return 0;
12540
12541   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12542       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12543     return 0;
12544
12545   if (fixp->fx_addsy == NULL)
12546     return 1;
12547
12548 #ifdef OBJ_ELF
12549   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12550       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12551       && fixp->fx_subsy == NULL)
12552     return 0;
12553 #endif
12554
12555   return 1;
12556 }
12557
12558 /* Translate internal representation of relocation info to BFD target
12559    format.  */
12560
12561 arelent **
12562 tc_gen_reloc (section, fixp)
12563      asection *section ATTRIBUTE_UNUSED;
12564      fixS *fixp;
12565 {
12566   static arelent *retval[4];
12567   arelent *reloc;
12568   bfd_reloc_code_real_type code;
12569
12570   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12571   retval[1] = NULL;
12572
12573   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12574   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12575   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12576
12577   if (mips_pic == EMBEDDED_PIC
12578       && SWITCH_TABLE (fixp))
12579     {
12580       /* For a switch table entry we use a special reloc.  The addend
12581          is actually the difference between the reloc address and the
12582          subtrahend.  */
12583       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12584       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12585         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12586       fixp->fx_r_type = BFD_RELOC_GPREL32;
12587     }
12588   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12589     {
12590       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12591         reloc->addend = fixp->fx_addnumber;
12592       else
12593         {
12594           /* We use a special addend for an internal RELLO reloc.  */
12595           if (symbol_section_p (fixp->fx_addsy))
12596             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12597           else
12598             reloc->addend = fixp->fx_addnumber + reloc->address;
12599         }
12600     }
12601   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12602     {
12603       assert (fixp->fx_next != NULL
12604               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12605
12606       /* The reloc is relative to the RELLO; adjust the addend
12607          accordingly.  */
12608       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12609         reloc->addend = fixp->fx_next->fx_addnumber;
12610       else
12611         {
12612           /* We use a special addend for an internal RELHI reloc.  */
12613           if (symbol_section_p (fixp->fx_addsy))
12614             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12615                              + fixp->fx_next->fx_where
12616                              - S_GET_VALUE (fixp->fx_subsy));
12617           else
12618             reloc->addend = (fixp->fx_addnumber
12619                              + fixp->fx_next->fx_frag->fr_address
12620                              + fixp->fx_next->fx_where);
12621         }
12622     }
12623   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12624     reloc->addend = fixp->fx_addnumber;
12625   else
12626     {
12627       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12628         /* A gruesome hack which is a result of the gruesome gas reloc
12629            handling.  */
12630         reloc->addend = reloc->address;
12631       else
12632         reloc->addend = -reloc->address;
12633     }
12634
12635   /* If this is a variant frag, we may need to adjust the existing
12636      reloc and generate a new one.  */
12637   if (fixp->fx_frag->fr_opcode != NULL
12638       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12639           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12640           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12641           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12642           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12643           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12644           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12645       && ! HAVE_NEWABI)
12646     {
12647       arelent *reloc2;
12648
12649       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12650
12651       /* If this is not the last reloc in this frag, then we have two
12652          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12653          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12654          the second one handle all of them.  */
12655       if (fixp->fx_next != NULL
12656           && fixp->fx_frag == fixp->fx_next->fx_frag)
12657         {
12658           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12659                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12660                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12661                       && (fixp->fx_next->fx_r_type
12662                           == BFD_RELOC_MIPS_GOT_LO16))
12663                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12664                       && (fixp->fx_next->fx_r_type
12665                           == BFD_RELOC_MIPS_CALL_LO16)));
12666           retval[0] = NULL;
12667           return retval;
12668         }
12669
12670       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12671       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12672       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12673       retval[2] = NULL;
12674       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12675       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12676       reloc2->address = (reloc->address
12677                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12678                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12679       reloc2->addend = fixp->fx_addnumber;
12680       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12681       assert (reloc2->howto != NULL);
12682
12683       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12684         {
12685           arelent *reloc3;
12686
12687           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12688           retval[3] = NULL;
12689           *reloc3 = *reloc2;
12690           reloc3->address += 4;
12691         }
12692
12693       if (mips_pic == NO_PIC)
12694         {
12695           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12696           fixp->fx_r_type = BFD_RELOC_HI16_S;
12697         }
12698       else if (mips_pic == SVR4_PIC)
12699         {
12700           switch (fixp->fx_r_type)
12701             {
12702             default:
12703               abort ();
12704             case BFD_RELOC_MIPS_GOT16:
12705               break;
12706             case BFD_RELOC_MIPS_CALL16:
12707             case BFD_RELOC_MIPS_GOT_LO16:
12708             case BFD_RELOC_MIPS_CALL_LO16:
12709               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12710               break;
12711             }
12712         }
12713       else
12714         abort ();
12715     }
12716
12717   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12718      entry to be used in the relocation's section offset.  */
12719   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12720     {
12721       reloc->address = reloc->addend;
12722       reloc->addend = 0;
12723     }
12724
12725   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12726      fixup_segment converted a non-PC relative reloc into a PC
12727      relative reloc.  In such a case, we need to convert the reloc
12728      code.  */
12729   code = fixp->fx_r_type;
12730   if (fixp->fx_pcrel)
12731     {
12732       switch (code)
12733         {
12734         case BFD_RELOC_8:
12735           code = BFD_RELOC_8_PCREL;
12736           break;
12737         case BFD_RELOC_16:
12738           code = BFD_RELOC_16_PCREL;
12739           break;
12740         case BFD_RELOC_32:
12741           code = BFD_RELOC_32_PCREL;
12742           break;
12743         case BFD_RELOC_64:
12744           code = BFD_RELOC_64_PCREL;
12745           break;
12746         case BFD_RELOC_8_PCREL:
12747         case BFD_RELOC_16_PCREL:
12748         case BFD_RELOC_32_PCREL:
12749         case BFD_RELOC_64_PCREL:
12750         case BFD_RELOC_16_PCREL_S2:
12751         case BFD_RELOC_PCREL_HI16_S:
12752         case BFD_RELOC_PCREL_LO16:
12753           break;
12754         default:
12755           as_bad_where (fixp->fx_file, fixp->fx_line,
12756                         _("Cannot make %s relocation PC relative"),
12757                         bfd_get_reloc_code_name (code));
12758         }
12759     }
12760
12761 #ifdef OBJ_ELF
12762   /* md_apply_fix3 has a double-subtraction hack to get
12763      bfd_install_relocation to behave nicely.  GPREL relocations are
12764      handled correctly without this hack, so undo it here.  We can't
12765      stop md_apply_fix3 from subtracting twice in the first place since
12766      the fake addend is required for variant frags above.  */
12767   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12768       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
12769       && reloc->addend != 0
12770       && mips_need_elf_addend_fixup (fixp))
12771     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12772 #endif
12773
12774   /* To support a PC relative reloc when generating embedded PIC code
12775      for ECOFF, we use a Cygnus extension.  We check for that here to
12776      make sure that we don't let such a reloc escape normally.  */
12777   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12778        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12779       && code == BFD_RELOC_16_PCREL_S2
12780       && mips_pic != EMBEDDED_PIC)
12781     reloc->howto = NULL;
12782   else
12783     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12784
12785   if (reloc->howto == NULL)
12786     {
12787       as_bad_where (fixp->fx_file, fixp->fx_line,
12788                     _("Can not represent %s relocation in this object file format"),
12789                     bfd_get_reloc_code_name (code));
12790       retval[0] = NULL;
12791     }
12792
12793   return retval;
12794 }
12795
12796 /* Relax a machine dependent frag.  This returns the amount by which
12797    the current size of the frag should change.  */
12798
12799 int
12800 mips_relax_frag (fragp, stretch)
12801      fragS *fragp;
12802      long stretch;
12803 {
12804   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12805     return 0;
12806
12807   if (mips16_extended_frag (fragp, NULL, stretch))
12808     {
12809       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12810         return 0;
12811       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12812       return 2;
12813     }
12814   else
12815     {
12816       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12817         return 0;
12818       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12819       return -2;
12820     }
12821
12822   return 0;
12823 }
12824
12825 /* Convert a machine dependent frag.  */
12826
12827 void
12828 md_convert_frag (abfd, asec, fragp)
12829      bfd *abfd ATTRIBUTE_UNUSED;
12830      segT asec;
12831      fragS *fragp;
12832 {
12833   int old, new;
12834   char *fixptr;
12835
12836   if (RELAX_MIPS16_P (fragp->fr_subtype))
12837     {
12838       int type;
12839       register const struct mips16_immed_operand *op;
12840       boolean small, ext;
12841       offsetT val;
12842       bfd_byte *buf;
12843       unsigned long insn;
12844       boolean use_extend;
12845       unsigned short extend;
12846
12847       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12848       op = mips16_immed_operands;
12849       while (op->type != type)
12850         ++op;
12851
12852       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12853         {
12854           small = false;
12855           ext = true;
12856         }
12857       else
12858         {
12859           small = true;
12860           ext = false;
12861         }
12862
12863       resolve_symbol_value (fragp->fr_symbol);
12864       val = S_GET_VALUE (fragp->fr_symbol);
12865       if (op->pcrel)
12866         {
12867           addressT addr;
12868
12869           addr = fragp->fr_address + fragp->fr_fix;
12870
12871           /* The rules for the base address of a PC relative reloc are
12872              complicated; see mips16_extended_frag.  */
12873           if (type == 'p' || type == 'q')
12874             {
12875               addr += 2;
12876               if (ext)
12877                 addr += 2;
12878               /* Ignore the low bit in the target, since it will be
12879                  set for a text label.  */
12880               if ((val & 1) != 0)
12881                 --val;
12882             }
12883           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12884             addr -= 4;
12885           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12886             addr -= 2;
12887
12888           addr &= ~ (addressT) ((1 << op->shift) - 1);
12889           val -= addr;
12890
12891           /* Make sure the section winds up with the alignment we have
12892              assumed.  */
12893           if (op->shift > 0)
12894             record_alignment (asec, op->shift);
12895         }
12896
12897       if (ext
12898           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12899               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12900         as_warn_where (fragp->fr_file, fragp->fr_line,
12901                        _("extended instruction in delay slot"));
12902
12903       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12904
12905       if (target_big_endian)
12906         insn = bfd_getb16 (buf);
12907       else
12908         insn = bfd_getl16 (buf);
12909
12910       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12911                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12912                     small, ext, &insn, &use_extend, &extend);
12913
12914       if (use_extend)
12915         {
12916           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
12917           fragp->fr_fix += 2;
12918           buf += 2;
12919         }
12920
12921       md_number_to_chars ((char *) buf, insn, 2);
12922       fragp->fr_fix += 2;
12923       buf += 2;
12924     }
12925   else
12926     {
12927       if (fragp->fr_opcode == NULL)
12928         return;
12929
12930       old = RELAX_OLD (fragp->fr_subtype);
12931       new = RELAX_NEW (fragp->fr_subtype);
12932       fixptr = fragp->fr_literal + fragp->fr_fix;
12933
12934       if (new > 0)
12935         memcpy (fixptr - old, fixptr, new);
12936
12937       fragp->fr_fix += new - old;
12938     }
12939 }
12940
12941 #ifdef OBJ_ELF
12942
12943 /* This function is called after the relocs have been generated.
12944    We've been storing mips16 text labels as odd.  Here we convert them
12945    back to even for the convenience of the debugger.  */
12946
12947 void
12948 mips_frob_file_after_relocs ()
12949 {
12950   asymbol **syms;
12951   unsigned int count, i;
12952
12953   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12954     return;
12955
12956   syms = bfd_get_outsymbols (stdoutput);
12957   count = bfd_get_symcount (stdoutput);
12958   for (i = 0; i < count; i++, syms++)
12959     {
12960       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12961           && ((*syms)->value & 1) != 0)
12962         {
12963           (*syms)->value &= ~1;
12964           /* If the symbol has an odd size, it was probably computed
12965              incorrectly, so adjust that as well.  */
12966           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12967             ++elf_symbol (*syms)->internal_elf_sym.st_size;
12968         }
12969     }
12970 }
12971
12972 #endif
12973
12974 /* This function is called whenever a label is defined.  It is used
12975    when handling branch delays; if a branch has a label, we assume we
12976    can not move it.  */
12977
12978 void
12979 mips_define_label (sym)
12980      symbolS *sym;
12981 {
12982   struct insn_label_list *l;
12983
12984   if (free_insn_labels == NULL)
12985     l = (struct insn_label_list *) xmalloc (sizeof *l);
12986   else
12987     {
12988       l = free_insn_labels;
12989       free_insn_labels = l->next;
12990     }
12991
12992   l->label = sym;
12993   l->next = insn_labels;
12994   insn_labels = l;
12995 }
12996 \f
12997 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12998
12999 /* Some special processing for a MIPS ELF file.  */
13000
13001 void
13002 mips_elf_final_processing ()
13003 {
13004   /* Write out the register information.  */
13005   if (mips_abi != N64_ABI)
13006     {
13007       Elf32_RegInfo s;
13008
13009       s.ri_gprmask = mips_gprmask;
13010       s.ri_cprmask[0] = mips_cprmask[0];
13011       s.ri_cprmask[1] = mips_cprmask[1];
13012       s.ri_cprmask[2] = mips_cprmask[2];
13013       s.ri_cprmask[3] = mips_cprmask[3];
13014       /* The gp_value field is set by the MIPS ELF backend.  */
13015
13016       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13017                                        ((Elf32_External_RegInfo *)
13018                                         mips_regmask_frag));
13019     }
13020   else
13021     {
13022       Elf64_Internal_RegInfo s;
13023
13024       s.ri_gprmask = mips_gprmask;
13025       s.ri_pad = 0;
13026       s.ri_cprmask[0] = mips_cprmask[0];
13027       s.ri_cprmask[1] = mips_cprmask[1];
13028       s.ri_cprmask[2] = mips_cprmask[2];
13029       s.ri_cprmask[3] = mips_cprmask[3];
13030       /* The gp_value field is set by the MIPS ELF backend.  */
13031
13032       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13033                                        ((Elf64_External_RegInfo *)
13034                                         mips_regmask_frag));
13035     }
13036
13037   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13038      sort of BFD interface for this.  */
13039   if (mips_any_noreorder)
13040     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13041   if (mips_pic != NO_PIC)
13042     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13043
13044   /* Set MIPS ELF flags for ASEs.  */
13045   if (file_ase_mips16)
13046     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13047 #if 0 /* XXX FIXME */
13048   if (file_ase_mips3d)
13049     elf_elfheader (stdoutput)->e_flags |= ???;
13050 #endif
13051   if (file_ase_mdmx)
13052     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13053
13054   /* Set the MIPS ELF ABI flags.  */
13055   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13056     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13057   else if (mips_abi == O64_ABI)
13058     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13059   else if (mips_abi == EABI_ABI)
13060     {
13061       if (!file_mips_gp32)
13062         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13063       else
13064         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13065     }
13066   else if (mips_abi == N32_ABI)
13067     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13068
13069   /* Nothing to do for N64_ABI.  */
13070
13071   if (mips_32bitmode)
13072     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13073 }
13074
13075 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13076 \f
13077 typedef struct proc {
13078   symbolS *isym;
13079   unsigned long reg_mask;
13080   unsigned long reg_offset;
13081   unsigned long fpreg_mask;
13082   unsigned long fpreg_offset;
13083   unsigned long frame_offset;
13084   unsigned long frame_reg;
13085   unsigned long pc_reg;
13086 } procS;
13087
13088 static procS cur_proc;
13089 static procS *cur_proc_ptr;
13090 static int numprocs;
13091
13092 /* Fill in an rs_align_code fragment.  */
13093
13094 void
13095 mips_handle_align (fragp)
13096      fragS *fragp;
13097 {
13098   if (fragp->fr_type != rs_align_code)
13099     return;
13100
13101   if (mips_opts.mips16)
13102     {
13103       static const unsigned char be_nop[] = { 0x65, 0x00 };
13104       static const unsigned char le_nop[] = { 0x00, 0x65 };
13105
13106       int bytes;
13107       char *p;
13108
13109       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13110       p = fragp->fr_literal + fragp->fr_fix;
13111
13112       if (bytes & 1)
13113         {
13114           *p++ = 0;
13115           fragp->fr_fix++;
13116         }
13117
13118       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13119       fragp->fr_var = 2;
13120     }
13121
13122   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13123 }
13124
13125 static void
13126 md_obj_begin ()
13127 {
13128 }
13129
13130 static void
13131 md_obj_end ()
13132 {
13133   /* check for premature end, nesting errors, etc */
13134   if (cur_proc_ptr)
13135     as_warn (_("missing .end at end of assembly"));
13136 }
13137
13138 static long
13139 get_number ()
13140 {
13141   int negative = 0;
13142   long val = 0;
13143
13144   if (*input_line_pointer == '-')
13145     {
13146       ++input_line_pointer;
13147       negative = 1;
13148     }
13149   if (!ISDIGIT (*input_line_pointer))
13150     as_bad (_("expected simple number"));
13151   if (input_line_pointer[0] == '0')
13152     {
13153       if (input_line_pointer[1] == 'x')
13154         {
13155           input_line_pointer += 2;
13156           while (ISXDIGIT (*input_line_pointer))
13157             {
13158               val <<= 4;
13159               val |= hex_value (*input_line_pointer++);
13160             }
13161           return negative ? -val : val;
13162         }
13163       else
13164         {
13165           ++input_line_pointer;
13166           while (ISDIGIT (*input_line_pointer))
13167             {
13168               val <<= 3;
13169               val |= *input_line_pointer++ - '0';
13170             }
13171           return negative ? -val : val;
13172         }
13173     }
13174   if (!ISDIGIT (*input_line_pointer))
13175     {
13176       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13177               *input_line_pointer, *input_line_pointer);
13178       as_warn (_("invalid number"));
13179       return -1;
13180     }
13181   while (ISDIGIT (*input_line_pointer))
13182     {
13183       val *= 10;
13184       val += *input_line_pointer++ - '0';
13185     }
13186   return negative ? -val : val;
13187 }
13188
13189 /* The .file directive; just like the usual .file directive, but there
13190    is an initial number which is the ECOFF file index.  In the non-ECOFF
13191    case .file implies DWARF-2.  */
13192
13193 static void
13194 s_mips_file (x)
13195      int x ATTRIBUTE_UNUSED;
13196 {
13197   static int first_file_directive = 0;
13198
13199   if (ECOFF_DEBUGGING)
13200     {
13201       get_number ();
13202       s_app_file (0);
13203     }
13204   else
13205     {
13206       char *filename;
13207
13208       filename = dwarf2_directive_file (0);
13209
13210       /* Versions of GCC up to 3.1 start files with a ".file"
13211          directive even for stabs output.  Make sure that this
13212          ".file" is handled.  Note that you need a version of GCC
13213          after 3.1 in order to support DWARF-2 on MIPS.  */
13214       if (filename != NULL && ! first_file_directive)
13215         {
13216           (void) new_logical_line (filename, -1);
13217           s_app_file_string (filename);
13218         }
13219       first_file_directive = 1;
13220     }
13221 }
13222
13223 /* The .loc directive, implying DWARF-2.  */
13224
13225 static void
13226 s_mips_loc (x)
13227      int x ATTRIBUTE_UNUSED;
13228 {
13229   if (!ECOFF_DEBUGGING)
13230     dwarf2_directive_loc (0);
13231 }
13232
13233 /* The .end directive.  */
13234
13235 static void
13236 s_mips_end (x)
13237      int x ATTRIBUTE_UNUSED;
13238 {
13239   symbolS *p;
13240   int maybe_text;
13241
13242   /* Following functions need their own .frame and .cprestore directives.  */
13243   mips_frame_reg_valid = 0;
13244   mips_cprestore_valid = 0;
13245
13246   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13247     {
13248       p = get_symbol ();
13249       demand_empty_rest_of_line ();
13250     }
13251   else
13252     p = NULL;
13253
13254 #ifdef BFD_ASSEMBLER
13255   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13256     maybe_text = 1;
13257   else
13258     maybe_text = 0;
13259 #else
13260   if (now_seg != data_section && now_seg != bss_section)
13261     maybe_text = 1;
13262   else
13263     maybe_text = 0;
13264 #endif
13265
13266   if (!maybe_text)
13267     as_warn (_(".end not in text section"));
13268
13269   if (!cur_proc_ptr)
13270     {
13271       as_warn (_(".end directive without a preceding .ent directive."));
13272       demand_empty_rest_of_line ();
13273       return;
13274     }
13275
13276   if (p != NULL)
13277     {
13278       assert (S_GET_NAME (p));
13279       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13280         as_warn (_(".end symbol does not match .ent symbol."));
13281
13282       if (debug_type == DEBUG_STABS)
13283         stabs_generate_asm_endfunc (S_GET_NAME (p),
13284                                     S_GET_NAME (p));
13285     }
13286   else
13287     as_warn (_(".end directive missing or unknown symbol"));
13288
13289 #ifdef OBJ_ELF
13290   /* Generate a .pdr section.  */
13291   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13292     {
13293       segT saved_seg = now_seg;
13294       subsegT saved_subseg = now_subseg;
13295       valueT dot;
13296       expressionS exp;
13297       char *fragp;
13298
13299       dot = frag_now_fix ();
13300
13301 #ifdef md_flush_pending_output
13302       md_flush_pending_output ();
13303 #endif
13304
13305       assert (pdr_seg);
13306       subseg_set (pdr_seg, 0);
13307
13308       /* Write the symbol.  */
13309       exp.X_op = O_symbol;
13310       exp.X_add_symbol = p;
13311       exp.X_add_number = 0;
13312       emit_expr (&exp, 4);
13313
13314       fragp = frag_more (7 * 4);
13315
13316       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13317       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13318       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13319       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13320       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13321       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13322       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13323
13324       subseg_set (saved_seg, saved_subseg);
13325     }
13326 #endif /* OBJ_ELF */
13327
13328   cur_proc_ptr = NULL;
13329 }
13330
13331 /* The .aent and .ent directives.  */
13332
13333 static void
13334 s_mips_ent (aent)
13335      int aent;
13336 {
13337   symbolS *symbolP;
13338   int maybe_text;
13339
13340   symbolP = get_symbol ();
13341   if (*input_line_pointer == ',')
13342     ++input_line_pointer;
13343   SKIP_WHITESPACE ();
13344   if (ISDIGIT (*input_line_pointer)
13345       || *input_line_pointer == '-')
13346     get_number ();
13347
13348 #ifdef BFD_ASSEMBLER
13349   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13350     maybe_text = 1;
13351   else
13352     maybe_text = 0;
13353 #else
13354   if (now_seg != data_section && now_seg != bss_section)
13355     maybe_text = 1;
13356   else
13357     maybe_text = 0;
13358 #endif
13359
13360   if (!maybe_text)
13361     as_warn (_(".ent or .aent not in text section."));
13362
13363   if (!aent && cur_proc_ptr)
13364     as_warn (_("missing .end"));
13365
13366   if (!aent)
13367     {
13368       /* This function needs its own .frame and .cprestore directives.  */
13369       mips_frame_reg_valid = 0;
13370       mips_cprestore_valid = 0;
13371
13372       cur_proc_ptr = &cur_proc;
13373       memset (cur_proc_ptr, '\0', sizeof (procS));
13374
13375       cur_proc_ptr->isym = symbolP;
13376
13377       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13378
13379       ++numprocs;
13380
13381       if (debug_type == DEBUG_STABS)
13382         stabs_generate_asm_func (S_GET_NAME (symbolP),
13383                                  S_GET_NAME (symbolP));
13384     }
13385
13386   demand_empty_rest_of_line ();
13387 }
13388
13389 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13390    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13391    s_mips_frame is used so that we can set the PDR information correctly.
13392    We can't use the ecoff routines because they make reference to the ecoff
13393    symbol table (in the mdebug section).  */
13394
13395 static void
13396 s_mips_frame (ignore)
13397      int ignore ATTRIBUTE_UNUSED;
13398 {
13399 #ifdef OBJ_ELF
13400   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13401     {
13402       long val;
13403
13404       if (cur_proc_ptr == (procS *) NULL)
13405         {
13406           as_warn (_(".frame outside of .ent"));
13407           demand_empty_rest_of_line ();
13408           return;
13409         }
13410
13411       cur_proc_ptr->frame_reg = tc_get_register (1);
13412
13413       SKIP_WHITESPACE ();
13414       if (*input_line_pointer++ != ','
13415           || get_absolute_expression_and_terminator (&val) != ',')
13416         {
13417           as_warn (_("Bad .frame directive"));
13418           --input_line_pointer;
13419           demand_empty_rest_of_line ();
13420           return;
13421         }
13422
13423       cur_proc_ptr->frame_offset = val;
13424       cur_proc_ptr->pc_reg = tc_get_register (0);
13425
13426       demand_empty_rest_of_line ();
13427     }
13428   else
13429 #endif /* OBJ_ELF */
13430     s_ignore (ignore);
13431 }
13432
13433 /* The .fmask and .mask directives. If the mdebug section is present
13434    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13435    embedded targets, s_mips_mask is used so that we can set the PDR
13436    information correctly. We can't use the ecoff routines because they
13437    make reference to the ecoff symbol table (in the mdebug section).  */
13438
13439 static void
13440 s_mips_mask (reg_type)
13441      char reg_type;
13442 {
13443 #ifdef OBJ_ELF
13444   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13445     {
13446       long mask, off;
13447
13448       if (cur_proc_ptr == (procS *) NULL)
13449         {
13450           as_warn (_(".mask/.fmask outside of .ent"));
13451           demand_empty_rest_of_line ();
13452           return;
13453         }
13454
13455       if (get_absolute_expression_and_terminator (&mask) != ',')
13456         {
13457           as_warn (_("Bad .mask/.fmask directive"));
13458           --input_line_pointer;
13459           demand_empty_rest_of_line ();
13460           return;
13461         }
13462
13463       off = get_absolute_expression ();
13464
13465       if (reg_type == 'F')
13466         {
13467           cur_proc_ptr->fpreg_mask = mask;
13468           cur_proc_ptr->fpreg_offset = off;
13469         }
13470       else
13471         {
13472           cur_proc_ptr->reg_mask = mask;
13473           cur_proc_ptr->reg_offset = off;
13474         }
13475
13476       demand_empty_rest_of_line ();
13477     }
13478   else
13479 #endif /* OBJ_ELF */
13480     s_ignore (reg_type);
13481 }
13482
13483 /* The .loc directive.  */
13484
13485 #if 0
13486 static void
13487 s_loc (x)
13488      int x;
13489 {
13490   symbolS *symbolP;
13491   int lineno;
13492   int addroff;
13493
13494   assert (now_seg == text_section);
13495
13496   lineno = get_number ();
13497   addroff = frag_now_fix ();
13498
13499   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13500   S_SET_TYPE (symbolP, N_SLINE);
13501   S_SET_OTHER (symbolP, 0);
13502   S_SET_DESC (symbolP, lineno);
13503   symbolP->sy_segment = now_seg;
13504 }
13505 #endif
13506
13507 /* A table describing all the processors gas knows about.  Names are
13508    matched in the order listed.
13509
13510    To ease comparison, please keep this table in the same order as
13511    gcc's mips_cpu_info_table[].  */
13512 static const struct mips_cpu_info mips_cpu_info_table[] =
13513 {
13514   /* Entries for generic ISAs */
13515   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
13516   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
13517   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
13518   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
13519   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
13520   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
13521   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
13522
13523   /* MIPS I */
13524   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
13525   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
13526   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
13527
13528   /* MIPS II */
13529   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
13530
13531   /* MIPS III */
13532   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
13533   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
13534   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
13535   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
13536   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
13537   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
13538   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
13539   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
13540   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
13541
13542   /* MIPS IV */
13543   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
13544   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
13545   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
13546   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
13547   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
13548   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
13549   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
13550   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
13551   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
13552   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
13553
13554   /* MIPS 32 */
13555   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13556   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
13557   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
13558
13559   /* MIPS 64 */
13560   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
13561   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
13562
13563   /* Broadcom SB-1 CPU core */
13564   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
13565
13566   /* End marker */
13567   { NULL, 0, 0, 0 }
13568 };
13569
13570
13571 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13572    with a final "000" replaced by "k".  Ignore case.
13573
13574    Note: this function is shared between GCC and GAS.  */
13575
13576 static boolean
13577 mips_strict_matching_cpu_name_p (canonical, given)
13578      const char *canonical, *given;
13579 {
13580   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13581     given++, canonical++;
13582
13583   return ((*given == 0 && *canonical == 0)
13584           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13585 }
13586
13587
13588 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13589    CPU name.  We've traditionally allowed a lot of variation here.
13590
13591    Note: this function is shared between GCC and GAS.  */
13592
13593 static boolean
13594 mips_matching_cpu_name_p (canonical, given)
13595      const char *canonical, *given;
13596 {
13597   /* First see if the name matches exactly, or with a final "000"
13598      turned into "k".  */
13599   if (mips_strict_matching_cpu_name_p (canonical, given))
13600     return true;
13601
13602   /* If not, try comparing based on numerical designation alone.
13603      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
13604   if (TOLOWER (*given) == 'r')
13605     given++;
13606   if (!ISDIGIT (*given))
13607     return false;
13608
13609   /* Skip over some well-known prefixes in the canonical name,
13610      hoping to find a number there too.  */
13611   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13612     canonical += 2;
13613   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13614     canonical += 2;
13615   else if (TOLOWER (canonical[0]) == 'r')
13616     canonical += 1;
13617
13618   return mips_strict_matching_cpu_name_p (canonical, given);
13619 }
13620
13621
13622 /* Parse an option that takes the name of a processor as its argument.
13623    OPTION is the name of the option and CPU_STRING is the argument.
13624    Return the corresponding processor enumeration if the CPU_STRING is
13625    recognized, otherwise report an error and return null.
13626
13627    A similar function exists in GCC.  */
13628
13629 static const struct mips_cpu_info *
13630 mips_parse_cpu (option, cpu_string)
13631      const char *option, *cpu_string;
13632 {
13633   const struct mips_cpu_info *p;
13634
13635   /* 'from-abi' selects the most compatible architecture for the given
13636      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
13637      EABIs, we have to decide whether we're using the 32-bit or 64-bit
13638      version.  Look first at the -mgp options, if given, otherwise base
13639      the choice on MIPS_DEFAULT_64BIT.
13640
13641      Treat NO_ABI like the EABIs.  One reason to do this is that the
13642      plain 'mips' and 'mips64' configs have 'from-abi' as their default
13643      architecture.  This code picks MIPS I for 'mips' and MIPS III for
13644      'mips64', just as we did in the days before 'from-abi'.  */
13645   if (strcasecmp (cpu_string, "from-abi") == 0)
13646     {
13647       if (ABI_NEEDS_32BIT_REGS (mips_abi))
13648         return mips_cpu_info_from_isa (ISA_MIPS1);
13649
13650       if (ABI_NEEDS_64BIT_REGS (mips_abi))
13651         return mips_cpu_info_from_isa (ISA_MIPS3);
13652
13653       if (file_mips_gp32 >= 0)
13654         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13655
13656       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13657                                      ? ISA_MIPS3
13658                                      : ISA_MIPS1);
13659     }
13660
13661   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
13662   if (strcasecmp (cpu_string, "default") == 0)
13663     return 0;
13664
13665   for (p = mips_cpu_info_table; p->name != 0; p++)
13666     if (mips_matching_cpu_name_p (p->name, cpu_string))
13667       return p;
13668
13669   as_bad ("Bad value (%s) for %s", cpu_string, option);
13670   return 0;
13671 }
13672
13673 /* Return the canonical processor information for ISA (a member of the
13674    ISA_MIPS* enumeration).  */
13675
13676 static const struct mips_cpu_info *
13677 mips_cpu_info_from_isa (isa)
13678      int isa;
13679 {
13680   int i;
13681
13682   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13683     if (mips_cpu_info_table[i].is_isa
13684         && isa == mips_cpu_info_table[i].isa)
13685       return (&mips_cpu_info_table[i]);
13686
13687   return NULL;
13688 }
13689 \f
13690 static void
13691 show (stream, string, col_p, first_p)
13692      FILE *stream;
13693      const char *string;
13694      int *col_p;
13695      int *first_p;
13696 {
13697   if (*first_p)
13698     {
13699       fprintf (stream, "%24s", "");
13700       *col_p = 24;
13701     }
13702   else
13703     {
13704       fprintf (stream, ", ");
13705       *col_p += 2;
13706     }
13707
13708   if (*col_p + strlen (string) > 72)
13709     {
13710       fprintf (stream, "\n%24s", "");
13711       *col_p = 24;
13712     }
13713
13714   fprintf (stream, "%s", string);
13715   *col_p += strlen (string);
13716
13717   *first_p = 0;
13718 }
13719
13720 void
13721 md_show_usage (stream)
13722      FILE *stream;
13723 {
13724   int column, first;
13725   size_t i;
13726
13727   fprintf (stream, _("\
13728 MIPS options:\n\
13729 -membedded-pic          generate embedded position independent code\n\
13730 -EB                     generate big endian output\n\
13731 -EL                     generate little endian output\n\
13732 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
13733 -G NUM                  allow referencing objects up to NUM bytes\n\
13734                         implicitly with the gp register [default 8]\n"));
13735   fprintf (stream, _("\
13736 -mips1                  generate MIPS ISA I instructions\n\
13737 -mips2                  generate MIPS ISA II instructions\n\
13738 -mips3                  generate MIPS ISA III instructions\n\
13739 -mips4                  generate MIPS ISA IV instructions\n\
13740 -mips5                  generate MIPS ISA V instructions\n\
13741 -mips32                 generate MIPS32 ISA instructions\n\
13742 -mips64                 generate MIPS64 ISA instructions\n\
13743 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
13744
13745   first = 1;
13746
13747   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13748     show (stream, mips_cpu_info_table[i].name, &column, &first);
13749   show (stream, "from-abi", &column, &first);
13750   fputc ('\n', stream);
13751
13752   fprintf (stream, _("\
13753 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13754 -no-mCPU                don't generate code specific to CPU.\n\
13755                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
13756
13757   first = 1;
13758
13759   show (stream, "3900", &column, &first);
13760   show (stream, "4010", &column, &first);
13761   show (stream, "4100", &column, &first);
13762   show (stream, "4650", &column, &first);
13763   fputc ('\n', stream);
13764
13765   fprintf (stream, _("\
13766 -mips16                 generate mips16 instructions\n\
13767 -no-mips16              do not generate mips16 instructions\n"));
13768   fprintf (stream, _("\
13769 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
13770 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
13771 -O0                     remove unneeded NOPs, do not swap branches\n\
13772 -O                      remove unneeded NOPs and swap branches\n\
13773 -n                      warn about NOPs generated from macros\n\
13774 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
13775 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
13776 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
13777 #ifdef OBJ_ELF
13778   fprintf (stream, _("\
13779 -KPIC, -call_shared     generate SVR4 position independent code\n\
13780 -non_shared             do not generate position independent code\n\
13781 -xgot                   assume a 32 bit GOT\n\
13782 -mabi=ABI               create ABI conformant object file for:\n"));
13783
13784   first = 1;
13785
13786   show (stream, "32", &column, &first);
13787   show (stream, "o64", &column, &first);
13788   show (stream, "n32", &column, &first);
13789   show (stream, "64", &column, &first);
13790   show (stream, "eabi", &column, &first);
13791
13792   fputc ('\n', stream);
13793
13794   fprintf (stream, _("\
13795 -32                     create o32 ABI object file (default)\n\
13796 -n32                    create n32 ABI object file\n\
13797 -64                     create 64 ABI object file\n"));
13798 #endif
13799 }