* config/tc-mips.c (append_insn): In a compound relocation, take the
[platform/upstream/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 /* Whether or not we have code that can call pic code.  */
135 int mips_abicalls = FALSE;
136
137 /* This is the set of options which may be modified by the .set
138    pseudo-op.  We use a struct so that .set push and .set pop are more
139    reliable.  */
140
141 struct mips_set_options
142 {
143   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
144      if it has not been initialized.  Changed by `.set mipsN', and the
145      -mipsN command line option, and the default CPU.  */
146   int isa;
147   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
148      if they have not been initialized.  Changed by `.set <asename>', by
149      command line options, and based on the default architecture.  */
150   int ase_mips3d;
151   int ase_mdmx;
152   /* Whether we are assembling for the mips16 processor.  0 if we are
153      not, 1 if we are, and -1 if the value has not been initialized.
154      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
155      -nomips16 command line options, and the default CPU.  */
156   int mips16;
157   /* Non-zero if we should not reorder instructions.  Changed by `.set
158      reorder' and `.set noreorder'.  */
159   int noreorder;
160   /* Non-zero if we should not permit the $at ($1) register to be used
161      in instructions.  Changed by `.set at' and `.set noat'.  */
162   int noat;
163   /* Non-zero if we should warn when a macro instruction expands into
164      more than one machine instruction.  Changed by `.set nomacro' and
165      `.set macro'.  */
166   int warn_about_macros;
167   /* Non-zero if we should not move instructions.  Changed by `.set
168      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
169   int nomove;
170   /* Non-zero if we should not optimize branches by moving the target
171      of the branch into the delay slot.  Actually, we don't perform
172      this optimization anyhow.  Changed by `.set bopt' and `.set
173      nobopt'.  */
174   int nobopt;
175   /* Non-zero if we should not autoextend mips16 instructions.
176      Changed by `.set autoextend' and `.set noautoextend'.  */
177   int noautoextend;
178   /* Restrict general purpose registers and floating point registers
179      to 32 bit.  This is initially determined when -mgp32 or -mfp32
180      is passed but can changed if the assembler code uses .set mipsN.  */
181   int gp32;
182   int fp32;
183 };
184
185 /* True if -mgp32 was passed.  */
186 static int file_mips_gp32 = -1;
187
188 /* True if -mfp32 was passed.  */
189 static int file_mips_fp32 = -1;
190
191 /* This is the struct we use to hold the current set of options.  Note
192    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
193    -1 to indicate that they have not been initialized.  */
194
195 static struct mips_set_options mips_opts =
196 {
197   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
198 };
199
200 /* These variables are filled in with the masks of registers used.
201    The object format code reads them and puts them in the appropriate
202    place.  */
203 unsigned long mips_gprmask;
204 unsigned long mips_cprmask[4];
205
206 /* MIPS ISA we are using for this output file.  */
207 static int file_mips_isa = ISA_UNKNOWN;
208
209 /* True if -mips16 was passed or implied by arguments passed on the
210    command line (e.g., by -march).  */
211 static int file_ase_mips16;
212
213 /* True if -mips3d was passed or implied by arguments passed on the
214    command line (e.g., by -march).  */
215 static int file_ase_mips3d;
216
217 /* True if -mdmx was passed or implied by arguments passed on the
218    command line (e.g., by -march).  */
219 static int file_ase_mdmx;
220
221 /* The argument of the -march= flag.  The architecture we are assembling.  */
222 static int mips_arch = CPU_UNKNOWN;
223 static const char *mips_arch_string;
224 static const struct mips_cpu_info *mips_arch_info;
225
226 /* The argument of the -mtune= flag.  The architecture for which we
227    are optimizing.  */
228 static int mips_tune = CPU_UNKNOWN;
229 static const char *mips_tune_string;
230 static const struct mips_cpu_info *mips_tune_info;
231
232 /* True when generating 32-bit code for a 64-bit processor.  */
233 static int mips_32bitmode = 0;
234
235 /* Some ISA's have delay slots for instructions which read or write
236    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
237    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
238    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
239    delay slot in this ISA.  The uses of this macro assume that any
240    ISA that has delay slots for one of these, has them for all.  They
241    also assume that ISAs which don't have delays for these insns, don't
242    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
243 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
244    (ISA) == ISA_MIPS1                       \
245    || (ISA) == ISA_MIPS2                    \
246    || (ISA) == ISA_MIPS3                    \
247    )
248
249 /* True if the given ABI requires 32-bit registers.  */
250 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
251
252 /* Likewise 64-bit registers.  */
253 #define ABI_NEEDS_64BIT_REGS(ABI) \
254   ((ABI) == N32_ABI               \
255    || (ABI) == N64_ABI            \
256    || (ABI) == O64_ABI)
257
258 /*  Return true if ISA supports 64 bit gp register instructions.  */
259 #define ISA_HAS_64BIT_REGS(ISA) (    \
260    (ISA) == ISA_MIPS3                \
261    || (ISA) == ISA_MIPS4             \
262    || (ISA) == ISA_MIPS5             \
263    || (ISA) == ISA_MIPS64            \
264    )
265
266 /* Return true if ISA supports 64-bit right rotate (dror et al.)
267    instructions.  */
268 #define ISA_HAS_DROR(ISA) (     \
269    0                            \
270    )
271
272 /* Return true if ISA supports 32-bit right rotate (ror et al.)
273    instructions.  */
274 #define ISA_HAS_ROR(ISA) (      \
275    (ISA) == ISA_MIPS32R2        \
276    )
277
278 #define HAVE_32BIT_GPRS                            \
279     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
280
281 #define HAVE_32BIT_FPRS                            \
282     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
283
284 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
285 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
286
287 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
288
289 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
290
291 /* We can only have 64bit addresses if the object file format
292    supports it.  */
293 #define HAVE_32BIT_ADDRESSES                           \
294    (HAVE_32BIT_GPRS                                    \
295     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
296          || ! HAVE_64BIT_OBJECTS)                      \
297         && mips_pic != EMBEDDED_PIC))
298
299 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
300 #define HAVE_64BIT_ADDRESS_CONSTANTS (HAVE_64BIT_ADDRESSES \
301                                       || HAVE_64BIT_GPRS)
302
303 /* Return true if the given CPU supports the MIPS16 ASE.  */
304 #define CPU_HAS_MIPS16(cpu)                                             \
305    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
306     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
307
308 /* Return true if the given CPU supports the MIPS3D ASE.  */
309 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
310                                  )
311
312 /* Return true if the given CPU supports the MDMX ASE.  */
313 #define CPU_HAS_MDMX(cpu)       (FALSE                 \
314                                  )
315
316 /* True if CPU has a dror instruction.  */
317 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
318
319 /* True if CPU has a ror instruction.  */
320 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
321
322 /* Whether the processor uses hardware interlocks to protect
323    reads from the HI and LO registers, and thus does not
324    require nops to be inserted.  */
325
326 #define hilo_interlocks (mips_arch == CPU_R4010                       \
327                          || mips_arch == CPU_VR5500                   \
328                          || mips_arch == CPU_SB1                      \
329                          )
330
331 /* Whether the processor uses hardware interlocks to protect reads
332    from the GPRs, and thus does not require nops to be inserted.  */
333 #define gpr_interlocks \
334   (mips_opts.isa != ISA_MIPS1  \
335    || mips_arch == CPU_VR5400  \
336    || mips_arch == CPU_VR5500  \
337    || mips_arch == CPU_R3900)
338
339 /* As with other "interlocks" this is used by hardware that has FP
340    (co-processor) interlocks.  */
341 /* Itbl support may require additional care here.  */
342 #define cop_interlocks (mips_arch == CPU_R4300                        \
343                         || mips_arch == CPU_VR5400                    \
344                         || mips_arch == CPU_VR5500                    \
345                         || mips_arch == CPU_SB1                       \
346                         )
347
348 /* Is this a mfhi or mflo instruction?  */
349 #define MF_HILO_INSN(PINFO) \
350           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
351
352 /* MIPS PIC level.  */
353
354 enum mips_pic_level mips_pic;
355
356 /* Warn about all NOPS that the assembler generates.  */
357 static int warn_nops = 0;
358
359 /* 1 if we should generate 32 bit offsets from the $gp register in
360    SVR4_PIC mode.  Currently has no meaning in other modes.  */
361 static int mips_big_got = 0;
362
363 /* 1 if trap instructions should used for overflow rather than break
364    instructions.  */
365 static int mips_trap = 0;
366
367 /* 1 if double width floating point constants should not be constructed
368    by assembling two single width halves into two single width floating
369    point registers which just happen to alias the double width destination
370    register.  On some architectures this aliasing can be disabled by a bit
371    in the status register, and the setting of this bit cannot be determined
372    automatically at assemble time.  */
373 static int mips_disable_float_construction;
374
375 /* Non-zero if any .set noreorder directives were used.  */
376
377 static int mips_any_noreorder;
378
379 /* Non-zero if nops should be inserted when the register referenced in
380    an mfhi/mflo instruction is read in the next two instructions.  */
381 static int mips_7000_hilo_fix;
382
383 /* The size of the small data section.  */
384 static unsigned int g_switch_value = 8;
385 /* Whether the -G option was used.  */
386 static int g_switch_seen = 0;
387
388 #define N_RMASK 0xc4
389 #define N_VFP   0xd4
390
391 /* If we can determine in advance that GP optimization won't be
392    possible, we can skip the relaxation stuff that tries to produce
393    GP-relative references.  This makes delay slot optimization work
394    better.
395
396    This function can only provide a guess, but it seems to work for
397    gcc output.  It needs to guess right for gcc, otherwise gcc
398    will put what it thinks is a GP-relative instruction in a branch
399    delay slot.
400
401    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
402    fixed it for the non-PIC mode.  KR 95/04/07  */
403 static int nopic_need_relax PARAMS ((symbolS *, int));
404
405 /* handle of the OPCODE hash table */
406 static struct hash_control *op_hash = NULL;
407
408 /* The opcode hash table we use for the mips16.  */
409 static struct hash_control *mips16_op_hash = NULL;
410
411 /* This array holds the chars that always start a comment.  If the
412     pre-processor is disabled, these aren't very useful */
413 const char comment_chars[] = "#";
414
415 /* This array holds the chars that only start a comment at the beginning of
416    a line.  If the line seems to have the form '# 123 filename'
417    .line and .file directives will appear in the pre-processed output */
418 /* Note that input_file.c hand checks for '#' at the beginning of the
419    first line of the input file.  This is because the compiler outputs
420    #NO_APP at the beginning of its output.  */
421 /* Also note that C style comments are always supported.  */
422 const char line_comment_chars[] = "#";
423
424 /* This array holds machine specific line separator characters.  */
425 const char line_separator_chars[] = ";";
426
427 /* Chars that can be used to separate mant from exp in floating point nums */
428 const char EXP_CHARS[] = "eE";
429
430 /* Chars that mean this number is a floating point constant */
431 /* As in 0f12.456 */
432 /* or    0d1.2345e12 */
433 const char FLT_CHARS[] = "rRsSfFdDxXpP";
434
435 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
436    changed in read.c .  Ideally it shouldn't have to know about it at all,
437    but nothing is ideal around here.
438  */
439
440 static char *insn_error;
441
442 static int auto_align = 1;
443
444 /* When outputting SVR4 PIC code, the assembler needs to know the
445    offset in the stack frame from which to restore the $gp register.
446    This is set by the .cprestore pseudo-op, and saved in this
447    variable.  */
448 static offsetT mips_cprestore_offset = -1;
449
450 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
451    more optimizations, it can use a register value instead of a memory-saved
452    offset and even an other register than $gp as global pointer.  */
453 static offsetT mips_cpreturn_offset = -1;
454 static int mips_cpreturn_register = -1;
455 static int mips_gp_register = GP;
456 static int mips_gprel_offset = 0;
457
458 /* Whether mips_cprestore_offset has been set in the current function
459    (or whether it has already been warned about, if not).  */
460 static int mips_cprestore_valid = 0;
461
462 /* This is the register which holds the stack frame, as set by the
463    .frame pseudo-op.  This is needed to implement .cprestore.  */
464 static int mips_frame_reg = SP;
465
466 /* Whether mips_frame_reg has been set in the current function
467    (or whether it has already been warned about, if not).  */
468 static int mips_frame_reg_valid = 0;
469
470 /* To output NOP instructions correctly, we need to keep information
471    about the previous two instructions.  */
472
473 /* Whether we are optimizing.  The default value of 2 means to remove
474    unneeded NOPs and swap branch instructions when possible.  A value
475    of 1 means to not swap branches.  A value of 0 means to always
476    insert NOPs.  */
477 static int mips_optimize = 2;
478
479 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
480    equivalent to seeing no -g option at all.  */
481 static int mips_debug = 0;
482
483 /* The previous instruction.  */
484 static struct mips_cl_insn prev_insn;
485
486 /* The instruction before prev_insn.  */
487 static struct mips_cl_insn prev_prev_insn;
488
489 /* If we don't want information for prev_insn or prev_prev_insn, we
490    point the insn_mo field at this dummy integer.  */
491 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
492
493 /* Non-zero if prev_insn is valid.  */
494 static int prev_insn_valid;
495
496 /* The frag for the previous instruction.  */
497 static struct frag *prev_insn_frag;
498
499 /* The offset into prev_insn_frag for the previous instruction.  */
500 static long prev_insn_where;
501
502 /* The reloc type for the previous instruction, if any.  */
503 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
504
505 /* The reloc for the previous instruction, if any.  */
506 static fixS *prev_insn_fixp[3];
507
508 /* Non-zero if the previous instruction was in a delay slot.  */
509 static int prev_insn_is_delay_slot;
510
511 /* Non-zero if the previous instruction was in a .set noreorder.  */
512 static int prev_insn_unreordered;
513
514 /* Non-zero if the previous instruction uses an extend opcode (if
515    mips16).  */
516 static int prev_insn_extended;
517
518 /* Non-zero if the previous previous instruction was in a .set
519    noreorder.  */
520 static int prev_prev_insn_unreordered;
521
522 /* If this is set, it points to a frag holding nop instructions which
523    were inserted before the start of a noreorder section.  If those
524    nops turn out to be unnecessary, the size of the frag can be
525    decreased.  */
526 static fragS *prev_nop_frag;
527
528 /* The number of nop instructions we created in prev_nop_frag.  */
529 static int prev_nop_frag_holds;
530
531 /* The number of nop instructions that we know we need in
532    prev_nop_frag.  */
533 static int prev_nop_frag_required;
534
535 /* The number of instructions we've seen since prev_nop_frag.  */
536 static int prev_nop_frag_since;
537
538 /* For ECOFF and ELF, relocations against symbols are done in two
539    parts, with a HI relocation and a LO relocation.  Each relocation
540    has only 16 bits of space to store an addend.  This means that in
541    order for the linker to handle carries correctly, it must be able
542    to locate both the HI and the LO relocation.  This means that the
543    relocations must appear in order in the relocation table.
544
545    In order to implement this, we keep track of each unmatched HI
546    relocation.  We then sort them so that they immediately precede the
547    corresponding LO relocation.  */
548
549 struct mips_hi_fixup
550 {
551   /* Next HI fixup.  */
552   struct mips_hi_fixup *next;
553   /* This fixup.  */
554   fixS *fixp;
555   /* The section this fixup is in.  */
556   segT seg;
557 };
558
559 /* The list of unmatched HI relocs.  */
560
561 static struct mips_hi_fixup *mips_hi_fixup_list;
562
563 /* The frag containing the last explicit relocation operator.
564    Null if explicit relocations have not been used.  */
565
566 static fragS *prev_reloc_op_frag;
567
568 /* Map normal MIPS register numbers to mips16 register numbers.  */
569
570 #define X ILLEGAL_REG
571 static const int mips32_to_16_reg_map[] =
572 {
573   X, X, 2, 3, 4, 5, 6, 7,
574   X, X, X, X, X, X, X, X,
575   0, 1, X, X, X, X, X, X,
576   X, X, X, X, X, X, X, X
577 };
578 #undef X
579
580 /* Map mips16 register numbers to normal MIPS register numbers.  */
581
582 static const unsigned int mips16_to_32_reg_map[] =
583 {
584   16, 17, 2, 3, 4, 5, 6, 7
585 };
586
587 static int mips_fix_4122_bugs;
588
589 /* We don't relax branches by default, since this causes us to expand
590    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
591    fail to compute the offset before expanding the macro to the most
592    efficient expansion.  */
593
594 static int mips_relax_branch;
595 \f
596 /* Since the MIPS does not have multiple forms of PC relative
597    instructions, we do not have to do relaxing as is done on other
598    platforms.  However, we do have to handle GP relative addressing
599    correctly, which turns out to be a similar problem.
600
601    Every macro that refers to a symbol can occur in (at least) two
602    forms, one with GP relative addressing and one without.  For
603    example, loading a global variable into a register generally uses
604    a macro instruction like this:
605      lw $4,i
606    If i can be addressed off the GP register (this is true if it is in
607    the .sbss or .sdata section, or if it is known to be smaller than
608    the -G argument) this will generate the following instruction:
609      lw $4,i($gp)
610    This instruction will use a GPREL reloc.  If i can not be addressed
611    off the GP register, the following instruction sequence will be used:
612      lui $at,i
613      lw $4,i($at)
614    In this case the first instruction will have a HI16 reloc, and the
615    second reloc will have a LO16 reloc.  Both relocs will be against
616    the symbol i.
617
618    The issue here is that we may not know whether i is GP addressable
619    until after we see the instruction that uses it.  Therefore, we
620    want to be able to choose the final instruction sequence only at
621    the end of the assembly.  This is similar to the way other
622    platforms choose the size of a PC relative instruction only at the
623    end of assembly.
624
625    When generating position independent code we do not use GP
626    addressing in quite the same way, but the issue still arises as
627    external symbols and local symbols must be handled differently.
628
629    We handle these issues by actually generating both possible
630    instruction sequences.  The longer one is put in a frag_var with
631    type rs_machine_dependent.  We encode what to do with the frag in
632    the subtype field.  We encode (1) the number of existing bytes to
633    replace, (2) the number of new bytes to use, (3) the offset from
634    the start of the existing bytes to the first reloc we must generate
635    (that is, the offset is applied from the start of the existing
636    bytes after they are replaced by the new bytes, if any), (4) the
637    offset from the start of the existing bytes to the second reloc,
638    (5) whether a third reloc is needed (the third reloc is always four
639    bytes after the second reloc), and (6) whether to warn if this
640    variant is used (this is sometimes needed if .set nomacro or .set
641    noat is in effect).  All these numbers are reasonably small.
642
643    Generating two instruction sequences must be handled carefully to
644    ensure that delay slots are handled correctly.  Fortunately, there
645    are a limited number of cases.  When the second instruction
646    sequence is generated, append_insn is directed to maintain the
647    existing delay slot information, so it continues to apply to any
648    code after the second instruction sequence.  This means that the
649    second instruction sequence must not impose any requirements not
650    required by the first instruction sequence.
651
652    These variant frags are then handled in functions called by the
653    machine independent code.  md_estimate_size_before_relax returns
654    the final size of the frag.  md_convert_frag sets up the final form
655    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
656    one if needed.  */
657 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
658   ((relax_substateT) \
659    (((old) << 23) \
660     | ((new) << 16) \
661     | (((reloc1) + 64) << 9) \
662     | (((reloc2) + 64) << 2) \
663     | ((reloc3) ? (1 << 1) : 0) \
664     | ((warn) ? 1 : 0)))
665 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
666 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
667 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
668 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
669 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
670 #define RELAX_WARN(i) ((i) & 1)
671
672 /* Branch without likely bit.  If label is out of range, we turn:
673
674         beq reg1, reg2, label
675         delay slot
676
677    into
678
679         bne reg1, reg2, 0f
680         nop
681         j label
682      0: delay slot
683
684    with the following opcode replacements:
685
686         beq <-> bne
687         blez <-> bgtz
688         bltz <-> bgez
689         bc1f <-> bc1t
690
691         bltzal <-> bgezal  (with jal label instead of j label)
692
693    Even though keeping the delay slot instruction in the delay slot of
694    the branch would be more efficient, it would be very tricky to do
695    correctly, because we'd have to introduce a variable frag *after*
696    the delay slot instruction, and expand that instead.  Let's do it
697    the easy way for now, even if the branch-not-taken case now costs
698    one additional instruction.  Out-of-range branches are not supposed
699    to be common, anyway.
700
701    Branch likely.  If label is out of range, we turn:
702
703         beql reg1, reg2, label
704         delay slot (annulled if branch not taken)
705
706    into
707
708         beql reg1, reg2, 1f
709         nop
710         beql $0, $0, 2f
711         nop
712      1: j[al] label
713         delay slot (executed only if branch taken)
714      2:
715
716    It would be possible to generate a shorter sequence by losing the
717    likely bit, generating something like:
718
719         bne reg1, reg2, 0f
720         nop
721         j[al] label
722         delay slot (executed only if branch taken)
723      0:
724
725         beql -> bne
726         bnel -> beq
727         blezl -> bgtz
728         bgtzl -> blez
729         bltzl -> bgez
730         bgezl -> bltz
731         bc1fl -> bc1t
732         bc1tl -> bc1f
733
734         bltzall -> bgezal  (with jal label instead of j label)
735         bgezall -> bltzal  (ditto)
736
737
738    but it's not clear that it would actually improve performance.  */
739 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
740   ((relax_substateT) \
741    (0xc0000000 \
742     | ((toofar) ? 1 : 0) \
743     | ((link) ? 2 : 0) \
744     | ((likely) ? 4 : 0) \
745     | ((uncond) ? 8 : 0)))
746 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
747 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
748 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
749 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
750 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
751
752 /* For mips16 code, we use an entirely different form of relaxation.
753    mips16 supports two versions of most instructions which take
754    immediate values: a small one which takes some small value, and a
755    larger one which takes a 16 bit value.  Since branches also follow
756    this pattern, relaxing these values is required.
757
758    We can assemble both mips16 and normal MIPS code in a single
759    object.  Therefore, we need to support this type of relaxation at
760    the same time that we support the relaxation described above.  We
761    use the high bit of the subtype field to distinguish these cases.
762
763    The information we store for this type of relaxation is the
764    argument code found in the opcode file for this relocation, whether
765    the user explicitly requested a small or extended form, and whether
766    the relocation is in a jump or jal delay slot.  That tells us the
767    size of the value, and how it should be stored.  We also store
768    whether the fragment is considered to be extended or not.  We also
769    store whether this is known to be a branch to a different section,
770    whether we have tried to relax this frag yet, and whether we have
771    ever extended a PC relative fragment because of a shift count.  */
772 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
773   (0x80000000                                                   \
774    | ((type) & 0xff)                                            \
775    | ((small) ? 0x100 : 0)                                      \
776    | ((ext) ? 0x200 : 0)                                        \
777    | ((dslot) ? 0x400 : 0)                                      \
778    | ((jal_dslot) ? 0x800 : 0))
779 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
780 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
781 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
782 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
783 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
784 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
785 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
786 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
787 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
788 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
789 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
790 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
791
792 /* Is the given value a sign-extended 32-bit value?  */
793 #define IS_SEXT_32BIT_NUM(x)                                            \
794   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
795    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
796
797 /* Is the given value a sign-extended 16-bit value?  */
798 #define IS_SEXT_16BIT_NUM(x)                                            \
799   (((x) &~ (offsetT) 0x7fff) == 0                                       \
800    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
801
802 \f
803 /* Prototypes for static functions.  */
804
805 #ifdef __STDC__
806 #define internalError() \
807     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
808 #else
809 #define internalError() as_fatal (_("MIPS internal Error"));
810 #endif
811
812 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
813
814 static inline bfd_boolean reloc_needs_lo_p
815   PARAMS ((bfd_reloc_code_real_type));
816 static inline bfd_boolean fixup_has_matching_lo_p
817   PARAMS ((fixS *));
818 static int insn_uses_reg
819   PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
820            enum mips_regclass class));
821 static int reg_needs_delay
822   PARAMS ((unsigned int));
823 static void mips16_mark_labels
824   PARAMS ((void));
825 static void append_insn
826   PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
827            bfd_reloc_code_real_type *r));
828 static void mips_no_prev_insn
829   PARAMS ((int));
830 static void mips_emit_delays
831   PARAMS ((bfd_boolean));
832 #ifdef USE_STDARG
833 static void macro_build
834   PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
835            const char *fmt, ...));
836 #else
837 static void macro_build ();
838 #endif
839 static void mips16_macro_build
840   PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
841 static void macro_build_jalr
842   PARAMS ((int, expressionS *));
843 static void macro_build_lui
844   PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
845 static void macro_build_ldst_constoffset
846   PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
847            int valreg, int breg));
848 static void set_at
849   PARAMS ((int *counter, int reg, int unsignedp));
850 static void check_absolute_expr
851   PARAMS ((struct mips_cl_insn * ip, expressionS *));
852 static void load_register
853   PARAMS ((int *, int, expressionS *, int));
854 static void load_address
855   PARAMS ((int *, int, expressionS *, int *));
856 static void move_register
857   PARAMS ((int *, int, int));
858 static void macro
859   PARAMS ((struct mips_cl_insn * ip));
860 static void mips16_macro
861   PARAMS ((struct mips_cl_insn * ip));
862 #ifdef LOSING_COMPILER
863 static void macro2
864   PARAMS ((struct mips_cl_insn * ip));
865 #endif
866 static void mips_ip
867   PARAMS ((char *str, struct mips_cl_insn * ip));
868 static void mips16_ip
869   PARAMS ((char *str, struct mips_cl_insn * ip));
870 static void mips16_immed
871   PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
872            bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
873 static bfd_boolean parse_relocation
874   PARAMS ((char **, bfd_reloc_code_real_type *));
875 static size_t my_getSmallExpression
876   PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
877 static void my_getExpression
878   PARAMS ((expressionS *, char *));
879 #ifdef OBJ_ELF
880 static int support_64bit_objects
881   PARAMS((void));
882 #endif
883 static void mips_set_option_string
884   PARAMS ((const char **, const char *));
885 static symbolS *get_symbol
886   PARAMS ((void));
887 static void mips_align
888   PARAMS ((int to, int fill, symbolS *label));
889 static void s_align
890   PARAMS ((int));
891 static void s_change_sec
892   PARAMS ((int));
893 static void s_change_section
894   PARAMS ((int));
895 static void s_cons
896   PARAMS ((int));
897 static void s_float_cons
898   PARAMS ((int));
899 static void s_mips_globl
900   PARAMS ((int));
901 static void s_option
902   PARAMS ((int));
903 static void s_mipsset
904   PARAMS ((int));
905 static void s_abicalls
906   PARAMS ((int));
907 static void s_cpload
908   PARAMS ((int));
909 static void s_cpsetup
910   PARAMS ((int));
911 static void s_cplocal
912   PARAMS ((int));
913 static void s_cprestore
914   PARAMS ((int));
915 static void s_cpreturn
916   PARAMS ((int));
917 static void s_gpvalue
918   PARAMS ((int));
919 static void s_gpword
920   PARAMS ((int));
921 static void s_gpdword
922   PARAMS ((int));
923 static void s_cpadd
924   PARAMS ((int));
925 static void s_insn
926   PARAMS ((int));
927 static void md_obj_begin
928   PARAMS ((void));
929 static void md_obj_end
930   PARAMS ((void));
931 static long get_number
932   PARAMS ((void));
933 static void s_mips_ent
934   PARAMS ((int));
935 static void s_mips_end
936   PARAMS ((int));
937 static void s_mips_frame
938   PARAMS ((int));
939 static void s_mips_mask
940   PARAMS ((int));
941 static void s_mips_stab
942   PARAMS ((int));
943 static void s_mips_weakext
944   PARAMS ((int));
945 static void s_mips_file
946   PARAMS ((int));
947 static void s_mips_loc
948   PARAMS ((int));
949 static bfd_boolean pic_need_relax
950   PARAMS ((symbolS *, asection *));
951 static int mips16_extended_frag
952   PARAMS ((fragS *, asection *, long));
953 static int relaxed_branch_length (fragS *, asection *, int);
954 static int validate_mips_insn
955   PARAMS ((const struct mips_opcode *));
956 static void show
957   PARAMS ((FILE *, const char *, int *, int *));
958 #ifdef OBJ_ELF
959 static int mips_need_elf_addend_fixup
960   PARAMS ((fixS *));
961 #endif
962
963 /* Table and functions used to map between CPU/ISA names, and
964    ISA levels, and CPU numbers.  */
965
966 struct mips_cpu_info
967 {
968   const char *name;           /* CPU or ISA name.  */
969   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
970   int isa;                    /* ISA level.  */
971   int cpu;                    /* CPU number (default CPU if ISA).  */
972 };
973
974 static void mips_set_architecture
975   PARAMS ((const struct mips_cpu_info *));
976 static void mips_set_tune
977   PARAMS ((const struct mips_cpu_info *));
978 static bfd_boolean mips_strict_matching_cpu_name_p
979   PARAMS ((const char *, const char *));
980 static bfd_boolean mips_matching_cpu_name_p
981   PARAMS ((const char *, const char *));
982 static const struct mips_cpu_info *mips_parse_cpu
983   PARAMS ((const char *, const char *));
984 static const struct mips_cpu_info *mips_cpu_info_from_isa
985   PARAMS ((int));
986 \f
987 /* Pseudo-op table.
988
989    The following pseudo-ops from the Kane and Heinrich MIPS book
990    should be defined here, but are currently unsupported: .alias,
991    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
992
993    The following pseudo-ops from the Kane and Heinrich MIPS book are
994    specific to the type of debugging information being generated, and
995    should be defined by the object format: .aent, .begin, .bend,
996    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
997    .vreg.
998
999    The following pseudo-ops from the Kane and Heinrich MIPS book are
1000    not MIPS CPU specific, but are also not specific to the object file
1001    format.  This file is probably the best place to define them, but
1002    they are not currently supported: .asm0, .endr, .lab, .repeat,
1003    .struct.  */
1004
1005 static const pseudo_typeS mips_pseudo_table[] =
1006 {
1007   /* MIPS specific pseudo-ops.  */
1008   {"option", s_option, 0},
1009   {"set", s_mipsset, 0},
1010   {"rdata", s_change_sec, 'r'},
1011   {"sdata", s_change_sec, 's'},
1012   {"livereg", s_ignore, 0},
1013   {"abicalls", s_abicalls, 0},
1014   {"cpload", s_cpload, 0},
1015   {"cpsetup", s_cpsetup, 0},
1016   {"cplocal", s_cplocal, 0},
1017   {"cprestore", s_cprestore, 0},
1018   {"cpreturn", s_cpreturn, 0},
1019   {"gpvalue", s_gpvalue, 0},
1020   {"gpword", s_gpword, 0},
1021   {"gpdword", s_gpdword, 0},
1022   {"cpadd", s_cpadd, 0},
1023   {"insn", s_insn, 0},
1024
1025   /* Relatively generic pseudo-ops that happen to be used on MIPS
1026      chips.  */
1027   {"asciiz", stringer, 1},
1028   {"bss", s_change_sec, 'b'},
1029   {"err", s_err, 0},
1030   {"half", s_cons, 1},
1031   {"dword", s_cons, 3},
1032   {"weakext", s_mips_weakext, 0},
1033
1034   /* These pseudo-ops are defined in read.c, but must be overridden
1035      here for one reason or another.  */
1036   {"align", s_align, 0},
1037   {"byte", s_cons, 0},
1038   {"data", s_change_sec, 'd'},
1039   {"double", s_float_cons, 'd'},
1040   {"float", s_float_cons, 'f'},
1041   {"globl", s_mips_globl, 0},
1042   {"global", s_mips_globl, 0},
1043   {"hword", s_cons, 1},
1044   {"int", s_cons, 2},
1045   {"long", s_cons, 2},
1046   {"octa", s_cons, 4},
1047   {"quad", s_cons, 3},
1048   {"section", s_change_section, 0},
1049   {"short", s_cons, 1},
1050   {"single", s_float_cons, 'f'},
1051   {"stabn", s_mips_stab, 'n'},
1052   {"text", s_change_sec, 't'},
1053   {"word", s_cons, 2},
1054
1055   { "extern", ecoff_directive_extern, 0},
1056
1057   { NULL, NULL, 0 },
1058 };
1059
1060 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1061 {
1062   /* These pseudo-ops should be defined by the object file format.
1063      However, a.out doesn't support them, so we have versions here.  */
1064   {"aent", s_mips_ent, 1},
1065   {"bgnb", s_ignore, 0},
1066   {"end", s_mips_end, 0},
1067   {"endb", s_ignore, 0},
1068   {"ent", s_mips_ent, 0},
1069   {"file", s_mips_file, 0},
1070   {"fmask", s_mips_mask, 'F'},
1071   {"frame", s_mips_frame, 0},
1072   {"loc", s_mips_loc, 0},
1073   {"mask", s_mips_mask, 'R'},
1074   {"verstamp", s_ignore, 0},
1075   { NULL, NULL, 0 },
1076 };
1077
1078 extern void pop_insert PARAMS ((const pseudo_typeS *));
1079
1080 void
1081 mips_pop_insert ()
1082 {
1083   pop_insert (mips_pseudo_table);
1084   if (! ECOFF_DEBUGGING)
1085     pop_insert (mips_nonecoff_pseudo_table);
1086 }
1087 \f
1088 /* Symbols labelling the current insn.  */
1089
1090 struct insn_label_list
1091 {
1092   struct insn_label_list *next;
1093   symbolS *label;
1094 };
1095
1096 static struct insn_label_list *insn_labels;
1097 static struct insn_label_list *free_insn_labels;
1098
1099 static void mips_clear_insn_labels PARAMS ((void));
1100
1101 static inline void
1102 mips_clear_insn_labels ()
1103 {
1104   register struct insn_label_list **pl;
1105
1106   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1107     ;
1108   *pl = insn_labels;
1109   insn_labels = NULL;
1110 }
1111 \f
1112 static char *expr_end;
1113
1114 /* Expressions which appear in instructions.  These are set by
1115    mips_ip.  */
1116
1117 static expressionS imm_expr;
1118 static expressionS offset_expr;
1119
1120 /* Relocs associated with imm_expr and offset_expr.  */
1121
1122 static bfd_reloc_code_real_type imm_reloc[3]
1123   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1124 static bfd_reloc_code_real_type offset_reloc[3]
1125   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1126
1127 /* These are set by mips16_ip if an explicit extension is used.  */
1128
1129 static bfd_boolean mips16_small, mips16_ext;
1130
1131 #ifdef OBJ_ELF
1132 /* The pdr segment for per procedure frame/regmask info.  Not used for
1133    ECOFF debugging.  */
1134
1135 static segT pdr_seg;
1136 #endif
1137
1138 /* The default target format to use.  */
1139
1140 const char *
1141 mips_target_format ()
1142 {
1143   switch (OUTPUT_FLAVOR)
1144     {
1145     case bfd_target_aout_flavour:
1146       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1147     case bfd_target_ecoff_flavour:
1148       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1149     case bfd_target_coff_flavour:
1150       return "pe-mips";
1151     case bfd_target_elf_flavour:
1152 #ifdef TE_TMIPS
1153       /* This is traditional mips.  */
1154       return (target_big_endian
1155               ? (HAVE_64BIT_OBJECTS
1156                  ? "elf64-tradbigmips"
1157                  : (HAVE_NEWABI
1158                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1159               : (HAVE_64BIT_OBJECTS
1160                  ? "elf64-tradlittlemips"
1161                  : (HAVE_NEWABI
1162                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1163 #else
1164       return (target_big_endian
1165               ? (HAVE_64BIT_OBJECTS
1166                  ? "elf64-bigmips"
1167                  : (HAVE_NEWABI
1168                     ? "elf32-nbigmips" : "elf32-bigmips"))
1169               : (HAVE_64BIT_OBJECTS
1170                  ? "elf64-littlemips"
1171                  : (HAVE_NEWABI
1172                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1173 #endif
1174     default:
1175       abort ();
1176       return NULL;
1177     }
1178 }
1179
1180 /* This function is called once, at assembler startup time.  It should
1181    set up all the tables, etc. that the MD part of the assembler will need.  */
1182
1183 void
1184 md_begin ()
1185 {
1186   register const char *retval = NULL;
1187   int i = 0;
1188   int broken = 0;
1189
1190   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1191     as_warn (_("Could not set architecture and machine"));
1192
1193   op_hash = hash_new ();
1194
1195   for (i = 0; i < NUMOPCODES;)
1196     {
1197       const char *name = mips_opcodes[i].name;
1198
1199       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1200       if (retval != NULL)
1201         {
1202           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1203                    mips_opcodes[i].name, retval);
1204           /* Probably a memory allocation problem?  Give up now.  */
1205           as_fatal (_("Broken assembler.  No assembly attempted."));
1206         }
1207       do
1208         {
1209           if (mips_opcodes[i].pinfo != INSN_MACRO)
1210             {
1211               if (!validate_mips_insn (&mips_opcodes[i]))
1212                 broken = 1;
1213             }
1214           ++i;
1215         }
1216       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1217     }
1218
1219   mips16_op_hash = hash_new ();
1220
1221   i = 0;
1222   while (i < bfd_mips16_num_opcodes)
1223     {
1224       const char *name = mips16_opcodes[i].name;
1225
1226       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1227       if (retval != NULL)
1228         as_fatal (_("internal: can't hash `%s': %s"),
1229                   mips16_opcodes[i].name, retval);
1230       do
1231         {
1232           if (mips16_opcodes[i].pinfo != INSN_MACRO
1233               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1234                   != mips16_opcodes[i].match))
1235             {
1236               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1237                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1238               broken = 1;
1239             }
1240           ++i;
1241         }
1242       while (i < bfd_mips16_num_opcodes
1243              && strcmp (mips16_opcodes[i].name, name) == 0);
1244     }
1245
1246   if (broken)
1247     as_fatal (_("Broken assembler.  No assembly attempted."));
1248
1249   /* We add all the general register names to the symbol table.  This
1250      helps us detect invalid uses of them.  */
1251   for (i = 0; i < 32; i++)
1252     {
1253       char buf[5];
1254
1255       sprintf (buf, "$%d", i);
1256       symbol_table_insert (symbol_new (buf, reg_section, i,
1257                                        &zero_address_frag));
1258     }
1259   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1260                                    &zero_address_frag));
1261   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1262                                    &zero_address_frag));
1263   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1264                                    &zero_address_frag));
1265   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1266                                    &zero_address_frag));
1267   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1268                                    &zero_address_frag));
1269   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1270                                    &zero_address_frag));
1271   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1272                                    &zero_address_frag));
1273   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1274                                    &zero_address_frag));
1275   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1276                                    &zero_address_frag));
1277
1278   /* If we don't add these register names to the symbol table, they
1279      may end up being added as regular symbols by operand(), and then
1280      make it to the object file as undefined in case they're not
1281      regarded as local symbols.  They're local in o32, since `$' is a
1282      local symbol prefix, but not in n32 or n64.  */
1283   for (i = 0; i < 8; i++)
1284     {
1285       char buf[6];
1286
1287       sprintf (buf, "$fcc%i", i);
1288       symbol_table_insert (symbol_new (buf, reg_section, -1,
1289                                        &zero_address_frag));
1290     }
1291
1292   mips_no_prev_insn (FALSE);
1293
1294   mips_gprmask = 0;
1295   mips_cprmask[0] = 0;
1296   mips_cprmask[1] = 0;
1297   mips_cprmask[2] = 0;
1298   mips_cprmask[3] = 0;
1299
1300   /* set the default alignment for the text section (2**2) */
1301   record_alignment (text_section, 2);
1302
1303   if (USE_GLOBAL_POINTER_OPT)
1304     bfd_set_gp_size (stdoutput, g_switch_value);
1305
1306   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1307     {
1308       /* On a native system, sections must be aligned to 16 byte
1309          boundaries.  When configured for an embedded ELF target, we
1310          don't bother.  */
1311       if (strcmp (TARGET_OS, "elf") != 0)
1312         {
1313           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1314           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1315           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1316         }
1317
1318       /* Create a .reginfo section for register masks and a .mdebug
1319          section for debugging information.  */
1320       {
1321         segT seg;
1322         subsegT subseg;
1323         flagword flags;
1324         segT sec;
1325
1326         seg = now_seg;
1327         subseg = now_subseg;
1328
1329         /* The ABI says this section should be loaded so that the
1330            running program can access it.  However, we don't load it
1331            if we are configured for an embedded target */
1332         flags = SEC_READONLY | SEC_DATA;
1333         if (strcmp (TARGET_OS, "elf") != 0)
1334           flags |= SEC_ALLOC | SEC_LOAD;
1335
1336         if (mips_abi != N64_ABI)
1337           {
1338             sec = subseg_new (".reginfo", (subsegT) 0);
1339
1340             bfd_set_section_flags (stdoutput, sec, flags);
1341             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1342
1343 #ifdef OBJ_ELF
1344             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1345 #endif
1346           }
1347         else
1348           {
1349             /* The 64-bit ABI uses a .MIPS.options section rather than
1350                .reginfo section.  */
1351             sec = subseg_new (".MIPS.options", (subsegT) 0);
1352             bfd_set_section_flags (stdoutput, sec, flags);
1353             bfd_set_section_alignment (stdoutput, sec, 3);
1354
1355 #ifdef OBJ_ELF
1356             /* Set up the option header.  */
1357             {
1358               Elf_Internal_Options opthdr;
1359               char *f;
1360
1361               opthdr.kind = ODK_REGINFO;
1362               opthdr.size = (sizeof (Elf_External_Options)
1363                              + sizeof (Elf64_External_RegInfo));
1364               opthdr.section = 0;
1365               opthdr.info = 0;
1366               f = frag_more (sizeof (Elf_External_Options));
1367               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1368                                              (Elf_External_Options *) f);
1369
1370               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1371             }
1372 #endif
1373           }
1374
1375         if (ECOFF_DEBUGGING)
1376           {
1377             sec = subseg_new (".mdebug", (subsegT) 0);
1378             (void) bfd_set_section_flags (stdoutput, sec,
1379                                           SEC_HAS_CONTENTS | SEC_READONLY);
1380             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1381           }
1382 #ifdef OBJ_ELF
1383         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1384           {
1385             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1386             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1387                                           SEC_READONLY | SEC_RELOC
1388                                           | SEC_DEBUGGING);
1389             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1390           }
1391 #endif
1392
1393         subseg_set (seg, subseg);
1394       }
1395     }
1396
1397   if (! ECOFF_DEBUGGING)
1398     md_obj_begin ();
1399 }
1400
1401 void
1402 md_mips_end ()
1403 {
1404   if (! ECOFF_DEBUGGING)
1405     md_obj_end ();
1406 }
1407
1408 void
1409 md_assemble (str)
1410      char *str;
1411 {
1412   struct mips_cl_insn insn;
1413   bfd_reloc_code_real_type unused_reloc[3]
1414     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1415
1416   imm_expr.X_op = O_absent;
1417   offset_expr.X_op = O_absent;
1418   imm_reloc[0] = BFD_RELOC_UNUSED;
1419   imm_reloc[1] = BFD_RELOC_UNUSED;
1420   imm_reloc[2] = BFD_RELOC_UNUSED;
1421   offset_reloc[0] = BFD_RELOC_UNUSED;
1422   offset_reloc[1] = BFD_RELOC_UNUSED;
1423   offset_reloc[2] = BFD_RELOC_UNUSED;
1424
1425   if (mips_opts.mips16)
1426     mips16_ip (str, &insn);
1427   else
1428     {
1429       mips_ip (str, &insn);
1430       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1431             str, insn.insn_opcode));
1432     }
1433
1434   if (insn_error)
1435     {
1436       as_bad ("%s `%s'", insn_error, str);
1437       return;
1438     }
1439
1440   if (insn.insn_mo->pinfo == INSN_MACRO)
1441     {
1442       if (mips_opts.mips16)
1443         mips16_macro (&insn);
1444       else
1445         macro (&insn);
1446     }
1447   else
1448     {
1449       if (imm_expr.X_op != O_absent)
1450         append_insn (NULL, &insn, &imm_expr, imm_reloc);
1451       else if (offset_expr.X_op != O_absent)
1452         append_insn (NULL, &insn, &offset_expr, offset_reloc);
1453       else
1454         append_insn (NULL, &insn, NULL, unused_reloc);
1455     }
1456 }
1457
1458 /* Return true if the given relocation might need a matching %lo().
1459    Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1460    applied to local symbols.  */
1461
1462 static inline bfd_boolean
1463 reloc_needs_lo_p (reloc)
1464      bfd_reloc_code_real_type reloc;
1465 {
1466   return (reloc == BFD_RELOC_HI16_S
1467           || reloc == BFD_RELOC_MIPS_GOT16);
1468 }
1469
1470 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1471    relocation.  */
1472
1473 static inline bfd_boolean
1474 fixup_has_matching_lo_p (fixp)
1475      fixS *fixp;
1476 {
1477   return (fixp->fx_next != NULL
1478           && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1479           && fixp->fx_addsy == fixp->fx_next->fx_addsy
1480           && fixp->fx_offset == fixp->fx_next->fx_offset);
1481 }
1482
1483 /* See whether instruction IP reads register REG.  CLASS is the type
1484    of register.  */
1485
1486 static int
1487 insn_uses_reg (ip, reg, class)
1488      struct mips_cl_insn *ip;
1489      unsigned int reg;
1490      enum mips_regclass class;
1491 {
1492   if (class == MIPS16_REG)
1493     {
1494       assert (mips_opts.mips16);
1495       reg = mips16_to_32_reg_map[reg];
1496       class = MIPS_GR_REG;
1497     }
1498
1499   /* Don't report on general register ZERO, since it never changes.  */
1500   if (class == MIPS_GR_REG && reg == ZERO)
1501     return 0;
1502
1503   if (class == MIPS_FP_REG)
1504     {
1505       assert (! mips_opts.mips16);
1506       /* If we are called with either $f0 or $f1, we must check $f0.
1507          This is not optimal, because it will introduce an unnecessary
1508          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1509          need to distinguish reading both $f0 and $f1 or just one of
1510          them.  Note that we don't have to check the other way,
1511          because there is no instruction that sets both $f0 and $f1
1512          and requires a delay.  */
1513       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1514           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1515               == (reg &~ (unsigned) 1)))
1516         return 1;
1517       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1518           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1519               == (reg &~ (unsigned) 1)))
1520         return 1;
1521     }
1522   else if (! mips_opts.mips16)
1523     {
1524       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1525           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1526         return 1;
1527       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1528           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1529         return 1;
1530     }
1531   else
1532     {
1533       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1534           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1535                                     & MIPS16OP_MASK_RX)]
1536               == reg))
1537         return 1;
1538       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1539           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1540                                     & MIPS16OP_MASK_RY)]
1541               == reg))
1542         return 1;
1543       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1544           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1545                                     & MIPS16OP_MASK_MOVE32Z)]
1546               == reg))
1547         return 1;
1548       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1549         return 1;
1550       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1551         return 1;
1552       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1553         return 1;
1554       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1555           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1556               & MIPS16OP_MASK_REGR32) == reg)
1557         return 1;
1558     }
1559
1560   return 0;
1561 }
1562
1563 /* This function returns true if modifying a register requires a
1564    delay.  */
1565
1566 static int
1567 reg_needs_delay (reg)
1568      unsigned int reg;
1569 {
1570   unsigned long prev_pinfo;
1571
1572   prev_pinfo = prev_insn.insn_mo->pinfo;
1573   if (! mips_opts.noreorder
1574       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1575       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1576           || (! gpr_interlocks
1577               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1578     {
1579       /* A load from a coprocessor or from memory.  All load
1580          delays delay the use of general register rt for one
1581          instruction on the r3000.  The r6000 and r4000 use
1582          interlocks.  */
1583       /* Itbl support may require additional care here.  */
1584       know (prev_pinfo & INSN_WRITE_GPR_T);
1585       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1586         return 1;
1587     }
1588
1589   return 0;
1590 }
1591
1592 /* Mark instruction labels in mips16 mode.  This permits the linker to
1593    handle them specially, such as generating jalx instructions when
1594    needed.  We also make them odd for the duration of the assembly, in
1595    order to generate the right sort of code.  We will make them even
1596    in the adjust_symtab routine, while leaving them marked.  This is
1597    convenient for the debugger and the disassembler.  The linker knows
1598    to make them odd again.  */
1599
1600 static void
1601 mips16_mark_labels ()
1602 {
1603   if (mips_opts.mips16)
1604     {
1605       struct insn_label_list *l;
1606       valueT val;
1607
1608       for (l = insn_labels; l != NULL; l = l->next)
1609         {
1610 #ifdef OBJ_ELF
1611           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1612             S_SET_OTHER (l->label, STO_MIPS16);
1613 #endif
1614           val = S_GET_VALUE (l->label);
1615           if ((val & 1) == 0)
1616             S_SET_VALUE (l->label, val + 1);
1617         }
1618     }
1619 }
1620
1621 /* Output an instruction.  PLACE is where to put the instruction; if
1622    it is NULL, this uses frag_more to get room.  IP is the instruction
1623    information.  ADDRESS_EXPR is an operand of the instruction to be
1624    used with RELOC_TYPE.  */
1625
1626 static void
1627 append_insn (place, ip, address_expr, reloc_type)
1628      char *place;
1629      struct mips_cl_insn *ip;
1630      expressionS *address_expr;
1631      bfd_reloc_code_real_type *reloc_type;
1632 {
1633   register unsigned long prev_pinfo, pinfo;
1634   char *f;
1635   fixS *fixp[3];
1636   int nops = 0;
1637   bfd_boolean force_new_frag = FALSE;
1638
1639   /* Mark instruction labels in mips16 mode.  */
1640   mips16_mark_labels ();
1641
1642   prev_pinfo = prev_insn.insn_mo->pinfo;
1643   pinfo = ip->insn_mo->pinfo;
1644
1645   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1646     {
1647       int prev_prev_nop;
1648
1649       /* If the previous insn required any delay slots, see if we need
1650          to insert a NOP or two.  There are eight kinds of possible
1651          hazards, of which an instruction can have at most one type.
1652          (1) a load from memory delay
1653          (2) a load from a coprocessor delay
1654          (3) an unconditional branch delay
1655          (4) a conditional branch delay
1656          (5) a move to coprocessor register delay
1657          (6) a load coprocessor register from memory delay
1658          (7) a coprocessor condition code delay
1659          (8) a HI/LO special register delay
1660
1661          There are a lot of optimizations we could do that we don't.
1662          In particular, we do not, in general, reorder instructions.
1663          If you use gcc with optimization, it will reorder
1664          instructions and generally do much more optimization then we
1665          do here; repeating all that work in the assembler would only
1666          benefit hand written assembly code, and does not seem worth
1667          it.  */
1668
1669       /* This is how a NOP is emitted.  */
1670 #define emit_nop()                                      \
1671   (mips_opts.mips16                                     \
1672    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1673    : md_number_to_chars (frag_more (4), 0, 4))
1674
1675       /* The previous insn might require a delay slot, depending upon
1676          the contents of the current insn.  */
1677       if (! mips_opts.mips16
1678           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1679           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1680                && ! cop_interlocks)
1681               || (! gpr_interlocks
1682                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1683         {
1684           /* A load from a coprocessor or from memory.  All load
1685              delays delay the use of general register rt for one
1686              instruction on the r3000.  The r6000 and r4000 use
1687              interlocks.  */
1688           /* Itbl support may require additional care here.  */
1689           know (prev_pinfo & INSN_WRITE_GPR_T);
1690           if (mips_optimize == 0
1691               || insn_uses_reg (ip,
1692                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1693                                  & OP_MASK_RT),
1694                                 MIPS_GR_REG))
1695             ++nops;
1696         }
1697       else if (! mips_opts.mips16
1698                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1699                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1700                     && ! cop_interlocks)
1701                    || (mips_opts.isa == ISA_MIPS1
1702                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1703         {
1704           /* A generic coprocessor delay.  The previous instruction
1705              modified a coprocessor general or control register.  If
1706              it modified a control register, we need to avoid any
1707              coprocessor instruction (this is probably not always
1708              required, but it sometimes is).  If it modified a general
1709              register, we avoid using that register.
1710
1711              On the r6000 and r4000 loading a coprocessor register
1712              from memory is interlocked, and does not require a delay.
1713
1714              This case is not handled very well.  There is no special
1715              knowledge of CP0 handling, and the coprocessors other
1716              than the floating point unit are not distinguished at
1717              all.  */
1718           /* Itbl support may require additional care here. FIXME!
1719              Need to modify this to include knowledge about
1720              user specified delays!  */
1721           if (prev_pinfo & INSN_WRITE_FPR_T)
1722             {
1723               if (mips_optimize == 0
1724                   || insn_uses_reg (ip,
1725                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1726                                      & OP_MASK_FT),
1727                                     MIPS_FP_REG))
1728                 ++nops;
1729             }
1730           else if (prev_pinfo & INSN_WRITE_FPR_S)
1731             {
1732               if (mips_optimize == 0
1733                   || insn_uses_reg (ip,
1734                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1735                                      & OP_MASK_FS),
1736                                     MIPS_FP_REG))
1737                 ++nops;
1738             }
1739           else
1740             {
1741               /* We don't know exactly what the previous instruction
1742                  does.  If the current instruction uses a coprocessor
1743                  register, we must insert a NOP.  If previous
1744                  instruction may set the condition codes, and the
1745                  current instruction uses them, we must insert two
1746                  NOPS.  */
1747               /* Itbl support may require additional care here.  */
1748               if (mips_optimize == 0
1749                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1750                       && (pinfo & INSN_READ_COND_CODE)))
1751                 nops += 2;
1752               else if (pinfo & INSN_COP)
1753                 ++nops;
1754             }
1755         }
1756       else if (! mips_opts.mips16
1757                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1758                && (prev_pinfo & INSN_WRITE_COND_CODE)
1759                && ! cop_interlocks)
1760         {
1761           /* The previous instruction sets the coprocessor condition
1762              codes, but does not require a general coprocessor delay
1763              (this means it is a floating point comparison
1764              instruction).  If this instruction uses the condition
1765              codes, we need to insert a single NOP.  */
1766           /* Itbl support may require additional care here.  */
1767           if (mips_optimize == 0
1768               || (pinfo & INSN_READ_COND_CODE))
1769             ++nops;
1770         }
1771
1772       /* If we're fixing up mfhi/mflo for the r7000 and the
1773          previous insn was an mfhi/mflo and the current insn
1774          reads the register that the mfhi/mflo wrote to, then
1775          insert two nops.  */
1776
1777       else if (mips_7000_hilo_fix
1778                && MF_HILO_INSN (prev_pinfo)
1779                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1780                                       & OP_MASK_RD),
1781                                  MIPS_GR_REG))
1782         {
1783           nops += 2;
1784         }
1785
1786       /* If we're fixing up mfhi/mflo for the r7000 and the
1787          2nd previous insn was an mfhi/mflo and the current insn
1788          reads the register that the mfhi/mflo wrote to, then
1789          insert one nop.  */
1790
1791       else if (mips_7000_hilo_fix
1792                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1793                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1794                                        & OP_MASK_RD),
1795                                     MIPS_GR_REG))
1796
1797         {
1798           ++nops;
1799         }
1800
1801       else if (prev_pinfo & INSN_READ_LO)
1802         {
1803           /* The previous instruction reads the LO register; if the
1804              current instruction writes to the LO register, we must
1805              insert two NOPS.  Some newer processors have interlocks.
1806              Also the tx39's multiply instructions can be exectuted
1807              immediatly after a read from HI/LO (without the delay),
1808              though the tx39's divide insns still do require the
1809              delay.  */
1810           if (! (hilo_interlocks
1811                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1812               && (mips_optimize == 0
1813                   || (pinfo & INSN_WRITE_LO)))
1814             nops += 2;
1815           /* Most mips16 branch insns don't have a delay slot.
1816              If a read from LO is immediately followed by a branch
1817              to a write to LO we have a read followed by a write
1818              less than 2 insns away.  We assume the target of
1819              a branch might be a write to LO, and insert a nop
1820              between a read and an immediately following branch.  */
1821           else if (mips_opts.mips16
1822                    && (mips_optimize == 0
1823                        || (pinfo & MIPS16_INSN_BRANCH)))
1824             ++nops;
1825         }
1826       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1827         {
1828           /* The previous instruction reads the HI register; if the
1829              current instruction writes to the HI register, we must
1830              insert a NOP.  Some newer processors have interlocks.
1831              Also the note tx39's multiply above.  */
1832           if (! (hilo_interlocks
1833                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1834               && (mips_optimize == 0
1835                   || (pinfo & INSN_WRITE_HI)))
1836             nops += 2;
1837           /* Most mips16 branch insns don't have a delay slot.
1838              If a read from HI is immediately followed by a branch
1839              to a write to HI we have a read followed by a write
1840              less than 2 insns away.  We assume the target of
1841              a branch might be a write to HI, and insert a nop
1842              between a read and an immediately following branch.  */
1843           else if (mips_opts.mips16
1844                    && (mips_optimize == 0
1845                        || (pinfo & MIPS16_INSN_BRANCH)))
1846             ++nops;
1847         }
1848
1849       /* If the previous instruction was in a noreorder section, then
1850          we don't want to insert the nop after all.  */
1851       /* Itbl support may require additional care here.  */
1852       if (prev_insn_unreordered)
1853         nops = 0;
1854
1855       /* There are two cases which require two intervening
1856          instructions: 1) setting the condition codes using a move to
1857          coprocessor instruction which requires a general coprocessor
1858          delay and then reading the condition codes 2) reading the HI
1859          or LO register and then writing to it (except on processors
1860          which have interlocks).  If we are not already emitting a NOP
1861          instruction, we must check for these cases compared to the
1862          instruction previous to the previous instruction.  */
1863       if ((! mips_opts.mips16
1864            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1865            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1866            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1867            && (pinfo & INSN_READ_COND_CODE)
1868            && ! cop_interlocks)
1869           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1870               && (pinfo & INSN_WRITE_LO)
1871               && ! (hilo_interlocks
1872                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1873           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1874               && (pinfo & INSN_WRITE_HI)
1875               && ! (hilo_interlocks
1876                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1877         prev_prev_nop = 1;
1878       else
1879         prev_prev_nop = 0;
1880
1881       if (prev_prev_insn_unreordered)
1882         prev_prev_nop = 0;
1883
1884       if (prev_prev_nop && nops == 0)
1885         ++nops;
1886
1887       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1888         {
1889           /* We're out of bits in pinfo, so we must resort to string
1890              ops here.  Shortcuts are selected based on opcodes being
1891              limited to the VR4122 instruction set.  */
1892           int min_nops = 0;
1893           const char *pn = prev_insn.insn_mo->name;
1894           const char *tn = ip->insn_mo->name;
1895           if (strncmp(pn, "macc", 4) == 0
1896               || strncmp(pn, "dmacc", 5) == 0)
1897             {
1898               /* Errata 21 - [D]DIV[U] after [D]MACC */
1899               if (strstr (tn, "div"))
1900                 {
1901                   min_nops = 1;
1902                 }
1903
1904               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1905               if (pn[0] == 'd' /* dmacc */
1906                   && (strncmp(tn, "dmult", 5) == 0
1907                       || strncmp(tn, "dmacc", 5) == 0))
1908                 {
1909                   min_nops = 1;
1910                 }
1911
1912               /* Errata 24 - MT{LO,HI} after [D]MACC */
1913               if (strcmp (tn, "mtlo") == 0
1914                   || strcmp (tn, "mthi") == 0)
1915                 {
1916                   min_nops = 1;
1917                 }
1918
1919             }
1920           else if (strncmp(pn, "dmult", 5) == 0
1921                    && (strncmp(tn, "dmult", 5) == 0
1922                        || strncmp(tn, "dmacc", 5) == 0))
1923             {
1924               /* Here is the rest of errata 23.  */
1925               min_nops = 1;
1926             }
1927           if (nops < min_nops)
1928             nops = min_nops;
1929         }
1930
1931       /* If we are being given a nop instruction, don't bother with
1932          one of the nops we would otherwise output.  This will only
1933          happen when a nop instruction is used with mips_optimize set
1934          to 0.  */
1935       if (nops > 0
1936           && ! mips_opts.noreorder
1937           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1938         --nops;
1939
1940       /* Now emit the right number of NOP instructions.  */
1941       if (nops > 0 && ! mips_opts.noreorder)
1942         {
1943           fragS *old_frag;
1944           unsigned long old_frag_offset;
1945           int i;
1946           struct insn_label_list *l;
1947
1948           old_frag = frag_now;
1949           old_frag_offset = frag_now_fix ();
1950
1951           for (i = 0; i < nops; i++)
1952             emit_nop ();
1953
1954           if (listing)
1955             {
1956               listing_prev_line ();
1957               /* We may be at the start of a variant frag.  In case we
1958                  are, make sure there is enough space for the frag
1959                  after the frags created by listing_prev_line.  The
1960                  argument to frag_grow here must be at least as large
1961                  as the argument to all other calls to frag_grow in
1962                  this file.  We don't have to worry about being in the
1963                  middle of a variant frag, because the variants insert
1964                  all needed nop instructions themselves.  */
1965               frag_grow (40);
1966             }
1967
1968           for (l = insn_labels; l != NULL; l = l->next)
1969             {
1970               valueT val;
1971
1972               assert (S_GET_SEGMENT (l->label) == now_seg);
1973               symbol_set_frag (l->label, frag_now);
1974               val = (valueT) frag_now_fix ();
1975               /* mips16 text labels are stored as odd.  */
1976               if (mips_opts.mips16)
1977                 ++val;
1978               S_SET_VALUE (l->label, val);
1979             }
1980
1981 #ifndef NO_ECOFF_DEBUGGING
1982           if (ECOFF_DEBUGGING)
1983             ecoff_fix_loc (old_frag, old_frag_offset);
1984 #endif
1985         }
1986       else if (prev_nop_frag != NULL)
1987         {
1988           /* We have a frag holding nops we may be able to remove.  If
1989              we don't need any nops, we can decrease the size of
1990              prev_nop_frag by the size of one instruction.  If we do
1991              need some nops, we count them in prev_nops_required.  */
1992           if (prev_nop_frag_since == 0)
1993             {
1994               if (nops == 0)
1995                 {
1996                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1997                   --prev_nop_frag_holds;
1998                 }
1999               else
2000                 prev_nop_frag_required += nops;
2001             }
2002           else
2003             {
2004               if (prev_prev_nop == 0)
2005                 {
2006                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2007                   --prev_nop_frag_holds;
2008                 }
2009               else
2010                 ++prev_nop_frag_required;
2011             }
2012
2013           if (prev_nop_frag_holds <= prev_nop_frag_required)
2014             prev_nop_frag = NULL;
2015
2016           ++prev_nop_frag_since;
2017
2018           /* Sanity check: by the time we reach the second instruction
2019              after prev_nop_frag, we should have used up all the nops
2020              one way or another.  */
2021           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2022         }
2023     }
2024
2025   if (place == NULL
2026       && address_expr
2027       && *reloc_type == BFD_RELOC_16_PCREL_S2
2028       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2029           || pinfo & INSN_COND_BRANCH_LIKELY)
2030       && mips_relax_branch
2031       /* Don't try branch relaxation within .set nomacro, or within
2032          .set noat if we use $at for PIC computations.  If it turns
2033          out that the branch was out-of-range, we'll get an error.  */
2034       && !mips_opts.warn_about_macros
2035       && !(mips_opts.noat && mips_pic != NO_PIC)
2036       && !mips_opts.mips16)
2037     {
2038       f = frag_var (rs_machine_dependent,
2039                     relaxed_branch_length
2040                     (NULL, NULL,
2041                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2042                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2043                     RELAX_BRANCH_ENCODE
2044                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
2045                      pinfo & INSN_COND_BRANCH_LIKELY,
2046                      pinfo & INSN_WRITE_GPR_31,
2047                      0),
2048                     address_expr->X_add_symbol,
2049                     address_expr->X_add_number,
2050                     0);
2051       *reloc_type = BFD_RELOC_UNUSED;
2052     }
2053   else if (*reloc_type > BFD_RELOC_UNUSED)
2054     {
2055       /* We need to set up a variant frag.  */
2056       assert (mips_opts.mips16 && address_expr != NULL);
2057       f = frag_var (rs_machine_dependent, 4, 0,
2058                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2059                                          mips16_small, mips16_ext,
2060                                          (prev_pinfo
2061                                           & INSN_UNCOND_BRANCH_DELAY),
2062                                          (*prev_insn_reloc_type
2063                                           == BFD_RELOC_MIPS16_JMP)),
2064                     make_expr_symbol (address_expr), 0, NULL);
2065     }
2066   else if (place != NULL)
2067     f = place;
2068   else if (mips_opts.mips16
2069            && ! ip->use_extend
2070            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2071     {
2072       /* Make sure there is enough room to swap this instruction with
2073          a following jump instruction.  */
2074       frag_grow (6);
2075       f = frag_more (2);
2076     }
2077   else
2078     {
2079       if (mips_opts.mips16
2080           && mips_opts.noreorder
2081           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2082         as_warn (_("extended instruction in delay slot"));
2083
2084       f = frag_more (4);
2085     }
2086
2087   fixp[0] = fixp[1] = fixp[2] = NULL;
2088   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2089     {
2090       if (address_expr->X_op == O_constant)
2091         {
2092           valueT tmp;
2093
2094           switch (*reloc_type)
2095             {
2096             case BFD_RELOC_32:
2097               ip->insn_opcode |= address_expr->X_add_number;
2098               break;
2099
2100             case BFD_RELOC_MIPS_HIGHEST:
2101               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2102               tmp >>= 16;
2103               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2104               break;
2105
2106             case BFD_RELOC_MIPS_HIGHER:
2107               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2108               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2109               break;
2110
2111             case BFD_RELOC_HI16_S:
2112               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2113                                   >> 16) & 0xffff;
2114               break;
2115
2116             case BFD_RELOC_HI16:
2117               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2118               break;
2119
2120             case BFD_RELOC_LO16:
2121             case BFD_RELOC_MIPS_GOT_DISP:
2122               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2123               break;
2124
2125             case BFD_RELOC_MIPS_JMP:
2126               if ((address_expr->X_add_number & 3) != 0)
2127                 as_bad (_("jump to misaligned address (0x%lx)"),
2128                         (unsigned long) address_expr->X_add_number);
2129               if (address_expr->X_add_number & ~0xfffffff)
2130                 as_bad (_("jump address range overflow (0x%lx)"),
2131                         (unsigned long) address_expr->X_add_number);
2132               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2133               break;
2134
2135             case BFD_RELOC_MIPS16_JMP:
2136               if ((address_expr->X_add_number & 3) != 0)
2137                 as_bad (_("jump to misaligned address (0x%lx)"),
2138                         (unsigned long) address_expr->X_add_number);
2139               if (address_expr->X_add_number & ~0xfffffff)
2140                 as_bad (_("jump address range overflow (0x%lx)"),
2141                         (unsigned long) address_expr->X_add_number);
2142               ip->insn_opcode |=
2143                 (((address_expr->X_add_number & 0x7c0000) << 3)
2144                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2145                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2146               break;
2147
2148             case BFD_RELOC_16_PCREL_S2:
2149               goto need_reloc;
2150
2151             default:
2152               internalError ();
2153             }
2154         }
2155       else
2156         {
2157         need_reloc:
2158           /* Don't generate a reloc if we are writing into a variant frag.  */
2159           if (place == NULL)
2160             {
2161               reloc_howto_type *howto;
2162               int i;
2163
2164               /* In a compound relocation, it is the final (outermost)
2165                  operator that determines the relocated field.  */
2166               for (i = 1; i < 3; i++)
2167                 if (reloc_type[i] == BFD_RELOC_UNUSED)
2168                   break;
2169
2170               howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2171               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2172                                      bfd_get_reloc_size(howto),
2173                                      address_expr,
2174                                      reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2175                                      reloc_type[0]);
2176
2177               /* These relocations can have an addend that won't fit in
2178                  4 octets for 64bit assembly.  */
2179               if (HAVE_64BIT_GPRS
2180                   && ! howto->partial_inplace
2181                   && (reloc_type[0] == BFD_RELOC_16
2182                       || reloc_type[0] == BFD_RELOC_32
2183                       || reloc_type[0] == BFD_RELOC_MIPS_JMP
2184                       || reloc_type[0] == BFD_RELOC_HI16_S
2185                       || reloc_type[0] == BFD_RELOC_LO16
2186                       || reloc_type[0] == BFD_RELOC_GPREL16
2187                       || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2188                       || reloc_type[0] == BFD_RELOC_GPREL32
2189                       || reloc_type[0] == BFD_RELOC_64
2190                       || reloc_type[0] == BFD_RELOC_CTOR
2191                       || reloc_type[0] == BFD_RELOC_MIPS_SUB
2192                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2193                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2194                       || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2195                       || reloc_type[0] == BFD_RELOC_MIPS_REL16
2196                       || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2197                 fixp[0]->fx_no_overflow = 1;
2198
2199               if (reloc_needs_lo_p (*reloc_type))
2200                 {
2201                   struct mips_hi_fixup *hi_fixup;
2202
2203                   /* Reuse the last entry if it already has a matching %lo.  */
2204                   hi_fixup = mips_hi_fixup_list;
2205                   if (hi_fixup == 0
2206                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2207                     {
2208                       hi_fixup = ((struct mips_hi_fixup *)
2209                                   xmalloc (sizeof (struct mips_hi_fixup)));
2210                       hi_fixup->next = mips_hi_fixup_list;
2211                       mips_hi_fixup_list = hi_fixup;
2212                     }
2213                   hi_fixup->fixp = fixp[0];
2214                   hi_fixup->seg = now_seg;
2215                 }
2216
2217               /* Add fixups for the second and third relocations, if given.
2218                  Note that the ABI allows the second relocation to be
2219                  against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
2220                  moment we only use RSS_UNDEF, but we could add support
2221                  for the others if it ever becomes necessary.  */
2222               for (i = 1; i < 3; i++)
2223                 if (reloc_type[i] != BFD_RELOC_UNUSED)
2224                   {
2225                     address_expr->X_op = O_absent;
2226                     address_expr->X_add_symbol = 0;
2227                     address_expr->X_add_number = 0;
2228
2229                     fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2230                                            fixp[0]->fx_size, address_expr,
2231                                            FALSE, reloc_type[i]);
2232                   }
2233             }
2234         }
2235     }
2236
2237   if (! mips_opts.mips16)
2238     {
2239       md_number_to_chars (f, ip->insn_opcode, 4);
2240 #ifdef OBJ_ELF
2241       dwarf2_emit_insn (4);
2242 #endif
2243     }
2244   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2245     {
2246       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2247       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2248 #ifdef OBJ_ELF
2249       dwarf2_emit_insn (4);
2250 #endif
2251     }
2252   else
2253     {
2254       if (ip->use_extend)
2255         {
2256           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2257           f += 2;
2258         }
2259       md_number_to_chars (f, ip->insn_opcode, 2);
2260 #ifdef OBJ_ELF
2261       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2262 #endif
2263     }
2264
2265   /* Update the register mask information.  */
2266   if (! mips_opts.mips16)
2267     {
2268       if (pinfo & INSN_WRITE_GPR_D)
2269         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2270       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2271         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2272       if (pinfo & INSN_READ_GPR_S)
2273         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2274       if (pinfo & INSN_WRITE_GPR_31)
2275         mips_gprmask |= 1 << RA;
2276       if (pinfo & INSN_WRITE_FPR_D)
2277         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2278       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2279         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2280       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2281         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2282       if ((pinfo & INSN_READ_FPR_R) != 0)
2283         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2284       if (pinfo & INSN_COP)
2285         {
2286           /* We don't keep enough information to sort these cases out.
2287              The itbl support does keep this information however, although
2288              we currently don't support itbl fprmats as part of the cop
2289              instruction.  May want to add this support in the future.  */
2290         }
2291       /* Never set the bit for $0, which is always zero.  */
2292       mips_gprmask &= ~1 << 0;
2293     }
2294   else
2295     {
2296       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2297         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2298                               & MIPS16OP_MASK_RX);
2299       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2300         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2301                               & MIPS16OP_MASK_RY);
2302       if (pinfo & MIPS16_INSN_WRITE_Z)
2303         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2304                               & MIPS16OP_MASK_RZ);
2305       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2306         mips_gprmask |= 1 << TREG;
2307       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2308         mips_gprmask |= 1 << SP;
2309       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2310         mips_gprmask |= 1 << RA;
2311       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2312         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2313       if (pinfo & MIPS16_INSN_READ_Z)
2314         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2315                               & MIPS16OP_MASK_MOVE32Z);
2316       if (pinfo & MIPS16_INSN_READ_GPR_X)
2317         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2318                               & MIPS16OP_MASK_REGR32);
2319     }
2320
2321   if (place == NULL && ! mips_opts.noreorder)
2322     {
2323       /* Filling the branch delay slot is more complex.  We try to
2324          switch the branch with the previous instruction, which we can
2325          do if the previous instruction does not set up a condition
2326          that the branch tests and if the branch is not itself the
2327          target of any branch.  */
2328       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2329           || (pinfo & INSN_COND_BRANCH_DELAY))
2330         {
2331           if (mips_optimize < 2
2332               /* If we have seen .set volatile or .set nomove, don't
2333                  optimize.  */
2334               || mips_opts.nomove != 0
2335               /* If we had to emit any NOP instructions, then we
2336                  already know we can not swap.  */
2337               || nops != 0
2338               /* If we don't even know the previous insn, we can not
2339                  swap.  */
2340               || ! prev_insn_valid
2341               /* If the previous insn is already in a branch delay
2342                  slot, then we can not swap.  */
2343               || prev_insn_is_delay_slot
2344               /* If the previous previous insn was in a .set
2345                  noreorder, we can't swap.  Actually, the MIPS
2346                  assembler will swap in this situation.  However, gcc
2347                  configured -with-gnu-as will generate code like
2348                    .set noreorder
2349                    lw   $4,XXX
2350                    .set reorder
2351                    INSN
2352                    bne  $4,$0,foo
2353                  in which we can not swap the bne and INSN.  If gcc is
2354                  not configured -with-gnu-as, it does not output the
2355                  .set pseudo-ops.  We don't have to check
2356                  prev_insn_unreordered, because prev_insn_valid will
2357                  be 0 in that case.  We don't want to use
2358                  prev_prev_insn_valid, because we do want to be able
2359                  to swap at the start of a function.  */
2360               || prev_prev_insn_unreordered
2361               /* If the branch is itself the target of a branch, we
2362                  can not swap.  We cheat on this; all we check for is
2363                  whether there is a label on this instruction.  If
2364                  there are any branches to anything other than a
2365                  label, users must use .set noreorder.  */
2366               || insn_labels != NULL
2367               /* If the previous instruction is in a variant frag, we
2368                  can not do the swap.  This does not apply to the
2369                  mips16, which uses variant frags for different
2370                  purposes.  */
2371               || (! mips_opts.mips16
2372                   && prev_insn_frag->fr_type == rs_machine_dependent)
2373               /* If the branch reads the condition codes, we don't
2374                  even try to swap, because in the sequence
2375                    ctc1 $X,$31
2376                    INSN
2377                    INSN
2378                    bc1t LABEL
2379                  we can not swap, and I don't feel like handling that
2380                  case.  */
2381               || (! mips_opts.mips16
2382                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2383                   && (pinfo & INSN_READ_COND_CODE))
2384               /* We can not swap with an instruction that requires a
2385                  delay slot, becase the target of the branch might
2386                  interfere with that instruction.  */
2387               || (! mips_opts.mips16
2388                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2389                   && (prev_pinfo
2390               /* Itbl support may require additional care here.  */
2391                       & (INSN_LOAD_COPROC_DELAY
2392                          | INSN_COPROC_MOVE_DELAY
2393                          | INSN_WRITE_COND_CODE)))
2394               || (! (hilo_interlocks
2395                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2396                   && (prev_pinfo
2397                       & (INSN_READ_LO
2398                          | INSN_READ_HI)))
2399               || (! mips_opts.mips16
2400                   && ! gpr_interlocks
2401                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2402               || (! mips_opts.mips16
2403                   && mips_opts.isa == ISA_MIPS1
2404                   /* Itbl support may require additional care here.  */
2405                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2406               /* We can not swap with a branch instruction.  */
2407               || (prev_pinfo
2408                   & (INSN_UNCOND_BRANCH_DELAY
2409                      | INSN_COND_BRANCH_DELAY
2410                      | INSN_COND_BRANCH_LIKELY))
2411               /* We do not swap with a trap instruction, since it
2412                  complicates trap handlers to have the trap
2413                  instruction be in a delay slot.  */
2414               || (prev_pinfo & INSN_TRAP)
2415               /* If the branch reads a register that the previous
2416                  instruction sets, we can not swap.  */
2417               || (! mips_opts.mips16
2418                   && (prev_pinfo & INSN_WRITE_GPR_T)
2419                   && insn_uses_reg (ip,
2420                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2421                                      & OP_MASK_RT),
2422                                     MIPS_GR_REG))
2423               || (! mips_opts.mips16
2424                   && (prev_pinfo & INSN_WRITE_GPR_D)
2425                   && insn_uses_reg (ip,
2426                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2427                                      & OP_MASK_RD),
2428                                     MIPS_GR_REG))
2429               || (mips_opts.mips16
2430                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2431                        && insn_uses_reg (ip,
2432                                          ((prev_insn.insn_opcode
2433                                            >> MIPS16OP_SH_RX)
2434                                           & MIPS16OP_MASK_RX),
2435                                          MIPS16_REG))
2436                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2437                           && insn_uses_reg (ip,
2438                                             ((prev_insn.insn_opcode
2439                                               >> MIPS16OP_SH_RY)
2440                                              & MIPS16OP_MASK_RY),
2441                                             MIPS16_REG))
2442                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2443                           && insn_uses_reg (ip,
2444                                             ((prev_insn.insn_opcode
2445                                               >> MIPS16OP_SH_RZ)
2446                                              & MIPS16OP_MASK_RZ),
2447                                             MIPS16_REG))
2448                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2449                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2450                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2451                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2452                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2453                           && insn_uses_reg (ip,
2454                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2455                                                                      insn_opcode),
2456                                             MIPS_GR_REG))))
2457               /* If the branch writes a register that the previous
2458                  instruction sets, we can not swap (we know that
2459                  branches write only to RD or to $31).  */
2460               || (! mips_opts.mips16
2461                   && (prev_pinfo & INSN_WRITE_GPR_T)
2462                   && (((pinfo & INSN_WRITE_GPR_D)
2463                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2464                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2465                       || ((pinfo & INSN_WRITE_GPR_31)
2466                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2467                                & OP_MASK_RT)
2468                               == RA))))
2469               || (! mips_opts.mips16
2470                   && (prev_pinfo & INSN_WRITE_GPR_D)
2471                   && (((pinfo & INSN_WRITE_GPR_D)
2472                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2473                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2474                       || ((pinfo & INSN_WRITE_GPR_31)
2475                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2476                                & OP_MASK_RD)
2477                               == RA))))
2478               || (mips_opts.mips16
2479                   && (pinfo & MIPS16_INSN_WRITE_31)
2480                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2481                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2482                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2483                               == RA))))
2484               /* If the branch writes a register that the previous
2485                  instruction reads, we can not swap (we know that
2486                  branches only write to RD or to $31).  */
2487               || (! mips_opts.mips16
2488                   && (pinfo & INSN_WRITE_GPR_D)
2489                   && insn_uses_reg (&prev_insn,
2490                                     ((ip->insn_opcode >> OP_SH_RD)
2491                                      & OP_MASK_RD),
2492                                     MIPS_GR_REG))
2493               || (! mips_opts.mips16
2494                   && (pinfo & INSN_WRITE_GPR_31)
2495                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2496               || (mips_opts.mips16
2497                   && (pinfo & MIPS16_INSN_WRITE_31)
2498                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2499               /* If we are generating embedded PIC code, the branch
2500                  might be expanded into a sequence which uses $at, so
2501                  we can't swap with an instruction which reads it.  */
2502               || (mips_pic == EMBEDDED_PIC
2503                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2504               /* If the previous previous instruction has a load
2505                  delay, and sets a register that the branch reads, we
2506                  can not swap.  */
2507               || (! mips_opts.mips16
2508                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2509               /* Itbl support may require additional care here.  */
2510                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2511                       || (! gpr_interlocks
2512                           && (prev_prev_insn.insn_mo->pinfo
2513                               & INSN_LOAD_MEMORY_DELAY)))
2514                   && insn_uses_reg (ip,
2515                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2516                                      & OP_MASK_RT),
2517                                     MIPS_GR_REG))
2518               /* If one instruction sets a condition code and the
2519                  other one uses a condition code, we can not swap.  */
2520               || ((pinfo & INSN_READ_COND_CODE)
2521                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2522               || ((pinfo & INSN_WRITE_COND_CODE)
2523                   && (prev_pinfo & INSN_READ_COND_CODE))
2524               /* If the previous instruction uses the PC, we can not
2525                  swap.  */
2526               || (mips_opts.mips16
2527                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2528               /* If the previous instruction was extended, we can not
2529                  swap.  */
2530               || (mips_opts.mips16 && prev_insn_extended)
2531               /* If the previous instruction had a fixup in mips16
2532                  mode, we can not swap.  This normally means that the
2533                  previous instruction was a 4 byte branch anyhow.  */
2534               || (mips_opts.mips16 && prev_insn_fixp[0])
2535               /* If the previous instruction is a sync, sync.l, or
2536                  sync.p, we can not swap.  */
2537               || (prev_pinfo & INSN_SYNC))
2538             {
2539               /* We could do even better for unconditional branches to
2540                  portions of this object file; we could pick up the
2541                  instruction at the destination, put it in the delay
2542                  slot, and bump the destination address.  */
2543               emit_nop ();
2544               /* Update the previous insn information.  */
2545               prev_prev_insn = *ip;
2546               prev_insn.insn_mo = &dummy_opcode;
2547             }
2548           else
2549             {
2550               /* It looks like we can actually do the swap.  */
2551               if (! mips_opts.mips16)
2552                 {
2553                   char *prev_f;
2554                   char temp[4];
2555
2556                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2557                   memcpy (temp, prev_f, 4);
2558                   memcpy (prev_f, f, 4);
2559                   memcpy (f, temp, 4);
2560                   if (prev_insn_fixp[0])
2561                     {
2562                       prev_insn_fixp[0]->fx_frag = frag_now;
2563                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2564                     }
2565                   if (prev_insn_fixp[1])
2566                     {
2567                       prev_insn_fixp[1]->fx_frag = frag_now;
2568                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2569                     }
2570                   if (prev_insn_fixp[2])
2571                     {
2572                       prev_insn_fixp[2]->fx_frag = frag_now;
2573                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2574                     }
2575                   if (prev_insn_fixp[0] && HAVE_NEWABI
2576                       && prev_insn_frag != frag_now
2577                       && (prev_insn_fixp[0]->fx_r_type
2578                           == BFD_RELOC_MIPS_GOT_DISP
2579                           || (prev_insn_fixp[0]->fx_r_type
2580                               == BFD_RELOC_MIPS_CALL16)))
2581                     {
2582                       /* To avoid confusion in tc_gen_reloc, we must
2583                          ensure that this does not become a variant
2584                          frag.  */
2585                       force_new_frag = TRUE;
2586                     }
2587                   if (fixp[0])
2588                     {
2589                       fixp[0]->fx_frag = prev_insn_frag;
2590                       fixp[0]->fx_where = prev_insn_where;
2591                     }
2592                   if (fixp[1])
2593                     {
2594                       fixp[1]->fx_frag = prev_insn_frag;
2595                       fixp[1]->fx_where = prev_insn_where;
2596                     }
2597                   if (fixp[2])
2598                     {
2599                       fixp[2]->fx_frag = prev_insn_frag;
2600                       fixp[2]->fx_where = prev_insn_where;
2601                     }
2602                 }
2603               else
2604                 {
2605                   char *prev_f;
2606                   char temp[2];
2607
2608                   assert (prev_insn_fixp[0] == NULL);
2609                   assert (prev_insn_fixp[1] == NULL);
2610                   assert (prev_insn_fixp[2] == NULL);
2611                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2612                   memcpy (temp, prev_f, 2);
2613                   memcpy (prev_f, f, 2);
2614                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2615                     {
2616                       assert (*reloc_type == BFD_RELOC_UNUSED);
2617                       memcpy (f, temp, 2);
2618                     }
2619                   else
2620                     {
2621                       memcpy (f, f + 2, 2);
2622                       memcpy (f + 2, temp, 2);
2623                     }
2624                   if (fixp[0])
2625                     {
2626                       fixp[0]->fx_frag = prev_insn_frag;
2627                       fixp[0]->fx_where = prev_insn_where;
2628                     }
2629                   if (fixp[1])
2630                     {
2631                       fixp[1]->fx_frag = prev_insn_frag;
2632                       fixp[1]->fx_where = prev_insn_where;
2633                     }
2634                   if (fixp[2])
2635                     {
2636                       fixp[2]->fx_frag = prev_insn_frag;
2637                       fixp[2]->fx_where = prev_insn_where;
2638                     }
2639                 }
2640
2641               /* Update the previous insn information; leave prev_insn
2642                  unchanged.  */
2643               prev_prev_insn = *ip;
2644             }
2645           prev_insn_is_delay_slot = 1;
2646
2647           /* If that was an unconditional branch, forget the previous
2648              insn information.  */
2649           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2650             {
2651               prev_prev_insn.insn_mo = &dummy_opcode;
2652               prev_insn.insn_mo = &dummy_opcode;
2653             }
2654
2655           prev_insn_fixp[0] = NULL;
2656           prev_insn_fixp[1] = NULL;
2657           prev_insn_fixp[2] = NULL;
2658           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2659           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2660           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2661           prev_insn_extended = 0;
2662         }
2663       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2664         {
2665           /* We don't yet optimize a branch likely.  What we should do
2666              is look at the target, copy the instruction found there
2667              into the delay slot, and increment the branch to jump to
2668              the next instruction.  */
2669           emit_nop ();
2670           /* Update the previous insn information.  */
2671           prev_prev_insn = *ip;
2672           prev_insn.insn_mo = &dummy_opcode;
2673           prev_insn_fixp[0] = NULL;
2674           prev_insn_fixp[1] = NULL;
2675           prev_insn_fixp[2] = NULL;
2676           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2677           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2678           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2679           prev_insn_extended = 0;
2680         }
2681       else
2682         {
2683           /* Update the previous insn information.  */
2684           if (nops > 0)
2685             prev_prev_insn.insn_mo = &dummy_opcode;
2686           else
2687             prev_prev_insn = prev_insn;
2688           prev_insn = *ip;
2689
2690           /* Any time we see a branch, we always fill the delay slot
2691              immediately; since this insn is not a branch, we know it
2692              is not in a delay slot.  */
2693           prev_insn_is_delay_slot = 0;
2694
2695           prev_insn_fixp[0] = fixp[0];
2696           prev_insn_fixp[1] = fixp[1];
2697           prev_insn_fixp[2] = fixp[2];
2698           prev_insn_reloc_type[0] = reloc_type[0];
2699           prev_insn_reloc_type[1] = reloc_type[1];
2700           prev_insn_reloc_type[2] = reloc_type[2];
2701           if (mips_opts.mips16)
2702             prev_insn_extended = (ip->use_extend
2703                                   || *reloc_type > BFD_RELOC_UNUSED);
2704         }
2705
2706       prev_prev_insn_unreordered = prev_insn_unreordered;
2707       prev_insn_unreordered = 0;
2708       prev_insn_frag = frag_now;
2709       prev_insn_where = f - frag_now->fr_literal;
2710       prev_insn_valid = 1;
2711     }
2712   else if (place == NULL)
2713     {
2714       /* We need to record a bit of information even when we are not
2715          reordering, in order to determine the base address for mips16
2716          PC relative relocs.  */
2717       prev_prev_insn = prev_insn;
2718       prev_insn = *ip;
2719       prev_insn_reloc_type[0] = reloc_type[0];
2720       prev_insn_reloc_type[1] = reloc_type[1];
2721       prev_insn_reloc_type[2] = reloc_type[2];
2722       prev_prev_insn_unreordered = prev_insn_unreordered;
2723       prev_insn_unreordered = 1;
2724     }
2725
2726   /* We just output an insn, so the next one doesn't have a label.  */
2727   mips_clear_insn_labels ();
2728
2729   /* We must ensure that the frag to which an instruction that was
2730      moved from a non-variant frag doesn't become a variant frag,
2731      otherwise tc_gen_reloc may get confused.  */
2732   if (force_new_frag)
2733     {
2734       frag_wane (frag_now);
2735       frag_new (0);
2736     }
2737 }
2738
2739 /* This function forgets that there was any previous instruction or
2740    label.  If PRESERVE is non-zero, it remembers enough information to
2741    know whether nops are needed before a noreorder section.  */
2742
2743 static void
2744 mips_no_prev_insn (preserve)
2745      int preserve;
2746 {
2747   if (! preserve)
2748     {
2749       prev_insn.insn_mo = &dummy_opcode;
2750       prev_prev_insn.insn_mo = &dummy_opcode;
2751       prev_nop_frag = NULL;
2752       prev_nop_frag_holds = 0;
2753       prev_nop_frag_required = 0;
2754       prev_nop_frag_since = 0;
2755     }
2756   prev_insn_valid = 0;
2757   prev_insn_is_delay_slot = 0;
2758   prev_insn_unreordered = 0;
2759   prev_insn_extended = 0;
2760   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2761   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2762   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2763   prev_prev_insn_unreordered = 0;
2764   mips_clear_insn_labels ();
2765 }
2766
2767 /* This function must be called whenever we turn on noreorder or emit
2768    something other than instructions.  It inserts any NOPS which might
2769    be needed by the previous instruction, and clears the information
2770    kept for the previous instructions.  The INSNS parameter is true if
2771    instructions are to follow.  */
2772
2773 static void
2774 mips_emit_delays (insns)
2775      bfd_boolean insns;
2776 {
2777   if (! mips_opts.noreorder)
2778     {
2779       int nops;
2780
2781       nops = 0;
2782       if ((! mips_opts.mips16
2783            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2784            && (! cop_interlocks
2785                && (prev_insn.insn_mo->pinfo
2786                    & (INSN_LOAD_COPROC_DELAY
2787                       | INSN_COPROC_MOVE_DELAY
2788                       | INSN_WRITE_COND_CODE))))
2789           || (! hilo_interlocks
2790               && (prev_insn.insn_mo->pinfo
2791                   & (INSN_READ_LO
2792                      | INSN_READ_HI)))
2793           || (! mips_opts.mips16
2794               && ! gpr_interlocks
2795               && (prev_insn.insn_mo->pinfo
2796                   & INSN_LOAD_MEMORY_DELAY))
2797           || (! mips_opts.mips16
2798               && mips_opts.isa == ISA_MIPS1
2799               && (prev_insn.insn_mo->pinfo
2800                   & INSN_COPROC_MEMORY_DELAY)))
2801         {
2802           /* Itbl support may require additional care here.  */
2803           ++nops;
2804           if ((! mips_opts.mips16
2805                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2806                && (! cop_interlocks
2807                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2808               || (! hilo_interlocks
2809                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2810                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2811             ++nops;
2812
2813           if (prev_insn_unreordered)
2814             nops = 0;
2815         }
2816       else if ((! mips_opts.mips16
2817                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2818                 && (! cop_interlocks
2819                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2820                || (! hilo_interlocks
2821                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2822                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2823         {
2824           /* Itbl support may require additional care here.  */
2825           if (! prev_prev_insn_unreordered)
2826             ++nops;
2827         }
2828
2829       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2830         {
2831           int min_nops = 0;
2832           const char *pn = prev_insn.insn_mo->name;
2833           if (strncmp(pn, "macc", 4) == 0
2834               || strncmp(pn, "dmacc", 5) == 0
2835               || strncmp(pn, "dmult", 5) == 0)
2836             {
2837               min_nops = 1;
2838             }
2839           if (nops < min_nops)
2840             nops = min_nops;
2841         }
2842
2843       if (nops > 0)
2844         {
2845           struct insn_label_list *l;
2846
2847           if (insns)
2848             {
2849               /* Record the frag which holds the nop instructions, so
2850                  that we can remove them if we don't need them.  */
2851               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2852               prev_nop_frag = frag_now;
2853               prev_nop_frag_holds = nops;
2854               prev_nop_frag_required = 0;
2855               prev_nop_frag_since = 0;
2856             }
2857
2858           for (; nops > 0; --nops)
2859             emit_nop ();
2860
2861           if (insns)
2862             {
2863               /* Move on to a new frag, so that it is safe to simply
2864                  decrease the size of prev_nop_frag.  */
2865               frag_wane (frag_now);
2866               frag_new (0);
2867             }
2868
2869           for (l = insn_labels; l != NULL; l = l->next)
2870             {
2871               valueT val;
2872
2873               assert (S_GET_SEGMENT (l->label) == now_seg);
2874               symbol_set_frag (l->label, frag_now);
2875               val = (valueT) frag_now_fix ();
2876               /* mips16 text labels are stored as odd.  */
2877               if (mips_opts.mips16)
2878                 ++val;
2879               S_SET_VALUE (l->label, val);
2880             }
2881         }
2882     }
2883
2884   /* Mark instruction labels in mips16 mode.  */
2885   if (insns)
2886     mips16_mark_labels ();
2887
2888   mips_no_prev_insn (insns);
2889 }
2890
2891 /* Build an instruction created by a macro expansion.  This is passed
2892    a pointer to the count of instructions created so far, an
2893    expression, the name of the instruction to build, an operand format
2894    string, and corresponding arguments.  */
2895
2896 #ifdef USE_STDARG
2897 static void
2898 macro_build (char *place,
2899              int *counter,
2900              expressionS * ep,
2901              const char *name,
2902              const char *fmt,
2903              ...)
2904 #else
2905 static void
2906 macro_build (place, counter, ep, name, fmt, va_alist)
2907      char *place;
2908      int *counter;
2909      expressionS *ep;
2910      const char *name;
2911      const char *fmt;
2912      va_dcl
2913 #endif
2914 {
2915   struct mips_cl_insn insn;
2916   bfd_reloc_code_real_type r[3];
2917   va_list args;
2918
2919 #ifdef USE_STDARG
2920   va_start (args, fmt);
2921 #else
2922   va_start (args);
2923 #endif
2924
2925   /*
2926    * If the macro is about to expand into a second instruction,
2927    * print a warning if needed. We need to pass ip as a parameter
2928    * to generate a better warning message here...
2929    */
2930   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2931     as_warn (_("Macro instruction expanded into multiple instructions"));
2932
2933   /*
2934    * If the macro is about to expand into a second instruction,
2935    * and it is in a delay slot, print a warning.
2936    */
2937   if (place == NULL
2938       && *counter == 1
2939       && mips_opts.noreorder
2940       && (prev_prev_insn.insn_mo->pinfo
2941           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2942              | INSN_COND_BRANCH_LIKELY)) != 0)
2943     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2944
2945   if (place == NULL)
2946     ++*counter;         /* bump instruction counter */
2947
2948   if (mips_opts.mips16)
2949     {
2950       mips16_macro_build (place, counter, ep, name, fmt, args);
2951       va_end (args);
2952       return;
2953     }
2954
2955   r[0] = BFD_RELOC_UNUSED;
2956   r[1] = BFD_RELOC_UNUSED;
2957   r[2] = BFD_RELOC_UNUSED;
2958   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2959   assert (insn.insn_mo);
2960   assert (strcmp (name, insn.insn_mo->name) == 0);
2961
2962   /* Search until we get a match for NAME.  */
2963   while (1)
2964     {
2965       /* It is assumed here that macros will never generate
2966          MDMX or MIPS-3D instructions.  */
2967       if (strcmp (fmt, insn.insn_mo->args) == 0
2968           && insn.insn_mo->pinfo != INSN_MACRO
2969           && OPCODE_IS_MEMBER (insn.insn_mo,
2970                                (mips_opts.isa
2971                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2972                                mips_arch)
2973           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2974         break;
2975
2976       ++insn.insn_mo;
2977       assert (insn.insn_mo->name);
2978       assert (strcmp (name, insn.insn_mo->name) == 0);
2979     }
2980
2981   insn.insn_opcode = insn.insn_mo->match;
2982   for (;;)
2983     {
2984       switch (*fmt++)
2985         {
2986         case '\0':
2987           break;
2988
2989         case ',':
2990         case '(':
2991         case ')':
2992           continue;
2993
2994         case 't':
2995         case 'w':
2996         case 'E':
2997           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2998           continue;
2999
3000         case 'c':
3001           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3002           continue;
3003
3004         case 'T':
3005         case 'W':
3006           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3007           continue;
3008
3009         case 'd':
3010         case 'G':
3011         case 'K':
3012           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3013           continue;
3014
3015         case 'U':
3016           {
3017             int tmp = va_arg (args, int);
3018
3019             insn.insn_opcode |= tmp << OP_SH_RT;
3020             insn.insn_opcode |= tmp << OP_SH_RD;
3021             continue;
3022           }
3023
3024         case 'V':
3025         case 'S':
3026           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3027           continue;
3028
3029         case 'z':
3030           continue;
3031
3032         case '<':
3033           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3034           continue;
3035
3036         case 'D':
3037           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3038           continue;
3039
3040         case 'B':
3041           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3042           continue;
3043
3044         case 'J':
3045           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3046           continue;
3047
3048         case 'q':
3049           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3050           continue;
3051
3052         case 'b':
3053         case 's':
3054         case 'r':
3055         case 'v':
3056           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3057           continue;
3058
3059         case 'i':
3060         case 'j':
3061         case 'o':
3062           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3063           assert (*r == BFD_RELOC_GPREL16
3064                   || *r == BFD_RELOC_MIPS_LITERAL
3065                   || *r == BFD_RELOC_MIPS_HIGHER
3066                   || *r == BFD_RELOC_HI16_S
3067                   || *r == BFD_RELOC_LO16
3068                   || *r == BFD_RELOC_MIPS_GOT16
3069                   || *r == BFD_RELOC_MIPS_CALL16
3070                   || *r == BFD_RELOC_MIPS_GOT_DISP
3071                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3072                   || *r == BFD_RELOC_MIPS_GOT_OFST
3073                   || *r == BFD_RELOC_MIPS_GOT_LO16
3074                   || *r == BFD_RELOC_MIPS_CALL_LO16
3075                   || (ep->X_op == O_subtract
3076                       && *r == BFD_RELOC_PCREL_LO16));
3077           continue;
3078
3079         case 'u':
3080           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3081           assert (ep != NULL
3082                   && (ep->X_op == O_constant
3083                       || (ep->X_op == O_symbol
3084                           && (*r == BFD_RELOC_MIPS_HIGHEST
3085                               || *r == BFD_RELOC_HI16_S
3086                               || *r == BFD_RELOC_HI16
3087                               || *r == BFD_RELOC_GPREL16
3088                               || *r == BFD_RELOC_MIPS_GOT_HI16
3089                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3090                       || (ep->X_op == O_subtract
3091                           && *r == BFD_RELOC_PCREL_HI16_S)));
3092           continue;
3093
3094         case 'p':
3095           assert (ep != NULL);
3096           /*
3097            * This allows macro() to pass an immediate expression for
3098            * creating short branches without creating a symbol.
3099            * Note that the expression still might come from the assembly
3100            * input, in which case the value is not checked for range nor
3101            * is a relocation entry generated (yuck).
3102            */
3103           if (ep->X_op == O_constant)
3104             {
3105               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3106               ep = NULL;
3107             }
3108           else
3109             *r = BFD_RELOC_16_PCREL_S2;
3110           continue;
3111
3112         case 'a':
3113           assert (ep != NULL);
3114           *r = BFD_RELOC_MIPS_JMP;
3115           continue;
3116
3117         case 'C':
3118           insn.insn_opcode |= va_arg (args, unsigned long);
3119           continue;
3120
3121         default:
3122           internalError ();
3123         }
3124       break;
3125     }
3126   va_end (args);
3127   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3128
3129   append_insn (place, &insn, ep, r);
3130 }
3131
3132 static void
3133 mips16_macro_build (place, counter, ep, name, fmt, args)
3134      char *place;
3135      int *counter ATTRIBUTE_UNUSED;
3136      expressionS *ep;
3137      const char *name;
3138      const char *fmt;
3139      va_list args;
3140 {
3141   struct mips_cl_insn insn;
3142   bfd_reloc_code_real_type r[3]
3143     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3144
3145   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3146   assert (insn.insn_mo);
3147   assert (strcmp (name, insn.insn_mo->name) == 0);
3148
3149   while (strcmp (fmt, insn.insn_mo->args) != 0
3150          || insn.insn_mo->pinfo == INSN_MACRO)
3151     {
3152       ++insn.insn_mo;
3153       assert (insn.insn_mo->name);
3154       assert (strcmp (name, insn.insn_mo->name) == 0);
3155     }
3156
3157   insn.insn_opcode = insn.insn_mo->match;
3158   insn.use_extend = FALSE;
3159
3160   for (;;)
3161     {
3162       int c;
3163
3164       c = *fmt++;
3165       switch (c)
3166         {
3167         case '\0':
3168           break;
3169
3170         case ',':
3171         case '(':
3172         case ')':
3173           continue;
3174
3175         case 'y':
3176         case 'w':
3177           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3178           continue;
3179
3180         case 'x':
3181         case 'v':
3182           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3183           continue;
3184
3185         case 'z':
3186           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3187           continue;
3188
3189         case 'Z':
3190           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3191           continue;
3192
3193         case '0':
3194         case 'S':
3195         case 'P':
3196         case 'R':
3197           continue;
3198
3199         case 'X':
3200           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3201           continue;
3202
3203         case 'Y':
3204           {
3205             int regno;
3206
3207             regno = va_arg (args, int);
3208             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3209             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3210           }
3211           continue;
3212
3213         case '<':
3214         case '>':
3215         case '4':
3216         case '5':
3217         case 'H':
3218         case 'W':
3219         case 'D':
3220         case 'j':
3221         case '8':
3222         case 'V':
3223         case 'C':
3224         case 'U':
3225         case 'k':
3226         case 'K':
3227         case 'p':
3228         case 'q':
3229           {
3230             assert (ep != NULL);
3231
3232             if (ep->X_op != O_constant)
3233               *r = (int) BFD_RELOC_UNUSED + c;
3234             else
3235               {
3236                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3237                               FALSE, &insn.insn_opcode, &insn.use_extend,
3238                               &insn.extend);
3239                 ep = NULL;
3240                 *r = BFD_RELOC_UNUSED;
3241               }
3242           }
3243           continue;
3244
3245         case '6':
3246           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3247           continue;
3248         }
3249
3250       break;
3251     }
3252
3253   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3254
3255   append_insn (place, &insn, ep, r);
3256 }
3257
3258 /*
3259  * Generate a "jalr" instruction with a relocation hint to the called
3260  * function.  This occurs in NewABI PIC code.
3261  */
3262 static void
3263 macro_build_jalr (icnt, ep)
3264      int icnt;
3265      expressionS *ep;
3266 {
3267   char *f = NULL;;
3268
3269   if (HAVE_NEWABI)
3270     {
3271       frag_grow (4);
3272       f = frag_more (0);
3273     }
3274   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3275                RA, PIC_CALL_REG);
3276   if (HAVE_NEWABI)
3277     fix_new_exp (frag_now, f - frag_now->fr_literal,
3278                  4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3279 }
3280
3281 /*
3282  * Generate a "lui" instruction.
3283  */
3284 static void
3285 macro_build_lui (place, counter, ep, regnum)
3286      char *place;
3287      int *counter;
3288      expressionS *ep;
3289      int regnum;
3290 {
3291   expressionS high_expr;
3292   struct mips_cl_insn insn;
3293   bfd_reloc_code_real_type r[3]
3294     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3295   const char *name = "lui";
3296   const char *fmt = "t,u";
3297
3298   assert (! mips_opts.mips16);
3299
3300   if (place == NULL)
3301     high_expr = *ep;
3302   else
3303     {
3304       high_expr.X_op = O_constant;
3305       high_expr.X_add_number = ep->X_add_number;
3306     }
3307
3308   if (high_expr.X_op == O_constant)
3309     {
3310       /* we can compute the instruction now without a relocation entry */
3311       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3312                                 >> 16) & 0xffff;
3313       *r = BFD_RELOC_UNUSED;
3314     }
3315   else
3316     {
3317       assert (ep->X_op == O_symbol);
3318       /* _gp_disp is a special case, used from s_cpload.  */
3319       assert (mips_pic == NO_PIC
3320               || (! HAVE_NEWABI
3321                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3322       *r = BFD_RELOC_HI16_S;
3323     }
3324
3325   /*
3326    * If the macro is about to expand into a second instruction,
3327    * print a warning if needed. We need to pass ip as a parameter
3328    * to generate a better warning message here...
3329    */
3330   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3331     as_warn (_("Macro instruction expanded into multiple instructions"));
3332
3333   if (place == NULL)
3334     ++*counter;         /* bump instruction counter */
3335
3336   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3337   assert (insn.insn_mo);
3338   assert (strcmp (name, insn.insn_mo->name) == 0);
3339   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3340
3341   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3342   if (*r == BFD_RELOC_UNUSED)
3343     {
3344       insn.insn_opcode |= high_expr.X_add_number;
3345       append_insn (place, &insn, NULL, r);
3346     }
3347   else
3348     append_insn (place, &insn, &high_expr, r);
3349 }
3350
3351 /* Generate a sequence of instructions to do a load or store from a constant
3352    offset off of a base register (breg) into/from a target register (treg),
3353    using AT if necessary.  */
3354 static void
3355 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3356      char *place;
3357      int *counter;
3358      expressionS *ep;
3359      const char *op;
3360      int treg, breg;
3361 {
3362   assert (ep->X_op == O_constant);
3363
3364   /* Right now, this routine can only handle signed 32-bit contants.  */
3365   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3366     as_warn (_("operand overflow"));
3367
3368   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3369     {
3370       /* Signed 16-bit offset will fit in the op.  Easy!  */
3371       macro_build (place, counter, ep, op, "t,o(b)", treg,
3372                    (int) BFD_RELOC_LO16, breg);
3373     }
3374   else
3375     {
3376       /* 32-bit offset, need multiple instructions and AT, like:
3377            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3378            addu     $tempreg,$tempreg,$breg
3379            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3380          to handle the complete offset.  */
3381       macro_build_lui (place, counter, ep, AT);
3382       if (place != NULL)
3383         place += 4;
3384       macro_build (place, counter, (expressionS *) NULL,
3385                    HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3386                    ? "add" : "addu" : "daddu",
3387                    "d,v,t", AT, AT, breg);
3388       if (place != NULL)
3389         place += 4;
3390       macro_build (place, counter, ep, op, "t,o(b)", treg,
3391                    (int) BFD_RELOC_LO16, AT);
3392
3393       if (mips_opts.noat)
3394         as_warn (_("Macro used $at after \".set noat\""));
3395     }
3396 }
3397
3398 /*                      set_at()
3399  * Generates code to set the $at register to true (one)
3400  * if reg is less than the immediate expression.
3401  */
3402 static void
3403 set_at (counter, reg, unsignedp)
3404      int *counter;
3405      int reg;
3406      int unsignedp;
3407 {
3408   if (imm_expr.X_op == O_constant
3409       && imm_expr.X_add_number >= -0x8000
3410       && imm_expr.X_add_number < 0x8000)
3411     macro_build ((char *) NULL, counter, &imm_expr,
3412                  unsignedp ? "sltiu" : "slti",
3413                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3414   else
3415     {
3416       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3417       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3418                    unsignedp ? "sltu" : "slt",
3419                    "d,v,t", AT, reg, AT);
3420     }
3421 }
3422
3423 /* Warn if an expression is not a constant.  */
3424
3425 static void
3426 check_absolute_expr (ip, ex)
3427      struct mips_cl_insn *ip;
3428      expressionS *ex;
3429 {
3430   if (ex->X_op == O_big)
3431     as_bad (_("unsupported large constant"));
3432   else if (ex->X_op != O_constant)
3433     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3434 }
3435
3436 /* Count the leading zeroes by performing a binary chop. This is a
3437    bulky bit of source, but performance is a LOT better for the
3438    majority of values than a simple loop to count the bits:
3439        for (lcnt = 0; (lcnt < 32); lcnt++)
3440          if ((v) & (1 << (31 - lcnt)))
3441            break;
3442   However it is not code size friendly, and the gain will drop a bit
3443   on certain cached systems.
3444 */
3445 #define COUNT_TOP_ZEROES(v)             \
3446   (((v) & ~0xffff) == 0                 \
3447    ? ((v) & ~0xff) == 0                 \
3448      ? ((v) & ~0xf) == 0                \
3449        ? ((v) & ~0x3) == 0              \
3450          ? ((v) & ~0x1) == 0            \
3451            ? !(v)                       \
3452              ? 32                       \
3453              : 31                       \
3454            : 30                         \
3455          : ((v) & ~0x7) == 0            \
3456            ? 29                         \
3457            : 28                         \
3458        : ((v) & ~0x3f) == 0             \
3459          ? ((v) & ~0x1f) == 0           \
3460            ? 27                         \
3461            : 26                         \
3462          : ((v) & ~0x7f) == 0           \
3463            ? 25                         \
3464            : 24                         \
3465      : ((v) & ~0xfff) == 0              \
3466        ? ((v) & ~0x3ff) == 0            \
3467          ? ((v) & ~0x1ff) == 0          \
3468            ? 23                         \
3469            : 22                         \
3470          : ((v) & ~0x7ff) == 0          \
3471            ? 21                         \
3472            : 20                         \
3473        : ((v) & ~0x3fff) == 0           \
3474          ? ((v) & ~0x1fff) == 0         \
3475            ? 19                         \
3476            : 18                         \
3477          : ((v) & ~0x7fff) == 0         \
3478            ? 17                         \
3479            : 16                         \
3480    : ((v) & ~0xffffff) == 0             \
3481      ? ((v) & ~0xfffff) == 0            \
3482        ? ((v) & ~0x3ffff) == 0          \
3483          ? ((v) & ~0x1ffff) == 0        \
3484            ? 15                         \
3485            : 14                         \
3486          : ((v) & ~0x7ffff) == 0        \
3487            ? 13                         \
3488            : 12                         \
3489        : ((v) & ~0x3fffff) == 0         \
3490          ? ((v) & ~0x1fffff) == 0       \
3491            ? 11                         \
3492            : 10                         \
3493          : ((v) & ~0x7fffff) == 0       \
3494            ? 9                          \
3495            : 8                          \
3496      : ((v) & ~0xfffffff) == 0          \
3497        ? ((v) & ~0x3ffffff) == 0        \
3498          ? ((v) & ~0x1ffffff) == 0      \
3499            ? 7                          \
3500            : 6                          \
3501          : ((v) & ~0x7ffffff) == 0      \
3502            ? 5                          \
3503            : 4                          \
3504        : ((v) & ~0x3fffffff) == 0       \
3505          ? ((v) & ~0x1fffffff) == 0     \
3506            ? 3                          \
3507            : 2                          \
3508          : ((v) & ~0x7fffffff) == 0     \
3509            ? 1                          \
3510            : 0)
3511
3512 /*                      load_register()
3513  *  This routine generates the least number of instructions neccessary to load
3514  *  an absolute expression value into a register.
3515  */
3516 static void
3517 load_register (counter, reg, ep, dbl)
3518      int *counter;
3519      int reg;
3520      expressionS *ep;
3521      int dbl;
3522 {
3523   int freg;
3524   expressionS hi32, lo32;
3525
3526   if (ep->X_op != O_big)
3527     {
3528       assert (ep->X_op == O_constant);
3529       if (ep->X_add_number < 0x8000
3530           && (ep->X_add_number >= 0
3531               || (ep->X_add_number >= -0x8000
3532                   && (! dbl
3533                       || ! ep->X_unsigned
3534                       || sizeof (ep->X_add_number) > 4))))
3535         {
3536           /* We can handle 16 bit signed values with an addiu to
3537              $zero.  No need to ever use daddiu here, since $zero and
3538              the result are always correct in 32 bit mode.  */
3539           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3540                        (int) BFD_RELOC_LO16);
3541           return;
3542         }
3543       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3544         {
3545           /* We can handle 16 bit unsigned values with an ori to
3546              $zero.  */
3547           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3548                        (int) BFD_RELOC_LO16);
3549           return;
3550         }
3551       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3552                 && (! dbl
3553                     || ! ep->X_unsigned
3554                     || sizeof (ep->X_add_number) > 4
3555                     || (ep->X_add_number & 0x80000000) == 0))
3556                || ((HAVE_32BIT_GPRS || ! dbl)
3557                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3558                || (HAVE_32BIT_GPRS
3559                    && ! dbl
3560                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3561                        == ~ (offsetT) 0xffffffff)))
3562         {
3563           /* 32 bit values require an lui.  */
3564           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3565                        (int) BFD_RELOC_HI16);
3566           if ((ep->X_add_number & 0xffff) != 0)
3567             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3568                          (int) BFD_RELOC_LO16);
3569           return;
3570         }
3571     }
3572
3573   /* The value is larger than 32 bits.  */
3574
3575   if (HAVE_32BIT_GPRS)
3576     {
3577       as_bad (_("Number (0x%lx) larger than 32 bits"),
3578               (unsigned long) ep->X_add_number);
3579       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3580                    (int) BFD_RELOC_LO16);
3581       return;
3582     }
3583
3584   if (ep->X_op != O_big)
3585     {
3586       hi32 = *ep;
3587       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3588       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3589       hi32.X_add_number &= 0xffffffff;
3590       lo32 = *ep;
3591       lo32.X_add_number &= 0xffffffff;
3592     }
3593   else
3594     {
3595       assert (ep->X_add_number > 2);
3596       if (ep->X_add_number == 3)
3597         generic_bignum[3] = 0;
3598       else if (ep->X_add_number > 4)
3599         as_bad (_("Number larger than 64 bits"));
3600       lo32.X_op = O_constant;
3601       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3602       hi32.X_op = O_constant;
3603       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3604     }
3605
3606   if (hi32.X_add_number == 0)
3607     freg = 0;
3608   else
3609     {
3610       int shift, bit;
3611       unsigned long hi, lo;
3612
3613       if (hi32.X_add_number == (offsetT) 0xffffffff)
3614         {
3615           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3616             {
3617               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3618                            reg, 0, (int) BFD_RELOC_LO16);
3619               return;
3620             }
3621           if (lo32.X_add_number & 0x80000000)
3622             {
3623               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3624                            (int) BFD_RELOC_HI16);
3625               if (lo32.X_add_number & 0xffff)
3626                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3627                              reg, reg, (int) BFD_RELOC_LO16);
3628               return;
3629             }
3630         }
3631
3632       /* Check for 16bit shifted constant.  We know that hi32 is
3633          non-zero, so start the mask on the first bit of the hi32
3634          value.  */
3635       shift = 17;
3636       do
3637         {
3638           unsigned long himask, lomask;
3639
3640           if (shift < 32)
3641             {
3642               himask = 0xffff >> (32 - shift);
3643               lomask = (0xffff << shift) & 0xffffffff;
3644             }
3645           else
3646             {
3647               himask = 0xffff << (shift - 32);
3648               lomask = 0;
3649             }
3650           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3651               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3652             {
3653               expressionS tmp;
3654
3655               tmp.X_op = O_constant;
3656               if (shift < 32)
3657                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3658                                     | (lo32.X_add_number >> shift));
3659               else
3660                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3661               macro_build ((char *) NULL, counter, &tmp,
3662                            "ori", "t,r,i", reg, 0,
3663                            (int) BFD_RELOC_LO16);
3664               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3665                            (shift >= 32) ? "dsll32" : "dsll",
3666                            "d,w,<", reg, reg,
3667                            (shift >= 32) ? shift - 32 : shift);
3668               return;
3669             }
3670           ++shift;
3671         }
3672       while (shift <= (64 - 16));
3673
3674       /* Find the bit number of the lowest one bit, and store the
3675          shifted value in hi/lo.  */
3676       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3677       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3678       if (lo != 0)
3679         {
3680           bit = 0;
3681           while ((lo & 1) == 0)
3682             {
3683               lo >>= 1;
3684               ++bit;
3685             }
3686           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3687           hi >>= bit;
3688         }
3689       else
3690         {
3691           bit = 32;
3692           while ((hi & 1) == 0)
3693             {
3694               hi >>= 1;
3695               ++bit;
3696             }
3697           lo = hi;
3698           hi = 0;
3699         }
3700
3701       /* Optimize if the shifted value is a (power of 2) - 1.  */
3702       if ((hi == 0 && ((lo + 1) & lo) == 0)
3703           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3704         {
3705           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3706           if (shift != 0)
3707             {
3708               expressionS tmp;
3709
3710               /* This instruction will set the register to be all
3711                  ones.  */
3712               tmp.X_op = O_constant;
3713               tmp.X_add_number = (offsetT) -1;
3714               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3715                            reg, 0, (int) BFD_RELOC_LO16);
3716               if (bit != 0)
3717                 {
3718                   bit += shift;
3719                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3720                                (bit >= 32) ? "dsll32" : "dsll",
3721                                "d,w,<", reg, reg,
3722                                (bit >= 32) ? bit - 32 : bit);
3723                 }
3724               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3725                            (shift >= 32) ? "dsrl32" : "dsrl",
3726                            "d,w,<", reg, reg,
3727                            (shift >= 32) ? shift - 32 : shift);
3728               return;
3729             }
3730         }
3731
3732       /* Sign extend hi32 before calling load_register, because we can
3733          generally get better code when we load a sign extended value.  */
3734       if ((hi32.X_add_number & 0x80000000) != 0)
3735         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3736       load_register (counter, reg, &hi32, 0);
3737       freg = reg;
3738     }
3739   if ((lo32.X_add_number & 0xffff0000) == 0)
3740     {
3741       if (freg != 0)
3742         {
3743           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3744                        "dsll32", "d,w,<", reg, freg, 0);
3745           freg = reg;
3746         }
3747     }
3748   else
3749     {
3750       expressionS mid16;
3751
3752       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3753         {
3754           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3755                        (int) BFD_RELOC_HI16);
3756           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3757                        "dsrl32", "d,w,<", reg, reg, 0);
3758           return;
3759         }
3760
3761       if (freg != 0)
3762         {
3763           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3764                        "d,w,<", reg, freg, 16);
3765           freg = reg;
3766         }
3767       mid16 = lo32;
3768       mid16.X_add_number >>= 16;
3769       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3770                    freg, (int) BFD_RELOC_LO16);
3771       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3772                    "d,w,<", reg, reg, 16);
3773       freg = reg;
3774     }
3775   if ((lo32.X_add_number & 0xffff) != 0)
3776     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3777                  (int) BFD_RELOC_LO16);
3778 }
3779
3780 /* Load an address into a register.  */
3781
3782 static void
3783 load_address (counter, reg, ep, used_at)
3784      int *counter;
3785      int reg;
3786      expressionS *ep;
3787      int *used_at;
3788 {
3789   char *p = NULL;
3790
3791   if (ep->X_op != O_constant
3792       && ep->X_op != O_symbol)
3793     {
3794       as_bad (_("expression too complex"));
3795       ep->X_op = O_constant;
3796     }
3797
3798   if (ep->X_op == O_constant)
3799     {
3800       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3801       return;
3802     }
3803
3804   if (mips_pic == NO_PIC)
3805     {
3806       /* If this is a reference to a GP relative symbol, we want
3807            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3808          Otherwise we want
3809            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3810            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3811          If we have an addend, we always use the latter form.
3812
3813          With 64bit address space and a usable $at we want
3814            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3815            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3816            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3817            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3818            dsll32       $reg,0
3819            daddu        $reg,$reg,$at
3820
3821          If $at is already in use, we use a path which is suboptimal
3822          on superscalar processors.
3823            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3824            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3825            dsll         $reg,16
3826            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3827            dsll         $reg,16
3828            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3829        */
3830       if (HAVE_64BIT_ADDRESSES)
3831         {
3832           /* We don't do GP optimization for now because RELAX_ENCODE can't
3833              hold the data for such large chunks.  */
3834
3835           if (*used_at == 0 && ! mips_opts.noat)
3836             {
3837               macro_build (p, counter, ep, "lui", "t,u",
3838                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3839               macro_build (p, counter, ep, "lui", "t,u",
3840                            AT, (int) BFD_RELOC_HI16_S);
3841               macro_build (p, counter, ep, "daddiu", "t,r,j",
3842                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3843               macro_build (p, counter, ep, "daddiu", "t,r,j",
3844                            AT, AT, (int) BFD_RELOC_LO16);
3845               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3846                            "d,w,<", reg, reg, 0);
3847               macro_build (p, counter, (expressionS *) NULL, "daddu",
3848                            "d,v,t", reg, reg, AT);
3849               *used_at = 1;
3850             }
3851           else
3852             {
3853               macro_build (p, counter, ep, "lui", "t,u",
3854                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3855               macro_build (p, counter, ep, "daddiu", "t,r,j",
3856                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3857               macro_build (p, counter, (expressionS *) NULL, "dsll",
3858                            "d,w,<", reg, reg, 16);
3859               macro_build (p, counter, ep, "daddiu", "t,r,j",
3860                            reg, reg, (int) BFD_RELOC_HI16_S);
3861               macro_build (p, counter, (expressionS *) NULL, "dsll",
3862                            "d,w,<", reg, reg, 16);
3863               macro_build (p, counter, ep, "daddiu", "t,r,j",
3864                            reg, reg, (int) BFD_RELOC_LO16);
3865             }
3866         }
3867       else
3868         {
3869           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3870               && ! nopic_need_relax (ep->X_add_symbol, 1))
3871             {
3872               frag_grow (20);
3873               macro_build ((char *) NULL, counter, ep,
3874                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3875                            ? "addi" : "addiu" : "daddiu", "t,r,j",
3876                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3877               p = frag_var (rs_machine_dependent, 8, 0,
3878                             RELAX_ENCODE (4, 8, 0, 4, 0,
3879                                           mips_opts.warn_about_macros),
3880                             ep->X_add_symbol, 0, NULL);
3881             }
3882           macro_build_lui (p, counter, ep, reg);
3883           if (p != NULL)
3884             p += 4;
3885           macro_build (p, counter, ep,
3886                        HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3887                        ? "addi" : "addiu" : "daddiu",
3888                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3889         }
3890     }
3891   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3892     {
3893       expressionS ex;
3894
3895       /* If this is a reference to an external symbol, we want
3896            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3897          Otherwise we want
3898            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3899            nop
3900            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3901          If there is a constant, it must be added in after.
3902
3903          If we have NewABI, we want
3904            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
3905          unless we're referencing a global symbol with a non-zero
3906          offset, in which case cst must be added separately.  */
3907       if (HAVE_NEWABI)
3908         {
3909           frag_grow (12);
3910
3911           if (ep->X_add_number)
3912             {
3913               frag_now->tc_frag_data.tc_fr_offset =
3914                 ex.X_add_number = ep->X_add_number;
3915               ep->X_add_number = 0;
3916               macro_build ((char *) NULL, counter, ep,
3917                            HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3918                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3919               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3920                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3921               ex.X_op = O_constant;
3922               macro_build ((char *) NULL, counter, &ex,
3923                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
3924                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3925               p = frag_var (rs_machine_dependent, 8, 0,
3926                             RELAX_ENCODE (8, 4, 0, 0, 0,
3927                                           mips_opts.warn_about_macros),
3928                             ep->X_add_symbol, 0, (char *) NULL);
3929               ep->X_add_number = ex.X_add_number;
3930             }
3931
3932           macro_build (p, counter, ep,
3933                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3934                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3935
3936           if (! p)
3937             {
3938               /* To avoid confusion in tc_gen_reloc, we must ensure
3939                  that this does not become a variant frag.  */
3940               frag_wane (frag_now);
3941               frag_new (0);
3942             }
3943         }
3944       else
3945         {
3946           ex.X_add_number = ep->X_add_number;
3947           ep->X_add_number = 0;
3948           frag_grow (20);
3949           macro_build ((char *) NULL, counter, ep,
3950                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3951                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3952           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3953           p = frag_var (rs_machine_dependent, 4, 0,
3954                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3955                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3956           macro_build (p, counter, ep,
3957                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3958                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3959
3960           if (ex.X_add_number != 0)
3961             {
3962               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3963                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3964               ex.X_op = O_constant;
3965               macro_build ((char *) NULL, counter, &ex,
3966                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3967                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3968             }
3969         }
3970     }
3971   else if (mips_pic == SVR4_PIC)
3972     {
3973       expressionS ex;
3974       int off;
3975
3976       /* This is the large GOT case.  If this is a reference to an
3977          external symbol, we want
3978            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3979            addu         $reg,$reg,$gp
3980            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3981
3982          Otherwise, for a reference to a local symbol in old ABI, we want
3983            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3984            nop
3985            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3986          If there is a constant, it must be added in after.
3987
3988          In the NewABI, for local symbols, with or without offsets, we want:
3989            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3990            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3991       */
3992       if (HAVE_NEWABI)
3993         {
3994           frag_grow (24);
3995
3996           frag_now->tc_frag_data.tc_fr_offset =
3997             ex.X_add_number = ep->X_add_number;
3998           ep->X_add_number = 0;
3999           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4000                        (int) BFD_RELOC_MIPS_GOT_HI16);
4001           macro_build ((char *) NULL, counter, (expressionS *) NULL,
4002                        HAVE_32BIT_ADDRESSES ? "add" : "daddu", "d,v,t", reg,
4003                        reg, mips_gp_register);
4004           macro_build ((char *) NULL, counter, ep,
4005                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4006                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4007           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4008             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4009           else if (ex.X_add_number)
4010             {
4011               ex.X_op = O_constant;
4012               macro_build ((char *) NULL, counter, &ex,
4013                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
4014                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4015             }
4016
4017           ep->X_add_number = ex.X_add_number;
4018           p = frag_var (rs_machine_dependent, 8, 0,
4019                         RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
4020                                       mips_opts.warn_about_macros),
4021                         ep->X_add_symbol, 0, (char *) NULL);
4022           macro_build (p, counter, ep,
4023                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4024                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4025           macro_build (p + 4, counter, ep,
4026                        HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
4027                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
4028         }
4029       else
4030         {
4031           ex.X_add_number = ep->X_add_number;
4032           ep->X_add_number = 0;
4033           if (reg_needs_delay (mips_gp_register))
4034             off = 4;
4035           else
4036             off = 0;
4037           frag_grow (32);
4038           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4039                        (int) BFD_RELOC_MIPS_GOT_HI16);
4040           macro_build ((char *) NULL, counter, (expressionS *) NULL,
4041                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
4042                        reg, mips_gp_register);
4043           macro_build ((char *) NULL, counter, ep,
4044                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4045                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4046           p = frag_var (rs_machine_dependent, 12 + off, 0,
4047                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
4048                                       mips_opts.warn_about_macros),
4049                         ep->X_add_symbol, 0, NULL);
4050           if (off > 0)
4051             {
4052               /* We need a nop before loading from $gp.  This special
4053                  check is required because the lui which starts the main
4054                  instruction stream does not refer to $gp, and so will not
4055                  insert the nop which may be required.  */
4056               macro_build (p, counter, (expressionS *) NULL, "nop", "");
4057                 p += 4;
4058             }
4059           macro_build (p, counter, ep,
4060                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4061                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4062           p += 4;
4063           macro_build (p, counter, (expressionS *) NULL, "nop", "");
4064           p += 4;
4065           macro_build (p, counter, ep,
4066                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4067                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4068
4069           if (ex.X_add_number != 0)
4070             {
4071               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4072                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4073               ex.X_op = O_constant;
4074               macro_build ((char *) NULL, counter, &ex,
4075                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4076                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4077             }
4078         }
4079     }
4080   else if (mips_pic == EMBEDDED_PIC)
4081     {
4082       /* We always do
4083            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
4084        */
4085       macro_build ((char *) NULL, counter, ep,
4086                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4087                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4088     }
4089   else
4090     abort ();
4091 }
4092
4093 /* Move the contents of register SOURCE into register DEST.  */
4094
4095 static void
4096 move_register (counter, dest, source)
4097      int *counter;
4098      int dest;
4099      int source;
4100 {
4101   macro_build ((char *) NULL, counter, (expressionS *) NULL,
4102                HAVE_32BIT_GPRS ? "addu" : "daddu",
4103                "d,v,t", dest, source, 0);
4104 }
4105
4106 /*
4107  *                      Build macros
4108  *   This routine implements the seemingly endless macro or synthesized
4109  * instructions and addressing modes in the mips assembly language. Many
4110  * of these macros are simple and are similar to each other. These could
4111  * probably be handled by some kind of table or grammer aproach instead of
4112  * this verbose method. Others are not simple macros but are more like
4113  * optimizing code generation.
4114  *   One interesting optimization is when several store macros appear
4115  * consecutivly that would load AT with the upper half of the same address.
4116  * The ensuing load upper instructions are ommited. This implies some kind
4117  * of global optimization. We currently only optimize within a single macro.
4118  *   For many of the load and store macros if the address is specified as a
4119  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4120  * first load register 'at' with zero and use it as the base register. The
4121  * mips assembler simply uses register $zero. Just one tiny optimization
4122  * we're missing.
4123  */
4124 static void
4125 macro (ip)
4126      struct mips_cl_insn *ip;
4127 {
4128   register int treg, sreg, dreg, breg;
4129   int tempreg;
4130   int mask;
4131   int icnt = 0;
4132   int used_at = 0;
4133   expressionS expr1;
4134   const char *s;
4135   const char *s2;
4136   const char *fmt;
4137   int likely = 0;
4138   int dbl = 0;
4139   int coproc = 0;
4140   int lr = 0;
4141   int imm = 0;
4142   offsetT maxnum;
4143   int off;
4144   bfd_reloc_code_real_type r;
4145   int hold_mips_optimize;
4146
4147   assert (! mips_opts.mips16);
4148
4149   treg = (ip->insn_opcode >> 16) & 0x1f;
4150   dreg = (ip->insn_opcode >> 11) & 0x1f;
4151   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4152   mask = ip->insn_mo->mask;
4153
4154   expr1.X_op = O_constant;
4155   expr1.X_op_symbol = NULL;
4156   expr1.X_add_symbol = NULL;
4157   expr1.X_add_number = 1;
4158
4159   /* Umatched fixups should not be put in the same frag as a relaxable
4160      macro.  For example, suppose we have:
4161
4162         lui $4,%hi(l1)          # 1
4163         la $5,l2                # 2
4164         addiu $4,$4,%lo(l1)     # 3
4165
4166      If instructions 1 and 2 were put in the same frag, md_frob_file would
4167      move the fixup for #1 after the fixups for the "unrelaxed" version of
4168      #2.  This would confuse tc_gen_reloc, which expects the relocations
4169      for #2 to be the last for that frag.
4170
4171      Also, if tc_gen_reloc sees certain relocations in a variant frag,
4172      it assumes that they belong to a relaxable macro.  We mustn't put
4173      other uses of such relocations into a variant frag.
4174
4175      To avoid both problems, finish the current frag it contains a
4176      %reloc() operator.  The macro then goes into a new frag.  */
4177   if (prev_reloc_op_frag == frag_now)
4178     {
4179       frag_wane (frag_now);
4180       frag_new (0);
4181     }
4182
4183   switch (mask)
4184     {
4185     case M_DABS:
4186       dbl = 1;
4187     case M_ABS:
4188       /* bgez $a0,.+12
4189          move v0,$a0
4190          sub v0,$zero,$a0
4191          */
4192
4193       mips_emit_delays (TRUE);
4194       ++mips_opts.noreorder;
4195       mips_any_noreorder = 1;
4196
4197       expr1.X_add_number = 8;
4198       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4199       if (dreg == sreg)
4200         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4201                      0);
4202       else
4203         move_register (&icnt, dreg, sreg);
4204       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4205                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4206
4207       --mips_opts.noreorder;
4208       return;
4209
4210     case M_ADD_I:
4211       s = "addi";
4212       s2 = "add";
4213       goto do_addi;
4214     case M_ADDU_I:
4215       s = "addiu";
4216       s2 = "addu";
4217       goto do_addi;
4218     case M_DADD_I:
4219       dbl = 1;
4220       s = "daddi";
4221       s2 = "dadd";
4222       goto do_addi;
4223     case M_DADDU_I:
4224       dbl = 1;
4225       s = "daddiu";
4226       s2 = "daddu";
4227     do_addi:
4228       if (imm_expr.X_op == O_constant
4229           && imm_expr.X_add_number >= -0x8000
4230           && imm_expr.X_add_number < 0x8000)
4231         {
4232           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4233                        (int) BFD_RELOC_LO16);
4234           return;
4235         }
4236       load_register (&icnt, AT, &imm_expr, dbl);
4237       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4238                    treg, sreg, AT);
4239       break;
4240
4241     case M_AND_I:
4242       s = "andi";
4243       s2 = "and";
4244       goto do_bit;
4245     case M_OR_I:
4246       s = "ori";
4247       s2 = "or";
4248       goto do_bit;
4249     case M_NOR_I:
4250       s = "";
4251       s2 = "nor";
4252       goto do_bit;
4253     case M_XOR_I:
4254       s = "xori";
4255       s2 = "xor";
4256     do_bit:
4257       if (imm_expr.X_op == O_constant
4258           && imm_expr.X_add_number >= 0
4259           && imm_expr.X_add_number < 0x10000)
4260         {
4261           if (mask != M_NOR_I)
4262             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4263                          sreg, (int) BFD_RELOC_LO16);
4264           else
4265             {
4266               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4267                            treg, sreg, (int) BFD_RELOC_LO16);
4268               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4269                            "d,v,t", treg, treg, 0);
4270             }
4271           return;
4272         }
4273
4274       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4275       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4276                    treg, sreg, AT);
4277       break;
4278
4279     case M_BEQ_I:
4280       s = "beq";
4281       goto beq_i;
4282     case M_BEQL_I:
4283       s = "beql";
4284       likely = 1;
4285       goto beq_i;
4286     case M_BNE_I:
4287       s = "bne";
4288       goto beq_i;
4289     case M_BNEL_I:
4290       s = "bnel";
4291       likely = 1;
4292     beq_i:
4293       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4294         {
4295           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4296                        0);
4297           return;
4298         }
4299       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4300       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4301       break;
4302
4303     case M_BGEL:
4304       likely = 1;
4305     case M_BGE:
4306       if (treg == 0)
4307         {
4308           macro_build ((char *) NULL, &icnt, &offset_expr,
4309                        likely ? "bgezl" : "bgez", "s,p", sreg);
4310           return;
4311         }
4312       if (sreg == 0)
4313         {
4314           macro_build ((char *) NULL, &icnt, &offset_expr,
4315                        likely ? "blezl" : "blez", "s,p", treg);
4316           return;
4317         }
4318       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4319                    AT, sreg, treg);
4320       macro_build ((char *) NULL, &icnt, &offset_expr,
4321                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4322       break;
4323
4324     case M_BGTL_I:
4325       likely = 1;
4326     case M_BGT_I:
4327       /* check for > max integer */
4328       maxnum = 0x7fffffff;
4329       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4330         {
4331           maxnum <<= 16;
4332           maxnum |= 0xffff;
4333           maxnum <<= 16;
4334           maxnum |= 0xffff;
4335         }
4336       if (imm_expr.X_op == O_constant
4337           && imm_expr.X_add_number >= maxnum
4338           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4339         {
4340         do_false:
4341           /* result is always false */
4342           if (! likely)
4343             {
4344               if (warn_nops)
4345                 as_warn (_("Branch %s is always false (nop)"),
4346                          ip->insn_mo->name);
4347               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4348                            "", 0);
4349             }
4350           else
4351             {
4352               if (warn_nops)
4353                 as_warn (_("Branch likely %s is always false"),
4354                          ip->insn_mo->name);
4355               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4356                            "s,t,p", 0, 0);
4357             }
4358           return;
4359         }
4360       if (imm_expr.X_op != O_constant)
4361         as_bad (_("Unsupported large constant"));
4362       ++imm_expr.X_add_number;
4363       /* FALLTHROUGH */
4364     case M_BGE_I:
4365     case M_BGEL_I:
4366       if (mask == M_BGEL_I)
4367         likely = 1;
4368       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4369         {
4370           macro_build ((char *) NULL, &icnt, &offset_expr,
4371                        likely ? "bgezl" : "bgez", "s,p", sreg);
4372           return;
4373         }
4374       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4375         {
4376           macro_build ((char *) NULL, &icnt, &offset_expr,
4377                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4378           return;
4379         }
4380       maxnum = 0x7fffffff;
4381       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4382         {
4383           maxnum <<= 16;
4384           maxnum |= 0xffff;
4385           maxnum <<= 16;
4386           maxnum |= 0xffff;
4387         }
4388       maxnum = - maxnum - 1;
4389       if (imm_expr.X_op == O_constant
4390           && imm_expr.X_add_number <= maxnum
4391           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4392         {
4393         do_true:
4394           /* result is always true */
4395           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4396           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4397           return;
4398         }
4399       set_at (&icnt, sreg, 0);
4400       macro_build ((char *) NULL, &icnt, &offset_expr,
4401                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4402       break;
4403
4404     case M_BGEUL:
4405       likely = 1;
4406     case M_BGEU:
4407       if (treg == 0)
4408         goto do_true;
4409       if (sreg == 0)
4410         {
4411           macro_build ((char *) NULL, &icnt, &offset_expr,
4412                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4413           return;
4414         }
4415       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4416                    "d,v,t", AT, sreg, treg);
4417       macro_build ((char *) NULL, &icnt, &offset_expr,
4418                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4419       break;
4420
4421     case M_BGTUL_I:
4422       likely = 1;
4423     case M_BGTU_I:
4424       if (sreg == 0
4425           || (HAVE_32BIT_GPRS
4426               && imm_expr.X_op == O_constant
4427               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4428         goto do_false;
4429       if (imm_expr.X_op != O_constant)
4430         as_bad (_("Unsupported large constant"));
4431       ++imm_expr.X_add_number;
4432       /* FALLTHROUGH */
4433     case M_BGEU_I:
4434     case M_BGEUL_I:
4435       if (mask == M_BGEUL_I)
4436         likely = 1;
4437       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4438         goto do_true;
4439       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4440         {
4441           macro_build ((char *) NULL, &icnt, &offset_expr,
4442                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4443           return;
4444         }
4445       set_at (&icnt, sreg, 1);
4446       macro_build ((char *) NULL, &icnt, &offset_expr,
4447                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4448       break;
4449
4450     case M_BGTL:
4451       likely = 1;
4452     case M_BGT:
4453       if (treg == 0)
4454         {
4455           macro_build ((char *) NULL, &icnt, &offset_expr,
4456                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4457           return;
4458         }
4459       if (sreg == 0)
4460         {
4461           macro_build ((char *) NULL, &icnt, &offset_expr,
4462                        likely ? "bltzl" : "bltz", "s,p", treg);
4463           return;
4464         }
4465       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4466                    AT, treg, sreg);
4467       macro_build ((char *) NULL, &icnt, &offset_expr,
4468                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4469       break;
4470
4471     case M_BGTUL:
4472       likely = 1;
4473     case M_BGTU:
4474       if (treg == 0)
4475         {
4476           macro_build ((char *) NULL, &icnt, &offset_expr,
4477                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4478           return;
4479         }
4480       if (sreg == 0)
4481         goto do_false;
4482       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4483                    "d,v,t", AT, treg, sreg);
4484       macro_build ((char *) NULL, &icnt, &offset_expr,
4485                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4486       break;
4487
4488     case M_BLEL:
4489       likely = 1;
4490     case M_BLE:
4491       if (treg == 0)
4492         {
4493           macro_build ((char *) NULL, &icnt, &offset_expr,
4494                        likely ? "blezl" : "blez", "s,p", sreg);
4495           return;
4496         }
4497       if (sreg == 0)
4498         {
4499           macro_build ((char *) NULL, &icnt, &offset_expr,
4500                        likely ? "bgezl" : "bgez", "s,p", treg);
4501           return;
4502         }
4503       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4504                    AT, treg, sreg);
4505       macro_build ((char *) NULL, &icnt, &offset_expr,
4506                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4507       break;
4508
4509     case M_BLEL_I:
4510       likely = 1;
4511     case M_BLE_I:
4512       maxnum = 0x7fffffff;
4513       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4514         {
4515           maxnum <<= 16;
4516           maxnum |= 0xffff;
4517           maxnum <<= 16;
4518           maxnum |= 0xffff;
4519         }
4520       if (imm_expr.X_op == O_constant
4521           && imm_expr.X_add_number >= maxnum
4522           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4523         goto do_true;
4524       if (imm_expr.X_op != O_constant)
4525         as_bad (_("Unsupported large constant"));
4526       ++imm_expr.X_add_number;
4527       /* FALLTHROUGH */
4528     case M_BLT_I:
4529     case M_BLTL_I:
4530       if (mask == M_BLTL_I)
4531         likely = 1;
4532       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4533         {
4534           macro_build ((char *) NULL, &icnt, &offset_expr,
4535                        likely ? "bltzl" : "bltz", "s,p", sreg);
4536           return;
4537         }
4538       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4539         {
4540           macro_build ((char *) NULL, &icnt, &offset_expr,
4541                        likely ? "blezl" : "blez", "s,p", sreg);
4542           return;
4543         }
4544       set_at (&icnt, sreg, 0);
4545       macro_build ((char *) NULL, &icnt, &offset_expr,
4546                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4547       break;
4548
4549     case M_BLEUL:
4550       likely = 1;
4551     case M_BLEU:
4552       if (treg == 0)
4553         {
4554           macro_build ((char *) NULL, &icnt, &offset_expr,
4555                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4556           return;
4557         }
4558       if (sreg == 0)
4559         goto do_true;
4560       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4561                    "d,v,t", AT, treg, sreg);
4562       macro_build ((char *) NULL, &icnt, &offset_expr,
4563                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4564       break;
4565
4566     case M_BLEUL_I:
4567       likely = 1;
4568     case M_BLEU_I:
4569       if (sreg == 0
4570           || (HAVE_32BIT_GPRS
4571               && imm_expr.X_op == O_constant
4572               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4573         goto do_true;
4574       if (imm_expr.X_op != O_constant)
4575         as_bad (_("Unsupported large constant"));
4576       ++imm_expr.X_add_number;
4577       /* FALLTHROUGH */
4578     case M_BLTU_I:
4579     case M_BLTUL_I:
4580       if (mask == M_BLTUL_I)
4581         likely = 1;
4582       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4583         goto do_false;
4584       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4585         {
4586           macro_build ((char *) NULL, &icnt, &offset_expr,
4587                        likely ? "beql" : "beq",
4588                        "s,t,p", sreg, 0);
4589           return;
4590         }
4591       set_at (&icnt, sreg, 1);
4592       macro_build ((char *) NULL, &icnt, &offset_expr,
4593                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4594       break;
4595
4596     case M_BLTL:
4597       likely = 1;
4598     case M_BLT:
4599       if (treg == 0)
4600         {
4601           macro_build ((char *) NULL, &icnt, &offset_expr,
4602                        likely ? "bltzl" : "bltz", "s,p", sreg);
4603           return;
4604         }
4605       if (sreg == 0)
4606         {
4607           macro_build ((char *) NULL, &icnt, &offset_expr,
4608                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4609           return;
4610         }
4611       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4612                    AT, sreg, treg);
4613       macro_build ((char *) NULL, &icnt, &offset_expr,
4614                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4615       break;
4616
4617     case M_BLTUL:
4618       likely = 1;
4619     case M_BLTU:
4620       if (treg == 0)
4621         goto do_false;
4622       if (sreg == 0)
4623         {
4624           macro_build ((char *) NULL, &icnt, &offset_expr,
4625                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4626           return;
4627         }
4628       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4629                    "d,v,t", AT, sreg,
4630                    treg);
4631       macro_build ((char *) NULL, &icnt, &offset_expr,
4632                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4633       break;
4634
4635     case M_DDIV_3:
4636       dbl = 1;
4637     case M_DIV_3:
4638       s = "mflo";
4639       goto do_div3;
4640     case M_DREM_3:
4641       dbl = 1;
4642     case M_REM_3:
4643       s = "mfhi";
4644     do_div3:
4645       if (treg == 0)
4646         {
4647           as_warn (_("Divide by zero."));
4648           if (mips_trap)
4649             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4650                          "s,t,q", 0, 0, 7);
4651           else
4652             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4653                          "c", 7);
4654           return;
4655         }
4656
4657       mips_emit_delays (TRUE);
4658       ++mips_opts.noreorder;
4659       mips_any_noreorder = 1;
4660       if (mips_trap)
4661         {
4662           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4663                        "s,t,q", treg, 0, 7);
4664           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4665                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4666         }
4667       else
4668         {
4669           expr1.X_add_number = 8;
4670           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4671           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4672                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4673           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4674                        "c", 7);
4675         }
4676       expr1.X_add_number = -1;
4677       macro_build ((char *) NULL, &icnt, &expr1,
4678                    dbl ? "daddiu" : "addiu",
4679                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4680       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4681       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4682       if (dbl)
4683         {
4684           expr1.X_add_number = 1;
4685           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4686                        (int) BFD_RELOC_LO16);
4687           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4688                        "d,w,<", AT, AT, 31);
4689         }
4690       else
4691         {
4692           expr1.X_add_number = 0x80000000;
4693           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4694                        (int) BFD_RELOC_HI16);
4695         }
4696       if (mips_trap)
4697         {
4698           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4699                        "s,t,q", sreg, AT, 6);
4700           /* We want to close the noreorder block as soon as possible, so
4701              that later insns are available for delay slot filling.  */
4702           --mips_opts.noreorder;
4703         }
4704       else
4705         {
4706           expr1.X_add_number = 8;
4707           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4708           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4709                        0);
4710
4711           /* We want to close the noreorder block as soon as possible, so
4712              that later insns are available for delay slot filling.  */
4713           --mips_opts.noreorder;
4714
4715           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4716                        "c", 6);
4717         }
4718       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4719       break;
4720
4721     case M_DIV_3I:
4722       s = "div";
4723       s2 = "mflo";
4724       goto do_divi;
4725     case M_DIVU_3I:
4726       s = "divu";
4727       s2 = "mflo";
4728       goto do_divi;
4729     case M_REM_3I:
4730       s = "div";
4731       s2 = "mfhi";
4732       goto do_divi;
4733     case M_REMU_3I:
4734       s = "divu";
4735       s2 = "mfhi";
4736       goto do_divi;
4737     case M_DDIV_3I:
4738       dbl = 1;
4739       s = "ddiv";
4740       s2 = "mflo";
4741       goto do_divi;
4742     case M_DDIVU_3I:
4743       dbl = 1;
4744       s = "ddivu";
4745       s2 = "mflo";
4746       goto do_divi;
4747     case M_DREM_3I:
4748       dbl = 1;
4749       s = "ddiv";
4750       s2 = "mfhi";
4751       goto do_divi;
4752     case M_DREMU_3I:
4753       dbl = 1;
4754       s = "ddivu";
4755       s2 = "mfhi";
4756     do_divi:
4757       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4758         {
4759           as_warn (_("Divide by zero."));
4760           if (mips_trap)
4761             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4762                          "s,t,q", 0, 0, 7);
4763           else
4764             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4765                          "c", 7);
4766           return;
4767         }
4768       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4769         {
4770           if (strcmp (s2, "mflo") == 0)
4771             move_register (&icnt, dreg, sreg);
4772           else
4773             move_register (&icnt, dreg, 0);
4774           return;
4775         }
4776       if (imm_expr.X_op == O_constant
4777           && imm_expr.X_add_number == -1
4778           && s[strlen (s) - 1] != 'u')
4779         {
4780           if (strcmp (s2, "mflo") == 0)
4781             {
4782               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4783                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4784             }
4785           else
4786             move_register (&icnt, dreg, 0);
4787           return;
4788         }
4789
4790       load_register (&icnt, AT, &imm_expr, dbl);
4791       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4792                    sreg, AT);
4793       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4794       break;
4795
4796     case M_DIVU_3:
4797       s = "divu";
4798       s2 = "mflo";
4799       goto do_divu3;
4800     case M_REMU_3:
4801       s = "divu";
4802       s2 = "mfhi";
4803       goto do_divu3;
4804     case M_DDIVU_3:
4805       s = "ddivu";
4806       s2 = "mflo";
4807       goto do_divu3;
4808     case M_DREMU_3:
4809       s = "ddivu";
4810       s2 = "mfhi";
4811     do_divu3:
4812       mips_emit_delays (TRUE);
4813       ++mips_opts.noreorder;
4814       mips_any_noreorder = 1;
4815       if (mips_trap)
4816         {
4817           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4818                        "s,t,q", treg, 0, 7);
4819           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4820                        sreg, treg);
4821           /* We want to close the noreorder block as soon as possible, so
4822              that later insns are available for delay slot filling.  */
4823           --mips_opts.noreorder;
4824         }
4825       else
4826         {
4827           expr1.X_add_number = 8;
4828           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4829           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4830                        sreg, treg);
4831
4832           /* We want to close the noreorder block as soon as possible, so
4833              that later insns are available for delay slot filling.  */
4834           --mips_opts.noreorder;
4835           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4836                        "c", 7);
4837         }
4838       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4839       return;
4840
4841     case M_DLA_AB:
4842       dbl = 1;
4843     case M_LA_AB:
4844       /* Load the address of a symbol into a register.  If breg is not
4845          zero, we then add a base register to it.  */
4846
4847       if (dbl && HAVE_32BIT_GPRS)
4848         as_warn (_("dla used to load 32-bit register"));
4849
4850       if (! dbl && HAVE_64BIT_OBJECTS)
4851         as_warn (_("la used to load 64-bit address"));
4852
4853       if (offset_expr.X_op == O_constant
4854           && offset_expr.X_add_number >= -0x8000
4855           && offset_expr.X_add_number < 0x8000)
4856         {
4857           macro_build ((char *) NULL, &icnt, &offset_expr,
4858                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" :
4859                        HAVE_NEWABI ? "addi" : "addiu",
4860                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4861           return;
4862         }
4863
4864       if (treg == breg)
4865         {
4866           tempreg = AT;
4867           used_at = 1;
4868         }
4869       else
4870         {
4871           tempreg = treg;
4872           used_at = 0;
4873         }
4874
4875       /* When generating embedded PIC code, we permit expressions of
4876          the form
4877            la   $treg,foo-bar
4878            la   $treg,foo-bar($breg)
4879          where bar is an address in the current section.  These are used
4880          when getting the addresses of functions.  We don't permit
4881          X_add_number to be non-zero, because if the symbol is
4882          external the relaxing code needs to know that any addend is
4883          purely the offset to X_op_symbol.  */
4884       if (mips_pic == EMBEDDED_PIC
4885           && offset_expr.X_op == O_subtract
4886           && (symbol_constant_p (offset_expr.X_op_symbol)
4887               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4888               : (symbol_equated_p (offset_expr.X_op_symbol)
4889                  && (S_GET_SEGMENT
4890                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4891                       ->X_add_symbol)
4892                      == now_seg)))
4893           && (offset_expr.X_add_number == 0
4894               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4895         {
4896           if (breg == 0)
4897             {
4898               tempreg = treg;
4899               used_at = 0;
4900               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4901                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4902             }
4903           else
4904             {
4905               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4906                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4907               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4908                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4909                            "d,v,t", tempreg, tempreg, breg);
4910             }
4911           macro_build ((char *) NULL, &icnt, &offset_expr,
4912                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4913                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4914           if (! used_at)
4915             return;
4916           break;
4917         }
4918
4919       if (offset_expr.X_op != O_symbol
4920           && offset_expr.X_op != O_constant)
4921         {
4922           as_bad (_("expression too complex"));
4923           offset_expr.X_op = O_constant;
4924         }
4925
4926       if (offset_expr.X_op == O_constant)
4927         load_register (&icnt, tempreg, &offset_expr,
4928                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4929                         ? (dbl || HAVE_64BIT_ADDRESSES)
4930                         : HAVE_64BIT_ADDRESSES));
4931       else if (mips_pic == NO_PIC)
4932         {
4933           /* If this is a reference to a GP relative symbol, we want
4934                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4935              Otherwise we want
4936                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4937                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4938              If we have a constant, we need two instructions anyhow,
4939              so we may as well always use the latter form.
4940
4941             With 64bit address space and a usable $at we want
4942               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4943               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4944               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4945               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4946               dsll32    $tempreg,0
4947               daddu     $tempreg,$tempreg,$at
4948
4949             If $at is already in use, we use a path which is suboptimal
4950             on superscalar processors.
4951               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4952               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4953               dsll      $tempreg,16
4954               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4955               dsll      $tempreg,16
4956               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4957           */
4958           char *p = NULL;
4959           if (HAVE_64BIT_ADDRESSES)
4960             {
4961               /* We don't do GP optimization for now because RELAX_ENCODE can't
4962                  hold the data for such large chunks.  */
4963
4964               if (used_at == 0 && ! mips_opts.noat)
4965                 {
4966                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4967                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4968                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4969                                AT, (int) BFD_RELOC_HI16_S);
4970                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4971                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4972                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4973                                AT, AT, (int) BFD_RELOC_LO16);
4974                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4975                                "d,w,<", tempreg, tempreg, 0);
4976                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4977                                "d,v,t", tempreg, tempreg, AT);
4978                   used_at = 1;
4979                 }
4980               else
4981                 {
4982                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4983                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4984                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4985                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4986                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4987                                tempreg, tempreg, 16);
4988                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4989                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4990                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4991                                tempreg, tempreg, 16);
4992                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4993                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4994                 }
4995             }
4996           else
4997             {
4998               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4999                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
5000                 {
5001                   frag_grow (20);
5002                   macro_build ((char *) NULL, &icnt, &offset_expr,
5003                                HAVE_NEWABI ? "addi" : "addiu",
5004                                "t,r,j", tempreg, mips_gp_register,
5005                                (int) BFD_RELOC_GPREL16);
5006                   p = frag_var (rs_machine_dependent, 8, 0,
5007                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5008                                               mips_opts.warn_about_macros),
5009                                 offset_expr.X_add_symbol, 0, NULL);
5010                 }
5011               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5012               if (p != NULL)
5013                 p += 4;
5014               macro_build (p, &icnt, &offset_expr,
5015                            HAVE_NEWABI ? "addi" : "addiu",
5016                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5017             }
5018         }
5019       else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5020         {
5021           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5022
5023           /* If this is a reference to an external symbol, and there
5024              is no constant, we want
5025                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5026              or if tempreg is PIC_CALL_REG
5027                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5028              For a local symbol, we want
5029                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5030                nop
5031                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5032
5033              If we have a small constant, and this is a reference to
5034              an external symbol, we want
5035                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5036                nop
5037                addiu    $tempreg,$tempreg,<constant>
5038              For a local symbol, we want the same instruction
5039              sequence, but we output a BFD_RELOC_LO16 reloc on the
5040              addiu instruction.
5041
5042              If we have a large constant, and this is a reference to
5043              an external symbol, we want
5044                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5045                lui      $at,<hiconstant>
5046                addiu    $at,$at,<loconstant>
5047                addu     $tempreg,$tempreg,$at
5048              For a local symbol, we want the same instruction
5049              sequence, but we output a BFD_RELOC_LO16 reloc on the
5050              addiu instruction.
5051            */
5052
5053           expr1.X_add_number = offset_expr.X_add_number;
5054           offset_expr.X_add_number = 0;
5055           frag_grow (32);
5056           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5057             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5058           macro_build ((char *) NULL, &icnt, &offset_expr,
5059                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5060                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
5061           if (expr1.X_add_number == 0)
5062             {
5063               int off;
5064               char *p;
5065
5066               if (breg == 0)
5067                 off = 0;
5068               else
5069                 {
5070                   /* We're going to put in an addu instruction using
5071                      tempreg, so we may as well insert the nop right
5072                      now.  */
5073                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5074                                "nop", "");
5075                   off = 4;
5076                 }
5077               p = frag_var (rs_machine_dependent, 8 - off, 0,
5078                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5079                                           (breg == 0
5080                                            ? mips_opts.warn_about_macros
5081                                            : 0)),
5082                             offset_expr.X_add_symbol, 0, NULL);
5083               if (breg == 0)
5084                 {
5085                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5086                   p += 4;
5087                 }
5088               macro_build (p, &icnt, &expr1,
5089                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5090                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5091               /* FIXME: If breg == 0, and the next instruction uses
5092                  $tempreg, then if this variant case is used an extra
5093                  nop will be generated.  */
5094             }
5095           else if (expr1.X_add_number >= -0x8000
5096                    && expr1.X_add_number < 0x8000)
5097             {
5098               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5099                            "nop", "");
5100               macro_build ((char *) NULL, &icnt, &expr1,
5101                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5102                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5103               frag_var (rs_machine_dependent, 0, 0,
5104                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5105                         offset_expr.X_add_symbol, 0, NULL);
5106             }
5107           else
5108             {
5109               int off1;
5110
5111               /* If we are going to add in a base register, and the
5112                  target register and the base register are the same,
5113                  then we are using AT as a temporary register.  Since
5114                  we want to load the constant into AT, we add our
5115                  current AT (from the global offset table) and the
5116                  register into the register now, and pretend we were
5117                  not using a base register.  */
5118               if (breg != treg)
5119                 off1 = 0;
5120               else
5121                 {
5122                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5123                                "nop", "");
5124                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5125                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5126                                "d,v,t", treg, AT, breg);
5127                   breg = 0;
5128                   tempreg = treg;
5129                   off1 = -8;
5130                 }
5131
5132               /* Set mips_optimize around the lui instruction to avoid
5133                  inserting an unnecessary nop after the lw.  */
5134               hold_mips_optimize = mips_optimize;
5135               mips_optimize = 2;
5136               macro_build_lui (NULL, &icnt, &expr1, AT);
5137               mips_optimize = hold_mips_optimize;
5138
5139               macro_build ((char *) NULL, &icnt, &expr1,
5140                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5141                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5142               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5143                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5144                            "d,v,t", tempreg, tempreg, AT);
5145               frag_var (rs_machine_dependent, 0, 0,
5146                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5147                         offset_expr.X_add_symbol, 0, NULL);
5148               used_at = 1;
5149             }
5150         }
5151       else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5152         {
5153           char *p = NULL;
5154           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5155           int adj = 0;
5156
5157           /* If this is a reference to an external, and there is no
5158              constant, or local symbol (*), with or without a
5159              constant, we want
5160                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5161              or if tempreg is PIC_CALL_REG
5162                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5163
5164              If we have a small constant, and this is a reference to
5165              an external symbol, we want
5166                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5167                addiu    $tempreg,$tempreg,<constant>
5168
5169              If we have a large constant, and this is a reference to
5170              an external symbol, we want
5171                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5172                lui      $at,<hiconstant>
5173                addiu    $at,$at,<loconstant>
5174                addu     $tempreg,$tempreg,$at
5175
5176              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5177              local symbols, even though it introduces an additional
5178              instruction.  */
5179
5180           frag_grow (28);
5181           if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
5182             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5183           if (offset_expr.X_add_number)
5184             {
5185               frag_now->tc_frag_data.tc_fr_offset =
5186                 expr1.X_add_number = offset_expr.X_add_number;
5187               offset_expr.X_add_number = 0;
5188
5189               macro_build ((char *) NULL, &icnt, &offset_expr,
5190                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5191                            "t,o(b)", tempreg, lw_reloc_type,
5192                            mips_gp_register);
5193
5194               if (expr1.X_add_number >= -0x8000
5195                   && expr1.X_add_number < 0x8000)
5196                 {
5197                   macro_build ((char *) NULL, &icnt, &expr1,
5198                                HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5199                                "t,r,j", tempreg, tempreg,
5200                                (int) BFD_RELOC_LO16);
5201                   p = frag_var (rs_machine_dependent, 4, 0,
5202                                 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5203                                 offset_expr.X_add_symbol, 0, NULL);
5204                 }
5205               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5206                 {
5207                   int dreg;
5208
5209                   /* If we are going to add in a base register, and the
5210                      target register and the base register are the same,
5211                      then we are using AT as a temporary register.  Since
5212                      we want to load the constant into AT, we add our
5213                      current AT (from the global offset table) and the
5214                      register into the register now, and pretend we were
5215                      not using a base register.  */
5216                   if (breg != treg)
5217                     dreg = tempreg;
5218                   else
5219                     {
5220                       assert (tempreg == AT);
5221                       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5222                                    HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5223                                    "d,v,t", treg, AT, breg);
5224                       dreg = treg;
5225                       adj = 4;
5226                     }
5227
5228                   macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5229                   macro_build ((char *) NULL, &icnt, &expr1,
5230                                HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5231                                "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5232                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5233                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5234                                "d,v,t", dreg, dreg, AT);
5235
5236                   p = frag_var (rs_machine_dependent, 4 + adj, 0,
5237                                 RELAX_ENCODE (16 + adj, 4 + adj,
5238                                               0, 0, 0, 0),
5239                                 offset_expr.X_add_symbol, 0, NULL);
5240
5241                   used_at = 1;
5242                 }
5243               else
5244                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5245
5246               offset_expr.X_add_number = expr1.X_add_number;
5247
5248               macro_build (p, &icnt, &offset_expr,
5249                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5250                            "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_DISP,
5251                            mips_gp_register);
5252               if (adj)
5253                 {
5254                   macro_build (p + 4, &icnt, (expressionS *) NULL,
5255                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5256                                "d,v,t", treg, tempreg, breg);
5257                   breg = 0;
5258                   tempreg = treg;
5259                 }
5260             }
5261           else
5262             {
5263               macro_build ((char *) NULL, &icnt, &offset_expr,
5264                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5265                            "t,o(b)", tempreg, lw_reloc_type,
5266                            mips_gp_register);
5267               if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5268                 p = frag_var (rs_machine_dependent, 0, 0,
5269                               RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5270                               offset_expr.X_add_symbol, 0, NULL);
5271             }
5272
5273           if (! p)
5274             {
5275               /* To avoid confusion in tc_gen_reloc, we must ensure
5276                  that this does not become a variant frag.  */
5277               frag_wane (frag_now);
5278               frag_new (0);
5279             }
5280         }
5281       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5282         {
5283           int gpdel;
5284           char *p;
5285           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5286           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5287           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5288
5289           /* This is the large GOT case.  If this is a reference to an
5290              external symbol, and there is no constant, we want
5291                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5292                addu     $tempreg,$tempreg,$gp
5293                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5294              or if tempreg is PIC_CALL_REG
5295                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5296                addu     $tempreg,$tempreg,$gp
5297                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5298              For a local symbol, we want
5299                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5300                nop
5301                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5302
5303              If we have a small constant, and this is a reference to
5304              an external symbol, we want
5305                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5306                addu     $tempreg,$tempreg,$gp
5307                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5308                nop
5309                addiu    $tempreg,$tempreg,<constant>
5310              For a local symbol, we want
5311                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5312                nop
5313                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5314
5315              If we have a large constant, and this is a reference to
5316              an external symbol, we want
5317                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5318                addu     $tempreg,$tempreg,$gp
5319                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5320                lui      $at,<hiconstant>
5321                addiu    $at,$at,<loconstant>
5322                addu     $tempreg,$tempreg,$at
5323              For a local symbol, we want
5324                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5325                lui      $at,<hiconstant>
5326                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5327                addu     $tempreg,$tempreg,$at
5328           */
5329
5330           expr1.X_add_number = offset_expr.X_add_number;
5331           offset_expr.X_add_number = 0;
5332           frag_grow (52);
5333           if (reg_needs_delay (mips_gp_register))
5334             gpdel = 4;
5335           else
5336             gpdel = 0;
5337           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5338             {
5339               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5340               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5341             }
5342           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5343                        tempreg, lui_reloc_type);
5344           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5345                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5346                        "d,v,t", tempreg, tempreg, mips_gp_register);
5347           macro_build ((char *) NULL, &icnt, &offset_expr,
5348                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5349                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5350           if (expr1.X_add_number == 0)
5351             {
5352               int off;
5353
5354               if (breg == 0)
5355                 off = 0;
5356               else
5357                 {
5358                   /* We're going to put in an addu instruction using
5359                      tempreg, so we may as well insert the nop right
5360                      now.  */
5361                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5362                                "nop", "");
5363                   off = 4;
5364                 }
5365
5366               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5367                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5368                                           8 + gpdel, 0,
5369                                           (breg == 0
5370                                            ? mips_opts.warn_about_macros
5371                                            : 0)),
5372                             offset_expr.X_add_symbol, 0, NULL);
5373             }
5374           else if (expr1.X_add_number >= -0x8000
5375                    && expr1.X_add_number < 0x8000)
5376             {
5377               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5378                            "nop", "");
5379               macro_build ((char *) NULL, &icnt, &expr1,
5380                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5381                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5382
5383               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5384                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5385                                           (breg == 0
5386                                            ? mips_opts.warn_about_macros
5387                                            : 0)),
5388                             offset_expr.X_add_symbol, 0, NULL);
5389             }
5390           else
5391             {
5392               int adj, dreg;
5393
5394               /* If we are going to add in a base register, and the
5395                  target register and the base register are the same,
5396                  then we are using AT as a temporary register.  Since
5397                  we want to load the constant into AT, we add our
5398                  current AT (from the global offset table) and the
5399                  register into the register now, and pretend we were
5400                  not using a base register.  */
5401               if (breg != treg)
5402                 {
5403                   adj = 0;
5404                   dreg = tempreg;
5405                 }
5406               else
5407                 {
5408                   assert (tempreg == AT);
5409                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5410                                "nop", "");
5411                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5412                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5413                                "d,v,t", treg, AT, breg);
5414                   dreg = treg;
5415                   adj = 8;
5416                 }
5417
5418               /* Set mips_optimize around the lui instruction to avoid
5419                  inserting an unnecessary nop after the lw.  */
5420               hold_mips_optimize = mips_optimize;
5421               mips_optimize = 2;
5422               macro_build_lui (NULL, &icnt, &expr1, AT);
5423               mips_optimize = hold_mips_optimize;
5424
5425               macro_build ((char *) NULL, &icnt, &expr1,
5426                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5427                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5428               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5429                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5430                            "d,v,t", dreg, dreg, AT);
5431
5432               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5433                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5434                                           8 + gpdel, 0,
5435                                           (breg == 0
5436                                            ? mips_opts.warn_about_macros
5437                                            : 0)),
5438                             offset_expr.X_add_symbol, 0, NULL);
5439
5440               used_at = 1;
5441             }
5442
5443           if (gpdel > 0)
5444             {
5445               /* This is needed because this instruction uses $gp, but
5446                  the first instruction on the main stream does not.  */
5447               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5448               p += 4;
5449             }
5450
5451           macro_build (p, &icnt, &offset_expr,
5452                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5453                        "t,o(b)", tempreg,
5454                        local_reloc_type,
5455                        mips_gp_register);
5456           p += 4;
5457           if (expr1.X_add_number >= -0x8000
5458               && expr1.X_add_number < 0x8000)
5459             {
5460               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5461               p += 4;
5462               macro_build (p, &icnt, &expr1,
5463                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5464                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5465               /* FIXME: If add_number is 0, and there was no base
5466                  register, the external symbol case ended with a load,
5467                  so if the symbol turns out to not be external, and
5468                  the next instruction uses tempreg, an unnecessary nop
5469                  will be inserted.  */
5470             }
5471           else
5472             {
5473               if (breg == treg)
5474                 {
5475                   /* We must add in the base register now, as in the
5476                      external symbol case.  */
5477                   assert (tempreg == AT);
5478                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5479                   p += 4;
5480                   macro_build (p, &icnt, (expressionS *) NULL,
5481                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5482                                "d,v,t", treg, AT, breg);
5483                   p += 4;
5484                   tempreg = treg;
5485                   /* We set breg to 0 because we have arranged to add
5486                      it in in both cases.  */
5487                   breg = 0;
5488                 }
5489
5490               macro_build_lui (p, &icnt, &expr1, AT);
5491               p += 4;
5492               macro_build (p, &icnt, &expr1,
5493                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5494                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5495               p += 4;
5496               macro_build (p, &icnt, (expressionS *) NULL,
5497                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5498                            "d,v,t", tempreg, tempreg, AT);
5499               p += 4;
5500             }
5501         }
5502       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5503         {
5504           char *p = NULL;
5505           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5506           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5507           int adj = 0;
5508
5509           /* This is the large GOT case.  If this is a reference to an
5510              external symbol, and there is no constant, we want
5511                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5512                add      $tempreg,$tempreg,$gp
5513                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5514              or if tempreg is PIC_CALL_REG
5515                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5516                add      $tempreg,$tempreg,$gp
5517                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5518
5519              If we have a small constant, and this is a reference to
5520              an external symbol, we want
5521                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5522                add      $tempreg,$tempreg,$gp
5523                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5524                addi     $tempreg,$tempreg,<constant>
5525
5526              If we have a large constant, and this is a reference to
5527              an external symbol, we want
5528                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5529                addu     $tempreg,$tempreg,$gp
5530                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5531                lui      $at,<hiconstant>
5532                addi     $at,$at,<loconstant>
5533                add      $tempreg,$tempreg,$at
5534
5535              If we have NewABI, and we know it's a local symbol, we want
5536                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
5537                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
5538              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
5539
5540           frag_grow (40);
5541
5542           frag_now->tc_frag_data.tc_fr_offset =
5543             expr1.X_add_number = offset_expr.X_add_number;
5544           offset_expr.X_add_number = 0;
5545
5546           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5547             {
5548               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5549               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5550             }
5551           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5552                        tempreg, lui_reloc_type);
5553           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5554                        HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5555                        "d,v,t", tempreg, tempreg, mips_gp_register);
5556           macro_build ((char *) NULL, &icnt, &offset_expr,
5557                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5558                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5559
5560           if (expr1.X_add_number == 0)
5561             {
5562               p = frag_var (rs_machine_dependent, 8, 0,
5563                             RELAX_ENCODE (12, 8, 0, 4, 0,
5564                                           mips_opts.warn_about_macros),
5565                             offset_expr.X_add_symbol, 0, NULL);
5566             }
5567           else if (expr1.X_add_number >= -0x8000
5568                    && expr1.X_add_number < 0x8000)
5569             {
5570               macro_build ((char *) NULL, &icnt, &expr1,
5571                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5572                            "t,r,j", tempreg, tempreg,
5573                            (int) BFD_RELOC_LO16);
5574               p = frag_var (rs_machine_dependent, 8, 0,
5575                             RELAX_ENCODE (16, 8, 0, 4, 0,
5576                                           mips_opts.warn_about_macros),
5577                             offset_expr.X_add_symbol, 0, NULL);
5578             }
5579           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5580             {
5581               int dreg;
5582
5583               /* If we are going to add in a base register, and the
5584                  target register and the base register are the same,
5585                  then we are using AT as a temporary register.  Since
5586                  we want to load the constant into AT, we add our
5587                  current AT (from the global offset table) and the
5588                  register into the register now, and pretend we were
5589                  not using a base register.  */
5590               if (breg != treg)
5591                 dreg = tempreg;
5592               else
5593                 {
5594                   assert (tempreg == AT);
5595                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5596                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5597                                "d,v,t", treg, AT, breg);
5598                   dreg = treg;
5599                   adj = 4;
5600                 }
5601
5602               /* Set mips_optimize around the lui instruction to avoid
5603                  inserting an unnecessary nop after the lw.  */
5604               macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5605               macro_build ((char *) NULL, &icnt, &expr1,
5606                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5607                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5608               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5609                            HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5610                            "d,v,t", dreg, dreg, AT);
5611
5612               p = frag_var (rs_machine_dependent, 8 + adj, 0,
5613                             RELAX_ENCODE (24 + adj, 8 + adj,
5614                                           0, 4, 0,
5615                                           (breg == 0
5616                                            ? mips_opts.warn_about_macros
5617                                            : 0)),
5618                             offset_expr.X_add_symbol, 0, NULL);
5619
5620               used_at = 1;
5621             }
5622           else
5623             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5624
5625           offset_expr.X_add_number = expr1.X_add_number;
5626           macro_build (p, &icnt, &offset_expr,
5627                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5628                        tempreg,
5629                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5630           macro_build (p + 4, &icnt, &offset_expr,
5631                        HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
5632                        tempreg, tempreg, (int) BFD_RELOC_MIPS_GOT_OFST);
5633           if (adj)
5634             {
5635               macro_build (p + 8, &icnt, (expressionS *) NULL,
5636                            HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5637                            "d,v,t", treg, tempreg, breg);
5638               breg = 0;
5639               tempreg = treg;
5640             }
5641         }
5642       else if (mips_pic == EMBEDDED_PIC)
5643         {
5644           /* We use
5645                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5646              */
5647           macro_build ((char *) NULL, &icnt, &offset_expr,
5648                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5649                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5650         }
5651       else
5652         abort ();
5653
5654       if (breg != 0)
5655         {
5656           char *s;
5657
5658           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5659             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" :
5660               HAVE_NEWABI ? "add" : "addu";
5661           else
5662             s = HAVE_64BIT_ADDRESSES ? "daddu" : HAVE_NEWABI ? "add" : "addu";
5663
5664           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5665                        "d,v,t", treg, tempreg, breg);
5666         }
5667
5668       if (! used_at)
5669         return;
5670
5671       break;
5672
5673     case M_J_A:
5674       /* The j instruction may not be used in PIC code, since it
5675          requires an absolute address.  We convert it to a b
5676          instruction.  */
5677       if (mips_pic == NO_PIC)
5678         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5679       else
5680         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5681       return;
5682
5683       /* The jal instructions must be handled as macros because when
5684          generating PIC code they expand to multi-instruction
5685          sequences.  Normally they are simple instructions.  */
5686     case M_JAL_1:
5687       dreg = RA;
5688       /* Fall through.  */
5689     case M_JAL_2:
5690       if (mips_pic == NO_PIC
5691           || mips_pic == EMBEDDED_PIC)
5692         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5693                      "d,s", dreg, sreg);
5694       else if (mips_pic == SVR4_PIC)
5695         {
5696           if (sreg != PIC_CALL_REG)
5697             as_warn (_("MIPS PIC call to register other than $25"));
5698
5699           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5700                        "d,s", dreg, sreg);
5701           if (! HAVE_NEWABI)
5702             {
5703               if (mips_cprestore_offset < 0)
5704                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5705               else
5706                 {
5707                   if (! mips_frame_reg_valid)
5708                     {
5709                       as_warn (_("No .frame pseudo-op used in PIC code"));
5710                       /* Quiet this warning.  */
5711                       mips_frame_reg_valid = 1;
5712                     }
5713                   if (! mips_cprestore_valid)
5714                     {
5715                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5716                       /* Quiet this warning.  */
5717                       mips_cprestore_valid = 1;
5718                     }
5719                   expr1.X_add_number = mips_cprestore_offset;
5720                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5721                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5722                                                 mips_gp_register, mips_frame_reg);
5723                 }
5724             }
5725         }
5726       else
5727         abort ();
5728
5729       return;
5730
5731     case M_JAL_A:
5732       if (mips_pic == NO_PIC)
5733         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5734       else if (mips_pic == SVR4_PIC)
5735         {
5736           char *p;
5737
5738           /* If this is a reference to an external symbol, and we are
5739              using a small GOT, we want
5740                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5741                nop
5742                jalr     $ra,$25
5743                nop
5744                lw       $gp,cprestore($sp)
5745              The cprestore value is set using the .cprestore
5746              pseudo-op.  If we are using a big GOT, we want
5747                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5748                addu     $25,$25,$gp
5749                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5750                nop
5751                jalr     $ra,$25
5752                nop
5753                lw       $gp,cprestore($sp)
5754              If the symbol is not external, we want
5755                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5756                nop
5757                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5758                jalr     $ra,$25
5759                nop
5760                lw $gp,cprestore($sp)
5761
5762              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5763              sequences above, minus nops, unless the symbol is local,
5764              which enables us to use GOT_PAGE/GOT_OFST (big got) or
5765              GOT_DISP.  */
5766           if (HAVE_NEWABI)
5767             {
5768               if (! mips_big_got)
5769                 {
5770                   frag_grow (4);
5771                   macro_build ((char *) NULL, &icnt, &offset_expr,
5772                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5773                                "t,o(b)", PIC_CALL_REG,
5774                                (int) BFD_RELOC_MIPS_CALL16,
5775                                mips_gp_register);
5776                   frag_var (rs_machine_dependent, 0, 0,
5777                             RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5778                             offset_expr.X_add_symbol, 0, NULL);
5779                 }
5780               else
5781                 {
5782                   frag_grow (20);
5783                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5784                                "t,u", PIC_CALL_REG,
5785                                (int) BFD_RELOC_MIPS_CALL_HI16);
5786                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5787                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5788                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5789                                mips_gp_register);
5790                   macro_build ((char *) NULL, &icnt, &offset_expr,
5791                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5792                                "t,o(b)", PIC_CALL_REG,
5793                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5794                   p = frag_var (rs_machine_dependent, 8, 0,
5795                                 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5796                                 offset_expr.X_add_symbol, 0, NULL);
5797                   macro_build (p, &icnt, &offset_expr,
5798                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5799                                PIC_CALL_REG, (int) BFD_RELOC_MIPS_GOT_PAGE,
5800                                mips_gp_register);
5801                   macro_build (p + 4, &icnt, &offset_expr,
5802                                HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5803                                "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5804                                (int) BFD_RELOC_MIPS_GOT_OFST);
5805                 }
5806
5807               macro_build_jalr (icnt, &offset_expr);
5808             }
5809           else
5810             {
5811               frag_grow (40);
5812               if (! mips_big_got)
5813                 {
5814                   macro_build ((char *) NULL, &icnt, &offset_expr,
5815                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5816                                "t,o(b)", PIC_CALL_REG,
5817                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5818                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5819                                "nop", "");
5820                   p = frag_var (rs_machine_dependent, 4, 0,
5821                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5822                                 offset_expr.X_add_symbol, 0, NULL);
5823                 }
5824               else
5825                 {
5826                   int gpdel;
5827
5828                   if (reg_needs_delay (mips_gp_register))
5829                     gpdel = 4;
5830                   else
5831                     gpdel = 0;
5832                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5833                                "t,u", PIC_CALL_REG,
5834                                (int) BFD_RELOC_MIPS_CALL_HI16);
5835                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5836                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5837                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5838                                mips_gp_register);
5839                   macro_build ((char *) NULL, &icnt, &offset_expr,
5840                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5841                                "t,o(b)", PIC_CALL_REG,
5842                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5843                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5844                                "nop", "");
5845                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5846                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5847                                               8 + gpdel, 0, 0),
5848                                 offset_expr.X_add_symbol, 0, NULL);
5849                   if (gpdel > 0)
5850                     {
5851                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5852                       p += 4;
5853                     }
5854                   macro_build (p, &icnt, &offset_expr,
5855                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5856                                "t,o(b)", PIC_CALL_REG,
5857                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5858                   p += 4;
5859                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5860                   p += 4;
5861                 }
5862               macro_build (p, &icnt, &offset_expr,
5863                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5864                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5865                            (int) BFD_RELOC_LO16);
5866               macro_build_jalr (icnt, &offset_expr);
5867
5868               if (mips_cprestore_offset < 0)
5869                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5870               else
5871                 {
5872                   if (! mips_frame_reg_valid)
5873                     {
5874                       as_warn (_("No .frame pseudo-op used in PIC code"));
5875                       /* Quiet this warning.  */
5876                       mips_frame_reg_valid = 1;
5877                     }
5878                   if (! mips_cprestore_valid)
5879                     {
5880                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5881                       /* Quiet this warning.  */
5882                       mips_cprestore_valid = 1;
5883                     }
5884                   if (mips_opts.noreorder)
5885                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5886                                  "nop", "");
5887                   expr1.X_add_number = mips_cprestore_offset;
5888                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5889                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5890                                                 mips_gp_register, mips_frame_reg);
5891                 }
5892             }
5893         }
5894       else if (mips_pic == EMBEDDED_PIC)
5895         {
5896           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5897           /* The linker may expand the call to a longer sequence which
5898              uses $at, so we must break rather than return.  */
5899           break;
5900         }
5901       else
5902         abort ();
5903
5904       return;
5905
5906     case M_LB_AB:
5907       s = "lb";
5908       goto ld;
5909     case M_LBU_AB:
5910       s = "lbu";
5911       goto ld;
5912     case M_LH_AB:
5913       s = "lh";
5914       goto ld;
5915     case M_LHU_AB:
5916       s = "lhu";
5917       goto ld;
5918     case M_LW_AB:
5919       s = "lw";
5920       goto ld;
5921     case M_LWC0_AB:
5922       s = "lwc0";
5923       /* Itbl support may require additional care here.  */
5924       coproc = 1;
5925       goto ld;
5926     case M_LWC1_AB:
5927       s = "lwc1";
5928       /* Itbl support may require additional care here.  */
5929       coproc = 1;
5930       goto ld;
5931     case M_LWC2_AB:
5932       s = "lwc2";
5933       /* Itbl support may require additional care here.  */
5934       coproc = 1;
5935       goto ld;
5936     case M_LWC3_AB:
5937       s = "lwc3";
5938       /* Itbl support may require additional care here.  */
5939       coproc = 1;
5940       goto ld;
5941     case M_LWL_AB:
5942       s = "lwl";
5943       lr = 1;
5944       goto ld;
5945     case M_LWR_AB:
5946       s = "lwr";
5947       lr = 1;
5948       goto ld;
5949     case M_LDC1_AB:
5950       if (mips_arch == CPU_R4650)
5951         {
5952           as_bad (_("opcode not supported on this processor"));
5953           return;
5954         }
5955       s = "ldc1";
5956       /* Itbl support may require additional care here.  */
5957       coproc = 1;
5958       goto ld;
5959     case M_LDC2_AB:
5960       s = "ldc2";
5961       /* Itbl support may require additional care here.  */
5962       coproc = 1;
5963       goto ld;
5964     case M_LDC3_AB:
5965       s = "ldc3";
5966       /* Itbl support may require additional care here.  */
5967       coproc = 1;
5968       goto ld;
5969     case M_LDL_AB:
5970       s = "ldl";
5971       lr = 1;
5972       goto ld;
5973     case M_LDR_AB:
5974       s = "ldr";
5975       lr = 1;
5976       goto ld;
5977     case M_LL_AB:
5978       s = "ll";
5979       goto ld;
5980     case M_LLD_AB:
5981       s = "lld";
5982       goto ld;
5983     case M_LWU_AB:
5984       s = "lwu";
5985     ld:
5986       if (breg == treg || coproc || lr)
5987         {
5988           tempreg = AT;
5989           used_at = 1;
5990         }
5991       else
5992         {
5993           tempreg = treg;
5994           used_at = 0;
5995         }
5996       goto ld_st;
5997     case M_SB_AB:
5998       s = "sb";
5999       goto st;
6000     case M_SH_AB:
6001       s = "sh";
6002       goto st;
6003     case M_SW_AB:
6004       s = "sw";
6005       goto st;
6006     case M_SWC0_AB:
6007       s = "swc0";
6008       /* Itbl support may require additional care here.  */
6009       coproc = 1;
6010       goto st;
6011     case M_SWC1_AB:
6012       s = "swc1";
6013       /* Itbl support may require additional care here.  */
6014       coproc = 1;
6015       goto st;
6016     case M_SWC2_AB:
6017       s = "swc2";
6018       /* Itbl support may require additional care here.  */
6019       coproc = 1;
6020       goto st;
6021     case M_SWC3_AB:
6022       s = "swc3";
6023       /* Itbl support may require additional care here.  */
6024       coproc = 1;
6025       goto st;
6026     case M_SWL_AB:
6027       s = "swl";
6028       goto st;
6029     case M_SWR_AB:
6030       s = "swr";
6031       goto st;
6032     case M_SC_AB:
6033       s = "sc";
6034       goto st;
6035     case M_SCD_AB:
6036       s = "scd";
6037       goto st;
6038     case M_SDC1_AB:
6039       if (mips_arch == CPU_R4650)
6040         {
6041           as_bad (_("opcode not supported on this processor"));
6042           return;
6043         }
6044       s = "sdc1";
6045       coproc = 1;
6046       /* Itbl support may require additional care here.  */
6047       goto st;
6048     case M_SDC2_AB:
6049       s = "sdc2";
6050       /* Itbl support may require additional care here.  */
6051       coproc = 1;
6052       goto st;
6053     case M_SDC3_AB:
6054       s = "sdc3";
6055       /* Itbl support may require additional care here.  */
6056       coproc = 1;
6057       goto st;
6058     case M_SDL_AB:
6059       s = "sdl";
6060       goto st;
6061     case M_SDR_AB:
6062       s = "sdr";
6063     st:
6064       tempreg = AT;
6065       used_at = 1;
6066     ld_st:
6067       /* Itbl support may require additional care here.  */
6068       if (mask == M_LWC1_AB
6069           || mask == M_SWC1_AB
6070           || mask == M_LDC1_AB
6071           || mask == M_SDC1_AB
6072           || mask == M_L_DAB
6073           || mask == M_S_DAB)
6074         fmt = "T,o(b)";
6075       else if (coproc)
6076         fmt = "E,o(b)";
6077       else
6078         fmt = "t,o(b)";
6079
6080       /* For embedded PIC, we allow loads where the offset is calculated
6081          by subtracting a symbol in the current segment from an unknown
6082          symbol, relative to a base register, e.g.:
6083                 <op>    $treg, <sym>-<localsym>($breg)
6084          This is used by the compiler for switch statements.  */
6085       if (mips_pic == EMBEDDED_PIC
6086           && offset_expr.X_op == O_subtract
6087           && (symbol_constant_p (offset_expr.X_op_symbol)
6088               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
6089               : (symbol_equated_p (offset_expr.X_op_symbol)
6090                  && (S_GET_SEGMENT
6091                      (symbol_get_value_expression (offset_expr.X_op_symbol)
6092                       ->X_add_symbol)
6093                      == now_seg)))
6094           && breg != 0
6095           && (offset_expr.X_add_number == 0
6096               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
6097         {
6098           /* For this case, we output the instructions:
6099                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
6100                 addiu   $tempreg,$tempreg,$breg
6101                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
6102              If the relocation would fit entirely in 16 bits, it would be
6103              nice to emit:
6104                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
6105              instead, but that seems quite difficult.  */
6106           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6107                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
6108           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6109                        ((bfd_arch_bits_per_address (stdoutput) == 32
6110                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
6111                         ? HAVE_NEWABI ? "add" : "addu" : "daddu"),
6112                        "d,v,t", tempreg, tempreg, breg);
6113           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6114                        (int) BFD_RELOC_PCREL_LO16, tempreg);
6115           if (! used_at)
6116             return;
6117           break;
6118         }
6119
6120       if (offset_expr.X_op != O_constant
6121           && offset_expr.X_op != O_symbol)
6122         {
6123           as_bad (_("expression too complex"));
6124           offset_expr.X_op = O_constant;
6125         }
6126
6127       /* A constant expression in PIC code can be handled just as it
6128          is in non PIC code.  */
6129       if (mips_pic == NO_PIC
6130           || offset_expr.X_op == O_constant)
6131         {
6132           char *p;
6133
6134           /* If this is a reference to a GP relative symbol, and there
6135              is no base register, we want
6136                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6137              Otherwise, if there is no base register, we want
6138                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6139                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6140              If we have a constant, we need two instructions anyhow,
6141              so we always use the latter form.
6142
6143              If we have a base register, and this is a reference to a
6144              GP relative symbol, we want
6145                addu     $tempreg,$breg,$gp
6146                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6147              Otherwise we want
6148                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6149                addu     $tempreg,$tempreg,$breg
6150                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6151              With a constant we always use the latter case.
6152
6153              With 64bit address space and no base register and $at usable,
6154              we want
6155                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6156                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6157                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6158                dsll32   $tempreg,0
6159                daddu    $tempreg,$at
6160                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6161              If we have a base register, we want
6162                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6163                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6164                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6165                daddu    $at,$breg
6166                dsll32   $tempreg,0
6167                daddu    $tempreg,$at
6168                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6169
6170              Without $at we can't generate the optimal path for superscalar
6171              processors here since this would require two temporary registers.
6172                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6173                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6174                dsll     $tempreg,16
6175                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6176                dsll     $tempreg,16
6177                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6178              If we have a base register, we want
6179                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6180                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6181                dsll     $tempreg,16
6182                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6183                dsll     $tempreg,16
6184                daddu    $tempreg,$tempreg,$breg
6185                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6186
6187              If we have 64-bit addresses, as an optimization, for
6188              addresses which are 32-bit constants (e.g. kseg0/kseg1
6189              addresses) we fall back to the 32-bit address generation
6190              mechanism since it is more efficient.  Note that due to
6191              the signed offset used by memory operations, the 32-bit
6192              range is shifted down by 32768 here.  This code should
6193              probably attempt to generate 64-bit constants more
6194              efficiently in general.
6195
6196              As an extension for architectures with 64-bit registers,
6197              we don't truncate 64-bit addresses given as literal
6198              constants down to 32 bits, to support existing practice
6199              in the mips64 Linux (the kernel), that compiles source
6200              files with -mabi=64, assembling them as o32 or n32 (with
6201              -Wa,-32 or -Wa,-n32).  This is not beautiful, but since
6202              the whole kernel is loaded into a memory region that is
6203              addressible with sign-extended 32-bit addresses, it is
6204              wasteful to compute the upper 32 bits of every
6205              non-literal address, that takes more space and time.
6206              Some day this should probably be implemented as an
6207              assembler option, such that the kernel doesn't have to
6208              use such ugly hacks, even though it will still have to
6209              end up converting the binary to ELF32 for a number of
6210              platforms whose boot loaders don't support ELF64
6211              binaries.  */
6212           if ((offset_expr.X_op != O_constant && HAVE_64BIT_ADDRESSES)
6213               || (offset_expr.X_op == O_constant
6214                   && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)
6215                   && HAVE_64BIT_ADDRESS_CONSTANTS))
6216             {
6217               p = NULL;
6218
6219               /* We don't do GP optimization for now because RELAX_ENCODE can't
6220                  hold the data for such large chunks.  */
6221
6222               if (used_at == 0 && ! mips_opts.noat)
6223                 {
6224                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6225                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6226                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6227                                AT, (int) BFD_RELOC_HI16_S);
6228                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6229                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6230                   if (breg != 0)
6231                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6232                                  "d,v,t", AT, AT, breg);
6233                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
6234                                "d,w,<", tempreg, tempreg, 0);
6235                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6236                                "d,v,t", tempreg, tempreg, AT);
6237                   macro_build (p, &icnt, &offset_expr, s,
6238                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6239                   used_at = 1;
6240                 }
6241               else
6242                 {
6243                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6244                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6245                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6246                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6247                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6248                                "d,w,<", tempreg, tempreg, 16);
6249                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6250                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
6251                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6252                                "d,w,<", tempreg, tempreg, 16);
6253                   if (breg != 0)
6254                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6255                                  "d,v,t", tempreg, tempreg, breg);
6256                   macro_build (p, &icnt, &offset_expr, s,
6257                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6258                 }
6259
6260               return;
6261             }
6262           else if (offset_expr.X_op == O_constant
6263                    && !HAVE_64BIT_ADDRESS_CONSTANTS
6264                    && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6265             as_bad (_("load/store address overflow (max 32 bits)"));
6266
6267           if (breg == 0)
6268             {
6269               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6270                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6271                 p = NULL;
6272               else
6273                 {
6274                   frag_grow (20);
6275                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6276                                treg, (int) BFD_RELOC_GPREL16,
6277                                mips_gp_register);
6278                   p = frag_var (rs_machine_dependent, 8, 0,
6279                                 RELAX_ENCODE (4, 8, 0, 4, 0,
6280                                               (mips_opts.warn_about_macros
6281                                                || (used_at
6282                                                    && mips_opts.noat))),
6283                                 offset_expr.X_add_symbol, 0, NULL);
6284                   used_at = 0;
6285                 }
6286               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6287               if (p != NULL)
6288                 p += 4;
6289               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6290                            (int) BFD_RELOC_LO16, tempreg);
6291             }
6292           else
6293             {
6294               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6295                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6296                 p = NULL;
6297               else
6298                 {
6299                   frag_grow (28);
6300                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6301                                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6302                                ? "add" : "addu" : "daddu",
6303                                "d,v,t", tempreg, breg, mips_gp_register);
6304                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6305                                treg, (int) BFD_RELOC_GPREL16, tempreg);
6306                   p = frag_var (rs_machine_dependent, 12, 0,
6307                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6308                                 offset_expr.X_add_symbol, 0, NULL);
6309                 }
6310               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6311               if (p != NULL)
6312                 p += 4;
6313               macro_build (p, &icnt, (expressionS *) NULL,
6314                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6315                            ? "add" : "addu" : "daddu",
6316                            "d,v,t", tempreg, tempreg, breg);
6317               if (p != NULL)
6318                 p += 4;
6319               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6320                            (int) BFD_RELOC_LO16, tempreg);
6321             }
6322         }
6323       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6324         {
6325           char *p;
6326           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6327
6328           /* If this is a reference to an external symbol, we want
6329                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6330                nop
6331                <op>     $treg,0($tempreg)
6332              Otherwise we want
6333                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6334                nop
6335                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6336                <op>     $treg,0($tempreg)
6337
6338              For NewABI, we want
6339                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6340                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
6341
6342              If there is a base register, we add it to $tempreg before
6343              the <op>.  If there is a constant, we stick it in the
6344              <op> instruction.  We don't handle constants larger than
6345              16 bits, because we have no way to load the upper 16 bits
6346              (actually, we could handle them for the subset of cases
6347              in which we are not using $at).  */
6348           assert (offset_expr.X_op == O_symbol);
6349           if (HAVE_NEWABI)
6350             {
6351               macro_build ((char *) NULL, &icnt, &offset_expr,
6352                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6353                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6354                            mips_gp_register);
6355               if (breg != 0)
6356                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6357                              HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6358                              "d,v,t", tempreg, tempreg, breg);
6359               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6360                            (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6361
6362               if (! used_at)
6363                 return;
6364
6365               break;
6366             }
6367           expr1.X_add_number = offset_expr.X_add_number;
6368           offset_expr.X_add_number = 0;
6369           if (expr1.X_add_number < -0x8000
6370               || expr1.X_add_number >= 0x8000)
6371             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6372           frag_grow (20);
6373           macro_build ((char *) NULL, &icnt, &offset_expr,
6374                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
6375                        (int) lw_reloc_type, mips_gp_register);
6376           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6377           p = frag_var (rs_machine_dependent, 4, 0,
6378                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6379                         offset_expr.X_add_symbol, 0, NULL);
6380           macro_build (p, &icnt, &offset_expr,
6381                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6382                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6383           if (breg != 0)
6384             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6385                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6386                          "d,v,t", tempreg, tempreg, breg);
6387           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6388                        (int) BFD_RELOC_LO16, tempreg);
6389         }
6390       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6391         {
6392           int gpdel;
6393           char *p;
6394
6395           /* If this is a reference to an external symbol, we want
6396                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6397                addu     $tempreg,$tempreg,$gp
6398                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6399                <op>     $treg,0($tempreg)
6400              Otherwise we want
6401                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6402                nop
6403                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6404                <op>     $treg,0($tempreg)
6405              If there is a base register, we add it to $tempreg before
6406              the <op>.  If there is a constant, we stick it in the
6407              <op> instruction.  We don't handle constants larger than
6408              16 bits, because we have no way to load the upper 16 bits
6409              (actually, we could handle them for the subset of cases
6410              in which we are not using $at).  */
6411           assert (offset_expr.X_op == O_symbol);
6412           expr1.X_add_number = offset_expr.X_add_number;
6413           offset_expr.X_add_number = 0;
6414           if (expr1.X_add_number < -0x8000
6415               || expr1.X_add_number >= 0x8000)
6416             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6417           if (reg_needs_delay (mips_gp_register))
6418             gpdel = 4;
6419           else
6420             gpdel = 0;
6421           frag_grow (36);
6422           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6423                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6424           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6425                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6426                        "d,v,t", tempreg, tempreg, mips_gp_register);
6427           macro_build ((char *) NULL, &icnt, &offset_expr,
6428                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6429                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6430                        tempreg);
6431           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6432                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6433                         offset_expr.X_add_symbol, 0, NULL);
6434           if (gpdel > 0)
6435             {
6436               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6437               p += 4;
6438             }
6439           macro_build (p, &icnt, &offset_expr,
6440                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6441                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6442                        mips_gp_register);
6443           p += 4;
6444           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6445           p += 4;
6446           macro_build (p, &icnt, &offset_expr,
6447                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6448                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6449           if (breg != 0)
6450             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6451                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6452                          "d,v,t", tempreg, tempreg, breg);
6453           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6454                        (int) BFD_RELOC_LO16, tempreg);
6455         }
6456       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6457         {
6458           char *p;
6459           int bregsz = breg != 0 ? 4 : 0;
6460
6461           /* If this is a reference to an external symbol, we want
6462                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6463                add      $tempreg,$tempreg,$gp
6464                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6465                <op>     $treg,<ofst>($tempreg)
6466              Otherwise, for local symbols, we want:
6467                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6468                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
6469           assert (offset_expr.X_op == O_symbol);
6470           frag_now->tc_frag_data.tc_fr_offset =
6471             expr1.X_add_number = offset_expr.X_add_number;
6472           offset_expr.X_add_number = 0;
6473           if (expr1.X_add_number < -0x8000
6474               || expr1.X_add_number >= 0x8000)
6475             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6476           frag_grow (36);
6477           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6478                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6479           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6480                        HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6481                        "d,v,t", tempreg, tempreg, mips_gp_register);
6482           macro_build ((char *) NULL, &icnt, &offset_expr,
6483                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6484                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6485                        tempreg);
6486           if (breg != 0)
6487             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6488                          HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6489                          "d,v,t", tempreg, tempreg, breg);
6490           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6491                        (int) BFD_RELOC_LO16, tempreg);
6492
6493           offset_expr.X_add_number = expr1.X_add_number;
6494           p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6495                         RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6496                                       0, 4 + bregsz, 0, 0),
6497                         offset_expr.X_add_symbol, 0, NULL);
6498           macro_build (p, &icnt, &offset_expr,
6499                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6500                        "t,o(b)", tempreg,
6501                        (int) BFD_RELOC_MIPS_GOT_PAGE,
6502                        mips_gp_register);
6503           if (breg != 0)
6504             macro_build (p + 4, &icnt, (expressionS *) NULL,
6505                          HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6506                          "d,v,t", tempreg, tempreg, breg);
6507           macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6508                        (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6509         }
6510       else if (mips_pic == EMBEDDED_PIC)
6511         {
6512           /* If there is no base register, we want
6513                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6514              If there is a base register, we want
6515                addu     $tempreg,$breg,$gp
6516                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6517              */
6518           assert (offset_expr.X_op == O_symbol);
6519           if (breg == 0)
6520             {
6521               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6522                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6523               used_at = 0;
6524             }
6525           else
6526             {
6527               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6528                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6529                            "d,v,t", tempreg, breg, mips_gp_register);
6530               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6531                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6532             }
6533         }
6534       else
6535         abort ();
6536
6537       if (! used_at)
6538         return;
6539
6540       break;
6541
6542     case M_LI:
6543     case M_LI_S:
6544       load_register (&icnt, treg, &imm_expr, 0);
6545       return;
6546
6547     case M_DLI:
6548       load_register (&icnt, treg, &imm_expr, 1);
6549       return;
6550
6551     case M_LI_SS:
6552       if (imm_expr.X_op == O_constant)
6553         {
6554           load_register (&icnt, AT, &imm_expr, 0);
6555           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6556                        "mtc1", "t,G", AT, treg);
6557           break;
6558         }
6559       else
6560         {
6561           assert (offset_expr.X_op == O_symbol
6562                   && strcmp (segment_name (S_GET_SEGMENT
6563                                            (offset_expr.X_add_symbol)),
6564                              ".lit4") == 0
6565                   && offset_expr.X_add_number == 0);
6566           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6567                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6568           return;
6569         }
6570
6571     case M_LI_D:
6572       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6573          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6574          order 32 bits of the value and the low order 32 bits are either
6575          zero or in OFFSET_EXPR.  */
6576       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6577         {
6578           if (HAVE_64BIT_GPRS)
6579             load_register (&icnt, treg, &imm_expr, 1);
6580           else
6581             {
6582               int hreg, lreg;
6583
6584               if (target_big_endian)
6585                 {
6586                   hreg = treg;
6587                   lreg = treg + 1;
6588                 }
6589               else
6590                 {
6591                   hreg = treg + 1;
6592                   lreg = treg;
6593                 }
6594
6595               if (hreg <= 31)
6596                 load_register (&icnt, hreg, &imm_expr, 0);
6597               if (lreg <= 31)
6598                 {
6599                   if (offset_expr.X_op == O_absent)
6600                     move_register (&icnt, lreg, 0);
6601                   else
6602                     {
6603                       assert (offset_expr.X_op == O_constant);
6604                       load_register (&icnt, lreg, &offset_expr, 0);
6605                     }
6606                 }
6607             }
6608           return;
6609         }
6610
6611       /* We know that sym is in the .rdata section.  First we get the
6612          upper 16 bits of the address.  */
6613       if (mips_pic == NO_PIC)
6614         {
6615           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6616         }
6617       else if (mips_pic == SVR4_PIC)
6618         {
6619           macro_build ((char *) NULL, &icnt, &offset_expr,
6620                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6621                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6622                        mips_gp_register);
6623         }
6624       else if (mips_pic == EMBEDDED_PIC)
6625         {
6626           /* For embedded PIC we pick up the entire address off $gp in
6627              a single instruction.  */
6628           macro_build ((char *) NULL, &icnt, &offset_expr,
6629                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6630                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6631           offset_expr.X_op = O_constant;
6632           offset_expr.X_add_number = 0;
6633         }
6634       else
6635         abort ();
6636
6637       /* Now we load the register(s).  */
6638       if (HAVE_64BIT_GPRS)
6639         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6640                      treg, (int) BFD_RELOC_LO16, AT);
6641       else
6642         {
6643           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6644                        treg, (int) BFD_RELOC_LO16, AT);
6645           if (treg != RA)
6646             {
6647               /* FIXME: How in the world do we deal with the possible
6648                  overflow here?  */
6649               offset_expr.X_add_number += 4;
6650               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6651                            treg + 1, (int) BFD_RELOC_LO16, AT);
6652             }
6653         }
6654
6655       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6656          does not become a variant frag.  */
6657       frag_wane (frag_now);
6658       frag_new (0);
6659
6660       break;
6661
6662     case M_LI_DD:
6663       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6664          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6665          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6666          the value and the low order 32 bits are either zero or in
6667          OFFSET_EXPR.  */
6668       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6669         {
6670           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6671           if (HAVE_64BIT_FPRS)
6672             {
6673               assert (HAVE_64BIT_GPRS);
6674               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6675                            "dmtc1", "t,S", AT, treg);
6676             }
6677           else
6678             {
6679               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6680                            "mtc1", "t,G", AT, treg + 1);
6681               if (offset_expr.X_op == O_absent)
6682                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6683                              "mtc1", "t,G", 0, treg);
6684               else
6685                 {
6686                   assert (offset_expr.X_op == O_constant);
6687                   load_register (&icnt, AT, &offset_expr, 0);
6688                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6689                                "mtc1", "t,G", AT, treg);
6690                 }
6691             }
6692           break;
6693         }
6694
6695       assert (offset_expr.X_op == O_symbol
6696               && offset_expr.X_add_number == 0);
6697       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6698       if (strcmp (s, ".lit8") == 0)
6699         {
6700           if (mips_opts.isa != ISA_MIPS1)
6701             {
6702               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6703                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6704                            mips_gp_register);
6705               return;
6706             }
6707           breg = mips_gp_register;
6708           r = BFD_RELOC_MIPS_LITERAL;
6709           goto dob;
6710         }
6711       else
6712         {
6713           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6714           if (mips_pic == SVR4_PIC)
6715             macro_build ((char *) NULL, &icnt, &offset_expr,
6716                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6717                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6718                          mips_gp_register);
6719           else
6720             {
6721               /* FIXME: This won't work for a 64 bit address.  */
6722               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6723             }
6724
6725           if (mips_opts.isa != ISA_MIPS1)
6726             {
6727               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6728                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6729
6730               /* To avoid confusion in tc_gen_reloc, we must ensure
6731                  that this does not become a variant frag.  */
6732               frag_wane (frag_now);
6733               frag_new (0);
6734
6735               break;
6736             }
6737           breg = AT;
6738           r = BFD_RELOC_LO16;
6739           goto dob;
6740         }
6741
6742     case M_L_DOB:
6743       if (mips_arch == CPU_R4650)
6744         {
6745           as_bad (_("opcode not supported on this processor"));
6746           return;
6747         }
6748       /* Even on a big endian machine $fn comes before $fn+1.  We have
6749          to adjust when loading from memory.  */
6750       r = BFD_RELOC_LO16;
6751     dob:
6752       assert (mips_opts.isa == ISA_MIPS1);
6753       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6754                    target_big_endian ? treg + 1 : treg,
6755                    (int) r, breg);
6756       /* FIXME: A possible overflow which I don't know how to deal
6757          with.  */
6758       offset_expr.X_add_number += 4;
6759       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6760                    target_big_endian ? treg : treg + 1,
6761                    (int) r, breg);
6762
6763       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6764          does not become a variant frag.  */
6765       frag_wane (frag_now);
6766       frag_new (0);
6767
6768       if (breg != AT)
6769         return;
6770       break;
6771
6772     case M_L_DAB:
6773       /*
6774        * The MIPS assembler seems to check for X_add_number not
6775        * being double aligned and generating:
6776        *        lui     at,%hi(foo+1)
6777        *        addu    at,at,v1
6778        *        addiu   at,at,%lo(foo+1)
6779        *        lwc1    f2,0(at)
6780        *        lwc1    f3,4(at)
6781        * But, the resulting address is the same after relocation so why
6782        * generate the extra instruction?
6783        */
6784       if (mips_arch == CPU_R4650)
6785         {
6786           as_bad (_("opcode not supported on this processor"));
6787           return;
6788         }
6789       /* Itbl support may require additional care here.  */
6790       coproc = 1;
6791       if (mips_opts.isa != ISA_MIPS1)
6792         {
6793           s = "ldc1";
6794           goto ld;
6795         }
6796
6797       s = "lwc1";
6798       fmt = "T,o(b)";
6799       goto ldd_std;
6800
6801     case M_S_DAB:
6802       if (mips_arch == CPU_R4650)
6803         {
6804           as_bad (_("opcode not supported on this processor"));
6805           return;
6806         }
6807
6808       if (mips_opts.isa != ISA_MIPS1)
6809         {
6810           s = "sdc1";
6811           goto st;
6812         }
6813
6814       s = "swc1";
6815       fmt = "T,o(b)";
6816       /* Itbl support may require additional care here.  */
6817       coproc = 1;
6818       goto ldd_std;
6819
6820     case M_LD_AB:
6821       if (HAVE_64BIT_GPRS)
6822         {
6823           s = "ld";
6824           goto ld;
6825         }
6826
6827       s = "lw";
6828       fmt = "t,o(b)";
6829       goto ldd_std;
6830
6831     case M_SD_AB:
6832       if (HAVE_64BIT_GPRS)
6833         {
6834           s = "sd";
6835           goto st;
6836         }
6837
6838       s = "sw";
6839       fmt = "t,o(b)";
6840
6841     ldd_std:
6842       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6843          loads for the case of doing a pair of loads to simulate an 'ld'.
6844          This is not currently done by the compiler, and assembly coders
6845          writing embedded-pic code can cope.  */
6846
6847       if (offset_expr.X_op != O_symbol
6848           && offset_expr.X_op != O_constant)
6849         {
6850           as_bad (_("expression too complex"));
6851           offset_expr.X_op = O_constant;
6852         }
6853
6854       /* Even on a big endian machine $fn comes before $fn+1.  We have
6855          to adjust when loading from memory.  We set coproc if we must
6856          load $fn+1 first.  */
6857       /* Itbl support may require additional care here.  */
6858       if (! target_big_endian)
6859         coproc = 0;
6860
6861       if (mips_pic == NO_PIC
6862           || offset_expr.X_op == O_constant)
6863         {
6864           char *p;
6865
6866           /* If this is a reference to a GP relative symbol, we want
6867                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6868                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6869              If we have a base register, we use this
6870                addu     $at,$breg,$gp
6871                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6872                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6873              If this is not a GP relative symbol, we want
6874                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6875                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6876                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6877              If there is a base register, we add it to $at after the
6878              lui instruction.  If there is a constant, we always use
6879              the last case.  */
6880           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6881               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6882             {
6883               p = NULL;
6884               used_at = 1;
6885             }
6886           else
6887             {
6888               int off;
6889
6890               if (breg == 0)
6891                 {
6892                   frag_grow (28);
6893                   tempreg = mips_gp_register;
6894                   off = 0;
6895                   used_at = 0;
6896                 }
6897               else
6898                 {
6899                   frag_grow (36);
6900                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6901                                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6902                                ? "add" : "addu" : "daddu",
6903                                "d,v,t", AT, breg, mips_gp_register);
6904                   tempreg = AT;
6905                   off = 4;
6906                   used_at = 1;
6907                 }
6908
6909               /* Itbl support may require additional care here.  */
6910               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6911                            coproc ? treg + 1 : treg,
6912                            (int) BFD_RELOC_GPREL16, tempreg);
6913               offset_expr.X_add_number += 4;
6914
6915               /* Set mips_optimize to 2 to avoid inserting an
6916                  undesired nop.  */
6917               hold_mips_optimize = mips_optimize;
6918               mips_optimize = 2;
6919               /* Itbl support may require additional care here.  */
6920               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6921                            coproc ? treg : treg + 1,
6922                            (int) BFD_RELOC_GPREL16, tempreg);
6923               mips_optimize = hold_mips_optimize;
6924
6925               p = frag_var (rs_machine_dependent, 12 + off, 0,
6926                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6927                                           used_at && mips_opts.noat),
6928                             offset_expr.X_add_symbol, 0, NULL);
6929
6930               /* We just generated two relocs.  When tc_gen_reloc
6931                  handles this case, it will skip the first reloc and
6932                  handle the second.  The second reloc already has an
6933                  extra addend of 4, which we added above.  We must
6934                  subtract it out, and then subtract another 4 to make
6935                  the first reloc come out right.  The second reloc
6936                  will come out right because we are going to add 4 to
6937                  offset_expr when we build its instruction below.
6938
6939                  If we have a symbol, then we don't want to include
6940                  the offset, because it will wind up being included
6941                  when we generate the reloc.  */
6942
6943               if (offset_expr.X_op == O_constant)
6944                 offset_expr.X_add_number -= 8;
6945               else
6946                 {
6947                   offset_expr.X_add_number = -4;
6948                   offset_expr.X_op = O_constant;
6949                 }
6950             }
6951           macro_build_lui (p, &icnt, &offset_expr, AT);
6952           if (p != NULL)
6953             p += 4;
6954           if (breg != 0)
6955             {
6956               macro_build (p, &icnt, (expressionS *) NULL,
6957                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6958                            ? "add" : "addu" : "daddu",
6959                            "d,v,t", AT, breg, AT);
6960               if (p != NULL)
6961                 p += 4;
6962             }
6963           /* Itbl support may require additional care here.  */
6964           macro_build (p, &icnt, &offset_expr, s, fmt,
6965                        coproc ? treg + 1 : treg,
6966                        (int) BFD_RELOC_LO16, AT);
6967           if (p != NULL)
6968             p += 4;
6969           /* FIXME: How do we handle overflow here?  */
6970           offset_expr.X_add_number += 4;
6971           /* Itbl support may require additional care here.  */
6972           macro_build (p, &icnt, &offset_expr, s, fmt,
6973                        coproc ? treg : treg + 1,
6974                        (int) BFD_RELOC_LO16, AT);
6975         }
6976       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6977         {
6978           int off;
6979
6980           /* If this is a reference to an external symbol, we want
6981                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6982                nop
6983                <op>     $treg,0($at)
6984                <op>     $treg+1,4($at)
6985              Otherwise we want
6986                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6987                nop
6988                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6989                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6990              If there is a base register we add it to $at before the
6991              lwc1 instructions.  If there is a constant we include it
6992              in the lwc1 instructions.  */
6993           used_at = 1;
6994           expr1.X_add_number = offset_expr.X_add_number;
6995           offset_expr.X_add_number = 0;
6996           if (expr1.X_add_number < -0x8000
6997               || expr1.X_add_number >= 0x8000 - 4)
6998             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6999           if (breg == 0)
7000             off = 0;
7001           else
7002             off = 4;
7003           frag_grow (24 + off);
7004           macro_build ((char *) NULL, &icnt, &offset_expr,
7005                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
7006                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
7007           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7008           if (breg != 0)
7009             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7010                          HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7011                          ? "add" : "addu" : "daddu",
7012                          "d,v,t", AT, breg, AT);
7013           /* Itbl support may require additional care here.  */
7014           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7015                        coproc ? treg + 1 : treg,
7016                        (int) BFD_RELOC_LO16, AT);
7017           expr1.X_add_number += 4;
7018
7019           /* Set mips_optimize to 2 to avoid inserting an undesired
7020              nop.  */
7021           hold_mips_optimize = mips_optimize;
7022           mips_optimize = 2;
7023           /* Itbl support may require additional care here.  */
7024           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7025                        coproc ? treg : treg + 1,
7026                        (int) BFD_RELOC_LO16, AT);
7027           mips_optimize = hold_mips_optimize;
7028
7029           (void) frag_var (rs_machine_dependent, 0, 0,
7030                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
7031                            offset_expr.X_add_symbol, 0, NULL);
7032         }
7033       else if (mips_pic == SVR4_PIC)
7034         {
7035           int gpdel, off;
7036           char *p;
7037
7038           /* If this is a reference to an external symbol, we want
7039                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
7040                addu     $at,$at,$gp
7041                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
7042                nop
7043                <op>     $treg,0($at)
7044                <op>     $treg+1,4($at)
7045              Otherwise we want
7046                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7047                nop
7048                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
7049                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
7050              If there is a base register we add it to $at before the
7051              lwc1 instructions.  If there is a constant we include it
7052              in the lwc1 instructions.  */
7053           used_at = 1;
7054           expr1.X_add_number = offset_expr.X_add_number;
7055           offset_expr.X_add_number = 0;
7056           if (expr1.X_add_number < -0x8000
7057               || expr1.X_add_number >= 0x8000 - 4)
7058             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7059           if (reg_needs_delay (mips_gp_register))
7060             gpdel = 4;
7061           else
7062             gpdel = 0;
7063           if (breg == 0)
7064             off = 0;
7065           else
7066             off = 4;
7067           frag_grow (56);
7068           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
7069                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
7070           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7071                        HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7072                        ? "add" : "addu" : "daddu",
7073                        "d,v,t", AT, AT, mips_gp_register);
7074           macro_build ((char *) NULL, &icnt, &offset_expr,
7075                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
7076                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
7077           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7078           if (breg != 0)
7079             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7080                          HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7081                          ? "add" : "addu" : "daddu",
7082                          "d,v,t", AT, breg, AT);
7083           /* Itbl support may require additional care here.  */
7084           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7085                        coproc ? treg + 1 : treg,
7086                        (int) BFD_RELOC_LO16, AT);
7087           expr1.X_add_number += 4;
7088
7089           /* Set mips_optimize to 2 to avoid inserting an undesired
7090              nop.  */
7091           hold_mips_optimize = mips_optimize;
7092           mips_optimize = 2;
7093           /* Itbl support may require additional care here.  */
7094           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7095                        coproc ? treg : treg + 1,
7096                        (int) BFD_RELOC_LO16, AT);
7097           mips_optimize = hold_mips_optimize;
7098           expr1.X_add_number -= 4;
7099
7100           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
7101                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
7102                                       8 + gpdel + off, 1, 0),
7103                         offset_expr.X_add_symbol, 0, NULL);
7104           if (gpdel > 0)
7105             {
7106               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7107               p += 4;
7108             }
7109           macro_build (p, &icnt, &offset_expr,
7110                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
7111                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
7112                        mips_gp_register);
7113           p += 4;
7114           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7115           p += 4;
7116           if (breg != 0)
7117             {
7118               macro_build (p, &icnt, (expressionS *) NULL,
7119                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7120                            ? "add" : "addu" : "daddu",
7121                            "d,v,t", AT, breg, AT);
7122               p += 4;
7123             }
7124           /* Itbl support may require additional care here.  */
7125           macro_build (p, &icnt, &expr1, s, fmt,
7126                        coproc ? treg + 1 : treg,
7127                        (int) BFD_RELOC_LO16, AT);
7128           p += 4;
7129           expr1.X_add_number += 4;
7130
7131           /* Set mips_optimize to 2 to avoid inserting an undesired
7132              nop.  */
7133           hold_mips_optimize = mips_optimize;
7134           mips_optimize = 2;
7135           /* Itbl support may require additional care here.  */
7136           macro_build (p, &icnt, &expr1, s, fmt,
7137                        coproc ? treg : treg + 1,
7138                        (int) BFD_RELOC_LO16, AT);
7139           mips_optimize = hold_mips_optimize;
7140         }
7141       else if (mips_pic == EMBEDDED_PIC)
7142         {
7143           /* If there is no base register, we use
7144                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
7145                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
7146              If we have a base register, we use
7147                addu     $at,$breg,$gp
7148                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
7149                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
7150              */
7151           if (breg == 0)
7152             {
7153               tempreg = mips_gp_register;
7154               used_at = 0;
7155             }
7156           else
7157             {
7158               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7159                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7160                            "d,v,t", AT, breg, mips_gp_register);
7161               tempreg = AT;
7162               used_at = 1;
7163             }
7164
7165           /* Itbl support may require additional care here.  */
7166           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7167                        coproc ? treg + 1 : treg,
7168                        (int) BFD_RELOC_GPREL16, tempreg);
7169           offset_expr.X_add_number += 4;
7170           /* Itbl support may require additional care here.  */
7171           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7172                        coproc ? treg : treg + 1,
7173                        (int) BFD_RELOC_GPREL16, tempreg);
7174         }
7175       else
7176         abort ();
7177
7178       if (! used_at)
7179         return;
7180
7181       break;
7182
7183     case M_LD_OB:
7184       s = "lw";
7185       goto sd_ob;
7186     case M_SD_OB:
7187       s = "sw";
7188     sd_ob:
7189       assert (HAVE_32BIT_ADDRESSES);
7190       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7191                    (int) BFD_RELOC_LO16, breg);
7192       offset_expr.X_add_number += 4;
7193       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7194                    (int) BFD_RELOC_LO16, breg);
7195       return;
7196
7197    /* New code added to support COPZ instructions.
7198       This code builds table entries out of the macros in mip_opcodes.
7199       R4000 uses interlocks to handle coproc delays.
7200       Other chips (like the R3000) require nops to be inserted for delays.
7201
7202       FIXME: Currently, we require that the user handle delays.
7203       In order to fill delay slots for non-interlocked chips,
7204       we must have a way to specify delays based on the coprocessor.
7205       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7206       What are the side-effects of the cop instruction?
7207       What cache support might we have and what are its effects?
7208       Both coprocessor & memory require delays. how long???
7209       What registers are read/set/modified?
7210
7211       If an itbl is provided to interpret cop instructions,
7212       this knowledge can be encoded in the itbl spec.  */
7213
7214     case M_COP0:
7215       s = "c0";
7216       goto copz;
7217     case M_COP1:
7218       s = "c1";
7219       goto copz;
7220     case M_COP2:
7221       s = "c2";
7222       goto copz;
7223     case M_COP3:
7224       s = "c3";
7225     copz:
7226       /* For now we just do C (same as Cz).  The parameter will be
7227          stored in insn_opcode by mips_ip.  */
7228       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
7229                    ip->insn_opcode);
7230       return;
7231
7232     case M_MOVE:
7233       move_register (&icnt, dreg, sreg);
7234       return;
7235
7236 #ifdef LOSING_COMPILER
7237     default:
7238       /* Try and see if this is a new itbl instruction.
7239          This code builds table entries out of the macros in mip_opcodes.
7240          FIXME: For now we just assemble the expression and pass it's
7241          value along as a 32-bit immediate.
7242          We may want to have the assembler assemble this value,
7243          so that we gain the assembler's knowledge of delay slots,
7244          symbols, etc.
7245          Would it be more efficient to use mask (id) here? */
7246       if (itbl_have_entries
7247           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7248         {
7249           s = ip->insn_mo->name;
7250           s2 = "cop3";
7251           coproc = ITBL_DECODE_PNUM (immed_expr);;
7252           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
7253           return;
7254         }
7255       macro2 (ip);
7256       return;
7257     }
7258   if (mips_opts.noat)
7259     as_warn (_("Macro used $at after \".set noat\""));
7260 }
7261
7262 static void
7263 macro2 (ip)
7264      struct mips_cl_insn *ip;
7265 {
7266   register int treg, sreg, dreg, breg;
7267   int tempreg;
7268   int mask;
7269   int icnt = 0;
7270   int used_at;
7271   expressionS expr1;
7272   const char *s;
7273   const char *s2;
7274   const char *fmt;
7275   int likely = 0;
7276   int dbl = 0;
7277   int coproc = 0;
7278   int lr = 0;
7279   int imm = 0;
7280   int off;
7281   offsetT maxnum;
7282   bfd_reloc_code_real_type r;
7283   char *p;
7284
7285   treg = (ip->insn_opcode >> 16) & 0x1f;
7286   dreg = (ip->insn_opcode >> 11) & 0x1f;
7287   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7288   mask = ip->insn_mo->mask;
7289
7290   expr1.X_op = O_constant;
7291   expr1.X_op_symbol = NULL;
7292   expr1.X_add_symbol = NULL;
7293   expr1.X_add_number = 1;
7294
7295   switch (mask)
7296     {
7297 #endif /* LOSING_COMPILER */
7298
7299     case M_DMUL:
7300       dbl = 1;
7301     case M_MUL:
7302       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7303                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7304       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7305                    dreg);
7306       return;
7307
7308     case M_DMUL_I:
7309       dbl = 1;
7310     case M_MUL_I:
7311       /* The MIPS assembler some times generates shifts and adds.  I'm
7312          not trying to be that fancy. GCC should do this for us
7313          anyway.  */
7314       load_register (&icnt, AT, &imm_expr, dbl);
7315       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7316                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
7317       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7318                    dreg);
7319       break;
7320
7321     case M_DMULO_I:
7322       dbl = 1;
7323     case M_MULO_I:
7324       imm = 1;
7325       goto do_mulo;
7326
7327     case M_DMULO:
7328       dbl = 1;
7329     case M_MULO:
7330     do_mulo:
7331       mips_emit_delays (TRUE);
7332       ++mips_opts.noreorder;
7333       mips_any_noreorder = 1;
7334       if (imm)
7335         load_register (&icnt, AT, &imm_expr, dbl);
7336       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7337                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7338       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7339                    dreg);
7340       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7341                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7342       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7343                    AT);
7344       if (mips_trap)
7345         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7346                      "s,t,q", dreg, AT, 6);
7347       else
7348         {
7349           expr1.X_add_number = 8;
7350           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
7351                        AT);
7352           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7353                        0);
7354           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7355                        "c", 6);
7356         }
7357       --mips_opts.noreorder;
7358       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
7359       break;
7360
7361     case M_DMULOU_I:
7362       dbl = 1;
7363     case M_MULOU_I:
7364       imm = 1;
7365       goto do_mulou;
7366
7367     case M_DMULOU:
7368       dbl = 1;
7369     case M_MULOU:
7370     do_mulou:
7371       mips_emit_delays (TRUE);
7372       ++mips_opts.noreorder;
7373       mips_any_noreorder = 1;
7374       if (imm)
7375         load_register (&icnt, AT, &imm_expr, dbl);
7376       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7377                    dbl ? "dmultu" : "multu",
7378                    "s,t", sreg, imm ? AT : treg);
7379       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7380                    AT);
7381       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7382                    dreg);
7383       if (mips_trap)
7384         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7385                      "s,t,q", AT, 0, 6);
7386       else
7387         {
7388           expr1.X_add_number = 8;
7389           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7390           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7391                        0);
7392           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7393                        "c", 6);
7394         }
7395       --mips_opts.noreorder;
7396       break;
7397
7398     case M_DROL:
7399       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7400         {
7401           if (dreg == sreg)
7402             {
7403               tempreg = AT;
7404               used_at = 1;
7405             }
7406           else
7407             {
7408               tempreg = dreg;
7409               used_at = 0;
7410             }
7411           macro_build ((char *) NULL, &icnt, NULL, "dnegu",
7412                        "d,w", tempreg, treg);
7413           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7414                        "d,t,s", dreg, sreg, tempreg);
7415           if (used_at)
7416             break;
7417           return;
7418         }
7419       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7420                    "d,v,t", AT, 0, treg);
7421       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7422                    "d,t,s", AT, sreg, AT);
7423       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7424                    "d,t,s", dreg, sreg, treg);
7425       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7426                    "d,v,t", dreg, dreg, AT);
7427       break;
7428
7429     case M_ROL:
7430       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7431         {
7432           if (dreg == sreg)
7433             {
7434               tempreg = AT;
7435               used_at = 1;
7436             }
7437           else
7438             {
7439               tempreg = dreg;
7440               used_at = 0;
7441             }
7442           macro_build ((char *) NULL, &icnt, NULL, "negu",
7443                        "d,w", tempreg, treg);
7444           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7445                        "d,t,s", dreg, sreg, tempreg);
7446           if (used_at)
7447             break;
7448           return;
7449         }
7450       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7451                    "d,v,t", AT, 0, treg);
7452       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7453                    "d,t,s", AT, sreg, AT);
7454       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7455                    "d,t,s", dreg, sreg, treg);
7456       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7457                    "d,v,t", dreg, dreg, AT);
7458       break;
7459
7460     case M_DROL_I:
7461       {
7462         unsigned int rot;
7463         char *l, *r;
7464
7465         if (imm_expr.X_op != O_constant)
7466           as_bad (_("Improper rotate count"));
7467         rot = imm_expr.X_add_number & 0x3f;
7468         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7469           {
7470             rot = (64 - rot) & 0x3f;
7471             if (rot >= 32)
7472               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7473                            "d,w,<", dreg, sreg, rot - 32);
7474             else
7475               macro_build ((char *) NULL, &icnt, NULL, "dror",
7476                            "d,w,<", dreg, sreg, rot);
7477             return;
7478           }
7479         if (rot == 0)
7480           {
7481             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7482                          "d,w,<", dreg, sreg, 0);
7483             return;
7484           }
7485         l = (rot < 0x20) ? "dsll" : "dsll32";
7486         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7487         rot &= 0x1f;
7488         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7489                      "d,w,<", AT, sreg, rot);
7490         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7491                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7492         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7493                      "d,v,t", dreg, dreg, AT);
7494       }
7495       break;
7496
7497     case M_ROL_I:
7498       {
7499         unsigned int rot;
7500
7501         if (imm_expr.X_op != O_constant)
7502           as_bad (_("Improper rotate count"));
7503         rot = imm_expr.X_add_number & 0x1f;
7504         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7505           {
7506             macro_build ((char *) NULL, &icnt, NULL, "ror",
7507                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7508             return;
7509           }
7510         if (rot == 0)
7511           {
7512             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7513                          "d,w,<", dreg, sreg, 0);
7514             return;
7515           }
7516         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7517                      "d,w,<", AT, sreg, rot);
7518         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7519                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7520         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7521                      "d,v,t", dreg, dreg, AT);
7522       }
7523       break;
7524
7525     case M_DROR:
7526       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7527         {
7528           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7529                        "d,t,s", dreg, sreg, treg);
7530           return;
7531         }
7532       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7533                    "d,v,t", AT, 0, treg);
7534       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7535                    "d,t,s", AT, sreg, AT);
7536       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7537                    "d,t,s", dreg, sreg, treg);
7538       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7539                    "d,v,t", dreg, dreg, AT);
7540       break;
7541
7542     case M_ROR:
7543       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7544         {
7545           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7546                        "d,t,s", dreg, sreg, treg);
7547           return;
7548         }
7549       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7550                    "d,v,t", AT, 0, treg);
7551       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7552                    "d,t,s", AT, sreg, AT);
7553       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7554                    "d,t,s", dreg, sreg, treg);
7555       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7556                    "d,v,t", dreg, dreg, AT);
7557       break;
7558
7559     case M_DROR_I:
7560       {
7561         unsigned int rot;
7562         char *l, *r;
7563
7564         if (imm_expr.X_op != O_constant)
7565           as_bad (_("Improper rotate count"));
7566         rot = imm_expr.X_add_number & 0x3f;
7567         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7568           {
7569             if (rot >= 32)
7570               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7571                            "d,w,<", dreg, sreg, rot - 32);
7572             else
7573               macro_build ((char *) NULL, &icnt, NULL, "dror",
7574                            "d,w,<", dreg, sreg, rot);
7575             return;
7576           }
7577         if (rot == 0)
7578           {
7579             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7580                          "d,w,<", dreg, sreg, 0);
7581             return;
7582           }
7583         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7584         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7585         rot &= 0x1f;
7586         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7587                      "d,w,<", AT, sreg, rot);
7588         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7589                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7590         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7591                      "d,v,t", dreg, dreg, AT);
7592       }
7593       break;
7594
7595     case M_ROR_I:
7596       {
7597         unsigned int rot;
7598
7599         if (imm_expr.X_op != O_constant)
7600           as_bad (_("Improper rotate count"));
7601         rot = imm_expr.X_add_number & 0x1f;
7602         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7603           {
7604             macro_build ((char *) NULL, &icnt, NULL, "ror",
7605                          "d,w,<", dreg, sreg, rot);
7606             return;
7607           }
7608         if (rot == 0)
7609           {
7610             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7611                          "d,w,<", dreg, sreg, 0);
7612             return;
7613           }
7614         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7615                      "d,w,<", AT, sreg, rot);
7616         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7617                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7618         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7619                      "d,v,t", dreg, dreg, AT);
7620       }
7621       break;
7622
7623     case M_S_DOB:
7624       if (mips_arch == CPU_R4650)
7625         {
7626           as_bad (_("opcode not supported on this processor"));
7627           return;
7628         }
7629       assert (mips_opts.isa == ISA_MIPS1);
7630       /* Even on a big endian machine $fn comes before $fn+1.  We have
7631          to adjust when storing to memory.  */
7632       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7633                    target_big_endian ? treg + 1 : treg,
7634                    (int) BFD_RELOC_LO16, breg);
7635       offset_expr.X_add_number += 4;
7636       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7637                    target_big_endian ? treg : treg + 1,
7638                    (int) BFD_RELOC_LO16, breg);
7639       return;
7640
7641     case M_SEQ:
7642       if (sreg == 0)
7643         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7644                      treg, (int) BFD_RELOC_LO16);
7645       else if (treg == 0)
7646         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7647                      sreg, (int) BFD_RELOC_LO16);
7648       else
7649         {
7650           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7651                        "d,v,t", dreg, sreg, treg);
7652           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7653                        dreg, (int) BFD_RELOC_LO16);
7654         }
7655       return;
7656
7657     case M_SEQ_I:
7658       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7659         {
7660           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7661                        sreg, (int) BFD_RELOC_LO16);
7662           return;
7663         }
7664       if (sreg == 0)
7665         {
7666           as_warn (_("Instruction %s: result is always false"),
7667                    ip->insn_mo->name);
7668           move_register (&icnt, dreg, 0);
7669           return;
7670         }
7671       if (imm_expr.X_op == O_constant
7672           && imm_expr.X_add_number >= 0
7673           && imm_expr.X_add_number < 0x10000)
7674         {
7675           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7676                        sreg, (int) BFD_RELOC_LO16);
7677           used_at = 0;
7678         }
7679       else if (imm_expr.X_op == O_constant
7680                && imm_expr.X_add_number > -0x8000
7681                && imm_expr.X_add_number < 0)
7682         {
7683           imm_expr.X_add_number = -imm_expr.X_add_number;
7684           macro_build ((char *) NULL, &icnt, &imm_expr,
7685                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7686                        "t,r,j", dreg, sreg,
7687                        (int) BFD_RELOC_LO16);
7688           used_at = 0;
7689         }
7690       else
7691         {
7692           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7693           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7694                        "d,v,t", dreg, sreg, AT);
7695           used_at = 1;
7696         }
7697       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7698                    (int) BFD_RELOC_LO16);
7699       if (used_at)
7700         break;
7701       return;
7702
7703     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7704       s = "slt";
7705       goto sge;
7706     case M_SGEU:
7707       s = "sltu";
7708     sge:
7709       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7710                    dreg, sreg, treg);
7711       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7712                    (int) BFD_RELOC_LO16);
7713       return;
7714
7715     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7716     case M_SGEU_I:
7717       if (imm_expr.X_op == O_constant
7718           && imm_expr.X_add_number >= -0x8000
7719           && imm_expr.X_add_number < 0x8000)
7720         {
7721           macro_build ((char *) NULL, &icnt, &imm_expr,
7722                        mask == M_SGE_I ? "slti" : "sltiu",
7723                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7724           used_at = 0;
7725         }
7726       else
7727         {
7728           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7729           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7730                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7731                        AT);
7732           used_at = 1;
7733         }
7734       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7735                    (int) BFD_RELOC_LO16);
7736       if (used_at)
7737         break;
7738       return;
7739
7740     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7741       s = "slt";
7742       goto sgt;
7743     case M_SGTU:
7744       s = "sltu";
7745     sgt:
7746       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7747                    dreg, treg, sreg);
7748       return;
7749
7750     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7751       s = "slt";
7752       goto sgti;
7753     case M_SGTU_I:
7754       s = "sltu";
7755     sgti:
7756       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7757       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7758                    dreg, AT, sreg);
7759       break;
7760
7761     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7762       s = "slt";
7763       goto sle;
7764     case M_SLEU:
7765       s = "sltu";
7766     sle:
7767       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7768                    dreg, treg, sreg);
7769       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7770                    (int) BFD_RELOC_LO16);
7771       return;
7772
7773     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7774       s = "slt";
7775       goto slei;
7776     case M_SLEU_I:
7777       s = "sltu";
7778     slei:
7779       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7780       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7781                    dreg, AT, sreg);
7782       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7783                    (int) BFD_RELOC_LO16);
7784       break;
7785
7786     case M_SLT_I:
7787       if (imm_expr.X_op == O_constant
7788           && imm_expr.X_add_number >= -0x8000
7789           && imm_expr.X_add_number < 0x8000)
7790         {
7791           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7792                        dreg, sreg, (int) BFD_RELOC_LO16);
7793           return;
7794         }
7795       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7796       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7797                    dreg, sreg, AT);
7798       break;
7799
7800     case M_SLTU_I:
7801       if (imm_expr.X_op == O_constant
7802           && imm_expr.X_add_number >= -0x8000
7803           && imm_expr.X_add_number < 0x8000)
7804         {
7805           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7806                        dreg, sreg, (int) BFD_RELOC_LO16);
7807           return;
7808         }
7809       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7810       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7811                    "d,v,t", dreg, sreg, AT);
7812       break;
7813
7814     case M_SNE:
7815       if (sreg == 0)
7816         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7817                      "d,v,t", dreg, 0, treg);
7818       else if (treg == 0)
7819         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7820                      "d,v,t", dreg, 0, sreg);
7821       else
7822         {
7823           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7824                        "d,v,t", dreg, sreg, treg);
7825           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7826                        "d,v,t", dreg, 0, dreg);
7827         }
7828       return;
7829
7830     case M_SNE_I:
7831       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7832         {
7833           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7834                        "d,v,t", dreg, 0, sreg);
7835           return;
7836         }
7837       if (sreg == 0)
7838         {
7839           as_warn (_("Instruction %s: result is always true"),
7840                    ip->insn_mo->name);
7841           macro_build ((char *) NULL, &icnt, &expr1,
7842                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7843                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7844           return;
7845         }
7846       if (imm_expr.X_op == O_constant
7847           && imm_expr.X_add_number >= 0
7848           && imm_expr.X_add_number < 0x10000)
7849         {
7850           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7851                        dreg, sreg, (int) BFD_RELOC_LO16);
7852           used_at = 0;
7853         }
7854       else if (imm_expr.X_op == O_constant
7855                && imm_expr.X_add_number > -0x8000
7856                && imm_expr.X_add_number < 0)
7857         {
7858           imm_expr.X_add_number = -imm_expr.X_add_number;
7859           macro_build ((char *) NULL, &icnt, &imm_expr,
7860                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7861                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7862           used_at = 0;
7863         }
7864       else
7865         {
7866           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7867           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7868                        "d,v,t", dreg, sreg, AT);
7869           used_at = 1;
7870         }
7871       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7872                    "d,v,t", dreg, 0, dreg);
7873       if (used_at)
7874         break;
7875       return;
7876
7877     case M_DSUB_I:
7878       dbl = 1;
7879     case M_SUB_I:
7880       if (imm_expr.X_op == O_constant
7881           && imm_expr.X_add_number > -0x8000
7882           && imm_expr.X_add_number <= 0x8000)
7883         {
7884           imm_expr.X_add_number = -imm_expr.X_add_number;
7885           macro_build ((char *) NULL, &icnt, &imm_expr,
7886                        dbl ? "daddi" : "addi",
7887                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7888           return;
7889         }
7890       load_register (&icnt, AT, &imm_expr, dbl);
7891       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7892                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7893       break;
7894
7895     case M_DSUBU_I:
7896       dbl = 1;
7897     case M_SUBU_I:
7898       if (imm_expr.X_op == O_constant
7899           && imm_expr.X_add_number > -0x8000
7900           && imm_expr.X_add_number <= 0x8000)
7901         {
7902           imm_expr.X_add_number = -imm_expr.X_add_number;
7903           macro_build ((char *) NULL, &icnt, &imm_expr,
7904                        dbl ? "daddiu" : "addiu",
7905                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7906           return;
7907         }
7908       load_register (&icnt, AT, &imm_expr, dbl);
7909       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7910                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7911       break;
7912
7913     case M_TEQ_I:
7914       s = "teq";
7915       goto trap;
7916     case M_TGE_I:
7917       s = "tge";
7918       goto trap;
7919     case M_TGEU_I:
7920       s = "tgeu";
7921       goto trap;
7922     case M_TLT_I:
7923       s = "tlt";
7924       goto trap;
7925     case M_TLTU_I:
7926       s = "tltu";
7927       goto trap;
7928     case M_TNE_I:
7929       s = "tne";
7930     trap:
7931       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7932       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7933                    AT);
7934       break;
7935
7936     case M_TRUNCWS:
7937     case M_TRUNCWD:
7938       assert (mips_opts.isa == ISA_MIPS1);
7939       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7940       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7941
7942       /*
7943        * Is the double cfc1 instruction a bug in the mips assembler;
7944        * or is there a reason for it?
7945        */
7946       mips_emit_delays (TRUE);
7947       ++mips_opts.noreorder;
7948       mips_any_noreorder = 1;
7949       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7950                    treg, RA);
7951       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7952                    treg, RA);
7953       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7954       expr1.X_add_number = 3;
7955       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7956                    (int) BFD_RELOC_LO16);
7957       expr1.X_add_number = 2;
7958       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7959                      (int) BFD_RELOC_LO16);
7960       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7961                    AT, RA);
7962       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7963       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7964               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7965       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7966                    treg, RA);
7967       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7968       --mips_opts.noreorder;
7969       break;
7970
7971     case M_ULH:
7972       s = "lb";
7973       goto ulh;
7974     case M_ULHU:
7975       s = "lbu";
7976     ulh:
7977       if (offset_expr.X_add_number >= 0x7fff)
7978         as_bad (_("operand overflow"));
7979       if (! target_big_endian)
7980         ++offset_expr.X_add_number;
7981       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7982                    (int) BFD_RELOC_LO16, breg);
7983       if (! target_big_endian)
7984         --offset_expr.X_add_number;
7985       else
7986         ++offset_expr.X_add_number;
7987       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7988                    (int) BFD_RELOC_LO16, breg);
7989       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7990                    AT, AT, 8);
7991       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7992                    treg, treg, AT);
7993       break;
7994
7995     case M_ULD:
7996       s = "ldl";
7997       s2 = "ldr";
7998       off = 7;
7999       goto ulw;
8000     case M_ULW:
8001       s = "lwl";
8002       s2 = "lwr";
8003       off = 3;
8004     ulw:
8005       if (offset_expr.X_add_number >= 0x8000 - off)
8006         as_bad (_("operand overflow"));
8007       if (treg != breg)
8008         tempreg = treg;
8009       else
8010         tempreg = AT;
8011       if (! target_big_endian)
8012         offset_expr.X_add_number += off;
8013       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
8014                    (int) BFD_RELOC_LO16, breg);
8015       if (! target_big_endian)
8016         offset_expr.X_add_number -= off;
8017       else
8018         offset_expr.X_add_number += off;
8019       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
8020                    (int) BFD_RELOC_LO16, breg);
8021
8022       /* If necessary, move the result in tempreg the final destination.  */
8023       if (treg == tempreg)
8024         return;
8025       /* Protect second load's delay slot.  */
8026       if (!gpr_interlocks)
8027         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8028       move_register (&icnt, treg, tempreg);
8029       break;
8030
8031     case M_ULD_A:
8032       s = "ldl";
8033       s2 = "ldr";
8034       off = 7;
8035       goto ulwa;
8036     case M_ULW_A:
8037       s = "lwl";
8038       s2 = "lwr";
8039       off = 3;
8040     ulwa:
8041       used_at = 1;
8042       load_address (&icnt, AT, &offset_expr, &used_at);
8043       if (breg != 0)
8044         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8045                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8046                      ? "add" : "addu" : "daddu",
8047                      "d,v,t", AT, AT, breg);
8048       if (! target_big_endian)
8049         expr1.X_add_number = off;
8050       else
8051         expr1.X_add_number = 0;
8052       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8053                    (int) BFD_RELOC_LO16, AT);
8054       if (! target_big_endian)
8055         expr1.X_add_number = 0;
8056       else
8057         expr1.X_add_number = off;
8058       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8059                    (int) BFD_RELOC_LO16, AT);
8060       break;
8061
8062     case M_ULH_A:
8063     case M_ULHU_A:
8064       used_at = 1;
8065       load_address (&icnt, AT, &offset_expr, &used_at);
8066       if (breg != 0)
8067         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8068                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8069                      ? "add" : "addu" : "daddu",
8070                      "d,v,t", AT, AT, breg);
8071       if (target_big_endian)
8072         expr1.X_add_number = 0;
8073       macro_build ((char *) NULL, &icnt, &expr1,
8074                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
8075                    (int) BFD_RELOC_LO16, AT);
8076       if (target_big_endian)
8077         expr1.X_add_number = 1;
8078       else
8079         expr1.X_add_number = 0;
8080       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8081                    (int) BFD_RELOC_LO16, AT);
8082       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8083                    treg, treg, 8);
8084       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8085                    treg, treg, AT);
8086       break;
8087
8088     case M_USH:
8089       if (offset_expr.X_add_number >= 0x7fff)
8090         as_bad (_("operand overflow"));
8091       if (target_big_endian)
8092         ++offset_expr.X_add_number;
8093       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
8094                    (int) BFD_RELOC_LO16, breg);
8095       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8096                    AT, treg, 8);
8097       if (target_big_endian)
8098         --offset_expr.X_add_number;
8099       else
8100         ++offset_expr.X_add_number;
8101       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
8102                    (int) BFD_RELOC_LO16, breg);
8103       break;
8104
8105     case M_USD:
8106       s = "sdl";
8107       s2 = "sdr";
8108       off = 7;
8109       goto usw;
8110     case M_USW:
8111       s = "swl";
8112       s2 = "swr";
8113       off = 3;
8114     usw:
8115       if (offset_expr.X_add_number >= 0x8000 - off)
8116         as_bad (_("operand overflow"));
8117       if (! target_big_endian)
8118         offset_expr.X_add_number += off;
8119       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
8120                    (int) BFD_RELOC_LO16, breg);
8121       if (! target_big_endian)
8122         offset_expr.X_add_number -= off;
8123       else
8124         offset_expr.X_add_number += off;
8125       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
8126                    (int) BFD_RELOC_LO16, breg);
8127       return;
8128
8129     case M_USD_A:
8130       s = "sdl";
8131       s2 = "sdr";
8132       off = 7;
8133       goto uswa;
8134     case M_USW_A:
8135       s = "swl";
8136       s2 = "swr";
8137       off = 3;
8138     uswa:
8139       used_at = 1;
8140       load_address (&icnt, AT, &offset_expr, &used_at);
8141       if (breg != 0)
8142         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8143                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8144                      ? "add" : "addu" : "daddu",
8145                      "d,v,t", AT, AT, breg);
8146       if (! target_big_endian)
8147         expr1.X_add_number = off;
8148       else
8149         expr1.X_add_number = 0;
8150       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8151                    (int) BFD_RELOC_LO16, AT);
8152       if (! target_big_endian)
8153         expr1.X_add_number = 0;
8154       else
8155         expr1.X_add_number = off;
8156       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8157                    (int) BFD_RELOC_LO16, AT);
8158       break;
8159
8160     case M_USH_A:
8161       used_at = 1;
8162       load_address (&icnt, AT, &offset_expr, &used_at);
8163       if (breg != 0)
8164         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8165                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8166                      ? "add" : "addu" : "daddu",
8167                      "d,v,t", AT, AT, breg);
8168       if (! target_big_endian)
8169         expr1.X_add_number = 0;
8170       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8171                    (int) BFD_RELOC_LO16, AT);
8172       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8173                    treg, treg, 8);
8174       if (! target_big_endian)
8175         expr1.X_add_number = 1;
8176       else
8177         expr1.X_add_number = 0;
8178       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8179                    (int) BFD_RELOC_LO16, AT);
8180       if (! target_big_endian)
8181         expr1.X_add_number = 0;
8182       else
8183         expr1.X_add_number = 1;
8184       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8185                    (int) BFD_RELOC_LO16, AT);
8186       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8187                    treg, treg, 8);
8188       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8189                    treg, treg, AT);
8190       break;
8191
8192     default:
8193       /* FIXME: Check if this is one of the itbl macros, since they
8194          are added dynamically.  */
8195       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
8196       break;
8197     }
8198   if (mips_opts.noat)
8199     as_warn (_("Macro used $at after \".set noat\""));
8200 }
8201
8202 /* Implement macros in mips16 mode.  */
8203
8204 static void
8205 mips16_macro (ip)
8206      struct mips_cl_insn *ip;
8207 {
8208   int mask;
8209   int xreg, yreg, zreg, tmp;
8210   int icnt;
8211   expressionS expr1;
8212   int dbl;
8213   const char *s, *s2, *s3;
8214
8215   mask = ip->insn_mo->mask;
8216
8217   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
8218   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
8219   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
8220
8221   icnt = 0;
8222
8223   expr1.X_op = O_constant;
8224   expr1.X_op_symbol = NULL;
8225   expr1.X_add_symbol = NULL;
8226   expr1.X_add_number = 1;
8227
8228   dbl = 0;
8229
8230   switch (mask)
8231     {
8232     default:
8233       internalError ();
8234
8235     case M_DDIV_3:
8236       dbl = 1;
8237     case M_DIV_3:
8238       s = "mflo";
8239       goto do_div3;
8240     case M_DREM_3:
8241       dbl = 1;
8242     case M_REM_3:
8243       s = "mfhi";
8244     do_div3:
8245       mips_emit_delays (TRUE);
8246       ++mips_opts.noreorder;
8247       mips_any_noreorder = 1;
8248       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8249                    dbl ? "ddiv" : "div",
8250                    "0,x,y", xreg, yreg);
8251       expr1.X_add_number = 2;
8252       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8253       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
8254                    7);
8255
8256       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8257          since that causes an overflow.  We should do that as well,
8258          but I don't see how to do the comparisons without a temporary
8259          register.  */
8260       --mips_opts.noreorder;
8261       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
8262       break;
8263
8264     case M_DIVU_3:
8265       s = "divu";
8266       s2 = "mflo";
8267       goto do_divu3;
8268     case M_REMU_3:
8269       s = "divu";
8270       s2 = "mfhi";
8271       goto do_divu3;
8272     case M_DDIVU_3:
8273       s = "ddivu";
8274       s2 = "mflo";
8275       goto do_divu3;
8276     case M_DREMU_3:
8277       s = "ddivu";
8278       s2 = "mfhi";
8279     do_divu3:
8280       mips_emit_delays (TRUE);
8281       ++mips_opts.noreorder;
8282       mips_any_noreorder = 1;
8283       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
8284                    xreg, yreg);
8285       expr1.X_add_number = 2;
8286       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8287       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
8288                    "6", 7);
8289       --mips_opts.noreorder;
8290       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
8291       break;
8292
8293     case M_DMUL:
8294       dbl = 1;
8295     case M_MUL:
8296       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8297                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
8298       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
8299                    zreg);
8300       return;
8301
8302     case M_DSUBU_I:
8303       dbl = 1;
8304       goto do_subu;
8305     case M_SUBU_I:
8306     do_subu:
8307       if (imm_expr.X_op != O_constant)
8308         as_bad (_("Unsupported large constant"));
8309       imm_expr.X_add_number = -imm_expr.X_add_number;
8310       macro_build ((char *) NULL, &icnt, &imm_expr,
8311                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
8312       break;
8313
8314     case M_SUBU_I_2:
8315       if (imm_expr.X_op != O_constant)
8316         as_bad (_("Unsupported large constant"));
8317       imm_expr.X_add_number = -imm_expr.X_add_number;
8318       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
8319                    "x,k", xreg);
8320       break;
8321
8322     case M_DSUBU_I_2:
8323       if (imm_expr.X_op != O_constant)
8324         as_bad (_("Unsupported large constant"));
8325       imm_expr.X_add_number = -imm_expr.X_add_number;
8326       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
8327                    "y,j", yreg);
8328       break;
8329
8330     case M_BEQ:
8331       s = "cmp";
8332       s2 = "bteqz";
8333       goto do_branch;
8334     case M_BNE:
8335       s = "cmp";
8336       s2 = "btnez";
8337       goto do_branch;
8338     case M_BLT:
8339       s = "slt";
8340       s2 = "btnez";
8341       goto do_branch;
8342     case M_BLTU:
8343       s = "sltu";
8344       s2 = "btnez";
8345       goto do_branch;
8346     case M_BLE:
8347       s = "slt";
8348       s2 = "bteqz";
8349       goto do_reverse_branch;
8350     case M_BLEU:
8351       s = "sltu";
8352       s2 = "bteqz";
8353       goto do_reverse_branch;
8354     case M_BGE:
8355       s = "slt";
8356       s2 = "bteqz";
8357       goto do_branch;
8358     case M_BGEU:
8359       s = "sltu";
8360       s2 = "bteqz";
8361       goto do_branch;
8362     case M_BGT:
8363       s = "slt";
8364       s2 = "btnez";
8365       goto do_reverse_branch;
8366     case M_BGTU:
8367       s = "sltu";
8368       s2 = "btnez";
8369
8370     do_reverse_branch:
8371       tmp = xreg;
8372       xreg = yreg;
8373       yreg = tmp;
8374
8375     do_branch:
8376       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
8377                    xreg, yreg);
8378       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8379       break;
8380
8381     case M_BEQ_I:
8382       s = "cmpi";
8383       s2 = "bteqz";
8384       s3 = "x,U";
8385       goto do_branch_i;
8386     case M_BNE_I:
8387       s = "cmpi";
8388       s2 = "btnez";
8389       s3 = "x,U";
8390       goto do_branch_i;
8391     case M_BLT_I:
8392       s = "slti";
8393       s2 = "btnez";
8394       s3 = "x,8";
8395       goto do_branch_i;
8396     case M_BLTU_I:
8397       s = "sltiu";
8398       s2 = "btnez";
8399       s3 = "x,8";
8400       goto do_branch_i;
8401     case M_BLE_I:
8402       s = "slti";
8403       s2 = "btnez";
8404       s3 = "x,8";
8405       goto do_addone_branch_i;
8406     case M_BLEU_I:
8407       s = "sltiu";
8408       s2 = "btnez";
8409       s3 = "x,8";
8410       goto do_addone_branch_i;
8411     case M_BGE_I:
8412       s = "slti";
8413       s2 = "bteqz";
8414       s3 = "x,8";
8415       goto do_branch_i;
8416     case M_BGEU_I:
8417       s = "sltiu";
8418       s2 = "bteqz";
8419       s3 = "x,8";
8420       goto do_branch_i;
8421     case M_BGT_I:
8422       s = "slti";
8423       s2 = "bteqz";
8424       s3 = "x,8";
8425       goto do_addone_branch_i;
8426     case M_BGTU_I:
8427       s = "sltiu";
8428       s2 = "bteqz";
8429       s3 = "x,8";
8430
8431     do_addone_branch_i:
8432       if (imm_expr.X_op != O_constant)
8433         as_bad (_("Unsupported large constant"));
8434       ++imm_expr.X_add_number;
8435
8436     do_branch_i:
8437       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8438       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8439       break;
8440
8441     case M_ABS:
8442       expr1.X_add_number = 0;
8443       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
8444       if (xreg != yreg)
8445         move_register (&icnt, xreg, yreg);
8446       expr1.X_add_number = 2;
8447       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8448       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8449                    "neg", "x,w", xreg, xreg);
8450     }
8451 }
8452
8453 /* For consistency checking, verify that all bits are specified either
8454    by the match/mask part of the instruction definition, or by the
8455    operand list.  */
8456 static int
8457 validate_mips_insn (opc)
8458      const struct mips_opcode *opc;
8459 {
8460   const char *p = opc->args;
8461   char c;
8462   unsigned long used_bits = opc->mask;
8463
8464   if ((used_bits & opc->match) != opc->match)
8465     {
8466       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8467               opc->name, opc->args);
8468       return 0;
8469     }
8470 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8471   while (*p)
8472     switch (c = *p++)
8473       {
8474       case ',': break;
8475       case '(': break;
8476       case ')': break;
8477       case '+':
8478         switch (c = *p++)
8479           {
8480           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8481           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8482           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8483           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8484                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8485           default:
8486             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8487                     c, opc->name, opc->args);
8488             return 0;
8489           }
8490         break;
8491       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8492       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8493       case 'A': break;
8494       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8495       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8496       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8497       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8498       case 'F': break;
8499       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8500       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8501       case 'I': break;
8502       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8503       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8504       case 'L': break;
8505       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8506       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8507       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8508       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8509                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8510       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8511       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8512       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8513       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8514       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8515       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8516       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8517       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8518       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8519       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8520       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8521       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8522       case 'f': break;
8523       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8524       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8525       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8526       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8527       case 'l': break;
8528       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8529       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8530       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8531       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8532       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8533       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8534       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8535       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8536       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8537       case 'x': break;
8538       case 'z': break;
8539       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8540       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8541                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8542       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8543       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8544       case '[': break;
8545       case ']': break;
8546       default:
8547         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8548                 c, opc->name, opc->args);
8549         return 0;
8550       }
8551 #undef USE_BITS
8552   if (used_bits != 0xffffffff)
8553     {
8554       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8555               ~used_bits & 0xffffffff, opc->name, opc->args);
8556       return 0;
8557     }
8558   return 1;
8559 }
8560
8561 /* This routine assembles an instruction into its binary format.  As a
8562    side effect, it sets one of the global variables imm_reloc or
8563    offset_reloc to the type of relocation to do if one of the operands
8564    is an address expression.  */
8565
8566 static void
8567 mips_ip (str, ip)
8568      char *str;
8569      struct mips_cl_insn *ip;
8570 {
8571   char *s;
8572   const char *args;
8573   char c = 0;
8574   struct mips_opcode *insn;
8575   char *argsStart;
8576   unsigned int regno;
8577   unsigned int lastregno = 0;
8578   unsigned int lastpos = 0;
8579   unsigned int limlo, limhi;
8580   char *s_reset;
8581   char save_c = 0;
8582
8583   insn_error = NULL;
8584
8585   /* If the instruction contains a '.', we first try to match an instruction
8586      including the '.'.  Then we try again without the '.'.  */
8587   insn = NULL;
8588   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8589     continue;
8590
8591   /* If we stopped on whitespace, then replace the whitespace with null for
8592      the call to hash_find.  Save the character we replaced just in case we
8593      have to re-parse the instruction.  */
8594   if (ISSPACE (*s))
8595     {
8596       save_c = *s;
8597       *s++ = '\0';
8598     }
8599
8600   insn = (struct mips_opcode *) hash_find (op_hash, str);
8601
8602   /* If we didn't find the instruction in the opcode table, try again, but
8603      this time with just the instruction up to, but not including the
8604      first '.'.  */
8605   if (insn == NULL)
8606     {
8607       /* Restore the character we overwrite above (if any).  */
8608       if (save_c)
8609         *(--s) = save_c;
8610
8611       /* Scan up to the first '.' or whitespace.  */
8612       for (s = str;
8613            *s != '\0' && *s != '.' && !ISSPACE (*s);
8614            ++s)
8615         continue;
8616
8617       /* If we did not find a '.', then we can quit now.  */
8618       if (*s != '.')
8619         {
8620           insn_error = "unrecognized opcode";
8621           return;
8622         }
8623
8624       /* Lookup the instruction in the hash table.  */
8625       *s++ = '\0';
8626       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8627         {
8628           insn_error = "unrecognized opcode";
8629           return;
8630         }
8631     }
8632
8633   argsStart = s;
8634   for (;;)
8635     {
8636       bfd_boolean ok;
8637
8638       assert (strcmp (insn->name, str) == 0);
8639
8640       if (OPCODE_IS_MEMBER (insn,
8641                             (mips_opts.isa
8642                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8643                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8644                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8645                             mips_arch))
8646         ok = TRUE;
8647       else
8648         ok = FALSE;
8649
8650       if (insn->pinfo != INSN_MACRO)
8651         {
8652           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8653             ok = FALSE;
8654         }
8655
8656       if (! ok)
8657         {
8658           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8659               && strcmp (insn->name, insn[1].name) == 0)
8660             {
8661               ++insn;
8662               continue;
8663             }
8664           else
8665             {
8666               if (!insn_error)
8667                 {
8668                   static char buf[100];
8669                   if (mips_arch_info->is_isa)
8670                     sprintf (buf,
8671                              _("opcode not supported at this ISA level (%s)"),
8672                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8673                   else
8674                     sprintf (buf,
8675                              _("opcode not supported on this processor: %s (%s)"),
8676                              mips_arch_info->name,
8677                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8678                   insn_error = buf;
8679                 }
8680               if (save_c)
8681                 *(--s) = save_c;
8682               return;
8683             }
8684         }
8685
8686       ip->insn_mo = insn;
8687       ip->insn_opcode = insn->match;
8688       insn_error = NULL;
8689       for (args = insn->args;; ++args)
8690         {
8691           int is_mdmx;
8692
8693           s += strspn (s, " \t");
8694           is_mdmx = 0;
8695           switch (*args)
8696             {
8697             case '\0':          /* end of args */
8698               if (*s == '\0')
8699                 return;
8700               break;
8701
8702             case ',':
8703               if (*s++ == *args)
8704                 continue;
8705               s--;
8706               switch (*++args)
8707                 {
8708                 case 'r':
8709                 case 'v':
8710                   ip->insn_opcode |= lastregno << OP_SH_RS;
8711                   continue;
8712
8713                 case 'w':
8714                   ip->insn_opcode |= lastregno << OP_SH_RT;
8715                   continue;
8716
8717                 case 'W':
8718                   ip->insn_opcode |= lastregno << OP_SH_FT;
8719                   continue;
8720
8721                 case 'V':
8722                   ip->insn_opcode |= lastregno << OP_SH_FS;
8723                   continue;
8724                 }
8725               break;
8726
8727             case '(':
8728               /* Handle optional base register.
8729                  Either the base register is omitted or
8730                  we must have a left paren.  */
8731               /* This is dependent on the next operand specifier
8732                  is a base register specification.  */
8733               assert (args[1] == 'b' || args[1] == '5'
8734                       || args[1] == '-' || args[1] == '4');
8735               if (*s == '\0')
8736                 return;
8737
8738             case ')':           /* these must match exactly */
8739             case '[':
8740             case ']':
8741               if (*s++ == *args)
8742                 continue;
8743               break;
8744
8745             case '+':           /* Opcode extension character.  */
8746               switch (*++args)
8747                 {
8748                 case 'A':               /* ins/ext position, becomes LSB.  */
8749                   limlo = 0;
8750                   limhi = 31;
8751                   my_getExpression (&imm_expr, s);
8752                   check_absolute_expr (ip, &imm_expr);
8753                   if ((unsigned long) imm_expr.X_add_number < limlo
8754                       || (unsigned long) imm_expr.X_add_number > limhi)
8755                     {
8756                       as_bad (_("Improper position (%lu)"),
8757                               (unsigned long) imm_expr.X_add_number);
8758                       imm_expr.X_add_number = limlo;
8759                     }
8760                   lastpos = imm_expr.X_add_number;
8761                   ip->insn_opcode |= (imm_expr.X_add_number
8762                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8763                   imm_expr.X_op = O_absent;
8764                   s = expr_end;
8765                   continue;
8766
8767                 case 'B':               /* ins size, becomes MSB.  */
8768                   limlo = 1;
8769                   limhi = 32;
8770                   my_getExpression (&imm_expr, s);
8771                   check_absolute_expr (ip, &imm_expr);
8772                   /* Check for negative input so that small negative numbers
8773                      will not succeed incorrectly.  The checks against
8774                      (pos+size) transitively check "size" itself,
8775                      assuming that "pos" is reasonable.  */
8776                   if ((long) imm_expr.X_add_number < 0
8777                       || ((unsigned long) imm_expr.X_add_number
8778                           + lastpos) < limlo
8779                       || ((unsigned long) imm_expr.X_add_number
8780                           + lastpos) > limhi)
8781                     {
8782                       as_bad (_("Improper insert size (%lu, position %lu)"),
8783                               (unsigned long) imm_expr.X_add_number,
8784                               (unsigned long) lastpos);
8785                       imm_expr.X_add_number = limlo - lastpos;
8786                     }
8787                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8788                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8789                   imm_expr.X_op = O_absent;
8790                   s = expr_end;
8791                   continue;
8792
8793                 case 'C':               /* ext size, becomes MSBD.  */
8794                   limlo = 1;
8795                   limhi = 32;
8796                   my_getExpression (&imm_expr, s);
8797                   check_absolute_expr (ip, &imm_expr);
8798                   /* Check for negative input so that small negative numbers
8799                      will not succeed incorrectly.  The checks against
8800                      (pos+size) transitively check "size" itself,
8801                      assuming that "pos" is reasonable.  */
8802                   if ((long) imm_expr.X_add_number < 0
8803                       || ((unsigned long) imm_expr.X_add_number
8804                           + lastpos) < limlo
8805                       || ((unsigned long) imm_expr.X_add_number
8806                           + lastpos) > limhi)
8807                     {
8808                       as_bad (_("Improper extract size (%lu, position %lu)"),
8809                               (unsigned long) imm_expr.X_add_number,
8810                               (unsigned long) lastpos);
8811                       imm_expr.X_add_number = limlo - lastpos;
8812                     }
8813                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8814                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8815                   imm_expr.X_op = O_absent;
8816                   s = expr_end;
8817                   continue;
8818
8819                 case 'D':
8820                   /* +D is for disassembly only; never match.  */
8821                   break;
8822
8823                 default:
8824                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8825                     *args, insn->name, insn->args);
8826                   /* Further processing is fruitless.  */
8827                   return;
8828                 }
8829               break;
8830
8831             case '<':           /* must be at least one digit */
8832               /*
8833                * According to the manual, if the shift amount is greater
8834                * than 31 or less than 0, then the shift amount should be
8835                * mod 32.  In reality the mips assembler issues an error.
8836                * We issue a warning and mask out all but the low 5 bits.
8837                */
8838               my_getExpression (&imm_expr, s);
8839               check_absolute_expr (ip, &imm_expr);
8840               if ((unsigned long) imm_expr.X_add_number > 31)
8841                 {
8842                   as_warn (_("Improper shift amount (%lu)"),
8843                            (unsigned long) imm_expr.X_add_number);
8844                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8845                 }
8846               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8847               imm_expr.X_op = O_absent;
8848               s = expr_end;
8849               continue;
8850
8851             case '>':           /* shift amount minus 32 */
8852               my_getExpression (&imm_expr, s);
8853               check_absolute_expr (ip, &imm_expr);
8854               if ((unsigned long) imm_expr.X_add_number < 32
8855                   || (unsigned long) imm_expr.X_add_number > 63)
8856                 break;
8857               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8858               imm_expr.X_op = O_absent;
8859               s = expr_end;
8860               continue;
8861
8862             case 'k':           /* cache code */
8863             case 'h':           /* prefx code */
8864               my_getExpression (&imm_expr, s);
8865               check_absolute_expr (ip, &imm_expr);
8866               if ((unsigned long) imm_expr.X_add_number > 31)
8867                 {
8868                   as_warn (_("Invalid value for `%s' (%lu)"),
8869                            ip->insn_mo->name,
8870                            (unsigned long) imm_expr.X_add_number);
8871                   imm_expr.X_add_number &= 0x1f;
8872                 }
8873               if (*args == 'k')
8874                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8875               else
8876                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8877               imm_expr.X_op = O_absent;
8878               s = expr_end;
8879               continue;
8880
8881             case 'c':           /* break code */
8882               my_getExpression (&imm_expr, s);
8883               check_absolute_expr (ip, &imm_expr);
8884               if ((unsigned long) imm_expr.X_add_number > 1023)
8885                 {
8886                   as_warn (_("Illegal break code (%lu)"),
8887                            (unsigned long) imm_expr.X_add_number);
8888                   imm_expr.X_add_number &= OP_MASK_CODE;
8889                 }
8890               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8891               imm_expr.X_op = O_absent;
8892               s = expr_end;
8893               continue;
8894
8895             case 'q':           /* lower break code */
8896               my_getExpression (&imm_expr, s);
8897               check_absolute_expr (ip, &imm_expr);
8898               if ((unsigned long) imm_expr.X_add_number > 1023)
8899                 {
8900                   as_warn (_("Illegal lower break code (%lu)"),
8901                            (unsigned long) imm_expr.X_add_number);
8902                   imm_expr.X_add_number &= OP_MASK_CODE2;
8903                 }
8904               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8905               imm_expr.X_op = O_absent;
8906               s = expr_end;
8907               continue;
8908
8909             case 'B':           /* 20-bit syscall/break code.  */
8910               my_getExpression (&imm_expr, s);
8911               check_absolute_expr (ip, &imm_expr);
8912               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8913                 as_warn (_("Illegal 20-bit code (%lu)"),
8914                          (unsigned long) imm_expr.X_add_number);
8915               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8916               imm_expr.X_op = O_absent;
8917               s = expr_end;
8918               continue;
8919
8920             case 'C':           /* Coprocessor code */
8921               my_getExpression (&imm_expr, s);
8922               check_absolute_expr (ip, &imm_expr);
8923               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8924                 {
8925                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8926                            (unsigned long) imm_expr.X_add_number);
8927                   imm_expr.X_add_number &= ((1 << 25) - 1);
8928                 }
8929               ip->insn_opcode |= imm_expr.X_add_number;
8930               imm_expr.X_op = O_absent;
8931               s = expr_end;
8932               continue;
8933
8934             case 'J':           /* 19-bit wait code.  */
8935               my_getExpression (&imm_expr, s);
8936               check_absolute_expr (ip, &imm_expr);
8937               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8938                 as_warn (_("Illegal 19-bit code (%lu)"),
8939                          (unsigned long) imm_expr.X_add_number);
8940               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8941               imm_expr.X_op = O_absent;
8942               s = expr_end;
8943               continue;
8944
8945             case 'P':           /* Performance register */
8946               my_getExpression (&imm_expr, s);
8947               check_absolute_expr (ip, &imm_expr);
8948               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8949                 {
8950                   as_warn (_("Invalid performance register (%lu)"),
8951                            (unsigned long) imm_expr.X_add_number);
8952                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8953                 }
8954               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8955               imm_expr.X_op = O_absent;
8956               s = expr_end;
8957               continue;
8958
8959             case 'b':           /* base register */
8960             case 'd':           /* destination register */
8961             case 's':           /* source register */
8962             case 't':           /* target register */
8963             case 'r':           /* both target and source */
8964             case 'v':           /* both dest and source */
8965             case 'w':           /* both dest and target */
8966             case 'E':           /* coprocessor target register */
8967             case 'G':           /* coprocessor destination register */
8968             case 'K':           /* 'rdhwr' destination register */
8969             case 'x':           /* ignore register name */
8970             case 'z':           /* must be zero register */
8971             case 'U':           /* destination register (clo/clz).  */
8972               s_reset = s;
8973               if (s[0] == '$')
8974                 {
8975
8976                   if (ISDIGIT (s[1]))
8977                     {
8978                       ++s;
8979                       regno = 0;
8980                       do
8981                         {
8982                           regno *= 10;
8983                           regno += *s - '0';
8984                           ++s;
8985                         }
8986                       while (ISDIGIT (*s));
8987                       if (regno > 31)
8988                         as_bad (_("Invalid register number (%d)"), regno);
8989                     }
8990                   else if (*args == 'E' || *args == 'G' || *args == 'K')
8991                     goto notreg;
8992                   else
8993                     {
8994                       if (s[1] == 'r' && s[2] == 'a')
8995                         {
8996                           s += 3;
8997                           regno = RA;
8998                         }
8999                       else if (s[1] == 'f' && s[2] == 'p')
9000                         {
9001                           s += 3;
9002                           regno = FP;
9003                         }
9004                       else if (s[1] == 's' && s[2] == 'p')
9005                         {
9006                           s += 3;
9007                           regno = SP;
9008                         }
9009                       else if (s[1] == 'g' && s[2] == 'p')
9010                         {
9011                           s += 3;
9012                           regno = GP;
9013                         }
9014                       else if (s[1] == 'a' && s[2] == 't')
9015                         {
9016                           s += 3;
9017                           regno = AT;
9018                         }
9019                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9020                         {
9021                           s += 4;
9022                           regno = KT0;
9023                         }
9024                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9025                         {
9026                           s += 4;
9027                           regno = KT1;
9028                         }
9029                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9030                         {
9031                           s += 5;
9032                           regno = ZERO;
9033                         }
9034                       else if (itbl_have_entries)
9035                         {
9036                           char *p, *n;
9037                           unsigned long r;
9038
9039                           p = s + 1;    /* advance past '$' */
9040                           n = itbl_get_field (&p);  /* n is name */
9041
9042                           /* See if this is a register defined in an
9043                              itbl entry.  */
9044                           if (itbl_get_reg_val (n, &r))
9045                             {
9046                               /* Get_field advances to the start of
9047                                  the next field, so we need to back
9048                                  rack to the end of the last field.  */
9049                               if (p)
9050                                 s = p - 1;
9051                               else
9052                                 s = strchr (s, '\0');
9053                               regno = r;
9054                             }
9055                           else
9056                             goto notreg;
9057                         }
9058                       else
9059                         goto notreg;
9060                     }
9061                   if (regno == AT
9062                       && ! mips_opts.noat
9063                       && *args != 'E'
9064                       && *args != 'G'
9065                       && *args != 'K')
9066                     as_warn (_("Used $at without \".set noat\""));
9067                   c = *args;
9068                   if (*s == ' ')
9069                     ++s;
9070                   if (args[1] != *s)
9071                     {
9072                       if (c == 'r' || c == 'v' || c == 'w')
9073                         {
9074                           regno = lastregno;
9075                           s = s_reset;
9076                           ++args;
9077                         }
9078                     }
9079                   /* 'z' only matches $0.  */
9080                   if (c == 'z' && regno != 0)
9081                     break;
9082
9083         /* Now that we have assembled one operand, we use the args string
9084          * to figure out where it goes in the instruction.  */
9085                   switch (c)
9086                     {
9087                     case 'r':
9088                     case 's':
9089                     case 'v':
9090                     case 'b':
9091                       ip->insn_opcode |= regno << OP_SH_RS;
9092                       break;
9093                     case 'd':
9094                     case 'G':
9095                     case 'K':
9096                       ip->insn_opcode |= regno << OP_SH_RD;
9097                       break;
9098                     case 'U':
9099                       ip->insn_opcode |= regno << OP_SH_RD;
9100                       ip->insn_opcode |= regno << OP_SH_RT;
9101                       break;
9102                     case 'w':
9103                     case 't':
9104                     case 'E':
9105                       ip->insn_opcode |= regno << OP_SH_RT;
9106                       break;
9107                     case 'x':
9108                       /* This case exists because on the r3000 trunc
9109                          expands into a macro which requires a gp
9110                          register.  On the r6000 or r4000 it is
9111                          assembled into a single instruction which
9112                          ignores the register.  Thus the insn version
9113                          is MIPS_ISA2 and uses 'x', and the macro
9114                          version is MIPS_ISA1 and uses 't'.  */
9115                       break;
9116                     case 'z':
9117                       /* This case is for the div instruction, which
9118                          acts differently if the destination argument
9119                          is $0.  This only matches $0, and is checked
9120                          outside the switch.  */
9121                       break;
9122                     case 'D':
9123                       /* Itbl operand; not yet implemented. FIXME ?? */
9124                       break;
9125                       /* What about all other operands like 'i', which
9126                          can be specified in the opcode table? */
9127                     }
9128                   lastregno = regno;
9129                   continue;
9130                 }
9131             notreg:
9132               switch (*args++)
9133                 {
9134                 case 'r':
9135                 case 'v':
9136                   ip->insn_opcode |= lastregno << OP_SH_RS;
9137                   continue;
9138                 case 'w':
9139                   ip->insn_opcode |= lastregno << OP_SH_RT;
9140                   continue;
9141                 }
9142               break;
9143
9144             case 'O':           /* MDMX alignment immediate constant.  */
9145               my_getExpression (&imm_expr, s);
9146               check_absolute_expr (ip, &imm_expr);
9147               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
9148                 {
9149                   as_warn ("Improper align amount (%ld), using low bits",
9150                            (long) imm_expr.X_add_number);
9151                   imm_expr.X_add_number &= OP_MASK_ALN;
9152                 }
9153               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
9154               imm_expr.X_op = O_absent;
9155               s = expr_end;
9156               continue;
9157
9158             case 'Q':           /* MDMX vector, element sel, or const.  */
9159               if (s[0] != '$')
9160                 {
9161                   /* MDMX Immediate.  */
9162                   my_getExpression (&imm_expr, s);
9163                   check_absolute_expr (ip, &imm_expr);
9164                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
9165                     {
9166                       as_warn (_("Invalid MDMX Immediate (%ld)"),
9167                                (long) imm_expr.X_add_number);
9168                       imm_expr.X_add_number &= OP_MASK_FT;
9169                     }
9170                   imm_expr.X_add_number &= OP_MASK_FT;
9171                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9172                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9173                   else
9174                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
9175                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
9176                   imm_expr.X_op = O_absent;
9177                   s = expr_end;
9178                   continue;
9179                 }
9180               /* Not MDMX Immediate.  Fall through.  */
9181             case 'X':           /* MDMX destination register.  */
9182             case 'Y':           /* MDMX source register.  */
9183             case 'Z':           /* MDMX target register.  */
9184               is_mdmx = 1;
9185             case 'D':           /* floating point destination register */
9186             case 'S':           /* floating point source register */
9187             case 'T':           /* floating point target register */
9188             case 'R':           /* floating point source register */
9189             case 'V':
9190             case 'W':
9191               s_reset = s;
9192               /* Accept $fN for FP and MDMX register numbers, and in
9193                  addition accept $vN for MDMX register numbers.  */
9194               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
9195                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
9196                       && ISDIGIT (s[2])))
9197                 {
9198                   s += 2;
9199                   regno = 0;
9200                   do
9201                     {
9202                       regno *= 10;
9203                       regno += *s - '0';
9204                       ++s;
9205                     }
9206                   while (ISDIGIT (*s));
9207
9208                   if (regno > 31)
9209                     as_bad (_("Invalid float register number (%d)"), regno);
9210
9211                   if ((regno & 1) != 0
9212                       && HAVE_32BIT_FPRS
9213                       && ! (strcmp (str, "mtc1") == 0
9214                             || strcmp (str, "mfc1") == 0
9215                             || strcmp (str, "lwc1") == 0
9216                             || strcmp (str, "swc1") == 0
9217                             || strcmp (str, "l.s") == 0
9218                             || strcmp (str, "s.s") == 0))
9219                     as_warn (_("Float register should be even, was %d"),
9220                              regno);
9221
9222                   c = *args;
9223                   if (*s == ' ')
9224                     ++s;
9225                   if (args[1] != *s)
9226                     {
9227                       if (c == 'V' || c == 'W')
9228                         {
9229                           regno = lastregno;
9230                           s = s_reset;
9231                           ++args;
9232                         }
9233                     }
9234                   switch (c)
9235                     {
9236                     case 'D':
9237                     case 'X':
9238                       ip->insn_opcode |= regno << OP_SH_FD;
9239                       break;
9240                     case 'V':
9241                     case 'S':
9242                     case 'Y':
9243                       ip->insn_opcode |= regno << OP_SH_FS;
9244                       break;
9245                     case 'Q':
9246                       /* This is like 'Z', but also needs to fix the MDMX
9247                          vector/scalar select bits.  Note that the
9248                          scalar immediate case is handled above.  */
9249                       if (*s == '[')
9250                         {
9251                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9252                           int max_el = (is_qh ? 3 : 7);
9253                           s++;
9254                           my_getExpression(&imm_expr, s);
9255                           check_absolute_expr (ip, &imm_expr);
9256                           s = expr_end;
9257                           if (imm_expr.X_add_number > max_el)
9258                             as_bad(_("Bad element selector %ld"),
9259                                    (long) imm_expr.X_add_number);
9260                           imm_expr.X_add_number &= max_el;
9261                           ip->insn_opcode |= (imm_expr.X_add_number
9262                                               << (OP_SH_VSEL +
9263                                                   (is_qh ? 2 : 1)));
9264                           if (*s != ']')
9265                             as_warn(_("Expecting ']' found '%s'"), s);
9266                           else
9267                             s++;
9268                         }
9269                       else
9270                         {
9271                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9272                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9273                                                 << OP_SH_VSEL);
9274                           else
9275                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9276                                                 OP_SH_VSEL);
9277                         }
9278                       /* Fall through */
9279                     case 'W':
9280                     case 'T':
9281                     case 'Z':
9282                       ip->insn_opcode |= regno << OP_SH_FT;
9283                       break;
9284                     case 'R':
9285                       ip->insn_opcode |= regno << OP_SH_FR;
9286                       break;
9287                     }
9288                   lastregno = regno;
9289                   continue;
9290                 }
9291
9292               switch (*args++)
9293                 {
9294                 case 'V':
9295                   ip->insn_opcode |= lastregno << OP_SH_FS;
9296                   continue;
9297                 case 'W':
9298                   ip->insn_opcode |= lastregno << OP_SH_FT;
9299                   continue;
9300                 }
9301               break;
9302
9303             case 'I':
9304               my_getExpression (&imm_expr, s);
9305               if (imm_expr.X_op != O_big
9306                   && imm_expr.X_op != O_constant)
9307                 insn_error = _("absolute expression required");
9308               s = expr_end;
9309               continue;
9310
9311             case 'A':
9312               my_getExpression (&offset_expr, s);
9313               *imm_reloc = BFD_RELOC_32;
9314               s = expr_end;
9315               continue;
9316
9317             case 'F':
9318             case 'L':
9319             case 'f':
9320             case 'l':
9321               {
9322                 int f64;
9323                 int using_gprs;
9324                 char *save_in;
9325                 char *err;
9326                 unsigned char temp[8];
9327                 int len;
9328                 unsigned int length;
9329                 segT seg;
9330                 subsegT subseg;
9331                 char *p;
9332
9333                 /* These only appear as the last operand in an
9334                    instruction, and every instruction that accepts
9335                    them in any variant accepts them in all variants.
9336                    This means we don't have to worry about backing out
9337                    any changes if the instruction does not match.
9338
9339                    The difference between them is the size of the
9340                    floating point constant and where it goes.  For 'F'
9341                    and 'L' the constant is 64 bits; for 'f' and 'l' it
9342                    is 32 bits.  Where the constant is placed is based
9343                    on how the MIPS assembler does things:
9344                     F -- .rdata
9345                     L -- .lit8
9346                     f -- immediate value
9347                     l -- .lit4
9348
9349                     The .lit4 and .lit8 sections are only used if
9350                     permitted by the -G argument.
9351
9352                     When generating embedded PIC code, we use the
9353                     .lit8 section but not the .lit4 section (we can do
9354                     .lit4 inline easily; we need to put .lit8
9355                     somewhere in the data segment, and using .lit8
9356                     permits the linker to eventually combine identical
9357                     .lit8 entries).
9358
9359                     The code below needs to know whether the target register
9360                     is 32 or 64 bits wide.  It relies on the fact 'f' and
9361                     'F' are used with GPR-based instructions and 'l' and
9362                     'L' are used with FPR-based instructions.  */
9363
9364                 f64 = *args == 'F' || *args == 'L';
9365                 using_gprs = *args == 'F' || *args == 'f';
9366
9367                 save_in = input_line_pointer;
9368                 input_line_pointer = s;
9369                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9370                 length = len;
9371                 s = input_line_pointer;
9372                 input_line_pointer = save_in;
9373                 if (err != NULL && *err != '\0')
9374                   {
9375                     as_bad (_("Bad floating point constant: %s"), err);
9376                     memset (temp, '\0', sizeof temp);
9377                     length = f64 ? 8 : 4;
9378                   }
9379
9380                 assert (length == (unsigned) (f64 ? 8 : 4));
9381
9382                 if (*args == 'f'
9383                     || (*args == 'l'
9384                         && (! USE_GLOBAL_POINTER_OPT
9385                             || mips_pic == EMBEDDED_PIC
9386                             || g_switch_value < 4
9387                             || (temp[0] == 0 && temp[1] == 0)
9388                             || (temp[2] == 0 && temp[3] == 0))))
9389                   {
9390                     imm_expr.X_op = O_constant;
9391                     if (! target_big_endian)
9392                       imm_expr.X_add_number = bfd_getl32 (temp);
9393                     else
9394                       imm_expr.X_add_number = bfd_getb32 (temp);
9395                   }
9396                 else if (length > 4
9397                          && ! mips_disable_float_construction
9398                          /* Constants can only be constructed in GPRs and
9399                             copied to FPRs if the GPRs are at least as wide
9400                             as the FPRs.  Force the constant into memory if
9401                             we are using 64-bit FPRs but the GPRs are only
9402                             32 bits wide.  */
9403                          && (using_gprs
9404                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9405                          && ((temp[0] == 0 && temp[1] == 0)
9406                              || (temp[2] == 0 && temp[3] == 0))
9407                          && ((temp[4] == 0 && temp[5] == 0)
9408                              || (temp[6] == 0 && temp[7] == 0)))
9409                   {
9410                     /* The value is simple enough to load with a couple of
9411                        instructions.  If using 32-bit registers, set
9412                        imm_expr to the high order 32 bits and offset_expr to
9413                        the low order 32 bits.  Otherwise, set imm_expr to
9414                        the entire 64 bit constant.  */
9415                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9416                       {
9417                         imm_expr.X_op = O_constant;
9418                         offset_expr.X_op = O_constant;
9419                         if (! target_big_endian)
9420                           {
9421                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
9422                             offset_expr.X_add_number = bfd_getl32 (temp);
9423                           }
9424                         else
9425                           {
9426                             imm_expr.X_add_number = bfd_getb32 (temp);
9427                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9428                           }
9429                         if (offset_expr.X_add_number == 0)
9430                           offset_expr.X_op = O_absent;
9431                       }
9432                     else if (sizeof (imm_expr.X_add_number) > 4)
9433                       {
9434                         imm_expr.X_op = O_constant;
9435                         if (! target_big_endian)
9436                           imm_expr.X_add_number = bfd_getl64 (temp);
9437                         else
9438                           imm_expr.X_add_number = bfd_getb64 (temp);
9439                       }
9440                     else
9441                       {
9442                         imm_expr.X_op = O_big;
9443                         imm_expr.X_add_number = 4;
9444                         if (! target_big_endian)
9445                           {
9446                             generic_bignum[0] = bfd_getl16 (temp);
9447                             generic_bignum[1] = bfd_getl16 (temp + 2);
9448                             generic_bignum[2] = bfd_getl16 (temp + 4);
9449                             generic_bignum[3] = bfd_getl16 (temp + 6);
9450                           }
9451                         else
9452                           {
9453                             generic_bignum[0] = bfd_getb16 (temp + 6);
9454                             generic_bignum[1] = bfd_getb16 (temp + 4);
9455                             generic_bignum[2] = bfd_getb16 (temp + 2);
9456                             generic_bignum[3] = bfd_getb16 (temp);
9457                           }
9458                       }
9459                   }
9460                 else
9461                   {
9462                     const char *newname;
9463                     segT new_seg;
9464
9465                     /* Switch to the right section.  */
9466                     seg = now_seg;
9467                     subseg = now_subseg;
9468                     switch (*args)
9469                       {
9470                       default: /* unused default case avoids warnings.  */
9471                       case 'L':
9472                         newname = RDATA_SECTION_NAME;
9473                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9474                             || mips_pic == EMBEDDED_PIC)
9475                           newname = ".lit8";
9476                         break;
9477                       case 'F':
9478                         if (mips_pic == EMBEDDED_PIC)
9479                           newname = ".lit8";
9480                         else
9481                           newname = RDATA_SECTION_NAME;
9482                         break;
9483                       case 'l':
9484                         assert (!USE_GLOBAL_POINTER_OPT
9485                                 || g_switch_value >= 4);
9486                         newname = ".lit4";
9487                         break;
9488                       }
9489                     new_seg = subseg_new (newname, (subsegT) 0);
9490                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9491                       bfd_set_section_flags (stdoutput, new_seg,
9492                                              (SEC_ALLOC
9493                                               | SEC_LOAD
9494                                               | SEC_READONLY
9495                                               | SEC_DATA));
9496                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9497                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9498                         && strcmp (TARGET_OS, "elf") != 0)
9499                       record_alignment (new_seg, 4);
9500                     else
9501                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9502                     if (seg == now_seg)
9503                       as_bad (_("Can't use floating point insn in this section"));
9504
9505                     /* Set the argument to the current address in the
9506                        section.  */
9507                     offset_expr.X_op = O_symbol;
9508                     offset_expr.X_add_symbol =
9509                       symbol_new ("L0\001", now_seg,
9510                                   (valueT) frag_now_fix (), frag_now);
9511                     offset_expr.X_add_number = 0;
9512
9513                     /* Put the floating point number into the section.  */
9514                     p = frag_more ((int) length);
9515                     memcpy (p, temp, length);
9516
9517                     /* Switch back to the original section.  */
9518                     subseg_set (seg, subseg);
9519                   }
9520               }
9521               continue;
9522
9523             case 'i':           /* 16 bit unsigned immediate */
9524             case 'j':           /* 16 bit signed immediate */
9525               *imm_reloc = BFD_RELOC_LO16;
9526               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9527                 {
9528                   int more;
9529                   offsetT minval, maxval;
9530
9531                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9532                           && strcmp (insn->name, insn[1].name) == 0);
9533
9534                   /* If the expression was written as an unsigned number,
9535                      only treat it as signed if there are no more
9536                      alternatives.  */
9537                   if (more
9538                       && *args == 'j'
9539                       && sizeof (imm_expr.X_add_number) <= 4
9540                       && imm_expr.X_op == O_constant
9541                       && imm_expr.X_add_number < 0
9542                       && imm_expr.X_unsigned
9543                       && HAVE_64BIT_GPRS)
9544                     break;
9545
9546                   /* For compatibility with older assemblers, we accept
9547                      0x8000-0xffff as signed 16-bit numbers when only
9548                      signed numbers are allowed.  */
9549                   if (*args == 'i')
9550                     minval = 0, maxval = 0xffff;
9551                   else if (more)
9552                     minval = -0x8000, maxval = 0x7fff;
9553                   else
9554                     minval = -0x8000, maxval = 0xffff;
9555
9556                   if (imm_expr.X_op != O_constant
9557                       || imm_expr.X_add_number < minval
9558                       || imm_expr.X_add_number > maxval)
9559                     {
9560                       if (more)
9561                         break;
9562                       if (imm_expr.X_op == O_constant
9563                           || imm_expr.X_op == O_big)
9564                         as_bad (_("expression out of range"));
9565                     }
9566                 }
9567               s = expr_end;
9568               continue;
9569
9570             case 'o':           /* 16 bit offset */
9571               /* Check whether there is only a single bracketed expression
9572                  left.  If so, it must be the base register and the
9573                  constant must be zero.  */
9574               if (*s == '(' && strchr (s + 1, '(') == 0)
9575                 {
9576                   offset_expr.X_op = O_constant;
9577                   offset_expr.X_add_number = 0;
9578                   continue;
9579                 }
9580
9581               /* If this value won't fit into a 16 bit offset, then go
9582                  find a macro that will generate the 32 bit offset
9583                  code pattern.  */
9584               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9585                   && (offset_expr.X_op != O_constant
9586                       || offset_expr.X_add_number >= 0x8000
9587                       || offset_expr.X_add_number < -0x8000))
9588                 break;
9589
9590               s = expr_end;
9591               continue;
9592
9593             case 'p':           /* pc relative offset */
9594               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9595               my_getExpression (&offset_expr, s);
9596               s = expr_end;
9597               continue;
9598
9599             case 'u':           /* upper 16 bits */
9600               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9601                   && imm_expr.X_op == O_constant
9602                   && (imm_expr.X_add_number < 0
9603                       || imm_expr.X_add_number >= 0x10000))
9604                 as_bad (_("lui expression not in range 0..65535"));
9605               s = expr_end;
9606               continue;
9607
9608             case 'a':           /* 26 bit address */
9609               my_getExpression (&offset_expr, s);
9610               s = expr_end;
9611               *offset_reloc = BFD_RELOC_MIPS_JMP;
9612               continue;
9613
9614             case 'N':           /* 3 bit branch condition code */
9615             case 'M':           /* 3 bit compare condition code */
9616               if (strncmp (s, "$fcc", 4) != 0)
9617                 break;
9618               s += 4;
9619               regno = 0;
9620               do
9621                 {
9622                   regno *= 10;
9623                   regno += *s - '0';
9624                   ++s;
9625                 }
9626               while (ISDIGIT (*s));
9627               if (regno > 7)
9628                 as_bad (_("invalid condition code register $fcc%d"), regno);
9629               if (*args == 'N')
9630                 ip->insn_opcode |= regno << OP_SH_BCC;
9631               else
9632                 ip->insn_opcode |= regno << OP_SH_CCC;
9633               continue;
9634
9635             case 'H':
9636               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9637                 s += 2;
9638               if (ISDIGIT (*s))
9639                 {
9640                   c = 0;
9641                   do
9642                     {
9643                       c *= 10;
9644                       c += *s - '0';
9645                       ++s;
9646                     }
9647                   while (ISDIGIT (*s));
9648                 }
9649               else
9650                 c = 8; /* Invalid sel value.  */
9651
9652               if (c > 7)
9653                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9654               ip->insn_opcode |= c;
9655               continue;
9656
9657             case 'e':
9658               /* Must be at least one digit.  */
9659               my_getExpression (&imm_expr, s);
9660               check_absolute_expr (ip, &imm_expr);
9661
9662               if ((unsigned long) imm_expr.X_add_number
9663                   > (unsigned long) OP_MASK_VECBYTE)
9664                 {
9665                   as_bad (_("bad byte vector index (%ld)"),
9666                            (long) imm_expr.X_add_number);
9667                   imm_expr.X_add_number = 0;
9668                 }
9669
9670               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9671               imm_expr.X_op = O_absent;
9672               s = expr_end;
9673               continue;
9674
9675             case '%':
9676               my_getExpression (&imm_expr, s);
9677               check_absolute_expr (ip, &imm_expr);
9678
9679               if ((unsigned long) imm_expr.X_add_number
9680                   > (unsigned long) OP_MASK_VECALIGN)
9681                 {
9682                   as_bad (_("bad byte vector index (%ld)"),
9683                            (long) imm_expr.X_add_number);
9684                   imm_expr.X_add_number = 0;
9685                 }
9686
9687               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9688               imm_expr.X_op = O_absent;
9689               s = expr_end;
9690               continue;
9691
9692             default:
9693               as_bad (_("bad char = '%c'\n"), *args);
9694               internalError ();
9695             }
9696           break;
9697         }
9698       /* Args don't match.  */
9699       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9700           !strcmp (insn->name, insn[1].name))
9701         {
9702           ++insn;
9703           s = argsStart;
9704           insn_error = _("illegal operands");
9705           continue;
9706         }
9707       if (save_c)
9708         *(--s) = save_c;
9709       insn_error = _("illegal operands");
9710       return;
9711     }
9712 }
9713
9714 /* This routine assembles an instruction into its binary format when
9715    assembling for the mips16.  As a side effect, it sets one of the
9716    global variables imm_reloc or offset_reloc to the type of
9717    relocation to do if one of the operands is an address expression.
9718    It also sets mips16_small and mips16_ext if the user explicitly
9719    requested a small or extended instruction.  */
9720
9721 static void
9722 mips16_ip (str, ip)
9723      char *str;
9724      struct mips_cl_insn *ip;
9725 {
9726   char *s;
9727   const char *args;
9728   struct mips_opcode *insn;
9729   char *argsstart;
9730   unsigned int regno;
9731   unsigned int lastregno = 0;
9732   char *s_reset;
9733
9734   insn_error = NULL;
9735
9736   mips16_small = FALSE;
9737   mips16_ext = FALSE;
9738
9739   for (s = str; ISLOWER (*s); ++s)
9740     ;
9741   switch (*s)
9742     {
9743     case '\0':
9744       break;
9745
9746     case ' ':
9747       *s++ = '\0';
9748       break;
9749
9750     case '.':
9751       if (s[1] == 't' && s[2] == ' ')
9752         {
9753           *s = '\0';
9754           mips16_small = TRUE;
9755           s += 3;
9756           break;
9757         }
9758       else if (s[1] == 'e' && s[2] == ' ')
9759         {
9760           *s = '\0';
9761           mips16_ext = TRUE;
9762           s += 3;
9763           break;
9764         }
9765       /* Fall through.  */
9766     default:
9767       insn_error = _("unknown opcode");
9768       return;
9769     }
9770
9771   if (mips_opts.noautoextend && ! mips16_ext)
9772     mips16_small = TRUE;
9773
9774   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9775     {
9776       insn_error = _("unrecognized opcode");
9777       return;
9778     }
9779
9780   argsstart = s;
9781   for (;;)
9782     {
9783       assert (strcmp (insn->name, str) == 0);
9784
9785       ip->insn_mo = insn;
9786       ip->insn_opcode = insn->match;
9787       ip->use_extend = FALSE;
9788       imm_expr.X_op = O_absent;
9789       imm_reloc[0] = BFD_RELOC_UNUSED;
9790       imm_reloc[1] = BFD_RELOC_UNUSED;
9791       imm_reloc[2] = BFD_RELOC_UNUSED;
9792       offset_expr.X_op = O_absent;
9793       offset_reloc[0] = BFD_RELOC_UNUSED;
9794       offset_reloc[1] = BFD_RELOC_UNUSED;
9795       offset_reloc[2] = BFD_RELOC_UNUSED;
9796       for (args = insn->args; 1; ++args)
9797         {
9798           int c;
9799
9800           if (*s == ' ')
9801             ++s;
9802
9803           /* In this switch statement we call break if we did not find
9804              a match, continue if we did find a match, or return if we
9805              are done.  */
9806
9807           c = *args;
9808           switch (c)
9809             {
9810             case '\0':
9811               if (*s == '\0')
9812                 {
9813                   /* Stuff the immediate value in now, if we can.  */
9814                   if (imm_expr.X_op == O_constant
9815                       && *imm_reloc > BFD_RELOC_UNUSED
9816                       && insn->pinfo != INSN_MACRO)
9817                     {
9818                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9819                                     imm_expr.X_add_number, TRUE, mips16_small,
9820                                     mips16_ext, &ip->insn_opcode,
9821                                     &ip->use_extend, &ip->extend);
9822                       imm_expr.X_op = O_absent;
9823                       *imm_reloc = BFD_RELOC_UNUSED;
9824                     }
9825
9826                   return;
9827                 }
9828               break;
9829
9830             case ',':
9831               if (*s++ == c)
9832                 continue;
9833               s--;
9834               switch (*++args)
9835                 {
9836                 case 'v':
9837                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9838                   continue;
9839                 case 'w':
9840                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9841                   continue;
9842                 }
9843               break;
9844
9845             case '(':
9846             case ')':
9847               if (*s++ == c)
9848                 continue;
9849               break;
9850
9851             case 'v':
9852             case 'w':
9853               if (s[0] != '$')
9854                 {
9855                   if (c == 'v')
9856                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9857                   else
9858                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9859                   ++args;
9860                   continue;
9861                 }
9862               /* Fall through.  */
9863             case 'x':
9864             case 'y':
9865             case 'z':
9866             case 'Z':
9867             case '0':
9868             case 'S':
9869             case 'R':
9870             case 'X':
9871             case 'Y':
9872               if (s[0] != '$')
9873                 break;
9874               s_reset = s;
9875               if (ISDIGIT (s[1]))
9876                 {
9877                   ++s;
9878                   regno = 0;
9879                   do
9880                     {
9881                       regno *= 10;
9882                       regno += *s - '0';
9883                       ++s;
9884                     }
9885                   while (ISDIGIT (*s));
9886                   if (regno > 31)
9887                     {
9888                       as_bad (_("invalid register number (%d)"), regno);
9889                       regno = 2;
9890                     }
9891                 }
9892               else
9893                 {
9894                   if (s[1] == 'r' && s[2] == 'a')
9895                     {
9896                       s += 3;
9897                       regno = RA;
9898                     }
9899                   else if (s[1] == 'f' && s[2] == 'p')
9900                     {
9901                       s += 3;
9902                       regno = FP;
9903                     }
9904                   else if (s[1] == 's' && s[2] == 'p')
9905                     {
9906                       s += 3;
9907                       regno = SP;
9908                     }
9909                   else if (s[1] == 'g' && s[2] == 'p')
9910                     {
9911                       s += 3;
9912                       regno = GP;
9913                     }
9914                   else if (s[1] == 'a' && s[2] == 't')
9915                     {
9916                       s += 3;
9917                       regno = AT;
9918                     }
9919                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9920                     {
9921                       s += 4;
9922                       regno = KT0;
9923                     }
9924                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9925                     {
9926                       s += 4;
9927                       regno = KT1;
9928                     }
9929                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9930                     {
9931                       s += 5;
9932                       regno = ZERO;
9933                     }
9934                   else
9935                     break;
9936                 }
9937
9938               if (*s == ' ')
9939                 ++s;
9940               if (args[1] != *s)
9941                 {
9942                   if (c == 'v' || c == 'w')
9943                     {
9944                       regno = mips16_to_32_reg_map[lastregno];
9945                       s = s_reset;
9946                       ++args;
9947                     }
9948                 }
9949
9950               switch (c)
9951                 {
9952                 case 'x':
9953                 case 'y':
9954                 case 'z':
9955                 case 'v':
9956                 case 'w':
9957                 case 'Z':
9958                   regno = mips32_to_16_reg_map[regno];
9959                   break;
9960
9961                 case '0':
9962                   if (regno != 0)
9963                     regno = ILLEGAL_REG;
9964                   break;
9965
9966                 case 'S':
9967                   if (regno != SP)
9968                     regno = ILLEGAL_REG;
9969                   break;
9970
9971                 case 'R':
9972                   if (regno != RA)
9973                     regno = ILLEGAL_REG;
9974                   break;
9975
9976                 case 'X':
9977                 case 'Y':
9978                   if (regno == AT && ! mips_opts.noat)
9979                     as_warn (_("used $at without \".set noat\""));
9980                   break;
9981
9982                 default:
9983                   internalError ();
9984                 }
9985
9986               if (regno == ILLEGAL_REG)
9987                 break;
9988
9989               switch (c)
9990                 {
9991                 case 'x':
9992                 case 'v':
9993                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9994                   break;
9995                 case 'y':
9996                 case 'w':
9997                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9998                   break;
9999                 case 'z':
10000                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
10001                   break;
10002                 case 'Z':
10003                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
10004                 case '0':
10005                 case 'S':
10006                 case 'R':
10007                   break;
10008                 case 'X':
10009                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
10010                   break;
10011                 case 'Y':
10012                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
10013                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
10014                   break;
10015                 default:
10016                   internalError ();
10017                 }
10018
10019               lastregno = regno;
10020               continue;
10021
10022             case 'P':
10023               if (strncmp (s, "$pc", 3) == 0)
10024                 {
10025                   s += 3;
10026                   continue;
10027                 }
10028               break;
10029
10030             case '<':
10031             case '>':
10032             case '[':
10033             case ']':
10034             case '4':
10035             case '5':
10036             case 'H':
10037             case 'W':
10038             case 'D':
10039             case 'j':
10040             case '8':
10041             case 'V':
10042             case 'C':
10043             case 'U':
10044             case 'k':
10045             case 'K':
10046               if (s[0] == '%'
10047                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
10048                 {
10049                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
10050                      and generate the appropriate reloc.  If the text
10051                      inside %gprel is not a symbol name with an
10052                      optional offset, then we generate a normal reloc
10053                      and will probably fail later.  */
10054                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
10055                   if (imm_expr.X_op == O_symbol)
10056                     {
10057                       mips16_ext = TRUE;
10058                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
10059                       s = expr_end;
10060                       ip->use_extend = TRUE;
10061                       ip->extend = 0;
10062                       continue;
10063                     }
10064                 }
10065               else
10066                 {
10067                   /* Just pick up a normal expression.  */
10068                   my_getExpression (&imm_expr, s);
10069                 }
10070
10071               if (imm_expr.X_op == O_register)
10072                 {
10073                   /* What we thought was an expression turned out to
10074                      be a register.  */
10075
10076                   if (s[0] == '(' && args[1] == '(')
10077                     {
10078                       /* It looks like the expression was omitted
10079                          before a register indirection, which means
10080                          that the expression is implicitly zero.  We
10081                          still set up imm_expr, so that we handle
10082                          explicit extensions correctly.  */
10083                       imm_expr.X_op = O_constant;
10084                       imm_expr.X_add_number = 0;
10085                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10086                       continue;
10087                     }
10088
10089                   break;
10090                 }
10091
10092               /* We need to relax this instruction.  */
10093               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10094               s = expr_end;
10095               continue;
10096
10097             case 'p':
10098             case 'q':
10099             case 'A':
10100             case 'B':
10101             case 'E':
10102               /* We use offset_reloc rather than imm_reloc for the PC
10103                  relative operands.  This lets macros with both
10104                  immediate and address operands work correctly.  */
10105               my_getExpression (&offset_expr, s);
10106
10107               if (offset_expr.X_op == O_register)
10108                 break;
10109
10110               /* We need to relax this instruction.  */
10111               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
10112               s = expr_end;
10113               continue;
10114
10115             case '6':           /* break code */
10116               my_getExpression (&imm_expr, s);
10117               check_absolute_expr (ip, &imm_expr);
10118               if ((unsigned long) imm_expr.X_add_number > 63)
10119                 {
10120                   as_warn (_("Invalid value for `%s' (%lu)"),
10121                            ip->insn_mo->name,
10122                            (unsigned long) imm_expr.X_add_number);
10123                   imm_expr.X_add_number &= 0x3f;
10124                 }
10125               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
10126               imm_expr.X_op = O_absent;
10127               s = expr_end;
10128               continue;
10129
10130             case 'a':           /* 26 bit address */
10131               my_getExpression (&offset_expr, s);
10132               s = expr_end;
10133               *offset_reloc = BFD_RELOC_MIPS16_JMP;
10134               ip->insn_opcode <<= 16;
10135               continue;
10136
10137             case 'l':           /* register list for entry macro */
10138             case 'L':           /* register list for exit macro */
10139               {
10140                 int mask;
10141
10142                 if (c == 'l')
10143                   mask = 0;
10144                 else
10145                   mask = 7 << 3;
10146                 while (*s != '\0')
10147                   {
10148                     int freg, reg1, reg2;
10149
10150                     while (*s == ' ' || *s == ',')
10151                       ++s;
10152                     if (*s != '$')
10153                       {
10154                         as_bad (_("can't parse register list"));
10155                         break;
10156                       }
10157                     ++s;
10158                     if (*s != 'f')
10159                       freg = 0;
10160                     else
10161                       {
10162                         freg = 1;
10163                         ++s;
10164                       }
10165                     reg1 = 0;
10166                     while (ISDIGIT (*s))
10167                       {
10168                         reg1 *= 10;
10169                         reg1 += *s - '0';
10170                         ++s;
10171                       }
10172                     if (*s == ' ')
10173                       ++s;
10174                     if (*s != '-')
10175                       reg2 = reg1;
10176                     else
10177                       {
10178                         ++s;
10179                         if (*s != '$')
10180                           break;
10181                         ++s;
10182                         if (freg)
10183                           {
10184                             if (*s == 'f')
10185                               ++s;
10186                             else
10187                               {
10188                                 as_bad (_("invalid register list"));
10189                                 break;
10190                               }
10191                           }
10192                         reg2 = 0;
10193                         while (ISDIGIT (*s))
10194                           {
10195                             reg2 *= 10;
10196                             reg2 += *s - '0';
10197                             ++s;
10198                           }
10199                       }
10200                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10201                       {
10202                         mask &= ~ (7 << 3);
10203                         mask |= 5 << 3;
10204                       }
10205                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10206                       {
10207                         mask &= ~ (7 << 3);
10208                         mask |= 6 << 3;
10209                       }
10210                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10211                       mask |= (reg2 - 3) << 3;
10212                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10213                       mask |= (reg2 - 15) << 1;
10214                     else if (reg1 == RA && reg2 == RA)
10215                       mask |= 1;
10216                     else
10217                       {
10218                         as_bad (_("invalid register list"));
10219                         break;
10220                       }
10221                   }
10222                 /* The mask is filled in in the opcode table for the
10223                    benefit of the disassembler.  We remove it before
10224                    applying the actual mask.  */
10225                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10226                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10227               }
10228             continue;
10229
10230             case 'e':           /* extend code */
10231               my_getExpression (&imm_expr, s);
10232               check_absolute_expr (ip, &imm_expr);
10233               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10234                 {
10235                   as_warn (_("Invalid value for `%s' (%lu)"),
10236                            ip->insn_mo->name,
10237                            (unsigned long) imm_expr.X_add_number);
10238                   imm_expr.X_add_number &= 0x7ff;
10239                 }
10240               ip->insn_opcode |= imm_expr.X_add_number;
10241               imm_expr.X_op = O_absent;
10242               s = expr_end;
10243               continue;
10244
10245             default:
10246               internalError ();
10247             }
10248           break;
10249         }
10250
10251       /* Args don't match.  */
10252       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10253           strcmp (insn->name, insn[1].name) == 0)
10254         {
10255           ++insn;
10256           s = argsstart;
10257           continue;
10258         }
10259
10260       insn_error = _("illegal operands");
10261
10262       return;
10263     }
10264 }
10265
10266 /* This structure holds information we know about a mips16 immediate
10267    argument type.  */
10268
10269 struct mips16_immed_operand
10270 {
10271   /* The type code used in the argument string in the opcode table.  */
10272   int type;
10273   /* The number of bits in the short form of the opcode.  */
10274   int nbits;
10275   /* The number of bits in the extended form of the opcode.  */
10276   int extbits;
10277   /* The amount by which the short form is shifted when it is used;
10278      for example, the sw instruction has a shift count of 2.  */
10279   int shift;
10280   /* The amount by which the short form is shifted when it is stored
10281      into the instruction code.  */
10282   int op_shift;
10283   /* Non-zero if the short form is unsigned.  */
10284   int unsp;
10285   /* Non-zero if the extended form is unsigned.  */
10286   int extu;
10287   /* Non-zero if the value is PC relative.  */
10288   int pcrel;
10289 };
10290
10291 /* The mips16 immediate operand types.  */
10292
10293 static const struct mips16_immed_operand mips16_immed_operands[] =
10294 {
10295   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10296   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10297   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10298   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10299   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10300   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10301   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10302   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10303   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10304   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10305   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10306   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10307   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10308   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10309   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10310   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10311   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10312   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10313   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10314   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10315   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10316 };
10317
10318 #define MIPS16_NUM_IMMED \
10319   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10320
10321 /* Handle a mips16 instruction with an immediate value.  This or's the
10322    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
10323    whether an extended value is needed; if one is needed, it sets
10324    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
10325    If SMALL is true, an unextended opcode was explicitly requested.
10326    If EXT is true, an extended opcode was explicitly requested.  If
10327    WARN is true, warn if EXT does not match reality.  */
10328
10329 static void
10330 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
10331               extend)
10332      char *file;
10333      unsigned int line;
10334      int type;
10335      offsetT val;
10336      bfd_boolean warn;
10337      bfd_boolean small;
10338      bfd_boolean ext;
10339      unsigned long *insn;
10340      bfd_boolean *use_extend;
10341      unsigned short *extend;
10342 {
10343   register const struct mips16_immed_operand *op;
10344   int mintiny, maxtiny;
10345   bfd_boolean needext;
10346
10347   op = mips16_immed_operands;
10348   while (op->type != type)
10349     {
10350       ++op;
10351       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10352     }
10353
10354   if (op->unsp)
10355     {
10356       if (type == '<' || type == '>' || type == '[' || type == ']')
10357         {
10358           mintiny = 1;
10359           maxtiny = 1 << op->nbits;
10360         }
10361       else
10362         {
10363           mintiny = 0;
10364           maxtiny = (1 << op->nbits) - 1;
10365         }
10366     }
10367   else
10368     {
10369       mintiny = - (1 << (op->nbits - 1));
10370       maxtiny = (1 << (op->nbits - 1)) - 1;
10371     }
10372
10373   /* Branch offsets have an implicit 0 in the lowest bit.  */
10374   if (type == 'p' || type == 'q')
10375     val /= 2;
10376
10377   if ((val & ((1 << op->shift) - 1)) != 0
10378       || val < (mintiny << op->shift)
10379       || val > (maxtiny << op->shift))
10380     needext = TRUE;
10381   else
10382     needext = FALSE;
10383
10384   if (warn && ext && ! needext)
10385     as_warn_where (file, line,
10386                    _("extended operand requested but not required"));
10387   if (small && needext)
10388     as_bad_where (file, line, _("invalid unextended operand value"));
10389
10390   if (small || (! ext && ! needext))
10391     {
10392       int insnval;
10393
10394       *use_extend = FALSE;
10395       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10396       insnval <<= op->op_shift;
10397       *insn |= insnval;
10398     }
10399   else
10400     {
10401       long minext, maxext;
10402       int extval;
10403
10404       if (op->extu)
10405         {
10406           minext = 0;
10407           maxext = (1 << op->extbits) - 1;
10408         }
10409       else
10410         {
10411           minext = - (1 << (op->extbits - 1));
10412           maxext = (1 << (op->extbits - 1)) - 1;
10413         }
10414       if (val < minext || val > maxext)
10415         as_bad_where (file, line,
10416                       _("operand value out of range for instruction"));
10417
10418       *use_extend = TRUE;
10419       if (op->extbits == 16)
10420         {
10421           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10422           val &= 0x1f;
10423         }
10424       else if (op->extbits == 15)
10425         {
10426           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10427           val &= 0xf;
10428         }
10429       else
10430         {
10431           extval = ((val & 0x1f) << 6) | (val & 0x20);
10432           val = 0;
10433         }
10434
10435       *extend = (unsigned short) extval;
10436       *insn |= val;
10437     }
10438 }
10439 \f
10440 static const struct percent_op_match
10441 {
10442   const char *str;
10443   bfd_reloc_code_real_type reloc;
10444 } percent_op[] =
10445 {
10446   {"%lo", BFD_RELOC_LO16},
10447 #ifdef OBJ_ELF
10448   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10449   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10450   {"%call16", BFD_RELOC_MIPS_CALL16},
10451   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10452   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10453   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10454   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10455   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10456   {"%got", BFD_RELOC_MIPS_GOT16},
10457   {"%gp_rel", BFD_RELOC_GPREL16},
10458   {"%half", BFD_RELOC_16},
10459   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10460   {"%higher", BFD_RELOC_MIPS_HIGHER},
10461   {"%neg", BFD_RELOC_MIPS_SUB},
10462 #endif
10463   {"%hi", BFD_RELOC_HI16_S}
10464 };
10465
10466
10467 /* Return true if *STR points to a relocation operator.  When returning true,
10468    move *STR over the operator and store its relocation code in *RELOC.
10469    Leave both *STR and *RELOC alone when returning false.  */
10470
10471 static bfd_boolean
10472 parse_relocation (str, reloc)
10473      char **str;
10474      bfd_reloc_code_real_type *reloc;
10475 {
10476   size_t i;
10477
10478   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10479     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10480       {
10481         *str += strlen (percent_op[i].str);
10482         *reloc = percent_op[i].reloc;
10483
10484         /* Check whether the output BFD supports this relocation.
10485            If not, issue an error and fall back on something safe.  */
10486         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10487           {
10488             as_bad ("relocation %s isn't supported by the current ABI",
10489                     percent_op[i].str);
10490             *reloc = BFD_RELOC_LO16;
10491           }
10492         return TRUE;
10493       }
10494   return FALSE;
10495 }
10496
10497
10498 /* Parse string STR as a 16-bit relocatable operand.  Store the
10499    expression in *EP and the relocations in the array starting
10500    at RELOC.  Return the number of relocation operators used.
10501
10502    On exit, EXPR_END points to the first character after the expression.
10503    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10504
10505 static size_t
10506 my_getSmallExpression (ep, reloc, str)
10507      expressionS *ep;
10508      bfd_reloc_code_real_type *reloc;
10509      char *str;
10510 {
10511   bfd_reloc_code_real_type reversed_reloc[3];
10512   size_t reloc_index, i;
10513   int crux_depth, str_depth;
10514   char *crux;
10515
10516   /* Search for the start of the main expression, recoding relocations
10517      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10518      of the main expression and with CRUX_DEPTH containing the number
10519      of open brackets at that point.  */
10520   reloc_index = -1;
10521   str_depth = 0;
10522   do
10523     {
10524       reloc_index++;
10525       crux = str;
10526       crux_depth = str_depth;
10527
10528       /* Skip over whitespace and brackets, keeping count of the number
10529          of brackets.  */
10530       while (*str == ' ' || *str == '\t' || *str == '(')
10531         if (*str++ == '(')
10532           str_depth++;
10533     }
10534   while (*str == '%'
10535          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10536          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10537
10538   my_getExpression (ep, crux);
10539   str = expr_end;
10540
10541   /* Match every open bracket.  */
10542   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10543     if (*str++ == ')')
10544       crux_depth--;
10545
10546   if (crux_depth > 0)
10547     as_bad ("unclosed '('");
10548
10549   expr_end = str;
10550
10551   if (reloc_index == 0)
10552     reloc[0] = BFD_RELOC_LO16;
10553   else
10554     {
10555       prev_reloc_op_frag = frag_now;
10556       for (i = 0; i < reloc_index; i++)
10557         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10558     }
10559
10560   return reloc_index;
10561 }
10562
10563 static void
10564 my_getExpression (ep, str)
10565      expressionS *ep;
10566      char *str;
10567 {
10568   char *save_in;
10569   valueT val;
10570
10571   save_in = input_line_pointer;
10572   input_line_pointer = str;
10573   expression (ep);
10574   expr_end = input_line_pointer;
10575   input_line_pointer = save_in;
10576
10577   /* If we are in mips16 mode, and this is an expression based on `.',
10578      then we bump the value of the symbol by 1 since that is how other
10579      text symbols are handled.  We don't bother to handle complex
10580      expressions, just `.' plus or minus a constant.  */
10581   if (mips_opts.mips16
10582       && ep->X_op == O_symbol
10583       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10584       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10585       && symbol_get_frag (ep->X_add_symbol) == frag_now
10586       && symbol_constant_p (ep->X_add_symbol)
10587       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10588     S_SET_VALUE (ep->X_add_symbol, val + 1);
10589 }
10590
10591 /* Turn a string in input_line_pointer into a floating point constant
10592    of type TYPE, and store the appropriate bytes in *LITP.  The number
10593    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10594    returned, or NULL on OK.  */
10595
10596 char *
10597 md_atof (type, litP, sizeP)
10598      int type;
10599      char *litP;
10600      int *sizeP;
10601 {
10602   int prec;
10603   LITTLENUM_TYPE words[4];
10604   char *t;
10605   int i;
10606
10607   switch (type)
10608     {
10609     case 'f':
10610       prec = 2;
10611       break;
10612
10613     case 'd':
10614       prec = 4;
10615       break;
10616
10617     default:
10618       *sizeP = 0;
10619       return _("bad call to md_atof");
10620     }
10621
10622   t = atof_ieee (input_line_pointer, type, words);
10623   if (t)
10624     input_line_pointer = t;
10625
10626   *sizeP = prec * 2;
10627
10628   if (! target_big_endian)
10629     {
10630       for (i = prec - 1; i >= 0; i--)
10631         {
10632           md_number_to_chars (litP, (valueT) words[i], 2);
10633           litP += 2;
10634         }
10635     }
10636   else
10637     {
10638       for (i = 0; i < prec; i++)
10639         {
10640           md_number_to_chars (litP, (valueT) words[i], 2);
10641           litP += 2;
10642         }
10643     }
10644
10645   return NULL;
10646 }
10647
10648 void
10649 md_number_to_chars (buf, val, n)
10650      char *buf;
10651      valueT val;
10652      int n;
10653 {
10654   if (target_big_endian)
10655     number_to_chars_bigendian (buf, val, n);
10656   else
10657     number_to_chars_littleendian (buf, val, n);
10658 }
10659 \f
10660 #ifdef OBJ_ELF
10661 static int support_64bit_objects(void)
10662 {
10663   const char **list, **l;
10664   int yes;
10665
10666   list = bfd_target_list ();
10667   for (l = list; *l != NULL; l++)
10668 #ifdef TE_TMIPS
10669     /* This is traditional mips */
10670     if (strcmp (*l, "elf64-tradbigmips") == 0
10671         || strcmp (*l, "elf64-tradlittlemips") == 0)
10672 #else
10673     if (strcmp (*l, "elf64-bigmips") == 0
10674         || strcmp (*l, "elf64-littlemips") == 0)
10675 #endif
10676       break;
10677   yes = (*l != NULL);
10678   free (list);
10679   return yes;
10680 }
10681 #endif /* OBJ_ELF */
10682
10683 const char *md_shortopts = "nO::g::G:";
10684
10685 struct option md_longopts[] =
10686 {
10687   /* Options which specify architecture.  */
10688 #define OPTION_ARCH_BASE    (OPTION_MD_BASE)
10689 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10690   {"march", required_argument, NULL, OPTION_MARCH},
10691 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10692   {"mtune", required_argument, NULL, OPTION_MTUNE},
10693 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10694   {"mips0", no_argument, NULL, OPTION_MIPS1},
10695   {"mips1", no_argument, NULL, OPTION_MIPS1},
10696 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10697   {"mips2", no_argument, NULL, OPTION_MIPS2},
10698 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10699   {"mips3", no_argument, NULL, OPTION_MIPS3},
10700 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10701   {"mips4", no_argument, NULL, OPTION_MIPS4},
10702 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10703   {"mips5", no_argument, NULL, OPTION_MIPS5},
10704 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10705   {"mips32", no_argument, NULL, OPTION_MIPS32},
10706 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10707   {"mips64", no_argument, NULL, OPTION_MIPS64},
10708 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10709   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10710
10711   /* Options which specify Application Specific Extensions (ASEs).  */
10712 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 10)
10713 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10714   {"mips16", no_argument, NULL, OPTION_MIPS16},
10715 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10716   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10717 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10718   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10719 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10720   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10721 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10722   {"mdmx", no_argument, NULL, OPTION_MDMX},
10723 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10724   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10725
10726   /* Old-style architecture options.  Don't add more of these.  */
10727 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10728 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10729   {"m4650", no_argument, NULL, OPTION_M4650},
10730 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10731   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10732 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10733   {"m4010", no_argument, NULL, OPTION_M4010},
10734 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10735   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10736 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10737   {"m4100", no_argument, NULL, OPTION_M4100},
10738 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10739   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10740 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10741   {"m3900", no_argument, NULL, OPTION_M3900},
10742 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10743   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10744
10745   /* Options which enable bug fixes.  */
10746 #define OPTION_FIX_BASE    (OPTION_COMPAT_ARCH_BASE + 8)
10747 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10748   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10749 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10750   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10751   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10752 #define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10753 #define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10754   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10755   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10756
10757   /* Miscellaneous options.  */
10758 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10759 #define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
10760   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10761 #define OPTION_TRAP (OPTION_MISC_BASE + 1)
10762   {"trap", no_argument, NULL, OPTION_TRAP},
10763   {"no-break", no_argument, NULL, OPTION_TRAP},
10764 #define OPTION_BREAK (OPTION_MISC_BASE + 2)
10765   {"break", no_argument, NULL, OPTION_BREAK},
10766   {"no-trap", no_argument, NULL, OPTION_BREAK},
10767 #define OPTION_EB (OPTION_MISC_BASE + 3)
10768   {"EB", no_argument, NULL, OPTION_EB},
10769 #define OPTION_EL (OPTION_MISC_BASE + 4)
10770   {"EL", no_argument, NULL, OPTION_EL},
10771 #define OPTION_FP32 (OPTION_MISC_BASE + 5)
10772   {"mfp32", no_argument, NULL, OPTION_FP32},
10773 #define OPTION_GP32 (OPTION_MISC_BASE + 6)
10774   {"mgp32", no_argument, NULL, OPTION_GP32},
10775 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10776   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10777 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
10778   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10779 #define OPTION_FP64 (OPTION_MISC_BASE + 9)
10780   {"mfp64", no_argument, NULL, OPTION_FP64},
10781 #define OPTION_GP64 (OPTION_MISC_BASE + 10)
10782   {"mgp64", no_argument, NULL, OPTION_GP64},
10783 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10784 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
10785   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10786   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10787
10788   /* ELF-specific options.  */
10789 #ifdef OBJ_ELF
10790 #define OPTION_ELF_BASE    (OPTION_MISC_BASE + 13)
10791 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10792   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10793   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10794 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10795   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10796 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10797   {"xgot",        no_argument, NULL, OPTION_XGOT},
10798 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10799   {"mabi", required_argument, NULL, OPTION_MABI},
10800 #define OPTION_32          (OPTION_ELF_BASE + 4)
10801   {"32",          no_argument, NULL, OPTION_32},
10802 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10803   {"n32",         no_argument, NULL, OPTION_N32},
10804 #define OPTION_64          (OPTION_ELF_BASE + 6)
10805   {"64",          no_argument, NULL, OPTION_64},
10806 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10807   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10808 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10809   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10810 #endif /* OBJ_ELF */
10811
10812   {NULL, no_argument, NULL, 0}
10813 };
10814 size_t md_longopts_size = sizeof (md_longopts);
10815
10816 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10817    NEW_VALUE.  Warn if another value was already specified.  Note:
10818    we have to defer parsing the -march and -mtune arguments in order
10819    to handle 'from-abi' correctly, since the ABI might be specified
10820    in a later argument.  */
10821
10822 static void
10823 mips_set_option_string (string_ptr, new_value)
10824      const char **string_ptr, *new_value;
10825 {
10826   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10827     as_warn (_("A different %s was already specified, is now %s"),
10828              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10829              new_value);
10830
10831   *string_ptr = new_value;
10832 }
10833
10834 int
10835 md_parse_option (c, arg)
10836      int c;
10837      char *arg;
10838 {
10839   switch (c)
10840     {
10841     case OPTION_CONSTRUCT_FLOATS:
10842       mips_disable_float_construction = 0;
10843       break;
10844
10845     case OPTION_NO_CONSTRUCT_FLOATS:
10846       mips_disable_float_construction = 1;
10847       break;
10848
10849     case OPTION_TRAP:
10850       mips_trap = 1;
10851       break;
10852
10853     case OPTION_BREAK:
10854       mips_trap = 0;
10855       break;
10856
10857     case OPTION_EB:
10858       target_big_endian = 1;
10859       break;
10860
10861     case OPTION_EL:
10862       target_big_endian = 0;
10863       break;
10864
10865     case 'n':
10866       warn_nops = 1;
10867       break;
10868
10869     case 'O':
10870       if (arg && arg[1] == '0')
10871         mips_optimize = 1;
10872       else
10873         mips_optimize = 2;
10874       break;
10875
10876     case 'g':
10877       if (arg == NULL)
10878         mips_debug = 2;
10879       else
10880         mips_debug = atoi (arg);
10881       /* When the MIPS assembler sees -g or -g2, it does not do
10882          optimizations which limit full symbolic debugging.  We take
10883          that to be equivalent to -O0.  */
10884       if (mips_debug == 2)
10885         mips_optimize = 1;
10886       break;
10887
10888     case OPTION_MIPS1:
10889       file_mips_isa = ISA_MIPS1;
10890       break;
10891
10892     case OPTION_MIPS2:
10893       file_mips_isa = ISA_MIPS2;
10894       break;
10895
10896     case OPTION_MIPS3:
10897       file_mips_isa = ISA_MIPS3;
10898       break;
10899
10900     case OPTION_MIPS4:
10901       file_mips_isa = ISA_MIPS4;
10902       break;
10903
10904     case OPTION_MIPS5:
10905       file_mips_isa = ISA_MIPS5;
10906       break;
10907
10908     case OPTION_MIPS32:
10909       file_mips_isa = ISA_MIPS32;
10910       break;
10911
10912     case OPTION_MIPS32R2:
10913       file_mips_isa = ISA_MIPS32R2;
10914       break;
10915
10916     case OPTION_MIPS64:
10917       file_mips_isa = ISA_MIPS64;
10918       break;
10919
10920     case OPTION_MTUNE:
10921       mips_set_option_string (&mips_tune_string, arg);
10922       break;
10923
10924     case OPTION_MARCH:
10925       mips_set_option_string (&mips_arch_string, arg);
10926       break;
10927
10928     case OPTION_M4650:
10929       mips_set_option_string (&mips_arch_string, "4650");
10930       mips_set_option_string (&mips_tune_string, "4650");
10931       break;
10932
10933     case OPTION_NO_M4650:
10934       break;
10935
10936     case OPTION_M4010:
10937       mips_set_option_string (&mips_arch_string, "4010");
10938       mips_set_option_string (&mips_tune_string, "4010");
10939       break;
10940
10941     case OPTION_NO_M4010:
10942       break;
10943
10944     case OPTION_M4100:
10945       mips_set_option_string (&mips_arch_string, "4100");
10946       mips_set_option_string (&mips_tune_string, "4100");
10947       break;
10948
10949     case OPTION_NO_M4100:
10950       break;
10951
10952     case OPTION_M3900:
10953       mips_set_option_string (&mips_arch_string, "3900");
10954       mips_set_option_string (&mips_tune_string, "3900");
10955       break;
10956
10957     case OPTION_NO_M3900:
10958       break;
10959
10960     case OPTION_MDMX:
10961       mips_opts.ase_mdmx = 1;
10962       break;
10963
10964     case OPTION_NO_MDMX:
10965       mips_opts.ase_mdmx = 0;
10966       break;
10967
10968     case OPTION_MIPS16:
10969       mips_opts.mips16 = 1;
10970       mips_no_prev_insn (FALSE);
10971       break;
10972
10973     case OPTION_NO_MIPS16:
10974       mips_opts.mips16 = 0;
10975       mips_no_prev_insn (FALSE);
10976       break;
10977
10978     case OPTION_MIPS3D:
10979       mips_opts.ase_mips3d = 1;
10980       break;
10981
10982     case OPTION_NO_MIPS3D:
10983       mips_opts.ase_mips3d = 0;
10984       break;
10985
10986     case OPTION_MEMBEDDED_PIC:
10987       mips_pic = EMBEDDED_PIC;
10988       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10989         {
10990           as_bad (_("-G may not be used with embedded PIC code"));
10991           return 0;
10992         }
10993       g_switch_value = 0x7fffffff;
10994       break;
10995
10996     case OPTION_FIX_VR4122:
10997       mips_fix_4122_bugs = 1;
10998       break;
10999
11000     case OPTION_NO_FIX_VR4122:
11001       mips_fix_4122_bugs = 0;
11002       break;
11003
11004     case OPTION_RELAX_BRANCH:
11005       mips_relax_branch = 1;
11006       break;
11007
11008     case OPTION_NO_RELAX_BRANCH:
11009       mips_relax_branch = 0;
11010       break;
11011
11012 #ifdef OBJ_ELF
11013       /* When generating ELF code, we permit -KPIC and -call_shared to
11014          select SVR4_PIC, and -non_shared to select no PIC.  This is
11015          intended to be compatible with Irix 5.  */
11016     case OPTION_CALL_SHARED:
11017       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11018         {
11019           as_bad (_("-call_shared is supported only for ELF format"));
11020           return 0;
11021         }
11022       mips_pic = SVR4_PIC;
11023       mips_abicalls = TRUE;
11024       if (g_switch_seen && g_switch_value != 0)
11025         {
11026           as_bad (_("-G may not be used with SVR4 PIC code"));
11027           return 0;
11028         }
11029       g_switch_value = 0;
11030       break;
11031
11032     case OPTION_NON_SHARED:
11033       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11034         {
11035           as_bad (_("-non_shared is supported only for ELF format"));
11036           return 0;
11037         }
11038       mips_pic = NO_PIC;
11039       mips_abicalls = FALSE;
11040       break;
11041
11042       /* The -xgot option tells the assembler to use 32 offsets when
11043          accessing the got in SVR4_PIC mode.  It is for Irix
11044          compatibility.  */
11045     case OPTION_XGOT:
11046       mips_big_got = 1;
11047       break;
11048 #endif /* OBJ_ELF */
11049
11050     case 'G':
11051       if (! USE_GLOBAL_POINTER_OPT)
11052         {
11053           as_bad (_("-G is not supported for this configuration"));
11054           return 0;
11055         }
11056       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
11057         {
11058           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
11059           return 0;
11060         }
11061       else
11062         g_switch_value = atoi (arg);
11063       g_switch_seen = 1;
11064       break;
11065
11066 #ifdef OBJ_ELF
11067       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11068          and -mabi=64.  */
11069     case OPTION_32:
11070       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11071         {
11072           as_bad (_("-32 is supported for ELF format only"));
11073           return 0;
11074         }
11075       mips_abi = O32_ABI;
11076       break;
11077
11078     case OPTION_N32:
11079       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11080         {
11081           as_bad (_("-n32 is supported for ELF format only"));
11082           return 0;
11083         }
11084       mips_abi = N32_ABI;
11085       break;
11086
11087     case OPTION_64:
11088       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11089         {
11090           as_bad (_("-64 is supported for ELF format only"));
11091           return 0;
11092         }
11093       mips_abi = N64_ABI;
11094       if (! support_64bit_objects())
11095         as_fatal (_("No compiled in support for 64 bit object file format"));
11096       break;
11097 #endif /* OBJ_ELF */
11098
11099     case OPTION_GP32:
11100       file_mips_gp32 = 1;
11101       break;
11102
11103     case OPTION_GP64:
11104       file_mips_gp32 = 0;
11105       break;
11106
11107     case OPTION_FP32:
11108       file_mips_fp32 = 1;
11109       break;
11110
11111     case OPTION_FP64:
11112       file_mips_fp32 = 0;
11113       break;
11114
11115 #ifdef OBJ_ELF
11116     case OPTION_MABI:
11117       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11118         {
11119           as_bad (_("-mabi is supported for ELF format only"));
11120           return 0;
11121         }
11122       if (strcmp (arg, "32") == 0)
11123         mips_abi = O32_ABI;
11124       else if (strcmp (arg, "o64") == 0)
11125         mips_abi = O64_ABI;
11126       else if (strcmp (arg, "n32") == 0)
11127         mips_abi = N32_ABI;
11128       else if (strcmp (arg, "64") == 0)
11129         {
11130           mips_abi = N64_ABI;
11131           if (! support_64bit_objects())
11132             as_fatal (_("No compiled in support for 64 bit object file "
11133                         "format"));
11134         }
11135       else if (strcmp (arg, "eabi") == 0)
11136         mips_abi = EABI_ABI;
11137       else
11138         {
11139           as_fatal (_("invalid abi -mabi=%s"), arg);
11140           return 0;
11141         }
11142       break;
11143 #endif /* OBJ_ELF */
11144
11145     case OPTION_M7000_HILO_FIX:
11146       mips_7000_hilo_fix = TRUE;
11147       break;
11148
11149     case OPTION_MNO_7000_HILO_FIX:
11150       mips_7000_hilo_fix = FALSE;
11151       break;
11152
11153 #ifdef OBJ_ELF
11154     case OPTION_MDEBUG:
11155       mips_flag_mdebug = TRUE;
11156       break;
11157
11158     case OPTION_NO_MDEBUG:
11159       mips_flag_mdebug = FALSE;
11160       break;
11161 #endif /* OBJ_ELF */
11162
11163     default:
11164       return 0;
11165     }
11166
11167   return 1;
11168 }
11169 \f
11170 /* Set up globals to generate code for the ISA or processor
11171    described by INFO.  */
11172
11173 static void
11174 mips_set_architecture (info)
11175      const struct mips_cpu_info *info;
11176 {
11177   if (info != 0)
11178     {
11179       mips_arch_info = info;
11180       mips_arch = info->cpu;
11181       mips_opts.isa = info->isa;
11182     }
11183 }
11184
11185
11186 /* Likewise for tuning.  */
11187
11188 static void
11189 mips_set_tune (info)
11190      const struct mips_cpu_info *info;
11191 {
11192   if (info != 0)
11193     {
11194       mips_tune_info = info;
11195       mips_tune = info->cpu;
11196     }
11197 }
11198
11199
11200 void
11201 mips_after_parse_args ()
11202 {
11203   /* GP relative stuff not working for PE */
11204   if (strncmp (TARGET_OS, "pe", 2) == 0
11205       && g_switch_value != 0)
11206     {
11207       if (g_switch_seen)
11208         as_bad (_("-G not supported in this configuration."));
11209       g_switch_value = 0;
11210     }
11211
11212   if (mips_abi == NO_ABI)
11213     mips_abi = MIPS_DEFAULT_ABI;
11214
11215   /* The following code determines the architecture and register size.
11216      Similar code was added to GCC 3.3 (see override_options() in
11217      config/mips/mips.c).  The GAS and GCC code should be kept in sync
11218      as much as possible.  */
11219
11220   if (mips_arch_string != 0)
11221     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
11222
11223   if (mips_tune_string != 0)
11224     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
11225
11226   if (file_mips_isa != ISA_UNKNOWN)
11227     {
11228       /* Handle -mipsN.  At this point, file_mips_isa contains the
11229          ISA level specified by -mipsN, while mips_opts.isa contains
11230          the -march selection (if any).  */
11231       if (mips_arch_info != 0)
11232         {
11233           /* -march takes precedence over -mipsN, since it is more descriptive.
11234              There's no harm in specifying both as long as the ISA levels
11235              are the same.  */
11236           if (file_mips_isa != mips_opts.isa)
11237             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11238                     mips_cpu_info_from_isa (file_mips_isa)->name,
11239                     mips_cpu_info_from_isa (mips_opts.isa)->name);
11240         }
11241       else
11242         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
11243     }
11244
11245   if (mips_arch_info == 0)
11246     mips_set_architecture (mips_parse_cpu ("default CPU",
11247                                            MIPS_CPU_STRING_DEFAULT));
11248
11249   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11250     as_bad ("-march=%s is not compatible with the selected ABI",
11251             mips_arch_info->name);
11252
11253   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
11254   if (mips_tune_info == 0)
11255     mips_set_tune (mips_arch_info);
11256
11257   if (file_mips_gp32 >= 0)
11258     {
11259       /* The user specified the size of the integer registers.  Make sure
11260          it agrees with the ABI and ISA.  */
11261       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11262         as_bad (_("-mgp64 used with a 32-bit processor"));
11263       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11264         as_bad (_("-mgp32 used with a 64-bit ABI"));
11265       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11266         as_bad (_("-mgp64 used with a 32-bit ABI"));
11267     }
11268   else
11269     {
11270       /* Infer the integer register size from the ABI and processor.
11271          Restrict ourselves to 32-bit registers if that's all the
11272          processor has, or if the ABI cannot handle 64-bit registers.  */
11273       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11274                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11275     }
11276
11277   /* ??? GAS treats single-float processors as though they had 64-bit
11278      float registers (although it complains when double-precision
11279      instructions are used).  As things stand, saying they have 32-bit
11280      registers would lead to spurious "register must be even" messages.
11281      So here we assume float registers are always the same size as
11282      integer ones, unless the user says otherwise.  */
11283   if (file_mips_fp32 < 0)
11284     file_mips_fp32 = file_mips_gp32;
11285
11286   /* End of GCC-shared inference code.  */
11287
11288   /* ??? When do we want this flag to be set?   Who uses it?  */
11289   if (file_mips_gp32 == 1
11290       && mips_abi == NO_ABI
11291       && ISA_HAS_64BIT_REGS (mips_opts.isa))
11292     mips_32bitmode = 1;
11293
11294   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11295     as_bad (_("trap exception not supported at ISA 1"));
11296
11297   /* If the selected architecture includes support for ASEs, enable
11298      generation of code for them.  */
11299   if (mips_opts.mips16 == -1)
11300     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
11301   if (mips_opts.ase_mips3d == -1)
11302     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
11303   if (mips_opts.ase_mdmx == -1)
11304     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
11305
11306   file_mips_isa = mips_opts.isa;
11307   file_ase_mips16 = mips_opts.mips16;
11308   file_ase_mips3d = mips_opts.ase_mips3d;
11309   file_ase_mdmx = mips_opts.ase_mdmx;
11310   mips_opts.gp32 = file_mips_gp32;
11311   mips_opts.fp32 = file_mips_fp32;
11312
11313   if (mips_flag_mdebug < 0)
11314     {
11315 #ifdef OBJ_MAYBE_ECOFF
11316       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11317         mips_flag_mdebug = 1;
11318       else
11319 #endif /* OBJ_MAYBE_ECOFF */
11320         mips_flag_mdebug = 0;
11321     }
11322 }
11323 \f
11324 void
11325 mips_init_after_args ()
11326 {
11327   /* initialize opcodes */
11328   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11329   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11330 }
11331
11332 long
11333 md_pcrel_from (fixP)
11334      fixS *fixP;
11335 {
11336   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11337   switch (fixP->fx_r_type)
11338     {
11339     case BFD_RELOC_16_PCREL_S2:
11340     case BFD_RELOC_MIPS_JMP:
11341       /* Return the address of the delay slot.  */
11342       return addr + 4;
11343     default:
11344       return addr;
11345     }
11346 }
11347
11348 /* This is called before the symbol table is processed.  In order to
11349    work with gcc when using mips-tfile, we must keep all local labels.
11350    However, in other cases, we want to discard them.  If we were
11351    called with -g, but we didn't see any debugging information, it may
11352    mean that gcc is smuggling debugging information through to
11353    mips-tfile, in which case we must generate all local labels.  */
11354
11355 void
11356 mips_frob_file_before_adjust ()
11357 {
11358 #ifndef NO_ECOFF_DEBUGGING
11359   if (ECOFF_DEBUGGING
11360       && mips_debug != 0
11361       && ! ecoff_debugging_seen)
11362     flag_keep_locals = 1;
11363 #endif
11364 }
11365
11366 /* Sort any unmatched HI16_S relocs so that they immediately precede
11367    the corresponding LO reloc.  This is called before md_apply_fix3 and
11368    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
11369    explicit use of the %hi modifier.  */
11370
11371 void
11372 mips_frob_file ()
11373 {
11374   struct mips_hi_fixup *l;
11375
11376   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11377     {
11378       segment_info_type *seginfo;
11379       int pass;
11380
11381       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11382
11383       /* If a GOT16 relocation turns out to be against a global symbol,
11384          there isn't supposed to be a matching LO.  */
11385       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11386           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11387         continue;
11388
11389       /* Check quickly whether the next fixup happens to be a matching %lo.  */
11390       if (fixup_has_matching_lo_p (l->fixp))
11391         continue;
11392
11393       /* Look through the fixups for this segment for a matching %lo.
11394          When we find one, move the %hi just in front of it.  We do
11395          this in two passes.  In the first pass, we try to find a
11396          unique %lo.  In the second pass, we permit multiple %hi
11397          relocs for a single %lo (this is a GNU extension).  */
11398       seginfo = seg_info (l->seg);
11399       for (pass = 0; pass < 2; pass++)
11400         {
11401           fixS *f, *prev;
11402
11403           prev = NULL;
11404           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11405             {
11406               /* Check whether this is a %lo fixup which matches l->fixp.  */
11407               if (f->fx_r_type == BFD_RELOC_LO16
11408                   && f->fx_addsy == l->fixp->fx_addsy
11409                   && f->fx_offset == l->fixp->fx_offset
11410                   && (pass == 1
11411                       || prev == NULL
11412                       || !reloc_needs_lo_p (prev->fx_r_type)
11413                       || !fixup_has_matching_lo_p (prev)))
11414                 {
11415                   fixS **pf;
11416
11417                   /* Move l->fixp before f.  */
11418                   for (pf = &seginfo->fix_root;
11419                        *pf != l->fixp;
11420                        pf = &(*pf)->fx_next)
11421                     assert (*pf != NULL);
11422
11423                   *pf = l->fixp->fx_next;
11424
11425                   l->fixp->fx_next = f;
11426                   if (prev == NULL)
11427                     seginfo->fix_root = l->fixp;
11428                   else
11429                     prev->fx_next = l->fixp;
11430
11431                   break;
11432                 }
11433
11434               prev = f;
11435             }
11436
11437           if (f != NULL)
11438             break;
11439
11440 #if 0 /* GCC code motion plus incomplete dead code elimination
11441          can leave a %hi without a %lo.  */
11442           if (pass == 1)
11443             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11444                            _("Unmatched %%hi reloc"));
11445 #endif
11446         }
11447     }
11448 }
11449
11450 /* When generating embedded PIC code we need to use a special
11451    relocation to represent the difference of two symbols in the .text
11452    section (switch tables use a difference of this sort).  See
11453    include/coff/mips.h for details.  This macro checks whether this
11454    fixup requires the special reloc.  */
11455 #define SWITCH_TABLE(fixp) \
11456   ((fixp)->fx_r_type == BFD_RELOC_32 \
11457    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11458    && (fixp)->fx_addsy != NULL \
11459    && (fixp)->fx_subsy != NULL \
11460    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11461    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11462
11463 /* When generating embedded PIC code we must keep all PC relative
11464    relocations, in case the linker has to relax a call.  We also need
11465    to keep relocations for switch table entries.
11466
11467    We may have combined relocations without symbols in the N32/N64 ABI.
11468    We have to prevent gas from dropping them.  */
11469
11470 int
11471 mips_force_relocation (fixp)
11472      fixS *fixp;
11473 {
11474   if (generic_force_reloc (fixp))
11475     return 1;
11476
11477   if (HAVE_NEWABI
11478       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11479       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11480           || fixp->fx_r_type == BFD_RELOC_HI16_S
11481           || fixp->fx_r_type == BFD_RELOC_LO16))
11482     return 1;
11483
11484   return (mips_pic == EMBEDDED_PIC
11485           && (fixp->fx_pcrel
11486               || SWITCH_TABLE (fixp)
11487               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11488               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11489 }
11490
11491 /* This hook is called before a fix is simplified.  We don't really
11492    decide whether to skip a fix here.  Rather, we turn global symbols
11493    used as branch targets into local symbols, such that they undergo
11494    simplification.  We can only do this if the symbol is defined and
11495    it is in the same section as the branch.  If this doesn't hold, we
11496    emit a better error message than just saying the relocation is not
11497    valid for the selected object format.
11498
11499    FIXP is the fix-up we're going to try to simplify, SEG is the
11500    segment in which the fix up occurs.  The return value should be
11501    non-zero to indicate the fix-up is valid for further
11502    simplifications.  */
11503
11504 int
11505 mips_validate_fix (fixP, seg)
11506      struct fix *fixP;
11507      asection *seg;
11508 {
11509   /* There's a lot of discussion on whether it should be possible to
11510      use R_MIPS_PC16 to represent branch relocations.  The outcome
11511      seems to be that it can, but gas/bfd are very broken in creating
11512      RELA relocations for this, so for now we only accept branches to
11513      symbols in the same section.  Anything else is of dubious value,
11514      since there's no guarantee that at link time the symbol would be
11515      in range.  Even for branches to local symbols this is arguably
11516      wrong, since it we assume the symbol is not going to be
11517      overridden, which should be possible per ELF library semantics,
11518      but then, there isn't a dynamic relocation that could be used to
11519      this effect, and the target would likely be out of range as well.
11520
11521      Unfortunately, it seems that there is too much code out there
11522      that relies on branches to symbols that are global to be resolved
11523      as if they were local, like the IRIX tools do, so we do it as
11524      well, but with a warning so that people are reminded to fix their
11525      code.  If we ever get back to using R_MIPS_PC16 for branch
11526      targets, this entire block should go away (and probably the
11527      whole function).  */
11528
11529   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11530       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11531             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11532            && mips_pic != EMBEDDED_PIC)
11533           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11534       && fixP->fx_addsy)
11535     {
11536       if (! S_IS_DEFINED (fixP->fx_addsy))
11537         {
11538           as_bad_where (fixP->fx_file, fixP->fx_line,
11539                         _("Cannot branch to undefined symbol."));
11540           /* Avoid any further errors about this fixup.  */
11541           fixP->fx_done = 1;
11542         }
11543       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11544         {
11545           as_bad_where (fixP->fx_file, fixP->fx_line,
11546                         _("Cannot branch to symbol in another section."));
11547           fixP->fx_done = 1;
11548         }
11549       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11550         {
11551           symbolS *sym = fixP->fx_addsy;
11552
11553           as_warn_where (fixP->fx_file, fixP->fx_line,
11554                          _("Pretending global symbol used as branch target is local."));
11555
11556           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11557                                           S_GET_SEGMENT (sym),
11558                                           S_GET_VALUE (sym),
11559                                           symbol_get_frag (sym));
11560           copy_symbol_attributes (fixP->fx_addsy, sym);
11561           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11562           assert (symbol_resolved_p (sym));
11563           symbol_mark_resolved (fixP->fx_addsy);
11564         }
11565     }
11566
11567   return 1;
11568 }
11569
11570 #ifdef OBJ_ELF
11571 static int
11572 mips_need_elf_addend_fixup (fixP)
11573      fixS *fixP;
11574 {
11575   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11576     return 1;
11577   if (mips_pic == EMBEDDED_PIC
11578       && S_IS_WEAK (fixP->fx_addsy))
11579     return 1;
11580   if (mips_pic != EMBEDDED_PIC
11581       && (S_IS_WEAK (fixP->fx_addsy)
11582           || S_IS_EXTERNAL (fixP->fx_addsy))
11583       && !S_IS_COMMON (fixP->fx_addsy))
11584     return 1;
11585   if (((bfd_get_section_flags (stdoutput,
11586                                S_GET_SEGMENT (fixP->fx_addsy))
11587         & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11588       || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11589                    ".gnu.linkonce",
11590                    sizeof (".gnu.linkonce") - 1))
11591     return 1;
11592   return 0;
11593 }
11594 #endif
11595
11596 /* Apply a fixup to the object file.  */
11597
11598 void
11599 md_apply_fix3 (fixP, valP, seg)
11600      fixS *fixP;
11601      valueT *valP;
11602      segT seg ATTRIBUTE_UNUSED;
11603 {
11604   bfd_byte *buf;
11605   long insn;
11606   static int previous_fx_r_type = 0;
11607   reloc_howto_type *howto;
11608
11609   /* We ignore generic BFD relocations we don't know about.  */
11610   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11611   if (! howto)
11612     return;
11613
11614   assert (fixP->fx_size == 4
11615           || fixP->fx_r_type == BFD_RELOC_16
11616           || fixP->fx_r_type == BFD_RELOC_64
11617           || fixP->fx_r_type == BFD_RELOC_CTOR
11618           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11619           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11620           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11621
11622   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11623
11624   /* If we aren't adjusting this fixup to be against the section
11625      symbol, we need to adjust the value.  */
11626 #ifdef OBJ_ELF
11627   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11628     {
11629       if (mips_need_elf_addend_fixup (fixP)
11630           && howto->partial_inplace
11631           && fixP->fx_r_type != BFD_RELOC_GPREL16
11632           && fixP->fx_r_type != BFD_RELOC_GPREL32
11633           && fixP->fx_r_type != BFD_RELOC_MIPS16_GPREL)
11634         {
11635           /* In this case, the bfd_install_relocation routine will
11636              incorrectly add the symbol value back in.  We just want
11637              the addend to appear in the object file.
11638
11639              The condition above used to include
11640              "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11641
11642              However, howto can't be trusted here, because we
11643              might change the reloc type in tc_gen_reloc.  We can
11644              check howto->partial_inplace because that conversion
11645              happens to preserve howto->partial_inplace; but it
11646              does not preserve howto->pcrel_offset.  I've just
11647              eliminated the check, because all MIPS PC-relative
11648              relocations are marked howto->pcrel_offset.
11649
11650              howto->pcrel_offset was originally added for
11651              R_MIPS_PC16, which is generated for code like
11652
11653                     globl g1 .text
11654                     .text
11655                     .space 20
11656              g1:
11657              x:
11658                     bal g1
11659            */
11660           *valP -= S_GET_VALUE (fixP->fx_addsy);
11661         }
11662
11663       /* This code was generated using trial and error and so is
11664          fragile and not trustworthy.  If you change it, you should
11665          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11666          they still pass.  */
11667       if (fixP->fx_pcrel)
11668         {
11669           *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11670
11671           /* BFD's REL handling, for MIPS, is _very_ weird.
11672              This gives the right results, but it can't possibly
11673              be the way things are supposed to work.  */
11674           *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11675         }
11676     }
11677 #endif
11678
11679   /* We are not done if this is a composite relocation to set up gp.  */
11680   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11681       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11682            || (fixP->fx_r_type == BFD_RELOC_64
11683                && (previous_fx_r_type == BFD_RELOC_GPREL32
11684                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11685            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11686                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11687                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11688     fixP->fx_done = 1;
11689   previous_fx_r_type = fixP->fx_r_type;
11690
11691   switch (fixP->fx_r_type)
11692     {
11693     case BFD_RELOC_MIPS_JMP:
11694     case BFD_RELOC_MIPS_SHIFT5:
11695     case BFD_RELOC_MIPS_SHIFT6:
11696     case BFD_RELOC_MIPS_GOT_DISP:
11697     case BFD_RELOC_MIPS_GOT_PAGE:
11698     case BFD_RELOC_MIPS_GOT_OFST:
11699     case BFD_RELOC_MIPS_SUB:
11700     case BFD_RELOC_MIPS_INSERT_A:
11701     case BFD_RELOC_MIPS_INSERT_B:
11702     case BFD_RELOC_MIPS_DELETE:
11703     case BFD_RELOC_MIPS_HIGHEST:
11704     case BFD_RELOC_MIPS_HIGHER:
11705     case BFD_RELOC_MIPS_SCN_DISP:
11706     case BFD_RELOC_MIPS_REL16:
11707     case BFD_RELOC_MIPS_RELGOT:
11708     case BFD_RELOC_MIPS_JALR:
11709     case BFD_RELOC_HI16:
11710     case BFD_RELOC_HI16_S:
11711     case BFD_RELOC_GPREL16:
11712     case BFD_RELOC_MIPS_LITERAL:
11713     case BFD_RELOC_MIPS_CALL16:
11714     case BFD_RELOC_MIPS_GOT16:
11715     case BFD_RELOC_GPREL32:
11716     case BFD_RELOC_MIPS_GOT_HI16:
11717     case BFD_RELOC_MIPS_GOT_LO16:
11718     case BFD_RELOC_MIPS_CALL_HI16:
11719     case BFD_RELOC_MIPS_CALL_LO16:
11720     case BFD_RELOC_MIPS16_GPREL:
11721       if (fixP->fx_pcrel)
11722         as_bad_where (fixP->fx_file, fixP->fx_line,
11723                       _("Invalid PC relative reloc"));
11724       /* Nothing needed to do. The value comes from the reloc entry */
11725       break;
11726
11727     case BFD_RELOC_MIPS16_JMP:
11728       /* We currently always generate a reloc against a symbol, which
11729          means that we don't want an addend even if the symbol is
11730          defined.  */
11731       *valP = 0;
11732       break;
11733
11734     case BFD_RELOC_PCREL_HI16_S:
11735       /* The addend for this is tricky if it is internal, so we just
11736          do everything here rather than in bfd_install_relocation.  */
11737       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11738         break;
11739       if (fixP->fx_addsy
11740           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11741         {
11742           /* For an external symbol adjust by the address to make it
11743              pcrel_offset.  We use the address of the RELLO reloc
11744              which follows this one.  */
11745           *valP += (fixP->fx_next->fx_frag->fr_address
11746                     + fixP->fx_next->fx_where);
11747         }
11748       *valP = ((*valP + 0x8000) >> 16) & 0xffff;
11749       if (target_big_endian)
11750         buf += 2;
11751       md_number_to_chars ((char *) buf, *valP, 2);
11752       break;
11753
11754     case BFD_RELOC_PCREL_LO16:
11755       /* The addend for this is tricky if it is internal, so we just
11756          do everything here rather than in bfd_install_relocation.  */
11757       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11758         break;
11759       if (fixP->fx_addsy
11760           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11761         *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11762       if (target_big_endian)
11763         buf += 2;
11764       md_number_to_chars ((char *) buf, *valP, 2);
11765       break;
11766
11767     case BFD_RELOC_64:
11768       /* This is handled like BFD_RELOC_32, but we output a sign
11769          extended value if we are only 32 bits.  */
11770       if (fixP->fx_done
11771           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11772         {
11773           if (8 <= sizeof (valueT))
11774             md_number_to_chars (buf, *valP, 8);
11775           else
11776             {
11777               valueT hiv;
11778
11779               if ((*valP & 0x80000000) != 0)
11780                 hiv = 0xffffffff;
11781               else
11782                 hiv = 0;
11783               md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11784                                   *valP, 4);
11785               md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11786                                   hiv, 4);
11787             }
11788         }
11789       break;
11790
11791     case BFD_RELOC_RVA:
11792     case BFD_RELOC_32:
11793       /* If we are deleting this reloc entry, we must fill in the
11794          value now.  This can happen if we have a .word which is not
11795          resolved when it appears but is later defined.  We also need
11796          to fill in the value if this is an embedded PIC switch table
11797          entry.  */
11798       if (fixP->fx_done
11799           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11800         md_number_to_chars (buf, *valP, 4);
11801       break;
11802
11803     case BFD_RELOC_16:
11804       /* If we are deleting this reloc entry, we must fill in the
11805          value now.  */
11806       assert (fixP->fx_size == 2);
11807       if (fixP->fx_done)
11808         md_number_to_chars (buf, *valP, 2);
11809       break;
11810
11811     case BFD_RELOC_LO16:
11812       /* When handling an embedded PIC switch statement, we can wind
11813          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11814       if (fixP->fx_done)
11815         {
11816           if (*valP + 0x8000 > 0xffff)
11817             as_bad_where (fixP->fx_file, fixP->fx_line,
11818                           _("relocation overflow"));
11819           if (target_big_endian)
11820             buf += 2;
11821           md_number_to_chars ((char *) buf, *valP, 2);
11822         }
11823       break;
11824
11825     case BFD_RELOC_16_PCREL_S2:
11826       if ((*valP & 0x3) != 0)
11827         as_bad_where (fixP->fx_file, fixP->fx_line,
11828                       _("Branch to odd address (%lx)"), (long) *valP);
11829
11830       /*
11831        * We need to save the bits in the instruction since fixup_segment()
11832        * might be deleting the relocation entry (i.e., a branch within
11833        * the current segment).
11834        */
11835       if (! fixP->fx_done)
11836         break;
11837
11838       /* update old instruction data */
11839       if (target_big_endian)
11840         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11841       else
11842         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11843
11844       if (*valP + 0x20000 <= 0x3ffff)
11845         {
11846           insn |= (*valP >> 2) & 0xffff;
11847           md_number_to_chars ((char *) buf, (valueT) insn, 4);
11848         }
11849       else if (mips_pic == NO_PIC
11850                && fixP->fx_done
11851                && fixP->fx_frag->fr_address >= text_section->vma
11852                && (fixP->fx_frag->fr_address
11853                    < text_section->vma + text_section->_raw_size)
11854                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
11855                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
11856                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11857         {
11858           /* The branch offset is too large.  If this is an
11859              unconditional branch, and we are not generating PIC code,
11860              we can convert it to an absolute jump instruction.  */
11861           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
11862             insn = 0x0c000000;  /* jal */
11863           else
11864             insn = 0x08000000;  /* j */
11865           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11866           fixP->fx_done = 0;
11867           fixP->fx_addsy = section_symbol (text_section);
11868           *valP += md_pcrel_from (fixP);
11869           md_number_to_chars ((char *) buf, (valueT) insn, 4);
11870         }
11871       else
11872         {
11873           /* If we got here, we have branch-relaxation disabled,
11874              and there's nothing we can do to fix this instruction
11875              without turning it into a longer sequence.  */
11876           as_bad_where (fixP->fx_file, fixP->fx_line,
11877                         _("Branch out of range"));
11878         }
11879       break;
11880
11881     case BFD_RELOC_VTABLE_INHERIT:
11882       fixP->fx_done = 0;
11883       if (fixP->fx_addsy
11884           && !S_IS_DEFINED (fixP->fx_addsy)
11885           && !S_IS_WEAK (fixP->fx_addsy))
11886         S_SET_WEAK (fixP->fx_addsy);
11887       break;
11888
11889     case BFD_RELOC_VTABLE_ENTRY:
11890       fixP->fx_done = 0;
11891       break;
11892
11893     default:
11894       internalError ();
11895     }
11896
11897   /* Remember value for tc_gen_reloc.  */
11898   fixP->fx_addnumber = *valP;
11899 }
11900
11901 #if 0
11902 void
11903 printInsn (oc)
11904      unsigned long oc;
11905 {
11906   const struct mips_opcode *p;
11907   int treg, sreg, dreg, shamt;
11908   short imm;
11909   const char *args;
11910   int i;
11911
11912   for (i = 0; i < NUMOPCODES; ++i)
11913     {
11914       p = &mips_opcodes[i];
11915       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11916         {
11917           printf ("%08lx %s\t", oc, p->name);
11918           treg = (oc >> 16) & 0x1f;
11919           sreg = (oc >> 21) & 0x1f;
11920           dreg = (oc >> 11) & 0x1f;
11921           shamt = (oc >> 6) & 0x1f;
11922           imm = oc;
11923           for (args = p->args;; ++args)
11924             {
11925               switch (*args)
11926                 {
11927                 case '\0':
11928                   printf ("\n");
11929                   break;
11930
11931                 case ',':
11932                 case '(':
11933                 case ')':
11934                   printf ("%c", *args);
11935                   continue;
11936
11937                 case 'r':
11938                   assert (treg == sreg);
11939                   printf ("$%d,$%d", treg, sreg);
11940                   continue;
11941
11942                 case 'd':
11943                 case 'G':
11944                   printf ("$%d", dreg);
11945                   continue;
11946
11947                 case 't':
11948                 case 'E':
11949                   printf ("$%d", treg);
11950                   continue;
11951
11952                 case 'k':
11953                   printf ("0x%x", treg);
11954                   continue;
11955
11956                 case 'b':
11957                 case 's':
11958                   printf ("$%d", sreg);
11959                   continue;
11960
11961                 case 'a':
11962                   printf ("0x%08lx", oc & 0x1ffffff);
11963                   continue;
11964
11965                 case 'i':
11966                 case 'j':
11967                 case 'o':
11968                 case 'u':
11969                   printf ("%d", imm);
11970                   continue;
11971
11972                 case '<':
11973                 case '>':
11974                   printf ("$%d", shamt);
11975                   continue;
11976
11977                 default:
11978                   internalError ();
11979                 }
11980               break;
11981             }
11982           return;
11983         }
11984     }
11985   printf (_("%08lx  UNDEFINED\n"), oc);
11986 }
11987 #endif
11988
11989 static symbolS *
11990 get_symbol ()
11991 {
11992   int c;
11993   char *name;
11994   symbolS *p;
11995
11996   name = input_line_pointer;
11997   c = get_symbol_end ();
11998   p = (symbolS *) symbol_find_or_make (name);
11999   *input_line_pointer = c;
12000   return p;
12001 }
12002
12003 /* Align the current frag to a given power of two.  The MIPS assembler
12004    also automatically adjusts any preceding label.  */
12005
12006 static void
12007 mips_align (to, fill, label)
12008      int to;
12009      int fill;
12010      symbolS *label;
12011 {
12012   mips_emit_delays (FALSE);
12013   frag_align (to, fill, 0);
12014   record_alignment (now_seg, to);
12015   if (label != NULL)
12016     {
12017       assert (S_GET_SEGMENT (label) == now_seg);
12018       symbol_set_frag (label, frag_now);
12019       S_SET_VALUE (label, (valueT) frag_now_fix ());
12020     }
12021 }
12022
12023 /* Align to a given power of two.  .align 0 turns off the automatic
12024    alignment used by the data creating pseudo-ops.  */
12025
12026 static void
12027 s_align (x)
12028      int x ATTRIBUTE_UNUSED;
12029 {
12030   register int temp;
12031   register long temp_fill;
12032   long max_alignment = 15;
12033
12034   /*
12035
12036     o  Note that the assembler pulls down any immediately preceeding label
12037        to the aligned address.
12038     o  It's not documented but auto alignment is reinstated by
12039        a .align pseudo instruction.
12040     o  Note also that after auto alignment is turned off the mips assembler
12041        issues an error on attempt to assemble an improperly aligned data item.
12042        We don't.
12043
12044     */
12045
12046   temp = get_absolute_expression ();
12047   if (temp > max_alignment)
12048     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
12049   else if (temp < 0)
12050     {
12051       as_warn (_("Alignment negative: 0 assumed."));
12052       temp = 0;
12053     }
12054   if (*input_line_pointer == ',')
12055     {
12056       ++input_line_pointer;
12057       temp_fill = get_absolute_expression ();
12058     }
12059   else
12060     temp_fill = 0;
12061   if (temp)
12062     {
12063       auto_align = 1;
12064       mips_align (temp, (int) temp_fill,
12065                   insn_labels != NULL ? insn_labels->label : NULL);
12066     }
12067   else
12068     {
12069       auto_align = 0;
12070     }
12071
12072   demand_empty_rest_of_line ();
12073 }
12074
12075 void
12076 mips_flush_pending_output ()
12077 {
12078   mips_emit_delays (FALSE);
12079   mips_clear_insn_labels ();
12080 }
12081
12082 static void
12083 s_change_sec (sec)
12084      int sec;
12085 {
12086   segT seg;
12087
12088   /* When generating embedded PIC code, we only use the .text, .lit8,
12089      .sdata and .sbss sections.  We change the .data and .rdata
12090      pseudo-ops to use .sdata.  */
12091   if (mips_pic == EMBEDDED_PIC
12092       && (sec == 'd' || sec == 'r'))
12093     sec = 's';
12094
12095 #ifdef OBJ_ELF
12096   /* The ELF backend needs to know that we are changing sections, so
12097      that .previous works correctly.  We could do something like check
12098      for an obj_section_change_hook macro, but that might be confusing
12099      as it would not be appropriate to use it in the section changing
12100      functions in read.c, since obj-elf.c intercepts those.  FIXME:
12101      This should be cleaner, somehow.  */
12102   obj_elf_section_change_hook ();
12103 #endif
12104
12105   mips_emit_delays (FALSE);
12106   switch (sec)
12107     {
12108     case 't':
12109       s_text (0);
12110       break;
12111     case 'd':
12112       s_data (0);
12113       break;
12114     case 'b':
12115       subseg_set (bss_section, (subsegT) get_absolute_expression ());
12116       demand_empty_rest_of_line ();
12117       break;
12118
12119     case 'r':
12120       if (USE_GLOBAL_POINTER_OPT)
12121         {
12122           seg = subseg_new (RDATA_SECTION_NAME,
12123                             (subsegT) get_absolute_expression ());
12124           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12125             {
12126               bfd_set_section_flags (stdoutput, seg,
12127                                      (SEC_ALLOC
12128                                       | SEC_LOAD
12129                                       | SEC_READONLY
12130                                       | SEC_RELOC
12131                                       | SEC_DATA));
12132               if (strcmp (TARGET_OS, "elf") != 0)
12133                 record_alignment (seg, 4);
12134             }
12135           demand_empty_rest_of_line ();
12136         }
12137       else
12138         {
12139           as_bad (_("No read only data section in this object file format"));
12140           demand_empty_rest_of_line ();
12141           return;
12142         }
12143       break;
12144
12145     case 's':
12146       if (USE_GLOBAL_POINTER_OPT)
12147         {
12148           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12149           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12150             {
12151               bfd_set_section_flags (stdoutput, seg,
12152                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
12153                                      | SEC_DATA);
12154               if (strcmp (TARGET_OS, "elf") != 0)
12155                 record_alignment (seg, 4);
12156             }
12157           demand_empty_rest_of_line ();
12158           break;
12159         }
12160       else
12161         {
12162           as_bad (_("Global pointers not supported; recompile -G 0"));
12163           demand_empty_rest_of_line ();
12164           return;
12165         }
12166     }
12167
12168   auto_align = 1;
12169 }
12170
12171 void
12172 s_change_section (ignore)
12173      int ignore ATTRIBUTE_UNUSED;
12174 {
12175 #ifdef OBJ_ELF
12176   char *section_name;
12177   char c;
12178   char next_c = 0;
12179   int section_type;
12180   int section_flag;
12181   int section_entry_size;
12182   int section_alignment;
12183
12184   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12185     return;
12186
12187   section_name = input_line_pointer;
12188   c = get_symbol_end ();
12189   if (c)
12190     next_c = *(input_line_pointer + 1);
12191
12192   /* Do we have .section Name<,"flags">?  */
12193   if (c != ',' || (c == ',' && next_c == '"'))
12194     {
12195       /* just after name is now '\0'.  */
12196       *input_line_pointer = c;
12197       input_line_pointer = section_name;
12198       obj_elf_section (ignore);
12199       return;
12200     }
12201   input_line_pointer++;
12202
12203   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
12204   if (c == ',')
12205     section_type = get_absolute_expression ();
12206   else
12207     section_type = 0;
12208   if (*input_line_pointer++ == ',')
12209     section_flag = get_absolute_expression ();
12210   else
12211     section_flag = 0;
12212   if (*input_line_pointer++ == ',')
12213     section_entry_size = get_absolute_expression ();
12214   else
12215     section_entry_size = 0;
12216   if (*input_line_pointer++ == ',')
12217     section_alignment = get_absolute_expression ();
12218   else
12219     section_alignment = 0;
12220
12221   section_name = xstrdup (section_name);
12222
12223   obj_elf_change_section (section_name, section_type, section_flag,
12224                           section_entry_size, 0, 0, 0);
12225
12226   if (now_seg->name != section_name)
12227     free (section_name);
12228 #endif /* OBJ_ELF */
12229 }
12230
12231 void
12232 mips_enable_auto_align ()
12233 {
12234   auto_align = 1;
12235 }
12236
12237 static void
12238 s_cons (log_size)
12239      int log_size;
12240 {
12241   symbolS *label;
12242
12243   label = insn_labels != NULL ? insn_labels->label : NULL;
12244   mips_emit_delays (FALSE);
12245   if (log_size > 0 && auto_align)
12246     mips_align (log_size, 0, label);
12247   mips_clear_insn_labels ();
12248   cons (1 << log_size);
12249 }
12250
12251 static void
12252 s_float_cons (type)
12253      int type;
12254 {
12255   symbolS *label;
12256
12257   label = insn_labels != NULL ? insn_labels->label : NULL;
12258
12259   mips_emit_delays (FALSE);
12260
12261   if (auto_align)
12262     {
12263       if (type == 'd')
12264         mips_align (3, 0, label);
12265       else
12266         mips_align (2, 0, label);
12267     }
12268
12269   mips_clear_insn_labels ();
12270
12271   float_cons (type);
12272 }
12273
12274 /* Handle .globl.  We need to override it because on Irix 5 you are
12275    permitted to say
12276        .globl foo .text
12277    where foo is an undefined symbol, to mean that foo should be
12278    considered to be the address of a function.  */
12279
12280 static void
12281 s_mips_globl (x)
12282      int x ATTRIBUTE_UNUSED;
12283 {
12284   char *name;
12285   int c;
12286   symbolS *symbolP;
12287   flagword flag;
12288
12289   name = input_line_pointer;
12290   c = get_symbol_end ();
12291   symbolP = symbol_find_or_make (name);
12292   *input_line_pointer = c;
12293   SKIP_WHITESPACE ();
12294
12295   /* On Irix 5, every global symbol that is not explicitly labelled as
12296      being a function is apparently labelled as being an object.  */
12297   flag = BSF_OBJECT;
12298
12299   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12300     {
12301       char *secname;
12302       asection *sec;
12303
12304       secname = input_line_pointer;
12305       c = get_symbol_end ();
12306       sec = bfd_get_section_by_name (stdoutput, secname);
12307       if (sec == NULL)
12308         as_bad (_("%s: no such section"), secname);
12309       *input_line_pointer = c;
12310
12311       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12312         flag = BSF_FUNCTION;
12313     }
12314
12315   symbol_get_bfdsym (symbolP)->flags |= flag;
12316
12317   S_SET_EXTERNAL (symbolP);
12318   demand_empty_rest_of_line ();
12319 }
12320
12321 static void
12322 s_option (x)
12323      int x ATTRIBUTE_UNUSED;
12324 {
12325   char *opt;
12326   char c;
12327
12328   opt = input_line_pointer;
12329   c = get_symbol_end ();
12330
12331   if (*opt == 'O')
12332     {
12333       /* FIXME: What does this mean?  */
12334     }
12335   else if (strncmp (opt, "pic", 3) == 0)
12336     {
12337       int i;
12338
12339       i = atoi (opt + 3);
12340       if (i == 0)
12341         mips_pic = NO_PIC;
12342       else if (i == 2)
12343         {
12344         mips_pic = SVR4_PIC;
12345           mips_abicalls = TRUE;
12346         }
12347       else
12348         as_bad (_(".option pic%d not supported"), i);
12349
12350       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12351         {
12352           if (g_switch_seen && g_switch_value != 0)
12353             as_warn (_("-G may not be used with SVR4 PIC code"));
12354           g_switch_value = 0;
12355           bfd_set_gp_size (stdoutput, 0);
12356         }
12357     }
12358   else
12359     as_warn (_("Unrecognized option \"%s\""), opt);
12360
12361   *input_line_pointer = c;
12362   demand_empty_rest_of_line ();
12363 }
12364
12365 /* This structure is used to hold a stack of .set values.  */
12366
12367 struct mips_option_stack
12368 {
12369   struct mips_option_stack *next;
12370   struct mips_set_options options;
12371 };
12372
12373 static struct mips_option_stack *mips_opts_stack;
12374
12375 /* Handle the .set pseudo-op.  */
12376
12377 static void
12378 s_mipsset (x)
12379      int x ATTRIBUTE_UNUSED;
12380 {
12381   char *name = input_line_pointer, ch;
12382
12383   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12384     ++input_line_pointer;
12385   ch = *input_line_pointer;
12386   *input_line_pointer = '\0';
12387
12388   if (strcmp (name, "reorder") == 0)
12389     {
12390       if (mips_opts.noreorder && prev_nop_frag != NULL)
12391         {
12392           /* If we still have pending nops, we can discard them.  The
12393              usual nop handling will insert any that are still
12394              needed.  */
12395           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12396                                     * (mips_opts.mips16 ? 2 : 4));
12397           prev_nop_frag = NULL;
12398         }
12399       mips_opts.noreorder = 0;
12400     }
12401   else if (strcmp (name, "noreorder") == 0)
12402     {
12403       mips_emit_delays (TRUE);
12404       mips_opts.noreorder = 1;
12405       mips_any_noreorder = 1;
12406     }
12407   else if (strcmp (name, "at") == 0)
12408     {
12409       mips_opts.noat = 0;
12410     }
12411   else if (strcmp (name, "noat") == 0)
12412     {
12413       mips_opts.noat = 1;
12414     }
12415   else if (strcmp (name, "macro") == 0)
12416     {
12417       mips_opts.warn_about_macros = 0;
12418     }
12419   else if (strcmp (name, "nomacro") == 0)
12420     {
12421       if (mips_opts.noreorder == 0)
12422         as_bad (_("`noreorder' must be set before `nomacro'"));
12423       mips_opts.warn_about_macros = 1;
12424     }
12425   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12426     {
12427       mips_opts.nomove = 0;
12428     }
12429   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12430     {
12431       mips_opts.nomove = 1;
12432     }
12433   else if (strcmp (name, "bopt") == 0)
12434     {
12435       mips_opts.nobopt = 0;
12436     }
12437   else if (strcmp (name, "nobopt") == 0)
12438     {
12439       mips_opts.nobopt = 1;
12440     }
12441   else if (strcmp (name, "mips16") == 0
12442            || strcmp (name, "MIPS-16") == 0)
12443     mips_opts.mips16 = 1;
12444   else if (strcmp (name, "nomips16") == 0
12445            || strcmp (name, "noMIPS-16") == 0)
12446     mips_opts.mips16 = 0;
12447   else if (strcmp (name, "mips3d") == 0)
12448     mips_opts.ase_mips3d = 1;
12449   else if (strcmp (name, "nomips3d") == 0)
12450     mips_opts.ase_mips3d = 0;
12451   else if (strcmp (name, "mdmx") == 0)
12452     mips_opts.ase_mdmx = 1;
12453   else if (strcmp (name, "nomdmx") == 0)
12454     mips_opts.ase_mdmx = 0;
12455   else if (strncmp (name, "mips", 4) == 0)
12456     {
12457       int reset = 0;
12458
12459       /* Permit the user to change the ISA on the fly.  Needless to
12460          say, misuse can cause serious problems.  */
12461       if (strcmp (name, "mips0") == 0)
12462         {
12463           reset = 1;
12464           mips_opts.isa = file_mips_isa;
12465         }
12466       else if (strcmp (name, "mips1") == 0)
12467         mips_opts.isa = ISA_MIPS1;
12468       else if (strcmp (name, "mips2") == 0)
12469         mips_opts.isa = ISA_MIPS2;
12470       else if (strcmp (name, "mips3") == 0)
12471         mips_opts.isa = ISA_MIPS3;
12472       else if (strcmp (name, "mips4") == 0)
12473         mips_opts.isa = ISA_MIPS4;
12474       else if (strcmp (name, "mips5") == 0)
12475         mips_opts.isa = ISA_MIPS5;
12476       else if (strcmp (name, "mips32") == 0)
12477         mips_opts.isa = ISA_MIPS32;
12478       else if (strcmp (name, "mips32r2") == 0)
12479         mips_opts.isa = ISA_MIPS32R2;
12480       else if (strcmp (name, "mips64") == 0)
12481         mips_opts.isa = ISA_MIPS64;
12482       else
12483         as_bad (_("unknown ISA level %s"), name + 4);
12484
12485       switch (mips_opts.isa)
12486         {
12487         case  0:
12488           break;
12489         case ISA_MIPS1:
12490         case ISA_MIPS2:
12491         case ISA_MIPS32:
12492         case ISA_MIPS32R2:
12493           mips_opts.gp32 = 1;
12494           mips_opts.fp32 = 1;
12495           break;
12496         case ISA_MIPS3:
12497         case ISA_MIPS4:
12498         case ISA_MIPS5:
12499         case ISA_MIPS64:
12500           mips_opts.gp32 = 0;
12501           mips_opts.fp32 = 0;
12502           break;
12503         default:
12504           as_bad (_("unknown ISA level %s"), name + 4);
12505           break;
12506         }
12507       if (reset)
12508         {
12509           mips_opts.gp32 = file_mips_gp32;
12510           mips_opts.fp32 = file_mips_fp32;
12511         }
12512     }
12513   else if (strcmp (name, "autoextend") == 0)
12514     mips_opts.noautoextend = 0;
12515   else if (strcmp (name, "noautoextend") == 0)
12516     mips_opts.noautoextend = 1;
12517   else if (strcmp (name, "push") == 0)
12518     {
12519       struct mips_option_stack *s;
12520
12521       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12522       s->next = mips_opts_stack;
12523       s->options = mips_opts;
12524       mips_opts_stack = s;
12525     }
12526   else if (strcmp (name, "pop") == 0)
12527     {
12528       struct mips_option_stack *s;
12529
12530       s = mips_opts_stack;
12531       if (s == NULL)
12532         as_bad (_(".set pop with no .set push"));
12533       else
12534         {
12535           /* If we're changing the reorder mode we need to handle
12536              delay slots correctly.  */
12537           if (s->options.noreorder && ! mips_opts.noreorder)
12538             mips_emit_delays (TRUE);
12539           else if (! s->options.noreorder && mips_opts.noreorder)
12540             {
12541               if (prev_nop_frag != NULL)
12542                 {
12543                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12544                                             * (mips_opts.mips16 ? 2 : 4));
12545                   prev_nop_frag = NULL;
12546                 }
12547             }
12548
12549           mips_opts = s->options;
12550           mips_opts_stack = s->next;
12551           free (s);
12552         }
12553     }
12554   else
12555     {
12556       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12557     }
12558   *input_line_pointer = ch;
12559   demand_empty_rest_of_line ();
12560 }
12561
12562 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12563    .option pic2.  It means to generate SVR4 PIC calls.  */
12564
12565 static void
12566 s_abicalls (ignore)
12567      int ignore ATTRIBUTE_UNUSED;
12568 {
12569   mips_pic = SVR4_PIC;
12570   mips_abicalls = TRUE;
12571   if (USE_GLOBAL_POINTER_OPT)
12572     {
12573       if (g_switch_seen && g_switch_value != 0)
12574         as_warn (_("-G may not be used with SVR4 PIC code"));
12575       g_switch_value = 0;
12576     }
12577   bfd_set_gp_size (stdoutput, 0);
12578   demand_empty_rest_of_line ();
12579 }
12580
12581 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12582    PIC code.  It sets the $gp register for the function based on the
12583    function address, which is in the register named in the argument.
12584    This uses a relocation against _gp_disp, which is handled specially
12585    by the linker.  The result is:
12586         lui     $gp,%hi(_gp_disp)
12587         addiu   $gp,$gp,%lo(_gp_disp)
12588         addu    $gp,$gp,.cpload argument
12589    The .cpload argument is normally $25 == $t9.  */
12590
12591 static void
12592 s_cpload (ignore)
12593      int ignore ATTRIBUTE_UNUSED;
12594 {
12595   expressionS ex;
12596   int icnt = 0;
12597
12598   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12599      .cpload is ignored.  */
12600   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12601     {
12602       s_ignore (0);
12603       return;
12604     }
12605
12606   /* .cpload should be in a .set noreorder section.  */
12607   if (mips_opts.noreorder == 0)
12608     as_warn (_(".cpload not in noreorder section"));
12609
12610   ex.X_op = O_symbol;
12611   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12612   ex.X_op_symbol = NULL;
12613   ex.X_add_number = 0;
12614
12615   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12616   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12617
12618   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12619   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12620                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12621
12622   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12623                mips_gp_register, mips_gp_register, tc_get_register (0));
12624
12625   demand_empty_rest_of_line ();
12626 }
12627
12628 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12629      .cpsetup $reg1, offset|$reg2, label
12630
12631    If offset is given, this results in:
12632      sd         $gp, offset($sp)
12633      lui        $gp, %hi(%neg(%gp_rel(label)))
12634      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12635      daddu      $gp, $gp, $reg1
12636
12637    If $reg2 is given, this results in:
12638      daddu      $reg2, $gp, $0
12639      lui        $gp, %hi(%neg(%gp_rel(label)))
12640      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12641      daddu      $gp, $gp, $reg1
12642    $reg1 is normally $25 == $t9.  */
12643 static void
12644 s_cpsetup (ignore)
12645      int ignore ATTRIBUTE_UNUSED;
12646 {
12647   expressionS ex_off;
12648   expressionS ex_sym;
12649   int reg1;
12650   int icnt = 0;
12651   char *f;
12652
12653   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12654      We also need NewABI support.  */
12655   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12656     {
12657       s_ignore (0);
12658       return;
12659     }
12660
12661   reg1 = tc_get_register (0);
12662   SKIP_WHITESPACE ();
12663   if (*input_line_pointer != ',')
12664     {
12665       as_bad (_("missing argument separator ',' for .cpsetup"));
12666       return;
12667     }
12668   else
12669     ++input_line_pointer;
12670   SKIP_WHITESPACE ();
12671   if (*input_line_pointer == '$')
12672     {
12673       mips_cpreturn_register = tc_get_register (0);
12674       mips_cpreturn_offset = -1;
12675     }
12676   else
12677     {
12678       mips_cpreturn_offset = get_absolute_expression ();
12679       mips_cpreturn_register = -1;
12680     }
12681   SKIP_WHITESPACE ();
12682   if (*input_line_pointer != ',')
12683     {
12684       as_bad (_("missing argument separator ',' for .cpsetup"));
12685       return;
12686     }
12687   else
12688     ++input_line_pointer;
12689   SKIP_WHITESPACE ();
12690   expression (&ex_sym);
12691
12692   if (mips_cpreturn_register == -1)
12693     {
12694       ex_off.X_op = O_constant;
12695       ex_off.X_add_symbol = NULL;
12696       ex_off.X_op_symbol = NULL;
12697       ex_off.X_add_number = mips_cpreturn_offset;
12698
12699       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12700                    mips_gp_register, (int) BFD_RELOC_LO16, mips_frame_reg);
12701     }
12702   else
12703     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12704                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12705
12706   /* Ensure there's room for the next two instructions, so that `f'
12707      doesn't end up with an address in the wrong frag.  */
12708   frag_grow (8);
12709   f = frag_more (0);
12710   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12711                (int) BFD_RELOC_GPREL16);
12712   fix_new (frag_now, f - frag_now->fr_literal,
12713            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12714   fix_new (frag_now, f - frag_now->fr_literal,
12715            4, NULL, 0, 0, BFD_RELOC_HI16_S);
12716
12717   f = frag_more (0);
12718   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12719                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12720   fix_new (frag_now, f - frag_now->fr_literal,
12721            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12722   fix_new (frag_now, f - frag_now->fr_literal,
12723            4, NULL, 0, 0, BFD_RELOC_LO16);
12724
12725   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12726                HAVE_64BIT_ADDRESSES ? "daddu" : "add", "d,v,t",
12727                mips_gp_register, mips_gp_register, reg1);
12728
12729   demand_empty_rest_of_line ();
12730 }
12731
12732 static void
12733 s_cplocal (ignore)
12734      int ignore ATTRIBUTE_UNUSED;
12735 {
12736   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12737    .cplocal is ignored.  */
12738   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12739     {
12740       s_ignore (0);
12741       return;
12742     }
12743
12744   mips_gp_register = tc_get_register (0);
12745   demand_empty_rest_of_line ();
12746 }
12747
12748 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12749    offset from $sp.  The offset is remembered, and after making a PIC
12750    call $gp is restored from that location.  */
12751
12752 static void
12753 s_cprestore (ignore)
12754      int ignore ATTRIBUTE_UNUSED;
12755 {
12756   expressionS ex;
12757   int icnt = 0;
12758
12759   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12760      .cprestore is ignored.  */
12761   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12762     {
12763       s_ignore (0);
12764       return;
12765     }
12766
12767   mips_cprestore_offset = get_absolute_expression ();
12768   mips_cprestore_valid = 1;
12769
12770   ex.X_op = O_constant;
12771   ex.X_add_symbol = NULL;
12772   ex.X_op_symbol = NULL;
12773   ex.X_add_number = mips_cprestore_offset;
12774
12775   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12776                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12777                                 mips_gp_register, mips_frame_reg);
12778
12779   demand_empty_rest_of_line ();
12780 }
12781
12782 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12783    was given in the preceeding .gpsetup, it results in:
12784      ld         $gp, offset($sp)
12785
12786    If a register $reg2 was given there, it results in:
12787      daddiu     $gp, $gp, $reg2
12788  */
12789 static void
12790 s_cpreturn (ignore)
12791      int ignore ATTRIBUTE_UNUSED;
12792 {
12793   expressionS ex;
12794   int icnt = 0;
12795
12796   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12797      We also need NewABI support.  */
12798   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12799     {
12800       s_ignore (0);
12801       return;
12802     }
12803
12804   if (mips_cpreturn_register == -1)
12805     {
12806       ex.X_op = O_constant;
12807       ex.X_add_symbol = NULL;
12808       ex.X_op_symbol = NULL;
12809       ex.X_add_number = mips_cpreturn_offset;
12810
12811       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12812                    mips_gp_register, (int) BFD_RELOC_LO16, mips_frame_reg);
12813     }
12814   else
12815     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12816                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12817
12818   demand_empty_rest_of_line ();
12819 }
12820
12821 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12822    code.  It sets the offset to use in gp_rel relocations.  */
12823
12824 static void
12825 s_gpvalue (ignore)
12826      int ignore ATTRIBUTE_UNUSED;
12827 {
12828   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12829      We also need NewABI support.  */
12830   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12831     {
12832       s_ignore (0);
12833       return;
12834     }
12835
12836   mips_gprel_offset = get_absolute_expression ();
12837
12838   demand_empty_rest_of_line ();
12839 }
12840
12841 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12842    code.  It generates a 32 bit GP relative reloc.  */
12843
12844 static void
12845 s_gpword (ignore)
12846      int ignore ATTRIBUTE_UNUSED;
12847 {
12848   symbolS *label;
12849   expressionS ex;
12850   char *p;
12851
12852   /* When not generating PIC code, this is treated as .word.  */
12853   if (mips_pic != SVR4_PIC)
12854     {
12855       s_cons (2);
12856       return;
12857     }
12858
12859   label = insn_labels != NULL ? insn_labels->label : NULL;
12860   mips_emit_delays (TRUE);
12861   if (auto_align)
12862     mips_align (2, 0, label);
12863   mips_clear_insn_labels ();
12864
12865   expression (&ex);
12866
12867   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12868     {
12869       as_bad (_("Unsupported use of .gpword"));
12870       ignore_rest_of_line ();
12871     }
12872
12873   p = frag_more (4);
12874   md_number_to_chars (p, (valueT) 0, 4);
12875   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12876                BFD_RELOC_GPREL32);
12877
12878   demand_empty_rest_of_line ();
12879 }
12880
12881 static void
12882 s_gpdword (ignore)
12883      int ignore ATTRIBUTE_UNUSED;
12884 {
12885   symbolS *label;
12886   expressionS ex;
12887   char *p;
12888
12889   /* When not generating PIC code, this is treated as .dword.  */
12890   if (mips_pic != SVR4_PIC)
12891     {
12892       s_cons (3);
12893       return;
12894     }
12895
12896   label = insn_labels != NULL ? insn_labels->label : NULL;
12897   mips_emit_delays (TRUE);
12898   if (auto_align)
12899     mips_align (3, 0, label);
12900   mips_clear_insn_labels ();
12901
12902   expression (&ex);
12903
12904   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12905     {
12906       as_bad (_("Unsupported use of .gpdword"));
12907       ignore_rest_of_line ();
12908     }
12909
12910   p = frag_more (8);
12911   md_number_to_chars (p, (valueT) 0, 8);
12912   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12913                BFD_RELOC_GPREL32);
12914
12915   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12916   ex.X_op = O_absent;
12917   ex.X_add_symbol = 0;
12918   ex.X_add_number = 0;
12919   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12920                BFD_RELOC_64);
12921
12922   demand_empty_rest_of_line ();
12923 }
12924
12925 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12926    tables in SVR4 PIC code.  */
12927
12928 static void
12929 s_cpadd (ignore)
12930      int ignore ATTRIBUTE_UNUSED;
12931 {
12932   int icnt = 0;
12933   int reg;
12934
12935   /* This is ignored when not generating SVR4 PIC code.  */
12936   if (mips_pic != SVR4_PIC)
12937     {
12938       s_ignore (0);
12939       return;
12940     }
12941
12942   /* Add $gp to the register named as an argument.  */
12943   reg = tc_get_register (0);
12944   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12945                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI ? "add" : "addu" : "daddu",
12946                "d,v,t", reg, reg, mips_gp_register);
12947
12948   demand_empty_rest_of_line ();
12949 }
12950
12951 /* Handle the .insn pseudo-op.  This marks instruction labels in
12952    mips16 mode.  This permits the linker to handle them specially,
12953    such as generating jalx instructions when needed.  We also make
12954    them odd for the duration of the assembly, in order to generate the
12955    right sort of code.  We will make them even in the adjust_symtab
12956    routine, while leaving them marked.  This is convenient for the
12957    debugger and the disassembler.  The linker knows to make them odd
12958    again.  */
12959
12960 static void
12961 s_insn (ignore)
12962      int ignore ATTRIBUTE_UNUSED;
12963 {
12964   mips16_mark_labels ();
12965
12966   demand_empty_rest_of_line ();
12967 }
12968
12969 /* Handle a .stabn directive.  We need these in order to mark a label
12970    as being a mips16 text label correctly.  Sometimes the compiler
12971    will emit a label, followed by a .stabn, and then switch sections.
12972    If the label and .stabn are in mips16 mode, then the label is
12973    really a mips16 text label.  */
12974
12975 static void
12976 s_mips_stab (type)
12977      int type;
12978 {
12979   if (type == 'n')
12980     mips16_mark_labels ();
12981
12982   s_stab (type);
12983 }
12984
12985 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12986  */
12987
12988 static void
12989 s_mips_weakext (ignore)
12990      int ignore ATTRIBUTE_UNUSED;
12991 {
12992   char *name;
12993   int c;
12994   symbolS *symbolP;
12995   expressionS exp;
12996
12997   name = input_line_pointer;
12998   c = get_symbol_end ();
12999   symbolP = symbol_find_or_make (name);
13000   S_SET_WEAK (symbolP);
13001   *input_line_pointer = c;
13002
13003   SKIP_WHITESPACE ();
13004
13005   if (! is_end_of_line[(unsigned char) *input_line_pointer])
13006     {
13007       if (S_IS_DEFINED (symbolP))
13008         {
13009           as_bad ("ignoring attempt to redefine symbol %s",
13010                   S_GET_NAME (symbolP));
13011           ignore_rest_of_line ();
13012           return;
13013         }
13014
13015       if (*input_line_pointer == ',')
13016         {
13017           ++input_line_pointer;
13018           SKIP_WHITESPACE ();
13019         }
13020
13021       expression (&exp);
13022       if (exp.X_op != O_symbol)
13023         {
13024           as_bad ("bad .weakext directive");
13025           ignore_rest_of_line ();
13026           return;
13027         }
13028       symbol_set_value_expression (symbolP, &exp);
13029     }
13030
13031   demand_empty_rest_of_line ();
13032 }
13033
13034 /* Parse a register string into a number.  Called from the ECOFF code
13035    to parse .frame.  The argument is non-zero if this is the frame
13036    register, so that we can record it in mips_frame_reg.  */
13037
13038 int
13039 tc_get_register (frame)
13040      int frame;
13041 {
13042   int reg;
13043
13044   SKIP_WHITESPACE ();
13045   if (*input_line_pointer++ != '$')
13046     {
13047       as_warn (_("expected `$'"));
13048       reg = ZERO;
13049     }
13050   else if (ISDIGIT (*input_line_pointer))
13051     {
13052       reg = get_absolute_expression ();
13053       if (reg < 0 || reg >= 32)
13054         {
13055           as_warn (_("Bad register number"));
13056           reg = ZERO;
13057         }
13058     }
13059   else
13060     {
13061       if (strncmp (input_line_pointer, "ra", 2) == 0)
13062         {
13063           reg = RA;
13064           input_line_pointer += 2;
13065         }
13066       else if (strncmp (input_line_pointer, "fp", 2) == 0)
13067         {
13068           reg = FP;
13069           input_line_pointer += 2;
13070         }
13071       else if (strncmp (input_line_pointer, "sp", 2) == 0)
13072         {
13073           reg = SP;
13074           input_line_pointer += 2;
13075         }
13076       else if (strncmp (input_line_pointer, "gp", 2) == 0)
13077         {
13078           reg = GP;
13079           input_line_pointer += 2;
13080         }
13081       else if (strncmp (input_line_pointer, "at", 2) == 0)
13082         {
13083           reg = AT;
13084           input_line_pointer += 2;
13085         }
13086       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
13087         {
13088           reg = KT0;
13089           input_line_pointer += 3;
13090         }
13091       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
13092         {
13093           reg = KT1;
13094           input_line_pointer += 3;
13095         }
13096       else if (strncmp (input_line_pointer, "zero", 4) == 0)
13097         {
13098           reg = ZERO;
13099           input_line_pointer += 4;
13100         }
13101       else
13102         {
13103           as_warn (_("Unrecognized register name"));
13104           reg = ZERO;
13105           while (ISALNUM(*input_line_pointer))
13106            input_line_pointer++;
13107         }
13108     }
13109   if (frame)
13110     {
13111       mips_frame_reg = reg != 0 ? reg : SP;
13112       mips_frame_reg_valid = 1;
13113       mips_cprestore_valid = 0;
13114     }
13115   return reg;
13116 }
13117
13118 valueT
13119 md_section_align (seg, addr)
13120      asection *seg;
13121      valueT addr;
13122 {
13123   int align = bfd_get_section_alignment (stdoutput, seg);
13124
13125 #ifdef OBJ_ELF
13126   /* We don't need to align ELF sections to the full alignment.
13127      However, Irix 5 may prefer that we align them at least to a 16
13128      byte boundary.  We don't bother to align the sections if we are
13129      targeted for an embedded system.  */
13130   if (strcmp (TARGET_OS, "elf") == 0)
13131     return addr;
13132   if (align > 4)
13133     align = 4;
13134 #endif
13135
13136   return ((addr + (1 << align) - 1) & (-1 << align));
13137 }
13138
13139 /* Utility routine, called from above as well.  If called while the
13140    input file is still being read, it's only an approximation.  (For
13141    example, a symbol may later become defined which appeared to be
13142    undefined earlier.)  */
13143
13144 static int
13145 nopic_need_relax (sym, before_relaxing)
13146      symbolS *sym;
13147      int before_relaxing;
13148 {
13149   if (sym == 0)
13150     return 0;
13151
13152   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
13153     {
13154       const char *symname;
13155       int change;
13156
13157       /* Find out whether this symbol can be referenced off the $gp
13158          register.  It can be if it is smaller than the -G size or if
13159          it is in the .sdata or .sbss section.  Certain symbols can
13160          not be referenced off the $gp, although it appears as though
13161          they can.  */
13162       symname = S_GET_NAME (sym);
13163       if (symname != (const char *) NULL
13164           && (strcmp (symname, "eprol") == 0
13165               || strcmp (symname, "etext") == 0
13166               || strcmp (symname, "_gp") == 0
13167               || strcmp (symname, "edata") == 0
13168               || strcmp (symname, "_fbss") == 0
13169               || strcmp (symname, "_fdata") == 0
13170               || strcmp (symname, "_ftext") == 0
13171               || strcmp (symname, "end") == 0
13172               || strcmp (symname, "_gp_disp") == 0))
13173         change = 1;
13174       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13175                && (0
13176 #ifndef NO_ECOFF_DEBUGGING
13177                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
13178                        && (symbol_get_obj (sym)->ecoff_extern_size
13179                            <= g_switch_value))
13180 #endif
13181                    /* We must defer this decision until after the whole
13182                       file has been read, since there might be a .extern
13183                       after the first use of this symbol.  */
13184                    || (before_relaxing
13185 #ifndef NO_ECOFF_DEBUGGING
13186                        && symbol_get_obj (sym)->ecoff_extern_size == 0
13187 #endif
13188                        && S_GET_VALUE (sym) == 0)
13189                    || (S_GET_VALUE (sym) != 0
13190                        && S_GET_VALUE (sym) <= g_switch_value)))
13191         change = 0;
13192       else
13193         {
13194           const char *segname;
13195
13196           segname = segment_name (S_GET_SEGMENT (sym));
13197           assert (strcmp (segname, ".lit8") != 0
13198                   && strcmp (segname, ".lit4") != 0);
13199           change = (strcmp (segname, ".sdata") != 0
13200                     && strcmp (segname, ".sbss") != 0
13201                     && strncmp (segname, ".sdata.", 7) != 0
13202                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
13203         }
13204       return change;
13205     }
13206   else
13207     /* We are not optimizing for the $gp register.  */
13208     return 1;
13209 }
13210
13211
13212 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
13213
13214 static bfd_boolean
13215 pic_need_relax (sym, segtype)
13216      symbolS *sym;
13217      asection *segtype;
13218 {
13219   asection *symsec;
13220   bfd_boolean linkonce;
13221
13222   /* Handle the case of a symbol equated to another symbol.  */
13223   while (symbol_equated_reloc_p (sym))
13224     {
13225       symbolS *n;
13226
13227       /* It's possible to get a loop here in a badly written
13228          program.  */
13229       n = symbol_get_value_expression (sym)->X_add_symbol;
13230       if (n == sym)
13231         break;
13232       sym = n;
13233     }
13234
13235   symsec = S_GET_SEGMENT (sym);
13236
13237   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13238   linkonce = FALSE;
13239   if (symsec != segtype && ! S_IS_LOCAL (sym))
13240     {
13241       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13242           != 0)
13243         linkonce = TRUE;
13244
13245       /* The GNU toolchain uses an extension for ELF: a section
13246          beginning with the magic string .gnu.linkonce is a linkonce
13247          section.  */
13248       if (strncmp (segment_name (symsec), ".gnu.linkonce",
13249                    sizeof ".gnu.linkonce" - 1) == 0)
13250         linkonce = TRUE;
13251     }
13252
13253   /* This must duplicate the test in adjust_reloc_syms.  */
13254   return (symsec != &bfd_und_section
13255           && symsec != &bfd_abs_section
13256           && ! bfd_is_com_section (symsec)
13257           && !linkonce
13258 #ifdef OBJ_ELF
13259           /* A global or weak symbol is treated as external.  */
13260           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13261               || (! S_IS_WEAK (sym)
13262                   && (! S_IS_EXTERNAL (sym)
13263                       || mips_pic == EMBEDDED_PIC)))
13264 #endif
13265           );
13266 }
13267
13268
13269 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13270    extended opcode.  SEC is the section the frag is in.  */
13271
13272 static int
13273 mips16_extended_frag (fragp, sec, stretch)
13274      fragS *fragp;
13275      asection *sec;
13276      long stretch;
13277 {
13278   int type;
13279   register const struct mips16_immed_operand *op;
13280   offsetT val;
13281   int mintiny, maxtiny;
13282   segT symsec;
13283   fragS *sym_frag;
13284
13285   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13286     return 0;
13287   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13288     return 1;
13289
13290   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13291   op = mips16_immed_operands;
13292   while (op->type != type)
13293     {
13294       ++op;
13295       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13296     }
13297
13298   if (op->unsp)
13299     {
13300       if (type == '<' || type == '>' || type == '[' || type == ']')
13301         {
13302           mintiny = 1;
13303           maxtiny = 1 << op->nbits;
13304         }
13305       else
13306         {
13307           mintiny = 0;
13308           maxtiny = (1 << op->nbits) - 1;
13309         }
13310     }
13311   else
13312     {
13313       mintiny = - (1 << (op->nbits - 1));
13314       maxtiny = (1 << (op->nbits - 1)) - 1;
13315     }
13316
13317   sym_frag = symbol_get_frag (fragp->fr_symbol);
13318   val = S_GET_VALUE (fragp->fr_symbol);
13319   symsec = S_GET_SEGMENT (fragp->fr_symbol);
13320
13321   if (op->pcrel)
13322     {
13323       addressT addr;
13324
13325       /* We won't have the section when we are called from
13326          mips_relax_frag.  However, we will always have been called
13327          from md_estimate_size_before_relax first.  If this is a
13328          branch to a different section, we mark it as such.  If SEC is
13329          NULL, and the frag is not marked, then it must be a branch to
13330          the same section.  */
13331       if (sec == NULL)
13332         {
13333           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13334             return 1;
13335         }
13336       else
13337         {
13338           /* Must have been called from md_estimate_size_before_relax.  */
13339           if (symsec != sec)
13340             {
13341               fragp->fr_subtype =
13342                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13343
13344               /* FIXME: We should support this, and let the linker
13345                  catch branches and loads that are out of range.  */
13346               as_bad_where (fragp->fr_file, fragp->fr_line,
13347                             _("unsupported PC relative reference to different section"));
13348
13349               return 1;
13350             }
13351           if (fragp != sym_frag && sym_frag->fr_address == 0)
13352             /* Assume non-extended on the first relaxation pass.
13353                The address we have calculated will be bogus if this is
13354                a forward branch to another frag, as the forward frag
13355                will have fr_address == 0.  */
13356             return 0;
13357         }
13358
13359       /* In this case, we know for sure that the symbol fragment is in
13360          the same section.  If the relax_marker of the symbol fragment
13361          differs from the relax_marker of this fragment, we have not
13362          yet adjusted the symbol fragment fr_address.  We want to add
13363          in STRETCH in order to get a better estimate of the address.
13364          This particularly matters because of the shift bits.  */
13365       if (stretch != 0
13366           && sym_frag->relax_marker != fragp->relax_marker)
13367         {
13368           fragS *f;
13369
13370           /* Adjust stretch for any alignment frag.  Note that if have
13371              been expanding the earlier code, the symbol may be
13372              defined in what appears to be an earlier frag.  FIXME:
13373              This doesn't handle the fr_subtype field, which specifies
13374              a maximum number of bytes to skip when doing an
13375              alignment.  */
13376           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13377             {
13378               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13379                 {
13380                   if (stretch < 0)
13381                     stretch = - ((- stretch)
13382                                  & ~ ((1 << (int) f->fr_offset) - 1));
13383                   else
13384                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13385                   if (stretch == 0)
13386                     break;
13387                 }
13388             }
13389           if (f != NULL)
13390             val += stretch;
13391         }
13392
13393       addr = fragp->fr_address + fragp->fr_fix;
13394
13395       /* The base address rules are complicated.  The base address of
13396          a branch is the following instruction.  The base address of a
13397          PC relative load or add is the instruction itself, but if it
13398          is in a delay slot (in which case it can not be extended) use
13399          the address of the instruction whose delay slot it is in.  */
13400       if (type == 'p' || type == 'q')
13401         {
13402           addr += 2;
13403
13404           /* If we are currently assuming that this frag should be
13405              extended, then, the current address is two bytes
13406              higher.  */
13407           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13408             addr += 2;
13409
13410           /* Ignore the low bit in the target, since it will be set
13411              for a text label.  */
13412           if ((val & 1) != 0)
13413             --val;
13414         }
13415       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13416         addr -= 4;
13417       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13418         addr -= 2;
13419
13420       val -= addr & ~ ((1 << op->shift) - 1);
13421
13422       /* Branch offsets have an implicit 0 in the lowest bit.  */
13423       if (type == 'p' || type == 'q')
13424         val /= 2;
13425
13426       /* If any of the shifted bits are set, we must use an extended
13427          opcode.  If the address depends on the size of this
13428          instruction, this can lead to a loop, so we arrange to always
13429          use an extended opcode.  We only check this when we are in
13430          the main relaxation loop, when SEC is NULL.  */
13431       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13432         {
13433           fragp->fr_subtype =
13434             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13435           return 1;
13436         }
13437
13438       /* If we are about to mark a frag as extended because the value
13439          is precisely maxtiny + 1, then there is a chance of an
13440          infinite loop as in the following code:
13441              la $4,foo
13442              .skip      1020
13443              .align     2
13444            foo:
13445          In this case when the la is extended, foo is 0x3fc bytes
13446          away, so the la can be shrunk, but then foo is 0x400 away, so
13447          the la must be extended.  To avoid this loop, we mark the
13448          frag as extended if it was small, and is about to become
13449          extended with a value of maxtiny + 1.  */
13450       if (val == ((maxtiny + 1) << op->shift)
13451           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13452           && sec == NULL)
13453         {
13454           fragp->fr_subtype =
13455             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13456           return 1;
13457         }
13458     }
13459   else if (symsec != absolute_section && sec != NULL)
13460     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13461
13462   if ((val & ((1 << op->shift) - 1)) != 0
13463       || val < (mintiny << op->shift)
13464       || val > (maxtiny << op->shift))
13465     return 1;
13466   else
13467     return 0;
13468 }
13469
13470 /* Compute the length of a branch sequence, and adjust the
13471    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13472    worst-case length is computed, with UPDATE being used to indicate
13473    whether an unconditional (-1), branch-likely (+1) or regular (0)
13474    branch is to be computed.  */
13475 static int
13476 relaxed_branch_length (fragp, sec, update)
13477      fragS *fragp;
13478      asection *sec;
13479      int update;
13480 {
13481   bfd_boolean toofar;
13482   int length;
13483
13484   if (fragp
13485       && S_IS_DEFINED (fragp->fr_symbol)
13486       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13487     {
13488       addressT addr;
13489       offsetT val;
13490
13491       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13492
13493       addr = fragp->fr_address + fragp->fr_fix + 4;
13494
13495       val -= addr;
13496
13497       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13498     }
13499   else if (fragp)
13500     /* If the symbol is not defined or it's in a different segment,
13501        assume the user knows what's going on and emit a short
13502        branch.  */
13503     toofar = FALSE;
13504   else
13505     toofar = TRUE;
13506
13507   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13508     fragp->fr_subtype
13509       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13510                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13511                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13512                              toofar);
13513
13514   length = 4;
13515   if (toofar)
13516     {
13517       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13518         length += 8;
13519
13520       if (mips_pic != NO_PIC)
13521         {
13522           /* Additional space for PIC loading of target address.  */
13523           length += 8;
13524           if (mips_opts.isa == ISA_MIPS1)
13525             /* Additional space for $at-stabilizing nop.  */
13526             length += 4;
13527         }
13528
13529       /* If branch is conditional.  */
13530       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13531         length += 8;
13532     }
13533
13534   return length;
13535 }
13536
13537 /* Estimate the size of a frag before relaxing.  Unless this is the
13538    mips16, we are not really relaxing here, and the final size is
13539    encoded in the subtype information.  For the mips16, we have to
13540    decide whether we are using an extended opcode or not.  */
13541
13542 int
13543 md_estimate_size_before_relax (fragp, segtype)
13544      fragS *fragp;
13545      asection *segtype;
13546 {
13547   int change;
13548
13549   if (RELAX_BRANCH_P (fragp->fr_subtype))
13550     {
13551
13552       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13553
13554       return fragp->fr_var;
13555     }
13556
13557   if (RELAX_MIPS16_P (fragp->fr_subtype))
13558     /* We don't want to modify the EXTENDED bit here; it might get us
13559        into infinite loops.  We change it only in mips_relax_frag().  */
13560     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13561
13562   if (mips_pic == NO_PIC)
13563     change = nopic_need_relax (fragp->fr_symbol, 0);
13564   else if (mips_pic == SVR4_PIC)
13565     change = pic_need_relax (fragp->fr_symbol, segtype);
13566   else
13567     abort ();
13568
13569   if (change)
13570     {
13571       /* Record the offset to the first reloc in the fr_opcode field.
13572          This lets md_convert_frag and tc_gen_reloc know that the code
13573          must be expanded.  */
13574       fragp->fr_opcode = (fragp->fr_literal
13575                           + fragp->fr_fix
13576                           - RELAX_OLD (fragp->fr_subtype)
13577                           + RELAX_RELOC1 (fragp->fr_subtype));
13578       /* FIXME: This really needs as_warn_where.  */
13579       if (RELAX_WARN (fragp->fr_subtype))
13580         as_warn (_("AT used after \".set noat\" or macro used after "
13581                    "\".set nomacro\""));
13582
13583       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13584     }
13585
13586   return 0;
13587 }
13588
13589 /* This is called to see whether a reloc against a defined symbol
13590    should be converted into a reloc against a section.  Don't adjust
13591    MIPS16 jump relocations, so we don't have to worry about the format
13592    of the offset in the .o file.  Don't adjust relocations against
13593    mips16 symbols, so that the linker can find them if it needs to set
13594    up a stub.  */
13595
13596 int
13597 mips_fix_adjustable (fixp)
13598      fixS *fixp;
13599 {
13600   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13601     return 0;
13602
13603   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13604       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13605     return 0;
13606
13607   if (fixp->fx_addsy == NULL)
13608     return 1;
13609
13610 #ifdef OBJ_ELF
13611   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13612       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13613       && fixp->fx_subsy == NULL)
13614     return 0;
13615 #endif
13616
13617   return 1;
13618 }
13619
13620 /* Translate internal representation of relocation info to BFD target
13621    format.  */
13622
13623 arelent **
13624 tc_gen_reloc (section, fixp)
13625      asection *section ATTRIBUTE_UNUSED;
13626      fixS *fixp;
13627 {
13628   static arelent *retval[4];
13629   arelent *reloc;
13630   bfd_reloc_code_real_type code;
13631
13632   memset (retval, 0, sizeof(retval));
13633   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13634   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13635   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13636   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13637
13638   if (mips_pic == EMBEDDED_PIC
13639       && SWITCH_TABLE (fixp))
13640     {
13641       /* For a switch table entry we use a special reloc.  The addend
13642          is actually the difference between the reloc address and the
13643          subtrahend.  */
13644       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13645       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13646         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13647       fixp->fx_r_type = BFD_RELOC_GPREL32;
13648     }
13649   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13650     {
13651       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13652         reloc->addend = fixp->fx_addnumber;
13653       else
13654         {
13655           /* We use a special addend for an internal RELLO reloc.  */
13656           if (symbol_section_p (fixp->fx_addsy))
13657             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13658           else
13659             reloc->addend = fixp->fx_addnumber + reloc->address;
13660         }
13661     }
13662   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13663     {
13664       assert (fixp->fx_next != NULL
13665               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13666
13667       /* The reloc is relative to the RELLO; adjust the addend
13668          accordingly.  */
13669       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13670         reloc->addend = fixp->fx_next->fx_addnumber;
13671       else
13672         {
13673           /* We use a special addend for an internal RELHI reloc.  */
13674           if (symbol_section_p (fixp->fx_addsy))
13675             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13676                              + fixp->fx_next->fx_where
13677                              - S_GET_VALUE (fixp->fx_subsy));
13678           else
13679             reloc->addend = (fixp->fx_addnumber
13680                              + fixp->fx_next->fx_frag->fr_address
13681                              + fixp->fx_next->fx_where);
13682         }
13683     }
13684   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13685     reloc->addend = fixp->fx_addnumber;
13686   else
13687     {
13688       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13689         /* A gruesome hack which is a result of the gruesome gas reloc
13690            handling.  */
13691         reloc->addend = reloc->address;
13692       else
13693         reloc->addend = -reloc->address;
13694     }
13695
13696   /* If this is a variant frag, we may need to adjust the existing
13697      reloc and generate a new one.  */
13698   if (fixp->fx_frag->fr_opcode != NULL
13699       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13700            && ! HAVE_NEWABI)
13701           || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13702               && HAVE_NEWABI)
13703           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13704           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13705           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13706           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13707           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13708           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13709     )
13710     {
13711       arelent *reloc2;
13712
13713       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13714
13715       /* If this is not the last reloc in this frag, then we have two
13716          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13717          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13718          the second one handle all of them.  */
13719       if (fixp->fx_next != NULL
13720           && fixp->fx_frag == fixp->fx_next->fx_frag)
13721         {
13722           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13723                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13724                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13725                       && (fixp->fx_next->fx_r_type
13726                           == BFD_RELOC_MIPS_GOT_LO16))
13727                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13728                       && (fixp->fx_next->fx_r_type
13729                           == BFD_RELOC_MIPS_CALL_LO16)));
13730           retval[0] = NULL;
13731           return retval;
13732         }
13733
13734       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13735       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13736       reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13737       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13738       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13739       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13740       reloc2->address = (reloc->address
13741                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13742                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13743       reloc2->addend = fixp->fx_addnumber - S_GET_VALUE (fixp->fx_addsy)
13744         + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13745       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13746       assert (reloc2->howto != NULL);
13747
13748       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13749         {
13750           arelent *reloc3;
13751
13752           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13753           *reloc3 = *reloc2;
13754           reloc3->address += 4;
13755         }
13756
13757       if (mips_pic == NO_PIC)
13758         {
13759           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13760           fixp->fx_r_type = BFD_RELOC_HI16_S;
13761         }
13762       else if (mips_pic == SVR4_PIC)
13763         {
13764           switch (fixp->fx_r_type)
13765             {
13766             default:
13767               abort ();
13768             case BFD_RELOC_MIPS_GOT16:
13769               break;
13770             case BFD_RELOC_MIPS_GOT_LO16:
13771             case BFD_RELOC_MIPS_CALL_LO16:
13772               if (HAVE_NEWABI)
13773                 {
13774                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13775                   reloc2->howto = bfd_reloc_type_lookup
13776                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13777                 }
13778               else
13779                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13780               break;
13781             case BFD_RELOC_MIPS_CALL16:
13782             case BFD_RELOC_MIPS_GOT_OFST:
13783             case BFD_RELOC_MIPS_GOT_DISP:
13784               if (HAVE_NEWABI)
13785                 {
13786                   /* It may seem nonsensical to relax GOT_DISP to
13787                      GOT_DISP, but we're actually turning a GOT_DISP
13788                      without offset into a GOT_DISP with an offset,
13789                      getting rid of the separate addition, which we can
13790                      do when the symbol is found to be local.  */
13791                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13792                   retval[1] = NULL;
13793                 }
13794               else
13795                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13796               break;
13797             }
13798         }
13799       else
13800         abort ();
13801     }
13802
13803   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13804      entry to be used in the relocation's section offset.  */
13805   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13806     {
13807       reloc->address = reloc->addend;
13808       reloc->addend = 0;
13809     }
13810
13811   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13812      fixup_segment converted a non-PC relative reloc into a PC
13813      relative reloc.  In such a case, we need to convert the reloc
13814      code.  */
13815   code = fixp->fx_r_type;
13816   if (fixp->fx_pcrel)
13817     {
13818       switch (code)
13819         {
13820         case BFD_RELOC_8:
13821           code = BFD_RELOC_8_PCREL;
13822           break;
13823         case BFD_RELOC_16:
13824           code = BFD_RELOC_16_PCREL;
13825           break;
13826         case BFD_RELOC_32:
13827           code = BFD_RELOC_32_PCREL;
13828           break;
13829         case BFD_RELOC_64:
13830           code = BFD_RELOC_64_PCREL;
13831           break;
13832         case BFD_RELOC_8_PCREL:
13833         case BFD_RELOC_16_PCREL:
13834         case BFD_RELOC_32_PCREL:
13835         case BFD_RELOC_64_PCREL:
13836         case BFD_RELOC_16_PCREL_S2:
13837         case BFD_RELOC_PCREL_HI16_S:
13838         case BFD_RELOC_PCREL_LO16:
13839           break;
13840         default:
13841           as_bad_where (fixp->fx_file, fixp->fx_line,
13842                         _("Cannot make %s relocation PC relative"),
13843                         bfd_get_reloc_code_name (code));
13844         }
13845     }
13846
13847   /* To support a PC relative reloc when generating embedded PIC code
13848      for ECOFF, we use a Cygnus extension.  We check for that here to
13849      make sure that we don't let such a reloc escape normally.  */
13850   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13851        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13852       && code == BFD_RELOC_16_PCREL_S2
13853       && mips_pic != EMBEDDED_PIC)
13854     reloc->howto = NULL;
13855   else
13856     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13857
13858   if (reloc->howto == NULL)
13859     {
13860       as_bad_where (fixp->fx_file, fixp->fx_line,
13861                     _("Can not represent %s relocation in this object file format"),
13862                     bfd_get_reloc_code_name (code));
13863       retval[0] = NULL;
13864     }
13865
13866   return retval;
13867 }
13868
13869 /* Relax a machine dependent frag.  This returns the amount by which
13870    the current size of the frag should change.  */
13871
13872 int
13873 mips_relax_frag (sec, fragp, stretch)
13874      asection *sec;
13875      fragS *fragp;
13876      long stretch;
13877 {
13878   if (RELAX_BRANCH_P (fragp->fr_subtype))
13879     {
13880       offsetT old_var = fragp->fr_var;
13881
13882       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13883
13884       return fragp->fr_var - old_var;
13885     }
13886
13887   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13888     return 0;
13889
13890   if (mips16_extended_frag (fragp, NULL, stretch))
13891     {
13892       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13893         return 0;
13894       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13895       return 2;
13896     }
13897   else
13898     {
13899       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13900         return 0;
13901       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13902       return -2;
13903     }
13904
13905   return 0;
13906 }
13907
13908 /* Convert a machine dependent frag.  */
13909
13910 void
13911 md_convert_frag (abfd, asec, fragp)
13912      bfd *abfd ATTRIBUTE_UNUSED;
13913      segT asec;
13914      fragS *fragp;
13915 {
13916   int old, new;
13917   char *fixptr;
13918
13919   if (RELAX_BRANCH_P (fragp->fr_subtype))
13920     {
13921       bfd_byte *buf;
13922       unsigned long insn;
13923       expressionS exp;
13924       fixS *fixp;
13925
13926       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13927
13928       if (target_big_endian)
13929         insn = bfd_getb32 (buf);
13930       else
13931         insn = bfd_getl32 (buf);
13932
13933       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13934         {
13935           /* We generate a fixup instead of applying it right now
13936              because, if there are linker relaxations, we're going to
13937              need the relocations.  */
13938           exp.X_op = O_symbol;
13939           exp.X_add_symbol = fragp->fr_symbol;
13940           exp.X_add_number = fragp->fr_offset;
13941
13942           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13943                               4, &exp, 1,
13944                               BFD_RELOC_16_PCREL_S2);
13945           fixp->fx_file = fragp->fr_file;
13946           fixp->fx_line = fragp->fr_line;
13947
13948           md_number_to_chars ((char *)buf, insn, 4);
13949           buf += 4;
13950         }
13951       else
13952         {
13953           int i;
13954
13955           as_warn_where (fragp->fr_file, fragp->fr_line,
13956                          _("relaxed out-of-range branch into a jump"));
13957
13958           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13959             goto uncond;
13960
13961           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13962             {
13963               /* Reverse the branch.  */
13964               switch ((insn >> 28) & 0xf)
13965                 {
13966                 case 4:
13967                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13968                      have the condition reversed by tweaking a single
13969                      bit, and their opcodes all have 0x4???????.  */
13970                   assert ((insn & 0xf1000000) == 0x41000000);
13971                   insn ^= 0x00010000;
13972                   break;
13973
13974                 case 0:
13975                   /* bltz       0x04000000      bgez    0x04010000
13976                      bltzal     0x04100000      bgezal  0x04110000 */
13977                   assert ((insn & 0xfc0e0000) == 0x04000000);
13978                   insn ^= 0x00010000;
13979                   break;
13980
13981                 case 1:
13982                   /* beq        0x10000000      bne     0x14000000
13983                      blez       0x18000000      bgtz    0x1c000000 */
13984                   insn ^= 0x04000000;
13985                   break;
13986
13987                 default:
13988                   abort ();
13989                 }
13990             }
13991
13992           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13993             {
13994               /* Clear the and-link bit.  */
13995               assert ((insn & 0xfc1c0000) == 0x04100000);
13996
13997               /* bltzal 0x04100000      bgezal  0x04110000
13998                 bltzall 0x04120000     bgezall  0x04130000 */
13999               insn &= ~0x00100000;
14000             }
14001
14002           /* Branch over the branch (if the branch was likely) or the
14003              full jump (not likely case).  Compute the offset from the
14004              current instruction to branch to.  */
14005           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14006             i = 16;
14007           else
14008             {
14009               /* How many bytes in instructions we've already emitted?  */
14010               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14011               /* How many bytes in instructions from here to the end?  */
14012               i = fragp->fr_var - i;
14013             }
14014           /* Convert to instruction count.  */
14015           i >>= 2;
14016           /* Branch counts from the next instruction.  */
14017           i--;
14018           insn |= i;
14019           /* Branch over the jump.  */
14020           md_number_to_chars ((char *)buf, insn, 4);
14021           buf += 4;
14022
14023           /* Nop */
14024           md_number_to_chars ((char*)buf, 0, 4);
14025           buf += 4;
14026
14027           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14028             {
14029               /* beql $0, $0, 2f */
14030               insn = 0x50000000;
14031               /* Compute the PC offset from the current instruction to
14032                  the end of the variable frag.  */
14033               /* How many bytes in instructions we've already emitted?  */
14034               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14035               /* How many bytes in instructions from here to the end?  */
14036               i = fragp->fr_var - i;
14037               /* Convert to instruction count.  */
14038               i >>= 2;
14039               /* Don't decrement i, because we want to branch over the
14040                  delay slot.  */
14041
14042               insn |= i;
14043               md_number_to_chars ((char *)buf, insn, 4);
14044               buf += 4;
14045
14046               md_number_to_chars ((char *)buf, 0, 4);
14047               buf += 4;
14048             }
14049
14050         uncond:
14051           if (mips_pic == NO_PIC)
14052             {
14053               /* j or jal.  */
14054               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
14055                       ? 0x0c000000 : 0x08000000);
14056               exp.X_op = O_symbol;
14057               exp.X_add_symbol = fragp->fr_symbol;
14058               exp.X_add_number = fragp->fr_offset;
14059
14060               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14061                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
14062               fixp->fx_file = fragp->fr_file;
14063               fixp->fx_line = fragp->fr_line;
14064
14065               md_number_to_chars ((char*)buf, insn, 4);
14066               buf += 4;
14067             }
14068           else
14069             {
14070               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
14071               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
14072               exp.X_op = O_symbol;
14073               exp.X_add_symbol = fragp->fr_symbol;
14074               exp.X_add_number = fragp->fr_offset;
14075
14076               if (fragp->fr_offset)
14077                 {
14078                   exp.X_add_symbol = make_expr_symbol (&exp);
14079                   exp.X_add_number = 0;
14080                 }
14081
14082               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14083                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
14084               fixp->fx_file = fragp->fr_file;
14085               fixp->fx_line = fragp->fr_line;
14086
14087               md_number_to_chars ((char*)buf, insn, 4);
14088               buf += 4;
14089
14090               if (mips_opts.isa == ISA_MIPS1)
14091                 {
14092                   /* nop */
14093                   md_number_to_chars ((char*)buf, 0, 4);
14094                   buf += 4;
14095                 }
14096
14097               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
14098               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
14099
14100               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14101                                   4, &exp, 0, BFD_RELOC_LO16);
14102               fixp->fx_file = fragp->fr_file;
14103               fixp->fx_line = fragp->fr_line;
14104
14105               md_number_to_chars ((char*)buf, insn, 4);
14106               buf += 4;
14107
14108               /* j(al)r $at.  */
14109               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14110                 insn = 0x0020f809;
14111               else
14112                 insn = 0x00200008;
14113
14114               md_number_to_chars ((char*)buf, insn, 4);
14115               buf += 4;
14116             }
14117         }
14118
14119       assert (buf == (bfd_byte *)fragp->fr_literal
14120               + fragp->fr_fix + fragp->fr_var);
14121
14122       fragp->fr_fix += fragp->fr_var;
14123
14124       return;
14125     }
14126
14127   if (RELAX_MIPS16_P (fragp->fr_subtype))
14128     {
14129       int type;
14130       register const struct mips16_immed_operand *op;
14131       bfd_boolean small, ext;
14132       offsetT val;
14133       bfd_byte *buf;
14134       unsigned long insn;
14135       bfd_boolean use_extend;
14136       unsigned short extend;
14137
14138       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
14139       op = mips16_immed_operands;
14140       while (op->type != type)
14141         ++op;
14142
14143       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14144         {
14145           small = FALSE;
14146           ext = TRUE;
14147         }
14148       else
14149         {
14150           small = TRUE;
14151           ext = FALSE;
14152         }
14153
14154       resolve_symbol_value (fragp->fr_symbol);
14155       val = S_GET_VALUE (fragp->fr_symbol);
14156       if (op->pcrel)
14157         {
14158           addressT addr;
14159
14160           addr = fragp->fr_address + fragp->fr_fix;
14161
14162           /* The rules for the base address of a PC relative reloc are
14163              complicated; see mips16_extended_frag.  */
14164           if (type == 'p' || type == 'q')
14165             {
14166               addr += 2;
14167               if (ext)
14168                 addr += 2;
14169               /* Ignore the low bit in the target, since it will be
14170                  set for a text label.  */
14171               if ((val & 1) != 0)
14172                 --val;
14173             }
14174           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
14175             addr -= 4;
14176           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
14177             addr -= 2;
14178
14179           addr &= ~ (addressT) ((1 << op->shift) - 1);
14180           val -= addr;
14181
14182           /* Make sure the section winds up with the alignment we have
14183              assumed.  */
14184           if (op->shift > 0)
14185             record_alignment (asec, op->shift);
14186         }
14187
14188       if (ext
14189           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
14190               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
14191         as_warn_where (fragp->fr_file, fragp->fr_line,
14192                        _("extended instruction in delay slot"));
14193
14194       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
14195
14196       if (target_big_endian)
14197         insn = bfd_getb16 (buf);
14198       else
14199         insn = bfd_getl16 (buf);
14200
14201       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
14202                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
14203                     small, ext, &insn, &use_extend, &extend);
14204
14205       if (use_extend)
14206         {
14207           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
14208           fragp->fr_fix += 2;
14209           buf += 2;
14210         }
14211
14212       md_number_to_chars ((char *) buf, insn, 2);
14213       fragp->fr_fix += 2;
14214       buf += 2;
14215     }
14216   else
14217     {
14218       if (fragp->fr_opcode == NULL)
14219         return;
14220
14221       old = RELAX_OLD (fragp->fr_subtype);
14222       new = RELAX_NEW (fragp->fr_subtype);
14223       fixptr = fragp->fr_literal + fragp->fr_fix;
14224
14225       if (new > 0)
14226         memmove (fixptr - old, fixptr, new);
14227
14228       fragp->fr_fix += new - old;
14229     }
14230 }
14231
14232 #ifdef OBJ_ELF
14233
14234 /* This function is called after the relocs have been generated.
14235    We've been storing mips16 text labels as odd.  Here we convert them
14236    back to even for the convenience of the debugger.  */
14237
14238 void
14239 mips_frob_file_after_relocs ()
14240 {
14241   asymbol **syms;
14242   unsigned int count, i;
14243
14244   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
14245     return;
14246
14247   syms = bfd_get_outsymbols (stdoutput);
14248   count = bfd_get_symcount (stdoutput);
14249   for (i = 0; i < count; i++, syms++)
14250     {
14251       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
14252           && ((*syms)->value & 1) != 0)
14253         {
14254           (*syms)->value &= ~1;
14255           /* If the symbol has an odd size, it was probably computed
14256              incorrectly, so adjust that as well.  */
14257           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14258             ++elf_symbol (*syms)->internal_elf_sym.st_size;
14259         }
14260     }
14261 }
14262
14263 #endif
14264
14265 /* This function is called whenever a label is defined.  It is used
14266    when handling branch delays; if a branch has a label, we assume we
14267    can not move it.  */
14268
14269 void
14270 mips_define_label (sym)
14271      symbolS *sym;
14272 {
14273   struct insn_label_list *l;
14274
14275   if (free_insn_labels == NULL)
14276     l = (struct insn_label_list *) xmalloc (sizeof *l);
14277   else
14278     {
14279       l = free_insn_labels;
14280       free_insn_labels = l->next;
14281     }
14282
14283   l->label = sym;
14284   l->next = insn_labels;
14285   insn_labels = l;
14286 }
14287 \f
14288 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14289
14290 /* Some special processing for a MIPS ELF file.  */
14291
14292 void
14293 mips_elf_final_processing ()
14294 {
14295   /* Write out the register information.  */
14296   if (mips_abi != N64_ABI)
14297     {
14298       Elf32_RegInfo s;
14299
14300       s.ri_gprmask = mips_gprmask;
14301       s.ri_cprmask[0] = mips_cprmask[0];
14302       s.ri_cprmask[1] = mips_cprmask[1];
14303       s.ri_cprmask[2] = mips_cprmask[2];
14304       s.ri_cprmask[3] = mips_cprmask[3];
14305       /* The gp_value field is set by the MIPS ELF backend.  */
14306
14307       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14308                                        ((Elf32_External_RegInfo *)
14309                                         mips_regmask_frag));
14310     }
14311   else
14312     {
14313       Elf64_Internal_RegInfo s;
14314
14315       s.ri_gprmask = mips_gprmask;
14316       s.ri_pad = 0;
14317       s.ri_cprmask[0] = mips_cprmask[0];
14318       s.ri_cprmask[1] = mips_cprmask[1];
14319       s.ri_cprmask[2] = mips_cprmask[2];
14320       s.ri_cprmask[3] = mips_cprmask[3];
14321       /* The gp_value field is set by the MIPS ELF backend.  */
14322
14323       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14324                                        ((Elf64_External_RegInfo *)
14325                                         mips_regmask_frag));
14326     }
14327
14328   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
14329      sort of BFD interface for this.  */
14330   if (mips_any_noreorder)
14331     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14332   if (mips_pic != NO_PIC)
14333     {
14334     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14335       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14336     }
14337   if (mips_abicalls)
14338     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14339
14340   /* Set MIPS ELF flags for ASEs.  */
14341   if (file_ase_mips16)
14342     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14343 #if 0 /* XXX FIXME */
14344   if (file_ase_mips3d)
14345     elf_elfheader (stdoutput)->e_flags |= ???;
14346 #endif
14347   if (file_ase_mdmx)
14348     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14349
14350   /* Set the MIPS ELF ABI flags.  */
14351   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14352     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14353   else if (mips_abi == O64_ABI)
14354     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14355   else if (mips_abi == EABI_ABI)
14356     {
14357       if (!file_mips_gp32)
14358         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14359       else
14360         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14361     }
14362   else if (mips_abi == N32_ABI)
14363     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14364
14365   /* Nothing to do for N64_ABI.  */
14366
14367   if (mips_32bitmode)
14368     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14369 }
14370
14371 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14372 \f
14373 typedef struct proc {
14374   symbolS *isym;
14375   unsigned long reg_mask;
14376   unsigned long reg_offset;
14377   unsigned long fpreg_mask;
14378   unsigned long fpreg_offset;
14379   unsigned long frame_offset;
14380   unsigned long frame_reg;
14381   unsigned long pc_reg;
14382 } procS;
14383
14384 static procS cur_proc;
14385 static procS *cur_proc_ptr;
14386 static int numprocs;
14387
14388 /* Fill in an rs_align_code fragment.  */
14389
14390 void
14391 mips_handle_align (fragp)
14392      fragS *fragp;
14393 {
14394   if (fragp->fr_type != rs_align_code)
14395     return;
14396
14397   if (mips_opts.mips16)
14398     {
14399       static const unsigned char be_nop[] = { 0x65, 0x00 };
14400       static const unsigned char le_nop[] = { 0x00, 0x65 };
14401
14402       int bytes;
14403       char *p;
14404
14405       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14406       p = fragp->fr_literal + fragp->fr_fix;
14407
14408       if (bytes & 1)
14409         {
14410           *p++ = 0;
14411           fragp->fr_fix++;
14412         }
14413
14414       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14415       fragp->fr_var = 2;
14416     }
14417
14418   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14419 }
14420
14421 static void
14422 md_obj_begin ()
14423 {
14424 }
14425
14426 static void
14427 md_obj_end ()
14428 {
14429   /* check for premature end, nesting errors, etc */
14430   if (cur_proc_ptr)
14431     as_warn (_("missing .end at end of assembly"));
14432 }
14433
14434 static long
14435 get_number ()
14436 {
14437   int negative = 0;
14438   long val = 0;
14439
14440   if (*input_line_pointer == '-')
14441     {
14442       ++input_line_pointer;
14443       negative = 1;
14444     }
14445   if (!ISDIGIT (*input_line_pointer))
14446     as_bad (_("expected simple number"));
14447   if (input_line_pointer[0] == '0')
14448     {
14449       if (input_line_pointer[1] == 'x')
14450         {
14451           input_line_pointer += 2;
14452           while (ISXDIGIT (*input_line_pointer))
14453             {
14454               val <<= 4;
14455               val |= hex_value (*input_line_pointer++);
14456             }
14457           return negative ? -val : val;
14458         }
14459       else
14460         {
14461           ++input_line_pointer;
14462           while (ISDIGIT (*input_line_pointer))
14463             {
14464               val <<= 3;
14465               val |= *input_line_pointer++ - '0';
14466             }
14467           return negative ? -val : val;
14468         }
14469     }
14470   if (!ISDIGIT (*input_line_pointer))
14471     {
14472       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14473               *input_line_pointer, *input_line_pointer);
14474       as_warn (_("invalid number"));
14475       return -1;
14476     }
14477   while (ISDIGIT (*input_line_pointer))
14478     {
14479       val *= 10;
14480       val += *input_line_pointer++ - '0';
14481     }
14482   return negative ? -val : val;
14483 }
14484
14485 /* The .file directive; just like the usual .file directive, but there
14486    is an initial number which is the ECOFF file index.  In the non-ECOFF
14487    case .file implies DWARF-2.  */
14488
14489 static void
14490 s_mips_file (x)
14491      int x ATTRIBUTE_UNUSED;
14492 {
14493   static int first_file_directive = 0;
14494
14495   if (ECOFF_DEBUGGING)
14496     {
14497       get_number ();
14498       s_app_file (0);
14499     }
14500   else
14501     {
14502       char *filename;
14503
14504       filename = dwarf2_directive_file (0);
14505
14506       /* Versions of GCC up to 3.1 start files with a ".file"
14507          directive even for stabs output.  Make sure that this
14508          ".file" is handled.  Note that you need a version of GCC
14509          after 3.1 in order to support DWARF-2 on MIPS.  */
14510       if (filename != NULL && ! first_file_directive)
14511         {
14512           (void) new_logical_line (filename, -1);
14513           s_app_file_string (filename);
14514         }
14515       first_file_directive = 1;
14516     }
14517 }
14518
14519 /* The .loc directive, implying DWARF-2.  */
14520
14521 static void
14522 s_mips_loc (x)
14523      int x ATTRIBUTE_UNUSED;
14524 {
14525   if (!ECOFF_DEBUGGING)
14526     dwarf2_directive_loc (0);
14527 }
14528
14529 /* The .end directive.  */
14530
14531 static void
14532 s_mips_end (x)
14533      int x ATTRIBUTE_UNUSED;
14534 {
14535   symbolS *p;
14536
14537   /* Following functions need their own .frame and .cprestore directives.  */
14538   mips_frame_reg_valid = 0;
14539   mips_cprestore_valid = 0;
14540
14541   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14542     {
14543       p = get_symbol ();
14544       demand_empty_rest_of_line ();
14545     }
14546   else
14547     p = NULL;
14548
14549   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14550     as_warn (_(".end not in text section"));
14551
14552   if (!cur_proc_ptr)
14553     {
14554       as_warn (_(".end directive without a preceding .ent directive."));
14555       demand_empty_rest_of_line ();
14556       return;
14557     }
14558
14559   if (p != NULL)
14560     {
14561       assert (S_GET_NAME (p));
14562       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14563         as_warn (_(".end symbol does not match .ent symbol."));
14564
14565       if (debug_type == DEBUG_STABS)
14566         stabs_generate_asm_endfunc (S_GET_NAME (p),
14567                                     S_GET_NAME (p));
14568     }
14569   else
14570     as_warn (_(".end directive missing or unknown symbol"));
14571
14572 #ifdef OBJ_ELF
14573   /* Generate a .pdr section.  */
14574   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14575     {
14576       segT saved_seg = now_seg;
14577       subsegT saved_subseg = now_subseg;
14578       valueT dot;
14579       expressionS exp;
14580       char *fragp;
14581
14582       dot = frag_now_fix ();
14583
14584 #ifdef md_flush_pending_output
14585       md_flush_pending_output ();
14586 #endif
14587
14588       assert (pdr_seg);
14589       subseg_set (pdr_seg, 0);
14590
14591       /* Write the symbol.  */
14592       exp.X_op = O_symbol;
14593       exp.X_add_symbol = p;
14594       exp.X_add_number = 0;
14595       emit_expr (&exp, 4);
14596
14597       fragp = frag_more (7 * 4);
14598
14599       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14600       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14601       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14602       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14603       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14604       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14605       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14606
14607       subseg_set (saved_seg, saved_subseg);
14608     }
14609 #endif /* OBJ_ELF */
14610
14611   cur_proc_ptr = NULL;
14612 }
14613
14614 /* The .aent and .ent directives.  */
14615
14616 static void
14617 s_mips_ent (aent)
14618      int aent;
14619 {
14620   symbolS *symbolP;
14621
14622   symbolP = get_symbol ();
14623   if (*input_line_pointer == ',')
14624     ++input_line_pointer;
14625   SKIP_WHITESPACE ();
14626   if (ISDIGIT (*input_line_pointer)
14627       || *input_line_pointer == '-')
14628     get_number ();
14629
14630   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14631     as_warn (_(".ent or .aent not in text section."));
14632
14633   if (!aent && cur_proc_ptr)
14634     as_warn (_("missing .end"));
14635
14636   if (!aent)
14637     {
14638       /* This function needs its own .frame and .cprestore directives.  */
14639       mips_frame_reg_valid = 0;
14640       mips_cprestore_valid = 0;
14641
14642       cur_proc_ptr = &cur_proc;
14643       memset (cur_proc_ptr, '\0', sizeof (procS));
14644
14645       cur_proc_ptr->isym = symbolP;
14646
14647       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14648
14649       ++numprocs;
14650
14651       if (debug_type == DEBUG_STABS)
14652         stabs_generate_asm_func (S_GET_NAME (symbolP),
14653                                  S_GET_NAME (symbolP));
14654     }
14655
14656   demand_empty_rest_of_line ();
14657 }
14658
14659 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14660    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14661    s_mips_frame is used so that we can set the PDR information correctly.
14662    We can't use the ecoff routines because they make reference to the ecoff
14663    symbol table (in the mdebug section).  */
14664
14665 static void
14666 s_mips_frame (ignore)
14667      int ignore ATTRIBUTE_UNUSED;
14668 {
14669 #ifdef OBJ_ELF
14670   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14671     {
14672       long val;
14673
14674       if (cur_proc_ptr == (procS *) NULL)
14675         {
14676           as_warn (_(".frame outside of .ent"));
14677           demand_empty_rest_of_line ();
14678           return;
14679         }
14680
14681       cur_proc_ptr->frame_reg = tc_get_register (1);
14682
14683       SKIP_WHITESPACE ();
14684       if (*input_line_pointer++ != ','
14685           || get_absolute_expression_and_terminator (&val) != ',')
14686         {
14687           as_warn (_("Bad .frame directive"));
14688           --input_line_pointer;
14689           demand_empty_rest_of_line ();
14690           return;
14691         }
14692
14693       cur_proc_ptr->frame_offset = val;
14694       cur_proc_ptr->pc_reg = tc_get_register (0);
14695
14696       demand_empty_rest_of_line ();
14697     }
14698   else
14699 #endif /* OBJ_ELF */
14700     s_ignore (ignore);
14701 }
14702
14703 /* The .fmask and .mask directives. If the mdebug section is present
14704    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14705    embedded targets, s_mips_mask is used so that we can set the PDR
14706    information correctly. We can't use the ecoff routines because they
14707    make reference to the ecoff symbol table (in the mdebug section).  */
14708
14709 static void
14710 s_mips_mask (reg_type)
14711      char reg_type;
14712 {
14713 #ifdef OBJ_ELF
14714   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14715     {
14716       long mask, off;
14717
14718       if (cur_proc_ptr == (procS *) NULL)
14719         {
14720           as_warn (_(".mask/.fmask outside of .ent"));
14721           demand_empty_rest_of_line ();
14722           return;
14723         }
14724
14725       if (get_absolute_expression_and_terminator (&mask) != ',')
14726         {
14727           as_warn (_("Bad .mask/.fmask directive"));
14728           --input_line_pointer;
14729           demand_empty_rest_of_line ();
14730           return;
14731         }
14732
14733       off = get_absolute_expression ();
14734
14735       if (reg_type == 'F')
14736         {
14737           cur_proc_ptr->fpreg_mask = mask;
14738           cur_proc_ptr->fpreg_offset = off;
14739         }
14740       else
14741         {
14742           cur_proc_ptr->reg_mask = mask;
14743           cur_proc_ptr->reg_offset = off;
14744         }
14745
14746       demand_empty_rest_of_line ();
14747     }
14748   else
14749 #endif /* OBJ_ELF */
14750     s_ignore (reg_type);
14751 }
14752
14753 /* The .loc directive.  */
14754
14755 #if 0
14756 static void
14757 s_loc (x)
14758      int x;
14759 {
14760   symbolS *symbolP;
14761   int lineno;
14762   int addroff;
14763
14764   assert (now_seg == text_section);
14765
14766   lineno = get_number ();
14767   addroff = frag_now_fix ();
14768
14769   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14770   S_SET_TYPE (symbolP, N_SLINE);
14771   S_SET_OTHER (symbolP, 0);
14772   S_SET_DESC (symbolP, lineno);
14773   symbolP->sy_segment = now_seg;
14774 }
14775 #endif
14776
14777 /* A table describing all the processors gas knows about.  Names are
14778    matched in the order listed.
14779
14780    To ease comparison, please keep this table in the same order as
14781    gcc's mips_cpu_info_table[].  */
14782 static const struct mips_cpu_info mips_cpu_info_table[] =
14783 {
14784   /* Entries for generic ISAs */
14785   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14786   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14787   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14788   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14789   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14790   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14791   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14792   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14793
14794   /* MIPS I */
14795   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14796   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14797   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14798
14799   /* MIPS II */
14800   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14801
14802   /* MIPS III */
14803   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14804   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14805   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14806   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14807   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14808   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14809   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14810   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14811   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14812   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14813   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14814   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14815
14816   /* MIPS IV */
14817   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14818   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14819   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14820   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14821   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14822   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14823   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14824   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14825   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14826   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14827   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14828   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14829
14830   /* MIPS 32 */
14831   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14832   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14833   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14834
14835   /* MIPS 64 */
14836   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14837   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14838
14839   /* Broadcom SB-1 CPU core */
14840   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14841
14842   /* End marker */
14843   { NULL, 0, 0, 0 }
14844 };
14845
14846
14847 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14848    with a final "000" replaced by "k".  Ignore case.
14849
14850    Note: this function is shared between GCC and GAS.  */
14851
14852 static bfd_boolean
14853 mips_strict_matching_cpu_name_p (canonical, given)
14854      const char *canonical, *given;
14855 {
14856   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14857     given++, canonical++;
14858
14859   return ((*given == 0 && *canonical == 0)
14860           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14861 }
14862
14863
14864 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14865    CPU name.  We've traditionally allowed a lot of variation here.
14866
14867    Note: this function is shared between GCC and GAS.  */
14868
14869 static bfd_boolean
14870 mips_matching_cpu_name_p (canonical, given)
14871      const char *canonical, *given;
14872 {
14873   /* First see if the name matches exactly, or with a final "000"
14874      turned into "k".  */
14875   if (mips_strict_matching_cpu_name_p (canonical, given))
14876     return TRUE;
14877
14878   /* If not, try comparing based on numerical designation alone.
14879      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14880   if (TOLOWER (*given) == 'r')
14881     given++;
14882   if (!ISDIGIT (*given))
14883     return FALSE;
14884
14885   /* Skip over some well-known prefixes in the canonical name,
14886      hoping to find a number there too.  */
14887   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14888     canonical += 2;
14889   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14890     canonical += 2;
14891   else if (TOLOWER (canonical[0]) == 'r')
14892     canonical += 1;
14893
14894   return mips_strict_matching_cpu_name_p (canonical, given);
14895 }
14896
14897
14898 /* Parse an option that takes the name of a processor as its argument.
14899    OPTION is the name of the option and CPU_STRING is the argument.
14900    Return the corresponding processor enumeration if the CPU_STRING is
14901    recognized, otherwise report an error and return null.
14902
14903    A similar function exists in GCC.  */
14904
14905 static const struct mips_cpu_info *
14906 mips_parse_cpu (option, cpu_string)
14907      const char *option, *cpu_string;
14908 {
14909   const struct mips_cpu_info *p;
14910
14911   /* 'from-abi' selects the most compatible architecture for the given
14912      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14913      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14914      version.  Look first at the -mgp options, if given, otherwise base
14915      the choice on MIPS_DEFAULT_64BIT.
14916
14917      Treat NO_ABI like the EABIs.  One reason to do this is that the
14918      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14919      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14920      'mips64', just as we did in the days before 'from-abi'.  */
14921   if (strcasecmp (cpu_string, "from-abi") == 0)
14922     {
14923       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14924         return mips_cpu_info_from_isa (ISA_MIPS1);
14925
14926       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14927         return mips_cpu_info_from_isa (ISA_MIPS3);
14928
14929       if (file_mips_gp32 >= 0)
14930         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14931
14932       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14933                                      ? ISA_MIPS3
14934                                      : ISA_MIPS1);
14935     }
14936
14937   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14938   if (strcasecmp (cpu_string, "default") == 0)
14939     return 0;
14940
14941   for (p = mips_cpu_info_table; p->name != 0; p++)
14942     if (mips_matching_cpu_name_p (p->name, cpu_string))
14943       return p;
14944
14945   as_bad ("Bad value (%s) for %s", cpu_string, option);
14946   return 0;
14947 }
14948
14949 /* Return the canonical processor information for ISA (a member of the
14950    ISA_MIPS* enumeration).  */
14951
14952 static const struct mips_cpu_info *
14953 mips_cpu_info_from_isa (isa)
14954      int isa;
14955 {
14956   int i;
14957
14958   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14959     if (mips_cpu_info_table[i].is_isa
14960         && isa == mips_cpu_info_table[i].isa)
14961       return (&mips_cpu_info_table[i]);
14962
14963   return NULL;
14964 }
14965 \f
14966 static void
14967 show (stream, string, col_p, first_p)
14968      FILE *stream;
14969      const char *string;
14970      int *col_p;
14971      int *first_p;
14972 {
14973   if (*first_p)
14974     {
14975       fprintf (stream, "%24s", "");
14976       *col_p = 24;
14977     }
14978   else
14979     {
14980       fprintf (stream, ", ");
14981       *col_p += 2;
14982     }
14983
14984   if (*col_p + strlen (string) > 72)
14985     {
14986       fprintf (stream, "\n%24s", "");
14987       *col_p = 24;
14988     }
14989
14990   fprintf (stream, "%s", string);
14991   *col_p += strlen (string);
14992
14993   *first_p = 0;
14994 }
14995
14996 void
14997 md_show_usage (stream)
14998      FILE *stream;
14999 {
15000   int column, first;
15001   size_t i;
15002
15003   fprintf (stream, _("\
15004 MIPS options:\n\
15005 -membedded-pic          generate embedded position independent code\n\
15006 -EB                     generate big endian output\n\
15007 -EL                     generate little endian output\n\
15008 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
15009 -G NUM                  allow referencing objects up to NUM bytes\n\
15010                         implicitly with the gp register [default 8]\n"));
15011   fprintf (stream, _("\
15012 -mips1                  generate MIPS ISA I instructions\n\
15013 -mips2                  generate MIPS ISA II instructions\n\
15014 -mips3                  generate MIPS ISA III instructions\n\
15015 -mips4                  generate MIPS ISA IV instructions\n\
15016 -mips5                  generate MIPS ISA V instructions\n\
15017 -mips32                 generate MIPS32 ISA instructions\n\
15018 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
15019 -mips64                 generate MIPS64 ISA instructions\n\
15020 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
15021
15022   first = 1;
15023
15024   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15025     show (stream, mips_cpu_info_table[i].name, &column, &first);
15026   show (stream, "from-abi", &column, &first);
15027   fputc ('\n', stream);
15028
15029   fprintf (stream, _("\
15030 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
15031 -no-mCPU                don't generate code specific to CPU.\n\
15032                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
15033
15034   first = 1;
15035
15036   show (stream, "3900", &column, &first);
15037   show (stream, "4010", &column, &first);
15038   show (stream, "4100", &column, &first);
15039   show (stream, "4650", &column, &first);
15040   fputc ('\n', stream);
15041
15042   fprintf (stream, _("\
15043 -mips16                 generate mips16 instructions\n\
15044 -no-mips16              do not generate mips16 instructions\n"));
15045   fprintf (stream, _("\
15046 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
15047 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
15048 -O0                     remove unneeded NOPs, do not swap branches\n\
15049 -O                      remove unneeded NOPs and swap branches\n\
15050 -n                      warn about NOPs generated from macros\n\
15051 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
15052 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
15053 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
15054 #ifdef OBJ_ELF
15055   fprintf (stream, _("\
15056 -KPIC, -call_shared     generate SVR4 position independent code\n\
15057 -non_shared             do not generate position independent code\n\
15058 -xgot                   assume a 32 bit GOT\n\
15059 -mabi=ABI               create ABI conformant object file for:\n"));
15060
15061   first = 1;
15062
15063   show (stream, "32", &column, &first);
15064   show (stream, "o64", &column, &first);
15065   show (stream, "n32", &column, &first);
15066   show (stream, "64", &column, &first);
15067   show (stream, "eabi", &column, &first);
15068
15069   fputc ('\n', stream);
15070
15071   fprintf (stream, _("\
15072 -32                     create o32 ABI object file (default)\n\
15073 -n32                    create n32 ABI object file\n\
15074 -64                     create 64 ABI object file\n"));
15075 #endif
15076 }
15077
15078 enum dwarf2_format
15079 mips_dwarf2_format ()
15080 {
15081   if (mips_abi == N64_ABI)
15082     {
15083 #ifdef TE_IRIX
15084       return dwarf2_format_64bit_irix;
15085 #else
15086       return dwarf2_format_64bit;
15087 #endif
15088     }
15089   else
15090     return dwarf2_format_32bit;
15091 }