* config/tc-mips.c (percent_op): Ensure longer percent_op's are
[external/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 #ifdef OBJ_MAYBE_ELF
48 /* Clean up namespace so we can include obj-elf.h too.  */
49 static int mips_output_flavor PARAMS ((void));
50 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
51 #undef OBJ_PROCESS_STAB
52 #undef OUTPUT_FLAVOR
53 #undef S_GET_ALIGN
54 #undef S_GET_SIZE
55 #undef S_SET_ALIGN
56 #undef S_SET_SIZE
57 #undef obj_frob_file
58 #undef obj_frob_file_after_relocs
59 #undef obj_frob_symbol
60 #undef obj_pop_insert
61 #undef obj_sec_sym_ok_for_reloc
62 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
63
64 #include "obj-elf.h"
65 /* Fix any of them that we actually care about.  */
66 #undef OUTPUT_FLAVOR
67 #define OUTPUT_FLAVOR mips_output_flavor()
68 #endif
69
70 #if defined (OBJ_ELF)
71 #include "elf/mips.h"
72 #endif
73
74 #ifndef ECOFF_DEBUGGING
75 #define NO_ECOFF_DEBUGGING
76 #define ECOFF_DEBUGGING 0
77 #endif
78
79 #include "ecoff.h"
80
81 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
82 static char *mips_regmask_frag;
83 #endif
84
85 #define AT  1
86 #define TREG 24
87 #define PIC_CALL_REG 25
88 #define KT0 26
89 #define KT1 27
90 #define GP  28
91 #define SP  29
92 #define FP  30
93 #define RA  31
94
95 #define ILLEGAL_REG (32)
96
97 /* Allow override of standard little-endian ECOFF format.  */
98
99 #ifndef ECOFF_LITTLE_FORMAT
100 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
101 #endif
102
103 extern int target_big_endian;
104
105 /* The name of the readonly data section.  */
106 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
107                             ? ".data" \
108                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
109                             ? ".rdata" \
110                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
111                             ? ".rdata" \
112                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
113                             ? ".rodata" \
114                             : (abort (), ""))
115
116 /* The ABI to use.  */
117 enum mips_abi_level
118 {
119   NO_ABI = 0,
120   O32_ABI,
121   O64_ABI,
122   N32_ABI,
123   N64_ABI,
124   EABI_ABI
125 };
126
127 /* MIPS ABI we are using for this output file.  */
128 static enum mips_abi_level file_mips_abi = NO_ABI;
129
130 /* This is the set of options which may be modified by the .set
131    pseudo-op.  We use a struct so that .set push and .set pop are more
132    reliable.  */
133
134 struct mips_set_options
135 {
136   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
137      if it has not been initialized.  Changed by `.set mipsN', and the
138      -mipsN command line option, and the default CPU.  */
139   int isa;
140   /* Whether we are assembling for the mips16 processor.  0 if we are
141      not, 1 if we are, and -1 if the value has not been initialized.
142      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
143      -nomips16 command line options, and the default CPU.  */
144   int mips16;
145   /* Non-zero if we should not reorder instructions.  Changed by `.set
146      reorder' and `.set noreorder'.  */
147   int noreorder;
148   /* Non-zero if we should not permit the $at ($1) register to be used
149      in instructions.  Changed by `.set at' and `.set noat'.  */
150   int noat;
151   /* Non-zero if we should warn when a macro instruction expands into
152      more than one machine instruction.  Changed by `.set nomacro' and
153      `.set macro'.  */
154   int warn_about_macros;
155   /* Non-zero if we should not move instructions.  Changed by `.set
156      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
157   int nomove;
158   /* Non-zero if we should not optimize branches by moving the target
159      of the branch into the delay slot.  Actually, we don't perform
160      this optimization anyhow.  Changed by `.set bopt' and `.set
161      nobopt'.  */
162   int nobopt;
163   /* Non-zero if we should not autoextend mips16 instructions.
164      Changed by `.set autoextend' and `.set noautoextend'.  */
165   int noautoextend;
166   /* Restrict general purpose registers and floating point registers
167      to 32 bit.  This is initially determined when -mgp32 or -mfp32
168      is passed but can changed if the assembler code uses .set mipsN.  */
169   int gp32;
170   int fp32;
171   /* The ABI currently in use. This is changed by .set mipsN to loosen
172      restrictions and doesn't affect the whole file.  */
173   enum mips_abi_level abi;
174 };
175
176 /* True if -mgp32 was passed.  */
177 static int file_mips_gp32 = -1;
178
179 /* True if -mfp32 was passed.  */
180 static int file_mips_fp32 = -1;
181
182 /* This is the struct we use to hold the current set of options.  Note
183    that we must set the isa field to ISA_UNKNOWN and the mips16 field to
184    -1 to indicate that they have not been initialized.  */
185
186 static struct mips_set_options mips_opts =
187 {
188   ISA_UNKNOWN, -1, 0, 0, 0, 0, 0, 0, 0, 0, NO_ABI
189 };
190
191 /* These variables are filled in with the masks of registers used.
192    The object format code reads them and puts them in the appropriate
193    place.  */
194 unsigned long mips_gprmask;
195 unsigned long mips_cprmask[4];
196
197 /* MIPS ISA we are using for this output file.  */
198 static int file_mips_isa = ISA_UNKNOWN;
199
200 /* The argument of the -mcpu= flag.  Historical for code generation.  */
201 static int mips_cpu = CPU_UNKNOWN;
202
203 /* The argument of the -march= flag.  The architecture we are assembling.  */
204 static int mips_arch = CPU_UNKNOWN;
205
206 /* The argument of the -mtune= flag.  The architecture for which we
207    are optimizing.  */
208 static int mips_tune = CPU_UNKNOWN;
209
210 /* Whether we should mark the file EABI64 or EABI32.  */
211 static int mips_eabi64 = 0;
212
213 /* If they asked for mips1 or mips2 and a cpu that is
214    mips3 or greater, then mark the object file 32BITMODE.  */
215 static int mips_32bitmode = 0;
216
217 /* Some ISA's have delay slots for instructions which read or write
218    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
219    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
220    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
221    delay slot in this ISA.  The uses of this macro assume that any
222    ISA that has delay slots for one of these, has them for all.  They
223    also assume that ISAs which don't have delays for these insns, don't
224    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
225 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
226    (ISA) == ISA_MIPS1                       \
227    || (ISA) == ISA_MIPS2                    \
228    || (ISA) == ISA_MIPS3                    \
229    )
230
231 /*  Return true if ISA supports 64 bit gp register instructions.  */
232 #define ISA_HAS_64BIT_REGS(ISA) (    \
233    (ISA) == ISA_MIPS3                \
234    || (ISA) == ISA_MIPS4             \
235    || (ISA) == ISA_MIPS5             \
236    || (ISA) == ISA_MIPS64            \
237    )
238
239 #define HAVE_32BIT_GPRS                            \
240     (mips_opts.gp32                                \
241      || mips_opts.abi == O32_ABI                   \
242      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
243
244 #define HAVE_32BIT_FPRS                            \
245     (mips_opts.fp32                                \
246      || mips_opts.abi == O32_ABI                   \
247      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
248
249 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
250 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
251
252 #define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
253
254 #define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
255
256 /* We can only have 64bit addresses if the object file format
257    supports it.  */
258 #define HAVE_32BIT_ADDRESSES                           \
259    (HAVE_32BIT_GPRS                                    \
260     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
261          || ! HAVE_64BIT_OBJECTS)                      \
262         && mips_pic != EMBEDDED_PIC))
263
264 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
265
266 /* Whether the processor uses hardware interlocks to protect
267    reads from the HI and LO registers, and thus does not
268    require nops to be inserted.  */
269
270 #define hilo_interlocks (mips_arch == CPU_R4010                       \
271                          || mips_arch == CPU_SB1                      \
272                          )
273
274 /* Whether the processor uses hardware interlocks to protect reads
275    from the GPRs, and thus does not require nops to be inserted.  */
276 #define gpr_interlocks \
277   (mips_opts.isa != ISA_MIPS1  \
278    || mips_arch == CPU_R3900)
279
280 /* As with other "interlocks" this is used by hardware that has FP
281    (co-processor) interlocks.  */
282 /* Itbl support may require additional care here.  */
283 #define cop_interlocks (mips_arch == CPU_R4300                        \
284                         || mips_arch == CPU_SB1                       \
285                         )
286
287 /* Is this a mfhi or mflo instruction?  */
288 #define MF_HILO_INSN(PINFO) \
289           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
290
291 /* MIPS PIC level.  */
292
293 enum mips_pic_level
294 {
295   /* Do not generate PIC code.  */
296   NO_PIC,
297
298   /* Generate PIC code as in Irix 4.  This is not implemented, and I'm
299      not sure what it is supposed to do.  */
300   IRIX4_PIC,
301
302   /* Generate PIC code as in the SVR4 MIPS ABI.  */
303   SVR4_PIC,
304
305   /* Generate PIC code without using a global offset table: the data
306      segment has a maximum size of 64K, all data references are off
307      the $gp register, and all text references are PC relative.  This
308      is used on some embedded systems.  */
309   EMBEDDED_PIC
310 };
311
312 static enum mips_pic_level mips_pic;
313
314 /* Warn about all NOPS that the assembler generates.  */
315 static int warn_nops = 0;
316
317 /* 1 if we should generate 32 bit offsets from the GP register in
318    SVR4_PIC mode.  Currently has no meaning in other modes.  */
319 static int mips_big_got;
320
321 /* 1 if trap instructions should used for overflow rather than break
322    instructions.  */
323 static int mips_trap;
324
325 /* 1 if double width floating point constants should not be constructed
326    by assembling two single width halves into two single width floating
327    point registers which just happen to alias the double width destination
328    register.  On some architectures this aliasing can be disabled by a bit
329    in the status register, and the setting of this bit cannot be determined
330    automatically at assemble time.  */
331 static int mips_disable_float_construction;
332
333 /* Non-zero if any .set noreorder directives were used.  */
334
335 static int mips_any_noreorder;
336
337 /* Non-zero if nops should be inserted when the register referenced in
338    an mfhi/mflo instruction is read in the next two instructions.  */
339 static int mips_7000_hilo_fix;
340
341 /* The size of the small data section.  */
342 static unsigned int g_switch_value = 8;
343 /* Whether the -G option was used.  */
344 static int g_switch_seen = 0;
345
346 #define N_RMASK 0xc4
347 #define N_VFP   0xd4
348
349 /* If we can determine in advance that GP optimization won't be
350    possible, we can skip the relaxation stuff that tries to produce
351    GP-relative references.  This makes delay slot optimization work
352    better.
353
354    This function can only provide a guess, but it seems to work for
355    gcc output.  It needs to guess right for gcc, otherwise gcc
356    will put what it thinks is a GP-relative instruction in a branch
357    delay slot.
358
359    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
360    fixed it for the non-PIC mode.  KR 95/04/07  */
361 static int nopic_need_relax PARAMS ((symbolS *, int));
362
363 /* handle of the OPCODE hash table */
364 static struct hash_control *op_hash = NULL;
365
366 /* The opcode hash table we use for the mips16.  */
367 static struct hash_control *mips16_op_hash = NULL;
368
369 /* This array holds the chars that always start a comment.  If the
370     pre-processor is disabled, these aren't very useful */
371 const char comment_chars[] = "#";
372
373 /* This array holds the chars that only start a comment at the beginning of
374    a line.  If the line seems to have the form '# 123 filename'
375    .line and .file directives will appear in the pre-processed output */
376 /* Note that input_file.c hand checks for '#' at the beginning of the
377    first line of the input file.  This is because the compiler outputs
378    #NO_APP at the beginning of its output.  */
379 /* Also note that C style comments are always supported.  */
380 const char line_comment_chars[] = "#";
381
382 /* This array holds machine specific line separator characters.  */
383 const char line_separator_chars[] = ";";
384
385 /* Chars that can be used to separate mant from exp in floating point nums */
386 const char EXP_CHARS[] = "eE";
387
388 /* Chars that mean this number is a floating point constant */
389 /* As in 0f12.456 */
390 /* or    0d1.2345e12 */
391 const char FLT_CHARS[] = "rRsSfFdDxXpP";
392
393 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
394    changed in read.c .  Ideally it shouldn't have to know about it at all,
395    but nothing is ideal around here.
396  */
397
398 static char *insn_error;
399
400 static int auto_align = 1;
401
402 /* When outputting SVR4 PIC code, the assembler needs to know the
403    offset in the stack frame from which to restore the $gp register.
404    This is set by the .cprestore pseudo-op, and saved in this
405    variable.  */
406 static offsetT mips_cprestore_offset = -1;
407
408 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
409    more optimizations, it can use a register value instead of a memory-saved
410    offset and even an other register than $gp as global pointer.  */
411 static offsetT mips_cpreturn_offset = -1;
412 static int mips_cpreturn_register = -1;
413 static int mips_gp_register = GP;
414
415 /* Whether mips_cprestore_offset has been set in the current function
416    (or whether it has already been warned about, if not).  */
417 static int mips_cprestore_valid = 0;
418
419 /* This is the register which holds the stack frame, as set by the
420    .frame pseudo-op.  This is needed to implement .cprestore.  */
421 static int mips_frame_reg = SP;
422
423 /* Whether mips_frame_reg has been set in the current function
424    (or whether it has already been warned about, if not).  */
425 static int mips_frame_reg_valid = 0;
426
427 /* To output NOP instructions correctly, we need to keep information
428    about the previous two instructions.  */
429
430 /* Whether we are optimizing.  The default value of 2 means to remove
431    unneeded NOPs and swap branch instructions when possible.  A value
432    of 1 means to not swap branches.  A value of 0 means to always
433    insert NOPs.  */
434 static int mips_optimize = 2;
435
436 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
437    equivalent to seeing no -g option at all.  */
438 static int mips_debug = 0;
439
440 /* The previous instruction.  */
441 static struct mips_cl_insn prev_insn;
442
443 /* The instruction before prev_insn.  */
444 static struct mips_cl_insn prev_prev_insn;
445
446 /* If we don't want information for prev_insn or prev_prev_insn, we
447    point the insn_mo field at this dummy integer.  */
448 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
449
450 /* Non-zero if prev_insn is valid.  */
451 static int prev_insn_valid;
452
453 /* The frag for the previous instruction.  */
454 static struct frag *prev_insn_frag;
455
456 /* The offset into prev_insn_frag for the previous instruction.  */
457 static long prev_insn_where;
458
459 /* The reloc type for the previous instruction, if any.  */
460 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
461
462 /* The reloc for the previous instruction, if any.  */
463 static fixS *prev_insn_fixp[3];
464
465 /* Non-zero if the previous instruction was in a delay slot.  */
466 static int prev_insn_is_delay_slot;
467
468 /* Non-zero if the previous instruction was in a .set noreorder.  */
469 static int prev_insn_unreordered;
470
471 /* Non-zero if the previous instruction uses an extend opcode (if
472    mips16).  */
473 static int prev_insn_extended;
474
475 /* Non-zero if the previous previous instruction was in a .set
476    noreorder.  */
477 static int prev_prev_insn_unreordered;
478
479 /* If this is set, it points to a frag holding nop instructions which
480    were inserted before the start of a noreorder section.  If those
481    nops turn out to be unnecessary, the size of the frag can be
482    decreased.  */
483 static fragS *prev_nop_frag;
484
485 /* The number of nop instructions we created in prev_nop_frag.  */
486 static int prev_nop_frag_holds;
487
488 /* The number of nop instructions that we know we need in
489    prev_nop_frag.  */
490 static int prev_nop_frag_required;
491
492 /* The number of instructions we've seen since prev_nop_frag.  */
493 static int prev_nop_frag_since;
494
495 /* For ECOFF and ELF, relocations against symbols are done in two
496    parts, with a HI relocation and a LO relocation.  Each relocation
497    has only 16 bits of space to store an addend.  This means that in
498    order for the linker to handle carries correctly, it must be able
499    to locate both the HI and the LO relocation.  This means that the
500    relocations must appear in order in the relocation table.
501
502    In order to implement this, we keep track of each unmatched HI
503    relocation.  We then sort them so that they immediately precede the
504    corresponding LO relocation.  */
505
506 struct mips_hi_fixup
507 {
508   /* Next HI fixup.  */
509   struct mips_hi_fixup *next;
510   /* This fixup.  */
511   fixS *fixp;
512   /* The section this fixup is in.  */
513   segT seg;
514 };
515
516 /* The list of unmatched HI relocs.  */
517
518 static struct mips_hi_fixup *mips_hi_fixup_list;
519
520 /* Map normal MIPS register numbers to mips16 register numbers.  */
521
522 #define X ILLEGAL_REG
523 static const int mips32_to_16_reg_map[] =
524 {
525   X, X, 2, 3, 4, 5, 6, 7,
526   X, X, X, X, X, X, X, X,
527   0, 1, X, X, X, X, X, X,
528   X, X, X, X, X, X, X, X
529 };
530 #undef X
531
532 /* Map mips16 register numbers to normal MIPS register numbers.  */
533
534 static const unsigned int mips16_to_32_reg_map[] =
535 {
536   16, 17, 2, 3, 4, 5, 6, 7
537 };
538 \f
539 /* Since the MIPS does not have multiple forms of PC relative
540    instructions, we do not have to do relaxing as is done on other
541    platforms.  However, we do have to handle GP relative addressing
542    correctly, which turns out to be a similar problem.
543
544    Every macro that refers to a symbol can occur in (at least) two
545    forms, one with GP relative addressing and one without.  For
546    example, loading a global variable into a register generally uses
547    a macro instruction like this:
548      lw $4,i
549    If i can be addressed off the GP register (this is true if it is in
550    the .sbss or .sdata section, or if it is known to be smaller than
551    the -G argument) this will generate the following instruction:
552      lw $4,i($gp)
553    This instruction will use a GPREL reloc.  If i can not be addressed
554    off the GP register, the following instruction sequence will be used:
555      lui $at,i
556      lw $4,i($at)
557    In this case the first instruction will have a HI16 reloc, and the
558    second reloc will have a LO16 reloc.  Both relocs will be against
559    the symbol i.
560
561    The issue here is that we may not know whether i is GP addressable
562    until after we see the instruction that uses it.  Therefore, we
563    want to be able to choose the final instruction sequence only at
564    the end of the assembly.  This is similar to the way other
565    platforms choose the size of a PC relative instruction only at the
566    end of assembly.
567
568    When generating position independent code we do not use GP
569    addressing in quite the same way, but the issue still arises as
570    external symbols and local symbols must be handled differently.
571
572    We handle these issues by actually generating both possible
573    instruction sequences.  The longer one is put in a frag_var with
574    type rs_machine_dependent.  We encode what to do with the frag in
575    the subtype field.  We encode (1) the number of existing bytes to
576    replace, (2) the number of new bytes to use, (3) the offset from
577    the start of the existing bytes to the first reloc we must generate
578    (that is, the offset is applied from the start of the existing
579    bytes after they are replaced by the new bytes, if any), (4) the
580    offset from the start of the existing bytes to the second reloc,
581    (5) whether a third reloc is needed (the third reloc is always four
582    bytes after the second reloc), and (6) whether to warn if this
583    variant is used (this is sometimes needed if .set nomacro or .set
584    noat is in effect).  All these numbers are reasonably small.
585
586    Generating two instruction sequences must be handled carefully to
587    ensure that delay slots are handled correctly.  Fortunately, there
588    are a limited number of cases.  When the second instruction
589    sequence is generated, append_insn is directed to maintain the
590    existing delay slot information, so it continues to apply to any
591    code after the second instruction sequence.  This means that the
592    second instruction sequence must not impose any requirements not
593    required by the first instruction sequence.
594
595    These variant frags are then handled in functions called by the
596    machine independent code.  md_estimate_size_before_relax returns
597    the final size of the frag.  md_convert_frag sets up the final form
598    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
599    one if needed.  */
600 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
601   ((relax_substateT) \
602    (((old) << 23) \
603     | ((new) << 16) \
604     | (((reloc1) + 64) << 9) \
605     | (((reloc2) + 64) << 2) \
606     | ((reloc3) ? (1 << 1) : 0) \
607     | ((warn) ? 1 : 0)))
608 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
609 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
610 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
611 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
612 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
613 #define RELAX_WARN(i) ((i) & 1)
614
615 /* For mips16 code, we use an entirely different form of relaxation.
616    mips16 supports two versions of most instructions which take
617    immediate values: a small one which takes some small value, and a
618    larger one which takes a 16 bit value.  Since branches also follow
619    this pattern, relaxing these values is required.
620
621    We can assemble both mips16 and normal MIPS code in a single
622    object.  Therefore, we need to support this type of relaxation at
623    the same time that we support the relaxation described above.  We
624    use the high bit of the subtype field to distinguish these cases.
625
626    The information we store for this type of relaxation is the
627    argument code found in the opcode file for this relocation, whether
628    the user explicitly requested a small or extended form, and whether
629    the relocation is in a jump or jal delay slot.  That tells us the
630    size of the value, and how it should be stored.  We also store
631    whether the fragment is considered to be extended or not.  We also
632    store whether this is known to be a branch to a different section,
633    whether we have tried to relax this frag yet, and whether we have
634    ever extended a PC relative fragment because of a shift count.  */
635 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
636   (0x80000000                                                   \
637    | ((type) & 0xff)                                            \
638    | ((small) ? 0x100 : 0)                                      \
639    | ((ext) ? 0x200 : 0)                                        \
640    | ((dslot) ? 0x400 : 0)                                      \
641    | ((jal_dslot) ? 0x800 : 0))
642 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
643 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
644 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
645 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
646 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
647 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
648 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
649 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
650 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
651 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
652 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
653 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
654 \f
655 /* Prototypes for static functions.  */
656
657 #ifdef __STDC__
658 #define internalError() \
659     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
660 #else
661 #define internalError() as_fatal (_("MIPS internal Error"));
662 #endif
663
664 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
665
666 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
667                                   unsigned int reg, enum mips_regclass class));
668 static int reg_needs_delay PARAMS ((unsigned int));
669 static void mips16_mark_labels PARAMS ((void));
670 static void append_insn PARAMS ((char *place,
671                                  struct mips_cl_insn * ip,
672                                  expressionS * p,
673                                  bfd_reloc_code_real_type *r,
674                                  boolean));
675 static void mips_no_prev_insn PARAMS ((int));
676 static void mips_emit_delays PARAMS ((boolean));
677 #ifdef USE_STDARG
678 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
679                                  const char *name, const char *fmt,
680                                  ...));
681 #else
682 static void macro_build ();
683 #endif
684 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
685                                         const char *, const char *,
686                                         va_list));
687 static void macro_build_lui PARAMS ((char *place, int *counter,
688                                      expressionS * ep, int regnum));
689 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
690 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
691                                          expressionS *));
692 static void load_register PARAMS ((int *, int, expressionS *, int));
693 static void load_address PARAMS ((int *, int, expressionS *, int, int *));
694 static void move_register PARAMS ((int *, int, int));
695 static void macro PARAMS ((struct mips_cl_insn * ip));
696 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
697 #ifdef LOSING_COMPILER
698 static void macro2 PARAMS ((struct mips_cl_insn * ip));
699 #endif
700 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
701 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
702 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
703                                   boolean, boolean, unsigned long *,
704                                   boolean *, unsigned short *));
705 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
706 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
707 static int my_getSmallExpression PARAMS ((expressionS *, char *));
708 static void my_getExpression PARAMS ((expressionS *, char *));
709 #ifdef OBJ_ELF
710 static int support_64bit_objects PARAMS((void));
711 #endif
712 static symbolS *get_symbol PARAMS ((void));
713 static void mips_align PARAMS ((int to, int fill, symbolS *label));
714 static void s_align PARAMS ((int));
715 static void s_change_sec PARAMS ((int));
716 static void s_cons PARAMS ((int));
717 static void s_float_cons PARAMS ((int));
718 static void s_mips_globl PARAMS ((int));
719 static void s_option PARAMS ((int));
720 static void s_mipsset PARAMS ((int));
721 static void s_abicalls PARAMS ((int));
722 static void s_cpload PARAMS ((int));
723 static void s_cpsetup PARAMS ((int));
724 static void s_cplocal PARAMS ((int));
725 static void s_cprestore PARAMS ((int));
726 static void s_cpreturn PARAMS ((int));
727 static void s_gpvalue PARAMS ((int));
728 static void s_gpword PARAMS ((int));
729 static void s_cpadd PARAMS ((int));
730 static void s_insn PARAMS ((int));
731 static void md_obj_begin PARAMS ((void));
732 static void md_obj_end PARAMS ((void));
733 static long get_number PARAMS ((void));
734 static void s_mips_ent PARAMS ((int));
735 static void s_mips_end PARAMS ((int));
736 static void s_mips_frame PARAMS ((int));
737 static void s_mips_mask PARAMS ((int));
738 static void s_mips_stab PARAMS ((int));
739 static void s_mips_weakext PARAMS ((int));
740 static void s_file PARAMS ((int));
741 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
742 static const char *mips_isa_to_str PARAMS ((int));
743 static const char *mips_cpu_to_str PARAMS ((int));
744 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
745 static void show PARAMS ((FILE *, char *, int *, int *));
746 #ifdef OBJ_ELF
747 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
748 #endif
749
750 /* Return values of my_getSmallExpression().  */
751
752 enum small_ex_type
753 {
754   S_EX_NONE = 0,
755   S_EX_REGISTER,
756
757   /* Direct relocation creation by %percent_op().  */
758   S_EX_HALF,
759   S_EX_HI,
760   S_EX_LO,
761   S_EX_GP_REL,
762   S_EX_GOT,
763   S_EX_CALL16,
764   S_EX_GOT_DISP,
765   S_EX_GOT_PAGE,
766   S_EX_GOT_OFST,
767   S_EX_GOT_HI,
768   S_EX_GOT_LO,
769   S_EX_NEG,
770   S_EX_HIGHER,
771   S_EX_HIGHEST,
772   S_EX_CALL_HI,
773   S_EX_CALL_LO
774 };
775
776 /* Table and functions used to map between CPU/ISA names, and
777    ISA levels, and CPU numbers.  */
778
779 struct mips_cpu_info
780 {
781   const char *name;           /* CPU or ISA name.  */
782   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
783   int isa;                    /* ISA level.  */
784   int cpu;                    /* CPU number (default CPU if ISA).  */
785 };
786
787 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
788 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
789 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
790 \f
791 /* Pseudo-op table.
792
793    The following pseudo-ops from the Kane and Heinrich MIPS book
794    should be defined here, but are currently unsupported: .alias,
795    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
796
797    The following pseudo-ops from the Kane and Heinrich MIPS book are
798    specific to the type of debugging information being generated, and
799    should be defined by the object format: .aent, .begin, .bend,
800    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
801    .vreg.
802
803    The following pseudo-ops from the Kane and Heinrich MIPS book are
804    not MIPS CPU specific, but are also not specific to the object file
805    format.  This file is probably the best place to define them, but
806    they are not currently supported: .asm0, .endr, .lab, .repeat,
807    .struct.  */
808
809 static const pseudo_typeS mips_pseudo_table[] =
810 {
811   /* MIPS specific pseudo-ops.  */
812   {"option", s_option, 0},
813   {"set", s_mipsset, 0},
814   {"rdata", s_change_sec, 'r'},
815   {"sdata", s_change_sec, 's'},
816   {"livereg", s_ignore, 0},
817   {"abicalls", s_abicalls, 0},
818   {"cpload", s_cpload, 0},
819   {"cpsetup", s_cpsetup, 0},
820   {"cplocal", s_cplocal, 0},
821   {"cprestore", s_cprestore, 0},
822   {"cpreturn", s_cpreturn, 0},
823   {"gpvalue", s_gpvalue, 0},
824   {"gpword", s_gpword, 0},
825   {"cpadd", s_cpadd, 0},
826   {"insn", s_insn, 0},
827
828   /* Relatively generic pseudo-ops that happen to be used on MIPS
829      chips.  */
830   {"asciiz", stringer, 1},
831   {"bss", s_change_sec, 'b'},
832   {"err", s_err, 0},
833   {"half", s_cons, 1},
834   {"dword", s_cons, 3},
835   {"weakext", s_mips_weakext, 0},
836
837   /* These pseudo-ops are defined in read.c, but must be overridden
838      here for one reason or another.  */
839   {"align", s_align, 0},
840   {"byte", s_cons, 0},
841   {"data", s_change_sec, 'd'},
842   {"double", s_float_cons, 'd'},
843   {"float", s_float_cons, 'f'},
844   {"globl", s_mips_globl, 0},
845   {"global", s_mips_globl, 0},
846   {"hword", s_cons, 1},
847   {"int", s_cons, 2},
848   {"long", s_cons, 2},
849   {"octa", s_cons, 4},
850   {"quad", s_cons, 3},
851   {"short", s_cons, 1},
852   {"single", s_float_cons, 'f'},
853   {"stabn", s_mips_stab, 'n'},
854   {"text", s_change_sec, 't'},
855   {"word", s_cons, 2},
856
857 #ifdef MIPS_STABS_ELF
858   { "extern", ecoff_directive_extern, 0},
859 #endif
860
861   { NULL, NULL, 0 },
862 };
863
864 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
865 {
866   /* These pseudo-ops should be defined by the object file format.
867      However, a.out doesn't support them, so we have versions here.  */
868   {"aent", s_mips_ent, 1},
869   {"bgnb", s_ignore, 0},
870   {"end", s_mips_end, 0},
871   {"endb", s_ignore, 0},
872   {"ent", s_mips_ent, 0},
873   {"file", s_file, 0},
874   {"fmask", s_mips_mask, 'F'},
875   {"frame", s_mips_frame, 0},
876   {"loc", s_ignore, 0},
877   {"mask", s_mips_mask, 'R'},
878   {"verstamp", s_ignore, 0},
879   { NULL, NULL, 0 },
880 };
881
882 extern void pop_insert PARAMS ((const pseudo_typeS *));
883
884 void
885 mips_pop_insert ()
886 {
887   pop_insert (mips_pseudo_table);
888   if (! ECOFF_DEBUGGING)
889     pop_insert (mips_nonecoff_pseudo_table);
890 }
891 \f
892 /* Symbols labelling the current insn.  */
893
894 struct insn_label_list
895 {
896   struct insn_label_list *next;
897   symbolS *label;
898 };
899
900 static struct insn_label_list *insn_labels;
901 static struct insn_label_list *free_insn_labels;
902
903 static void mips_clear_insn_labels PARAMS ((void));
904
905 static inline void
906 mips_clear_insn_labels ()
907 {
908   register struct insn_label_list **pl;
909
910   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
911     ;
912   *pl = insn_labels;
913   insn_labels = NULL;
914 }
915 \f
916 static char *expr_end;
917
918 /* Expressions which appear in instructions.  These are set by
919    mips_ip.  */
920
921 static expressionS imm_expr;
922 static expressionS offset_expr;
923
924 /* Relocs associated with imm_expr and offset_expr.  */
925
926 static bfd_reloc_code_real_type imm_reloc[3]
927   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
928 static bfd_reloc_code_real_type offset_reloc[3]
929   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
930
931 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
932
933 static boolean imm_unmatched_hi;
934
935 /* These are set by mips16_ip if an explicit extension is used.  */
936
937 static boolean mips16_small, mips16_ext;
938
939 #ifdef MIPS_STABS_ELF
940 /* The pdr segment for per procedure frame/regmask info */
941
942 static segT pdr_seg;
943 #endif
944
945 static const char *
946 mips_isa_to_str (isa)
947      int isa;
948 {
949   const struct mips_cpu_info *ci;
950   static char s[20];
951
952   ci = mips_cpu_info_from_isa (isa);
953   if (ci != NULL)
954     return (ci->name);
955
956   sprintf (s, "ISA#%d", isa);
957   return s;
958 }
959
960 static const char *
961 mips_cpu_to_str (cpu)
962      int cpu;
963 {
964   const struct mips_cpu_info *ci;
965   static char s[16];
966
967   ci = mips_cpu_info_from_cpu (cpu);
968   if (ci != NULL)
969     return (ci->name);
970
971   sprintf (s, "CPU#%d", cpu);
972   return s;
973 }
974
975 /* The default target format to use.  */
976
977 const char *
978 mips_target_format ()
979 {
980   switch (OUTPUT_FLAVOR)
981     {
982     case bfd_target_aout_flavour:
983       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
984     case bfd_target_ecoff_flavour:
985       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
986     case bfd_target_coff_flavour:
987       return "pe-mips";
988     case bfd_target_elf_flavour:
989 #ifdef TE_TMIPS
990       /* This is traditional mips */
991       return (target_big_endian
992               ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
993                  : "elf32-tradbigmips")
994               : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
995                  : "elf32-tradlittlemips"));
996 #else
997       return (target_big_endian
998               ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
999               : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1000                  : "elf32-littlemips"));
1001 #endif
1002     default:
1003       abort ();
1004       return NULL;
1005     }
1006 }
1007
1008 /* This function is called once, at assembler startup time.  It should
1009    set up all the tables, etc. that the MD part of the assembler will need.  */
1010
1011 void
1012 md_begin ()
1013 {
1014   register const char *retval = NULL;
1015   int i = 0;
1016   const char *cpu;
1017   char *a = NULL;
1018   int broken = 0;
1019   int mips_isa_from_cpu;
1020   int target_cpu_had_mips16 = 0;
1021   const struct mips_cpu_info *ci;
1022
1023   /* GP relative stuff not working for PE */
1024   if (strncmp (TARGET_OS, "pe", 2) == 0
1025       && g_switch_value != 0)
1026     {
1027       if (g_switch_seen)
1028         as_bad (_("-G not supported in this configuration."));
1029       g_switch_value = 0;
1030     }
1031
1032   cpu = TARGET_CPU;
1033   if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1034     {
1035       a = xmalloc (sizeof TARGET_CPU);
1036       strcpy (a, TARGET_CPU);
1037       a[(sizeof TARGET_CPU) - 3] = '\0';
1038       cpu = a;
1039     }
1040
1041   if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
1042     {
1043       target_cpu_had_mips16 = 1;
1044       cpu += sizeof "mips16" - 1;
1045     }
1046
1047   if (mips_opts.mips16 < 0)
1048     mips_opts.mips16 = target_cpu_had_mips16;
1049
1050   /* Backward compatibility for historic -mcpu= option.  Check for
1051      incompatible options, warn if -mcpu is used.  */
1052   if (mips_cpu != CPU_UNKNOWN
1053       && mips_arch != CPU_UNKNOWN
1054       && mips_cpu != mips_arch)
1055     {
1056       as_fatal (_("The -mcpu option can't be used together with -march. "
1057                   "Use -mtune instead of -mcpu."));
1058     }
1059
1060   if (mips_cpu != CPU_UNKNOWN
1061       && mips_tune != CPU_UNKNOWN
1062       && mips_cpu != mips_tune)
1063     {
1064       as_fatal (_("The -mcpu option can't be used together with -mtune. "
1065                   "Use -march instead of -mcpu."));
1066     }
1067
1068 #if 1
1069   /* For backward compatibility, let -mipsN set various defaults.  */
1070   /* This code should go away, to be replaced with something rather more
1071      draconian.  Until GCC 3.1 has been released for some reasonable
1072      amount of time, however, we need to support this.  */
1073   if (mips_opts.isa != ISA_UNKNOWN)
1074     {
1075       /* Translate -mipsN to the appropriate settings of file_mips_gp32
1076          and file_mips_fp32.  Tag binaries as using the mipsN ISA.  */
1077       if (file_mips_gp32 < 0)
1078         {
1079           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1080             file_mips_gp32 = 0;
1081           else
1082             file_mips_gp32 = 1;
1083         }
1084       if (file_mips_fp32 < 0)
1085         {
1086           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1087             file_mips_fp32 = 0;
1088           else
1089             file_mips_fp32 = 1;
1090         }
1091
1092       ci = mips_cpu_info_from_isa (mips_opts.isa);
1093       assert (ci != NULL);
1094       /* -mipsN has higher priority than -mcpu but lower than -march.  */
1095       if (mips_arch == CPU_UNKNOWN)
1096         mips_arch = ci->cpu;
1097
1098       /* Default mips_abi.  */
1099       if (mips_opts.abi == NO_ABI)
1100         {
1101           if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
1102             mips_opts.abi = O32_ABI;
1103           else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
1104             mips_opts.abi = O64_ABI;
1105         }
1106     }
1107
1108   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1109     {
1110       ci = mips_cpu_info_from_cpu (mips_cpu);
1111       assert (ci != NULL);
1112       mips_arch = ci->cpu;
1113       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
1114                  "-mtune instead."));
1115     }
1116
1117   /* Set tune from -mcpu, not from -mipsN.  */
1118   if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1119     {
1120       ci = mips_cpu_info_from_cpu (mips_cpu);
1121       assert (ci != NULL);
1122       mips_tune = ci->cpu;
1123     }
1124
1125   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1126      specified on the command line, or some other value if one was.
1127      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1128      the command line, or will be set otherwise if one was.  */
1129
1130   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1131     /* Handled above.  */;
1132 #else
1133   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1134     {
1135       ci = mips_cpu_info_from_cpu (mips_cpu);
1136       assert (ci != NULL);
1137       mips_arch = ci->cpu;
1138       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
1139                  "-mtune instead."));
1140     }
1141
1142   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1143      specified on the command line, or some other value if one was.
1144      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1145      the command line, or will be set otherwise if one was.  */
1146
1147   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1148     {
1149       /* We have to check if the isa is the default isa of arch.  Otherwise
1150          we'll get invalid object file headers.  */
1151       ci = mips_cpu_info_from_cpu (mips_arch);
1152       assert (ci != NULL);
1153       if (mips_opts.isa != ci->isa)
1154         {
1155           /* This really should be an error instead of a warning, but old
1156              compilers only have -mcpu which sets both arch and tune.  For
1157              now, we discard arch and preserve tune.  */
1158           as_warn (_("The -march option is incompatible to -mipsN and "
1159                      "therefore ignored."));
1160           if (mips_tune == CPU_UNKNOWN)
1161             mips_tune = mips_arch;
1162           ci = mips_cpu_info_from_isa (mips_opts.isa);
1163           assert (ci != NULL);
1164           mips_arch = ci->cpu;
1165         }
1166     }
1167 #endif
1168   else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
1169     {
1170       /* We have ARCH, we need ISA.  */
1171       ci = mips_cpu_info_from_cpu (mips_arch);
1172       assert (ci != NULL);
1173       mips_opts.isa = ci->isa;
1174     }
1175   else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1176     {
1177       /* We have ISA, we need default ARCH.  */
1178       ci = mips_cpu_info_from_isa (mips_opts.isa);
1179       assert (ci != NULL);
1180       mips_arch = ci->cpu;
1181     }
1182   else
1183     {
1184       /* We need to set both ISA and ARCH from target cpu.  */
1185       ci = mips_cpu_info_from_name (cpu);
1186       if (ci == NULL)
1187         ci = mips_cpu_info_from_cpu (CPU_R3000);
1188       assert (ci != NULL);
1189       mips_opts.isa = ci->isa;
1190       mips_arch = ci->cpu;
1191     }
1192
1193   if (mips_tune == CPU_UNKNOWN)
1194     mips_tune = mips_arch;
1195
1196   ci = mips_cpu_info_from_cpu (mips_arch);
1197   assert (ci != NULL);
1198   mips_isa_from_cpu = ci->isa;
1199
1200   /* End of TARGET_CPU processing, get rid of malloced memory
1201      if necessary.  */
1202   cpu = NULL;
1203   if (a != NULL)
1204     {
1205       free (a);
1206       a = NULL;
1207     }
1208
1209   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
1210     as_bad (_("trap exception not supported at ISA 1"));
1211
1212   /* Set the EABI kind based on the ISA before the user gets
1213      to change the ISA with directives.  This isn't really
1214      the best, but then neither is basing the abi on the isa.  */
1215   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
1216       && mips_opts.abi == EABI_ABI)
1217     mips_eabi64 = 1;
1218
1219   /* If they asked for mips1 or mips2 and a cpu that is
1220      mips3 or greater, then mark the object file 32BITMODE.  */
1221   if (mips_isa_from_cpu != ISA_UNKNOWN
1222       && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1223       && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1224     mips_32bitmode = 1;
1225
1226   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1227     as_warn (_("Could not set architecture and machine"));
1228
1229   if (file_mips_gp32 < 0)
1230     file_mips_gp32 = 0;
1231   if (file_mips_fp32 < 0)
1232     file_mips_fp32 = 0;
1233
1234   file_mips_isa = mips_opts.isa;
1235   file_mips_abi = mips_opts.abi;
1236   mips_opts.gp32 = file_mips_gp32;
1237   mips_opts.fp32 = file_mips_fp32;
1238
1239   op_hash = hash_new ();
1240
1241   for (i = 0; i < NUMOPCODES;)
1242     {
1243       const char *name = mips_opcodes[i].name;
1244
1245       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1246       if (retval != NULL)
1247         {
1248           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1249                    mips_opcodes[i].name, retval);
1250           /* Probably a memory allocation problem?  Give up now.  */
1251           as_fatal (_("Broken assembler.  No assembly attempted."));
1252         }
1253       do
1254         {
1255           if (mips_opcodes[i].pinfo != INSN_MACRO)
1256             {
1257               if (!validate_mips_insn (&mips_opcodes[i]))
1258                 broken = 1;
1259             }
1260           ++i;
1261         }
1262       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1263     }
1264
1265   mips16_op_hash = hash_new ();
1266
1267   i = 0;
1268   while (i < bfd_mips16_num_opcodes)
1269     {
1270       const char *name = mips16_opcodes[i].name;
1271
1272       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1273       if (retval != NULL)
1274         as_fatal (_("internal: can't hash `%s': %s"),
1275                   mips16_opcodes[i].name, retval);
1276       do
1277         {
1278           if (mips16_opcodes[i].pinfo != INSN_MACRO
1279               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1280                   != mips16_opcodes[i].match))
1281             {
1282               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1283                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1284               broken = 1;
1285             }
1286           ++i;
1287         }
1288       while (i < bfd_mips16_num_opcodes
1289              && strcmp (mips16_opcodes[i].name, name) == 0);
1290     }
1291
1292   if (broken)
1293     as_fatal (_("Broken assembler.  No assembly attempted."));
1294
1295   /* We add all the general register names to the symbol table.  This
1296      helps us detect invalid uses of them.  */
1297   for (i = 0; i < 32; i++)
1298     {
1299       char buf[5];
1300
1301       sprintf (buf, "$%d", i);
1302       symbol_table_insert (symbol_new (buf, reg_section, i,
1303                                        &zero_address_frag));
1304     }
1305   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1306                                    &zero_address_frag));
1307   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1308                                    &zero_address_frag));
1309   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1310                                    &zero_address_frag));
1311   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1312                                    &zero_address_frag));
1313   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1314                                    &zero_address_frag));
1315   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1316                                    &zero_address_frag));
1317   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1318                                    &zero_address_frag));
1319
1320   mips_no_prev_insn (false);
1321
1322   mips_gprmask = 0;
1323   mips_cprmask[0] = 0;
1324   mips_cprmask[1] = 0;
1325   mips_cprmask[2] = 0;
1326   mips_cprmask[3] = 0;
1327
1328   /* set the default alignment for the text section (2**2) */
1329   record_alignment (text_section, 2);
1330
1331   if (USE_GLOBAL_POINTER_OPT)
1332     bfd_set_gp_size (stdoutput, g_switch_value);
1333
1334   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1335     {
1336       /* On a native system, sections must be aligned to 16 byte
1337          boundaries.  When configured for an embedded ELF target, we
1338          don't bother.  */
1339       if (strcmp (TARGET_OS, "elf") != 0)
1340         {
1341           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1342           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1343           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1344         }
1345
1346       /* Create a .reginfo section for register masks and a .mdebug
1347          section for debugging information.  */
1348       {
1349         segT seg;
1350         subsegT subseg;
1351         flagword flags;
1352         segT sec;
1353
1354         seg = now_seg;
1355         subseg = now_subseg;
1356
1357         /* The ABI says this section should be loaded so that the
1358            running program can access it.  However, we don't load it
1359            if we are configured for an embedded target */
1360         flags = SEC_READONLY | SEC_DATA;
1361         if (strcmp (TARGET_OS, "elf") != 0)
1362           flags |= SEC_ALLOC | SEC_LOAD;
1363
1364         if (! HAVE_NEWABI)
1365           {
1366             sec = subseg_new (".reginfo", (subsegT) 0);
1367
1368             (void) bfd_set_section_flags (stdoutput, sec, flags);
1369             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1370
1371 #ifdef OBJ_ELF
1372             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1373 #endif
1374           }
1375         else
1376           {
1377             /* The 64-bit ABI uses a .MIPS.options section rather than
1378                .reginfo section.  */
1379             sec = subseg_new (".MIPS.options", (subsegT) 0);
1380             (void) bfd_set_section_flags (stdoutput, sec, flags);
1381             (void) bfd_set_section_alignment (stdoutput, sec, 3);
1382
1383 #ifdef OBJ_ELF
1384             /* Set up the option header.  */
1385             {
1386               Elf_Internal_Options opthdr;
1387               char *f;
1388
1389               opthdr.kind = ODK_REGINFO;
1390               opthdr.size = (sizeof (Elf_External_Options)
1391                              + sizeof (Elf64_External_RegInfo));
1392               opthdr.section = 0;
1393               opthdr.info = 0;
1394               f = frag_more (sizeof (Elf_External_Options));
1395               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1396                                              (Elf_External_Options *) f);
1397
1398               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1399             }
1400 #endif
1401           }
1402
1403         if (ECOFF_DEBUGGING)
1404           {
1405             sec = subseg_new (".mdebug", (subsegT) 0);
1406             (void) bfd_set_section_flags (stdoutput, sec,
1407                                           SEC_HAS_CONTENTS | SEC_READONLY);
1408             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1409           }
1410
1411 #ifdef MIPS_STABS_ELF
1412         pdr_seg = subseg_new (".pdr", (subsegT) 0);
1413         (void) bfd_set_section_flags (stdoutput, pdr_seg,
1414                              SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1415         (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1416 #endif
1417
1418         subseg_set (seg, subseg);
1419       }
1420     }
1421
1422   if (! ECOFF_DEBUGGING)
1423     md_obj_begin ();
1424 }
1425
1426 void
1427 md_mips_end ()
1428 {
1429   if (! ECOFF_DEBUGGING)
1430     md_obj_end ();
1431 }
1432
1433 void
1434 md_assemble (str)
1435      char *str;
1436 {
1437   struct mips_cl_insn insn;
1438   bfd_reloc_code_real_type unused_reloc[3]
1439     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1440
1441   imm_expr.X_op = O_absent;
1442   imm_unmatched_hi = false;
1443   offset_expr.X_op = O_absent;
1444   imm_reloc[0] = BFD_RELOC_UNUSED;
1445   imm_reloc[1] = BFD_RELOC_UNUSED;
1446   imm_reloc[2] = BFD_RELOC_UNUSED;
1447   offset_reloc[0] = BFD_RELOC_UNUSED;
1448   offset_reloc[1] = BFD_RELOC_UNUSED;
1449   offset_reloc[2] = BFD_RELOC_UNUSED;
1450
1451   if (mips_opts.mips16)
1452     mips16_ip (str, &insn);
1453   else
1454     {
1455       mips_ip (str, &insn);
1456       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1457             str, insn.insn_opcode));
1458     }
1459
1460   if (insn_error)
1461     {
1462       as_bad ("%s `%s'", insn_error, str);
1463       return;
1464     }
1465
1466   if (insn.insn_mo->pinfo == INSN_MACRO)
1467     {
1468       if (mips_opts.mips16)
1469         mips16_macro (&insn);
1470       else
1471         macro (&insn);
1472     }
1473   else
1474     {
1475       if (imm_expr.X_op != O_absent)
1476         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1477       else if (offset_expr.X_op != O_absent)
1478         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1479       else
1480         append_insn (NULL, &insn, NULL, unused_reloc, false);
1481     }
1482 }
1483
1484 /* See whether instruction IP reads register REG.  CLASS is the type
1485    of register.  */
1486
1487 static int
1488 insn_uses_reg (ip, reg, class)
1489      struct mips_cl_insn *ip;
1490      unsigned int reg;
1491      enum mips_regclass class;
1492 {
1493   if (class == MIPS16_REG)
1494     {
1495       assert (mips_opts.mips16);
1496       reg = mips16_to_32_reg_map[reg];
1497       class = MIPS_GR_REG;
1498     }
1499
1500   /* Don't report on general register 0, since it never changes.  */
1501   if (class == MIPS_GR_REG && reg == 0)
1502     return 0;
1503
1504   if (class == MIPS_FP_REG)
1505     {
1506       assert (! mips_opts.mips16);
1507       /* If we are called with either $f0 or $f1, we must check $f0.
1508          This is not optimal, because it will introduce an unnecessary
1509          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1510          need to distinguish reading both $f0 and $f1 or just one of
1511          them.  Note that we don't have to check the other way,
1512          because there is no instruction that sets both $f0 and $f1
1513          and requires a delay.  */
1514       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1515           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1516               == (reg &~ (unsigned) 1)))
1517         return 1;
1518       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1519           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1520               == (reg &~ (unsigned) 1)))
1521         return 1;
1522     }
1523   else if (! mips_opts.mips16)
1524     {
1525       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1526           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1527         return 1;
1528       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1529           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1530         return 1;
1531     }
1532   else
1533     {
1534       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1535           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1536                                     & MIPS16OP_MASK_RX)]
1537               == reg))
1538         return 1;
1539       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1540           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1541                                     & MIPS16OP_MASK_RY)]
1542               == reg))
1543         return 1;
1544       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1545           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1546                                     & MIPS16OP_MASK_MOVE32Z)]
1547               == reg))
1548         return 1;
1549       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1550         return 1;
1551       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1552         return 1;
1553       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1554         return 1;
1555       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1556           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1557               & MIPS16OP_MASK_REGR32) == reg)
1558         return 1;
1559     }
1560
1561   return 0;
1562 }
1563
1564 /* This function returns true if modifying a register requires a
1565    delay.  */
1566
1567 static int
1568 reg_needs_delay (reg)
1569      unsigned int reg;
1570 {
1571   unsigned long prev_pinfo;
1572
1573   prev_pinfo = prev_insn.insn_mo->pinfo;
1574   if (! mips_opts.noreorder
1575       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1576       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1577           || (! gpr_interlocks
1578               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1579     {
1580       /* A load from a coprocessor or from memory.  All load
1581          delays delay the use of general register rt for one
1582          instruction on the r3000.  The r6000 and r4000 use
1583          interlocks.  */
1584       /* Itbl support may require additional care here.  */
1585       know (prev_pinfo & INSN_WRITE_GPR_T);
1586       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1587         return 1;
1588     }
1589
1590   return 0;
1591 }
1592
1593 /* Mark instruction labels in mips16 mode.  This permits the linker to
1594    handle them specially, such as generating jalx instructions when
1595    needed.  We also make them odd for the duration of the assembly, in
1596    order to generate the right sort of code.  We will make them even
1597    in the adjust_symtab routine, while leaving them marked.  This is
1598    convenient for the debugger and the disassembler.  The linker knows
1599    to make them odd again.  */
1600
1601 static void
1602 mips16_mark_labels ()
1603 {
1604   if (mips_opts.mips16)
1605     {
1606       struct insn_label_list *l;
1607       valueT val;
1608
1609       for (l = insn_labels; l != NULL; l = l->next)
1610         {
1611 #ifdef OBJ_ELF
1612           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1613             S_SET_OTHER (l->label, STO_MIPS16);
1614 #endif
1615           val = S_GET_VALUE (l->label);
1616           if ((val & 1) == 0)
1617             S_SET_VALUE (l->label, val + 1);
1618         }
1619     }
1620 }
1621
1622 /* Output an instruction.  PLACE is where to put the instruction; if
1623    it is NULL, this uses frag_more to get room.  IP is the instruction
1624    information.  ADDRESS_EXPR is an operand of the instruction to be
1625    used with RELOC_TYPE.  */
1626
1627 static void
1628 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1629      char *place;
1630      struct mips_cl_insn *ip;
1631      expressionS *address_expr;
1632      bfd_reloc_code_real_type *reloc_type;
1633      boolean unmatched_hi;
1634 {
1635   register unsigned long prev_pinfo, pinfo;
1636   char *f;
1637   fixS *fixp[3];
1638   int nops = 0;
1639
1640   /* Mark instruction labels in mips16 mode.  */
1641   if (mips_opts.mips16)
1642     mips16_mark_labels ();
1643
1644   prev_pinfo = prev_insn.insn_mo->pinfo;
1645   pinfo = ip->insn_mo->pinfo;
1646
1647   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1648     {
1649       int prev_prev_nop;
1650
1651       /* If the previous insn required any delay slots, see if we need
1652          to insert a NOP or two.  There are eight kinds of possible
1653          hazards, of which an instruction can have at most one type.
1654          (1) a load from memory delay
1655          (2) a load from a coprocessor delay
1656          (3) an unconditional branch delay
1657          (4) a conditional branch delay
1658          (5) a move to coprocessor register delay
1659          (6) a load coprocessor register from memory delay
1660          (7) a coprocessor condition code delay
1661          (8) a HI/LO special register delay
1662
1663          There are a lot of optimizations we could do that we don't.
1664          In particular, we do not, in general, reorder instructions.
1665          If you use gcc with optimization, it will reorder
1666          instructions and generally do much more optimization then we
1667          do here; repeating all that work in the assembler would only
1668          benefit hand written assembly code, and does not seem worth
1669          it.  */
1670
1671       /* This is how a NOP is emitted.  */
1672 #define emit_nop()                                      \
1673   (mips_opts.mips16                                     \
1674    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1675    : md_number_to_chars (frag_more (4), 0, 4))
1676
1677       /* The previous insn might require a delay slot, depending upon
1678          the contents of the current insn.  */
1679       if (! mips_opts.mips16
1680           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1681           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1682                && ! cop_interlocks)
1683               || (! gpr_interlocks
1684                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1685         {
1686           /* A load from a coprocessor or from memory.  All load
1687              delays delay the use of general register rt for one
1688              instruction on the r3000.  The r6000 and r4000 use
1689              interlocks.  */
1690           /* Itbl support may require additional care here.  */
1691           know (prev_pinfo & INSN_WRITE_GPR_T);
1692           if (mips_optimize == 0
1693               || insn_uses_reg (ip,
1694                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1695                                  & OP_MASK_RT),
1696                                 MIPS_GR_REG))
1697             ++nops;
1698         }
1699       else if (! mips_opts.mips16
1700                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1701                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1702                     && ! cop_interlocks)
1703                    || (mips_opts.isa == ISA_MIPS1
1704                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1705         {
1706           /* A generic coprocessor delay.  The previous instruction
1707              modified a coprocessor general or control register.  If
1708              it modified a control register, we need to avoid any
1709              coprocessor instruction (this is probably not always
1710              required, but it sometimes is).  If it modified a general
1711              register, we avoid using that register.
1712
1713              On the r6000 and r4000 loading a coprocessor register
1714              from memory is interlocked, and does not require a delay.
1715
1716              This case is not handled very well.  There is no special
1717              knowledge of CP0 handling, and the coprocessors other
1718              than the floating point unit are not distinguished at
1719              all.  */
1720           /* Itbl support may require additional care here. FIXME!
1721              Need to modify this to include knowledge about
1722              user specified delays!  */
1723           if (prev_pinfo & INSN_WRITE_FPR_T)
1724             {
1725               if (mips_optimize == 0
1726                   || insn_uses_reg (ip,
1727                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1728                                      & OP_MASK_FT),
1729                                     MIPS_FP_REG))
1730                 ++nops;
1731             }
1732           else if (prev_pinfo & INSN_WRITE_FPR_S)
1733             {
1734               if (mips_optimize == 0
1735                   || insn_uses_reg (ip,
1736                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1737                                      & OP_MASK_FS),
1738                                     MIPS_FP_REG))
1739                 ++nops;
1740             }
1741           else
1742             {
1743               /* We don't know exactly what the previous instruction
1744                  does.  If the current instruction uses a coprocessor
1745                  register, we must insert a NOP.  If previous
1746                  instruction may set the condition codes, and the
1747                  current instruction uses them, we must insert two
1748                  NOPS.  */
1749               /* Itbl support may require additional care here.  */
1750               if (mips_optimize == 0
1751                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1752                       && (pinfo & INSN_READ_COND_CODE)))
1753                 nops += 2;
1754               else if (pinfo & INSN_COP)
1755                 ++nops;
1756             }
1757         }
1758       else if (! mips_opts.mips16
1759                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1760                && (prev_pinfo & INSN_WRITE_COND_CODE)
1761                && ! cop_interlocks)
1762         {
1763           /* The previous instruction sets the coprocessor condition
1764              codes, but does not require a general coprocessor delay
1765              (this means it is a floating point comparison
1766              instruction).  If this instruction uses the condition
1767              codes, we need to insert a single NOP.  */
1768           /* Itbl support may require additional care here.  */
1769           if (mips_optimize == 0
1770               || (pinfo & INSN_READ_COND_CODE))
1771             ++nops;
1772         }
1773
1774       /* If we're fixing up mfhi/mflo for the r7000 and the
1775          previous insn was an mfhi/mflo and the current insn
1776          reads the register that the mfhi/mflo wrote to, then
1777          insert two nops.  */
1778
1779       else if (mips_7000_hilo_fix
1780                && MF_HILO_INSN (prev_pinfo)
1781                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1782                                       & OP_MASK_RD),
1783                                  MIPS_GR_REG))
1784         {
1785           nops += 2;
1786         }
1787
1788       /* If we're fixing up mfhi/mflo for the r7000 and the
1789          2nd previous insn was an mfhi/mflo and the current insn
1790          reads the register that the mfhi/mflo wrote to, then
1791          insert one nop.  */
1792
1793       else if (mips_7000_hilo_fix
1794                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1795                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1796                                        & OP_MASK_RD),
1797                                     MIPS_GR_REG))
1798
1799         {
1800           nops += 1;
1801         }
1802
1803       else if (prev_pinfo & INSN_READ_LO)
1804         {
1805           /* The previous instruction reads the LO register; if the
1806              current instruction writes to the LO register, we must
1807              insert two NOPS.  Some newer processors have interlocks.
1808              Also the tx39's multiply instructions can be exectuted
1809              immediatly after a read from HI/LO (without the delay),
1810              though the tx39's divide insns still do require the
1811              delay.  */
1812           if (! (hilo_interlocks
1813                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1814               && (mips_optimize == 0
1815                   || (pinfo & INSN_WRITE_LO)))
1816             nops += 2;
1817           /* Most mips16 branch insns don't have a delay slot.
1818              If a read from LO is immediately followed by a branch
1819              to a write to LO we have a read followed by a write
1820              less than 2 insns away.  We assume the target of
1821              a branch might be a write to LO, and insert a nop
1822              between a read and an immediately following branch.  */
1823           else if (mips_opts.mips16
1824                    && (mips_optimize == 0
1825                        || (pinfo & MIPS16_INSN_BRANCH)))
1826             nops += 1;
1827         }
1828       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1829         {
1830           /* The previous instruction reads the HI register; if the
1831              current instruction writes to the HI register, we must
1832              insert a NOP.  Some newer processors have interlocks.
1833              Also the note tx39's multiply above.  */
1834           if (! (hilo_interlocks
1835                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1836               && (mips_optimize == 0
1837                   || (pinfo & INSN_WRITE_HI)))
1838             nops += 2;
1839           /* Most mips16 branch insns don't have a delay slot.
1840              If a read from HI is immediately followed by a branch
1841              to a write to HI we have a read followed by a write
1842              less than 2 insns away.  We assume the target of
1843              a branch might be a write to HI, and insert a nop
1844              between a read and an immediately following branch.  */
1845           else if (mips_opts.mips16
1846                    && (mips_optimize == 0
1847                        || (pinfo & MIPS16_INSN_BRANCH)))
1848             nops += 1;
1849         }
1850
1851       /* If the previous instruction was in a noreorder section, then
1852          we don't want to insert the nop after all.  */
1853       /* Itbl support may require additional care here.  */
1854       if (prev_insn_unreordered)
1855         nops = 0;
1856
1857       /* There are two cases which require two intervening
1858          instructions: 1) setting the condition codes using a move to
1859          coprocessor instruction which requires a general coprocessor
1860          delay and then reading the condition codes 2) reading the HI
1861          or LO register and then writing to it (except on processors
1862          which have interlocks).  If we are not already emitting a NOP
1863          instruction, we must check for these cases compared to the
1864          instruction previous to the previous instruction.  */
1865       if ((! mips_opts.mips16
1866            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1867            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1868            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1869            && (pinfo & INSN_READ_COND_CODE)
1870            && ! cop_interlocks)
1871           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1872               && (pinfo & INSN_WRITE_LO)
1873               && ! (hilo_interlocks
1874                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1875           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1876               && (pinfo & INSN_WRITE_HI)
1877               && ! (hilo_interlocks
1878                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1879         prev_prev_nop = 1;
1880       else
1881         prev_prev_nop = 0;
1882
1883       if (prev_prev_insn_unreordered)
1884         prev_prev_nop = 0;
1885
1886       if (prev_prev_nop && nops == 0)
1887         ++nops;
1888
1889       /* If we are being given a nop instruction, don't bother with
1890          one of the nops we would otherwise output.  This will only
1891          happen when a nop instruction is used with mips_optimize set
1892          to 0.  */
1893       if (nops > 0
1894           && ! mips_opts.noreorder
1895           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1896         --nops;
1897
1898       /* Now emit the right number of NOP instructions.  */
1899       if (nops > 0 && ! mips_opts.noreorder)
1900         {
1901           fragS *old_frag;
1902           unsigned long old_frag_offset;
1903           int i;
1904           struct insn_label_list *l;
1905
1906           old_frag = frag_now;
1907           old_frag_offset = frag_now_fix ();
1908
1909           for (i = 0; i < nops; i++)
1910             emit_nop ();
1911
1912           if (listing)
1913             {
1914               listing_prev_line ();
1915               /* We may be at the start of a variant frag.  In case we
1916                  are, make sure there is enough space for the frag
1917                  after the frags created by listing_prev_line.  The
1918                  argument to frag_grow here must be at least as large
1919                  as the argument to all other calls to frag_grow in
1920                  this file.  We don't have to worry about being in the
1921                  middle of a variant frag, because the variants insert
1922                  all needed nop instructions themselves.  */
1923               frag_grow (40);
1924             }
1925
1926           for (l = insn_labels; l != NULL; l = l->next)
1927             {
1928               valueT val;
1929
1930               assert (S_GET_SEGMENT (l->label) == now_seg);
1931               symbol_set_frag (l->label, frag_now);
1932               val = (valueT) frag_now_fix ();
1933               /* mips16 text labels are stored as odd.  */
1934               if (mips_opts.mips16)
1935                 val += 1;
1936               S_SET_VALUE (l->label, val);
1937             }
1938
1939 #ifndef NO_ECOFF_DEBUGGING
1940           if (ECOFF_DEBUGGING)
1941             ecoff_fix_loc (old_frag, old_frag_offset);
1942 #endif
1943         }
1944       else if (prev_nop_frag != NULL)
1945         {
1946           /* We have a frag holding nops we may be able to remove.  If
1947              we don't need any nops, we can decrease the size of
1948              prev_nop_frag by the size of one instruction.  If we do
1949              need some nops, we count them in prev_nops_required.  */
1950           if (prev_nop_frag_since == 0)
1951             {
1952               if (nops == 0)
1953                 {
1954                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1955                   --prev_nop_frag_holds;
1956                 }
1957               else
1958                 prev_nop_frag_required += nops;
1959             }
1960           else
1961             {
1962               if (prev_prev_nop == 0)
1963                 {
1964                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1965                   --prev_nop_frag_holds;
1966                 }
1967               else
1968                 ++prev_nop_frag_required;
1969             }
1970
1971           if (prev_nop_frag_holds <= prev_nop_frag_required)
1972             prev_nop_frag = NULL;
1973
1974           ++prev_nop_frag_since;
1975
1976           /* Sanity check: by the time we reach the second instruction
1977              after prev_nop_frag, we should have used up all the nops
1978              one way or another.  */
1979           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1980         }
1981     }
1982
1983   if (*reloc_type > BFD_RELOC_UNUSED)
1984     {
1985       /* We need to set up a variant frag.  */
1986       assert (mips_opts.mips16 && address_expr != NULL);
1987       f = frag_var (rs_machine_dependent, 4, 0,
1988                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1989                                          mips16_small, mips16_ext,
1990                                          (prev_pinfo
1991                                           & INSN_UNCOND_BRANCH_DELAY),
1992                                          (*prev_insn_reloc_type
1993                                           == BFD_RELOC_MIPS16_JMP)),
1994                     make_expr_symbol (address_expr), 0, NULL);
1995     }
1996   else if (place != NULL)
1997     f = place;
1998   else if (mips_opts.mips16
1999            && ! ip->use_extend
2000            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2001     {
2002       /* Make sure there is enough room to swap this instruction with
2003          a following jump instruction.  */
2004       frag_grow (6);
2005       f = frag_more (2);
2006     }
2007   else
2008     {
2009       if (mips_opts.mips16
2010           && mips_opts.noreorder
2011           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2012         as_warn (_("extended instruction in delay slot"));
2013
2014       f = frag_more (4);
2015     }
2016
2017   fixp[0] = fixp[1] = fixp[2] = NULL;
2018   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2019     {
2020       if (address_expr->X_op == O_constant)
2021         {
2022           unsigned long tmp;
2023
2024           switch (*reloc_type)
2025             {
2026             case BFD_RELOC_32:
2027               ip->insn_opcode |= address_expr->X_add_number;
2028               break;
2029
2030             case BFD_RELOC_MIPS_HIGHEST:
2031               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2032               tmp >>= 16;
2033               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2034               break;
2035
2036             case BFD_RELOC_MIPS_HIGHER:
2037               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2038               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2039               break;
2040
2041             case BFD_RELOC_HI16_S:
2042               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2043                                   >> 16) & 0xffff;
2044               break;
2045
2046             case BFD_RELOC_HI16:
2047               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2048               break;
2049
2050             case BFD_RELOC_LO16:
2051               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2052               break;
2053
2054             case BFD_RELOC_MIPS_JMP:
2055               if ((address_expr->X_add_number & 3) != 0)
2056                 as_bad (_("jump to misaligned address (0x%lx)"),
2057                         (unsigned long) address_expr->X_add_number);
2058               if (address_expr->X_add_number & ~0xfffffff
2059                   || address_expr->X_add_number > 0x7fffffc)
2060                 as_bad (_("jump address range overflow (0x%lx)"),
2061                         (unsigned long) address_expr->X_add_number);
2062               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2063               break;
2064
2065             case BFD_RELOC_MIPS16_JMP:
2066               if ((address_expr->X_add_number & 3) != 0)
2067                 as_bad (_("jump to misaligned address (0x%lx)"),
2068                         (unsigned long) address_expr->X_add_number);
2069               if (address_expr->X_add_number & ~0xfffffff
2070                   || address_expr->X_add_number > 0x7fffffc)
2071                 as_bad (_("jump address range overflow (0x%lx)"),
2072                         (unsigned long) address_expr->X_add_number);
2073               ip->insn_opcode |=
2074                 (((address_expr->X_add_number & 0x7c0000) << 3)
2075                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2076                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2077               break;
2078
2079             case BFD_RELOC_16_PCREL:
2080               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2081               break;
2082
2083             case BFD_RELOC_16_PCREL_S2:
2084               goto need_reloc;
2085
2086             default:
2087               internalError ();
2088             }
2089         }
2090       else
2091         {
2092         need_reloc:
2093           /* Don't generate a reloc if we are writing into a variant frag.  */
2094           if (place == NULL)
2095             {
2096               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2097                                      address_expr,
2098                                      (*reloc_type == BFD_RELOC_16_PCREL
2099                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
2100                                      reloc_type[0]);
2101
2102               /* These relocations can have an addend that won't fit in
2103                  4 octets for 64bit assembly.  */
2104               if (HAVE_64BIT_GPRS &&
2105                   (*reloc_type == BFD_RELOC_16
2106                   || *reloc_type == BFD_RELOC_32
2107                   || *reloc_type == BFD_RELOC_MIPS_JMP
2108                   || *reloc_type == BFD_RELOC_HI16_S
2109                   || *reloc_type == BFD_RELOC_LO16
2110                   || *reloc_type == BFD_RELOC_GPREL16
2111                   || *reloc_type == BFD_RELOC_MIPS_LITERAL
2112                   || *reloc_type == BFD_RELOC_GPREL32
2113                   || *reloc_type == BFD_RELOC_64
2114                   || *reloc_type == BFD_RELOC_CTOR
2115                   || *reloc_type == BFD_RELOC_MIPS_SUB
2116                   || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2117                   || *reloc_type == BFD_RELOC_MIPS_HIGHER
2118                   || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2119                   || *reloc_type == BFD_RELOC_MIPS_REL16
2120                   || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2121                 fixp[0]->fx_no_overflow = 1;
2122
2123               if (unmatched_hi)
2124                 {
2125                   struct mips_hi_fixup *hi_fixup;
2126
2127                   assert (*reloc_type == BFD_RELOC_HI16_S);
2128                   hi_fixup = ((struct mips_hi_fixup *)
2129                               xmalloc (sizeof (struct mips_hi_fixup)));
2130                   hi_fixup->fixp = fixp[0];
2131                   hi_fixup->seg = now_seg;
2132                   hi_fixup->next = mips_hi_fixup_list;
2133                   mips_hi_fixup_list = hi_fixup;
2134                 }
2135
2136               if (reloc_type[1] != BFD_RELOC_UNUSED)
2137                 {
2138                   /* FIXME: This symbol can be one of
2139                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2140                   address_expr->X_op = O_absent;
2141                   address_expr->X_add_symbol = 0;
2142                   address_expr->X_add_number = 0;
2143
2144                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2145                                          4, address_expr, false,
2146                                          reloc_type[1]);
2147
2148                   /* These relocations can have an addend that won't fit in
2149                      4 octets for 64bit assembly.  */
2150                   if (HAVE_64BIT_GPRS &&
2151                       (*reloc_type == BFD_RELOC_16
2152                        || *reloc_type == BFD_RELOC_32
2153                        || *reloc_type == BFD_RELOC_MIPS_JMP
2154                        || *reloc_type == BFD_RELOC_HI16_S
2155                        || *reloc_type == BFD_RELOC_LO16
2156                        || *reloc_type == BFD_RELOC_GPREL16
2157                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2158                        || *reloc_type == BFD_RELOC_GPREL32
2159                        || *reloc_type == BFD_RELOC_64
2160                        || *reloc_type == BFD_RELOC_CTOR
2161                        || *reloc_type == BFD_RELOC_MIPS_SUB
2162                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2163                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2164                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2165                        || *reloc_type == BFD_RELOC_MIPS_REL16
2166                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2167                      fixp[1]->fx_no_overflow = 1;
2168
2169                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2170                     {
2171                       address_expr->X_op = O_absent;
2172                       address_expr->X_add_symbol = 0;
2173                       address_expr->X_add_number = 0;
2174
2175                       fixp[2] = fix_new_exp (frag_now,
2176                                              f - frag_now->fr_literal, 4,
2177                                              address_expr, false,
2178                                              reloc_type[2]);
2179
2180                       /* These relocations can have an addend that won't fit in
2181                          4 octets for 64bit assembly.  */
2182                       if (HAVE_64BIT_GPRS &&
2183                           (*reloc_type == BFD_RELOC_16
2184                            || *reloc_type == BFD_RELOC_32
2185                            || *reloc_type == BFD_RELOC_MIPS_JMP
2186                            || *reloc_type == BFD_RELOC_HI16_S
2187                            || *reloc_type == BFD_RELOC_LO16
2188                            || *reloc_type == BFD_RELOC_GPREL16
2189                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2190                            || *reloc_type == BFD_RELOC_GPREL32
2191                            || *reloc_type == BFD_RELOC_64
2192                            || *reloc_type == BFD_RELOC_CTOR
2193                            || *reloc_type == BFD_RELOC_MIPS_SUB
2194                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2195                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2196                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2197                            || *reloc_type == BFD_RELOC_MIPS_REL16
2198                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2199                        fixp[2]->fx_no_overflow = 1;
2200                     }
2201                 }
2202             }
2203         }
2204     }
2205
2206   if (! mips_opts.mips16)
2207     md_number_to_chars (f, ip->insn_opcode, 4);
2208   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2209     {
2210       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2211       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2212     }
2213   else
2214     {
2215       if (ip->use_extend)
2216         {
2217           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2218           f += 2;
2219         }
2220       md_number_to_chars (f, ip->insn_opcode, 2);
2221     }
2222
2223   /* Update the register mask information.  */
2224   if (! mips_opts.mips16)
2225     {
2226       if (pinfo & INSN_WRITE_GPR_D)
2227         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2228       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2229         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2230       if (pinfo & INSN_READ_GPR_S)
2231         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2232       if (pinfo & INSN_WRITE_GPR_31)
2233         mips_gprmask |= 1 << 31;
2234       if (pinfo & INSN_WRITE_FPR_D)
2235         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2236       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2237         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2238       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2239         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2240       if ((pinfo & INSN_READ_FPR_R) != 0)
2241         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2242       if (pinfo & INSN_COP)
2243         {
2244           /* We don't keep enough information to sort these cases out.
2245              The itbl support does keep this information however, although
2246              we currently don't support itbl fprmats as part of the cop
2247              instruction.  May want to add this support in the future.  */
2248         }
2249       /* Never set the bit for $0, which is always zero.  */
2250       mips_gprmask &= ~1 << 0;
2251     }
2252   else
2253     {
2254       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2255         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2256                               & MIPS16OP_MASK_RX);
2257       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2258         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2259                               & MIPS16OP_MASK_RY);
2260       if (pinfo & MIPS16_INSN_WRITE_Z)
2261         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2262                               & MIPS16OP_MASK_RZ);
2263       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2264         mips_gprmask |= 1 << TREG;
2265       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2266         mips_gprmask |= 1 << SP;
2267       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2268         mips_gprmask |= 1 << RA;
2269       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2270         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2271       if (pinfo & MIPS16_INSN_READ_Z)
2272         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2273                               & MIPS16OP_MASK_MOVE32Z);
2274       if (pinfo & MIPS16_INSN_READ_GPR_X)
2275         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2276                               & MIPS16OP_MASK_REGR32);
2277     }
2278
2279   if (place == NULL && ! mips_opts.noreorder)
2280     {
2281       /* Filling the branch delay slot is more complex.  We try to
2282          switch the branch with the previous instruction, which we can
2283          do if the previous instruction does not set up a condition
2284          that the branch tests and if the branch is not itself the
2285          target of any branch.  */
2286       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2287           || (pinfo & INSN_COND_BRANCH_DELAY))
2288         {
2289           if (mips_optimize < 2
2290               /* If we have seen .set volatile or .set nomove, don't
2291                  optimize.  */
2292               || mips_opts.nomove != 0
2293               /* If we had to emit any NOP instructions, then we
2294                  already know we can not swap.  */
2295               || nops != 0
2296               /* If we don't even know the previous insn, we can not
2297                  swap.  */
2298               || ! prev_insn_valid
2299               /* If the previous insn is already in a branch delay
2300                  slot, then we can not swap.  */
2301               || prev_insn_is_delay_slot
2302               /* If the previous previous insn was in a .set
2303                  noreorder, we can't swap.  Actually, the MIPS
2304                  assembler will swap in this situation.  However, gcc
2305                  configured -with-gnu-as will generate code like
2306                    .set noreorder
2307                    lw   $4,XXX
2308                    .set reorder
2309                    INSN
2310                    bne  $4,$0,foo
2311                  in which we can not swap the bne and INSN.  If gcc is
2312                  not configured -with-gnu-as, it does not output the
2313                  .set pseudo-ops.  We don't have to check
2314                  prev_insn_unreordered, because prev_insn_valid will
2315                  be 0 in that case.  We don't want to use
2316                  prev_prev_insn_valid, because we do want to be able
2317                  to swap at the start of a function.  */
2318               || prev_prev_insn_unreordered
2319               /* If the branch is itself the target of a branch, we
2320                  can not swap.  We cheat on this; all we check for is
2321                  whether there is a label on this instruction.  If
2322                  there are any branches to anything other than a
2323                  label, users must use .set noreorder.  */
2324               || insn_labels != NULL
2325               /* If the previous instruction is in a variant frag, we
2326                  can not do the swap.  This does not apply to the
2327                  mips16, which uses variant frags for different
2328                  purposes.  */
2329               || (! mips_opts.mips16
2330                   && prev_insn_frag->fr_type == rs_machine_dependent)
2331               /* If the branch reads the condition codes, we don't
2332                  even try to swap, because in the sequence
2333                    ctc1 $X,$31
2334                    INSN
2335                    INSN
2336                    bc1t LABEL
2337                  we can not swap, and I don't feel like handling that
2338                  case.  */
2339               || (! mips_opts.mips16
2340                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2341                   && (pinfo & INSN_READ_COND_CODE))
2342               /* We can not swap with an instruction that requires a
2343                  delay slot, becase the target of the branch might
2344                  interfere with that instruction.  */
2345               || (! mips_opts.mips16
2346                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2347                   && (prev_pinfo
2348               /* Itbl support may require additional care here.  */
2349                       & (INSN_LOAD_COPROC_DELAY
2350                          | INSN_COPROC_MOVE_DELAY
2351                          | INSN_WRITE_COND_CODE)))
2352               || (! (hilo_interlocks
2353                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2354                   && (prev_pinfo
2355                       & (INSN_READ_LO
2356                          | INSN_READ_HI)))
2357               || (! mips_opts.mips16
2358                   && ! gpr_interlocks
2359                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2360               || (! mips_opts.mips16
2361                   && mips_opts.isa == ISA_MIPS1
2362                   /* Itbl support may require additional care here.  */
2363                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2364               /* We can not swap with a branch instruction.  */
2365               || (prev_pinfo
2366                   & (INSN_UNCOND_BRANCH_DELAY
2367                      | INSN_COND_BRANCH_DELAY
2368                      | INSN_COND_BRANCH_LIKELY))
2369               /* We do not swap with a trap instruction, since it
2370                  complicates trap handlers to have the trap
2371                  instruction be in a delay slot.  */
2372               || (prev_pinfo & INSN_TRAP)
2373               /* If the branch reads a register that the previous
2374                  instruction sets, we can not swap.  */
2375               || (! mips_opts.mips16
2376                   && (prev_pinfo & INSN_WRITE_GPR_T)
2377                   && insn_uses_reg (ip,
2378                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2379                                      & OP_MASK_RT),
2380                                     MIPS_GR_REG))
2381               || (! mips_opts.mips16
2382                   && (prev_pinfo & INSN_WRITE_GPR_D)
2383                   && insn_uses_reg (ip,
2384                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2385                                      & OP_MASK_RD),
2386                                     MIPS_GR_REG))
2387               || (mips_opts.mips16
2388                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2389                        && insn_uses_reg (ip,
2390                                          ((prev_insn.insn_opcode
2391                                            >> MIPS16OP_SH_RX)
2392                                           & MIPS16OP_MASK_RX),
2393                                          MIPS16_REG))
2394                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2395                           && insn_uses_reg (ip,
2396                                             ((prev_insn.insn_opcode
2397                                               >> MIPS16OP_SH_RY)
2398                                              & MIPS16OP_MASK_RY),
2399                                             MIPS16_REG))
2400                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2401                           && insn_uses_reg (ip,
2402                                             ((prev_insn.insn_opcode
2403                                               >> MIPS16OP_SH_RZ)
2404                                              & MIPS16OP_MASK_RZ),
2405                                             MIPS16_REG))
2406                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2407                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2408                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2409                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2410                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2411                           && insn_uses_reg (ip,
2412                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2413                                                                      insn_opcode),
2414                                             MIPS_GR_REG))))
2415               /* If the branch writes a register that the previous
2416                  instruction sets, we can not swap (we know that
2417                  branches write only to RD or to $31).  */
2418               || (! mips_opts.mips16
2419                   && (prev_pinfo & INSN_WRITE_GPR_T)
2420                   && (((pinfo & INSN_WRITE_GPR_D)
2421                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2422                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2423                       || ((pinfo & INSN_WRITE_GPR_31)
2424                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2425                                & OP_MASK_RT)
2426                               == 31))))
2427               || (! mips_opts.mips16
2428                   && (prev_pinfo & INSN_WRITE_GPR_D)
2429                   && (((pinfo & INSN_WRITE_GPR_D)
2430                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2431                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2432                       || ((pinfo & INSN_WRITE_GPR_31)
2433                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2434                                & OP_MASK_RD)
2435                               == 31))))
2436               || (mips_opts.mips16
2437                   && (pinfo & MIPS16_INSN_WRITE_31)
2438                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2439                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2440                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2441                               == RA))))
2442               /* If the branch writes a register that the previous
2443                  instruction reads, we can not swap (we know that
2444                  branches only write to RD or to $31).  */
2445               || (! mips_opts.mips16
2446                   && (pinfo & INSN_WRITE_GPR_D)
2447                   && insn_uses_reg (&prev_insn,
2448                                     ((ip->insn_opcode >> OP_SH_RD)
2449                                      & OP_MASK_RD),
2450                                     MIPS_GR_REG))
2451               || (! mips_opts.mips16
2452                   && (pinfo & INSN_WRITE_GPR_31)
2453                   && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2454               || (mips_opts.mips16
2455                   && (pinfo & MIPS16_INSN_WRITE_31)
2456                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2457               /* If we are generating embedded PIC code, the branch
2458                  might be expanded into a sequence which uses $at, so
2459                  we can't swap with an instruction which reads it.  */
2460               || (mips_pic == EMBEDDED_PIC
2461                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2462               /* If the previous previous instruction has a load
2463                  delay, and sets a register that the branch reads, we
2464                  can not swap.  */
2465               || (! mips_opts.mips16
2466                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2467               /* Itbl support may require additional care here.  */
2468                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2469                       || (! gpr_interlocks
2470                           && (prev_prev_insn.insn_mo->pinfo
2471                               & INSN_LOAD_MEMORY_DELAY)))
2472                   && insn_uses_reg (ip,
2473                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2474                                      & OP_MASK_RT),
2475                                     MIPS_GR_REG))
2476               /* If one instruction sets a condition code and the
2477                  other one uses a condition code, we can not swap.  */
2478               || ((pinfo & INSN_READ_COND_CODE)
2479                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2480               || ((pinfo & INSN_WRITE_COND_CODE)
2481                   && (prev_pinfo & INSN_READ_COND_CODE))
2482               /* If the previous instruction uses the PC, we can not
2483                  swap.  */
2484               || (mips_opts.mips16
2485                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2486               /* If the previous instruction was extended, we can not
2487                  swap.  */
2488               || (mips_opts.mips16 && prev_insn_extended)
2489               /* If the previous instruction had a fixup in mips16
2490                  mode, we can not swap.  This normally means that the
2491                  previous instruction was a 4 byte branch anyhow.  */
2492               || (mips_opts.mips16 && prev_insn_fixp[0])
2493               /* If the previous instruction is a sync, sync.l, or
2494                  sync.p, we can not swap.  */
2495               || (prev_pinfo & INSN_SYNC))
2496             {
2497               /* We could do even better for unconditional branches to
2498                  portions of this object file; we could pick up the
2499                  instruction at the destination, put it in the delay
2500                  slot, and bump the destination address.  */
2501               emit_nop ();
2502               /* Update the previous insn information.  */
2503               prev_prev_insn = *ip;
2504               prev_insn.insn_mo = &dummy_opcode;
2505             }
2506           else
2507             {
2508               /* It looks like we can actually do the swap.  */
2509               if (! mips_opts.mips16)
2510                 {
2511                   char *prev_f;
2512                   char temp[4];
2513
2514                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2515                   memcpy (temp, prev_f, 4);
2516                   memcpy (prev_f, f, 4);
2517                   memcpy (f, temp, 4);
2518                   if (prev_insn_fixp[0])
2519                     {
2520                       prev_insn_fixp[0]->fx_frag = frag_now;
2521                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2522                     }
2523                   if (prev_insn_fixp[1])
2524                     {
2525                       prev_insn_fixp[1]->fx_frag = frag_now;
2526                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2527                     }
2528                   if (prev_insn_fixp[2])
2529                     {
2530                       prev_insn_fixp[2]->fx_frag = frag_now;
2531                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2532                     }
2533                   if (fixp[0])
2534                     {
2535                       fixp[0]->fx_frag = prev_insn_frag;
2536                       fixp[0]->fx_where = prev_insn_where;
2537                     }
2538                   if (fixp[1])
2539                     {
2540                       fixp[1]->fx_frag = prev_insn_frag;
2541                       fixp[1]->fx_where = prev_insn_where;
2542                     }
2543                   if (fixp[2])
2544                     {
2545                       fixp[2]->fx_frag = prev_insn_frag;
2546                       fixp[2]->fx_where = prev_insn_where;
2547                     }
2548                 }
2549               else
2550                 {
2551                   char *prev_f;
2552                   char temp[2];
2553
2554                   assert (prev_insn_fixp[0] == NULL);
2555                   assert (prev_insn_fixp[1] == NULL);
2556                   assert (prev_insn_fixp[2] == NULL);
2557                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2558                   memcpy (temp, prev_f, 2);
2559                   memcpy (prev_f, f, 2);
2560                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2561                     {
2562                       assert (*reloc_type == BFD_RELOC_UNUSED);
2563                       memcpy (f, temp, 2);
2564                     }
2565                   else
2566                     {
2567                       memcpy (f, f + 2, 2);
2568                       memcpy (f + 2, temp, 2);
2569                     }
2570                   if (fixp[0])
2571                     {
2572                       fixp[0]->fx_frag = prev_insn_frag;
2573                       fixp[0]->fx_where = prev_insn_where;
2574                     }
2575                   if (fixp[1])
2576                     {
2577                       fixp[1]->fx_frag = prev_insn_frag;
2578                       fixp[1]->fx_where = prev_insn_where;
2579                     }
2580                   if (fixp[2])
2581                     {
2582                       fixp[2]->fx_frag = prev_insn_frag;
2583                       fixp[2]->fx_where = prev_insn_where;
2584                     }
2585                 }
2586
2587               /* Update the previous insn information; leave prev_insn
2588                  unchanged.  */
2589               prev_prev_insn = *ip;
2590             }
2591           prev_insn_is_delay_slot = 1;
2592
2593           /* If that was an unconditional branch, forget the previous
2594              insn information.  */
2595           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2596             {
2597               prev_prev_insn.insn_mo = &dummy_opcode;
2598               prev_insn.insn_mo = &dummy_opcode;
2599             }
2600
2601           prev_insn_fixp[0] = NULL;
2602           prev_insn_fixp[1] = NULL;
2603           prev_insn_fixp[2] = NULL;
2604           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2605           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2606           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2607           prev_insn_extended = 0;
2608         }
2609       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2610         {
2611           /* We don't yet optimize a branch likely.  What we should do
2612              is look at the target, copy the instruction found there
2613              into the delay slot, and increment the branch to jump to
2614              the next instruction.  */
2615           emit_nop ();
2616           /* Update the previous insn information.  */
2617           prev_prev_insn = *ip;
2618           prev_insn.insn_mo = &dummy_opcode;
2619           prev_insn_fixp[0] = NULL;
2620           prev_insn_fixp[1] = NULL;
2621           prev_insn_fixp[2] = NULL;
2622           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2623           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2624           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2625           prev_insn_extended = 0;
2626         }
2627       else
2628         {
2629           /* Update the previous insn information.  */
2630           if (nops > 0)
2631             prev_prev_insn.insn_mo = &dummy_opcode;
2632           else
2633             prev_prev_insn = prev_insn;
2634           prev_insn = *ip;
2635
2636           /* Any time we see a branch, we always fill the delay slot
2637              immediately; since this insn is not a branch, we know it
2638              is not in a delay slot.  */
2639           prev_insn_is_delay_slot = 0;
2640
2641           prev_insn_fixp[0] = fixp[0];
2642           prev_insn_fixp[1] = fixp[1];
2643           prev_insn_fixp[2] = fixp[2];
2644           prev_insn_reloc_type[0] = reloc_type[0];
2645           prev_insn_reloc_type[1] = reloc_type[1];
2646           prev_insn_reloc_type[2] = reloc_type[2];
2647           if (mips_opts.mips16)
2648             prev_insn_extended = (ip->use_extend
2649                                   || *reloc_type > BFD_RELOC_UNUSED);
2650         }
2651
2652       prev_prev_insn_unreordered = prev_insn_unreordered;
2653       prev_insn_unreordered = 0;
2654       prev_insn_frag = frag_now;
2655       prev_insn_where = f - frag_now->fr_literal;
2656       prev_insn_valid = 1;
2657     }
2658   else if (place == NULL)
2659     {
2660       /* We need to record a bit of information even when we are not
2661          reordering, in order to determine the base address for mips16
2662          PC relative relocs.  */
2663       prev_prev_insn = prev_insn;
2664       prev_insn = *ip;
2665       prev_insn_reloc_type[0] = reloc_type[0];
2666       prev_insn_reloc_type[1] = reloc_type[1];
2667       prev_insn_reloc_type[2] = reloc_type[2];
2668       prev_prev_insn_unreordered = prev_insn_unreordered;
2669       prev_insn_unreordered = 1;
2670     }
2671
2672   /* We just output an insn, so the next one doesn't have a label.  */
2673   mips_clear_insn_labels ();
2674
2675   /* We must ensure that a fixup associated with an unmatched %hi
2676      reloc does not become a variant frag.  Otherwise, the
2677      rearrangement of %hi relocs in frob_file may confuse
2678      tc_gen_reloc.  */
2679   if (unmatched_hi)
2680     {
2681       frag_wane (frag_now);
2682       frag_new (0);
2683     }
2684 }
2685
2686 /* This function forgets that there was any previous instruction or
2687    label.  If PRESERVE is non-zero, it remembers enough information to
2688    know whether nops are needed before a noreorder section.  */
2689
2690 static void
2691 mips_no_prev_insn (preserve)
2692      int preserve;
2693 {
2694   if (! preserve)
2695     {
2696       prev_insn.insn_mo = &dummy_opcode;
2697       prev_prev_insn.insn_mo = &dummy_opcode;
2698       prev_nop_frag = NULL;
2699       prev_nop_frag_holds = 0;
2700       prev_nop_frag_required = 0;
2701       prev_nop_frag_since = 0;
2702     }
2703   prev_insn_valid = 0;
2704   prev_insn_is_delay_slot = 0;
2705   prev_insn_unreordered = 0;
2706   prev_insn_extended = 0;
2707   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2708   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2709   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2710   prev_prev_insn_unreordered = 0;
2711   mips_clear_insn_labels ();
2712 }
2713
2714 /* This function must be called whenever we turn on noreorder or emit
2715    something other than instructions.  It inserts any NOPS which might
2716    be needed by the previous instruction, and clears the information
2717    kept for the previous instructions.  The INSNS parameter is true if
2718    instructions are to follow.  */
2719
2720 static void
2721 mips_emit_delays (insns)
2722      boolean insns;
2723 {
2724   if (! mips_opts.noreorder)
2725     {
2726       int nops;
2727
2728       nops = 0;
2729       if ((! mips_opts.mips16
2730            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2731            && (! cop_interlocks
2732                && (prev_insn.insn_mo->pinfo
2733                    & (INSN_LOAD_COPROC_DELAY
2734                       | INSN_COPROC_MOVE_DELAY
2735                       | INSN_WRITE_COND_CODE))))
2736           || (! hilo_interlocks
2737               && (prev_insn.insn_mo->pinfo
2738                   & (INSN_READ_LO
2739                      | INSN_READ_HI)))
2740           || (! mips_opts.mips16
2741               && ! gpr_interlocks
2742               && (prev_insn.insn_mo->pinfo
2743                   & INSN_LOAD_MEMORY_DELAY))
2744           || (! mips_opts.mips16
2745               && mips_opts.isa == ISA_MIPS1
2746               && (prev_insn.insn_mo->pinfo
2747                   & INSN_COPROC_MEMORY_DELAY)))
2748         {
2749           /* Itbl support may require additional care here.  */
2750           ++nops;
2751           if ((! mips_opts.mips16
2752                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2753                && (! cop_interlocks
2754                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2755               || (! hilo_interlocks
2756                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2757                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2758             ++nops;
2759
2760           if (prev_insn_unreordered)
2761             nops = 0;
2762         }
2763       else if ((! mips_opts.mips16
2764                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2765                 && (! cop_interlocks
2766                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2767                || (! hilo_interlocks
2768                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2769                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2770         {
2771           /* Itbl support may require additional care here.  */
2772           if (! prev_prev_insn_unreordered)
2773             ++nops;
2774         }
2775
2776       if (nops > 0)
2777         {
2778           struct insn_label_list *l;
2779
2780           if (insns)
2781             {
2782               /* Record the frag which holds the nop instructions, so
2783                  that we can remove them if we don't need them.  */
2784               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2785               prev_nop_frag = frag_now;
2786               prev_nop_frag_holds = nops;
2787               prev_nop_frag_required = 0;
2788               prev_nop_frag_since = 0;
2789             }
2790
2791           for (; nops > 0; --nops)
2792             emit_nop ();
2793
2794           if (insns)
2795             {
2796               /* Move on to a new frag, so that it is safe to simply
2797                  decrease the size of prev_nop_frag.  */
2798               frag_wane (frag_now);
2799               frag_new (0);
2800             }
2801
2802           for (l = insn_labels; l != NULL; l = l->next)
2803             {
2804               valueT val;
2805
2806               assert (S_GET_SEGMENT (l->label) == now_seg);
2807               symbol_set_frag (l->label, frag_now);
2808               val = (valueT) frag_now_fix ();
2809               /* mips16 text labels are stored as odd.  */
2810               if (mips_opts.mips16)
2811                 val += 1;
2812               S_SET_VALUE (l->label, val);
2813             }
2814         }
2815     }
2816
2817   /* Mark instruction labels in mips16 mode.  */
2818   if (mips_opts.mips16 && insns)
2819     mips16_mark_labels ();
2820
2821   mips_no_prev_insn (insns);
2822 }
2823
2824 /* Build an instruction created by a macro expansion.  This is passed
2825    a pointer to the count of instructions created so far, an
2826    expression, the name of the instruction to build, an operand format
2827    string, and corresponding arguments.  */
2828
2829 #ifdef USE_STDARG
2830 static void
2831 macro_build (char *place,
2832              int *counter,
2833              expressionS * ep,
2834              const char *name,
2835              const char *fmt,
2836              ...)
2837 #else
2838 static void
2839 macro_build (place, counter, ep, name, fmt, va_alist)
2840      char *place;
2841      int *counter;
2842      expressionS *ep;
2843      const char *name;
2844      const char *fmt;
2845      va_dcl
2846 #endif
2847 {
2848   struct mips_cl_insn insn;
2849   bfd_reloc_code_real_type r[3];
2850   va_list args;
2851
2852 #ifdef USE_STDARG
2853   va_start (args, fmt);
2854 #else
2855   va_start (args);
2856 #endif
2857
2858   /*
2859    * If the macro is about to expand into a second instruction,
2860    * print a warning if needed. We need to pass ip as a parameter
2861    * to generate a better warning message here...
2862    */
2863   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2864     as_warn (_("Macro instruction expanded into multiple instructions"));
2865
2866   if (place == NULL)
2867     *counter += 1;              /* bump instruction counter */
2868
2869   if (mips_opts.mips16)
2870     {
2871       mips16_macro_build (place, counter, ep, name, fmt, args);
2872       va_end (args);
2873       return;
2874     }
2875
2876   r[0] = BFD_RELOC_UNUSED;
2877   r[1] = BFD_RELOC_UNUSED;
2878   r[2] = BFD_RELOC_UNUSED;
2879   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2880   assert (insn.insn_mo);
2881   assert (strcmp (name, insn.insn_mo->name) == 0);
2882
2883   /* Search until we get a match for NAME.  */
2884   while (1)
2885     {
2886       if (strcmp (fmt, insn.insn_mo->args) == 0
2887           && insn.insn_mo->pinfo != INSN_MACRO
2888           && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2889           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2890         break;
2891
2892       ++insn.insn_mo;
2893       assert (insn.insn_mo->name);
2894       assert (strcmp (name, insn.insn_mo->name) == 0);
2895     }
2896
2897   insn.insn_opcode = insn.insn_mo->match;
2898   for (;;)
2899     {
2900       switch (*fmt++)
2901         {
2902         case '\0':
2903           break;
2904
2905         case ',':
2906         case '(':
2907         case ')':
2908           continue;
2909
2910         case 't':
2911         case 'w':
2912         case 'E':
2913           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2914           continue;
2915
2916         case 'c':
2917           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2918           continue;
2919
2920         case 'T':
2921         case 'W':
2922           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2923           continue;
2924
2925         case 'd':
2926         case 'G':
2927           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2928           continue;
2929
2930         case 'U':
2931           {
2932             int tmp = va_arg (args, int);
2933
2934             insn.insn_opcode |= tmp << OP_SH_RT;
2935             insn.insn_opcode |= tmp << OP_SH_RD;
2936             continue;
2937           }
2938
2939         case 'V':
2940         case 'S':
2941           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2942           continue;
2943
2944         case 'z':
2945           continue;
2946
2947         case '<':
2948           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2949           continue;
2950
2951         case 'D':
2952           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2953           continue;
2954
2955         case 'B':
2956           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2957           continue;
2958
2959         case 'J':
2960           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2961           continue;
2962
2963         case 'q':
2964           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2965           continue;
2966
2967         case 'b':
2968         case 's':
2969         case 'r':
2970         case 'v':
2971           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2972           continue;
2973
2974         case 'i':
2975         case 'j':
2976         case 'o':
2977           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2978           assert (*r == BFD_RELOC_GPREL16
2979                   || *r == BFD_RELOC_MIPS_LITERAL
2980                   || *r == BFD_RELOC_MIPS_HIGHER
2981                   || *r == BFD_RELOC_HI16_S
2982                   || *r == BFD_RELOC_LO16
2983                   || *r == BFD_RELOC_MIPS_GOT16
2984                   || *r == BFD_RELOC_MIPS_CALL16
2985                   || *r == BFD_RELOC_MIPS_GOT_LO16
2986                   || *r == BFD_RELOC_MIPS_CALL_LO16
2987                   || (ep->X_op == O_subtract
2988                       && *r == BFD_RELOC_PCREL_LO16));
2989           continue;
2990
2991         case 'u':
2992           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2993           assert (ep != NULL
2994                   && (ep->X_op == O_constant
2995                       || (ep->X_op == O_symbol
2996                           && (*r == BFD_RELOC_MIPS_HIGHEST
2997                               || *r == BFD_RELOC_HI16_S
2998                               || *r == BFD_RELOC_HI16
2999                               || *r == BFD_RELOC_GPREL16
3000                               || *r == BFD_RELOC_MIPS_GOT_HI16
3001                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3002                       || (ep->X_op == O_subtract
3003                           && *r == BFD_RELOC_PCREL_HI16_S)));
3004           continue;
3005
3006         case 'p':
3007           assert (ep != NULL);
3008           /*
3009            * This allows macro() to pass an immediate expression for
3010            * creating short branches without creating a symbol.
3011            * Note that the expression still might come from the assembly
3012            * input, in which case the value is not checked for range nor
3013            * is a relocation entry generated (yuck).
3014            */
3015           if (ep->X_op == O_constant)
3016             {
3017               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3018               ep = NULL;
3019             }
3020           else
3021             if (mips_pic == EMBEDDED_PIC)
3022               *r = BFD_RELOC_16_PCREL_S2;
3023             else
3024               *r = BFD_RELOC_16_PCREL;
3025           continue;
3026
3027         case 'a':
3028           assert (ep != NULL);
3029           *r = BFD_RELOC_MIPS_JMP;
3030           continue;
3031
3032         case 'C':
3033           insn.insn_opcode |= va_arg (args, unsigned long);
3034           continue;
3035
3036         default:
3037           internalError ();
3038         }
3039       break;
3040     }
3041   va_end (args);
3042   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3043
3044   append_insn (place, &insn, ep, r, false);
3045 }
3046
3047 static void
3048 mips16_macro_build (place, counter, ep, name, fmt, args)
3049      char *place;
3050      int *counter ATTRIBUTE_UNUSED;
3051      expressionS *ep;
3052      const char *name;
3053      const char *fmt;
3054      va_list args;
3055 {
3056   struct mips_cl_insn insn;
3057   bfd_reloc_code_real_type r[3]
3058     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3059
3060   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3061   assert (insn.insn_mo);
3062   assert (strcmp (name, insn.insn_mo->name) == 0);
3063
3064   while (strcmp (fmt, insn.insn_mo->args) != 0
3065          || insn.insn_mo->pinfo == INSN_MACRO)
3066     {
3067       ++insn.insn_mo;
3068       assert (insn.insn_mo->name);
3069       assert (strcmp (name, insn.insn_mo->name) == 0);
3070     }
3071
3072   insn.insn_opcode = insn.insn_mo->match;
3073   insn.use_extend = false;
3074
3075   for (;;)
3076     {
3077       int c;
3078
3079       c = *fmt++;
3080       switch (c)
3081         {
3082         case '\0':
3083           break;
3084
3085         case ',':
3086         case '(':
3087         case ')':
3088           continue;
3089
3090         case 'y':
3091         case 'w':
3092           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3093           continue;
3094
3095         case 'x':
3096         case 'v':
3097           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3098           continue;
3099
3100         case 'z':
3101           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3102           continue;
3103
3104         case 'Z':
3105           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3106           continue;
3107
3108         case '0':
3109         case 'S':
3110         case 'P':
3111         case 'R':
3112           continue;
3113
3114         case 'X':
3115           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3116           continue;
3117
3118         case 'Y':
3119           {
3120             int regno;
3121
3122             regno = va_arg (args, int);
3123             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3124             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3125           }
3126           continue;
3127
3128         case '<':
3129         case '>':
3130         case '4':
3131         case '5':
3132         case 'H':
3133         case 'W':
3134         case 'D':
3135         case 'j':
3136         case '8':
3137         case 'V':
3138         case 'C':
3139         case 'U':
3140         case 'k':
3141         case 'K':
3142         case 'p':
3143         case 'q':
3144           {
3145             assert (ep != NULL);
3146
3147             if (ep->X_op != O_constant)
3148               *r = BFD_RELOC_UNUSED + c;
3149             else
3150               {
3151                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3152                               false, &insn.insn_opcode, &insn.use_extend,
3153                               &insn.extend);
3154                 ep = NULL;
3155                 *r = BFD_RELOC_UNUSED;
3156               }
3157           }
3158           continue;
3159
3160         case '6':
3161           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3162           continue;
3163         }
3164
3165       break;
3166     }
3167
3168   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3169
3170   append_insn (place, &insn, ep, r, false);
3171 }
3172
3173 /*
3174  * Generate a "lui" instruction.
3175  */
3176 static void
3177 macro_build_lui (place, counter, ep, regnum)
3178      char *place;
3179      int *counter;
3180      expressionS *ep;
3181      int regnum;
3182 {
3183   expressionS high_expr;
3184   struct mips_cl_insn insn;
3185   bfd_reloc_code_real_type r[3]
3186     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3187   CONST char *name = "lui";
3188   CONST char *fmt = "t,u";
3189
3190   assert (! mips_opts.mips16);
3191
3192   if (place == NULL)
3193     high_expr = *ep;
3194   else
3195     {
3196       high_expr.X_op = O_constant;
3197       high_expr.X_add_number = ep->X_add_number;
3198     }
3199
3200   if (high_expr.X_op == O_constant)
3201     {
3202       /* we can compute the instruction now without a relocation entry */
3203       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3204                                 >> 16) & 0xffff;
3205       *r = BFD_RELOC_UNUSED;
3206     }
3207   else if (! HAVE_NEWABI)
3208     {
3209       assert (ep->X_op == O_symbol);
3210       /* _gp_disp is a special case, used from s_cpload.  */
3211       assert (mips_pic == NO_PIC
3212               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3213       *r = BFD_RELOC_HI16_S;
3214     }
3215
3216   /*
3217    * If the macro is about to expand into a second instruction,
3218    * print a warning if needed. We need to pass ip as a parameter
3219    * to generate a better warning message here...
3220    */
3221   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3222     as_warn (_("Macro instruction expanded into multiple instructions"));
3223
3224   if (place == NULL)
3225     *counter += 1;              /* bump instruction counter */
3226
3227   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3228   assert (insn.insn_mo);
3229   assert (strcmp (name, insn.insn_mo->name) == 0);
3230   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3231
3232   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3233   if (*r == BFD_RELOC_UNUSED)
3234     {
3235       insn.insn_opcode |= high_expr.X_add_number;
3236       append_insn (place, &insn, NULL, r, false);
3237     }
3238   else
3239     append_insn (place, &insn, &high_expr, r, false);
3240 }
3241
3242 /*                      set_at()
3243  * Generates code to set the $at register to true (one)
3244  * if reg is less than the immediate expression.
3245  */
3246 static void
3247 set_at (counter, reg, unsignedp)
3248      int *counter;
3249      int reg;
3250      int unsignedp;
3251 {
3252   if (imm_expr.X_op == O_constant
3253       && imm_expr.X_add_number >= -0x8000
3254       && imm_expr.X_add_number < 0x8000)
3255     macro_build ((char *) NULL, counter, &imm_expr,
3256                  unsignedp ? "sltiu" : "slti",
3257                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3258   else
3259     {
3260       load_register (counter, AT, &imm_expr, 0);
3261       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3262                    unsignedp ? "sltu" : "slt",
3263                    "d,v,t", AT, reg, AT);
3264     }
3265 }
3266
3267 /* Warn if an expression is not a constant.  */
3268
3269 static void
3270 check_absolute_expr (ip, ex)
3271      struct mips_cl_insn *ip;
3272      expressionS *ex;
3273 {
3274   if (ex->X_op == O_big)
3275     as_bad (_("unsupported large constant"));
3276   else if (ex->X_op != O_constant)
3277     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3278 }
3279
3280 /* Count the leading zeroes by performing a binary chop. This is a
3281    bulky bit of source, but performance is a LOT better for the
3282    majority of values than a simple loop to count the bits:
3283        for (lcnt = 0; (lcnt < 32); lcnt++)
3284          if ((v) & (1 << (31 - lcnt)))
3285            break;
3286   However it is not code size friendly, and the gain will drop a bit
3287   on certain cached systems.
3288 */
3289 #define COUNT_TOP_ZEROES(v)             \
3290   (((v) & ~0xffff) == 0                 \
3291    ? ((v) & ~0xff) == 0                 \
3292      ? ((v) & ~0xf) == 0                \
3293        ? ((v) & ~0x3) == 0              \
3294          ? ((v) & ~0x1) == 0            \
3295            ? !(v)                       \
3296              ? 32                       \
3297              : 31                       \
3298            : 30                         \
3299          : ((v) & ~0x7) == 0            \
3300            ? 29                         \
3301            : 28                         \
3302        : ((v) & ~0x3f) == 0             \
3303          ? ((v) & ~0x1f) == 0           \
3304            ? 27                         \
3305            : 26                         \
3306          : ((v) & ~0x7f) == 0           \
3307            ? 25                         \
3308            : 24                         \
3309      : ((v) & ~0xfff) == 0              \
3310        ? ((v) & ~0x3ff) == 0            \
3311          ? ((v) & ~0x1ff) == 0          \
3312            ? 23                         \
3313            : 22                         \
3314          : ((v) & ~0x7ff) == 0          \
3315            ? 21                         \
3316            : 20                         \
3317        : ((v) & ~0x3fff) == 0           \
3318          ? ((v) & ~0x1fff) == 0         \
3319            ? 19                         \
3320            : 18                         \
3321          : ((v) & ~0x7fff) == 0         \
3322            ? 17                         \
3323            : 16                         \
3324    : ((v) & ~0xffffff) == 0             \
3325      ? ((v) & ~0xfffff) == 0            \
3326        ? ((v) & ~0x3ffff) == 0          \
3327          ? ((v) & ~0x1ffff) == 0        \
3328            ? 15                         \
3329            : 14                         \
3330          : ((v) & ~0x7ffff) == 0        \
3331            ? 13                         \
3332            : 12                         \
3333        : ((v) & ~0x3fffff) == 0         \
3334          ? ((v) & ~0x1fffff) == 0       \
3335            ? 11                         \
3336            : 10                         \
3337          : ((v) & ~0x7fffff) == 0       \
3338            ? 9                          \
3339            : 8                          \
3340      : ((v) & ~0xfffffff) == 0          \
3341        ? ((v) & ~0x3ffffff) == 0        \
3342          ? ((v) & ~0x1ffffff) == 0      \
3343            ? 7                          \
3344            : 6                          \
3345          : ((v) & ~0x7ffffff) == 0      \
3346            ? 5                          \
3347            : 4                          \
3348        : ((v) & ~0x3fffffff) == 0       \
3349          ? ((v) & ~0x1fffffff) == 0     \
3350            ? 3                          \
3351            : 2                          \
3352          : ((v) & ~0x7fffffff) == 0     \
3353            ? 1                          \
3354            : 0)
3355
3356 /*                      load_register()
3357  *  This routine generates the least number of instructions neccessary to load
3358  *  an absolute expression value into a register.
3359  */
3360 static void
3361 load_register (counter, reg, ep, dbl)
3362      int *counter;
3363      int reg;
3364      expressionS *ep;
3365      int dbl;
3366 {
3367   int freg;
3368   expressionS hi32, lo32;
3369
3370   if (ep->X_op != O_big)
3371     {
3372       assert (ep->X_op == O_constant);
3373       if (ep->X_add_number < 0x8000
3374           && (ep->X_add_number >= 0
3375               || (ep->X_add_number >= -0x8000
3376                   && (! dbl
3377                       || ! ep->X_unsigned
3378                       || sizeof (ep->X_add_number) > 4))))
3379         {
3380           /* We can handle 16 bit signed values with an addiu to
3381              $zero.  No need to ever use daddiu here, since $zero and
3382              the result are always correct in 32 bit mode.  */
3383           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3384                        (int) BFD_RELOC_LO16);
3385           return;
3386         }
3387       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3388         {
3389           /* We can handle 16 bit unsigned values with an ori to
3390              $zero.  */
3391           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3392                        (int) BFD_RELOC_LO16);
3393           return;
3394         }
3395       else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
3396                  || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
3397                      == ~ (offsetT) 0x7fffffff))
3398                 && (! dbl
3399                     || ! ep->X_unsigned
3400                     || sizeof (ep->X_add_number) > 4
3401                     || (ep->X_add_number & 0x80000000) == 0))
3402                || ((HAVE_32BIT_GPRS || ! dbl)
3403                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3404                || (HAVE_32BIT_GPRS
3405                    && ! dbl
3406                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3407                        == ~ (offsetT) 0xffffffff)))
3408         {
3409           /* 32 bit values require an lui.  */
3410           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3411                        (int) BFD_RELOC_HI16);
3412           if ((ep->X_add_number & 0xffff) != 0)
3413             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3414                          (int) BFD_RELOC_LO16);
3415           return;
3416         }
3417     }
3418
3419   /* The value is larger than 32 bits.  */
3420
3421   if (HAVE_32BIT_GPRS)
3422     {
3423       as_bad (_("Number (0x%lx) larger than 32 bits"),
3424               (unsigned long) ep->X_add_number);
3425       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3426                    (int) BFD_RELOC_LO16);
3427       return;
3428     }
3429
3430   if (ep->X_op != O_big)
3431     {
3432       hi32 = *ep;
3433       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3434       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3435       hi32.X_add_number &= 0xffffffff;
3436       lo32 = *ep;
3437       lo32.X_add_number &= 0xffffffff;
3438     }
3439   else
3440     {
3441       assert (ep->X_add_number > 2);
3442       if (ep->X_add_number == 3)
3443         generic_bignum[3] = 0;
3444       else if (ep->X_add_number > 4)
3445         as_bad (_("Number larger than 64 bits"));
3446       lo32.X_op = O_constant;
3447       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3448       hi32.X_op = O_constant;
3449       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3450     }
3451
3452   if (hi32.X_add_number == 0)
3453     freg = 0;
3454   else
3455     {
3456       int shift, bit;
3457       unsigned long hi, lo;
3458
3459       if (hi32.X_add_number == (offsetT) 0xffffffff)
3460         {
3461           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3462             {
3463               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3464                            reg, 0, (int) BFD_RELOC_LO16);
3465               return;
3466             }
3467           if (lo32.X_add_number & 0x80000000)
3468             {
3469               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3470                            (int) BFD_RELOC_HI16);
3471               if (lo32.X_add_number & 0xffff)
3472                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3473                              reg, reg, (int) BFD_RELOC_LO16);
3474               return;
3475             }
3476         }
3477
3478       /* Check for 16bit shifted constant.  We know that hi32 is
3479          non-zero, so start the mask on the first bit of the hi32
3480          value.  */
3481       shift = 17;
3482       do
3483         {
3484           unsigned long himask, lomask;
3485
3486           if (shift < 32)
3487             {
3488               himask = 0xffff >> (32 - shift);
3489               lomask = (0xffff << shift) & 0xffffffff;
3490             }
3491           else
3492             {
3493               himask = 0xffff << (shift - 32);
3494               lomask = 0;
3495             }
3496           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3497               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3498             {
3499               expressionS tmp;
3500
3501               tmp.X_op = O_constant;
3502               if (shift < 32)
3503                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3504                                     | (lo32.X_add_number >> shift));
3505               else
3506                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3507               macro_build ((char *) NULL, counter, &tmp,
3508                            "ori", "t,r,i", reg, 0,
3509                            (int) BFD_RELOC_LO16);
3510               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3511                            (shift >= 32) ? "dsll32" : "dsll",
3512                            "d,w,<", reg, reg,
3513                            (shift >= 32) ? shift - 32 : shift);
3514               return;
3515             }
3516           shift++;
3517         }
3518       while (shift <= (64 - 16));
3519
3520       /* Find the bit number of the lowest one bit, and store the
3521          shifted value in hi/lo.  */
3522       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3523       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3524       if (lo != 0)
3525         {
3526           bit = 0;
3527           while ((lo & 1) == 0)
3528             {
3529               lo >>= 1;
3530               ++bit;
3531             }
3532           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3533           hi >>= bit;
3534         }
3535       else
3536         {
3537           bit = 32;
3538           while ((hi & 1) == 0)
3539             {
3540               hi >>= 1;
3541               ++bit;
3542             }
3543           lo = hi;
3544           hi = 0;
3545         }
3546
3547       /* Optimize if the shifted value is a (power of 2) - 1.  */
3548       if ((hi == 0 && ((lo + 1) & lo) == 0)
3549           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3550         {
3551           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3552           if (shift != 0)
3553             {
3554               expressionS tmp;
3555
3556               /* This instruction will set the register to be all
3557                  ones.  */
3558               tmp.X_op = O_constant;
3559               tmp.X_add_number = (offsetT) -1;
3560               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3561                            reg, 0, (int) BFD_RELOC_LO16);
3562               if (bit != 0)
3563                 {
3564                   bit += shift;
3565                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3566                                (bit >= 32) ? "dsll32" : "dsll",
3567                                "d,w,<", reg, reg,
3568                                (bit >= 32) ? bit - 32 : bit);
3569                 }
3570               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3571                            (shift >= 32) ? "dsrl32" : "dsrl",
3572                            "d,w,<", reg, reg,
3573                            (shift >= 32) ? shift - 32 : shift);
3574               return;
3575             }
3576         }
3577
3578       /* Sign extend hi32 before calling load_register, because we can
3579          generally get better code when we load a sign extended value.  */
3580       if ((hi32.X_add_number & 0x80000000) != 0)
3581         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3582       load_register (counter, reg, &hi32, 0);
3583       freg = reg;
3584     }
3585   if ((lo32.X_add_number & 0xffff0000) == 0)
3586     {
3587       if (freg != 0)
3588         {
3589           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3590                        "dsll32", "d,w,<", reg, freg, 0);
3591           freg = reg;
3592         }
3593     }
3594   else
3595     {
3596       expressionS mid16;
3597
3598       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3599         {
3600           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3601                        (int) BFD_RELOC_HI16);
3602           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3603                        "dsrl32", "d,w,<", reg, reg, 0);
3604           return;
3605         }
3606
3607       if (freg != 0)
3608         {
3609           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3610                        "d,w,<", reg, freg, 16);
3611           freg = reg;
3612         }
3613       mid16 = lo32;
3614       mid16.X_add_number >>= 16;
3615       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3616                    freg, (int) BFD_RELOC_LO16);
3617       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3618                    "d,w,<", reg, reg, 16);
3619       freg = reg;
3620     }
3621   if ((lo32.X_add_number & 0xffff) != 0)
3622     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3623                  (int) BFD_RELOC_LO16);
3624 }
3625
3626 /* Load an address into a register.  */
3627
3628 static void
3629 load_address (counter, reg, ep, dbl, used_at)
3630      int *counter;
3631      int reg;
3632      expressionS *ep;
3633      int dbl;
3634      int *used_at;
3635 {
3636   char *p;
3637
3638   if (ep->X_op != O_constant
3639       && ep->X_op != O_symbol)
3640     {
3641       as_bad (_("expression too complex"));
3642       ep->X_op = O_constant;
3643     }
3644
3645   if (ep->X_op == O_constant)
3646     {
3647       load_register (counter, reg, ep, dbl);
3648       return;
3649     }
3650
3651   if (mips_pic == NO_PIC)
3652     {
3653       /* If this is a reference to a GP relative symbol, we want
3654            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3655          Otherwise we want
3656            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3657            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3658          If we have an addend, we always use the latter form.
3659
3660          With 64bit address space and a usable $at we want
3661            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3662            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3663            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3664            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3665            dsll32       $reg,0
3666            dadd         $reg,$reg,$at
3667
3668          If $at is already in use, we use an path which is suboptimal
3669          on superscalar processors.
3670            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3671            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3672            dsll         $reg,16
3673            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3674            dsll         $reg,16
3675            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3676        */
3677       if (dbl)
3678         {
3679           p = NULL;
3680
3681           /* We don't do GP optimization for now because RELAX_ENCODE can't
3682              hold the data for such large chunks.  */
3683
3684           if (*used_at == 0)
3685             {
3686               macro_build (p, counter, ep, "lui", "t,u",
3687                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3688               macro_build (p, counter, ep, "lui", "t,u",
3689                            AT, (int) BFD_RELOC_HI16_S);
3690               macro_build (p, counter, ep, "daddiu", "t,r,j",
3691                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3692               macro_build (p, counter, ep, "daddiu", "t,r,j",
3693                            AT, AT, (int) BFD_RELOC_LO16);
3694               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3695                            "d,w,<", reg, reg, 0);
3696               macro_build (p, counter, (expressionS *) NULL, "dadd",
3697                            "d,v,t", reg, reg, AT);
3698               *used_at = 1;
3699             }
3700           else
3701             {
3702               macro_build (p, counter, ep, "lui", "t,u",
3703                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3704               macro_build (p, counter, ep, "daddiu", "t,r,j",
3705                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3706               macro_build (p, counter, (expressionS *) NULL, "dsll",
3707                            "d,w,<", reg, reg, 16);
3708               macro_build (p, counter, ep, "daddiu", "t,r,j",
3709                            reg, reg, (int) BFD_RELOC_HI16_S);
3710               macro_build (p, counter, (expressionS *) NULL, "dsll",
3711                            "d,w,<", reg, reg, 16);
3712               macro_build (p, counter, ep, "daddiu", "t,r,j",
3713                            reg, reg, (int) BFD_RELOC_LO16);
3714             }
3715         }
3716       else
3717         {
3718           p = NULL;
3719           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3720               && ! nopic_need_relax (ep->X_add_symbol, 1))
3721             {
3722               frag_grow (20);
3723               macro_build ((char *) NULL, counter, ep,
3724                            dbl ? "daddiu" : "addiu", "t,r,j", reg, GP,
3725                            (int) BFD_RELOC_GPREL16);
3726               p = frag_var (rs_machine_dependent, 8, 0,
3727                             RELAX_ENCODE (4, 8, 0, 4, 0,
3728                                           mips_opts.warn_about_macros),
3729                             ep->X_add_symbol, 0, NULL);
3730             }
3731           macro_build_lui (p, counter, ep, reg);
3732           if (p != NULL)
3733             p += 4;
3734           macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3735                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3736         }
3737     }
3738   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3739     {
3740       expressionS ex;
3741
3742       /* If this is a reference to an external symbol, we want
3743            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3744          Otherwise we want
3745            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3746            nop
3747            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3748          If there is a constant, it must be added in after.  */
3749       ex.X_add_number = ep->X_add_number;
3750       ep->X_add_number = 0;
3751       frag_grow (20);
3752       macro_build ((char *) NULL, counter, ep,
3753                    HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3754                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3755       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3756       p = frag_var (rs_machine_dependent, 4, 0,
3757                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3758                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3759       macro_build (p, counter, ep,
3760                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3761                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3762       if (ex.X_add_number != 0)
3763         {
3764           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3765             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3766           ex.X_op = O_constant;
3767           macro_build ((char *) NULL, counter, &ex,
3768                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3769                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3770         }
3771     }
3772   else if (mips_pic == SVR4_PIC)
3773     {
3774       expressionS ex;
3775       int off;
3776
3777       /* This is the large GOT case.  If this is a reference to an
3778          external symbol, we want
3779            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3780            addu         $reg,$reg,$gp
3781            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3782          Otherwise, for a reference to a local symbol, we want
3783            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3784            nop
3785            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3786          If there is a constant, it must be added in after.  */
3787       ex.X_add_number = ep->X_add_number;
3788       ep->X_add_number = 0;
3789       if (reg_needs_delay (GP))
3790         off = 4;
3791       else
3792         off = 0;
3793       frag_grow (32);
3794       macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3795                    (int) BFD_RELOC_MIPS_GOT_HI16);
3796       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3797                    dbl ? "daddu" : "addu", "d,v,t", reg, reg, GP);
3798       macro_build ((char *) NULL, counter, ep, dbl ? "ld" : "lw",
3799                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3800       p = frag_var (rs_machine_dependent, 12 + off, 0,
3801                     RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3802                                   mips_opts.warn_about_macros),
3803                     ep->X_add_symbol, 0, NULL);
3804       if (off > 0)
3805         {
3806           /* We need a nop before loading from $gp.  This special
3807              check is required because the lui which starts the main
3808              instruction stream does not refer to $gp, and so will not
3809              insert the nop which may be required.  */
3810           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3811           p += 4;
3812         }
3813       macro_build (p, counter, ep, dbl ? "ld" : "lw",
3814                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3815       p += 4;
3816       macro_build (p, counter, (expressionS *) NULL, "nop", "");
3817       p += 4;
3818       macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3819                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3820       if (ex.X_add_number != 0)
3821         {
3822           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3823             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3824           ex.X_op = O_constant;
3825           macro_build ((char *) NULL, counter, &ex, dbl ? "daddiu" : "addiu",
3826                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3827         }
3828     }
3829   else if (mips_pic == EMBEDDED_PIC)
3830     {
3831       /* We always do
3832            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3833          */
3834       macro_build ((char *) NULL, counter, ep, dbl ? "daddiu" : "addiu",
3835                    "t,r,j", reg, GP, (int) BFD_RELOC_GPREL16);
3836     }
3837   else
3838     abort ();
3839 }
3840
3841 /* Move the contents of register SOURCE into register DEST.  */
3842
3843 static void
3844 move_register (counter, dest, source)
3845      int *counter;
3846      int dest;
3847      int source;
3848 {
3849   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3850                HAVE_32BIT_GPRS ? "addu" : "daddu",
3851                "d,v,t", dest, source, 0);
3852 }
3853
3854 /*
3855  *                      Build macros
3856  *   This routine implements the seemingly endless macro or synthesized
3857  * instructions and addressing modes in the mips assembly language. Many
3858  * of these macros are simple and are similar to each other. These could
3859  * probably be handled by some kind of table or grammer aproach instead of
3860  * this verbose method. Others are not simple macros but are more like
3861  * optimizing code generation.
3862  *   One interesting optimization is when several store macros appear
3863  * consecutivly that would load AT with the upper half of the same address.
3864  * The ensuing load upper instructions are ommited. This implies some kind
3865  * of global optimization. We currently only optimize within a single macro.
3866  *   For many of the load and store macros if the address is specified as a
3867  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3868  * first load register 'at' with zero and use it as the base register. The
3869  * mips assembler simply uses register $zero. Just one tiny optimization
3870  * we're missing.
3871  */
3872 static void
3873 macro (ip)
3874      struct mips_cl_insn *ip;
3875 {
3876   register int treg, sreg, dreg, breg;
3877   int tempreg;
3878   int mask;
3879   int icnt = 0;
3880   int used_at = 0;
3881   expressionS expr1;
3882   const char *s;
3883   const char *s2;
3884   const char *fmt;
3885   int likely = 0;
3886   int dbl = 0;
3887   int coproc = 0;
3888   int lr = 0;
3889   int imm = 0;
3890   offsetT maxnum;
3891   int off;
3892   bfd_reloc_code_real_type r;
3893   char *p;
3894   int hold_mips_optimize;
3895
3896   assert (! mips_opts.mips16);
3897
3898   treg = (ip->insn_opcode >> 16) & 0x1f;
3899   dreg = (ip->insn_opcode >> 11) & 0x1f;
3900   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3901   mask = ip->insn_mo->mask;
3902
3903   expr1.X_op = O_constant;
3904   expr1.X_op_symbol = NULL;
3905   expr1.X_add_symbol = NULL;
3906   expr1.X_add_number = 1;
3907
3908   switch (mask)
3909     {
3910     case M_DABS:
3911       dbl = 1;
3912     case M_ABS:
3913       /* bgez $a0,.+12
3914          move v0,$a0
3915          sub v0,$zero,$a0
3916          */
3917
3918       mips_emit_delays (true);
3919       ++mips_opts.noreorder;
3920       mips_any_noreorder = 1;
3921
3922       expr1.X_add_number = 8;
3923       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3924       if (dreg == sreg)
3925         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3926                      0);
3927       else
3928         move_register (&icnt, dreg, sreg);
3929       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3930                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3931
3932       --mips_opts.noreorder;
3933       return;
3934
3935     case M_ADD_I:
3936       s = "addi";
3937       s2 = "add";
3938       goto do_addi;
3939     case M_ADDU_I:
3940       s = "addiu";
3941       s2 = "addu";
3942       goto do_addi;
3943     case M_DADD_I:
3944       dbl = 1;
3945       s = "daddi";
3946       s2 = "dadd";
3947       goto do_addi;
3948     case M_DADDU_I:
3949       dbl = 1;
3950       s = "daddiu";
3951       s2 = "daddu";
3952     do_addi:
3953       if (imm_expr.X_op == O_constant
3954           && imm_expr.X_add_number >= -0x8000
3955           && imm_expr.X_add_number < 0x8000)
3956         {
3957           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3958                        (int) BFD_RELOC_LO16);
3959           return;
3960         }
3961       load_register (&icnt, AT, &imm_expr, dbl);
3962       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3963                    treg, sreg, AT);
3964       break;
3965
3966     case M_AND_I:
3967       s = "andi";
3968       s2 = "and";
3969       goto do_bit;
3970     case M_OR_I:
3971       s = "ori";
3972       s2 = "or";
3973       goto do_bit;
3974     case M_NOR_I:
3975       s = "";
3976       s2 = "nor";
3977       goto do_bit;
3978     case M_XOR_I:
3979       s = "xori";
3980       s2 = "xor";
3981     do_bit:
3982       if (imm_expr.X_op == O_constant
3983           && imm_expr.X_add_number >= 0
3984           && imm_expr.X_add_number < 0x10000)
3985         {
3986           if (mask != M_NOR_I)
3987             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3988                          sreg, (int) BFD_RELOC_LO16);
3989           else
3990             {
3991               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3992                            treg, sreg, (int) BFD_RELOC_LO16);
3993               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3994                            "d,v,t", treg, treg, 0);
3995             }
3996           return;
3997         }
3998
3999       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4000       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4001                    treg, sreg, AT);
4002       break;
4003
4004     case M_BEQ_I:
4005       s = "beq";
4006       goto beq_i;
4007     case M_BEQL_I:
4008       s = "beql";
4009       likely = 1;
4010       goto beq_i;
4011     case M_BNE_I:
4012       s = "bne";
4013       goto beq_i;
4014     case M_BNEL_I:
4015       s = "bnel";
4016       likely = 1;
4017     beq_i:
4018       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4019         {
4020           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4021                        0);
4022           return;
4023         }
4024       load_register (&icnt, AT, &imm_expr, 0);
4025       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4026       break;
4027
4028     case M_BGEL:
4029       likely = 1;
4030     case M_BGE:
4031       if (treg == 0)
4032         {
4033           macro_build ((char *) NULL, &icnt, &offset_expr,
4034                        likely ? "bgezl" : "bgez", "s,p", sreg);
4035           return;
4036         }
4037       if (sreg == 0)
4038         {
4039           macro_build ((char *) NULL, &icnt, &offset_expr,
4040                        likely ? "blezl" : "blez", "s,p", treg);
4041           return;
4042         }
4043       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4044                    AT, sreg, treg);
4045       macro_build ((char *) NULL, &icnt, &offset_expr,
4046                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4047       break;
4048
4049     case M_BGTL_I:
4050       likely = 1;
4051     case M_BGT_I:
4052       /* check for > max integer */
4053       maxnum = 0x7fffffff;
4054       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4055         {
4056           maxnum <<= 16;
4057           maxnum |= 0xffff;
4058           maxnum <<= 16;
4059           maxnum |= 0xffff;
4060         }
4061       if (imm_expr.X_op == O_constant
4062           && imm_expr.X_add_number >= maxnum
4063           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4064         {
4065         do_false:
4066           /* result is always false */
4067           if (! likely)
4068             {
4069               if (warn_nops)
4070                 as_warn (_("Branch %s is always false (nop)"),
4071                          ip->insn_mo->name);
4072               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4073                            "", 0);
4074             }
4075           else
4076             {
4077               if (warn_nops)
4078                 as_warn (_("Branch likely %s is always false"),
4079                          ip->insn_mo->name);
4080               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4081                            "s,t,p", 0, 0);
4082             }
4083           return;
4084         }
4085       if (imm_expr.X_op != O_constant)
4086         as_bad (_("Unsupported large constant"));
4087       imm_expr.X_add_number++;
4088       /* FALLTHROUGH */
4089     case M_BGE_I:
4090     case M_BGEL_I:
4091       if (mask == M_BGEL_I)
4092         likely = 1;
4093       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4094         {
4095           macro_build ((char *) NULL, &icnt, &offset_expr,
4096                        likely ? "bgezl" : "bgez", "s,p", sreg);
4097           return;
4098         }
4099       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4100         {
4101           macro_build ((char *) NULL, &icnt, &offset_expr,
4102                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4103           return;
4104         }
4105       maxnum = 0x7fffffff;
4106       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4107         {
4108           maxnum <<= 16;
4109           maxnum |= 0xffff;
4110           maxnum <<= 16;
4111           maxnum |= 0xffff;
4112         }
4113       maxnum = - maxnum - 1;
4114       if (imm_expr.X_op == O_constant
4115           && imm_expr.X_add_number <= maxnum
4116           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4117         {
4118         do_true:
4119           /* result is always true */
4120           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4121           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4122           return;
4123         }
4124       set_at (&icnt, sreg, 0);
4125       macro_build ((char *) NULL, &icnt, &offset_expr,
4126                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4127       break;
4128
4129     case M_BGEUL:
4130       likely = 1;
4131     case M_BGEU:
4132       if (treg == 0)
4133         goto do_true;
4134       if (sreg == 0)
4135         {
4136           macro_build ((char *) NULL, &icnt, &offset_expr,
4137                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4138           return;
4139         }
4140       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4141                    "d,v,t", AT, sreg, treg);
4142       macro_build ((char *) NULL, &icnt, &offset_expr,
4143                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4144       break;
4145
4146     case M_BGTUL_I:
4147       likely = 1;
4148     case M_BGTU_I:
4149       if (sreg == 0
4150           || (HAVE_32BIT_GPRS
4151               && imm_expr.X_op == O_constant
4152               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4153         goto do_false;
4154       if (imm_expr.X_op != O_constant)
4155         as_bad (_("Unsupported large constant"));
4156       imm_expr.X_add_number++;
4157       /* FALLTHROUGH */
4158     case M_BGEU_I:
4159     case M_BGEUL_I:
4160       if (mask == M_BGEUL_I)
4161         likely = 1;
4162       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4163         goto do_true;
4164       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4165         {
4166           macro_build ((char *) NULL, &icnt, &offset_expr,
4167                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4168           return;
4169         }
4170       set_at (&icnt, sreg, 1);
4171       macro_build ((char *) NULL, &icnt, &offset_expr,
4172                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4173       break;
4174
4175     case M_BGTL:
4176       likely = 1;
4177     case M_BGT:
4178       if (treg == 0)
4179         {
4180           macro_build ((char *) NULL, &icnt, &offset_expr,
4181                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4182           return;
4183         }
4184       if (sreg == 0)
4185         {
4186           macro_build ((char *) NULL, &icnt, &offset_expr,
4187                        likely ? "bltzl" : "bltz", "s,p", treg);
4188           return;
4189         }
4190       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4191                    AT, treg, sreg);
4192       macro_build ((char *) NULL, &icnt, &offset_expr,
4193                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4194       break;
4195
4196     case M_BGTUL:
4197       likely = 1;
4198     case M_BGTU:
4199       if (treg == 0)
4200         {
4201           macro_build ((char *) NULL, &icnt, &offset_expr,
4202                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4203           return;
4204         }
4205       if (sreg == 0)
4206         goto do_false;
4207       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4208                    "d,v,t", AT, treg, sreg);
4209       macro_build ((char *) NULL, &icnt, &offset_expr,
4210                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4211       break;
4212
4213     case M_BLEL:
4214       likely = 1;
4215     case M_BLE:
4216       if (treg == 0)
4217         {
4218           macro_build ((char *) NULL, &icnt, &offset_expr,
4219                        likely ? "blezl" : "blez", "s,p", sreg);
4220           return;
4221         }
4222       if (sreg == 0)
4223         {
4224           macro_build ((char *) NULL, &icnt, &offset_expr,
4225                        likely ? "bgezl" : "bgez", "s,p", treg);
4226           return;
4227         }
4228       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4229                    AT, treg, sreg);
4230       macro_build ((char *) NULL, &icnt, &offset_expr,
4231                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4232       break;
4233
4234     case M_BLEL_I:
4235       likely = 1;
4236     case M_BLE_I:
4237       maxnum = 0x7fffffff;
4238       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4239         {
4240           maxnum <<= 16;
4241           maxnum |= 0xffff;
4242           maxnum <<= 16;
4243           maxnum |= 0xffff;
4244         }
4245       if (imm_expr.X_op == O_constant
4246           && imm_expr.X_add_number >= maxnum
4247           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4248         goto do_true;
4249       if (imm_expr.X_op != O_constant)
4250         as_bad (_("Unsupported large constant"));
4251       imm_expr.X_add_number++;
4252       /* FALLTHROUGH */
4253     case M_BLT_I:
4254     case M_BLTL_I:
4255       if (mask == M_BLTL_I)
4256         likely = 1;
4257       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4258         {
4259           macro_build ((char *) NULL, &icnt, &offset_expr,
4260                        likely ? "bltzl" : "bltz", "s,p", sreg);
4261           return;
4262         }
4263       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4264         {
4265           macro_build ((char *) NULL, &icnt, &offset_expr,
4266                        likely ? "blezl" : "blez", "s,p", sreg);
4267           return;
4268         }
4269       set_at (&icnt, sreg, 0);
4270       macro_build ((char *) NULL, &icnt, &offset_expr,
4271                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4272       break;
4273
4274     case M_BLEUL:
4275       likely = 1;
4276     case M_BLEU:
4277       if (treg == 0)
4278         {
4279           macro_build ((char *) NULL, &icnt, &offset_expr,
4280                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4281           return;
4282         }
4283       if (sreg == 0)
4284         goto do_true;
4285       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4286                    "d,v,t", AT, treg, sreg);
4287       macro_build ((char *) NULL, &icnt, &offset_expr,
4288                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4289       break;
4290
4291     case M_BLEUL_I:
4292       likely = 1;
4293     case M_BLEU_I:
4294       if (sreg == 0
4295           || (HAVE_32BIT_GPRS
4296               && imm_expr.X_op == O_constant
4297               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4298         goto do_true;
4299       if (imm_expr.X_op != O_constant)
4300         as_bad (_("Unsupported large constant"));
4301       imm_expr.X_add_number++;
4302       /* FALLTHROUGH */
4303     case M_BLTU_I:
4304     case M_BLTUL_I:
4305       if (mask == M_BLTUL_I)
4306         likely = 1;
4307       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4308         goto do_false;
4309       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4310         {
4311           macro_build ((char *) NULL, &icnt, &offset_expr,
4312                        likely ? "beql" : "beq",
4313                        "s,t,p", sreg, 0);
4314           return;
4315         }
4316       set_at (&icnt, sreg, 1);
4317       macro_build ((char *) NULL, &icnt, &offset_expr,
4318                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4319       break;
4320
4321     case M_BLTL:
4322       likely = 1;
4323     case M_BLT:
4324       if (treg == 0)
4325         {
4326           macro_build ((char *) NULL, &icnt, &offset_expr,
4327                        likely ? "bltzl" : "bltz", "s,p", sreg);
4328           return;
4329         }
4330       if (sreg == 0)
4331         {
4332           macro_build ((char *) NULL, &icnt, &offset_expr,
4333                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4334           return;
4335         }
4336       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4337                    AT, sreg, treg);
4338       macro_build ((char *) NULL, &icnt, &offset_expr,
4339                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4340       break;
4341
4342     case M_BLTUL:
4343       likely = 1;
4344     case M_BLTU:
4345       if (treg == 0)
4346         goto do_false;
4347       if (sreg == 0)
4348         {
4349           macro_build ((char *) NULL, &icnt, &offset_expr,
4350                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4351           return;
4352         }
4353       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4354                    "d,v,t", AT, sreg,
4355                    treg);
4356       macro_build ((char *) NULL, &icnt, &offset_expr,
4357                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4358       break;
4359
4360     case M_DDIV_3:
4361       dbl = 1;
4362     case M_DIV_3:
4363       s = "mflo";
4364       goto do_div3;
4365     case M_DREM_3:
4366       dbl = 1;
4367     case M_REM_3:
4368       s = "mfhi";
4369     do_div3:
4370       if (treg == 0)
4371         {
4372           as_warn (_("Divide by zero."));
4373           if (mips_trap)
4374             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4375                          "s,t", 0, 0);
4376           else
4377             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4378                          "c", 7);
4379           return;
4380         }
4381
4382       mips_emit_delays (true);
4383       ++mips_opts.noreorder;
4384       mips_any_noreorder = 1;
4385       if (mips_trap)
4386         {
4387           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4388                        "s,t", treg, 0);
4389           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4390                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4391         }
4392       else
4393         {
4394           expr1.X_add_number = 8;
4395           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4396           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4397                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4398           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4399                        "c", 7);
4400         }
4401       expr1.X_add_number = -1;
4402       macro_build ((char *) NULL, &icnt, &expr1,
4403                    dbl ? "daddiu" : "addiu",
4404                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4405       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4406       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4407       if (dbl)
4408         {
4409           expr1.X_add_number = 1;
4410           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4411                        (int) BFD_RELOC_LO16);
4412           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4413                        "d,w,<", AT, AT, 31);
4414         }
4415       else
4416         {
4417           expr1.X_add_number = 0x80000000;
4418           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4419                        (int) BFD_RELOC_HI16);
4420         }
4421       if (mips_trap)
4422         {
4423           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4424                        "s,t", sreg, AT);
4425           /* We want to close the noreorder block as soon as possible, so
4426              that later insns are available for delay slot filling.  */
4427           --mips_opts.noreorder;
4428         }
4429       else
4430         {
4431           expr1.X_add_number = 8;
4432           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4433           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4434                        0);
4435
4436           /* We want to close the noreorder block as soon as possible, so
4437              that later insns are available for delay slot filling.  */
4438           --mips_opts.noreorder;
4439
4440           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4441                        "c", 6);
4442         }
4443       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4444       break;
4445
4446     case M_DIV_3I:
4447       s = "div";
4448       s2 = "mflo";
4449       goto do_divi;
4450     case M_DIVU_3I:
4451       s = "divu";
4452       s2 = "mflo";
4453       goto do_divi;
4454     case M_REM_3I:
4455       s = "div";
4456       s2 = "mfhi";
4457       goto do_divi;
4458     case M_REMU_3I:
4459       s = "divu";
4460       s2 = "mfhi";
4461       goto do_divi;
4462     case M_DDIV_3I:
4463       dbl = 1;
4464       s = "ddiv";
4465       s2 = "mflo";
4466       goto do_divi;
4467     case M_DDIVU_3I:
4468       dbl = 1;
4469       s = "ddivu";
4470       s2 = "mflo";
4471       goto do_divi;
4472     case M_DREM_3I:
4473       dbl = 1;
4474       s = "ddiv";
4475       s2 = "mfhi";
4476       goto do_divi;
4477     case M_DREMU_3I:
4478       dbl = 1;
4479       s = "ddivu";
4480       s2 = "mfhi";
4481     do_divi:
4482       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4483         {
4484           as_warn (_("Divide by zero."));
4485           if (mips_trap)
4486             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4487                          "s,t", 0, 0);
4488           else
4489             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4490                          "c", 7);
4491           return;
4492         }
4493       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4494         {
4495           if (strcmp (s2, "mflo") == 0)
4496             move_register (&icnt, dreg, sreg);
4497           else
4498             move_register (&icnt, dreg, 0);
4499           return;
4500         }
4501       if (imm_expr.X_op == O_constant
4502           && imm_expr.X_add_number == -1
4503           && s[strlen (s) - 1] != 'u')
4504         {
4505           if (strcmp (s2, "mflo") == 0)
4506             {
4507               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4508                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4509             }
4510           else
4511             move_register (&icnt, dreg, 0);
4512           return;
4513         }
4514
4515       load_register (&icnt, AT, &imm_expr, dbl);
4516       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4517                    sreg, AT);
4518       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4519       break;
4520
4521     case M_DIVU_3:
4522       s = "divu";
4523       s2 = "mflo";
4524       goto do_divu3;
4525     case M_REMU_3:
4526       s = "divu";
4527       s2 = "mfhi";
4528       goto do_divu3;
4529     case M_DDIVU_3:
4530       s = "ddivu";
4531       s2 = "mflo";
4532       goto do_divu3;
4533     case M_DREMU_3:
4534       s = "ddivu";
4535       s2 = "mfhi";
4536     do_divu3:
4537       mips_emit_delays (true);
4538       ++mips_opts.noreorder;
4539       mips_any_noreorder = 1;
4540       if (mips_trap)
4541         {
4542           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4543                        "s,t", treg, 0);
4544           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4545                        sreg, treg);
4546           /* We want to close the noreorder block as soon as possible, so
4547              that later insns are available for delay slot filling.  */
4548           --mips_opts.noreorder;
4549         }
4550       else
4551         {
4552           expr1.X_add_number = 8;
4553           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4554           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4555                        sreg, treg);
4556
4557           /* We want to close the noreorder block as soon as possible, so
4558              that later insns are available for delay slot filling.  */
4559           --mips_opts.noreorder;
4560           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4561                        "c", 7);
4562         }
4563       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4564       return;
4565
4566     case M_DLA_AB:
4567       dbl = 1;
4568     case M_LA_AB:
4569       /* Load the address of a symbol into a register.  If breg is not
4570          zero, we then add a base register to it.  */
4571
4572       if (treg == breg)
4573         {
4574           tempreg = AT;
4575           used_at = 1;
4576         }
4577       else
4578         {
4579           tempreg = treg;
4580           used_at = 0;
4581         }
4582
4583       /* When generating embedded PIC code, we permit expressions of
4584          the form
4585            la   $treg,foo-bar
4586            la   $treg,foo-bar($breg)
4587          where bar is an address in the current section.  These are used
4588          when getting the addresses of functions.  We don't permit
4589          X_add_number to be non-zero, because if the symbol is
4590          external the relaxing code needs to know that any addend is
4591          purely the offset to X_op_symbol.  */
4592       if (mips_pic == EMBEDDED_PIC
4593           && offset_expr.X_op == O_subtract
4594           && (symbol_constant_p (offset_expr.X_op_symbol)
4595               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4596               : (symbol_equated_p (offset_expr.X_op_symbol)
4597                  && (S_GET_SEGMENT
4598                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4599                       ->X_add_symbol)
4600                      == now_seg)))
4601           && (offset_expr.X_add_number == 0
4602               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4603         {
4604           if (breg == 0)
4605             {
4606               tempreg = treg;
4607               used_at = 0;
4608               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4609                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4610             }
4611           else
4612             {
4613               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4614                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4615               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4616                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4617                            "d,v,t", tempreg, tempreg, breg);
4618             }
4619           macro_build ((char *) NULL, &icnt, &offset_expr,
4620                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4621                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4622           if (! used_at)
4623             return;
4624           break;
4625         }
4626
4627       if (offset_expr.X_op != O_symbol
4628           && offset_expr.X_op != O_constant)
4629         {
4630           as_bad (_("expression too complex"));
4631           offset_expr.X_op = O_constant;
4632         }
4633
4634       if (offset_expr.X_op == O_constant)
4635         load_register (&icnt, tempreg, &offset_expr, dbl);
4636       else if (mips_pic == NO_PIC)
4637         {
4638           /* If this is a reference to a GP relative symbol, we want
4639                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4640              Otherwise we want
4641                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4642                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4643              If we have a constant, we need two instructions anyhow,
4644              so we may as well always use the latter form.
4645
4646             With 64bit address space and a usable $at we want
4647               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4648               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4649               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4650               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4651               dsll32    $tempreg,0
4652               dadd      $tempreg,$tempreg,$at
4653
4654             If $at is already in use, we use an path which is suboptimal
4655             on superscalar processors.
4656               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4657               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4658               dsll      $tempreg,16
4659               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4660               dsll      $tempreg,16
4661               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4662           */
4663           p = NULL;
4664           if (HAVE_64BIT_ADDRESSES)
4665             {
4666               /* We don't do GP optimization for now because RELAX_ENCODE can't
4667                  hold the data for such large chunks.  */
4668
4669             if (used_at == 0)
4670               {
4671                 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4672                              tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4673                 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4674                              AT, (int) BFD_RELOC_HI16_S);
4675                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4676                              tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4677                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4678                              AT, AT, (int) BFD_RELOC_LO16);
4679                 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4680                              "d,w,<", tempreg, tempreg, 0);
4681                 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4682                              tempreg, tempreg, AT);
4683                 used_at = 1;
4684               }
4685             else
4686               {
4687                 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4688                              tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4689                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4690                              tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4691                 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4692                              tempreg, tempreg, 16);
4693                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4694                              tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4695                 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4696                              tempreg, tempreg, 16);
4697                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4698                              tempreg, tempreg, (int) BFD_RELOC_LO16);
4699               }
4700           }
4701         else
4702           {
4703             if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4704                 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4705               {
4706                 frag_grow (20);
4707                 macro_build ((char *) NULL, &icnt, &offset_expr,
4708                              HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4709                              "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
4710                 p = frag_var (rs_machine_dependent, 8, 0,
4711                               RELAX_ENCODE (4, 8, 0, 4, 0,
4712                                             mips_opts.warn_about_macros),
4713                               offset_expr.X_add_symbol, 0, NULL);
4714               }
4715             macro_build_lui (p, &icnt, &offset_expr, tempreg);
4716             if (p != NULL)
4717               p += 4;
4718             macro_build (p, &icnt, &offset_expr,
4719                          HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4720                          "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4721           }
4722         }
4723       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4724         {
4725           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4726
4727           /* If this is a reference to an external symbol, and there
4728              is no constant, we want
4729                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4730              or if tempreg is PIC_CALL_REG
4731                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4732              For a local symbol, we want
4733                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4734                nop
4735                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4736
4737              If we have a small constant, and this is a reference to
4738              an external symbol, we want
4739                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4740                nop
4741                addiu    $tempreg,$tempreg,<constant>
4742              For a local symbol, we want the same instruction
4743              sequence, but we output a BFD_RELOC_LO16 reloc on the
4744              addiu instruction.
4745
4746              If we have a large constant, and this is a reference to
4747              an external symbol, we want
4748                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4749                lui      $at,<hiconstant>
4750                addiu    $at,$at,<loconstant>
4751                addu     $tempreg,$tempreg,$at
4752              For a local symbol, we want the same instruction
4753              sequence, but we output a BFD_RELOC_LO16 reloc on the
4754              addiu instruction.  */
4755           expr1.X_add_number = offset_expr.X_add_number;
4756           offset_expr.X_add_number = 0;
4757           frag_grow (32);
4758           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4759             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4760           macro_build ((char *) NULL, &icnt, &offset_expr, dbl ? "ld" : "lw",
4761                        "t,o(b)", tempreg, lw_reloc_type, GP);
4762           if (expr1.X_add_number == 0)
4763             {
4764               int off;
4765
4766               if (breg == 0)
4767                 off = 0;
4768               else
4769                 {
4770                   /* We're going to put in an addu instruction using
4771                      tempreg, so we may as well insert the nop right
4772                      now.  */
4773                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4774                                "nop", "");
4775                   off = 4;
4776                 }
4777               p = frag_var (rs_machine_dependent, 8 - off, 0,
4778                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4779                                           (breg == 0
4780                                            ? mips_opts.warn_about_macros
4781                                            : 0)),
4782                             offset_expr.X_add_symbol, 0, NULL);
4783               if (breg == 0)
4784                 {
4785                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4786                   p += 4;
4787                 }
4788               macro_build (p, &icnt, &expr1,
4789                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4790                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4791               /* FIXME: If breg == 0, and the next instruction uses
4792                  $tempreg, then if this variant case is used an extra
4793                  nop will be generated.  */
4794             }
4795           else if (expr1.X_add_number >= -0x8000
4796                    && expr1.X_add_number < 0x8000)
4797             {
4798               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4799                            "nop", "");
4800               macro_build ((char *) NULL, &icnt, &expr1,
4801                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4802                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4803               frag_var (rs_machine_dependent, 0, 0,
4804                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4805                         offset_expr.X_add_symbol, 0, NULL);
4806             }
4807           else
4808             {
4809               int off1;
4810
4811               /* If we are going to add in a base register, and the
4812                  target register and the base register are the same,
4813                  then we are using AT as a temporary register.  Since
4814                  we want to load the constant into AT, we add our
4815                  current AT (from the global offset table) and the
4816                  register into the register now, and pretend we were
4817                  not using a base register.  */
4818               if (breg != treg)
4819                 off1 = 0;
4820               else
4821                 {
4822                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4823                                "nop", "");
4824                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4825                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4826                                "d,v,t", treg, AT, breg);
4827                   breg = 0;
4828                   tempreg = treg;
4829                   off1 = -8;
4830                 }
4831
4832               /* Set mips_optimize around the lui instruction to avoid
4833                  inserting an unnecessary nop after the lw.  */
4834               hold_mips_optimize = mips_optimize;
4835               mips_optimize = 2;
4836               macro_build_lui (NULL, &icnt, &expr1, AT);
4837               mips_optimize = hold_mips_optimize;
4838
4839               macro_build ((char *) NULL, &icnt, &expr1,
4840                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4841                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4842               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4843                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4844                            "d,v,t", tempreg, tempreg, AT);
4845               frag_var (rs_machine_dependent, 0, 0,
4846                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4847                         offset_expr.X_add_symbol, 0, NULL);
4848               used_at = 1;
4849             }
4850         }
4851       else if (mips_pic == SVR4_PIC)
4852         {
4853           int gpdel;
4854           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4855           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4856
4857           /* This is the large GOT case.  If this is a reference to an
4858              external symbol, and there is no constant, we want
4859                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4860                addu     $tempreg,$tempreg,$gp
4861                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4862              or if tempreg is PIC_CALL_REG
4863                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4864                addu     $tempreg,$tempreg,$gp
4865                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4866              For a local symbol, we want
4867                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4868                nop
4869                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4870
4871              If we have a small constant, and this is a reference to
4872              an external symbol, we want
4873                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4874                addu     $tempreg,$tempreg,$gp
4875                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4876                nop
4877                addiu    $tempreg,$tempreg,<constant>
4878              For a local symbol, we want
4879                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4880                nop
4881                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4882
4883              If we have a large constant, and this is a reference to
4884              an external symbol, we want
4885                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4886                addu     $tempreg,$tempreg,$gp
4887                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4888                lui      $at,<hiconstant>
4889                addiu    $at,$at,<loconstant>
4890                addu     $tempreg,$tempreg,$at
4891              For a local symbol, we want
4892                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4893                lui      $at,<hiconstant>
4894                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4895                addu     $tempreg,$tempreg,$at
4896              */
4897           expr1.X_add_number = offset_expr.X_add_number;
4898           offset_expr.X_add_number = 0;
4899           frag_grow (52);
4900           if (reg_needs_delay (GP))
4901             gpdel = 4;
4902           else
4903             gpdel = 0;
4904           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4905             {
4906               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4907               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4908             }
4909           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4910                        tempreg, lui_reloc_type);
4911           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4912                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4913                        "d,v,t", tempreg, tempreg, GP);
4914           macro_build ((char *) NULL, &icnt, &offset_expr,
4915                        dbl ? "ld" : "lw",
4916                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4917           if (expr1.X_add_number == 0)
4918             {
4919               int off;
4920
4921               if (breg == 0)
4922                 off = 0;
4923               else
4924                 {
4925                   /* We're going to put in an addu instruction using
4926                      tempreg, so we may as well insert the nop right
4927                      now.  */
4928                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4929                                "nop", "");
4930                   off = 4;
4931                 }
4932
4933               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4934                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4935                                           8 + gpdel, 0,
4936                                           (breg == 0
4937                                            ? mips_opts.warn_about_macros
4938                                            : 0)),
4939                             offset_expr.X_add_symbol, 0, NULL);
4940             }
4941           else if (expr1.X_add_number >= -0x8000
4942                    && expr1.X_add_number < 0x8000)
4943             {
4944               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4945                            "nop", "");
4946               macro_build ((char *) NULL, &icnt, &expr1,
4947                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4948                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4949
4950               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4951                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4952                                           (breg == 0
4953                                            ? mips_opts.warn_about_macros
4954                                            : 0)),
4955                             offset_expr.X_add_symbol, 0, NULL);
4956             }
4957           else
4958             {
4959               int adj, dreg;
4960
4961               /* If we are going to add in a base register, and the
4962                  target register and the base register are the same,
4963                  then we are using AT as a temporary register.  Since
4964                  we want to load the constant into AT, we add our
4965                  current AT (from the global offset table) and the
4966                  register into the register now, and pretend we were
4967                  not using a base register.  */
4968               if (breg != treg)
4969                 {
4970                   adj = 0;
4971                   dreg = tempreg;
4972                 }
4973               else
4974                 {
4975                   assert (tempreg == AT);
4976                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4977                                "nop", "");
4978                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4979                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4980                                "d,v,t", treg, AT, breg);
4981                   dreg = treg;
4982                   adj = 8;
4983                 }
4984
4985               /* Set mips_optimize around the lui instruction to avoid
4986                  inserting an unnecessary nop after the lw.  */
4987               hold_mips_optimize = mips_optimize;
4988               mips_optimize = 2;
4989               macro_build_lui (NULL, &icnt, &expr1, AT);
4990               mips_optimize = hold_mips_optimize;
4991
4992               macro_build ((char *) NULL, &icnt, &expr1,
4993                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4994                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4995               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4996                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4997                            "d,v,t", dreg, dreg, AT);
4998
4999               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5000                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5001                                           8 + gpdel, 0,
5002                                           (breg == 0
5003                                            ? mips_opts.warn_about_macros
5004                                            : 0)),
5005                             offset_expr.X_add_symbol, 0, NULL);
5006
5007               used_at = 1;
5008             }
5009
5010           if (gpdel > 0)
5011             {
5012               /* This is needed because this instruction uses $gp, but
5013                  the first instruction on the main stream does not.  */
5014               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5015               p += 4;
5016             }
5017           macro_build (p, &icnt, &offset_expr,
5018                        dbl ? "ld" : "lw",
5019                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5020           p += 4;
5021           if (expr1.X_add_number >= -0x8000
5022               && expr1.X_add_number < 0x8000)
5023             {
5024               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5025               p += 4;
5026               macro_build (p, &icnt, &expr1,
5027                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5028                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5029               /* FIXME: If add_number is 0, and there was no base
5030                  register, the external symbol case ended with a load,
5031                  so if the symbol turns out to not be external, and
5032                  the next instruction uses tempreg, an unnecessary nop
5033                  will be inserted.  */
5034             }
5035           else
5036             {
5037               if (breg == treg)
5038                 {
5039                   /* We must add in the base register now, as in the
5040                      external symbol case.  */
5041                   assert (tempreg == AT);
5042                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5043                   p += 4;
5044                   macro_build (p, &icnt, (expressionS *) NULL,
5045                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5046                                "d,v,t", treg, AT, breg);
5047                   p += 4;
5048                   tempreg = treg;
5049                   /* We set breg to 0 because we have arranged to add
5050                      it in in both cases.  */
5051                   breg = 0;
5052                 }
5053
5054               macro_build_lui (p, &icnt, &expr1, AT);
5055               p += 4;
5056               macro_build (p, &icnt, &expr1,
5057                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5058                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5059               p += 4;
5060               macro_build (p, &icnt, (expressionS *) NULL,
5061                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5062                            "d,v,t", tempreg, tempreg, AT);
5063               p += 4;
5064             }
5065         }
5066       else if (mips_pic == EMBEDDED_PIC)
5067         {
5068           /* We use
5069                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5070              */
5071           macro_build ((char *) NULL, &icnt, &offset_expr,
5072                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5073                        "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
5074         }
5075       else
5076         abort ();
5077
5078       if (breg != 0)
5079         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5080                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5081                      "d,v,t", treg, tempreg, breg);
5082
5083       if (! used_at)
5084         return;
5085
5086       break;
5087
5088     case M_J_A:
5089       /* The j instruction may not be used in PIC code, since it
5090          requires an absolute address.  We convert it to a b
5091          instruction.  */
5092       if (mips_pic == NO_PIC)
5093         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5094       else
5095         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5096       return;
5097
5098       /* The jal instructions must be handled as macros because when
5099          generating PIC code they expand to multi-instruction
5100          sequences.  Normally they are simple instructions.  */
5101     case M_JAL_1:
5102       dreg = RA;
5103       /* Fall through.  */
5104     case M_JAL_2:
5105       if (mips_pic == NO_PIC
5106           || mips_pic == EMBEDDED_PIC)
5107         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5108                      "d,s", dreg, sreg);
5109       else if (mips_pic == SVR4_PIC)
5110         {
5111           if (sreg != PIC_CALL_REG)
5112             as_warn (_("MIPS PIC call to register other than $25"));
5113
5114           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5115                        "d,s", dreg, sreg);
5116           if (! HAVE_NEWABI)
5117             {
5118               if (mips_cprestore_offset < 0)
5119                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5120               else
5121                 {
5122                   if (! mips_frame_reg_valid)
5123                     {
5124                       as_warn (_("No .frame pseudo-op used in PIC code"));
5125                       /* Quiet this warning.  */
5126                       mips_frame_reg_valid = 1;
5127                     }
5128                   if (! mips_cprestore_valid)
5129                     {
5130                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5131                       /* Quiet this warning.  */
5132                       mips_cprestore_valid = 1;
5133                     }
5134                   expr1.X_add_number = mips_cprestore_offset;
5135                   macro_build ((char *) NULL, &icnt, &expr1,
5136                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5137                                GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5138                 }
5139             }
5140         }
5141       else
5142         abort ();
5143
5144       return;
5145
5146     case M_JAL_A:
5147       if (mips_pic == NO_PIC)
5148         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5149       else if (mips_pic == SVR4_PIC)
5150         {
5151           /* If this is a reference to an external symbol, and we are
5152              using a small GOT, we want
5153                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5154                nop
5155                jalr     $25
5156                nop
5157                lw       $gp,cprestore($sp)
5158              The cprestore value is set using the .cprestore
5159              pseudo-op.  If we are using a big GOT, we want
5160                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5161                addu     $25,$25,$gp
5162                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5163                nop
5164                jalr     $25
5165                nop
5166                lw       $gp,cprestore($sp)
5167              If the symbol is not external, we want
5168                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5169                nop
5170                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5171                jalr     $25
5172                nop
5173                lw $gp,cprestore($sp) */
5174           frag_grow (40);
5175           if (! mips_big_got)
5176             {
5177               macro_build ((char *) NULL, &icnt, &offset_expr,
5178                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5179                            "t,o(b)", PIC_CALL_REG,
5180                            (int) BFD_RELOC_MIPS_CALL16, GP);
5181               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5182                            "nop", "");
5183               p = frag_var (rs_machine_dependent, 4, 0,
5184                             RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5185                             offset_expr.X_add_symbol, 0, NULL);
5186             }
5187           else
5188             {
5189               int gpdel;
5190
5191               if (reg_needs_delay (GP))
5192                 gpdel = 4;
5193               else
5194                 gpdel = 0;
5195               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5196                            PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5197               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5198                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5199                            "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
5200               macro_build ((char *) NULL, &icnt, &offset_expr,
5201                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5202                            "t,o(b)", PIC_CALL_REG,
5203                            (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5204               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5205                            "nop", "");
5206               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5207                             RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5208                                           0, 0),
5209                             offset_expr.X_add_symbol, 0, NULL);
5210               if (gpdel > 0)
5211                 {
5212                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5213                   p += 4;
5214                 }
5215               macro_build (p, &icnt, &offset_expr,
5216                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5217                            "t,o(b)", PIC_CALL_REG,
5218                            (int) BFD_RELOC_MIPS_GOT16, GP);
5219               p += 4;
5220               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5221               p += 4;
5222             }
5223           macro_build (p, &icnt, &offset_expr,
5224                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5225                        "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5226                        (int) BFD_RELOC_LO16);
5227           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5228                        "jalr", "s", PIC_CALL_REG);
5229           if (! HAVE_NEWABI)
5230             {
5231               if (mips_cprestore_offset < 0)
5232                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5233               else
5234                 {
5235                   if (! mips_frame_reg_valid)
5236                     {
5237                       as_warn (_("No .frame pseudo-op used in PIC code"));
5238                       /* Quiet this warning.  */
5239                       mips_frame_reg_valid = 1;
5240                     }
5241                   if (! mips_cprestore_valid)
5242                     {
5243                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5244                       /* Quiet this warning.  */
5245                       mips_cprestore_valid = 1;
5246                     }
5247                   if (mips_opts.noreorder)
5248                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5249                              "nop", "");
5250                   expr1.X_add_number = mips_cprestore_offset;
5251                   macro_build ((char *) NULL, &icnt, &expr1,
5252                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5253                                GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5254                 }
5255             }
5256         }
5257       else if (mips_pic == EMBEDDED_PIC)
5258         {
5259           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5260           /* The linker may expand the call to a longer sequence which
5261              uses $at, so we must break rather than return.  */
5262           break;
5263         }
5264       else
5265         abort ();
5266
5267       return;
5268
5269     case M_LB_AB:
5270       s = "lb";
5271       goto ld;
5272     case M_LBU_AB:
5273       s = "lbu";
5274       goto ld;
5275     case M_LH_AB:
5276       s = "lh";
5277       goto ld;
5278     case M_LHU_AB:
5279       s = "lhu";
5280       goto ld;
5281     case M_LW_AB:
5282       s = "lw";
5283       goto ld;
5284     case M_LWC0_AB:
5285       s = "lwc0";
5286       /* Itbl support may require additional care here.  */
5287       coproc = 1;
5288       goto ld;
5289     case M_LWC1_AB:
5290       s = "lwc1";
5291       /* Itbl support may require additional care here.  */
5292       coproc = 1;
5293       goto ld;
5294     case M_LWC2_AB:
5295       s = "lwc2";
5296       /* Itbl support may require additional care here.  */
5297       coproc = 1;
5298       goto ld;
5299     case M_LWC3_AB:
5300       s = "lwc3";
5301       /* Itbl support may require additional care here.  */
5302       coproc = 1;
5303       goto ld;
5304     case M_LWL_AB:
5305       s = "lwl";
5306       lr = 1;
5307       goto ld;
5308     case M_LWR_AB:
5309       s = "lwr";
5310       lr = 1;
5311       goto ld;
5312     case M_LDC1_AB:
5313       if (mips_arch == CPU_R4650)
5314         {
5315           as_bad (_("opcode not supported on this processor"));
5316           return;
5317         }
5318       s = "ldc1";
5319       /* Itbl support may require additional care here.  */
5320       coproc = 1;
5321       goto ld;
5322     case M_LDC2_AB:
5323       s = "ldc2";
5324       /* Itbl support may require additional care here.  */
5325       coproc = 1;
5326       goto ld;
5327     case M_LDC3_AB:
5328       s = "ldc3";
5329       /* Itbl support may require additional care here.  */
5330       coproc = 1;
5331       goto ld;
5332     case M_LDL_AB:
5333       s = "ldl";
5334       lr = 1;
5335       goto ld;
5336     case M_LDR_AB:
5337       s = "ldr";
5338       lr = 1;
5339       goto ld;
5340     case M_LL_AB:
5341       s = "ll";
5342       goto ld;
5343     case M_LLD_AB:
5344       s = "lld";
5345       goto ld;
5346     case M_LWU_AB:
5347       s = "lwu";
5348     ld:
5349       if (breg == treg || coproc || lr)
5350         {
5351           tempreg = AT;
5352           used_at = 1;
5353         }
5354       else
5355         {
5356           tempreg = treg;
5357           used_at = 0;
5358         }
5359       goto ld_st;
5360     case M_SB_AB:
5361       s = "sb";
5362       goto st;
5363     case M_SH_AB:
5364       s = "sh";
5365       goto st;
5366     case M_SW_AB:
5367       s = "sw";
5368       goto st;
5369     case M_SWC0_AB:
5370       s = "swc0";
5371       /* Itbl support may require additional care here.  */
5372       coproc = 1;
5373       goto st;
5374     case M_SWC1_AB:
5375       s = "swc1";
5376       /* Itbl support may require additional care here.  */
5377       coproc = 1;
5378       goto st;
5379     case M_SWC2_AB:
5380       s = "swc2";
5381       /* Itbl support may require additional care here.  */
5382       coproc = 1;
5383       goto st;
5384     case M_SWC3_AB:
5385       s = "swc3";
5386       /* Itbl support may require additional care here.  */
5387       coproc = 1;
5388       goto st;
5389     case M_SWL_AB:
5390       s = "swl";
5391       goto st;
5392     case M_SWR_AB:
5393       s = "swr";
5394       goto st;
5395     case M_SC_AB:
5396       s = "sc";
5397       goto st;
5398     case M_SCD_AB:
5399       s = "scd";
5400       goto st;
5401     case M_SDC1_AB:
5402       if (mips_arch == CPU_R4650)
5403         {
5404           as_bad (_("opcode not supported on this processor"));
5405           return;
5406         }
5407       s = "sdc1";
5408       coproc = 1;
5409       /* Itbl support may require additional care here.  */
5410       goto st;
5411     case M_SDC2_AB:
5412       s = "sdc2";
5413       /* Itbl support may require additional care here.  */
5414       coproc = 1;
5415       goto st;
5416     case M_SDC3_AB:
5417       s = "sdc3";
5418       /* Itbl support may require additional care here.  */
5419       coproc = 1;
5420       goto st;
5421     case M_SDL_AB:
5422       s = "sdl";
5423       goto st;
5424     case M_SDR_AB:
5425       s = "sdr";
5426     st:
5427       tempreg = AT;
5428       used_at = 1;
5429     ld_st:
5430       /* Itbl support may require additional care here.  */
5431       if (mask == M_LWC1_AB
5432           || mask == M_SWC1_AB
5433           || mask == M_LDC1_AB
5434           || mask == M_SDC1_AB
5435           || mask == M_L_DAB
5436           || mask == M_S_DAB)
5437         fmt = "T,o(b)";
5438       else if (coproc)
5439         fmt = "E,o(b)";
5440       else
5441         fmt = "t,o(b)";
5442
5443       /* For embedded PIC, we allow loads where the offset is calculated
5444          by subtracting a symbol in the current segment from an unknown
5445          symbol, relative to a base register, e.g.:
5446                 <op>    $treg, <sym>-<localsym>($breg)
5447          This is used by the compiler for switch statements.  */
5448       if (mips_pic == EMBEDDED_PIC
5449           && offset_expr.X_op == O_subtract
5450           && (symbol_constant_p (offset_expr.X_op_symbol)
5451               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5452               : (symbol_equated_p (offset_expr.X_op_symbol)
5453                  && (S_GET_SEGMENT
5454                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5455                       ->X_add_symbol)
5456                      == now_seg)))
5457           && breg != 0
5458           && (offset_expr.X_add_number == 0
5459               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5460         {
5461           /* For this case, we output the instructions:
5462                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5463                 addiu   $tempreg,$tempreg,$breg
5464                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5465              If the relocation would fit entirely in 16 bits, it would be
5466              nice to emit:
5467                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5468              instead, but that seems quite difficult.  */
5469           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5470                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5471           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5472                        ((bfd_arch_bits_per_address (stdoutput) == 32
5473                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5474                         ? "addu" : "daddu"),
5475                        "d,v,t", tempreg, tempreg, breg);
5476           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5477                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5478           if (! used_at)
5479             return;
5480           break;
5481         }
5482
5483       if (offset_expr.X_op != O_constant
5484           && offset_expr.X_op != O_symbol)
5485         {
5486           as_bad (_("expression too complex"));
5487           offset_expr.X_op = O_constant;
5488         }
5489
5490       /* A constant expression in PIC code can be handled just as it
5491          is in non PIC code.  */
5492       if (mips_pic == NO_PIC
5493           || offset_expr.X_op == O_constant)
5494         {
5495           /* If this is a reference to a GP relative symbol, and there
5496              is no base register, we want
5497                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5498              Otherwise, if there is no base register, we want
5499                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5500                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5501              If we have a constant, we need two instructions anyhow,
5502              so we always use the latter form.
5503
5504              If we have a base register, and this is a reference to a
5505              GP relative symbol, we want
5506                addu     $tempreg,$breg,$gp
5507                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5508              Otherwise we want
5509                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5510                addu     $tempreg,$tempreg,$breg
5511                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5512              With a constant we always use the latter case.
5513
5514              With 64bit address space and no base register and $at usable,
5515              we want
5516                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5517                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5518                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5519                dsll32   $tempreg,0
5520                daddu    $tempreg,$at
5521                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5522              If we have a base register, we want
5523                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5524                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5525                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5526                daddu    $at,$breg
5527                dsll32   $tempreg,0
5528                daddu    $tempreg,$at
5529                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5530
5531              Without $at we can't generate the optimal path for superscalar
5532              processors here since this would require two temporary registers.
5533                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5534                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5535                dsll     $tempreg,16
5536                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5537                dsll     $tempreg,16
5538                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5539              If we have a base register, we want
5540                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5541                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5542                dsll     $tempreg,16
5543                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5544                dsll     $tempreg,16
5545                daddu    $tempreg,$tempreg,$breg
5546                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5547            */
5548           if (HAVE_64BIT_ADDRESSES)
5549             {
5550               p = NULL;
5551
5552               /* We don't do GP optimization for now because RELAX_ENCODE can't
5553                  hold the data for such large chunks.  */
5554
5555               if (used_at == 0)
5556                 {
5557                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5558                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5559                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5560                                AT, (int) BFD_RELOC_HI16_S);
5561                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5562                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5563                   if (breg != 0)
5564                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5565                                  "d,v,t", AT, AT, breg);
5566                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5567                                "d,w,<", tempreg, tempreg, 0);
5568                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5569                                "d,v,t", tempreg, tempreg, AT);
5570                   macro_build (p, &icnt, &offset_expr, s,
5571                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5572                   used_at = 1;
5573                 }
5574               else
5575                 {
5576                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5577                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5578                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5579                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5580                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5581                                "d,w,<", tempreg, tempreg, 16);
5582                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5583                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5584                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5585                                "d,w,<", tempreg, tempreg, 16);
5586                   if (breg != 0)
5587                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5588                                  "d,v,t", tempreg, tempreg, breg);
5589                   macro_build (p, &icnt, &offset_expr, s,
5590                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5591                 }
5592
5593               return;
5594             }
5595
5596           if (breg == 0)
5597             {
5598               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5599                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5600                 p = NULL;
5601               else
5602                 {
5603                   frag_grow (20);
5604                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5605                                treg, (int) BFD_RELOC_GPREL16, GP);
5606                   p = frag_var (rs_machine_dependent, 8, 0,
5607                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5608                                               (mips_opts.warn_about_macros
5609                                                || (used_at
5610                                                    && mips_opts.noat))),
5611                                 offset_expr.X_add_symbol, 0, NULL);
5612                   used_at = 0;
5613                 }
5614               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5615               if (p != NULL)
5616                 p += 4;
5617               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5618                            (int) BFD_RELOC_LO16, tempreg);
5619             }
5620           else
5621             {
5622               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5623                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5624                 p = NULL;
5625               else
5626                 {
5627                   frag_grow (28);
5628                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5629                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5630                                "d,v,t", tempreg, breg, GP);
5631                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5632                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5633                   p = frag_var (rs_machine_dependent, 12, 0,
5634                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5635                                 offset_expr.X_add_symbol, 0, NULL);
5636                 }
5637               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5638               if (p != NULL)
5639                 p += 4;
5640               macro_build (p, &icnt, (expressionS *) NULL,
5641                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5642                            "d,v,t", tempreg, tempreg, breg);
5643               if (p != NULL)
5644                 p += 4;
5645               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5646                            (int) BFD_RELOC_LO16, tempreg);
5647             }
5648         }
5649       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5650         {
5651           /* If this is a reference to an external symbol, we want
5652                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5653                nop
5654                <op>     $treg,0($tempreg)
5655              Otherwise we want
5656                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5657                nop
5658                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5659                <op>     $treg,0($tempreg)
5660              If there is a base register, we add it to $tempreg before
5661              the <op>.  If there is a constant, we stick it in the
5662              <op> instruction.  We don't handle constants larger than
5663              16 bits, because we have no way to load the upper 16 bits
5664              (actually, we could handle them for the subset of cases
5665              in which we are not using $at).  */
5666           assert (offset_expr.X_op == O_symbol);
5667           expr1.X_add_number = offset_expr.X_add_number;
5668           offset_expr.X_add_number = 0;
5669           if (expr1.X_add_number < -0x8000
5670               || expr1.X_add_number >= 0x8000)
5671             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5672           frag_grow (20);
5673           macro_build ((char *) NULL, &icnt, &offset_expr,
5674                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5675                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5676           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5677           p = frag_var (rs_machine_dependent, 4, 0,
5678                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5679                         offset_expr.X_add_symbol, 0, NULL);
5680           macro_build (p, &icnt, &offset_expr,
5681                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5682                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
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       else if (mips_pic == SVR4_PIC)
5691         {
5692           int gpdel;
5693
5694           /* If this is a reference to an external symbol, we want
5695                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5696                addu     $tempreg,$tempreg,$gp
5697                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5698                <op>     $treg,0($tempreg)
5699              Otherwise we want
5700                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5701                nop
5702                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5703                <op>     $treg,0($tempreg)
5704              If there is a base register, we add it to $tempreg before
5705              the <op>.  If there is a constant, we stick it in the
5706              <op> instruction.  We don't handle constants larger than
5707              16 bits, because we have no way to load the upper 16 bits
5708              (actually, we could handle them for the subset of cases
5709              in which we are not using $at).  */
5710           assert (offset_expr.X_op == O_symbol);
5711           expr1.X_add_number = offset_expr.X_add_number;
5712           offset_expr.X_add_number = 0;
5713           if (expr1.X_add_number < -0x8000
5714               || expr1.X_add_number >= 0x8000)
5715             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5716           if (reg_needs_delay (GP))
5717             gpdel = 4;
5718           else
5719             gpdel = 0;
5720           frag_grow (36);
5721           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5722                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5723           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5724                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5725                        "d,v,t", tempreg, tempreg, GP);
5726           macro_build ((char *) NULL, &icnt, &offset_expr,
5727                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5728                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5729                        tempreg);
5730           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5731                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5732                         offset_expr.X_add_symbol, 0, NULL);
5733           if (gpdel > 0)
5734             {
5735               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5736               p += 4;
5737             }
5738           macro_build (p, &icnt, &offset_expr,
5739                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5740                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5741           p += 4;
5742           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5743           p += 4;
5744           macro_build (p, &icnt, &offset_expr,
5745                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5746                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5747           if (breg != 0)
5748             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5749                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5750                          "d,v,t", tempreg, tempreg, breg);
5751           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5752                        (int) BFD_RELOC_LO16, tempreg);
5753         }
5754       else if (mips_pic == EMBEDDED_PIC)
5755         {
5756           /* If there is no base register, we want
5757                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5758              If there is a base register, we want
5759                addu     $tempreg,$breg,$gp
5760                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5761              */
5762           assert (offset_expr.X_op == O_symbol);
5763           if (breg == 0)
5764             {
5765               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5766                            treg, (int) BFD_RELOC_GPREL16, GP);
5767               used_at = 0;
5768             }
5769           else
5770             {
5771               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5772                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5773                            "d,v,t", tempreg, breg, GP);
5774               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5775                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5776             }
5777         }
5778       else
5779         abort ();
5780
5781       if (! used_at)
5782         return;
5783
5784       break;
5785
5786     case M_LI:
5787     case M_LI_S:
5788       load_register (&icnt, treg, &imm_expr, 0);
5789       return;
5790
5791     case M_DLI:
5792       load_register (&icnt, treg, &imm_expr, 1);
5793       return;
5794
5795     case M_LI_SS:
5796       if (imm_expr.X_op == O_constant)
5797         {
5798           load_register (&icnt, AT, &imm_expr, 0);
5799           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5800                        "mtc1", "t,G", AT, treg);
5801           break;
5802         }
5803       else
5804         {
5805           assert (offset_expr.X_op == O_symbol
5806                   && strcmp (segment_name (S_GET_SEGMENT
5807                                            (offset_expr.X_add_symbol)),
5808                              ".lit4") == 0
5809                   && offset_expr.X_add_number == 0);
5810           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5811                        treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5812           return;
5813         }
5814
5815     case M_LI_D:
5816       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5817          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5818          order 32 bits of the value and the low order 32 bits are either
5819          zero or in OFFSET_EXPR.  */
5820       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5821         {
5822           if (HAVE_64BIT_GPRS)
5823             load_register (&icnt, treg, &imm_expr, 1);
5824           else
5825             {
5826               int hreg, lreg;
5827
5828               if (target_big_endian)
5829                 {
5830                   hreg = treg;
5831                   lreg = treg + 1;
5832                 }
5833               else
5834                 {
5835                   hreg = treg + 1;
5836                   lreg = treg;
5837                 }
5838
5839               if (hreg <= 31)
5840                 load_register (&icnt, hreg, &imm_expr, 0);
5841               if (lreg <= 31)
5842                 {
5843                   if (offset_expr.X_op == O_absent)
5844                     move_register (&icnt, lreg, 0);
5845                   else
5846                     {
5847                       assert (offset_expr.X_op == O_constant);
5848                       load_register (&icnt, lreg, &offset_expr, 0);
5849                     }
5850                 }
5851             }
5852           return;
5853         }
5854
5855       /* We know that sym is in the .rdata section.  First we get the
5856          upper 16 bits of the address.  */
5857       if (mips_pic == NO_PIC)
5858         {
5859           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5860         }
5861       else if (mips_pic == SVR4_PIC)
5862         {
5863           macro_build ((char *) NULL, &icnt, &offset_expr,
5864                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5865                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5866         }
5867       else if (mips_pic == EMBEDDED_PIC)
5868         {
5869           /* For embedded PIC we pick up the entire address off $gp in
5870              a single instruction.  */
5871           macro_build ((char *) NULL, &icnt, &offset_expr,
5872                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5873                        "t,r,j", AT, GP, (int) BFD_RELOC_GPREL16);
5874           offset_expr.X_op = O_constant;
5875           offset_expr.X_add_number = 0;
5876         }
5877       else
5878         abort ();
5879
5880       /* Now we load the register(s).  */
5881       if (HAVE_64BIT_GPRS)
5882         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5883                      treg, (int) BFD_RELOC_LO16, AT);
5884       else
5885         {
5886           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5887                        treg, (int) BFD_RELOC_LO16, AT);
5888           if (treg != 31)
5889             {
5890               /* FIXME: How in the world do we deal with the possible
5891                  overflow here?  */
5892               offset_expr.X_add_number += 4;
5893               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5894                            treg + 1, (int) BFD_RELOC_LO16, AT);
5895             }
5896         }
5897
5898       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5899          does not become a variant frag.  */
5900       frag_wane (frag_now);
5901       frag_new (0);
5902
5903       break;
5904
5905     case M_LI_DD:
5906       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5907          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5908          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5909          the value and the low order 32 bits are either zero or in
5910          OFFSET_EXPR.  */
5911       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5912         {
5913           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5914           if (HAVE_64BIT_FPRS)
5915             {
5916               assert (HAVE_64BIT_GPRS);
5917               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5918                            "dmtc1", "t,S", AT, treg);
5919             }
5920           else
5921             {
5922               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5923                            "mtc1", "t,G", AT, treg + 1);
5924               if (offset_expr.X_op == O_absent)
5925                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5926                              "mtc1", "t,G", 0, treg);
5927               else
5928                 {
5929                   assert (offset_expr.X_op == O_constant);
5930                   load_register (&icnt, AT, &offset_expr, 0);
5931                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5932                                "mtc1", "t,G", AT, treg);
5933                 }
5934             }
5935           break;
5936         }
5937
5938       assert (offset_expr.X_op == O_symbol
5939               && offset_expr.X_add_number == 0);
5940       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5941       if (strcmp (s, ".lit8") == 0)
5942         {
5943           if (mips_opts.isa != ISA_MIPS1)
5944             {
5945               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5946                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5947               return;
5948             }
5949           breg = GP;
5950           r = BFD_RELOC_MIPS_LITERAL;
5951           goto dob;
5952         }
5953       else
5954         {
5955           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5956           if (mips_pic == SVR4_PIC)
5957             macro_build ((char *) NULL, &icnt, &offset_expr,
5958                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5959                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5960           else
5961             {
5962               /* FIXME: This won't work for a 64 bit address.  */
5963               macro_build_lui (NULL, &icnt, &offset_expr, AT);
5964             }
5965
5966           if (mips_opts.isa != ISA_MIPS1)
5967             {
5968               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5969                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5970
5971               /* To avoid confusion in tc_gen_reloc, we must ensure
5972                  that this does not become a variant frag.  */
5973               frag_wane (frag_now);
5974               frag_new (0);
5975
5976               break;
5977             }
5978           breg = AT;
5979           r = BFD_RELOC_LO16;
5980           goto dob;
5981         }
5982
5983     case M_L_DOB:
5984       if (mips_arch == CPU_R4650)
5985         {
5986           as_bad (_("opcode not supported on this processor"));
5987           return;
5988         }
5989       /* Even on a big endian machine $fn comes before $fn+1.  We have
5990          to adjust when loading from memory.  */
5991       r = BFD_RELOC_LO16;
5992     dob:
5993       assert (mips_opts.isa == ISA_MIPS1);
5994       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5995                    target_big_endian ? treg + 1 : treg,
5996                    (int) r, breg);
5997       /* FIXME: A possible overflow which I don't know how to deal
5998          with.  */
5999       offset_expr.X_add_number += 4;
6000       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6001                    target_big_endian ? treg : treg + 1,
6002                    (int) r, breg);
6003
6004       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6005          does not become a variant frag.  */
6006       frag_wane (frag_now);
6007       frag_new (0);
6008
6009       if (breg != AT)
6010         return;
6011       break;
6012
6013     case M_L_DAB:
6014       /*
6015        * The MIPS assembler seems to check for X_add_number not
6016        * being double aligned and generating:
6017        *        lui     at,%hi(foo+1)
6018        *        addu    at,at,v1
6019        *        addiu   at,at,%lo(foo+1)
6020        *        lwc1    f2,0(at)
6021        *        lwc1    f3,4(at)
6022        * But, the resulting address is the same after relocation so why
6023        * generate the extra instruction?
6024        */
6025       if (mips_arch == CPU_R4650)
6026         {
6027           as_bad (_("opcode not supported on this processor"));
6028           return;
6029         }
6030       /* Itbl support may require additional care here.  */
6031       coproc = 1;
6032       if (mips_opts.isa != ISA_MIPS1)
6033         {
6034           s = "ldc1";
6035           goto ld;
6036         }
6037
6038       s = "lwc1";
6039       fmt = "T,o(b)";
6040       goto ldd_std;
6041
6042     case M_S_DAB:
6043       if (mips_arch == CPU_R4650)
6044         {
6045           as_bad (_("opcode not supported on this processor"));
6046           return;
6047         }
6048
6049       if (mips_opts.isa != ISA_MIPS1)
6050         {
6051           s = "sdc1";
6052           goto st;
6053         }
6054
6055       s = "swc1";
6056       fmt = "T,o(b)";
6057       /* Itbl support may require additional care here.  */
6058       coproc = 1;
6059       goto ldd_std;
6060
6061     case M_LD_AB:
6062       if (HAVE_64BIT_GPRS)
6063         {
6064           s = "ld";
6065           goto ld;
6066         }
6067
6068       s = "lw";
6069       fmt = "t,o(b)";
6070       goto ldd_std;
6071
6072     case M_SD_AB:
6073       if (HAVE_64BIT_GPRS)
6074         {
6075           s = "sd";
6076           goto st;
6077         }
6078
6079       s = "sw";
6080       fmt = "t,o(b)";
6081
6082     ldd_std:
6083       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6084          loads for the case of doing a pair of loads to simulate an 'ld'.
6085          This is not currently done by the compiler, and assembly coders
6086          writing embedded-pic code can cope.  */
6087
6088       if (offset_expr.X_op != O_symbol
6089           && offset_expr.X_op != O_constant)
6090         {
6091           as_bad (_("expression too complex"));
6092           offset_expr.X_op = O_constant;
6093         }
6094
6095       /* Even on a big endian machine $fn comes before $fn+1.  We have
6096          to adjust when loading from memory.  We set coproc if we must
6097          load $fn+1 first.  */
6098       /* Itbl support may require additional care here.  */
6099       if (! target_big_endian)
6100         coproc = 0;
6101
6102       if (mips_pic == NO_PIC
6103           || offset_expr.X_op == O_constant)
6104         {
6105           /* If this is a reference to a GP relative symbol, we want
6106                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6107                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6108              If we have a base register, we use this
6109                addu     $at,$breg,$gp
6110                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6111                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6112              If this is not a GP relative symbol, we want
6113                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6114                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6115                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6116              If there is a base register, we add it to $at after the
6117              lui instruction.  If there is a constant, we always use
6118              the last case.  */
6119           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6120               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6121             {
6122               p = NULL;
6123               used_at = 1;
6124             }
6125           else
6126             {
6127               int off;
6128
6129               if (breg == 0)
6130                 {
6131                   frag_grow (28);
6132                   tempreg = GP;
6133                   off = 0;
6134                   used_at = 0;
6135                 }
6136               else
6137                 {
6138                   frag_grow (36);
6139                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6140                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6141                                "d,v,t", AT, breg, GP);
6142                   tempreg = AT;
6143                   off = 4;
6144                   used_at = 1;
6145                 }
6146
6147               /* Itbl support may require additional care here.  */
6148               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6149                            coproc ? treg + 1 : treg,
6150                            (int) BFD_RELOC_GPREL16, tempreg);
6151               offset_expr.X_add_number += 4;
6152
6153               /* Set mips_optimize to 2 to avoid inserting an
6154                  undesired nop.  */
6155               hold_mips_optimize = mips_optimize;
6156               mips_optimize = 2;
6157               /* Itbl support may require additional care here.  */
6158               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6159                            coproc ? treg : treg + 1,
6160                            (int) BFD_RELOC_GPREL16, tempreg);
6161               mips_optimize = hold_mips_optimize;
6162
6163               p = frag_var (rs_machine_dependent, 12 + off, 0,
6164                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6165                                           used_at && mips_opts.noat),
6166                             offset_expr.X_add_symbol, 0, NULL);
6167
6168               /* We just generated two relocs.  When tc_gen_reloc
6169                  handles this case, it will skip the first reloc and
6170                  handle the second.  The second reloc already has an
6171                  extra addend of 4, which we added above.  We must
6172                  subtract it out, and then subtract another 4 to make
6173                  the first reloc come out right.  The second reloc
6174                  will come out right because we are going to add 4 to
6175                  offset_expr when we build its instruction below.
6176
6177                  If we have a symbol, then we don't want to include
6178                  the offset, because it will wind up being included
6179                  when we generate the reloc.  */
6180
6181               if (offset_expr.X_op == O_constant)
6182                 offset_expr.X_add_number -= 8;
6183               else
6184                 {
6185                   offset_expr.X_add_number = -4;
6186                   offset_expr.X_op = O_constant;
6187                 }
6188             }
6189           macro_build_lui (p, &icnt, &offset_expr, AT);
6190           if (p != NULL)
6191             p += 4;
6192           if (breg != 0)
6193             {
6194               macro_build (p, &icnt, (expressionS *) NULL,
6195                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6196                            "d,v,t", AT, breg, AT);
6197               if (p != NULL)
6198                 p += 4;
6199             }
6200           /* Itbl support may require additional care here.  */
6201           macro_build (p, &icnt, &offset_expr, s, fmt,
6202                        coproc ? treg + 1 : treg,
6203                        (int) BFD_RELOC_LO16, AT);
6204           if (p != NULL)
6205             p += 4;
6206           /* FIXME: How do we handle overflow here?  */
6207           offset_expr.X_add_number += 4;
6208           /* Itbl support may require additional care here.  */
6209           macro_build (p, &icnt, &offset_expr, s, fmt,
6210                        coproc ? treg : treg + 1,
6211                        (int) BFD_RELOC_LO16, AT);
6212         }
6213       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6214         {
6215           int off;
6216
6217           /* If this is a reference to an external symbol, we want
6218                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6219                nop
6220                <op>     $treg,0($at)
6221                <op>     $treg+1,4($at)
6222              Otherwise we want
6223                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6224                nop
6225                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6226                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6227              If there is a base register we add it to $at before the
6228              lwc1 instructions.  If there is a constant we include it
6229              in the lwc1 instructions.  */
6230           used_at = 1;
6231           expr1.X_add_number = offset_expr.X_add_number;
6232           offset_expr.X_add_number = 0;
6233           if (expr1.X_add_number < -0x8000
6234               || expr1.X_add_number >= 0x8000 - 4)
6235             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6236           if (breg == 0)
6237             off = 0;
6238           else
6239             off = 4;
6240           frag_grow (24 + off);
6241           macro_build ((char *) NULL, &icnt, &offset_expr,
6242                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6243                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6244           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6245           if (breg != 0)
6246             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6247                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6248                          "d,v,t", AT, breg, AT);
6249           /* Itbl support may require additional care here.  */
6250           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6251                        coproc ? treg + 1 : treg,
6252                        (int) BFD_RELOC_LO16, AT);
6253           expr1.X_add_number += 4;
6254
6255           /* Set mips_optimize to 2 to avoid inserting an undesired
6256              nop.  */
6257           hold_mips_optimize = mips_optimize;
6258           mips_optimize = 2;
6259           /* Itbl support may require additional care here.  */
6260           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6261                        coproc ? treg : treg + 1,
6262                        (int) BFD_RELOC_LO16, AT);
6263           mips_optimize = hold_mips_optimize;
6264
6265           (void) frag_var (rs_machine_dependent, 0, 0,
6266                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6267                            offset_expr.X_add_symbol, 0, NULL);
6268         }
6269       else if (mips_pic == SVR4_PIC)
6270         {
6271           int gpdel, off;
6272
6273           /* If this is a reference to an external symbol, we want
6274                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6275                addu     $at,$at,$gp
6276                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6277                nop
6278                <op>     $treg,0($at)
6279                <op>     $treg+1,4($at)
6280              Otherwise we want
6281                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6282                nop
6283                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6284                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6285              If there is a base register we add it to $at before the
6286              lwc1 instructions.  If there is a constant we include it
6287              in the lwc1 instructions.  */
6288           used_at = 1;
6289           expr1.X_add_number = offset_expr.X_add_number;
6290           offset_expr.X_add_number = 0;
6291           if (expr1.X_add_number < -0x8000
6292               || expr1.X_add_number >= 0x8000 - 4)
6293             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6294           if (reg_needs_delay (GP))
6295             gpdel = 4;
6296           else
6297             gpdel = 0;
6298           if (breg == 0)
6299             off = 0;
6300           else
6301             off = 4;
6302           frag_grow (56);
6303           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6304                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6305           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6306                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6307                        "d,v,t", AT, AT, GP);
6308           macro_build ((char *) NULL, &icnt, &offset_expr,
6309                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6310                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6311           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6312           if (breg != 0)
6313             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6314                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6315                          "d,v,t", AT, breg, AT);
6316           /* Itbl support may require additional care here.  */
6317           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6318                        coproc ? treg + 1 : treg,
6319                        (int) BFD_RELOC_LO16, AT);
6320           expr1.X_add_number += 4;
6321
6322           /* Set mips_optimize to 2 to avoid inserting an undesired
6323              nop.  */
6324           hold_mips_optimize = mips_optimize;
6325           mips_optimize = 2;
6326           /* Itbl support may require additional care here.  */
6327           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6328                        coproc ? treg : treg + 1,
6329                        (int) BFD_RELOC_LO16, AT);
6330           mips_optimize = hold_mips_optimize;
6331           expr1.X_add_number -= 4;
6332
6333           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6334                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6335                                       8 + gpdel + off, 1, 0),
6336                         offset_expr.X_add_symbol, 0, NULL);
6337           if (gpdel > 0)
6338             {
6339               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6340               p += 4;
6341             }
6342           macro_build (p, &icnt, &offset_expr,
6343                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6344                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6345           p += 4;
6346           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6347           p += 4;
6348           if (breg != 0)
6349             {
6350               macro_build (p, &icnt, (expressionS *) NULL,
6351                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6352                            "d,v,t", AT, breg, AT);
6353               p += 4;
6354             }
6355           /* Itbl support may require additional care here.  */
6356           macro_build (p, &icnt, &expr1, s, fmt,
6357                        coproc ? treg + 1 : treg,
6358                        (int) BFD_RELOC_LO16, AT);
6359           p += 4;
6360           expr1.X_add_number += 4;
6361
6362           /* Set mips_optimize to 2 to avoid inserting an undesired
6363              nop.  */
6364           hold_mips_optimize = mips_optimize;
6365           mips_optimize = 2;
6366           /* Itbl support may require additional care here.  */
6367           macro_build (p, &icnt, &expr1, s, fmt,
6368                        coproc ? treg : treg + 1,
6369                        (int) BFD_RELOC_LO16, AT);
6370           mips_optimize = hold_mips_optimize;
6371         }
6372       else if (mips_pic == EMBEDDED_PIC)
6373         {
6374           /* If there is no base register, we use
6375                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6376                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6377              If we have a base register, we use
6378                addu     $at,$breg,$gp
6379                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6380                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6381              */
6382           if (breg == 0)
6383             {
6384               tempreg = GP;
6385               used_at = 0;
6386             }
6387           else
6388             {
6389               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6390                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6391                            "d,v,t", AT, breg, GP);
6392               tempreg = AT;
6393               used_at = 1;
6394             }
6395
6396           /* Itbl support may require additional care here.  */
6397           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6398                        coproc ? treg + 1 : treg,
6399                        (int) BFD_RELOC_GPREL16, tempreg);
6400           offset_expr.X_add_number += 4;
6401           /* Itbl support may require additional care here.  */
6402           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6403                        coproc ? treg : treg + 1,
6404                        (int) BFD_RELOC_GPREL16, tempreg);
6405         }
6406       else
6407         abort ();
6408
6409       if (! used_at)
6410         return;
6411
6412       break;
6413
6414     case M_LD_OB:
6415       s = "lw";
6416       goto sd_ob;
6417     case M_SD_OB:
6418       s = "sw";
6419     sd_ob:
6420       assert (HAVE_32BIT_ADDRESSES);
6421       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6422                    (int) BFD_RELOC_LO16, breg);
6423       offset_expr.X_add_number += 4;
6424       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6425                    (int) BFD_RELOC_LO16, breg);
6426       return;
6427
6428    /* New code added to support COPZ instructions.
6429       This code builds table entries out of the macros in mip_opcodes.
6430       R4000 uses interlocks to handle coproc delays.
6431       Other chips (like the R3000) require nops to be inserted for delays.
6432
6433       FIXME: Currently, we require that the user handle delays.
6434       In order to fill delay slots for non-interlocked chips,
6435       we must have a way to specify delays based on the coprocessor.
6436       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6437       What are the side-effects of the cop instruction?
6438       What cache support might we have and what are its effects?
6439       Both coprocessor & memory require delays. how long???
6440       What registers are read/set/modified?
6441
6442       If an itbl is provided to interpret cop instructions,
6443       this knowledge can be encoded in the itbl spec.  */
6444
6445     case M_COP0:
6446       s = "c0";
6447       goto copz;
6448     case M_COP1:
6449       s = "c1";
6450       goto copz;
6451     case M_COP2:
6452       s = "c2";
6453       goto copz;
6454     case M_COP3:
6455       s = "c3";
6456     copz:
6457       /* For now we just do C (same as Cz).  The parameter will be
6458          stored in insn_opcode by mips_ip.  */
6459       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6460                    ip->insn_opcode);
6461       return;
6462
6463     case M_MOVE:
6464       move_register (&icnt, dreg, sreg);
6465       return;
6466
6467 #ifdef LOSING_COMPILER
6468     default:
6469       /* Try and see if this is a new itbl instruction.
6470          This code builds table entries out of the macros in mip_opcodes.
6471          FIXME: For now we just assemble the expression and pass it's
6472          value along as a 32-bit immediate.
6473          We may want to have the assembler assemble this value,
6474          so that we gain the assembler's knowledge of delay slots,
6475          symbols, etc.
6476          Would it be more efficient to use mask (id) here? */
6477       if (itbl_have_entries
6478           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6479         {
6480           s = ip->insn_mo->name;
6481           s2 = "cop3";
6482           coproc = ITBL_DECODE_PNUM (immed_expr);;
6483           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6484           return;
6485         }
6486       macro2 (ip);
6487       return;
6488     }
6489   if (mips_opts.noat)
6490     as_warn (_("Macro used $at after \".set noat\""));
6491 }
6492
6493 static void
6494 macro2 (ip)
6495      struct mips_cl_insn *ip;
6496 {
6497   register int treg, sreg, dreg, breg;
6498   int tempreg;
6499   int mask;
6500   int icnt = 0;
6501   int used_at;
6502   expressionS expr1;
6503   const char *s;
6504   const char *s2;
6505   const char *fmt;
6506   int likely = 0;
6507   int dbl = 0;
6508   int coproc = 0;
6509   int lr = 0;
6510   int imm = 0;
6511   int off;
6512   offsetT maxnum;
6513   bfd_reloc_code_real_type r;
6514   char *p;
6515
6516   treg = (ip->insn_opcode >> 16) & 0x1f;
6517   dreg = (ip->insn_opcode >> 11) & 0x1f;
6518   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6519   mask = ip->insn_mo->mask;
6520
6521   expr1.X_op = O_constant;
6522   expr1.X_op_symbol = NULL;
6523   expr1.X_add_symbol = NULL;
6524   expr1.X_add_number = 1;
6525
6526   switch (mask)
6527     {
6528 #endif /* LOSING_COMPILER */
6529
6530     case M_DMUL:
6531       dbl = 1;
6532     case M_MUL:
6533       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6534                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6535       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6536                    dreg);
6537       return;
6538
6539     case M_DMUL_I:
6540       dbl = 1;
6541     case M_MUL_I:
6542       /* The MIPS assembler some times generates shifts and adds.  I'm
6543          not trying to be that fancy. GCC should do this for us
6544          anyway.  */
6545       load_register (&icnt, AT, &imm_expr, dbl);
6546       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6547                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6548       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6549                    dreg);
6550       break;
6551
6552     case M_DMULO_I:
6553       dbl = 1;
6554     case M_MULO_I:
6555       imm = 1;
6556       goto do_mulo;
6557
6558     case M_DMULO:
6559       dbl = 1;
6560     case M_MULO:
6561     do_mulo:
6562       mips_emit_delays (true);
6563       ++mips_opts.noreorder;
6564       mips_any_noreorder = 1;
6565       if (imm)
6566         load_register (&icnt, AT, &imm_expr, dbl);
6567       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6568                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6569       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6570                    dreg);
6571       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6572                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
6573       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6574                    AT);
6575       if (mips_trap)
6576         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6577                      dreg, AT);
6578       else
6579         {
6580           expr1.X_add_number = 8;
6581           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6582                        AT);
6583           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6584                        0);
6585           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6586                        "c", 6);
6587         }
6588       --mips_opts.noreorder;
6589       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6590       break;
6591
6592     case M_DMULOU_I:
6593       dbl = 1;
6594     case M_MULOU_I:
6595       imm = 1;
6596       goto do_mulou;
6597
6598     case M_DMULOU:
6599       dbl = 1;
6600     case M_MULOU:
6601     do_mulou:
6602       mips_emit_delays (true);
6603       ++mips_opts.noreorder;
6604       mips_any_noreorder = 1;
6605       if (imm)
6606         load_register (&icnt, AT, &imm_expr, dbl);
6607       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6608                    dbl ? "dmultu" : "multu",
6609                    "s,t", sreg, imm ? AT : treg);
6610       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6611                    AT);
6612       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6613                    dreg);
6614       if (mips_trap)
6615         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6616                      AT, 0);
6617       else
6618         {
6619           expr1.X_add_number = 8;
6620           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6621           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6622                        0);
6623           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6624                        "c", 6);
6625         }
6626       --mips_opts.noreorder;
6627       break;
6628
6629     case M_ROL:
6630       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6631                    "d,v,t", AT, 0, treg);
6632       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6633                    "d,t,s", AT, sreg, AT);
6634       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6635                    "d,t,s", dreg, sreg, treg);
6636       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6637                    "d,v,t", dreg, dreg, AT);
6638       break;
6639
6640     case M_ROL_I:
6641       if (imm_expr.X_op != O_constant)
6642         as_bad (_("rotate count too large"));
6643       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6644                    AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6645       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6646                    dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6647       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6648                    dreg, dreg, AT);
6649       break;
6650
6651     case M_ROR:
6652       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6653                    "d,v,t", AT, 0, treg);
6654       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6655                    "d,t,s", AT, sreg, AT);
6656       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6657                    "d,t,s", dreg, sreg, treg);
6658       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6659                    "d,v,t", dreg, dreg, AT);
6660       break;
6661
6662     case M_ROR_I:
6663       if (imm_expr.X_op != O_constant)
6664         as_bad (_("rotate count too large"));
6665       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6666                    AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6667       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6668                    dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6669       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6670                    dreg, dreg, AT);
6671       break;
6672
6673     case M_S_DOB:
6674       if (mips_arch == CPU_R4650)
6675         {
6676           as_bad (_("opcode not supported on this processor"));
6677           return;
6678         }
6679       assert (mips_opts.isa == ISA_MIPS1);
6680       /* Even on a big endian machine $fn comes before $fn+1.  We have
6681          to adjust when storing to memory.  */
6682       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6683                    target_big_endian ? treg + 1 : treg,
6684                    (int) BFD_RELOC_LO16, breg);
6685       offset_expr.X_add_number += 4;
6686       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6687                    target_big_endian ? treg : treg + 1,
6688                    (int) BFD_RELOC_LO16, breg);
6689       return;
6690
6691     case M_SEQ:
6692       if (sreg == 0)
6693         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6694                      treg, (int) BFD_RELOC_LO16);
6695       else if (treg == 0)
6696         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6697                      sreg, (int) BFD_RELOC_LO16);
6698       else
6699         {
6700           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6701                        "d,v,t", dreg, sreg, treg);
6702           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6703                        dreg, (int) BFD_RELOC_LO16);
6704         }
6705       return;
6706
6707     case M_SEQ_I:
6708       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6709         {
6710           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6711                        sreg, (int) BFD_RELOC_LO16);
6712           return;
6713         }
6714       if (sreg == 0)
6715         {
6716           as_warn (_("Instruction %s: result is always false"),
6717                    ip->insn_mo->name);
6718           move_register (&icnt, dreg, 0);
6719           return;
6720         }
6721       if (imm_expr.X_op == O_constant
6722           && imm_expr.X_add_number >= 0
6723           && imm_expr.X_add_number < 0x10000)
6724         {
6725           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6726                        sreg, (int) BFD_RELOC_LO16);
6727           used_at = 0;
6728         }
6729       else if (imm_expr.X_op == O_constant
6730                && imm_expr.X_add_number > -0x8000
6731                && imm_expr.X_add_number < 0)
6732         {
6733           imm_expr.X_add_number = -imm_expr.X_add_number;
6734           macro_build ((char *) NULL, &icnt, &imm_expr,
6735                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6736                        "t,r,j", dreg, sreg,
6737                        (int) BFD_RELOC_LO16);
6738           used_at = 0;
6739         }
6740       else
6741         {
6742           load_register (&icnt, AT, &imm_expr, 0);
6743           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6744                        "d,v,t", dreg, sreg, AT);
6745           used_at = 1;
6746         }
6747       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6748                    (int) BFD_RELOC_LO16);
6749       if (used_at)
6750         break;
6751       return;
6752
6753     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6754       s = "slt";
6755       goto sge;
6756     case M_SGEU:
6757       s = "sltu";
6758     sge:
6759       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6760                    dreg, sreg, treg);
6761       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6762                    (int) BFD_RELOC_LO16);
6763       return;
6764
6765     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6766     case M_SGEU_I:
6767       if (imm_expr.X_op == O_constant
6768           && imm_expr.X_add_number >= -0x8000
6769           && imm_expr.X_add_number < 0x8000)
6770         {
6771           macro_build ((char *) NULL, &icnt, &imm_expr,
6772                        mask == M_SGE_I ? "slti" : "sltiu",
6773                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6774           used_at = 0;
6775         }
6776       else
6777         {
6778           load_register (&icnt, AT, &imm_expr, 0);
6779           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6780                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6781                        AT);
6782           used_at = 1;
6783         }
6784       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6785                    (int) BFD_RELOC_LO16);
6786       if (used_at)
6787         break;
6788       return;
6789
6790     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6791       s = "slt";
6792       goto sgt;
6793     case M_SGTU:
6794       s = "sltu";
6795     sgt:
6796       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6797                    dreg, treg, sreg);
6798       return;
6799
6800     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6801       s = "slt";
6802       goto sgti;
6803     case M_SGTU_I:
6804       s = "sltu";
6805     sgti:
6806       load_register (&icnt, AT, &imm_expr, 0);
6807       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6808                    dreg, AT, sreg);
6809       break;
6810
6811     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6812       s = "slt";
6813       goto sle;
6814     case M_SLEU:
6815       s = "sltu";
6816     sle:
6817       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6818                    dreg, treg, sreg);
6819       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6820                    (int) BFD_RELOC_LO16);
6821       return;
6822
6823     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6824       s = "slt";
6825       goto slei;
6826     case M_SLEU_I:
6827       s = "sltu";
6828     slei:
6829       load_register (&icnt, AT, &imm_expr, 0);
6830       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6831                    dreg, AT, sreg);
6832       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6833                    (int) BFD_RELOC_LO16);
6834       break;
6835
6836     case M_SLT_I:
6837       if (imm_expr.X_op == O_constant
6838           && imm_expr.X_add_number >= -0x8000
6839           && imm_expr.X_add_number < 0x8000)
6840         {
6841           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6842                        dreg, sreg, (int) BFD_RELOC_LO16);
6843           return;
6844         }
6845       load_register (&icnt, AT, &imm_expr, 0);
6846       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6847                    dreg, sreg, AT);
6848       break;
6849
6850     case M_SLTU_I:
6851       if (imm_expr.X_op == O_constant
6852           && imm_expr.X_add_number >= -0x8000
6853           && imm_expr.X_add_number < 0x8000)
6854         {
6855           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6856                        dreg, sreg, (int) BFD_RELOC_LO16);
6857           return;
6858         }
6859       load_register (&icnt, AT, &imm_expr, 0);
6860       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6861                    "d,v,t", dreg, sreg, AT);
6862       break;
6863
6864     case M_SNE:
6865       if (sreg == 0)
6866         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6867                      "d,v,t", dreg, 0, treg);
6868       else if (treg == 0)
6869         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6870                      "d,v,t", dreg, 0, sreg);
6871       else
6872         {
6873           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6874                        "d,v,t", dreg, sreg, treg);
6875           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6876                        "d,v,t", dreg, 0, dreg);
6877         }
6878       return;
6879
6880     case M_SNE_I:
6881       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6882         {
6883           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6884                        "d,v,t", dreg, 0, sreg);
6885           return;
6886         }
6887       if (sreg == 0)
6888         {
6889           as_warn (_("Instruction %s: result is always true"),
6890                    ip->insn_mo->name);
6891           macro_build ((char *) NULL, &icnt, &expr1,
6892                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6893                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6894           return;
6895         }
6896       if (imm_expr.X_op == O_constant
6897           && imm_expr.X_add_number >= 0
6898           && imm_expr.X_add_number < 0x10000)
6899         {
6900           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6901                        dreg, sreg, (int) BFD_RELOC_LO16);
6902           used_at = 0;
6903         }
6904       else if (imm_expr.X_op == O_constant
6905                && imm_expr.X_add_number > -0x8000
6906                && imm_expr.X_add_number < 0)
6907         {
6908           imm_expr.X_add_number = -imm_expr.X_add_number;
6909           macro_build ((char *) NULL, &icnt, &imm_expr,
6910                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6911                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6912           used_at = 0;
6913         }
6914       else
6915         {
6916           load_register (&icnt, AT, &imm_expr, 0);
6917           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6918                        "d,v,t", dreg, sreg, AT);
6919           used_at = 1;
6920         }
6921       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6922                    "d,v,t", dreg, 0, dreg);
6923       if (used_at)
6924         break;
6925       return;
6926
6927     case M_DSUB_I:
6928       dbl = 1;
6929     case M_SUB_I:
6930       if (imm_expr.X_op == O_constant
6931           && imm_expr.X_add_number > -0x8000
6932           && imm_expr.X_add_number <= 0x8000)
6933         {
6934           imm_expr.X_add_number = -imm_expr.X_add_number;
6935           macro_build ((char *) NULL, &icnt, &imm_expr,
6936                        dbl ? "daddi" : "addi",
6937                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6938           return;
6939         }
6940       load_register (&icnt, AT, &imm_expr, dbl);
6941       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6942                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6943       break;
6944
6945     case M_DSUBU_I:
6946       dbl = 1;
6947     case M_SUBU_I:
6948       if (imm_expr.X_op == O_constant
6949           && imm_expr.X_add_number > -0x8000
6950           && imm_expr.X_add_number <= 0x8000)
6951         {
6952           imm_expr.X_add_number = -imm_expr.X_add_number;
6953           macro_build ((char *) NULL, &icnt, &imm_expr,
6954                        dbl ? "daddiu" : "addiu",
6955                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6956           return;
6957         }
6958       load_register (&icnt, AT, &imm_expr, dbl);
6959       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6960                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
6961       break;
6962
6963     case M_TEQ_I:
6964       s = "teq";
6965       goto trap;
6966     case M_TGE_I:
6967       s = "tge";
6968       goto trap;
6969     case M_TGEU_I:
6970       s = "tgeu";
6971       goto trap;
6972     case M_TLT_I:
6973       s = "tlt";
6974       goto trap;
6975     case M_TLTU_I:
6976       s = "tltu";
6977       goto trap;
6978     case M_TNE_I:
6979       s = "tne";
6980     trap:
6981       load_register (&icnt, AT, &imm_expr, 0);
6982       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
6983                    AT);
6984       break;
6985
6986     case M_TRUNCWS:
6987     case M_TRUNCWD:
6988       assert (mips_opts.isa == ISA_MIPS1);
6989       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
6990       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
6991
6992       /*
6993        * Is the double cfc1 instruction a bug in the mips assembler;
6994        * or is there a reason for it?
6995        */
6996       mips_emit_delays (true);
6997       ++mips_opts.noreorder;
6998       mips_any_noreorder = 1;
6999       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7000                    treg, 31);
7001       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7002                    treg, 31);
7003       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7004       expr1.X_add_number = 3;
7005       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7006                    (int) BFD_RELOC_LO16);
7007       expr1.X_add_number = 2;
7008       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7009                      (int) BFD_RELOC_LO16);
7010       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7011                    AT, 31);
7012       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7013       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7014               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7015       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7016                    treg, 31);
7017       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7018       --mips_opts.noreorder;
7019       break;
7020
7021     case M_ULH:
7022       s = "lb";
7023       goto ulh;
7024     case M_ULHU:
7025       s = "lbu";
7026     ulh:
7027       if (offset_expr.X_add_number >= 0x7fff)
7028         as_bad (_("operand overflow"));
7029       /* avoid load delay */
7030       if (! target_big_endian)
7031         offset_expr.X_add_number += 1;
7032       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7033                    (int) BFD_RELOC_LO16, breg);
7034       if (! target_big_endian)
7035         offset_expr.X_add_number -= 1;
7036       else
7037         offset_expr.X_add_number += 1;
7038       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7039                    (int) BFD_RELOC_LO16, breg);
7040       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7041                    treg, treg, 8);
7042       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7043                    treg, treg, AT);
7044       break;
7045
7046     case M_ULD:
7047       s = "ldl";
7048       s2 = "ldr";
7049       off = 7;
7050       goto ulw;
7051     case M_ULW:
7052       s = "lwl";
7053       s2 = "lwr";
7054       off = 3;
7055     ulw:
7056       if (offset_expr.X_add_number >= 0x8000 - off)
7057         as_bad (_("operand overflow"));
7058       if (! target_big_endian)
7059         offset_expr.X_add_number += off;
7060       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7061                    (int) BFD_RELOC_LO16, breg);
7062       if (! target_big_endian)
7063         offset_expr.X_add_number -= off;
7064       else
7065         offset_expr.X_add_number += off;
7066       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7067                    (int) BFD_RELOC_LO16, breg);
7068       return;
7069
7070     case M_ULD_A:
7071       s = "ldl";
7072       s2 = "ldr";
7073       off = 7;
7074       goto ulwa;
7075     case M_ULW_A:
7076       s = "lwl";
7077       s2 = "lwr";
7078       off = 3;
7079     ulwa:
7080       used_at = 1;
7081       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7082       if (breg != 0)
7083         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7084                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7085                      "d,v,t", AT, AT, breg);
7086       if (! target_big_endian)
7087         expr1.X_add_number = off;
7088       else
7089         expr1.X_add_number = 0;
7090       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7091                    (int) BFD_RELOC_LO16, AT);
7092       if (! target_big_endian)
7093         expr1.X_add_number = 0;
7094       else
7095         expr1.X_add_number = off;
7096       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7097                    (int) BFD_RELOC_LO16, AT);
7098       break;
7099
7100     case M_ULH_A:
7101     case M_ULHU_A:
7102       used_at = 1;
7103       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7104       if (breg != 0)
7105         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7106                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7107                      "d,v,t", AT, AT, breg);
7108       if (target_big_endian)
7109         expr1.X_add_number = 0;
7110       macro_build ((char *) NULL, &icnt, &expr1,
7111                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7112                    (int) BFD_RELOC_LO16, AT);
7113       if (target_big_endian)
7114         expr1.X_add_number = 1;
7115       else
7116         expr1.X_add_number = 0;
7117       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7118                    (int) BFD_RELOC_LO16, AT);
7119       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7120                    treg, treg, 8);
7121       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7122                    treg, treg, AT);
7123       break;
7124
7125     case M_USH:
7126       if (offset_expr.X_add_number >= 0x7fff)
7127         as_bad (_("operand overflow"));
7128       if (target_big_endian)
7129         offset_expr.X_add_number += 1;
7130       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7131                    (int) BFD_RELOC_LO16, breg);
7132       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7133                    AT, treg, 8);
7134       if (target_big_endian)
7135         offset_expr.X_add_number -= 1;
7136       else
7137         offset_expr.X_add_number += 1;
7138       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7139                    (int) BFD_RELOC_LO16, breg);
7140       break;
7141
7142     case M_USD:
7143       s = "sdl";
7144       s2 = "sdr";
7145       off = 7;
7146       goto usw;
7147     case M_USW:
7148       s = "swl";
7149       s2 = "swr";
7150       off = 3;
7151     usw:
7152       if (offset_expr.X_add_number >= 0x8000 - off)
7153         as_bad (_("operand overflow"));
7154       if (! target_big_endian)
7155         offset_expr.X_add_number += off;
7156       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7157                    (int) BFD_RELOC_LO16, breg);
7158       if (! target_big_endian)
7159         offset_expr.X_add_number -= off;
7160       else
7161         offset_expr.X_add_number += off;
7162       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7163                    (int) BFD_RELOC_LO16, breg);
7164       return;
7165
7166     case M_USD_A:
7167       s = "sdl";
7168       s2 = "sdr";
7169       off = 7;
7170       goto uswa;
7171     case M_USW_A:
7172       s = "swl";
7173       s2 = "swr";
7174       off = 3;
7175     uswa:
7176       used_at = 1;
7177       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7178       if (breg != 0)
7179         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7180                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7181                      "d,v,t", AT, AT, breg);
7182       if (! target_big_endian)
7183         expr1.X_add_number = off;
7184       else
7185         expr1.X_add_number = 0;
7186       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7187                    (int) BFD_RELOC_LO16, AT);
7188       if (! target_big_endian)
7189         expr1.X_add_number = 0;
7190       else
7191         expr1.X_add_number = off;
7192       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7193                    (int) BFD_RELOC_LO16, AT);
7194       break;
7195
7196     case M_USH_A:
7197       used_at = 1;
7198       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7199       if (breg != 0)
7200         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7201                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7202                      "d,v,t", AT, AT, breg);
7203       if (! target_big_endian)
7204         expr1.X_add_number = 0;
7205       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7206                    (int) BFD_RELOC_LO16, AT);
7207       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7208                    treg, treg, 8);
7209       if (! target_big_endian)
7210         expr1.X_add_number = 1;
7211       else
7212         expr1.X_add_number = 0;
7213       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7214                    (int) BFD_RELOC_LO16, AT);
7215       if (! target_big_endian)
7216         expr1.X_add_number = 0;
7217       else
7218         expr1.X_add_number = 1;
7219       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7220                    (int) BFD_RELOC_LO16, AT);
7221       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7222                    treg, treg, 8);
7223       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7224                    treg, treg, AT);
7225       break;
7226
7227     default:
7228       /* FIXME: Check if this is one of the itbl macros, since they
7229          are added dynamically.  */
7230       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7231       break;
7232     }
7233   if (mips_opts.noat)
7234     as_warn (_("Macro used $at after \".set noat\""));
7235 }
7236
7237 /* Implement macros in mips16 mode.  */
7238
7239 static void
7240 mips16_macro (ip)
7241      struct mips_cl_insn *ip;
7242 {
7243   int mask;
7244   int xreg, yreg, zreg, tmp;
7245   int icnt;
7246   expressionS expr1;
7247   int dbl;
7248   const char *s, *s2, *s3;
7249
7250   mask = ip->insn_mo->mask;
7251
7252   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7253   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7254   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7255
7256   icnt = 0;
7257
7258   expr1.X_op = O_constant;
7259   expr1.X_op_symbol = NULL;
7260   expr1.X_add_symbol = NULL;
7261   expr1.X_add_number = 1;
7262
7263   dbl = 0;
7264
7265   switch (mask)
7266     {
7267     default:
7268       internalError ();
7269
7270     case M_DDIV_3:
7271       dbl = 1;
7272     case M_DIV_3:
7273       s = "mflo";
7274       goto do_div3;
7275     case M_DREM_3:
7276       dbl = 1;
7277     case M_REM_3:
7278       s = "mfhi";
7279     do_div3:
7280       mips_emit_delays (true);
7281       ++mips_opts.noreorder;
7282       mips_any_noreorder = 1;
7283       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7284                    dbl ? "ddiv" : "div",
7285                    "0,x,y", xreg, yreg);
7286       expr1.X_add_number = 2;
7287       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7288       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7289                    7);
7290
7291       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7292          since that causes an overflow.  We should do that as well,
7293          but I don't see how to do the comparisons without a temporary
7294          register.  */
7295       --mips_opts.noreorder;
7296       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7297       break;
7298
7299     case M_DIVU_3:
7300       s = "divu";
7301       s2 = "mflo";
7302       goto do_divu3;
7303     case M_REMU_3:
7304       s = "divu";
7305       s2 = "mfhi";
7306       goto do_divu3;
7307     case M_DDIVU_3:
7308       s = "ddivu";
7309       s2 = "mflo";
7310       goto do_divu3;
7311     case M_DREMU_3:
7312       s = "ddivu";
7313       s2 = "mfhi";
7314     do_divu3:
7315       mips_emit_delays (true);
7316       ++mips_opts.noreorder;
7317       mips_any_noreorder = 1;
7318       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7319                    xreg, yreg);
7320       expr1.X_add_number = 2;
7321       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7322         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7323                      "6", 7);
7324       --mips_opts.noreorder;
7325       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7326       break;
7327
7328     case M_DMUL:
7329       dbl = 1;
7330     case M_MUL:
7331       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7332                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7333       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7334                    zreg);
7335       return;
7336
7337     case M_DSUBU_I:
7338       dbl = 1;
7339       goto do_subu;
7340     case M_SUBU_I:
7341     do_subu:
7342       if (imm_expr.X_op != O_constant)
7343         as_bad (_("Unsupported large constant"));
7344       imm_expr.X_add_number = -imm_expr.X_add_number;
7345       macro_build ((char *) NULL, &icnt, &imm_expr,
7346                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7347       break;
7348
7349     case M_SUBU_I_2:
7350       if (imm_expr.X_op != O_constant)
7351         as_bad (_("Unsupported large constant"));
7352       imm_expr.X_add_number = -imm_expr.X_add_number;
7353       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7354                    "x,k", xreg);
7355       break;
7356
7357     case M_DSUBU_I_2:
7358       if (imm_expr.X_op != O_constant)
7359         as_bad (_("Unsupported large constant"));
7360       imm_expr.X_add_number = -imm_expr.X_add_number;
7361       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7362                    "y,j", yreg);
7363       break;
7364
7365     case M_BEQ:
7366       s = "cmp";
7367       s2 = "bteqz";
7368       goto do_branch;
7369     case M_BNE:
7370       s = "cmp";
7371       s2 = "btnez";
7372       goto do_branch;
7373     case M_BLT:
7374       s = "slt";
7375       s2 = "btnez";
7376       goto do_branch;
7377     case M_BLTU:
7378       s = "sltu";
7379       s2 = "btnez";
7380       goto do_branch;
7381     case M_BLE:
7382       s = "slt";
7383       s2 = "bteqz";
7384       goto do_reverse_branch;
7385     case M_BLEU:
7386       s = "sltu";
7387       s2 = "bteqz";
7388       goto do_reverse_branch;
7389     case M_BGE:
7390       s = "slt";
7391       s2 = "bteqz";
7392       goto do_branch;
7393     case M_BGEU:
7394       s = "sltu";
7395       s2 = "bteqz";
7396       goto do_branch;
7397     case M_BGT:
7398       s = "slt";
7399       s2 = "btnez";
7400       goto do_reverse_branch;
7401     case M_BGTU:
7402       s = "sltu";
7403       s2 = "btnez";
7404
7405     do_reverse_branch:
7406       tmp = xreg;
7407       xreg = yreg;
7408       yreg = tmp;
7409
7410     do_branch:
7411       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7412                    xreg, yreg);
7413       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7414       break;
7415
7416     case M_BEQ_I:
7417       s = "cmpi";
7418       s2 = "bteqz";
7419       s3 = "x,U";
7420       goto do_branch_i;
7421     case M_BNE_I:
7422       s = "cmpi";
7423       s2 = "btnez";
7424       s3 = "x,U";
7425       goto do_branch_i;
7426     case M_BLT_I:
7427       s = "slti";
7428       s2 = "btnez";
7429       s3 = "x,8";
7430       goto do_branch_i;
7431     case M_BLTU_I:
7432       s = "sltiu";
7433       s2 = "btnez";
7434       s3 = "x,8";
7435       goto do_branch_i;
7436     case M_BLE_I:
7437       s = "slti";
7438       s2 = "btnez";
7439       s3 = "x,8";
7440       goto do_addone_branch_i;
7441     case M_BLEU_I:
7442       s = "sltiu";
7443       s2 = "btnez";
7444       s3 = "x,8";
7445       goto do_addone_branch_i;
7446     case M_BGE_I:
7447       s = "slti";
7448       s2 = "bteqz";
7449       s3 = "x,8";
7450       goto do_branch_i;
7451     case M_BGEU_I:
7452       s = "sltiu";
7453       s2 = "bteqz";
7454       s3 = "x,8";
7455       goto do_branch_i;
7456     case M_BGT_I:
7457       s = "slti";
7458       s2 = "bteqz";
7459       s3 = "x,8";
7460       goto do_addone_branch_i;
7461     case M_BGTU_I:
7462       s = "sltiu";
7463       s2 = "bteqz";
7464       s3 = "x,8";
7465
7466     do_addone_branch_i:
7467       if (imm_expr.X_op != O_constant)
7468         as_bad (_("Unsupported large constant"));
7469       ++imm_expr.X_add_number;
7470
7471     do_branch_i:
7472       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7473       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7474       break;
7475
7476     case M_ABS:
7477       expr1.X_add_number = 0;
7478       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8",  yreg);
7479       if (xreg != yreg)
7480         move_register (&icnt, xreg, yreg);
7481       expr1.X_add_number = 2;
7482       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7483       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7484                    "neg", "x,w", xreg, xreg);
7485     }
7486 }
7487
7488 /* For consistency checking, verify that all bits are specified either
7489    by the match/mask part of the instruction definition, or by the
7490    operand list.  */
7491 static int
7492 validate_mips_insn (opc)
7493      const struct mips_opcode *opc;
7494 {
7495   const char *p = opc->args;
7496   char c;
7497   unsigned long used_bits = opc->mask;
7498
7499   if ((used_bits & opc->match) != opc->match)
7500     {
7501       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7502               opc->name, opc->args);
7503       return 0;
7504     }
7505 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7506   while (*p)
7507     switch (c = *p++)
7508       {
7509       case ',': break;
7510       case '(': break;
7511       case ')': break;
7512       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7513       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7514       case 'A': break;
7515       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7516       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7517       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7518       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7519       case 'F': break;
7520       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7521       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7522       case 'I': break;
7523       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7524       case 'L': break;
7525       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7526       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7527       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7528       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7529       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7530       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7531       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7532       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7533       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7534       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7535       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7536       case 'f': break;
7537       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7538       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7539       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7540       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7541       case 'l': break;
7542       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7543       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7544       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7545       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7546       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7547       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7548       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7549       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7550       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7551       case 'x': break;
7552       case 'z': break;
7553       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7554       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7555                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7556       default:
7557         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7558                 c, opc->name, opc->args);
7559         return 0;
7560       }
7561 #undef USE_BITS
7562   if (used_bits != 0xffffffff)
7563     {
7564       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7565               ~used_bits & 0xffffffff, opc->name, opc->args);
7566       return 0;
7567     }
7568   return 1;
7569 }
7570
7571 /* This routine assembles an instruction into its binary format.  As a
7572    side effect, it sets one of the global variables imm_reloc or
7573    offset_reloc to the type of relocation to do if one of the operands
7574    is an address expression.  */
7575
7576 static void
7577 mips_ip (str, ip)
7578      char *str;
7579      struct mips_cl_insn *ip;
7580 {
7581   char *s;
7582   const char *args;
7583   char c = 0;
7584   struct mips_opcode *insn;
7585   char *argsStart;
7586   unsigned int regno;
7587   unsigned int lastregno = 0;
7588   char *s_reset;
7589   char save_c = 0;
7590   int full_opcode_match = 1;
7591
7592   insn_error = NULL;
7593
7594   /* If the instruction contains a '.', we first try to match an instruction
7595      including the '.'.  Then we try again without the '.'.  */
7596   insn = NULL;
7597   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7598     continue;
7599
7600   /* If we stopped on whitespace, then replace the whitespace with null for
7601      the call to hash_find.  Save the character we replaced just in case we
7602      have to re-parse the instruction.  */
7603   if (ISSPACE (*s))
7604     {
7605       save_c = *s;
7606       *s++ = '\0';
7607     }
7608
7609   insn = (struct mips_opcode *) hash_find (op_hash, str);
7610
7611   /* If we didn't find the instruction in the opcode table, try again, but
7612      this time with just the instruction up to, but not including the
7613      first '.'.  */
7614   if (insn == NULL)
7615     {
7616       /* Restore the character we overwrite above (if any).  */
7617       if (save_c)
7618         *(--s) = save_c;
7619
7620       /* Scan up to the first '.' or whitespace.  */
7621       for (s = str;
7622            *s != '\0' && *s != '.' && !ISSPACE (*s);
7623            ++s)
7624         continue;
7625
7626       /* If we did not find a '.', then we can quit now.  */
7627       if (*s != '.')
7628         {
7629           insn_error = "unrecognized opcode";
7630           return;
7631         }
7632
7633       /* Lookup the instruction in the hash table.  */
7634       *s++ = '\0';
7635       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7636         {
7637           insn_error = "unrecognized opcode";
7638           return;
7639         }
7640
7641       full_opcode_match = 0;
7642     }
7643
7644   argsStart = s;
7645   for (;;)
7646     {
7647       boolean ok;
7648
7649       assert (strcmp (insn->name, str) == 0);
7650
7651       if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_arch))
7652         ok = true;
7653       else
7654         ok = false;
7655
7656       if (insn->pinfo != INSN_MACRO)
7657         {
7658           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7659             ok = false;
7660         }
7661
7662       if (! ok)
7663         {
7664           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7665               && strcmp (insn->name, insn[1].name) == 0)
7666             {
7667               ++insn;
7668               continue;
7669             }
7670           else
7671             {
7672               if (!insn_error)
7673                 {
7674                   static char buf[100];
7675                   sprintf (buf,
7676                            _("opcode not supported on this processor: %s (%s)"),
7677                            mips_cpu_to_str (mips_arch),
7678                            mips_isa_to_str (mips_opts.isa));
7679
7680                   insn_error = buf;
7681                 }
7682               if (save_c)
7683                 *(--s) = save_c;
7684               return;
7685             }
7686         }
7687
7688       ip->insn_mo = insn;
7689       ip->insn_opcode = insn->match;
7690       insn_error = NULL;
7691       for (args = insn->args;; ++args)
7692         {
7693           s += strspn (s, " \t");
7694           switch (*args)
7695             {
7696             case '\0':          /* end of args */
7697               if (*s == '\0')
7698                 return;
7699               break;
7700
7701             case ',':
7702               if (*s++ == *args)
7703                 continue;
7704               s--;
7705               switch (*++args)
7706                 {
7707                 case 'r':
7708                 case 'v':
7709                   ip->insn_opcode |= lastregno << OP_SH_RS;
7710                   continue;
7711
7712                 case 'w':
7713                   ip->insn_opcode |= lastregno << OP_SH_RT;
7714                   continue;
7715
7716                 case 'W':
7717                   ip->insn_opcode |= lastregno << OP_SH_FT;
7718                   continue;
7719
7720                 case 'V':
7721                   ip->insn_opcode |= lastregno << OP_SH_FS;
7722                   continue;
7723                 }
7724               break;
7725
7726             case '(':
7727               /* Handle optional base register.
7728                  Either the base register is omitted or
7729                  we must have a left paren.  */
7730               /* This is dependent on the next operand specifier
7731                  is a base register specification.  */
7732               assert (args[1] == 'b' || args[1] == '5'
7733                       || args[1] == '-' || args[1] == '4');
7734               if (*s == '\0')
7735                 return;
7736
7737             case ')':           /* these must match exactly */
7738               if (*s++ == *args)
7739                 continue;
7740               break;
7741
7742             case '<':           /* must be at least one digit */
7743               /*
7744                * According to the manual, if the shift amount is greater
7745                * than 31 or less than 0, then the shift amount should be
7746                * mod 32.  In reality the mips assembler issues an error.
7747                * We issue a warning and mask out all but the low 5 bits.
7748                */
7749               my_getExpression (&imm_expr, s);
7750               check_absolute_expr (ip, &imm_expr);
7751               if ((unsigned long) imm_expr.X_add_number > 31)
7752                 {
7753                   as_warn (_("Improper shift amount (%ld)"),
7754                            (long) imm_expr.X_add_number);
7755                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7756                 }
7757               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7758               imm_expr.X_op = O_absent;
7759               s = expr_end;
7760               continue;
7761
7762             case '>':           /* shift amount minus 32 */
7763               my_getExpression (&imm_expr, s);
7764               check_absolute_expr (ip, &imm_expr);
7765               if ((unsigned long) imm_expr.X_add_number < 32
7766                   || (unsigned long) imm_expr.X_add_number > 63)
7767                 break;
7768               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7769               imm_expr.X_op = O_absent;
7770               s = expr_end;
7771               continue;
7772
7773             case 'k':           /* cache code */
7774             case 'h':           /* prefx code */
7775               my_getExpression (&imm_expr, s);
7776               check_absolute_expr (ip, &imm_expr);
7777               if ((unsigned long) imm_expr.X_add_number > 31)
7778                 {
7779                   as_warn (_("Invalid value for `%s' (%lu)"),
7780                            ip->insn_mo->name,
7781                            (unsigned long) imm_expr.X_add_number);
7782                   imm_expr.X_add_number &= 0x1f;
7783                 }
7784               if (*args == 'k')
7785                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7786               else
7787                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7788               imm_expr.X_op = O_absent;
7789               s = expr_end;
7790               continue;
7791
7792             case 'c':           /* break code */
7793               my_getExpression (&imm_expr, s);
7794               check_absolute_expr (ip, &imm_expr);
7795               if ((unsigned) imm_expr.X_add_number > 1023)
7796                 {
7797                   as_warn (_("Illegal break code (%ld)"),
7798                            (long) imm_expr.X_add_number);
7799                   imm_expr.X_add_number &= OP_MASK_CODE;
7800                 }
7801               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7802               imm_expr.X_op = O_absent;
7803               s = expr_end;
7804               continue;
7805
7806             case 'q':           /* lower break code */
7807               my_getExpression (&imm_expr, s);
7808               check_absolute_expr (ip, &imm_expr);
7809               if ((unsigned) imm_expr.X_add_number > 1023)
7810                 {
7811                   as_warn (_("Illegal lower break code (%ld)"),
7812                            (long) imm_expr.X_add_number);
7813                   imm_expr.X_add_number &= OP_MASK_CODE2;
7814                 }
7815               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7816               imm_expr.X_op = O_absent;
7817               s = expr_end;
7818               continue;
7819
7820             case 'B':           /* 20-bit syscall/break code.  */
7821               my_getExpression (&imm_expr, s);
7822               check_absolute_expr (ip, &imm_expr);
7823               if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
7824                 as_warn (_("Illegal 20-bit code (%ld)"),
7825                          (long) imm_expr.X_add_number);
7826               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7827               imm_expr.X_op = O_absent;
7828               s = expr_end;
7829               continue;
7830
7831             case 'C':           /* Coprocessor code */
7832               my_getExpression (&imm_expr, s);
7833               check_absolute_expr (ip, &imm_expr);
7834               if ((unsigned long) imm_expr.X_add_number >= (1<<25))
7835                 {
7836                   as_warn (_("Coproccesor code > 25 bits (%ld)"),
7837                            (long) imm_expr.X_add_number);
7838                   imm_expr.X_add_number &= ((1<<25) - 1);
7839                 }
7840               ip->insn_opcode |= imm_expr.X_add_number;
7841               imm_expr.X_op = O_absent;
7842               s = expr_end;
7843               continue;
7844
7845             case 'J':           /* 19-bit wait code.  */
7846               my_getExpression (&imm_expr, s);
7847               check_absolute_expr (ip, &imm_expr);
7848               if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
7849                 as_warn (_("Illegal 19-bit code (%ld)"),
7850                          (long) imm_expr.X_add_number);
7851               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7852               imm_expr.X_op = O_absent;
7853               s = expr_end;
7854               continue;
7855
7856             case 'P':           /* Performance register */
7857               my_getExpression (&imm_expr, s);
7858               check_absolute_expr (ip, &imm_expr);
7859               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7860                 {
7861                   as_warn (_("Invalid performance register (%ld)"),
7862                            (long) imm_expr.X_add_number);
7863                   imm_expr.X_add_number &= OP_MASK_PERFREG;
7864                 }
7865               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7866               imm_expr.X_op = O_absent;
7867               s = expr_end;
7868               continue;
7869
7870             case 'b':           /* base register */
7871             case 'd':           /* destination register */
7872             case 's':           /* source register */
7873             case 't':           /* target register */
7874             case 'r':           /* both target and source */
7875             case 'v':           /* both dest and source */
7876             case 'w':           /* both dest and target */
7877             case 'E':           /* coprocessor target register */
7878             case 'G':           /* coprocessor destination register */
7879             case 'x':           /* ignore register name */
7880             case 'z':           /* must be zero register */
7881             case 'U':           /* destination register (clo/clz).  */
7882               s_reset = s;
7883               if (s[0] == '$')
7884                 {
7885
7886                   if (ISDIGIT (s[1]))
7887                     {
7888                       ++s;
7889                       regno = 0;
7890                       do
7891                         {
7892                           regno *= 10;
7893                           regno += *s - '0';
7894                           ++s;
7895                         }
7896                       while (ISDIGIT (*s));
7897                       if (regno > 31)
7898                         as_bad (_("Invalid register number (%d)"), regno);
7899                     }
7900                   else if (*args == 'E' || *args == 'G')
7901                     goto notreg;
7902                   else
7903                     {
7904                       if (s[1] == 'f' && s[2] == 'p')
7905                         {
7906                           s += 3;
7907                           regno = FP;
7908                         }
7909                       else if (s[1] == 's' && s[2] == 'p')
7910                         {
7911                           s += 3;
7912                           regno = SP;
7913                         }
7914                       else if (s[1] == 'g' && s[2] == 'p')
7915                         {
7916                           s += 3;
7917                           regno = GP;
7918                         }
7919                       else if (s[1] == 'a' && s[2] == 't')
7920                         {
7921                           s += 3;
7922                           regno = AT;
7923                         }
7924                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7925                         {
7926                           s += 4;
7927                           regno = KT0;
7928                         }
7929                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7930                         {
7931                           s += 4;
7932                           regno = KT1;
7933                         }
7934                       else if (itbl_have_entries)
7935                         {
7936                           char *p, *n;
7937                           unsigned long r;
7938
7939                           p = s + 1;    /* advance past '$' */
7940                           n = itbl_get_field (&p);  /* n is name */
7941
7942                           /* See if this is a register defined in an
7943                              itbl entry.  */
7944                           if (itbl_get_reg_val (n, &r))
7945                             {
7946                               /* Get_field advances to the start of
7947                                  the next field, so we need to back
7948                                  rack to the end of the last field.  */
7949                               if (p)
7950                                 s = p - 1;
7951                               else
7952                                 s = strchr (s, '\0');
7953                               regno = r;
7954                             }
7955                           else
7956                             goto notreg;
7957                         }
7958                       else
7959                         goto notreg;
7960                     }
7961                   if (regno == AT
7962                       && ! mips_opts.noat
7963                       && *args != 'E'
7964                       && *args != 'G')
7965                     as_warn (_("Used $at without \".set noat\""));
7966                   c = *args;
7967                   if (*s == ' ')
7968                     s++;
7969                   if (args[1] != *s)
7970                     {
7971                       if (c == 'r' || c == 'v' || c == 'w')
7972                         {
7973                           regno = lastregno;
7974                           s = s_reset;
7975                           args++;
7976                         }
7977                     }
7978                   /* 'z' only matches $0.  */
7979                   if (c == 'z' && regno != 0)
7980                     break;
7981
7982         /* Now that we have assembled one operand, we use the args string
7983          * to figure out where it goes in the instruction.  */
7984                   switch (c)
7985                     {
7986                     case 'r':
7987                     case 's':
7988                     case 'v':
7989                     case 'b':
7990                       ip->insn_opcode |= regno << OP_SH_RS;
7991                       break;
7992                     case 'd':
7993                     case 'G':
7994                       ip->insn_opcode |= regno << OP_SH_RD;
7995                       break;
7996                     case 'U':
7997                       ip->insn_opcode |= regno << OP_SH_RD;
7998                       ip->insn_opcode |= regno << OP_SH_RT;
7999                       break;
8000                     case 'w':
8001                     case 't':
8002                     case 'E':
8003                       ip->insn_opcode |= regno << OP_SH_RT;
8004                       break;
8005                     case 'x':
8006                       /* This case exists because on the r3000 trunc
8007                          expands into a macro which requires a gp
8008                          register.  On the r6000 or r4000 it is
8009                          assembled into a single instruction which
8010                          ignores the register.  Thus the insn version
8011                          is MIPS_ISA2 and uses 'x', and the macro
8012                          version is MIPS_ISA1 and uses 't'.  */
8013                       break;
8014                     case 'z':
8015                       /* This case is for the div instruction, which
8016                          acts differently if the destination argument
8017                          is $0.  This only matches $0, and is checked
8018                          outside the switch.  */
8019                       break;
8020                     case 'D':
8021                       /* Itbl operand; not yet implemented. FIXME ?? */
8022                       break;
8023                       /* What about all other operands like 'i', which
8024                          can be specified in the opcode table? */
8025                     }
8026                   lastregno = regno;
8027                   continue;
8028                 }
8029             notreg:
8030               switch (*args++)
8031                 {
8032                 case 'r':
8033                 case 'v':
8034                   ip->insn_opcode |= lastregno << OP_SH_RS;
8035                   continue;
8036                 case 'w':
8037                   ip->insn_opcode |= lastregno << OP_SH_RT;
8038                   continue;
8039                 }
8040               break;
8041
8042             case 'D':           /* floating point destination register */
8043             case 'S':           /* floating point source register */
8044             case 'T':           /* floating point target register */
8045             case 'R':           /* floating point source register */
8046             case 'V':
8047             case 'W':
8048               s_reset = s;
8049               if (s[0] == '$' && s[1] == 'f'
8050                   && ISDIGIT (s[2]))
8051                 {
8052                   s += 2;
8053                   regno = 0;
8054                   do
8055                     {
8056                       regno *= 10;
8057                       regno += *s - '0';
8058                       ++s;
8059                     }
8060                   while (ISDIGIT (*s));
8061
8062                   if (regno > 31)
8063                     as_bad (_("Invalid float register number (%d)"), regno);
8064
8065                   if ((regno & 1) != 0
8066                       && HAVE_32BIT_FPRS
8067                       && ! (strcmp (str, "mtc1") == 0
8068                             || strcmp (str, "mfc1") == 0
8069                             || strcmp (str, "lwc1") == 0
8070                             || strcmp (str, "swc1") == 0
8071                             || strcmp (str, "l.s") == 0
8072                             || strcmp (str, "s.s") == 0))
8073                     as_warn (_("Float register should be even, was %d"),
8074                              regno);
8075
8076                   c = *args;
8077                   if (*s == ' ')
8078                     s++;
8079                   if (args[1] != *s)
8080                     {
8081                       if (c == 'V' || c == 'W')
8082                         {
8083                           regno = lastregno;
8084                           s = s_reset;
8085                           args++;
8086                         }
8087                     }
8088                   switch (c)
8089                     {
8090                     case 'D':
8091                       ip->insn_opcode |= regno << OP_SH_FD;
8092                       break;
8093                     case 'V':
8094                     case 'S':
8095                       ip->insn_opcode |= regno << OP_SH_FS;
8096                       break;
8097                     case 'W':
8098                     case 'T':
8099                       ip->insn_opcode |= regno << OP_SH_FT;
8100                       break;
8101                     case 'R':
8102                       ip->insn_opcode |= regno << OP_SH_FR;
8103                       break;
8104                     }
8105                   lastregno = regno;
8106                   continue;
8107                 }
8108
8109               switch (*args++)
8110                 {
8111                 case 'V':
8112                   ip->insn_opcode |= lastregno << OP_SH_FS;
8113                   continue;
8114                 case 'W':
8115                   ip->insn_opcode |= lastregno << OP_SH_FT;
8116                   continue;
8117                 }
8118               break;
8119
8120             case 'I':
8121               my_getExpression (&imm_expr, s);
8122               if (imm_expr.X_op != O_big
8123                   && imm_expr.X_op != O_constant)
8124                 insn_error = _("absolute expression required");
8125               s = expr_end;
8126               continue;
8127
8128             case 'A':
8129               my_getExpression (&offset_expr, s);
8130               *imm_reloc = BFD_RELOC_32;
8131               s = expr_end;
8132               continue;
8133
8134             case 'F':
8135             case 'L':
8136             case 'f':
8137             case 'l':
8138               {
8139                 int f64;
8140                 int using_gprs;
8141                 char *save_in;
8142                 char *err;
8143                 unsigned char temp[8];
8144                 int len;
8145                 unsigned int length;
8146                 segT seg;
8147                 subsegT subseg;
8148                 char *p;
8149
8150                 /* These only appear as the last operand in an
8151                    instruction, and every instruction that accepts
8152                    them in any variant accepts them in all variants.
8153                    This means we don't have to worry about backing out
8154                    any changes if the instruction does not match.
8155
8156                    The difference between them is the size of the
8157                    floating point constant and where it goes.  For 'F'
8158                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8159                    is 32 bits.  Where the constant is placed is based
8160                    on how the MIPS assembler does things:
8161                     F -- .rdata
8162                     L -- .lit8
8163                     f -- immediate value
8164                     l -- .lit4
8165
8166                     The .lit4 and .lit8 sections are only used if
8167                     permitted by the -G argument.
8168
8169                     When generating embedded PIC code, we use the
8170                     .lit8 section but not the .lit4 section (we can do
8171                     .lit4 inline easily; we need to put .lit8
8172                     somewhere in the data segment, and using .lit8
8173                     permits the linker to eventually combine identical
8174                     .lit8 entries).
8175
8176                     The code below needs to know whether the target register
8177                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8178                     'F' are used with GPR-based instructions and 'l' and
8179                     'L' are used with FPR-based instructions.  */
8180
8181                 f64 = *args == 'F' || *args == 'L';
8182                 using_gprs = *args == 'F' || *args == 'f';
8183
8184                 save_in = input_line_pointer;
8185                 input_line_pointer = s;
8186                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8187                 length = len;
8188                 s = input_line_pointer;
8189                 input_line_pointer = save_in;
8190                 if (err != NULL && *err != '\0')
8191                   {
8192                     as_bad (_("Bad floating point constant: %s"), err);
8193                     memset (temp, '\0', sizeof temp);
8194                     length = f64 ? 8 : 4;
8195                   }
8196
8197                 assert (length == (unsigned) (f64 ? 8 : 4));
8198
8199                 if (*args == 'f'
8200                     || (*args == 'l'
8201                         && (! USE_GLOBAL_POINTER_OPT
8202                             || mips_pic == EMBEDDED_PIC
8203                             || g_switch_value < 4
8204                             || (temp[0] == 0 && temp[1] == 0)
8205                             || (temp[2] == 0 && temp[3] == 0))))
8206                   {
8207                     imm_expr.X_op = O_constant;
8208                     if (! target_big_endian)
8209                       imm_expr.X_add_number = bfd_getl32 (temp);
8210                     else
8211                       imm_expr.X_add_number = bfd_getb32 (temp);
8212                   }
8213                 else if (length > 4
8214                          && ! mips_disable_float_construction
8215                          /* Constants can only be constructed in GPRs and
8216                             copied to FPRs if the GPRs are at least as wide
8217                             as the FPRs.  Force the constant into memory if
8218                             we are using 64-bit FPRs but the GPRs are only
8219                             32 bits wide.  */
8220                          && (using_gprs
8221                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8222                          && ((temp[0] == 0 && temp[1] == 0)
8223                              || (temp[2] == 0 && temp[3] == 0))
8224                          && ((temp[4] == 0 && temp[5] == 0)
8225                              || (temp[6] == 0 && temp[7] == 0)))
8226                   {
8227                     /* The value is simple enough to load with a couple of
8228                        instructions.  If using 32-bit registers, set
8229                        imm_expr to the high order 32 bits and offset_expr to
8230                        the low order 32 bits.  Otherwise, set imm_expr to
8231                        the entire 64 bit constant.  */
8232                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8233                       {
8234                         imm_expr.X_op = O_constant;
8235                         offset_expr.X_op = O_constant;
8236                         if (! target_big_endian)
8237                           {
8238                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8239                             offset_expr.X_add_number = bfd_getl32 (temp);
8240                           }
8241                         else
8242                           {
8243                             imm_expr.X_add_number = bfd_getb32 (temp);
8244                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8245                           }
8246                         if (offset_expr.X_add_number == 0)
8247                           offset_expr.X_op = O_absent;
8248                       }
8249                     else if (sizeof (imm_expr.X_add_number) > 4)
8250                       {
8251                         imm_expr.X_op = O_constant;
8252                         if (! target_big_endian)
8253                           imm_expr.X_add_number = bfd_getl64 (temp);
8254                         else
8255                           imm_expr.X_add_number = bfd_getb64 (temp);
8256                       }
8257                     else
8258                       {
8259                         imm_expr.X_op = O_big;
8260                         imm_expr.X_add_number = 4;
8261                         if (! target_big_endian)
8262                           {
8263                             generic_bignum[0] = bfd_getl16 (temp);
8264                             generic_bignum[1] = bfd_getl16 (temp + 2);
8265                             generic_bignum[2] = bfd_getl16 (temp + 4);
8266                             generic_bignum[3] = bfd_getl16 (temp + 6);
8267                           }
8268                         else
8269                           {
8270                             generic_bignum[0] = bfd_getb16 (temp + 6);
8271                             generic_bignum[1] = bfd_getb16 (temp + 4);
8272                             generic_bignum[2] = bfd_getb16 (temp + 2);
8273                             generic_bignum[3] = bfd_getb16 (temp);
8274                           }
8275                       }
8276                   }
8277                 else
8278                   {
8279                     const char *newname;
8280                     segT new_seg;
8281
8282                     /* Switch to the right section.  */
8283                     seg = now_seg;
8284                     subseg = now_subseg;
8285                     switch (*args)
8286                       {
8287                       default: /* unused default case avoids warnings.  */
8288                       case 'L':
8289                         newname = RDATA_SECTION_NAME;
8290                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8291                             || mips_pic == EMBEDDED_PIC)
8292                           newname = ".lit8";
8293                         break;
8294                       case 'F':
8295                         if (mips_pic == EMBEDDED_PIC)
8296                           newname = ".lit8";
8297                         else
8298                           newname = RDATA_SECTION_NAME;
8299                         break;
8300                       case 'l':
8301                         assert (!USE_GLOBAL_POINTER_OPT
8302                                 || g_switch_value >= 4);
8303                         newname = ".lit4";
8304                         break;
8305                       }
8306                     new_seg = subseg_new (newname, (subsegT) 0);
8307                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8308                       bfd_set_section_flags (stdoutput, new_seg,
8309                                              (SEC_ALLOC
8310                                               | SEC_LOAD
8311                                               | SEC_READONLY
8312                                               | SEC_DATA));
8313                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8314                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8315                         && strcmp (TARGET_OS, "elf") != 0)
8316                       record_alignment (new_seg, 4);
8317                     else
8318                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8319                     if (seg == now_seg)
8320                       as_bad (_("Can't use floating point insn in this section"));
8321
8322                     /* Set the argument to the current address in the
8323                        section.  */
8324                     offset_expr.X_op = O_symbol;
8325                     offset_expr.X_add_symbol =
8326                       symbol_new ("L0\001", now_seg,
8327                                   (valueT) frag_now_fix (), frag_now);
8328                     offset_expr.X_add_number = 0;
8329
8330                     /* Put the floating point number into the section.  */
8331                     p = frag_more ((int) length);
8332                     memcpy (p, temp, length);
8333
8334                     /* Switch back to the original section.  */
8335                     subseg_set (seg, subseg);
8336                   }
8337               }
8338               continue;
8339
8340             case 'i':           /* 16 bit unsigned immediate */
8341             case 'j':           /* 16 bit signed immediate */
8342               *imm_reloc = BFD_RELOC_LO16;
8343               c = my_getSmallExpression (&imm_expr, s);
8344               if (c != S_EX_NONE)
8345                 {
8346                   if (c != S_EX_LO)
8347                     {
8348                       if (imm_expr.X_op == O_constant)
8349                         imm_expr.X_add_number =
8350                           (imm_expr.X_add_number >> 16) & 0xffff;
8351 #ifdef OBJ_ELF
8352                       else if (c == S_EX_HIGHEST)
8353                           *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8354                       else if (c == S_EX_HIGHER)
8355                           *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8356                       else if (c == S_EX_GP_REL)
8357                         {
8358                           /* This occurs in NewABI only.  */
8359                           c = my_getSmallExpression (&imm_expr, s);
8360                           if (c != S_EX_NEG)
8361                             as_bad (_("bad composition of relocations"));
8362                           else
8363                             {
8364                               c = my_getSmallExpression (&imm_expr, s);
8365                               if (c != S_EX_LO)
8366                                 as_bad (_("bad composition of relocations"));
8367                               else
8368                                 {
8369                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8370                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8371                                   imm_reloc[2] = BFD_RELOC_LO16;
8372                                 }
8373                             }
8374                         }
8375 #endif
8376                       else if (c == S_EX_HI)
8377                         {
8378                           *imm_reloc = BFD_RELOC_HI16_S;
8379                           imm_unmatched_hi = true;
8380                         }
8381                       else
8382                         *imm_reloc = BFD_RELOC_HI16;
8383                     }
8384                   else if (imm_expr.X_op == O_constant)
8385                     imm_expr.X_add_number &= 0xffff;
8386                 }
8387               if (*args == 'i')
8388                 {
8389                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8390                       || ((imm_expr.X_add_number < 0
8391                            || imm_expr.X_add_number >= 0x10000)
8392                           && imm_expr.X_op == O_constant))
8393                     {
8394                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8395                           !strcmp (insn->name, insn[1].name))
8396                         break;
8397                       if (imm_expr.X_op == O_constant
8398                           || imm_expr.X_op == O_big)
8399                         as_bad (_("16 bit expression not in range 0..65535"));
8400                     }
8401                 }
8402               else
8403                 {
8404                   int more;
8405                   offsetT max;
8406
8407                   /* The upper bound should be 0x8000, but
8408                      unfortunately the MIPS assembler accepts numbers
8409                      from 0x8000 to 0xffff and sign extends them, and
8410                      we want to be compatible.  We only permit this
8411                      extended range for an instruction which does not
8412                      provide any further alternates, since those
8413                      alternates may handle other cases.  People should
8414                      use the numbers they mean, rather than relying on
8415                      a mysterious sign extension.  */
8416                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8417                           strcmp (insn->name, insn[1].name) == 0);
8418                   if (more)
8419                     max = 0x8000;
8420                   else
8421                     max = 0x10000;
8422                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8423                       || ((imm_expr.X_add_number < -0x8000
8424                            || imm_expr.X_add_number >= max)
8425                           && imm_expr.X_op == O_constant)
8426                       || (more
8427                           && imm_expr.X_add_number < 0
8428                           && HAVE_64BIT_GPRS
8429                           && imm_expr.X_unsigned
8430                           && sizeof (imm_expr.X_add_number) <= 4))
8431                     {
8432                       if (more)
8433                         break;
8434                       if (imm_expr.X_op == O_constant
8435                           || imm_expr.X_op == O_big)
8436                         as_bad (_("16 bit expression not in range -32768..32767"));
8437                     }
8438                 }
8439               s = expr_end;
8440               continue;
8441
8442             case 'o':           /* 16 bit offset */
8443               c = my_getSmallExpression (&offset_expr, s);
8444
8445               /* If this value won't fit into a 16 bit offset, then go
8446                  find a macro that will generate the 32 bit offset
8447                  code pattern.  */
8448               if (c == S_EX_NONE
8449                   && (offset_expr.X_op != O_constant
8450                       || offset_expr.X_add_number >= 0x8000
8451                       || offset_expr.X_add_number < -0x8000))
8452                 break;
8453
8454               if (c == S_EX_HI)
8455                 {
8456                   if (offset_expr.X_op != O_constant)
8457                     break;
8458                   offset_expr.X_add_number =
8459                     (offset_expr.X_add_number >> 16) & 0xffff;
8460                 }
8461               *offset_reloc = BFD_RELOC_LO16;
8462               s = expr_end;
8463               continue;
8464
8465             case 'p':           /* pc relative offset */
8466               if (mips_pic == EMBEDDED_PIC)
8467                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8468               else
8469                 *offset_reloc = BFD_RELOC_16_PCREL;
8470               my_getExpression (&offset_expr, s);
8471               s = expr_end;
8472               continue;
8473
8474             case 'u':           /* upper 16 bits */
8475               c = my_getSmallExpression (&imm_expr, s);
8476               *imm_reloc = BFD_RELOC_LO16;
8477               if (c != S_EX_NONE)
8478                 {
8479                   if (c != S_EX_LO)
8480                     {
8481                       if (imm_expr.X_op == O_constant)
8482                         imm_expr.X_add_number =
8483                           (imm_expr.X_add_number >> 16) & 0xffff;
8484                       else if (c == S_EX_HI)
8485                         {
8486                           *imm_reloc = BFD_RELOC_HI16_S;
8487                           imm_unmatched_hi = true;
8488                         }
8489 #ifdef OBJ_ELF
8490                       else if (c == S_EX_HIGHEST)
8491                           *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8492                       else if (c == S_EX_GP_REL)
8493                         {
8494                           /* This occurs in NewABI only.  */
8495                           c = my_getSmallExpression (&imm_expr, s);
8496                           if (c != S_EX_NEG)
8497                             as_bad (_("bad composition of relocations"));
8498                           else
8499                             {
8500                               c = my_getSmallExpression (&imm_expr, s);
8501                               if (c != S_EX_HI)
8502                                 as_bad (_("bad composition of relocations"));
8503                               else
8504                                 {
8505                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8506                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8507                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8508                                 }
8509                             }
8510                         }
8511 #endif
8512                       else
8513                         *imm_reloc = BFD_RELOC_HI16;
8514                     }
8515                   else if (imm_expr.X_op == O_constant)
8516                     imm_expr.X_add_number &= 0xffff;
8517                 }
8518               if (imm_expr.X_op == O_constant
8519                   && (imm_expr.X_add_number < 0
8520                       || imm_expr.X_add_number >= 0x10000))
8521                 as_bad (_("lui expression not in range 0..65535"));
8522               s = expr_end;
8523               continue;
8524
8525             case 'a':           /* 26 bit address */
8526               my_getExpression (&offset_expr, s);
8527               s = expr_end;
8528               *offset_reloc = BFD_RELOC_MIPS_JMP;
8529               continue;
8530
8531             case 'N':           /* 3 bit branch condition code */
8532             case 'M':           /* 3 bit compare condition code */
8533               if (strncmp (s, "$fcc", 4) != 0)
8534                 break;
8535               s += 4;
8536               regno = 0;
8537               do
8538                 {
8539                   regno *= 10;
8540                   regno += *s - '0';
8541                   ++s;
8542                 }
8543               while (ISDIGIT (*s));
8544               if (regno > 7)
8545                 as_bad (_("invalid condition code register $fcc%d"), regno);
8546               if (*args == 'N')
8547                 ip->insn_opcode |= regno << OP_SH_BCC;
8548               else
8549                 ip->insn_opcode |= regno << OP_SH_CCC;
8550               continue;
8551
8552             case 'H':
8553               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8554                 s += 2;
8555               if (ISDIGIT (*s))
8556                 {
8557                   c = 0;
8558                   do
8559                     {
8560                       c *= 10;
8561                       c += *s - '0';
8562                       ++s;
8563                     }
8564                   while (ISDIGIT (*s));
8565                 }
8566               else
8567                 c = 8; /* Invalid sel value.  */
8568
8569               if (c > 7)
8570                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8571               ip->insn_opcode |= c;
8572               continue;
8573
8574             default:
8575               as_bad (_("bad char = '%c'\n"), *args);
8576               internalError ();
8577             }
8578           break;
8579         }
8580       /* Args don't match.  */
8581       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8582           !strcmp (insn->name, insn[1].name))
8583         {
8584           ++insn;
8585           s = argsStart;
8586           insn_error = _("illegal operands");
8587           continue;
8588         }
8589       if (save_c)
8590         *(--s) = save_c;
8591       insn_error = _("illegal operands");
8592       return;
8593     }
8594 }
8595
8596 /* This routine assembles an instruction into its binary format when
8597    assembling for the mips16.  As a side effect, it sets one of the
8598    global variables imm_reloc or offset_reloc to the type of
8599    relocation to do if one of the operands is an address expression.
8600    It also sets mips16_small and mips16_ext if the user explicitly
8601    requested a small or extended instruction.  */
8602
8603 static void
8604 mips16_ip (str, ip)
8605      char *str;
8606      struct mips_cl_insn *ip;
8607 {
8608   char *s;
8609   const char *args;
8610   struct mips_opcode *insn;
8611   char *argsstart;
8612   unsigned int regno;
8613   unsigned int lastregno = 0;
8614   char *s_reset;
8615
8616   insn_error = NULL;
8617
8618   mips16_small = false;
8619   mips16_ext = false;
8620
8621   for (s = str; ISLOWER (*s); ++s)
8622     ;
8623   switch (*s)
8624     {
8625     case '\0':
8626       break;
8627
8628     case ' ':
8629       *s++ = '\0';
8630       break;
8631
8632     case '.':
8633       if (s[1] == 't' && s[2] == ' ')
8634         {
8635           *s = '\0';
8636           mips16_small = true;
8637           s += 3;
8638           break;
8639         }
8640       else if (s[1] == 'e' && s[2] == ' ')
8641         {
8642           *s = '\0';
8643           mips16_ext = true;
8644           s += 3;
8645           break;
8646         }
8647       /* Fall through.  */
8648     default:
8649       insn_error = _("unknown opcode");
8650       return;
8651     }
8652
8653   if (mips_opts.noautoextend && ! mips16_ext)
8654     mips16_small = true;
8655
8656   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8657     {
8658       insn_error = _("unrecognized opcode");
8659       return;
8660     }
8661
8662   argsstart = s;
8663   for (;;)
8664     {
8665       assert (strcmp (insn->name, str) == 0);
8666
8667       ip->insn_mo = insn;
8668       ip->insn_opcode = insn->match;
8669       ip->use_extend = false;
8670       imm_expr.X_op = O_absent;
8671       imm_reloc[0] = BFD_RELOC_UNUSED;
8672       imm_reloc[1] = BFD_RELOC_UNUSED;
8673       imm_reloc[2] = BFD_RELOC_UNUSED;
8674       offset_expr.X_op = O_absent;
8675       offset_reloc[0] = BFD_RELOC_UNUSED;
8676       offset_reloc[1] = BFD_RELOC_UNUSED;
8677       offset_reloc[2] = BFD_RELOC_UNUSED;
8678       for (args = insn->args; 1; ++args)
8679         {
8680           int c;
8681
8682           if (*s == ' ')
8683             ++s;
8684
8685           /* In this switch statement we call break if we did not find
8686              a match, continue if we did find a match, or return if we
8687              are done.  */
8688
8689           c = *args;
8690           switch (c)
8691             {
8692             case '\0':
8693               if (*s == '\0')
8694                 {
8695                   /* Stuff the immediate value in now, if we can.  */
8696                   if (imm_expr.X_op == O_constant
8697                       && *imm_reloc > BFD_RELOC_UNUSED
8698                       && insn->pinfo != INSN_MACRO)
8699                     {
8700                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8701                                     imm_expr.X_add_number, true, mips16_small,
8702                                     mips16_ext, &ip->insn_opcode,
8703                                     &ip->use_extend, &ip->extend);
8704                       imm_expr.X_op = O_absent;
8705                       *imm_reloc = BFD_RELOC_UNUSED;
8706                     }
8707
8708                   return;
8709                 }
8710               break;
8711
8712             case ',':
8713               if (*s++ == c)
8714                 continue;
8715               s--;
8716               switch (*++args)
8717                 {
8718                 case 'v':
8719                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8720                   continue;
8721                 case 'w':
8722                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8723                   continue;
8724                 }
8725               break;
8726
8727             case '(':
8728             case ')':
8729               if (*s++ == c)
8730                 continue;
8731               break;
8732
8733             case 'v':
8734             case 'w':
8735               if (s[0] != '$')
8736                 {
8737                   if (c == 'v')
8738                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8739                   else
8740                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8741                   ++args;
8742                   continue;
8743                 }
8744               /* Fall through.  */
8745             case 'x':
8746             case 'y':
8747             case 'z':
8748             case 'Z':
8749             case '0':
8750             case 'S':
8751             case 'R':
8752             case 'X':
8753             case 'Y':
8754               if (s[0] != '$')
8755                 break;
8756               s_reset = s;
8757               if (ISDIGIT (s[1]))
8758                 {
8759                   ++s;
8760                   regno = 0;
8761                   do
8762                     {
8763                       regno *= 10;
8764                       regno += *s - '0';
8765                       ++s;
8766                     }
8767                   while (ISDIGIT (*s));
8768                   if (regno > 31)
8769                     {
8770                       as_bad (_("invalid register number (%d)"), regno);
8771                       regno = 2;
8772                     }
8773                 }
8774               else
8775                 {
8776                   if (s[1] == 'f' && s[2] == 'p')
8777                     {
8778                       s += 3;
8779                       regno = FP;
8780                     }
8781                   else if (s[1] == 's' && s[2] == 'p')
8782                     {
8783                       s += 3;
8784                       regno = SP;
8785                     }
8786                   else if (s[1] == 'g' && s[2] == 'p')
8787                     {
8788                       s += 3;
8789                       regno = GP;
8790                     }
8791                   else if (s[1] == 'a' && s[2] == 't')
8792                     {
8793                       s += 3;
8794                       regno = AT;
8795                     }
8796                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8797                     {
8798                       s += 4;
8799                       regno = KT0;
8800                     }
8801                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8802                     {
8803                       s += 4;
8804                       regno = KT1;
8805                     }
8806                   else
8807                     break;
8808                 }
8809
8810               if (*s == ' ')
8811                 ++s;
8812               if (args[1] != *s)
8813                 {
8814                   if (c == 'v' || c == 'w')
8815                     {
8816                       regno = mips16_to_32_reg_map[lastregno];
8817                       s = s_reset;
8818                       args++;
8819                     }
8820                 }
8821
8822               switch (c)
8823                 {
8824                 case 'x':
8825                 case 'y':
8826                 case 'z':
8827                 case 'v':
8828                 case 'w':
8829                 case 'Z':
8830                   regno = mips32_to_16_reg_map[regno];
8831                   break;
8832
8833                 case '0':
8834                   if (regno != 0)
8835                     regno = ILLEGAL_REG;
8836                   break;
8837
8838                 case 'S':
8839                   if (regno != SP)
8840                     regno = ILLEGAL_REG;
8841                   break;
8842
8843                 case 'R':
8844                   if (regno != RA)
8845                     regno = ILLEGAL_REG;
8846                   break;
8847
8848                 case 'X':
8849                 case 'Y':
8850                   if (regno == AT && ! mips_opts.noat)
8851                     as_warn (_("used $at without \".set noat\""));
8852                   break;
8853
8854                 default:
8855                   internalError ();
8856                 }
8857
8858               if (regno == ILLEGAL_REG)
8859                 break;
8860
8861               switch (c)
8862                 {
8863                 case 'x':
8864                 case 'v':
8865                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8866                   break;
8867                 case 'y':
8868                 case 'w':
8869                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8870                   break;
8871                 case 'z':
8872                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8873                   break;
8874                 case 'Z':
8875                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8876                 case '0':
8877                 case 'S':
8878                 case 'R':
8879                   break;
8880                 case 'X':
8881                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8882                   break;
8883                 case 'Y':
8884                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8885                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8886                   break;
8887                 default:
8888                   internalError ();
8889                 }
8890
8891               lastregno = regno;
8892               continue;
8893
8894             case 'P':
8895               if (strncmp (s, "$pc", 3) == 0)
8896                 {
8897                   s += 3;
8898                   continue;
8899                 }
8900               break;
8901
8902             case '<':
8903             case '>':
8904             case '[':
8905             case ']':
8906             case '4':
8907             case '5':
8908             case 'H':
8909             case 'W':
8910             case 'D':
8911             case 'j':
8912             case '8':
8913             case 'V':
8914             case 'C':
8915             case 'U':
8916             case 'k':
8917             case 'K':
8918               if (s[0] == '%'
8919                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8920                 {
8921                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
8922                      and generate the appropriate reloc.  If the text
8923                      inside %gprel is not a symbol name with an
8924                      optional offset, then we generate a normal reloc
8925                      and will probably fail later.  */
8926                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8927                   if (imm_expr.X_op == O_symbol)
8928                     {
8929                       mips16_ext = true;
8930                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
8931                       s = expr_end;
8932                       ip->use_extend = true;
8933                       ip->extend = 0;
8934                       continue;
8935                     }
8936                 }
8937               else
8938                 {
8939                   /* Just pick up a normal expression.  */
8940                   my_getExpression (&imm_expr, s);
8941                 }
8942
8943               if (imm_expr.X_op == O_register)
8944                 {
8945                   /* What we thought was an expression turned out to
8946                      be a register.  */
8947
8948                   if (s[0] == '(' && args[1] == '(')
8949                     {
8950                       /* It looks like the expression was omitted
8951                          before a register indirection, which means
8952                          that the expression is implicitly zero.  We
8953                          still set up imm_expr, so that we handle
8954                          explicit extensions correctly.  */
8955                       imm_expr.X_op = O_constant;
8956                       imm_expr.X_add_number = 0;
8957                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8958                       continue;
8959                     }
8960
8961                   break;
8962                 }
8963
8964               /* We need to relax this instruction.  */
8965               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8966               s = expr_end;
8967               continue;
8968
8969             case 'p':
8970             case 'q':
8971             case 'A':
8972             case 'B':
8973             case 'E':
8974               /* We use offset_reloc rather than imm_reloc for the PC
8975                  relative operands.  This lets macros with both
8976                  immediate and address operands work correctly.  */
8977               my_getExpression (&offset_expr, s);
8978
8979               if (offset_expr.X_op == O_register)
8980                 break;
8981
8982               /* We need to relax this instruction.  */
8983               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8984               s = expr_end;
8985               continue;
8986
8987             case '6':           /* break code */
8988               my_getExpression (&imm_expr, s);
8989               check_absolute_expr (ip, &imm_expr);
8990               if ((unsigned long) imm_expr.X_add_number > 63)
8991                 {
8992                   as_warn (_("Invalid value for `%s' (%lu)"),
8993                            ip->insn_mo->name,
8994                            (unsigned long) imm_expr.X_add_number);
8995                   imm_expr.X_add_number &= 0x3f;
8996                 }
8997               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
8998               imm_expr.X_op = O_absent;
8999               s = expr_end;
9000               continue;
9001
9002             case 'a':           /* 26 bit address */
9003               my_getExpression (&offset_expr, s);
9004               s = expr_end;
9005               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9006               ip->insn_opcode <<= 16;
9007               continue;
9008
9009             case 'l':           /* register list for entry macro */
9010             case 'L':           /* register list for exit macro */
9011               {
9012                 int mask;
9013
9014                 if (c == 'l')
9015                   mask = 0;
9016                 else
9017                   mask = 7 << 3;
9018                 while (*s != '\0')
9019                   {
9020                     int freg, reg1, reg2;
9021
9022                     while (*s == ' ' || *s == ',')
9023                       ++s;
9024                     if (*s != '$')
9025                       {
9026                         as_bad (_("can't parse register list"));
9027                         break;
9028                       }
9029                     ++s;
9030                     if (*s != 'f')
9031                       freg = 0;
9032                     else
9033                       {
9034                         freg = 1;
9035                         ++s;
9036                       }
9037                     reg1 = 0;
9038                     while (ISDIGIT (*s))
9039                       {
9040                         reg1 *= 10;
9041                         reg1 += *s - '0';
9042                         ++s;
9043                       }
9044                     if (*s == ' ')
9045                       ++s;
9046                     if (*s != '-')
9047                       reg2 = reg1;
9048                     else
9049                       {
9050                         ++s;
9051                         if (*s != '$')
9052                           break;
9053                         ++s;
9054                         if (freg)
9055                           {
9056                             if (*s == 'f')
9057                               ++s;
9058                             else
9059                               {
9060                                 as_bad (_("invalid register list"));
9061                                 break;
9062                               }
9063                           }
9064                         reg2 = 0;
9065                         while (ISDIGIT (*s))
9066                           {
9067                             reg2 *= 10;
9068                             reg2 += *s - '0';
9069                             ++s;
9070                           }
9071                       }
9072                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9073                       {
9074                         mask &= ~ (7 << 3);
9075                         mask |= 5 << 3;
9076                       }
9077                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9078                       {
9079                         mask &= ~ (7 << 3);
9080                         mask |= 6 << 3;
9081                       }
9082                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9083                       mask |= (reg2 - 3) << 3;
9084                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9085                       mask |= (reg2 - 15) << 1;
9086                     else if (reg1 == 31 && reg2 == 31)
9087                       mask |= 1;
9088                     else
9089                       {
9090                         as_bad (_("invalid register list"));
9091                         break;
9092                       }
9093                   }
9094                 /* The mask is filled in in the opcode table for the
9095                    benefit of the disassembler.  We remove it before
9096                    applying the actual mask.  */
9097                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9098                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9099               }
9100             continue;
9101
9102             case 'e':           /* extend code */
9103               my_getExpression (&imm_expr, s);
9104               check_absolute_expr (ip, &imm_expr);
9105               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9106                 {
9107                   as_warn (_("Invalid value for `%s' (%lu)"),
9108                            ip->insn_mo->name,
9109                            (unsigned long) imm_expr.X_add_number);
9110                   imm_expr.X_add_number &= 0x7ff;
9111                 }
9112               ip->insn_opcode |= imm_expr.X_add_number;
9113               imm_expr.X_op = O_absent;
9114               s = expr_end;
9115               continue;
9116
9117             default:
9118               internalError ();
9119             }
9120           break;
9121         }
9122
9123       /* Args don't match.  */
9124       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9125           strcmp (insn->name, insn[1].name) == 0)
9126         {
9127           ++insn;
9128           s = argsstart;
9129           continue;
9130         }
9131
9132       insn_error = _("illegal operands");
9133
9134       return;
9135     }
9136 }
9137
9138 /* This structure holds information we know about a mips16 immediate
9139    argument type.  */
9140
9141 struct mips16_immed_operand
9142 {
9143   /* The type code used in the argument string in the opcode table.  */
9144   int type;
9145   /* The number of bits in the short form of the opcode.  */
9146   int nbits;
9147   /* The number of bits in the extended form of the opcode.  */
9148   int extbits;
9149   /* The amount by which the short form is shifted when it is used;
9150      for example, the sw instruction has a shift count of 2.  */
9151   int shift;
9152   /* The amount by which the short form is shifted when it is stored
9153      into the instruction code.  */
9154   int op_shift;
9155   /* Non-zero if the short form is unsigned.  */
9156   int unsp;
9157   /* Non-zero if the extended form is unsigned.  */
9158   int extu;
9159   /* Non-zero if the value is PC relative.  */
9160   int pcrel;
9161 };
9162
9163 /* The mips16 immediate operand types.  */
9164
9165 static const struct mips16_immed_operand mips16_immed_operands[] =
9166 {
9167   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9168   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9169   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9170   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9171   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9172   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9173   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9174   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9175   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9176   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9177   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9178   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9179   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9180   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9181   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9182   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9183   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9184   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9185   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9186   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9187   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9188 };
9189
9190 #define MIPS16_NUM_IMMED \
9191   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9192
9193 /* Handle a mips16 instruction with an immediate value.  This or's the
9194    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9195    whether an extended value is needed; if one is needed, it sets
9196    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9197    If SMALL is true, an unextended opcode was explicitly requested.
9198    If EXT is true, an extended opcode was explicitly requested.  If
9199    WARN is true, warn if EXT does not match reality.  */
9200
9201 static void
9202 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9203               extend)
9204      char *file;
9205      unsigned int line;
9206      int type;
9207      offsetT val;
9208      boolean warn;
9209      boolean small;
9210      boolean ext;
9211      unsigned long *insn;
9212      boolean *use_extend;
9213      unsigned short *extend;
9214 {
9215   register const struct mips16_immed_operand *op;
9216   int mintiny, maxtiny;
9217   boolean needext;
9218
9219   op = mips16_immed_operands;
9220   while (op->type != type)
9221     {
9222       ++op;
9223       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9224     }
9225
9226   if (op->unsp)
9227     {
9228       if (type == '<' || type == '>' || type == '[' || type == ']')
9229         {
9230           mintiny = 1;
9231           maxtiny = 1 << op->nbits;
9232         }
9233       else
9234         {
9235           mintiny = 0;
9236           maxtiny = (1 << op->nbits) - 1;
9237         }
9238     }
9239   else
9240     {
9241       mintiny = - (1 << (op->nbits - 1));
9242       maxtiny = (1 << (op->nbits - 1)) - 1;
9243     }
9244
9245   /* Branch offsets have an implicit 0 in the lowest bit.  */
9246   if (type == 'p' || type == 'q')
9247     val /= 2;
9248
9249   if ((val & ((1 << op->shift) - 1)) != 0
9250       || val < (mintiny << op->shift)
9251       || val > (maxtiny << op->shift))
9252     needext = true;
9253   else
9254     needext = false;
9255
9256   if (warn && ext && ! needext)
9257     as_warn_where (file, line,
9258                    _("extended operand requested but not required"));
9259   if (small && needext)
9260     as_bad_where (file, line, _("invalid unextended operand value"));
9261
9262   if (small || (! ext && ! needext))
9263     {
9264       int insnval;
9265
9266       *use_extend = false;
9267       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9268       insnval <<= op->op_shift;
9269       *insn |= insnval;
9270     }
9271   else
9272     {
9273       long minext, maxext;
9274       int extval;
9275
9276       if (op->extu)
9277         {
9278           minext = 0;
9279           maxext = (1 << op->extbits) - 1;
9280         }
9281       else
9282         {
9283           minext = - (1 << (op->extbits - 1));
9284           maxext = (1 << (op->extbits - 1)) - 1;
9285         }
9286       if (val < minext || val > maxext)
9287         as_bad_where (file, line,
9288                       _("operand value out of range for instruction"));
9289
9290       *use_extend = true;
9291       if (op->extbits == 16)
9292         {
9293           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9294           val &= 0x1f;
9295         }
9296       else if (op->extbits == 15)
9297         {
9298           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9299           val &= 0xf;
9300         }
9301       else
9302         {
9303           extval = ((val & 0x1f) << 6) | (val & 0x20);
9304           val = 0;
9305         }
9306
9307       *extend = (unsigned short) extval;
9308       *insn |= val;
9309     }
9310 }
9311 \f
9312 static struct percent_op_match
9313 {
9314    const char *str;
9315    const enum small_ex_type type;
9316 } percent_op[] =
9317 {
9318   {"%lo", S_EX_LO},
9319 #ifdef OBJ_ELF
9320   {"%call_hi", S_EX_CALL_HI},
9321   {"%call_lo", S_EX_CALL_LO},
9322   {"%call16", S_EX_CALL16},
9323   {"%got_disp", S_EX_GOT_DISP},
9324   {"%got_page", S_EX_GOT_PAGE},
9325   {"%got_ofst", S_EX_GOT_OFST},
9326   {"%got_hi", S_EX_GOT_HI},
9327   {"%got_lo", S_EX_GOT_LO},
9328   {"%got", S_EX_GOT},
9329   {"%gp_rel", S_EX_GP_REL},
9330   {"%half", S_EX_HALF},
9331   {"%highest", S_EX_HIGHEST},
9332   {"%higher", S_EX_HIGHER},
9333   {"%neg", S_EX_NEG},
9334 #endif
9335   {"%hi", S_EX_HI}
9336 };
9337
9338 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9339    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9340    can be nested, this is handled by blanking the innermost, parsing the
9341    rest by subsequent calls.  */
9342
9343 static int
9344 my_getSmallParser (str, len, nestlevel)
9345      char **str;
9346      unsigned int *len;
9347      int *nestlevel;
9348 {
9349   *len = 0;
9350   *str += strspn (*str, " \t");
9351   /* Check for expression in parentheses.  */
9352   if (**str == '(')
9353     {
9354       char *b = *str + 1 + strspn (*str + 1, " \t");
9355       char *e;
9356
9357       /* Check for base register.  */
9358       if (b[0] == '$')
9359         {
9360           if (strchr (b, ')')
9361               && (e = b + strcspn (b, ") \t"))
9362               && e - b > 1 && e - b < 4)
9363             {
9364                if ((e - b == 3
9365                     && ((b[1] == 'f' && b[2] == 'p')
9366                         || (b[1] == 's' && b[2] == 'p')
9367                         || (b[1] == 'g' && b[2] == 'p')
9368                         || (b[1] == 'a' && b[2] == 't')
9369                         || (ISDIGIT (b[1])
9370                             && ISDIGIT (b[2]))))
9371                    || (ISDIGIT (b[1])))
9372                  {
9373                    *len = strcspn (*str, ")") + 1;
9374                    return S_EX_REGISTER;
9375                  }
9376             }
9377         }
9378       /* Check for percent_op (in parentheses).  */
9379       else if (b[0] == '%')
9380         {
9381           *str = b;
9382           return my_getPercentOp (str, len, nestlevel);
9383         }
9384
9385       /* Some other expression in the parentheses, which can contain
9386          parentheses itself. Attempt to find the matching one.  */
9387       {
9388         int pcnt = 1;
9389         char *s;
9390
9391         *len = 1;
9392         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9393           {
9394             if (*s == '(')
9395               pcnt++;
9396             else if (*s == ')')
9397               pcnt--;
9398           }
9399       }
9400     }
9401   /* Check for percent_op (outside of parentheses).  */
9402   else if (*str[0] == '%')
9403     return my_getPercentOp (str, len, nestlevel);
9404
9405   /* Any other expression.  */
9406   return S_EX_NONE;
9407 }
9408
9409 static int
9410 my_getPercentOp (str, len, nestlevel)
9411      char **str;
9412      unsigned int *len;
9413      int *nestlevel;
9414 {
9415   char *tmp = *str + 1;
9416   unsigned int i = 0;
9417
9418   while (ISALPHA (*tmp) || *tmp == '_')
9419     {
9420       *tmp = TOLOWER (*tmp);
9421       tmp++;
9422     }
9423   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9424     {
9425       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9426           i++;
9427       else
9428         {
9429           int type = percent_op[i].type;
9430
9431           /* Only %hi and %lo are allowed for OldABI.  */
9432           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9433             return S_EX_NONE;
9434
9435           *len = strlen (percent_op[i].str);
9436           (*nestlevel)++;
9437           return type;
9438         }
9439     }
9440   return S_EX_NONE;
9441 }
9442
9443 static int
9444 my_getSmallExpression (ep, str)
9445      expressionS *ep;
9446      char *str;
9447 {
9448   static char *oldstr = NULL;
9449   int c = S_EX_NONE;
9450   int oldc;
9451   int nestlevel = -1;
9452   unsigned int len;
9453
9454   /* Don't update oldstr if the last call had nested percent_op's. We need
9455      it to parse the outer ones later.  */
9456   if (! oldstr)
9457     oldstr = str;
9458
9459   do
9460     {
9461       oldc = c;
9462       c = my_getSmallParser (&str, &len, &nestlevel);
9463       if (c != S_EX_NONE && c != S_EX_REGISTER)
9464         str += len;
9465     }
9466   while (c != S_EX_NONE && c != S_EX_REGISTER);
9467
9468   if (nestlevel >= 0)
9469     {
9470       /* A percent_op was encountered.  Don't try to get an expression if
9471          it is already blanked out.  */
9472       if (*(str + strspn (str + 1, " )")) != ')')
9473         {
9474           char save;
9475
9476           /* Let my_getExpression() stop at the closing parenthesis.  */
9477           save = *(str + len);
9478           *(str + len) = '\0';
9479           my_getExpression (ep, str);
9480           *(str + len) = save;
9481         }
9482       if (nestlevel > 0)
9483         {
9484           /* Blank out including the % sign and the proper matching
9485              parenthesis.  */
9486           int pcnt = 1;
9487           char *s = strrchr (oldstr, '%');
9488           char *end;
9489
9490           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9491             {
9492               if (*end == '(')
9493                 pcnt++;
9494               else if (*end == ')')
9495                 pcnt--;
9496             }
9497
9498           memset (s, ' ', end - s);
9499           str = oldstr;
9500         }
9501       else
9502         expr_end = str + len;
9503
9504       c = oldc;
9505     }
9506   else if (c == S_EX_NONE)
9507     {
9508       my_getExpression (ep, str);
9509     }
9510   else if (c == S_EX_REGISTER)
9511     {
9512       ep->X_op = O_constant;
9513       expr_end = str;
9514       ep->X_add_symbol = NULL;
9515       ep->X_op_symbol = NULL;
9516       ep->X_add_number = 0;
9517     }
9518   else
9519     {
9520       as_fatal(_("internal error"));
9521     }
9522
9523   if (nestlevel <= 0)
9524     /* All percent_op's have been handled.  */
9525     oldstr = NULL;
9526
9527   return c;
9528 }
9529
9530 static void
9531 my_getExpression (ep, str)
9532      expressionS *ep;
9533      char *str;
9534 {
9535   char *save_in;
9536   valueT val;
9537
9538   save_in = input_line_pointer;
9539   input_line_pointer = str;
9540   expression (ep);
9541   expr_end = input_line_pointer;
9542   input_line_pointer = save_in;
9543
9544   /* If we are in mips16 mode, and this is an expression based on `.',
9545      then we bump the value of the symbol by 1 since that is how other
9546      text symbols are handled.  We don't bother to handle complex
9547      expressions, just `.' plus or minus a constant.  */
9548   if (mips_opts.mips16
9549       && ep->X_op == O_symbol
9550       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9551       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9552       && symbol_get_frag (ep->X_add_symbol) == frag_now
9553       && symbol_constant_p (ep->X_add_symbol)
9554       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9555     S_SET_VALUE (ep->X_add_symbol, val + 1);
9556 }
9557
9558 /* Turn a string in input_line_pointer into a floating point constant
9559    of type TYPE, and store the appropriate bytes in *LITP.  The number
9560    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9561    returned, or NULL on OK.  */
9562
9563 char *
9564 md_atof (type, litP, sizeP)
9565      int type;
9566      char *litP;
9567      int *sizeP;
9568 {
9569   int prec;
9570   LITTLENUM_TYPE words[4];
9571   char *t;
9572   int i;
9573
9574   switch (type)
9575     {
9576     case 'f':
9577       prec = 2;
9578       break;
9579
9580     case 'd':
9581       prec = 4;
9582       break;
9583
9584     default:
9585       *sizeP = 0;
9586       return _("bad call to md_atof");
9587     }
9588
9589   t = atof_ieee (input_line_pointer, type, words);
9590   if (t)
9591     input_line_pointer = t;
9592
9593   *sizeP = prec * 2;
9594
9595   if (! target_big_endian)
9596     {
9597       for (i = prec - 1; i >= 0; i--)
9598         {
9599           md_number_to_chars (litP, (valueT) words[i], 2);
9600           litP += 2;
9601         }
9602     }
9603   else
9604     {
9605       for (i = 0; i < prec; i++)
9606         {
9607           md_number_to_chars (litP, (valueT) words[i], 2);
9608           litP += 2;
9609         }
9610     }
9611
9612   return NULL;
9613 }
9614
9615 void
9616 md_number_to_chars (buf, val, n)
9617      char *buf;
9618      valueT val;
9619      int n;
9620 {
9621   if (target_big_endian)
9622     number_to_chars_bigendian (buf, val, n);
9623   else
9624     number_to_chars_littleendian (buf, val, n);
9625 }
9626 \f
9627 #ifdef OBJ_ELF
9628 static int support_64bit_objects(void)
9629 {
9630   const char **list, **l;
9631
9632   list = bfd_target_list ();
9633   for (l = list; *l != NULL; l++)
9634 #ifdef TE_TMIPS
9635     /* This is traditional mips */
9636     if (strcmp (*l, "elf64-tradbigmips") == 0
9637         || strcmp (*l, "elf64-tradlittlemips") == 0)
9638 #else
9639     if (strcmp (*l, "elf64-bigmips") == 0
9640         || strcmp (*l, "elf64-littlemips") == 0)
9641 #endif
9642       break;
9643   free (list);
9644   return (*l != NULL);
9645 }
9646 #endif /* OBJ_ELF */
9647
9648 CONST char *md_shortopts = "nO::g::G:";
9649
9650 struct option md_longopts[] =
9651 {
9652 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9653   {"mips0", no_argument, NULL, OPTION_MIPS1},
9654   {"mips1", no_argument, NULL, OPTION_MIPS1},
9655 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9656   {"mips2", no_argument, NULL, OPTION_MIPS2},
9657 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9658   {"mips3", no_argument, NULL, OPTION_MIPS3},
9659 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9660   {"mips4", no_argument, NULL, OPTION_MIPS4},
9661 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9662   {"mips5", no_argument, NULL, OPTION_MIPS5},
9663 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9664   {"mips32", no_argument, NULL, OPTION_MIPS32},
9665 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9666   {"mips64", no_argument, NULL, OPTION_MIPS64},
9667 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9668   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9669 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9670   {"trap", no_argument, NULL, OPTION_TRAP},
9671   {"no-break", no_argument, NULL, OPTION_TRAP},
9672 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9673   {"break", no_argument, NULL, OPTION_BREAK},
9674   {"no-trap", no_argument, NULL, OPTION_BREAK},
9675 #define OPTION_EB (OPTION_MD_BASE + 11)
9676   {"EB", no_argument, NULL, OPTION_EB},
9677 #define OPTION_EL (OPTION_MD_BASE + 12)
9678   {"EL", no_argument, NULL, OPTION_EL},
9679 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9680   {"mips16", no_argument, NULL, OPTION_MIPS16},
9681 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9682   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9683 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9684   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9685 #define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 16)
9686   {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
9687 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9688   {"mfp32", no_argument, NULL, OPTION_FP32},
9689 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9690   {"mgp32", no_argument, NULL, OPTION_GP32},
9691 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9692   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9693 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9694   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9695 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9696   {"march", required_argument, NULL, OPTION_MARCH},
9697 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9698   {"mtune", required_argument, NULL, OPTION_MTUNE},
9699 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9700   {"mcpu", required_argument, NULL, OPTION_MCPU},
9701 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9702   {"m4650", no_argument, NULL, OPTION_M4650},
9703 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9704   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9705 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9706   {"m4010", no_argument, NULL, OPTION_M4010},
9707 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9708   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9709 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9710   {"m4100", no_argument, NULL, OPTION_M4100},
9711 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9712   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9713 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9714   {"m3900", no_argument, NULL, OPTION_M3900},
9715 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9716   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9717 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9718   {"mgp64", no_argument, NULL, OPTION_GP64},
9719 #ifdef OBJ_ELF
9720 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 33)
9721 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9722   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9723   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9724 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
9725   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
9726 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
9727   {"xgot",        no_argument, NULL, OPTION_XGOT},
9728 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
9729   {"mabi", required_argument, NULL, OPTION_MABI},
9730 #define OPTION_32          (OPTION_ELF_BASE + 4)
9731   {"32",          no_argument, NULL, OPTION_32},
9732 #define OPTION_N32         (OPTION_ELF_BASE + 5)
9733   {"n32",         no_argument, NULL, OPTION_N32},
9734 #define OPTION_64          (OPTION_ELF_BASE + 6)
9735   {"64",          no_argument, NULL, OPTION_64},
9736 #endif /* OBJ_ELF */
9737   {NULL, no_argument, NULL, 0}
9738 };
9739 size_t md_longopts_size = sizeof (md_longopts);
9740
9741 int
9742 md_parse_option (c, arg)
9743      int c;
9744      char *arg;
9745 {
9746   switch (c)
9747     {
9748     case OPTION_CONSTRUCT_FLOATS:
9749       mips_disable_float_construction = 0;
9750       break;
9751
9752     case OPTION_NO_CONSTRUCT_FLOATS:
9753       mips_disable_float_construction = 1;
9754       break;
9755
9756     case OPTION_TRAP:
9757       mips_trap = 1;
9758       break;
9759
9760     case OPTION_BREAK:
9761       mips_trap = 0;
9762       break;
9763
9764     case OPTION_EB:
9765       target_big_endian = 1;
9766       break;
9767
9768     case OPTION_EL:
9769       target_big_endian = 0;
9770       break;
9771
9772     case 'n':
9773       warn_nops = 1;
9774       break;
9775
9776     case 'O':
9777       if (arg && arg[1] == '0')
9778         mips_optimize = 1;
9779       else
9780         mips_optimize = 2;
9781       break;
9782
9783     case 'g':
9784       if (arg == NULL)
9785         mips_debug = 2;
9786       else
9787         mips_debug = atoi (arg);
9788       /* When the MIPS assembler sees -g or -g2, it does not do
9789          optimizations which limit full symbolic debugging.  We take
9790          that to be equivalent to -O0.  */
9791       if (mips_debug == 2)
9792         mips_optimize = 1;
9793       break;
9794
9795     case OPTION_MIPS1:
9796       mips_opts.isa = ISA_MIPS1;
9797       break;
9798
9799     case OPTION_MIPS2:
9800       mips_opts.isa = ISA_MIPS2;
9801       break;
9802
9803     case OPTION_MIPS3:
9804       mips_opts.isa = ISA_MIPS3;
9805       break;
9806
9807     case OPTION_MIPS4:
9808       mips_opts.isa = ISA_MIPS4;
9809       break;
9810
9811     case OPTION_MIPS5:
9812       mips_opts.isa = ISA_MIPS5;
9813       break;
9814
9815     case OPTION_MIPS32:
9816       mips_opts.isa = ISA_MIPS32;
9817       break;
9818
9819     case OPTION_MIPS64:
9820       mips_opts.isa = ISA_MIPS64;
9821       break;
9822
9823     case OPTION_MTUNE:
9824     case OPTION_MARCH:
9825     case OPTION_MCPU:
9826       {
9827         int cpu = CPU_UNKNOWN;
9828
9829         /* Identify the processor type.  */
9830         if (strcasecmp (arg, "default") != 0)
9831           {
9832             const struct mips_cpu_info *ci;
9833
9834             ci = mips_cpu_info_from_name (arg);
9835             if (ci == NULL || ci->is_isa)
9836               {
9837                 switch (c)
9838                   {
9839                   case OPTION_MTUNE:
9840                     as_fatal (_("invalid architecture -mtune=%s"), arg);
9841                     break;
9842                   case OPTION_MARCH:
9843                     as_fatal (_("invalid architecture -march=%s"), arg);
9844                     break;
9845                   case OPTION_MCPU:
9846                     as_fatal (_("invalid architecture -mcpu=%s"), arg);
9847                     break;
9848                   }
9849               }
9850             else
9851                 cpu = ci->cpu;
9852           }
9853
9854         switch (c)
9855           {
9856           case OPTION_MTUNE:
9857             if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9858               as_warn(_("A different -mtune= was already specified, is now "
9859                         "-mtune=%s"), arg);
9860             mips_tune = cpu;
9861             break;
9862           case OPTION_MARCH:
9863             if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9864               as_warn(_("A different -march= was already specified, is now "
9865                         "-march=%s"), arg);
9866             mips_arch = cpu;
9867             break;
9868           case OPTION_MCPU:
9869             if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9870               as_warn(_("A different -mcpu= was already specified, is now "
9871                         "-mcpu=%s"), arg);
9872             mips_cpu = cpu;
9873           }
9874       }
9875       break;
9876
9877     case OPTION_M4650:
9878       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9879           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9880         as_warn(_("A different -march= or -mtune= was already specified, "
9881                   "is now -m4650"));
9882       mips_arch = CPU_R4650;
9883       mips_tune = CPU_R4650;
9884       break;
9885
9886     case OPTION_NO_M4650:
9887       break;
9888
9889     case OPTION_M4010:
9890       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9891           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9892         as_warn(_("A different -march= or -mtune= was already specified, "
9893                   "is now -m4010"));
9894       mips_arch = CPU_R4010;
9895       mips_tune = CPU_R4010;
9896       break;
9897
9898     case OPTION_NO_M4010:
9899       break;
9900
9901     case OPTION_M4100:
9902       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9903           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9904         as_warn(_("A different -march= or -mtune= was already specified, "
9905                   "is now -m4100"));
9906       mips_arch = CPU_VR4100;
9907       mips_tune = CPU_VR4100;
9908       break;
9909
9910     case OPTION_NO_M4100:
9911       break;
9912
9913     case OPTION_M3900:
9914       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9915           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9916         as_warn(_("A different -march= or -mtune= was already specified, "
9917                   "is now -m3900"));
9918       mips_arch = CPU_R3900;
9919       mips_tune = CPU_R3900;
9920       break;
9921
9922     case OPTION_NO_M3900:
9923       break;
9924
9925     case OPTION_MIPS16:
9926       mips_opts.mips16 = 1;
9927       mips_no_prev_insn (false);
9928       break;
9929
9930     case OPTION_NO_MIPS16:
9931       mips_opts.mips16 = 0;
9932       mips_no_prev_insn (false);
9933       break;
9934
9935     case OPTION_MEMBEDDED_PIC:
9936       mips_pic = EMBEDDED_PIC;
9937       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9938         {
9939           as_bad (_("-G may not be used with embedded PIC code"));
9940           return 0;
9941         }
9942       g_switch_value = 0x7fffffff;
9943       break;
9944
9945 #ifdef OBJ_ELF
9946       /* When generating ELF code, we permit -KPIC and -call_shared to
9947          select SVR4_PIC, and -non_shared to select no PIC.  This is
9948          intended to be compatible with Irix 5.  */
9949     case OPTION_CALL_SHARED:
9950       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9951         {
9952           as_bad (_("-call_shared is supported only for ELF format"));
9953           return 0;
9954         }
9955       mips_pic = SVR4_PIC;
9956       if (g_switch_seen && g_switch_value != 0)
9957         {
9958           as_bad (_("-G may not be used with SVR4 PIC code"));
9959           return 0;
9960         }
9961       g_switch_value = 0;
9962       break;
9963
9964     case OPTION_NON_SHARED:
9965       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9966         {
9967           as_bad (_("-non_shared is supported only for ELF format"));
9968           return 0;
9969         }
9970       mips_pic = NO_PIC;
9971       break;
9972
9973       /* The -xgot option tells the assembler to use 32 offsets when
9974          accessing the got in SVR4_PIC mode.  It is for Irix
9975          compatibility.  */
9976     case OPTION_XGOT:
9977       mips_big_got = 1;
9978       break;
9979 #endif /* OBJ_ELF */
9980
9981     case 'G':
9982       if (! USE_GLOBAL_POINTER_OPT)
9983         {
9984           as_bad (_("-G is not supported for this configuration"));
9985           return 0;
9986         }
9987       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
9988         {
9989           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
9990           return 0;
9991         }
9992       else
9993         g_switch_value = atoi (arg);
9994       g_switch_seen = 1;
9995       break;
9996
9997 #ifdef OBJ_ELF
9998       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
9999          and -mabi=64.  */
10000     case OPTION_32:
10001       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10002         {
10003           as_bad (_("-32 is supported for ELF format only"));
10004           return 0;
10005         }
10006       mips_opts.abi = O32_ABI;
10007       break;
10008
10009     case OPTION_N32:
10010       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10011         {
10012           as_bad (_("-n32 is supported for ELF format only"));
10013           return 0;
10014         }
10015       mips_opts.abi = N32_ABI;
10016       break;
10017
10018     case OPTION_64:
10019       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10020         {
10021           as_bad (_("-64 is supported for ELF format only"));
10022           return 0;
10023         }
10024       mips_opts.abi = N64_ABI;
10025       if (! support_64bit_objects())
10026         as_fatal (_("No compiled in support for 64 bit object file format"));
10027       break;
10028 #endif /* OBJ_ELF */
10029
10030     case OPTION_GP32:
10031       file_mips_gp32 = 1;
10032       if (mips_opts.abi != O32_ABI)
10033         mips_opts.abi = NO_ABI;
10034       break;
10035
10036     case OPTION_GP64:
10037       file_mips_gp32 = 0;
10038       if (mips_opts.abi == O32_ABI)
10039         mips_opts.abi = NO_ABI;
10040       break;
10041
10042     case OPTION_FP32:
10043       file_mips_fp32 = 1;
10044       if (mips_opts.abi != O32_ABI)
10045         mips_opts.abi = NO_ABI;
10046       break;
10047
10048 #ifdef OBJ_ELF
10049     case OPTION_MABI:
10050       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10051         {
10052           as_bad (_("-mabi is supported for ELF format only"));
10053           return 0;
10054         }
10055       if (strcmp (arg, "32") == 0)
10056         mips_opts.abi = O32_ABI;
10057       else if (strcmp (arg, "o64") == 0)
10058         mips_opts.abi = O64_ABI;
10059       else if (strcmp (arg, "n32") == 0)
10060         mips_opts.abi = N32_ABI;
10061       else if (strcmp (arg, "64") == 0)
10062         {
10063           mips_opts.abi = N64_ABI;
10064           if (! support_64bit_objects())
10065             as_fatal (_("No compiled in support for 64 bit object file "
10066                         "format"));
10067         }
10068       else if (strcmp (arg, "eabi") == 0)
10069         mips_opts.abi = EABI_ABI;
10070       else
10071         mips_opts.abi = NO_ABI;
10072       break;
10073 #endif /* OBJ_ELF */
10074
10075     case OPTION_M7000_HILO_FIX:
10076       mips_7000_hilo_fix = true;
10077       break;
10078
10079     case OPTION_NO_M7000_HILO_FIX:
10080       mips_7000_hilo_fix = false;
10081       break;
10082
10083     default:
10084       return 0;
10085     }
10086
10087   return 1;
10088 }
10089
10090 static void
10091 show (stream, string, col_p, first_p)
10092      FILE *stream;
10093      char *string;
10094      int *col_p;
10095      int *first_p;
10096 {
10097   if (*first_p)
10098     {
10099       fprintf (stream, "%24s", "");
10100       *col_p = 24;
10101     }
10102   else
10103     {
10104       fprintf (stream, ", ");
10105       *col_p += 2;
10106     }
10107
10108   if (*col_p + strlen (string) > 72)
10109     {
10110       fprintf (stream, "\n%24s", "");
10111       *col_p = 24;
10112     }
10113
10114   fprintf (stream, "%s", string);
10115   *col_p += strlen (string);
10116
10117   *first_p = 0;
10118 }
10119
10120 void
10121 md_show_usage (stream)
10122      FILE *stream;
10123 {
10124   int column, first;
10125
10126   fprintf (stream, _("\
10127 MIPS options:\n\
10128 -membedded-pic          generate embedded position independent code\n\
10129 -EB                     generate big endian output\n\
10130 -EL                     generate little endian output\n\
10131 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
10132 -G NUM                  allow referencing objects up to NUM bytes\n\
10133                         implicitly with the gp register [default 8]\n"));
10134   fprintf (stream, _("\
10135 -mips1                  generate MIPS ISA I instructions\n\
10136 -mips2                  generate MIPS ISA II instructions\n\
10137 -mips3                  generate MIPS ISA III instructions\n\
10138 -mips4                  generate MIPS ISA IV instructions\n\
10139 -mips5                  generate MIPS ISA V instructions\n\
10140 -mips32                 generate MIPS32 ISA instructions\n\
10141 -mips64                 generate MIPS64 ISA instructions\n\
10142 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
10143
10144   first = 1;
10145
10146   show (stream, "2000", &column, &first);
10147   show (stream, "3000", &column, &first);
10148   show (stream, "3900", &column, &first);
10149   show (stream, "4000", &column, &first);
10150   show (stream, "4010", &column, &first);
10151   show (stream, "4100", &column, &first);
10152   show (stream, "4111", &column, &first);
10153   show (stream, "4300", &column, &first);
10154   show (stream, "4400", &column, &first);
10155   show (stream, "4600", &column, &first);
10156   show (stream, "4650", &column, &first);
10157   show (stream, "5000", &column, &first);
10158   show (stream, "5200", &column, &first);
10159   show (stream, "5230", &column, &first);
10160   show (stream, "5231", &column, &first);
10161   show (stream, "5261", &column, &first);
10162   show (stream, "5721", &column, &first);
10163   show (stream, "6000", &column, &first);
10164   show (stream, "8000", &column, &first);
10165   show (stream, "10000", &column, &first);
10166   show (stream, "12000", &column, &first);
10167   show (stream, "sb1", &column, &first);
10168   fputc ('\n', stream);
10169
10170   fprintf (stream, _("\
10171 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10172 -no-mCPU                don't generate code specific to CPU.\n\
10173                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
10174
10175   first = 1;
10176
10177   show (stream, "3900", &column, &first);
10178   show (stream, "4010", &column, &first);
10179   show (stream, "4100", &column, &first);
10180   show (stream, "4650", &column, &first);
10181   fputc ('\n', stream);
10182
10183   fprintf (stream, _("\
10184 -mips16                 generate mips16 instructions\n\
10185 -no-mips16              do not generate mips16 instructions\n"));
10186   fprintf (stream, _("\
10187 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
10188 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
10189 -O0                     remove unneeded NOPs, do not swap branches\n\
10190 -O                      remove unneeded NOPs and swap branches\n\
10191 -n                      warn about NOPs generated from macros\n\
10192 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10193 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
10194 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
10195 #ifdef OBJ_ELF
10196   fprintf (stream, _("\
10197 -KPIC, -call_shared     generate SVR4 position independent code\n\
10198 -non_shared             do not generate position independent code\n\
10199 -xgot                   assume a 32 bit GOT\n\
10200 -mabi=ABI               create ABI conformant object file for:\n"));
10201
10202   first = 1;
10203
10204   show (stream, "32", &column, &first);
10205   show (stream, "o64", &column, &first);
10206   show (stream, "n32", &column, &first);
10207   show (stream, "64", &column, &first);
10208   show (stream, "eabi", &column, &first);
10209                              
10210   fputc ('\n', stream);
10211
10212   fprintf (stream, _("\
10213 -32                     create o32 ABI object file (default)\n\
10214 -n32                    create n32 ABI object file\n\
10215 -64                     create 64 ABI object file\n"));
10216 #endif
10217 }
10218 \f
10219 void
10220 mips_init_after_args ()
10221 {
10222   /* initialize opcodes */
10223   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10224   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10225 }
10226
10227 long
10228 md_pcrel_from (fixP)
10229      fixS *fixP;
10230 {
10231   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10232       && fixP->fx_addsy != (symbolS *) NULL
10233       && ! S_IS_DEFINED (fixP->fx_addsy))
10234     {
10235       /* This makes a branch to an undefined symbol be a branch to the
10236          current location.  */
10237       if (mips_pic == EMBEDDED_PIC)
10238         return 4;
10239       else
10240         return 1;
10241     }
10242
10243   /* return the address of the delay slot */
10244   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10245 }
10246
10247 /* This is called before the symbol table is processed.  In order to
10248    work with gcc when using mips-tfile, we must keep all local labels.
10249    However, in other cases, we want to discard them.  If we were
10250    called with -g, but we didn't see any debugging information, it may
10251    mean that gcc is smuggling debugging information through to
10252    mips-tfile, in which case we must generate all local labels.  */
10253
10254 void
10255 mips_frob_file_before_adjust ()
10256 {
10257 #ifndef NO_ECOFF_DEBUGGING
10258   if (ECOFF_DEBUGGING
10259       && mips_debug != 0
10260       && ! ecoff_debugging_seen)
10261     flag_keep_locals = 1;
10262 #endif
10263 }
10264
10265 /* Sort any unmatched HI16_S relocs so that they immediately precede
10266    the corresponding LO reloc.  This is called before md_apply_fix3 and
10267    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10268    explicit use of the %hi modifier.  */
10269
10270 void
10271 mips_frob_file ()
10272 {
10273   struct mips_hi_fixup *l;
10274
10275   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10276     {
10277       segment_info_type *seginfo;
10278       int pass;
10279
10280       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10281
10282       /* Check quickly whether the next fixup happens to be a matching
10283          %lo.  */
10284       if (l->fixp->fx_next != NULL
10285           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10286           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10287           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10288         continue;
10289
10290       /* Look through the fixups for this segment for a matching %lo.
10291          When we find one, move the %hi just in front of it.  We do
10292          this in two passes.  In the first pass, we try to find a
10293          unique %lo.  In the second pass, we permit multiple %hi
10294          relocs for a single %lo (this is a GNU extension).  */
10295       seginfo = seg_info (l->seg);
10296       for (pass = 0; pass < 2; pass++)
10297         {
10298           fixS *f, *prev;
10299
10300           prev = NULL;
10301           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10302             {
10303               /* Check whether this is a %lo fixup which matches l->fixp.  */
10304               if (f->fx_r_type == BFD_RELOC_LO16
10305                   && f->fx_addsy == l->fixp->fx_addsy
10306                   && f->fx_offset == l->fixp->fx_offset
10307                   && (pass == 1
10308                       || prev == NULL
10309                       || prev->fx_r_type != BFD_RELOC_HI16_S
10310                       || prev->fx_addsy != f->fx_addsy
10311                       || prev->fx_offset !=  f->fx_offset))
10312                 {
10313                   fixS **pf;
10314
10315                   /* Move l->fixp before f.  */
10316                   for (pf = &seginfo->fix_root;
10317                        *pf != l->fixp;
10318                        pf = &(*pf)->fx_next)
10319                     assert (*pf != NULL);
10320
10321                   *pf = l->fixp->fx_next;
10322
10323                   l->fixp->fx_next = f;
10324                   if (prev == NULL)
10325                     seginfo->fix_root = l->fixp;
10326                   else
10327                     prev->fx_next = l->fixp;
10328
10329                   break;
10330                 }
10331
10332               prev = f;
10333             }
10334
10335           if (f != NULL)
10336             break;
10337
10338 #if 0 /* GCC code motion plus incomplete dead code elimination
10339          can leave a %hi without a %lo.  */
10340           if (pass == 1)
10341             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10342                            _("Unmatched %%hi reloc"));
10343 #endif
10344         }
10345     }
10346 }
10347
10348 /* When generating embedded PIC code we need to use a special
10349    relocation to represent the difference of two symbols in the .text
10350    section (switch tables use a difference of this sort).  See
10351    include/coff/mips.h for details.  This macro checks whether this
10352    fixup requires the special reloc.  */
10353 #define SWITCH_TABLE(fixp) \
10354   ((fixp)->fx_r_type == BFD_RELOC_32 \
10355    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10356    && (fixp)->fx_addsy != NULL \
10357    && (fixp)->fx_subsy != NULL \
10358    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10359    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10360
10361 /* When generating embedded PIC code we must keep all PC relative
10362    relocations, in case the linker has to relax a call.  We also need
10363    to keep relocations for switch table entries.
10364
10365    We may have combined relocations without symbols in the N32/N64 ABI.
10366    We have to prevent gas from dropping them.  */
10367
10368 int
10369 mips_force_relocation (fixp)
10370      fixS *fixp;
10371 {
10372   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10373       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10374     return 1;
10375
10376   if (HAVE_NEWABI
10377       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10378       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10379           || fixp->fx_r_type == BFD_RELOC_HI16_S
10380           || fixp->fx_r_type == BFD_RELOC_LO16))
10381     return 1;
10382
10383   return (mips_pic == EMBEDDED_PIC
10384           && (fixp->fx_pcrel
10385               || SWITCH_TABLE (fixp)
10386               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10387               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10388 }
10389
10390 #ifdef OBJ_ELF
10391 static int
10392 mips_need_elf_addend_fixup (fixP)
10393      fixS *fixP;
10394 {
10395   return (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16
10396           || ((S_IS_WEAK (fixP->fx_addsy)
10397                || S_IS_EXTERN (fixP->fx_addsy))
10398               && !S_IS_COMMON (fixP->fx_addsy))
10399           || (symbol_used_in_reloc_p (fixP->fx_addsy)
10400               && (((bfd_get_section_flags (stdoutput,
10401                                            S_GET_SEGMENT (fixP->fx_addsy))
10402                     & SEC_LINK_ONCE) != 0)
10403                   || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10404                                ".gnu.linkonce",
10405                                sizeof (".gnu.linkonce") - 1))));
10406 }
10407 #endif
10408
10409 /* Apply a fixup to the object file.  */
10410
10411 void
10412 md_apply_fix3 (fixP, valP, seg)
10413      fixS *fixP;
10414      valueT * valP;
10415      segT seg ATTRIBUTE_UNUSED;
10416 {
10417   unsigned char *buf;
10418   long insn;
10419   valueT value;
10420
10421   assert (fixP->fx_size == 4
10422           || fixP->fx_r_type == BFD_RELOC_16
10423           || fixP->fx_r_type == BFD_RELOC_32
10424           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10425           || fixP->fx_r_type == BFD_RELOC_HI16_S
10426           || fixP->fx_r_type == BFD_RELOC_LO16
10427           || fixP->fx_r_type == BFD_RELOC_GPREL16
10428           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10429           || fixP->fx_r_type == BFD_RELOC_GPREL32
10430           || fixP->fx_r_type == BFD_RELOC_64
10431           || fixP->fx_r_type == BFD_RELOC_CTOR
10432           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10433           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10434           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10435           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10436           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10437           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10438           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10439           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10440
10441   value = * valP;
10442
10443   /* If we aren't adjusting this fixup to be against the section
10444      symbol, we need to adjust the value.  */
10445 #ifdef OBJ_ELF
10446   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10447     {
10448       if (mips_need_elf_addend_fixup (fixP))
10449         {
10450           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10451
10452           value -= symval;
10453           if (value != 0 && ! fixP->fx_pcrel)
10454             {
10455               /* In this case, the bfd_install_relocation routine will
10456                  incorrectly add the symbol value back in.  We just want
10457                  the addend to appear in the object file.  */
10458               value -= symval;
10459
10460               /* Make sure the addend is still non-zero.  If it became zero
10461                  after the last operation, set it to a spurious value and
10462                  subtract the same value from the object file's contents.  */
10463               if (value == 0)
10464                 {
10465                   value = 8;
10466
10467                   /* The in-place addends for LO16 relocations are signed;
10468                      leave the matching HI16 in-place addends as zero.  */
10469                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10470                     {
10471                       reloc_howto_type *howto;
10472                       bfd_vma contents, mask, field;
10473
10474                       howto = bfd_reloc_type_lookup (stdoutput,
10475                                                      fixP->fx_r_type);
10476
10477                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10478                                                + fixP->fx_where,
10479                                                fixP->fx_size * 8,
10480                                                target_big_endian);
10481
10482                       /* MASK has bits set where the relocation should go.
10483                          FIELD is -value, shifted into the appropriate place
10484                          for this relocation.  */
10485                       mask = 1 << (howto->bitsize - 1);
10486                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10487                       field = (-value >> howto->rightshift) << howto->bitpos;
10488
10489                       bfd_put_bits ((field & mask) | (contents & ~mask),
10490                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10491                                     fixP->fx_size * 8,
10492                                     target_big_endian);
10493                     }
10494                 }
10495             }
10496         }
10497
10498       /* This code was generated using trial and error and so is
10499          fragile and not trustworthy.  If you change it, you should
10500          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10501          they still pass.  */
10502       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10503         {
10504           value += fixP->fx_frag->fr_address + fixP->fx_where;
10505
10506           /* BFD's REL handling, for MIPS, is _very_ weird.
10507              This gives the right results, but it can't possibly
10508              be the way things are supposed to work.  */
10509           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10510                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10511               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10512             value += fixP->fx_frag->fr_address + fixP->fx_where;
10513         }
10514     }
10515 #endif
10516
10517   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10518
10519   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10520     fixP->fx_done = 1;
10521
10522   switch (fixP->fx_r_type)
10523     {
10524     case BFD_RELOC_MIPS_JMP:
10525     case BFD_RELOC_MIPS_SHIFT5:
10526     case BFD_RELOC_MIPS_SHIFT6:
10527     case BFD_RELOC_MIPS_GOT_DISP:
10528     case BFD_RELOC_MIPS_GOT_PAGE:
10529     case BFD_RELOC_MIPS_GOT_OFST:
10530     case BFD_RELOC_MIPS_SUB:
10531     case BFD_RELOC_MIPS_INSERT_A:
10532     case BFD_RELOC_MIPS_INSERT_B:
10533     case BFD_RELOC_MIPS_DELETE:
10534     case BFD_RELOC_MIPS_HIGHEST:
10535     case BFD_RELOC_MIPS_HIGHER:
10536     case BFD_RELOC_MIPS_SCN_DISP:
10537     case BFD_RELOC_MIPS_REL16:
10538     case BFD_RELOC_MIPS_RELGOT:
10539     case BFD_RELOC_MIPS_JALR:
10540     case BFD_RELOC_HI16:
10541     case BFD_RELOC_HI16_S:
10542     case BFD_RELOC_GPREL16:
10543     case BFD_RELOC_MIPS_LITERAL:
10544     case BFD_RELOC_MIPS_CALL16:
10545     case BFD_RELOC_MIPS_GOT16:
10546     case BFD_RELOC_GPREL32:
10547     case BFD_RELOC_MIPS_GOT_HI16:
10548     case BFD_RELOC_MIPS_GOT_LO16:
10549     case BFD_RELOC_MIPS_CALL_HI16:
10550     case BFD_RELOC_MIPS_CALL_LO16:
10551     case BFD_RELOC_MIPS16_GPREL:
10552       if (fixP->fx_pcrel)
10553         as_bad_where (fixP->fx_file, fixP->fx_line,
10554                       _("Invalid PC relative reloc"));
10555       /* Nothing needed to do. The value comes from the reloc entry */
10556       break;
10557
10558     case BFD_RELOC_MIPS16_JMP:
10559       /* We currently always generate a reloc against a symbol, which
10560          means that we don't want an addend even if the symbol is
10561          defined.  */
10562       fixP->fx_addnumber = 0;
10563       break;
10564
10565     case BFD_RELOC_PCREL_HI16_S:
10566       /* The addend for this is tricky if it is internal, so we just
10567          do everything here rather than in bfd_install_relocation.  */
10568       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10569           && !fixP->fx_done
10570           && value != 0)
10571         break;
10572       if (fixP->fx_addsy
10573           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10574         {
10575           /* For an external symbol adjust by the address to make it
10576              pcrel_offset.  We use the address of the RELLO reloc
10577              which follows this one.  */
10578           value += (fixP->fx_next->fx_frag->fr_address
10579                     + fixP->fx_next->fx_where);
10580         }
10581       value = ((value + 0x8000) >> 16) & 0xffff;
10582       buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10583       if (target_big_endian)
10584         buf += 2;
10585       md_number_to_chars (buf, value, 2);
10586       break;
10587
10588     case BFD_RELOC_PCREL_LO16:
10589       /* The addend for this is tricky if it is internal, so we just
10590          do everything here rather than in bfd_install_relocation.  */
10591       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10592           && !fixP->fx_done
10593           && value != 0)
10594         break;
10595       if (fixP->fx_addsy
10596           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10597         value += fixP->fx_frag->fr_address + fixP->fx_where;
10598       buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10599       if (target_big_endian)
10600         buf += 2;
10601       md_number_to_chars (buf, value, 2);
10602       break;
10603
10604     case BFD_RELOC_64:
10605       /* This is handled like BFD_RELOC_32, but we output a sign
10606          extended value if we are only 32 bits.  */
10607       if (fixP->fx_done
10608           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10609         {
10610           if (8 <= sizeof (valueT))
10611             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10612                                 value, 8);
10613           else
10614             {
10615               long w1, w2;
10616               long hiv;
10617
10618               w1 = w2 = fixP->fx_where;
10619               if (target_big_endian)
10620                 w1 += 4;
10621               else
10622                 w2 += 4;
10623               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10624               if ((value & 0x80000000) != 0)
10625                 hiv = 0xffffffff;
10626               else
10627                 hiv = 0;
10628               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10629             }
10630         }
10631       break;
10632
10633     case BFD_RELOC_RVA:
10634     case BFD_RELOC_32:
10635       /* If we are deleting this reloc entry, we must fill in the
10636          value now.  This can happen if we have a .word which is not
10637          resolved when it appears but is later defined.  We also need
10638          to fill in the value if this is an embedded PIC switch table
10639          entry.  */
10640       if (fixP->fx_done
10641           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10642         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10643                             value, 4);
10644       break;
10645
10646     case BFD_RELOC_16:
10647       /* If we are deleting this reloc entry, we must fill in the
10648          value now.  */
10649       assert (fixP->fx_size == 2);
10650       if (fixP->fx_done)
10651         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10652                             value, 2);
10653       break;
10654
10655     case BFD_RELOC_LO16:
10656       /* When handling an embedded PIC switch statement, we can wind
10657          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
10658       if (fixP->fx_done)
10659         {
10660           if (value + 0x8000 > 0xffff)
10661             as_bad_where (fixP->fx_file, fixP->fx_line,
10662                           _("relocation overflow"));
10663           buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10664           if (target_big_endian)
10665             buf += 2;
10666           md_number_to_chars (buf, value, 2);
10667         }
10668       break;
10669
10670     case BFD_RELOC_16_PCREL_S2:
10671       if ((value & 0x3) != 0)
10672         as_bad_where (fixP->fx_file, fixP->fx_line,
10673                       _("Branch to odd address (%lx)"), (long) value);
10674
10675       /* Fall through.  */
10676
10677     case BFD_RELOC_16_PCREL:
10678       /*
10679        * We need to save the bits in the instruction since fixup_segment()
10680        * might be deleting the relocation entry (i.e., a branch within
10681        * the current segment).
10682        */
10683       if (!fixP->fx_done && value != 0)
10684         break;
10685       /* If 'value' is zero, the remaining reloc code won't actually
10686          do the store, so it must be done here.  This is probably
10687          a bug somewhere.  */
10688       if (!fixP->fx_done)
10689         value -= fixP->fx_frag->fr_address + fixP->fx_where;
10690
10691       value = (offsetT) value >> 2;
10692
10693       /* update old instruction data */
10694       buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10695       if (target_big_endian)
10696         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10697       else
10698         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10699
10700       if (value + 0x8000 <= 0xffff)
10701         insn |= value & 0xffff;
10702       else
10703         {
10704           /* The branch offset is too large.  If this is an
10705              unconditional branch, and we are not generating PIC code,
10706              we can convert it to an absolute jump instruction.  */
10707           if (mips_pic == NO_PIC
10708               && fixP->fx_done
10709               && fixP->fx_frag->fr_address >= text_section->vma
10710               && (fixP->fx_frag->fr_address
10711                   < text_section->vma + text_section->_raw_size)
10712               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
10713                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
10714                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10715             {
10716               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
10717                 insn = 0x0c000000;      /* jal */
10718               else
10719                 insn = 0x08000000;      /* j */
10720               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10721               fixP->fx_done = 0;
10722               fixP->fx_addsy = section_symbol (text_section);
10723               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10724             }
10725           else
10726             {
10727               /* FIXME.  It would be possible in principle to handle
10728                  conditional branches which overflow.  They could be
10729                  transformed into a branch around a jump.  This would
10730                  require setting up variant frags for each different
10731                  branch type.  The native MIPS assembler attempts to
10732                  handle these cases, but it appears to do it
10733                  incorrectly.  */
10734               as_bad_where (fixP->fx_file, fixP->fx_line,
10735                             _("Branch out of range"));
10736             }
10737         }
10738
10739       md_number_to_chars ((char *) buf, (valueT) insn, 4);
10740       break;
10741
10742     case BFD_RELOC_VTABLE_INHERIT:
10743       fixP->fx_done = 0;
10744       if (fixP->fx_addsy
10745           && !S_IS_DEFINED (fixP->fx_addsy)
10746           && !S_IS_WEAK (fixP->fx_addsy))
10747         S_SET_WEAK (fixP->fx_addsy);
10748       break;
10749
10750     case BFD_RELOC_VTABLE_ENTRY:
10751       fixP->fx_done = 0;
10752       break;
10753
10754     default:
10755       internalError ();
10756     }
10757 }
10758
10759 #if 0
10760 void
10761 printInsn (oc)
10762      unsigned long oc;
10763 {
10764   const struct mips_opcode *p;
10765   int treg, sreg, dreg, shamt;
10766   short imm;
10767   const char *args;
10768   int i;
10769
10770   for (i = 0; i < NUMOPCODES; ++i)
10771     {
10772       p = &mips_opcodes[i];
10773       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10774         {
10775           printf ("%08lx %s\t", oc, p->name);
10776           treg = (oc >> 16) & 0x1f;
10777           sreg = (oc >> 21) & 0x1f;
10778           dreg = (oc >> 11) & 0x1f;
10779           shamt = (oc >> 6) & 0x1f;
10780           imm = oc;
10781           for (args = p->args;; ++args)
10782             {
10783               switch (*args)
10784                 {
10785                 case '\0':
10786                   printf ("\n");
10787                   break;
10788
10789                 case ',':
10790                 case '(':
10791                 case ')':
10792                   printf ("%c", *args);
10793                   continue;
10794
10795                 case 'r':
10796                   assert (treg == sreg);
10797                   printf ("$%d,$%d", treg, sreg);
10798                   continue;
10799
10800                 case 'd':
10801                 case 'G':
10802                   printf ("$%d", dreg);
10803                   continue;
10804
10805                 case 't':
10806                 case 'E':
10807                   printf ("$%d", treg);
10808                   continue;
10809
10810                 case 'k':
10811                   printf ("0x%x", treg);
10812                   continue;
10813
10814                 case 'b':
10815                 case 's':
10816                   printf ("$%d", sreg);
10817                   continue;
10818
10819                 case 'a':
10820                   printf ("0x%08lx", oc & 0x1ffffff);
10821                   continue;
10822
10823                 case 'i':
10824                 case 'j':
10825                 case 'o':
10826                 case 'u':
10827                   printf ("%d", imm);
10828                   continue;
10829
10830                 case '<':
10831                 case '>':
10832                   printf ("$%d", shamt);
10833                   continue;
10834
10835                 default:
10836                   internalError ();
10837                 }
10838               break;
10839             }
10840           return;
10841         }
10842     }
10843   printf (_("%08lx  UNDEFINED\n"), oc);
10844 }
10845 #endif
10846
10847 static symbolS *
10848 get_symbol ()
10849 {
10850   int c;
10851   char *name;
10852   symbolS *p;
10853
10854   name = input_line_pointer;
10855   c = get_symbol_end ();
10856   p = (symbolS *) symbol_find_or_make (name);
10857   *input_line_pointer = c;
10858   return p;
10859 }
10860
10861 /* Align the current frag to a given power of two.  The MIPS assembler
10862    also automatically adjusts any preceding label.  */
10863
10864 static void
10865 mips_align (to, fill, label)
10866      int to;
10867      int fill;
10868      symbolS *label;
10869 {
10870   mips_emit_delays (false);
10871   frag_align (to, fill, 0);
10872   record_alignment (now_seg, to);
10873   if (label != NULL)
10874     {
10875       assert (S_GET_SEGMENT (label) == now_seg);
10876       symbol_set_frag (label, frag_now);
10877       S_SET_VALUE (label, (valueT) frag_now_fix ());
10878     }
10879 }
10880
10881 /* Align to a given power of two.  .align 0 turns off the automatic
10882    alignment used by the data creating pseudo-ops.  */
10883
10884 static void
10885 s_align (x)
10886      int x ATTRIBUTE_UNUSED;
10887 {
10888   register int temp;
10889   register long temp_fill;
10890   long max_alignment = 15;
10891
10892   /*
10893
10894     o  Note that the assembler pulls down any immediately preceeding label
10895        to the aligned address.
10896     o  It's not documented but auto alignment is reinstated by
10897        a .align pseudo instruction.
10898     o  Note also that after auto alignment is turned off the mips assembler
10899        issues an error on attempt to assemble an improperly aligned data item.
10900        We don't.
10901
10902     */
10903
10904   temp = get_absolute_expression ();
10905   if (temp > max_alignment)
10906     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10907   else if (temp < 0)
10908     {
10909       as_warn (_("Alignment negative: 0 assumed."));
10910       temp = 0;
10911     }
10912   if (*input_line_pointer == ',')
10913     {
10914       input_line_pointer++;
10915       temp_fill = get_absolute_expression ();
10916     }
10917   else
10918     temp_fill = 0;
10919   if (temp)
10920     {
10921       auto_align = 1;
10922       mips_align (temp, (int) temp_fill,
10923                   insn_labels != NULL ? insn_labels->label : NULL);
10924     }
10925   else
10926     {
10927       auto_align = 0;
10928     }
10929
10930   demand_empty_rest_of_line ();
10931 }
10932
10933 void
10934 mips_flush_pending_output ()
10935 {
10936   mips_emit_delays (false);
10937   mips_clear_insn_labels ();
10938 }
10939
10940 static void
10941 s_change_sec (sec)
10942      int sec;
10943 {
10944   segT seg;
10945
10946   /* When generating embedded PIC code, we only use the .text, .lit8,
10947      .sdata and .sbss sections.  We change the .data and .rdata
10948      pseudo-ops to use .sdata.  */
10949   if (mips_pic == EMBEDDED_PIC
10950       && (sec == 'd' || sec == 'r'))
10951     sec = 's';
10952
10953 #ifdef OBJ_ELF
10954   /* The ELF backend needs to know that we are changing sections, so
10955      that .previous works correctly.  We could do something like check
10956      for an obj_section_change_hook macro, but that might be confusing
10957      as it would not be appropriate to use it in the section changing
10958      functions in read.c, since obj-elf.c intercepts those.  FIXME:
10959      This should be cleaner, somehow.  */
10960   obj_elf_section_change_hook ();
10961 #endif
10962
10963   mips_emit_delays (false);
10964   switch (sec)
10965     {
10966     case 't':
10967       s_text (0);
10968       break;
10969     case 'd':
10970       s_data (0);
10971       break;
10972     case 'b':
10973       subseg_set (bss_section, (subsegT) get_absolute_expression ());
10974       demand_empty_rest_of_line ();
10975       break;
10976
10977     case 'r':
10978       if (USE_GLOBAL_POINTER_OPT)
10979         {
10980           seg = subseg_new (RDATA_SECTION_NAME,
10981                             (subsegT) get_absolute_expression ());
10982           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10983             {
10984               bfd_set_section_flags (stdoutput, seg,
10985                                      (SEC_ALLOC
10986                                       | SEC_LOAD
10987                                       | SEC_READONLY
10988                                       | SEC_RELOC
10989                                       | SEC_DATA));
10990               if (strcmp (TARGET_OS, "elf") != 0)
10991                 record_alignment (seg, 4);
10992             }
10993           demand_empty_rest_of_line ();
10994         }
10995       else
10996         {
10997           as_bad (_("No read only data section in this object file format"));
10998           demand_empty_rest_of_line ();
10999           return;
11000         }
11001       break;
11002
11003     case 's':
11004       if (USE_GLOBAL_POINTER_OPT)
11005         {
11006           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11007           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11008             {
11009               bfd_set_section_flags (stdoutput, seg,
11010                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11011                                      | SEC_DATA);
11012               if (strcmp (TARGET_OS, "elf") != 0)
11013                 record_alignment (seg, 4);
11014             }
11015           demand_empty_rest_of_line ();
11016           break;
11017         }
11018       else
11019         {
11020           as_bad (_("Global pointers not supported; recompile -G 0"));
11021           demand_empty_rest_of_line ();
11022           return;
11023         }
11024     }
11025
11026   auto_align = 1;
11027 }
11028
11029 void
11030 mips_enable_auto_align ()
11031 {
11032   auto_align = 1;
11033 }
11034
11035 static void
11036 s_cons (log_size)
11037      int log_size;
11038 {
11039   symbolS *label;
11040
11041   label = insn_labels != NULL ? insn_labels->label : NULL;
11042   mips_emit_delays (false);
11043   if (log_size > 0 && auto_align)
11044     mips_align (log_size, 0, label);
11045   mips_clear_insn_labels ();
11046   cons (1 << log_size);
11047 }
11048
11049 static void
11050 s_float_cons (type)
11051      int type;
11052 {
11053   symbolS *label;
11054
11055   label = insn_labels != NULL ? insn_labels->label : NULL;
11056
11057   mips_emit_delays (false);
11058
11059   if (auto_align)
11060     {
11061       if (type == 'd')
11062         mips_align (3, 0, label);
11063       else
11064         mips_align (2, 0, label);
11065     }
11066
11067   mips_clear_insn_labels ();
11068
11069   float_cons (type);
11070 }
11071
11072 /* Handle .globl.  We need to override it because on Irix 5 you are
11073    permitted to say
11074        .globl foo .text
11075    where foo is an undefined symbol, to mean that foo should be
11076    considered to be the address of a function.  */
11077
11078 static void
11079 s_mips_globl (x)
11080      int x ATTRIBUTE_UNUSED;
11081 {
11082   char *name;
11083   int c;
11084   symbolS *symbolP;
11085   flagword flag;
11086
11087   name = input_line_pointer;
11088   c = get_symbol_end ();
11089   symbolP = symbol_find_or_make (name);
11090   *input_line_pointer = c;
11091   SKIP_WHITESPACE ();
11092
11093   /* On Irix 5, every global symbol that is not explicitly labelled as
11094      being a function is apparently labelled as being an object.  */
11095   flag = BSF_OBJECT;
11096
11097   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11098     {
11099       char *secname;
11100       asection *sec;
11101
11102       secname = input_line_pointer;
11103       c = get_symbol_end ();
11104       sec = bfd_get_section_by_name (stdoutput, secname);
11105       if (sec == NULL)
11106         as_bad (_("%s: no such section"), secname);
11107       *input_line_pointer = c;
11108
11109       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11110         flag = BSF_FUNCTION;
11111     }
11112
11113   symbol_get_bfdsym (symbolP)->flags |= flag;
11114
11115   S_SET_EXTERNAL (symbolP);
11116   demand_empty_rest_of_line ();
11117 }
11118
11119 static void
11120 s_option (x)
11121      int x ATTRIBUTE_UNUSED;
11122 {
11123   char *opt;
11124   char c;
11125
11126   opt = input_line_pointer;
11127   c = get_symbol_end ();
11128
11129   if (*opt == 'O')
11130     {
11131       /* FIXME: What does this mean?  */
11132     }
11133   else if (strncmp (opt, "pic", 3) == 0)
11134     {
11135       int i;
11136
11137       i = atoi (opt + 3);
11138       if (i == 0)
11139         mips_pic = NO_PIC;
11140       else if (i == 2)
11141         mips_pic = SVR4_PIC;
11142       else
11143         as_bad (_(".option pic%d not supported"), i);
11144
11145       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11146         {
11147           if (g_switch_seen && g_switch_value != 0)
11148             as_warn (_("-G may not be used with SVR4 PIC code"));
11149           g_switch_value = 0;
11150           bfd_set_gp_size (stdoutput, 0);
11151         }
11152     }
11153   else
11154     as_warn (_("Unrecognized option \"%s\""), opt);
11155
11156   *input_line_pointer = c;
11157   demand_empty_rest_of_line ();
11158 }
11159
11160 /* This structure is used to hold a stack of .set values.  */
11161
11162 struct mips_option_stack
11163 {
11164   struct mips_option_stack *next;
11165   struct mips_set_options options;
11166 };
11167
11168 static struct mips_option_stack *mips_opts_stack;
11169
11170 /* Handle the .set pseudo-op.  */
11171
11172 static void
11173 s_mipsset (x)
11174      int x ATTRIBUTE_UNUSED;
11175 {
11176   char *name = input_line_pointer, ch;
11177
11178   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11179     input_line_pointer++;
11180   ch = *input_line_pointer;
11181   *input_line_pointer = '\0';
11182
11183   if (strcmp (name, "reorder") == 0)
11184     {
11185       if (mips_opts.noreorder && prev_nop_frag != NULL)
11186         {
11187           /* If we still have pending nops, we can discard them.  The
11188              usual nop handling will insert any that are still
11189              needed.  */
11190           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11191                                     * (mips_opts.mips16 ? 2 : 4));
11192           prev_nop_frag = NULL;
11193         }
11194       mips_opts.noreorder = 0;
11195     }
11196   else if (strcmp (name, "noreorder") == 0)
11197     {
11198       mips_emit_delays (true);
11199       mips_opts.noreorder = 1;
11200       mips_any_noreorder = 1;
11201     }
11202   else if (strcmp (name, "at") == 0)
11203     {
11204       mips_opts.noat = 0;
11205     }
11206   else if (strcmp (name, "noat") == 0)
11207     {
11208       mips_opts.noat = 1;
11209     }
11210   else if (strcmp (name, "macro") == 0)
11211     {
11212       mips_opts.warn_about_macros = 0;
11213     }
11214   else if (strcmp (name, "nomacro") == 0)
11215     {
11216       if (mips_opts.noreorder == 0)
11217         as_bad (_("`noreorder' must be set before `nomacro'"));
11218       mips_opts.warn_about_macros = 1;
11219     }
11220   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11221     {
11222       mips_opts.nomove = 0;
11223     }
11224   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11225     {
11226       mips_opts.nomove = 1;
11227     }
11228   else if (strcmp (name, "bopt") == 0)
11229     {
11230       mips_opts.nobopt = 0;
11231     }
11232   else if (strcmp (name, "nobopt") == 0)
11233     {
11234       mips_opts.nobopt = 1;
11235     }
11236   else if (strcmp (name, "mips16") == 0
11237            || strcmp (name, "MIPS-16") == 0)
11238     mips_opts.mips16 = 1;
11239   else if (strcmp (name, "nomips16") == 0
11240            || strcmp (name, "noMIPS-16") == 0)
11241     mips_opts.mips16 = 0;
11242   else if (strncmp (name, "mips", 4) == 0)
11243     {
11244       int isa;
11245
11246       /* Permit the user to change the ISA on the fly.  Needless to
11247          say, misuse can cause serious problems.  */
11248       isa = atoi (name + 4);
11249       switch (isa)
11250       {
11251       case  0:
11252         mips_opts.gp32 = file_mips_gp32;
11253         mips_opts.fp32 = file_mips_fp32;
11254         mips_opts.abi = file_mips_abi;
11255         break;
11256       case  1:
11257       case  2:
11258       case 32:
11259         mips_opts.gp32 = 1;
11260         mips_opts.fp32 = 1;
11261         break;
11262       case  3:
11263       case  4:
11264       case  5:
11265       case 64:
11266         /* Loosen ABI register width restriction.  */
11267         if (mips_opts.abi == O32_ABI)
11268           mips_opts.abi = NO_ABI;
11269         mips_opts.gp32 = 0;
11270         mips_opts.fp32 = 0;
11271         break;
11272       default:
11273         as_bad (_("unknown ISA level %s"), name + 4);
11274         break;
11275       }
11276
11277       switch (isa)
11278       {
11279       case  0: mips_opts.isa = file_mips_isa;   break;
11280       case  1: mips_opts.isa = ISA_MIPS1;       break;
11281       case  2: mips_opts.isa = ISA_MIPS2;       break;
11282       case  3: mips_opts.isa = ISA_MIPS3;       break;
11283       case  4: mips_opts.isa = ISA_MIPS4;       break;
11284       case  5: mips_opts.isa = ISA_MIPS5;       break;
11285       case 32: mips_opts.isa = ISA_MIPS32;      break;
11286       case 64: mips_opts.isa = ISA_MIPS64;      break;
11287       default: as_bad (_("unknown ISA level %s"), name + 4); break;
11288       }
11289     }
11290   else if (strcmp (name, "autoextend") == 0)
11291     mips_opts.noautoextend = 0;
11292   else if (strcmp (name, "noautoextend") == 0)
11293     mips_opts.noautoextend = 1;
11294   else if (strcmp (name, "push") == 0)
11295     {
11296       struct mips_option_stack *s;
11297
11298       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11299       s->next = mips_opts_stack;
11300       s->options = mips_opts;
11301       mips_opts_stack = s;
11302     }
11303   else if (strcmp (name, "pop") == 0)
11304     {
11305       struct mips_option_stack *s;
11306
11307       s = mips_opts_stack;
11308       if (s == NULL)
11309         as_bad (_(".set pop with no .set push"));
11310       else
11311         {
11312           /* If we're changing the reorder mode we need to handle
11313              delay slots correctly.  */
11314           if (s->options.noreorder && ! mips_opts.noreorder)
11315             mips_emit_delays (true);
11316           else if (! s->options.noreorder && mips_opts.noreorder)
11317             {
11318               if (prev_nop_frag != NULL)
11319                 {
11320                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11321                                             * (mips_opts.mips16 ? 2 : 4));
11322                   prev_nop_frag = NULL;
11323                 }
11324             }
11325
11326           mips_opts = s->options;
11327           mips_opts_stack = s->next;
11328           free (s);
11329         }
11330     }
11331   else
11332     {
11333       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11334     }
11335   *input_line_pointer = ch;
11336   demand_empty_rest_of_line ();
11337 }
11338
11339 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11340    .option pic2.  It means to generate SVR4 PIC calls.  */
11341
11342 static void
11343 s_abicalls (ignore)
11344      int ignore ATTRIBUTE_UNUSED;
11345 {
11346   mips_pic = SVR4_PIC;
11347   if (USE_GLOBAL_POINTER_OPT)
11348     {
11349       if (g_switch_seen && g_switch_value != 0)
11350         as_warn (_("-G may not be used with SVR4 PIC code"));
11351       g_switch_value = 0;
11352     }
11353   bfd_set_gp_size (stdoutput, 0);
11354   demand_empty_rest_of_line ();
11355 }
11356
11357 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11358    PIC code.  It sets the $gp register for the function based on the
11359    function address, which is in the register named in the argument.
11360    This uses a relocation against _gp_disp, which is handled specially
11361    by the linker.  The result is:
11362         lui     $gp,%hi(_gp_disp)
11363         addiu   $gp,$gp,%lo(_gp_disp)
11364         addu    $gp,$gp,.cpload argument
11365    The .cpload argument is normally $25 == $t9.  */
11366
11367 static void
11368 s_cpload (ignore)
11369      int ignore ATTRIBUTE_UNUSED;
11370 {
11371   expressionS ex;
11372   int icnt = 0;
11373
11374   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11375      .cpload is ignored.  */
11376   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11377     {
11378       s_ignore (0);
11379       return;
11380     }
11381
11382   /* .cpload should be in a .set noreorder section.  */
11383   if (mips_opts.noreorder == 0)
11384     as_warn (_(".cpload not in noreorder section"));
11385
11386   ex.X_op = O_symbol;
11387   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11388   ex.X_op_symbol = NULL;
11389   ex.X_add_number = 0;
11390
11391   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11392   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11393
11394   macro_build_lui (NULL, &icnt, &ex, GP);
11395   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
11396                (int) BFD_RELOC_LO16);
11397
11398   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11399                GP, GP, tc_get_register (0));
11400
11401   demand_empty_rest_of_line ();
11402 }
11403
11404 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11405      .cpsetup $reg1, offset|$reg2, label
11406
11407    If offset is given, this results in:
11408      sd         $gp, offset($sp)
11409      lui        $gp, %hi(%neg(%gp_rel(label)))
11410      daddiu     $gp, $gp, %lo(%neg(%gp_rel(label)))
11411      addu       $gp, $gp, $reg1
11412
11413    If $reg2 is given, this results in:
11414      daddu      $reg2, $gp, $0
11415      lui        $gp, %hi(%neg(%gp_rel(label)))
11416      daddiu     $gp, $gp, %lo(%neg(%gp_rel(label)))
11417      addu       $gp, $gp, $reg1
11418  */
11419 static void
11420 s_cpsetup (ignore)
11421      int ignore ATTRIBUTE_UNUSED;
11422 {
11423   expressionS ex_off;
11424   expressionS ex_sym;
11425   int reg1;
11426   int icnt = 0;
11427   char *sym;
11428
11429   /* If we are not generating SVR4 PIC code, .cpload is ignored.
11430      We also need NewABI support.  */
11431   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11432     {
11433       s_ignore (0);
11434       return;
11435     }
11436
11437   reg1 = tc_get_register (0);
11438   SKIP_WHITESPACE ();
11439   if (*input_line_pointer != ',')
11440     {
11441       as_bad (_("missing argument separator ',' for .cpsetup"));
11442       return;
11443     }
11444   else
11445     input_line_pointer++;
11446   SKIP_WHITESPACE ();
11447   if (*input_line_pointer == '$')
11448     mips_cpreturn_register = tc_get_register (0);
11449   else
11450     mips_cpreturn_offset = get_absolute_expression ();
11451   SKIP_WHITESPACE ();
11452   if (*input_line_pointer != ',')
11453     {
11454       as_bad (_("missing argument separator ',' for .cpsetup"));
11455       return;
11456     }
11457   else
11458     input_line_pointer++;
11459   SKIP_WHITESPACE ();
11460   sym = input_line_pointer;
11461   while (ISALNUM (*input_line_pointer))
11462     input_line_pointer++;
11463   *input_line_pointer = 0;
11464
11465   ex_sym.X_op = O_symbol;
11466   ex_sym.X_add_symbol = symbol_find_or_make (sym);
11467   ex_sym.X_op_symbol = NULL;
11468   ex_sym.X_add_number = 0;
11469
11470   if (mips_cpreturn_register == -1)
11471     {
11472       ex_off.X_op = O_constant;
11473       ex_off.X_add_symbol = NULL;
11474       ex_off.X_op_symbol = NULL;
11475       ex_off.X_add_number = mips_cpreturn_offset;
11476
11477       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11478                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11479     }
11480   else
11481     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11482                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11483
11484   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11485                (int) BFD_RELOC_GPREL16);
11486   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11487            NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11488   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11489            NULL, 0, 0, BFD_RELOC_HI16_S);
11490   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11491                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11492   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11493            NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11494   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11495            NULL, 0, 0, BFD_RELOC_LO16);
11496   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11497                "d,v,t", mips_gp_register, mips_gp_register, reg1);
11498
11499   demand_empty_rest_of_line ();
11500 }
11501
11502 static void
11503 s_cplocal (ignore)
11504      int ignore ATTRIBUTE_UNUSED;
11505 {
11506   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11507    .cplocal is ignored.  */
11508   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11509     {
11510       s_ignore (0);
11511       return;
11512     }
11513
11514   mips_gp_register = tc_get_register (0);
11515 }
11516
11517 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11518    offset from $sp.  The offset is remembered, and after making a PIC
11519    call $gp is restored from that location.  */
11520
11521 static void
11522 s_cprestore (ignore)
11523      int ignore ATTRIBUTE_UNUSED;
11524 {
11525   expressionS ex;
11526   int icnt = 0;
11527
11528   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11529    .cprestore is ignored.  */
11530   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11531     {
11532       s_ignore (0);
11533       return;
11534     }
11535
11536   mips_cprestore_offset = get_absolute_expression ();
11537   mips_cprestore_valid = 1;
11538
11539   ex.X_op = O_constant;
11540   ex.X_add_symbol = NULL;
11541   ex.X_op_symbol = NULL;
11542   ex.X_add_number = mips_cprestore_offset;
11543
11544   macro_build ((char *) NULL, &icnt, &ex,
11545                HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11546                "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
11547
11548   demand_empty_rest_of_line ();
11549 }
11550
11551 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11552    was given in the preceeding .gpsetup, it results in:
11553      ld         $gp, offset($sp)
11554
11555    If a register $reg2 was given there, it results in:
11556      daddiu     $gp, $gp, $reg2
11557  */
11558 static void
11559 s_cpreturn (ignore)
11560      int ignore ATTRIBUTE_UNUSED;
11561 {
11562   expressionS ex;
11563   int icnt = 0;
11564
11565   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11566      We also need NewABI support.  */
11567   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11568     {
11569       s_ignore (0);
11570       return;
11571     }
11572
11573   if (mips_cpreturn_register == -1)
11574     {
11575       ex.X_op = O_constant;
11576       ex.X_add_symbol = NULL;
11577       ex.X_op_symbol = NULL;
11578       ex.X_add_number = mips_cpreturn_offset;
11579
11580       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11581                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11582     }
11583   else
11584     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11585                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11586
11587   demand_empty_rest_of_line ();
11588 }
11589
11590 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
11591    code.  It sets the offset to use in gp_rel relocations.  */
11592
11593 static void
11594 s_gpvalue (ignore)
11595      int ignore ATTRIBUTE_UNUSED;
11596 {
11597   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11598      We also need NewABI support.  */
11599   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11600     {
11601       s_ignore (0);
11602       return;
11603     }
11604
11605   mips_cpreturn_offset = get_absolute_expression ();
11606
11607   demand_empty_rest_of_line ();
11608 }
11609
11610 /* Handle the .gpword pseudo-op.  This is used when generating PIC
11611    code.  It generates a 32 bit GP relative reloc.  */
11612
11613 static void
11614 s_gpword (ignore)
11615      int ignore ATTRIBUTE_UNUSED;
11616 {
11617   symbolS *label;
11618   expressionS ex;
11619   char *p;
11620
11621   /* When not generating PIC code, this is treated as .word.  */
11622   if (mips_pic != SVR4_PIC)
11623     {
11624       s_cons (2);
11625       return;
11626     }
11627
11628   label = insn_labels != NULL ? insn_labels->label : NULL;
11629   mips_emit_delays (true);
11630   if (auto_align)
11631     mips_align (2, 0, label);
11632   mips_clear_insn_labels ();
11633
11634   expression (&ex);
11635
11636   if (ex.X_op != O_symbol || ex.X_add_number != 0)
11637     {
11638       as_bad (_("Unsupported use of .gpword"));
11639       ignore_rest_of_line ();
11640     }
11641
11642   p = frag_more (4);
11643   md_number_to_chars (p, (valueT) 0, 4);
11644   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
11645                BFD_RELOC_GPREL32);
11646
11647   demand_empty_rest_of_line ();
11648 }
11649
11650 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
11651    tables in SVR4 PIC code.  */
11652
11653 static void
11654 s_cpadd (ignore)
11655      int ignore ATTRIBUTE_UNUSED;
11656 {
11657   int icnt = 0;
11658   int reg;
11659
11660   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11661      code.  */
11662   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11663     {
11664       s_ignore (0);
11665       return;
11666     }
11667
11668   /* Add $gp to the register named as an argument.  */
11669   reg = tc_get_register (0);
11670   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11671                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11672                "d,v,t", reg, reg, GP);
11673
11674   demand_empty_rest_of_line ();
11675 }
11676
11677 /* Handle the .insn pseudo-op.  This marks instruction labels in
11678    mips16 mode.  This permits the linker to handle them specially,
11679    such as generating jalx instructions when needed.  We also make
11680    them odd for the duration of the assembly, in order to generate the
11681    right sort of code.  We will make them even in the adjust_symtab
11682    routine, while leaving them marked.  This is convenient for the
11683    debugger and the disassembler.  The linker knows to make them odd
11684    again.  */
11685
11686 static void
11687 s_insn (ignore)
11688      int ignore ATTRIBUTE_UNUSED;
11689 {
11690   if (mips_opts.mips16)
11691     mips16_mark_labels ();
11692
11693   demand_empty_rest_of_line ();
11694 }
11695
11696 /* Handle a .stabn directive.  We need these in order to mark a label
11697    as being a mips16 text label correctly.  Sometimes the compiler
11698    will emit a label, followed by a .stabn, and then switch sections.
11699    If the label and .stabn are in mips16 mode, then the label is
11700    really a mips16 text label.  */
11701
11702 static void
11703 s_mips_stab (type)
11704      int type;
11705 {
11706   if (type == 'n' && mips_opts.mips16)
11707     mips16_mark_labels ();
11708
11709   s_stab (type);
11710 }
11711
11712 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11713  */
11714
11715 static void
11716 s_mips_weakext (ignore)
11717      int ignore ATTRIBUTE_UNUSED;
11718 {
11719   char *name;
11720   int c;
11721   symbolS *symbolP;
11722   expressionS exp;
11723
11724   name = input_line_pointer;
11725   c = get_symbol_end ();
11726   symbolP = symbol_find_or_make (name);
11727   S_SET_WEAK (symbolP);
11728   *input_line_pointer = c;
11729
11730   SKIP_WHITESPACE ();
11731
11732   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11733     {
11734       if (S_IS_DEFINED (symbolP))
11735         {
11736           as_bad ("ignoring attempt to redefine symbol %s",
11737                   S_GET_NAME (symbolP));
11738           ignore_rest_of_line ();
11739           return;
11740         }
11741
11742       if (*input_line_pointer == ',')
11743         {
11744           ++input_line_pointer;
11745           SKIP_WHITESPACE ();
11746         }
11747
11748       expression (&exp);
11749       if (exp.X_op != O_symbol)
11750         {
11751           as_bad ("bad .weakext directive");
11752           ignore_rest_of_line();
11753           return;
11754         }
11755       symbol_set_value_expression (symbolP, &exp);
11756     }
11757
11758   demand_empty_rest_of_line ();
11759 }
11760
11761 /* Parse a register string into a number.  Called from the ECOFF code
11762    to parse .frame.  The argument is non-zero if this is the frame
11763    register, so that we can record it in mips_frame_reg.  */
11764
11765 int
11766 tc_get_register (frame)
11767      int frame;
11768 {
11769   int reg;
11770
11771   SKIP_WHITESPACE ();
11772   if (*input_line_pointer++ != '$')
11773     {
11774       as_warn (_("expected `$'"));
11775       reg = 0;
11776     }
11777   else if (ISDIGIT (*input_line_pointer))
11778     {
11779       reg = get_absolute_expression ();
11780       if (reg < 0 || reg >= 32)
11781         {
11782           as_warn (_("Bad register number"));
11783           reg = 0;
11784         }
11785     }
11786   else
11787     {
11788       if (strncmp (input_line_pointer, "fp", 2) == 0)
11789         reg = FP;
11790       else if (strncmp (input_line_pointer, "sp", 2) == 0)
11791         reg = SP;
11792       else if (strncmp (input_line_pointer, "gp", 2) == 0)
11793         reg = GP;
11794       else if (strncmp (input_line_pointer, "at", 2) == 0)
11795         reg = AT;
11796       else
11797         {
11798           as_warn (_("Unrecognized register name"));
11799           reg = 0;
11800         }
11801       input_line_pointer += 2;
11802     }
11803   if (frame)
11804     {
11805       mips_frame_reg = reg != 0 ? reg : SP;
11806       mips_frame_reg_valid = 1;
11807       mips_cprestore_valid = 0;
11808     }
11809   return reg;
11810 }
11811
11812 valueT
11813 md_section_align (seg, addr)
11814      asection *seg;
11815      valueT addr;
11816 {
11817   int align = bfd_get_section_alignment (stdoutput, seg);
11818
11819 #ifdef OBJ_ELF
11820   /* We don't need to align ELF sections to the full alignment.
11821      However, Irix 5 may prefer that we align them at least to a 16
11822      byte boundary.  We don't bother to align the sections if we are
11823      targeted for an embedded system.  */
11824   if (strcmp (TARGET_OS, "elf") == 0)
11825     return addr;
11826   if (align > 4)
11827     align = 4;
11828 #endif
11829
11830   return ((addr + (1 << align) - 1) & (-1 << align));
11831 }
11832
11833 /* Utility routine, called from above as well.  If called while the
11834    input file is still being read, it's only an approximation.  (For
11835    example, a symbol may later become defined which appeared to be
11836    undefined earlier.)  */
11837
11838 static int
11839 nopic_need_relax (sym, before_relaxing)
11840      symbolS *sym;
11841      int before_relaxing;
11842 {
11843   if (sym == 0)
11844     return 0;
11845
11846   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
11847     {
11848       const char *symname;
11849       int change;
11850
11851       /* Find out whether this symbol can be referenced off the GP
11852          register.  It can be if it is smaller than the -G size or if
11853          it is in the .sdata or .sbss section.  Certain symbols can
11854          not be referenced off the GP, although it appears as though
11855          they can.  */
11856       symname = S_GET_NAME (sym);
11857       if (symname != (const char *) NULL
11858           && (strcmp (symname, "eprol") == 0
11859               || strcmp (symname, "etext") == 0
11860               || strcmp (symname, "_gp") == 0
11861               || strcmp (symname, "edata") == 0
11862               || strcmp (symname, "_fbss") == 0
11863               || strcmp (symname, "_fdata") == 0
11864               || strcmp (symname, "_ftext") == 0
11865               || strcmp (symname, "end") == 0
11866               || strcmp (symname, "_gp_disp") == 0))
11867         change = 1;
11868       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11869                && (0
11870 #ifndef NO_ECOFF_DEBUGGING
11871                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
11872                        && (symbol_get_obj (sym)->ecoff_extern_size
11873                            <= g_switch_value))
11874 #endif
11875                    /* We must defer this decision until after the whole
11876                       file has been read, since there might be a .extern
11877                       after the first use of this symbol.  */
11878                    || (before_relaxing
11879 #ifndef NO_ECOFF_DEBUGGING
11880                        && symbol_get_obj (sym)->ecoff_extern_size == 0
11881 #endif
11882                        && S_GET_VALUE (sym) == 0)
11883                    || (S_GET_VALUE (sym) != 0
11884                        && S_GET_VALUE (sym) <= g_switch_value)))
11885         change = 0;
11886       else
11887         {
11888           const char *segname;
11889
11890           segname = segment_name (S_GET_SEGMENT (sym));
11891           assert (strcmp (segname, ".lit8") != 0
11892                   && strcmp (segname, ".lit4") != 0);
11893           change = (strcmp (segname, ".sdata") != 0
11894                     && strcmp (segname, ".sbss") != 0
11895                     && strncmp (segname, ".sdata.", 7) != 0
11896                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
11897         }
11898       return change;
11899     }
11900   else
11901     /* We are not optimizing for the GP register.  */
11902     return 1;
11903 }
11904
11905 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11906    extended opcode.  SEC is the section the frag is in.  */
11907
11908 static int
11909 mips16_extended_frag (fragp, sec, stretch)
11910      fragS *fragp;
11911      asection *sec;
11912      long stretch;
11913 {
11914   int type;
11915   register const struct mips16_immed_operand *op;
11916   offsetT val;
11917   int mintiny, maxtiny;
11918   segT symsec;
11919   fragS *sym_frag;
11920
11921   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
11922     return 0;
11923   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
11924     return 1;
11925
11926   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11927   op = mips16_immed_operands;
11928   while (op->type != type)
11929     {
11930       ++op;
11931       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
11932     }
11933
11934   if (op->unsp)
11935     {
11936       if (type == '<' || type == '>' || type == '[' || type == ']')
11937         {
11938           mintiny = 1;
11939           maxtiny = 1 << op->nbits;
11940         }
11941       else
11942         {
11943           mintiny = 0;
11944           maxtiny = (1 << op->nbits) - 1;
11945         }
11946     }
11947   else
11948     {
11949       mintiny = - (1 << (op->nbits - 1));
11950       maxtiny = (1 << (op->nbits - 1)) - 1;
11951     }
11952
11953   sym_frag = symbol_get_frag (fragp->fr_symbol);
11954   val = S_GET_VALUE (fragp->fr_symbol);
11955   symsec = S_GET_SEGMENT (fragp->fr_symbol);
11956
11957   if (op->pcrel)
11958     {
11959       addressT addr;
11960
11961       /* We won't have the section when we are called from
11962          mips_relax_frag.  However, we will always have been called
11963          from md_estimate_size_before_relax first.  If this is a
11964          branch to a different section, we mark it as such.  If SEC is
11965          NULL, and the frag is not marked, then it must be a branch to
11966          the same section.  */
11967       if (sec == NULL)
11968         {
11969           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
11970             return 1;
11971         }
11972       else
11973         {
11974           /* Must have been called from md_estimate_size_before_relax.  */
11975           if (symsec != sec)
11976             {
11977               fragp->fr_subtype =
11978                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11979
11980               /* FIXME: We should support this, and let the linker
11981                  catch branches and loads that are out of range.  */
11982               as_bad_where (fragp->fr_file, fragp->fr_line,
11983                             _("unsupported PC relative reference to different section"));
11984
11985               return 1;
11986             }
11987           if (fragp != sym_frag && sym_frag->fr_address == 0)
11988             /* Assume non-extended on the first relaxation pass.
11989                The address we have calculated will be bogus if this is
11990                a forward branch to another frag, as the forward frag
11991                will have fr_address == 0.  */
11992             return 0;
11993         }
11994
11995       /* In this case, we know for sure that the symbol fragment is in
11996          the same section.  If the relax_marker of the symbol fragment
11997          differs from the relax_marker of this fragment, we have not
11998          yet adjusted the symbol fragment fr_address.  We want to add
11999          in STRETCH in order to get a better estimate of the address.
12000          This particularly matters because of the shift bits.  */
12001       if (stretch != 0
12002           && sym_frag->relax_marker != fragp->relax_marker)
12003         {
12004           fragS *f;
12005
12006           /* Adjust stretch for any alignment frag.  Note that if have
12007              been expanding the earlier code, the symbol may be
12008              defined in what appears to be an earlier frag.  FIXME:
12009              This doesn't handle the fr_subtype field, which specifies
12010              a maximum number of bytes to skip when doing an
12011              alignment.  */
12012           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12013             {
12014               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12015                 {
12016                   if (stretch < 0)
12017                     stretch = - ((- stretch)
12018                                  & ~ ((1 << (int) f->fr_offset) - 1));
12019                   else
12020                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12021                   if (stretch == 0)
12022                     break;
12023                 }
12024             }
12025           if (f != NULL)
12026             val += stretch;
12027         }
12028
12029       addr = fragp->fr_address + fragp->fr_fix;
12030
12031       /* The base address rules are complicated.  The base address of
12032          a branch is the following instruction.  The base address of a
12033          PC relative load or add is the instruction itself, but if it
12034          is in a delay slot (in which case it can not be extended) use
12035          the address of the instruction whose delay slot it is in.  */
12036       if (type == 'p' || type == 'q')
12037         {
12038           addr += 2;
12039
12040           /* If we are currently assuming that this frag should be
12041              extended, then, the current address is two bytes
12042              higher.  */
12043           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12044             addr += 2;
12045
12046           /* Ignore the low bit in the target, since it will be set
12047              for a text label.  */
12048           if ((val & 1) != 0)
12049             --val;
12050         }
12051       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12052         addr -= 4;
12053       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12054         addr -= 2;
12055
12056       val -= addr & ~ ((1 << op->shift) - 1);
12057
12058       /* Branch offsets have an implicit 0 in the lowest bit.  */
12059       if (type == 'p' || type == 'q')
12060         val /= 2;
12061
12062       /* If any of the shifted bits are set, we must use an extended
12063          opcode.  If the address depends on the size of this
12064          instruction, this can lead to a loop, so we arrange to always
12065          use an extended opcode.  We only check this when we are in
12066          the main relaxation loop, when SEC is NULL.  */
12067       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12068         {
12069           fragp->fr_subtype =
12070             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12071           return 1;
12072         }
12073
12074       /* If we are about to mark a frag as extended because the value
12075          is precisely maxtiny + 1, then there is a chance of an
12076          infinite loop as in the following code:
12077              la $4,foo
12078              .skip      1020
12079              .align     2
12080            foo:
12081          In this case when the la is extended, foo is 0x3fc bytes
12082          away, so the la can be shrunk, but then foo is 0x400 away, so
12083          the la must be extended.  To avoid this loop, we mark the
12084          frag as extended if it was small, and is about to become
12085          extended with a value of maxtiny + 1.  */
12086       if (val == ((maxtiny + 1) << op->shift)
12087           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12088           && sec == NULL)
12089         {
12090           fragp->fr_subtype =
12091             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12092           return 1;
12093         }
12094     }
12095   else if (symsec != absolute_section && sec != NULL)
12096     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12097
12098   if ((val & ((1 << op->shift) - 1)) != 0
12099       || val < (mintiny << op->shift)
12100       || val > (maxtiny << op->shift))
12101     return 1;
12102   else
12103     return 0;
12104 }
12105
12106 /* Estimate the size of a frag before relaxing.  Unless this is the
12107    mips16, we are not really relaxing here, and the final size is
12108    encoded in the subtype information.  For the mips16, we have to
12109    decide whether we are using an extended opcode or not.  */
12110
12111 int
12112 md_estimate_size_before_relax (fragp, segtype)
12113      fragS *fragp;
12114      asection *segtype;
12115 {
12116   int change = 0;
12117   boolean linkonce = false;
12118
12119   if (RELAX_MIPS16_P (fragp->fr_subtype))
12120     {
12121       if (mips16_extended_frag (fragp, segtype, 0))
12122         {
12123           fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12124           return 4;
12125         }
12126       else
12127         {
12128           fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12129           return 2;
12130         }
12131     }
12132
12133   if (mips_pic == NO_PIC)
12134     {
12135       change = nopic_need_relax (fragp->fr_symbol, 0);
12136     }
12137   else if (mips_pic == SVR4_PIC)
12138     {
12139       symbolS *sym;
12140       asection *symsec;
12141
12142       sym = fragp->fr_symbol;
12143
12144       /* Handle the case of a symbol equated to another symbol.  */
12145       while (symbol_equated_reloc_p (sym))
12146         {
12147           symbolS *n;
12148
12149           /* It's possible to get a loop here in a badly written
12150              program.  */
12151           n = symbol_get_value_expression (sym)->X_add_symbol;
12152           if (n == sym)
12153             break;
12154           sym = n;
12155         }
12156
12157       symsec = S_GET_SEGMENT (sym);
12158
12159       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12160       if (symsec != segtype && ! S_IS_LOCAL (sym))
12161         {
12162           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12163               != 0)
12164             linkonce = true;
12165
12166           /* The GNU toolchain uses an extension for ELF: a section
12167              beginning with the magic string .gnu.linkonce is a linkonce
12168              section.  */
12169           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12170                        sizeof ".gnu.linkonce" - 1) == 0)
12171             linkonce = true;
12172         }
12173
12174       /* This must duplicate the test in adjust_reloc_syms.  */
12175       change = (symsec != &bfd_und_section
12176                 && symsec != &bfd_abs_section
12177                 && ! bfd_is_com_section (symsec)
12178                 && !linkonce
12179 #ifdef OBJ_ELF
12180                 /* A global or weak symbol is treated as external.  */
12181                 && (OUTPUT_FLAVOR == bfd_target_elf_flavour
12182                     && ! (S_IS_EXTERN (sym) || S_IS_WEAK (sym)))
12183 #endif
12184                 );
12185     }
12186   else
12187     abort ();
12188
12189   if (change)
12190     {
12191       /* Record the offset to the first reloc in the fr_opcode field.
12192          This lets md_convert_frag and tc_gen_reloc know that the code
12193          must be expanded.  */
12194       fragp->fr_opcode = (fragp->fr_literal
12195                           + fragp->fr_fix
12196                           - RELAX_OLD (fragp->fr_subtype)
12197                           + RELAX_RELOC1 (fragp->fr_subtype));
12198       /* FIXME: This really needs as_warn_where.  */
12199       if (RELAX_WARN (fragp->fr_subtype))
12200         as_warn (_("AT used after \".set noat\" or macro used after "
12201                    "\".set nomacro\""));
12202
12203       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12204     }
12205
12206   return 0;
12207 }
12208
12209 /* This is called to see whether a reloc against a defined symbol
12210    should be converted into a reloc against a section.  Don't adjust
12211    MIPS16 jump relocations, so we don't have to worry about the format
12212    of the offset in the .o file.  Don't adjust relocations against
12213    mips16 symbols, so that the linker can find them if it needs to set
12214    up a stub.  */
12215
12216 int
12217 mips_fix_adjustable (fixp)
12218      fixS *fixp;
12219 {
12220 #ifdef OBJ_ELF
12221   /* Prevent all adjustments to global symbols.  */
12222   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12223       && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12224     return 0;
12225 #endif
12226   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12227     return 0;
12228   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12229       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12230     return 0;
12231   if (fixp->fx_addsy == NULL)
12232     return 1;
12233 #ifdef OBJ_ELF
12234   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12235       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12236       && fixp->fx_subsy == NULL)
12237     return 0;
12238 #endif
12239   return 1;
12240 }
12241
12242 /* Translate internal representation of relocation info to BFD target
12243    format.  */
12244
12245 arelent **
12246 tc_gen_reloc (section, fixp)
12247      asection *section ATTRIBUTE_UNUSED;
12248      fixS *fixp;
12249 {
12250   static arelent *retval[4];
12251   arelent *reloc;
12252   bfd_reloc_code_real_type code;
12253
12254   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12255   retval[1] = NULL;
12256
12257   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12258   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12259   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12260
12261   if (mips_pic == EMBEDDED_PIC
12262       && SWITCH_TABLE (fixp))
12263     {
12264       /* For a switch table entry we use a special reloc.  The addend
12265          is actually the difference between the reloc address and the
12266          subtrahend.  */
12267       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12268       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12269         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12270       fixp->fx_r_type = BFD_RELOC_GPREL32;
12271     }
12272   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12273     reloc->addend = fixp->fx_addnumber;
12274   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12275     {
12276       /* We use a special addend for an internal RELLO reloc.  */
12277       if (symbol_section_p (fixp->fx_addsy))
12278         reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12279       else
12280         reloc->addend = fixp->fx_addnumber + reloc->address;
12281     }
12282   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12283     {
12284       assert (fixp->fx_next != NULL
12285               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12286       /* We use a special addend for an internal RELHI reloc.  The
12287          reloc is relative to the RELLO; adjust the addend
12288          accordingly.  */
12289       if (symbol_section_p (fixp->fx_addsy))
12290         reloc->addend = (fixp->fx_next->fx_frag->fr_address
12291                          + fixp->fx_next->fx_where
12292                          - S_GET_VALUE (fixp->fx_subsy));
12293       else
12294         reloc->addend = (fixp->fx_addnumber
12295                          + fixp->fx_next->fx_frag->fr_address
12296                          + fixp->fx_next->fx_where);
12297     }
12298   else
12299     {
12300       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12301         /* A gruesome hack which is a result of the gruesome gas reloc
12302            handling.  */
12303         reloc->addend = reloc->address;
12304       else
12305         reloc->addend = -reloc->address;
12306     }
12307
12308   /* If this is a variant frag, we may need to adjust the existing
12309      reloc and generate a new one.  */
12310   if (fixp->fx_frag->fr_opcode != NULL
12311       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12312           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12313           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12314           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12315           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12316           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12317           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12318       && ! HAVE_NEWABI)
12319     {
12320       arelent *reloc2;
12321
12322       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12323
12324       /* If this is not the last reloc in this frag, then we have two
12325          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12326          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12327          the second one handle all of them.  */
12328       if (fixp->fx_next != NULL
12329           && fixp->fx_frag == fixp->fx_next->fx_frag)
12330         {
12331           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12332                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12333                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12334                       && (fixp->fx_next->fx_r_type
12335                           == BFD_RELOC_MIPS_GOT_LO16))
12336                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12337                       && (fixp->fx_next->fx_r_type
12338                           == BFD_RELOC_MIPS_CALL_LO16)));
12339           retval[0] = NULL;
12340           return retval;
12341         }
12342
12343       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12344       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12345       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12346       retval[2] = NULL;
12347       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12348       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12349       reloc2->address = (reloc->address
12350                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12351                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12352       reloc2->addend = fixp->fx_addnumber;
12353       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12354       assert (reloc2->howto != NULL);
12355
12356       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12357         {
12358           arelent *reloc3;
12359
12360           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12361           retval[3] = NULL;
12362           *reloc3 = *reloc2;
12363           reloc3->address += 4;
12364         }
12365
12366       if (mips_pic == NO_PIC)
12367         {
12368           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12369           fixp->fx_r_type = BFD_RELOC_HI16_S;
12370         }
12371       else if (mips_pic == SVR4_PIC)
12372         {
12373           switch (fixp->fx_r_type)
12374             {
12375             default:
12376               abort ();
12377             case BFD_RELOC_MIPS_GOT16:
12378               break;
12379             case BFD_RELOC_MIPS_CALL16:
12380             case BFD_RELOC_MIPS_GOT_LO16:
12381             case BFD_RELOC_MIPS_CALL_LO16:
12382               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12383               break;
12384             }
12385         }
12386       else
12387         abort ();
12388     }
12389
12390   /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12391      to be used in the relocation's section offset.  */
12392   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12393     {
12394       reloc->address = reloc->addend;
12395       reloc->addend = 0;
12396     }
12397
12398   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12399      fixup_segment converted a non-PC relative reloc into a PC
12400      relative reloc.  In such a case, we need to convert the reloc
12401      code.  */
12402   code = fixp->fx_r_type;
12403   if (fixp->fx_pcrel)
12404     {
12405       switch (code)
12406         {
12407         case BFD_RELOC_8:
12408           code = BFD_RELOC_8_PCREL;
12409           break;
12410         case BFD_RELOC_16:
12411           code = BFD_RELOC_16_PCREL;
12412           break;
12413         case BFD_RELOC_32:
12414           code = BFD_RELOC_32_PCREL;
12415           break;
12416         case BFD_RELOC_64:
12417           code = BFD_RELOC_64_PCREL;
12418           break;
12419         case BFD_RELOC_8_PCREL:
12420         case BFD_RELOC_16_PCREL:
12421         case BFD_RELOC_32_PCREL:
12422         case BFD_RELOC_64_PCREL:
12423         case BFD_RELOC_16_PCREL_S2:
12424         case BFD_RELOC_PCREL_HI16_S:
12425         case BFD_RELOC_PCREL_LO16:
12426           break;
12427         default:
12428           as_bad_where (fixp->fx_file, fixp->fx_line,
12429                         _("Cannot make %s relocation PC relative"),
12430                         bfd_get_reloc_code_name (code));
12431         }
12432     }
12433
12434 #ifdef OBJ_ELF
12435   /* md_apply_fix3 has a double-subtraction hack to get
12436      bfd_install_relocation to behave nicely.  GPREL relocations are
12437      handled correctly without this hack, so undo it here.  We can't
12438      stop md_apply_fix3 from subtracting twice in the first place since
12439      the fake addend is required for variant frags above.  */
12440   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12441       && code == BFD_RELOC_GPREL16
12442       && reloc->addend != 0
12443       && mips_need_elf_addend_fixup (fixp))
12444     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12445 #endif
12446
12447   /* To support a PC relative reloc when generating embedded PIC code
12448      for ECOFF, we use a Cygnus extension.  We check for that here to
12449      make sure that we don't let such a reloc escape normally.  */
12450   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12451        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12452       && code == BFD_RELOC_16_PCREL_S2
12453       && mips_pic != EMBEDDED_PIC)
12454     reloc->howto = NULL;
12455   else
12456     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12457
12458   if (reloc->howto == NULL)
12459     {
12460       as_bad_where (fixp->fx_file, fixp->fx_line,
12461                     _("Can not represent %s relocation in this object file format"),
12462                     bfd_get_reloc_code_name (code));
12463       retval[0] = NULL;
12464     }
12465
12466   return retval;
12467 }
12468
12469 /* Relax a machine dependent frag.  This returns the amount by which
12470    the current size of the frag should change.  */
12471
12472 int
12473 mips_relax_frag (fragp, stretch)
12474      fragS *fragp;
12475      long stretch;
12476 {
12477   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12478     return 0;
12479
12480   if (mips16_extended_frag (fragp, NULL, stretch))
12481     {
12482       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12483         return 0;
12484       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12485       return 2;
12486     }
12487   else
12488     {
12489       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12490         return 0;
12491       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12492       return -2;
12493     }
12494
12495   return 0;
12496 }
12497
12498 /* Convert a machine dependent frag.  */
12499
12500 void
12501 md_convert_frag (abfd, asec, fragp)
12502      bfd *abfd ATTRIBUTE_UNUSED;
12503      segT asec;
12504      fragS *fragp;
12505 {
12506   int old, new;
12507   char *fixptr;
12508
12509   if (RELAX_MIPS16_P (fragp->fr_subtype))
12510     {
12511       int type;
12512       register const struct mips16_immed_operand *op;
12513       boolean small, ext;
12514       offsetT val;
12515       bfd_byte *buf;
12516       unsigned long insn;
12517       boolean use_extend;
12518       unsigned short extend;
12519
12520       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12521       op = mips16_immed_operands;
12522       while (op->type != type)
12523         ++op;
12524
12525       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12526         {
12527           small = false;
12528           ext = true;
12529         }
12530       else
12531         {
12532           small = true;
12533           ext = false;
12534         }
12535
12536       resolve_symbol_value (fragp->fr_symbol);
12537       val = S_GET_VALUE (fragp->fr_symbol);
12538       if (op->pcrel)
12539         {
12540           addressT addr;
12541
12542           addr = fragp->fr_address + fragp->fr_fix;
12543
12544           /* The rules for the base address of a PC relative reloc are
12545              complicated; see mips16_extended_frag.  */
12546           if (type == 'p' || type == 'q')
12547             {
12548               addr += 2;
12549               if (ext)
12550                 addr += 2;
12551               /* Ignore the low bit in the target, since it will be
12552                  set for a text label.  */
12553               if ((val & 1) != 0)
12554                 --val;
12555             }
12556           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12557             addr -= 4;
12558           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12559             addr -= 2;
12560
12561           addr &= ~ (addressT) ((1 << op->shift) - 1);
12562           val -= addr;
12563
12564           /* Make sure the section winds up with the alignment we have
12565              assumed.  */
12566           if (op->shift > 0)
12567             record_alignment (asec, op->shift);
12568         }
12569
12570       if (ext
12571           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12572               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12573         as_warn_where (fragp->fr_file, fragp->fr_line,
12574                        _("extended instruction in delay slot"));
12575
12576       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12577
12578       if (target_big_endian)
12579         insn = bfd_getb16 (buf);
12580       else
12581         insn = bfd_getl16 (buf);
12582
12583       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12584                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12585                     small, ext, &insn, &use_extend, &extend);
12586
12587       if (use_extend)
12588         {
12589           md_number_to_chars (buf, 0xf000 | extend, 2);
12590           fragp->fr_fix += 2;
12591           buf += 2;
12592         }
12593
12594       md_number_to_chars (buf, insn, 2);
12595       fragp->fr_fix += 2;
12596       buf += 2;
12597     }
12598   else
12599     {
12600       if (fragp->fr_opcode == NULL)
12601         return;
12602
12603       old = RELAX_OLD (fragp->fr_subtype);
12604       new = RELAX_NEW (fragp->fr_subtype);
12605       fixptr = fragp->fr_literal + fragp->fr_fix;
12606
12607       if (new > 0)
12608         memcpy (fixptr - old, fixptr, new);
12609
12610       fragp->fr_fix += new - old;
12611     }
12612 }
12613
12614 #ifdef OBJ_ELF
12615
12616 /* This function is called after the relocs have been generated.
12617    We've been storing mips16 text labels as odd.  Here we convert them
12618    back to even for the convenience of the debugger.  */
12619
12620 void
12621 mips_frob_file_after_relocs ()
12622 {
12623   asymbol **syms;
12624   unsigned int count, i;
12625
12626   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12627     return;
12628
12629   syms = bfd_get_outsymbols (stdoutput);
12630   count = bfd_get_symcount (stdoutput);
12631   for (i = 0; i < count; i++, syms++)
12632     {
12633       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12634           && ((*syms)->value & 1) != 0)
12635         {
12636           (*syms)->value &= ~1;
12637           /* If the symbol has an odd size, it was probably computed
12638              incorrectly, so adjust that as well.  */
12639           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12640             ++elf_symbol (*syms)->internal_elf_sym.st_size;
12641         }
12642     }
12643 }
12644
12645 #endif
12646
12647 /* This function is called whenever a label is defined.  It is used
12648    when handling branch delays; if a branch has a label, we assume we
12649    can not move it.  */
12650
12651 void
12652 mips_define_label (sym)
12653      symbolS *sym;
12654 {
12655   struct insn_label_list *l;
12656
12657   if (free_insn_labels == NULL)
12658     l = (struct insn_label_list *) xmalloc (sizeof *l);
12659   else
12660     {
12661       l = free_insn_labels;
12662       free_insn_labels = l->next;
12663     }
12664
12665   l->label = sym;
12666   l->next = insn_labels;
12667   insn_labels = l;
12668 }
12669 \f
12670 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12671
12672 /* Some special processing for a MIPS ELF file.  */
12673
12674 void
12675 mips_elf_final_processing ()
12676 {
12677   /* Write out the register information.  */
12678   if (! HAVE_NEWABI)
12679     {
12680       Elf32_RegInfo s;
12681
12682       s.ri_gprmask = mips_gprmask;
12683       s.ri_cprmask[0] = mips_cprmask[0];
12684       s.ri_cprmask[1] = mips_cprmask[1];
12685       s.ri_cprmask[2] = mips_cprmask[2];
12686       s.ri_cprmask[3] = mips_cprmask[3];
12687       /* The gp_value field is set by the MIPS ELF backend.  */
12688
12689       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12690                                        ((Elf32_External_RegInfo *)
12691                                         mips_regmask_frag));
12692     }
12693   else
12694     {
12695       Elf64_Internal_RegInfo s;
12696
12697       s.ri_gprmask = mips_gprmask;
12698       s.ri_pad = 0;
12699       s.ri_cprmask[0] = mips_cprmask[0];
12700       s.ri_cprmask[1] = mips_cprmask[1];
12701       s.ri_cprmask[2] = mips_cprmask[2];
12702       s.ri_cprmask[3] = mips_cprmask[3];
12703       /* The gp_value field is set by the MIPS ELF backend.  */
12704
12705       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12706                                        ((Elf64_External_RegInfo *)
12707                                         mips_regmask_frag));
12708     }
12709
12710   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
12711      sort of BFD interface for this.  */
12712   if (mips_any_noreorder)
12713     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12714   if (mips_pic != NO_PIC)
12715     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12716
12717   /* Set the MIPS ELF ABI flags.  */
12718   if (file_mips_abi == NO_ABI)
12719     ;
12720   else if (file_mips_abi == O32_ABI)
12721     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
12722   else if (file_mips_abi == O64_ABI)
12723     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
12724   else if (file_mips_abi == EABI_ABI)
12725     {
12726       if (mips_eabi64)
12727         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12728       else
12729         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12730     }
12731   else if (file_mips_abi == N32_ABI)
12732     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12733
12734   /* Nothing to do for "64".  */
12735
12736   if (mips_32bitmode)
12737     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12738 }
12739
12740 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12741 \f
12742 typedef struct proc {
12743   symbolS *isym;
12744   unsigned long reg_mask;
12745   unsigned long reg_offset;
12746   unsigned long fpreg_mask;
12747   unsigned long fpreg_offset;
12748   unsigned long frame_offset;
12749   unsigned long frame_reg;
12750   unsigned long pc_reg;
12751 } procS;
12752
12753 static procS cur_proc;
12754 static procS *cur_proc_ptr;
12755 static int numprocs;
12756
12757 /* Fill in an rs_align_code fragment.  */
12758
12759 void
12760 mips_handle_align (fragp)
12761      fragS *fragp;
12762 {
12763   if (fragp->fr_type != rs_align_code)
12764     return;
12765
12766   if (mips_opts.mips16)
12767     {
12768       static const unsigned char be_nop[] = { 0x65, 0x00 };
12769       static const unsigned char le_nop[] = { 0x00, 0x65 };
12770
12771       int bytes;
12772       char *p;
12773
12774       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12775       p = fragp->fr_literal + fragp->fr_fix;
12776
12777       if (bytes & 1)
12778         {
12779           *p++ = 0;
12780           fragp->fr_fix += 1;
12781         }
12782
12783       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12784       fragp->fr_var = 2;
12785     }
12786
12787   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
12788 }
12789
12790 static void
12791 md_obj_begin ()
12792 {
12793 }
12794
12795 static void
12796 md_obj_end ()
12797 {
12798   /* check for premature end, nesting errors, etc */
12799   if (cur_proc_ptr)
12800     as_warn (_("missing .end at end of assembly"));
12801 }
12802
12803 static long
12804 get_number ()
12805 {
12806   int negative = 0;
12807   long val = 0;
12808
12809   if (*input_line_pointer == '-')
12810     {
12811       ++input_line_pointer;
12812       negative = 1;
12813     }
12814   if (!ISDIGIT (*input_line_pointer))
12815     as_bad (_("expected simple number"));
12816   if (input_line_pointer[0] == '0')
12817     {
12818       if (input_line_pointer[1] == 'x')
12819         {
12820           input_line_pointer += 2;
12821           while (ISXDIGIT (*input_line_pointer))
12822             {
12823               val <<= 4;
12824               val |= hex_value (*input_line_pointer++);
12825             }
12826           return negative ? -val : val;
12827         }
12828       else
12829         {
12830           ++input_line_pointer;
12831           while (ISDIGIT (*input_line_pointer))
12832             {
12833               val <<= 3;
12834               val |= *input_line_pointer++ - '0';
12835             }
12836           return negative ? -val : val;
12837         }
12838     }
12839   if (!ISDIGIT (*input_line_pointer))
12840     {
12841       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12842               *input_line_pointer, *input_line_pointer);
12843       as_warn (_("invalid number"));
12844       return -1;
12845     }
12846   while (ISDIGIT (*input_line_pointer))
12847     {
12848       val *= 10;
12849       val += *input_line_pointer++ - '0';
12850     }
12851   return negative ? -val : val;
12852 }
12853
12854 /* The .file directive; just like the usual .file directive, but there
12855    is an initial number which is the ECOFF file index.  */
12856
12857 static void
12858 s_file (x)
12859      int x ATTRIBUTE_UNUSED;
12860 {
12861   int line;
12862
12863   line = get_number ();
12864   s_app_file (0);
12865 }
12866
12867 /* The .end directive.  */
12868
12869 static void
12870 s_mips_end (x)
12871      int x ATTRIBUTE_UNUSED;
12872 {
12873   symbolS *p;
12874   int maybe_text;
12875
12876   /* Following functions need their own .frame and .cprestore directives.  */
12877   mips_frame_reg_valid = 0;
12878   mips_cprestore_valid = 0;
12879
12880   if (!is_end_of_line[(unsigned char) *input_line_pointer])
12881     {
12882       p = get_symbol ();
12883       demand_empty_rest_of_line ();
12884     }
12885   else
12886     p = NULL;
12887
12888 #ifdef BFD_ASSEMBLER
12889   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12890     maybe_text = 1;
12891   else
12892     maybe_text = 0;
12893 #else
12894   if (now_seg != data_section && now_seg != bss_section)
12895     maybe_text = 1;
12896   else
12897     maybe_text = 0;
12898 #endif
12899
12900   if (!maybe_text)
12901     as_warn (_(".end not in text section"));
12902
12903   if (!cur_proc_ptr)
12904     {
12905       as_warn (_(".end directive without a preceding .ent directive."));
12906       demand_empty_rest_of_line ();
12907       return;
12908     }
12909
12910   if (p != NULL)
12911     {
12912       assert (S_GET_NAME (p));
12913       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
12914         as_warn (_(".end symbol does not match .ent symbol."));
12915     }
12916   else
12917     as_warn (_(".end directive missing or unknown symbol"));
12918
12919 #ifdef MIPS_STABS_ELF
12920   {
12921     segT saved_seg = now_seg;
12922     subsegT saved_subseg = now_subseg;
12923     valueT dot;
12924     expressionS exp;
12925     char *fragp;
12926
12927     dot = frag_now_fix ();
12928
12929 #ifdef md_flush_pending_output
12930     md_flush_pending_output ();
12931 #endif
12932
12933     assert (pdr_seg);
12934     subseg_set (pdr_seg, 0);
12935
12936     /* Write the symbol.  */
12937     exp.X_op = O_symbol;
12938     exp.X_add_symbol = p;
12939     exp.X_add_number = 0;
12940     emit_expr (&exp, 4);
12941
12942     fragp = frag_more (7 * 4);
12943
12944     md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
12945     md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
12946     md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
12947     md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
12948     md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
12949     md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
12950     md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
12951
12952     subseg_set (saved_seg, saved_subseg);
12953   }
12954 #endif
12955
12956   cur_proc_ptr = NULL;
12957 }
12958
12959 /* The .aent and .ent directives.  */
12960
12961 static void
12962 s_mips_ent (aent)
12963      int aent;
12964 {
12965   int number = 0;
12966   symbolS *symbolP;
12967   int maybe_text;
12968
12969   symbolP = get_symbol ();
12970   if (*input_line_pointer == ',')
12971     input_line_pointer++;
12972   SKIP_WHITESPACE ();
12973   if (ISDIGIT (*input_line_pointer)
12974       || *input_line_pointer == '-')
12975     number = get_number ();
12976
12977 #ifdef BFD_ASSEMBLER
12978   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12979     maybe_text = 1;
12980   else
12981     maybe_text = 0;
12982 #else
12983   if (now_seg != data_section && now_seg != bss_section)
12984     maybe_text = 1;
12985   else
12986     maybe_text = 0;
12987 #endif
12988
12989   if (!maybe_text)
12990     as_warn (_(".ent or .aent not in text section."));
12991
12992   if (!aent && cur_proc_ptr)
12993     as_warn (_("missing .end"));
12994
12995   if (!aent)
12996     {
12997       /* This function needs its own .frame and .cprestore directives.  */
12998       mips_frame_reg_valid = 0;
12999       mips_cprestore_valid = 0;
13000
13001       cur_proc_ptr = &cur_proc;
13002       memset (cur_proc_ptr, '\0', sizeof (procS));
13003
13004       cur_proc_ptr->isym = symbolP;
13005
13006       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13007
13008       numprocs++;
13009     }
13010
13011   demand_empty_rest_of_line ();
13012 }
13013
13014 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13015    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13016    s_mips_frame is used so that we can set the PDR information correctly.
13017    We can't use the ecoff routines because they make reference to the ecoff
13018    symbol table (in the mdebug section).  */
13019
13020 static void
13021 s_mips_frame (ignore)
13022      int ignore ATTRIBUTE_UNUSED;
13023 {
13024 #ifdef MIPS_STABS_ELF
13025
13026   long val;
13027
13028   if (cur_proc_ptr == (procS *) NULL)
13029     {
13030       as_warn (_(".frame outside of .ent"));
13031       demand_empty_rest_of_line ();
13032       return;
13033     }
13034
13035   cur_proc_ptr->frame_reg = tc_get_register (1);
13036
13037   SKIP_WHITESPACE ();
13038   if (*input_line_pointer++ != ','
13039       || get_absolute_expression_and_terminator (&val) != ',')
13040     {
13041       as_warn (_("Bad .frame directive"));
13042       --input_line_pointer;
13043       demand_empty_rest_of_line ();
13044       return;
13045     }
13046
13047   cur_proc_ptr->frame_offset = val;
13048   cur_proc_ptr->pc_reg = tc_get_register (0);
13049
13050   demand_empty_rest_of_line ();
13051 #else
13052   s_ignore (ignore);
13053 #endif /* MIPS_STABS_ELF */
13054 }
13055
13056 /* The .fmask and .mask directives. If the mdebug section is present
13057    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13058    embedded targets, s_mips_mask is used so that we can set the PDR
13059    information correctly. We can't use the ecoff routines because they
13060    make reference to the ecoff symbol table (in the mdebug section).  */
13061
13062 static void
13063 s_mips_mask (reg_type)
13064      char reg_type;
13065 {
13066 #ifdef MIPS_STABS_ELF
13067   long mask, off;
13068
13069   if (cur_proc_ptr == (procS *) NULL)
13070     {
13071       as_warn (_(".mask/.fmask outside of .ent"));
13072       demand_empty_rest_of_line ();
13073       return;
13074     }
13075
13076   if (get_absolute_expression_and_terminator (&mask) != ',')
13077     {
13078       as_warn (_("Bad .mask/.fmask directive"));
13079       --input_line_pointer;
13080       demand_empty_rest_of_line ();
13081       return;
13082     }
13083
13084   off = get_absolute_expression ();
13085
13086   if (reg_type == 'F')
13087     {
13088       cur_proc_ptr->fpreg_mask = mask;
13089       cur_proc_ptr->fpreg_offset = off;
13090     }
13091   else
13092     {
13093       cur_proc_ptr->reg_mask = mask;
13094       cur_proc_ptr->reg_offset = off;
13095     }
13096
13097   demand_empty_rest_of_line ();
13098 #else
13099   s_ignore (reg_type);
13100 #endif /* MIPS_STABS_ELF */
13101 }
13102
13103 /* The .loc directive.  */
13104
13105 #if 0
13106 static void
13107 s_loc (x)
13108      int x;
13109 {
13110   symbolS *symbolP;
13111   int lineno;
13112   int addroff;
13113
13114   assert (now_seg == text_section);
13115
13116   lineno = get_number ();
13117   addroff = frag_now_fix ();
13118
13119   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13120   S_SET_TYPE (symbolP, N_SLINE);
13121   S_SET_OTHER (symbolP, 0);
13122   S_SET_DESC (symbolP, lineno);
13123   symbolP->sy_segment = now_seg;
13124 }
13125 #endif
13126
13127 /* CPU name/ISA/number mapping table.
13128
13129    Entries are grouped by type.  The first matching CPU or ISA entry
13130    gets chosen by CPU or ISA, so it should be the 'canonical' name
13131    for that type.  Entries after that within the type are sorted
13132    alphabetically.
13133
13134    Case is ignored in comparison, so put the canonical entry in the
13135    appropriate case but everything else in lower case to ease eye pain.  */
13136 static const struct mips_cpu_info mips_cpu_info_table[] =
13137 {
13138   /* MIPS1 ISA */
13139   { "MIPS1",          1,      ISA_MIPS1,      CPU_R3000, },
13140   { "mips",           1,      ISA_MIPS1,      CPU_R3000, },
13141
13142   /* MIPS2 ISA */
13143   { "MIPS2",          1,      ISA_MIPS2,      CPU_R6000, },
13144
13145   /* MIPS3 ISA */
13146   { "MIPS3",          1,      ISA_MIPS3,      CPU_R4000, },
13147
13148   /* MIPS4 ISA */
13149   { "MIPS4",          1,      ISA_MIPS4,      CPU_R8000, },
13150
13151   /* MIPS5 ISA */
13152   { "MIPS5",          1,      ISA_MIPS5,      CPU_MIPS5, },
13153   { "Generic-MIPS5",  0,      ISA_MIPS5,      CPU_MIPS5, },
13154
13155   /* MIPS32 ISA */
13156   { "MIPS32",         1,      ISA_MIPS32,     CPU_MIPS32, },
13157   { "mipsisa32",      0,      ISA_MIPS32,     CPU_MIPS32, },
13158   { "Generic-MIPS32", 0,      ISA_MIPS32,     CPU_MIPS32, },
13159   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13160   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32, },
13161   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32, },
13162
13163   /* For historical reasons.  */
13164   { "MIPS64",         1,      ISA_MIPS3,      CPU_R4000, },
13165
13166   /* MIPS64 ISA */
13167   { "mipsisa64",      1,      ISA_MIPS64,     CPU_MIPS64, },
13168   { "Generic-MIPS64", 0,      ISA_MIPS64,     CPU_MIPS64, },
13169   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64, },
13170   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64, },
13171
13172   /* R2000 CPU */
13173   { "R2000",          0,      ISA_MIPS1,      CPU_R2000, },
13174   { "2000",           0,      ISA_MIPS1,      CPU_R2000, },
13175   { "2k",             0,      ISA_MIPS1,      CPU_R2000, },
13176   { "r2k",            0,      ISA_MIPS1,      CPU_R2000, },
13177
13178   /* R3000 CPU */
13179   { "R3000",          0,      ISA_MIPS1,      CPU_R3000, },
13180   { "3000",           0,      ISA_MIPS1,      CPU_R3000, },
13181   { "3k",             0,      ISA_MIPS1,      CPU_R3000, },
13182   { "r3k",            0,      ISA_MIPS1,      CPU_R3000, },
13183
13184   /* TX3900 CPU */
13185   { "R3900",          0,      ISA_MIPS1,      CPU_R3900, },
13186   { "3900",           0,      ISA_MIPS1,      CPU_R3900, },
13187   { "mipstx39",       0,      ISA_MIPS1,      CPU_R3900, },
13188
13189   /* R4000 CPU */
13190   { "R4000",          0,      ISA_MIPS3,      CPU_R4000, },
13191   { "4000",           0,      ISA_MIPS3,      CPU_R4000, },
13192   { "4k",             0,      ISA_MIPS3,      CPU_R4000, },   /* beware */
13193   { "r4k",            0,      ISA_MIPS3,      CPU_R4000, },
13194
13195   /* R4010 CPU */
13196   { "R4010",          0,      ISA_MIPS2,      CPU_R4010, },
13197   { "4010",           0,      ISA_MIPS2,      CPU_R4010, },
13198
13199   /* R4400 CPU */
13200   { "R4400",          0,      ISA_MIPS3,      CPU_R4400, },
13201   { "4400",           0,      ISA_MIPS3,      CPU_R4400, },
13202
13203   /* R4600 CPU */
13204   { "R4600",          0,      ISA_MIPS3,      CPU_R4600, },
13205   { "4600",           0,      ISA_MIPS3,      CPU_R4600, },
13206   { "mips64orion",    0,      ISA_MIPS3,      CPU_R4600, },
13207   { "orion",          0,      ISA_MIPS3,      CPU_R4600, },
13208
13209   /* R4650 CPU */
13210   { "R4650",          0,      ISA_MIPS3,      CPU_R4650, },
13211   { "4650",           0,      ISA_MIPS3,      CPU_R4650, },
13212
13213   /* R6000 CPU */
13214   { "R6000",          0,      ISA_MIPS2,      CPU_R6000, },
13215   { "6000",           0,      ISA_MIPS2,      CPU_R6000, },
13216   { "6k",             0,      ISA_MIPS2,      CPU_R6000, },
13217   { "r6k",            0,      ISA_MIPS2,      CPU_R6000, },
13218
13219   /* R8000 CPU */
13220   { "R8000",          0,      ISA_MIPS4,      CPU_R8000, },
13221   { "8000",           0,      ISA_MIPS4,      CPU_R8000, },
13222   { "8k",             0,      ISA_MIPS4,      CPU_R8000, },
13223   { "r8k",            0,      ISA_MIPS4,      CPU_R8000, },
13224
13225   /* R10000 CPU */
13226   { "R10000",         0,      ISA_MIPS4,      CPU_R10000, },
13227   { "10000",          0,      ISA_MIPS4,      CPU_R10000, },
13228   { "10k",            0,      ISA_MIPS4,      CPU_R10000, },
13229   { "r10k",           0,      ISA_MIPS4,      CPU_R10000, },
13230
13231   /* R12000 CPU */
13232   { "R12000",         0,      ISA_MIPS4,      CPU_R12000, },
13233   { "12000",          0,      ISA_MIPS4,      CPU_R12000, },
13234   { "12k",            0,      ISA_MIPS4,      CPU_R12000, },
13235   { "r12k",           0,      ISA_MIPS4,      CPU_R12000, },
13236
13237   /* VR4100 CPU */
13238   { "VR4100",         0,      ISA_MIPS3,      CPU_VR4100, },
13239   { "4100",           0,      ISA_MIPS3,      CPU_VR4100, },
13240   { "mips64vr4100",   0,      ISA_MIPS3,      CPU_VR4100, },
13241   { "r4100",          0,      ISA_MIPS3,      CPU_VR4100, },
13242
13243   /* VR4111 CPU */
13244   { "VR4111",         0,      ISA_MIPS3,      CPU_R4111, },
13245   { "4111",           0,      ISA_MIPS3,      CPU_R4111, },
13246   { "mips64vr4111",   0,      ISA_MIPS3,      CPU_R4111, },
13247   { "r4111",          0,      ISA_MIPS3,      CPU_R4111, },
13248
13249   /* VR4300 CPU */
13250   { "VR4300",         0,      ISA_MIPS3,      CPU_R4300, },
13251   { "4300",           0,      ISA_MIPS3,      CPU_R4300, },
13252   { "mips64vr4300",   0,      ISA_MIPS3,      CPU_R4300, },
13253   { "r4300",          0,      ISA_MIPS3,      CPU_R4300, },
13254
13255   /* VR5000 CPU */
13256   { "VR5000",         0,      ISA_MIPS4,      CPU_R5000, },
13257   { "5000",           0,      ISA_MIPS4,      CPU_R5000, },
13258   { "5k",             0,      ISA_MIPS4,      CPU_R5000, },
13259   { "mips64vr5000",   0,      ISA_MIPS4,      CPU_R5000, },
13260   { "r5000",          0,      ISA_MIPS4,      CPU_R5000, },
13261   { "r5200",          0,      ISA_MIPS4,      CPU_R5000, },
13262   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000, },
13263   { "r5230",          0,      ISA_MIPS4,      CPU_R5000, },
13264   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000, },
13265   { "r5231",          0,      ISA_MIPS4,      CPU_R5000, },
13266   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000, },
13267   { "r5261",          0,      ISA_MIPS4,      CPU_R5000, },
13268   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000, },
13269   { "r5721",          0,      ISA_MIPS4,      CPU_R5000, },
13270   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000, },
13271   { "r5k",            0,      ISA_MIPS4,      CPU_R5000, },
13272   { "r7000",          0,      ISA_MIPS4,      CPU_R5000, },
13273
13274   /* Broadcom SB-1 CPU */
13275   { "SB-1",           0,      ISA_MIPS64,     CPU_SB1, },
13276   { "sb-1250",        0,      ISA_MIPS64,     CPU_SB1, },
13277   { "sb1",            0,      ISA_MIPS64,     CPU_SB1, },
13278   { "sb1250",         0,      ISA_MIPS64,     CPU_SB1, },
13279
13280   /* End marker.  */
13281   { NULL, 0, 0, 0, },
13282 };
13283
13284 static const struct mips_cpu_info *
13285 mips_cpu_info_from_name (name)
13286      const char *name;
13287 {
13288   int i;
13289
13290   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13291     if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13292       return (&mips_cpu_info_table[i]);
13293
13294   return NULL;
13295 }
13296
13297 static const struct mips_cpu_info *
13298 mips_cpu_info_from_isa (isa)
13299      int isa;
13300 {
13301   int i;
13302
13303   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13304     if (mips_cpu_info_table[i].is_isa
13305       && isa == mips_cpu_info_table[i].isa)
13306       return (&mips_cpu_info_table[i]);
13307
13308   return NULL;
13309 }
13310
13311 static const struct mips_cpu_info *
13312 mips_cpu_info_from_cpu (cpu)
13313      int cpu;
13314 {
13315   int i;
13316
13317   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13318     if (!mips_cpu_info_table[i].is_isa
13319       && cpu == mips_cpu_info_table[i].cpu)
13320       return (&mips_cpu_info_table[i]);
13321
13322   return NULL;
13323 }