OSDN Git Service

717078fc2bea1c95be03f4d717b3685b61827006
[pf3gnuchains/pf3gnuchains4x.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too.  */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT  1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP  28
95 #define SP  29
96 #define FP  30
97 #define RA  31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format.  */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section.  */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111                             ? ".data" \
112                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113                             ? ".rdata" \
114                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115                             ? ".rdata" \
116                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117                             ? ".rodata" \
118                             : (abort (), ""))
119
120 /* The ABI to use.  */
121 enum mips_abi_level
122 {
123   NO_ABI = 0,
124   O32_ABI,
125   O64_ABI,
126   N32_ABI,
127   N64_ABI,
128   EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file.  */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* This is the set of options which may be modified by the .set
135    pseudo-op.  We use a struct so that .set push and .set pop are more
136    reliable.  */
137
138 struct mips_set_options
139 {
140   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
141      if it has not been initialized.  Changed by `.set mipsN', and the
142      -mipsN command line option, and the default CPU.  */
143   int isa;
144   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
145      if they have not been initialized.  Changed by `.set <asename>', by
146      command line options, and based on the default architecture.  */
147   int ase_mips3d;
148   int ase_mdmx;
149   /* Whether we are assembling for the mips16 processor.  0 if we are
150      not, 1 if we are, and -1 if the value has not been initialized.
151      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152      -nomips16 command line options, and the default CPU.  */
153   int mips16;
154   /* Non-zero if we should not reorder instructions.  Changed by `.set
155      reorder' and `.set noreorder'.  */
156   int noreorder;
157   /* Non-zero if we should not permit the $at ($1) register to be used
158      in instructions.  Changed by `.set at' and `.set noat'.  */
159   int noat;
160   /* Non-zero if we should warn when a macro instruction expands into
161      more than one machine instruction.  Changed by `.set nomacro' and
162      `.set macro'.  */
163   int warn_about_macros;
164   /* Non-zero if we should not move instructions.  Changed by `.set
165      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
166   int nomove;
167   /* Non-zero if we should not optimize branches by moving the target
168      of the branch into the delay slot.  Actually, we don't perform
169      this optimization anyhow.  Changed by `.set bopt' and `.set
170      nobopt'.  */
171   int nobopt;
172   /* Non-zero if we should not autoextend mips16 instructions.
173      Changed by `.set autoextend' and `.set noautoextend'.  */
174   int noautoextend;
175   /* Restrict general purpose registers and floating point registers
176      to 32 bit.  This is initially determined when -mgp32 or -mfp32
177      is passed but can changed if the assembler code uses .set mipsN.  */
178   int gp32;
179   int fp32;
180 };
181
182 /* True if -mgp32 was passed.  */
183 static int file_mips_gp32 = -1;
184
185 /* True if -mfp32 was passed.  */
186 static int file_mips_fp32 = -1;
187
188 /* This is the struct we use to hold the current set of options.  Note
189    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190    -1 to indicate that they have not been initialized.  */
191
192 static struct mips_set_options mips_opts =
193 {
194   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
195 };
196
197 /* These variables are filled in with the masks of registers used.
198    The object format code reads them and puts them in the appropriate
199    place.  */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
202
203 /* MIPS ISA we are using for this output file.  */
204 static int file_mips_isa = ISA_UNKNOWN;
205
206 /* True if -mips16 was passed or implied by arguments passed on the
207    command line (e.g., by -march).  */
208 static int file_ase_mips16;
209
210 /* True if -mips3d was passed or implied by arguments passed on the
211    command line (e.g., by -march).  */
212 static int file_ase_mips3d;
213
214 /* True if -mdmx was passed or implied by arguments passed on the
215    command line (e.g., by -march).  */
216 static int file_ase_mdmx;
217
218 /* The argument of the -march= flag.  The architecture we are assembling.  */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
222
223 /* The argument of the -mtune= flag.  The architecture for which we
224    are optimizing.  */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
228
229 /* True when generating 32-bit code for a 64-bit processor.  */
230 static int mips_32bitmode = 0;
231
232 /* Some ISA's have delay slots for instructions which read or write
233    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236    delay slot in this ISA.  The uses of this macro assume that any
237    ISA that has delay slots for one of these, has them for all.  They
238    also assume that ISAs which don't have delays for these insns, don't
239    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
240 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
241    (ISA) == ISA_MIPS1                       \
242    || (ISA) == ISA_MIPS2                    \
243    || (ISA) == ISA_MIPS3                    \
244    )
245
246 /* True if the given ABI requires 32-bit registers.  */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249 /* Likewise 64-bit registers.  */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251   ((ABI) == N32_ABI               \
252    || (ABI) == N64_ABI            \
253    || (ABI) == O64_ABI)
254
255 /*  Return true if ISA supports 64 bit gp register instructions.  */
256 #define ISA_HAS_64BIT_REGS(ISA) (    \
257    (ISA) == ISA_MIPS3                \
258    || (ISA) == ISA_MIPS4             \
259    || (ISA) == ISA_MIPS5             \
260    || (ISA) == ISA_MIPS64            \
261    )
262
263 #define HAVE_32BIT_GPRS                            \
264     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
265
266 #define HAVE_32BIT_FPRS                            \
267     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
268
269 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
271
272 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
273
274 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
275
276 /* We can only have 64bit addresses if the object file format
277    supports it.  */
278 #define HAVE_32BIT_ADDRESSES                           \
279    (HAVE_32BIT_GPRS                                    \
280     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
281          || ! HAVE_64BIT_OBJECTS)                      \
282         && mips_pic != EMBEDDED_PIC))
283
284 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
285
286 /* Return true if the given CPU supports the MIPS16 ASE.  */
287 #define CPU_HAS_MIPS16(cpu)                                             \
288    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
289     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
290
291 /* Return true if the given CPU supports the MIPS3D ASE.  */
292 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
293                                  )
294
295 /* Return true if the given CPU supports the MDMX ASE.  */
296 #define CPU_HAS_MDMX(cpu)       (false                 \
297                                  )
298
299 /* True if CPU has a dror instruction.  */
300 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
301
302 /* True if CPU has a ror instruction.  */
303 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
304
305 /* Whether the processor uses hardware interlocks to protect
306    reads from the HI and LO registers, and thus does not
307    require nops to be inserted.  */
308
309 #define hilo_interlocks (mips_arch == CPU_R4010                       \
310                          || mips_arch == CPU_VR5500                   \
311                          || mips_arch == CPU_SB1                      \
312                          )
313
314 /* Whether the processor uses hardware interlocks to protect reads
315    from the GPRs, and thus does not require nops to be inserted.  */
316 #define gpr_interlocks \
317   (mips_opts.isa != ISA_MIPS1  \
318    || mips_arch == CPU_VR5400  \
319    || mips_arch == CPU_VR5500  \
320    || mips_arch == CPU_R3900)
321
322 /* As with other "interlocks" this is used by hardware that has FP
323    (co-processor) interlocks.  */
324 /* Itbl support may require additional care here.  */
325 #define cop_interlocks (mips_arch == CPU_R4300                        \
326                         || mips_arch == CPU_VR5400                    \
327                         || mips_arch == CPU_VR5500                    \
328                         || mips_arch == CPU_SB1                       \
329                         )
330
331 /* Is this a mfhi or mflo instruction?  */
332 #define MF_HILO_INSN(PINFO) \
333           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
334
335 /* MIPS PIC level.  */
336
337 enum mips_pic_level mips_pic;
338
339 /* Warn about all NOPS that the assembler generates.  */
340 static int warn_nops = 0;
341
342 /* 1 if we should generate 32 bit offsets from the $gp register in
343    SVR4_PIC mode.  Currently has no meaning in other modes.  */
344 static int mips_big_got = 0;
345
346 /* 1 if trap instructions should used for overflow rather than break
347    instructions.  */
348 static int mips_trap = 0;
349
350 /* 1 if double width floating point constants should not be constructed
351    by assembling two single width halves into two single width floating
352    point registers which just happen to alias the double width destination
353    register.  On some architectures this aliasing can be disabled by a bit
354    in the status register, and the setting of this bit cannot be determined
355    automatically at assemble time.  */
356 static int mips_disable_float_construction;
357
358 /* Non-zero if any .set noreorder directives were used.  */
359
360 static int mips_any_noreorder;
361
362 /* Non-zero if nops should be inserted when the register referenced in
363    an mfhi/mflo instruction is read in the next two instructions.  */
364 static int mips_7000_hilo_fix;
365
366 /* The size of the small data section.  */
367 static unsigned int g_switch_value = 8;
368 /* Whether the -G option was used.  */
369 static int g_switch_seen = 0;
370
371 #define N_RMASK 0xc4
372 #define N_VFP   0xd4
373
374 /* If we can determine in advance that GP optimization won't be
375    possible, we can skip the relaxation stuff that tries to produce
376    GP-relative references.  This makes delay slot optimization work
377    better.
378
379    This function can only provide a guess, but it seems to work for
380    gcc output.  It needs to guess right for gcc, otherwise gcc
381    will put what it thinks is a GP-relative instruction in a branch
382    delay slot.
383
384    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
385    fixed it for the non-PIC mode.  KR 95/04/07  */
386 static int nopic_need_relax PARAMS ((symbolS *, int));
387
388 /* handle of the OPCODE hash table */
389 static struct hash_control *op_hash = NULL;
390
391 /* The opcode hash table we use for the mips16.  */
392 static struct hash_control *mips16_op_hash = NULL;
393
394 /* This array holds the chars that always start a comment.  If the
395     pre-processor is disabled, these aren't very useful */
396 const char comment_chars[] = "#";
397
398 /* This array holds the chars that only start a comment at the beginning of
399    a line.  If the line seems to have the form '# 123 filename'
400    .line and .file directives will appear in the pre-processed output */
401 /* Note that input_file.c hand checks for '#' at the beginning of the
402    first line of the input file.  This is because the compiler outputs
403    #NO_APP at the beginning of its output.  */
404 /* Also note that C style comments are always supported.  */
405 const char line_comment_chars[] = "#";
406
407 /* This array holds machine specific line separator characters.  */
408 const char line_separator_chars[] = ";";
409
410 /* Chars that can be used to separate mant from exp in floating point nums */
411 const char EXP_CHARS[] = "eE";
412
413 /* Chars that mean this number is a floating point constant */
414 /* As in 0f12.456 */
415 /* or    0d1.2345e12 */
416 const char FLT_CHARS[] = "rRsSfFdDxXpP";
417
418 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
419    changed in read.c .  Ideally it shouldn't have to know about it at all,
420    but nothing is ideal around here.
421  */
422
423 static char *insn_error;
424
425 static int auto_align = 1;
426
427 /* When outputting SVR4 PIC code, the assembler needs to know the
428    offset in the stack frame from which to restore the $gp register.
429    This is set by the .cprestore pseudo-op, and saved in this
430    variable.  */
431 static offsetT mips_cprestore_offset = -1;
432
433 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
434    more optimizations, it can use a register value instead of a memory-saved
435    offset and even an other register than $gp as global pointer.  */
436 static offsetT mips_cpreturn_offset = -1;
437 static int mips_cpreturn_register = -1;
438 static int mips_gp_register = GP;
439 static int mips_gprel_offset = 0;
440
441 /* Whether mips_cprestore_offset has been set in the current function
442    (or whether it has already been warned about, if not).  */
443 static int mips_cprestore_valid = 0;
444
445 /* This is the register which holds the stack frame, as set by the
446    .frame pseudo-op.  This is needed to implement .cprestore.  */
447 static int mips_frame_reg = SP;
448
449 /* Whether mips_frame_reg has been set in the current function
450    (or whether it has already been warned about, if not).  */
451 static int mips_frame_reg_valid = 0;
452
453 /* To output NOP instructions correctly, we need to keep information
454    about the previous two instructions.  */
455
456 /* Whether we are optimizing.  The default value of 2 means to remove
457    unneeded NOPs and swap branch instructions when possible.  A value
458    of 1 means to not swap branches.  A value of 0 means to always
459    insert NOPs.  */
460 static int mips_optimize = 2;
461
462 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
463    equivalent to seeing no -g option at all.  */
464 static int mips_debug = 0;
465
466 /* The previous instruction.  */
467 static struct mips_cl_insn prev_insn;
468
469 /* The instruction before prev_insn.  */
470 static struct mips_cl_insn prev_prev_insn;
471
472 /* If we don't want information for prev_insn or prev_prev_insn, we
473    point the insn_mo field at this dummy integer.  */
474 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
475
476 /* Non-zero if prev_insn is valid.  */
477 static int prev_insn_valid;
478
479 /* The frag for the previous instruction.  */
480 static struct frag *prev_insn_frag;
481
482 /* The offset into prev_insn_frag for the previous instruction.  */
483 static long prev_insn_where;
484
485 /* The reloc type for the previous instruction, if any.  */
486 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
487
488 /* The reloc for the previous instruction, if any.  */
489 static fixS *prev_insn_fixp[3];
490
491 /* Non-zero if the previous instruction was in a delay slot.  */
492 static int prev_insn_is_delay_slot;
493
494 /* Non-zero if the previous instruction was in a .set noreorder.  */
495 static int prev_insn_unreordered;
496
497 /* Non-zero if the previous instruction uses an extend opcode (if
498    mips16).  */
499 static int prev_insn_extended;
500
501 /* Non-zero if the previous previous instruction was in a .set
502    noreorder.  */
503 static int prev_prev_insn_unreordered;
504
505 /* If this is set, it points to a frag holding nop instructions which
506    were inserted before the start of a noreorder section.  If those
507    nops turn out to be unnecessary, the size of the frag can be
508    decreased.  */
509 static fragS *prev_nop_frag;
510
511 /* The number of nop instructions we created in prev_nop_frag.  */
512 static int prev_nop_frag_holds;
513
514 /* The number of nop instructions that we know we need in
515    prev_nop_frag.  */
516 static int prev_nop_frag_required;
517
518 /* The number of instructions we've seen since prev_nop_frag.  */
519 static int prev_nop_frag_since;
520
521 /* For ECOFF and ELF, relocations against symbols are done in two
522    parts, with a HI relocation and a LO relocation.  Each relocation
523    has only 16 bits of space to store an addend.  This means that in
524    order for the linker to handle carries correctly, it must be able
525    to locate both the HI and the LO relocation.  This means that the
526    relocations must appear in order in the relocation table.
527
528    In order to implement this, we keep track of each unmatched HI
529    relocation.  We then sort them so that they immediately precede the
530    corresponding LO relocation.  */
531
532 struct mips_hi_fixup
533 {
534   /* Next HI fixup.  */
535   struct mips_hi_fixup *next;
536   /* This fixup.  */
537   fixS *fixp;
538   /* The section this fixup is in.  */
539   segT seg;
540 };
541
542 /* The list of unmatched HI relocs.  */
543
544 static struct mips_hi_fixup *mips_hi_fixup_list;
545
546 /* Map normal MIPS register numbers to mips16 register numbers.  */
547
548 #define X ILLEGAL_REG
549 static const int mips32_to_16_reg_map[] =
550 {
551   X, X, 2, 3, 4, 5, 6, 7,
552   X, X, X, X, X, X, X, X,
553   0, 1, X, X, X, X, X, X,
554   X, X, X, X, X, X, X, X
555 };
556 #undef X
557
558 /* Map mips16 register numbers to normal MIPS register numbers.  */
559
560 static const unsigned int mips16_to_32_reg_map[] =
561 {
562   16, 17, 2, 3, 4, 5, 6, 7
563 };
564
565 static int mips_fix_4122_bugs;
566
567 /* We don't relax branches by default, since this causes us to expand
568    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
569    fail to compute the offset before expanding the macro to the most
570    efficient expansion.  */
571
572 static int mips_relax_branch;
573 \f
574 /* Since the MIPS does not have multiple forms of PC relative
575    instructions, we do not have to do relaxing as is done on other
576    platforms.  However, we do have to handle GP relative addressing
577    correctly, which turns out to be a similar problem.
578
579    Every macro that refers to a symbol can occur in (at least) two
580    forms, one with GP relative addressing and one without.  For
581    example, loading a global variable into a register generally uses
582    a macro instruction like this:
583      lw $4,i
584    If i can be addressed off the GP register (this is true if it is in
585    the .sbss or .sdata section, or if it is known to be smaller than
586    the -G argument) this will generate the following instruction:
587      lw $4,i($gp)
588    This instruction will use a GPREL reloc.  If i can not be addressed
589    off the GP register, the following instruction sequence will be used:
590      lui $at,i
591      lw $4,i($at)
592    In this case the first instruction will have a HI16 reloc, and the
593    second reloc will have a LO16 reloc.  Both relocs will be against
594    the symbol i.
595
596    The issue here is that we may not know whether i is GP addressable
597    until after we see the instruction that uses it.  Therefore, we
598    want to be able to choose the final instruction sequence only at
599    the end of the assembly.  This is similar to the way other
600    platforms choose the size of a PC relative instruction only at the
601    end of assembly.
602
603    When generating position independent code we do not use GP
604    addressing in quite the same way, but the issue still arises as
605    external symbols and local symbols must be handled differently.
606
607    We handle these issues by actually generating both possible
608    instruction sequences.  The longer one is put in a frag_var with
609    type rs_machine_dependent.  We encode what to do with the frag in
610    the subtype field.  We encode (1) the number of existing bytes to
611    replace, (2) the number of new bytes to use, (3) the offset from
612    the start of the existing bytes to the first reloc we must generate
613    (that is, the offset is applied from the start of the existing
614    bytes after they are replaced by the new bytes, if any), (4) the
615    offset from the start of the existing bytes to the second reloc,
616    (5) whether a third reloc is needed (the third reloc is always four
617    bytes after the second reloc), and (6) whether to warn if this
618    variant is used (this is sometimes needed if .set nomacro or .set
619    noat is in effect).  All these numbers are reasonably small.
620
621    Generating two instruction sequences must be handled carefully to
622    ensure that delay slots are handled correctly.  Fortunately, there
623    are a limited number of cases.  When the second instruction
624    sequence is generated, append_insn is directed to maintain the
625    existing delay slot information, so it continues to apply to any
626    code after the second instruction sequence.  This means that the
627    second instruction sequence must not impose any requirements not
628    required by the first instruction sequence.
629
630    These variant frags are then handled in functions called by the
631    machine independent code.  md_estimate_size_before_relax returns
632    the final size of the frag.  md_convert_frag sets up the final form
633    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
634    one if needed.  */
635 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
636   ((relax_substateT) \
637    (((old) << 23) \
638     | ((new) << 16) \
639     | (((reloc1) + 64) << 9) \
640     | (((reloc2) + 64) << 2) \
641     | ((reloc3) ? (1 << 1) : 0) \
642     | ((warn) ? 1 : 0)))
643 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
644 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
645 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
646 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
647 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
648 #define RELAX_WARN(i) ((i) & 1)
649
650 /* Branch without likely bit.  If label is out of range, we turn:
651
652         beq reg1, reg2, label
653         delay slot
654
655    into
656
657         bne reg1, reg2, 0f
658         nop
659         j label
660      0: delay slot
661
662    with the following opcode replacements:
663
664         beq <-> bne
665         blez <-> bgtz
666         bltz <-> bgez
667         bc1f <-> bc1t
668
669         bltzal <-> bgezal  (with jal label instead of j label)
670
671    Even though keeping the delay slot instruction in the delay slot of
672    the branch would be more efficient, it would be very tricky to do
673    correctly, because we'd have to introduce a variable frag *after*
674    the delay slot instruction, and expand that instead.  Let's do it
675    the easy way for now, even if the branch-not-taken case now costs
676    one additional instruction.  Out-of-range branches are not supposed
677    to be common, anyway.
678
679    Branch likely.  If label is out of range, we turn:
680
681         beql reg1, reg2, label
682         delay slot (annulled if branch not taken)
683
684    into
685
686         beql reg1, reg2, 1f
687         nop
688         beql $0, $0, 2f
689         nop
690      1: j[al] label
691         delay slot (executed only if branch taken)
692      2:
693
694    It would be possible to generate a shorter sequence by losing the
695    likely bit, generating something like:
696      
697         bne reg1, reg2, 0f
698         nop
699         j[al] label
700         delay slot (executed only if branch taken)
701      0:
702
703         beql -> bne
704         bnel -> beq
705         blezl -> bgtz
706         bgtzl -> blez
707         bltzl -> bgez
708         bgezl -> bltz
709         bc1fl -> bc1t
710         bc1tl -> bc1f
711
712         bltzall -> bgezal  (with jal label instead of j label)
713         bgezall -> bltzal  (ditto)
714
715
716    but it's not clear that it would actually improve performance.  */
717 #define RELAX_BRANCH_ENCODE(reloc_s2, uncond, likely, link, toofar) \
718   ((relax_substateT) \
719    (0xc0000000 \
720     | ((toofar) ? 1 : 0) \
721     | ((link) ? 2 : 0) \
722     | ((likely) ? 4 : 0) \
723     | ((uncond) ? 8 : 0) \
724     | ((reloc_s2) ? 16 : 0)))
725 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
726 #define RELAX_BRANCH_RELOC_S2(i) (((i) & 16) != 0)
727 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
728 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
729 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
730 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1))
731
732 /* For mips16 code, we use an entirely different form of relaxation.
733    mips16 supports two versions of most instructions which take
734    immediate values: a small one which takes some small value, and a
735    larger one which takes a 16 bit value.  Since branches also follow
736    this pattern, relaxing these values is required.
737
738    We can assemble both mips16 and normal MIPS code in a single
739    object.  Therefore, we need to support this type of relaxation at
740    the same time that we support the relaxation described above.  We
741    use the high bit of the subtype field to distinguish these cases.
742
743    The information we store for this type of relaxation is the
744    argument code found in the opcode file for this relocation, whether
745    the user explicitly requested a small or extended form, and whether
746    the relocation is in a jump or jal delay slot.  That tells us the
747    size of the value, and how it should be stored.  We also store
748    whether the fragment is considered to be extended or not.  We also
749    store whether this is known to be a branch to a different section,
750    whether we have tried to relax this frag yet, and whether we have
751    ever extended a PC relative fragment because of a shift count.  */
752 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
753   (0x80000000                                                   \
754    | ((type) & 0xff)                                            \
755    | ((small) ? 0x100 : 0)                                      \
756    | ((ext) ? 0x200 : 0)                                        \
757    | ((dslot) ? 0x400 : 0)                                      \
758    | ((jal_dslot) ? 0x800 : 0))
759 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
760 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
761 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
762 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
763 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
764 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
765 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
766 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
767 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
768 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
769 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
770 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
771
772 /* Is the given value a sign-extended 32-bit value?  */
773 #define IS_SEXT_32BIT_NUM(x)                                            \
774   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
775    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
776
777 /* Is the given value a sign-extended 16-bit value?  */
778 #define IS_SEXT_16BIT_NUM(x)                                            \
779   (((x) &~ (offsetT) 0x7fff) == 0                                       \
780    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
781
782 \f
783 /* Prototypes for static functions.  */
784
785 #ifdef __STDC__
786 #define internalError() \
787     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
788 #else
789 #define internalError() as_fatal (_("MIPS internal Error"));
790 #endif
791
792 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
793
794 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
795                                   unsigned int reg, enum mips_regclass class));
796 static int reg_needs_delay PARAMS ((unsigned int));
797 static void mips16_mark_labels PARAMS ((void));
798 static void append_insn PARAMS ((char *place,
799                                  struct mips_cl_insn * ip,
800                                  expressionS * p,
801                                  bfd_reloc_code_real_type *r,
802                                  boolean));
803 static void mips_no_prev_insn PARAMS ((int));
804 static void mips_emit_delays PARAMS ((boolean));
805 #ifdef USE_STDARG
806 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
807                                  const char *name, const char *fmt,
808                                  ...));
809 #else
810 static void macro_build ();
811 #endif
812 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
813                                         const char *, const char *,
814                                         va_list));
815 static void macro_build_jalr PARAMS ((int, expressionS *));
816 static void macro_build_lui PARAMS ((char *place, int *counter,
817                                      expressionS * ep, int regnum));
818 static void macro_build_ldst_constoffset PARAMS ((char *place, int *counter,
819                                                   expressionS * ep, const char *op,
820                                                   int valreg, int breg));
821 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
822 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
823                                          expressionS *));
824 static void load_register PARAMS ((int *, int, expressionS *, int));
825 static void load_address PARAMS ((int *, int, expressionS *, int *));
826 static void move_register PARAMS ((int *, int, int));
827 static void macro PARAMS ((struct mips_cl_insn * ip));
828 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
829 #ifdef LOSING_COMPILER
830 static void macro2 PARAMS ((struct mips_cl_insn * ip));
831 #endif
832 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
833 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
834 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
835                                   boolean, boolean, unsigned long *,
836                                   boolean *, unsigned short *));
837 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
838 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
839 static int my_getSmallExpression PARAMS ((expressionS *, char *));
840 static void my_getExpression PARAMS ((expressionS *, char *));
841 #ifdef OBJ_ELF
842 static int support_64bit_objects PARAMS((void));
843 #endif
844 static void mips_set_option_string PARAMS ((const char **, const char *));
845 static symbolS *get_symbol PARAMS ((void));
846 static void mips_align PARAMS ((int to, int fill, symbolS *label));
847 static void s_align PARAMS ((int));
848 static void s_change_sec PARAMS ((int));
849 static void s_change_section PARAMS ((int));
850 static void s_cons PARAMS ((int));
851 static void s_float_cons PARAMS ((int));
852 static void s_mips_globl PARAMS ((int));
853 static void s_option PARAMS ((int));
854 static void s_mipsset PARAMS ((int));
855 static void s_abicalls PARAMS ((int));
856 static void s_cpload PARAMS ((int));
857 static void s_cpsetup PARAMS ((int));
858 static void s_cplocal PARAMS ((int));
859 static void s_cprestore PARAMS ((int));
860 static void s_cpreturn PARAMS ((int));
861 static void s_gpvalue PARAMS ((int));
862 static void s_gpword PARAMS ((int));
863 static void s_gpdword PARAMS ((int));
864 static void s_cpadd PARAMS ((int));
865 static void s_insn PARAMS ((int));
866 static void md_obj_begin PARAMS ((void));
867 static void md_obj_end PARAMS ((void));
868 static long get_number PARAMS ((void));
869 static void s_mips_ent PARAMS ((int));
870 static void s_mips_end PARAMS ((int));
871 static void s_mips_frame PARAMS ((int));
872 static void s_mips_mask PARAMS ((int));
873 static void s_mips_stab PARAMS ((int));
874 static void s_mips_weakext PARAMS ((int));
875 static void s_mips_file PARAMS ((int));
876 static void s_mips_loc PARAMS ((int));
877 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
878 static int relaxed_branch_length (fragS *, asection *, int);
879 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
880 static void show PARAMS ((FILE *, const char *, int *, int *));
881 #ifdef OBJ_ELF
882 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
883 #endif
884
885 /* Return values of my_getSmallExpression().  */
886
887 enum small_ex_type
888 {
889   S_EX_NONE = 0,
890   S_EX_REGISTER,
891
892   /* Direct relocation creation by %percent_op().  */
893   S_EX_HALF,
894   S_EX_HI,
895   S_EX_LO,
896   S_EX_GP_REL,
897   S_EX_GOT,
898   S_EX_CALL16,
899   S_EX_GOT_DISP,
900   S_EX_GOT_PAGE,
901   S_EX_GOT_OFST,
902   S_EX_GOT_HI,
903   S_EX_GOT_LO,
904   S_EX_NEG,
905   S_EX_HIGHER,
906   S_EX_HIGHEST,
907   S_EX_CALL_HI,
908   S_EX_CALL_LO
909 };
910
911 /* Table and functions used to map between CPU/ISA names, and
912    ISA levels, and CPU numbers.  */
913
914 struct mips_cpu_info
915 {
916   const char *name;           /* CPU or ISA name.  */
917   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
918   int isa;                    /* ISA level.  */
919   int cpu;                    /* CPU number (default CPU if ISA).  */
920 };
921
922 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
923 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
924 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
925                                                         const char *));
926 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
927 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
928                                                            const char *));
929 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
930 \f
931 /* Pseudo-op table.
932
933    The following pseudo-ops from the Kane and Heinrich MIPS book
934    should be defined here, but are currently unsupported: .alias,
935    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
936
937    The following pseudo-ops from the Kane and Heinrich MIPS book are
938    specific to the type of debugging information being generated, and
939    should be defined by the object format: .aent, .begin, .bend,
940    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
941    .vreg.
942
943    The following pseudo-ops from the Kane and Heinrich MIPS book are
944    not MIPS CPU specific, but are also not specific to the object file
945    format.  This file is probably the best place to define them, but
946    they are not currently supported: .asm0, .endr, .lab, .repeat,
947    .struct.  */
948
949 static const pseudo_typeS mips_pseudo_table[] =
950 {
951   /* MIPS specific pseudo-ops.  */
952   {"option", s_option, 0},
953   {"set", s_mipsset, 0},
954   {"rdata", s_change_sec, 'r'},
955   {"sdata", s_change_sec, 's'},
956   {"livereg", s_ignore, 0},
957   {"abicalls", s_abicalls, 0},
958   {"cpload", s_cpload, 0},
959   {"cpsetup", s_cpsetup, 0},
960   {"cplocal", s_cplocal, 0},
961   {"cprestore", s_cprestore, 0},
962   {"cpreturn", s_cpreturn, 0},
963   {"gpvalue", s_gpvalue, 0},
964   {"gpword", s_gpword, 0},
965   {"gpdword", s_gpdword, 0},
966   {"cpadd", s_cpadd, 0},
967   {"insn", s_insn, 0},
968
969   /* Relatively generic pseudo-ops that happen to be used on MIPS
970      chips.  */
971   {"asciiz", stringer, 1},
972   {"bss", s_change_sec, 'b'},
973   {"err", s_err, 0},
974   {"half", s_cons, 1},
975   {"dword", s_cons, 3},
976   {"weakext", s_mips_weakext, 0},
977
978   /* These pseudo-ops are defined in read.c, but must be overridden
979      here for one reason or another.  */
980   {"align", s_align, 0},
981   {"byte", s_cons, 0},
982   {"data", s_change_sec, 'd'},
983   {"double", s_float_cons, 'd'},
984   {"float", s_float_cons, 'f'},
985   {"globl", s_mips_globl, 0},
986   {"global", s_mips_globl, 0},
987   {"hword", s_cons, 1},
988   {"int", s_cons, 2},
989   {"long", s_cons, 2},
990   {"octa", s_cons, 4},
991   {"quad", s_cons, 3},
992   {"section", s_change_section, 0},
993   {"short", s_cons, 1},
994   {"single", s_float_cons, 'f'},
995   {"stabn", s_mips_stab, 'n'},
996   {"text", s_change_sec, 't'},
997   {"word", s_cons, 2},
998
999   { "extern", ecoff_directive_extern, 0},
1000
1001   { NULL, NULL, 0 },
1002 };
1003
1004 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1005 {
1006   /* These pseudo-ops should be defined by the object file format.
1007      However, a.out doesn't support them, so we have versions here.  */
1008   {"aent", s_mips_ent, 1},
1009   {"bgnb", s_ignore, 0},
1010   {"end", s_mips_end, 0},
1011   {"endb", s_ignore, 0},
1012   {"ent", s_mips_ent, 0},
1013   {"file", s_mips_file, 0},
1014   {"fmask", s_mips_mask, 'F'},
1015   {"frame", s_mips_frame, 0},
1016   {"loc", s_mips_loc, 0},
1017   {"mask", s_mips_mask, 'R'},
1018   {"verstamp", s_ignore, 0},
1019   { NULL, NULL, 0 },
1020 };
1021
1022 extern void pop_insert PARAMS ((const pseudo_typeS *));
1023
1024 void
1025 mips_pop_insert ()
1026 {
1027   pop_insert (mips_pseudo_table);
1028   if (! ECOFF_DEBUGGING)
1029     pop_insert (mips_nonecoff_pseudo_table);
1030 }
1031 \f
1032 /* Symbols labelling the current insn.  */
1033
1034 struct insn_label_list
1035 {
1036   struct insn_label_list *next;
1037   symbolS *label;
1038 };
1039
1040 static struct insn_label_list *insn_labels;
1041 static struct insn_label_list *free_insn_labels;
1042
1043 static void mips_clear_insn_labels PARAMS ((void));
1044
1045 static inline void
1046 mips_clear_insn_labels ()
1047 {
1048   register struct insn_label_list **pl;
1049
1050   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1051     ;
1052   *pl = insn_labels;
1053   insn_labels = NULL;
1054 }
1055 \f
1056 static char *expr_end;
1057
1058 /* Expressions which appear in instructions.  These are set by
1059    mips_ip.  */
1060
1061 static expressionS imm_expr;
1062 static expressionS offset_expr;
1063
1064 /* Relocs associated with imm_expr and offset_expr.  */
1065
1066 static bfd_reloc_code_real_type imm_reloc[3]
1067   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1068 static bfd_reloc_code_real_type offset_reloc[3]
1069   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1070
1071 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
1072
1073 static boolean imm_unmatched_hi;
1074
1075 /* These are set by mips16_ip if an explicit extension is used.  */
1076
1077 static boolean mips16_small, mips16_ext;
1078
1079 #ifdef OBJ_ELF
1080 /* The pdr segment for per procedure frame/regmask info.  Not used for
1081    ECOFF debugging.  */
1082
1083 static segT pdr_seg;
1084 #endif
1085
1086 /* The default target format to use.  */
1087
1088 const char *
1089 mips_target_format ()
1090 {
1091   switch (OUTPUT_FLAVOR)
1092     {
1093     case bfd_target_aout_flavour:
1094       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1095     case bfd_target_ecoff_flavour:
1096       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1097     case bfd_target_coff_flavour:
1098       return "pe-mips";
1099     case bfd_target_elf_flavour:
1100 #ifdef TE_TMIPS
1101       /* This is traditional mips.  */
1102       return (target_big_endian
1103               ? (HAVE_64BIT_OBJECTS
1104                  ? "elf64-tradbigmips"
1105                  : (HAVE_NEWABI
1106                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1107               : (HAVE_64BIT_OBJECTS
1108                  ? "elf64-tradlittlemips"
1109                  : (HAVE_NEWABI
1110                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1111 #else
1112       return (target_big_endian
1113               ? (HAVE_64BIT_OBJECTS
1114                  ? "elf64-bigmips"
1115                  : (HAVE_NEWABI
1116                     ? "elf32-nbigmips" : "elf32-bigmips"))
1117               : (HAVE_64BIT_OBJECTS
1118                  ? "elf64-littlemips"
1119                  : (HAVE_NEWABI
1120                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1121 #endif
1122     default:
1123       abort ();
1124       return NULL;
1125     }
1126 }
1127
1128 /* This function is called once, at assembler startup time.  It should
1129    set up all the tables, etc. that the MD part of the assembler will need.  */
1130
1131 void
1132 md_begin ()
1133 {
1134   register const char *retval = NULL;
1135   int i = 0;
1136   int broken = 0;
1137
1138   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1139     as_warn (_("Could not set architecture and machine"));
1140
1141   op_hash = hash_new ();
1142
1143   for (i = 0; i < NUMOPCODES;)
1144     {
1145       const char *name = mips_opcodes[i].name;
1146
1147       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1148       if (retval != NULL)
1149         {
1150           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1151                    mips_opcodes[i].name, retval);
1152           /* Probably a memory allocation problem?  Give up now.  */
1153           as_fatal (_("Broken assembler.  No assembly attempted."));
1154         }
1155       do
1156         {
1157           if (mips_opcodes[i].pinfo != INSN_MACRO)
1158             {
1159               if (!validate_mips_insn (&mips_opcodes[i]))
1160                 broken = 1;
1161             }
1162           ++i;
1163         }
1164       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1165     }
1166
1167   mips16_op_hash = hash_new ();
1168
1169   i = 0;
1170   while (i < bfd_mips16_num_opcodes)
1171     {
1172       const char *name = mips16_opcodes[i].name;
1173
1174       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1175       if (retval != NULL)
1176         as_fatal (_("internal: can't hash `%s': %s"),
1177                   mips16_opcodes[i].name, retval);
1178       do
1179         {
1180           if (mips16_opcodes[i].pinfo != INSN_MACRO
1181               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1182                   != mips16_opcodes[i].match))
1183             {
1184               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1185                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1186               broken = 1;
1187             }
1188           ++i;
1189         }
1190       while (i < bfd_mips16_num_opcodes
1191              && strcmp (mips16_opcodes[i].name, name) == 0);
1192     }
1193
1194   if (broken)
1195     as_fatal (_("Broken assembler.  No assembly attempted."));
1196
1197   /* We add all the general register names to the symbol table.  This
1198      helps us detect invalid uses of them.  */
1199   for (i = 0; i < 32; i++)
1200     {
1201       char buf[5];
1202
1203       sprintf (buf, "$%d", i);
1204       symbol_table_insert (symbol_new (buf, reg_section, i,
1205                                        &zero_address_frag));
1206     }
1207   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1208                                    &zero_address_frag));
1209   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1210                                    &zero_address_frag));
1211   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1212                                    &zero_address_frag));
1213   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1214                                    &zero_address_frag));
1215   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1216                                    &zero_address_frag));
1217   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1218                                    &zero_address_frag));
1219   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1220                                    &zero_address_frag));
1221   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1222                                    &zero_address_frag));
1223   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1224                                    &zero_address_frag));
1225
1226   /* If we don't add these register names to the symbol table, they
1227      may end up being added as regular symbols by operand(), and then
1228      make it to the object file as undefined in case they're not
1229      regarded as local symbols.  They're local in o32, since `$' is a
1230      local symbol prefix, but not in n32 or n64.  */
1231   for (i = 0; i < 8; i++)
1232     {
1233       char buf[6];
1234
1235       sprintf (buf, "$fcc%i", i);
1236       symbol_table_insert (symbol_new (buf, reg_section, -1,
1237                                        &zero_address_frag));
1238     }
1239
1240   mips_no_prev_insn (false);
1241
1242   mips_gprmask = 0;
1243   mips_cprmask[0] = 0;
1244   mips_cprmask[1] = 0;
1245   mips_cprmask[2] = 0;
1246   mips_cprmask[3] = 0;
1247
1248   /* set the default alignment for the text section (2**2) */
1249   record_alignment (text_section, 2);
1250
1251   if (USE_GLOBAL_POINTER_OPT)
1252     bfd_set_gp_size (stdoutput, g_switch_value);
1253
1254   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1255     {
1256       /* On a native system, sections must be aligned to 16 byte
1257          boundaries.  When configured for an embedded ELF target, we
1258          don't bother.  */
1259       if (strcmp (TARGET_OS, "elf") != 0)
1260         {
1261           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1262           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1263           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1264         }
1265
1266       /* Create a .reginfo section for register masks and a .mdebug
1267          section for debugging information.  */
1268       {
1269         segT seg;
1270         subsegT subseg;
1271         flagword flags;
1272         segT sec;
1273
1274         seg = now_seg;
1275         subseg = now_subseg;
1276
1277         /* The ABI says this section should be loaded so that the
1278            running program can access it.  However, we don't load it
1279            if we are configured for an embedded target */
1280         flags = SEC_READONLY | SEC_DATA;
1281         if (strcmp (TARGET_OS, "elf") != 0)
1282           flags |= SEC_ALLOC | SEC_LOAD;
1283
1284         if (mips_abi != N64_ABI)
1285           {
1286             sec = subseg_new (".reginfo", (subsegT) 0);
1287
1288             bfd_set_section_flags (stdoutput, sec, flags);
1289             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1290
1291 #ifdef OBJ_ELF
1292             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1293 #endif
1294           }
1295         else
1296           {
1297             /* The 64-bit ABI uses a .MIPS.options section rather than
1298                .reginfo section.  */
1299             sec = subseg_new (".MIPS.options", (subsegT) 0);
1300             bfd_set_section_flags (stdoutput, sec, flags);
1301             bfd_set_section_alignment (stdoutput, sec, 3);
1302
1303 #ifdef OBJ_ELF
1304             /* Set up the option header.  */
1305             {
1306               Elf_Internal_Options opthdr;
1307               char *f;
1308
1309               opthdr.kind = ODK_REGINFO;
1310               opthdr.size = (sizeof (Elf_External_Options)
1311                              + sizeof (Elf64_External_RegInfo));
1312               opthdr.section = 0;
1313               opthdr.info = 0;
1314               f = frag_more (sizeof (Elf_External_Options));
1315               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1316                                              (Elf_External_Options *) f);
1317
1318               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1319             }
1320 #endif
1321           }
1322
1323         if (ECOFF_DEBUGGING)
1324           {
1325             sec = subseg_new (".mdebug", (subsegT) 0);
1326             (void) bfd_set_section_flags (stdoutput, sec,
1327                                           SEC_HAS_CONTENTS | SEC_READONLY);
1328             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1329           }
1330 #ifdef OBJ_ELF
1331         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1332           {
1333             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1334             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1335                                           SEC_READONLY | SEC_RELOC
1336                                           | SEC_DEBUGGING);
1337             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1338           }
1339 #endif
1340
1341         subseg_set (seg, subseg);
1342       }
1343     }
1344
1345   if (! ECOFF_DEBUGGING)
1346     md_obj_begin ();
1347 }
1348
1349 void
1350 md_mips_end ()
1351 {
1352   if (! ECOFF_DEBUGGING)
1353     md_obj_end ();
1354 }
1355
1356 void
1357 md_assemble (str)
1358      char *str;
1359 {
1360   struct mips_cl_insn insn;
1361   bfd_reloc_code_real_type unused_reloc[3]
1362     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1363
1364   imm_expr.X_op = O_absent;
1365   imm_unmatched_hi = false;
1366   offset_expr.X_op = O_absent;
1367   imm_reloc[0] = BFD_RELOC_UNUSED;
1368   imm_reloc[1] = BFD_RELOC_UNUSED;
1369   imm_reloc[2] = BFD_RELOC_UNUSED;
1370   offset_reloc[0] = BFD_RELOC_UNUSED;
1371   offset_reloc[1] = BFD_RELOC_UNUSED;
1372   offset_reloc[2] = BFD_RELOC_UNUSED;
1373
1374   if (mips_opts.mips16)
1375     mips16_ip (str, &insn);
1376   else
1377     {
1378       mips_ip (str, &insn);
1379       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1380             str, insn.insn_opcode));
1381     }
1382
1383   if (insn_error)
1384     {
1385       as_bad ("%s `%s'", insn_error, str);
1386       return;
1387     }
1388
1389   if (insn.insn_mo->pinfo == INSN_MACRO)
1390     {
1391       if (mips_opts.mips16)
1392         mips16_macro (&insn);
1393       else
1394         macro (&insn);
1395     }
1396   else
1397     {
1398       if (imm_expr.X_op != O_absent)
1399         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1400       else if (offset_expr.X_op != O_absent)
1401         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1402       else
1403         append_insn (NULL, &insn, NULL, unused_reloc, false);
1404     }
1405 }
1406
1407 /* See whether instruction IP reads register REG.  CLASS is the type
1408    of register.  */
1409
1410 static int
1411 insn_uses_reg (ip, reg, class)
1412      struct mips_cl_insn *ip;
1413      unsigned int reg;
1414      enum mips_regclass class;
1415 {
1416   if (class == MIPS16_REG)
1417     {
1418       assert (mips_opts.mips16);
1419       reg = mips16_to_32_reg_map[reg];
1420       class = MIPS_GR_REG;
1421     }
1422
1423   /* Don't report on general register ZERO, since it never changes.  */
1424   if (class == MIPS_GR_REG && reg == ZERO)
1425     return 0;
1426
1427   if (class == MIPS_FP_REG)
1428     {
1429       assert (! mips_opts.mips16);
1430       /* If we are called with either $f0 or $f1, we must check $f0.
1431          This is not optimal, because it will introduce an unnecessary
1432          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1433          need to distinguish reading both $f0 and $f1 or just one of
1434          them.  Note that we don't have to check the other way,
1435          because there is no instruction that sets both $f0 and $f1
1436          and requires a delay.  */
1437       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1438           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1439               == (reg &~ (unsigned) 1)))
1440         return 1;
1441       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1442           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1443               == (reg &~ (unsigned) 1)))
1444         return 1;
1445     }
1446   else if (! mips_opts.mips16)
1447     {
1448       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1449           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1450         return 1;
1451       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1452           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1453         return 1;
1454     }
1455   else
1456     {
1457       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1458           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1459                                     & MIPS16OP_MASK_RX)]
1460               == reg))
1461         return 1;
1462       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1463           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1464                                     & MIPS16OP_MASK_RY)]
1465               == reg))
1466         return 1;
1467       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1468           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1469                                     & MIPS16OP_MASK_MOVE32Z)]
1470               == reg))
1471         return 1;
1472       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1473         return 1;
1474       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1475         return 1;
1476       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1477         return 1;
1478       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1479           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1480               & MIPS16OP_MASK_REGR32) == reg)
1481         return 1;
1482     }
1483
1484   return 0;
1485 }
1486
1487 /* This function returns true if modifying a register requires a
1488    delay.  */
1489
1490 static int
1491 reg_needs_delay (reg)
1492      unsigned int reg;
1493 {
1494   unsigned long prev_pinfo;
1495
1496   prev_pinfo = prev_insn.insn_mo->pinfo;
1497   if (! mips_opts.noreorder
1498       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1499       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1500           || (! gpr_interlocks
1501               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1502     {
1503       /* A load from a coprocessor or from memory.  All load
1504          delays delay the use of general register rt for one
1505          instruction on the r3000.  The r6000 and r4000 use
1506          interlocks.  */
1507       /* Itbl support may require additional care here.  */
1508       know (prev_pinfo & INSN_WRITE_GPR_T);
1509       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1510         return 1;
1511     }
1512
1513   return 0;
1514 }
1515
1516 /* Mark instruction labels in mips16 mode.  This permits the linker to
1517    handle them specially, such as generating jalx instructions when
1518    needed.  We also make them odd for the duration of the assembly, in
1519    order to generate the right sort of code.  We will make them even
1520    in the adjust_symtab routine, while leaving them marked.  This is
1521    convenient for the debugger and the disassembler.  The linker knows
1522    to make them odd again.  */
1523
1524 static void
1525 mips16_mark_labels ()
1526 {
1527   if (mips_opts.mips16)
1528     {
1529       struct insn_label_list *l;
1530       valueT val;
1531
1532       for (l = insn_labels; l != NULL; l = l->next)
1533         {
1534 #ifdef OBJ_ELF
1535           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1536             S_SET_OTHER (l->label, STO_MIPS16);
1537 #endif
1538           val = S_GET_VALUE (l->label);
1539           if ((val & 1) == 0)
1540             S_SET_VALUE (l->label, val + 1);
1541         }
1542     }
1543 }
1544
1545 /* Output an instruction.  PLACE is where to put the instruction; if
1546    it is NULL, this uses frag_more to get room.  IP is the instruction
1547    information.  ADDRESS_EXPR is an operand of the instruction to be
1548    used with RELOC_TYPE.  */
1549
1550 static void
1551 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1552      char *place;
1553      struct mips_cl_insn *ip;
1554      expressionS *address_expr;
1555      bfd_reloc_code_real_type *reloc_type;
1556      boolean unmatched_hi;
1557 {
1558   register unsigned long prev_pinfo, pinfo;
1559   char *f;
1560   fixS *fixp[3];
1561   int nops = 0;
1562
1563   /* Mark instruction labels in mips16 mode.  */
1564   mips16_mark_labels ();
1565
1566   prev_pinfo = prev_insn.insn_mo->pinfo;
1567   pinfo = ip->insn_mo->pinfo;
1568
1569   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1570     {
1571       int prev_prev_nop;
1572
1573       /* If the previous insn required any delay slots, see if we need
1574          to insert a NOP or two.  There are eight kinds of possible
1575          hazards, of which an instruction can have at most one type.
1576          (1) a load from memory delay
1577          (2) a load from a coprocessor delay
1578          (3) an unconditional branch delay
1579          (4) a conditional branch delay
1580          (5) a move to coprocessor register delay
1581          (6) a load coprocessor register from memory delay
1582          (7) a coprocessor condition code delay
1583          (8) a HI/LO special register delay
1584
1585          There are a lot of optimizations we could do that we don't.
1586          In particular, we do not, in general, reorder instructions.
1587          If you use gcc with optimization, it will reorder
1588          instructions and generally do much more optimization then we
1589          do here; repeating all that work in the assembler would only
1590          benefit hand written assembly code, and does not seem worth
1591          it.  */
1592
1593       /* This is how a NOP is emitted.  */
1594 #define emit_nop()                                      \
1595   (mips_opts.mips16                                     \
1596    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1597    : md_number_to_chars (frag_more (4), 0, 4))
1598
1599       /* The previous insn might require a delay slot, depending upon
1600          the contents of the current insn.  */
1601       if (! mips_opts.mips16
1602           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1603           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1604                && ! cop_interlocks)
1605               || (! gpr_interlocks
1606                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1607         {
1608           /* A load from a coprocessor or from memory.  All load
1609              delays delay the use of general register rt for one
1610              instruction on the r3000.  The r6000 and r4000 use
1611              interlocks.  */
1612           /* Itbl support may require additional care here.  */
1613           know (prev_pinfo & INSN_WRITE_GPR_T);
1614           if (mips_optimize == 0
1615               || insn_uses_reg (ip,
1616                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1617                                  & OP_MASK_RT),
1618                                 MIPS_GR_REG))
1619             ++nops;
1620         }
1621       else if (! mips_opts.mips16
1622                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1623                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1624                     && ! cop_interlocks)
1625                    || (mips_opts.isa == ISA_MIPS1
1626                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1627         {
1628           /* A generic coprocessor delay.  The previous instruction
1629              modified a coprocessor general or control register.  If
1630              it modified a control register, we need to avoid any
1631              coprocessor instruction (this is probably not always
1632              required, but it sometimes is).  If it modified a general
1633              register, we avoid using that register.
1634
1635              On the r6000 and r4000 loading a coprocessor register
1636              from memory is interlocked, and does not require a delay.
1637
1638              This case is not handled very well.  There is no special
1639              knowledge of CP0 handling, and the coprocessors other
1640              than the floating point unit are not distinguished at
1641              all.  */
1642           /* Itbl support may require additional care here. FIXME!
1643              Need to modify this to include knowledge about
1644              user specified delays!  */
1645           if (prev_pinfo & INSN_WRITE_FPR_T)
1646             {
1647               if (mips_optimize == 0
1648                   || insn_uses_reg (ip,
1649                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1650                                      & OP_MASK_FT),
1651                                     MIPS_FP_REG))
1652                 ++nops;
1653             }
1654           else if (prev_pinfo & INSN_WRITE_FPR_S)
1655             {
1656               if (mips_optimize == 0
1657                   || insn_uses_reg (ip,
1658                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1659                                      & OP_MASK_FS),
1660                                     MIPS_FP_REG))
1661                 ++nops;
1662             }
1663           else
1664             {
1665               /* We don't know exactly what the previous instruction
1666                  does.  If the current instruction uses a coprocessor
1667                  register, we must insert a NOP.  If previous
1668                  instruction may set the condition codes, and the
1669                  current instruction uses them, we must insert two
1670                  NOPS.  */
1671               /* Itbl support may require additional care here.  */
1672               if (mips_optimize == 0
1673                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1674                       && (pinfo & INSN_READ_COND_CODE)))
1675                 nops += 2;
1676               else if (pinfo & INSN_COP)
1677                 ++nops;
1678             }
1679         }
1680       else if (! mips_opts.mips16
1681                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1682                && (prev_pinfo & INSN_WRITE_COND_CODE)
1683                && ! cop_interlocks)
1684         {
1685           /* The previous instruction sets the coprocessor condition
1686              codes, but does not require a general coprocessor delay
1687              (this means it is a floating point comparison
1688              instruction).  If this instruction uses the condition
1689              codes, we need to insert a single NOP.  */
1690           /* Itbl support may require additional care here.  */
1691           if (mips_optimize == 0
1692               || (pinfo & INSN_READ_COND_CODE))
1693             ++nops;
1694         }
1695
1696       /* If we're fixing up mfhi/mflo for the r7000 and the
1697          previous insn was an mfhi/mflo and the current insn
1698          reads the register that the mfhi/mflo wrote to, then
1699          insert two nops.  */
1700
1701       else if (mips_7000_hilo_fix
1702                && MF_HILO_INSN (prev_pinfo)
1703                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1704                                       & OP_MASK_RD),
1705                                  MIPS_GR_REG))
1706         {
1707           nops += 2;
1708         }
1709
1710       /* If we're fixing up mfhi/mflo for the r7000 and the
1711          2nd previous insn was an mfhi/mflo and the current insn
1712          reads the register that the mfhi/mflo wrote to, then
1713          insert one nop.  */
1714
1715       else if (mips_7000_hilo_fix
1716                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1717                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1718                                        & OP_MASK_RD),
1719                                     MIPS_GR_REG))
1720
1721         {
1722           ++nops;
1723         }
1724
1725       else if (prev_pinfo & INSN_READ_LO)
1726         {
1727           /* The previous instruction reads the LO register; if the
1728              current instruction writes to the LO register, we must
1729              insert two NOPS.  Some newer processors have interlocks.
1730              Also the tx39's multiply instructions can be exectuted
1731              immediatly after a read from HI/LO (without the delay),
1732              though the tx39's divide insns still do require the
1733              delay.  */
1734           if (! (hilo_interlocks
1735                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1736               && (mips_optimize == 0
1737                   || (pinfo & INSN_WRITE_LO)))
1738             nops += 2;
1739           /* Most mips16 branch insns don't have a delay slot.
1740              If a read from LO is immediately followed by a branch
1741              to a write to LO we have a read followed by a write
1742              less than 2 insns away.  We assume the target of
1743              a branch might be a write to LO, and insert a nop
1744              between a read and an immediately following branch.  */
1745           else if (mips_opts.mips16
1746                    && (mips_optimize == 0
1747                        || (pinfo & MIPS16_INSN_BRANCH)))
1748             ++nops;
1749         }
1750       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1751         {
1752           /* The previous instruction reads the HI register; if the
1753              current instruction writes to the HI register, we must
1754              insert a NOP.  Some newer processors have interlocks.
1755              Also the note tx39's multiply above.  */
1756           if (! (hilo_interlocks
1757                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1758               && (mips_optimize == 0
1759                   || (pinfo & INSN_WRITE_HI)))
1760             nops += 2;
1761           /* Most mips16 branch insns don't have a delay slot.
1762              If a read from HI is immediately followed by a branch
1763              to a write to HI we have a read followed by a write
1764              less than 2 insns away.  We assume the target of
1765              a branch might be a write to HI, and insert a nop
1766              between a read and an immediately following branch.  */
1767           else if (mips_opts.mips16
1768                    && (mips_optimize == 0
1769                        || (pinfo & MIPS16_INSN_BRANCH)))
1770             ++nops;
1771         }
1772
1773       /* If the previous instruction was in a noreorder section, then
1774          we don't want to insert the nop after all.  */
1775       /* Itbl support may require additional care here.  */
1776       if (prev_insn_unreordered)
1777         nops = 0;
1778
1779       /* There are two cases which require two intervening
1780          instructions: 1) setting the condition codes using a move to
1781          coprocessor instruction which requires a general coprocessor
1782          delay and then reading the condition codes 2) reading the HI
1783          or LO register and then writing to it (except on processors
1784          which have interlocks).  If we are not already emitting a NOP
1785          instruction, we must check for these cases compared to the
1786          instruction previous to the previous instruction.  */
1787       if ((! mips_opts.mips16
1788            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1789            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1790            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1791            && (pinfo & INSN_READ_COND_CODE)
1792            && ! cop_interlocks)
1793           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1794               && (pinfo & INSN_WRITE_LO)
1795               && ! (hilo_interlocks
1796                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1797           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1798               && (pinfo & INSN_WRITE_HI)
1799               && ! (hilo_interlocks
1800                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1801         prev_prev_nop = 1;
1802       else
1803         prev_prev_nop = 0;
1804
1805       if (prev_prev_insn_unreordered)
1806         prev_prev_nop = 0;
1807
1808       if (prev_prev_nop && nops == 0)
1809         ++nops;
1810
1811       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1812         {
1813           /* We're out of bits in pinfo, so we must resort to string
1814              ops here.  Shortcuts are selected based on opcodes being
1815              limited to the VR4122 instruction set.  */
1816           int min_nops = 0;
1817           const char *pn = prev_insn.insn_mo->name;
1818           const char *tn = ip->insn_mo->name;
1819           if (strncmp(pn, "macc", 4) == 0
1820               || strncmp(pn, "dmacc", 5) == 0)
1821             {
1822               /* Errata 21 - [D]DIV[U] after [D]MACC */
1823               if (strstr (tn, "div"))
1824                 {
1825                   min_nops = 1;
1826                 }
1827
1828               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1829               if (pn[0] == 'd' /* dmacc */
1830                   && (strncmp(tn, "dmult", 5) == 0
1831                       || strncmp(tn, "dmacc", 5) == 0))
1832                 {
1833                   min_nops = 1;
1834                 }
1835
1836               /* Errata 24 - MT{LO,HI} after [D]MACC */
1837               if (strcmp (tn, "mtlo") == 0
1838                   || strcmp (tn, "mthi") == 0)
1839                 {
1840                   min_nops = 1;
1841                 }
1842
1843             }
1844           else if (strncmp(pn, "dmult", 5) == 0
1845                    && (strncmp(tn, "dmult", 5) == 0
1846                        || strncmp(tn, "dmacc", 5) == 0))
1847             {
1848               /* Here is the rest of errata 23.  */
1849               min_nops = 1;
1850             }
1851           if (nops < min_nops)
1852             nops = min_nops;
1853         }
1854
1855       /* If we are being given a nop instruction, don't bother with
1856          one of the nops we would otherwise output.  This will only
1857          happen when a nop instruction is used with mips_optimize set
1858          to 0.  */
1859       if (nops > 0
1860           && ! mips_opts.noreorder
1861           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1862         --nops;
1863
1864       /* Now emit the right number of NOP instructions.  */
1865       if (nops > 0 && ! mips_opts.noreorder)
1866         {
1867           fragS *old_frag;
1868           unsigned long old_frag_offset;
1869           int i;
1870           struct insn_label_list *l;
1871
1872           old_frag = frag_now;
1873           old_frag_offset = frag_now_fix ();
1874
1875           for (i = 0; i < nops; i++)
1876             emit_nop ();
1877
1878           if (listing)
1879             {
1880               listing_prev_line ();
1881               /* We may be at the start of a variant frag.  In case we
1882                  are, make sure there is enough space for the frag
1883                  after the frags created by listing_prev_line.  The
1884                  argument to frag_grow here must be at least as large
1885                  as the argument to all other calls to frag_grow in
1886                  this file.  We don't have to worry about being in the
1887                  middle of a variant frag, because the variants insert
1888                  all needed nop instructions themselves.  */
1889               frag_grow (40);
1890             }
1891
1892           for (l = insn_labels; l != NULL; l = l->next)
1893             {
1894               valueT val;
1895
1896               assert (S_GET_SEGMENT (l->label) == now_seg);
1897               symbol_set_frag (l->label, frag_now);
1898               val = (valueT) frag_now_fix ();
1899               /* mips16 text labels are stored as odd.  */
1900               if (mips_opts.mips16)
1901                 ++val;
1902               S_SET_VALUE (l->label, val);
1903             }
1904
1905 #ifndef NO_ECOFF_DEBUGGING
1906           if (ECOFF_DEBUGGING)
1907             ecoff_fix_loc (old_frag, old_frag_offset);
1908 #endif
1909         }
1910       else if (prev_nop_frag != NULL)
1911         {
1912           /* We have a frag holding nops we may be able to remove.  If
1913              we don't need any nops, we can decrease the size of
1914              prev_nop_frag by the size of one instruction.  If we do
1915              need some nops, we count them in prev_nops_required.  */
1916           if (prev_nop_frag_since == 0)
1917             {
1918               if (nops == 0)
1919                 {
1920                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1921                   --prev_nop_frag_holds;
1922                 }
1923               else
1924                 prev_nop_frag_required += nops;
1925             }
1926           else
1927             {
1928               if (prev_prev_nop == 0)
1929                 {
1930                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1931                   --prev_nop_frag_holds;
1932                 }
1933               else
1934                 ++prev_nop_frag_required;
1935             }
1936
1937           if (prev_nop_frag_holds <= prev_nop_frag_required)
1938             prev_nop_frag = NULL;
1939
1940           ++prev_nop_frag_since;
1941
1942           /* Sanity check: by the time we reach the second instruction
1943              after prev_nop_frag, we should have used up all the nops
1944              one way or another.  */
1945           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1946         }
1947     }
1948
1949   if (place == NULL
1950       && address_expr
1951       && ((*reloc_type == BFD_RELOC_16_PCREL
1952            && address_expr->X_op != O_constant)
1953           || *reloc_type == BFD_RELOC_16_PCREL_S2)
1954       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1955           || pinfo & INSN_COND_BRANCH_LIKELY)
1956       && mips_relax_branch
1957       /* Don't try branch relaxation within .set nomacro, or within
1958          .set noat if we use $at for PIC computations.  If it turns
1959          out that the branch was out-of-range, we'll get an error.  */
1960       && !mips_opts.warn_about_macros
1961       && !(mips_opts.noat && mips_pic != NO_PIC)
1962       && !mips_opts.mips16)
1963     {
1964       f = frag_var (rs_machine_dependent,
1965                     relaxed_branch_length
1966                     (NULL, NULL,
1967                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1968                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1969                     RELAX_BRANCH_ENCODE
1970                     (*reloc_type == BFD_RELOC_16_PCREL_S2,
1971                      pinfo & INSN_UNCOND_BRANCH_DELAY,
1972                      pinfo & INSN_COND_BRANCH_LIKELY,
1973                      pinfo & INSN_WRITE_GPR_31,
1974                      0),
1975                     address_expr->X_add_symbol,
1976                     address_expr->X_add_number,
1977                     0);
1978       *reloc_type = BFD_RELOC_UNUSED;
1979     }
1980   else if (*reloc_type > BFD_RELOC_UNUSED)
1981     {
1982       /* We need to set up a variant frag.  */
1983       assert (mips_opts.mips16 && address_expr != NULL);
1984       f = frag_var (rs_machine_dependent, 4, 0,
1985                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1986                                          mips16_small, mips16_ext,
1987                                          (prev_pinfo
1988                                           & INSN_UNCOND_BRANCH_DELAY),
1989                                          (*prev_insn_reloc_type
1990                                           == BFD_RELOC_MIPS16_JMP)),
1991                     make_expr_symbol (address_expr), 0, NULL);
1992     }
1993   else if (place != NULL)
1994     f = place;
1995   else if (mips_opts.mips16
1996            && ! ip->use_extend
1997            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1998     {
1999       /* Make sure there is enough room to swap this instruction with
2000          a following jump instruction.  */
2001       frag_grow (6);
2002       f = frag_more (2);
2003     }
2004   else
2005     {
2006       if (mips_opts.mips16
2007           && mips_opts.noreorder
2008           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2009         as_warn (_("extended instruction in delay slot"));
2010
2011       f = frag_more (4);
2012     }
2013
2014   fixp[0] = fixp[1] = fixp[2] = NULL;
2015   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2016     {
2017       if (address_expr->X_op == O_constant)
2018         {
2019           valueT tmp;
2020
2021           switch (*reloc_type)
2022             {
2023             case BFD_RELOC_32:
2024               ip->insn_opcode |= address_expr->X_add_number;
2025               break;
2026
2027             case BFD_RELOC_MIPS_HIGHEST:
2028               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2029               tmp >>= 16;
2030               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2031               break;
2032
2033             case BFD_RELOC_MIPS_HIGHER:
2034               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2035               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2036               break;
2037
2038             case BFD_RELOC_HI16_S:
2039               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2040                                   >> 16) & 0xffff;
2041               break;
2042
2043             case BFD_RELOC_HI16:
2044               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2045               break;
2046
2047             case BFD_RELOC_LO16:
2048             case BFD_RELOC_MIPS_GOT_DISP:
2049               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2050               break;
2051
2052             case BFD_RELOC_MIPS_JMP:
2053               if ((address_expr->X_add_number & 3) != 0)
2054                 as_bad (_("jump to misaligned address (0x%lx)"),
2055                         (unsigned long) address_expr->X_add_number);
2056               if (address_expr->X_add_number & ~0xfffffff)
2057                 as_bad (_("jump address range overflow (0x%lx)"),
2058                         (unsigned long) address_expr->X_add_number);
2059               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2060               break;
2061
2062             case BFD_RELOC_MIPS16_JMP:
2063               if ((address_expr->X_add_number & 3) != 0)
2064                 as_bad (_("jump to misaligned address (0x%lx)"),
2065                         (unsigned long) address_expr->X_add_number);
2066               if (address_expr->X_add_number & ~0xfffffff)
2067                 as_bad (_("jump address range overflow (0x%lx)"),
2068                         (unsigned long) address_expr->X_add_number);
2069               ip->insn_opcode |=
2070                 (((address_expr->X_add_number & 0x7c0000) << 3)
2071                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2072                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2073               break;
2074
2075             case BFD_RELOC_16_PCREL:
2076               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2077               break;
2078
2079             case BFD_RELOC_16_PCREL_S2:
2080               goto need_reloc;
2081
2082             default:
2083               internalError ();
2084             }
2085         }
2086       else
2087         {
2088         need_reloc:
2089           /* Don't generate a reloc if we are writing into a variant frag.  */
2090           if (place == NULL)
2091             {
2092               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2093                                      address_expr,
2094                                      (*reloc_type == BFD_RELOC_16_PCREL
2095                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
2096                                      reloc_type[0]);
2097
2098               /* These relocations can have an addend that won't fit in
2099                  4 octets for 64bit assembly.  */
2100               if (HAVE_64BIT_GPRS &&
2101                   (*reloc_type == BFD_RELOC_16
2102                    || *reloc_type == BFD_RELOC_32
2103                    || *reloc_type == BFD_RELOC_MIPS_JMP
2104                    || *reloc_type == BFD_RELOC_HI16_S
2105                    || *reloc_type == BFD_RELOC_LO16
2106                    || *reloc_type == BFD_RELOC_GPREL16
2107                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2108                    || *reloc_type == BFD_RELOC_GPREL32
2109                    || *reloc_type == BFD_RELOC_64
2110                    || *reloc_type == BFD_RELOC_CTOR
2111                    || *reloc_type == BFD_RELOC_MIPS_SUB
2112                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2113                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2114                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2115                    || *reloc_type == BFD_RELOC_MIPS_REL16
2116                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2117                 fixp[0]->fx_no_overflow = 1;
2118
2119               if (unmatched_hi)
2120                 {
2121                   struct mips_hi_fixup *hi_fixup;
2122
2123                   assert (*reloc_type == BFD_RELOC_HI16_S);
2124                   hi_fixup = ((struct mips_hi_fixup *)
2125                               xmalloc (sizeof (struct mips_hi_fixup)));
2126                   hi_fixup->fixp = fixp[0];
2127                   hi_fixup->seg = now_seg;
2128                   hi_fixup->next = mips_hi_fixup_list;
2129                   mips_hi_fixup_list = hi_fixup;
2130                 }
2131
2132               if (reloc_type[1] != BFD_RELOC_UNUSED)
2133                 {
2134                   /* FIXME: This symbol can be one of
2135                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2136                   address_expr->X_op = O_absent;
2137                   address_expr->X_add_symbol = 0;
2138                   address_expr->X_add_number = 0;
2139
2140                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2141                                          4, address_expr, false,
2142                                          reloc_type[1]);
2143
2144                   /* These relocations can have an addend that won't fit in
2145                      4 octets for 64bit assembly.  */
2146                   if (HAVE_64BIT_GPRS &&
2147                       (*reloc_type == BFD_RELOC_16
2148                        || *reloc_type == BFD_RELOC_32
2149                        || *reloc_type == BFD_RELOC_MIPS_JMP
2150                        || *reloc_type == BFD_RELOC_HI16_S
2151                        || *reloc_type == BFD_RELOC_LO16
2152                        || *reloc_type == BFD_RELOC_GPREL16
2153                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2154                        || *reloc_type == BFD_RELOC_GPREL32
2155                        || *reloc_type == BFD_RELOC_64
2156                        || *reloc_type == BFD_RELOC_CTOR
2157                        || *reloc_type == BFD_RELOC_MIPS_SUB
2158                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2159                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2160                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2161                        || *reloc_type == BFD_RELOC_MIPS_REL16
2162                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2163                     fixp[1]->fx_no_overflow = 1;
2164
2165                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2166                     {
2167                       address_expr->X_op = O_absent;
2168                       address_expr->X_add_symbol = 0;
2169                       address_expr->X_add_number = 0;
2170
2171                       fixp[2] = fix_new_exp (frag_now,
2172                                              f - frag_now->fr_literal, 4,
2173                                              address_expr, false,
2174                                              reloc_type[2]);
2175
2176                       /* These relocations can have an addend that won't fit in
2177                          4 octets for 64bit assembly.  */
2178                       if (HAVE_64BIT_GPRS &&
2179                           (*reloc_type == BFD_RELOC_16
2180                            || *reloc_type == BFD_RELOC_32
2181                            || *reloc_type == BFD_RELOC_MIPS_JMP
2182                            || *reloc_type == BFD_RELOC_HI16_S
2183                            || *reloc_type == BFD_RELOC_LO16
2184                            || *reloc_type == BFD_RELOC_GPREL16
2185                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2186                            || *reloc_type == BFD_RELOC_GPREL32
2187                            || *reloc_type == BFD_RELOC_64
2188                            || *reloc_type == BFD_RELOC_CTOR
2189                            || *reloc_type == BFD_RELOC_MIPS_SUB
2190                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2191                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2192                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2193                            || *reloc_type == BFD_RELOC_MIPS_REL16
2194                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2195                         fixp[2]->fx_no_overflow = 1;
2196                     }
2197                 }
2198             }
2199         }
2200     }
2201
2202   if (! mips_opts.mips16)
2203     {
2204       md_number_to_chars (f, ip->insn_opcode, 4);
2205 #ifdef OBJ_ELF
2206       dwarf2_emit_insn (4);
2207 #endif
2208     }
2209   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2210     {
2211       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2212       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2213 #ifdef OBJ_ELF
2214       dwarf2_emit_insn (4);
2215 #endif
2216     }
2217   else
2218     {
2219       if (ip->use_extend)
2220         {
2221           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2222           f += 2;
2223         }
2224       md_number_to_chars (f, ip->insn_opcode, 2);
2225 #ifdef OBJ_ELF
2226       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2227 #endif
2228     }
2229
2230   /* Update the register mask information.  */
2231   if (! mips_opts.mips16)
2232     {
2233       if (pinfo & INSN_WRITE_GPR_D)
2234         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2235       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2236         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2237       if (pinfo & INSN_READ_GPR_S)
2238         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2239       if (pinfo & INSN_WRITE_GPR_31)
2240         mips_gprmask |= 1 << RA;
2241       if (pinfo & INSN_WRITE_FPR_D)
2242         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2243       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2244         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2245       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2246         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2247       if ((pinfo & INSN_READ_FPR_R) != 0)
2248         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2249       if (pinfo & INSN_COP)
2250         {
2251           /* We don't keep enough information to sort these cases out.
2252              The itbl support does keep this information however, although
2253              we currently don't support itbl fprmats as part of the cop
2254              instruction.  May want to add this support in the future.  */
2255         }
2256       /* Never set the bit for $0, which is always zero.  */
2257       mips_gprmask &= ~1 << 0;
2258     }
2259   else
2260     {
2261       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2262         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2263                               & MIPS16OP_MASK_RX);
2264       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2265         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2266                               & MIPS16OP_MASK_RY);
2267       if (pinfo & MIPS16_INSN_WRITE_Z)
2268         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2269                               & MIPS16OP_MASK_RZ);
2270       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2271         mips_gprmask |= 1 << TREG;
2272       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2273         mips_gprmask |= 1 << SP;
2274       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2275         mips_gprmask |= 1 << RA;
2276       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2277         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2278       if (pinfo & MIPS16_INSN_READ_Z)
2279         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2280                               & MIPS16OP_MASK_MOVE32Z);
2281       if (pinfo & MIPS16_INSN_READ_GPR_X)
2282         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2283                               & MIPS16OP_MASK_REGR32);
2284     }
2285
2286   if (place == NULL && ! mips_opts.noreorder)
2287     {
2288       /* Filling the branch delay slot is more complex.  We try to
2289          switch the branch with the previous instruction, which we can
2290          do if the previous instruction does not set up a condition
2291          that the branch tests and if the branch is not itself the
2292          target of any branch.  */
2293       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2294           || (pinfo & INSN_COND_BRANCH_DELAY))
2295         {
2296           if (mips_optimize < 2
2297               /* If we have seen .set volatile or .set nomove, don't
2298                  optimize.  */
2299               || mips_opts.nomove != 0
2300               /* If we had to emit any NOP instructions, then we
2301                  already know we can not swap.  */
2302               || nops != 0
2303               /* If we don't even know the previous insn, we can not
2304                  swap.  */
2305               || ! prev_insn_valid
2306               /* If the previous insn is already in a branch delay
2307                  slot, then we can not swap.  */
2308               || prev_insn_is_delay_slot
2309               /* If the previous previous insn was in a .set
2310                  noreorder, we can't swap.  Actually, the MIPS
2311                  assembler will swap in this situation.  However, gcc
2312                  configured -with-gnu-as will generate code like
2313                    .set noreorder
2314                    lw   $4,XXX
2315                    .set reorder
2316                    INSN
2317                    bne  $4,$0,foo
2318                  in which we can not swap the bne and INSN.  If gcc is
2319                  not configured -with-gnu-as, it does not output the
2320                  .set pseudo-ops.  We don't have to check
2321                  prev_insn_unreordered, because prev_insn_valid will
2322                  be 0 in that case.  We don't want to use
2323                  prev_prev_insn_valid, because we do want to be able
2324                  to swap at the start of a function.  */
2325               || prev_prev_insn_unreordered
2326               /* If the branch is itself the target of a branch, we
2327                  can not swap.  We cheat on this; all we check for is
2328                  whether there is a label on this instruction.  If
2329                  there are any branches to anything other than a
2330                  label, users must use .set noreorder.  */
2331               || insn_labels != NULL
2332               /* If the previous instruction is in a variant frag, we
2333                  can not do the swap.  This does not apply to the
2334                  mips16, which uses variant frags for different
2335                  purposes.  */
2336               || (! mips_opts.mips16
2337                   && prev_insn_frag->fr_type == rs_machine_dependent)
2338               /* If the branch reads the condition codes, we don't
2339                  even try to swap, because in the sequence
2340                    ctc1 $X,$31
2341                    INSN
2342                    INSN
2343                    bc1t LABEL
2344                  we can not swap, and I don't feel like handling that
2345                  case.  */
2346               || (! mips_opts.mips16
2347                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2348                   && (pinfo & INSN_READ_COND_CODE))
2349               /* We can not swap with an instruction that requires a
2350                  delay slot, becase the target of the branch might
2351                  interfere with that instruction.  */
2352               || (! mips_opts.mips16
2353                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2354                   && (prev_pinfo
2355               /* Itbl support may require additional care here.  */
2356                       & (INSN_LOAD_COPROC_DELAY
2357                          | INSN_COPROC_MOVE_DELAY
2358                          | INSN_WRITE_COND_CODE)))
2359               || (! (hilo_interlocks
2360                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2361                   && (prev_pinfo
2362                       & (INSN_READ_LO
2363                          | INSN_READ_HI)))
2364               || (! mips_opts.mips16
2365                   && ! gpr_interlocks
2366                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2367               || (! mips_opts.mips16
2368                   && mips_opts.isa == ISA_MIPS1
2369                   /* Itbl support may require additional care here.  */
2370                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2371               /* We can not swap with a branch instruction.  */
2372               || (prev_pinfo
2373                   & (INSN_UNCOND_BRANCH_DELAY
2374                      | INSN_COND_BRANCH_DELAY
2375                      | INSN_COND_BRANCH_LIKELY))
2376               /* We do not swap with a trap instruction, since it
2377                  complicates trap handlers to have the trap
2378                  instruction be in a delay slot.  */
2379               || (prev_pinfo & INSN_TRAP)
2380               /* If the branch reads a register that the previous
2381                  instruction sets, we can not swap.  */
2382               || (! mips_opts.mips16
2383                   && (prev_pinfo & INSN_WRITE_GPR_T)
2384                   && insn_uses_reg (ip,
2385                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2386                                      & OP_MASK_RT),
2387                                     MIPS_GR_REG))
2388               || (! mips_opts.mips16
2389                   && (prev_pinfo & INSN_WRITE_GPR_D)
2390                   && insn_uses_reg (ip,
2391                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2392                                      & OP_MASK_RD),
2393                                     MIPS_GR_REG))
2394               || (mips_opts.mips16
2395                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2396                        && insn_uses_reg (ip,
2397                                          ((prev_insn.insn_opcode
2398                                            >> MIPS16OP_SH_RX)
2399                                           & MIPS16OP_MASK_RX),
2400                                          MIPS16_REG))
2401                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2402                           && insn_uses_reg (ip,
2403                                             ((prev_insn.insn_opcode
2404                                               >> MIPS16OP_SH_RY)
2405                                              & MIPS16OP_MASK_RY),
2406                                             MIPS16_REG))
2407                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2408                           && insn_uses_reg (ip,
2409                                             ((prev_insn.insn_opcode
2410                                               >> MIPS16OP_SH_RZ)
2411                                              & MIPS16OP_MASK_RZ),
2412                                             MIPS16_REG))
2413                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2414                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2415                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2416                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2417                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2418                           && insn_uses_reg (ip,
2419                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2420                                                                      insn_opcode),
2421                                             MIPS_GR_REG))))
2422               /* If the branch writes a register that the previous
2423                  instruction sets, we can not swap (we know that
2424                  branches write only to RD or to $31).  */
2425               || (! mips_opts.mips16
2426                   && (prev_pinfo & INSN_WRITE_GPR_T)
2427                   && (((pinfo & INSN_WRITE_GPR_D)
2428                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2429                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2430                       || ((pinfo & INSN_WRITE_GPR_31)
2431                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2432                                & OP_MASK_RT)
2433                               == RA))))
2434               || (! mips_opts.mips16
2435                   && (prev_pinfo & INSN_WRITE_GPR_D)
2436                   && (((pinfo & INSN_WRITE_GPR_D)
2437                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2438                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2439                       || ((pinfo & INSN_WRITE_GPR_31)
2440                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2441                                & OP_MASK_RD)
2442                               == RA))))
2443               || (mips_opts.mips16
2444                   && (pinfo & MIPS16_INSN_WRITE_31)
2445                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2446                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2447                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2448                               == RA))))
2449               /* If the branch writes a register that the previous
2450                  instruction reads, we can not swap (we know that
2451                  branches only write to RD or to $31).  */
2452               || (! mips_opts.mips16
2453                   && (pinfo & INSN_WRITE_GPR_D)
2454                   && insn_uses_reg (&prev_insn,
2455                                     ((ip->insn_opcode >> OP_SH_RD)
2456                                      & OP_MASK_RD),
2457                                     MIPS_GR_REG))
2458               || (! mips_opts.mips16
2459                   && (pinfo & INSN_WRITE_GPR_31)
2460                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2461               || (mips_opts.mips16
2462                   && (pinfo & MIPS16_INSN_WRITE_31)
2463                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2464               /* If we are generating embedded PIC code, the branch
2465                  might be expanded into a sequence which uses $at, so
2466                  we can't swap with an instruction which reads it.  */
2467               || (mips_pic == EMBEDDED_PIC
2468                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2469               /* If the previous previous instruction has a load
2470                  delay, and sets a register that the branch reads, we
2471                  can not swap.  */
2472               || (! mips_opts.mips16
2473                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2474               /* Itbl support may require additional care here.  */
2475                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2476                       || (! gpr_interlocks
2477                           && (prev_prev_insn.insn_mo->pinfo
2478                               & INSN_LOAD_MEMORY_DELAY)))
2479                   && insn_uses_reg (ip,
2480                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2481                                      & OP_MASK_RT),
2482                                     MIPS_GR_REG))
2483               /* If one instruction sets a condition code and the
2484                  other one uses a condition code, we can not swap.  */
2485               || ((pinfo & INSN_READ_COND_CODE)
2486                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2487               || ((pinfo & INSN_WRITE_COND_CODE)
2488                   && (prev_pinfo & INSN_READ_COND_CODE))
2489               /* If the previous instruction uses the PC, we can not
2490                  swap.  */
2491               || (mips_opts.mips16
2492                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2493               /* If the previous instruction was extended, we can not
2494                  swap.  */
2495               || (mips_opts.mips16 && prev_insn_extended)
2496               /* If the previous instruction had a fixup in mips16
2497                  mode, we can not swap.  This normally means that the
2498                  previous instruction was a 4 byte branch anyhow.  */
2499               || (mips_opts.mips16 && prev_insn_fixp[0])
2500               /* If the previous instruction is a sync, sync.l, or
2501                  sync.p, we can not swap.  */
2502               || (prev_pinfo & INSN_SYNC))
2503             {
2504               /* We could do even better for unconditional branches to
2505                  portions of this object file; we could pick up the
2506                  instruction at the destination, put it in the delay
2507                  slot, and bump the destination address.  */
2508               emit_nop ();
2509               /* Update the previous insn information.  */
2510               prev_prev_insn = *ip;
2511               prev_insn.insn_mo = &dummy_opcode;
2512             }
2513           else
2514             {
2515               /* It looks like we can actually do the swap.  */
2516               if (! mips_opts.mips16)
2517                 {
2518                   char *prev_f;
2519                   char temp[4];
2520
2521                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2522                   memcpy (temp, prev_f, 4);
2523                   memcpy (prev_f, f, 4);
2524                   memcpy (f, temp, 4);
2525                   if (prev_insn_fixp[0])
2526                     {
2527                       prev_insn_fixp[0]->fx_frag = frag_now;
2528                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2529                     }
2530                   if (prev_insn_fixp[1])
2531                     {
2532                       prev_insn_fixp[1]->fx_frag = frag_now;
2533                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2534                     }
2535                   if (prev_insn_fixp[2])
2536                     {
2537                       prev_insn_fixp[2]->fx_frag = frag_now;
2538                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2539                     }
2540                   if (fixp[0])
2541                     {
2542                       fixp[0]->fx_frag = prev_insn_frag;
2543                       fixp[0]->fx_where = prev_insn_where;
2544                     }
2545                   if (fixp[1])
2546                     {
2547                       fixp[1]->fx_frag = prev_insn_frag;
2548                       fixp[1]->fx_where = prev_insn_where;
2549                     }
2550                   if (fixp[2])
2551                     {
2552                       fixp[2]->fx_frag = prev_insn_frag;
2553                       fixp[2]->fx_where = prev_insn_where;
2554                     }
2555                 }
2556               else
2557                 {
2558                   char *prev_f;
2559                   char temp[2];
2560
2561                   assert (prev_insn_fixp[0] == NULL);
2562                   assert (prev_insn_fixp[1] == NULL);
2563                   assert (prev_insn_fixp[2] == NULL);
2564                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2565                   memcpy (temp, prev_f, 2);
2566                   memcpy (prev_f, f, 2);
2567                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2568                     {
2569                       assert (*reloc_type == BFD_RELOC_UNUSED);
2570                       memcpy (f, temp, 2);
2571                     }
2572                   else
2573                     {
2574                       memcpy (f, f + 2, 2);
2575                       memcpy (f + 2, temp, 2);
2576                     }
2577                   if (fixp[0])
2578                     {
2579                       fixp[0]->fx_frag = prev_insn_frag;
2580                       fixp[0]->fx_where = prev_insn_where;
2581                     }
2582                   if (fixp[1])
2583                     {
2584                       fixp[1]->fx_frag = prev_insn_frag;
2585                       fixp[1]->fx_where = prev_insn_where;
2586                     }
2587                   if (fixp[2])
2588                     {
2589                       fixp[2]->fx_frag = prev_insn_frag;
2590                       fixp[2]->fx_where = prev_insn_where;
2591                     }
2592                 }
2593
2594               /* Update the previous insn information; leave prev_insn
2595                  unchanged.  */
2596               prev_prev_insn = *ip;
2597             }
2598           prev_insn_is_delay_slot = 1;
2599
2600           /* If that was an unconditional branch, forget the previous
2601              insn information.  */
2602           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2603             {
2604               prev_prev_insn.insn_mo = &dummy_opcode;
2605               prev_insn.insn_mo = &dummy_opcode;
2606             }
2607
2608           prev_insn_fixp[0] = NULL;
2609           prev_insn_fixp[1] = NULL;
2610           prev_insn_fixp[2] = NULL;
2611           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2612           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2613           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2614           prev_insn_extended = 0;
2615         }
2616       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2617         {
2618           /* We don't yet optimize a branch likely.  What we should do
2619              is look at the target, copy the instruction found there
2620              into the delay slot, and increment the branch to jump to
2621              the next instruction.  */
2622           emit_nop ();
2623           /* Update the previous insn information.  */
2624           prev_prev_insn = *ip;
2625           prev_insn.insn_mo = &dummy_opcode;
2626           prev_insn_fixp[0] = NULL;
2627           prev_insn_fixp[1] = NULL;
2628           prev_insn_fixp[2] = NULL;
2629           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2630           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2631           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2632           prev_insn_extended = 0;
2633         }
2634       else
2635         {
2636           /* Update the previous insn information.  */
2637           if (nops > 0)
2638             prev_prev_insn.insn_mo = &dummy_opcode;
2639           else
2640             prev_prev_insn = prev_insn;
2641           prev_insn = *ip;
2642
2643           /* Any time we see a branch, we always fill the delay slot
2644              immediately; since this insn is not a branch, we know it
2645              is not in a delay slot.  */
2646           prev_insn_is_delay_slot = 0;
2647
2648           prev_insn_fixp[0] = fixp[0];
2649           prev_insn_fixp[1] = fixp[1];
2650           prev_insn_fixp[2] = fixp[2];
2651           prev_insn_reloc_type[0] = reloc_type[0];
2652           prev_insn_reloc_type[1] = reloc_type[1];
2653           prev_insn_reloc_type[2] = reloc_type[2];
2654           if (mips_opts.mips16)
2655             prev_insn_extended = (ip->use_extend
2656                                   || *reloc_type > BFD_RELOC_UNUSED);
2657         }
2658
2659       prev_prev_insn_unreordered = prev_insn_unreordered;
2660       prev_insn_unreordered = 0;
2661       prev_insn_frag = frag_now;
2662       prev_insn_where = f - frag_now->fr_literal;
2663       prev_insn_valid = 1;
2664     }
2665   else if (place == NULL)
2666     {
2667       /* We need to record a bit of information even when we are not
2668          reordering, in order to determine the base address for mips16
2669          PC relative relocs.  */
2670       prev_prev_insn = prev_insn;
2671       prev_insn = *ip;
2672       prev_insn_reloc_type[0] = reloc_type[0];
2673       prev_insn_reloc_type[1] = reloc_type[1];
2674       prev_insn_reloc_type[2] = reloc_type[2];
2675       prev_prev_insn_unreordered = prev_insn_unreordered;
2676       prev_insn_unreordered = 1;
2677     }
2678
2679   /* We just output an insn, so the next one doesn't have a label.  */
2680   mips_clear_insn_labels ();
2681
2682   /* We must ensure that a fixup associated with an unmatched %hi
2683      reloc does not become a variant frag.  Otherwise, the
2684      rearrangement of %hi relocs in frob_file may confuse
2685      tc_gen_reloc.  */
2686   if (unmatched_hi)
2687     {
2688       frag_wane (frag_now);
2689       frag_new (0);
2690     }
2691 }
2692
2693 /* This function forgets that there was any previous instruction or
2694    label.  If PRESERVE is non-zero, it remembers enough information to
2695    know whether nops are needed before a noreorder section.  */
2696
2697 static void
2698 mips_no_prev_insn (preserve)
2699      int preserve;
2700 {
2701   if (! preserve)
2702     {
2703       prev_insn.insn_mo = &dummy_opcode;
2704       prev_prev_insn.insn_mo = &dummy_opcode;
2705       prev_nop_frag = NULL;
2706       prev_nop_frag_holds = 0;
2707       prev_nop_frag_required = 0;
2708       prev_nop_frag_since = 0;
2709     }
2710   prev_insn_valid = 0;
2711   prev_insn_is_delay_slot = 0;
2712   prev_insn_unreordered = 0;
2713   prev_insn_extended = 0;
2714   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2715   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2716   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2717   prev_prev_insn_unreordered = 0;
2718   mips_clear_insn_labels ();
2719 }
2720
2721 /* This function must be called whenever we turn on noreorder or emit
2722    something other than instructions.  It inserts any NOPS which might
2723    be needed by the previous instruction, and clears the information
2724    kept for the previous instructions.  The INSNS parameter is true if
2725    instructions are to follow.  */
2726
2727 static void
2728 mips_emit_delays (insns)
2729      boolean insns;
2730 {
2731   if (! mips_opts.noreorder)
2732     {
2733       int nops;
2734
2735       nops = 0;
2736       if ((! mips_opts.mips16
2737            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2738            && (! cop_interlocks
2739                && (prev_insn.insn_mo->pinfo
2740                    & (INSN_LOAD_COPROC_DELAY
2741                       | INSN_COPROC_MOVE_DELAY
2742                       | INSN_WRITE_COND_CODE))))
2743           || (! hilo_interlocks
2744               && (prev_insn.insn_mo->pinfo
2745                   & (INSN_READ_LO
2746                      | INSN_READ_HI)))
2747           || (! mips_opts.mips16
2748               && ! gpr_interlocks
2749               && (prev_insn.insn_mo->pinfo
2750                   & INSN_LOAD_MEMORY_DELAY))
2751           || (! mips_opts.mips16
2752               && mips_opts.isa == ISA_MIPS1
2753               && (prev_insn.insn_mo->pinfo
2754                   & INSN_COPROC_MEMORY_DELAY)))
2755         {
2756           /* Itbl support may require additional care here.  */
2757           ++nops;
2758           if ((! mips_opts.mips16
2759                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2760                && (! cop_interlocks
2761                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2762               || (! hilo_interlocks
2763                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2764                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2765             ++nops;
2766
2767           if (prev_insn_unreordered)
2768             nops = 0;
2769         }
2770       else if ((! mips_opts.mips16
2771                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2772                 && (! cop_interlocks
2773                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2774                || (! hilo_interlocks
2775                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2776                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2777         {
2778           /* Itbl support may require additional care here.  */
2779           if (! prev_prev_insn_unreordered)
2780             ++nops;
2781         }
2782
2783       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2784         {
2785           int min_nops = 0;
2786           const char *pn = prev_insn.insn_mo->name;
2787           if (strncmp(pn, "macc", 4) == 0
2788               || strncmp(pn, "dmacc", 5) == 0
2789               || strncmp(pn, "dmult", 5) == 0)
2790             {
2791               min_nops = 1;
2792             }
2793           if (nops < min_nops)
2794             nops = min_nops;
2795         }
2796
2797       if (nops > 0)
2798         {
2799           struct insn_label_list *l;
2800
2801           if (insns)
2802             {
2803               /* Record the frag which holds the nop instructions, so
2804                  that we can remove them if we don't need them.  */
2805               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2806               prev_nop_frag = frag_now;
2807               prev_nop_frag_holds = nops;
2808               prev_nop_frag_required = 0;
2809               prev_nop_frag_since = 0;
2810             }
2811
2812           for (; nops > 0; --nops)
2813             emit_nop ();
2814
2815           if (insns)
2816             {
2817               /* Move on to a new frag, so that it is safe to simply
2818                  decrease the size of prev_nop_frag.  */
2819               frag_wane (frag_now);
2820               frag_new (0);
2821             }
2822
2823           for (l = insn_labels; l != NULL; l = l->next)
2824             {
2825               valueT val;
2826
2827               assert (S_GET_SEGMENT (l->label) == now_seg);
2828               symbol_set_frag (l->label, frag_now);
2829               val = (valueT) frag_now_fix ();
2830               /* mips16 text labels are stored as odd.  */
2831               if (mips_opts.mips16)
2832                 ++val;
2833               S_SET_VALUE (l->label, val);
2834             }
2835         }
2836     }
2837
2838   /* Mark instruction labels in mips16 mode.  */
2839   if (insns)
2840     mips16_mark_labels ();
2841
2842   mips_no_prev_insn (insns);
2843 }
2844
2845 /* Build an instruction created by a macro expansion.  This is passed
2846    a pointer to the count of instructions created so far, an
2847    expression, the name of the instruction to build, an operand format
2848    string, and corresponding arguments.  */
2849
2850 #ifdef USE_STDARG
2851 static void
2852 macro_build (char *place,
2853              int *counter,
2854              expressionS * ep,
2855              const char *name,
2856              const char *fmt,
2857              ...)
2858 #else
2859 static void
2860 macro_build (place, counter, ep, name, fmt, va_alist)
2861      char *place;
2862      int *counter;
2863      expressionS *ep;
2864      const char *name;
2865      const char *fmt;
2866      va_dcl
2867 #endif
2868 {
2869   struct mips_cl_insn insn;
2870   bfd_reloc_code_real_type r[3];
2871   va_list args;
2872
2873 #ifdef USE_STDARG
2874   va_start (args, fmt);
2875 #else
2876   va_start (args);
2877 #endif
2878
2879   /*
2880    * If the macro is about to expand into a second instruction,
2881    * print a warning if needed. We need to pass ip as a parameter
2882    * to generate a better warning message here...
2883    */
2884   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2885     as_warn (_("Macro instruction expanded into multiple instructions"));
2886
2887   /*
2888    * If the macro is about to expand into a second instruction,
2889    * and it is in a delay slot, print a warning.
2890    */
2891   if (place == NULL
2892       && *counter == 1
2893       && mips_opts.noreorder
2894       && (prev_prev_insn.insn_mo->pinfo
2895           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2896              | INSN_COND_BRANCH_LIKELY)) != 0)
2897     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2898
2899   if (place == NULL)
2900     ++*counter;         /* bump instruction counter */
2901
2902   if (mips_opts.mips16)
2903     {
2904       mips16_macro_build (place, counter, ep, name, fmt, args);
2905       va_end (args);
2906       return;
2907     }
2908
2909   r[0] = BFD_RELOC_UNUSED;
2910   r[1] = BFD_RELOC_UNUSED;
2911   r[2] = BFD_RELOC_UNUSED;
2912   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2913   assert (insn.insn_mo);
2914   assert (strcmp (name, insn.insn_mo->name) == 0);
2915
2916   /* Search until we get a match for NAME.  */
2917   while (1)
2918     {
2919       /* It is assumed here that macros will never generate 
2920          MDMX or MIPS-3D instructions.  */
2921       if (strcmp (fmt, insn.insn_mo->args) == 0
2922           && insn.insn_mo->pinfo != INSN_MACRO
2923           && OPCODE_IS_MEMBER (insn.insn_mo,
2924                                (mips_opts.isa
2925                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2926                                mips_arch)
2927           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2928         break;
2929
2930       ++insn.insn_mo;
2931       assert (insn.insn_mo->name);
2932       assert (strcmp (name, insn.insn_mo->name) == 0);
2933     }
2934
2935   insn.insn_opcode = insn.insn_mo->match;
2936   for (;;)
2937     {
2938       switch (*fmt++)
2939         {
2940         case '\0':
2941           break;
2942
2943         case ',':
2944         case '(':
2945         case ')':
2946           continue;
2947
2948         case 't':
2949         case 'w':
2950         case 'E':
2951           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2952           continue;
2953
2954         case 'c':
2955           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2956           continue;
2957
2958         case 'T':
2959         case 'W':
2960           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2961           continue;
2962
2963         case 'd':
2964         case 'G':
2965           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2966           continue;
2967
2968         case 'U':
2969           {
2970             int tmp = va_arg (args, int);
2971
2972             insn.insn_opcode |= tmp << OP_SH_RT;
2973             insn.insn_opcode |= tmp << OP_SH_RD;
2974             continue;
2975           }
2976
2977         case 'V':
2978         case 'S':
2979           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2980           continue;
2981
2982         case 'z':
2983           continue;
2984
2985         case '<':
2986           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2987           continue;
2988
2989         case 'D':
2990           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2991           continue;
2992
2993         case 'B':
2994           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2995           continue;
2996
2997         case 'J':
2998           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2999           continue;
3000
3001         case 'q':
3002           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3003           continue;
3004
3005         case 'b':
3006         case 's':
3007         case 'r':
3008         case 'v':
3009           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3010           continue;
3011
3012         case 'i':
3013         case 'j':
3014         case 'o':
3015           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3016           assert (*r == BFD_RELOC_GPREL16
3017                   || *r == BFD_RELOC_MIPS_LITERAL
3018                   || *r == BFD_RELOC_MIPS_HIGHER
3019                   || *r == BFD_RELOC_HI16_S
3020                   || *r == BFD_RELOC_LO16
3021                   || *r == BFD_RELOC_MIPS_GOT16
3022                   || *r == BFD_RELOC_MIPS_CALL16
3023                   || *r == BFD_RELOC_MIPS_GOT_DISP
3024                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3025                   || *r == BFD_RELOC_MIPS_GOT_OFST
3026                   || *r == BFD_RELOC_MIPS_GOT_LO16
3027                   || *r == BFD_RELOC_MIPS_CALL_LO16
3028                   || (ep->X_op == O_subtract
3029                       && *r == BFD_RELOC_PCREL_LO16));
3030           continue;
3031
3032         case 'u':
3033           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3034           assert (ep != NULL
3035                   && (ep->X_op == O_constant
3036                       || (ep->X_op == O_symbol
3037                           && (*r == BFD_RELOC_MIPS_HIGHEST
3038                               || *r == BFD_RELOC_HI16_S
3039                               || *r == BFD_RELOC_HI16
3040                               || *r == BFD_RELOC_GPREL16
3041                               || *r == BFD_RELOC_MIPS_GOT_HI16
3042                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3043                       || (ep->X_op == O_subtract
3044                           && *r == BFD_RELOC_PCREL_HI16_S)));
3045           continue;
3046
3047         case 'p':
3048           assert (ep != NULL);
3049           /*
3050            * This allows macro() to pass an immediate expression for
3051            * creating short branches without creating a symbol.
3052            * Note that the expression still might come from the assembly
3053            * input, in which case the value is not checked for range nor
3054            * is a relocation entry generated (yuck).
3055            */
3056           if (ep->X_op == O_constant)
3057             {
3058               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3059               ep = NULL;
3060             }
3061           else
3062             if (mips_pic == EMBEDDED_PIC)
3063               *r = BFD_RELOC_16_PCREL_S2;
3064             else
3065               *r = BFD_RELOC_16_PCREL;
3066           continue;
3067
3068         case 'a':
3069           assert (ep != NULL);
3070           *r = BFD_RELOC_MIPS_JMP;
3071           continue;
3072
3073         case 'C':
3074           insn.insn_opcode |= va_arg (args, unsigned long);
3075           continue;
3076
3077         default:
3078           internalError ();
3079         }
3080       break;
3081     }
3082   va_end (args);
3083   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3084
3085   append_insn (place, &insn, ep, r, false);
3086 }
3087
3088 static void
3089 mips16_macro_build (place, counter, ep, name, fmt, args)
3090      char *place;
3091      int *counter ATTRIBUTE_UNUSED;
3092      expressionS *ep;
3093      const char *name;
3094      const char *fmt;
3095      va_list args;
3096 {
3097   struct mips_cl_insn insn;
3098   bfd_reloc_code_real_type r[3]
3099     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3100
3101   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3102   assert (insn.insn_mo);
3103   assert (strcmp (name, insn.insn_mo->name) == 0);
3104
3105   while (strcmp (fmt, insn.insn_mo->args) != 0
3106          || insn.insn_mo->pinfo == INSN_MACRO)
3107     {
3108       ++insn.insn_mo;
3109       assert (insn.insn_mo->name);
3110       assert (strcmp (name, insn.insn_mo->name) == 0);
3111     }
3112
3113   insn.insn_opcode = insn.insn_mo->match;
3114   insn.use_extend = false;
3115
3116   for (;;)
3117     {
3118       int c;
3119
3120       c = *fmt++;
3121       switch (c)
3122         {
3123         case '\0':
3124           break;
3125
3126         case ',':
3127         case '(':
3128         case ')':
3129           continue;
3130
3131         case 'y':
3132         case 'w':
3133           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3134           continue;
3135
3136         case 'x':
3137         case 'v':
3138           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3139           continue;
3140
3141         case 'z':
3142           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3143           continue;
3144
3145         case 'Z':
3146           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3147           continue;
3148
3149         case '0':
3150         case 'S':
3151         case 'P':
3152         case 'R':
3153           continue;
3154
3155         case 'X':
3156           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3157           continue;
3158
3159         case 'Y':
3160           {
3161             int regno;
3162
3163             regno = va_arg (args, int);
3164             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3165             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3166           }
3167           continue;
3168
3169         case '<':
3170         case '>':
3171         case '4':
3172         case '5':
3173         case 'H':
3174         case 'W':
3175         case 'D':
3176         case 'j':
3177         case '8':
3178         case 'V':
3179         case 'C':
3180         case 'U':
3181         case 'k':
3182         case 'K':
3183         case 'p':
3184         case 'q':
3185           {
3186             assert (ep != NULL);
3187
3188             if (ep->X_op != O_constant)
3189               *r = (int) BFD_RELOC_UNUSED + c;
3190             else
3191               {
3192                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3193                               false, &insn.insn_opcode, &insn.use_extend,
3194                               &insn.extend);
3195                 ep = NULL;
3196                 *r = BFD_RELOC_UNUSED;
3197               }
3198           }
3199           continue;
3200
3201         case '6':
3202           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3203           continue;
3204         }
3205
3206       break;
3207     }
3208
3209   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3210
3211   append_insn (place, &insn, ep, r, false);
3212 }
3213
3214 /*
3215  * Generate a "jalr" instruction with a relocation hint to the called
3216  * function.  This occurs in NewABI PIC code.
3217  */
3218 static void
3219 macro_build_jalr (icnt, ep)
3220      int icnt;
3221      expressionS *ep;
3222 {
3223   char *f;
3224   
3225   if (HAVE_NEWABI)
3226     {
3227       frag_grow (4);
3228       f = frag_more (0);
3229     }
3230   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3231                RA, PIC_CALL_REG);
3232   if (HAVE_NEWABI)
3233     fix_new_exp (frag_now, f - frag_now->fr_literal,
3234                  0, ep, false, BFD_RELOC_MIPS_JALR);
3235 }
3236
3237 /*
3238  * Generate a "lui" instruction.
3239  */
3240 static void
3241 macro_build_lui (place, counter, ep, regnum)
3242      char *place;
3243      int *counter;
3244      expressionS *ep;
3245      int regnum;
3246 {
3247   expressionS high_expr;
3248   struct mips_cl_insn insn;
3249   bfd_reloc_code_real_type r[3]
3250     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3251   const char *name = "lui";
3252   const char *fmt = "t,u";
3253
3254   assert (! mips_opts.mips16);
3255
3256   if (place == NULL)
3257     high_expr = *ep;
3258   else
3259     {
3260       high_expr.X_op = O_constant;
3261       high_expr.X_add_number = ep->X_add_number;
3262     }
3263
3264   if (high_expr.X_op == O_constant)
3265     {
3266       /* we can compute the instruction now without a relocation entry */
3267       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3268                                 >> 16) & 0xffff;
3269       *r = BFD_RELOC_UNUSED;
3270     }
3271   else if (! HAVE_NEWABI)
3272     {
3273       assert (ep->X_op == O_symbol);
3274       /* _gp_disp is a special case, used from s_cpload.  */
3275       assert (mips_pic == NO_PIC
3276               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3277       *r = BFD_RELOC_HI16_S;
3278     }
3279
3280   /*
3281    * If the macro is about to expand into a second instruction,
3282    * print a warning if needed. We need to pass ip as a parameter
3283    * to generate a better warning message here...
3284    */
3285   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3286     as_warn (_("Macro instruction expanded into multiple instructions"));
3287
3288   if (place == NULL)
3289     ++*counter;         /* bump instruction counter */
3290
3291   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3292   assert (insn.insn_mo);
3293   assert (strcmp (name, insn.insn_mo->name) == 0);
3294   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3295
3296   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3297   if (*r == BFD_RELOC_UNUSED)
3298     {
3299       insn.insn_opcode |= high_expr.X_add_number;
3300       append_insn (place, &insn, NULL, r, false);
3301     }
3302   else
3303     append_insn (place, &insn, &high_expr, r, false);
3304 }
3305
3306 /* Generate a sequence of instructions to do a load or store from a constant
3307    offset off of a base register (breg) into/from a target register (treg),
3308    using AT if necessary.  */
3309 static void
3310 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3311      char *place;
3312      int *counter;
3313      expressionS *ep;
3314      const char *op;
3315      int treg, breg;
3316 {
3317   assert (ep->X_op == O_constant);
3318
3319   /* Right now, this routine can only handle signed 32-bit contants.  */
3320   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3321     as_warn (_("operand overflow"));
3322
3323   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3324     {
3325       /* Signed 16-bit offset will fit in the op.  Easy!  */
3326       macro_build (place, counter, ep, op, "t,o(b)", treg,
3327                    (int) BFD_RELOC_LO16, breg);
3328     }
3329   else
3330     {
3331       /* 32-bit offset, need multiple instructions and AT, like:
3332            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3333            addu     $tempreg,$tempreg,$breg
3334            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3335          to handle the complete offset.  */
3336       macro_build_lui (place, counter, ep, AT);
3337       if (place != NULL)
3338         place += 4;
3339       macro_build (place, counter, (expressionS *) NULL,
3340                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3341                    "d,v,t", AT, AT, breg);
3342       if (place != NULL)
3343         place += 4;
3344       macro_build (place, counter, ep, op, "t,o(b)", treg,
3345                    (int) BFD_RELOC_LO16, AT);
3346
3347       if (mips_opts.noat)
3348         as_warn (_("Macro used $at after \".set noat\""));
3349     }
3350 }
3351
3352 /*                      set_at()
3353  * Generates code to set the $at register to true (one)
3354  * if reg is less than the immediate expression.
3355  */
3356 static void
3357 set_at (counter, reg, unsignedp)
3358      int *counter;
3359      int reg;
3360      int unsignedp;
3361 {
3362   if (imm_expr.X_op == O_constant
3363       && imm_expr.X_add_number >= -0x8000
3364       && imm_expr.X_add_number < 0x8000)
3365     macro_build ((char *) NULL, counter, &imm_expr,
3366                  unsignedp ? "sltiu" : "slti",
3367                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3368   else
3369     {
3370       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3371       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3372                    unsignedp ? "sltu" : "slt",
3373                    "d,v,t", AT, reg, AT);
3374     }
3375 }
3376
3377 /* Warn if an expression is not a constant.  */
3378
3379 static void
3380 check_absolute_expr (ip, ex)
3381      struct mips_cl_insn *ip;
3382      expressionS *ex;
3383 {
3384   if (ex->X_op == O_big)
3385     as_bad (_("unsupported large constant"));
3386   else if (ex->X_op != O_constant)
3387     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3388 }
3389
3390 /* Count the leading zeroes by performing a binary chop. This is a
3391    bulky bit of source, but performance is a LOT better for the
3392    majority of values than a simple loop to count the bits:
3393        for (lcnt = 0; (lcnt < 32); lcnt++)
3394          if ((v) & (1 << (31 - lcnt)))
3395            break;
3396   However it is not code size friendly, and the gain will drop a bit
3397   on certain cached systems.
3398 */
3399 #define COUNT_TOP_ZEROES(v)             \
3400   (((v) & ~0xffff) == 0                 \
3401    ? ((v) & ~0xff) == 0                 \
3402      ? ((v) & ~0xf) == 0                \
3403        ? ((v) & ~0x3) == 0              \
3404          ? ((v) & ~0x1) == 0            \
3405            ? !(v)                       \
3406              ? 32                       \
3407              : 31                       \
3408            : 30                         \
3409          : ((v) & ~0x7) == 0            \
3410            ? 29                         \
3411            : 28                         \
3412        : ((v) & ~0x3f) == 0             \
3413          ? ((v) & ~0x1f) == 0           \
3414            ? 27                         \
3415            : 26                         \
3416          : ((v) & ~0x7f) == 0           \
3417            ? 25                         \
3418            : 24                         \
3419      : ((v) & ~0xfff) == 0              \
3420        ? ((v) & ~0x3ff) == 0            \
3421          ? ((v) & ~0x1ff) == 0          \
3422            ? 23                         \
3423            : 22                         \
3424          : ((v) & ~0x7ff) == 0          \
3425            ? 21                         \
3426            : 20                         \
3427        : ((v) & ~0x3fff) == 0           \
3428          ? ((v) & ~0x1fff) == 0         \
3429            ? 19                         \
3430            : 18                         \
3431          : ((v) & ~0x7fff) == 0         \
3432            ? 17                         \
3433            : 16                         \
3434    : ((v) & ~0xffffff) == 0             \
3435      ? ((v) & ~0xfffff) == 0            \
3436        ? ((v) & ~0x3ffff) == 0          \
3437          ? ((v) & ~0x1ffff) == 0        \
3438            ? 15                         \
3439            : 14                         \
3440          : ((v) & ~0x7ffff) == 0        \
3441            ? 13                         \
3442            : 12                         \
3443        : ((v) & ~0x3fffff) == 0         \
3444          ? ((v) & ~0x1fffff) == 0       \
3445            ? 11                         \
3446            : 10                         \
3447          : ((v) & ~0x7fffff) == 0       \
3448            ? 9                          \
3449            : 8                          \
3450      : ((v) & ~0xfffffff) == 0          \
3451        ? ((v) & ~0x3ffffff) == 0        \
3452          ? ((v) & ~0x1ffffff) == 0      \
3453            ? 7                          \
3454            : 6                          \
3455          : ((v) & ~0x7ffffff) == 0      \
3456            ? 5                          \
3457            : 4                          \
3458        : ((v) & ~0x3fffffff) == 0       \
3459          ? ((v) & ~0x1fffffff) == 0     \
3460            ? 3                          \
3461            : 2                          \
3462          : ((v) & ~0x7fffffff) == 0     \
3463            ? 1                          \
3464            : 0)
3465
3466 /*                      load_register()
3467  *  This routine generates the least number of instructions neccessary to load
3468  *  an absolute expression value into a register.
3469  */
3470 static void
3471 load_register (counter, reg, ep, dbl)
3472      int *counter;
3473      int reg;
3474      expressionS *ep;
3475      int dbl;
3476 {
3477   int freg;
3478   expressionS hi32, lo32;
3479
3480   if (ep->X_op != O_big)
3481     {
3482       assert (ep->X_op == O_constant);
3483       if (ep->X_add_number < 0x8000
3484           && (ep->X_add_number >= 0
3485               || (ep->X_add_number >= -0x8000
3486                   && (! dbl
3487                       || ! ep->X_unsigned
3488                       || sizeof (ep->X_add_number) > 4))))
3489         {
3490           /* We can handle 16 bit signed values with an addiu to
3491              $zero.  No need to ever use daddiu here, since $zero and
3492              the result are always correct in 32 bit mode.  */
3493           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3494                        (int) BFD_RELOC_LO16);
3495           return;
3496         }
3497       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3498         {
3499           /* We can handle 16 bit unsigned values with an ori to
3500              $zero.  */
3501           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3502                        (int) BFD_RELOC_LO16);
3503           return;
3504         }
3505       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3506                 && (! dbl
3507                     || ! ep->X_unsigned
3508                     || sizeof (ep->X_add_number) > 4
3509                     || (ep->X_add_number & 0x80000000) == 0))
3510                || ((HAVE_32BIT_GPRS || ! dbl)
3511                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3512                || (HAVE_32BIT_GPRS
3513                    && ! dbl
3514                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3515                        == ~ (offsetT) 0xffffffff)))
3516         {
3517           /* 32 bit values require an lui.  */
3518           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3519                        (int) BFD_RELOC_HI16);
3520           if ((ep->X_add_number & 0xffff) != 0)
3521             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3522                          (int) BFD_RELOC_LO16);
3523           return;
3524         }
3525     }
3526
3527   /* The value is larger than 32 bits.  */
3528
3529   if (HAVE_32BIT_GPRS)
3530     {
3531       as_bad (_("Number (0x%lx) larger than 32 bits"),
3532               (unsigned long) ep->X_add_number);
3533       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3534                    (int) BFD_RELOC_LO16);
3535       return;
3536     }
3537
3538   if (ep->X_op != O_big)
3539     {
3540       hi32 = *ep;
3541       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3542       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3543       hi32.X_add_number &= 0xffffffff;
3544       lo32 = *ep;
3545       lo32.X_add_number &= 0xffffffff;
3546     }
3547   else
3548     {
3549       assert (ep->X_add_number > 2);
3550       if (ep->X_add_number == 3)
3551         generic_bignum[3] = 0;
3552       else if (ep->X_add_number > 4)
3553         as_bad (_("Number larger than 64 bits"));
3554       lo32.X_op = O_constant;
3555       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3556       hi32.X_op = O_constant;
3557       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3558     }
3559
3560   if (hi32.X_add_number == 0)
3561     freg = 0;
3562   else
3563     {
3564       int shift, bit;
3565       unsigned long hi, lo;
3566
3567       if (hi32.X_add_number == (offsetT) 0xffffffff)
3568         {
3569           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3570             {
3571               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3572                            reg, 0, (int) BFD_RELOC_LO16);
3573               return;
3574             }
3575           if (lo32.X_add_number & 0x80000000)
3576             {
3577               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3578                            (int) BFD_RELOC_HI16);
3579               if (lo32.X_add_number & 0xffff)
3580                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3581                              reg, reg, (int) BFD_RELOC_LO16);
3582               return;
3583             }
3584         }
3585
3586       /* Check for 16bit shifted constant.  We know that hi32 is
3587          non-zero, so start the mask on the first bit of the hi32
3588          value.  */
3589       shift = 17;
3590       do
3591         {
3592           unsigned long himask, lomask;
3593
3594           if (shift < 32)
3595             {
3596               himask = 0xffff >> (32 - shift);
3597               lomask = (0xffff << shift) & 0xffffffff;
3598             }
3599           else
3600             {
3601               himask = 0xffff << (shift - 32);
3602               lomask = 0;
3603             }
3604           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3605               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3606             {
3607               expressionS tmp;
3608
3609               tmp.X_op = O_constant;
3610               if (shift < 32)
3611                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3612                                     | (lo32.X_add_number >> shift));
3613               else
3614                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3615               macro_build ((char *) NULL, counter, &tmp,
3616                            "ori", "t,r,i", reg, 0,
3617                            (int) BFD_RELOC_LO16);
3618               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3619                            (shift >= 32) ? "dsll32" : "dsll",
3620                            "d,w,<", reg, reg,
3621                            (shift >= 32) ? shift - 32 : shift);
3622               return;
3623             }
3624           ++shift;
3625         }
3626       while (shift <= (64 - 16));
3627
3628       /* Find the bit number of the lowest one bit, and store the
3629          shifted value in hi/lo.  */
3630       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3631       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3632       if (lo != 0)
3633         {
3634           bit = 0;
3635           while ((lo & 1) == 0)
3636             {
3637               lo >>= 1;
3638               ++bit;
3639             }
3640           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3641           hi >>= bit;
3642         }
3643       else
3644         {
3645           bit = 32;
3646           while ((hi & 1) == 0)
3647             {
3648               hi >>= 1;
3649               ++bit;
3650             }
3651           lo = hi;
3652           hi = 0;
3653         }
3654
3655       /* Optimize if the shifted value is a (power of 2) - 1.  */
3656       if ((hi == 0 && ((lo + 1) & lo) == 0)
3657           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3658         {
3659           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3660           if (shift != 0)
3661             {
3662               expressionS tmp;
3663
3664               /* This instruction will set the register to be all
3665                  ones.  */
3666               tmp.X_op = O_constant;
3667               tmp.X_add_number = (offsetT) -1;
3668               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3669                            reg, 0, (int) BFD_RELOC_LO16);
3670               if (bit != 0)
3671                 {
3672                   bit += shift;
3673                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3674                                (bit >= 32) ? "dsll32" : "dsll",
3675                                "d,w,<", reg, reg,
3676                                (bit >= 32) ? bit - 32 : bit);
3677                 }
3678               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3679                            (shift >= 32) ? "dsrl32" : "dsrl",
3680                            "d,w,<", reg, reg,
3681                            (shift >= 32) ? shift - 32 : shift);
3682               return;
3683             }
3684         }
3685
3686       /* Sign extend hi32 before calling load_register, because we can
3687          generally get better code when we load a sign extended value.  */
3688       if ((hi32.X_add_number & 0x80000000) != 0)
3689         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3690       load_register (counter, reg, &hi32, 0);
3691       freg = reg;
3692     }
3693   if ((lo32.X_add_number & 0xffff0000) == 0)
3694     {
3695       if (freg != 0)
3696         {
3697           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3698                        "dsll32", "d,w,<", reg, freg, 0);
3699           freg = reg;
3700         }
3701     }
3702   else
3703     {
3704       expressionS mid16;
3705
3706       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3707         {
3708           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3709                        (int) BFD_RELOC_HI16);
3710           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3711                        "dsrl32", "d,w,<", reg, reg, 0);
3712           return;
3713         }
3714
3715       if (freg != 0)
3716         {
3717           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3718                        "d,w,<", reg, freg, 16);
3719           freg = reg;
3720         }
3721       mid16 = lo32;
3722       mid16.X_add_number >>= 16;
3723       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3724                    freg, (int) BFD_RELOC_LO16);
3725       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3726                    "d,w,<", reg, reg, 16);
3727       freg = reg;
3728     }
3729   if ((lo32.X_add_number & 0xffff) != 0)
3730     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3731                  (int) BFD_RELOC_LO16);
3732 }
3733
3734 /* Load an address into a register.  */
3735
3736 static void
3737 load_address (counter, reg, ep, used_at)
3738      int *counter;
3739      int reg;
3740      expressionS *ep;
3741      int *used_at;
3742 {
3743   char *p = NULL;
3744
3745   if (ep->X_op != O_constant
3746       && ep->X_op != O_symbol)
3747     {
3748       as_bad (_("expression too complex"));
3749       ep->X_op = O_constant;
3750     }
3751
3752   if (ep->X_op == O_constant)
3753     {
3754       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3755       return;
3756     }
3757
3758   if (mips_pic == NO_PIC)
3759     {
3760       /* If this is a reference to a GP relative symbol, we want
3761            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3762          Otherwise we want
3763            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3764            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3765          If we have an addend, we always use the latter form.
3766
3767          With 64bit address space and a usable $at we want
3768            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3769            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3770            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3771            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3772            dsll32       $reg,0
3773            daddu        $reg,$reg,$at
3774
3775          If $at is already in use, we use an path which is suboptimal
3776          on superscalar processors.
3777            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3778            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3779            dsll         $reg,16
3780            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3781            dsll         $reg,16
3782            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3783        */
3784       if (HAVE_64BIT_ADDRESSES)
3785         {
3786           /* We don't do GP optimization for now because RELAX_ENCODE can't
3787              hold the data for such large chunks.  */
3788
3789           if (*used_at == 0 && ! mips_opts.noat)
3790             {
3791               macro_build (p, counter, ep, "lui", "t,u",
3792                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3793               macro_build (p, counter, ep, "lui", "t,u",
3794                            AT, (int) BFD_RELOC_HI16_S);
3795               macro_build (p, counter, ep, "daddiu", "t,r,j",
3796                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3797               macro_build (p, counter, ep, "daddiu", "t,r,j",
3798                            AT, AT, (int) BFD_RELOC_LO16);
3799               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3800                            "d,w,<", reg, reg, 0);
3801               macro_build (p, counter, (expressionS *) NULL, "daddu",
3802                            "d,v,t", reg, reg, AT);
3803               *used_at = 1;
3804             }
3805           else
3806             {
3807               macro_build (p, counter, ep, "lui", "t,u",
3808                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3809               macro_build (p, counter, ep, "daddiu", "t,r,j",
3810                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3811               macro_build (p, counter, (expressionS *) NULL, "dsll",
3812                            "d,w,<", reg, reg, 16);
3813               macro_build (p, counter, ep, "daddiu", "t,r,j",
3814                            reg, reg, (int) BFD_RELOC_HI16_S);
3815               macro_build (p, counter, (expressionS *) NULL, "dsll",
3816                            "d,w,<", reg, reg, 16);
3817               macro_build (p, counter, ep, "daddiu", "t,r,j",
3818                            reg, reg, (int) BFD_RELOC_LO16);
3819             }
3820         }
3821       else
3822         {
3823           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3824               && ! nopic_need_relax (ep->X_add_symbol, 1))
3825             {
3826               frag_grow (20);
3827               macro_build ((char *) NULL, counter, ep,
3828                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3829                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3830               p = frag_var (rs_machine_dependent, 8, 0,
3831                             RELAX_ENCODE (4, 8, 0, 4, 0,
3832                                           mips_opts.warn_about_macros),
3833                             ep->X_add_symbol, 0, NULL);
3834             }
3835           macro_build_lui (p, counter, ep, reg);
3836           if (p != NULL)
3837             p += 4;
3838           macro_build (p, counter, ep,
3839                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3840                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3841         }
3842     }
3843   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3844     {
3845       expressionS ex;
3846
3847       /* If this is a reference to an external symbol, we want
3848            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3849          Otherwise we want
3850            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3851            nop
3852            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3853          If we have NewABI, we want
3854            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3855          If there is a constant, it must be added in after.  */
3856       ex.X_add_number = ep->X_add_number;
3857       ep->X_add_number = 0;
3858       frag_grow (20);
3859       if (HAVE_NEWABI)
3860         {
3861           macro_build ((char *) NULL, counter, ep,
3862                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3863                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3864         }
3865       else
3866         {
3867           macro_build ((char *) NULL, counter, ep,
3868                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3869                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3870           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3871           p = frag_var (rs_machine_dependent, 4, 0,
3872                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3873                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3874           macro_build (p, counter, ep,
3875                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3876                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3877         }
3878
3879       if (ex.X_add_number != 0)
3880         {
3881           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3882             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3883           ex.X_op = O_constant;
3884           macro_build ((char *) NULL, counter, &ex,
3885                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3886                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3887         }
3888     }
3889   else if (mips_pic == SVR4_PIC)
3890     {
3891       expressionS ex;
3892       int off;
3893
3894       /* This is the large GOT case.  If this is a reference to an
3895          external symbol, we want
3896            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3897            addu         $reg,$reg,$gp
3898            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3899          Otherwise, for a reference to a local symbol, we want
3900            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3901            nop
3902            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3903          If we have NewABI, we want
3904            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3905            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3906          If there is a constant, it must be added in after.  */
3907       ex.X_add_number = ep->X_add_number;
3908       ep->X_add_number = 0;
3909       if (HAVE_NEWABI)
3910         {
3911           macro_build ((char *) NULL, counter, ep,
3912                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3913                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3914           macro_build (p, counter, ep,
3915                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3916                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3917         }
3918       else
3919         {
3920           if (reg_needs_delay (mips_gp_register))
3921             off = 4;
3922           else
3923             off = 0;
3924           frag_grow (32);
3925           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3926                        (int) BFD_RELOC_MIPS_GOT_HI16);
3927           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3928                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3929                        reg, mips_gp_register);
3930           macro_build ((char *) NULL, counter, ep,
3931                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3932                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3933           p = frag_var (rs_machine_dependent, 12 + off, 0,
3934                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3935                                       mips_opts.warn_about_macros),
3936                         ep->X_add_symbol, 0, NULL);
3937           if (off > 0)
3938             {
3939               /* We need a nop before loading from $gp.  This special
3940                  check is required because the lui which starts the main
3941                  instruction stream does not refer to $gp, and so will not
3942                  insert the nop which may be required.  */
3943               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3944                 p += 4;
3945             }
3946           macro_build (p, counter, ep,
3947                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3948                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3949           p += 4;
3950           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3951           p += 4;
3952           macro_build (p, counter, ep,
3953                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3954                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3955         }
3956
3957       if (ex.X_add_number != 0)
3958         {
3959           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3960             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3961           ex.X_op = O_constant;
3962           macro_build ((char *) NULL, counter, &ex,
3963                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3964                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3965         }
3966     }
3967   else if (mips_pic == EMBEDDED_PIC)
3968     {
3969       /* We always do
3970            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3971        */
3972       macro_build ((char *) NULL, counter, ep,
3973                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3974                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3975     }
3976   else
3977     abort ();
3978 }
3979
3980 /* Move the contents of register SOURCE into register DEST.  */
3981
3982 static void
3983 move_register (counter, dest, source)
3984      int *counter;
3985      int dest;
3986      int source;
3987 {
3988   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3989                HAVE_32BIT_GPRS ? "addu" : "daddu",
3990                "d,v,t", dest, source, 0);
3991 }
3992
3993 /*
3994  *                      Build macros
3995  *   This routine implements the seemingly endless macro or synthesized
3996  * instructions and addressing modes in the mips assembly language. Many
3997  * of these macros are simple and are similar to each other. These could
3998  * probably be handled by some kind of table or grammer aproach instead of
3999  * this verbose method. Others are not simple macros but are more like
4000  * optimizing code generation.
4001  *   One interesting optimization is when several store macros appear
4002  * consecutivly that would load AT with the upper half of the same address.
4003  * The ensuing load upper instructions are ommited. This implies some kind
4004  * of global optimization. We currently only optimize within a single macro.
4005  *   For many of the load and store macros if the address is specified as a
4006  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4007  * first load register 'at' with zero and use it as the base register. The
4008  * mips assembler simply uses register $zero. Just one tiny optimization
4009  * we're missing.
4010  */
4011 static void
4012 macro (ip)
4013      struct mips_cl_insn *ip;
4014 {
4015   register int treg, sreg, dreg, breg;
4016   int tempreg;
4017   int mask;
4018   int icnt = 0;
4019   int used_at = 0;
4020   expressionS expr1;
4021   const char *s;
4022   const char *s2;
4023   const char *fmt;
4024   int likely = 0;
4025   int dbl = 0;
4026   int coproc = 0;
4027   int lr = 0;
4028   int imm = 0;
4029   offsetT maxnum;
4030   int off;
4031   bfd_reloc_code_real_type r;
4032   int hold_mips_optimize;
4033
4034   assert (! mips_opts.mips16);
4035
4036   treg = (ip->insn_opcode >> 16) & 0x1f;
4037   dreg = (ip->insn_opcode >> 11) & 0x1f;
4038   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4039   mask = ip->insn_mo->mask;
4040
4041   expr1.X_op = O_constant;
4042   expr1.X_op_symbol = NULL;
4043   expr1.X_add_symbol = NULL;
4044   expr1.X_add_number = 1;
4045
4046   switch (mask)
4047     {
4048     case M_DABS:
4049       dbl = 1;
4050     case M_ABS:
4051       /* bgez $a0,.+12
4052          move v0,$a0
4053          sub v0,$zero,$a0
4054          */
4055
4056       mips_emit_delays (true);
4057       ++mips_opts.noreorder;
4058       mips_any_noreorder = 1;
4059
4060       expr1.X_add_number = 8;
4061       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4062       if (dreg == sreg)
4063         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4064                      0);
4065       else
4066         move_register (&icnt, dreg, sreg);
4067       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4068                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4069
4070       --mips_opts.noreorder;
4071       return;
4072
4073     case M_ADD_I:
4074       s = "addi";
4075       s2 = "add";
4076       goto do_addi;
4077     case M_ADDU_I:
4078       s = "addiu";
4079       s2 = "addu";
4080       goto do_addi;
4081     case M_DADD_I:
4082       dbl = 1;
4083       s = "daddi";
4084       s2 = "dadd";
4085       goto do_addi;
4086     case M_DADDU_I:
4087       dbl = 1;
4088       s = "daddiu";
4089       s2 = "daddu";
4090     do_addi:
4091       if (imm_expr.X_op == O_constant
4092           && imm_expr.X_add_number >= -0x8000
4093           && imm_expr.X_add_number < 0x8000)
4094         {
4095           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4096                        (int) BFD_RELOC_LO16);
4097           return;
4098         }
4099       load_register (&icnt, AT, &imm_expr, dbl);
4100       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4101                    treg, sreg, AT);
4102       break;
4103
4104     case M_AND_I:
4105       s = "andi";
4106       s2 = "and";
4107       goto do_bit;
4108     case M_OR_I:
4109       s = "ori";
4110       s2 = "or";
4111       goto do_bit;
4112     case M_NOR_I:
4113       s = "";
4114       s2 = "nor";
4115       goto do_bit;
4116     case M_XOR_I:
4117       s = "xori";
4118       s2 = "xor";
4119     do_bit:
4120       if (imm_expr.X_op == O_constant
4121           && imm_expr.X_add_number >= 0
4122           && imm_expr.X_add_number < 0x10000)
4123         {
4124           if (mask != M_NOR_I)
4125             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4126                          sreg, (int) BFD_RELOC_LO16);
4127           else
4128             {
4129               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4130                            treg, sreg, (int) BFD_RELOC_LO16);
4131               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4132                            "d,v,t", treg, treg, 0);
4133             }
4134           return;
4135         }
4136
4137       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4138       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4139                    treg, sreg, AT);
4140       break;
4141
4142     case M_BEQ_I:
4143       s = "beq";
4144       goto beq_i;
4145     case M_BEQL_I:
4146       s = "beql";
4147       likely = 1;
4148       goto beq_i;
4149     case M_BNE_I:
4150       s = "bne";
4151       goto beq_i;
4152     case M_BNEL_I:
4153       s = "bnel";
4154       likely = 1;
4155     beq_i:
4156       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4157         {
4158           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4159                        0);
4160           return;
4161         }
4162       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4163       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4164       break;
4165
4166     case M_BGEL:
4167       likely = 1;
4168     case M_BGE:
4169       if (treg == 0)
4170         {
4171           macro_build ((char *) NULL, &icnt, &offset_expr,
4172                        likely ? "bgezl" : "bgez", "s,p", sreg);
4173           return;
4174         }
4175       if (sreg == 0)
4176         {
4177           macro_build ((char *) NULL, &icnt, &offset_expr,
4178                        likely ? "blezl" : "blez", "s,p", treg);
4179           return;
4180         }
4181       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4182                    AT, sreg, treg);
4183       macro_build ((char *) NULL, &icnt, &offset_expr,
4184                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4185       break;
4186
4187     case M_BGTL_I:
4188       likely = 1;
4189     case M_BGT_I:
4190       /* check for > max integer */
4191       maxnum = 0x7fffffff;
4192       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4193         {
4194           maxnum <<= 16;
4195           maxnum |= 0xffff;
4196           maxnum <<= 16;
4197           maxnum |= 0xffff;
4198         }
4199       if (imm_expr.X_op == O_constant
4200           && imm_expr.X_add_number >= maxnum
4201           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4202         {
4203         do_false:
4204           /* result is always false */
4205           if (! likely)
4206             {
4207               if (warn_nops)
4208                 as_warn (_("Branch %s is always false (nop)"),
4209                          ip->insn_mo->name);
4210               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4211                            "", 0);
4212             }
4213           else
4214             {
4215               if (warn_nops)
4216                 as_warn (_("Branch likely %s is always false"),
4217                          ip->insn_mo->name);
4218               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4219                            "s,t,p", 0, 0);
4220             }
4221           return;
4222         }
4223       if (imm_expr.X_op != O_constant)
4224         as_bad (_("Unsupported large constant"));
4225       ++imm_expr.X_add_number;
4226       /* FALLTHROUGH */
4227     case M_BGE_I:
4228     case M_BGEL_I:
4229       if (mask == M_BGEL_I)
4230         likely = 1;
4231       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4232         {
4233           macro_build ((char *) NULL, &icnt, &offset_expr,
4234                        likely ? "bgezl" : "bgez", "s,p", sreg);
4235           return;
4236         }
4237       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4238         {
4239           macro_build ((char *) NULL, &icnt, &offset_expr,
4240                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4241           return;
4242         }
4243       maxnum = 0x7fffffff;
4244       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4245         {
4246           maxnum <<= 16;
4247           maxnum |= 0xffff;
4248           maxnum <<= 16;
4249           maxnum |= 0xffff;
4250         }
4251       maxnum = - maxnum - 1;
4252       if (imm_expr.X_op == O_constant
4253           && imm_expr.X_add_number <= maxnum
4254           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4255         {
4256         do_true:
4257           /* result is always true */
4258           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4259           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4260           return;
4261         }
4262       set_at (&icnt, sreg, 0);
4263       macro_build ((char *) NULL, &icnt, &offset_expr,
4264                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4265       break;
4266
4267     case M_BGEUL:
4268       likely = 1;
4269     case M_BGEU:
4270       if (treg == 0)
4271         goto do_true;
4272       if (sreg == 0)
4273         {
4274           macro_build ((char *) NULL, &icnt, &offset_expr,
4275                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4276           return;
4277         }
4278       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4279                    "d,v,t", AT, sreg, treg);
4280       macro_build ((char *) NULL, &icnt, &offset_expr,
4281                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4282       break;
4283
4284     case M_BGTUL_I:
4285       likely = 1;
4286     case M_BGTU_I:
4287       if (sreg == 0
4288           || (HAVE_32BIT_GPRS
4289               && imm_expr.X_op == O_constant
4290               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4291         goto do_false;
4292       if (imm_expr.X_op != O_constant)
4293         as_bad (_("Unsupported large constant"));
4294       ++imm_expr.X_add_number;
4295       /* FALLTHROUGH */
4296     case M_BGEU_I:
4297     case M_BGEUL_I:
4298       if (mask == M_BGEUL_I)
4299         likely = 1;
4300       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4301         goto do_true;
4302       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4303         {
4304           macro_build ((char *) NULL, &icnt, &offset_expr,
4305                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4306           return;
4307         }
4308       set_at (&icnt, sreg, 1);
4309       macro_build ((char *) NULL, &icnt, &offset_expr,
4310                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4311       break;
4312
4313     case M_BGTL:
4314       likely = 1;
4315     case M_BGT:
4316       if (treg == 0)
4317         {
4318           macro_build ((char *) NULL, &icnt, &offset_expr,
4319                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4320           return;
4321         }
4322       if (sreg == 0)
4323         {
4324           macro_build ((char *) NULL, &icnt, &offset_expr,
4325                        likely ? "bltzl" : "bltz", "s,p", treg);
4326           return;
4327         }
4328       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4329                    AT, treg, sreg);
4330       macro_build ((char *) NULL, &icnt, &offset_expr,
4331                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4332       break;
4333
4334     case M_BGTUL:
4335       likely = 1;
4336     case M_BGTU:
4337       if (treg == 0)
4338         {
4339           macro_build ((char *) NULL, &icnt, &offset_expr,
4340                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4341           return;
4342         }
4343       if (sreg == 0)
4344         goto do_false;
4345       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4346                    "d,v,t", AT, treg, sreg);
4347       macro_build ((char *) NULL, &icnt, &offset_expr,
4348                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4349       break;
4350
4351     case M_BLEL:
4352       likely = 1;
4353     case M_BLE:
4354       if (treg == 0)
4355         {
4356           macro_build ((char *) NULL, &icnt, &offset_expr,
4357                        likely ? "blezl" : "blez", "s,p", sreg);
4358           return;
4359         }
4360       if (sreg == 0)
4361         {
4362           macro_build ((char *) NULL, &icnt, &offset_expr,
4363                        likely ? "bgezl" : "bgez", "s,p", treg);
4364           return;
4365         }
4366       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4367                    AT, treg, sreg);
4368       macro_build ((char *) NULL, &icnt, &offset_expr,
4369                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4370       break;
4371
4372     case M_BLEL_I:
4373       likely = 1;
4374     case M_BLE_I:
4375       maxnum = 0x7fffffff;
4376       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4377         {
4378           maxnum <<= 16;
4379           maxnum |= 0xffff;
4380           maxnum <<= 16;
4381           maxnum |= 0xffff;
4382         }
4383       if (imm_expr.X_op == O_constant
4384           && imm_expr.X_add_number >= maxnum
4385           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4386         goto do_true;
4387       if (imm_expr.X_op != O_constant)
4388         as_bad (_("Unsupported large constant"));
4389       ++imm_expr.X_add_number;
4390       /* FALLTHROUGH */
4391     case M_BLT_I:
4392     case M_BLTL_I:
4393       if (mask == M_BLTL_I)
4394         likely = 1;
4395       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4396         {
4397           macro_build ((char *) NULL, &icnt, &offset_expr,
4398                        likely ? "bltzl" : "bltz", "s,p", sreg);
4399           return;
4400         }
4401       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4402         {
4403           macro_build ((char *) NULL, &icnt, &offset_expr,
4404                        likely ? "blezl" : "blez", "s,p", sreg);
4405           return;
4406         }
4407       set_at (&icnt, sreg, 0);
4408       macro_build ((char *) NULL, &icnt, &offset_expr,
4409                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4410       break;
4411
4412     case M_BLEUL:
4413       likely = 1;
4414     case M_BLEU:
4415       if (treg == 0)
4416         {
4417           macro_build ((char *) NULL, &icnt, &offset_expr,
4418                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4419           return;
4420         }
4421       if (sreg == 0)
4422         goto do_true;
4423       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4424                    "d,v,t", AT, treg, sreg);
4425       macro_build ((char *) NULL, &icnt, &offset_expr,
4426                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4427       break;
4428
4429     case M_BLEUL_I:
4430       likely = 1;
4431     case M_BLEU_I:
4432       if (sreg == 0
4433           || (HAVE_32BIT_GPRS
4434               && imm_expr.X_op == O_constant
4435               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4436         goto do_true;
4437       if (imm_expr.X_op != O_constant)
4438         as_bad (_("Unsupported large constant"));
4439       ++imm_expr.X_add_number;
4440       /* FALLTHROUGH */
4441     case M_BLTU_I:
4442     case M_BLTUL_I:
4443       if (mask == M_BLTUL_I)
4444         likely = 1;
4445       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4446         goto do_false;
4447       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4448         {
4449           macro_build ((char *) NULL, &icnt, &offset_expr,
4450                        likely ? "beql" : "beq",
4451                        "s,t,p", sreg, 0);
4452           return;
4453         }
4454       set_at (&icnt, sreg, 1);
4455       macro_build ((char *) NULL, &icnt, &offset_expr,
4456                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4457       break;
4458
4459     case M_BLTL:
4460       likely = 1;
4461     case M_BLT:
4462       if (treg == 0)
4463         {
4464           macro_build ((char *) NULL, &icnt, &offset_expr,
4465                        likely ? "bltzl" : "bltz", "s,p", sreg);
4466           return;
4467         }
4468       if (sreg == 0)
4469         {
4470           macro_build ((char *) NULL, &icnt, &offset_expr,
4471                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4472           return;
4473         }
4474       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4475                    AT, sreg, treg);
4476       macro_build ((char *) NULL, &icnt, &offset_expr,
4477                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4478       break;
4479
4480     case M_BLTUL:
4481       likely = 1;
4482     case M_BLTU:
4483       if (treg == 0)
4484         goto do_false;
4485       if (sreg == 0)
4486         {
4487           macro_build ((char *) NULL, &icnt, &offset_expr,
4488                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4489           return;
4490         }
4491       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4492                    "d,v,t", AT, sreg,
4493                    treg);
4494       macro_build ((char *) NULL, &icnt, &offset_expr,
4495                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4496       break;
4497
4498     case M_DDIV_3:
4499       dbl = 1;
4500     case M_DIV_3:
4501       s = "mflo";
4502       goto do_div3;
4503     case M_DREM_3:
4504       dbl = 1;
4505     case M_REM_3:
4506       s = "mfhi";
4507     do_div3:
4508       if (treg == 0)
4509         {
4510           as_warn (_("Divide by zero."));
4511           if (mips_trap)
4512             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4513                          "s,t,q", 0, 0, 7);
4514           else
4515             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4516                          "c", 7);
4517           return;
4518         }
4519
4520       mips_emit_delays (true);
4521       ++mips_opts.noreorder;
4522       mips_any_noreorder = 1;
4523       if (mips_trap)
4524         {
4525           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4526                        "s,t,q", treg, 0, 7);
4527           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4528                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4529         }
4530       else
4531         {
4532           expr1.X_add_number = 8;
4533           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4534           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4535                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4536           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4537                        "c", 7);
4538         }
4539       expr1.X_add_number = -1;
4540       macro_build ((char *) NULL, &icnt, &expr1,
4541                    dbl ? "daddiu" : "addiu",
4542                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4543       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4544       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4545       if (dbl)
4546         {
4547           expr1.X_add_number = 1;
4548           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4549                        (int) BFD_RELOC_LO16);
4550           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4551                        "d,w,<", AT, AT, 31);
4552         }
4553       else
4554         {
4555           expr1.X_add_number = 0x80000000;
4556           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4557                        (int) BFD_RELOC_HI16);
4558         }
4559       if (mips_trap)
4560         {
4561           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4562                        "s,t,q", sreg, AT, 6);
4563           /* We want to close the noreorder block as soon as possible, so
4564              that later insns are available for delay slot filling.  */
4565           --mips_opts.noreorder;
4566         }
4567       else
4568         {
4569           expr1.X_add_number = 8;
4570           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4571           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4572                        0);
4573
4574           /* We want to close the noreorder block as soon as possible, so
4575              that later insns are available for delay slot filling.  */
4576           --mips_opts.noreorder;
4577
4578           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4579                        "c", 6);
4580         }
4581       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4582       break;
4583
4584     case M_DIV_3I:
4585       s = "div";
4586       s2 = "mflo";
4587       goto do_divi;
4588     case M_DIVU_3I:
4589       s = "divu";
4590       s2 = "mflo";
4591       goto do_divi;
4592     case M_REM_3I:
4593       s = "div";
4594       s2 = "mfhi";
4595       goto do_divi;
4596     case M_REMU_3I:
4597       s = "divu";
4598       s2 = "mfhi";
4599       goto do_divi;
4600     case M_DDIV_3I:
4601       dbl = 1;
4602       s = "ddiv";
4603       s2 = "mflo";
4604       goto do_divi;
4605     case M_DDIVU_3I:
4606       dbl = 1;
4607       s = "ddivu";
4608       s2 = "mflo";
4609       goto do_divi;
4610     case M_DREM_3I:
4611       dbl = 1;
4612       s = "ddiv";
4613       s2 = "mfhi";
4614       goto do_divi;
4615     case M_DREMU_3I:
4616       dbl = 1;
4617       s = "ddivu";
4618       s2 = "mfhi";
4619     do_divi:
4620       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4621         {
4622           as_warn (_("Divide by zero."));
4623           if (mips_trap)
4624             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4625                          "s,t,q", 0, 0, 7);
4626           else
4627             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4628                          "c", 7);
4629           return;
4630         }
4631       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4632         {
4633           if (strcmp (s2, "mflo") == 0)
4634             move_register (&icnt, dreg, sreg);
4635           else
4636             move_register (&icnt, dreg, 0);
4637           return;
4638         }
4639       if (imm_expr.X_op == O_constant
4640           && imm_expr.X_add_number == -1
4641           && s[strlen (s) - 1] != 'u')
4642         {
4643           if (strcmp (s2, "mflo") == 0)
4644             {
4645               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4646                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4647             }
4648           else
4649             move_register (&icnt, dreg, 0);
4650           return;
4651         }
4652
4653       load_register (&icnt, AT, &imm_expr, dbl);
4654       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4655                    sreg, AT);
4656       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4657       break;
4658
4659     case M_DIVU_3:
4660       s = "divu";
4661       s2 = "mflo";
4662       goto do_divu3;
4663     case M_REMU_3:
4664       s = "divu";
4665       s2 = "mfhi";
4666       goto do_divu3;
4667     case M_DDIVU_3:
4668       s = "ddivu";
4669       s2 = "mflo";
4670       goto do_divu3;
4671     case M_DREMU_3:
4672       s = "ddivu";
4673       s2 = "mfhi";
4674     do_divu3:
4675       mips_emit_delays (true);
4676       ++mips_opts.noreorder;
4677       mips_any_noreorder = 1;
4678       if (mips_trap)
4679         {
4680           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4681                        "s,t,q", treg, 0, 7);
4682           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4683                        sreg, treg);
4684           /* We want to close the noreorder block as soon as possible, so
4685              that later insns are available for delay slot filling.  */
4686           --mips_opts.noreorder;
4687         }
4688       else
4689         {
4690           expr1.X_add_number = 8;
4691           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4692           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4693                        sreg, treg);
4694
4695           /* We want to close the noreorder block as soon as possible, so
4696              that later insns are available for delay slot filling.  */
4697           --mips_opts.noreorder;
4698           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4699                        "c", 7);
4700         }
4701       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4702       return;
4703
4704     case M_DLA_AB:
4705       dbl = 1;
4706     case M_LA_AB:
4707       /* Load the address of a symbol into a register.  If breg is not
4708          zero, we then add a base register to it.  */
4709
4710       if (dbl && HAVE_32BIT_GPRS)
4711         as_warn (_("dla used to load 32-bit register"));
4712
4713       if (! dbl && HAVE_64BIT_OBJECTS)
4714         as_warn (_("la used to load 64-bit address"));
4715
4716       if (offset_expr.X_op == O_constant
4717           && offset_expr.X_add_number >= -0x8000
4718           && offset_expr.X_add_number < 0x8000)
4719         {
4720           macro_build ((char *) NULL, &icnt, &offset_expr,
4721                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4722                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4723           return;
4724         }
4725
4726       if (treg == breg)
4727         {
4728           tempreg = AT;
4729           used_at = 1;
4730         }
4731       else
4732         {
4733           tempreg = treg;
4734           used_at = 0;
4735         }
4736
4737       /* When generating embedded PIC code, we permit expressions of
4738          the form
4739            la   $treg,foo-bar
4740            la   $treg,foo-bar($breg)
4741          where bar is an address in the current section.  These are used
4742          when getting the addresses of functions.  We don't permit
4743          X_add_number to be non-zero, because if the symbol is
4744          external the relaxing code needs to know that any addend is
4745          purely the offset to X_op_symbol.  */
4746       if (mips_pic == EMBEDDED_PIC
4747           && offset_expr.X_op == O_subtract
4748           && (symbol_constant_p (offset_expr.X_op_symbol)
4749               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4750               : (symbol_equated_p (offset_expr.X_op_symbol)
4751                  && (S_GET_SEGMENT
4752                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4753                       ->X_add_symbol)
4754                      == now_seg)))
4755           && (offset_expr.X_add_number == 0
4756               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4757         {
4758           if (breg == 0)
4759             {
4760               tempreg = treg;
4761               used_at = 0;
4762               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4763                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4764             }
4765           else
4766             {
4767               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4768                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4769               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4770                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4771                            "d,v,t", tempreg, tempreg, breg);
4772             }
4773           macro_build ((char *) NULL, &icnt, &offset_expr,
4774                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4775                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4776           if (! used_at)
4777             return;
4778           break;
4779         }
4780
4781       if (offset_expr.X_op != O_symbol
4782           && offset_expr.X_op != O_constant)
4783         {
4784           as_bad (_("expression too complex"));
4785           offset_expr.X_op = O_constant;
4786         }
4787
4788       if (offset_expr.X_op == O_constant)
4789         load_register (&icnt, tempreg, &offset_expr,
4790                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4791                         ? (dbl || HAVE_64BIT_ADDRESSES)
4792                         : HAVE_64BIT_ADDRESSES));
4793       else if (mips_pic == NO_PIC)
4794         {
4795           /* If this is a reference to a GP relative symbol, we want
4796                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4797              Otherwise we want
4798                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4799                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4800              If we have a constant, we need two instructions anyhow,
4801              so we may as well always use the latter form.
4802
4803             With 64bit address space and a usable $at we want
4804               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4805               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4806               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4807               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4808               dsll32    $tempreg,0
4809               daddu     $tempreg,$tempreg,$at
4810
4811             If $at is already in use, we use an path which is suboptimal
4812             on superscalar processors.
4813               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4814               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4815               dsll      $tempreg,16
4816               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4817               dsll      $tempreg,16
4818               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4819           */
4820           char *p = NULL;
4821           if (HAVE_64BIT_ADDRESSES)
4822             {
4823               /* We don't do GP optimization for now because RELAX_ENCODE can't
4824                  hold the data for such large chunks.  */
4825
4826               if (used_at == 0 && ! mips_opts.noat)
4827                 {
4828                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4829                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4830                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4831                                AT, (int) BFD_RELOC_HI16_S);
4832                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4833                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4834                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4835                                AT, AT, (int) BFD_RELOC_LO16);
4836                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4837                                "d,w,<", tempreg, tempreg, 0);
4838                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4839                                "d,v,t", tempreg, tempreg, AT);
4840                   used_at = 1;
4841                 }
4842               else
4843                 {
4844                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4845                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4846                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4847                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4848                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4849                                tempreg, tempreg, 16);
4850                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4851                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4852                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4853                                tempreg, tempreg, 16);
4854                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4855                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4856                 }
4857             }
4858           else
4859             {
4860               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4861                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4862                 {
4863                   frag_grow (20);
4864                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4865                                "t,r,j", tempreg, mips_gp_register,
4866                                (int) BFD_RELOC_GPREL16);
4867                   p = frag_var (rs_machine_dependent, 8, 0,
4868                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4869                                               mips_opts.warn_about_macros),
4870                                 offset_expr.X_add_symbol, 0, NULL);
4871                 }
4872               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4873               if (p != NULL)
4874                 p += 4;
4875               macro_build (p, &icnt, &offset_expr, "addiu",
4876                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4877             }
4878         }
4879       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4880         {
4881           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4882
4883           /* If this is a reference to an external symbol, and there
4884              is no constant, we want
4885                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4886              or if tempreg is PIC_CALL_REG
4887                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4888              For a local symbol, we want
4889                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4890                nop
4891                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4892
4893              If we have a small constant, and this is a reference to
4894              an external symbol, we want
4895                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4896                nop
4897                addiu    $tempreg,$tempreg,<constant>
4898              For a local symbol, we want the same instruction
4899              sequence, but we output a BFD_RELOC_LO16 reloc on the
4900              addiu instruction.
4901
4902              If we have a large constant, and this is a reference to
4903              an external symbol, we want
4904                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4905                lui      $at,<hiconstant>
4906                addiu    $at,$at,<loconstant>
4907                addu     $tempreg,$tempreg,$at
4908              For a local symbol, we want the same instruction
4909              sequence, but we output a BFD_RELOC_LO16 reloc on the
4910              addiu instruction.
4911
4912              For NewABI, we want for local or external data addresses
4913                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4914              For a local function symbol, we want
4915                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4916                nop
4917                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4918            */
4919
4920           expr1.X_add_number = offset_expr.X_add_number;
4921           offset_expr.X_add_number = 0;
4922           frag_grow (32);
4923           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4924             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4925           else if (HAVE_NEWABI)
4926             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4927           macro_build ((char *) NULL, &icnt, &offset_expr,
4928                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4929                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4930           if (expr1.X_add_number == 0)
4931             {
4932               int off;
4933               char *p;
4934
4935               if (breg == 0)
4936                 off = 0;
4937               else
4938                 {
4939                   /* We're going to put in an addu instruction using
4940                      tempreg, so we may as well insert the nop right
4941                      now.  */
4942                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4943                                "nop", "");
4944                   off = 4;
4945                 }
4946               p = frag_var (rs_machine_dependent, 8 - off, 0,
4947                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4948                                           (breg == 0
4949                                            ? mips_opts.warn_about_macros
4950                                            : 0)),
4951                             offset_expr.X_add_symbol, 0, NULL);
4952               if (breg == 0)
4953                 {
4954                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4955                   p += 4;
4956                 }
4957               macro_build (p, &icnt, &expr1,
4958                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4959                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4960               /* FIXME: If breg == 0, and the next instruction uses
4961                  $tempreg, then if this variant case is used an extra
4962                  nop will be generated.  */
4963             }
4964           else if (expr1.X_add_number >= -0x8000
4965                    && expr1.X_add_number < 0x8000)
4966             {
4967               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4968                            "nop", "");
4969               macro_build ((char *) NULL, &icnt, &expr1,
4970                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4971                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4972               frag_var (rs_machine_dependent, 0, 0,
4973                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4974                         offset_expr.X_add_symbol, 0, NULL);
4975             }
4976           else
4977             {
4978               int off1;
4979
4980               /* If we are going to add in a base register, and the
4981                  target register and the base register are the same,
4982                  then we are using AT as a temporary register.  Since
4983                  we want to load the constant into AT, we add our
4984                  current AT (from the global offset table) and the
4985                  register into the register now, and pretend we were
4986                  not using a base register.  */
4987               if (breg != treg)
4988                 off1 = 0;
4989               else
4990                 {
4991                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4992                                "nop", "");
4993                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4994                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4995                                "d,v,t", treg, AT, breg);
4996                   breg = 0;
4997                   tempreg = treg;
4998                   off1 = -8;
4999                 }
5000
5001               /* Set mips_optimize around the lui instruction to avoid
5002                  inserting an unnecessary nop after the lw.  */
5003               hold_mips_optimize = mips_optimize;
5004               mips_optimize = 2;
5005               macro_build_lui (NULL, &icnt, &expr1, AT);
5006               mips_optimize = hold_mips_optimize;
5007
5008               macro_build ((char *) NULL, &icnt, &expr1,
5009                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5010                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5011               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5012                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5013                            "d,v,t", tempreg, tempreg, AT);
5014               frag_var (rs_machine_dependent, 0, 0,
5015                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5016                         offset_expr.X_add_symbol, 0, NULL);
5017               used_at = 1;
5018             }
5019         }
5020       else if (mips_pic == SVR4_PIC)
5021         {
5022           int gpdel;
5023           char *p;
5024           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5025           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5026           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5027
5028           /* This is the large GOT case.  If this is a reference to an
5029              external symbol, and there is no constant, we want
5030                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5031                addu     $tempreg,$tempreg,$gp
5032                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5033              or if tempreg is PIC_CALL_REG
5034                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5035                addu     $tempreg,$tempreg,$gp
5036                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5037              For a local symbol, we want
5038                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5039                nop
5040                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5041
5042              If we have a small constant, and this is a reference to
5043              an external symbol, we want
5044                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5045                addu     $tempreg,$tempreg,$gp
5046                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5047                nop
5048                addiu    $tempreg,$tempreg,<constant>
5049              For a local symbol, we want
5050                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5051                nop
5052                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5053
5054              If we have a large constant, and this is a reference to
5055              an external symbol, we want
5056                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5057                addu     $tempreg,$tempreg,$gp
5058                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5059                lui      $at,<hiconstant>
5060                addiu    $at,$at,<loconstant>
5061                addu     $tempreg,$tempreg,$at
5062              For a local symbol, we want
5063                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5064                lui      $at,<hiconstant>
5065                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5066                addu     $tempreg,$tempreg,$at
5067
5068              For NewABI, we want for local data addresses
5069               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5070            */
5071
5072           expr1.X_add_number = offset_expr.X_add_number;
5073           offset_expr.X_add_number = 0;
5074           frag_grow (52);
5075           if (reg_needs_delay (mips_gp_register))
5076             gpdel = 4;
5077           else
5078             gpdel = 0;
5079           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5080             {
5081               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5082               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5083             }
5084           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5085                        tempreg, lui_reloc_type);
5086           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5087                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5088                        "d,v,t", tempreg, tempreg, mips_gp_register);
5089           macro_build ((char *) NULL, &icnt, &offset_expr,
5090                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5091                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5092           if (expr1.X_add_number == 0)
5093             {
5094               int off;
5095
5096               if (breg == 0)
5097                 off = 0;
5098               else
5099                 {
5100                   /* We're going to put in an addu instruction using
5101                      tempreg, so we may as well insert the nop right
5102                      now.  */
5103                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5104                                "nop", "");
5105                   off = 4;
5106                 }
5107
5108               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5109                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5110                                           8 + gpdel, 0,
5111                                           (breg == 0
5112                                            ? mips_opts.warn_about_macros
5113                                            : 0)),
5114                             offset_expr.X_add_symbol, 0, NULL);
5115             }
5116           else if (expr1.X_add_number >= -0x8000
5117                    && expr1.X_add_number < 0x8000)
5118             {
5119               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5120                            "nop", "");
5121               macro_build ((char *) NULL, &icnt, &expr1,
5122                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5123                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5124
5125               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5126                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5127                                           (breg == 0
5128                                            ? mips_opts.warn_about_macros
5129                                            : 0)),
5130                             offset_expr.X_add_symbol, 0, NULL);
5131             }
5132           else
5133             {
5134               int adj, dreg;
5135
5136               /* If we are going to add in a base register, and the
5137                  target register and the base register are the same,
5138                  then we are using AT as a temporary register.  Since
5139                  we want to load the constant into AT, we add our
5140                  current AT (from the global offset table) and the
5141                  register into the register now, and pretend we were
5142                  not using a base register.  */
5143               if (breg != treg)
5144                 {
5145                   adj = 0;
5146                   dreg = tempreg;
5147                 }
5148               else
5149                 {
5150                   assert (tempreg == AT);
5151                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5152                                "nop", "");
5153                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5154                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5155                                "d,v,t", treg, AT, breg);
5156                   dreg = treg;
5157                   adj = 8;
5158                 }
5159
5160               /* Set mips_optimize around the lui instruction to avoid
5161                  inserting an unnecessary nop after the lw.  */
5162               hold_mips_optimize = mips_optimize;
5163               mips_optimize = 2;
5164               macro_build_lui (NULL, &icnt, &expr1, AT);
5165               mips_optimize = hold_mips_optimize;
5166
5167               macro_build ((char *) NULL, &icnt, &expr1,
5168                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5169                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5170               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5171                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5172                            "d,v,t", dreg, dreg, AT);
5173
5174               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5175                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5176                                           8 + gpdel, 0,
5177                                           (breg == 0
5178                                            ? mips_opts.warn_about_macros
5179                                            : 0)),
5180                             offset_expr.X_add_symbol, 0, NULL);
5181
5182               used_at = 1;
5183             }
5184
5185           if (gpdel > 0)
5186             {
5187               /* This is needed because this instruction uses $gp, but
5188                  the first instruction on the main stream does not.  */
5189               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5190               p += 4;
5191             }
5192
5193           if (HAVE_NEWABI)
5194             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5195           macro_build (p, &icnt, &offset_expr,
5196                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5197                        "t,o(b)", tempreg,
5198                        local_reloc_type,
5199                        mips_gp_register);
5200           p += 4;
5201           if (expr1.X_add_number == 0 && HAVE_NEWABI)
5202             {
5203               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5204             }
5205          else
5206            if (expr1.X_add_number >= -0x8000
5207               && expr1.X_add_number < 0x8000)
5208             {
5209               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5210               p += 4;
5211               macro_build (p, &icnt, &expr1,
5212                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5213                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5214               /* FIXME: If add_number is 0, and there was no base
5215                  register, the external symbol case ended with a load,
5216                  so if the symbol turns out to not be external, and
5217                  the next instruction uses tempreg, an unnecessary nop
5218                  will be inserted.  */
5219             }
5220           else
5221             {
5222               if (breg == treg)
5223                 {
5224                   /* We must add in the base register now, as in the
5225                      external symbol case.  */
5226                   assert (tempreg == AT);
5227                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5228                   p += 4;
5229                   macro_build (p, &icnt, (expressionS *) NULL,
5230                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5231                                "d,v,t", treg, AT, breg);
5232                   p += 4;
5233                   tempreg = treg;
5234                   /* We set breg to 0 because we have arranged to add
5235                      it in in both cases.  */
5236                   breg = 0;
5237                 }
5238
5239               macro_build_lui (p, &icnt, &expr1, AT);
5240               p += 4;
5241               macro_build (p, &icnt, &expr1,
5242                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5243                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5244               p += 4;
5245               macro_build (p, &icnt, (expressionS *) NULL,
5246                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5247                            "d,v,t", tempreg, tempreg, AT);
5248               p += 4;
5249             }
5250         }
5251       else if (mips_pic == EMBEDDED_PIC)
5252         {
5253           /* We use
5254                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5255              */
5256           macro_build ((char *) NULL, &icnt, &offset_expr,
5257                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5258                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5259         }
5260       else
5261         abort ();
5262
5263       if (breg != 0)
5264         {
5265           char *s;
5266
5267           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5268             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5269           else
5270             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5271
5272           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5273                        "d,v,t", treg, tempreg, breg);
5274         }
5275
5276       if (! used_at)
5277         return;
5278
5279       break;
5280
5281     case M_J_A:
5282       /* The j instruction may not be used in PIC code, since it
5283          requires an absolute address.  We convert it to a b
5284          instruction.  */
5285       if (mips_pic == NO_PIC)
5286         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5287       else
5288         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5289       return;
5290
5291       /* The jal instructions must be handled as macros because when
5292          generating PIC code they expand to multi-instruction
5293          sequences.  Normally they are simple instructions.  */
5294     case M_JAL_1:
5295       dreg = RA;
5296       /* Fall through.  */
5297     case M_JAL_2:
5298       if (mips_pic == NO_PIC
5299           || mips_pic == EMBEDDED_PIC)
5300         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5301                      "d,s", dreg, sreg);
5302       else if (mips_pic == SVR4_PIC)
5303         {
5304           if (sreg != PIC_CALL_REG)
5305             as_warn (_("MIPS PIC call to register other than $25"));
5306
5307           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5308                        "d,s", dreg, sreg);
5309           if (! HAVE_NEWABI)
5310             {
5311               if (mips_cprestore_offset < 0)
5312                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5313               else
5314                 {
5315                   if (! mips_frame_reg_valid)
5316                     {
5317                       as_warn (_("No .frame pseudo-op used in PIC code"));
5318                       /* Quiet this warning.  */
5319                       mips_frame_reg_valid = 1;
5320                     }
5321                   if (! mips_cprestore_valid)
5322                     {
5323                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5324                       /* Quiet this warning.  */
5325                       mips_cprestore_valid = 1;
5326                     }
5327                   expr1.X_add_number = mips_cprestore_offset;
5328                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5329                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5330                                                 mips_gp_register, mips_frame_reg);
5331                 }
5332             }
5333         }
5334       else
5335         abort ();
5336
5337       return;
5338
5339     case M_JAL_A:
5340       if (mips_pic == NO_PIC)
5341         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5342       else if (mips_pic == SVR4_PIC)
5343         {
5344           char *p;
5345
5346           /* If this is a reference to an external symbol, and we are
5347              using a small GOT, we want
5348                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5349                nop
5350                jalr     $ra,$25
5351                nop
5352                lw       $gp,cprestore($sp)
5353              The cprestore value is set using the .cprestore
5354              pseudo-op.  If we are using a big GOT, we want
5355                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5356                addu     $25,$25,$gp
5357                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5358                nop
5359                jalr     $ra,$25
5360                nop
5361                lw       $gp,cprestore($sp)
5362              If the symbol is not external, we want
5363                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5364                nop
5365                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5366                jalr     $ra,$25
5367                nop
5368                lw $gp,cprestore($sp)
5369              For NewABI, we want
5370                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5371                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5372            */
5373           if (HAVE_NEWABI)
5374             {
5375               macro_build ((char *) NULL, &icnt, &offset_expr,
5376                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5377                            "t,o(b)", PIC_CALL_REG,
5378                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5379               macro_build_jalr (icnt, &offset_expr);
5380             }
5381           else
5382             {
5383               frag_grow (40);
5384               if (! mips_big_got)
5385                 {
5386                   macro_build ((char *) NULL, &icnt, &offset_expr,
5387                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5388                                "t,o(b)", PIC_CALL_REG,
5389                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5390                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5391                                "nop", "");
5392                   p = frag_var (rs_machine_dependent, 4, 0,
5393                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5394                                 offset_expr.X_add_symbol, 0, NULL);
5395                 }
5396               else
5397                 {
5398                   int gpdel;
5399
5400                   if (reg_needs_delay (mips_gp_register))
5401                     gpdel = 4;
5402                   else
5403                     gpdel = 0;
5404                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5405                                "t,u", PIC_CALL_REG,
5406                                (int) BFD_RELOC_MIPS_CALL_HI16);
5407                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5408                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5409                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5410                                mips_gp_register);
5411                   macro_build ((char *) NULL, &icnt, &offset_expr,
5412                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5413                                "t,o(b)", PIC_CALL_REG,
5414                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5415                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5416                                "nop", "");
5417                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5418                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5419                                               8 + gpdel, 0, 0),
5420                                 offset_expr.X_add_symbol, 0, NULL);
5421                   if (gpdel > 0)
5422                     {
5423                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5424                       p += 4;
5425                     }
5426                   macro_build (p, &icnt, &offset_expr,
5427                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5428                                "t,o(b)", PIC_CALL_REG,
5429                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5430                   p += 4;
5431                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5432                   p += 4;
5433                 }
5434               macro_build (p, &icnt, &offset_expr,
5435                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5436                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5437                            (int) BFD_RELOC_LO16);
5438               macro_build_jalr (icnt, &offset_expr);
5439
5440               if (mips_cprestore_offset < 0)
5441                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5442               else
5443                 {
5444                   if (! mips_frame_reg_valid)
5445                     {
5446                       as_warn (_("No .frame pseudo-op used in PIC code"));
5447                       /* Quiet this warning.  */
5448                       mips_frame_reg_valid = 1;
5449                     }
5450                   if (! mips_cprestore_valid)
5451                     {
5452                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5453                       /* Quiet this warning.  */
5454                       mips_cprestore_valid = 1;
5455                     }
5456                   if (mips_opts.noreorder)
5457                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5458                                  "nop", "");
5459                   expr1.X_add_number = mips_cprestore_offset;
5460                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5461                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5462                                                 mips_gp_register, mips_frame_reg);
5463                 }
5464             }
5465         }
5466       else if (mips_pic == EMBEDDED_PIC)
5467         {
5468           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5469           /* The linker may expand the call to a longer sequence which
5470              uses $at, so we must break rather than return.  */
5471           break;
5472         }
5473       else
5474         abort ();
5475
5476       return;
5477
5478     case M_LB_AB:
5479       s = "lb";
5480       goto ld;
5481     case M_LBU_AB:
5482       s = "lbu";
5483       goto ld;
5484     case M_LH_AB:
5485       s = "lh";
5486       goto ld;
5487     case M_LHU_AB:
5488       s = "lhu";
5489       goto ld;
5490     case M_LW_AB:
5491       s = "lw";
5492       goto ld;
5493     case M_LWC0_AB:
5494       s = "lwc0";
5495       /* Itbl support may require additional care here.  */
5496       coproc = 1;
5497       goto ld;
5498     case M_LWC1_AB:
5499       s = "lwc1";
5500       /* Itbl support may require additional care here.  */
5501       coproc = 1;
5502       goto ld;
5503     case M_LWC2_AB:
5504       s = "lwc2";
5505       /* Itbl support may require additional care here.  */
5506       coproc = 1;
5507       goto ld;
5508     case M_LWC3_AB:
5509       s = "lwc3";
5510       /* Itbl support may require additional care here.  */
5511       coproc = 1;
5512       goto ld;
5513     case M_LWL_AB:
5514       s = "lwl";
5515       lr = 1;
5516       goto ld;
5517     case M_LWR_AB:
5518       s = "lwr";
5519       lr = 1;
5520       goto ld;
5521     case M_LDC1_AB:
5522       if (mips_arch == CPU_R4650)
5523         {
5524           as_bad (_("opcode not supported on this processor"));
5525           return;
5526         }
5527       s = "ldc1";
5528       /* Itbl support may require additional care here.  */
5529       coproc = 1;
5530       goto ld;
5531     case M_LDC2_AB:
5532       s = "ldc2";
5533       /* Itbl support may require additional care here.  */
5534       coproc = 1;
5535       goto ld;
5536     case M_LDC3_AB:
5537       s = "ldc3";
5538       /* Itbl support may require additional care here.  */
5539       coproc = 1;
5540       goto ld;
5541     case M_LDL_AB:
5542       s = "ldl";
5543       lr = 1;
5544       goto ld;
5545     case M_LDR_AB:
5546       s = "ldr";
5547       lr = 1;
5548       goto ld;
5549     case M_LL_AB:
5550       s = "ll";
5551       goto ld;
5552     case M_LLD_AB:
5553       s = "lld";
5554       goto ld;
5555     case M_LWU_AB:
5556       s = "lwu";
5557     ld:
5558       if (breg == treg || coproc || lr)
5559         {
5560           tempreg = AT;
5561           used_at = 1;
5562         }
5563       else
5564         {
5565           tempreg = treg;
5566           used_at = 0;
5567         }
5568       goto ld_st;
5569     case M_SB_AB:
5570       s = "sb";
5571       goto st;
5572     case M_SH_AB:
5573       s = "sh";
5574       goto st;
5575     case M_SW_AB:
5576       s = "sw";
5577       goto st;
5578     case M_SWC0_AB:
5579       s = "swc0";
5580       /* Itbl support may require additional care here.  */
5581       coproc = 1;
5582       goto st;
5583     case M_SWC1_AB:
5584       s = "swc1";
5585       /* Itbl support may require additional care here.  */
5586       coproc = 1;
5587       goto st;
5588     case M_SWC2_AB:
5589       s = "swc2";
5590       /* Itbl support may require additional care here.  */
5591       coproc = 1;
5592       goto st;
5593     case M_SWC3_AB:
5594       s = "swc3";
5595       /* Itbl support may require additional care here.  */
5596       coproc = 1;
5597       goto st;
5598     case M_SWL_AB:
5599       s = "swl";
5600       goto st;
5601     case M_SWR_AB:
5602       s = "swr";
5603       goto st;
5604     case M_SC_AB:
5605       s = "sc";
5606       goto st;
5607     case M_SCD_AB:
5608       s = "scd";
5609       goto st;
5610     case M_SDC1_AB:
5611       if (mips_arch == CPU_R4650)
5612         {
5613           as_bad (_("opcode not supported on this processor"));
5614           return;
5615         }
5616       s = "sdc1";
5617       coproc = 1;
5618       /* Itbl support may require additional care here.  */
5619       goto st;
5620     case M_SDC2_AB:
5621       s = "sdc2";
5622       /* Itbl support may require additional care here.  */
5623       coproc = 1;
5624       goto st;
5625     case M_SDC3_AB:
5626       s = "sdc3";
5627       /* Itbl support may require additional care here.  */
5628       coproc = 1;
5629       goto st;
5630     case M_SDL_AB:
5631       s = "sdl";
5632       goto st;
5633     case M_SDR_AB:
5634       s = "sdr";
5635     st:
5636       tempreg = AT;
5637       used_at = 1;
5638     ld_st:
5639       /* Itbl support may require additional care here.  */
5640       if (mask == M_LWC1_AB
5641           || mask == M_SWC1_AB
5642           || mask == M_LDC1_AB
5643           || mask == M_SDC1_AB
5644           || mask == M_L_DAB
5645           || mask == M_S_DAB)
5646         fmt = "T,o(b)";
5647       else if (coproc)
5648         fmt = "E,o(b)";
5649       else
5650         fmt = "t,o(b)";
5651
5652       /* For embedded PIC, we allow loads where the offset is calculated
5653          by subtracting a symbol in the current segment from an unknown
5654          symbol, relative to a base register, e.g.:
5655                 <op>    $treg, <sym>-<localsym>($breg)
5656          This is used by the compiler for switch statements.  */
5657       if (mips_pic == EMBEDDED_PIC
5658           && offset_expr.X_op == O_subtract
5659           && (symbol_constant_p (offset_expr.X_op_symbol)
5660               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5661               : (symbol_equated_p (offset_expr.X_op_symbol)
5662                  && (S_GET_SEGMENT
5663                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5664                       ->X_add_symbol)
5665                      == now_seg)))
5666           && breg != 0
5667           && (offset_expr.X_add_number == 0
5668               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5669         {
5670           /* For this case, we output the instructions:
5671                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5672                 addiu   $tempreg,$tempreg,$breg
5673                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5674              If the relocation would fit entirely in 16 bits, it would be
5675              nice to emit:
5676                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5677              instead, but that seems quite difficult.  */
5678           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5679                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5680           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5681                        ((bfd_arch_bits_per_address (stdoutput) == 32
5682                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5683                         ? "addu" : "daddu"),
5684                        "d,v,t", tempreg, tempreg, breg);
5685           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5686                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5687           if (! used_at)
5688             return;
5689           break;
5690         }
5691
5692       if (offset_expr.X_op != O_constant
5693           && offset_expr.X_op != O_symbol)
5694         {
5695           as_bad (_("expression too complex"));
5696           offset_expr.X_op = O_constant;
5697         }
5698
5699       /* A constant expression in PIC code can be handled just as it
5700          is in non PIC code.  */
5701       if (mips_pic == NO_PIC
5702           || offset_expr.X_op == O_constant)
5703         {
5704           char *p;
5705
5706           /* If this is a reference to a GP relative symbol, and there
5707              is no base register, we want
5708                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5709              Otherwise, if there is no base register, we want
5710                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5711                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5712              If we have a constant, we need two instructions anyhow,
5713              so we always use the latter form.
5714
5715              If we have a base register, and this is a reference to a
5716              GP relative symbol, we want
5717                addu     $tempreg,$breg,$gp
5718                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5719              Otherwise we want
5720                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5721                addu     $tempreg,$tempreg,$breg
5722                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5723              With a constant we always use the latter case.
5724
5725              With 64bit address space and no base register and $at usable,
5726              we want
5727                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5728                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5729                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5730                dsll32   $tempreg,0
5731                daddu    $tempreg,$at
5732                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5733              If we have a base register, we want
5734                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5735                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5736                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5737                daddu    $at,$breg
5738                dsll32   $tempreg,0
5739                daddu    $tempreg,$at
5740                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5741
5742              Without $at we can't generate the optimal path for superscalar
5743              processors here since this would require two temporary registers.
5744                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5745                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5746                dsll     $tempreg,16
5747                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5748                dsll     $tempreg,16
5749                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5750              If we have a base register, we want
5751                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5752                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5753                dsll     $tempreg,16
5754                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5755                dsll     $tempreg,16
5756                daddu    $tempreg,$tempreg,$breg
5757                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5758
5759              If we have 64-bit addresses, as an optimization, for
5760              addresses which are 32-bit constants (e.g. kseg0/kseg1
5761              addresses) we fall back to the 32-bit address generation
5762              mechanism since it is more efficient.  Note that due to
5763              the signed offset used by memory operations, the 32-bit
5764              range is shifted down by 32768 here.  This code should
5765              probably attempt to generate 64-bit constants more
5766              efficiently in general.
5767            */
5768           if (HAVE_64BIT_ADDRESSES
5769               && !(offset_expr.X_op == O_constant
5770                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5771             {
5772               p = NULL;
5773
5774               /* We don't do GP optimization for now because RELAX_ENCODE can't
5775                  hold the data for such large chunks.  */
5776
5777               if (used_at == 0 && ! mips_opts.noat)
5778                 {
5779                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5780                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5781                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5782                                AT, (int) BFD_RELOC_HI16_S);
5783                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5784                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5785                   if (breg != 0)
5786                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5787                                  "d,v,t", AT, AT, breg);
5788                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5789                                "d,w,<", tempreg, tempreg, 0);
5790                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5791                                "d,v,t", tempreg, tempreg, AT);
5792                   macro_build (p, &icnt, &offset_expr, s,
5793                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5794                   used_at = 1;
5795                 }
5796               else
5797                 {
5798                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5799                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5800                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5801                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5802                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5803                                "d,w,<", tempreg, tempreg, 16);
5804                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5805                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5806                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5807                                "d,w,<", tempreg, tempreg, 16);
5808                   if (breg != 0)
5809                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5810                                  "d,v,t", tempreg, tempreg, breg);
5811                   macro_build (p, &icnt, &offset_expr, s,
5812                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5813                 }
5814
5815               return;
5816             }
5817
5818           if (breg == 0)
5819             {
5820               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5821                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5822                 p = NULL;
5823               else
5824                 {
5825                   frag_grow (20);
5826                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5827                                treg, (int) BFD_RELOC_GPREL16,
5828                                mips_gp_register);
5829                   p = frag_var (rs_machine_dependent, 8, 0,
5830                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5831                                               (mips_opts.warn_about_macros
5832                                                || (used_at
5833                                                    && mips_opts.noat))),
5834                                 offset_expr.X_add_symbol, 0, NULL);
5835                   used_at = 0;
5836                 }
5837               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5838               if (p != NULL)
5839                 p += 4;
5840               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5841                            (int) BFD_RELOC_LO16, tempreg);
5842             }
5843           else
5844             {
5845               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5846                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5847                 p = NULL;
5848               else
5849                 {
5850                   frag_grow (28);
5851                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5852                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5853                                "d,v,t", tempreg, breg, mips_gp_register);
5854                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5855                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5856                   p = frag_var (rs_machine_dependent, 12, 0,
5857                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5858                                 offset_expr.X_add_symbol, 0, NULL);
5859                 }
5860               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5861               if (p != NULL)
5862                 p += 4;
5863               macro_build (p, &icnt, (expressionS *) NULL,
5864                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5865                            "d,v,t", tempreg, tempreg, breg);
5866               if (p != NULL)
5867                 p += 4;
5868               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5869                            (int) BFD_RELOC_LO16, tempreg);
5870             }
5871         }
5872       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5873         {
5874           char *p;
5875           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5876
5877           /* If this is a reference to an external symbol, we want
5878                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5879                nop
5880                <op>     $treg,0($tempreg)
5881              Otherwise we want
5882                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5883                nop
5884                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5885                <op>     $treg,0($tempreg)
5886              If we have NewABI, we want
5887                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5888              If there is a base register, we add it to $tempreg before
5889              the <op>.  If there is a constant, we stick it in the
5890              <op> instruction.  We don't handle constants larger than
5891              16 bits, because we have no way to load the upper 16 bits
5892              (actually, we could handle them for the subset of cases
5893              in which we are not using $at).  */
5894           assert (offset_expr.X_op == O_symbol);
5895           expr1.X_add_number = offset_expr.X_add_number;
5896           offset_expr.X_add_number = 0;
5897           if (HAVE_NEWABI)
5898             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5899           if (expr1.X_add_number < -0x8000
5900               || expr1.X_add_number >= 0x8000)
5901             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5902           frag_grow (20);
5903           macro_build ((char *) NULL, &icnt, &offset_expr,
5904                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5905                        (int) lw_reloc_type, mips_gp_register);
5906           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5907           p = frag_var (rs_machine_dependent, 4, 0,
5908                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5909                         offset_expr.X_add_symbol, 0, NULL);
5910           macro_build (p, &icnt, &offset_expr,
5911                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5912                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5913           if (breg != 0)
5914             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5915                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5916                          "d,v,t", tempreg, tempreg, breg);
5917           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5918                        (int) BFD_RELOC_LO16, tempreg);
5919         }
5920       else if (mips_pic == SVR4_PIC)
5921         {
5922           int gpdel;
5923           char *p;
5924
5925           /* If this is a reference to an external symbol, we want
5926                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5927                addu     $tempreg,$tempreg,$gp
5928                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5929                <op>     $treg,0($tempreg)
5930              Otherwise we want
5931                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5932                nop
5933                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5934                <op>     $treg,0($tempreg)
5935              If there is a base register, we add it to $tempreg before
5936              the <op>.  If there is a constant, we stick it in the
5937              <op> instruction.  We don't handle constants larger than
5938              16 bits, because we have no way to load the upper 16 bits
5939              (actually, we could handle them for the subset of cases
5940              in which we are not using $at).
5941
5942              For NewABI, we want
5943                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5944                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5945                <op>     $treg,0($tempreg)
5946            */
5947           assert (offset_expr.X_op == O_symbol);
5948           expr1.X_add_number = offset_expr.X_add_number;
5949           offset_expr.X_add_number = 0;
5950           if (expr1.X_add_number < -0x8000
5951               || expr1.X_add_number >= 0x8000)
5952             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5953           if (HAVE_NEWABI)
5954             {
5955               macro_build ((char *) NULL, &icnt, &offset_expr,
5956                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5957                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5958                            mips_gp_register);
5959               macro_build ((char *) NULL, &icnt, &offset_expr,
5960                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5961                            "t,r,j", tempreg, tempreg,
5962                            BFD_RELOC_MIPS_GOT_OFST);
5963               if (breg != 0)
5964                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5965                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5966                              "d,v,t", tempreg, tempreg, breg);
5967               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5968                            (int) BFD_RELOC_LO16, tempreg);
5969
5970               if (! used_at)
5971                 return;
5972
5973               break;
5974             }
5975           if (reg_needs_delay (mips_gp_register))
5976             gpdel = 4;
5977           else
5978             gpdel = 0;
5979           frag_grow (36);
5980           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5981                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5982           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5983                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5984                        "d,v,t", tempreg, tempreg, mips_gp_register);
5985           macro_build ((char *) NULL, &icnt, &offset_expr,
5986                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5987                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5988                        tempreg);
5989           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5990                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5991                         offset_expr.X_add_symbol, 0, NULL);
5992           if (gpdel > 0)
5993             {
5994               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5995               p += 4;
5996             }
5997           macro_build (p, &icnt, &offset_expr,
5998                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5999                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6000                        mips_gp_register);
6001           p += 4;
6002           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6003           p += 4;
6004           macro_build (p, &icnt, &offset_expr,
6005                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6006                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6007           if (breg != 0)
6008             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6009                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6010                          "d,v,t", tempreg, tempreg, breg);
6011           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6012                        (int) BFD_RELOC_LO16, tempreg);
6013         }
6014       else if (mips_pic == EMBEDDED_PIC)
6015         {
6016           /* If there is no base register, we want
6017                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6018              If there is a base register, we want
6019                addu     $tempreg,$breg,$gp
6020                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6021              */
6022           assert (offset_expr.X_op == O_symbol);
6023           if (breg == 0)
6024             {
6025               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6026                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6027               used_at = 0;
6028             }
6029           else
6030             {
6031               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6032                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6033                            "d,v,t", tempreg, breg, mips_gp_register);
6034               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6035                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6036             }
6037         }
6038       else
6039         abort ();
6040
6041       if (! used_at)
6042         return;
6043
6044       break;
6045
6046     case M_LI:
6047     case M_LI_S:
6048       load_register (&icnt, treg, &imm_expr, 0);
6049       return;
6050
6051     case M_DLI:
6052       load_register (&icnt, treg, &imm_expr, 1);
6053       return;
6054
6055     case M_LI_SS:
6056       if (imm_expr.X_op == O_constant)
6057         {
6058           load_register (&icnt, AT, &imm_expr, 0);
6059           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6060                        "mtc1", "t,G", AT, treg);
6061           break;
6062         }
6063       else
6064         {
6065           assert (offset_expr.X_op == O_symbol
6066                   && strcmp (segment_name (S_GET_SEGMENT
6067                                            (offset_expr.X_add_symbol)),
6068                              ".lit4") == 0
6069                   && offset_expr.X_add_number == 0);
6070           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6071                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6072           return;
6073         }
6074
6075     case M_LI_D:
6076       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6077          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6078          order 32 bits of the value and the low order 32 bits are either
6079          zero or in OFFSET_EXPR.  */
6080       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6081         {
6082           if (HAVE_64BIT_GPRS)
6083             load_register (&icnt, treg, &imm_expr, 1);
6084           else
6085             {
6086               int hreg, lreg;
6087
6088               if (target_big_endian)
6089                 {
6090                   hreg = treg;
6091                   lreg = treg + 1;
6092                 }
6093               else
6094                 {
6095                   hreg = treg + 1;
6096                   lreg = treg;
6097                 }
6098
6099               if (hreg <= 31)
6100                 load_register (&icnt, hreg, &imm_expr, 0);
6101               if (lreg <= 31)
6102                 {
6103                   if (offset_expr.X_op == O_absent)
6104                     move_register (&icnt, lreg, 0);
6105                   else
6106                     {
6107                       assert (offset_expr.X_op == O_constant);
6108                       load_register (&icnt, lreg, &offset_expr, 0);
6109                     }
6110                 }
6111             }
6112           return;
6113         }
6114
6115       /* We know that sym is in the .rdata section.  First we get the
6116          upper 16 bits of the address.  */
6117       if (mips_pic == NO_PIC)
6118         {
6119           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6120         }
6121       else if (mips_pic == SVR4_PIC)
6122         {
6123           macro_build ((char *) NULL, &icnt, &offset_expr,
6124                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6125                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6126                        mips_gp_register);
6127         }
6128       else if (mips_pic == EMBEDDED_PIC)
6129         {
6130           /* For embedded PIC we pick up the entire address off $gp in
6131              a single instruction.  */
6132           macro_build ((char *) NULL, &icnt, &offset_expr,
6133                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6134                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6135           offset_expr.X_op = O_constant;
6136           offset_expr.X_add_number = 0;
6137         }
6138       else
6139         abort ();
6140
6141       /* Now we load the register(s).  */
6142       if (HAVE_64BIT_GPRS)
6143         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6144                      treg, (int) BFD_RELOC_LO16, AT);
6145       else
6146         {
6147           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6148                        treg, (int) BFD_RELOC_LO16, AT);
6149           if (treg != RA)
6150             {
6151               /* FIXME: How in the world do we deal with the possible
6152                  overflow here?  */
6153               offset_expr.X_add_number += 4;
6154               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6155                            treg + 1, (int) BFD_RELOC_LO16, AT);
6156             }
6157         }
6158
6159       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6160          does not become a variant frag.  */
6161       frag_wane (frag_now);
6162       frag_new (0);
6163
6164       break;
6165
6166     case M_LI_DD:
6167       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6168          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6169          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6170          the value and the low order 32 bits are either zero or in
6171          OFFSET_EXPR.  */
6172       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6173         {
6174           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6175           if (HAVE_64BIT_FPRS)
6176             {
6177               assert (HAVE_64BIT_GPRS);
6178               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6179                            "dmtc1", "t,S", AT, treg);
6180             }
6181           else
6182             {
6183               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6184                            "mtc1", "t,G", AT, treg + 1);
6185               if (offset_expr.X_op == O_absent)
6186                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6187                              "mtc1", "t,G", 0, treg);
6188               else
6189                 {
6190                   assert (offset_expr.X_op == O_constant);
6191                   load_register (&icnt, AT, &offset_expr, 0);
6192                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6193                                "mtc1", "t,G", AT, treg);
6194                 }
6195             }
6196           break;
6197         }
6198
6199       assert (offset_expr.X_op == O_symbol
6200               && offset_expr.X_add_number == 0);
6201       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6202       if (strcmp (s, ".lit8") == 0)
6203         {
6204           if (mips_opts.isa != ISA_MIPS1)
6205             {
6206               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6207                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6208                            mips_gp_register);
6209               return;
6210             }
6211           breg = mips_gp_register;
6212           r = BFD_RELOC_MIPS_LITERAL;
6213           goto dob;
6214         }
6215       else
6216         {
6217           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6218           if (mips_pic == SVR4_PIC)
6219             macro_build ((char *) NULL, &icnt, &offset_expr,
6220                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6221                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6222                          mips_gp_register);
6223           else
6224             {
6225               /* FIXME: This won't work for a 64 bit address.  */
6226               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6227             }
6228
6229           if (mips_opts.isa != ISA_MIPS1)
6230             {
6231               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6232                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6233
6234               /* To avoid confusion in tc_gen_reloc, we must ensure
6235                  that this does not become a variant frag.  */
6236               frag_wane (frag_now);
6237               frag_new (0);
6238
6239               break;
6240             }
6241           breg = AT;
6242           r = BFD_RELOC_LO16;
6243           goto dob;
6244         }
6245
6246     case M_L_DOB:
6247       if (mips_arch == CPU_R4650)
6248         {
6249           as_bad (_("opcode not supported on this processor"));
6250           return;
6251         }
6252       /* Even on a big endian machine $fn comes before $fn+1.  We have
6253          to adjust when loading from memory.  */
6254       r = BFD_RELOC_LO16;
6255     dob:
6256       assert (mips_opts.isa == ISA_MIPS1);
6257       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6258                    target_big_endian ? treg + 1 : treg,
6259                    (int) r, breg);
6260       /* FIXME: A possible overflow which I don't know how to deal
6261          with.  */
6262       offset_expr.X_add_number += 4;
6263       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6264                    target_big_endian ? treg : treg + 1,
6265                    (int) r, breg);
6266
6267       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6268          does not become a variant frag.  */
6269       frag_wane (frag_now);
6270       frag_new (0);
6271
6272       if (breg != AT)
6273         return;
6274       break;
6275
6276     case M_L_DAB:
6277       /*
6278        * The MIPS assembler seems to check for X_add_number not
6279        * being double aligned and generating:
6280        *        lui     at,%hi(foo+1)
6281        *        addu    at,at,v1
6282        *        addiu   at,at,%lo(foo+1)
6283        *        lwc1    f2,0(at)
6284        *        lwc1    f3,4(at)
6285        * But, the resulting address is the same after relocation so why
6286        * generate the extra instruction?
6287        */
6288       if (mips_arch == CPU_R4650)
6289         {
6290           as_bad (_("opcode not supported on this processor"));
6291           return;
6292         }
6293       /* Itbl support may require additional care here.  */
6294       coproc = 1;
6295       if (mips_opts.isa != ISA_MIPS1)
6296         {
6297           s = "ldc1";
6298           goto ld;
6299         }
6300
6301       s = "lwc1";
6302       fmt = "T,o(b)";
6303       goto ldd_std;
6304
6305     case M_S_DAB:
6306       if (mips_arch == CPU_R4650)
6307         {
6308           as_bad (_("opcode not supported on this processor"));
6309           return;
6310         }
6311
6312       if (mips_opts.isa != ISA_MIPS1)
6313         {
6314           s = "sdc1";
6315           goto st;
6316         }
6317
6318       s = "swc1";
6319       fmt = "T,o(b)";
6320       /* Itbl support may require additional care here.  */
6321       coproc = 1;
6322       goto ldd_std;
6323
6324     case M_LD_AB:
6325       if (HAVE_64BIT_GPRS)
6326         {
6327           s = "ld";
6328           goto ld;
6329         }
6330
6331       s = "lw";
6332       fmt = "t,o(b)";
6333       goto ldd_std;
6334
6335     case M_SD_AB:
6336       if (HAVE_64BIT_GPRS)
6337         {
6338           s = "sd";
6339           goto st;
6340         }
6341
6342       s = "sw";
6343       fmt = "t,o(b)";
6344
6345     ldd_std:
6346       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6347          loads for the case of doing a pair of loads to simulate an 'ld'.
6348          This is not currently done by the compiler, and assembly coders
6349          writing embedded-pic code can cope.  */
6350
6351       if (offset_expr.X_op != O_symbol
6352           && offset_expr.X_op != O_constant)
6353         {
6354           as_bad (_("expression too complex"));
6355           offset_expr.X_op = O_constant;
6356         }
6357
6358       /* Even on a big endian machine $fn comes before $fn+1.  We have
6359          to adjust when loading from memory.  We set coproc if we must
6360          load $fn+1 first.  */
6361       /* Itbl support may require additional care here.  */
6362       if (! target_big_endian)
6363         coproc = 0;
6364
6365       if (mips_pic == NO_PIC
6366           || offset_expr.X_op == O_constant)
6367         {
6368           char *p;
6369
6370           /* If this is a reference to a GP relative symbol, we want
6371                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6372                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6373              If we have a base register, we use this
6374                addu     $at,$breg,$gp
6375                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6376                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6377              If this is not a GP relative symbol, we want
6378                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6379                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6380                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6381              If there is a base register, we add it to $at after the
6382              lui instruction.  If there is a constant, we always use
6383              the last case.  */
6384           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6385               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6386             {
6387               p = NULL;
6388               used_at = 1;
6389             }
6390           else
6391             {
6392               int off;
6393
6394               if (breg == 0)
6395                 {
6396                   frag_grow (28);
6397                   tempreg = mips_gp_register;
6398                   off = 0;
6399                   used_at = 0;
6400                 }
6401               else
6402                 {
6403                   frag_grow (36);
6404                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6405                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6406                                "d,v,t", AT, breg, mips_gp_register);
6407                   tempreg = AT;
6408                   off = 4;
6409                   used_at = 1;
6410                 }
6411
6412               /* Itbl support may require additional care here.  */
6413               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6414                            coproc ? treg + 1 : treg,
6415                            (int) BFD_RELOC_GPREL16, tempreg);
6416               offset_expr.X_add_number += 4;
6417
6418               /* Set mips_optimize to 2 to avoid inserting an
6419                  undesired nop.  */
6420               hold_mips_optimize = mips_optimize;
6421               mips_optimize = 2;
6422               /* Itbl support may require additional care here.  */
6423               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6424                            coproc ? treg : treg + 1,
6425                            (int) BFD_RELOC_GPREL16, tempreg);
6426               mips_optimize = hold_mips_optimize;
6427
6428               p = frag_var (rs_machine_dependent, 12 + off, 0,
6429                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6430                                           used_at && mips_opts.noat),
6431                             offset_expr.X_add_symbol, 0, NULL);
6432
6433               /* We just generated two relocs.  When tc_gen_reloc
6434                  handles this case, it will skip the first reloc and
6435                  handle the second.  The second reloc already has an
6436                  extra addend of 4, which we added above.  We must
6437                  subtract it out, and then subtract another 4 to make
6438                  the first reloc come out right.  The second reloc
6439                  will come out right because we are going to add 4 to
6440                  offset_expr when we build its instruction below.
6441
6442                  If we have a symbol, then we don't want to include
6443                  the offset, because it will wind up being included
6444                  when we generate the reloc.  */
6445
6446               if (offset_expr.X_op == O_constant)
6447                 offset_expr.X_add_number -= 8;
6448               else
6449                 {
6450                   offset_expr.X_add_number = -4;
6451                   offset_expr.X_op = O_constant;
6452                 }
6453             }
6454           macro_build_lui (p, &icnt, &offset_expr, AT);
6455           if (p != NULL)
6456             p += 4;
6457           if (breg != 0)
6458             {
6459               macro_build (p, &icnt, (expressionS *) NULL,
6460                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6461                            "d,v,t", AT, breg, AT);
6462               if (p != NULL)
6463                 p += 4;
6464             }
6465           /* Itbl support may require additional care here.  */
6466           macro_build (p, &icnt, &offset_expr, s, fmt,
6467                        coproc ? treg + 1 : treg,
6468                        (int) BFD_RELOC_LO16, AT);
6469           if (p != NULL)
6470             p += 4;
6471           /* FIXME: How do we handle overflow here?  */
6472           offset_expr.X_add_number += 4;
6473           /* Itbl support may require additional care here.  */
6474           macro_build (p, &icnt, &offset_expr, s, fmt,
6475                        coproc ? treg : treg + 1,
6476                        (int) BFD_RELOC_LO16, AT);
6477         }
6478       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6479         {
6480           int off;
6481
6482           /* If this is a reference to an external symbol, we want
6483                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6484                nop
6485                <op>     $treg,0($at)
6486                <op>     $treg+1,4($at)
6487              Otherwise we want
6488                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6489                nop
6490                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6491                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6492              If there is a base register we add it to $at before the
6493              lwc1 instructions.  If there is a constant we include it
6494              in the lwc1 instructions.  */
6495           used_at = 1;
6496           expr1.X_add_number = offset_expr.X_add_number;
6497           offset_expr.X_add_number = 0;
6498           if (expr1.X_add_number < -0x8000
6499               || expr1.X_add_number >= 0x8000 - 4)
6500             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6501           if (breg == 0)
6502             off = 0;
6503           else
6504             off = 4;
6505           frag_grow (24 + off);
6506           macro_build ((char *) NULL, &icnt, &offset_expr,
6507                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6508                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6509           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6510           if (breg != 0)
6511             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6512                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6513                          "d,v,t", AT, breg, AT);
6514           /* Itbl support may require additional care here.  */
6515           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6516                        coproc ? treg + 1 : treg,
6517                        (int) BFD_RELOC_LO16, AT);
6518           expr1.X_add_number += 4;
6519
6520           /* Set mips_optimize to 2 to avoid inserting an undesired
6521              nop.  */
6522           hold_mips_optimize = mips_optimize;
6523           mips_optimize = 2;
6524           /* Itbl support may require additional care here.  */
6525           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6526                        coproc ? treg : treg + 1,
6527                        (int) BFD_RELOC_LO16, AT);
6528           mips_optimize = hold_mips_optimize;
6529
6530           (void) frag_var (rs_machine_dependent, 0, 0,
6531                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6532                            offset_expr.X_add_symbol, 0, NULL);
6533         }
6534       else if (mips_pic == SVR4_PIC)
6535         {
6536           int gpdel, off;
6537           char *p;
6538
6539           /* If this is a reference to an external symbol, we want
6540                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6541                addu     $at,$at,$gp
6542                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6543                nop
6544                <op>     $treg,0($at)
6545                <op>     $treg+1,4($at)
6546              Otherwise we want
6547                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6548                nop
6549                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6550                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6551              If there is a base register we add it to $at before the
6552              lwc1 instructions.  If there is a constant we include it
6553              in the lwc1 instructions.  */
6554           used_at = 1;
6555           expr1.X_add_number = offset_expr.X_add_number;
6556           offset_expr.X_add_number = 0;
6557           if (expr1.X_add_number < -0x8000
6558               || expr1.X_add_number >= 0x8000 - 4)
6559             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6560           if (reg_needs_delay (mips_gp_register))
6561             gpdel = 4;
6562           else
6563             gpdel = 0;
6564           if (breg == 0)
6565             off = 0;
6566           else
6567             off = 4;
6568           frag_grow (56);
6569           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6570                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6571           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6572                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6573                        "d,v,t", AT, AT, mips_gp_register);
6574           macro_build ((char *) NULL, &icnt, &offset_expr,
6575                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6576                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6577           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6578           if (breg != 0)
6579             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6580                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6581                          "d,v,t", AT, breg, AT);
6582           /* Itbl support may require additional care here.  */
6583           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6584                        coproc ? treg + 1 : treg,
6585                        (int) BFD_RELOC_LO16, AT);
6586           expr1.X_add_number += 4;
6587
6588           /* Set mips_optimize to 2 to avoid inserting an undesired
6589              nop.  */
6590           hold_mips_optimize = mips_optimize;
6591           mips_optimize = 2;
6592           /* Itbl support may require additional care here.  */
6593           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6594                        coproc ? treg : treg + 1,
6595                        (int) BFD_RELOC_LO16, AT);
6596           mips_optimize = hold_mips_optimize;
6597           expr1.X_add_number -= 4;
6598
6599           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6600                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6601                                       8 + gpdel + off, 1, 0),
6602                         offset_expr.X_add_symbol, 0, NULL);
6603           if (gpdel > 0)
6604             {
6605               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6606               p += 4;
6607             }
6608           macro_build (p, &icnt, &offset_expr,
6609                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6610                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6611                        mips_gp_register);
6612           p += 4;
6613           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6614           p += 4;
6615           if (breg != 0)
6616             {
6617               macro_build (p, &icnt, (expressionS *) NULL,
6618                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6619                            "d,v,t", AT, breg, AT);
6620               p += 4;
6621             }
6622           /* Itbl support may require additional care here.  */
6623           macro_build (p, &icnt, &expr1, s, fmt,
6624                        coproc ? treg + 1 : treg,
6625                        (int) BFD_RELOC_LO16, AT);
6626           p += 4;
6627           expr1.X_add_number += 4;
6628
6629           /* Set mips_optimize to 2 to avoid inserting an undesired
6630              nop.  */
6631           hold_mips_optimize = mips_optimize;
6632           mips_optimize = 2;
6633           /* Itbl support may require additional care here.  */
6634           macro_build (p, &icnt, &expr1, s, fmt,
6635                        coproc ? treg : treg + 1,
6636                        (int) BFD_RELOC_LO16, AT);
6637           mips_optimize = hold_mips_optimize;
6638         }
6639       else if (mips_pic == EMBEDDED_PIC)
6640         {
6641           /* If there is no base register, we use
6642                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6643                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6644              If we have a base register, we use
6645                addu     $at,$breg,$gp
6646                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6647                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6648              */
6649           if (breg == 0)
6650             {
6651               tempreg = mips_gp_register;
6652               used_at = 0;
6653             }
6654           else
6655             {
6656               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6657                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6658                            "d,v,t", AT, breg, mips_gp_register);
6659               tempreg = AT;
6660               used_at = 1;
6661             }
6662
6663           /* Itbl support may require additional care here.  */
6664           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6665                        coproc ? treg + 1 : treg,
6666                        (int) BFD_RELOC_GPREL16, tempreg);
6667           offset_expr.X_add_number += 4;
6668           /* Itbl support may require additional care here.  */
6669           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6670                        coproc ? treg : treg + 1,
6671                        (int) BFD_RELOC_GPREL16, tempreg);
6672         }
6673       else
6674         abort ();
6675
6676       if (! used_at)
6677         return;
6678
6679       break;
6680
6681     case M_LD_OB:
6682       s = "lw";
6683       goto sd_ob;
6684     case M_SD_OB:
6685       s = "sw";
6686     sd_ob:
6687       assert (HAVE_32BIT_ADDRESSES);
6688       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6689                    (int) BFD_RELOC_LO16, breg);
6690       offset_expr.X_add_number += 4;
6691       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6692                    (int) BFD_RELOC_LO16, breg);
6693       return;
6694
6695    /* New code added to support COPZ instructions.
6696       This code builds table entries out of the macros in mip_opcodes.
6697       R4000 uses interlocks to handle coproc delays.
6698       Other chips (like the R3000) require nops to be inserted for delays.
6699
6700       FIXME: Currently, we require that the user handle delays.
6701       In order to fill delay slots for non-interlocked chips,
6702       we must have a way to specify delays based on the coprocessor.
6703       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6704       What are the side-effects of the cop instruction?
6705       What cache support might we have and what are its effects?
6706       Both coprocessor & memory require delays. how long???
6707       What registers are read/set/modified?
6708
6709       If an itbl is provided to interpret cop instructions,
6710       this knowledge can be encoded in the itbl spec.  */
6711
6712     case M_COP0:
6713       s = "c0";
6714       goto copz;
6715     case M_COP1:
6716       s = "c1";
6717       goto copz;
6718     case M_COP2:
6719       s = "c2";
6720       goto copz;
6721     case M_COP3:
6722       s = "c3";
6723     copz:
6724       /* For now we just do C (same as Cz).  The parameter will be
6725          stored in insn_opcode by mips_ip.  */
6726       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6727                    ip->insn_opcode);
6728       return;
6729
6730     case M_MOVE:
6731       move_register (&icnt, dreg, sreg);
6732       return;
6733
6734 #ifdef LOSING_COMPILER
6735     default:
6736       /* Try and see if this is a new itbl instruction.
6737          This code builds table entries out of the macros in mip_opcodes.
6738          FIXME: For now we just assemble the expression and pass it's
6739          value along as a 32-bit immediate.
6740          We may want to have the assembler assemble this value,
6741          so that we gain the assembler's knowledge of delay slots,
6742          symbols, etc.
6743          Would it be more efficient to use mask (id) here? */
6744       if (itbl_have_entries
6745           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6746         {
6747           s = ip->insn_mo->name;
6748           s2 = "cop3";
6749           coproc = ITBL_DECODE_PNUM (immed_expr);;
6750           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6751           return;
6752         }
6753       macro2 (ip);
6754       return;
6755     }
6756   if (mips_opts.noat)
6757     as_warn (_("Macro used $at after \".set noat\""));
6758 }
6759
6760 static void
6761 macro2 (ip)
6762      struct mips_cl_insn *ip;
6763 {
6764   register int treg, sreg, dreg, breg;
6765   int tempreg;
6766   int mask;
6767   int icnt = 0;
6768   int used_at;
6769   expressionS expr1;
6770   const char *s;
6771   const char *s2;
6772   const char *fmt;
6773   int likely = 0;
6774   int dbl = 0;
6775   int coproc = 0;
6776   int lr = 0;
6777   int imm = 0;
6778   int off;
6779   offsetT maxnum;
6780   bfd_reloc_code_real_type r;
6781   char *p;
6782
6783   treg = (ip->insn_opcode >> 16) & 0x1f;
6784   dreg = (ip->insn_opcode >> 11) & 0x1f;
6785   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6786   mask = ip->insn_mo->mask;
6787
6788   expr1.X_op = O_constant;
6789   expr1.X_op_symbol = NULL;
6790   expr1.X_add_symbol = NULL;
6791   expr1.X_add_number = 1;
6792
6793   switch (mask)
6794     {
6795 #endif /* LOSING_COMPILER */
6796
6797     case M_DMUL:
6798       dbl = 1;
6799     case M_MUL:
6800       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6801                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6802       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6803                    dreg);
6804       return;
6805
6806     case M_DMUL_I:
6807       dbl = 1;
6808     case M_MUL_I:
6809       /* The MIPS assembler some times generates shifts and adds.  I'm
6810          not trying to be that fancy. GCC should do this for us
6811          anyway.  */
6812       load_register (&icnt, AT, &imm_expr, dbl);
6813       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6814                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6815       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6816                    dreg);
6817       break;
6818
6819     case M_DMULO_I:
6820       dbl = 1;
6821     case M_MULO_I:
6822       imm = 1;
6823       goto do_mulo;
6824
6825     case M_DMULO:
6826       dbl = 1;
6827     case M_MULO:
6828     do_mulo:
6829       mips_emit_delays (true);
6830       ++mips_opts.noreorder;
6831       mips_any_noreorder = 1;
6832       if (imm)
6833         load_register (&icnt, AT, &imm_expr, dbl);
6834       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6835                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6836       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6837                    dreg);
6838       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6839                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6840       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6841                    AT);
6842       if (mips_trap)
6843         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6844                      "s,t,q", dreg, AT, 6);
6845       else
6846         {
6847           expr1.X_add_number = 8;
6848           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6849                        AT);
6850           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6851                        0);
6852           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6853                        "c", 6);
6854         }
6855       --mips_opts.noreorder;
6856       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6857       break;
6858
6859     case M_DMULOU_I:
6860       dbl = 1;
6861     case M_MULOU_I:
6862       imm = 1;
6863       goto do_mulou;
6864
6865     case M_DMULOU:
6866       dbl = 1;
6867     case M_MULOU:
6868     do_mulou:
6869       mips_emit_delays (true);
6870       ++mips_opts.noreorder;
6871       mips_any_noreorder = 1;
6872       if (imm)
6873         load_register (&icnt, AT, &imm_expr, dbl);
6874       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6875                    dbl ? "dmultu" : "multu",
6876                    "s,t", sreg, imm ? AT : treg);
6877       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6878                    AT);
6879       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6880                    dreg);
6881       if (mips_trap)
6882         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6883                      "s,t,q", AT, 0, 6);
6884       else
6885         {
6886           expr1.X_add_number = 8;
6887           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6888           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6889                        0);
6890           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6891                        "c", 6);
6892         }
6893       --mips_opts.noreorder;
6894       break;
6895
6896     case M_DROL:
6897       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6898                    "d,v,t", AT, 0, treg);
6899       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6900                    "d,t,s", AT, sreg, AT);
6901       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6902                    "d,t,s", dreg, sreg, treg);
6903       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6904                    "d,v,t", dreg, dreg, AT);
6905       break;
6906
6907     case M_ROL:
6908       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6909                    "d,v,t", AT, 0, treg);
6910       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6911                    "d,t,s", AT, sreg, AT);
6912       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6913                    "d,t,s", dreg, sreg, treg);
6914       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6915                    "d,v,t", dreg, dreg, AT);
6916       break;
6917
6918     case M_DROL_I:
6919       {
6920         unsigned int rot;
6921
6922         if (imm_expr.X_op != O_constant)
6923           as_bad (_("rotate count too large"));
6924         rot = imm_expr.X_add_number & 0x3f;
6925         if (CPU_HAS_DROR (mips_arch))
6926           {
6927             rot = (64 - rot) & 0x3f;
6928             if (rot >= 32)
6929               macro_build ((char *) NULL, &icnt, NULL, "dror32",
6930                            "d,w,<", dreg, sreg, rot - 32);
6931             else
6932               macro_build ((char *) NULL, &icnt, NULL, "dror",
6933                            "d,w,<", dreg, sreg, rot);
6934             break;
6935           }
6936         if (rot == 0)
6937           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6938                        "d,w,<", dreg, sreg, 0);
6939         else
6940           {
6941             char *l, *r;
6942
6943             l = (rot < 0x20) ? "dsll" : "dsll32";
6944             r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6945             rot &= 0x1f;
6946             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6947                          "d,w,<", AT, sreg, rot);
6948             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6949                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6950             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6951                          "d,v,t", dreg, dreg, AT);
6952           }
6953       }
6954       break;
6955
6956     case M_ROL_I:
6957       {
6958         unsigned int rot;
6959
6960         if (imm_expr.X_op != O_constant)
6961           as_bad (_("rotate count too large"));
6962         rot = imm_expr.X_add_number & 0x1f;
6963         if (CPU_HAS_ROR (mips_arch))
6964           {
6965             macro_build ((char *) NULL, &icnt, NULL, "ror",
6966                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6967             break;
6968           }
6969         if (rot == 0)
6970           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6971                        "d,w,<", dreg, sreg, 0);
6972         else
6973           {
6974             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6975                          "d,w,<", AT, sreg, rot);
6976             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6977                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6978             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6979                          "d,v,t", dreg, dreg, AT);
6980           }
6981       }
6982       break;
6983
6984     case M_DROR:
6985       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6986                    "d,v,t", AT, 0, treg);
6987       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6988                    "d,t,s", AT, sreg, AT);
6989       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6990                    "d,t,s", dreg, sreg, treg);
6991       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6992                    "d,v,t", dreg, dreg, AT);
6993       break;
6994
6995     case M_ROR:
6996       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6997                    "d,v,t", AT, 0, treg);
6998       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6999                    "d,t,s", AT, sreg, AT);
7000       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7001                    "d,t,s", dreg, sreg, treg);
7002       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7003                    "d,v,t", dreg, dreg, AT);
7004       break;
7005
7006     case M_DROR_I:
7007       {
7008         unsigned int rot;
7009
7010         if (imm_expr.X_op != O_constant)
7011           as_bad (_("rotate count too large"));
7012         rot = imm_expr.X_add_number & 0x3f;
7013         if (rot == 0)
7014           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7015                        "d,w,<", dreg, sreg, 0);
7016         else
7017           {
7018             char *l, *r;
7019
7020             r = (rot < 0x20) ? "dsrl" : "dsrl32";
7021             l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7022             rot &= 0x1f;
7023             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7024                          "d,w,<", AT, sreg, rot);
7025             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7026                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7027             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7028                          "d,v,t", dreg, dreg, AT);
7029           }
7030       }
7031       break;
7032
7033     case M_ROR_I:
7034       {
7035         unsigned int rot;
7036
7037         if (imm_expr.X_op != O_constant)
7038           as_bad (_("rotate count too large"));
7039         rot = imm_expr.X_add_number & 0x1f;
7040         if (rot == 0)
7041           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7042                        "d,w,<", dreg, sreg, 0);
7043         else
7044           {
7045             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7046                          "d,w,<", AT, sreg, rot);
7047             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7048                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7049             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7050                          "d,v,t", dreg, dreg, AT);
7051           }
7052       }
7053       break;
7054
7055     case M_S_DOB:
7056       if (mips_arch == CPU_R4650)
7057         {
7058           as_bad (_("opcode not supported on this processor"));
7059           return;
7060         }
7061       assert (mips_opts.isa == ISA_MIPS1);
7062       /* Even on a big endian machine $fn comes before $fn+1.  We have
7063          to adjust when storing to memory.  */
7064       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7065                    target_big_endian ? treg + 1 : treg,
7066                    (int) BFD_RELOC_LO16, breg);
7067       offset_expr.X_add_number += 4;
7068       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7069                    target_big_endian ? treg : treg + 1,
7070                    (int) BFD_RELOC_LO16, breg);
7071       return;
7072
7073     case M_SEQ:
7074       if (sreg == 0)
7075         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7076                      treg, (int) BFD_RELOC_LO16);
7077       else if (treg == 0)
7078         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7079                      sreg, (int) BFD_RELOC_LO16);
7080       else
7081         {
7082           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7083                        "d,v,t", dreg, sreg, treg);
7084           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7085                        dreg, (int) BFD_RELOC_LO16);
7086         }
7087       return;
7088
7089     case M_SEQ_I:
7090       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7091         {
7092           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7093                        sreg, (int) BFD_RELOC_LO16);
7094           return;
7095         }
7096       if (sreg == 0)
7097         {
7098           as_warn (_("Instruction %s: result is always false"),
7099                    ip->insn_mo->name);
7100           move_register (&icnt, dreg, 0);
7101           return;
7102         }
7103       if (imm_expr.X_op == O_constant
7104           && imm_expr.X_add_number >= 0
7105           && imm_expr.X_add_number < 0x10000)
7106         {
7107           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7108                        sreg, (int) BFD_RELOC_LO16);
7109           used_at = 0;
7110         }
7111       else if (imm_expr.X_op == O_constant
7112                && imm_expr.X_add_number > -0x8000
7113                && imm_expr.X_add_number < 0)
7114         {
7115           imm_expr.X_add_number = -imm_expr.X_add_number;
7116           macro_build ((char *) NULL, &icnt, &imm_expr,
7117                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7118                        "t,r,j", dreg, sreg,
7119                        (int) BFD_RELOC_LO16);
7120           used_at = 0;
7121         }
7122       else
7123         {
7124           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7125           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7126                        "d,v,t", dreg, sreg, AT);
7127           used_at = 1;
7128         }
7129       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7130                    (int) BFD_RELOC_LO16);
7131       if (used_at)
7132         break;
7133       return;
7134
7135     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7136       s = "slt";
7137       goto sge;
7138     case M_SGEU:
7139       s = "sltu";
7140     sge:
7141       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7142                    dreg, sreg, treg);
7143       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7144                    (int) BFD_RELOC_LO16);
7145       return;
7146
7147     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7148     case M_SGEU_I:
7149       if (imm_expr.X_op == O_constant
7150           && imm_expr.X_add_number >= -0x8000
7151           && imm_expr.X_add_number < 0x8000)
7152         {
7153           macro_build ((char *) NULL, &icnt, &imm_expr,
7154                        mask == M_SGE_I ? "slti" : "sltiu",
7155                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7156           used_at = 0;
7157         }
7158       else
7159         {
7160           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7161           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7162                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7163                        AT);
7164           used_at = 1;
7165         }
7166       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7167                    (int) BFD_RELOC_LO16);
7168       if (used_at)
7169         break;
7170       return;
7171
7172     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7173       s = "slt";
7174       goto sgt;
7175     case M_SGTU:
7176       s = "sltu";
7177     sgt:
7178       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7179                    dreg, treg, sreg);
7180       return;
7181
7182     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7183       s = "slt";
7184       goto sgti;
7185     case M_SGTU_I:
7186       s = "sltu";
7187     sgti:
7188       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7189       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7190                    dreg, AT, sreg);
7191       break;
7192
7193     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7194       s = "slt";
7195       goto sle;
7196     case M_SLEU:
7197       s = "sltu";
7198     sle:
7199       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7200                    dreg, treg, sreg);
7201       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7202                    (int) BFD_RELOC_LO16);
7203       return;
7204
7205     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7206       s = "slt";
7207       goto slei;
7208     case M_SLEU_I:
7209       s = "sltu";
7210     slei:
7211       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7212       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7213                    dreg, AT, sreg);
7214       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7215                    (int) BFD_RELOC_LO16);
7216       break;
7217
7218     case M_SLT_I:
7219       if (imm_expr.X_op == O_constant
7220           && imm_expr.X_add_number >= -0x8000
7221           && imm_expr.X_add_number < 0x8000)
7222         {
7223           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7224                        dreg, sreg, (int) BFD_RELOC_LO16);
7225           return;
7226         }
7227       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7228       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7229                    dreg, sreg, AT);
7230       break;
7231
7232     case M_SLTU_I:
7233       if (imm_expr.X_op == O_constant
7234           && imm_expr.X_add_number >= -0x8000
7235           && imm_expr.X_add_number < 0x8000)
7236         {
7237           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7238                        dreg, sreg, (int) BFD_RELOC_LO16);
7239           return;
7240         }
7241       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7242       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7243                    "d,v,t", dreg, sreg, AT);
7244       break;
7245
7246     case M_SNE:
7247       if (sreg == 0)
7248         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7249                      "d,v,t", dreg, 0, treg);
7250       else if (treg == 0)
7251         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7252                      "d,v,t", dreg, 0, sreg);
7253       else
7254         {
7255           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7256                        "d,v,t", dreg, sreg, treg);
7257           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7258                        "d,v,t", dreg, 0, dreg);
7259         }
7260       return;
7261
7262     case M_SNE_I:
7263       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7264         {
7265           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7266                        "d,v,t", dreg, 0, sreg);
7267           return;
7268         }
7269       if (sreg == 0)
7270         {
7271           as_warn (_("Instruction %s: result is always true"),
7272                    ip->insn_mo->name);
7273           macro_build ((char *) NULL, &icnt, &expr1,
7274                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7275                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7276           return;
7277         }
7278       if (imm_expr.X_op == O_constant
7279           && imm_expr.X_add_number >= 0
7280           && imm_expr.X_add_number < 0x10000)
7281         {
7282           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7283                        dreg, sreg, (int) BFD_RELOC_LO16);
7284           used_at = 0;
7285         }
7286       else if (imm_expr.X_op == O_constant
7287                && imm_expr.X_add_number > -0x8000
7288                && imm_expr.X_add_number < 0)
7289         {
7290           imm_expr.X_add_number = -imm_expr.X_add_number;
7291           macro_build ((char *) NULL, &icnt, &imm_expr,
7292                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7293                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7294           used_at = 0;
7295         }
7296       else
7297         {
7298           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7299           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7300                        "d,v,t", dreg, sreg, AT);
7301           used_at = 1;
7302         }
7303       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7304                    "d,v,t", dreg, 0, dreg);
7305       if (used_at)
7306         break;
7307       return;
7308
7309     case M_DSUB_I:
7310       dbl = 1;
7311     case M_SUB_I:
7312       if (imm_expr.X_op == O_constant
7313           && imm_expr.X_add_number > -0x8000
7314           && imm_expr.X_add_number <= 0x8000)
7315         {
7316           imm_expr.X_add_number = -imm_expr.X_add_number;
7317           macro_build ((char *) NULL, &icnt, &imm_expr,
7318                        dbl ? "daddi" : "addi",
7319                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7320           return;
7321         }
7322       load_register (&icnt, AT, &imm_expr, dbl);
7323       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7324                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7325       break;
7326
7327     case M_DSUBU_I:
7328       dbl = 1;
7329     case M_SUBU_I:
7330       if (imm_expr.X_op == O_constant
7331           && imm_expr.X_add_number > -0x8000
7332           && imm_expr.X_add_number <= 0x8000)
7333         {
7334           imm_expr.X_add_number = -imm_expr.X_add_number;
7335           macro_build ((char *) NULL, &icnt, &imm_expr,
7336                        dbl ? "daddiu" : "addiu",
7337                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7338           return;
7339         }
7340       load_register (&icnt, AT, &imm_expr, dbl);
7341       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7342                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7343       break;
7344
7345     case M_TEQ_I:
7346       s = "teq";
7347       goto trap;
7348     case M_TGE_I:
7349       s = "tge";
7350       goto trap;
7351     case M_TGEU_I:
7352       s = "tgeu";
7353       goto trap;
7354     case M_TLT_I:
7355       s = "tlt";
7356       goto trap;
7357     case M_TLTU_I:
7358       s = "tltu";
7359       goto trap;
7360     case M_TNE_I:
7361       s = "tne";
7362     trap:
7363       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7364       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7365                    AT);
7366       break;
7367
7368     case M_TRUNCWS:
7369     case M_TRUNCWD:
7370       assert (mips_opts.isa == ISA_MIPS1);
7371       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7372       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7373
7374       /*
7375        * Is the double cfc1 instruction a bug in the mips assembler;
7376        * or is there a reason for it?
7377        */
7378       mips_emit_delays (true);
7379       ++mips_opts.noreorder;
7380       mips_any_noreorder = 1;
7381       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7382                    treg, RA);
7383       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7384                    treg, RA);
7385       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7386       expr1.X_add_number = 3;
7387       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7388                    (int) BFD_RELOC_LO16);
7389       expr1.X_add_number = 2;
7390       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7391                      (int) BFD_RELOC_LO16);
7392       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7393                    AT, RA);
7394       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7395       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7396               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7397       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7398                    treg, RA);
7399       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7400       --mips_opts.noreorder;
7401       break;
7402
7403     case M_ULH:
7404       s = "lb";
7405       goto ulh;
7406     case M_ULHU:
7407       s = "lbu";
7408     ulh:
7409       if (offset_expr.X_add_number >= 0x7fff)
7410         as_bad (_("operand overflow"));
7411       /* avoid load delay */
7412       if (! target_big_endian)
7413         ++offset_expr.X_add_number;
7414       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7415                    (int) BFD_RELOC_LO16, breg);
7416       if (! target_big_endian)
7417         --offset_expr.X_add_number;
7418       else
7419         ++offset_expr.X_add_number;
7420       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7421                    (int) BFD_RELOC_LO16, breg);
7422       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7423                    treg, treg, 8);
7424       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7425                    treg, treg, AT);
7426       break;
7427
7428     case M_ULD:
7429       s = "ldl";
7430       s2 = "ldr";
7431       off = 7;
7432       goto ulw;
7433     case M_ULW:
7434       s = "lwl";
7435       s2 = "lwr";
7436       off = 3;
7437     ulw:
7438       if (offset_expr.X_add_number >= 0x8000 - off)
7439         as_bad (_("operand overflow"));
7440       if (! target_big_endian)
7441         offset_expr.X_add_number += off;
7442       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7443                    (int) BFD_RELOC_LO16, breg);
7444       if (! target_big_endian)
7445         offset_expr.X_add_number -= off;
7446       else
7447         offset_expr.X_add_number += off;
7448       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7449                    (int) BFD_RELOC_LO16, breg);
7450       return;
7451
7452     case M_ULD_A:
7453       s = "ldl";
7454       s2 = "ldr";
7455       off = 7;
7456       goto ulwa;
7457     case M_ULW_A:
7458       s = "lwl";
7459       s2 = "lwr";
7460       off = 3;
7461     ulwa:
7462       used_at = 1;
7463       load_address (&icnt, AT, &offset_expr, &used_at);
7464       if (breg != 0)
7465         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7466                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7467                      "d,v,t", AT, AT, breg);
7468       if (! target_big_endian)
7469         expr1.X_add_number = off;
7470       else
7471         expr1.X_add_number = 0;
7472       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7473                    (int) BFD_RELOC_LO16, AT);
7474       if (! target_big_endian)
7475         expr1.X_add_number = 0;
7476       else
7477         expr1.X_add_number = off;
7478       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7479                    (int) BFD_RELOC_LO16, AT);
7480       break;
7481
7482     case M_ULH_A:
7483     case M_ULHU_A:
7484       used_at = 1;
7485       load_address (&icnt, AT, &offset_expr, &used_at);
7486       if (breg != 0)
7487         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7488                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7489                      "d,v,t", AT, AT, breg);
7490       if (target_big_endian)
7491         expr1.X_add_number = 0;
7492       macro_build ((char *) NULL, &icnt, &expr1,
7493                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7494                    (int) BFD_RELOC_LO16, AT);
7495       if (target_big_endian)
7496         expr1.X_add_number = 1;
7497       else
7498         expr1.X_add_number = 0;
7499       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7500                    (int) BFD_RELOC_LO16, AT);
7501       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7502                    treg, treg, 8);
7503       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7504                    treg, treg, AT);
7505       break;
7506
7507     case M_USH:
7508       if (offset_expr.X_add_number >= 0x7fff)
7509         as_bad (_("operand overflow"));
7510       if (target_big_endian)
7511         ++offset_expr.X_add_number;
7512       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7513                    (int) BFD_RELOC_LO16, breg);
7514       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7515                    AT, treg, 8);
7516       if (target_big_endian)
7517         --offset_expr.X_add_number;
7518       else
7519         ++offset_expr.X_add_number;
7520       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7521                    (int) BFD_RELOC_LO16, breg);
7522       break;
7523
7524     case M_USD:
7525       s = "sdl";
7526       s2 = "sdr";
7527       off = 7;
7528       goto usw;
7529     case M_USW:
7530       s = "swl";
7531       s2 = "swr";
7532       off = 3;
7533     usw:
7534       if (offset_expr.X_add_number >= 0x8000 - off)
7535         as_bad (_("operand overflow"));
7536       if (! target_big_endian)
7537         offset_expr.X_add_number += off;
7538       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7539                    (int) BFD_RELOC_LO16, breg);
7540       if (! target_big_endian)
7541         offset_expr.X_add_number -= off;
7542       else
7543         offset_expr.X_add_number += off;
7544       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7545                    (int) BFD_RELOC_LO16, breg);
7546       return;
7547
7548     case M_USD_A:
7549       s = "sdl";
7550       s2 = "sdr";
7551       off = 7;
7552       goto uswa;
7553     case M_USW_A:
7554       s = "swl";
7555       s2 = "swr";
7556       off = 3;
7557     uswa:
7558       used_at = 1;
7559       load_address (&icnt, AT, &offset_expr, &used_at);
7560       if (breg != 0)
7561         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7562                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7563                      "d,v,t", AT, AT, breg);
7564       if (! target_big_endian)
7565         expr1.X_add_number = off;
7566       else
7567         expr1.X_add_number = 0;
7568       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7569                    (int) BFD_RELOC_LO16, AT);
7570       if (! target_big_endian)
7571         expr1.X_add_number = 0;
7572       else
7573         expr1.X_add_number = off;
7574       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7575                    (int) BFD_RELOC_LO16, AT);
7576       break;
7577
7578     case M_USH_A:
7579       used_at = 1;
7580       load_address (&icnt, AT, &offset_expr, &used_at);
7581       if (breg != 0)
7582         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7583                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7584                      "d,v,t", AT, AT, breg);
7585       if (! target_big_endian)
7586         expr1.X_add_number = 0;
7587       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7588                    (int) BFD_RELOC_LO16, AT);
7589       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7590                    treg, treg, 8);
7591       if (! target_big_endian)
7592         expr1.X_add_number = 1;
7593       else
7594         expr1.X_add_number = 0;
7595       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7596                    (int) BFD_RELOC_LO16, AT);
7597       if (! target_big_endian)
7598         expr1.X_add_number = 0;
7599       else
7600         expr1.X_add_number = 1;
7601       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7602                    (int) BFD_RELOC_LO16, AT);
7603       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7604                    treg, treg, 8);
7605       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7606                    treg, treg, AT);
7607       break;
7608
7609     default:
7610       /* FIXME: Check if this is one of the itbl macros, since they
7611          are added dynamically.  */
7612       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7613       break;
7614     }
7615   if (mips_opts.noat)
7616     as_warn (_("Macro used $at after \".set noat\""));
7617 }
7618
7619 /* Implement macros in mips16 mode.  */
7620
7621 static void
7622 mips16_macro (ip)
7623      struct mips_cl_insn *ip;
7624 {
7625   int mask;
7626   int xreg, yreg, zreg, tmp;
7627   int icnt;
7628   expressionS expr1;
7629   int dbl;
7630   const char *s, *s2, *s3;
7631
7632   mask = ip->insn_mo->mask;
7633
7634   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7635   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7636   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7637
7638   icnt = 0;
7639
7640   expr1.X_op = O_constant;
7641   expr1.X_op_symbol = NULL;
7642   expr1.X_add_symbol = NULL;
7643   expr1.X_add_number = 1;
7644
7645   dbl = 0;
7646
7647   switch (mask)
7648     {
7649     default:
7650       internalError ();
7651
7652     case M_DDIV_3:
7653       dbl = 1;
7654     case M_DIV_3:
7655       s = "mflo";
7656       goto do_div3;
7657     case M_DREM_3:
7658       dbl = 1;
7659     case M_REM_3:
7660       s = "mfhi";
7661     do_div3:
7662       mips_emit_delays (true);
7663       ++mips_opts.noreorder;
7664       mips_any_noreorder = 1;
7665       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7666                    dbl ? "ddiv" : "div",
7667                    "0,x,y", xreg, yreg);
7668       expr1.X_add_number = 2;
7669       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7670       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7671                    7);
7672
7673       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7674          since that causes an overflow.  We should do that as well,
7675          but I don't see how to do the comparisons without a temporary
7676          register.  */
7677       --mips_opts.noreorder;
7678       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7679       break;
7680
7681     case M_DIVU_3:
7682       s = "divu";
7683       s2 = "mflo";
7684       goto do_divu3;
7685     case M_REMU_3:
7686       s = "divu";
7687       s2 = "mfhi";
7688       goto do_divu3;
7689     case M_DDIVU_3:
7690       s = "ddivu";
7691       s2 = "mflo";
7692       goto do_divu3;
7693     case M_DREMU_3:
7694       s = "ddivu";
7695       s2 = "mfhi";
7696     do_divu3:
7697       mips_emit_delays (true);
7698       ++mips_opts.noreorder;
7699       mips_any_noreorder = 1;
7700       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7701                    xreg, yreg);
7702       expr1.X_add_number = 2;
7703       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7704       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7705                    "6", 7);
7706       --mips_opts.noreorder;
7707       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7708       break;
7709
7710     case M_DMUL:
7711       dbl = 1;
7712     case M_MUL:
7713       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7714                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7715       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7716                    zreg);
7717       return;
7718
7719     case M_DSUBU_I:
7720       dbl = 1;
7721       goto do_subu;
7722     case M_SUBU_I:
7723     do_subu:
7724       if (imm_expr.X_op != O_constant)
7725         as_bad (_("Unsupported large constant"));
7726       imm_expr.X_add_number = -imm_expr.X_add_number;
7727       macro_build ((char *) NULL, &icnt, &imm_expr,
7728                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7729       break;
7730
7731     case M_SUBU_I_2:
7732       if (imm_expr.X_op != O_constant)
7733         as_bad (_("Unsupported large constant"));
7734       imm_expr.X_add_number = -imm_expr.X_add_number;
7735       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7736                    "x,k", xreg);
7737       break;
7738
7739     case M_DSUBU_I_2:
7740       if (imm_expr.X_op != O_constant)
7741         as_bad (_("Unsupported large constant"));
7742       imm_expr.X_add_number = -imm_expr.X_add_number;
7743       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7744                    "y,j", yreg);
7745       break;
7746
7747     case M_BEQ:
7748       s = "cmp";
7749       s2 = "bteqz";
7750       goto do_branch;
7751     case M_BNE:
7752       s = "cmp";
7753       s2 = "btnez";
7754       goto do_branch;
7755     case M_BLT:
7756       s = "slt";
7757       s2 = "btnez";
7758       goto do_branch;
7759     case M_BLTU:
7760       s = "sltu";
7761       s2 = "btnez";
7762       goto do_branch;
7763     case M_BLE:
7764       s = "slt";
7765       s2 = "bteqz";
7766       goto do_reverse_branch;
7767     case M_BLEU:
7768       s = "sltu";
7769       s2 = "bteqz";
7770       goto do_reverse_branch;
7771     case M_BGE:
7772       s = "slt";
7773       s2 = "bteqz";
7774       goto do_branch;
7775     case M_BGEU:
7776       s = "sltu";
7777       s2 = "bteqz";
7778       goto do_branch;
7779     case M_BGT:
7780       s = "slt";
7781       s2 = "btnez";
7782       goto do_reverse_branch;
7783     case M_BGTU:
7784       s = "sltu";
7785       s2 = "btnez";
7786
7787     do_reverse_branch:
7788       tmp = xreg;
7789       xreg = yreg;
7790       yreg = tmp;
7791
7792     do_branch:
7793       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7794                    xreg, yreg);
7795       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7796       break;
7797
7798     case M_BEQ_I:
7799       s = "cmpi";
7800       s2 = "bteqz";
7801       s3 = "x,U";
7802       goto do_branch_i;
7803     case M_BNE_I:
7804       s = "cmpi";
7805       s2 = "btnez";
7806       s3 = "x,U";
7807       goto do_branch_i;
7808     case M_BLT_I:
7809       s = "slti";
7810       s2 = "btnez";
7811       s3 = "x,8";
7812       goto do_branch_i;
7813     case M_BLTU_I:
7814       s = "sltiu";
7815       s2 = "btnez";
7816       s3 = "x,8";
7817       goto do_branch_i;
7818     case M_BLE_I:
7819       s = "slti";
7820       s2 = "btnez";
7821       s3 = "x,8";
7822       goto do_addone_branch_i;
7823     case M_BLEU_I:
7824       s = "sltiu";
7825       s2 = "btnez";
7826       s3 = "x,8";
7827       goto do_addone_branch_i;
7828     case M_BGE_I:
7829       s = "slti";
7830       s2 = "bteqz";
7831       s3 = "x,8";
7832       goto do_branch_i;
7833     case M_BGEU_I:
7834       s = "sltiu";
7835       s2 = "bteqz";
7836       s3 = "x,8";
7837       goto do_branch_i;
7838     case M_BGT_I:
7839       s = "slti";
7840       s2 = "bteqz";
7841       s3 = "x,8";
7842       goto do_addone_branch_i;
7843     case M_BGTU_I:
7844       s = "sltiu";
7845       s2 = "bteqz";
7846       s3 = "x,8";
7847
7848     do_addone_branch_i:
7849       if (imm_expr.X_op != O_constant)
7850         as_bad (_("Unsupported large constant"));
7851       ++imm_expr.X_add_number;
7852
7853     do_branch_i:
7854       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7855       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7856       break;
7857
7858     case M_ABS:
7859       expr1.X_add_number = 0;
7860       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7861       if (xreg != yreg)
7862         move_register (&icnt, xreg, yreg);
7863       expr1.X_add_number = 2;
7864       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7865       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7866                    "neg", "x,w", xreg, xreg);
7867     }
7868 }
7869
7870 /* For consistency checking, verify that all bits are specified either
7871    by the match/mask part of the instruction definition, or by the
7872    operand list.  */
7873 static int
7874 validate_mips_insn (opc)
7875      const struct mips_opcode *opc;
7876 {
7877   const char *p = opc->args;
7878   char c;
7879   unsigned long used_bits = opc->mask;
7880
7881   if ((used_bits & opc->match) != opc->match)
7882     {
7883       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7884               opc->name, opc->args);
7885       return 0;
7886     }
7887 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7888   while (*p)
7889     switch (c = *p++)
7890       {
7891       case ',': break;
7892       case '(': break;
7893       case ')': break;
7894       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7895       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7896       case 'A': break;
7897       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7898       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7899       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7900       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7901       case 'F': break;
7902       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7903       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7904       case 'I': break;
7905       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7906       case 'L': break;
7907       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7908       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7909       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7910       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7911                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7912       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7913       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7914       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7915       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7916       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7917       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7918       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7919       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7920       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7921       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7922       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7923       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7924       case 'f': break;
7925       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7926       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7927       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7928       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7929       case 'l': break;
7930       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7931       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7932       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7933       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7934       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7935       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7936       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7937       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7938       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7939       case 'x': break;
7940       case 'z': break;
7941       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7942       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7943                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7944       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
7945       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
7946       case '[': break;
7947       case ']': break;
7948       default:
7949         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7950                 c, opc->name, opc->args);
7951         return 0;
7952       }
7953 #undef USE_BITS
7954   if (used_bits != 0xffffffff)
7955     {
7956       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7957               ~used_bits & 0xffffffff, opc->name, opc->args);
7958       return 0;
7959     }
7960   return 1;
7961 }
7962
7963 /* This routine assembles an instruction into its binary format.  As a
7964    side effect, it sets one of the global variables imm_reloc or
7965    offset_reloc to the type of relocation to do if one of the operands
7966    is an address expression.  */
7967
7968 static void
7969 mips_ip (str, ip)
7970      char *str;
7971      struct mips_cl_insn *ip;
7972 {
7973   char *s;
7974   const char *args;
7975   char c = 0;
7976   struct mips_opcode *insn;
7977   char *argsStart;
7978   unsigned int regno;
7979   unsigned int lastregno = 0;
7980   char *s_reset;
7981   char save_c = 0;
7982
7983   insn_error = NULL;
7984
7985   /* If the instruction contains a '.', we first try to match an instruction
7986      including the '.'.  Then we try again without the '.'.  */
7987   insn = NULL;
7988   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7989     continue;
7990
7991   /* If we stopped on whitespace, then replace the whitespace with null for
7992      the call to hash_find.  Save the character we replaced just in case we
7993      have to re-parse the instruction.  */
7994   if (ISSPACE (*s))
7995     {
7996       save_c = *s;
7997       *s++ = '\0';
7998     }
7999
8000   insn = (struct mips_opcode *) hash_find (op_hash, str);
8001
8002   /* If we didn't find the instruction in the opcode table, try again, but
8003      this time with just the instruction up to, but not including the
8004      first '.'.  */
8005   if (insn == NULL)
8006     {
8007       /* Restore the character we overwrite above (if any).  */
8008       if (save_c)
8009         *(--s) = save_c;
8010
8011       /* Scan up to the first '.' or whitespace.  */
8012       for (s = str;
8013            *s != '\0' && *s != '.' && !ISSPACE (*s);
8014            ++s)
8015         continue;
8016
8017       /* If we did not find a '.', then we can quit now.  */
8018       if (*s != '.')
8019         {
8020           insn_error = "unrecognized opcode";
8021           return;
8022         }
8023
8024       /* Lookup the instruction in the hash table.  */
8025       *s++ = '\0';
8026       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8027         {
8028           insn_error = "unrecognized opcode";
8029           return;
8030         }
8031     }
8032
8033   argsStart = s;
8034   for (;;)
8035     {
8036       boolean ok;
8037
8038       assert (strcmp (insn->name, str) == 0);
8039
8040       if (OPCODE_IS_MEMBER (insn,
8041                             (mips_opts.isa
8042                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8043                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8044                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8045                             mips_arch))
8046         ok = true;
8047       else
8048         ok = false;
8049
8050       if (insn->pinfo != INSN_MACRO)
8051         {
8052           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8053             ok = false;
8054         }
8055
8056       if (! ok)
8057         {
8058           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8059               && strcmp (insn->name, insn[1].name) == 0)
8060             {
8061               ++insn;
8062               continue;
8063             }
8064           else
8065             {
8066               if (!insn_error)
8067                 {
8068                   static char buf[100];
8069                   if (mips_arch_info->is_isa)
8070                     sprintf (buf,
8071                              _("opcode not supported at this ISA level (%s)"),
8072                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8073                   else
8074                     sprintf (buf,
8075                              _("opcode not supported on this processor: %s (%s)"),
8076                              mips_arch_info->name,
8077                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8078                   insn_error = buf;
8079                 }
8080               if (save_c)
8081                 *(--s) = save_c;
8082               return;
8083             }
8084         }
8085
8086       ip->insn_mo = insn;
8087       ip->insn_opcode = insn->match;
8088       insn_error = NULL;
8089       for (args = insn->args;; ++args)
8090         {
8091           int is_mdmx;
8092
8093           s += strspn (s, " \t");
8094           is_mdmx = 0;
8095           switch (*args)
8096             {
8097             case '\0':          /* end of args */
8098               if (*s == '\0')
8099                 return;
8100               break;
8101
8102             case ',':
8103               if (*s++ == *args)
8104                 continue;
8105               s--;
8106               switch (*++args)
8107                 {
8108                 case 'r':
8109                 case 'v':
8110                   ip->insn_opcode |= lastregno << OP_SH_RS;
8111                   continue;
8112
8113                 case 'w':
8114                   ip->insn_opcode |= lastregno << OP_SH_RT;
8115                   continue;
8116
8117                 case 'W':
8118                   ip->insn_opcode |= lastregno << OP_SH_FT;
8119                   continue;
8120
8121                 case 'V':
8122                   ip->insn_opcode |= lastregno << OP_SH_FS;
8123                   continue;
8124                 }
8125               break;
8126
8127             case '(':
8128               /* Handle optional base register.
8129                  Either the base register is omitted or
8130                  we must have a left paren.  */
8131               /* This is dependent on the next operand specifier
8132                  is a base register specification.  */
8133               assert (args[1] == 'b' || args[1] == '5'
8134                       || args[1] == '-' || args[1] == '4');
8135               if (*s == '\0')
8136                 return;
8137
8138             case ')':           /* these must match exactly */
8139             case '[':
8140             case ']':
8141               if (*s++ == *args)
8142                 continue;
8143               break;
8144
8145             case '<':           /* must be at least one digit */
8146               /*
8147                * According to the manual, if the shift amount is greater
8148                * than 31 or less than 0, then the shift amount should be
8149                * mod 32.  In reality the mips assembler issues an error.
8150                * We issue a warning and mask out all but the low 5 bits.
8151                */
8152               my_getExpression (&imm_expr, s);
8153               check_absolute_expr (ip, &imm_expr);
8154               if ((unsigned long) imm_expr.X_add_number > 31)
8155                 {
8156                   as_warn (_("Improper shift amount (%lu)"),
8157                            (unsigned long) imm_expr.X_add_number);
8158                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8159                 }
8160               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8161               imm_expr.X_op = O_absent;
8162               s = expr_end;
8163               continue;
8164
8165             case '>':           /* shift amount minus 32 */
8166               my_getExpression (&imm_expr, s);
8167               check_absolute_expr (ip, &imm_expr);
8168               if ((unsigned long) imm_expr.X_add_number < 32
8169                   || (unsigned long) imm_expr.X_add_number > 63)
8170                 break;
8171               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8172               imm_expr.X_op = O_absent;
8173               s = expr_end;
8174               continue;
8175
8176             case 'k':           /* cache code */
8177             case 'h':           /* prefx code */
8178               my_getExpression (&imm_expr, s);
8179               check_absolute_expr (ip, &imm_expr);
8180               if ((unsigned long) imm_expr.X_add_number > 31)
8181                 {
8182                   as_warn (_("Invalid value for `%s' (%lu)"),
8183                            ip->insn_mo->name,
8184                            (unsigned long) imm_expr.X_add_number);
8185                   imm_expr.X_add_number &= 0x1f;
8186                 }
8187               if (*args == 'k')
8188                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8189               else
8190                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8191               imm_expr.X_op = O_absent;
8192               s = expr_end;
8193               continue;
8194
8195             case 'c':           /* break code */
8196               my_getExpression (&imm_expr, s);
8197               check_absolute_expr (ip, &imm_expr);
8198               if ((unsigned long) imm_expr.X_add_number > 1023)
8199                 {
8200                   as_warn (_("Illegal break code (%lu)"),
8201                            (unsigned long) imm_expr.X_add_number);
8202                   imm_expr.X_add_number &= OP_MASK_CODE;
8203                 }
8204               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8205               imm_expr.X_op = O_absent;
8206               s = expr_end;
8207               continue;
8208
8209             case 'q':           /* lower break code */
8210               my_getExpression (&imm_expr, s);
8211               check_absolute_expr (ip, &imm_expr);
8212               if ((unsigned long) imm_expr.X_add_number > 1023)
8213                 {
8214                   as_warn (_("Illegal lower break code (%lu)"),
8215                            (unsigned long) imm_expr.X_add_number);
8216                   imm_expr.X_add_number &= OP_MASK_CODE2;
8217                 }
8218               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8219               imm_expr.X_op = O_absent;
8220               s = expr_end;
8221               continue;
8222
8223             case 'B':           /* 20-bit syscall/break code.  */
8224               my_getExpression (&imm_expr, s);
8225               check_absolute_expr (ip, &imm_expr);
8226               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8227                 as_warn (_("Illegal 20-bit code (%lu)"),
8228                          (unsigned long) imm_expr.X_add_number);
8229               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8230               imm_expr.X_op = O_absent;
8231               s = expr_end;
8232               continue;
8233
8234             case 'C':           /* Coprocessor code */
8235               my_getExpression (&imm_expr, s);
8236               check_absolute_expr (ip, &imm_expr);
8237               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8238                 {
8239                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8240                            (unsigned long) imm_expr.X_add_number);
8241                   imm_expr.X_add_number &= ((1 << 25) - 1);
8242                 }
8243               ip->insn_opcode |= imm_expr.X_add_number;
8244               imm_expr.X_op = O_absent;
8245               s = expr_end;
8246               continue;
8247
8248             case 'J':           /* 19-bit wait code.  */
8249               my_getExpression (&imm_expr, s);
8250               check_absolute_expr (ip, &imm_expr);
8251               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8252                 as_warn (_("Illegal 19-bit code (%lu)"),
8253                          (unsigned long) imm_expr.X_add_number);
8254               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8255               imm_expr.X_op = O_absent;
8256               s = expr_end;
8257               continue;
8258
8259             case 'P':           /* Performance register */
8260               my_getExpression (&imm_expr, s);
8261               check_absolute_expr (ip, &imm_expr);
8262               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8263                 {
8264                   as_warn (_("Invalid performance register (%lu)"),
8265                            (unsigned long) imm_expr.X_add_number);
8266                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8267                 }
8268               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8269               imm_expr.X_op = O_absent;
8270               s = expr_end;
8271               continue;
8272
8273             case 'b':           /* base register */
8274             case 'd':           /* destination register */
8275             case 's':           /* source register */
8276             case 't':           /* target register */
8277             case 'r':           /* both target and source */
8278             case 'v':           /* both dest and source */
8279             case 'w':           /* both dest and target */
8280             case 'E':           /* coprocessor target register */
8281             case 'G':           /* coprocessor destination register */
8282             case 'x':           /* ignore register name */
8283             case 'z':           /* must be zero register */
8284             case 'U':           /* destination register (clo/clz).  */
8285               s_reset = s;
8286               if (s[0] == '$')
8287                 {
8288
8289                   if (ISDIGIT (s[1]))
8290                     {
8291                       ++s;
8292                       regno = 0;
8293                       do
8294                         {
8295                           regno *= 10;
8296                           regno += *s - '0';
8297                           ++s;
8298                         }
8299                       while (ISDIGIT (*s));
8300                       if (regno > 31)
8301                         as_bad (_("Invalid register number (%d)"), regno);
8302                     }
8303                   else if (*args == 'E' || *args == 'G')
8304                     goto notreg;
8305                   else
8306                     {
8307                       if (s[1] == 'r' && s[2] == 'a')
8308                         {
8309                           s += 3;
8310                           regno = RA;
8311                         }
8312                       else if (s[1] == 'f' && s[2] == 'p')
8313                         {
8314                           s += 3;
8315                           regno = FP;
8316                         }
8317                       else if (s[1] == 's' && s[2] == 'p')
8318                         {
8319                           s += 3;
8320                           regno = SP;
8321                         }
8322                       else if (s[1] == 'g' && s[2] == 'p')
8323                         {
8324                           s += 3;
8325                           regno = GP;
8326                         }
8327                       else if (s[1] == 'a' && s[2] == 't')
8328                         {
8329                           s += 3;
8330                           regno = AT;
8331                         }
8332                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8333                         {
8334                           s += 4;
8335                           regno = KT0;
8336                         }
8337                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8338                         {
8339                           s += 4;
8340                           regno = KT1;
8341                         }
8342                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8343                         {
8344                           s += 5;
8345                           regno = ZERO;
8346                         }
8347                       else if (itbl_have_entries)
8348                         {
8349                           char *p, *n;
8350                           unsigned long r;
8351
8352                           p = s + 1;    /* advance past '$' */
8353                           n = itbl_get_field (&p);  /* n is name */
8354
8355                           /* See if this is a register defined in an
8356                              itbl entry.  */
8357                           if (itbl_get_reg_val (n, &r))
8358                             {
8359                               /* Get_field advances to the start of
8360                                  the next field, so we need to back
8361                                  rack to the end of the last field.  */
8362                               if (p)
8363                                 s = p - 1;
8364                               else
8365                                 s = strchr (s, '\0');
8366                               regno = r;
8367                             }
8368                           else
8369                             goto notreg;
8370                         }
8371                       else
8372                         goto notreg;
8373                     }
8374                   if (regno == AT
8375                       && ! mips_opts.noat
8376                       && *args != 'E'
8377                       && *args != 'G')
8378                     as_warn (_("Used $at without \".set noat\""));
8379                   c = *args;
8380                   if (*s == ' ')
8381                     ++s;
8382                   if (args[1] != *s)
8383                     {
8384                       if (c == 'r' || c == 'v' || c == 'w')
8385                         {
8386                           regno = lastregno;
8387                           s = s_reset;
8388                           ++args;
8389                         }
8390                     }
8391                   /* 'z' only matches $0.  */
8392                   if (c == 'z' && regno != 0)
8393                     break;
8394
8395         /* Now that we have assembled one operand, we use the args string
8396          * to figure out where it goes in the instruction.  */
8397                   switch (c)
8398                     {
8399                     case 'r':
8400                     case 's':
8401                     case 'v':
8402                     case 'b':
8403                       ip->insn_opcode |= regno << OP_SH_RS;
8404                       break;
8405                     case 'd':
8406                     case 'G':
8407                       ip->insn_opcode |= regno << OP_SH_RD;
8408                       break;
8409                     case 'U':
8410                       ip->insn_opcode |= regno << OP_SH_RD;
8411                       ip->insn_opcode |= regno << OP_SH_RT;
8412                       break;
8413                     case 'w':
8414                     case 't':
8415                     case 'E':
8416                       ip->insn_opcode |= regno << OP_SH_RT;
8417                       break;
8418                     case 'x':
8419                       /* This case exists because on the r3000 trunc
8420                          expands into a macro which requires a gp
8421                          register.  On the r6000 or r4000 it is
8422                          assembled into a single instruction which
8423                          ignores the register.  Thus the insn version
8424                          is MIPS_ISA2 and uses 'x', and the macro
8425                          version is MIPS_ISA1 and uses 't'.  */
8426                       break;
8427                     case 'z':
8428                       /* This case is for the div instruction, which
8429                          acts differently if the destination argument
8430                          is $0.  This only matches $0, and is checked
8431                          outside the switch.  */
8432                       break;
8433                     case 'D':
8434                       /* Itbl operand; not yet implemented. FIXME ?? */
8435                       break;
8436                       /* What about all other operands like 'i', which
8437                          can be specified in the opcode table? */
8438                     }
8439                   lastregno = regno;
8440                   continue;
8441                 }
8442             notreg:
8443               switch (*args++)
8444                 {
8445                 case 'r':
8446                 case 'v':
8447                   ip->insn_opcode |= lastregno << OP_SH_RS;
8448                   continue;
8449                 case 'w':
8450                   ip->insn_opcode |= lastregno << OP_SH_RT;
8451                   continue;
8452                 }
8453               break;
8454
8455             case 'O':           /* MDMX alignment immediate constant.  */
8456               my_getExpression (&imm_expr, s);
8457               check_absolute_expr (ip, &imm_expr);
8458               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8459                 {
8460                   as_warn ("Improper align amount (%ld), using low bits",
8461                            (long) imm_expr.X_add_number);
8462                   imm_expr.X_add_number &= OP_MASK_ALN;
8463                 }
8464               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8465               imm_expr.X_op = O_absent;
8466               s = expr_end;
8467               continue;
8468
8469             case 'Q':           /* MDMX vector, element sel, or const.  */
8470               if (s[0] != '$')
8471                 {
8472                   /* MDMX Immediate.  */
8473                   my_getExpression (&imm_expr, s);
8474                   check_absolute_expr (ip, &imm_expr);
8475                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8476                     {
8477                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8478                                (long) imm_expr.X_add_number);
8479                       imm_expr.X_add_number &= OP_MASK_FT;
8480                     }
8481                   imm_expr.X_add_number &= OP_MASK_FT;
8482                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8483                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8484                   else
8485                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8486                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8487                   imm_expr.X_op = O_absent;
8488                   s = expr_end;
8489                   continue;
8490                 }
8491               /* Not MDMX Immediate.  Fall through.  */
8492             case 'X':           /* MDMX destination register.  */
8493             case 'Y':           /* MDMX source register.  */
8494             case 'Z':           /* MDMX target register.  */
8495               is_mdmx = 1;
8496             case 'D':           /* floating point destination register */
8497             case 'S':           /* floating point source register */
8498             case 'T':           /* floating point target register */
8499             case 'R':           /* floating point source register */
8500             case 'V':
8501             case 'W':
8502               s_reset = s;
8503               /* Accept $fN for FP and MDMX register numbers, and in
8504                  addition accept $vN for MDMX register numbers.  */
8505               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8506                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8507                       && ISDIGIT (s[2])))
8508                 {
8509                   s += 2;
8510                   regno = 0;
8511                   do
8512                     {
8513                       regno *= 10;
8514                       regno += *s - '0';
8515                       ++s;
8516                     }
8517                   while (ISDIGIT (*s));
8518
8519                   if (regno > 31)
8520                     as_bad (_("Invalid float register number (%d)"), regno);
8521
8522                   if ((regno & 1) != 0
8523                       && HAVE_32BIT_FPRS
8524                       && ! (strcmp (str, "mtc1") == 0
8525                             || strcmp (str, "mfc1") == 0
8526                             || strcmp (str, "lwc1") == 0
8527                             || strcmp (str, "swc1") == 0
8528                             || strcmp (str, "l.s") == 0
8529                             || strcmp (str, "s.s") == 0))
8530                     as_warn (_("Float register should be even, was %d"),
8531                              regno);
8532
8533                   c = *args;
8534                   if (*s == ' ')
8535                     ++s;
8536                   if (args[1] != *s)
8537                     {
8538                       if (c == 'V' || c == 'W')
8539                         {
8540                           regno = lastregno;
8541                           s = s_reset;
8542                           ++args;
8543                         }
8544                     }
8545                   switch (c)
8546                     {
8547                     case 'D':
8548                     case 'X':
8549                       ip->insn_opcode |= regno << OP_SH_FD;
8550                       break;
8551                     case 'V':
8552                     case 'S':
8553                     case 'Y':
8554                       ip->insn_opcode |= regno << OP_SH_FS;
8555                       break;
8556                     case 'Q':
8557                       /* This is like 'Z', but also needs to fix the MDMX
8558                          vector/scalar select bits.  Note that the
8559                          scalar immediate case is handled above.  */
8560                       if (*s == '[')
8561                         {
8562                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8563                           int max_el = (is_qh ? 3 : 7);
8564                           s++;
8565                           my_getExpression(&imm_expr, s);
8566                           check_absolute_expr (ip, &imm_expr);
8567                           s = expr_end;
8568                           if (imm_expr.X_add_number > max_el)
8569                             as_bad(_("Bad element selector %ld"),
8570                                    (long) imm_expr.X_add_number);
8571                           imm_expr.X_add_number &= max_el;
8572                           ip->insn_opcode |= (imm_expr.X_add_number
8573                                               << (OP_SH_VSEL +
8574                                                   (is_qh ? 2 : 1)));
8575                           if (*s != ']')
8576                             as_warn(_("Expecting ']' found '%s'"), s);
8577                           else
8578                             s++;
8579                         }
8580                       else
8581                         {
8582                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8583                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8584                                                 << OP_SH_VSEL);
8585                           else
8586                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8587                                                 OP_SH_VSEL);
8588                         }
8589                       /* Fall through */
8590                     case 'W':
8591                     case 'T':
8592                     case 'Z':
8593                       ip->insn_opcode |= regno << OP_SH_FT;
8594                       break;
8595                     case 'R':
8596                       ip->insn_opcode |= regno << OP_SH_FR;
8597                       break;
8598                     }
8599                   lastregno = regno;
8600                   continue;
8601                 }
8602
8603               switch (*args++)
8604                 {
8605                 case 'V':
8606                   ip->insn_opcode |= lastregno << OP_SH_FS;
8607                   continue;
8608                 case 'W':
8609                   ip->insn_opcode |= lastregno << OP_SH_FT;
8610                   continue;
8611                 }
8612               break;
8613
8614             case 'I':
8615               my_getExpression (&imm_expr, s);
8616               if (imm_expr.X_op != O_big
8617                   && imm_expr.X_op != O_constant)
8618                 insn_error = _("absolute expression required");
8619               s = expr_end;
8620               continue;
8621
8622             case 'A':
8623               my_getExpression (&offset_expr, s);
8624               *imm_reloc = BFD_RELOC_32;
8625               s = expr_end;
8626               continue;
8627
8628             case 'F':
8629             case 'L':
8630             case 'f':
8631             case 'l':
8632               {
8633                 int f64;
8634                 int using_gprs;
8635                 char *save_in;
8636                 char *err;
8637                 unsigned char temp[8];
8638                 int len;
8639                 unsigned int length;
8640                 segT seg;
8641                 subsegT subseg;
8642                 char *p;
8643
8644                 /* These only appear as the last operand in an
8645                    instruction, and every instruction that accepts
8646                    them in any variant accepts them in all variants.
8647                    This means we don't have to worry about backing out
8648                    any changes if the instruction does not match.
8649
8650                    The difference between them is the size of the
8651                    floating point constant and where it goes.  For 'F'
8652                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8653                    is 32 bits.  Where the constant is placed is based
8654                    on how the MIPS assembler does things:
8655                     F -- .rdata
8656                     L -- .lit8
8657                     f -- immediate value
8658                     l -- .lit4
8659
8660                     The .lit4 and .lit8 sections are only used if
8661                     permitted by the -G argument.
8662
8663                     When generating embedded PIC code, we use the
8664                     .lit8 section but not the .lit4 section (we can do
8665                     .lit4 inline easily; we need to put .lit8
8666                     somewhere in the data segment, and using .lit8
8667                     permits the linker to eventually combine identical
8668                     .lit8 entries).
8669
8670                     The code below needs to know whether the target register
8671                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8672                     'F' are used with GPR-based instructions and 'l' and
8673                     'L' are used with FPR-based instructions.  */
8674
8675                 f64 = *args == 'F' || *args == 'L';
8676                 using_gprs = *args == 'F' || *args == 'f';
8677
8678                 save_in = input_line_pointer;
8679                 input_line_pointer = s;
8680                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8681                 length = len;
8682                 s = input_line_pointer;
8683                 input_line_pointer = save_in;
8684                 if (err != NULL && *err != '\0')
8685                   {
8686                     as_bad (_("Bad floating point constant: %s"), err);
8687                     memset (temp, '\0', sizeof temp);
8688                     length = f64 ? 8 : 4;
8689                   }
8690
8691                 assert (length == (unsigned) (f64 ? 8 : 4));
8692
8693                 if (*args == 'f'
8694                     || (*args == 'l'
8695                         && (! USE_GLOBAL_POINTER_OPT
8696                             || mips_pic == EMBEDDED_PIC
8697                             || g_switch_value < 4
8698                             || (temp[0] == 0 && temp[1] == 0)
8699                             || (temp[2] == 0 && temp[3] == 0))))
8700                   {
8701                     imm_expr.X_op = O_constant;
8702                     if (! target_big_endian)
8703                       imm_expr.X_add_number = bfd_getl32 (temp);
8704                     else
8705                       imm_expr.X_add_number = bfd_getb32 (temp);
8706                   }
8707                 else if (length > 4
8708                          && ! mips_disable_float_construction
8709                          /* Constants can only be constructed in GPRs and
8710                             copied to FPRs if the GPRs are at least as wide
8711                             as the FPRs.  Force the constant into memory if
8712                             we are using 64-bit FPRs but the GPRs are only
8713                             32 bits wide.  */
8714                          && (using_gprs
8715                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8716                          && ((temp[0] == 0 && temp[1] == 0)
8717                              || (temp[2] == 0 && temp[3] == 0))
8718                          && ((temp[4] == 0 && temp[5] == 0)
8719                              || (temp[6] == 0 && temp[7] == 0)))
8720                   {
8721                     /* The value is simple enough to load with a couple of
8722                        instructions.  If using 32-bit registers, set
8723                        imm_expr to the high order 32 bits and offset_expr to
8724                        the low order 32 bits.  Otherwise, set imm_expr to
8725                        the entire 64 bit constant.  */
8726                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8727                       {
8728                         imm_expr.X_op = O_constant;
8729                         offset_expr.X_op = O_constant;
8730                         if (! target_big_endian)
8731                           {
8732                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8733                             offset_expr.X_add_number = bfd_getl32 (temp);
8734                           }
8735                         else
8736                           {
8737                             imm_expr.X_add_number = bfd_getb32 (temp);
8738                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8739                           }
8740                         if (offset_expr.X_add_number == 0)
8741                           offset_expr.X_op = O_absent;
8742                       }
8743                     else if (sizeof (imm_expr.X_add_number) > 4)
8744                       {
8745                         imm_expr.X_op = O_constant;
8746                         if (! target_big_endian)
8747                           imm_expr.X_add_number = bfd_getl64 (temp);
8748                         else
8749                           imm_expr.X_add_number = bfd_getb64 (temp);
8750                       }
8751                     else
8752                       {
8753                         imm_expr.X_op = O_big;
8754                         imm_expr.X_add_number = 4;
8755                         if (! target_big_endian)
8756                           {
8757                             generic_bignum[0] = bfd_getl16 (temp);
8758                             generic_bignum[1] = bfd_getl16 (temp + 2);
8759                             generic_bignum[2] = bfd_getl16 (temp + 4);
8760                             generic_bignum[3] = bfd_getl16 (temp + 6);
8761                           }
8762                         else
8763                           {
8764                             generic_bignum[0] = bfd_getb16 (temp + 6);
8765                             generic_bignum[1] = bfd_getb16 (temp + 4);
8766                             generic_bignum[2] = bfd_getb16 (temp + 2);
8767                             generic_bignum[3] = bfd_getb16 (temp);
8768                           }
8769                       }
8770                   }
8771                 else
8772                   {
8773                     const char *newname;
8774                     segT new_seg;
8775
8776                     /* Switch to the right section.  */
8777                     seg = now_seg;
8778                     subseg = now_subseg;
8779                     switch (*args)
8780                       {
8781                       default: /* unused default case avoids warnings.  */
8782                       case 'L':
8783                         newname = RDATA_SECTION_NAME;
8784                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8785                             || mips_pic == EMBEDDED_PIC)
8786                           newname = ".lit8";
8787                         break;
8788                       case 'F':
8789                         if (mips_pic == EMBEDDED_PIC)
8790                           newname = ".lit8";
8791                         else
8792                           newname = RDATA_SECTION_NAME;
8793                         break;
8794                       case 'l':
8795                         assert (!USE_GLOBAL_POINTER_OPT
8796                                 || g_switch_value >= 4);
8797                         newname = ".lit4";
8798                         break;
8799                       }
8800                     new_seg = subseg_new (newname, (subsegT) 0);
8801                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8802                       bfd_set_section_flags (stdoutput, new_seg,
8803                                              (SEC_ALLOC
8804                                               | SEC_LOAD
8805                                               | SEC_READONLY
8806                                               | SEC_DATA));
8807                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8808                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8809                         && strcmp (TARGET_OS, "elf") != 0)
8810                       record_alignment (new_seg, 4);
8811                     else
8812                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8813                     if (seg == now_seg)
8814                       as_bad (_("Can't use floating point insn in this section"));
8815
8816                     /* Set the argument to the current address in the
8817                        section.  */
8818                     offset_expr.X_op = O_symbol;
8819                     offset_expr.X_add_symbol =
8820                       symbol_new ("L0\001", now_seg,
8821                                   (valueT) frag_now_fix (), frag_now);
8822                     offset_expr.X_add_number = 0;
8823
8824                     /* Put the floating point number into the section.  */
8825                     p = frag_more ((int) length);
8826                     memcpy (p, temp, length);
8827
8828                     /* Switch back to the original section.  */
8829                     subseg_set (seg, subseg);
8830                   }
8831               }
8832               continue;
8833
8834             case 'i':           /* 16 bit unsigned immediate */
8835             case 'j':           /* 16 bit signed immediate */
8836               *imm_reloc = BFD_RELOC_LO16;
8837               c = my_getSmallExpression (&imm_expr, s);
8838               if (c != S_EX_NONE)
8839                 {
8840                   if (c != S_EX_LO)
8841                     {
8842                       if (c == S_EX_HI)
8843                         {
8844                           *imm_reloc = BFD_RELOC_HI16_S;
8845                           imm_unmatched_hi = true;
8846                         }
8847 #ifdef OBJ_ELF
8848                       else if (c == S_EX_HIGHEST)
8849                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8850                       else if (c == S_EX_HIGHER)
8851                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8852                       else if (c == S_EX_GP_REL)
8853                         {
8854                           /* This occurs in NewABI only.  */
8855                           c = my_getSmallExpression (&imm_expr, s);
8856                           if (c != S_EX_NEG)
8857                             as_bad (_("bad composition of relocations"));
8858                           else
8859                             {
8860                               c = my_getSmallExpression (&imm_expr, s);
8861                               if (c != S_EX_LO)
8862                                 as_bad (_("bad composition of relocations"));
8863                               else
8864                                 {
8865                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8866                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8867                                   imm_reloc[2] = BFD_RELOC_LO16;
8868                                 }
8869                             }
8870                         }
8871 #endif
8872                       else
8873                         *imm_reloc = BFD_RELOC_HI16;
8874                     }
8875                   else if (imm_expr.X_op == O_constant)
8876                     imm_expr.X_add_number &= 0xffff;
8877                 }
8878               if (*args == 'i')
8879                 {
8880                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8881                       || ((imm_expr.X_add_number < 0
8882                            || imm_expr.X_add_number >= 0x10000)
8883                           && imm_expr.X_op == O_constant))
8884                     {
8885                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8886                           !strcmp (insn->name, insn[1].name))
8887                         break;
8888                       if (imm_expr.X_op == O_constant
8889                           || imm_expr.X_op == O_big)
8890                         as_bad (_("16 bit expression not in range 0..65535"));
8891                     }
8892                 }
8893               else
8894                 {
8895                   int more;
8896                   offsetT max;
8897
8898                   /* The upper bound should be 0x8000, but
8899                      unfortunately the MIPS assembler accepts numbers
8900                      from 0x8000 to 0xffff and sign extends them, and
8901                      we want to be compatible.  We only permit this
8902                      extended range for an instruction which does not
8903                      provide any further alternates, since those
8904                      alternates may handle other cases.  People should
8905                      use the numbers they mean, rather than relying on
8906                      a mysterious sign extension.  */
8907                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8908                           strcmp (insn->name, insn[1].name) == 0);
8909                   if (more)
8910                     max = 0x8000;
8911                   else
8912                     max = 0x10000;
8913                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8914                       || ((imm_expr.X_add_number < -0x8000
8915                            || imm_expr.X_add_number >= max)
8916                           && imm_expr.X_op == O_constant)
8917                       || (more
8918                           && imm_expr.X_add_number < 0
8919                           && HAVE_64BIT_GPRS
8920                           && imm_expr.X_unsigned
8921                           && sizeof (imm_expr.X_add_number) <= 4))
8922                     {
8923                       if (more)
8924                         break;
8925                       if (imm_expr.X_op == O_constant
8926                           || imm_expr.X_op == O_big)
8927                         as_bad (_("16 bit expression not in range -32768..32767"));
8928                     }
8929                 }
8930               s = expr_end;
8931               continue;
8932
8933             case 'o':           /* 16 bit offset */
8934               c = my_getSmallExpression (&offset_expr, s);
8935
8936               /* If this value won't fit into a 16 bit offset, then go
8937                  find a macro that will generate the 32 bit offset
8938                  code pattern.  */
8939               if (c == S_EX_NONE
8940                   && (offset_expr.X_op != O_constant
8941                       || offset_expr.X_add_number >= 0x8000
8942                       || offset_expr.X_add_number < -0x8000))
8943                 break;
8944
8945               if (c == S_EX_HI)
8946                 {
8947                   if (offset_expr.X_op != O_constant)
8948                     break;
8949                   offset_expr.X_add_number =
8950                     (offset_expr.X_add_number >> 16) & 0xffff;
8951                 }
8952               *offset_reloc = BFD_RELOC_LO16;
8953               s = expr_end;
8954               continue;
8955
8956             case 'p':           /* pc relative offset */
8957               if (mips_pic == EMBEDDED_PIC)
8958                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8959               else
8960                 *offset_reloc = BFD_RELOC_16_PCREL;
8961               my_getExpression (&offset_expr, s);
8962               s = expr_end;
8963               continue;
8964
8965             case 'u':           /* upper 16 bits */
8966               c = my_getSmallExpression (&imm_expr, s);
8967               *imm_reloc = BFD_RELOC_LO16;
8968               if (c != S_EX_NONE)
8969                 {
8970                   if (c != S_EX_LO)
8971                     {
8972                       if (c == S_EX_HI)
8973                         {
8974                           *imm_reloc = BFD_RELOC_HI16_S;
8975                           imm_unmatched_hi = true;
8976                         }
8977 #ifdef OBJ_ELF
8978                       else if (c == S_EX_HIGHEST)
8979                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8980                       else if (c == S_EX_GP_REL)
8981                         {
8982                           /* This occurs in NewABI only.  */
8983                           c = my_getSmallExpression (&imm_expr, s);
8984                           if (c != S_EX_NEG)
8985                             as_bad (_("bad composition of relocations"));
8986                           else
8987                             {
8988                               c = my_getSmallExpression (&imm_expr, s);
8989                               if (c != S_EX_HI)
8990                                 as_bad (_("bad composition of relocations"));
8991                               else
8992                                 {
8993                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8994                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8995                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8996                                 }
8997                             }
8998                         }
8999 #endif
9000                       else
9001                         *imm_reloc = BFD_RELOC_HI16;
9002                     }
9003                   else if (imm_expr.X_op == O_constant)
9004                     imm_expr.X_add_number &= 0xffff;
9005                 }
9006               else if (imm_expr.X_op == O_constant
9007                        && (imm_expr.X_add_number < 0
9008                            || imm_expr.X_add_number >= 0x10000))
9009                 as_bad (_("lui expression not in range 0..65535"));
9010               s = expr_end;
9011               continue;
9012
9013             case 'a':           /* 26 bit address */
9014               my_getExpression (&offset_expr, s);
9015               s = expr_end;
9016               *offset_reloc = BFD_RELOC_MIPS_JMP;
9017               continue;
9018
9019             case 'N':           /* 3 bit branch condition code */
9020             case 'M':           /* 3 bit compare condition code */
9021               if (strncmp (s, "$fcc", 4) != 0)
9022                 break;
9023               s += 4;
9024               regno = 0;
9025               do
9026                 {
9027                   regno *= 10;
9028                   regno += *s - '0';
9029                   ++s;
9030                 }
9031               while (ISDIGIT (*s));
9032               if (regno > 7)
9033                 as_bad (_("invalid condition code register $fcc%d"), regno);
9034               if (*args == 'N')
9035                 ip->insn_opcode |= regno << OP_SH_BCC;
9036               else
9037                 ip->insn_opcode |= regno << OP_SH_CCC;
9038               continue;
9039
9040             case 'H':
9041               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9042                 s += 2;
9043               if (ISDIGIT (*s))
9044                 {
9045                   c = 0;
9046                   do
9047                     {
9048                       c *= 10;
9049                       c += *s - '0';
9050                       ++s;
9051                     }
9052                   while (ISDIGIT (*s));
9053                 }
9054               else
9055                 c = 8; /* Invalid sel value.  */
9056
9057               if (c > 7)
9058                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9059               ip->insn_opcode |= c;
9060               continue;
9061
9062             case 'e':
9063               /* Must be at least one digit.  */
9064               my_getExpression (&imm_expr, s);
9065               check_absolute_expr (ip, &imm_expr);
9066
9067               if ((unsigned long) imm_expr.X_add_number
9068                   > (unsigned long) OP_MASK_VECBYTE)
9069                 {
9070                   as_bad (_("bad byte vector index (%ld)"),
9071                            (long) imm_expr.X_add_number);
9072                   imm_expr.X_add_number = 0;
9073                 }
9074
9075               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9076               imm_expr.X_op = O_absent;
9077               s = expr_end;
9078               continue;
9079
9080             case '%':
9081               my_getExpression (&imm_expr, s);
9082               check_absolute_expr (ip, &imm_expr);
9083
9084               if ((unsigned long) imm_expr.X_add_number
9085                   > (unsigned long) OP_MASK_VECALIGN)
9086                 {
9087                   as_bad (_("bad byte vector index (%ld)"),
9088                            (long) imm_expr.X_add_number);
9089                   imm_expr.X_add_number = 0;
9090                 }
9091
9092               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9093               imm_expr.X_op = O_absent;
9094               s = expr_end;
9095               continue;
9096
9097             default:
9098               as_bad (_("bad char = '%c'\n"), *args);
9099               internalError ();
9100             }
9101           break;
9102         }
9103       /* Args don't match.  */
9104       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9105           !strcmp (insn->name, insn[1].name))
9106         {
9107           ++insn;
9108           s = argsStart;
9109           insn_error = _("illegal operands");
9110           continue;
9111         }
9112       if (save_c)
9113         *(--s) = save_c;
9114       insn_error = _("illegal operands");
9115       return;
9116     }
9117 }
9118
9119 /* This routine assembles an instruction into its binary format when
9120    assembling for the mips16.  As a side effect, it sets one of the
9121    global variables imm_reloc or offset_reloc to the type of
9122    relocation to do if one of the operands is an address expression.
9123    It also sets mips16_small and mips16_ext if the user explicitly
9124    requested a small or extended instruction.  */
9125
9126 static void
9127 mips16_ip (str, ip)
9128      char *str;
9129      struct mips_cl_insn *ip;
9130 {
9131   char *s;
9132   const char *args;
9133   struct mips_opcode *insn;
9134   char *argsstart;
9135   unsigned int regno;
9136   unsigned int lastregno = 0;
9137   char *s_reset;
9138
9139   insn_error = NULL;
9140
9141   mips16_small = false;
9142   mips16_ext = false;
9143
9144   for (s = str; ISLOWER (*s); ++s)
9145     ;
9146   switch (*s)
9147     {
9148     case '\0':
9149       break;
9150
9151     case ' ':
9152       *s++ = '\0';
9153       break;
9154
9155     case '.':
9156       if (s[1] == 't' && s[2] == ' ')
9157         {
9158           *s = '\0';
9159           mips16_small = true;
9160           s += 3;
9161           break;
9162         }
9163       else if (s[1] == 'e' && s[2] == ' ')
9164         {
9165           *s = '\0';
9166           mips16_ext = true;
9167           s += 3;
9168           break;
9169         }
9170       /* Fall through.  */
9171     default:
9172       insn_error = _("unknown opcode");
9173       return;
9174     }
9175
9176   if (mips_opts.noautoextend && ! mips16_ext)
9177     mips16_small = true;
9178
9179   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9180     {
9181       insn_error = _("unrecognized opcode");
9182       return;
9183     }
9184
9185   argsstart = s;
9186   for (;;)
9187     {
9188       assert (strcmp (insn->name, str) == 0);
9189
9190       ip->insn_mo = insn;
9191       ip->insn_opcode = insn->match;
9192       ip->use_extend = false;
9193       imm_expr.X_op = O_absent;
9194       imm_reloc[0] = BFD_RELOC_UNUSED;
9195       imm_reloc[1] = BFD_RELOC_UNUSED;
9196       imm_reloc[2] = BFD_RELOC_UNUSED;
9197       offset_expr.X_op = O_absent;
9198       offset_reloc[0] = BFD_RELOC_UNUSED;
9199       offset_reloc[1] = BFD_RELOC_UNUSED;
9200       offset_reloc[2] = BFD_RELOC_UNUSED;
9201       for (args = insn->args; 1; ++args)
9202         {
9203           int c;
9204
9205           if (*s == ' ')
9206             ++s;
9207
9208           /* In this switch statement we call break if we did not find
9209              a match, continue if we did find a match, or return if we
9210              are done.  */
9211
9212           c = *args;
9213           switch (c)
9214             {
9215             case '\0':
9216               if (*s == '\0')
9217                 {
9218                   /* Stuff the immediate value in now, if we can.  */
9219                   if (imm_expr.X_op == O_constant
9220                       && *imm_reloc > BFD_RELOC_UNUSED
9221                       && insn->pinfo != INSN_MACRO)
9222                     {
9223                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9224                                     imm_expr.X_add_number, true, mips16_small,
9225                                     mips16_ext, &ip->insn_opcode,
9226                                     &ip->use_extend, &ip->extend);
9227                       imm_expr.X_op = O_absent;
9228                       *imm_reloc = BFD_RELOC_UNUSED;
9229                     }
9230
9231                   return;
9232                 }
9233               break;
9234
9235             case ',':
9236               if (*s++ == c)
9237                 continue;
9238               s--;
9239               switch (*++args)
9240                 {
9241                 case 'v':
9242                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9243                   continue;
9244                 case 'w':
9245                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9246                   continue;
9247                 }
9248               break;
9249
9250             case '(':
9251             case ')':
9252               if (*s++ == c)
9253                 continue;
9254               break;
9255
9256             case 'v':
9257             case 'w':
9258               if (s[0] != '$')
9259                 {
9260                   if (c == 'v')
9261                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9262                   else
9263                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9264                   ++args;
9265                   continue;
9266                 }
9267               /* Fall through.  */
9268             case 'x':
9269             case 'y':
9270             case 'z':
9271             case 'Z':
9272             case '0':
9273             case 'S':
9274             case 'R':
9275             case 'X':
9276             case 'Y':
9277               if (s[0] != '$')
9278                 break;
9279               s_reset = s;
9280               if (ISDIGIT (s[1]))
9281                 {
9282                   ++s;
9283                   regno = 0;
9284                   do
9285                     {
9286                       regno *= 10;
9287                       regno += *s - '0';
9288                       ++s;
9289                     }
9290                   while (ISDIGIT (*s));
9291                   if (regno > 31)
9292                     {
9293                       as_bad (_("invalid register number (%d)"), regno);
9294                       regno = 2;
9295                     }
9296                 }
9297               else
9298                 {
9299                   if (s[1] == 'r' && s[2] == 'a')
9300                     {
9301                       s += 3;
9302                       regno = RA;
9303                     }
9304                   else if (s[1] == 'f' && s[2] == 'p')
9305                     {
9306                       s += 3;
9307                       regno = FP;
9308                     }
9309                   else if (s[1] == 's' && s[2] == 'p')
9310                     {
9311                       s += 3;
9312                       regno = SP;
9313                     }
9314                   else if (s[1] == 'g' && s[2] == 'p')
9315                     {
9316                       s += 3;
9317                       regno = GP;
9318                     }
9319                   else if (s[1] == 'a' && s[2] == 't')
9320                     {
9321                       s += 3;
9322                       regno = AT;
9323                     }
9324                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9325                     {
9326                       s += 4;
9327                       regno = KT0;
9328                     }
9329                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9330                     {
9331                       s += 4;
9332                       regno = KT1;
9333                     }
9334                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9335                     {
9336                       s += 5;
9337                       regno = ZERO;
9338                     }
9339                   else
9340                     break;
9341                 }
9342
9343               if (*s == ' ')
9344                 ++s;
9345               if (args[1] != *s)
9346                 {
9347                   if (c == 'v' || c == 'w')
9348                     {
9349                       regno = mips16_to_32_reg_map[lastregno];
9350                       s = s_reset;
9351                       ++args;
9352                     }
9353                 }
9354
9355               switch (c)
9356                 {
9357                 case 'x':
9358                 case 'y':
9359                 case 'z':
9360                 case 'v':
9361                 case 'w':
9362                 case 'Z':
9363                   regno = mips32_to_16_reg_map[regno];
9364                   break;
9365
9366                 case '0':
9367                   if (regno != 0)
9368                     regno = ILLEGAL_REG;
9369                   break;
9370
9371                 case 'S':
9372                   if (regno != SP)
9373                     regno = ILLEGAL_REG;
9374                   break;
9375
9376                 case 'R':
9377                   if (regno != RA)
9378                     regno = ILLEGAL_REG;
9379                   break;
9380
9381                 case 'X':
9382                 case 'Y':
9383                   if (regno == AT && ! mips_opts.noat)
9384                     as_warn (_("used $at without \".set noat\""));
9385                   break;
9386
9387                 default:
9388                   internalError ();
9389                 }
9390
9391               if (regno == ILLEGAL_REG)
9392                 break;
9393
9394               switch (c)
9395                 {
9396                 case 'x':
9397                 case 'v':
9398                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9399                   break;
9400                 case 'y':
9401                 case 'w':
9402                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9403                   break;
9404                 case 'z':
9405                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9406                   break;
9407                 case 'Z':
9408                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9409                 case '0':
9410                 case 'S':
9411                 case 'R':
9412                   break;
9413                 case 'X':
9414                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9415                   break;
9416                 case 'Y':
9417                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9418                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9419                   break;
9420                 default:
9421                   internalError ();
9422                 }
9423
9424               lastregno = regno;
9425               continue;
9426
9427             case 'P':
9428               if (strncmp (s, "$pc", 3) == 0)
9429                 {
9430                   s += 3;
9431                   continue;
9432                 }
9433               break;
9434
9435             case '<':
9436             case '>':
9437             case '[':
9438             case ']':
9439             case '4':
9440             case '5':
9441             case 'H':
9442             case 'W':
9443             case 'D':
9444             case 'j':
9445             case '8':
9446             case 'V':
9447             case 'C':
9448             case 'U':
9449             case 'k':
9450             case 'K':
9451               if (s[0] == '%'
9452                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9453                 {
9454                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9455                      and generate the appropriate reloc.  If the text
9456                      inside %gprel is not a symbol name with an
9457                      optional offset, then we generate a normal reloc
9458                      and will probably fail later.  */
9459                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9460                   if (imm_expr.X_op == O_symbol)
9461                     {
9462                       mips16_ext = true;
9463                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9464                       s = expr_end;
9465                       ip->use_extend = true;
9466                       ip->extend = 0;
9467                       continue;
9468                     }
9469                 }
9470               else
9471                 {
9472                   /* Just pick up a normal expression.  */
9473                   my_getExpression (&imm_expr, s);
9474                 }
9475
9476               if (imm_expr.X_op == O_register)
9477                 {
9478                   /* What we thought was an expression turned out to
9479                      be a register.  */
9480
9481                   if (s[0] == '(' && args[1] == '(')
9482                     {
9483                       /* It looks like the expression was omitted
9484                          before a register indirection, which means
9485                          that the expression is implicitly zero.  We
9486                          still set up imm_expr, so that we handle
9487                          explicit extensions correctly.  */
9488                       imm_expr.X_op = O_constant;
9489                       imm_expr.X_add_number = 0;
9490                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9491                       continue;
9492                     }
9493
9494                   break;
9495                 }
9496
9497               /* We need to relax this instruction.  */
9498               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9499               s = expr_end;
9500               continue;
9501
9502             case 'p':
9503             case 'q':
9504             case 'A':
9505             case 'B':
9506             case 'E':
9507               /* We use offset_reloc rather than imm_reloc for the PC
9508                  relative operands.  This lets macros with both
9509                  immediate and address operands work correctly.  */
9510               my_getExpression (&offset_expr, s);
9511
9512               if (offset_expr.X_op == O_register)
9513                 break;
9514
9515               /* We need to relax this instruction.  */
9516               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9517               s = expr_end;
9518               continue;
9519
9520             case '6':           /* break code */
9521               my_getExpression (&imm_expr, s);
9522               check_absolute_expr (ip, &imm_expr);
9523               if ((unsigned long) imm_expr.X_add_number > 63)
9524                 {
9525                   as_warn (_("Invalid value for `%s' (%lu)"),
9526                            ip->insn_mo->name,
9527                            (unsigned long) imm_expr.X_add_number);
9528                   imm_expr.X_add_number &= 0x3f;
9529                 }
9530               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9531               imm_expr.X_op = O_absent;
9532               s = expr_end;
9533               continue;
9534
9535             case 'a':           /* 26 bit address */
9536               my_getExpression (&offset_expr, s);
9537               s = expr_end;
9538               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9539               ip->insn_opcode <<= 16;
9540               continue;
9541
9542             case 'l':           /* register list for entry macro */
9543             case 'L':           /* register list for exit macro */
9544               {
9545                 int mask;
9546
9547                 if (c == 'l')
9548                   mask = 0;
9549                 else
9550                   mask = 7 << 3;
9551                 while (*s != '\0')
9552                   {
9553                     int freg, reg1, reg2;
9554
9555                     while (*s == ' ' || *s == ',')
9556                       ++s;
9557                     if (*s != '$')
9558                       {
9559                         as_bad (_("can't parse register list"));
9560                         break;
9561                       }
9562                     ++s;
9563                     if (*s != 'f')
9564                       freg = 0;
9565                     else
9566                       {
9567                         freg = 1;
9568                         ++s;
9569                       }
9570                     reg1 = 0;
9571                     while (ISDIGIT (*s))
9572                       {
9573                         reg1 *= 10;
9574                         reg1 += *s - '0';
9575                         ++s;
9576                       }
9577                     if (*s == ' ')
9578                       ++s;
9579                     if (*s != '-')
9580                       reg2 = reg1;
9581                     else
9582                       {
9583                         ++s;
9584                         if (*s != '$')
9585                           break;
9586                         ++s;
9587                         if (freg)
9588                           {
9589                             if (*s == 'f')
9590                               ++s;
9591                             else
9592                               {
9593                                 as_bad (_("invalid register list"));
9594                                 break;
9595                               }
9596                           }
9597                         reg2 = 0;
9598                         while (ISDIGIT (*s))
9599                           {
9600                             reg2 *= 10;
9601                             reg2 += *s - '0';
9602                             ++s;
9603                           }
9604                       }
9605                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9606                       {
9607                         mask &= ~ (7 << 3);
9608                         mask |= 5 << 3;
9609                       }
9610                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9611                       {
9612                         mask &= ~ (7 << 3);
9613                         mask |= 6 << 3;
9614                       }
9615                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9616                       mask |= (reg2 - 3) << 3;
9617                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9618                       mask |= (reg2 - 15) << 1;
9619                     else if (reg1 == RA && reg2 == RA)
9620                       mask |= 1;
9621                     else
9622                       {
9623                         as_bad (_("invalid register list"));
9624                         break;
9625                       }
9626                   }
9627                 /* The mask is filled in in the opcode table for the
9628                    benefit of the disassembler.  We remove it before
9629                    applying the actual mask.  */
9630                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9631                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9632               }
9633             continue;
9634
9635             case 'e':           /* extend code */
9636               my_getExpression (&imm_expr, s);
9637               check_absolute_expr (ip, &imm_expr);
9638               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9639                 {
9640                   as_warn (_("Invalid value for `%s' (%lu)"),
9641                            ip->insn_mo->name,
9642                            (unsigned long) imm_expr.X_add_number);
9643                   imm_expr.X_add_number &= 0x7ff;
9644                 }
9645               ip->insn_opcode |= imm_expr.X_add_number;
9646               imm_expr.X_op = O_absent;
9647               s = expr_end;
9648               continue;
9649
9650             default:
9651               internalError ();
9652             }
9653           break;
9654         }
9655
9656       /* Args don't match.  */
9657       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9658           strcmp (insn->name, insn[1].name) == 0)
9659         {
9660           ++insn;
9661           s = argsstart;
9662           continue;
9663         }
9664
9665       insn_error = _("illegal operands");
9666
9667       return;
9668     }
9669 }
9670
9671 /* This structure holds information we know about a mips16 immediate
9672    argument type.  */
9673
9674 struct mips16_immed_operand
9675 {
9676   /* The type code used in the argument string in the opcode table.  */
9677   int type;
9678   /* The number of bits in the short form of the opcode.  */
9679   int nbits;
9680   /* The number of bits in the extended form of the opcode.  */
9681   int extbits;
9682   /* The amount by which the short form is shifted when it is used;
9683      for example, the sw instruction has a shift count of 2.  */
9684   int shift;
9685   /* The amount by which the short form is shifted when it is stored
9686      into the instruction code.  */
9687   int op_shift;
9688   /* Non-zero if the short form is unsigned.  */
9689   int unsp;
9690   /* Non-zero if the extended form is unsigned.  */
9691   int extu;
9692   /* Non-zero if the value is PC relative.  */
9693   int pcrel;
9694 };
9695
9696 /* The mips16 immediate operand types.  */
9697
9698 static const struct mips16_immed_operand mips16_immed_operands[] =
9699 {
9700   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9701   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9702   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9703   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9704   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9705   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9706   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9707   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9708   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9709   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9710   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9711   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9712   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9713   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9714   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9715   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9716   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9717   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9718   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9719   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9720   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9721 };
9722
9723 #define MIPS16_NUM_IMMED \
9724   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9725
9726 /* Handle a mips16 instruction with an immediate value.  This or's the
9727    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9728    whether an extended value is needed; if one is needed, it sets
9729    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9730    If SMALL is true, an unextended opcode was explicitly requested.
9731    If EXT is true, an extended opcode was explicitly requested.  If
9732    WARN is true, warn if EXT does not match reality.  */
9733
9734 static void
9735 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9736               extend)
9737      char *file;
9738      unsigned int line;
9739      int type;
9740      offsetT val;
9741      boolean warn;
9742      boolean small;
9743      boolean ext;
9744      unsigned long *insn;
9745      boolean *use_extend;
9746      unsigned short *extend;
9747 {
9748   register const struct mips16_immed_operand *op;
9749   int mintiny, maxtiny;
9750   boolean needext;
9751
9752   op = mips16_immed_operands;
9753   while (op->type != type)
9754     {
9755       ++op;
9756       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9757     }
9758
9759   if (op->unsp)
9760     {
9761       if (type == '<' || type == '>' || type == '[' || type == ']')
9762         {
9763           mintiny = 1;
9764           maxtiny = 1 << op->nbits;
9765         }
9766       else
9767         {
9768           mintiny = 0;
9769           maxtiny = (1 << op->nbits) - 1;
9770         }
9771     }
9772   else
9773     {
9774       mintiny = - (1 << (op->nbits - 1));
9775       maxtiny = (1 << (op->nbits - 1)) - 1;
9776     }
9777
9778   /* Branch offsets have an implicit 0 in the lowest bit.  */
9779   if (type == 'p' || type == 'q')
9780     val /= 2;
9781
9782   if ((val & ((1 << op->shift) - 1)) != 0
9783       || val < (mintiny << op->shift)
9784       || val > (maxtiny << op->shift))
9785     needext = true;
9786   else
9787     needext = false;
9788
9789   if (warn && ext && ! needext)
9790     as_warn_where (file, line,
9791                    _("extended operand requested but not required"));
9792   if (small && needext)
9793     as_bad_where (file, line, _("invalid unextended operand value"));
9794
9795   if (small || (! ext && ! needext))
9796     {
9797       int insnval;
9798
9799       *use_extend = false;
9800       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9801       insnval <<= op->op_shift;
9802       *insn |= insnval;
9803     }
9804   else
9805     {
9806       long minext, maxext;
9807       int extval;
9808
9809       if (op->extu)
9810         {
9811           minext = 0;
9812           maxext = (1 << op->extbits) - 1;
9813         }
9814       else
9815         {
9816           minext = - (1 << (op->extbits - 1));
9817           maxext = (1 << (op->extbits - 1)) - 1;
9818         }
9819       if (val < minext || val > maxext)
9820         as_bad_where (file, line,
9821                       _("operand value out of range for instruction"));
9822
9823       *use_extend = true;
9824       if (op->extbits == 16)
9825         {
9826           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9827           val &= 0x1f;
9828         }
9829       else if (op->extbits == 15)
9830         {
9831           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9832           val &= 0xf;
9833         }
9834       else
9835         {
9836           extval = ((val & 0x1f) << 6) | (val & 0x20);
9837           val = 0;
9838         }
9839
9840       *extend = (unsigned short) extval;
9841       *insn |= val;
9842     }
9843 }
9844 \f
9845 static struct percent_op_match
9846 {
9847    const char *str;
9848    const enum small_ex_type type;
9849 } percent_op[] =
9850 {
9851   {"%lo", S_EX_LO},
9852 #ifdef OBJ_ELF
9853   {"%call_hi", S_EX_CALL_HI},
9854   {"%call_lo", S_EX_CALL_LO},
9855   {"%call16", S_EX_CALL16},
9856   {"%got_disp", S_EX_GOT_DISP},
9857   {"%got_page", S_EX_GOT_PAGE},
9858   {"%got_ofst", S_EX_GOT_OFST},
9859   {"%got_hi", S_EX_GOT_HI},
9860   {"%got_lo", S_EX_GOT_LO},
9861   {"%got", S_EX_GOT},
9862   {"%gp_rel", S_EX_GP_REL},
9863   {"%half", S_EX_HALF},
9864   {"%highest", S_EX_HIGHEST},
9865   {"%higher", S_EX_HIGHER},
9866   {"%neg", S_EX_NEG},
9867 #endif
9868   {"%hi", S_EX_HI}
9869 };
9870
9871 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9872    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9873    can be nested, this is handled by blanking the innermost, parsing the
9874    rest by subsequent calls.  */
9875
9876 static int
9877 my_getSmallParser (str, len, nestlevel)
9878      char **str;
9879      unsigned int *len;
9880      int *nestlevel;
9881 {
9882   *len = 0;
9883   *str += strspn (*str, " \t");
9884   /* Check for expression in parentheses.  */
9885   if (**str == '(')
9886     {
9887       char *b = *str + 1 + strspn (*str + 1, " \t");
9888       char *e;
9889
9890       /* Check for base register.  */
9891       if (b[0] == '$')
9892         {
9893           if (strchr (b, ')')
9894               && (e = b + strcspn (b, ") \t"))
9895               && e - b > 1 && e - b < 4)
9896             {
9897               if ((e - b == 3
9898                    && ((b[1] == 'f' && b[2] == 'p')
9899                        || (b[1] == 's' && b[2] == 'p')
9900                        || (b[1] == 'g' && b[2] == 'p')
9901                        || (b[1] == 'a' && b[2] == 't')
9902                        || (ISDIGIT (b[1])
9903                            && ISDIGIT (b[2]))))
9904                   || (ISDIGIT (b[1])))
9905                 {
9906                   *len = strcspn (*str, ")") + 1;
9907                   return S_EX_REGISTER;
9908                 }
9909             }
9910         }
9911       /* Check for percent_op (in parentheses).  */
9912       else if (b[0] == '%')
9913         {
9914           *str = b;
9915           return my_getPercentOp (str, len, nestlevel);
9916         }
9917
9918       /* Some other expression in the parentheses, which can contain
9919          parentheses itself. Attempt to find the matching one.  */
9920       {
9921         int pcnt = 1;
9922         char *s;
9923
9924         *len = 1;
9925         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9926           {
9927             if (*s == '(')
9928               ++pcnt;
9929             else if (*s == ')')
9930               --pcnt;
9931           }
9932       }
9933     }
9934   /* Check for percent_op (outside of parentheses).  */
9935   else if (*str[0] == '%')
9936     return my_getPercentOp (str, len, nestlevel);
9937
9938   /* Any other expression.  */
9939   return S_EX_NONE;
9940 }
9941
9942 static int
9943 my_getPercentOp (str, len, nestlevel)
9944      char **str;
9945      unsigned int *len;
9946      int *nestlevel;
9947 {
9948   char *tmp = *str + 1;
9949   unsigned int i = 0;
9950
9951   while (ISALPHA (*tmp) || *tmp == '_')
9952     {
9953       *tmp = TOLOWER (*tmp);
9954       tmp++;
9955     }
9956   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9957     {
9958       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9959         i++;
9960       else
9961         {
9962           int type = percent_op[i].type;
9963
9964           /* Only %hi and %lo are allowed for OldABI.  */
9965           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9966             return S_EX_NONE;
9967
9968           *len = strlen (percent_op[i].str);
9969           ++(*nestlevel);
9970           return type;
9971         }
9972     }
9973   return S_EX_NONE;
9974 }
9975
9976 static int
9977 my_getSmallExpression (ep, str)
9978      expressionS *ep;
9979      char *str;
9980 {
9981   static char *oldstr = NULL;
9982   int c = S_EX_NONE;
9983   int oldc;
9984   int nestlevel = -1;
9985   unsigned int len;
9986
9987   /* Don't update oldstr if the last call had nested percent_op's. We need
9988      it to parse the outer ones later.  */
9989   if (! oldstr)
9990     oldstr = str;
9991
9992   do
9993     {
9994       oldc = c;
9995       c = my_getSmallParser (&str, &len, &nestlevel);
9996       if (c != S_EX_NONE && c != S_EX_REGISTER)
9997         str += len;
9998     }
9999   while (c != S_EX_NONE && c != S_EX_REGISTER);
10000
10001   if (nestlevel >= 0)
10002     {
10003       /* A percent_op was encountered.  Don't try to get an expression if
10004          it is already blanked out.  */
10005       if (*(str + strspn (str + 1, " )")) != ')')
10006         {
10007           char save;
10008
10009           /* Let my_getExpression() stop at the closing parenthesis.  */
10010           save = *(str + len);
10011           *(str + len) = '\0';
10012           my_getExpression (ep, str);
10013           *(str + len) = save;
10014         }
10015       if (nestlevel > 0)
10016         {
10017           /* Blank out including the % sign and the proper matching
10018              parenthesis.  */
10019           int pcnt = 1;
10020           char *s = strrchr (oldstr, '%');
10021           char *end;
10022
10023           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
10024             {
10025               if (*end == '(')
10026                 ++pcnt;
10027               else if (*end == ')')
10028                 --pcnt;
10029             }
10030
10031           memset (s, ' ', end - s);
10032           str = oldstr;
10033         }
10034       else
10035         expr_end = str + len;
10036
10037       c = oldc;
10038     }
10039   else if (c == S_EX_NONE)
10040     {
10041       my_getExpression (ep, str);
10042     }
10043   else if (c == S_EX_REGISTER)
10044     {
10045       ep->X_op = O_constant;
10046       expr_end = str;
10047       ep->X_add_symbol = NULL;
10048       ep->X_op_symbol = NULL;
10049       ep->X_add_number = 0;
10050     }
10051   else
10052     {
10053       as_fatal (_("internal error"));
10054     }
10055
10056   if (nestlevel <= 0)
10057     /* All percent_op's have been handled.  */
10058     oldstr = NULL;
10059
10060   return c;
10061 }
10062
10063 static void
10064 my_getExpression (ep, str)
10065      expressionS *ep;
10066      char *str;
10067 {
10068   char *save_in;
10069   valueT val;
10070
10071   save_in = input_line_pointer;
10072   input_line_pointer = str;
10073   expression (ep);
10074   expr_end = input_line_pointer;
10075   input_line_pointer = save_in;
10076
10077   /* If we are in mips16 mode, and this is an expression based on `.',
10078      then we bump the value of the symbol by 1 since that is how other
10079      text symbols are handled.  We don't bother to handle complex
10080      expressions, just `.' plus or minus a constant.  */
10081   if (mips_opts.mips16
10082       && ep->X_op == O_symbol
10083       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10084       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10085       && symbol_get_frag (ep->X_add_symbol) == frag_now
10086       && symbol_constant_p (ep->X_add_symbol)
10087       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10088     S_SET_VALUE (ep->X_add_symbol, val + 1);
10089 }
10090
10091 /* Turn a string in input_line_pointer into a floating point constant
10092    of type TYPE, and store the appropriate bytes in *LITP.  The number
10093    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10094    returned, or NULL on OK.  */
10095
10096 char *
10097 md_atof (type, litP, sizeP)
10098      int type;
10099      char *litP;
10100      int *sizeP;
10101 {
10102   int prec;
10103   LITTLENUM_TYPE words[4];
10104   char *t;
10105   int i;
10106
10107   switch (type)
10108     {
10109     case 'f':
10110       prec = 2;
10111       break;
10112
10113     case 'd':
10114       prec = 4;
10115       break;
10116
10117     default:
10118       *sizeP = 0;
10119       return _("bad call to md_atof");
10120     }
10121
10122   t = atof_ieee (input_line_pointer, type, words);
10123   if (t)
10124     input_line_pointer = t;
10125
10126   *sizeP = prec * 2;
10127
10128   if (! target_big_endian)
10129     {
10130       for (i = prec - 1; i >= 0; i--)
10131         {
10132           md_number_to_chars (litP, (valueT) words[i], 2);
10133           litP += 2;
10134         }
10135     }
10136   else
10137     {
10138       for (i = 0; i < prec; i++)
10139         {
10140           md_number_to_chars (litP, (valueT) words[i], 2);
10141           litP += 2;
10142         }
10143     }
10144
10145   return NULL;
10146 }
10147
10148 void
10149 md_number_to_chars (buf, val, n)
10150      char *buf;
10151      valueT val;
10152      int n;
10153 {
10154   if (target_big_endian)
10155     number_to_chars_bigendian (buf, val, n);
10156   else
10157     number_to_chars_littleendian (buf, val, n);
10158 }
10159 \f
10160 #ifdef OBJ_ELF
10161 static int support_64bit_objects(void)
10162 {
10163   const char **list, **l;
10164   int yes;
10165
10166   list = bfd_target_list ();
10167   for (l = list; *l != NULL; l++)
10168 #ifdef TE_TMIPS
10169     /* This is traditional mips */
10170     if (strcmp (*l, "elf64-tradbigmips") == 0
10171         || strcmp (*l, "elf64-tradlittlemips") == 0)
10172 #else
10173     if (strcmp (*l, "elf64-bigmips") == 0
10174         || strcmp (*l, "elf64-littlemips") == 0)
10175 #endif
10176       break;
10177   yes = (*l != NULL);
10178   free (list);
10179   return yes;
10180 }
10181 #endif /* OBJ_ELF */
10182
10183 const char *md_shortopts = "nO::g::G:";
10184
10185 struct option md_longopts[] =
10186 {
10187 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10188   {"mips0", no_argument, NULL, OPTION_MIPS1},
10189   {"mips1", no_argument, NULL, OPTION_MIPS1},
10190 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10191   {"mips2", no_argument, NULL, OPTION_MIPS2},
10192 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10193   {"mips3", no_argument, NULL, OPTION_MIPS3},
10194 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10195   {"mips4", no_argument, NULL, OPTION_MIPS4},
10196 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10197   {"mips5", no_argument, NULL, OPTION_MIPS5},
10198 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10199   {"mips32", no_argument, NULL, OPTION_MIPS32},
10200 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10201   {"mips64", no_argument, NULL, OPTION_MIPS64},
10202 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10203   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10204 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10205   {"trap", no_argument, NULL, OPTION_TRAP},
10206   {"no-break", no_argument, NULL, OPTION_TRAP},
10207 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10208   {"break", no_argument, NULL, OPTION_BREAK},
10209   {"no-trap", no_argument, NULL, OPTION_BREAK},
10210 #define OPTION_EB (OPTION_MD_BASE + 11)
10211   {"EB", no_argument, NULL, OPTION_EB},
10212 #define OPTION_EL (OPTION_MD_BASE + 12)
10213   {"EL", no_argument, NULL, OPTION_EL},
10214 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10215   {"mips16", no_argument, NULL, OPTION_MIPS16},
10216 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10217   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10218 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10219   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10220 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10221   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10222   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10223 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10224   {"mfp32", no_argument, NULL, OPTION_FP32},
10225 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10226   {"mgp32", no_argument, NULL, OPTION_GP32},
10227 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10228   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10229 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10230   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10231 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10232   {"march", required_argument, NULL, OPTION_MARCH},
10233 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10234   {"mtune", required_argument, NULL, OPTION_MTUNE},
10235 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10236   {"mfp64", no_argument, NULL, OPTION_FP64},
10237 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10238   {"m4650", no_argument, NULL, OPTION_M4650},
10239 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10240   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10241 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10242   {"m4010", no_argument, NULL, OPTION_M4010},
10243 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10244   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10245 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10246   {"m4100", no_argument, NULL, OPTION_M4100},
10247 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10248   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10249 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10250   {"m3900", no_argument, NULL, OPTION_M3900},
10251 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10252   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10253 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10254   {"mgp64", no_argument, NULL, OPTION_GP64},
10255 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10256   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10257 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10258   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10259 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10260   {"mdmx", no_argument, NULL, OPTION_MDMX},
10261 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10262   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10263 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10264 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10265   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10266   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10267 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10268 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10269   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10270   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10271 #ifdef OBJ_ELF
10272 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 41)
10273 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10274   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10275   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10276 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10277   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10278 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10279   {"xgot",        no_argument, NULL, OPTION_XGOT},
10280 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10281   {"mabi", required_argument, NULL, OPTION_MABI},
10282 #define OPTION_32          (OPTION_ELF_BASE + 4)
10283   {"32",          no_argument, NULL, OPTION_32},
10284 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10285   {"n32",         no_argument, NULL, OPTION_N32},
10286 #define OPTION_64          (OPTION_ELF_BASE + 6)
10287   {"64",          no_argument, NULL, OPTION_64},
10288 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10289   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10290 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10291   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10292 #endif /* OBJ_ELF */
10293   {NULL, no_argument, NULL, 0}
10294 };
10295 size_t md_longopts_size = sizeof (md_longopts);
10296
10297 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10298    NEW_VALUE.  Warn if another value was already specified.  Note:
10299    we have to defer parsing the -march and -mtune arguments in order
10300    to handle 'from-abi' correctly, since the ABI might be specified
10301    in a later argument.  */
10302
10303 static void
10304 mips_set_option_string (string_ptr, new_value)
10305      const char **string_ptr, *new_value;
10306 {
10307   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10308     as_warn (_("A different %s was already specified, is now %s"),
10309              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10310              new_value);
10311
10312   *string_ptr = new_value;
10313 }
10314
10315 int
10316 md_parse_option (c, arg)
10317      int c;
10318      char *arg;
10319 {
10320   switch (c)
10321     {
10322     case OPTION_CONSTRUCT_FLOATS:
10323       mips_disable_float_construction = 0;
10324       break;
10325
10326     case OPTION_NO_CONSTRUCT_FLOATS:
10327       mips_disable_float_construction = 1;
10328       break;
10329
10330     case OPTION_TRAP:
10331       mips_trap = 1;
10332       break;
10333
10334     case OPTION_BREAK:
10335       mips_trap = 0;
10336       break;
10337
10338     case OPTION_EB:
10339       target_big_endian = 1;
10340       break;
10341
10342     case OPTION_EL:
10343       target_big_endian = 0;
10344       break;
10345
10346     case 'n':
10347       warn_nops = 1;
10348       break;
10349
10350     case 'O':
10351       if (arg && arg[1] == '0')
10352         mips_optimize = 1;
10353       else
10354         mips_optimize = 2;
10355       break;
10356
10357     case 'g':
10358       if (arg == NULL)
10359         mips_debug = 2;
10360       else
10361         mips_debug = atoi (arg);
10362       /* When the MIPS assembler sees -g or -g2, it does not do
10363          optimizations which limit full symbolic debugging.  We take
10364          that to be equivalent to -O0.  */
10365       if (mips_debug == 2)
10366         mips_optimize = 1;
10367       break;
10368
10369     case OPTION_MIPS1:
10370       file_mips_isa = ISA_MIPS1;
10371       break;
10372
10373     case OPTION_MIPS2:
10374       file_mips_isa = ISA_MIPS2;
10375       break;
10376
10377     case OPTION_MIPS3:
10378       file_mips_isa = ISA_MIPS3;
10379       break;
10380
10381     case OPTION_MIPS4:
10382       file_mips_isa = ISA_MIPS4;
10383       break;
10384
10385     case OPTION_MIPS5:
10386       file_mips_isa = ISA_MIPS5;
10387       break;
10388
10389     case OPTION_MIPS32:
10390       file_mips_isa = ISA_MIPS32;
10391       break;
10392
10393     case OPTION_MIPS64:
10394       file_mips_isa = ISA_MIPS64;
10395       break;
10396
10397     case OPTION_MTUNE:
10398       mips_set_option_string (&mips_tune_string, arg);
10399       break;
10400
10401     case OPTION_MARCH:
10402       mips_set_option_string (&mips_arch_string, arg);
10403       break;
10404
10405     case OPTION_M4650:
10406       mips_set_option_string (&mips_arch_string, "4650");
10407       mips_set_option_string (&mips_tune_string, "4650");
10408       break;
10409
10410     case OPTION_NO_M4650:
10411       break;
10412
10413     case OPTION_M4010:
10414       mips_set_option_string (&mips_arch_string, "4010");
10415       mips_set_option_string (&mips_tune_string, "4010");
10416       break;
10417
10418     case OPTION_NO_M4010:
10419       break;
10420
10421     case OPTION_M4100:
10422       mips_set_option_string (&mips_arch_string, "4100");
10423       mips_set_option_string (&mips_tune_string, "4100");
10424       break;
10425
10426     case OPTION_NO_M4100:
10427       break;
10428
10429     case OPTION_M3900:
10430       mips_set_option_string (&mips_arch_string, "3900");
10431       mips_set_option_string (&mips_tune_string, "3900");
10432       break;
10433
10434     case OPTION_NO_M3900:
10435       break;
10436
10437     case OPTION_MDMX:
10438       mips_opts.ase_mdmx = 1;
10439       break;
10440
10441     case OPTION_NO_MDMX:
10442       mips_opts.ase_mdmx = 0;
10443       break;
10444
10445     case OPTION_MIPS16:
10446       mips_opts.mips16 = 1;
10447       mips_no_prev_insn (false);
10448       break;
10449
10450     case OPTION_NO_MIPS16:
10451       mips_opts.mips16 = 0;
10452       mips_no_prev_insn (false);
10453       break;
10454
10455     case OPTION_MIPS3D:
10456       mips_opts.ase_mips3d = 1;
10457       break;
10458
10459     case OPTION_NO_MIPS3D:
10460       mips_opts.ase_mips3d = 0;
10461       break;
10462
10463     case OPTION_MEMBEDDED_PIC:
10464       mips_pic = EMBEDDED_PIC;
10465       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10466         {
10467           as_bad (_("-G may not be used with embedded PIC code"));
10468           return 0;
10469         }
10470       g_switch_value = 0x7fffffff;
10471       break;
10472
10473     case OPTION_FIX_VR4122:
10474       mips_fix_4122_bugs = 1;
10475       break;
10476
10477     case OPTION_NO_FIX_VR4122:
10478       mips_fix_4122_bugs = 0;
10479       break;
10480
10481     case OPTION_RELAX_BRANCH:
10482       mips_relax_branch = 1;
10483       break;
10484
10485     case OPTION_NO_RELAX_BRANCH:
10486       mips_relax_branch = 0;
10487       break;
10488
10489 #ifdef OBJ_ELF
10490       /* When generating ELF code, we permit -KPIC and -call_shared to
10491          select SVR4_PIC, and -non_shared to select no PIC.  This is
10492          intended to be compatible with Irix 5.  */
10493     case OPTION_CALL_SHARED:
10494       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10495         {
10496           as_bad (_("-call_shared is supported only for ELF format"));
10497           return 0;
10498         }
10499       mips_pic = SVR4_PIC;
10500       if (g_switch_seen && g_switch_value != 0)
10501         {
10502           as_bad (_("-G may not be used with SVR4 PIC code"));
10503           return 0;
10504         }
10505       g_switch_value = 0;
10506       break;
10507
10508     case OPTION_NON_SHARED:
10509       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10510         {
10511           as_bad (_("-non_shared is supported only for ELF format"));
10512           return 0;
10513         }
10514       mips_pic = NO_PIC;
10515       break;
10516
10517       /* The -xgot option tells the assembler to use 32 offsets when
10518          accessing the got in SVR4_PIC mode.  It is for Irix
10519          compatibility.  */
10520     case OPTION_XGOT:
10521       mips_big_got = 1;
10522       break;
10523 #endif /* OBJ_ELF */
10524
10525     case 'G':
10526       if (! USE_GLOBAL_POINTER_OPT)
10527         {
10528           as_bad (_("-G is not supported for this configuration"));
10529           return 0;
10530         }
10531       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10532         {
10533           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10534           return 0;
10535         }
10536       else
10537         g_switch_value = atoi (arg);
10538       g_switch_seen = 1;
10539       break;
10540
10541 #ifdef OBJ_ELF
10542       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10543          and -mabi=64.  */
10544     case OPTION_32:
10545       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10546         {
10547           as_bad (_("-32 is supported for ELF format only"));
10548           return 0;
10549         }
10550       mips_abi = O32_ABI;
10551       break;
10552
10553     case OPTION_N32:
10554       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10555         {
10556           as_bad (_("-n32 is supported for ELF format only"));
10557           return 0;
10558         }
10559       mips_abi = N32_ABI;
10560       break;
10561
10562     case OPTION_64:
10563       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10564         {
10565           as_bad (_("-64 is supported for ELF format only"));
10566           return 0;
10567         }
10568       mips_abi = N64_ABI;
10569       if (! support_64bit_objects())
10570         as_fatal (_("No compiled in support for 64 bit object file format"));
10571       break;
10572 #endif /* OBJ_ELF */
10573
10574     case OPTION_GP32:
10575       file_mips_gp32 = 1;
10576       break;
10577
10578     case OPTION_GP64:
10579       file_mips_gp32 = 0;
10580       break;
10581
10582     case OPTION_FP32:
10583       file_mips_fp32 = 1;
10584       break;
10585
10586     case OPTION_FP64:
10587       file_mips_fp32 = 0;
10588       break;
10589
10590 #ifdef OBJ_ELF
10591     case OPTION_MABI:
10592       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10593         {
10594           as_bad (_("-mabi is supported for ELF format only"));
10595           return 0;
10596         }
10597       if (strcmp (arg, "32") == 0)
10598         mips_abi = O32_ABI;
10599       else if (strcmp (arg, "o64") == 0)
10600         mips_abi = O64_ABI;
10601       else if (strcmp (arg, "n32") == 0)
10602         mips_abi = N32_ABI;
10603       else if (strcmp (arg, "64") == 0)
10604         {
10605           mips_abi = N64_ABI;
10606           if (! support_64bit_objects())
10607             as_fatal (_("No compiled in support for 64 bit object file "
10608                         "format"));
10609         }
10610       else if (strcmp (arg, "eabi") == 0)
10611         mips_abi = EABI_ABI;
10612       else
10613         {
10614           as_fatal (_("invalid abi -mabi=%s"), arg);
10615           return 0;
10616         }
10617       break;
10618 #endif /* OBJ_ELF */
10619
10620     case OPTION_M7000_HILO_FIX:
10621       mips_7000_hilo_fix = true;
10622       break;
10623
10624     case OPTION_MNO_7000_HILO_FIX:
10625       mips_7000_hilo_fix = false;
10626       break;
10627
10628 #ifdef OBJ_ELF
10629     case OPTION_MDEBUG:
10630       mips_flag_mdebug = true;
10631       break;
10632
10633     case OPTION_NO_MDEBUG:
10634       mips_flag_mdebug = false;
10635       break;
10636 #endif /* OBJ_ELF */
10637
10638     default:
10639       return 0;
10640     }
10641
10642   return 1;
10643 }
10644 \f
10645 /* Set up globals to generate code for the ISA or processor
10646    described by INFO.  */
10647
10648 static void
10649 mips_set_architecture (info)
10650      const struct mips_cpu_info *info;
10651 {
10652   if (info != 0)
10653     {
10654       mips_arch_info = info;
10655       mips_arch = info->cpu;
10656       mips_opts.isa = info->isa;
10657     }
10658 }
10659
10660
10661 /* Likewise for tuning.  */
10662
10663 static void
10664 mips_set_tune (info)
10665      const struct mips_cpu_info *info;
10666 {
10667   if (info != 0)
10668     {
10669       mips_tune_info = info;
10670       mips_tune = info->cpu;
10671     }
10672 }
10673
10674
10675 void
10676 mips_after_parse_args ()
10677 {
10678   /* GP relative stuff not working for PE */
10679   if (strncmp (TARGET_OS, "pe", 2) == 0
10680       && g_switch_value != 0)
10681     {
10682       if (g_switch_seen)
10683         as_bad (_("-G not supported in this configuration."));
10684       g_switch_value = 0;
10685     }
10686
10687   /* The following code determines the architecture and register size.
10688      Similar code was added to GCC 3.3 (see override_options() in
10689      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10690      as much as possible.  */
10691
10692   if (mips_arch_string != 0)
10693     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10694
10695   if (mips_tune_string != 0)
10696     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10697
10698   if (file_mips_isa != ISA_UNKNOWN)
10699     {
10700       /* Handle -mipsN.  At this point, file_mips_isa contains the
10701          ISA level specified by -mipsN, while mips_opts.isa contains
10702          the -march selection (if any).  */
10703       if (mips_arch_info != 0)
10704         {
10705           /* -march takes precedence over -mipsN, since it is more descriptive.
10706              There's no harm in specifying both as long as the ISA levels
10707              are the same.  */
10708           if (file_mips_isa != mips_opts.isa)
10709             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10710                     mips_cpu_info_from_isa (file_mips_isa)->name,
10711                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10712         }
10713       else
10714         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10715     }
10716
10717   if (mips_arch_info == 0)
10718     mips_set_architecture (mips_parse_cpu ("default CPU",
10719                                            MIPS_CPU_STRING_DEFAULT));
10720
10721   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10722     as_bad ("-march=%s is not compatible with the selected ABI",
10723             mips_arch_info->name);
10724
10725   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10726   if (mips_tune_info == 0)
10727     mips_set_tune (mips_arch_info);
10728
10729   if (file_mips_gp32 >= 0)
10730     {
10731       /* The user specified the size of the integer registers.  Make sure
10732          it agrees with the ABI and ISA.  */
10733       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10734         as_bad (_("-mgp64 used with a 32-bit processor"));
10735       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10736         as_bad (_("-mgp32 used with a 64-bit ABI"));
10737       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10738         as_bad (_("-mgp64 used with a 32-bit ABI"));
10739     }
10740   else
10741     {
10742       /* Infer the integer register size from the ABI and processor.
10743          Restrict ourselves to 32-bit registers if that's all the
10744          processor has, or if the ABI cannot handle 64-bit registers.  */
10745       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10746                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10747     }
10748
10749   /* ??? GAS treats single-float processors as though they had 64-bit
10750      float registers (although it complains when double-precision
10751      instructions are used).  As things stand, saying they have 32-bit
10752      registers would lead to spurious "register must be even" messages.
10753      So here we assume float registers are always the same size as
10754      integer ones, unless the user says otherwise.  */
10755   if (file_mips_fp32 < 0)
10756     file_mips_fp32 = file_mips_gp32;
10757
10758   /* End of GCC-shared inference code.  */
10759
10760   /* ??? When do we want this flag to be set?   Who uses it?  */
10761   if (file_mips_gp32 == 1
10762       && mips_abi == NO_ABI
10763       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10764     mips_32bitmode = 1;
10765
10766   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10767     as_bad (_("trap exception not supported at ISA 1"));
10768
10769   /* If the selected architecture includes support for ASEs, enable
10770      generation of code for them.  */
10771   if (mips_opts.mips16 == -1)
10772     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10773   if (mips_opts.ase_mips3d == -1)
10774     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10775   if (mips_opts.ase_mdmx == -1)
10776     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10777
10778   file_mips_isa = mips_opts.isa;
10779   file_ase_mips16 = mips_opts.mips16;
10780   file_ase_mips3d = mips_opts.ase_mips3d;
10781   file_ase_mdmx = mips_opts.ase_mdmx;
10782   mips_opts.gp32 = file_mips_gp32;
10783   mips_opts.fp32 = file_mips_fp32;
10784
10785   if (mips_flag_mdebug < 0)
10786     {
10787 #ifdef OBJ_MAYBE_ECOFF
10788       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10789         mips_flag_mdebug = 1;
10790       else
10791 #endif /* OBJ_MAYBE_ECOFF */
10792         mips_flag_mdebug = 0;
10793     }
10794 }
10795 \f
10796 void
10797 mips_init_after_args ()
10798 {
10799   /* initialize opcodes */
10800   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10801   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10802 }
10803
10804 long
10805 md_pcrel_from (fixP)
10806      fixS *fixP;
10807 {
10808   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10809       && fixP->fx_addsy != (symbolS *) NULL
10810       && ! S_IS_DEFINED (fixP->fx_addsy))
10811     {
10812       /* This makes a branch to an undefined symbol be a branch to the
10813          current location.  */
10814       if (mips_pic == EMBEDDED_PIC)
10815         return 4;
10816       else
10817         return 1;
10818     }
10819
10820   /* Return the address of the delay slot.  */
10821   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10822 }
10823
10824 /* This is called before the symbol table is processed.  In order to
10825    work with gcc when using mips-tfile, we must keep all local labels.
10826    However, in other cases, we want to discard them.  If we were
10827    called with -g, but we didn't see any debugging information, it may
10828    mean that gcc is smuggling debugging information through to
10829    mips-tfile, in which case we must generate all local labels.  */
10830
10831 void
10832 mips_frob_file_before_adjust ()
10833 {
10834 #ifndef NO_ECOFF_DEBUGGING
10835   if (ECOFF_DEBUGGING
10836       && mips_debug != 0
10837       && ! ecoff_debugging_seen)
10838     flag_keep_locals = 1;
10839 #endif
10840 }
10841
10842 /* Sort any unmatched HI16_S relocs so that they immediately precede
10843    the corresponding LO reloc.  This is called before md_apply_fix3 and
10844    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10845    explicit use of the %hi modifier.  */
10846
10847 void
10848 mips_frob_file ()
10849 {
10850   struct mips_hi_fixup *l;
10851
10852   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10853     {
10854       segment_info_type *seginfo;
10855       int pass;
10856
10857       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10858
10859       /* Check quickly whether the next fixup happens to be a matching
10860          %lo.  */
10861       if (l->fixp->fx_next != NULL
10862           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10863           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10864           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10865         continue;
10866
10867       /* Look through the fixups for this segment for a matching %lo.
10868          When we find one, move the %hi just in front of it.  We do
10869          this in two passes.  In the first pass, we try to find a
10870          unique %lo.  In the second pass, we permit multiple %hi
10871          relocs for a single %lo (this is a GNU extension).  */
10872       seginfo = seg_info (l->seg);
10873       for (pass = 0; pass < 2; pass++)
10874         {
10875           fixS *f, *prev;
10876
10877           prev = NULL;
10878           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10879             {
10880               /* Check whether this is a %lo fixup which matches l->fixp.  */
10881               if (f->fx_r_type == BFD_RELOC_LO16
10882                   && f->fx_addsy == l->fixp->fx_addsy
10883                   && f->fx_offset == l->fixp->fx_offset
10884                   && (pass == 1
10885                       || prev == NULL
10886                       || prev->fx_r_type != BFD_RELOC_HI16_S
10887                       || prev->fx_addsy != f->fx_addsy
10888                       || prev->fx_offset !=  f->fx_offset))
10889                 {
10890                   fixS **pf;
10891
10892                   /* Move l->fixp before f.  */
10893                   for (pf = &seginfo->fix_root;
10894                        *pf != l->fixp;
10895                        pf = &(*pf)->fx_next)
10896                     assert (*pf != NULL);
10897
10898                   *pf = l->fixp->fx_next;
10899
10900                   l->fixp->fx_next = f;
10901                   if (prev == NULL)
10902                     seginfo->fix_root = l->fixp;
10903                   else
10904                     prev->fx_next = l->fixp;
10905
10906                   break;
10907                 }
10908
10909               prev = f;
10910             }
10911
10912           if (f != NULL)
10913             break;
10914
10915 #if 0 /* GCC code motion plus incomplete dead code elimination
10916          can leave a %hi without a %lo.  */
10917           if (pass == 1)
10918             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10919                            _("Unmatched %%hi reloc"));
10920 #endif
10921         }
10922     }
10923 }
10924
10925 /* When generating embedded PIC code we need to use a special
10926    relocation to represent the difference of two symbols in the .text
10927    section (switch tables use a difference of this sort).  See
10928    include/coff/mips.h for details.  This macro checks whether this
10929    fixup requires the special reloc.  */
10930 #define SWITCH_TABLE(fixp) \
10931   ((fixp)->fx_r_type == BFD_RELOC_32 \
10932    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10933    && (fixp)->fx_addsy != NULL \
10934    && (fixp)->fx_subsy != NULL \
10935    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10936    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10937
10938 /* When generating embedded PIC code we must keep all PC relative
10939    relocations, in case the linker has to relax a call.  We also need
10940    to keep relocations for switch table entries.
10941
10942    We may have combined relocations without symbols in the N32/N64 ABI.
10943    We have to prevent gas from dropping them.  */
10944
10945 int
10946 mips_force_relocation (fixp)
10947      fixS *fixp;
10948 {
10949   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10950       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10951       || S_FORCE_RELOC (fixp->fx_addsy))
10952     return 1;
10953
10954   if (HAVE_NEWABI
10955       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10956       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10957           || fixp->fx_r_type == BFD_RELOC_HI16_S
10958           || fixp->fx_r_type == BFD_RELOC_LO16))
10959     return 1;
10960
10961   return (mips_pic == EMBEDDED_PIC
10962           && (fixp->fx_pcrel
10963               || SWITCH_TABLE (fixp)
10964               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10965               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10966 }
10967
10968 #ifdef OBJ_ELF
10969 static int
10970 mips_need_elf_addend_fixup (fixP)
10971      fixS *fixP;
10972 {
10973   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10974     return 1;
10975   if (mips_pic == EMBEDDED_PIC
10976       && S_IS_WEAK (fixP->fx_addsy))
10977     return 1;
10978   if (mips_pic != EMBEDDED_PIC
10979       && (S_IS_WEAK (fixP->fx_addsy)
10980           || S_IS_EXTERNAL (fixP->fx_addsy))
10981       && !S_IS_COMMON (fixP->fx_addsy))
10982     return 1;
10983   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10984       && (((bfd_get_section_flags (stdoutput,
10985                                    S_GET_SEGMENT (fixP->fx_addsy))
10986             & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
10987           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10988                        ".gnu.linkonce",
10989                        sizeof (".gnu.linkonce") - 1)))
10990     return 1;
10991   return 0;
10992 }
10993 #endif
10994
10995 /* Apply a fixup to the object file.  */
10996
10997 void
10998 md_apply_fix3 (fixP, valP, seg)
10999      fixS *fixP;
11000      valueT *valP;
11001      segT seg ATTRIBUTE_UNUSED;
11002 {
11003   bfd_byte *buf;
11004   long insn;
11005   valueT value;
11006   static int previous_fx_r_type = 0;
11007
11008   /* FIXME: Maybe just return for all reloc types not listed below?
11009      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11010   if (fixP->fx_r_type == BFD_RELOC_8)
11011       return;
11012
11013   assert (fixP->fx_size == 4
11014           || fixP->fx_r_type == BFD_RELOC_16
11015           || fixP->fx_r_type == BFD_RELOC_32
11016           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11017           || fixP->fx_r_type == BFD_RELOC_HI16_S
11018           || fixP->fx_r_type == BFD_RELOC_LO16
11019           || fixP->fx_r_type == BFD_RELOC_GPREL16
11020           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11021           || fixP->fx_r_type == BFD_RELOC_GPREL32
11022           || fixP->fx_r_type == BFD_RELOC_64
11023           || fixP->fx_r_type == BFD_RELOC_CTOR
11024           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11025           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11026           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11027           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11028           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11029           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11030           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11031           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11032           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11033
11034   value = *valP;
11035
11036   /* If we aren't adjusting this fixup to be against the section
11037      symbol, we need to adjust the value.  */
11038 #ifdef OBJ_ELF
11039   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11040     {
11041       if (mips_need_elf_addend_fixup (fixP))
11042         {
11043           reloc_howto_type *howto;
11044           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11045
11046           value -= symval;
11047
11048           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11049           if (value != 0 && howto->partial_inplace
11050               && (! fixP->fx_pcrel || howto->pcrel_offset))
11051             {
11052               /* In this case, the bfd_install_relocation routine will
11053                  incorrectly add the symbol value back in.  We just want
11054                  the addend to appear in the object file.
11055                  
11056                  howto->pcrel_offset is added for R_MIPS_PC16, which is
11057                  generated for code like
11058                  
11059                         globl g1 .text
11060                         .text
11061                         .space 20
11062                  g1:
11063                  x:
11064                         bal g1
11065                */
11066               value -= symval;
11067
11068               /* Make sure the addend is still non-zero.  If it became zero
11069                  after the last operation, set it to a spurious value and
11070                  subtract the same value from the object file's contents.  */
11071               if (value == 0)
11072                 {
11073                   value = 8;
11074
11075                   /* The in-place addends for LO16 relocations are signed;
11076                      leave the matching HI16 in-place addends as zero.  */
11077                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11078                     {
11079                       bfd_vma contents, mask, field;
11080
11081                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11082                                                + fixP->fx_where,
11083                                                fixP->fx_size * 8,
11084                                                target_big_endian);
11085
11086                       /* MASK has bits set where the relocation should go.
11087                          FIELD is -value, shifted into the appropriate place
11088                          for this relocation.  */
11089                       mask = 1 << (howto->bitsize - 1);
11090                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11091                       field = (-value >> howto->rightshift) << howto->bitpos;
11092
11093                       bfd_put_bits ((field & mask) | (contents & ~mask),
11094                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11095                                     fixP->fx_size * 8,
11096                                     target_big_endian);
11097                     }
11098                 }
11099             }
11100         }
11101
11102       /* This code was generated using trial and error and so is
11103          fragile and not trustworthy.  If you change it, you should
11104          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11105          they still pass.  */
11106       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11107         {
11108           value += fixP->fx_frag->fr_address + fixP->fx_where;
11109
11110           /* BFD's REL handling, for MIPS, is _very_ weird.
11111              This gives the right results, but it can't possibly
11112              be the way things are supposed to work.  */
11113           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
11114                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
11115               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11116             value += fixP->fx_frag->fr_address + fixP->fx_where;
11117         }
11118     }
11119 #endif
11120
11121   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11122
11123   /* We are not done if this is a composite relocation to set up gp.  */
11124   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11125       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11126            || (fixP->fx_r_type == BFD_RELOC_64
11127                && (previous_fx_r_type == BFD_RELOC_GPREL32
11128                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11129            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11130                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11131                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11132     fixP->fx_done = 1;
11133   previous_fx_r_type = fixP->fx_r_type;
11134
11135   switch (fixP->fx_r_type)
11136     {
11137     case BFD_RELOC_MIPS_JMP:
11138     case BFD_RELOC_MIPS_SHIFT5:
11139     case BFD_RELOC_MIPS_SHIFT6:
11140     case BFD_RELOC_MIPS_GOT_DISP:
11141     case BFD_RELOC_MIPS_GOT_PAGE:
11142     case BFD_RELOC_MIPS_GOT_OFST:
11143     case BFD_RELOC_MIPS_SUB:
11144     case BFD_RELOC_MIPS_INSERT_A:
11145     case BFD_RELOC_MIPS_INSERT_B:
11146     case BFD_RELOC_MIPS_DELETE:
11147     case BFD_RELOC_MIPS_HIGHEST:
11148     case BFD_RELOC_MIPS_HIGHER:
11149     case BFD_RELOC_MIPS_SCN_DISP:
11150     case BFD_RELOC_MIPS_REL16:
11151     case BFD_RELOC_MIPS_RELGOT:
11152     case BFD_RELOC_MIPS_JALR:
11153     case BFD_RELOC_HI16:
11154     case BFD_RELOC_HI16_S:
11155     case BFD_RELOC_GPREL16:
11156     case BFD_RELOC_MIPS_LITERAL:
11157     case BFD_RELOC_MIPS_CALL16:
11158     case BFD_RELOC_MIPS_GOT16:
11159     case BFD_RELOC_GPREL32:
11160     case BFD_RELOC_MIPS_GOT_HI16:
11161     case BFD_RELOC_MIPS_GOT_LO16:
11162     case BFD_RELOC_MIPS_CALL_HI16:
11163     case BFD_RELOC_MIPS_CALL_LO16:
11164     case BFD_RELOC_MIPS16_GPREL:
11165       if (fixP->fx_pcrel)
11166         as_bad_where (fixP->fx_file, fixP->fx_line,
11167                       _("Invalid PC relative reloc"));
11168       /* Nothing needed to do. The value comes from the reloc entry */
11169       break;
11170
11171     case BFD_RELOC_MIPS16_JMP:
11172       /* We currently always generate a reloc against a symbol, which
11173          means that we don't want an addend even if the symbol is
11174          defined.  */
11175       fixP->fx_addnumber = 0;
11176       break;
11177
11178     case BFD_RELOC_PCREL_HI16_S:
11179       /* The addend for this is tricky if it is internal, so we just
11180          do everything here rather than in bfd_install_relocation.  */
11181       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11182           && !fixP->fx_done
11183           && value != 0)
11184         break;
11185       if (fixP->fx_addsy
11186           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11187         {
11188           /* For an external symbol adjust by the address to make it
11189              pcrel_offset.  We use the address of the RELLO reloc
11190              which follows this one.  */
11191           value += (fixP->fx_next->fx_frag->fr_address
11192                     + fixP->fx_next->fx_where);
11193         }
11194       value = ((value + 0x8000) >> 16) & 0xffff;
11195       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11196       if (target_big_endian)
11197         buf += 2;
11198       md_number_to_chars ((char *) buf, value, 2);
11199       break;
11200
11201     case BFD_RELOC_PCREL_LO16:
11202       /* The addend for this is tricky if it is internal, so we just
11203          do everything here rather than in bfd_install_relocation.  */
11204       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11205           && !fixP->fx_done
11206           && value != 0)
11207         break;
11208       if (fixP->fx_addsy
11209           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11210         value += fixP->fx_frag->fr_address + fixP->fx_where;
11211       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11212       if (target_big_endian)
11213         buf += 2;
11214       md_number_to_chars ((char *) buf, value, 2);
11215       break;
11216
11217     case BFD_RELOC_64:
11218       /* This is handled like BFD_RELOC_32, but we output a sign
11219          extended value if we are only 32 bits.  */
11220       if (fixP->fx_done
11221           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11222         {
11223           if (8 <= sizeof (valueT))
11224             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11225                                 value, 8);
11226           else
11227             {
11228               long w1, w2;
11229               long hiv;
11230
11231               w1 = w2 = fixP->fx_where;
11232               if (target_big_endian)
11233                 w1 += 4;
11234               else
11235                 w2 += 4;
11236               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11237               if ((value & 0x80000000) != 0)
11238                 hiv = 0xffffffff;
11239               else
11240                 hiv = 0;
11241               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11242             }
11243         }
11244       break;
11245
11246     case BFD_RELOC_RVA:
11247     case BFD_RELOC_32:
11248       /* If we are deleting this reloc entry, we must fill in the
11249          value now.  This can happen if we have a .word which is not
11250          resolved when it appears but is later defined.  We also need
11251          to fill in the value if this is an embedded PIC switch table
11252          entry.  */
11253       if (fixP->fx_done
11254           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11255         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11256                             value, 4);
11257       break;
11258
11259     case BFD_RELOC_16:
11260       /* If we are deleting this reloc entry, we must fill in the
11261          value now.  */
11262       assert (fixP->fx_size == 2);
11263       if (fixP->fx_done)
11264         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11265                             value, 2);
11266       break;
11267
11268     case BFD_RELOC_LO16:
11269       /* When handling an embedded PIC switch statement, we can wind
11270          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11271       if (fixP->fx_done)
11272         {
11273           if (value + 0x8000 > 0xffff)
11274             as_bad_where (fixP->fx_file, fixP->fx_line,
11275                           _("relocation overflow"));
11276           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11277           if (target_big_endian)
11278             buf += 2;
11279           md_number_to_chars ((char *) buf, value, 2);
11280         }
11281       break;
11282
11283     case BFD_RELOC_16_PCREL_S2:
11284       if ((value & 0x3) != 0)
11285         as_bad_where (fixP->fx_file, fixP->fx_line,
11286                       _("Branch to odd address (%lx)"), (long) value);
11287
11288       /* Fall through.  */
11289
11290     case BFD_RELOC_16_PCREL:
11291       /*
11292        * We need to save the bits in the instruction since fixup_segment()
11293        * might be deleting the relocation entry (i.e., a branch within
11294        * the current segment).
11295        */
11296       if (!fixP->fx_done && value != 0)
11297         break;
11298       /* If 'value' is zero, the remaining reloc code won't actually
11299          do the store, so it must be done here.  This is probably
11300          a bug somewhere.  */
11301       if (!fixP->fx_done
11302           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11303               || fixP->fx_addsy == NULL                 /* ??? */
11304               || ! S_IS_DEFINED (fixP->fx_addsy)))
11305         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11306
11307       value = (offsetT) value >> 2;
11308
11309       /* update old instruction data */
11310       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11311       if (target_big_endian)
11312         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11313       else
11314         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11315
11316       if (value + 0x8000 <= 0xffff)
11317         insn |= value & 0xffff;
11318       else
11319         {
11320           /* The branch offset is too large.  If this is an
11321              unconditional branch, and we are not generating PIC code,
11322              we can convert it to an absolute jump instruction.  */
11323           if (mips_pic == NO_PIC
11324               && fixP->fx_done
11325               && fixP->fx_frag->fr_address >= text_section->vma
11326               && (fixP->fx_frag->fr_address
11327                   < text_section->vma + text_section->_raw_size)
11328               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11329                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11330                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11331             {
11332               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11333                 insn = 0x0c000000;      /* jal */
11334               else
11335                 insn = 0x08000000;      /* j */
11336               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11337               fixP->fx_done = 0;
11338               fixP->fx_addsy = section_symbol (text_section);
11339               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11340             }
11341           else
11342             {
11343               /* If we got here, we have branch-relaxation disabled,
11344                  and there's nothing we can do to fix this instruction
11345                  without turning it into a longer sequence.  */
11346               as_bad_where (fixP->fx_file, fixP->fx_line,
11347                             _("Branch out of range"));
11348             }
11349         }
11350
11351       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11352       break;
11353
11354     case BFD_RELOC_VTABLE_INHERIT:
11355       fixP->fx_done = 0;
11356       if (fixP->fx_addsy
11357           && !S_IS_DEFINED (fixP->fx_addsy)
11358           && !S_IS_WEAK (fixP->fx_addsy))
11359         S_SET_WEAK (fixP->fx_addsy);
11360       break;
11361
11362     case BFD_RELOC_VTABLE_ENTRY:
11363       fixP->fx_done = 0;
11364       break;
11365
11366     default:
11367       internalError ();
11368     }
11369 }
11370
11371 #if 0
11372 void
11373 printInsn (oc)
11374      unsigned long oc;
11375 {
11376   const struct mips_opcode *p;
11377   int treg, sreg, dreg, shamt;
11378   short imm;
11379   const char *args;
11380   int i;
11381
11382   for (i = 0; i < NUMOPCODES; ++i)
11383     {
11384       p = &mips_opcodes[i];
11385       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11386         {
11387           printf ("%08lx %s\t", oc, p->name);
11388           treg = (oc >> 16) & 0x1f;
11389           sreg = (oc >> 21) & 0x1f;
11390           dreg = (oc >> 11) & 0x1f;
11391           shamt = (oc >> 6) & 0x1f;
11392           imm = oc;
11393           for (args = p->args;; ++args)
11394             {
11395               switch (*args)
11396                 {
11397                 case '\0':
11398                   printf ("\n");
11399                   break;
11400
11401                 case ',':
11402                 case '(':
11403                 case ')':
11404                   printf ("%c", *args);
11405                   continue;
11406
11407                 case 'r':
11408                   assert (treg == sreg);
11409                   printf ("$%d,$%d", treg, sreg);
11410                   continue;
11411
11412                 case 'd':
11413                 case 'G':
11414                   printf ("$%d", dreg);
11415                   continue;
11416
11417                 case 't':
11418                 case 'E':
11419                   printf ("$%d", treg);
11420                   continue;
11421
11422                 case 'k':
11423                   printf ("0x%x", treg);
11424                   continue;
11425
11426                 case 'b':
11427                 case 's':
11428                   printf ("$%d", sreg);
11429                   continue;
11430
11431                 case 'a':
11432                   printf ("0x%08lx", oc & 0x1ffffff);
11433                   continue;
11434
11435                 case 'i':
11436                 case 'j':
11437                 case 'o':
11438                 case 'u':
11439                   printf ("%d", imm);
11440                   continue;
11441
11442                 case '<':
11443                 case '>':
11444                   printf ("$%d", shamt);
11445                   continue;
11446
11447                 default:
11448                   internalError ();
11449                 }
11450               break;
11451             }
11452           return;
11453         }
11454     }
11455   printf (_("%08lx  UNDEFINED\n"), oc);
11456 }
11457 #endif
11458
11459 static symbolS *
11460 get_symbol ()
11461 {
11462   int c;
11463   char *name;
11464   symbolS *p;
11465
11466   name = input_line_pointer;
11467   c = get_symbol_end ();
11468   p = (symbolS *) symbol_find_or_make (name);
11469   *input_line_pointer = c;
11470   return p;
11471 }
11472
11473 /* Align the current frag to a given power of two.  The MIPS assembler
11474    also automatically adjusts any preceding label.  */
11475
11476 static void
11477 mips_align (to, fill, label)
11478      int to;
11479      int fill;
11480      symbolS *label;
11481 {
11482   mips_emit_delays (false);
11483   frag_align (to, fill, 0);
11484   record_alignment (now_seg, to);
11485   if (label != NULL)
11486     {
11487       assert (S_GET_SEGMENT (label) == now_seg);
11488       symbol_set_frag (label, frag_now);
11489       S_SET_VALUE (label, (valueT) frag_now_fix ());
11490     }
11491 }
11492
11493 /* Align to a given power of two.  .align 0 turns off the automatic
11494    alignment used by the data creating pseudo-ops.  */
11495
11496 static void
11497 s_align (x)
11498      int x ATTRIBUTE_UNUSED;
11499 {
11500   register int temp;
11501   register long temp_fill;
11502   long max_alignment = 15;
11503
11504   /*
11505
11506     o  Note that the assembler pulls down any immediately preceeding label
11507        to the aligned address.
11508     o  It's not documented but auto alignment is reinstated by
11509        a .align pseudo instruction.
11510     o  Note also that after auto alignment is turned off the mips assembler
11511        issues an error on attempt to assemble an improperly aligned data item.
11512        We don't.
11513
11514     */
11515
11516   temp = get_absolute_expression ();
11517   if (temp > max_alignment)
11518     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11519   else if (temp < 0)
11520     {
11521       as_warn (_("Alignment negative: 0 assumed."));
11522       temp = 0;
11523     }
11524   if (*input_line_pointer == ',')
11525     {
11526       ++input_line_pointer;
11527       temp_fill = get_absolute_expression ();
11528     }
11529   else
11530     temp_fill = 0;
11531   if (temp)
11532     {
11533       auto_align = 1;
11534       mips_align (temp, (int) temp_fill,
11535                   insn_labels != NULL ? insn_labels->label : NULL);
11536     }
11537   else
11538     {
11539       auto_align = 0;
11540     }
11541
11542   demand_empty_rest_of_line ();
11543 }
11544
11545 void
11546 mips_flush_pending_output ()
11547 {
11548   mips_emit_delays (false);
11549   mips_clear_insn_labels ();
11550 }
11551
11552 static void
11553 s_change_sec (sec)
11554      int sec;
11555 {
11556   segT seg;
11557
11558   /* When generating embedded PIC code, we only use the .text, .lit8,
11559      .sdata and .sbss sections.  We change the .data and .rdata
11560      pseudo-ops to use .sdata.  */
11561   if (mips_pic == EMBEDDED_PIC
11562       && (sec == 'd' || sec == 'r'))
11563     sec = 's';
11564
11565 #ifdef OBJ_ELF
11566   /* The ELF backend needs to know that we are changing sections, so
11567      that .previous works correctly.  We could do something like check
11568      for an obj_section_change_hook macro, but that might be confusing
11569      as it would not be appropriate to use it in the section changing
11570      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11571      This should be cleaner, somehow.  */
11572   obj_elf_section_change_hook ();
11573 #endif
11574
11575   mips_emit_delays (false);
11576   switch (sec)
11577     {
11578     case 't':
11579       s_text (0);
11580       break;
11581     case 'd':
11582       s_data (0);
11583       break;
11584     case 'b':
11585       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11586       demand_empty_rest_of_line ();
11587       break;
11588
11589     case 'r':
11590       if (USE_GLOBAL_POINTER_OPT)
11591         {
11592           seg = subseg_new (RDATA_SECTION_NAME,
11593                             (subsegT) get_absolute_expression ());
11594           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11595             {
11596               bfd_set_section_flags (stdoutput, seg,
11597                                      (SEC_ALLOC
11598                                       | SEC_LOAD
11599                                       | SEC_READONLY
11600                                       | SEC_RELOC
11601                                       | SEC_DATA));
11602               if (strcmp (TARGET_OS, "elf") != 0)
11603                 record_alignment (seg, 4);
11604             }
11605           demand_empty_rest_of_line ();
11606         }
11607       else
11608         {
11609           as_bad (_("No read only data section in this object file format"));
11610           demand_empty_rest_of_line ();
11611           return;
11612         }
11613       break;
11614
11615     case 's':
11616       if (USE_GLOBAL_POINTER_OPT)
11617         {
11618           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11619           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11620             {
11621               bfd_set_section_flags (stdoutput, seg,
11622                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11623                                      | SEC_DATA);
11624               if (strcmp (TARGET_OS, "elf") != 0)
11625                 record_alignment (seg, 4);
11626             }
11627           demand_empty_rest_of_line ();
11628           break;
11629         }
11630       else
11631         {
11632           as_bad (_("Global pointers not supported; recompile -G 0"));
11633           demand_empty_rest_of_line ();
11634           return;
11635         }
11636     }
11637
11638   auto_align = 1;
11639 }
11640   
11641 void
11642 s_change_section (ignore)
11643      int ignore ATTRIBUTE_UNUSED;
11644 {
11645 #ifdef OBJ_ELF
11646   char *section_name;
11647   char c;
11648   char next_c;
11649   int section_type;
11650   int section_flag;
11651   int section_entry_size;
11652   int section_alignment;
11653   
11654   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11655     return;
11656
11657   section_name = input_line_pointer;
11658   c = get_symbol_end ();
11659   next_c = *(input_line_pointer + 1);
11660
11661   /* Do we have .section Name<,"flags">?  */
11662   if (c != ',' || (c == ',' && next_c == '"'))
11663     {
11664       /* just after name is now '\0'.  */
11665       *input_line_pointer = c;
11666       input_line_pointer = section_name;
11667       obj_elf_section (ignore);
11668       return;
11669     }
11670   input_line_pointer++;
11671
11672   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11673   if (c == ',')
11674     section_type = get_absolute_expression ();
11675   else
11676     section_type = 0;
11677   if (*input_line_pointer++ == ',')
11678     section_flag = get_absolute_expression ();
11679   else
11680     section_flag = 0;
11681   if (*input_line_pointer++ == ',')
11682     section_entry_size = get_absolute_expression ();
11683   else
11684     section_entry_size = 0;
11685   if (*input_line_pointer++ == ',')
11686     section_alignment = get_absolute_expression ();
11687   else
11688     section_alignment = 0;
11689
11690   obj_elf_change_section (section_name, section_type, section_flag,
11691                           section_entry_size, 0, 0, 0);
11692 #endif /* OBJ_ELF */
11693 }
11694
11695 void
11696 mips_enable_auto_align ()
11697 {
11698   auto_align = 1;
11699 }
11700
11701 static void
11702 s_cons (log_size)
11703      int log_size;
11704 {
11705   symbolS *label;
11706
11707   label = insn_labels != NULL ? insn_labels->label : NULL;
11708   mips_emit_delays (false);
11709   if (log_size > 0 && auto_align)
11710     mips_align (log_size, 0, label);
11711   mips_clear_insn_labels ();
11712   cons (1 << log_size);
11713 }
11714
11715 static void
11716 s_float_cons (type)
11717      int type;
11718 {
11719   symbolS *label;
11720
11721   label = insn_labels != NULL ? insn_labels->label : NULL;
11722
11723   mips_emit_delays (false);
11724
11725   if (auto_align)
11726     {
11727       if (type == 'd')
11728         mips_align (3, 0, label);
11729       else
11730         mips_align (2, 0, label);
11731     }
11732
11733   mips_clear_insn_labels ();
11734
11735   float_cons (type);
11736 }
11737
11738 /* Handle .globl.  We need to override it because on Irix 5 you are
11739    permitted to say
11740        .globl foo .text
11741    where foo is an undefined symbol, to mean that foo should be
11742    considered to be the address of a function.  */
11743
11744 static void
11745 s_mips_globl (x)
11746      int x ATTRIBUTE_UNUSED;
11747 {
11748   char *name;
11749   int c;
11750   symbolS *symbolP;
11751   flagword flag;
11752
11753   name = input_line_pointer;
11754   c = get_symbol_end ();
11755   symbolP = symbol_find_or_make (name);
11756   *input_line_pointer = c;
11757   SKIP_WHITESPACE ();
11758
11759   /* On Irix 5, every global symbol that is not explicitly labelled as
11760      being a function is apparently labelled as being an object.  */
11761   flag = BSF_OBJECT;
11762
11763   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11764     {
11765       char *secname;
11766       asection *sec;
11767
11768       secname = input_line_pointer;
11769       c = get_symbol_end ();
11770       sec = bfd_get_section_by_name (stdoutput, secname);
11771       if (sec == NULL)
11772         as_bad (_("%s: no such section"), secname);
11773       *input_line_pointer = c;
11774
11775       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11776         flag = BSF_FUNCTION;
11777     }
11778
11779   symbol_get_bfdsym (symbolP)->flags |= flag;
11780
11781   S_SET_EXTERNAL (symbolP);
11782   demand_empty_rest_of_line ();
11783 }
11784
11785 static void
11786 s_option (x)
11787      int x ATTRIBUTE_UNUSED;
11788 {
11789   char *opt;
11790   char c;
11791
11792   opt = input_line_pointer;
11793   c = get_symbol_end ();
11794
11795   if (*opt == 'O')
11796     {
11797       /* FIXME: What does this mean?  */
11798     }
11799   else if (strncmp (opt, "pic", 3) == 0)
11800     {
11801       int i;
11802
11803       i = atoi (opt + 3);
11804       if (i == 0)
11805         mips_pic = NO_PIC;
11806       else if (i == 2)
11807         mips_pic = SVR4_PIC;
11808       else
11809         as_bad (_(".option pic%d not supported"), i);
11810
11811       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11812         {
11813           if (g_switch_seen && g_switch_value != 0)
11814             as_warn (_("-G may not be used with SVR4 PIC code"));
11815           g_switch_value = 0;
11816           bfd_set_gp_size (stdoutput, 0);
11817         }
11818     }
11819   else
11820     as_warn (_("Unrecognized option \"%s\""), opt);
11821
11822   *input_line_pointer = c;
11823   demand_empty_rest_of_line ();
11824 }
11825
11826 /* This structure is used to hold a stack of .set values.  */
11827
11828 struct mips_option_stack
11829 {
11830   struct mips_option_stack *next;
11831   struct mips_set_options options;
11832 };
11833
11834 static struct mips_option_stack *mips_opts_stack;
11835
11836 /* Handle the .set pseudo-op.  */
11837
11838 static void
11839 s_mipsset (x)
11840      int x ATTRIBUTE_UNUSED;
11841 {
11842   char *name = input_line_pointer, ch;
11843
11844   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11845     ++input_line_pointer;
11846   ch = *input_line_pointer;
11847   *input_line_pointer = '\0';
11848
11849   if (strcmp (name, "reorder") == 0)
11850     {
11851       if (mips_opts.noreorder && prev_nop_frag != NULL)
11852         {
11853           /* If we still have pending nops, we can discard them.  The
11854              usual nop handling will insert any that are still
11855              needed.  */
11856           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11857                                     * (mips_opts.mips16 ? 2 : 4));
11858           prev_nop_frag = NULL;
11859         }
11860       mips_opts.noreorder = 0;
11861     }
11862   else if (strcmp (name, "noreorder") == 0)
11863     {
11864       mips_emit_delays (true);
11865       mips_opts.noreorder = 1;
11866       mips_any_noreorder = 1;
11867     }
11868   else if (strcmp (name, "at") == 0)
11869     {
11870       mips_opts.noat = 0;
11871     }
11872   else if (strcmp (name, "noat") == 0)
11873     {
11874       mips_opts.noat = 1;
11875     }
11876   else if (strcmp (name, "macro") == 0)
11877     {
11878       mips_opts.warn_about_macros = 0;
11879     }
11880   else if (strcmp (name, "nomacro") == 0)
11881     {
11882       if (mips_opts.noreorder == 0)
11883         as_bad (_("`noreorder' must be set before `nomacro'"));
11884       mips_opts.warn_about_macros = 1;
11885     }
11886   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11887     {
11888       mips_opts.nomove = 0;
11889     }
11890   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11891     {
11892       mips_opts.nomove = 1;
11893     }
11894   else if (strcmp (name, "bopt") == 0)
11895     {
11896       mips_opts.nobopt = 0;
11897     }
11898   else if (strcmp (name, "nobopt") == 0)
11899     {
11900       mips_opts.nobopt = 1;
11901     }
11902   else if (strcmp (name, "mips16") == 0
11903            || strcmp (name, "MIPS-16") == 0)
11904     mips_opts.mips16 = 1;
11905   else if (strcmp (name, "nomips16") == 0
11906            || strcmp (name, "noMIPS-16") == 0)
11907     mips_opts.mips16 = 0;
11908   else if (strcmp (name, "mips3d") == 0)
11909     mips_opts.ase_mips3d = 1;
11910   else if (strcmp (name, "nomips3d") == 0)
11911     mips_opts.ase_mips3d = 0;
11912   else if (strcmp (name, "mdmx") == 0)
11913     mips_opts.ase_mdmx = 1;
11914   else if (strcmp (name, "nomdmx") == 0)
11915     mips_opts.ase_mdmx = 0;
11916   else if (strncmp (name, "mips", 4) == 0)
11917     {
11918       int isa;
11919
11920       /* Permit the user to change the ISA on the fly.  Needless to
11921          say, misuse can cause serious problems.  */
11922       isa = atoi (name + 4);
11923       switch (isa)
11924         {
11925         case  0:
11926           mips_opts.gp32 = file_mips_gp32;
11927           mips_opts.fp32 = file_mips_fp32;
11928           break;
11929         case  1:
11930         case  2:
11931         case 32:
11932           mips_opts.gp32 = 1;
11933           mips_opts.fp32 = 1;
11934           break;
11935         case  3:
11936         case  4:
11937         case  5:
11938         case 64:
11939           mips_opts.gp32 = 0;
11940           mips_opts.fp32 = 0;
11941           break;
11942         default:
11943           as_bad (_("unknown ISA level %s"), name + 4);
11944           break;
11945         }
11946
11947       switch (isa)
11948         {
11949         case  0: mips_opts.isa = file_mips_isa;   break;
11950         case  1: mips_opts.isa = ISA_MIPS1;       break;
11951         case  2: mips_opts.isa = ISA_MIPS2;       break;
11952         case  3: mips_opts.isa = ISA_MIPS3;       break;
11953         case  4: mips_opts.isa = ISA_MIPS4;       break;
11954         case  5: mips_opts.isa = ISA_MIPS5;       break;
11955         case 32: mips_opts.isa = ISA_MIPS32;      break;
11956         case 64: mips_opts.isa = ISA_MIPS64;      break;
11957         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11958         }
11959     }
11960   else if (strcmp (name, "autoextend") == 0)
11961     mips_opts.noautoextend = 0;
11962   else if (strcmp (name, "noautoextend") == 0)
11963     mips_opts.noautoextend = 1;
11964   else if (strcmp (name, "push") == 0)
11965     {
11966       struct mips_option_stack *s;
11967
11968       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11969       s->next = mips_opts_stack;
11970       s->options = mips_opts;
11971       mips_opts_stack = s;
11972     }
11973   else if (strcmp (name, "pop") == 0)
11974     {
11975       struct mips_option_stack *s;
11976
11977       s = mips_opts_stack;
11978       if (s == NULL)
11979         as_bad (_(".set pop with no .set push"));
11980       else
11981         {
11982           /* If we're changing the reorder mode we need to handle
11983              delay slots correctly.  */
11984           if (s->options.noreorder && ! mips_opts.noreorder)
11985             mips_emit_delays (true);
11986           else if (! s->options.noreorder && mips_opts.noreorder)
11987             {
11988               if (prev_nop_frag != NULL)
11989                 {
11990                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11991                                             * (mips_opts.mips16 ? 2 : 4));
11992                   prev_nop_frag = NULL;
11993                 }
11994             }
11995
11996           mips_opts = s->options;
11997           mips_opts_stack = s->next;
11998           free (s);
11999         }
12000     }
12001   else
12002     {
12003       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12004     }
12005   *input_line_pointer = ch;
12006   demand_empty_rest_of_line ();
12007 }
12008
12009 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12010    .option pic2.  It means to generate SVR4 PIC calls.  */
12011
12012 static void
12013 s_abicalls (ignore)
12014      int ignore ATTRIBUTE_UNUSED;
12015 {
12016   mips_pic = SVR4_PIC;
12017   if (USE_GLOBAL_POINTER_OPT)
12018     {
12019       if (g_switch_seen && g_switch_value != 0)
12020         as_warn (_("-G may not be used with SVR4 PIC code"));
12021       g_switch_value = 0;
12022     }
12023   bfd_set_gp_size (stdoutput, 0);
12024   demand_empty_rest_of_line ();
12025 }
12026
12027 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12028    PIC code.  It sets the $gp register for the function based on the
12029    function address, which is in the register named in the argument.
12030    This uses a relocation against _gp_disp, which is handled specially
12031    by the linker.  The result is:
12032         lui     $gp,%hi(_gp_disp)
12033         addiu   $gp,$gp,%lo(_gp_disp)
12034         addu    $gp,$gp,.cpload argument
12035    The .cpload argument is normally $25 == $t9.  */
12036
12037 static void
12038 s_cpload (ignore)
12039      int ignore ATTRIBUTE_UNUSED;
12040 {
12041   expressionS ex;
12042   int icnt = 0;
12043
12044   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12045      .cpload is ignored.  */
12046   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12047     {
12048       s_ignore (0);
12049       return;
12050     }
12051
12052   /* .cpload should be in a .set noreorder section.  */
12053   if (mips_opts.noreorder == 0)
12054     as_warn (_(".cpload not in noreorder section"));
12055
12056   ex.X_op = O_symbol;
12057   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12058   ex.X_op_symbol = NULL;
12059   ex.X_add_number = 0;
12060
12061   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12062   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12063
12064   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12065   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12066                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12067
12068   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12069                mips_gp_register, mips_gp_register, tc_get_register (0));
12070
12071   demand_empty_rest_of_line ();
12072 }
12073
12074 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12075      .cpsetup $reg1, offset|$reg2, label
12076
12077    If offset is given, this results in:
12078      sd         $gp, offset($sp)
12079      lui        $gp, %hi(%neg(%gp_rel(label)))
12080      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12081      daddu      $gp, $gp, $reg1
12082
12083    If $reg2 is given, this results in:
12084      daddu      $reg2, $gp, $0
12085      lui        $gp, %hi(%neg(%gp_rel(label)))
12086      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12087      daddu      $gp, $gp, $reg1
12088    $reg1 is normally $25 == $t9.  */
12089 static void
12090 s_cpsetup (ignore)
12091      int ignore ATTRIBUTE_UNUSED;
12092 {
12093   expressionS ex_off;
12094   expressionS ex_sym;
12095   int reg1;
12096   int icnt = 0;
12097   char *f;
12098
12099   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12100      We also need NewABI support.  */
12101   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12102     {
12103       s_ignore (0);
12104       return;
12105     }
12106
12107   reg1 = tc_get_register (0);
12108   SKIP_WHITESPACE ();
12109   if (*input_line_pointer != ',')
12110     {
12111       as_bad (_("missing argument separator ',' for .cpsetup"));
12112       return;
12113     }
12114   else
12115     ++input_line_pointer;
12116   SKIP_WHITESPACE ();
12117   if (*input_line_pointer == '$')
12118     {
12119       mips_cpreturn_register = tc_get_register (0);
12120       mips_cpreturn_offset = -1;
12121     }
12122   else
12123     {
12124       mips_cpreturn_offset = get_absolute_expression ();
12125       mips_cpreturn_register = -1;
12126     }
12127   SKIP_WHITESPACE ();
12128   if (*input_line_pointer != ',')
12129     {
12130       as_bad (_("missing argument separator ',' for .cpsetup"));
12131       return;
12132     }
12133   else
12134     ++input_line_pointer;
12135   SKIP_WHITESPACE ();
12136   expression (&ex_sym);
12137
12138   if (mips_cpreturn_register == -1)
12139     {
12140       ex_off.X_op = O_constant;
12141       ex_off.X_add_symbol = NULL;
12142       ex_off.X_op_symbol = NULL;
12143       ex_off.X_add_number = mips_cpreturn_offset;
12144
12145       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12146                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12147     }
12148   else
12149     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12150                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12151
12152   /* Ensure there's room for the next two instructions, so that `f'
12153      doesn't end up with an address in the wrong frag.  */
12154   frag_grow (8);
12155   f = frag_more (0);
12156   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12157                (int) BFD_RELOC_GPREL16);
12158   fix_new (frag_now, f - frag_now->fr_literal,
12159            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12160   fix_new (frag_now, f - frag_now->fr_literal,
12161            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12162
12163   f = frag_more (0);
12164   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12165                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12166   fix_new (frag_now, f - frag_now->fr_literal,
12167            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12168   fix_new (frag_now, f - frag_now->fr_literal,
12169            0, NULL, 0, 0, BFD_RELOC_LO16);
12170
12171   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12172                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12173                mips_gp_register, mips_gp_register, reg1);
12174
12175   demand_empty_rest_of_line ();
12176 }
12177
12178 static void
12179 s_cplocal (ignore)
12180      int ignore ATTRIBUTE_UNUSED;
12181 {
12182   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12183    .cplocal is ignored.  */
12184   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12185     {
12186       s_ignore (0);
12187       return;
12188     }
12189
12190   mips_gp_register = tc_get_register (0);
12191   demand_empty_rest_of_line ();
12192 }
12193
12194 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12195    offset from $sp.  The offset is remembered, and after making a PIC
12196    call $gp is restored from that location.  */
12197
12198 static void
12199 s_cprestore (ignore)
12200      int ignore ATTRIBUTE_UNUSED;
12201 {
12202   expressionS ex;
12203   int icnt = 0;
12204
12205   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12206      .cprestore is ignored.  */
12207   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12208     {
12209       s_ignore (0);
12210       return;
12211     }
12212
12213   mips_cprestore_offset = get_absolute_expression ();
12214   mips_cprestore_valid = 1;
12215
12216   ex.X_op = O_constant;
12217   ex.X_add_symbol = NULL;
12218   ex.X_op_symbol = NULL;
12219   ex.X_add_number = mips_cprestore_offset;
12220
12221   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12222                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12223                                 mips_gp_register, SP);
12224
12225   demand_empty_rest_of_line ();
12226 }
12227
12228 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12229    was given in the preceeding .gpsetup, it results in:
12230      ld         $gp, offset($sp)
12231
12232    If a register $reg2 was given there, it results in:
12233      daddiu     $gp, $gp, $reg2
12234  */
12235 static void
12236 s_cpreturn (ignore)
12237      int ignore ATTRIBUTE_UNUSED;
12238 {
12239   expressionS ex;
12240   int icnt = 0;
12241
12242   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12243      We also need NewABI support.  */
12244   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12245     {
12246       s_ignore (0);
12247       return;
12248     }
12249
12250   if (mips_cpreturn_register == -1)
12251     {
12252       ex.X_op = O_constant;
12253       ex.X_add_symbol = NULL;
12254       ex.X_op_symbol = NULL;
12255       ex.X_add_number = mips_cpreturn_offset;
12256
12257       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12258                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12259     }
12260   else
12261     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12262                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12263
12264   demand_empty_rest_of_line ();
12265 }
12266
12267 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12268    code.  It sets the offset to use in gp_rel relocations.  */
12269
12270 static void
12271 s_gpvalue (ignore)
12272      int ignore ATTRIBUTE_UNUSED;
12273 {
12274   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12275      We also need NewABI support.  */
12276   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12277     {
12278       s_ignore (0);
12279       return;
12280     }
12281
12282   mips_gprel_offset = get_absolute_expression ();
12283
12284   demand_empty_rest_of_line ();
12285 }
12286
12287 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12288    code.  It generates a 32 bit GP relative reloc.  */
12289
12290 static void
12291 s_gpword (ignore)
12292      int ignore ATTRIBUTE_UNUSED;
12293 {
12294   symbolS *label;
12295   expressionS ex;
12296   char *p;
12297
12298   /* When not generating PIC code, this is treated as .word.  */
12299   if (mips_pic != SVR4_PIC)
12300     {
12301       s_cons (2);
12302       return;
12303     }
12304
12305   label = insn_labels != NULL ? insn_labels->label : NULL;
12306   mips_emit_delays (true);
12307   if (auto_align)
12308     mips_align (2, 0, label);
12309   mips_clear_insn_labels ();
12310
12311   expression (&ex);
12312
12313   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12314     {
12315       as_bad (_("Unsupported use of .gpword"));
12316       ignore_rest_of_line ();
12317     }
12318
12319   p = frag_more (4);
12320   md_number_to_chars (p, (valueT) 0, 4);
12321   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12322                BFD_RELOC_GPREL32);
12323
12324   demand_empty_rest_of_line ();
12325 }
12326
12327 static void
12328 s_gpdword (ignore)
12329      int ignore ATTRIBUTE_UNUSED;
12330 {
12331   symbolS *label;
12332   expressionS ex;
12333   char *p;
12334
12335   /* When not generating PIC code, this is treated as .dword.  */
12336   if (mips_pic != SVR4_PIC)
12337     {
12338       s_cons (3);
12339       return;
12340     }
12341
12342   label = insn_labels != NULL ? insn_labels->label : NULL;
12343   mips_emit_delays (true);
12344   if (auto_align)
12345     mips_align (3, 0, label);
12346   mips_clear_insn_labels ();
12347
12348   expression (&ex);
12349
12350   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12351     {
12352       as_bad (_("Unsupported use of .gpdword"));
12353       ignore_rest_of_line ();
12354     }
12355
12356   p = frag_more (8);
12357   md_number_to_chars (p, (valueT) 0, 8);
12358   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, false,
12359                BFD_RELOC_GPREL32);
12360
12361   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12362   ex.X_op = O_absent;
12363   ex.X_add_symbol = 0;
12364   ex.X_add_number = 0;
12365   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, false,
12366                BFD_RELOC_64);
12367
12368   demand_empty_rest_of_line ();
12369 }
12370
12371 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12372    tables in SVR4 PIC code.  */
12373
12374 static void
12375 s_cpadd (ignore)
12376      int ignore ATTRIBUTE_UNUSED;
12377 {
12378   int icnt = 0;
12379   int reg;
12380
12381   /* This is ignored when not generating SVR4 PIC code.  */
12382   if (mips_pic != SVR4_PIC)
12383     {
12384       s_ignore (0);
12385       return;
12386     }
12387
12388   /* Add $gp to the register named as an argument.  */
12389   reg = tc_get_register (0);
12390   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12391                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12392                "d,v,t", reg, reg, mips_gp_register);
12393
12394   demand_empty_rest_of_line ();
12395 }
12396
12397 /* Handle the .insn pseudo-op.  This marks instruction labels in
12398    mips16 mode.  This permits the linker to handle them specially,
12399    such as generating jalx instructions when needed.  We also make
12400    them odd for the duration of the assembly, in order to generate the
12401    right sort of code.  We will make them even in the adjust_symtab
12402    routine, while leaving them marked.  This is convenient for the
12403    debugger and the disassembler.  The linker knows to make them odd
12404    again.  */
12405
12406 static void
12407 s_insn (ignore)
12408      int ignore ATTRIBUTE_UNUSED;
12409 {
12410   mips16_mark_labels ();
12411
12412   demand_empty_rest_of_line ();
12413 }
12414
12415 /* Handle a .stabn directive.  We need these in order to mark a label
12416    as being a mips16 text label correctly.  Sometimes the compiler
12417    will emit a label, followed by a .stabn, and then switch sections.
12418    If the label and .stabn are in mips16 mode, then the label is
12419    really a mips16 text label.  */
12420
12421 static void
12422 s_mips_stab (type)
12423      int type;
12424 {
12425   if (type == 'n')
12426     mips16_mark_labels ();
12427
12428   s_stab (type);
12429 }
12430
12431 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12432  */
12433
12434 static void
12435 s_mips_weakext (ignore)
12436      int ignore ATTRIBUTE_UNUSED;
12437 {
12438   char *name;
12439   int c;
12440   symbolS *symbolP;
12441   expressionS exp;
12442
12443   name = input_line_pointer;
12444   c = get_symbol_end ();
12445   symbolP = symbol_find_or_make (name);
12446   S_SET_WEAK (symbolP);
12447   *input_line_pointer = c;
12448
12449   SKIP_WHITESPACE ();
12450
12451   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12452     {
12453       if (S_IS_DEFINED (symbolP))
12454         {
12455           as_bad ("ignoring attempt to redefine symbol %s",
12456                   S_GET_NAME (symbolP));
12457           ignore_rest_of_line ();
12458           return;
12459         }
12460
12461       if (*input_line_pointer == ',')
12462         {
12463           ++input_line_pointer;
12464           SKIP_WHITESPACE ();
12465         }
12466
12467       expression (&exp);
12468       if (exp.X_op != O_symbol)
12469         {
12470           as_bad ("bad .weakext directive");
12471           ignore_rest_of_line ();
12472           return;
12473         }
12474       symbol_set_value_expression (symbolP, &exp);
12475     }
12476
12477   demand_empty_rest_of_line ();
12478 }
12479
12480 /* Parse a register string into a number.  Called from the ECOFF code
12481    to parse .frame.  The argument is non-zero if this is the frame
12482    register, so that we can record it in mips_frame_reg.  */
12483
12484 int
12485 tc_get_register (frame)
12486      int frame;
12487 {
12488   int reg;
12489
12490   SKIP_WHITESPACE ();
12491   if (*input_line_pointer++ != '$')
12492     {
12493       as_warn (_("expected `$'"));
12494       reg = ZERO;
12495     }
12496   else if (ISDIGIT (*input_line_pointer))
12497     {
12498       reg = get_absolute_expression ();
12499       if (reg < 0 || reg >= 32)
12500         {
12501           as_warn (_("Bad register number"));
12502           reg = ZERO;
12503         }
12504     }
12505   else
12506     {
12507       if (strncmp (input_line_pointer, "ra", 2) == 0)
12508         {
12509           reg = RA;
12510           input_line_pointer += 2;
12511         }
12512       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12513         {
12514           reg = FP;
12515           input_line_pointer += 2;
12516         }
12517       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12518         {
12519           reg = SP;
12520           input_line_pointer += 2;
12521         }
12522       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12523         {
12524           reg = GP;
12525           input_line_pointer += 2;
12526         }
12527       else if (strncmp (input_line_pointer, "at", 2) == 0)
12528         {
12529           reg = AT;
12530           input_line_pointer += 2;
12531         }
12532       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12533         {
12534           reg = KT0;
12535           input_line_pointer += 3;
12536         }
12537       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12538         {
12539           reg = KT1;
12540           input_line_pointer += 3;
12541         }
12542       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12543         {
12544           reg = ZERO;
12545           input_line_pointer += 4;
12546         }
12547       else
12548         {
12549           as_warn (_("Unrecognized register name"));
12550           reg = ZERO;
12551           while (ISALNUM(*input_line_pointer))
12552            input_line_pointer++;
12553         }
12554     }
12555   if (frame)
12556     {
12557       mips_frame_reg = reg != 0 ? reg : SP;
12558       mips_frame_reg_valid = 1;
12559       mips_cprestore_valid = 0;
12560     }
12561   return reg;
12562 }
12563
12564 valueT
12565 md_section_align (seg, addr)
12566      asection *seg;
12567      valueT addr;
12568 {
12569   int align = bfd_get_section_alignment (stdoutput, seg);
12570
12571 #ifdef OBJ_ELF
12572   /* We don't need to align ELF sections to the full alignment.
12573      However, Irix 5 may prefer that we align them at least to a 16
12574      byte boundary.  We don't bother to align the sections if we are
12575      targeted for an embedded system.  */
12576   if (strcmp (TARGET_OS, "elf") == 0)
12577     return addr;
12578   if (align > 4)
12579     align = 4;
12580 #endif
12581
12582   return ((addr + (1 << align) - 1) & (-1 << align));
12583 }
12584
12585 /* Utility routine, called from above as well.  If called while the
12586    input file is still being read, it's only an approximation.  (For
12587    example, a symbol may later become defined which appeared to be
12588    undefined earlier.)  */
12589
12590 static int
12591 nopic_need_relax (sym, before_relaxing)
12592      symbolS *sym;
12593      int before_relaxing;
12594 {
12595   if (sym == 0)
12596     return 0;
12597
12598   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12599     {
12600       const char *symname;
12601       int change;
12602
12603       /* Find out whether this symbol can be referenced off the $gp
12604          register.  It can be if it is smaller than the -G size or if
12605          it is in the .sdata or .sbss section.  Certain symbols can
12606          not be referenced off the $gp, although it appears as though
12607          they can.  */
12608       symname = S_GET_NAME (sym);
12609       if (symname != (const char *) NULL
12610           && (strcmp (symname, "eprol") == 0
12611               || strcmp (symname, "etext") == 0
12612               || strcmp (symname, "_gp") == 0
12613               || strcmp (symname, "edata") == 0
12614               || strcmp (symname, "_fbss") == 0
12615               || strcmp (symname, "_fdata") == 0
12616               || strcmp (symname, "_ftext") == 0
12617               || strcmp (symname, "end") == 0
12618               || strcmp (symname, "_gp_disp") == 0))
12619         change = 1;
12620       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12621                && (0
12622 #ifndef NO_ECOFF_DEBUGGING
12623                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12624                        && (symbol_get_obj (sym)->ecoff_extern_size
12625                            <= g_switch_value))
12626 #endif
12627                    /* We must defer this decision until after the whole
12628                       file has been read, since there might be a .extern
12629                       after the first use of this symbol.  */
12630                    || (before_relaxing
12631 #ifndef NO_ECOFF_DEBUGGING
12632                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12633 #endif
12634                        && S_GET_VALUE (sym) == 0)
12635                    || (S_GET_VALUE (sym) != 0
12636                        && S_GET_VALUE (sym) <= g_switch_value)))
12637         change = 0;
12638       else
12639         {
12640           const char *segname;
12641
12642           segname = segment_name (S_GET_SEGMENT (sym));
12643           assert (strcmp (segname, ".lit8") != 0
12644                   && strcmp (segname, ".lit4") != 0);
12645           change = (strcmp (segname, ".sdata") != 0
12646                     && strcmp (segname, ".sbss") != 0
12647                     && strncmp (segname, ".sdata.", 7) != 0
12648                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12649         }
12650       return change;
12651     }
12652   else
12653     /* We are not optimizing for the $gp register.  */
12654     return 1;
12655 }
12656
12657 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12658    extended opcode.  SEC is the section the frag is in.  */
12659
12660 static int
12661 mips16_extended_frag (fragp, sec, stretch)
12662      fragS *fragp;
12663      asection *sec;
12664      long stretch;
12665 {
12666   int type;
12667   register const struct mips16_immed_operand *op;
12668   offsetT val;
12669   int mintiny, maxtiny;
12670   segT symsec;
12671   fragS *sym_frag;
12672
12673   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12674     return 0;
12675   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12676     return 1;
12677
12678   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12679   op = mips16_immed_operands;
12680   while (op->type != type)
12681     {
12682       ++op;
12683       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12684     }
12685
12686   if (op->unsp)
12687     {
12688       if (type == '<' || type == '>' || type == '[' || type == ']')
12689         {
12690           mintiny = 1;
12691           maxtiny = 1 << op->nbits;
12692         }
12693       else
12694         {
12695           mintiny = 0;
12696           maxtiny = (1 << op->nbits) - 1;
12697         }
12698     }
12699   else
12700     {
12701       mintiny = - (1 << (op->nbits - 1));
12702       maxtiny = (1 << (op->nbits - 1)) - 1;
12703     }
12704
12705   sym_frag = symbol_get_frag (fragp->fr_symbol);
12706   val = S_GET_VALUE (fragp->fr_symbol);
12707   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12708
12709   if (op->pcrel)
12710     {
12711       addressT addr;
12712
12713       /* We won't have the section when we are called from
12714          mips_relax_frag.  However, we will always have been called
12715          from md_estimate_size_before_relax first.  If this is a
12716          branch to a different section, we mark it as such.  If SEC is
12717          NULL, and the frag is not marked, then it must be a branch to
12718          the same section.  */
12719       if (sec == NULL)
12720         {
12721           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12722             return 1;
12723         }
12724       else
12725         {
12726           /* Must have been called from md_estimate_size_before_relax.  */
12727           if (symsec != sec)
12728             {
12729               fragp->fr_subtype =
12730                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12731
12732               /* FIXME: We should support this, and let the linker
12733                  catch branches and loads that are out of range.  */
12734               as_bad_where (fragp->fr_file, fragp->fr_line,
12735                             _("unsupported PC relative reference to different section"));
12736
12737               return 1;
12738             }
12739           if (fragp != sym_frag && sym_frag->fr_address == 0)
12740             /* Assume non-extended on the first relaxation pass.
12741                The address we have calculated will be bogus if this is
12742                a forward branch to another frag, as the forward frag
12743                will have fr_address == 0.  */
12744             return 0;
12745         }
12746
12747       /* In this case, we know for sure that the symbol fragment is in
12748          the same section.  If the relax_marker of the symbol fragment
12749          differs from the relax_marker of this fragment, we have not
12750          yet adjusted the symbol fragment fr_address.  We want to add
12751          in STRETCH in order to get a better estimate of the address.
12752          This particularly matters because of the shift bits.  */
12753       if (stretch != 0
12754           && sym_frag->relax_marker != fragp->relax_marker)
12755         {
12756           fragS *f;
12757
12758           /* Adjust stretch for any alignment frag.  Note that if have
12759              been expanding the earlier code, the symbol may be
12760              defined in what appears to be an earlier frag.  FIXME:
12761              This doesn't handle the fr_subtype field, which specifies
12762              a maximum number of bytes to skip when doing an
12763              alignment.  */
12764           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12765             {
12766               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12767                 {
12768                   if (stretch < 0)
12769                     stretch = - ((- stretch)
12770                                  & ~ ((1 << (int) f->fr_offset) - 1));
12771                   else
12772                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12773                   if (stretch == 0)
12774                     break;
12775                 }
12776             }
12777           if (f != NULL)
12778             val += stretch;
12779         }
12780
12781       addr = fragp->fr_address + fragp->fr_fix;
12782
12783       /* The base address rules are complicated.  The base address of
12784          a branch is the following instruction.  The base address of a
12785          PC relative load or add is the instruction itself, but if it
12786          is in a delay slot (in which case it can not be extended) use
12787          the address of the instruction whose delay slot it is in.  */
12788       if (type == 'p' || type == 'q')
12789         {
12790           addr += 2;
12791
12792           /* If we are currently assuming that this frag should be
12793              extended, then, the current address is two bytes
12794              higher.  */
12795           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12796             addr += 2;
12797
12798           /* Ignore the low bit in the target, since it will be set
12799              for a text label.  */
12800           if ((val & 1) != 0)
12801             --val;
12802         }
12803       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12804         addr -= 4;
12805       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12806         addr -= 2;
12807
12808       val -= addr & ~ ((1 << op->shift) - 1);
12809
12810       /* Branch offsets have an implicit 0 in the lowest bit.  */
12811       if (type == 'p' || type == 'q')
12812         val /= 2;
12813
12814       /* If any of the shifted bits are set, we must use an extended
12815          opcode.  If the address depends on the size of this
12816          instruction, this can lead to a loop, so we arrange to always
12817          use an extended opcode.  We only check this when we are in
12818          the main relaxation loop, when SEC is NULL.  */
12819       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12820         {
12821           fragp->fr_subtype =
12822             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12823           return 1;
12824         }
12825
12826       /* If we are about to mark a frag as extended because the value
12827          is precisely maxtiny + 1, then there is a chance of an
12828          infinite loop as in the following code:
12829              la $4,foo
12830              .skip      1020
12831              .align     2
12832            foo:
12833          In this case when the la is extended, foo is 0x3fc bytes
12834          away, so the la can be shrunk, but then foo is 0x400 away, so
12835          the la must be extended.  To avoid this loop, we mark the
12836          frag as extended if it was small, and is about to become
12837          extended with a value of maxtiny + 1.  */
12838       if (val == ((maxtiny + 1) << op->shift)
12839           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12840           && sec == NULL)
12841         {
12842           fragp->fr_subtype =
12843             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12844           return 1;
12845         }
12846     }
12847   else if (symsec != absolute_section && sec != NULL)
12848     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12849
12850   if ((val & ((1 << op->shift) - 1)) != 0
12851       || val < (mintiny << op->shift)
12852       || val > (maxtiny << op->shift))
12853     return 1;
12854   else
12855     return 0;
12856 }
12857
12858 /* Compute the length of a branch sequence, and adjust the
12859    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
12860    worst-case length is computed, with UPDATE being used to indicate
12861    whether an unconditional (-1), branch-likely (+1) or regular (0)
12862    branch is to be computed.  */
12863 static int
12864 relaxed_branch_length (fragp, sec, update)
12865      fragS *fragp;
12866      asection *sec;
12867      int update;
12868 {
12869   boolean toofar;
12870   int length;
12871
12872   if (fragp
12873       && S_IS_DEFINED (fragp->fr_symbol)
12874       && sec == S_GET_SEGMENT (fragp->fr_symbol))
12875     {
12876       addressT addr;
12877       offsetT val;
12878
12879       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12880
12881       addr = fragp->fr_address + fragp->fr_fix + 4;
12882
12883       val -= addr;
12884
12885       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12886     }
12887   else if (fragp)
12888     /* If the symbol is not defined or it's in a different segment,
12889        assume the user knows what's going on and emit a short
12890        branch.  */
12891     toofar = false;
12892   else
12893     toofar = true;
12894
12895   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12896     fragp->fr_subtype
12897       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype),
12898                              RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12899                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12900                              RELAX_BRANCH_LINK (fragp->fr_subtype),
12901                              toofar);
12902
12903   length = 4;
12904   if (toofar)
12905     {
12906       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12907         length += 8;
12908
12909       if (mips_pic != NO_PIC)
12910         {
12911           /* Additional space for PIC loading of target address.  */
12912           length += 8;
12913           if (mips_opts.isa == ISA_MIPS1)
12914             /* Additional space for $at-stabilizing nop.  */
12915             length += 4;
12916         }
12917
12918       /* If branch is conditional.  */
12919       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12920         length += 8;
12921     }
12922   
12923   return length;
12924 }
12925
12926 /* Estimate the size of a frag before relaxing.  Unless this is the
12927    mips16, we are not really relaxing here, and the final size is
12928    encoded in the subtype information.  For the mips16, we have to
12929    decide whether we are using an extended opcode or not.  */
12930
12931 int
12932 md_estimate_size_before_relax (fragp, segtype)
12933      fragS *fragp;
12934      asection *segtype;
12935 {
12936   int change = 0;
12937   boolean linkonce = false;
12938
12939   if (RELAX_BRANCH_P (fragp->fr_subtype))
12940     {
12941
12942       fragp->fr_var = relaxed_branch_length (fragp, segtype, false);
12943       
12944       return fragp->fr_var;
12945     }
12946
12947   if (RELAX_MIPS16_P (fragp->fr_subtype))
12948     /* We don't want to modify the EXTENDED bit here; it might get us
12949        into infinite loops.  We change it only in mips_relax_frag().  */
12950     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12951
12952   if (mips_pic == NO_PIC)
12953     {
12954       change = nopic_need_relax (fragp->fr_symbol, 0);
12955     }
12956   else if (mips_pic == SVR4_PIC)
12957     {
12958       symbolS *sym;
12959       asection *symsec;
12960
12961       sym = fragp->fr_symbol;
12962
12963       /* Handle the case of a symbol equated to another symbol.  */
12964       while (symbol_equated_reloc_p (sym))
12965         {
12966           symbolS *n;
12967
12968           /* It's possible to get a loop here in a badly written
12969              program.  */
12970           n = symbol_get_value_expression (sym)->X_add_symbol;
12971           if (n == sym)
12972             break;
12973           sym = n;
12974         }
12975
12976       symsec = S_GET_SEGMENT (sym);
12977
12978       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12979       if (symsec != segtype && ! S_IS_LOCAL (sym))
12980         {
12981           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12982               != 0)
12983             linkonce = true;
12984
12985           /* The GNU toolchain uses an extension for ELF: a section
12986              beginning with the magic string .gnu.linkonce is a linkonce
12987              section.  */
12988           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12989                        sizeof ".gnu.linkonce" - 1) == 0)
12990             linkonce = true;
12991         }
12992
12993       /* This must duplicate the test in adjust_reloc_syms.  */
12994       change = (symsec != &bfd_und_section
12995                 && symsec != &bfd_abs_section
12996                 && ! bfd_is_com_section (symsec)
12997                 && !linkonce
12998 #ifdef OBJ_ELF
12999                 /* A global or weak symbol is treated as external.  */
13000                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13001                     || (! S_IS_WEAK (sym)
13002                         && (! S_IS_EXTERNAL (sym)
13003                             || mips_pic == EMBEDDED_PIC)))
13004 #endif
13005                 );
13006     }
13007   else
13008     abort ();
13009
13010   if (change)
13011     {
13012       /* Record the offset to the first reloc in the fr_opcode field.
13013          This lets md_convert_frag and tc_gen_reloc know that the code
13014          must be expanded.  */
13015       fragp->fr_opcode = (fragp->fr_literal
13016                           + fragp->fr_fix
13017                           - RELAX_OLD (fragp->fr_subtype)
13018                           + RELAX_RELOC1 (fragp->fr_subtype));
13019       /* FIXME: This really needs as_warn_where.  */
13020       if (RELAX_WARN (fragp->fr_subtype))
13021         as_warn (_("AT used after \".set noat\" or macro used after "
13022                    "\".set nomacro\""));
13023
13024       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13025     }
13026
13027   return 0;
13028 }
13029
13030 /* This is called to see whether a reloc against a defined symbol
13031    should be converted into a reloc against a section.  Don't adjust
13032    MIPS16 jump relocations, so we don't have to worry about the format
13033    of the offset in the .o file.  Don't adjust relocations against
13034    mips16 symbols, so that the linker can find them if it needs to set
13035    up a stub.  */
13036
13037 int
13038 mips_fix_adjustable (fixp)
13039      fixS *fixp;
13040 {
13041   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13042     return 0;
13043
13044   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13045       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13046     return 0;
13047
13048   if (fixp->fx_addsy == NULL)
13049     return 1;
13050
13051 #ifdef OBJ_ELF
13052   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13053       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13054       && fixp->fx_subsy == NULL)
13055     return 0;
13056 #endif
13057
13058   return 1;
13059 }
13060
13061 /* Translate internal representation of relocation info to BFD target
13062    format.  */
13063
13064 arelent **
13065 tc_gen_reloc (section, fixp)
13066      asection *section ATTRIBUTE_UNUSED;
13067      fixS *fixp;
13068 {
13069   static arelent *retval[4];
13070   arelent *reloc;
13071   bfd_reloc_code_real_type code;
13072
13073   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13074   retval[1] = NULL;
13075
13076   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13077   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13078   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13079
13080   if (mips_pic == EMBEDDED_PIC
13081       && SWITCH_TABLE (fixp))
13082     {
13083       /* For a switch table entry we use a special reloc.  The addend
13084          is actually the difference between the reloc address and the
13085          subtrahend.  */
13086       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13087       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13088         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13089       fixp->fx_r_type = BFD_RELOC_GPREL32;
13090     }
13091   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13092     {
13093       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13094         reloc->addend = fixp->fx_addnumber;
13095       else
13096         {
13097           /* We use a special addend for an internal RELLO reloc.  */
13098           if (symbol_section_p (fixp->fx_addsy))
13099             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13100           else
13101             reloc->addend = fixp->fx_addnumber + reloc->address;
13102         }
13103     }
13104   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13105     {
13106       assert (fixp->fx_next != NULL
13107               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13108
13109       /* The reloc is relative to the RELLO; adjust the addend
13110          accordingly.  */
13111       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13112         reloc->addend = fixp->fx_next->fx_addnumber;
13113       else
13114         {
13115           /* We use a special addend for an internal RELHI reloc.  */
13116           if (symbol_section_p (fixp->fx_addsy))
13117             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13118                              + fixp->fx_next->fx_where
13119                              - S_GET_VALUE (fixp->fx_subsy));
13120           else
13121             reloc->addend = (fixp->fx_addnumber
13122                              + fixp->fx_next->fx_frag->fr_address
13123                              + fixp->fx_next->fx_where);
13124         }
13125     }
13126   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13127     reloc->addend = fixp->fx_addnumber;
13128   else
13129     {
13130       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13131         /* A gruesome hack which is a result of the gruesome gas reloc
13132            handling.  */
13133         reloc->addend = reloc->address;
13134       else
13135         reloc->addend = -reloc->address;
13136     }
13137
13138   /* If this is a variant frag, we may need to adjust the existing
13139      reloc and generate a new one.  */
13140   if (fixp->fx_frag->fr_opcode != NULL
13141       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13142            && ! HAVE_NEWABI)
13143           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13144           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13145           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13146           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13147           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13148           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13149     )
13150     {
13151       arelent *reloc2;
13152
13153       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13154
13155       /* If this is not the last reloc in this frag, then we have two
13156          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13157          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13158          the second one handle all of them.  */
13159       if (fixp->fx_next != NULL
13160           && fixp->fx_frag == fixp->fx_next->fx_frag)
13161         {
13162           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13163                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13164                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13165                       && (fixp->fx_next->fx_r_type
13166                           == BFD_RELOC_MIPS_GOT_LO16))
13167                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13168                       && (fixp->fx_next->fx_r_type
13169                           == BFD_RELOC_MIPS_CALL_LO16)));
13170           retval[0] = NULL;
13171           return retval;
13172         }
13173
13174       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13175       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13176       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13177       retval[2] = NULL;
13178       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13179       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13180       reloc2->address = (reloc->address
13181                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13182                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13183       reloc2->addend = fixp->fx_addnumber;
13184       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13185       assert (reloc2->howto != NULL);
13186
13187       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13188         {
13189           arelent *reloc3;
13190
13191           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13192           retval[3] = NULL;
13193           *reloc3 = *reloc2;
13194           reloc3->address += 4;
13195         }
13196
13197       if (mips_pic == NO_PIC)
13198         {
13199           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13200           fixp->fx_r_type = BFD_RELOC_HI16_S;
13201         }
13202       else if (mips_pic == SVR4_PIC)
13203         {
13204           switch (fixp->fx_r_type)
13205             {
13206             default:
13207               abort ();
13208             case BFD_RELOC_MIPS_GOT16:
13209               break;
13210             case BFD_RELOC_MIPS_GOT_LO16:
13211             case BFD_RELOC_MIPS_CALL_LO16:
13212               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13213               break;
13214             case BFD_RELOC_MIPS_CALL16:
13215               if (HAVE_NEWABI)
13216                 {
13217                   /* BFD_RELOC_MIPS_GOT16;*/
13218                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13219                   reloc2->howto = bfd_reloc_type_lookup
13220                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13221                 }
13222               else
13223                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13224               break;
13225             }
13226         }
13227       else
13228         abort ();
13229
13230       /* newabi uses R_MIPS_GOT_DISP for local symbols */
13231       if (HAVE_NEWABI && BFD_RELOC_MIPS_GOT_LO16)
13232         {
13233           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13234           retval[1] = NULL;
13235         }
13236     }
13237
13238   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13239      entry to be used in the relocation's section offset.  */
13240   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13241     {
13242       reloc->address = reloc->addend;
13243       reloc->addend = 0;
13244     }
13245
13246   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13247      fixup_segment converted a non-PC relative reloc into a PC
13248      relative reloc.  In such a case, we need to convert the reloc
13249      code.  */
13250   code = fixp->fx_r_type;
13251   if (fixp->fx_pcrel)
13252     {
13253       switch (code)
13254         {
13255         case BFD_RELOC_8:
13256           code = BFD_RELOC_8_PCREL;
13257           break;
13258         case BFD_RELOC_16:
13259           code = BFD_RELOC_16_PCREL;
13260           break;
13261         case BFD_RELOC_32:
13262           code = BFD_RELOC_32_PCREL;
13263           break;
13264         case BFD_RELOC_64:
13265           code = BFD_RELOC_64_PCREL;
13266           break;
13267         case BFD_RELOC_8_PCREL:
13268         case BFD_RELOC_16_PCREL:
13269         case BFD_RELOC_32_PCREL:
13270         case BFD_RELOC_64_PCREL:
13271         case BFD_RELOC_16_PCREL_S2:
13272         case BFD_RELOC_PCREL_HI16_S:
13273         case BFD_RELOC_PCREL_LO16:
13274           break;
13275         default:
13276           as_bad_where (fixp->fx_file, fixp->fx_line,
13277                         _("Cannot make %s relocation PC relative"),
13278                         bfd_get_reloc_code_name (code));
13279         }
13280     }
13281
13282 #ifdef OBJ_ELF
13283   /* md_apply_fix3 has a double-subtraction hack to get
13284      bfd_install_relocation to behave nicely.  GPREL relocations are
13285      handled correctly without this hack, so undo it here.  We can't
13286      stop md_apply_fix3 from subtracting twice in the first place since
13287      the fake addend is required for variant frags above.  */
13288   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13289       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13290       && reloc->addend != 0
13291       && mips_need_elf_addend_fixup (fixp))
13292     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13293 #endif
13294
13295   /* To support a PC relative reloc when generating embedded PIC code
13296      for ECOFF, we use a Cygnus extension.  We check for that here to
13297      make sure that we don't let such a reloc escape normally.  */
13298   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13299        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13300       && code == BFD_RELOC_16_PCREL_S2
13301       && mips_pic != EMBEDDED_PIC)
13302     reloc->howto = NULL;
13303   else
13304     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13305
13306   if (reloc->howto == NULL)
13307     {
13308       as_bad_where (fixp->fx_file, fixp->fx_line,
13309                     _("Can not represent %s relocation in this object file format"),
13310                     bfd_get_reloc_code_name (code));
13311       retval[0] = NULL;
13312     }
13313
13314   return retval;
13315 }
13316
13317 /* Relax a machine dependent frag.  This returns the amount by which
13318    the current size of the frag should change.  */
13319
13320 int
13321 mips_relax_frag (sec, fragp, stretch)
13322      asection *sec;
13323      fragS *fragp;
13324      long stretch;
13325 {
13326   if (RELAX_BRANCH_P (fragp->fr_subtype))
13327     {
13328       offsetT old_var = fragp->fr_var;
13329       
13330       fragp->fr_var = relaxed_branch_length (fragp, sec, true);
13331
13332       return fragp->fr_var - old_var;
13333     }
13334
13335   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13336     return 0;
13337
13338   if (mips16_extended_frag (fragp, NULL, stretch))
13339     {
13340       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13341         return 0;
13342       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13343       return 2;
13344     }
13345   else
13346     {
13347       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13348         return 0;
13349       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13350       return -2;
13351     }
13352
13353   return 0;
13354 }
13355
13356 /* Convert a machine dependent frag.  */
13357
13358 void
13359 md_convert_frag (abfd, asec, fragp)
13360      bfd *abfd ATTRIBUTE_UNUSED;
13361      segT asec;
13362      fragS *fragp;
13363 {
13364   int old, new;
13365   char *fixptr;
13366
13367   if (RELAX_BRANCH_P (fragp->fr_subtype))
13368     {
13369       bfd_byte *buf;
13370       unsigned long insn;
13371       expressionS exp;
13372       fixS *fixp;
13373       
13374       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13375
13376       if (target_big_endian)
13377         insn = bfd_getb32 (buf);
13378       else
13379         insn = bfd_getl32 (buf);
13380           
13381       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13382         {
13383           /* We generate a fixup instead of applying it right now
13384              because, if there are linker relaxations, we're going to
13385              need the relocations.  */
13386           exp.X_op = O_symbol;
13387           exp.X_add_symbol = fragp->fr_symbol;
13388           exp.X_add_number = fragp->fr_offset;
13389
13390           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13391                               4, &exp, 1,
13392                               RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype)
13393                               ? BFD_RELOC_16_PCREL_S2
13394                               : BFD_RELOC_16_PCREL);
13395           fixp->fx_file = fragp->fr_file;
13396           fixp->fx_line = fragp->fr_line;
13397           
13398           md_number_to_chars ((char *)buf, insn, 4);
13399           buf += 4;
13400         }
13401       else
13402         {
13403           int i;
13404
13405           as_warn_where (fragp->fr_file, fragp->fr_line,
13406                          _("relaxed out-of-range branch into a jump"));
13407
13408           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13409             goto uncond;
13410
13411           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13412             {
13413               /* Reverse the branch.  */
13414               switch ((insn >> 28) & 0xf)
13415                 {
13416                 case 4:
13417                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13418                      have the condition reversed by tweaking a single
13419                      bit, and their opcodes all have 0x4???????.  */
13420                   assert ((insn & 0xf1000000) == 0x41000000);
13421                   insn ^= 0x00010000;
13422                   break;
13423
13424                 case 0:
13425                   /* bltz       0x04000000      bgez    0x04010000
13426                      bltzal     0x04100000      bgezal  0x04110000 */
13427                   assert ((insn & 0xfc0e0000) == 0x04000000);
13428                   insn ^= 0x00010000;
13429                   break;
13430                   
13431                 case 1:
13432                   /* beq        0x10000000      bne     0x14000000
13433                      blez       0x18000000      bgtz    0x1c000000 */
13434                   insn ^= 0x04000000;
13435                   break;
13436
13437                 default:
13438                   abort ();
13439                 }
13440             }
13441
13442           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13443             {
13444               /* Clear the and-link bit.  */
13445               assert ((insn & 0xfc1c0000) == 0x04100000);
13446
13447               /* bltzal 0x04100000      bgezal  0x04110000
13448                 bltzall 0x04120000     bgezall  0x04130000 */
13449               insn &= ~0x00100000;
13450             }
13451
13452           /* Branch over the branch (if the branch was likely) or the
13453              full jump (not likely case).  Compute the offset from the
13454              current instruction to branch to.  */
13455           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13456             i = 16;
13457           else
13458             {
13459               /* How many bytes in instructions we've already emitted?  */
13460               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13461               /* How many bytes in instructions from here to the end?  */
13462               i = fragp->fr_var - i;
13463             }
13464           /* Convert to instruction count.  */
13465           i >>= 2;
13466           /* Branch counts from the next instruction.  */
13467           i--; 
13468           insn |= i;
13469           /* Branch over the jump.  */
13470           md_number_to_chars ((char *)buf, insn, 4);
13471           buf += 4;
13472
13473           /* Nop */
13474           md_number_to_chars ((char*)buf, 0, 4);
13475           buf += 4;
13476
13477           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13478             {
13479               /* beql $0, $0, 2f */
13480               insn = 0x50000000;
13481               /* Compute the PC offset from the current instruction to
13482                  the end of the variable frag.  */
13483               /* How many bytes in instructions we've already emitted?  */
13484               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13485               /* How many bytes in instructions from here to the end?  */
13486               i = fragp->fr_var - i;
13487               /* Convert to instruction count.  */
13488               i >>= 2;
13489               /* Don't decrement i, because we want to branch over the
13490                  delay slot.  */
13491
13492               insn |= i;
13493               md_number_to_chars ((char *)buf, insn, 4);
13494               buf += 4;
13495
13496               md_number_to_chars ((char *)buf, 0, 4);
13497               buf += 4;
13498             }
13499
13500         uncond:
13501           if (mips_pic == NO_PIC)
13502             {
13503               /* j or jal.  */
13504               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13505                       ? 0x0c000000 : 0x08000000);
13506               exp.X_op = O_symbol;
13507               exp.X_add_symbol = fragp->fr_symbol;
13508               exp.X_add_number = fragp->fr_offset;
13509
13510               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13511                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13512               fixp->fx_file = fragp->fr_file;
13513               fixp->fx_line = fragp->fr_line;
13514
13515               md_number_to_chars ((char*)buf, insn, 4);
13516               buf += 4;
13517             }
13518           else
13519             {
13520               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13521               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13522               exp.X_op = O_symbol;
13523               exp.X_add_symbol = fragp->fr_symbol;
13524               exp.X_add_number = fragp->fr_offset;
13525
13526               if (fragp->fr_offset)
13527                 {
13528                   exp.X_add_symbol = make_expr_symbol (&exp);
13529                   exp.X_add_number = 0;
13530                 }
13531
13532               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13533                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13534               fixp->fx_file = fragp->fr_file;
13535               fixp->fx_line = fragp->fr_line;
13536
13537               md_number_to_chars ((char*)buf, insn, 4);
13538               buf += 4;
13539               
13540               if (mips_opts.isa == ISA_MIPS1)
13541                 {
13542                   /* nop */
13543                   md_number_to_chars ((char*)buf, 0, 4);
13544                   buf += 4;
13545                 }
13546
13547               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13548               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13549
13550               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13551                                   4, &exp, 0, BFD_RELOC_LO16);
13552               fixp->fx_file = fragp->fr_file;
13553               fixp->fx_line = fragp->fr_line;
13554               
13555               md_number_to_chars ((char*)buf, insn, 4);
13556               buf += 4;
13557
13558               /* j(al)r $at.  */
13559               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13560                 insn = 0x0020f809;
13561               else
13562                 insn = 0x00200008;
13563
13564               md_number_to_chars ((char*)buf, insn, 4);
13565               buf += 4;
13566             }
13567         }
13568
13569       assert (buf == (bfd_byte *)fragp->fr_literal
13570               + fragp->fr_fix + fragp->fr_var);
13571
13572       fragp->fr_fix += fragp->fr_var;
13573
13574       return;
13575     }
13576
13577   if (RELAX_MIPS16_P (fragp->fr_subtype))
13578     {
13579       int type;
13580       register const struct mips16_immed_operand *op;
13581       boolean small, ext;
13582       offsetT val;
13583       bfd_byte *buf;
13584       unsigned long insn;
13585       boolean use_extend;
13586       unsigned short extend;
13587
13588       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13589       op = mips16_immed_operands;
13590       while (op->type != type)
13591         ++op;
13592
13593       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13594         {
13595           small = false;
13596           ext = true;
13597         }
13598       else
13599         {
13600           small = true;
13601           ext = false;
13602         }
13603
13604       resolve_symbol_value (fragp->fr_symbol);
13605       val = S_GET_VALUE (fragp->fr_symbol);
13606       if (op->pcrel)
13607         {
13608           addressT addr;
13609
13610           addr = fragp->fr_address + fragp->fr_fix;
13611
13612           /* The rules for the base address of a PC relative reloc are
13613              complicated; see mips16_extended_frag.  */
13614           if (type == 'p' || type == 'q')
13615             {
13616               addr += 2;
13617               if (ext)
13618                 addr += 2;
13619               /* Ignore the low bit in the target, since it will be
13620                  set for a text label.  */
13621               if ((val & 1) != 0)
13622                 --val;
13623             }
13624           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13625             addr -= 4;
13626           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13627             addr -= 2;
13628
13629           addr &= ~ (addressT) ((1 << op->shift) - 1);
13630           val -= addr;
13631
13632           /* Make sure the section winds up with the alignment we have
13633              assumed.  */
13634           if (op->shift > 0)
13635             record_alignment (asec, op->shift);
13636         }
13637
13638       if (ext
13639           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13640               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13641         as_warn_where (fragp->fr_file, fragp->fr_line,
13642                        _("extended instruction in delay slot"));
13643
13644       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13645
13646       if (target_big_endian)
13647         insn = bfd_getb16 (buf);
13648       else
13649         insn = bfd_getl16 (buf);
13650
13651       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13652                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13653                     small, ext, &insn, &use_extend, &extend);
13654
13655       if (use_extend)
13656         {
13657           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13658           fragp->fr_fix += 2;
13659           buf += 2;
13660         }
13661
13662       md_number_to_chars ((char *) buf, insn, 2);
13663       fragp->fr_fix += 2;
13664       buf += 2;
13665     }
13666   else
13667     {
13668       if (fragp->fr_opcode == NULL)
13669         return;
13670
13671       old = RELAX_OLD (fragp->fr_subtype);
13672       new = RELAX_NEW (fragp->fr_subtype);
13673       fixptr = fragp->fr_literal + fragp->fr_fix;
13674
13675       if (new > 0)
13676         memcpy (fixptr - old, fixptr, new);
13677
13678       fragp->fr_fix += new - old;
13679     }
13680 }
13681
13682 #ifdef OBJ_ELF
13683
13684 /* This function is called after the relocs have been generated.
13685    We've been storing mips16 text labels as odd.  Here we convert them
13686    back to even for the convenience of the debugger.  */
13687
13688 void
13689 mips_frob_file_after_relocs ()
13690 {
13691   asymbol **syms;
13692   unsigned int count, i;
13693
13694   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13695     return;
13696
13697   syms = bfd_get_outsymbols (stdoutput);
13698   count = bfd_get_symcount (stdoutput);
13699   for (i = 0; i < count; i++, syms++)
13700     {
13701       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13702           && ((*syms)->value & 1) != 0)
13703         {
13704           (*syms)->value &= ~1;
13705           /* If the symbol has an odd size, it was probably computed
13706              incorrectly, so adjust that as well.  */
13707           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13708             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13709         }
13710     }
13711 }
13712
13713 #endif
13714
13715 /* This function is called whenever a label is defined.  It is used
13716    when handling branch delays; if a branch has a label, we assume we
13717    can not move it.  */
13718
13719 void
13720 mips_define_label (sym)
13721      symbolS *sym;
13722 {
13723   struct insn_label_list *l;
13724
13725   if (free_insn_labels == NULL)
13726     l = (struct insn_label_list *) xmalloc (sizeof *l);
13727   else
13728     {
13729       l = free_insn_labels;
13730       free_insn_labels = l->next;
13731     }
13732
13733   l->label = sym;
13734   l->next = insn_labels;
13735   insn_labels = l;
13736 }
13737 \f
13738 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13739
13740 /* Some special processing for a MIPS ELF file.  */
13741
13742 void
13743 mips_elf_final_processing ()
13744 {
13745   /* Write out the register information.  */
13746   if (mips_abi != N64_ABI)
13747     {
13748       Elf32_RegInfo s;
13749
13750       s.ri_gprmask = mips_gprmask;
13751       s.ri_cprmask[0] = mips_cprmask[0];
13752       s.ri_cprmask[1] = mips_cprmask[1];
13753       s.ri_cprmask[2] = mips_cprmask[2];
13754       s.ri_cprmask[3] = mips_cprmask[3];
13755       /* The gp_value field is set by the MIPS ELF backend.  */
13756
13757       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13758                                        ((Elf32_External_RegInfo *)
13759                                         mips_regmask_frag));
13760     }
13761   else
13762     {
13763       Elf64_Internal_RegInfo s;
13764
13765       s.ri_gprmask = mips_gprmask;
13766       s.ri_pad = 0;
13767       s.ri_cprmask[0] = mips_cprmask[0];
13768       s.ri_cprmask[1] = mips_cprmask[1];
13769       s.ri_cprmask[2] = mips_cprmask[2];
13770       s.ri_cprmask[3] = mips_cprmask[3];
13771       /* The gp_value field is set by the MIPS ELF backend.  */
13772
13773       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13774                                        ((Elf64_External_RegInfo *)
13775                                         mips_regmask_frag));
13776     }
13777
13778   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13779      sort of BFD interface for this.  */
13780   if (mips_any_noreorder)
13781     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13782   if (mips_pic != NO_PIC)
13783     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13784
13785   /* Set MIPS ELF flags for ASEs.  */
13786   if (file_ase_mips16)
13787     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13788 #if 0 /* XXX FIXME */
13789   if (file_ase_mips3d)
13790     elf_elfheader (stdoutput)->e_flags |= ???;
13791 #endif
13792   if (file_ase_mdmx)
13793     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13794
13795   /* Set the MIPS ELF ABI flags.  */
13796   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13797     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13798   else if (mips_abi == O64_ABI)
13799     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13800   else if (mips_abi == EABI_ABI)
13801     {
13802       if (!file_mips_gp32)
13803         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13804       else
13805         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13806     }
13807   else if (mips_abi == N32_ABI)
13808     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13809
13810   /* Nothing to do for N64_ABI.  */
13811
13812   if (mips_32bitmode)
13813     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13814 }
13815
13816 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13817 \f
13818 typedef struct proc {
13819   symbolS *isym;
13820   unsigned long reg_mask;
13821   unsigned long reg_offset;
13822   unsigned long fpreg_mask;
13823   unsigned long fpreg_offset;
13824   unsigned long frame_offset;
13825   unsigned long frame_reg;
13826   unsigned long pc_reg;
13827 } procS;
13828
13829 static procS cur_proc;
13830 static procS *cur_proc_ptr;
13831 static int numprocs;
13832
13833 /* Fill in an rs_align_code fragment.  */
13834
13835 void
13836 mips_handle_align (fragp)
13837      fragS *fragp;
13838 {
13839   if (fragp->fr_type != rs_align_code)
13840     return;
13841
13842   if (mips_opts.mips16)
13843     {
13844       static const unsigned char be_nop[] = { 0x65, 0x00 };
13845       static const unsigned char le_nop[] = { 0x00, 0x65 };
13846
13847       int bytes;
13848       char *p;
13849
13850       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13851       p = fragp->fr_literal + fragp->fr_fix;
13852
13853       if (bytes & 1)
13854         {
13855           *p++ = 0;
13856           fragp->fr_fix++;
13857         }
13858
13859       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13860       fragp->fr_var = 2;
13861     }
13862
13863   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13864 }
13865
13866 static void
13867 md_obj_begin ()
13868 {
13869 }
13870
13871 static void
13872 md_obj_end ()
13873 {
13874   /* check for premature end, nesting errors, etc */
13875   if (cur_proc_ptr)
13876     as_warn (_("missing .end at end of assembly"));
13877 }
13878
13879 static long
13880 get_number ()
13881 {
13882   int negative = 0;
13883   long val = 0;
13884
13885   if (*input_line_pointer == '-')
13886     {
13887       ++input_line_pointer;
13888       negative = 1;
13889     }
13890   if (!ISDIGIT (*input_line_pointer))
13891     as_bad (_("expected simple number"));
13892   if (input_line_pointer[0] == '0')
13893     {
13894       if (input_line_pointer[1] == 'x')
13895         {
13896           input_line_pointer += 2;
13897           while (ISXDIGIT (*input_line_pointer))
13898             {
13899               val <<= 4;
13900               val |= hex_value (*input_line_pointer++);
13901             }
13902           return negative ? -val : val;
13903         }
13904       else
13905         {
13906           ++input_line_pointer;
13907           while (ISDIGIT (*input_line_pointer))
13908             {
13909               val <<= 3;
13910               val |= *input_line_pointer++ - '0';
13911             }
13912           return negative ? -val : val;
13913         }
13914     }
13915   if (!ISDIGIT (*input_line_pointer))
13916     {
13917       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13918               *input_line_pointer, *input_line_pointer);
13919       as_warn (_("invalid number"));
13920       return -1;
13921     }
13922   while (ISDIGIT (*input_line_pointer))
13923     {
13924       val *= 10;
13925       val += *input_line_pointer++ - '0';
13926     }
13927   return negative ? -val : val;
13928 }
13929
13930 /* The .file directive; just like the usual .file directive, but there
13931    is an initial number which is the ECOFF file index.  In the non-ECOFF
13932    case .file implies DWARF-2.  */
13933
13934 static void
13935 s_mips_file (x)
13936      int x ATTRIBUTE_UNUSED;
13937 {
13938   static int first_file_directive = 0;
13939
13940   if (ECOFF_DEBUGGING)
13941     {
13942       get_number ();
13943       s_app_file (0);
13944     }
13945   else
13946     {
13947       char *filename;
13948
13949       filename = dwarf2_directive_file (0);
13950
13951       /* Versions of GCC up to 3.1 start files with a ".file"
13952          directive even for stabs output.  Make sure that this
13953          ".file" is handled.  Note that you need a version of GCC
13954          after 3.1 in order to support DWARF-2 on MIPS.  */
13955       if (filename != NULL && ! first_file_directive)
13956         {
13957           (void) new_logical_line (filename, -1);
13958           s_app_file_string (filename);
13959         }
13960       first_file_directive = 1;
13961     }
13962 }
13963
13964 /* The .loc directive, implying DWARF-2.  */
13965
13966 static void
13967 s_mips_loc (x)
13968      int x ATTRIBUTE_UNUSED;
13969 {
13970   if (!ECOFF_DEBUGGING)
13971     dwarf2_directive_loc (0);
13972 }
13973
13974 /* The .end directive.  */
13975
13976 static void
13977 s_mips_end (x)
13978      int x ATTRIBUTE_UNUSED;
13979 {
13980   symbolS *p;
13981   int maybe_text;
13982
13983   /* Following functions need their own .frame and .cprestore directives.  */
13984   mips_frame_reg_valid = 0;
13985   mips_cprestore_valid = 0;
13986
13987   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13988     {
13989       p = get_symbol ();
13990       demand_empty_rest_of_line ();
13991     }
13992   else
13993     p = NULL;
13994
13995 #ifdef BFD_ASSEMBLER
13996   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13997     maybe_text = 1;
13998   else
13999     maybe_text = 0;
14000 #else
14001   if (now_seg != data_section && now_seg != bss_section)
14002     maybe_text = 1;
14003   else
14004     maybe_text = 0;
14005 #endif
14006
14007   if (!maybe_text)
14008     as_warn (_(".end not in text section"));
14009
14010   if (!cur_proc_ptr)
14011     {
14012       as_warn (_(".end directive without a preceding .ent directive."));
14013       demand_empty_rest_of_line ();
14014       return;
14015     }
14016
14017   if (p != NULL)
14018     {
14019       assert (S_GET_NAME (p));
14020       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14021         as_warn (_(".end symbol does not match .ent symbol."));
14022
14023       if (debug_type == DEBUG_STABS)
14024         stabs_generate_asm_endfunc (S_GET_NAME (p),
14025                                     S_GET_NAME (p));
14026     }
14027   else
14028     as_warn (_(".end directive missing or unknown symbol"));
14029
14030 #ifdef OBJ_ELF
14031   /* Generate a .pdr section.  */
14032   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14033     {
14034       segT saved_seg = now_seg;
14035       subsegT saved_subseg = now_subseg;
14036       valueT dot;
14037       expressionS exp;
14038       char *fragp;
14039
14040       dot = frag_now_fix ();
14041
14042 #ifdef md_flush_pending_output
14043       md_flush_pending_output ();
14044 #endif
14045
14046       assert (pdr_seg);
14047       subseg_set (pdr_seg, 0);
14048
14049       /* Write the symbol.  */
14050       exp.X_op = O_symbol;
14051       exp.X_add_symbol = p;
14052       exp.X_add_number = 0;
14053       emit_expr (&exp, 4);
14054
14055       fragp = frag_more (7 * 4);
14056
14057       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14058       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14059       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14060       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14061       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14062       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14063       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14064
14065       subseg_set (saved_seg, saved_subseg);
14066     }
14067 #endif /* OBJ_ELF */
14068
14069   cur_proc_ptr = NULL;
14070 }
14071
14072 /* The .aent and .ent directives.  */
14073
14074 static void
14075 s_mips_ent (aent)
14076      int aent;
14077 {
14078   symbolS *symbolP;
14079   int maybe_text;
14080
14081   symbolP = get_symbol ();
14082   if (*input_line_pointer == ',')
14083     ++input_line_pointer;
14084   SKIP_WHITESPACE ();
14085   if (ISDIGIT (*input_line_pointer)
14086       || *input_line_pointer == '-')
14087     get_number ();
14088
14089 #ifdef BFD_ASSEMBLER
14090   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14091     maybe_text = 1;
14092   else
14093     maybe_text = 0;
14094 #else
14095   if (now_seg != data_section && now_seg != bss_section)
14096     maybe_text = 1;
14097   else
14098     maybe_text = 0;
14099 #endif
14100
14101   if (!maybe_text)
14102     as_warn (_(".ent or .aent not in text section."));
14103
14104   if (!aent && cur_proc_ptr)
14105     as_warn (_("missing .end"));
14106
14107   if (!aent)
14108     {
14109       /* This function needs its own .frame and .cprestore directives.  */
14110       mips_frame_reg_valid = 0;
14111       mips_cprestore_valid = 0;
14112
14113       cur_proc_ptr = &cur_proc;
14114       memset (cur_proc_ptr, '\0', sizeof (procS));
14115
14116       cur_proc_ptr->isym = symbolP;
14117
14118       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14119
14120       ++numprocs;
14121
14122       if (debug_type == DEBUG_STABS)
14123         stabs_generate_asm_func (S_GET_NAME (symbolP),
14124                                  S_GET_NAME (symbolP));
14125     }
14126
14127   demand_empty_rest_of_line ();
14128 }
14129
14130 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14131    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14132    s_mips_frame is used so that we can set the PDR information correctly.
14133    We can't use the ecoff routines because they make reference to the ecoff
14134    symbol table (in the mdebug section).  */
14135
14136 static void
14137 s_mips_frame (ignore)
14138      int ignore ATTRIBUTE_UNUSED;
14139 {
14140 #ifdef OBJ_ELF
14141   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14142     {
14143       long val;
14144
14145       if (cur_proc_ptr == (procS *) NULL)
14146         {
14147           as_warn (_(".frame outside of .ent"));
14148           demand_empty_rest_of_line ();
14149           return;
14150         }
14151
14152       cur_proc_ptr->frame_reg = tc_get_register (1);
14153
14154       SKIP_WHITESPACE ();
14155       if (*input_line_pointer++ != ','
14156           || get_absolute_expression_and_terminator (&val) != ',')
14157         {
14158           as_warn (_("Bad .frame directive"));
14159           --input_line_pointer;
14160           demand_empty_rest_of_line ();
14161           return;
14162         }
14163
14164       cur_proc_ptr->frame_offset = val;
14165       cur_proc_ptr->pc_reg = tc_get_register (0);
14166
14167       demand_empty_rest_of_line ();
14168     }
14169   else
14170 #endif /* OBJ_ELF */
14171     s_ignore (ignore);
14172 }
14173
14174 /* The .fmask and .mask directives. If the mdebug section is present
14175    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14176    embedded targets, s_mips_mask is used so that we can set the PDR
14177    information correctly. We can't use the ecoff routines because they
14178    make reference to the ecoff symbol table (in the mdebug section).  */
14179
14180 static void
14181 s_mips_mask (reg_type)
14182      char reg_type;
14183 {
14184 #ifdef OBJ_ELF
14185   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14186     {
14187       long mask, off;
14188
14189       if (cur_proc_ptr == (procS *) NULL)
14190         {
14191           as_warn (_(".mask/.fmask outside of .ent"));
14192           demand_empty_rest_of_line ();
14193           return;
14194         }
14195
14196       if (get_absolute_expression_and_terminator (&mask) != ',')
14197         {
14198           as_warn (_("Bad .mask/.fmask directive"));
14199           --input_line_pointer;
14200           demand_empty_rest_of_line ();
14201           return;
14202         }
14203
14204       off = get_absolute_expression ();
14205
14206       if (reg_type == 'F')
14207         {
14208           cur_proc_ptr->fpreg_mask = mask;
14209           cur_proc_ptr->fpreg_offset = off;
14210         }
14211       else
14212         {
14213           cur_proc_ptr->reg_mask = mask;
14214           cur_proc_ptr->reg_offset = off;
14215         }
14216
14217       demand_empty_rest_of_line ();
14218     }
14219   else
14220 #endif /* OBJ_ELF */
14221     s_ignore (reg_type);
14222 }
14223
14224 /* The .loc directive.  */
14225
14226 #if 0
14227 static void
14228 s_loc (x)
14229      int x;
14230 {
14231   symbolS *symbolP;
14232   int lineno;
14233   int addroff;
14234
14235   assert (now_seg == text_section);
14236
14237   lineno = get_number ();
14238   addroff = frag_now_fix ();
14239
14240   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14241   S_SET_TYPE (symbolP, N_SLINE);
14242   S_SET_OTHER (symbolP, 0);
14243   S_SET_DESC (symbolP, lineno);
14244   symbolP->sy_segment = now_seg;
14245 }
14246 #endif
14247
14248 /* A table describing all the processors gas knows about.  Names are
14249    matched in the order listed.
14250
14251    To ease comparison, please keep this table in the same order as
14252    gcc's mips_cpu_info_table[].  */
14253 static const struct mips_cpu_info mips_cpu_info_table[] =
14254 {
14255   /* Entries for generic ISAs */
14256   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14257   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14258   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14259   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14260   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14261   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14262   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14263
14264   /* MIPS I */
14265   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14266   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14267   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14268
14269   /* MIPS II */
14270   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14271
14272   /* MIPS III */
14273   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14274   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14275   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14276   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14277   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14278   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14279   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14280   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14281   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14282   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14283   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14284   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14285
14286   /* MIPS IV */
14287   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14288   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14289   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14290   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14291   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14292   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14293   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14294   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14295   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14296   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14297   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14298   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14299
14300   /* MIPS 32 */
14301   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14302   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14303   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14304
14305   /* MIPS 64 */
14306   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14307   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14308
14309   /* Broadcom SB-1 CPU core */
14310   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14311
14312   /* End marker */
14313   { NULL, 0, 0, 0 }
14314 };
14315
14316
14317 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14318    with a final "000" replaced by "k".  Ignore case.
14319
14320    Note: this function is shared between GCC and GAS.  */
14321
14322 static boolean
14323 mips_strict_matching_cpu_name_p (canonical, given)
14324      const char *canonical, *given;
14325 {
14326   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14327     given++, canonical++;
14328
14329   return ((*given == 0 && *canonical == 0)
14330           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14331 }
14332
14333
14334 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14335    CPU name.  We've traditionally allowed a lot of variation here.
14336
14337    Note: this function is shared between GCC and GAS.  */
14338
14339 static boolean
14340 mips_matching_cpu_name_p (canonical, given)
14341      const char *canonical, *given;
14342 {
14343   /* First see if the name matches exactly, or with a final "000"
14344      turned into "k".  */
14345   if (mips_strict_matching_cpu_name_p (canonical, given))
14346     return true;
14347
14348   /* If not, try comparing based on numerical designation alone.
14349      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14350   if (TOLOWER (*given) == 'r')
14351     given++;
14352   if (!ISDIGIT (*given))
14353     return false;
14354
14355   /* Skip over some well-known prefixes in the canonical name,
14356      hoping to find a number there too.  */
14357   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14358     canonical += 2;
14359   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14360     canonical += 2;
14361   else if (TOLOWER (canonical[0]) == 'r')
14362     canonical += 1;
14363
14364   return mips_strict_matching_cpu_name_p (canonical, given);
14365 }
14366
14367
14368 /* Parse an option that takes the name of a processor as its argument.
14369    OPTION is the name of the option and CPU_STRING is the argument.
14370    Return the corresponding processor enumeration if the CPU_STRING is
14371    recognized, otherwise report an error and return null.
14372
14373    A similar function exists in GCC.  */
14374
14375 static const struct mips_cpu_info *
14376 mips_parse_cpu (option, cpu_string)
14377      const char *option, *cpu_string;
14378 {
14379   const struct mips_cpu_info *p;
14380
14381   /* 'from-abi' selects the most compatible architecture for the given
14382      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14383      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14384      version.  Look first at the -mgp options, if given, otherwise base
14385      the choice on MIPS_DEFAULT_64BIT.
14386
14387      Treat NO_ABI like the EABIs.  One reason to do this is that the
14388      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14389      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14390      'mips64', just as we did in the days before 'from-abi'.  */
14391   if (strcasecmp (cpu_string, "from-abi") == 0)
14392     {
14393       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14394         return mips_cpu_info_from_isa (ISA_MIPS1);
14395
14396       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14397         return mips_cpu_info_from_isa (ISA_MIPS3);
14398
14399       if (file_mips_gp32 >= 0)
14400         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14401
14402       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14403                                      ? ISA_MIPS3
14404                                      : ISA_MIPS1);
14405     }
14406
14407   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14408   if (strcasecmp (cpu_string, "default") == 0)
14409     return 0;
14410
14411   for (p = mips_cpu_info_table; p->name != 0; p++)
14412     if (mips_matching_cpu_name_p (p->name, cpu_string))
14413       return p;
14414
14415   as_bad ("Bad value (%s) for %s", cpu_string, option);
14416   return 0;
14417 }
14418
14419 /* Return the canonical processor information for ISA (a member of the
14420    ISA_MIPS* enumeration).  */
14421
14422 static const struct mips_cpu_info *
14423 mips_cpu_info_from_isa (isa)
14424      int isa;
14425 {
14426   int i;
14427
14428   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14429     if (mips_cpu_info_table[i].is_isa
14430         && isa == mips_cpu_info_table[i].isa)
14431       return (&mips_cpu_info_table[i]);
14432
14433   return NULL;
14434 }
14435 \f
14436 static void
14437 show (stream, string, col_p, first_p)
14438      FILE *stream;
14439      const char *string;
14440      int *col_p;
14441      int *first_p;
14442 {
14443   if (*first_p)
14444     {
14445       fprintf (stream, "%24s", "");
14446       *col_p = 24;
14447     }
14448   else
14449     {
14450       fprintf (stream, ", ");
14451       *col_p += 2;
14452     }
14453
14454   if (*col_p + strlen (string) > 72)
14455     {
14456       fprintf (stream, "\n%24s", "");
14457       *col_p = 24;
14458     }
14459
14460   fprintf (stream, "%s", string);
14461   *col_p += strlen (string);
14462
14463   *first_p = 0;
14464 }
14465
14466 void
14467 md_show_usage (stream)
14468      FILE *stream;
14469 {
14470   int column, first;
14471   size_t i;
14472
14473   fprintf (stream, _("\
14474 MIPS options:\n\
14475 -membedded-pic          generate embedded position independent code\n\
14476 -EB                     generate big endian output\n\
14477 -EL                     generate little endian output\n\
14478 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14479 -G NUM                  allow referencing objects up to NUM bytes\n\
14480                         implicitly with the gp register [default 8]\n"));
14481   fprintf (stream, _("\
14482 -mips1                  generate MIPS ISA I instructions\n\
14483 -mips2                  generate MIPS ISA II instructions\n\
14484 -mips3                  generate MIPS ISA III instructions\n\
14485 -mips4                  generate MIPS ISA IV instructions\n\
14486 -mips5                  generate MIPS ISA V instructions\n\
14487 -mips32                 generate MIPS32 ISA instructions\n\
14488 -mips64                 generate MIPS64 ISA instructions\n\
14489 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14490
14491   first = 1;
14492
14493   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14494     show (stream, mips_cpu_info_table[i].name, &column, &first);
14495   show (stream, "from-abi", &column, &first);
14496   fputc ('\n', stream);
14497
14498   fprintf (stream, _("\
14499 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14500 -no-mCPU                don't generate code specific to CPU.\n\
14501                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14502
14503   first = 1;
14504
14505   show (stream, "3900", &column, &first);
14506   show (stream, "4010", &column, &first);
14507   show (stream, "4100", &column, &first);
14508   show (stream, "4650", &column, &first);
14509   fputc ('\n', stream);
14510
14511   fprintf (stream, _("\
14512 -mips16                 generate mips16 instructions\n\
14513 -no-mips16              do not generate mips16 instructions\n"));
14514   fprintf (stream, _("\
14515 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14516 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14517 -O0                     remove unneeded NOPs, do not swap branches\n\
14518 -O                      remove unneeded NOPs and swap branches\n\
14519 -n                      warn about NOPs generated from macros\n\
14520 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14521 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14522 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14523 #ifdef OBJ_ELF
14524   fprintf (stream, _("\
14525 -KPIC, -call_shared     generate SVR4 position independent code\n\
14526 -non_shared             do not generate position independent code\n\
14527 -xgot                   assume a 32 bit GOT\n\
14528 -mabi=ABI               create ABI conformant object file for:\n"));
14529
14530   first = 1;
14531
14532   show (stream, "32", &column, &first);
14533   show (stream, "o64", &column, &first);
14534   show (stream, "n32", &column, &first);
14535   show (stream, "64", &column, &first);
14536   show (stream, "eabi", &column, &first);
14537
14538   fputc ('\n', stream);
14539
14540   fprintf (stream, _("\
14541 -32                     create o32 ABI object file (default)\n\
14542 -n32                    create n32 ABI object file\n\
14543 -64                     create 64 ABI object file\n"));
14544 #endif
14545 }