* config/tc-mips.c (append_insn): Use actual relocation size for new
[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           reloc_howto_type *howto;
2158
2159         need_reloc:
2160           /* Don't generate a reloc if we are writing into a variant frag.  */
2161           if (place == NULL)
2162             {
2163               howto = bfd_reloc_type_lookup (stdoutput, reloc_type[0]);
2164               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2165                                      bfd_get_reloc_size(howto),
2166                                      address_expr,
2167                                      reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2168                                      reloc_type[0]);
2169
2170               /* These relocations can have an addend that won't fit in
2171                  4 octets for 64bit assembly.  */
2172               if (HAVE_64BIT_GPRS
2173                   && ! howto->partial_inplace
2174                   && (reloc_type[0] == BFD_RELOC_16
2175                       || reloc_type[0] == BFD_RELOC_32
2176                       || reloc_type[0] == BFD_RELOC_MIPS_JMP
2177                       || reloc_type[0] == BFD_RELOC_HI16_S
2178                       || reloc_type[0] == BFD_RELOC_LO16
2179                       || reloc_type[0] == BFD_RELOC_GPREL16
2180                       || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2181                       || reloc_type[0] == BFD_RELOC_GPREL32
2182                       || reloc_type[0] == BFD_RELOC_64
2183                       || reloc_type[0] == BFD_RELOC_CTOR
2184                       || reloc_type[0] == BFD_RELOC_MIPS_SUB
2185                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2186                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2187                       || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2188                       || reloc_type[0] == BFD_RELOC_MIPS_REL16
2189                       || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2190                 fixp[0]->fx_no_overflow = 1;
2191
2192               if (reloc_needs_lo_p (*reloc_type))
2193                 {
2194                   struct mips_hi_fixup *hi_fixup;
2195
2196                   /* Reuse the last entry if it already has a matching %lo.  */
2197                   hi_fixup = mips_hi_fixup_list;
2198                   if (hi_fixup == 0
2199                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2200                     {
2201                       hi_fixup = ((struct mips_hi_fixup *)
2202                                   xmalloc (sizeof (struct mips_hi_fixup)));
2203                       hi_fixup->next = mips_hi_fixup_list;
2204                       mips_hi_fixup_list = hi_fixup;
2205                     }
2206                   hi_fixup->fixp = fixp[0];
2207                   hi_fixup->seg = now_seg;
2208                 }
2209
2210               if (reloc_type[1] != BFD_RELOC_UNUSED)
2211                 {
2212                   /* FIXME: This symbol can be one of
2213                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2214                   address_expr->X_op = O_absent;
2215                   address_expr->X_add_symbol = 0;
2216                   address_expr->X_add_number = 0;
2217
2218                   howto = bfd_reloc_type_lookup (stdoutput, reloc_type[1]);
2219                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2220                                          bfd_get_reloc_size(howto),
2221                                          address_expr, FALSE, reloc_type[1]);
2222
2223                   /* These relocations can have an addend that won't fit in
2224                      4 octets for 64bit assembly.  */
2225                   if (HAVE_64BIT_GPRS
2226                       && ! howto->partial_inplace
2227                       && (reloc_type[1] == BFD_RELOC_16
2228                           || reloc_type[1] == BFD_RELOC_32
2229                           || reloc_type[1] == BFD_RELOC_MIPS_JMP
2230                           || reloc_type[1] == BFD_RELOC_HI16_S
2231                           || reloc_type[1] == BFD_RELOC_LO16
2232                           || reloc_type[1] == BFD_RELOC_GPREL16
2233                           || reloc_type[1] == BFD_RELOC_MIPS_LITERAL
2234                           || reloc_type[1] == BFD_RELOC_GPREL32
2235                           || reloc_type[1] == BFD_RELOC_64
2236                           || reloc_type[1] == BFD_RELOC_CTOR
2237                           || reloc_type[1] == BFD_RELOC_MIPS_SUB
2238                           || reloc_type[1] == BFD_RELOC_MIPS_HIGHEST
2239                           || reloc_type[1] == BFD_RELOC_MIPS_HIGHER
2240                           || reloc_type[1] == BFD_RELOC_MIPS_SCN_DISP
2241                           || reloc_type[1] == BFD_RELOC_MIPS_REL16
2242                           || reloc_type[1] == BFD_RELOC_MIPS_RELGOT))
2243                     fixp[1]->fx_no_overflow = 1;
2244
2245                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2246                     {
2247                       address_expr->X_op = O_absent;
2248                       address_expr->X_add_symbol = 0;
2249                       address_expr->X_add_number = 0;
2250
2251                       howto = bfd_reloc_type_lookup (stdoutput, reloc_type[2]);
2252                       fixp[2] = fix_new_exp (frag_now,
2253                                              f - frag_now->fr_literal,
2254                                              bfd_get_reloc_size(howto),
2255                                              address_expr, FALSE,
2256                                              reloc_type[2]);
2257
2258                       /* These relocations can have an addend that won't fit in
2259                          4 octets for 64bit assembly.  */
2260                       if (HAVE_64BIT_GPRS
2261                           && ! howto->partial_inplace
2262                           && (reloc_type[2] == BFD_RELOC_16
2263                               || reloc_type[2] == BFD_RELOC_32
2264                               || reloc_type[2] == BFD_RELOC_MIPS_JMP
2265                               || reloc_type[2] == BFD_RELOC_HI16_S
2266                               || reloc_type[2] == BFD_RELOC_LO16
2267                               || reloc_type[2] == BFD_RELOC_GPREL16
2268                               || reloc_type[2] == BFD_RELOC_MIPS_LITERAL
2269                               || reloc_type[2] == BFD_RELOC_GPREL32
2270                               || reloc_type[2] == BFD_RELOC_64
2271                               || reloc_type[2] == BFD_RELOC_CTOR
2272                               || reloc_type[2] == BFD_RELOC_MIPS_SUB
2273                               || reloc_type[2] == BFD_RELOC_MIPS_HIGHEST
2274                               || reloc_type[2] == BFD_RELOC_MIPS_HIGHER
2275                               || reloc_type[2] == BFD_RELOC_MIPS_SCN_DISP
2276                               || reloc_type[2] == BFD_RELOC_MIPS_REL16
2277                               || reloc_type[2] == BFD_RELOC_MIPS_RELGOT))
2278                         fixp[2]->fx_no_overflow = 1;
2279                     }
2280                 }
2281             }
2282         }
2283     }
2284
2285   if (! mips_opts.mips16)
2286     {
2287       md_number_to_chars (f, ip->insn_opcode, 4);
2288 #ifdef OBJ_ELF
2289       dwarf2_emit_insn (4);
2290 #endif
2291     }
2292   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2293     {
2294       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2295       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2296 #ifdef OBJ_ELF
2297       dwarf2_emit_insn (4);
2298 #endif
2299     }
2300   else
2301     {
2302       if (ip->use_extend)
2303         {
2304           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2305           f += 2;
2306         }
2307       md_number_to_chars (f, ip->insn_opcode, 2);
2308 #ifdef OBJ_ELF
2309       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2310 #endif
2311     }
2312
2313   /* Update the register mask information.  */
2314   if (! mips_opts.mips16)
2315     {
2316       if (pinfo & INSN_WRITE_GPR_D)
2317         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2318       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2319         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2320       if (pinfo & INSN_READ_GPR_S)
2321         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2322       if (pinfo & INSN_WRITE_GPR_31)
2323         mips_gprmask |= 1 << RA;
2324       if (pinfo & INSN_WRITE_FPR_D)
2325         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2326       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2327         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2328       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2329         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2330       if ((pinfo & INSN_READ_FPR_R) != 0)
2331         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2332       if (pinfo & INSN_COP)
2333         {
2334           /* We don't keep enough information to sort these cases out.
2335              The itbl support does keep this information however, although
2336              we currently don't support itbl fprmats as part of the cop
2337              instruction.  May want to add this support in the future.  */
2338         }
2339       /* Never set the bit for $0, which is always zero.  */
2340       mips_gprmask &= ~1 << 0;
2341     }
2342   else
2343     {
2344       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2345         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2346                               & MIPS16OP_MASK_RX);
2347       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2348         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2349                               & MIPS16OP_MASK_RY);
2350       if (pinfo & MIPS16_INSN_WRITE_Z)
2351         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2352                               & MIPS16OP_MASK_RZ);
2353       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2354         mips_gprmask |= 1 << TREG;
2355       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2356         mips_gprmask |= 1 << SP;
2357       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2358         mips_gprmask |= 1 << RA;
2359       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2360         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2361       if (pinfo & MIPS16_INSN_READ_Z)
2362         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2363                               & MIPS16OP_MASK_MOVE32Z);
2364       if (pinfo & MIPS16_INSN_READ_GPR_X)
2365         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2366                               & MIPS16OP_MASK_REGR32);
2367     }
2368
2369   if (place == NULL && ! mips_opts.noreorder)
2370     {
2371       /* Filling the branch delay slot is more complex.  We try to
2372          switch the branch with the previous instruction, which we can
2373          do if the previous instruction does not set up a condition
2374          that the branch tests and if the branch is not itself the
2375          target of any branch.  */
2376       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2377           || (pinfo & INSN_COND_BRANCH_DELAY))
2378         {
2379           if (mips_optimize < 2
2380               /* If we have seen .set volatile or .set nomove, don't
2381                  optimize.  */
2382               || mips_opts.nomove != 0
2383               /* If we had to emit any NOP instructions, then we
2384                  already know we can not swap.  */
2385               || nops != 0
2386               /* If we don't even know the previous insn, we can not
2387                  swap.  */
2388               || ! prev_insn_valid
2389               /* If the previous insn is already in a branch delay
2390                  slot, then we can not swap.  */
2391               || prev_insn_is_delay_slot
2392               /* If the previous previous insn was in a .set
2393                  noreorder, we can't swap.  Actually, the MIPS
2394                  assembler will swap in this situation.  However, gcc
2395                  configured -with-gnu-as will generate code like
2396                    .set noreorder
2397                    lw   $4,XXX
2398                    .set reorder
2399                    INSN
2400                    bne  $4,$0,foo
2401                  in which we can not swap the bne and INSN.  If gcc is
2402                  not configured -with-gnu-as, it does not output the
2403                  .set pseudo-ops.  We don't have to check
2404                  prev_insn_unreordered, because prev_insn_valid will
2405                  be 0 in that case.  We don't want to use
2406                  prev_prev_insn_valid, because we do want to be able
2407                  to swap at the start of a function.  */
2408               || prev_prev_insn_unreordered
2409               /* If the branch is itself the target of a branch, we
2410                  can not swap.  We cheat on this; all we check for is
2411                  whether there is a label on this instruction.  If
2412                  there are any branches to anything other than a
2413                  label, users must use .set noreorder.  */
2414               || insn_labels != NULL
2415               /* If the previous instruction is in a variant frag, we
2416                  can not do the swap.  This does not apply to the
2417                  mips16, which uses variant frags for different
2418                  purposes.  */
2419               || (! mips_opts.mips16
2420                   && prev_insn_frag->fr_type == rs_machine_dependent)
2421               /* If the branch reads the condition codes, we don't
2422                  even try to swap, because in the sequence
2423                    ctc1 $X,$31
2424                    INSN
2425                    INSN
2426                    bc1t LABEL
2427                  we can not swap, and I don't feel like handling that
2428                  case.  */
2429               || (! mips_opts.mips16
2430                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2431                   && (pinfo & INSN_READ_COND_CODE))
2432               /* We can not swap with an instruction that requires a
2433                  delay slot, becase the target of the branch might
2434                  interfere with that instruction.  */
2435               || (! mips_opts.mips16
2436                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2437                   && (prev_pinfo
2438               /* Itbl support may require additional care here.  */
2439                       & (INSN_LOAD_COPROC_DELAY
2440                          | INSN_COPROC_MOVE_DELAY
2441                          | INSN_WRITE_COND_CODE)))
2442               || (! (hilo_interlocks
2443                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2444                   && (prev_pinfo
2445                       & (INSN_READ_LO
2446                          | INSN_READ_HI)))
2447               || (! mips_opts.mips16
2448                   && ! gpr_interlocks
2449                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2450               || (! mips_opts.mips16
2451                   && mips_opts.isa == ISA_MIPS1
2452                   /* Itbl support may require additional care here.  */
2453                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2454               /* We can not swap with a branch instruction.  */
2455               || (prev_pinfo
2456                   & (INSN_UNCOND_BRANCH_DELAY
2457                      | INSN_COND_BRANCH_DELAY
2458                      | INSN_COND_BRANCH_LIKELY))
2459               /* We do not swap with a trap instruction, since it
2460                  complicates trap handlers to have the trap
2461                  instruction be in a delay slot.  */
2462               || (prev_pinfo & INSN_TRAP)
2463               /* If the branch reads a register that the previous
2464                  instruction sets, we can not swap.  */
2465               || (! mips_opts.mips16
2466                   && (prev_pinfo & INSN_WRITE_GPR_T)
2467                   && insn_uses_reg (ip,
2468                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2469                                      & OP_MASK_RT),
2470                                     MIPS_GR_REG))
2471               || (! mips_opts.mips16
2472                   && (prev_pinfo & INSN_WRITE_GPR_D)
2473                   && insn_uses_reg (ip,
2474                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2475                                      & OP_MASK_RD),
2476                                     MIPS_GR_REG))
2477               || (mips_opts.mips16
2478                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2479                        && insn_uses_reg (ip,
2480                                          ((prev_insn.insn_opcode
2481                                            >> MIPS16OP_SH_RX)
2482                                           & MIPS16OP_MASK_RX),
2483                                          MIPS16_REG))
2484                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2485                           && insn_uses_reg (ip,
2486                                             ((prev_insn.insn_opcode
2487                                               >> MIPS16OP_SH_RY)
2488                                              & MIPS16OP_MASK_RY),
2489                                             MIPS16_REG))
2490                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2491                           && insn_uses_reg (ip,
2492                                             ((prev_insn.insn_opcode
2493                                               >> MIPS16OP_SH_RZ)
2494                                              & MIPS16OP_MASK_RZ),
2495                                             MIPS16_REG))
2496                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2497                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2498                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2499                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2500                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2501                           && insn_uses_reg (ip,
2502                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2503                                                                      insn_opcode),
2504                                             MIPS_GR_REG))))
2505               /* If the branch writes a register that the previous
2506                  instruction sets, we can not swap (we know that
2507                  branches write only to RD or to $31).  */
2508               || (! mips_opts.mips16
2509                   && (prev_pinfo & INSN_WRITE_GPR_T)
2510                   && (((pinfo & INSN_WRITE_GPR_D)
2511                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2512                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2513                       || ((pinfo & INSN_WRITE_GPR_31)
2514                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2515                                & OP_MASK_RT)
2516                               == RA))))
2517               || (! mips_opts.mips16
2518                   && (prev_pinfo & INSN_WRITE_GPR_D)
2519                   && (((pinfo & INSN_WRITE_GPR_D)
2520                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2521                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2522                       || ((pinfo & INSN_WRITE_GPR_31)
2523                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2524                                & OP_MASK_RD)
2525                               == RA))))
2526               || (mips_opts.mips16
2527                   && (pinfo & MIPS16_INSN_WRITE_31)
2528                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2529                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2530                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2531                               == RA))))
2532               /* If the branch writes a register that the previous
2533                  instruction reads, we can not swap (we know that
2534                  branches only write to RD or to $31).  */
2535               || (! mips_opts.mips16
2536                   && (pinfo & INSN_WRITE_GPR_D)
2537                   && insn_uses_reg (&prev_insn,
2538                                     ((ip->insn_opcode >> OP_SH_RD)
2539                                      & OP_MASK_RD),
2540                                     MIPS_GR_REG))
2541               || (! mips_opts.mips16
2542                   && (pinfo & INSN_WRITE_GPR_31)
2543                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2544               || (mips_opts.mips16
2545                   && (pinfo & MIPS16_INSN_WRITE_31)
2546                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2547               /* If we are generating embedded PIC code, the branch
2548                  might be expanded into a sequence which uses $at, so
2549                  we can't swap with an instruction which reads it.  */
2550               || (mips_pic == EMBEDDED_PIC
2551                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2552               /* If the previous previous instruction has a load
2553                  delay, and sets a register that the branch reads, we
2554                  can not swap.  */
2555               || (! mips_opts.mips16
2556                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2557               /* Itbl support may require additional care here.  */
2558                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2559                       || (! gpr_interlocks
2560                           && (prev_prev_insn.insn_mo->pinfo
2561                               & INSN_LOAD_MEMORY_DELAY)))
2562                   && insn_uses_reg (ip,
2563                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2564                                      & OP_MASK_RT),
2565                                     MIPS_GR_REG))
2566               /* If one instruction sets a condition code and the
2567                  other one uses a condition code, we can not swap.  */
2568               || ((pinfo & INSN_READ_COND_CODE)
2569                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2570               || ((pinfo & INSN_WRITE_COND_CODE)
2571                   && (prev_pinfo & INSN_READ_COND_CODE))
2572               /* If the previous instruction uses the PC, we can not
2573                  swap.  */
2574               || (mips_opts.mips16
2575                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2576               /* If the previous instruction was extended, we can not
2577                  swap.  */
2578               || (mips_opts.mips16 && prev_insn_extended)
2579               /* If the previous instruction had a fixup in mips16
2580                  mode, we can not swap.  This normally means that the
2581                  previous instruction was a 4 byte branch anyhow.  */
2582               || (mips_opts.mips16 && prev_insn_fixp[0])
2583               /* If the previous instruction is a sync, sync.l, or
2584                  sync.p, we can not swap.  */
2585               || (prev_pinfo & INSN_SYNC))
2586             {
2587               /* We could do even better for unconditional branches to
2588                  portions of this object file; we could pick up the
2589                  instruction at the destination, put it in the delay
2590                  slot, and bump the destination address.  */
2591               emit_nop ();
2592               /* Update the previous insn information.  */
2593               prev_prev_insn = *ip;
2594               prev_insn.insn_mo = &dummy_opcode;
2595             }
2596           else
2597             {
2598               /* It looks like we can actually do the swap.  */
2599               if (! mips_opts.mips16)
2600                 {
2601                   char *prev_f;
2602                   char temp[4];
2603
2604                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2605                   memcpy (temp, prev_f, 4);
2606                   memcpy (prev_f, f, 4);
2607                   memcpy (f, temp, 4);
2608                   if (prev_insn_fixp[0])
2609                     {
2610                       prev_insn_fixp[0]->fx_frag = frag_now;
2611                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2612                     }
2613                   if (prev_insn_fixp[1])
2614                     {
2615                       prev_insn_fixp[1]->fx_frag = frag_now;
2616                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2617                     }
2618                   if (prev_insn_fixp[2])
2619                     {
2620                       prev_insn_fixp[2]->fx_frag = frag_now;
2621                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2622                     }
2623                   if (prev_insn_fixp[0] && HAVE_NEWABI
2624                       && prev_insn_frag != frag_now
2625                       && (prev_insn_fixp[0]->fx_r_type
2626                           == BFD_RELOC_MIPS_GOT_DISP
2627                           || (prev_insn_fixp[0]->fx_r_type
2628                               == BFD_RELOC_MIPS_CALL16)))
2629                     {
2630                       /* To avoid confusion in tc_gen_reloc, we must
2631                          ensure that this does not become a variant
2632                          frag.  */
2633                       force_new_frag = TRUE;
2634                     }
2635                   if (fixp[0])
2636                     {
2637                       fixp[0]->fx_frag = prev_insn_frag;
2638                       fixp[0]->fx_where = prev_insn_where;
2639                     }
2640                   if (fixp[1])
2641                     {
2642                       fixp[1]->fx_frag = prev_insn_frag;
2643                       fixp[1]->fx_where = prev_insn_where;
2644                     }
2645                   if (fixp[2])
2646                     {
2647                       fixp[2]->fx_frag = prev_insn_frag;
2648                       fixp[2]->fx_where = prev_insn_where;
2649                     }
2650                 }
2651               else
2652                 {
2653                   char *prev_f;
2654                   char temp[2];
2655
2656                   assert (prev_insn_fixp[0] == NULL);
2657                   assert (prev_insn_fixp[1] == NULL);
2658                   assert (prev_insn_fixp[2] == NULL);
2659                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2660                   memcpy (temp, prev_f, 2);
2661                   memcpy (prev_f, f, 2);
2662                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2663                     {
2664                       assert (*reloc_type == BFD_RELOC_UNUSED);
2665                       memcpy (f, temp, 2);
2666                     }
2667                   else
2668                     {
2669                       memcpy (f, f + 2, 2);
2670                       memcpy (f + 2, temp, 2);
2671                     }
2672                   if (fixp[0])
2673                     {
2674                       fixp[0]->fx_frag = prev_insn_frag;
2675                       fixp[0]->fx_where = prev_insn_where;
2676                     }
2677                   if (fixp[1])
2678                     {
2679                       fixp[1]->fx_frag = prev_insn_frag;
2680                       fixp[1]->fx_where = prev_insn_where;
2681                     }
2682                   if (fixp[2])
2683                     {
2684                       fixp[2]->fx_frag = prev_insn_frag;
2685                       fixp[2]->fx_where = prev_insn_where;
2686                     }
2687                 }
2688
2689               /* Update the previous insn information; leave prev_insn
2690                  unchanged.  */
2691               prev_prev_insn = *ip;
2692             }
2693           prev_insn_is_delay_slot = 1;
2694
2695           /* If that was an unconditional branch, forget the previous
2696              insn information.  */
2697           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2698             {
2699               prev_prev_insn.insn_mo = &dummy_opcode;
2700               prev_insn.insn_mo = &dummy_opcode;
2701             }
2702
2703           prev_insn_fixp[0] = NULL;
2704           prev_insn_fixp[1] = NULL;
2705           prev_insn_fixp[2] = NULL;
2706           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2707           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2708           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2709           prev_insn_extended = 0;
2710         }
2711       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2712         {
2713           /* We don't yet optimize a branch likely.  What we should do
2714              is look at the target, copy the instruction found there
2715              into the delay slot, and increment the branch to jump to
2716              the next instruction.  */
2717           emit_nop ();
2718           /* Update the previous insn information.  */
2719           prev_prev_insn = *ip;
2720           prev_insn.insn_mo = &dummy_opcode;
2721           prev_insn_fixp[0] = NULL;
2722           prev_insn_fixp[1] = NULL;
2723           prev_insn_fixp[2] = NULL;
2724           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2725           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2726           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2727           prev_insn_extended = 0;
2728         }
2729       else
2730         {
2731           /* Update the previous insn information.  */
2732           if (nops > 0)
2733             prev_prev_insn.insn_mo = &dummy_opcode;
2734           else
2735             prev_prev_insn = prev_insn;
2736           prev_insn = *ip;
2737
2738           /* Any time we see a branch, we always fill the delay slot
2739              immediately; since this insn is not a branch, we know it
2740              is not in a delay slot.  */
2741           prev_insn_is_delay_slot = 0;
2742
2743           prev_insn_fixp[0] = fixp[0];
2744           prev_insn_fixp[1] = fixp[1];
2745           prev_insn_fixp[2] = fixp[2];
2746           prev_insn_reloc_type[0] = reloc_type[0];
2747           prev_insn_reloc_type[1] = reloc_type[1];
2748           prev_insn_reloc_type[2] = reloc_type[2];
2749           if (mips_opts.mips16)
2750             prev_insn_extended = (ip->use_extend
2751                                   || *reloc_type > BFD_RELOC_UNUSED);
2752         }
2753
2754       prev_prev_insn_unreordered = prev_insn_unreordered;
2755       prev_insn_unreordered = 0;
2756       prev_insn_frag = frag_now;
2757       prev_insn_where = f - frag_now->fr_literal;
2758       prev_insn_valid = 1;
2759     }
2760   else if (place == NULL)
2761     {
2762       /* We need to record a bit of information even when we are not
2763          reordering, in order to determine the base address for mips16
2764          PC relative relocs.  */
2765       prev_prev_insn = prev_insn;
2766       prev_insn = *ip;
2767       prev_insn_reloc_type[0] = reloc_type[0];
2768       prev_insn_reloc_type[1] = reloc_type[1];
2769       prev_insn_reloc_type[2] = reloc_type[2];
2770       prev_prev_insn_unreordered = prev_insn_unreordered;
2771       prev_insn_unreordered = 1;
2772     }
2773
2774   /* We just output an insn, so the next one doesn't have a label.  */
2775   mips_clear_insn_labels ();
2776
2777   /* We must ensure that the frag to which an instruction that was
2778      moved from a non-variant frag doesn't become a variant frag,
2779      otherwise tc_gen_reloc may get confused.  */
2780   if (force_new_frag)
2781     {
2782       frag_wane (frag_now);
2783       frag_new (0);
2784     }
2785 }
2786
2787 /* This function forgets that there was any previous instruction or
2788    label.  If PRESERVE is non-zero, it remembers enough information to
2789    know whether nops are needed before a noreorder section.  */
2790
2791 static void
2792 mips_no_prev_insn (preserve)
2793      int preserve;
2794 {
2795   if (! preserve)
2796     {
2797       prev_insn.insn_mo = &dummy_opcode;
2798       prev_prev_insn.insn_mo = &dummy_opcode;
2799       prev_nop_frag = NULL;
2800       prev_nop_frag_holds = 0;
2801       prev_nop_frag_required = 0;
2802       prev_nop_frag_since = 0;
2803     }
2804   prev_insn_valid = 0;
2805   prev_insn_is_delay_slot = 0;
2806   prev_insn_unreordered = 0;
2807   prev_insn_extended = 0;
2808   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2809   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2810   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2811   prev_prev_insn_unreordered = 0;
2812   mips_clear_insn_labels ();
2813 }
2814
2815 /* This function must be called whenever we turn on noreorder or emit
2816    something other than instructions.  It inserts any NOPS which might
2817    be needed by the previous instruction, and clears the information
2818    kept for the previous instructions.  The INSNS parameter is true if
2819    instructions are to follow.  */
2820
2821 static void
2822 mips_emit_delays (insns)
2823      bfd_boolean insns;
2824 {
2825   if (! mips_opts.noreorder)
2826     {
2827       int nops;
2828
2829       nops = 0;
2830       if ((! mips_opts.mips16
2831            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2832            && (! cop_interlocks
2833                && (prev_insn.insn_mo->pinfo
2834                    & (INSN_LOAD_COPROC_DELAY
2835                       | INSN_COPROC_MOVE_DELAY
2836                       | INSN_WRITE_COND_CODE))))
2837           || (! hilo_interlocks
2838               && (prev_insn.insn_mo->pinfo
2839                   & (INSN_READ_LO
2840                      | INSN_READ_HI)))
2841           || (! mips_opts.mips16
2842               && ! gpr_interlocks
2843               && (prev_insn.insn_mo->pinfo
2844                   & INSN_LOAD_MEMORY_DELAY))
2845           || (! mips_opts.mips16
2846               && mips_opts.isa == ISA_MIPS1
2847               && (prev_insn.insn_mo->pinfo
2848                   & INSN_COPROC_MEMORY_DELAY)))
2849         {
2850           /* Itbl support may require additional care here.  */
2851           ++nops;
2852           if ((! mips_opts.mips16
2853                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2854                && (! cop_interlocks
2855                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2856               || (! hilo_interlocks
2857                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2858                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2859             ++nops;
2860
2861           if (prev_insn_unreordered)
2862             nops = 0;
2863         }
2864       else if ((! mips_opts.mips16
2865                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2866                 && (! cop_interlocks
2867                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2868                || (! hilo_interlocks
2869                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2870                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2871         {
2872           /* Itbl support may require additional care here.  */
2873           if (! prev_prev_insn_unreordered)
2874             ++nops;
2875         }
2876
2877       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2878         {
2879           int min_nops = 0;
2880           const char *pn = prev_insn.insn_mo->name;
2881           if (strncmp(pn, "macc", 4) == 0
2882               || strncmp(pn, "dmacc", 5) == 0
2883               || strncmp(pn, "dmult", 5) == 0)
2884             {
2885               min_nops = 1;
2886             }
2887           if (nops < min_nops)
2888             nops = min_nops;
2889         }
2890
2891       if (nops > 0)
2892         {
2893           struct insn_label_list *l;
2894
2895           if (insns)
2896             {
2897               /* Record the frag which holds the nop instructions, so
2898                  that we can remove them if we don't need them.  */
2899               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2900               prev_nop_frag = frag_now;
2901               prev_nop_frag_holds = nops;
2902               prev_nop_frag_required = 0;
2903               prev_nop_frag_since = 0;
2904             }
2905
2906           for (; nops > 0; --nops)
2907             emit_nop ();
2908
2909           if (insns)
2910             {
2911               /* Move on to a new frag, so that it is safe to simply
2912                  decrease the size of prev_nop_frag.  */
2913               frag_wane (frag_now);
2914               frag_new (0);
2915             }
2916
2917           for (l = insn_labels; l != NULL; l = l->next)
2918             {
2919               valueT val;
2920
2921               assert (S_GET_SEGMENT (l->label) == now_seg);
2922               symbol_set_frag (l->label, frag_now);
2923               val = (valueT) frag_now_fix ();
2924               /* mips16 text labels are stored as odd.  */
2925               if (mips_opts.mips16)
2926                 ++val;
2927               S_SET_VALUE (l->label, val);
2928             }
2929         }
2930     }
2931
2932   /* Mark instruction labels in mips16 mode.  */
2933   if (insns)
2934     mips16_mark_labels ();
2935
2936   mips_no_prev_insn (insns);
2937 }
2938
2939 /* Build an instruction created by a macro expansion.  This is passed
2940    a pointer to the count of instructions created so far, an
2941    expression, the name of the instruction to build, an operand format
2942    string, and corresponding arguments.  */
2943
2944 #ifdef USE_STDARG
2945 static void
2946 macro_build (char *place,
2947              int *counter,
2948              expressionS * ep,
2949              const char *name,
2950              const char *fmt,
2951              ...)
2952 #else
2953 static void
2954 macro_build (place, counter, ep, name, fmt, va_alist)
2955      char *place;
2956      int *counter;
2957      expressionS *ep;
2958      const char *name;
2959      const char *fmt;
2960      va_dcl
2961 #endif
2962 {
2963   struct mips_cl_insn insn;
2964   bfd_reloc_code_real_type r[3];
2965   va_list args;
2966
2967 #ifdef USE_STDARG
2968   va_start (args, fmt);
2969 #else
2970   va_start (args);
2971 #endif
2972
2973   /*
2974    * If the macro is about to expand into a second instruction,
2975    * print a warning if needed. We need to pass ip as a parameter
2976    * to generate a better warning message here...
2977    */
2978   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2979     as_warn (_("Macro instruction expanded into multiple instructions"));
2980
2981   /*
2982    * If the macro is about to expand into a second instruction,
2983    * and it is in a delay slot, print a warning.
2984    */
2985   if (place == NULL
2986       && *counter == 1
2987       && mips_opts.noreorder
2988       && (prev_prev_insn.insn_mo->pinfo
2989           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2990              | INSN_COND_BRANCH_LIKELY)) != 0)
2991     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2992
2993   if (place == NULL)
2994     ++*counter;         /* bump instruction counter */
2995
2996   if (mips_opts.mips16)
2997     {
2998       mips16_macro_build (place, counter, ep, name, fmt, args);
2999       va_end (args);
3000       return;
3001     }
3002
3003   r[0] = BFD_RELOC_UNUSED;
3004   r[1] = BFD_RELOC_UNUSED;
3005   r[2] = BFD_RELOC_UNUSED;
3006   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3007   assert (insn.insn_mo);
3008   assert (strcmp (name, insn.insn_mo->name) == 0);
3009
3010   /* Search until we get a match for NAME.  */
3011   while (1)
3012     {
3013       /* It is assumed here that macros will never generate
3014          MDMX or MIPS-3D instructions.  */
3015       if (strcmp (fmt, insn.insn_mo->args) == 0
3016           && insn.insn_mo->pinfo != INSN_MACRO
3017           && OPCODE_IS_MEMBER (insn.insn_mo,
3018                                (mips_opts.isa
3019                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
3020                                mips_arch)
3021           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
3022         break;
3023
3024       ++insn.insn_mo;
3025       assert (insn.insn_mo->name);
3026       assert (strcmp (name, insn.insn_mo->name) == 0);
3027     }
3028
3029   insn.insn_opcode = insn.insn_mo->match;
3030   for (;;)
3031     {
3032       switch (*fmt++)
3033         {
3034         case '\0':
3035           break;
3036
3037         case ',':
3038         case '(':
3039         case ')':
3040           continue;
3041
3042         case 't':
3043         case 'w':
3044         case 'E':
3045           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3046           continue;
3047
3048         case 'c':
3049           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3050           continue;
3051
3052         case 'T':
3053         case 'W':
3054           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3055           continue;
3056
3057         case 'd':
3058         case 'G':
3059         case 'K':
3060           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3061           continue;
3062
3063         case 'U':
3064           {
3065             int tmp = va_arg (args, int);
3066
3067             insn.insn_opcode |= tmp << OP_SH_RT;
3068             insn.insn_opcode |= tmp << OP_SH_RD;
3069             continue;
3070           }
3071
3072         case 'V':
3073         case 'S':
3074           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3075           continue;
3076
3077         case 'z':
3078           continue;
3079
3080         case '<':
3081           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3082           continue;
3083
3084         case 'D':
3085           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3086           continue;
3087
3088         case 'B':
3089           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3090           continue;
3091
3092         case 'J':
3093           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3094           continue;
3095
3096         case 'q':
3097           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3098           continue;
3099
3100         case 'b':
3101         case 's':
3102         case 'r':
3103         case 'v':
3104           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3105           continue;
3106
3107         case 'i':
3108         case 'j':
3109         case 'o':
3110           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3111           assert (*r == BFD_RELOC_GPREL16
3112                   || *r == BFD_RELOC_MIPS_LITERAL
3113                   || *r == BFD_RELOC_MIPS_HIGHER
3114                   || *r == BFD_RELOC_HI16_S
3115                   || *r == BFD_RELOC_LO16
3116                   || *r == BFD_RELOC_MIPS_GOT16
3117                   || *r == BFD_RELOC_MIPS_CALL16
3118                   || *r == BFD_RELOC_MIPS_GOT_DISP
3119                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3120                   || *r == BFD_RELOC_MIPS_GOT_OFST
3121                   || *r == BFD_RELOC_MIPS_GOT_LO16
3122                   || *r == BFD_RELOC_MIPS_CALL_LO16
3123                   || (ep->X_op == O_subtract
3124                       && *r == BFD_RELOC_PCREL_LO16));
3125           continue;
3126
3127         case 'u':
3128           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3129           assert (ep != NULL
3130                   && (ep->X_op == O_constant
3131                       || (ep->X_op == O_symbol
3132                           && (*r == BFD_RELOC_MIPS_HIGHEST
3133                               || *r == BFD_RELOC_HI16_S
3134                               || *r == BFD_RELOC_HI16
3135                               || *r == BFD_RELOC_GPREL16
3136                               || *r == BFD_RELOC_MIPS_GOT_HI16
3137                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3138                       || (ep->X_op == O_subtract
3139                           && *r == BFD_RELOC_PCREL_HI16_S)));
3140           continue;
3141
3142         case 'p':
3143           assert (ep != NULL);
3144           /*
3145            * This allows macro() to pass an immediate expression for
3146            * creating short branches without creating a symbol.
3147            * Note that the expression still might come from the assembly
3148            * input, in which case the value is not checked for range nor
3149            * is a relocation entry generated (yuck).
3150            */
3151           if (ep->X_op == O_constant)
3152             {
3153               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3154               ep = NULL;
3155             }
3156           else
3157             *r = BFD_RELOC_16_PCREL_S2;
3158           continue;
3159
3160         case 'a':
3161           assert (ep != NULL);
3162           *r = BFD_RELOC_MIPS_JMP;
3163           continue;
3164
3165         case 'C':
3166           insn.insn_opcode |= va_arg (args, unsigned long);
3167           continue;
3168
3169         default:
3170           internalError ();
3171         }
3172       break;
3173     }
3174   va_end (args);
3175   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3176
3177   append_insn (place, &insn, ep, r);
3178 }
3179
3180 static void
3181 mips16_macro_build (place, counter, ep, name, fmt, args)
3182      char *place;
3183      int *counter ATTRIBUTE_UNUSED;
3184      expressionS *ep;
3185      const char *name;
3186      const char *fmt;
3187      va_list args;
3188 {
3189   struct mips_cl_insn insn;
3190   bfd_reloc_code_real_type r[3]
3191     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3192
3193   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3194   assert (insn.insn_mo);
3195   assert (strcmp (name, insn.insn_mo->name) == 0);
3196
3197   while (strcmp (fmt, insn.insn_mo->args) != 0
3198          || insn.insn_mo->pinfo == INSN_MACRO)
3199     {
3200       ++insn.insn_mo;
3201       assert (insn.insn_mo->name);
3202       assert (strcmp (name, insn.insn_mo->name) == 0);
3203     }
3204
3205   insn.insn_opcode = insn.insn_mo->match;
3206   insn.use_extend = FALSE;
3207
3208   for (;;)
3209     {
3210       int c;
3211
3212       c = *fmt++;
3213       switch (c)
3214         {
3215         case '\0':
3216           break;
3217
3218         case ',':
3219         case '(':
3220         case ')':
3221           continue;
3222
3223         case 'y':
3224         case 'w':
3225           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3226           continue;
3227
3228         case 'x':
3229         case 'v':
3230           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3231           continue;
3232
3233         case 'z':
3234           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3235           continue;
3236
3237         case 'Z':
3238           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3239           continue;
3240
3241         case '0':
3242         case 'S':
3243         case 'P':
3244         case 'R':
3245           continue;
3246
3247         case 'X':
3248           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3249           continue;
3250
3251         case 'Y':
3252           {
3253             int regno;
3254
3255             regno = va_arg (args, int);
3256             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3257             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3258           }
3259           continue;
3260
3261         case '<':
3262         case '>':
3263         case '4':
3264         case '5':
3265         case 'H':
3266         case 'W':
3267         case 'D':
3268         case 'j':
3269         case '8':
3270         case 'V':
3271         case 'C':
3272         case 'U':
3273         case 'k':
3274         case 'K':
3275         case 'p':
3276         case 'q':
3277           {
3278             assert (ep != NULL);
3279
3280             if (ep->X_op != O_constant)
3281               *r = (int) BFD_RELOC_UNUSED + c;
3282             else
3283               {
3284                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3285                               FALSE, &insn.insn_opcode, &insn.use_extend,
3286                               &insn.extend);
3287                 ep = NULL;
3288                 *r = BFD_RELOC_UNUSED;
3289               }
3290           }
3291           continue;
3292
3293         case '6':
3294           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3295           continue;
3296         }
3297
3298       break;
3299     }
3300
3301   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3302
3303   append_insn (place, &insn, ep, r);
3304 }
3305
3306 /*
3307  * Generate a "jalr" instruction with a relocation hint to the called
3308  * function.  This occurs in NewABI PIC code.
3309  */
3310 static void
3311 macro_build_jalr (icnt, ep)
3312      int icnt;
3313      expressionS *ep;
3314 {
3315   char *f;
3316
3317   if (HAVE_NEWABI)
3318     {
3319       frag_grow (4);
3320       f = frag_more (0);
3321     }
3322   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3323                RA, PIC_CALL_REG);
3324   if (HAVE_NEWABI)
3325     fix_new_exp (frag_now, f - frag_now->fr_literal,
3326                  4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3327 }
3328
3329 /*
3330  * Generate a "lui" instruction.
3331  */
3332 static void
3333 macro_build_lui (place, counter, ep, regnum)
3334      char *place;
3335      int *counter;
3336      expressionS *ep;
3337      int regnum;
3338 {
3339   expressionS high_expr;
3340   struct mips_cl_insn insn;
3341   bfd_reloc_code_real_type r[3]
3342     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3343   const char *name = "lui";
3344   const char *fmt = "t,u";
3345
3346   assert (! mips_opts.mips16);
3347
3348   if (place == NULL)
3349     high_expr = *ep;
3350   else
3351     {
3352       high_expr.X_op = O_constant;
3353       high_expr.X_add_number = ep->X_add_number;
3354     }
3355
3356   if (high_expr.X_op == O_constant)
3357     {
3358       /* we can compute the instruction now without a relocation entry */
3359       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3360                                 >> 16) & 0xffff;
3361       *r = BFD_RELOC_UNUSED;
3362     }
3363   else
3364     {
3365       assert (ep->X_op == O_symbol);
3366       /* _gp_disp is a special case, used from s_cpload.  */
3367       assert (mips_pic == NO_PIC
3368               || (! HAVE_NEWABI
3369                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3370       *r = BFD_RELOC_HI16_S;
3371     }
3372
3373   /*
3374    * If the macro is about to expand into a second instruction,
3375    * print a warning if needed. We need to pass ip as a parameter
3376    * to generate a better warning message here...
3377    */
3378   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3379     as_warn (_("Macro instruction expanded into multiple instructions"));
3380
3381   if (place == NULL)
3382     ++*counter;         /* bump instruction counter */
3383
3384   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3385   assert (insn.insn_mo);
3386   assert (strcmp (name, insn.insn_mo->name) == 0);
3387   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3388
3389   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3390   if (*r == BFD_RELOC_UNUSED)
3391     {
3392       insn.insn_opcode |= high_expr.X_add_number;
3393       append_insn (place, &insn, NULL, r);
3394     }
3395   else
3396     append_insn (place, &insn, &high_expr, r);
3397 }
3398
3399 /* Generate a sequence of instructions to do a load or store from a constant
3400    offset off of a base register (breg) into/from a target register (treg),
3401    using AT if necessary.  */
3402 static void
3403 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3404      char *place;
3405      int *counter;
3406      expressionS *ep;
3407      const char *op;
3408      int treg, breg;
3409 {
3410   assert (ep->X_op == O_constant);
3411
3412   /* Right now, this routine can only handle signed 32-bit contants.  */
3413   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3414     as_warn (_("operand overflow"));
3415
3416   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3417     {
3418       /* Signed 16-bit offset will fit in the op.  Easy!  */
3419       macro_build (place, counter, ep, op, "t,o(b)", treg,
3420                    (int) BFD_RELOC_LO16, breg);
3421     }
3422   else
3423     {
3424       /* 32-bit offset, need multiple instructions and AT, like:
3425            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3426            addu     $tempreg,$tempreg,$breg
3427            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3428          to handle the complete offset.  */
3429       macro_build_lui (place, counter, ep, AT);
3430       if (place != NULL)
3431         place += 4;
3432       macro_build (place, counter, (expressionS *) NULL,
3433                    HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3434                    ? "add" : "addu" : "daddu",
3435                    "d,v,t", AT, AT, breg);
3436       if (place != NULL)
3437         place += 4;
3438       macro_build (place, counter, ep, op, "t,o(b)", treg,
3439                    (int) BFD_RELOC_LO16, AT);
3440
3441       if (mips_opts.noat)
3442         as_warn (_("Macro used $at after \".set noat\""));
3443     }
3444 }
3445
3446 /*                      set_at()
3447  * Generates code to set the $at register to true (one)
3448  * if reg is less than the immediate expression.
3449  */
3450 static void
3451 set_at (counter, reg, unsignedp)
3452      int *counter;
3453      int reg;
3454      int unsignedp;
3455 {
3456   if (imm_expr.X_op == O_constant
3457       && imm_expr.X_add_number >= -0x8000
3458       && imm_expr.X_add_number < 0x8000)
3459     macro_build ((char *) NULL, counter, &imm_expr,
3460                  unsignedp ? "sltiu" : "slti",
3461                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3462   else
3463     {
3464       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3465       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3466                    unsignedp ? "sltu" : "slt",
3467                    "d,v,t", AT, reg, AT);
3468     }
3469 }
3470
3471 /* Warn if an expression is not a constant.  */
3472
3473 static void
3474 check_absolute_expr (ip, ex)
3475      struct mips_cl_insn *ip;
3476      expressionS *ex;
3477 {
3478   if (ex->X_op == O_big)
3479     as_bad (_("unsupported large constant"));
3480   else if (ex->X_op != O_constant)
3481     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3482 }
3483
3484 /* Count the leading zeroes by performing a binary chop. This is a
3485    bulky bit of source, but performance is a LOT better for the
3486    majority of values than a simple loop to count the bits:
3487        for (lcnt = 0; (lcnt < 32); lcnt++)
3488          if ((v) & (1 << (31 - lcnt)))
3489            break;
3490   However it is not code size friendly, and the gain will drop a bit
3491   on certain cached systems.
3492 */
3493 #define COUNT_TOP_ZEROES(v)             \
3494   (((v) & ~0xffff) == 0                 \
3495    ? ((v) & ~0xff) == 0                 \
3496      ? ((v) & ~0xf) == 0                \
3497        ? ((v) & ~0x3) == 0              \
3498          ? ((v) & ~0x1) == 0            \
3499            ? !(v)                       \
3500              ? 32                       \
3501              : 31                       \
3502            : 30                         \
3503          : ((v) & ~0x7) == 0            \
3504            ? 29                         \
3505            : 28                         \
3506        : ((v) & ~0x3f) == 0             \
3507          ? ((v) & ~0x1f) == 0           \
3508            ? 27                         \
3509            : 26                         \
3510          : ((v) & ~0x7f) == 0           \
3511            ? 25                         \
3512            : 24                         \
3513      : ((v) & ~0xfff) == 0              \
3514        ? ((v) & ~0x3ff) == 0            \
3515          ? ((v) & ~0x1ff) == 0          \
3516            ? 23                         \
3517            : 22                         \
3518          : ((v) & ~0x7ff) == 0          \
3519            ? 21                         \
3520            : 20                         \
3521        : ((v) & ~0x3fff) == 0           \
3522          ? ((v) & ~0x1fff) == 0         \
3523            ? 19                         \
3524            : 18                         \
3525          : ((v) & ~0x7fff) == 0         \
3526            ? 17                         \
3527            : 16                         \
3528    : ((v) & ~0xffffff) == 0             \
3529      ? ((v) & ~0xfffff) == 0            \
3530        ? ((v) & ~0x3ffff) == 0          \
3531          ? ((v) & ~0x1ffff) == 0        \
3532            ? 15                         \
3533            : 14                         \
3534          : ((v) & ~0x7ffff) == 0        \
3535            ? 13                         \
3536            : 12                         \
3537        : ((v) & ~0x3fffff) == 0         \
3538          ? ((v) & ~0x1fffff) == 0       \
3539            ? 11                         \
3540            : 10                         \
3541          : ((v) & ~0x7fffff) == 0       \
3542            ? 9                          \
3543            : 8                          \
3544      : ((v) & ~0xfffffff) == 0          \
3545        ? ((v) & ~0x3ffffff) == 0        \
3546          ? ((v) & ~0x1ffffff) == 0      \
3547            ? 7                          \
3548            : 6                          \
3549          : ((v) & ~0x7ffffff) == 0      \
3550            ? 5                          \
3551            : 4                          \
3552        : ((v) & ~0x3fffffff) == 0       \
3553          ? ((v) & ~0x1fffffff) == 0     \
3554            ? 3                          \
3555            : 2                          \
3556          : ((v) & ~0x7fffffff) == 0     \
3557            ? 1                          \
3558            : 0)
3559
3560 /*                      load_register()
3561  *  This routine generates the least number of instructions neccessary to load
3562  *  an absolute expression value into a register.
3563  */
3564 static void
3565 load_register (counter, reg, ep, dbl)
3566      int *counter;
3567      int reg;
3568      expressionS *ep;
3569      int dbl;
3570 {
3571   int freg;
3572   expressionS hi32, lo32;
3573
3574   if (ep->X_op != O_big)
3575     {
3576       assert (ep->X_op == O_constant);
3577       if (ep->X_add_number < 0x8000
3578           && (ep->X_add_number >= 0
3579               || (ep->X_add_number >= -0x8000
3580                   && (! dbl
3581                       || ! ep->X_unsigned
3582                       || sizeof (ep->X_add_number) > 4))))
3583         {
3584           /* We can handle 16 bit signed values with an addiu to
3585              $zero.  No need to ever use daddiu here, since $zero and
3586              the result are always correct in 32 bit mode.  */
3587           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3588                        (int) BFD_RELOC_LO16);
3589           return;
3590         }
3591       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3592         {
3593           /* We can handle 16 bit unsigned values with an ori to
3594              $zero.  */
3595           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3596                        (int) BFD_RELOC_LO16);
3597           return;
3598         }
3599       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3600                 && (! dbl
3601                     || ! ep->X_unsigned
3602                     || sizeof (ep->X_add_number) > 4
3603                     || (ep->X_add_number & 0x80000000) == 0))
3604                || ((HAVE_32BIT_GPRS || ! dbl)
3605                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3606                || (HAVE_32BIT_GPRS
3607                    && ! dbl
3608                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3609                        == ~ (offsetT) 0xffffffff)))
3610         {
3611           /* 32 bit values require an lui.  */
3612           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3613                        (int) BFD_RELOC_HI16);
3614           if ((ep->X_add_number & 0xffff) != 0)
3615             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3616                          (int) BFD_RELOC_LO16);
3617           return;
3618         }
3619     }
3620
3621   /* The value is larger than 32 bits.  */
3622
3623   if (HAVE_32BIT_GPRS)
3624     {
3625       as_bad (_("Number (0x%lx) larger than 32 bits"),
3626               (unsigned long) ep->X_add_number);
3627       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3628                    (int) BFD_RELOC_LO16);
3629       return;
3630     }
3631
3632   if (ep->X_op != O_big)
3633     {
3634       hi32 = *ep;
3635       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3636       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3637       hi32.X_add_number &= 0xffffffff;
3638       lo32 = *ep;
3639       lo32.X_add_number &= 0xffffffff;
3640     }
3641   else
3642     {
3643       assert (ep->X_add_number > 2);
3644       if (ep->X_add_number == 3)
3645         generic_bignum[3] = 0;
3646       else if (ep->X_add_number > 4)
3647         as_bad (_("Number larger than 64 bits"));
3648       lo32.X_op = O_constant;
3649       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3650       hi32.X_op = O_constant;
3651       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3652     }
3653
3654   if (hi32.X_add_number == 0)
3655     freg = 0;
3656   else
3657     {
3658       int shift, bit;
3659       unsigned long hi, lo;
3660
3661       if (hi32.X_add_number == (offsetT) 0xffffffff)
3662         {
3663           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3664             {
3665               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3666                            reg, 0, (int) BFD_RELOC_LO16);
3667               return;
3668             }
3669           if (lo32.X_add_number & 0x80000000)
3670             {
3671               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3672                            (int) BFD_RELOC_HI16);
3673               if (lo32.X_add_number & 0xffff)
3674                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3675                              reg, reg, (int) BFD_RELOC_LO16);
3676               return;
3677             }
3678         }
3679
3680       /* Check for 16bit shifted constant.  We know that hi32 is
3681          non-zero, so start the mask on the first bit of the hi32
3682          value.  */
3683       shift = 17;
3684       do
3685         {
3686           unsigned long himask, lomask;
3687
3688           if (shift < 32)
3689             {
3690               himask = 0xffff >> (32 - shift);
3691               lomask = (0xffff << shift) & 0xffffffff;
3692             }
3693           else
3694             {
3695               himask = 0xffff << (shift - 32);
3696               lomask = 0;
3697             }
3698           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3699               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3700             {
3701               expressionS tmp;
3702
3703               tmp.X_op = O_constant;
3704               if (shift < 32)
3705                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3706                                     | (lo32.X_add_number >> shift));
3707               else
3708                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3709               macro_build ((char *) NULL, counter, &tmp,
3710                            "ori", "t,r,i", reg, 0,
3711                            (int) BFD_RELOC_LO16);
3712               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3713                            (shift >= 32) ? "dsll32" : "dsll",
3714                            "d,w,<", reg, reg,
3715                            (shift >= 32) ? shift - 32 : shift);
3716               return;
3717             }
3718           ++shift;
3719         }
3720       while (shift <= (64 - 16));
3721
3722       /* Find the bit number of the lowest one bit, and store the
3723          shifted value in hi/lo.  */
3724       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3725       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3726       if (lo != 0)
3727         {
3728           bit = 0;
3729           while ((lo & 1) == 0)
3730             {
3731               lo >>= 1;
3732               ++bit;
3733             }
3734           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3735           hi >>= bit;
3736         }
3737       else
3738         {
3739           bit = 32;
3740           while ((hi & 1) == 0)
3741             {
3742               hi >>= 1;
3743               ++bit;
3744             }
3745           lo = hi;
3746           hi = 0;
3747         }
3748
3749       /* Optimize if the shifted value is a (power of 2) - 1.  */
3750       if ((hi == 0 && ((lo + 1) & lo) == 0)
3751           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3752         {
3753           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3754           if (shift != 0)
3755             {
3756               expressionS tmp;
3757
3758               /* This instruction will set the register to be all
3759                  ones.  */
3760               tmp.X_op = O_constant;
3761               tmp.X_add_number = (offsetT) -1;
3762               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3763                            reg, 0, (int) BFD_RELOC_LO16);
3764               if (bit != 0)
3765                 {
3766                   bit += shift;
3767                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3768                                (bit >= 32) ? "dsll32" : "dsll",
3769                                "d,w,<", reg, reg,
3770                                (bit >= 32) ? bit - 32 : bit);
3771                 }
3772               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3773                            (shift >= 32) ? "dsrl32" : "dsrl",
3774                            "d,w,<", reg, reg,
3775                            (shift >= 32) ? shift - 32 : shift);
3776               return;
3777             }
3778         }
3779
3780       /* Sign extend hi32 before calling load_register, because we can
3781          generally get better code when we load a sign extended value.  */
3782       if ((hi32.X_add_number & 0x80000000) != 0)
3783         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3784       load_register (counter, reg, &hi32, 0);
3785       freg = reg;
3786     }
3787   if ((lo32.X_add_number & 0xffff0000) == 0)
3788     {
3789       if (freg != 0)
3790         {
3791           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3792                        "dsll32", "d,w,<", reg, freg, 0);
3793           freg = reg;
3794         }
3795     }
3796   else
3797     {
3798       expressionS mid16;
3799
3800       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3801         {
3802           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3803                        (int) BFD_RELOC_HI16);
3804           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3805                        "dsrl32", "d,w,<", reg, reg, 0);
3806           return;
3807         }
3808
3809       if (freg != 0)
3810         {
3811           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3812                        "d,w,<", reg, freg, 16);
3813           freg = reg;
3814         }
3815       mid16 = lo32;
3816       mid16.X_add_number >>= 16;
3817       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3818                    freg, (int) BFD_RELOC_LO16);
3819       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3820                    "d,w,<", reg, reg, 16);
3821       freg = reg;
3822     }
3823   if ((lo32.X_add_number & 0xffff) != 0)
3824     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3825                  (int) BFD_RELOC_LO16);
3826 }
3827
3828 /* Load an address into a register.  */
3829
3830 static void
3831 load_address (counter, reg, ep, used_at)
3832      int *counter;
3833      int reg;
3834      expressionS *ep;
3835      int *used_at;
3836 {
3837   char *p = NULL;
3838
3839   if (ep->X_op != O_constant
3840       && ep->X_op != O_symbol)
3841     {
3842       as_bad (_("expression too complex"));
3843       ep->X_op = O_constant;
3844     }
3845
3846   if (ep->X_op == O_constant)
3847     {
3848       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3849       return;
3850     }
3851
3852   if (mips_pic == NO_PIC)
3853     {
3854       /* If this is a reference to a GP relative symbol, we want
3855            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3856          Otherwise we want
3857            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3858            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3859          If we have an addend, we always use the latter form.
3860
3861          With 64bit address space and a usable $at we want
3862            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3863            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3864            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3865            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3866            dsll32       $reg,0
3867            daddu        $reg,$reg,$at
3868
3869          If $at is already in use, we use a path which is suboptimal
3870          on superscalar processors.
3871            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3872            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3873            dsll         $reg,16
3874            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3875            dsll         $reg,16
3876            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3877        */
3878       if (HAVE_64BIT_ADDRESSES)
3879         {
3880           /* We don't do GP optimization for now because RELAX_ENCODE can't
3881              hold the data for such large chunks.  */
3882
3883           if (*used_at == 0 && ! mips_opts.noat)
3884             {
3885               macro_build (p, counter, ep, "lui", "t,u",
3886                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3887               macro_build (p, counter, ep, "lui", "t,u",
3888                            AT, (int) BFD_RELOC_HI16_S);
3889               macro_build (p, counter, ep, "daddiu", "t,r,j",
3890                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3891               macro_build (p, counter, ep, "daddiu", "t,r,j",
3892                            AT, AT, (int) BFD_RELOC_LO16);
3893               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3894                            "d,w,<", reg, reg, 0);
3895               macro_build (p, counter, (expressionS *) NULL, "daddu",
3896                            "d,v,t", reg, reg, AT);
3897               *used_at = 1;
3898             }
3899           else
3900             {
3901               macro_build (p, counter, ep, "lui", "t,u",
3902                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3903               macro_build (p, counter, ep, "daddiu", "t,r,j",
3904                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3905               macro_build (p, counter, (expressionS *) NULL, "dsll",
3906                            "d,w,<", reg, reg, 16);
3907               macro_build (p, counter, ep, "daddiu", "t,r,j",
3908                            reg, reg, (int) BFD_RELOC_HI16_S);
3909               macro_build (p, counter, (expressionS *) NULL, "dsll",
3910                            "d,w,<", reg, reg, 16);
3911               macro_build (p, counter, ep, "daddiu", "t,r,j",
3912                            reg, reg, (int) BFD_RELOC_LO16);
3913             }
3914         }
3915       else
3916         {
3917           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3918               && ! nopic_need_relax (ep->X_add_symbol, 1))
3919             {
3920               frag_grow (20);
3921               macro_build ((char *) NULL, counter, ep,
3922                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3923                            ? "addi" : "addiu" : "daddiu", "t,r,j",
3924                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3925               p = frag_var (rs_machine_dependent, 8, 0,
3926                             RELAX_ENCODE (4, 8, 0, 4, 0,
3927                                           mips_opts.warn_about_macros),
3928                             ep->X_add_symbol, 0, NULL);
3929             }
3930           macro_build_lui (p, counter, ep, reg);
3931           if (p != NULL)
3932             p += 4;
3933           macro_build (p, counter, ep,
3934                        HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3935                        ? "addi" : "addiu" : "daddiu",
3936                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3937         }
3938     }
3939   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3940     {
3941       expressionS ex;
3942
3943       /* If this is a reference to an external symbol, we want
3944            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3945          Otherwise we want
3946            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3947            nop
3948            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3949          If there is a constant, it must be added in after.
3950
3951          If we have NewABI, we want
3952            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
3953          unless we're referencing a global symbol with a non-zero
3954          offset, in which case cst must be added separately.  */
3955       if (HAVE_NEWABI)
3956         {
3957           frag_grow (12);
3958
3959           if (ep->X_add_number)
3960             {
3961               frag_now->tc_frag_data.tc_fr_offset =
3962                 ex.X_add_number = ep->X_add_number;
3963               ep->X_add_number = 0;
3964               macro_build ((char *) NULL, counter, ep,
3965                            HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3966                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3967               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3968                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3969               ex.X_op = O_constant;
3970               macro_build ((char *) NULL, counter, &ex,
3971                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
3972                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3973               p = frag_var (rs_machine_dependent, 8, 0,
3974                             RELAX_ENCODE (8, 4, 0, 0, 0,
3975                                           mips_opts.warn_about_macros),
3976                             ep->X_add_symbol, 0, (char *) NULL);
3977               ep->X_add_number = ex.X_add_number;
3978             }
3979
3980           macro_build (p, counter, ep,
3981                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3982                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3983
3984           if (! p)
3985             {
3986               /* To avoid confusion in tc_gen_reloc, we must ensure
3987                  that this does not become a variant frag.  */
3988               frag_wane (frag_now);
3989               frag_new (0);
3990             }
3991         }
3992       else
3993         {
3994           ex.X_add_number = ep->X_add_number;
3995           ep->X_add_number = 0;
3996           frag_grow (20);
3997           macro_build ((char *) NULL, counter, ep,
3998                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3999                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4000           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
4001           p = frag_var (rs_machine_dependent, 4, 0,
4002                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
4003                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
4004           macro_build (p, counter, ep,
4005                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4006                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4007
4008           if (ex.X_add_number != 0)
4009             {
4010               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4011                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4012               ex.X_op = O_constant;
4013               macro_build ((char *) NULL, counter, &ex,
4014                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4015                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4016             }
4017         }
4018     }
4019   else if (mips_pic == SVR4_PIC)
4020     {
4021       expressionS ex;
4022       int off;
4023
4024       /* This is the large GOT case.  If this is a reference to an
4025          external symbol, we want
4026            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
4027            addu         $reg,$reg,$gp
4028            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
4029
4030          Otherwise, for a reference to a local symbol in old ABI, we want
4031            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
4032            nop
4033            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
4034          If there is a constant, it must be added in after.
4035
4036          In the NewABI, for local symbols, with or without offsets, we want:
4037            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
4038            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
4039       */
4040       if (HAVE_NEWABI)
4041         {
4042           frag_grow (24);
4043
4044           frag_now->tc_frag_data.tc_fr_offset =
4045             ex.X_add_number = ep->X_add_number;
4046           ep->X_add_number = 0;
4047           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4048                        (int) BFD_RELOC_MIPS_GOT_HI16);
4049           macro_build ((char *) NULL, counter, (expressionS *) NULL,
4050                        HAVE_32BIT_ADDRESSES ? "add" : "daddu", "d,v,t", reg,
4051                        reg, mips_gp_register);
4052           macro_build ((char *) NULL, counter, ep,
4053                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4054                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4055           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4056             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4057           else if (ex.X_add_number)
4058             {
4059               ex.X_op = O_constant;
4060               macro_build ((char *) NULL, counter, &ex,
4061                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
4062                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4063             }
4064
4065           ep->X_add_number = ex.X_add_number;
4066           p = frag_var (rs_machine_dependent, 8, 0,
4067                         RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
4068                                       mips_opts.warn_about_macros),
4069                         ep->X_add_symbol, 0, (char *) NULL);
4070           macro_build (p, counter, ep,
4071                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4072                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4073           macro_build (p + 4, counter, ep,
4074                        HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
4075                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
4076         }
4077       else
4078         {
4079           ex.X_add_number = ep->X_add_number;
4080           ep->X_add_number = 0;
4081           if (reg_needs_delay (mips_gp_register))
4082             off = 4;
4083           else
4084             off = 0;
4085           frag_grow (32);
4086           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4087                        (int) BFD_RELOC_MIPS_GOT_HI16);
4088           macro_build ((char *) NULL, counter, (expressionS *) NULL,
4089                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
4090                        reg, mips_gp_register);
4091           macro_build ((char *) NULL, counter, ep,
4092                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4093                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4094           p = frag_var (rs_machine_dependent, 12 + off, 0,
4095                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
4096                                       mips_opts.warn_about_macros),
4097                         ep->X_add_symbol, 0, NULL);
4098           if (off > 0)
4099             {
4100               /* We need a nop before loading from $gp.  This special
4101                  check is required because the lui which starts the main
4102                  instruction stream does not refer to $gp, and so will not
4103                  insert the nop which may be required.  */
4104               macro_build (p, counter, (expressionS *) NULL, "nop", "");
4105                 p += 4;
4106             }
4107           macro_build (p, counter, ep,
4108                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4109                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4110           p += 4;
4111           macro_build (p, counter, (expressionS *) NULL, "nop", "");
4112           p += 4;
4113           macro_build (p, counter, ep,
4114                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4115                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4116
4117           if (ex.X_add_number != 0)
4118             {
4119               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4120                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4121               ex.X_op = O_constant;
4122               macro_build ((char *) NULL, counter, &ex,
4123                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4124                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4125             }
4126         }
4127     }
4128   else if (mips_pic == EMBEDDED_PIC)
4129     {
4130       /* We always do
4131            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
4132        */
4133       macro_build ((char *) NULL, counter, ep,
4134                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4135                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4136     }
4137   else
4138     abort ();
4139 }
4140
4141 /* Move the contents of register SOURCE into register DEST.  */
4142
4143 static void
4144 move_register (counter, dest, source)
4145      int *counter;
4146      int dest;
4147      int source;
4148 {
4149   macro_build ((char *) NULL, counter, (expressionS *) NULL,
4150                HAVE_32BIT_GPRS ? "addu" : "daddu",
4151                "d,v,t", dest, source, 0);
4152 }
4153
4154 /*
4155  *                      Build macros
4156  *   This routine implements the seemingly endless macro or synthesized
4157  * instructions and addressing modes in the mips assembly language. Many
4158  * of these macros are simple and are similar to each other. These could
4159  * probably be handled by some kind of table or grammer aproach instead of
4160  * this verbose method. Others are not simple macros but are more like
4161  * optimizing code generation.
4162  *   One interesting optimization is when several store macros appear
4163  * consecutivly that would load AT with the upper half of the same address.
4164  * The ensuing load upper instructions are ommited. This implies some kind
4165  * of global optimization. We currently only optimize within a single macro.
4166  *   For many of the load and store macros if the address is specified as a
4167  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4168  * first load register 'at' with zero and use it as the base register. The
4169  * mips assembler simply uses register $zero. Just one tiny optimization
4170  * we're missing.
4171  */
4172 static void
4173 macro (ip)
4174      struct mips_cl_insn *ip;
4175 {
4176   register int treg, sreg, dreg, breg;
4177   int tempreg;
4178   int mask;
4179   int icnt = 0;
4180   int used_at = 0;
4181   expressionS expr1;
4182   const char *s;
4183   const char *s2;
4184   const char *fmt;
4185   int likely = 0;
4186   int dbl = 0;
4187   int coproc = 0;
4188   int lr = 0;
4189   int imm = 0;
4190   offsetT maxnum;
4191   int off;
4192   bfd_reloc_code_real_type r;
4193   int hold_mips_optimize;
4194
4195   assert (! mips_opts.mips16);
4196
4197   treg = (ip->insn_opcode >> 16) & 0x1f;
4198   dreg = (ip->insn_opcode >> 11) & 0x1f;
4199   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4200   mask = ip->insn_mo->mask;
4201
4202   expr1.X_op = O_constant;
4203   expr1.X_op_symbol = NULL;
4204   expr1.X_add_symbol = NULL;
4205   expr1.X_add_number = 1;
4206
4207   /* Umatched fixups should not be put in the same frag as a relaxable
4208      macro.  For example, suppose we have:
4209
4210         lui $4,%hi(l1)          # 1
4211         la $5,l2                # 2
4212         addiu $4,$4,%lo(l1)     # 3
4213
4214      If instructions 1 and 2 were put in the same frag, md_frob_file would
4215      move the fixup for #1 after the fixups for the "unrelaxed" version of
4216      #2.  This would confuse tc_gen_reloc, which expects the relocations
4217      for #2 to be the last for that frag.
4218
4219      Also, if tc_gen_reloc sees certain relocations in a variant frag,
4220      it assumes that they belong to a relaxable macro.  We mustn't put
4221      other uses of such relocations into a variant frag.
4222
4223      To avoid both problems, finish the current frag it contains a
4224      %reloc() operator.  The macro then goes into a new frag.  */
4225   if (prev_reloc_op_frag == frag_now)
4226     {
4227       frag_wane (frag_now);
4228       frag_new (0);
4229     }
4230
4231   switch (mask)
4232     {
4233     case M_DABS:
4234       dbl = 1;
4235     case M_ABS:
4236       /* bgez $a0,.+12
4237          move v0,$a0
4238          sub v0,$zero,$a0
4239          */
4240
4241       mips_emit_delays (TRUE);
4242       ++mips_opts.noreorder;
4243       mips_any_noreorder = 1;
4244
4245       expr1.X_add_number = 8;
4246       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4247       if (dreg == sreg)
4248         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4249                      0);
4250       else
4251         move_register (&icnt, dreg, sreg);
4252       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4253                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4254
4255       --mips_opts.noreorder;
4256       return;
4257
4258     case M_ADD_I:
4259       s = "addi";
4260       s2 = "add";
4261       goto do_addi;
4262     case M_ADDU_I:
4263       s = "addiu";
4264       s2 = "addu";
4265       goto do_addi;
4266     case M_DADD_I:
4267       dbl = 1;
4268       s = "daddi";
4269       s2 = "dadd";
4270       goto do_addi;
4271     case M_DADDU_I:
4272       dbl = 1;
4273       s = "daddiu";
4274       s2 = "daddu";
4275     do_addi:
4276       if (imm_expr.X_op == O_constant
4277           && imm_expr.X_add_number >= -0x8000
4278           && imm_expr.X_add_number < 0x8000)
4279         {
4280           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4281                        (int) BFD_RELOC_LO16);
4282           return;
4283         }
4284       load_register (&icnt, AT, &imm_expr, dbl);
4285       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4286                    treg, sreg, AT);
4287       break;
4288
4289     case M_AND_I:
4290       s = "andi";
4291       s2 = "and";
4292       goto do_bit;
4293     case M_OR_I:
4294       s = "ori";
4295       s2 = "or";
4296       goto do_bit;
4297     case M_NOR_I:
4298       s = "";
4299       s2 = "nor";
4300       goto do_bit;
4301     case M_XOR_I:
4302       s = "xori";
4303       s2 = "xor";
4304     do_bit:
4305       if (imm_expr.X_op == O_constant
4306           && imm_expr.X_add_number >= 0
4307           && imm_expr.X_add_number < 0x10000)
4308         {
4309           if (mask != M_NOR_I)
4310             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4311                          sreg, (int) BFD_RELOC_LO16);
4312           else
4313             {
4314               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4315                            treg, sreg, (int) BFD_RELOC_LO16);
4316               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4317                            "d,v,t", treg, treg, 0);
4318             }
4319           return;
4320         }
4321
4322       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4323       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4324                    treg, sreg, AT);
4325       break;
4326
4327     case M_BEQ_I:
4328       s = "beq";
4329       goto beq_i;
4330     case M_BEQL_I:
4331       s = "beql";
4332       likely = 1;
4333       goto beq_i;
4334     case M_BNE_I:
4335       s = "bne";
4336       goto beq_i;
4337     case M_BNEL_I:
4338       s = "bnel";
4339       likely = 1;
4340     beq_i:
4341       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4342         {
4343           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4344                        0);
4345           return;
4346         }
4347       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4348       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4349       break;
4350
4351     case M_BGEL:
4352       likely = 1;
4353     case M_BGE:
4354       if (treg == 0)
4355         {
4356           macro_build ((char *) NULL, &icnt, &offset_expr,
4357                        likely ? "bgezl" : "bgez", "s,p", sreg);
4358           return;
4359         }
4360       if (sreg == 0)
4361         {
4362           macro_build ((char *) NULL, &icnt, &offset_expr,
4363                        likely ? "blezl" : "blez", "s,p", treg);
4364           return;
4365         }
4366       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4367                    AT, sreg, treg);
4368       macro_build ((char *) NULL, &icnt, &offset_expr,
4369                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4370       break;
4371
4372     case M_BGTL_I:
4373       likely = 1;
4374     case M_BGT_I:
4375       /* check for > max integer */
4376       maxnum = 0x7fffffff;
4377       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4378         {
4379           maxnum <<= 16;
4380           maxnum |= 0xffff;
4381           maxnum <<= 16;
4382           maxnum |= 0xffff;
4383         }
4384       if (imm_expr.X_op == O_constant
4385           && imm_expr.X_add_number >= maxnum
4386           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4387         {
4388         do_false:
4389           /* result is always false */
4390           if (! likely)
4391             {
4392               if (warn_nops)
4393                 as_warn (_("Branch %s is always false (nop)"),
4394                          ip->insn_mo->name);
4395               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4396                            "", 0);
4397             }
4398           else
4399             {
4400               if (warn_nops)
4401                 as_warn (_("Branch likely %s is always false"),
4402                          ip->insn_mo->name);
4403               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4404                            "s,t,p", 0, 0);
4405             }
4406           return;
4407         }
4408       if (imm_expr.X_op != O_constant)
4409         as_bad (_("Unsupported large constant"));
4410       ++imm_expr.X_add_number;
4411       /* FALLTHROUGH */
4412     case M_BGE_I:
4413     case M_BGEL_I:
4414       if (mask == M_BGEL_I)
4415         likely = 1;
4416       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4417         {
4418           macro_build ((char *) NULL, &icnt, &offset_expr,
4419                        likely ? "bgezl" : "bgez", "s,p", sreg);
4420           return;
4421         }
4422       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4423         {
4424           macro_build ((char *) NULL, &icnt, &offset_expr,
4425                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4426           return;
4427         }
4428       maxnum = 0x7fffffff;
4429       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4430         {
4431           maxnum <<= 16;
4432           maxnum |= 0xffff;
4433           maxnum <<= 16;
4434           maxnum |= 0xffff;
4435         }
4436       maxnum = - maxnum - 1;
4437       if (imm_expr.X_op == O_constant
4438           && imm_expr.X_add_number <= maxnum
4439           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4440         {
4441         do_true:
4442           /* result is always true */
4443           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4444           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4445           return;
4446         }
4447       set_at (&icnt, sreg, 0);
4448       macro_build ((char *) NULL, &icnt, &offset_expr,
4449                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4450       break;
4451
4452     case M_BGEUL:
4453       likely = 1;
4454     case M_BGEU:
4455       if (treg == 0)
4456         goto do_true;
4457       if (sreg == 0)
4458         {
4459           macro_build ((char *) NULL, &icnt, &offset_expr,
4460                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4461           return;
4462         }
4463       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4464                    "d,v,t", AT, sreg, treg);
4465       macro_build ((char *) NULL, &icnt, &offset_expr,
4466                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4467       break;
4468
4469     case M_BGTUL_I:
4470       likely = 1;
4471     case M_BGTU_I:
4472       if (sreg == 0
4473           || (HAVE_32BIT_GPRS
4474               && imm_expr.X_op == O_constant
4475               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4476         goto do_false;
4477       if (imm_expr.X_op != O_constant)
4478         as_bad (_("Unsupported large constant"));
4479       ++imm_expr.X_add_number;
4480       /* FALLTHROUGH */
4481     case M_BGEU_I:
4482     case M_BGEUL_I:
4483       if (mask == M_BGEUL_I)
4484         likely = 1;
4485       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4486         goto do_true;
4487       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4488         {
4489           macro_build ((char *) NULL, &icnt, &offset_expr,
4490                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4491           return;
4492         }
4493       set_at (&icnt, sreg, 1);
4494       macro_build ((char *) NULL, &icnt, &offset_expr,
4495                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4496       break;
4497
4498     case M_BGTL:
4499       likely = 1;
4500     case M_BGT:
4501       if (treg == 0)
4502         {
4503           macro_build ((char *) NULL, &icnt, &offset_expr,
4504                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4505           return;
4506         }
4507       if (sreg == 0)
4508         {
4509           macro_build ((char *) NULL, &icnt, &offset_expr,
4510                        likely ? "bltzl" : "bltz", "s,p", treg);
4511           return;
4512         }
4513       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4514                    AT, treg, sreg);
4515       macro_build ((char *) NULL, &icnt, &offset_expr,
4516                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4517       break;
4518
4519     case M_BGTUL:
4520       likely = 1;
4521     case M_BGTU:
4522       if (treg == 0)
4523         {
4524           macro_build ((char *) NULL, &icnt, &offset_expr,
4525                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4526           return;
4527         }
4528       if (sreg == 0)
4529         goto do_false;
4530       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4531                    "d,v,t", AT, treg, sreg);
4532       macro_build ((char *) NULL, &icnt, &offset_expr,
4533                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4534       break;
4535
4536     case M_BLEL:
4537       likely = 1;
4538     case M_BLE:
4539       if (treg == 0)
4540         {
4541           macro_build ((char *) NULL, &icnt, &offset_expr,
4542                        likely ? "blezl" : "blez", "s,p", sreg);
4543           return;
4544         }
4545       if (sreg == 0)
4546         {
4547           macro_build ((char *) NULL, &icnt, &offset_expr,
4548                        likely ? "bgezl" : "bgez", "s,p", treg);
4549           return;
4550         }
4551       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4552                    AT, treg, sreg);
4553       macro_build ((char *) NULL, &icnt, &offset_expr,
4554                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4555       break;
4556
4557     case M_BLEL_I:
4558       likely = 1;
4559     case M_BLE_I:
4560       maxnum = 0x7fffffff;
4561       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4562         {
4563           maxnum <<= 16;
4564           maxnum |= 0xffff;
4565           maxnum <<= 16;
4566           maxnum |= 0xffff;
4567         }
4568       if (imm_expr.X_op == O_constant
4569           && imm_expr.X_add_number >= maxnum
4570           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4571         goto do_true;
4572       if (imm_expr.X_op != O_constant)
4573         as_bad (_("Unsupported large constant"));
4574       ++imm_expr.X_add_number;
4575       /* FALLTHROUGH */
4576     case M_BLT_I:
4577     case M_BLTL_I:
4578       if (mask == M_BLTL_I)
4579         likely = 1;
4580       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4581         {
4582           macro_build ((char *) NULL, &icnt, &offset_expr,
4583                        likely ? "bltzl" : "bltz", "s,p", sreg);
4584           return;
4585         }
4586       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4587         {
4588           macro_build ((char *) NULL, &icnt, &offset_expr,
4589                        likely ? "blezl" : "blez", "s,p", sreg);
4590           return;
4591         }
4592       set_at (&icnt, sreg, 0);
4593       macro_build ((char *) NULL, &icnt, &offset_expr,
4594                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4595       break;
4596
4597     case M_BLEUL:
4598       likely = 1;
4599     case M_BLEU:
4600       if (treg == 0)
4601         {
4602           macro_build ((char *) NULL, &icnt, &offset_expr,
4603                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4604           return;
4605         }
4606       if (sreg == 0)
4607         goto do_true;
4608       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4609                    "d,v,t", AT, treg, sreg);
4610       macro_build ((char *) NULL, &icnt, &offset_expr,
4611                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4612       break;
4613
4614     case M_BLEUL_I:
4615       likely = 1;
4616     case M_BLEU_I:
4617       if (sreg == 0
4618           || (HAVE_32BIT_GPRS
4619               && imm_expr.X_op == O_constant
4620               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4621         goto do_true;
4622       if (imm_expr.X_op != O_constant)
4623         as_bad (_("Unsupported large constant"));
4624       ++imm_expr.X_add_number;
4625       /* FALLTHROUGH */
4626     case M_BLTU_I:
4627     case M_BLTUL_I:
4628       if (mask == M_BLTUL_I)
4629         likely = 1;
4630       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4631         goto do_false;
4632       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4633         {
4634           macro_build ((char *) NULL, &icnt, &offset_expr,
4635                        likely ? "beql" : "beq",
4636                        "s,t,p", sreg, 0);
4637           return;
4638         }
4639       set_at (&icnt, sreg, 1);
4640       macro_build ((char *) NULL, &icnt, &offset_expr,
4641                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4642       break;
4643
4644     case M_BLTL:
4645       likely = 1;
4646     case M_BLT:
4647       if (treg == 0)
4648         {
4649           macro_build ((char *) NULL, &icnt, &offset_expr,
4650                        likely ? "bltzl" : "bltz", "s,p", sreg);
4651           return;
4652         }
4653       if (sreg == 0)
4654         {
4655           macro_build ((char *) NULL, &icnt, &offset_expr,
4656                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4657           return;
4658         }
4659       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4660                    AT, sreg, treg);
4661       macro_build ((char *) NULL, &icnt, &offset_expr,
4662                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4663       break;
4664
4665     case M_BLTUL:
4666       likely = 1;
4667     case M_BLTU:
4668       if (treg == 0)
4669         goto do_false;
4670       if (sreg == 0)
4671         {
4672           macro_build ((char *) NULL, &icnt, &offset_expr,
4673                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4674           return;
4675         }
4676       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4677                    "d,v,t", AT, sreg,
4678                    treg);
4679       macro_build ((char *) NULL, &icnt, &offset_expr,
4680                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4681       break;
4682
4683     case M_DDIV_3:
4684       dbl = 1;
4685     case M_DIV_3:
4686       s = "mflo";
4687       goto do_div3;
4688     case M_DREM_3:
4689       dbl = 1;
4690     case M_REM_3:
4691       s = "mfhi";
4692     do_div3:
4693       if (treg == 0)
4694         {
4695           as_warn (_("Divide by zero."));
4696           if (mips_trap)
4697             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4698                          "s,t,q", 0, 0, 7);
4699           else
4700             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4701                          "c", 7);
4702           return;
4703         }
4704
4705       mips_emit_delays (TRUE);
4706       ++mips_opts.noreorder;
4707       mips_any_noreorder = 1;
4708       if (mips_trap)
4709         {
4710           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4711                        "s,t,q", treg, 0, 7);
4712           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4713                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4714         }
4715       else
4716         {
4717           expr1.X_add_number = 8;
4718           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4719           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4720                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4721           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4722                        "c", 7);
4723         }
4724       expr1.X_add_number = -1;
4725       macro_build ((char *) NULL, &icnt, &expr1,
4726                    dbl ? "daddiu" : "addiu",
4727                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4728       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4729       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4730       if (dbl)
4731         {
4732           expr1.X_add_number = 1;
4733           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4734                        (int) BFD_RELOC_LO16);
4735           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4736                        "d,w,<", AT, AT, 31);
4737         }
4738       else
4739         {
4740           expr1.X_add_number = 0x80000000;
4741           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4742                        (int) BFD_RELOC_HI16);
4743         }
4744       if (mips_trap)
4745         {
4746           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4747                        "s,t,q", sreg, AT, 6);
4748           /* We want to close the noreorder block as soon as possible, so
4749              that later insns are available for delay slot filling.  */
4750           --mips_opts.noreorder;
4751         }
4752       else
4753         {
4754           expr1.X_add_number = 8;
4755           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4756           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4757                        0);
4758
4759           /* We want to close the noreorder block as soon as possible, so
4760              that later insns are available for delay slot filling.  */
4761           --mips_opts.noreorder;
4762
4763           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4764                        "c", 6);
4765         }
4766       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4767       break;
4768
4769     case M_DIV_3I:
4770       s = "div";
4771       s2 = "mflo";
4772       goto do_divi;
4773     case M_DIVU_3I:
4774       s = "divu";
4775       s2 = "mflo";
4776       goto do_divi;
4777     case M_REM_3I:
4778       s = "div";
4779       s2 = "mfhi";
4780       goto do_divi;
4781     case M_REMU_3I:
4782       s = "divu";
4783       s2 = "mfhi";
4784       goto do_divi;
4785     case M_DDIV_3I:
4786       dbl = 1;
4787       s = "ddiv";
4788       s2 = "mflo";
4789       goto do_divi;
4790     case M_DDIVU_3I:
4791       dbl = 1;
4792       s = "ddivu";
4793       s2 = "mflo";
4794       goto do_divi;
4795     case M_DREM_3I:
4796       dbl = 1;
4797       s = "ddiv";
4798       s2 = "mfhi";
4799       goto do_divi;
4800     case M_DREMU_3I:
4801       dbl = 1;
4802       s = "ddivu";
4803       s2 = "mfhi";
4804     do_divi:
4805       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4806         {
4807           as_warn (_("Divide by zero."));
4808           if (mips_trap)
4809             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4810                          "s,t,q", 0, 0, 7);
4811           else
4812             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4813                          "c", 7);
4814           return;
4815         }
4816       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4817         {
4818           if (strcmp (s2, "mflo") == 0)
4819             move_register (&icnt, dreg, sreg);
4820           else
4821             move_register (&icnt, dreg, 0);
4822           return;
4823         }
4824       if (imm_expr.X_op == O_constant
4825           && imm_expr.X_add_number == -1
4826           && s[strlen (s) - 1] != 'u')
4827         {
4828           if (strcmp (s2, "mflo") == 0)
4829             {
4830               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4831                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4832             }
4833           else
4834             move_register (&icnt, dreg, 0);
4835           return;
4836         }
4837
4838       load_register (&icnt, AT, &imm_expr, dbl);
4839       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4840                    sreg, AT);
4841       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4842       break;
4843
4844     case M_DIVU_3:
4845       s = "divu";
4846       s2 = "mflo";
4847       goto do_divu3;
4848     case M_REMU_3:
4849       s = "divu";
4850       s2 = "mfhi";
4851       goto do_divu3;
4852     case M_DDIVU_3:
4853       s = "ddivu";
4854       s2 = "mflo";
4855       goto do_divu3;
4856     case M_DREMU_3:
4857       s = "ddivu";
4858       s2 = "mfhi";
4859     do_divu3:
4860       mips_emit_delays (TRUE);
4861       ++mips_opts.noreorder;
4862       mips_any_noreorder = 1;
4863       if (mips_trap)
4864         {
4865           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4866                        "s,t,q", treg, 0, 7);
4867           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4868                        sreg, treg);
4869           /* We want to close the noreorder block as soon as possible, so
4870              that later insns are available for delay slot filling.  */
4871           --mips_opts.noreorder;
4872         }
4873       else
4874         {
4875           expr1.X_add_number = 8;
4876           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4877           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4878                        sreg, treg);
4879
4880           /* We want to close the noreorder block as soon as possible, so
4881              that later insns are available for delay slot filling.  */
4882           --mips_opts.noreorder;
4883           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4884                        "c", 7);
4885         }
4886       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4887       return;
4888
4889     case M_DLA_AB:
4890       dbl = 1;
4891     case M_LA_AB:
4892       /* Load the address of a symbol into a register.  If breg is not
4893          zero, we then add a base register to it.  */
4894
4895       if (dbl && HAVE_32BIT_GPRS)
4896         as_warn (_("dla used to load 32-bit register"));
4897
4898       if (! dbl && HAVE_64BIT_OBJECTS)
4899         as_warn (_("la used to load 64-bit address"));
4900
4901       if (offset_expr.X_op == O_constant
4902           && offset_expr.X_add_number >= -0x8000
4903           && offset_expr.X_add_number < 0x8000)
4904         {
4905           macro_build ((char *) NULL, &icnt, &offset_expr,
4906                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" :
4907                        HAVE_NEWABI ? "addi" : "addiu",
4908                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4909           return;
4910         }
4911
4912       if (treg == breg)
4913         {
4914           tempreg = AT;
4915           used_at = 1;
4916         }
4917       else
4918         {
4919           tempreg = treg;
4920           used_at = 0;
4921         }
4922
4923       /* When generating embedded PIC code, we permit expressions of
4924          the form
4925            la   $treg,foo-bar
4926            la   $treg,foo-bar($breg)
4927          where bar is an address in the current section.  These are used
4928          when getting the addresses of functions.  We don't permit
4929          X_add_number to be non-zero, because if the symbol is
4930          external the relaxing code needs to know that any addend is
4931          purely the offset to X_op_symbol.  */
4932       if (mips_pic == EMBEDDED_PIC
4933           && offset_expr.X_op == O_subtract
4934           && (symbol_constant_p (offset_expr.X_op_symbol)
4935               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4936               : (symbol_equated_p (offset_expr.X_op_symbol)
4937                  && (S_GET_SEGMENT
4938                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4939                       ->X_add_symbol)
4940                      == now_seg)))
4941           && (offset_expr.X_add_number == 0
4942               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4943         {
4944           if (breg == 0)
4945             {
4946               tempreg = treg;
4947               used_at = 0;
4948               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4949                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4950             }
4951           else
4952             {
4953               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4954                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4955               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4956                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4957                            "d,v,t", tempreg, tempreg, breg);
4958             }
4959           macro_build ((char *) NULL, &icnt, &offset_expr,
4960                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4961                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4962           if (! used_at)
4963             return;
4964           break;
4965         }
4966
4967       if (offset_expr.X_op != O_symbol
4968           && offset_expr.X_op != O_constant)
4969         {
4970           as_bad (_("expression too complex"));
4971           offset_expr.X_op = O_constant;
4972         }
4973
4974       if (offset_expr.X_op == O_constant)
4975         load_register (&icnt, tempreg, &offset_expr,
4976                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4977                         ? (dbl || HAVE_64BIT_ADDRESSES)
4978                         : HAVE_64BIT_ADDRESSES));
4979       else if (mips_pic == NO_PIC)
4980         {
4981           /* If this is a reference to a GP relative symbol, we want
4982                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4983              Otherwise we want
4984                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4985                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4986              If we have a constant, we need two instructions anyhow,
4987              so we may as well always use the latter form.
4988
4989             With 64bit address space and a usable $at we want
4990               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4991               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4992               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4993               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4994               dsll32    $tempreg,0
4995               daddu     $tempreg,$tempreg,$at
4996
4997             If $at is already in use, we use a path which is suboptimal
4998             on superscalar processors.
4999               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5000               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5001               dsll      $tempreg,16
5002               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
5003               dsll      $tempreg,16
5004               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
5005           */
5006           char *p = NULL;
5007           if (HAVE_64BIT_ADDRESSES)
5008             {
5009               /* We don't do GP optimization for now because RELAX_ENCODE can't
5010                  hold the data for such large chunks.  */
5011
5012               if (used_at == 0 && ! mips_opts.noat)
5013                 {
5014                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5015                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5016                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5017                                AT, (int) BFD_RELOC_HI16_S);
5018                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5019                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5020                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5021                                AT, AT, (int) BFD_RELOC_LO16);
5022                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5023                                "d,w,<", tempreg, tempreg, 0);
5024                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5025                                "d,v,t", tempreg, tempreg, AT);
5026                   used_at = 1;
5027                 }
5028               else
5029                 {
5030                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5031                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5032                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5033                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5034                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
5035                                tempreg, tempreg, 16);
5036                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5037                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5038                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
5039                                tempreg, tempreg, 16);
5040                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5041                                tempreg, tempreg, (int) BFD_RELOC_LO16);
5042                 }
5043             }
5044           else
5045             {
5046               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5047                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
5048                 {
5049                   frag_grow (20);
5050                   macro_build ((char *) NULL, &icnt, &offset_expr,
5051                                HAVE_NEWABI ? "addi" : "addiu",
5052                                "t,r,j", tempreg, mips_gp_register,
5053                                (int) BFD_RELOC_GPREL16);
5054                   p = frag_var (rs_machine_dependent, 8, 0,
5055                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5056                                               mips_opts.warn_about_macros),
5057                                 offset_expr.X_add_symbol, 0, NULL);
5058                 }
5059               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5060               if (p != NULL)
5061                 p += 4;
5062               macro_build (p, &icnt, &offset_expr,
5063                            HAVE_NEWABI ? "addi" : "addiu",
5064                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5065             }
5066         }
5067       else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5068         {
5069           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5070
5071           /* If this is a reference to an external symbol, and there
5072              is no constant, we want
5073                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5074              or if tempreg is PIC_CALL_REG
5075                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5076              For a local symbol, we want
5077                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5078                nop
5079                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5080
5081              If we have a small constant, and this is a reference to
5082              an external symbol, we want
5083                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5084                nop
5085                addiu    $tempreg,$tempreg,<constant>
5086              For a local symbol, we want the same instruction
5087              sequence, but we output a BFD_RELOC_LO16 reloc on the
5088              addiu instruction.
5089
5090              If we have a large constant, and this is a reference to
5091              an external symbol, we want
5092                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5093                lui      $at,<hiconstant>
5094                addiu    $at,$at,<loconstant>
5095                addu     $tempreg,$tempreg,$at
5096              For a local symbol, we want the same instruction
5097              sequence, but we output a BFD_RELOC_LO16 reloc on the
5098              addiu instruction.
5099            */
5100
5101           expr1.X_add_number = offset_expr.X_add_number;
5102           offset_expr.X_add_number = 0;
5103           frag_grow (32);
5104           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5105             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5106           macro_build ((char *) NULL, &icnt, &offset_expr,
5107                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5108                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
5109           if (expr1.X_add_number == 0)
5110             {
5111               int off;
5112               char *p;
5113
5114               if (breg == 0)
5115                 off = 0;
5116               else
5117                 {
5118                   /* We're going to put in an addu instruction using
5119                      tempreg, so we may as well insert the nop right
5120                      now.  */
5121                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5122                                "nop", "");
5123                   off = 4;
5124                 }
5125               p = frag_var (rs_machine_dependent, 8 - off, 0,
5126                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5127                                           (breg == 0
5128                                            ? mips_opts.warn_about_macros
5129                                            : 0)),
5130                             offset_expr.X_add_symbol, 0, NULL);
5131               if (breg == 0)
5132                 {
5133                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5134                   p += 4;
5135                 }
5136               macro_build (p, &icnt, &expr1,
5137                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5138                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5139               /* FIXME: If breg == 0, and the next instruction uses
5140                  $tempreg, then if this variant case is used an extra
5141                  nop will be generated.  */
5142             }
5143           else if (expr1.X_add_number >= -0x8000
5144                    && expr1.X_add_number < 0x8000)
5145             {
5146               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5147                            "nop", "");
5148               macro_build ((char *) NULL, &icnt, &expr1,
5149                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5150                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5151               frag_var (rs_machine_dependent, 0, 0,
5152                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5153                         offset_expr.X_add_symbol, 0, NULL);
5154             }
5155           else
5156             {
5157               int off1;
5158
5159               /* If we are going to add in a base register, and the
5160                  target register and the base register are the same,
5161                  then we are using AT as a temporary register.  Since
5162                  we want to load the constant into AT, we add our
5163                  current AT (from the global offset table) and the
5164                  register into the register now, and pretend we were
5165                  not using a base register.  */
5166               if (breg != treg)
5167                 off1 = 0;
5168               else
5169                 {
5170                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5171                                "nop", "");
5172                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5173                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5174                                "d,v,t", treg, AT, breg);
5175                   breg = 0;
5176                   tempreg = treg;
5177                   off1 = -8;
5178                 }
5179
5180               /* Set mips_optimize around the lui instruction to avoid
5181                  inserting an unnecessary nop after the lw.  */
5182               hold_mips_optimize = mips_optimize;
5183               mips_optimize = 2;
5184               macro_build_lui (NULL, &icnt, &expr1, AT);
5185               mips_optimize = hold_mips_optimize;
5186
5187               macro_build ((char *) NULL, &icnt, &expr1,
5188                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5189                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5190               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5191                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5192                            "d,v,t", tempreg, tempreg, AT);
5193               frag_var (rs_machine_dependent, 0, 0,
5194                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5195                         offset_expr.X_add_symbol, 0, NULL);
5196               used_at = 1;
5197             }
5198         }
5199       else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5200         {
5201           char *p = NULL;
5202           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5203           int adj = 0;
5204
5205           /* If this is a reference to an external, and there is no
5206              constant, or local symbol (*), with or without a
5207              constant, we want
5208                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5209              or if tempreg is PIC_CALL_REG
5210                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5211
5212              If we have a small constant, and this is a reference to
5213              an external symbol, we want
5214                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5215                addiu    $tempreg,$tempreg,<constant>
5216
5217              If we have a large constant, and this is a reference to
5218              an external symbol, we want
5219                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5220                lui      $at,<hiconstant>
5221                addiu    $at,$at,<loconstant>
5222                addu     $tempreg,$tempreg,$at
5223
5224              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5225              local symbols, even though it introduces an additional
5226              instruction.  */
5227
5228           frag_grow (28);
5229           if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
5230             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5231           if (offset_expr.X_add_number)
5232             {
5233               frag_now->tc_frag_data.tc_fr_offset =
5234                 expr1.X_add_number = offset_expr.X_add_number;
5235               offset_expr.X_add_number = 0;
5236
5237               macro_build ((char *) NULL, &icnt, &offset_expr,
5238                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5239                            "t,o(b)", tempreg, lw_reloc_type,
5240                            mips_gp_register);
5241
5242               if (expr1.X_add_number >= -0x8000
5243                   && expr1.X_add_number < 0x8000)
5244                 {
5245                   macro_build ((char *) NULL, &icnt, &expr1,
5246                                HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5247                                "t,r,j", tempreg, tempreg,
5248                                (int) BFD_RELOC_LO16);
5249                   p = frag_var (rs_machine_dependent, 4, 0,
5250                                 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5251                                 offset_expr.X_add_symbol, 0, NULL);
5252                 }
5253               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5254                 {
5255                   int dreg;
5256
5257                   /* If we are going to add in a base register, and the
5258                      target register and the base register are the same,
5259                      then we are using AT as a temporary register.  Since
5260                      we want to load the constant into AT, we add our
5261                      current AT (from the global offset table) and the
5262                      register into the register now, and pretend we were
5263                      not using a base register.  */
5264                   if (breg != treg)
5265                     dreg = tempreg;
5266                   else
5267                     {
5268                       assert (tempreg == AT);
5269                       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5270                                    HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5271                                    "d,v,t", treg, AT, breg);
5272                       dreg = treg;
5273                       adj = 4;
5274                     }
5275
5276                   macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5277                   macro_build ((char *) NULL, &icnt, &expr1,
5278                                HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5279                                "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5280                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5281                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5282                                "d,v,t", dreg, dreg, AT);
5283
5284                   p = frag_var (rs_machine_dependent, 4 + adj, 0,
5285                                 RELAX_ENCODE (16 + adj, 4 + adj,
5286                                               0, 0, 0, 0),
5287                                 offset_expr.X_add_symbol, 0, NULL);
5288
5289                   used_at = 1;
5290                 }
5291               else
5292                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5293
5294               offset_expr.X_add_number = expr1.X_add_number;
5295
5296               macro_build (p, &icnt, &offset_expr,
5297                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5298                            "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_DISP,
5299                            mips_gp_register);
5300               if (adj)
5301                 {
5302                   macro_build (p + 4, &icnt, (expressionS *) NULL,
5303                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5304                                "d,v,t", treg, tempreg, breg);
5305                   breg = 0;
5306                   tempreg = treg;
5307                 }
5308             }
5309           else
5310             {
5311               macro_build ((char *) NULL, &icnt, &offset_expr,
5312                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5313                            "t,o(b)", tempreg, lw_reloc_type,
5314                            mips_gp_register);
5315               if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5316                 p = frag_var (rs_machine_dependent, 0, 0,
5317                               RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5318                               offset_expr.X_add_symbol, 0, NULL);
5319             }
5320
5321           if (! p)
5322             {
5323               /* To avoid confusion in tc_gen_reloc, we must ensure
5324                  that this does not become a variant frag.  */
5325               frag_wane (frag_now);
5326               frag_new (0);
5327             }
5328         }
5329       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5330         {
5331           int gpdel;
5332           char *p;
5333           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5334           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5335           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5336
5337           /* This is the large GOT case.  If this is a reference to an
5338              external symbol, and there is no constant, we want
5339                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5340                addu     $tempreg,$tempreg,$gp
5341                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5342              or if tempreg is PIC_CALL_REG
5343                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5344                addu     $tempreg,$tempreg,$gp
5345                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5346              For a local symbol, we want
5347                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5348                nop
5349                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5350
5351              If we have a small constant, and this is a reference to
5352              an external symbol, we want
5353                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5354                addu     $tempreg,$tempreg,$gp
5355                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5356                nop
5357                addiu    $tempreg,$tempreg,<constant>
5358              For a local symbol, we want
5359                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5360                nop
5361                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5362
5363              If we have a large constant, and this is a reference to
5364              an external symbol, we want
5365                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5366                addu     $tempreg,$tempreg,$gp
5367                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5368                lui      $at,<hiconstant>
5369                addiu    $at,$at,<loconstant>
5370                addu     $tempreg,$tempreg,$at
5371              For a local symbol, we want
5372                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5373                lui      $at,<hiconstant>
5374                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5375                addu     $tempreg,$tempreg,$at
5376           */
5377
5378           expr1.X_add_number = offset_expr.X_add_number;
5379           offset_expr.X_add_number = 0;
5380           frag_grow (52);
5381           if (reg_needs_delay (mips_gp_register))
5382             gpdel = 4;
5383           else
5384             gpdel = 0;
5385           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5386             {
5387               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5388               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5389             }
5390           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5391                        tempreg, lui_reloc_type);
5392           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5393                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5394                        "d,v,t", tempreg, tempreg, mips_gp_register);
5395           macro_build ((char *) NULL, &icnt, &offset_expr,
5396                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5397                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5398           if (expr1.X_add_number == 0)
5399             {
5400               int off;
5401
5402               if (breg == 0)
5403                 off = 0;
5404               else
5405                 {
5406                   /* We're going to put in an addu instruction using
5407                      tempreg, so we may as well insert the nop right
5408                      now.  */
5409                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5410                                "nop", "");
5411                   off = 4;
5412                 }
5413
5414               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5415                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5416                                           8 + gpdel, 0,
5417                                           (breg == 0
5418                                            ? mips_opts.warn_about_macros
5419                                            : 0)),
5420                             offset_expr.X_add_symbol, 0, NULL);
5421             }
5422           else if (expr1.X_add_number >= -0x8000
5423                    && expr1.X_add_number < 0x8000)
5424             {
5425               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5426                            "nop", "");
5427               macro_build ((char *) NULL, &icnt, &expr1,
5428                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5429                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5430
5431               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5432                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5433                                           (breg == 0
5434                                            ? mips_opts.warn_about_macros
5435                                            : 0)),
5436                             offset_expr.X_add_symbol, 0, NULL);
5437             }
5438           else
5439             {
5440               int adj, dreg;
5441
5442               /* If we are going to add in a base register, and the
5443                  target register and the base register are the same,
5444                  then we are using AT as a temporary register.  Since
5445                  we want to load the constant into AT, we add our
5446                  current AT (from the global offset table) and the
5447                  register into the register now, and pretend we were
5448                  not using a base register.  */
5449               if (breg != treg)
5450                 {
5451                   adj = 0;
5452                   dreg = tempreg;
5453                 }
5454               else
5455                 {
5456                   assert (tempreg == AT);
5457                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5458                                "nop", "");
5459                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5460                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5461                                "d,v,t", treg, AT, breg);
5462                   dreg = treg;
5463                   adj = 8;
5464                 }
5465
5466               /* Set mips_optimize around the lui instruction to avoid
5467                  inserting an unnecessary nop after the lw.  */
5468               hold_mips_optimize = mips_optimize;
5469               mips_optimize = 2;
5470               macro_build_lui (NULL, &icnt, &expr1, AT);
5471               mips_optimize = hold_mips_optimize;
5472
5473               macro_build ((char *) NULL, &icnt, &expr1,
5474                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5475                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5476               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5477                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5478                            "d,v,t", dreg, dreg, AT);
5479
5480               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5481                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5482                                           8 + gpdel, 0,
5483                                           (breg == 0
5484                                            ? mips_opts.warn_about_macros
5485                                            : 0)),
5486                             offset_expr.X_add_symbol, 0, NULL);
5487
5488               used_at = 1;
5489             }
5490
5491           if (gpdel > 0)
5492             {
5493               /* This is needed because this instruction uses $gp, but
5494                  the first instruction on the main stream does not.  */
5495               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5496               p += 4;
5497             }
5498
5499           macro_build (p, &icnt, &offset_expr,
5500                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5501                        "t,o(b)", tempreg,
5502                        local_reloc_type,
5503                        mips_gp_register);
5504           p += 4;
5505           if (expr1.X_add_number >= -0x8000
5506               && expr1.X_add_number < 0x8000)
5507             {
5508               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5509               p += 4;
5510               macro_build (p, &icnt, &expr1,
5511                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5512                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5513               /* FIXME: If add_number is 0, and there was no base
5514                  register, the external symbol case ended with a load,
5515                  so if the symbol turns out to not be external, and
5516                  the next instruction uses tempreg, an unnecessary nop
5517                  will be inserted.  */
5518             }
5519           else
5520             {
5521               if (breg == treg)
5522                 {
5523                   /* We must add in the base register now, as in the
5524                      external symbol case.  */
5525                   assert (tempreg == AT);
5526                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5527                   p += 4;
5528                   macro_build (p, &icnt, (expressionS *) NULL,
5529                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5530                                "d,v,t", treg, AT, breg);
5531                   p += 4;
5532                   tempreg = treg;
5533                   /* We set breg to 0 because we have arranged to add
5534                      it in in both cases.  */
5535                   breg = 0;
5536                 }
5537
5538               macro_build_lui (p, &icnt, &expr1, AT);
5539               p += 4;
5540               macro_build (p, &icnt, &expr1,
5541                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5542                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5543               p += 4;
5544               macro_build (p, &icnt, (expressionS *) NULL,
5545                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5546                            "d,v,t", tempreg, tempreg, AT);
5547               p += 4;
5548             }
5549         }
5550       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5551         {
5552           char *p = NULL;
5553           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5554           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5555           int adj = 0;
5556
5557           /* This is the large GOT case.  If this is a reference to an
5558              external symbol, and there is no constant, we want
5559                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5560                add      $tempreg,$tempreg,$gp
5561                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5562              or if tempreg is PIC_CALL_REG
5563                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5564                add      $tempreg,$tempreg,$gp
5565                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5566
5567              If we have a small constant, and this is a reference to
5568              an external symbol, we want
5569                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5570                add      $tempreg,$tempreg,$gp
5571                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5572                addi     $tempreg,$tempreg,<constant>
5573
5574              If we have a large constant, and this is a reference to
5575              an external symbol, we want
5576                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5577                addu     $tempreg,$tempreg,$gp
5578                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5579                lui      $at,<hiconstant>
5580                addi     $at,$at,<loconstant>
5581                add      $tempreg,$tempreg,$at
5582
5583              If we have NewABI, and we know it's a local symbol, we want
5584                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
5585                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
5586              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
5587
5588           frag_grow (40);
5589
5590           frag_now->tc_frag_data.tc_fr_offset =
5591             expr1.X_add_number = offset_expr.X_add_number;
5592           offset_expr.X_add_number = 0;
5593
5594           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5595             {
5596               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5597               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5598             }
5599           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5600                        tempreg, lui_reloc_type);
5601           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5602                        HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5603                        "d,v,t", tempreg, tempreg, mips_gp_register);
5604           macro_build ((char *) NULL, &icnt, &offset_expr,
5605                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5606                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5607
5608           if (expr1.X_add_number == 0)
5609             {
5610               p = frag_var (rs_machine_dependent, 8, 0,
5611                             RELAX_ENCODE (12, 8, 0, 4, 0,
5612                                           mips_opts.warn_about_macros),
5613                             offset_expr.X_add_symbol, 0, NULL);
5614             }
5615           else if (expr1.X_add_number >= -0x8000
5616                    && expr1.X_add_number < 0x8000)
5617             {
5618               macro_build ((char *) NULL, &icnt, &expr1,
5619                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5620                            "t,r,j", tempreg, tempreg,
5621                            (int) BFD_RELOC_LO16);
5622               p = frag_var (rs_machine_dependent, 8, 0,
5623                             RELAX_ENCODE (16, 8, 0, 4, 0,
5624                                           mips_opts.warn_about_macros),
5625                             offset_expr.X_add_symbol, 0, NULL);
5626             }
5627           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5628             {
5629               int dreg;
5630
5631               /* If we are going to add in a base register, and the
5632                  target register and the base register are the same,
5633                  then we are using AT as a temporary register.  Since
5634                  we want to load the constant into AT, we add our
5635                  current AT (from the global offset table) and the
5636                  register into the register now, and pretend we were
5637                  not using a base register.  */
5638               if (breg != treg)
5639                 dreg = tempreg;
5640               else
5641                 {
5642                   assert (tempreg == AT);
5643                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5644                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5645                                "d,v,t", treg, AT, breg);
5646                   dreg = treg;
5647                   adj = 4;
5648                 }
5649
5650               /* Set mips_optimize around the lui instruction to avoid
5651                  inserting an unnecessary nop after the lw.  */
5652               macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5653               macro_build ((char *) NULL, &icnt, &expr1,
5654                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5655                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5656               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5657                            HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5658                            "d,v,t", dreg, dreg, AT);
5659
5660               p = frag_var (rs_machine_dependent, 8 + adj, 0,
5661                             RELAX_ENCODE (24 + adj, 8 + adj,
5662                                           0, 4, 0,
5663                                           (breg == 0
5664                                            ? mips_opts.warn_about_macros
5665                                            : 0)),
5666                             offset_expr.X_add_symbol, 0, NULL);
5667
5668               used_at = 1;
5669             }
5670           else
5671             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5672
5673           offset_expr.X_add_number = expr1.X_add_number;
5674           macro_build (p, &icnt, &offset_expr,
5675                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5676                        tempreg,
5677                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5678           macro_build (p + 4, &icnt, &offset_expr,
5679                        HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
5680                        tempreg, tempreg, (int) BFD_RELOC_MIPS_GOT_OFST);
5681           if (adj)
5682             {
5683               macro_build (p + 8, &icnt, (expressionS *) NULL,
5684                            HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5685                            "d,v,t", treg, tempreg, breg);
5686               breg = 0;
5687               tempreg = treg;
5688             }
5689         }
5690       else if (mips_pic == EMBEDDED_PIC)
5691         {
5692           /* We use
5693                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5694              */
5695           macro_build ((char *) NULL, &icnt, &offset_expr,
5696                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5697                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5698         }
5699       else
5700         abort ();
5701
5702       if (breg != 0)
5703         {
5704           char *s;
5705
5706           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5707             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" :
5708               HAVE_NEWABI ? "add" : "addu";
5709           else
5710             s = HAVE_64BIT_ADDRESSES ? "daddu" : HAVE_NEWABI ? "add" : "addu";
5711
5712           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5713                        "d,v,t", treg, tempreg, breg);
5714         }
5715
5716       if (! used_at)
5717         return;
5718
5719       break;
5720
5721     case M_J_A:
5722       /* The j instruction may not be used in PIC code, since it
5723          requires an absolute address.  We convert it to a b
5724          instruction.  */
5725       if (mips_pic == NO_PIC)
5726         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5727       else
5728         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5729       return;
5730
5731       /* The jal instructions must be handled as macros because when
5732          generating PIC code they expand to multi-instruction
5733          sequences.  Normally they are simple instructions.  */
5734     case M_JAL_1:
5735       dreg = RA;
5736       /* Fall through.  */
5737     case M_JAL_2:
5738       if (mips_pic == NO_PIC
5739           || mips_pic == EMBEDDED_PIC)
5740         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5741                      "d,s", dreg, sreg);
5742       else if (mips_pic == SVR4_PIC)
5743         {
5744           if (sreg != PIC_CALL_REG)
5745             as_warn (_("MIPS PIC call to register other than $25"));
5746
5747           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5748                        "d,s", dreg, sreg);
5749           if (! HAVE_NEWABI)
5750             {
5751               if (mips_cprestore_offset < 0)
5752                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5753               else
5754                 {
5755                   if (! mips_frame_reg_valid)
5756                     {
5757                       as_warn (_("No .frame pseudo-op used in PIC code"));
5758                       /* Quiet this warning.  */
5759                       mips_frame_reg_valid = 1;
5760                     }
5761                   if (! mips_cprestore_valid)
5762                     {
5763                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5764                       /* Quiet this warning.  */
5765                       mips_cprestore_valid = 1;
5766                     }
5767                   expr1.X_add_number = mips_cprestore_offset;
5768                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5769                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5770                                                 mips_gp_register, mips_frame_reg);
5771                 }
5772             }
5773         }
5774       else
5775         abort ();
5776
5777       return;
5778
5779     case M_JAL_A:
5780       if (mips_pic == NO_PIC)
5781         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5782       else if (mips_pic == SVR4_PIC)
5783         {
5784           char *p;
5785
5786           /* If this is a reference to an external symbol, and we are
5787              using a small GOT, we want
5788                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5789                nop
5790                jalr     $ra,$25
5791                nop
5792                lw       $gp,cprestore($sp)
5793              The cprestore value is set using the .cprestore
5794              pseudo-op.  If we are using a big GOT, we want
5795                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5796                addu     $25,$25,$gp
5797                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5798                nop
5799                jalr     $ra,$25
5800                nop
5801                lw       $gp,cprestore($sp)
5802              If the symbol is not external, we want
5803                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5804                nop
5805                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5806                jalr     $ra,$25
5807                nop
5808                lw $gp,cprestore($sp)
5809
5810              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5811              sequences above, minus nops, unless the symbol is local,
5812              which enables us to use GOT_PAGE/GOT_OFST (big got) or
5813              GOT_DISP.  */
5814           if (HAVE_NEWABI)
5815             {
5816               if (! mips_big_got)
5817                 {
5818                   frag_grow (4);
5819                   macro_build ((char *) NULL, &icnt, &offset_expr,
5820                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5821                                "t,o(b)", PIC_CALL_REG,
5822                                (int) BFD_RELOC_MIPS_CALL16,
5823                                mips_gp_register);
5824                   frag_var (rs_machine_dependent, 0, 0,
5825                             RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5826                             offset_expr.X_add_symbol, 0, NULL);
5827                 }
5828               else
5829                 {
5830                   frag_grow (20);
5831                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5832                                "t,u", PIC_CALL_REG,
5833                                (int) BFD_RELOC_MIPS_CALL_HI16);
5834                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5835                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5836                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5837                                mips_gp_register);
5838                   macro_build ((char *) NULL, &icnt, &offset_expr,
5839                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5840                                "t,o(b)", PIC_CALL_REG,
5841                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5842                   p = frag_var (rs_machine_dependent, 8, 0,
5843                                 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5844                                 offset_expr.X_add_symbol, 0, NULL);
5845                   macro_build (p, &icnt, &offset_expr,
5846                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5847                                PIC_CALL_REG, (int) BFD_RELOC_MIPS_GOT_PAGE,
5848                                mips_gp_register);
5849                   macro_build (p + 4, &icnt, &offset_expr,
5850                                HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5851                                "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5852                                (int) BFD_RELOC_MIPS_GOT_OFST);
5853                 }
5854
5855               macro_build_jalr (icnt, &offset_expr);
5856             }
5857           else
5858             {
5859               frag_grow (40);
5860               if (! mips_big_got)
5861                 {
5862                   macro_build ((char *) NULL, &icnt, &offset_expr,
5863                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5864                                "t,o(b)", PIC_CALL_REG,
5865                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5866                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5867                                "nop", "");
5868                   p = frag_var (rs_machine_dependent, 4, 0,
5869                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5870                                 offset_expr.X_add_symbol, 0, NULL);
5871                 }
5872               else
5873                 {
5874                   int gpdel;
5875
5876                   if (reg_needs_delay (mips_gp_register))
5877                     gpdel = 4;
5878                   else
5879                     gpdel = 0;
5880                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5881                                "t,u", PIC_CALL_REG,
5882                                (int) BFD_RELOC_MIPS_CALL_HI16);
5883                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5884                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5885                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5886                                mips_gp_register);
5887                   macro_build ((char *) NULL, &icnt, &offset_expr,
5888                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5889                                "t,o(b)", PIC_CALL_REG,
5890                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5891                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5892                                "nop", "");
5893                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5894                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5895                                               8 + gpdel, 0, 0),
5896                                 offset_expr.X_add_symbol, 0, NULL);
5897                   if (gpdel > 0)
5898                     {
5899                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5900                       p += 4;
5901                     }
5902                   macro_build (p, &icnt, &offset_expr,
5903                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5904                                "t,o(b)", PIC_CALL_REG,
5905                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5906                   p += 4;
5907                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5908                   p += 4;
5909                 }
5910               macro_build (p, &icnt, &offset_expr,
5911                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5912                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5913                            (int) BFD_RELOC_LO16);
5914               macro_build_jalr (icnt, &offset_expr);
5915
5916               if (mips_cprestore_offset < 0)
5917                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5918               else
5919                 {
5920                   if (! mips_frame_reg_valid)
5921                     {
5922                       as_warn (_("No .frame pseudo-op used in PIC code"));
5923                       /* Quiet this warning.  */
5924                       mips_frame_reg_valid = 1;
5925                     }
5926                   if (! mips_cprestore_valid)
5927                     {
5928                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5929                       /* Quiet this warning.  */
5930                       mips_cprestore_valid = 1;
5931                     }
5932                   if (mips_opts.noreorder)
5933                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5934                                  "nop", "");
5935                   expr1.X_add_number = mips_cprestore_offset;
5936                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5937                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5938                                                 mips_gp_register, mips_frame_reg);
5939                 }
5940             }
5941         }
5942       else if (mips_pic == EMBEDDED_PIC)
5943         {
5944           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5945           /* The linker may expand the call to a longer sequence which
5946              uses $at, so we must break rather than return.  */
5947           break;
5948         }
5949       else
5950         abort ();
5951
5952       return;
5953
5954     case M_LB_AB:
5955       s = "lb";
5956       goto ld;
5957     case M_LBU_AB:
5958       s = "lbu";
5959       goto ld;
5960     case M_LH_AB:
5961       s = "lh";
5962       goto ld;
5963     case M_LHU_AB:
5964       s = "lhu";
5965       goto ld;
5966     case M_LW_AB:
5967       s = "lw";
5968       goto ld;
5969     case M_LWC0_AB:
5970       s = "lwc0";
5971       /* Itbl support may require additional care here.  */
5972       coproc = 1;
5973       goto ld;
5974     case M_LWC1_AB:
5975       s = "lwc1";
5976       /* Itbl support may require additional care here.  */
5977       coproc = 1;
5978       goto ld;
5979     case M_LWC2_AB:
5980       s = "lwc2";
5981       /* Itbl support may require additional care here.  */
5982       coproc = 1;
5983       goto ld;
5984     case M_LWC3_AB:
5985       s = "lwc3";
5986       /* Itbl support may require additional care here.  */
5987       coproc = 1;
5988       goto ld;
5989     case M_LWL_AB:
5990       s = "lwl";
5991       lr = 1;
5992       goto ld;
5993     case M_LWR_AB:
5994       s = "lwr";
5995       lr = 1;
5996       goto ld;
5997     case M_LDC1_AB:
5998       if (mips_arch == CPU_R4650)
5999         {
6000           as_bad (_("opcode not supported on this processor"));
6001           return;
6002         }
6003       s = "ldc1";
6004       /* Itbl support may require additional care here.  */
6005       coproc = 1;
6006       goto ld;
6007     case M_LDC2_AB:
6008       s = "ldc2";
6009       /* Itbl support may require additional care here.  */
6010       coproc = 1;
6011       goto ld;
6012     case M_LDC3_AB:
6013       s = "ldc3";
6014       /* Itbl support may require additional care here.  */
6015       coproc = 1;
6016       goto ld;
6017     case M_LDL_AB:
6018       s = "ldl";
6019       lr = 1;
6020       goto ld;
6021     case M_LDR_AB:
6022       s = "ldr";
6023       lr = 1;
6024       goto ld;
6025     case M_LL_AB:
6026       s = "ll";
6027       goto ld;
6028     case M_LLD_AB:
6029       s = "lld";
6030       goto ld;
6031     case M_LWU_AB:
6032       s = "lwu";
6033     ld:
6034       if (breg == treg || coproc || lr)
6035         {
6036           tempreg = AT;
6037           used_at = 1;
6038         }
6039       else
6040         {
6041           tempreg = treg;
6042           used_at = 0;
6043         }
6044       goto ld_st;
6045     case M_SB_AB:
6046       s = "sb";
6047       goto st;
6048     case M_SH_AB:
6049       s = "sh";
6050       goto st;
6051     case M_SW_AB:
6052       s = "sw";
6053       goto st;
6054     case M_SWC0_AB:
6055       s = "swc0";
6056       /* Itbl support may require additional care here.  */
6057       coproc = 1;
6058       goto st;
6059     case M_SWC1_AB:
6060       s = "swc1";
6061       /* Itbl support may require additional care here.  */
6062       coproc = 1;
6063       goto st;
6064     case M_SWC2_AB:
6065       s = "swc2";
6066       /* Itbl support may require additional care here.  */
6067       coproc = 1;
6068       goto st;
6069     case M_SWC3_AB:
6070       s = "swc3";
6071       /* Itbl support may require additional care here.  */
6072       coproc = 1;
6073       goto st;
6074     case M_SWL_AB:
6075       s = "swl";
6076       goto st;
6077     case M_SWR_AB:
6078       s = "swr";
6079       goto st;
6080     case M_SC_AB:
6081       s = "sc";
6082       goto st;
6083     case M_SCD_AB:
6084       s = "scd";
6085       goto st;
6086     case M_SDC1_AB:
6087       if (mips_arch == CPU_R4650)
6088         {
6089           as_bad (_("opcode not supported on this processor"));
6090           return;
6091         }
6092       s = "sdc1";
6093       coproc = 1;
6094       /* Itbl support may require additional care here.  */
6095       goto st;
6096     case M_SDC2_AB:
6097       s = "sdc2";
6098       /* Itbl support may require additional care here.  */
6099       coproc = 1;
6100       goto st;
6101     case M_SDC3_AB:
6102       s = "sdc3";
6103       /* Itbl support may require additional care here.  */
6104       coproc = 1;
6105       goto st;
6106     case M_SDL_AB:
6107       s = "sdl";
6108       goto st;
6109     case M_SDR_AB:
6110       s = "sdr";
6111     st:
6112       tempreg = AT;
6113       used_at = 1;
6114     ld_st:
6115       /* Itbl support may require additional care here.  */
6116       if (mask == M_LWC1_AB
6117           || mask == M_SWC1_AB
6118           || mask == M_LDC1_AB
6119           || mask == M_SDC1_AB
6120           || mask == M_L_DAB
6121           || mask == M_S_DAB)
6122         fmt = "T,o(b)";
6123       else if (coproc)
6124         fmt = "E,o(b)";
6125       else
6126         fmt = "t,o(b)";
6127
6128       /* For embedded PIC, we allow loads where the offset is calculated
6129          by subtracting a symbol in the current segment from an unknown
6130          symbol, relative to a base register, e.g.:
6131                 <op>    $treg, <sym>-<localsym>($breg)
6132          This is used by the compiler for switch statements.  */
6133       if (mips_pic == EMBEDDED_PIC
6134           && offset_expr.X_op == O_subtract
6135           && (symbol_constant_p (offset_expr.X_op_symbol)
6136               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
6137               : (symbol_equated_p (offset_expr.X_op_symbol)
6138                  && (S_GET_SEGMENT
6139                      (symbol_get_value_expression (offset_expr.X_op_symbol)
6140                       ->X_add_symbol)
6141                      == now_seg)))
6142           && breg != 0
6143           && (offset_expr.X_add_number == 0
6144               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
6145         {
6146           /* For this case, we output the instructions:
6147                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
6148                 addiu   $tempreg,$tempreg,$breg
6149                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
6150              If the relocation would fit entirely in 16 bits, it would be
6151              nice to emit:
6152                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
6153              instead, but that seems quite difficult.  */
6154           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6155                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
6156           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6157                        ((bfd_arch_bits_per_address (stdoutput) == 32
6158                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
6159                         ? HAVE_NEWABI ? "add" : "addu" : "daddu"),
6160                        "d,v,t", tempreg, tempreg, breg);
6161           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6162                        (int) BFD_RELOC_PCREL_LO16, tempreg);
6163           if (! used_at)
6164             return;
6165           break;
6166         }
6167
6168       if (offset_expr.X_op != O_constant
6169           && offset_expr.X_op != O_symbol)
6170         {
6171           as_bad (_("expression too complex"));
6172           offset_expr.X_op = O_constant;
6173         }
6174
6175       /* A constant expression in PIC code can be handled just as it
6176          is in non PIC code.  */
6177       if (mips_pic == NO_PIC
6178           || offset_expr.X_op == O_constant)
6179         {
6180           char *p;
6181
6182           /* If this is a reference to a GP relative symbol, and there
6183              is no base register, we want
6184                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6185              Otherwise, if there is no base register, we want
6186                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6187                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6188              If we have a constant, we need two instructions anyhow,
6189              so we always use the latter form.
6190
6191              If we have a base register, and this is a reference to a
6192              GP relative symbol, we want
6193                addu     $tempreg,$breg,$gp
6194                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6195              Otherwise we want
6196                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6197                addu     $tempreg,$tempreg,$breg
6198                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6199              With a constant we always use the latter case.
6200
6201              With 64bit address space and no base register and $at usable,
6202              we want
6203                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6204                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6205                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6206                dsll32   $tempreg,0
6207                daddu    $tempreg,$at
6208                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6209              If we have a base register, we want
6210                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6211                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6212                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6213                daddu    $at,$breg
6214                dsll32   $tempreg,0
6215                daddu    $tempreg,$at
6216                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6217
6218              Without $at we can't generate the optimal path for superscalar
6219              processors here since this would require two temporary registers.
6220                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6221                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6222                dsll     $tempreg,16
6223                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6224                dsll     $tempreg,16
6225                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6226              If we have a base register, we want
6227                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6228                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6229                dsll     $tempreg,16
6230                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6231                dsll     $tempreg,16
6232                daddu    $tempreg,$tempreg,$breg
6233                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6234
6235              If we have 64-bit addresses, as an optimization, for
6236              addresses which are 32-bit constants (e.g. kseg0/kseg1
6237              addresses) we fall back to the 32-bit address generation
6238              mechanism since it is more efficient.  Note that due to
6239              the signed offset used by memory operations, the 32-bit
6240              range is shifted down by 32768 here.  This code should
6241              probably attempt to generate 64-bit constants more
6242              efficiently in general.
6243
6244              As an extension for architectures with 64-bit registers,
6245              we don't truncate 64-bit addresses given as literal
6246              constants down to 32 bits, to support existing practice
6247              in the mips64 Linux (the kernel), that compiles source
6248              files with -mabi=64, assembling them as o32 or n32 (with
6249              -Wa,-32 or -Wa,-n32).  This is not beautiful, but since
6250              the whole kernel is loaded into a memory region that is
6251              addressible with sign-extended 32-bit addresses, it is
6252              wasteful to compute the upper 32 bits of every
6253              non-literal address, that takes more space and time.
6254              Some day this should probably be implemented as an
6255              assembler option, such that the kernel doesn't have to
6256              use such ugly hacks, even though it will still have to
6257              end up converting the binary to ELF32 for a number of
6258              platforms whose boot loaders don't support ELF64
6259              binaries.  */
6260           if ((offset_expr.X_op != O_constant && HAVE_64BIT_ADDRESSES)
6261               || (offset_expr.X_op == O_constant
6262                   && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)
6263                   && HAVE_64BIT_ADDRESS_CONSTANTS))
6264             {
6265               p = NULL;
6266
6267               /* We don't do GP optimization for now because RELAX_ENCODE can't
6268                  hold the data for such large chunks.  */
6269
6270               if (used_at == 0 && ! mips_opts.noat)
6271                 {
6272                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6273                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6274                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6275                                AT, (int) BFD_RELOC_HI16_S);
6276                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6277                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6278                   if (breg != 0)
6279                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6280                                  "d,v,t", AT, AT, breg);
6281                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
6282                                "d,w,<", tempreg, tempreg, 0);
6283                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6284                                "d,v,t", tempreg, tempreg, AT);
6285                   macro_build (p, &icnt, &offset_expr, s,
6286                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6287                   used_at = 1;
6288                 }
6289               else
6290                 {
6291                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6292                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6293                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6294                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6295                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6296                                "d,w,<", tempreg, tempreg, 16);
6297                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6298                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
6299                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6300                                "d,w,<", tempreg, tempreg, 16);
6301                   if (breg != 0)
6302                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6303                                  "d,v,t", tempreg, tempreg, breg);
6304                   macro_build (p, &icnt, &offset_expr, s,
6305                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6306                 }
6307
6308               return;
6309             }
6310           else if (offset_expr.X_op == O_constant
6311                    && !HAVE_64BIT_ADDRESS_CONSTANTS
6312                    && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6313             as_bad (_("load/store address overflow (max 32 bits)"));
6314
6315           if (breg == 0)
6316             {
6317               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6318                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6319                 p = NULL;
6320               else
6321                 {
6322                   frag_grow (20);
6323                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6324                                treg, (int) BFD_RELOC_GPREL16,
6325                                mips_gp_register);
6326                   p = frag_var (rs_machine_dependent, 8, 0,
6327                                 RELAX_ENCODE (4, 8, 0, 4, 0,
6328                                               (mips_opts.warn_about_macros
6329                                                || (used_at
6330                                                    && mips_opts.noat))),
6331                                 offset_expr.X_add_symbol, 0, NULL);
6332                   used_at = 0;
6333                 }
6334               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6335               if (p != NULL)
6336                 p += 4;
6337               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6338                            (int) BFD_RELOC_LO16, tempreg);
6339             }
6340           else
6341             {
6342               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6343                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6344                 p = NULL;
6345               else
6346                 {
6347                   frag_grow (28);
6348                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6349                                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6350                                ? "add" : "addu" : "daddu",
6351                                "d,v,t", tempreg, breg, mips_gp_register);
6352                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6353                                treg, (int) BFD_RELOC_GPREL16, tempreg);
6354                   p = frag_var (rs_machine_dependent, 12, 0,
6355                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6356                                 offset_expr.X_add_symbol, 0, NULL);
6357                 }
6358               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6359               if (p != NULL)
6360                 p += 4;
6361               macro_build (p, &icnt, (expressionS *) NULL,
6362                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6363                            ? "add" : "addu" : "daddu",
6364                            "d,v,t", tempreg, tempreg, breg);
6365               if (p != NULL)
6366                 p += 4;
6367               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6368                            (int) BFD_RELOC_LO16, tempreg);
6369             }
6370         }
6371       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6372         {
6373           char *p;
6374           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6375
6376           /* If this is a reference to an external symbol, we want
6377                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6378                nop
6379                <op>     $treg,0($tempreg)
6380              Otherwise we want
6381                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6382                nop
6383                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6384                <op>     $treg,0($tempreg)
6385
6386              For NewABI, we want
6387                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6388                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
6389
6390              If there is a base register, we add it to $tempreg before
6391              the <op>.  If there is a constant, we stick it in the
6392              <op> instruction.  We don't handle constants larger than
6393              16 bits, because we have no way to load the upper 16 bits
6394              (actually, we could handle them for the subset of cases
6395              in which we are not using $at).  */
6396           assert (offset_expr.X_op == O_symbol);
6397           if (HAVE_NEWABI)
6398             {
6399               macro_build ((char *) NULL, &icnt, &offset_expr,
6400                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6401                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6402                            mips_gp_register);
6403               if (breg != 0)
6404                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6405                              HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6406                              "d,v,t", tempreg, tempreg, breg);
6407               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6408                            (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6409
6410               if (! used_at)
6411                 return;
6412
6413               break;
6414             }
6415           expr1.X_add_number = offset_expr.X_add_number;
6416           offset_expr.X_add_number = 0;
6417           if (expr1.X_add_number < -0x8000
6418               || expr1.X_add_number >= 0x8000)
6419             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6420           frag_grow (20);
6421           macro_build ((char *) NULL, &icnt, &offset_expr,
6422                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
6423                        (int) lw_reloc_type, mips_gp_register);
6424           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6425           p = frag_var (rs_machine_dependent, 4, 0,
6426                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6427                         offset_expr.X_add_symbol, 0, NULL);
6428           macro_build (p, &icnt, &offset_expr,
6429                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6430                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6431           if (breg != 0)
6432             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6433                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6434                          "d,v,t", tempreg, tempreg, breg);
6435           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6436                        (int) BFD_RELOC_LO16, tempreg);
6437         }
6438       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6439         {
6440           int gpdel;
6441           char *p;
6442
6443           /* If this is a reference to an external symbol, we want
6444                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6445                addu     $tempreg,$tempreg,$gp
6446                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6447                <op>     $treg,0($tempreg)
6448              Otherwise we want
6449                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6450                nop
6451                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6452                <op>     $treg,0($tempreg)
6453              If there is a base register, we add it to $tempreg before
6454              the <op>.  If there is a constant, we stick it in the
6455              <op> instruction.  We don't handle constants larger than
6456              16 bits, because we have no way to load the upper 16 bits
6457              (actually, we could handle them for the subset of cases
6458              in which we are not using $at).  */
6459           assert (offset_expr.X_op == O_symbol);
6460           expr1.X_add_number = offset_expr.X_add_number;
6461           offset_expr.X_add_number = 0;
6462           if (expr1.X_add_number < -0x8000
6463               || expr1.X_add_number >= 0x8000)
6464             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6465           if (reg_needs_delay (mips_gp_register))
6466             gpdel = 4;
6467           else
6468             gpdel = 0;
6469           frag_grow (36);
6470           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6471                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6472           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6473                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6474                        "d,v,t", tempreg, tempreg, mips_gp_register);
6475           macro_build ((char *) NULL, &icnt, &offset_expr,
6476                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6477                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6478                        tempreg);
6479           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6480                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6481                         offset_expr.X_add_symbol, 0, NULL);
6482           if (gpdel > 0)
6483             {
6484               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6485               p += 4;
6486             }
6487           macro_build (p, &icnt, &offset_expr,
6488                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6489                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6490                        mips_gp_register);
6491           p += 4;
6492           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6493           p += 4;
6494           macro_build (p, &icnt, &offset_expr,
6495                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6496                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6497           if (breg != 0)
6498             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6499                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6500                          "d,v,t", tempreg, tempreg, breg);
6501           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6502                        (int) BFD_RELOC_LO16, tempreg);
6503         }
6504       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6505         {
6506           char *p;
6507           int bregsz = breg != 0 ? 4 : 0;
6508
6509           /* If this is a reference to an external symbol, we want
6510                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6511                add      $tempreg,$tempreg,$gp
6512                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6513                <op>     $treg,<ofst>($tempreg)
6514              Otherwise, for local symbols, we want:
6515                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6516                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
6517           assert (offset_expr.X_op == O_symbol);
6518           frag_now->tc_frag_data.tc_fr_offset =
6519             expr1.X_add_number = offset_expr.X_add_number;
6520           offset_expr.X_add_number = 0;
6521           if (expr1.X_add_number < -0x8000
6522               || expr1.X_add_number >= 0x8000)
6523             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6524           frag_grow (36);
6525           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6526                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6527           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6528                        HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6529                        "d,v,t", tempreg, tempreg, mips_gp_register);
6530           macro_build ((char *) NULL, &icnt, &offset_expr,
6531                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6532                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6533                        tempreg);
6534           if (breg != 0)
6535             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6536                          HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6537                          "d,v,t", tempreg, tempreg, breg);
6538           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6539                        (int) BFD_RELOC_LO16, tempreg);
6540
6541           offset_expr.X_add_number = expr1.X_add_number;
6542           p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6543                         RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6544                                       0, 4 + bregsz, 0, 0),
6545                         offset_expr.X_add_symbol, 0, NULL);
6546           macro_build (p, &icnt, &offset_expr,
6547                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6548                        "t,o(b)", tempreg,
6549                        (int) BFD_RELOC_MIPS_GOT_PAGE,
6550                        mips_gp_register);
6551           if (breg != 0)
6552             macro_build (p + 4, &icnt, (expressionS *) NULL,
6553                          HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6554                          "d,v,t", tempreg, tempreg, breg);
6555           macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6556                        (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6557         }
6558       else if (mips_pic == EMBEDDED_PIC)
6559         {
6560           /* If there is no base register, we want
6561                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6562              If there is a base register, we want
6563                addu     $tempreg,$breg,$gp
6564                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6565              */
6566           assert (offset_expr.X_op == O_symbol);
6567           if (breg == 0)
6568             {
6569               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6570                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6571               used_at = 0;
6572             }
6573           else
6574             {
6575               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6576                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6577                            "d,v,t", tempreg, breg, mips_gp_register);
6578               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6579                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6580             }
6581         }
6582       else
6583         abort ();
6584
6585       if (! used_at)
6586         return;
6587
6588       break;
6589
6590     case M_LI:
6591     case M_LI_S:
6592       load_register (&icnt, treg, &imm_expr, 0);
6593       return;
6594
6595     case M_DLI:
6596       load_register (&icnt, treg, &imm_expr, 1);
6597       return;
6598
6599     case M_LI_SS:
6600       if (imm_expr.X_op == O_constant)
6601         {
6602           load_register (&icnt, AT, &imm_expr, 0);
6603           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6604                        "mtc1", "t,G", AT, treg);
6605           break;
6606         }
6607       else
6608         {
6609           assert (offset_expr.X_op == O_symbol
6610                   && strcmp (segment_name (S_GET_SEGMENT
6611                                            (offset_expr.X_add_symbol)),
6612                              ".lit4") == 0
6613                   && offset_expr.X_add_number == 0);
6614           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6615                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6616           return;
6617         }
6618
6619     case M_LI_D:
6620       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6621          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6622          order 32 bits of the value and the low order 32 bits are either
6623          zero or in OFFSET_EXPR.  */
6624       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6625         {
6626           if (HAVE_64BIT_GPRS)
6627             load_register (&icnt, treg, &imm_expr, 1);
6628           else
6629             {
6630               int hreg, lreg;
6631
6632               if (target_big_endian)
6633                 {
6634                   hreg = treg;
6635                   lreg = treg + 1;
6636                 }
6637               else
6638                 {
6639                   hreg = treg + 1;
6640                   lreg = treg;
6641                 }
6642
6643               if (hreg <= 31)
6644                 load_register (&icnt, hreg, &imm_expr, 0);
6645               if (lreg <= 31)
6646                 {
6647                   if (offset_expr.X_op == O_absent)
6648                     move_register (&icnt, lreg, 0);
6649                   else
6650                     {
6651                       assert (offset_expr.X_op == O_constant);
6652                       load_register (&icnt, lreg, &offset_expr, 0);
6653                     }
6654                 }
6655             }
6656           return;
6657         }
6658
6659       /* We know that sym is in the .rdata section.  First we get the
6660          upper 16 bits of the address.  */
6661       if (mips_pic == NO_PIC)
6662         {
6663           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6664         }
6665       else if (mips_pic == SVR4_PIC)
6666         {
6667           macro_build ((char *) NULL, &icnt, &offset_expr,
6668                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6669                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6670                        mips_gp_register);
6671         }
6672       else if (mips_pic == EMBEDDED_PIC)
6673         {
6674           /* For embedded PIC we pick up the entire address off $gp in
6675              a single instruction.  */
6676           macro_build ((char *) NULL, &icnt, &offset_expr,
6677                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6678                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6679           offset_expr.X_op = O_constant;
6680           offset_expr.X_add_number = 0;
6681         }
6682       else
6683         abort ();
6684
6685       /* Now we load the register(s).  */
6686       if (HAVE_64BIT_GPRS)
6687         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6688                      treg, (int) BFD_RELOC_LO16, AT);
6689       else
6690         {
6691           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6692                        treg, (int) BFD_RELOC_LO16, AT);
6693           if (treg != RA)
6694             {
6695               /* FIXME: How in the world do we deal with the possible
6696                  overflow here?  */
6697               offset_expr.X_add_number += 4;
6698               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6699                            treg + 1, (int) BFD_RELOC_LO16, AT);
6700             }
6701         }
6702
6703       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6704          does not become a variant frag.  */
6705       frag_wane (frag_now);
6706       frag_new (0);
6707
6708       break;
6709
6710     case M_LI_DD:
6711       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6712          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6713          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6714          the value and the low order 32 bits are either zero or in
6715          OFFSET_EXPR.  */
6716       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6717         {
6718           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6719           if (HAVE_64BIT_FPRS)
6720             {
6721               assert (HAVE_64BIT_GPRS);
6722               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6723                            "dmtc1", "t,S", AT, treg);
6724             }
6725           else
6726             {
6727               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6728                            "mtc1", "t,G", AT, treg + 1);
6729               if (offset_expr.X_op == O_absent)
6730                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6731                              "mtc1", "t,G", 0, treg);
6732               else
6733                 {
6734                   assert (offset_expr.X_op == O_constant);
6735                   load_register (&icnt, AT, &offset_expr, 0);
6736                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6737                                "mtc1", "t,G", AT, treg);
6738                 }
6739             }
6740           break;
6741         }
6742
6743       assert (offset_expr.X_op == O_symbol
6744               && offset_expr.X_add_number == 0);
6745       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6746       if (strcmp (s, ".lit8") == 0)
6747         {
6748           if (mips_opts.isa != ISA_MIPS1)
6749             {
6750               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6751                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6752                            mips_gp_register);
6753               return;
6754             }
6755           breg = mips_gp_register;
6756           r = BFD_RELOC_MIPS_LITERAL;
6757           goto dob;
6758         }
6759       else
6760         {
6761           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6762           if (mips_pic == SVR4_PIC)
6763             macro_build ((char *) NULL, &icnt, &offset_expr,
6764                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6765                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6766                          mips_gp_register);
6767           else
6768             {
6769               /* FIXME: This won't work for a 64 bit address.  */
6770               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6771             }
6772
6773           if (mips_opts.isa != ISA_MIPS1)
6774             {
6775               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6776                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6777
6778               /* To avoid confusion in tc_gen_reloc, we must ensure
6779                  that this does not become a variant frag.  */
6780               frag_wane (frag_now);
6781               frag_new (0);
6782
6783               break;
6784             }
6785           breg = AT;
6786           r = BFD_RELOC_LO16;
6787           goto dob;
6788         }
6789
6790     case M_L_DOB:
6791       if (mips_arch == CPU_R4650)
6792         {
6793           as_bad (_("opcode not supported on this processor"));
6794           return;
6795         }
6796       /* Even on a big endian machine $fn comes before $fn+1.  We have
6797          to adjust when loading from memory.  */
6798       r = BFD_RELOC_LO16;
6799     dob:
6800       assert (mips_opts.isa == ISA_MIPS1);
6801       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6802                    target_big_endian ? treg + 1 : treg,
6803                    (int) r, breg);
6804       /* FIXME: A possible overflow which I don't know how to deal
6805          with.  */
6806       offset_expr.X_add_number += 4;
6807       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6808                    target_big_endian ? treg : treg + 1,
6809                    (int) r, breg);
6810
6811       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6812          does not become a variant frag.  */
6813       frag_wane (frag_now);
6814       frag_new (0);
6815
6816       if (breg != AT)
6817         return;
6818       break;
6819
6820     case M_L_DAB:
6821       /*
6822        * The MIPS assembler seems to check for X_add_number not
6823        * being double aligned and generating:
6824        *        lui     at,%hi(foo+1)
6825        *        addu    at,at,v1
6826        *        addiu   at,at,%lo(foo+1)
6827        *        lwc1    f2,0(at)
6828        *        lwc1    f3,4(at)
6829        * But, the resulting address is the same after relocation so why
6830        * generate the extra instruction?
6831        */
6832       if (mips_arch == CPU_R4650)
6833         {
6834           as_bad (_("opcode not supported on this processor"));
6835           return;
6836         }
6837       /* Itbl support may require additional care here.  */
6838       coproc = 1;
6839       if (mips_opts.isa != ISA_MIPS1)
6840         {
6841           s = "ldc1";
6842           goto ld;
6843         }
6844
6845       s = "lwc1";
6846       fmt = "T,o(b)";
6847       goto ldd_std;
6848
6849     case M_S_DAB:
6850       if (mips_arch == CPU_R4650)
6851         {
6852           as_bad (_("opcode not supported on this processor"));
6853           return;
6854         }
6855
6856       if (mips_opts.isa != ISA_MIPS1)
6857         {
6858           s = "sdc1";
6859           goto st;
6860         }
6861
6862       s = "swc1";
6863       fmt = "T,o(b)";
6864       /* Itbl support may require additional care here.  */
6865       coproc = 1;
6866       goto ldd_std;
6867
6868     case M_LD_AB:
6869       if (HAVE_64BIT_GPRS)
6870         {
6871           s = "ld";
6872           goto ld;
6873         }
6874
6875       s = "lw";
6876       fmt = "t,o(b)";
6877       goto ldd_std;
6878
6879     case M_SD_AB:
6880       if (HAVE_64BIT_GPRS)
6881         {
6882           s = "sd";
6883           goto st;
6884         }
6885
6886       s = "sw";
6887       fmt = "t,o(b)";
6888
6889     ldd_std:
6890       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6891          loads for the case of doing a pair of loads to simulate an 'ld'.
6892          This is not currently done by the compiler, and assembly coders
6893          writing embedded-pic code can cope.  */
6894
6895       if (offset_expr.X_op != O_symbol
6896           && offset_expr.X_op != O_constant)
6897         {
6898           as_bad (_("expression too complex"));
6899           offset_expr.X_op = O_constant;
6900         }
6901
6902       /* Even on a big endian machine $fn comes before $fn+1.  We have
6903          to adjust when loading from memory.  We set coproc if we must
6904          load $fn+1 first.  */
6905       /* Itbl support may require additional care here.  */
6906       if (! target_big_endian)
6907         coproc = 0;
6908
6909       if (mips_pic == NO_PIC
6910           || offset_expr.X_op == O_constant)
6911         {
6912           char *p;
6913
6914           /* If this is a reference to a GP relative symbol, we want
6915                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6916                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6917              If we have a base register, we use this
6918                addu     $at,$breg,$gp
6919                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6920                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6921              If this is not a GP relative symbol, we want
6922                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6923                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6924                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6925              If there is a base register, we add it to $at after the
6926              lui instruction.  If there is a constant, we always use
6927              the last case.  */
6928           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6929               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6930             {
6931               p = NULL;
6932               used_at = 1;
6933             }
6934           else
6935             {
6936               int off;
6937
6938               if (breg == 0)
6939                 {
6940                   frag_grow (28);
6941                   tempreg = mips_gp_register;
6942                   off = 0;
6943                   used_at = 0;
6944                 }
6945               else
6946                 {
6947                   frag_grow (36);
6948                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6949                                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6950                                ? "add" : "addu" : "daddu",
6951                                "d,v,t", AT, breg, mips_gp_register);
6952                   tempreg = AT;
6953                   off = 4;
6954                   used_at = 1;
6955                 }
6956
6957               /* Itbl support may require additional care here.  */
6958               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6959                            coproc ? treg + 1 : treg,
6960                            (int) BFD_RELOC_GPREL16, tempreg);
6961               offset_expr.X_add_number += 4;
6962
6963               /* Set mips_optimize to 2 to avoid inserting an
6964                  undesired nop.  */
6965               hold_mips_optimize = mips_optimize;
6966               mips_optimize = 2;
6967               /* Itbl support may require additional care here.  */
6968               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6969                            coproc ? treg : treg + 1,
6970                            (int) BFD_RELOC_GPREL16, tempreg);
6971               mips_optimize = hold_mips_optimize;
6972
6973               p = frag_var (rs_machine_dependent, 12 + off, 0,
6974                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6975                                           used_at && mips_opts.noat),
6976                             offset_expr.X_add_symbol, 0, NULL);
6977
6978               /* We just generated two relocs.  When tc_gen_reloc
6979                  handles this case, it will skip the first reloc and
6980                  handle the second.  The second reloc already has an
6981                  extra addend of 4, which we added above.  We must
6982                  subtract it out, and then subtract another 4 to make
6983                  the first reloc come out right.  The second reloc
6984                  will come out right because we are going to add 4 to
6985                  offset_expr when we build its instruction below.
6986
6987                  If we have a symbol, then we don't want to include
6988                  the offset, because it will wind up being included
6989                  when we generate the reloc.  */
6990
6991               if (offset_expr.X_op == O_constant)
6992                 offset_expr.X_add_number -= 8;
6993               else
6994                 {
6995                   offset_expr.X_add_number = -4;
6996                   offset_expr.X_op = O_constant;
6997                 }
6998             }
6999           macro_build_lui (p, &icnt, &offset_expr, AT);
7000           if (p != NULL)
7001             p += 4;
7002           if (breg != 0)
7003             {
7004               macro_build (p, &icnt, (expressionS *) NULL,
7005                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7006                            ? "add" : "addu" : "daddu",
7007                            "d,v,t", AT, breg, AT);
7008               if (p != NULL)
7009                 p += 4;
7010             }
7011           /* Itbl support may require additional care here.  */
7012           macro_build (p, &icnt, &offset_expr, s, fmt,
7013                        coproc ? treg + 1 : treg,
7014                        (int) BFD_RELOC_LO16, AT);
7015           if (p != NULL)
7016             p += 4;
7017           /* FIXME: How do we handle overflow here?  */
7018           offset_expr.X_add_number += 4;
7019           /* Itbl support may require additional care here.  */
7020           macro_build (p, &icnt, &offset_expr, s, fmt,
7021                        coproc ? treg : treg + 1,
7022                        (int) BFD_RELOC_LO16, AT);
7023         }
7024       else if (mips_pic == SVR4_PIC && ! mips_big_got)
7025         {
7026           int off;
7027
7028           /* If this is a reference to an external symbol, we want
7029                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7030                nop
7031                <op>     $treg,0($at)
7032                <op>     $treg+1,4($at)
7033              Otherwise we want
7034                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7035                nop
7036                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
7037                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
7038              If there is a base register we add it to $at before the
7039              lwc1 instructions.  If there is a constant we include it
7040              in the lwc1 instructions.  */
7041           used_at = 1;
7042           expr1.X_add_number = offset_expr.X_add_number;
7043           offset_expr.X_add_number = 0;
7044           if (expr1.X_add_number < -0x8000
7045               || expr1.X_add_number >= 0x8000 - 4)
7046             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7047           if (breg == 0)
7048             off = 0;
7049           else
7050             off = 4;
7051           frag_grow (24 + off);
7052           macro_build ((char *) NULL, &icnt, &offset_expr,
7053                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
7054                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
7055           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7056           if (breg != 0)
7057             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7058                          HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7059                          ? "add" : "addu" : "daddu",
7060                          "d,v,t", AT, breg, AT);
7061           /* Itbl support may require additional care here.  */
7062           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7063                        coproc ? treg + 1 : treg,
7064                        (int) BFD_RELOC_LO16, AT);
7065           expr1.X_add_number += 4;
7066
7067           /* Set mips_optimize to 2 to avoid inserting an undesired
7068              nop.  */
7069           hold_mips_optimize = mips_optimize;
7070           mips_optimize = 2;
7071           /* Itbl support may require additional care here.  */
7072           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7073                        coproc ? treg : treg + 1,
7074                        (int) BFD_RELOC_LO16, AT);
7075           mips_optimize = hold_mips_optimize;
7076
7077           (void) frag_var (rs_machine_dependent, 0, 0,
7078                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
7079                            offset_expr.X_add_symbol, 0, NULL);
7080         }
7081       else if (mips_pic == SVR4_PIC)
7082         {
7083           int gpdel, off;
7084           char *p;
7085
7086           /* If this is a reference to an external symbol, we want
7087                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
7088                addu     $at,$at,$gp
7089                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
7090                nop
7091                <op>     $treg,0($at)
7092                <op>     $treg+1,4($at)
7093              Otherwise we want
7094                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7095                nop
7096                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
7097                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
7098              If there is a base register we add it to $at before the
7099              lwc1 instructions.  If there is a constant we include it
7100              in the lwc1 instructions.  */
7101           used_at = 1;
7102           expr1.X_add_number = offset_expr.X_add_number;
7103           offset_expr.X_add_number = 0;
7104           if (expr1.X_add_number < -0x8000
7105               || expr1.X_add_number >= 0x8000 - 4)
7106             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7107           if (reg_needs_delay (mips_gp_register))
7108             gpdel = 4;
7109           else
7110             gpdel = 0;
7111           if (breg == 0)
7112             off = 0;
7113           else
7114             off = 4;
7115           frag_grow (56);
7116           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
7117                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
7118           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7119                        HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7120                        ? "add" : "addu" : "daddu",
7121                        "d,v,t", AT, AT, mips_gp_register);
7122           macro_build ((char *) NULL, &icnt, &offset_expr,
7123                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
7124                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
7125           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7126           if (breg != 0)
7127             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7128                          HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7129                          ? "add" : "addu" : "daddu",
7130                          "d,v,t", AT, breg, AT);
7131           /* Itbl support may require additional care here.  */
7132           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7133                        coproc ? treg + 1 : treg,
7134                        (int) BFD_RELOC_LO16, AT);
7135           expr1.X_add_number += 4;
7136
7137           /* Set mips_optimize to 2 to avoid inserting an undesired
7138              nop.  */
7139           hold_mips_optimize = mips_optimize;
7140           mips_optimize = 2;
7141           /* Itbl support may require additional care here.  */
7142           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7143                        coproc ? treg : treg + 1,
7144                        (int) BFD_RELOC_LO16, AT);
7145           mips_optimize = hold_mips_optimize;
7146           expr1.X_add_number -= 4;
7147
7148           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
7149                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
7150                                       8 + gpdel + off, 1, 0),
7151                         offset_expr.X_add_symbol, 0, NULL);
7152           if (gpdel > 0)
7153             {
7154               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7155               p += 4;
7156             }
7157           macro_build (p, &icnt, &offset_expr,
7158                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
7159                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
7160                        mips_gp_register);
7161           p += 4;
7162           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7163           p += 4;
7164           if (breg != 0)
7165             {
7166               macro_build (p, &icnt, (expressionS *) NULL,
7167                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7168                            ? "add" : "addu" : "daddu",
7169                            "d,v,t", AT, breg, AT);
7170               p += 4;
7171             }
7172           /* Itbl support may require additional care here.  */
7173           macro_build (p, &icnt, &expr1, s, fmt,
7174                        coproc ? treg + 1 : treg,
7175                        (int) BFD_RELOC_LO16, AT);
7176           p += 4;
7177           expr1.X_add_number += 4;
7178
7179           /* Set mips_optimize to 2 to avoid inserting an undesired
7180              nop.  */
7181           hold_mips_optimize = mips_optimize;
7182           mips_optimize = 2;
7183           /* Itbl support may require additional care here.  */
7184           macro_build (p, &icnt, &expr1, s, fmt,
7185                        coproc ? treg : treg + 1,
7186                        (int) BFD_RELOC_LO16, AT);
7187           mips_optimize = hold_mips_optimize;
7188         }
7189       else if (mips_pic == EMBEDDED_PIC)
7190         {
7191           /* If there is no base register, we use
7192                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
7193                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
7194              If we have a base register, we use
7195                addu     $at,$breg,$gp
7196                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
7197                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
7198              */
7199           if (breg == 0)
7200             {
7201               tempreg = mips_gp_register;
7202               used_at = 0;
7203             }
7204           else
7205             {
7206               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7207                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7208                            "d,v,t", AT, breg, mips_gp_register);
7209               tempreg = AT;
7210               used_at = 1;
7211             }
7212
7213           /* Itbl support may require additional care here.  */
7214           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7215                        coproc ? treg + 1 : treg,
7216                        (int) BFD_RELOC_GPREL16, tempreg);
7217           offset_expr.X_add_number += 4;
7218           /* Itbl support may require additional care here.  */
7219           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7220                        coproc ? treg : treg + 1,
7221                        (int) BFD_RELOC_GPREL16, tempreg);
7222         }
7223       else
7224         abort ();
7225
7226       if (! used_at)
7227         return;
7228
7229       break;
7230
7231     case M_LD_OB:
7232       s = "lw";
7233       goto sd_ob;
7234     case M_SD_OB:
7235       s = "sw";
7236     sd_ob:
7237       assert (HAVE_32BIT_ADDRESSES);
7238       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7239                    (int) BFD_RELOC_LO16, breg);
7240       offset_expr.X_add_number += 4;
7241       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7242                    (int) BFD_RELOC_LO16, breg);
7243       return;
7244
7245    /* New code added to support COPZ instructions.
7246       This code builds table entries out of the macros in mip_opcodes.
7247       R4000 uses interlocks to handle coproc delays.
7248       Other chips (like the R3000) require nops to be inserted for delays.
7249
7250       FIXME: Currently, we require that the user handle delays.
7251       In order to fill delay slots for non-interlocked chips,
7252       we must have a way to specify delays based on the coprocessor.
7253       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7254       What are the side-effects of the cop instruction?
7255       What cache support might we have and what are its effects?
7256       Both coprocessor & memory require delays. how long???
7257       What registers are read/set/modified?
7258
7259       If an itbl is provided to interpret cop instructions,
7260       this knowledge can be encoded in the itbl spec.  */
7261
7262     case M_COP0:
7263       s = "c0";
7264       goto copz;
7265     case M_COP1:
7266       s = "c1";
7267       goto copz;
7268     case M_COP2:
7269       s = "c2";
7270       goto copz;
7271     case M_COP3:
7272       s = "c3";
7273     copz:
7274       /* For now we just do C (same as Cz).  The parameter will be
7275          stored in insn_opcode by mips_ip.  */
7276       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
7277                    ip->insn_opcode);
7278       return;
7279
7280     case M_MOVE:
7281       move_register (&icnt, dreg, sreg);
7282       return;
7283
7284 #ifdef LOSING_COMPILER
7285     default:
7286       /* Try and see if this is a new itbl instruction.
7287          This code builds table entries out of the macros in mip_opcodes.
7288          FIXME: For now we just assemble the expression and pass it's
7289          value along as a 32-bit immediate.
7290          We may want to have the assembler assemble this value,
7291          so that we gain the assembler's knowledge of delay slots,
7292          symbols, etc.
7293          Would it be more efficient to use mask (id) here? */
7294       if (itbl_have_entries
7295           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7296         {
7297           s = ip->insn_mo->name;
7298           s2 = "cop3";
7299           coproc = ITBL_DECODE_PNUM (immed_expr);;
7300           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
7301           return;
7302         }
7303       macro2 (ip);
7304       return;
7305     }
7306   if (mips_opts.noat)
7307     as_warn (_("Macro used $at after \".set noat\""));
7308 }
7309
7310 static void
7311 macro2 (ip)
7312      struct mips_cl_insn *ip;
7313 {
7314   register int treg, sreg, dreg, breg;
7315   int tempreg;
7316   int mask;
7317   int icnt = 0;
7318   int used_at;
7319   expressionS expr1;
7320   const char *s;
7321   const char *s2;
7322   const char *fmt;
7323   int likely = 0;
7324   int dbl = 0;
7325   int coproc = 0;
7326   int lr = 0;
7327   int imm = 0;
7328   int off;
7329   offsetT maxnum;
7330   bfd_reloc_code_real_type r;
7331   char *p;
7332
7333   treg = (ip->insn_opcode >> 16) & 0x1f;
7334   dreg = (ip->insn_opcode >> 11) & 0x1f;
7335   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7336   mask = ip->insn_mo->mask;
7337
7338   expr1.X_op = O_constant;
7339   expr1.X_op_symbol = NULL;
7340   expr1.X_add_symbol = NULL;
7341   expr1.X_add_number = 1;
7342
7343   switch (mask)
7344     {
7345 #endif /* LOSING_COMPILER */
7346
7347     case M_DMUL:
7348       dbl = 1;
7349     case M_MUL:
7350       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7351                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7352       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7353                    dreg);
7354       return;
7355
7356     case M_DMUL_I:
7357       dbl = 1;
7358     case M_MUL_I:
7359       /* The MIPS assembler some times generates shifts and adds.  I'm
7360          not trying to be that fancy. GCC should do this for us
7361          anyway.  */
7362       load_register (&icnt, AT, &imm_expr, dbl);
7363       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7364                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
7365       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7366                    dreg);
7367       break;
7368
7369     case M_DMULO_I:
7370       dbl = 1;
7371     case M_MULO_I:
7372       imm = 1;
7373       goto do_mulo;
7374
7375     case M_DMULO:
7376       dbl = 1;
7377     case M_MULO:
7378     do_mulo:
7379       mips_emit_delays (TRUE);
7380       ++mips_opts.noreorder;
7381       mips_any_noreorder = 1;
7382       if (imm)
7383         load_register (&icnt, AT, &imm_expr, dbl);
7384       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7385                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7386       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7387                    dreg);
7388       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7389                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7390       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7391                    AT);
7392       if (mips_trap)
7393         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7394                      "s,t,q", dreg, AT, 6);
7395       else
7396         {
7397           expr1.X_add_number = 8;
7398           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
7399                        AT);
7400           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7401                        0);
7402           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7403                        "c", 6);
7404         }
7405       --mips_opts.noreorder;
7406       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
7407       break;
7408
7409     case M_DMULOU_I:
7410       dbl = 1;
7411     case M_MULOU_I:
7412       imm = 1;
7413       goto do_mulou;
7414
7415     case M_DMULOU:
7416       dbl = 1;
7417     case M_MULOU:
7418     do_mulou:
7419       mips_emit_delays (TRUE);
7420       ++mips_opts.noreorder;
7421       mips_any_noreorder = 1;
7422       if (imm)
7423         load_register (&icnt, AT, &imm_expr, dbl);
7424       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7425                    dbl ? "dmultu" : "multu",
7426                    "s,t", sreg, imm ? AT : treg);
7427       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7428                    AT);
7429       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7430                    dreg);
7431       if (mips_trap)
7432         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7433                      "s,t,q", AT, 0, 6);
7434       else
7435         {
7436           expr1.X_add_number = 8;
7437           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7438           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7439                        0);
7440           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7441                        "c", 6);
7442         }
7443       --mips_opts.noreorder;
7444       break;
7445
7446     case M_DROL:
7447       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7448         {
7449           if (dreg == sreg)
7450             {
7451               tempreg = AT;
7452               used_at = 1;
7453             }
7454           else
7455             {
7456               tempreg = dreg;
7457               used_at = 0;
7458             }
7459           macro_build ((char *) NULL, &icnt, NULL, "dnegu",
7460                        "d,w", tempreg, treg);
7461           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7462                        "d,t,s", dreg, sreg, tempreg);
7463           if (used_at)
7464             break;
7465           return;
7466         }
7467       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7468                    "d,v,t", AT, 0, treg);
7469       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7470                    "d,t,s", AT, sreg, AT);
7471       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7472                    "d,t,s", dreg, sreg, treg);
7473       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7474                    "d,v,t", dreg, dreg, AT);
7475       break;
7476
7477     case M_ROL:
7478       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7479         {
7480           if (dreg == sreg)
7481             {
7482               tempreg = AT;
7483               used_at = 1;
7484             }
7485           else
7486             {
7487               tempreg = dreg;
7488               used_at = 0;
7489             }
7490           macro_build ((char *) NULL, &icnt, NULL, "negu",
7491                        "d,w", tempreg, treg);
7492           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7493                        "d,t,s", dreg, sreg, tempreg);
7494           if (used_at)
7495             break;
7496           return;
7497         }
7498       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7499                    "d,v,t", AT, 0, treg);
7500       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7501                    "d,t,s", AT, sreg, AT);
7502       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7503                    "d,t,s", dreg, sreg, treg);
7504       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7505                    "d,v,t", dreg, dreg, AT);
7506       break;
7507
7508     case M_DROL_I:
7509       {
7510         unsigned int rot;
7511         char *l, *r;
7512
7513         if (imm_expr.X_op != O_constant)
7514           as_bad (_("Improper rotate count"));
7515         rot = imm_expr.X_add_number & 0x3f;
7516         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7517           {
7518             rot = (64 - rot) & 0x3f;
7519             if (rot >= 32)
7520               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7521                            "d,w,<", dreg, sreg, rot - 32);
7522             else
7523               macro_build ((char *) NULL, &icnt, NULL, "dror",
7524                            "d,w,<", dreg, sreg, rot);
7525             return;
7526           }
7527         if (rot == 0)
7528           {
7529             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7530                          "d,w,<", dreg, sreg, 0);
7531             return;
7532           }
7533         l = (rot < 0x20) ? "dsll" : "dsll32";
7534         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7535         rot &= 0x1f;
7536         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7537                      "d,w,<", AT, sreg, rot);
7538         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7539                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7540         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7541                      "d,v,t", dreg, dreg, AT);
7542       }
7543       break;
7544
7545     case M_ROL_I:
7546       {
7547         unsigned int rot;
7548
7549         if (imm_expr.X_op != O_constant)
7550           as_bad (_("Improper rotate count"));
7551         rot = imm_expr.X_add_number & 0x1f;
7552         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7553           {
7554             macro_build ((char *) NULL, &icnt, NULL, "ror",
7555                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7556             return;
7557           }
7558         if (rot == 0)
7559           {
7560             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7561                          "d,w,<", dreg, sreg, 0);
7562             return;
7563           }
7564         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7565                      "d,w,<", AT, sreg, rot);
7566         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7567                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7568         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7569                      "d,v,t", dreg, dreg, AT);
7570       }
7571       break;
7572
7573     case M_DROR:
7574       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7575         {
7576           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7577                        "d,t,s", dreg, sreg, treg);
7578           return;
7579         }
7580       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7581                    "d,v,t", AT, 0, treg);
7582       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7583                    "d,t,s", AT, sreg, AT);
7584       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7585                    "d,t,s", dreg, sreg, treg);
7586       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7587                    "d,v,t", dreg, dreg, AT);
7588       break;
7589
7590     case M_ROR:
7591       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7592         {
7593           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7594                        "d,t,s", dreg, sreg, treg);
7595           return;
7596         }
7597       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7598                    "d,v,t", AT, 0, treg);
7599       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7600                    "d,t,s", AT, sreg, AT);
7601       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7602                    "d,t,s", dreg, sreg, treg);
7603       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7604                    "d,v,t", dreg, dreg, AT);
7605       break;
7606
7607     case M_DROR_I:
7608       {
7609         unsigned int rot;
7610         char *l, *r;
7611
7612         if (imm_expr.X_op != O_constant)
7613           as_bad (_("Improper rotate count"));
7614         rot = imm_expr.X_add_number & 0x3f;
7615         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7616           {
7617             if (rot >= 32)
7618               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7619                            "d,w,<", dreg, sreg, rot - 32);
7620             else
7621               macro_build ((char *) NULL, &icnt, NULL, "dror",
7622                            "d,w,<", dreg, sreg, rot);
7623             return;
7624           }
7625         if (rot == 0)
7626           {
7627             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7628                          "d,w,<", dreg, sreg, 0);
7629             return;
7630           }
7631         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7632         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7633         rot &= 0x1f;
7634         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7635                      "d,w,<", AT, sreg, rot);
7636         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7637                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7638         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7639                      "d,v,t", dreg, dreg, AT);
7640       }
7641       break;
7642
7643     case M_ROR_I:
7644       {
7645         unsigned int rot;
7646
7647         if (imm_expr.X_op != O_constant)
7648           as_bad (_("Improper rotate count"));
7649         rot = imm_expr.X_add_number & 0x1f;
7650         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7651           {
7652             macro_build ((char *) NULL, &icnt, NULL, "ror",
7653                          "d,w,<", dreg, sreg, rot);
7654             return;
7655           }
7656         if (rot == 0)
7657           {
7658             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7659                          "d,w,<", dreg, sreg, 0);
7660             return;
7661           }
7662         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7663                      "d,w,<", AT, sreg, rot);
7664         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7665                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7666         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7667                      "d,v,t", dreg, dreg, AT);
7668       }
7669       break;
7670
7671     case M_S_DOB:
7672       if (mips_arch == CPU_R4650)
7673         {
7674           as_bad (_("opcode not supported on this processor"));
7675           return;
7676         }
7677       assert (mips_opts.isa == ISA_MIPS1);
7678       /* Even on a big endian machine $fn comes before $fn+1.  We have
7679          to adjust when storing to memory.  */
7680       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7681                    target_big_endian ? treg + 1 : treg,
7682                    (int) BFD_RELOC_LO16, breg);
7683       offset_expr.X_add_number += 4;
7684       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7685                    target_big_endian ? treg : treg + 1,
7686                    (int) BFD_RELOC_LO16, breg);
7687       return;
7688
7689     case M_SEQ:
7690       if (sreg == 0)
7691         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7692                      treg, (int) BFD_RELOC_LO16);
7693       else if (treg == 0)
7694         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7695                      sreg, (int) BFD_RELOC_LO16);
7696       else
7697         {
7698           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7699                        "d,v,t", dreg, sreg, treg);
7700           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7701                        dreg, (int) BFD_RELOC_LO16);
7702         }
7703       return;
7704
7705     case M_SEQ_I:
7706       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7707         {
7708           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7709                        sreg, (int) BFD_RELOC_LO16);
7710           return;
7711         }
7712       if (sreg == 0)
7713         {
7714           as_warn (_("Instruction %s: result is always false"),
7715                    ip->insn_mo->name);
7716           move_register (&icnt, dreg, 0);
7717           return;
7718         }
7719       if (imm_expr.X_op == O_constant
7720           && imm_expr.X_add_number >= 0
7721           && imm_expr.X_add_number < 0x10000)
7722         {
7723           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7724                        sreg, (int) BFD_RELOC_LO16);
7725           used_at = 0;
7726         }
7727       else if (imm_expr.X_op == O_constant
7728                && imm_expr.X_add_number > -0x8000
7729                && imm_expr.X_add_number < 0)
7730         {
7731           imm_expr.X_add_number = -imm_expr.X_add_number;
7732           macro_build ((char *) NULL, &icnt, &imm_expr,
7733                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7734                        "t,r,j", dreg, sreg,
7735                        (int) BFD_RELOC_LO16);
7736           used_at = 0;
7737         }
7738       else
7739         {
7740           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7741           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7742                        "d,v,t", dreg, sreg, AT);
7743           used_at = 1;
7744         }
7745       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7746                    (int) BFD_RELOC_LO16);
7747       if (used_at)
7748         break;
7749       return;
7750
7751     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7752       s = "slt";
7753       goto sge;
7754     case M_SGEU:
7755       s = "sltu";
7756     sge:
7757       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7758                    dreg, sreg, treg);
7759       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7760                    (int) BFD_RELOC_LO16);
7761       return;
7762
7763     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7764     case M_SGEU_I:
7765       if (imm_expr.X_op == O_constant
7766           && imm_expr.X_add_number >= -0x8000
7767           && imm_expr.X_add_number < 0x8000)
7768         {
7769           macro_build ((char *) NULL, &icnt, &imm_expr,
7770                        mask == M_SGE_I ? "slti" : "sltiu",
7771                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7772           used_at = 0;
7773         }
7774       else
7775         {
7776           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7777           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7778                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7779                        AT);
7780           used_at = 1;
7781         }
7782       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7783                    (int) BFD_RELOC_LO16);
7784       if (used_at)
7785         break;
7786       return;
7787
7788     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7789       s = "slt";
7790       goto sgt;
7791     case M_SGTU:
7792       s = "sltu";
7793     sgt:
7794       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7795                    dreg, treg, sreg);
7796       return;
7797
7798     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7799       s = "slt";
7800       goto sgti;
7801     case M_SGTU_I:
7802       s = "sltu";
7803     sgti:
7804       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7805       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7806                    dreg, AT, sreg);
7807       break;
7808
7809     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7810       s = "slt";
7811       goto sle;
7812     case M_SLEU:
7813       s = "sltu";
7814     sle:
7815       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7816                    dreg, treg, sreg);
7817       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7818                    (int) BFD_RELOC_LO16);
7819       return;
7820
7821     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7822       s = "slt";
7823       goto slei;
7824     case M_SLEU_I:
7825       s = "sltu";
7826     slei:
7827       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7828       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7829                    dreg, AT, sreg);
7830       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7831                    (int) BFD_RELOC_LO16);
7832       break;
7833
7834     case M_SLT_I:
7835       if (imm_expr.X_op == O_constant
7836           && imm_expr.X_add_number >= -0x8000
7837           && imm_expr.X_add_number < 0x8000)
7838         {
7839           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7840                        dreg, sreg, (int) BFD_RELOC_LO16);
7841           return;
7842         }
7843       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7844       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7845                    dreg, sreg, AT);
7846       break;
7847
7848     case M_SLTU_I:
7849       if (imm_expr.X_op == O_constant
7850           && imm_expr.X_add_number >= -0x8000
7851           && imm_expr.X_add_number < 0x8000)
7852         {
7853           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7854                        dreg, sreg, (int) BFD_RELOC_LO16);
7855           return;
7856         }
7857       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7858       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7859                    "d,v,t", dreg, sreg, AT);
7860       break;
7861
7862     case M_SNE:
7863       if (sreg == 0)
7864         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7865                      "d,v,t", dreg, 0, treg);
7866       else if (treg == 0)
7867         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7868                      "d,v,t", dreg, 0, sreg);
7869       else
7870         {
7871           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7872                        "d,v,t", dreg, sreg, treg);
7873           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7874                        "d,v,t", dreg, 0, dreg);
7875         }
7876       return;
7877
7878     case M_SNE_I:
7879       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7880         {
7881           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7882                        "d,v,t", dreg, 0, sreg);
7883           return;
7884         }
7885       if (sreg == 0)
7886         {
7887           as_warn (_("Instruction %s: result is always true"),
7888                    ip->insn_mo->name);
7889           macro_build ((char *) NULL, &icnt, &expr1,
7890                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7891                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7892           return;
7893         }
7894       if (imm_expr.X_op == O_constant
7895           && imm_expr.X_add_number >= 0
7896           && imm_expr.X_add_number < 0x10000)
7897         {
7898           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7899                        dreg, sreg, (int) BFD_RELOC_LO16);
7900           used_at = 0;
7901         }
7902       else if (imm_expr.X_op == O_constant
7903                && imm_expr.X_add_number > -0x8000
7904                && imm_expr.X_add_number < 0)
7905         {
7906           imm_expr.X_add_number = -imm_expr.X_add_number;
7907           macro_build ((char *) NULL, &icnt, &imm_expr,
7908                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7909                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7910           used_at = 0;
7911         }
7912       else
7913         {
7914           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7915           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7916                        "d,v,t", dreg, sreg, AT);
7917           used_at = 1;
7918         }
7919       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7920                    "d,v,t", dreg, 0, dreg);
7921       if (used_at)
7922         break;
7923       return;
7924
7925     case M_DSUB_I:
7926       dbl = 1;
7927     case M_SUB_I:
7928       if (imm_expr.X_op == O_constant
7929           && imm_expr.X_add_number > -0x8000
7930           && imm_expr.X_add_number <= 0x8000)
7931         {
7932           imm_expr.X_add_number = -imm_expr.X_add_number;
7933           macro_build ((char *) NULL, &icnt, &imm_expr,
7934                        dbl ? "daddi" : "addi",
7935                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7936           return;
7937         }
7938       load_register (&icnt, AT, &imm_expr, dbl);
7939       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7940                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7941       break;
7942
7943     case M_DSUBU_I:
7944       dbl = 1;
7945     case M_SUBU_I:
7946       if (imm_expr.X_op == O_constant
7947           && imm_expr.X_add_number > -0x8000
7948           && imm_expr.X_add_number <= 0x8000)
7949         {
7950           imm_expr.X_add_number = -imm_expr.X_add_number;
7951           macro_build ((char *) NULL, &icnt, &imm_expr,
7952                        dbl ? "daddiu" : "addiu",
7953                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7954           return;
7955         }
7956       load_register (&icnt, AT, &imm_expr, dbl);
7957       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7958                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7959       break;
7960
7961     case M_TEQ_I:
7962       s = "teq";
7963       goto trap;
7964     case M_TGE_I:
7965       s = "tge";
7966       goto trap;
7967     case M_TGEU_I:
7968       s = "tgeu";
7969       goto trap;
7970     case M_TLT_I:
7971       s = "tlt";
7972       goto trap;
7973     case M_TLTU_I:
7974       s = "tltu";
7975       goto trap;
7976     case M_TNE_I:
7977       s = "tne";
7978     trap:
7979       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7980       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7981                    AT);
7982       break;
7983
7984     case M_TRUNCWS:
7985     case M_TRUNCWD:
7986       assert (mips_opts.isa == ISA_MIPS1);
7987       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7988       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7989
7990       /*
7991        * Is the double cfc1 instruction a bug in the mips assembler;
7992        * or is there a reason for it?
7993        */
7994       mips_emit_delays (TRUE);
7995       ++mips_opts.noreorder;
7996       mips_any_noreorder = 1;
7997       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7998                    treg, RA);
7999       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
8000                    treg, RA);
8001       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8002       expr1.X_add_number = 3;
8003       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
8004                    (int) BFD_RELOC_LO16);
8005       expr1.X_add_number = 2;
8006       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
8007                      (int) BFD_RELOC_LO16);
8008       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
8009                    AT, RA);
8010       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8011       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8012               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
8013       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
8014                    treg, RA);
8015       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8016       --mips_opts.noreorder;
8017       break;
8018
8019     case M_ULH:
8020       s = "lb";
8021       goto ulh;
8022     case M_ULHU:
8023       s = "lbu";
8024     ulh:
8025       if (offset_expr.X_add_number >= 0x7fff)
8026         as_bad (_("operand overflow"));
8027       if (! target_big_endian)
8028         ++offset_expr.X_add_number;
8029       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
8030                    (int) BFD_RELOC_LO16, breg);
8031       if (! target_big_endian)
8032         --offset_expr.X_add_number;
8033       else
8034         ++offset_expr.X_add_number;
8035       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
8036                    (int) BFD_RELOC_LO16, breg);
8037       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8038                    AT, AT, 8);
8039       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8040                    treg, treg, AT);
8041       break;
8042
8043     case M_ULD:
8044       s = "ldl";
8045       s2 = "ldr";
8046       off = 7;
8047       goto ulw;
8048     case M_ULW:
8049       s = "lwl";
8050       s2 = "lwr";
8051       off = 3;
8052     ulw:
8053       if (offset_expr.X_add_number >= 0x8000 - off)
8054         as_bad (_("operand overflow"));
8055       if (treg != breg)
8056         tempreg = treg;
8057       else
8058         tempreg = AT;
8059       if (! target_big_endian)
8060         offset_expr.X_add_number += off;
8061       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
8062                    (int) BFD_RELOC_LO16, breg);
8063       if (! target_big_endian)
8064         offset_expr.X_add_number -= off;
8065       else
8066         offset_expr.X_add_number += off;
8067       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
8068                    (int) BFD_RELOC_LO16, breg);
8069
8070       /* If necessary, move the result in tempreg the final destination.  */
8071       if (treg == tempreg)
8072         return;
8073       /* Protect second load's delay slot.  */
8074       if (!gpr_interlocks)
8075         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8076       move_register (&icnt, treg, tempreg);
8077       break;
8078
8079     case M_ULD_A:
8080       s = "ldl";
8081       s2 = "ldr";
8082       off = 7;
8083       goto ulwa;
8084     case M_ULW_A:
8085       s = "lwl";
8086       s2 = "lwr";
8087       off = 3;
8088     ulwa:
8089       used_at = 1;
8090       load_address (&icnt, AT, &offset_expr, &used_at);
8091       if (breg != 0)
8092         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8093                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8094                      ? "add" : "addu" : "daddu",
8095                      "d,v,t", AT, AT, breg);
8096       if (! target_big_endian)
8097         expr1.X_add_number = off;
8098       else
8099         expr1.X_add_number = 0;
8100       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8101                    (int) BFD_RELOC_LO16, AT);
8102       if (! target_big_endian)
8103         expr1.X_add_number = 0;
8104       else
8105         expr1.X_add_number = off;
8106       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8107                    (int) BFD_RELOC_LO16, AT);
8108       break;
8109
8110     case M_ULH_A:
8111     case M_ULHU_A:
8112       used_at = 1;
8113       load_address (&icnt, AT, &offset_expr, &used_at);
8114       if (breg != 0)
8115         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8116                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8117                      ? "add" : "addu" : "daddu",
8118                      "d,v,t", AT, AT, breg);
8119       if (target_big_endian)
8120         expr1.X_add_number = 0;
8121       macro_build ((char *) NULL, &icnt, &expr1,
8122                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
8123                    (int) BFD_RELOC_LO16, AT);
8124       if (target_big_endian)
8125         expr1.X_add_number = 1;
8126       else
8127         expr1.X_add_number = 0;
8128       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8129                    (int) BFD_RELOC_LO16, AT);
8130       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8131                    treg, treg, 8);
8132       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8133                    treg, treg, AT);
8134       break;
8135
8136     case M_USH:
8137       if (offset_expr.X_add_number >= 0x7fff)
8138         as_bad (_("operand overflow"));
8139       if (target_big_endian)
8140         ++offset_expr.X_add_number;
8141       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
8142                    (int) BFD_RELOC_LO16, breg);
8143       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8144                    AT, treg, 8);
8145       if (target_big_endian)
8146         --offset_expr.X_add_number;
8147       else
8148         ++offset_expr.X_add_number;
8149       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
8150                    (int) BFD_RELOC_LO16, breg);
8151       break;
8152
8153     case M_USD:
8154       s = "sdl";
8155       s2 = "sdr";
8156       off = 7;
8157       goto usw;
8158     case M_USW:
8159       s = "swl";
8160       s2 = "swr";
8161       off = 3;
8162     usw:
8163       if (offset_expr.X_add_number >= 0x8000 - off)
8164         as_bad (_("operand overflow"));
8165       if (! target_big_endian)
8166         offset_expr.X_add_number += off;
8167       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
8168                    (int) BFD_RELOC_LO16, breg);
8169       if (! target_big_endian)
8170         offset_expr.X_add_number -= off;
8171       else
8172         offset_expr.X_add_number += off;
8173       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
8174                    (int) BFD_RELOC_LO16, breg);
8175       return;
8176
8177     case M_USD_A:
8178       s = "sdl";
8179       s2 = "sdr";
8180       off = 7;
8181       goto uswa;
8182     case M_USW_A:
8183       s = "swl";
8184       s2 = "swr";
8185       off = 3;
8186     uswa:
8187       used_at = 1;
8188       load_address (&icnt, AT, &offset_expr, &used_at);
8189       if (breg != 0)
8190         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8191                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8192                      ? "add" : "addu" : "daddu",
8193                      "d,v,t", AT, AT, breg);
8194       if (! target_big_endian)
8195         expr1.X_add_number = off;
8196       else
8197         expr1.X_add_number = 0;
8198       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8199                    (int) BFD_RELOC_LO16, AT);
8200       if (! target_big_endian)
8201         expr1.X_add_number = 0;
8202       else
8203         expr1.X_add_number = off;
8204       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8205                    (int) BFD_RELOC_LO16, AT);
8206       break;
8207
8208     case M_USH_A:
8209       used_at = 1;
8210       load_address (&icnt, AT, &offset_expr, &used_at);
8211       if (breg != 0)
8212         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8213                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8214                      ? "add" : "addu" : "daddu",
8215                      "d,v,t", AT, AT, breg);
8216       if (! target_big_endian)
8217         expr1.X_add_number = 0;
8218       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8219                    (int) BFD_RELOC_LO16, AT);
8220       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8221                    treg, treg, 8);
8222       if (! target_big_endian)
8223         expr1.X_add_number = 1;
8224       else
8225         expr1.X_add_number = 0;
8226       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8227                    (int) BFD_RELOC_LO16, AT);
8228       if (! target_big_endian)
8229         expr1.X_add_number = 0;
8230       else
8231         expr1.X_add_number = 1;
8232       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8233                    (int) BFD_RELOC_LO16, AT);
8234       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8235                    treg, treg, 8);
8236       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8237                    treg, treg, AT);
8238       break;
8239
8240     default:
8241       /* FIXME: Check if this is one of the itbl macros, since they
8242          are added dynamically.  */
8243       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
8244       break;
8245     }
8246   if (mips_opts.noat)
8247     as_warn (_("Macro used $at after \".set noat\""));
8248 }
8249
8250 /* Implement macros in mips16 mode.  */
8251
8252 static void
8253 mips16_macro (ip)
8254      struct mips_cl_insn *ip;
8255 {
8256   int mask;
8257   int xreg, yreg, zreg, tmp;
8258   int icnt;
8259   expressionS expr1;
8260   int dbl;
8261   const char *s, *s2, *s3;
8262
8263   mask = ip->insn_mo->mask;
8264
8265   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
8266   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
8267   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
8268
8269   icnt = 0;
8270
8271   expr1.X_op = O_constant;
8272   expr1.X_op_symbol = NULL;
8273   expr1.X_add_symbol = NULL;
8274   expr1.X_add_number = 1;
8275
8276   dbl = 0;
8277
8278   switch (mask)
8279     {
8280     default:
8281       internalError ();
8282
8283     case M_DDIV_3:
8284       dbl = 1;
8285     case M_DIV_3:
8286       s = "mflo";
8287       goto do_div3;
8288     case M_DREM_3:
8289       dbl = 1;
8290     case M_REM_3:
8291       s = "mfhi";
8292     do_div3:
8293       mips_emit_delays (TRUE);
8294       ++mips_opts.noreorder;
8295       mips_any_noreorder = 1;
8296       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8297                    dbl ? "ddiv" : "div",
8298                    "0,x,y", xreg, yreg);
8299       expr1.X_add_number = 2;
8300       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8301       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
8302                    7);
8303
8304       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8305          since that causes an overflow.  We should do that as well,
8306          but I don't see how to do the comparisons without a temporary
8307          register.  */
8308       --mips_opts.noreorder;
8309       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
8310       break;
8311
8312     case M_DIVU_3:
8313       s = "divu";
8314       s2 = "mflo";
8315       goto do_divu3;
8316     case M_REMU_3:
8317       s = "divu";
8318       s2 = "mfhi";
8319       goto do_divu3;
8320     case M_DDIVU_3:
8321       s = "ddivu";
8322       s2 = "mflo";
8323       goto do_divu3;
8324     case M_DREMU_3:
8325       s = "ddivu";
8326       s2 = "mfhi";
8327     do_divu3:
8328       mips_emit_delays (TRUE);
8329       ++mips_opts.noreorder;
8330       mips_any_noreorder = 1;
8331       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
8332                    xreg, yreg);
8333       expr1.X_add_number = 2;
8334       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8335       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
8336                    "6", 7);
8337       --mips_opts.noreorder;
8338       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
8339       break;
8340
8341     case M_DMUL:
8342       dbl = 1;
8343     case M_MUL:
8344       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8345                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
8346       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
8347                    zreg);
8348       return;
8349
8350     case M_DSUBU_I:
8351       dbl = 1;
8352       goto do_subu;
8353     case M_SUBU_I:
8354     do_subu:
8355       if (imm_expr.X_op != O_constant)
8356         as_bad (_("Unsupported large constant"));
8357       imm_expr.X_add_number = -imm_expr.X_add_number;
8358       macro_build ((char *) NULL, &icnt, &imm_expr,
8359                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
8360       break;
8361
8362     case M_SUBU_I_2:
8363       if (imm_expr.X_op != O_constant)
8364         as_bad (_("Unsupported large constant"));
8365       imm_expr.X_add_number = -imm_expr.X_add_number;
8366       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
8367                    "x,k", xreg);
8368       break;
8369
8370     case M_DSUBU_I_2:
8371       if (imm_expr.X_op != O_constant)
8372         as_bad (_("Unsupported large constant"));
8373       imm_expr.X_add_number = -imm_expr.X_add_number;
8374       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
8375                    "y,j", yreg);
8376       break;
8377
8378     case M_BEQ:
8379       s = "cmp";
8380       s2 = "bteqz";
8381       goto do_branch;
8382     case M_BNE:
8383       s = "cmp";
8384       s2 = "btnez";
8385       goto do_branch;
8386     case M_BLT:
8387       s = "slt";
8388       s2 = "btnez";
8389       goto do_branch;
8390     case M_BLTU:
8391       s = "sltu";
8392       s2 = "btnez";
8393       goto do_branch;
8394     case M_BLE:
8395       s = "slt";
8396       s2 = "bteqz";
8397       goto do_reverse_branch;
8398     case M_BLEU:
8399       s = "sltu";
8400       s2 = "bteqz";
8401       goto do_reverse_branch;
8402     case M_BGE:
8403       s = "slt";
8404       s2 = "bteqz";
8405       goto do_branch;
8406     case M_BGEU:
8407       s = "sltu";
8408       s2 = "bteqz";
8409       goto do_branch;
8410     case M_BGT:
8411       s = "slt";
8412       s2 = "btnez";
8413       goto do_reverse_branch;
8414     case M_BGTU:
8415       s = "sltu";
8416       s2 = "btnez";
8417
8418     do_reverse_branch:
8419       tmp = xreg;
8420       xreg = yreg;
8421       yreg = tmp;
8422
8423     do_branch:
8424       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
8425                    xreg, yreg);
8426       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8427       break;
8428
8429     case M_BEQ_I:
8430       s = "cmpi";
8431       s2 = "bteqz";
8432       s3 = "x,U";
8433       goto do_branch_i;
8434     case M_BNE_I:
8435       s = "cmpi";
8436       s2 = "btnez";
8437       s3 = "x,U";
8438       goto do_branch_i;
8439     case M_BLT_I:
8440       s = "slti";
8441       s2 = "btnez";
8442       s3 = "x,8";
8443       goto do_branch_i;
8444     case M_BLTU_I:
8445       s = "sltiu";
8446       s2 = "btnez";
8447       s3 = "x,8";
8448       goto do_branch_i;
8449     case M_BLE_I:
8450       s = "slti";
8451       s2 = "btnez";
8452       s3 = "x,8";
8453       goto do_addone_branch_i;
8454     case M_BLEU_I:
8455       s = "sltiu";
8456       s2 = "btnez";
8457       s3 = "x,8";
8458       goto do_addone_branch_i;
8459     case M_BGE_I:
8460       s = "slti";
8461       s2 = "bteqz";
8462       s3 = "x,8";
8463       goto do_branch_i;
8464     case M_BGEU_I:
8465       s = "sltiu";
8466       s2 = "bteqz";
8467       s3 = "x,8";
8468       goto do_branch_i;
8469     case M_BGT_I:
8470       s = "slti";
8471       s2 = "bteqz";
8472       s3 = "x,8";
8473       goto do_addone_branch_i;
8474     case M_BGTU_I:
8475       s = "sltiu";
8476       s2 = "bteqz";
8477       s3 = "x,8";
8478
8479     do_addone_branch_i:
8480       if (imm_expr.X_op != O_constant)
8481         as_bad (_("Unsupported large constant"));
8482       ++imm_expr.X_add_number;
8483
8484     do_branch_i:
8485       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8486       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8487       break;
8488
8489     case M_ABS:
8490       expr1.X_add_number = 0;
8491       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
8492       if (xreg != yreg)
8493         move_register (&icnt, xreg, yreg);
8494       expr1.X_add_number = 2;
8495       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8496       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8497                    "neg", "x,w", xreg, xreg);
8498     }
8499 }
8500
8501 /* For consistency checking, verify that all bits are specified either
8502    by the match/mask part of the instruction definition, or by the
8503    operand list.  */
8504 static int
8505 validate_mips_insn (opc)
8506      const struct mips_opcode *opc;
8507 {
8508   const char *p = opc->args;
8509   char c;
8510   unsigned long used_bits = opc->mask;
8511
8512   if ((used_bits & opc->match) != opc->match)
8513     {
8514       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8515               opc->name, opc->args);
8516       return 0;
8517     }
8518 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8519   while (*p)
8520     switch (c = *p++)
8521       {
8522       case ',': break;
8523       case '(': break;
8524       case ')': break;
8525       case '+':
8526         switch (c = *p++)
8527           {
8528           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8529           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8530           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8531           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8532                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8533           default:
8534             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8535                     c, opc->name, opc->args);
8536             return 0;
8537           }
8538         break;
8539       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8540       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8541       case 'A': break;
8542       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8543       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8544       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8545       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8546       case 'F': break;
8547       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8548       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8549       case 'I': break;
8550       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8551       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8552       case 'L': break;
8553       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8554       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8555       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8556       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8557                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8558       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8559       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8560       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8561       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8562       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8563       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8564       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8565       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8566       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8567       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8568       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8569       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8570       case 'f': break;
8571       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8572       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8573       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8574       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8575       case 'l': break;
8576       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8577       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8578       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8579       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8580       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8581       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8582       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8583       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8584       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8585       case 'x': break;
8586       case 'z': break;
8587       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8588       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8589                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8590       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8591       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8592       case '[': break;
8593       case ']': break;
8594       default:
8595         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8596                 c, opc->name, opc->args);
8597         return 0;
8598       }
8599 #undef USE_BITS
8600   if (used_bits != 0xffffffff)
8601     {
8602       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8603               ~used_bits & 0xffffffff, opc->name, opc->args);
8604       return 0;
8605     }
8606   return 1;
8607 }
8608
8609 /* This routine assembles an instruction into its binary format.  As a
8610    side effect, it sets one of the global variables imm_reloc or
8611    offset_reloc to the type of relocation to do if one of the operands
8612    is an address expression.  */
8613
8614 static void
8615 mips_ip (str, ip)
8616      char *str;
8617      struct mips_cl_insn *ip;
8618 {
8619   char *s;
8620   const char *args;
8621   char c = 0;
8622   struct mips_opcode *insn;
8623   char *argsStart;
8624   unsigned int regno;
8625   unsigned int lastregno = 0;
8626   unsigned int lastpos = 0;
8627   unsigned int limlo, limhi;
8628   char *s_reset;
8629   char save_c = 0;
8630
8631   insn_error = NULL;
8632
8633   /* If the instruction contains a '.', we first try to match an instruction
8634      including the '.'.  Then we try again without the '.'.  */
8635   insn = NULL;
8636   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8637     continue;
8638
8639   /* If we stopped on whitespace, then replace the whitespace with null for
8640      the call to hash_find.  Save the character we replaced just in case we
8641      have to re-parse the instruction.  */
8642   if (ISSPACE (*s))
8643     {
8644       save_c = *s;
8645       *s++ = '\0';
8646     }
8647
8648   insn = (struct mips_opcode *) hash_find (op_hash, str);
8649
8650   /* If we didn't find the instruction in the opcode table, try again, but
8651      this time with just the instruction up to, but not including the
8652      first '.'.  */
8653   if (insn == NULL)
8654     {
8655       /* Restore the character we overwrite above (if any).  */
8656       if (save_c)
8657         *(--s) = save_c;
8658
8659       /* Scan up to the first '.' or whitespace.  */
8660       for (s = str;
8661            *s != '\0' && *s != '.' && !ISSPACE (*s);
8662            ++s)
8663         continue;
8664
8665       /* If we did not find a '.', then we can quit now.  */
8666       if (*s != '.')
8667         {
8668           insn_error = "unrecognized opcode";
8669           return;
8670         }
8671
8672       /* Lookup the instruction in the hash table.  */
8673       *s++ = '\0';
8674       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8675         {
8676           insn_error = "unrecognized opcode";
8677           return;
8678         }
8679     }
8680
8681   argsStart = s;
8682   for (;;)
8683     {
8684       bfd_boolean ok;
8685
8686       assert (strcmp (insn->name, str) == 0);
8687
8688       if (OPCODE_IS_MEMBER (insn,
8689                             (mips_opts.isa
8690                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8691                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8692                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8693                             mips_arch))
8694         ok = TRUE;
8695       else
8696         ok = FALSE;
8697
8698       if (insn->pinfo != INSN_MACRO)
8699         {
8700           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8701             ok = FALSE;
8702         }
8703
8704       if (! ok)
8705         {
8706           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8707               && strcmp (insn->name, insn[1].name) == 0)
8708             {
8709               ++insn;
8710               continue;
8711             }
8712           else
8713             {
8714               if (!insn_error)
8715                 {
8716                   static char buf[100];
8717                   if (mips_arch_info->is_isa)
8718                     sprintf (buf,
8719                              _("opcode not supported at this ISA level (%s)"),
8720                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8721                   else
8722                     sprintf (buf,
8723                              _("opcode not supported on this processor: %s (%s)"),
8724                              mips_arch_info->name,
8725                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8726                   insn_error = buf;
8727                 }
8728               if (save_c)
8729                 *(--s) = save_c;
8730               return;
8731             }
8732         }
8733
8734       ip->insn_mo = insn;
8735       ip->insn_opcode = insn->match;
8736       insn_error = NULL;
8737       for (args = insn->args;; ++args)
8738         {
8739           int is_mdmx;
8740
8741           s += strspn (s, " \t");
8742           is_mdmx = 0;
8743           switch (*args)
8744             {
8745             case '\0':          /* end of args */
8746               if (*s == '\0')
8747                 return;
8748               break;
8749
8750             case ',':
8751               if (*s++ == *args)
8752                 continue;
8753               s--;
8754               switch (*++args)
8755                 {
8756                 case 'r':
8757                 case 'v':
8758                   ip->insn_opcode |= lastregno << OP_SH_RS;
8759                   continue;
8760
8761                 case 'w':
8762                   ip->insn_opcode |= lastregno << OP_SH_RT;
8763                   continue;
8764
8765                 case 'W':
8766                   ip->insn_opcode |= lastregno << OP_SH_FT;
8767                   continue;
8768
8769                 case 'V':
8770                   ip->insn_opcode |= lastregno << OP_SH_FS;
8771                   continue;
8772                 }
8773               break;
8774
8775             case '(':
8776               /* Handle optional base register.
8777                  Either the base register is omitted or
8778                  we must have a left paren.  */
8779               /* This is dependent on the next operand specifier
8780                  is a base register specification.  */
8781               assert (args[1] == 'b' || args[1] == '5'
8782                       || args[1] == '-' || args[1] == '4');
8783               if (*s == '\0')
8784                 return;
8785
8786             case ')':           /* these must match exactly */
8787             case '[':
8788             case ']':
8789               if (*s++ == *args)
8790                 continue;
8791               break;
8792
8793             case '+':           /* Opcode extension character.  */
8794               switch (*++args)
8795                 {
8796                 case 'A':               /* ins/ext position, becomes LSB.  */
8797                   limlo = 0;
8798                   limhi = 31;
8799                   my_getExpression (&imm_expr, s);
8800                   check_absolute_expr (ip, &imm_expr);
8801                   if ((unsigned long) imm_expr.X_add_number < limlo
8802                       || (unsigned long) imm_expr.X_add_number > limhi)
8803                     {
8804                       as_bad (_("Improper position (%lu)"),
8805                               (unsigned long) imm_expr.X_add_number);
8806                       imm_expr.X_add_number = limlo;
8807                     }
8808                   lastpos = imm_expr.X_add_number;
8809                   ip->insn_opcode |= (imm_expr.X_add_number
8810                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8811                   imm_expr.X_op = O_absent;
8812                   s = expr_end;
8813                   continue;
8814
8815                 case 'B':               /* ins size, becomes MSB.  */
8816                   limlo = 1;
8817                   limhi = 32;
8818                   my_getExpression (&imm_expr, s);
8819                   check_absolute_expr (ip, &imm_expr);
8820                   /* Check for negative input so that small negative numbers
8821                      will not succeed incorrectly.  The checks against
8822                      (pos+size) transitively check "size" itself,
8823                      assuming that "pos" is reasonable.  */
8824                   if ((long) imm_expr.X_add_number < 0
8825                       || ((unsigned long) imm_expr.X_add_number
8826                           + lastpos) < limlo
8827                       || ((unsigned long) imm_expr.X_add_number
8828                           + lastpos) > limhi)
8829                     {
8830                       as_bad (_("Improper insert size (%lu, position %lu)"),
8831                               (unsigned long) imm_expr.X_add_number,
8832                               (unsigned long) lastpos);
8833                       imm_expr.X_add_number = limlo - lastpos;
8834                     }
8835                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8836                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8837                   imm_expr.X_op = O_absent;
8838                   s = expr_end;
8839                   continue;
8840
8841                 case 'C':               /* ext size, becomes MSBD.  */
8842                   limlo = 1;
8843                   limhi = 32;
8844                   my_getExpression (&imm_expr, s);
8845                   check_absolute_expr (ip, &imm_expr);
8846                   /* Check for negative input so that small negative numbers
8847                      will not succeed incorrectly.  The checks against
8848                      (pos+size) transitively check "size" itself,
8849                      assuming that "pos" is reasonable.  */
8850                   if ((long) imm_expr.X_add_number < 0
8851                       || ((unsigned long) imm_expr.X_add_number
8852                           + lastpos) < limlo
8853                       || ((unsigned long) imm_expr.X_add_number
8854                           + lastpos) > limhi)
8855                     {
8856                       as_bad (_("Improper extract size (%lu, position %lu)"),
8857                               (unsigned long) imm_expr.X_add_number,
8858                               (unsigned long) lastpos);
8859                       imm_expr.X_add_number = limlo - lastpos;
8860                     }
8861                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8862                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8863                   imm_expr.X_op = O_absent;
8864                   s = expr_end;
8865                   continue;
8866
8867                 case 'D':
8868                   /* +D is for disassembly only; never match.  */
8869                   break;
8870
8871                 default:
8872                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8873                     *args, insn->name, insn->args);
8874                   /* Further processing is fruitless.  */
8875                   return;
8876                 }
8877               break;
8878
8879             case '<':           /* must be at least one digit */
8880               /*
8881                * According to the manual, if the shift amount is greater
8882                * than 31 or less than 0, then the shift amount should be
8883                * mod 32.  In reality the mips assembler issues an error.
8884                * We issue a warning and mask out all but the low 5 bits.
8885                */
8886               my_getExpression (&imm_expr, s);
8887               check_absolute_expr (ip, &imm_expr);
8888               if ((unsigned long) imm_expr.X_add_number > 31)
8889                 {
8890                   as_warn (_("Improper shift amount (%lu)"),
8891                            (unsigned long) imm_expr.X_add_number);
8892                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8893                 }
8894               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8895               imm_expr.X_op = O_absent;
8896               s = expr_end;
8897               continue;
8898
8899             case '>':           /* shift amount minus 32 */
8900               my_getExpression (&imm_expr, s);
8901               check_absolute_expr (ip, &imm_expr);
8902               if ((unsigned long) imm_expr.X_add_number < 32
8903                   || (unsigned long) imm_expr.X_add_number > 63)
8904                 break;
8905               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8906               imm_expr.X_op = O_absent;
8907               s = expr_end;
8908               continue;
8909
8910             case 'k':           /* cache code */
8911             case 'h':           /* prefx code */
8912               my_getExpression (&imm_expr, s);
8913               check_absolute_expr (ip, &imm_expr);
8914               if ((unsigned long) imm_expr.X_add_number > 31)
8915                 {
8916                   as_warn (_("Invalid value for `%s' (%lu)"),
8917                            ip->insn_mo->name,
8918                            (unsigned long) imm_expr.X_add_number);
8919                   imm_expr.X_add_number &= 0x1f;
8920                 }
8921               if (*args == 'k')
8922                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8923               else
8924                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8925               imm_expr.X_op = O_absent;
8926               s = expr_end;
8927               continue;
8928
8929             case 'c':           /* break code */
8930               my_getExpression (&imm_expr, s);
8931               check_absolute_expr (ip, &imm_expr);
8932               if ((unsigned long) imm_expr.X_add_number > 1023)
8933                 {
8934                   as_warn (_("Illegal break code (%lu)"),
8935                            (unsigned long) imm_expr.X_add_number);
8936                   imm_expr.X_add_number &= OP_MASK_CODE;
8937                 }
8938               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8939               imm_expr.X_op = O_absent;
8940               s = expr_end;
8941               continue;
8942
8943             case 'q':           /* lower break code */
8944               my_getExpression (&imm_expr, s);
8945               check_absolute_expr (ip, &imm_expr);
8946               if ((unsigned long) imm_expr.X_add_number > 1023)
8947                 {
8948                   as_warn (_("Illegal lower break code (%lu)"),
8949                            (unsigned long) imm_expr.X_add_number);
8950                   imm_expr.X_add_number &= OP_MASK_CODE2;
8951                 }
8952               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8953               imm_expr.X_op = O_absent;
8954               s = expr_end;
8955               continue;
8956
8957             case 'B':           /* 20-bit syscall/break code.  */
8958               my_getExpression (&imm_expr, s);
8959               check_absolute_expr (ip, &imm_expr);
8960               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8961                 as_warn (_("Illegal 20-bit code (%lu)"),
8962                          (unsigned long) imm_expr.X_add_number);
8963               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8964               imm_expr.X_op = O_absent;
8965               s = expr_end;
8966               continue;
8967
8968             case 'C':           /* Coprocessor code */
8969               my_getExpression (&imm_expr, s);
8970               check_absolute_expr (ip, &imm_expr);
8971               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8972                 {
8973                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8974                            (unsigned long) imm_expr.X_add_number);
8975                   imm_expr.X_add_number &= ((1 << 25) - 1);
8976                 }
8977               ip->insn_opcode |= imm_expr.X_add_number;
8978               imm_expr.X_op = O_absent;
8979               s = expr_end;
8980               continue;
8981
8982             case 'J':           /* 19-bit wait code.  */
8983               my_getExpression (&imm_expr, s);
8984               check_absolute_expr (ip, &imm_expr);
8985               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8986                 as_warn (_("Illegal 19-bit code (%lu)"),
8987                          (unsigned long) imm_expr.X_add_number);
8988               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8989               imm_expr.X_op = O_absent;
8990               s = expr_end;
8991               continue;
8992
8993             case 'P':           /* Performance register */
8994               my_getExpression (&imm_expr, s);
8995               check_absolute_expr (ip, &imm_expr);
8996               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8997                 {
8998                   as_warn (_("Invalid performance register (%lu)"),
8999                            (unsigned long) imm_expr.X_add_number);
9000                   imm_expr.X_add_number &= OP_MASK_PERFREG;
9001                 }
9002               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
9003               imm_expr.X_op = O_absent;
9004               s = expr_end;
9005               continue;
9006
9007             case 'b':           /* base register */
9008             case 'd':           /* destination register */
9009             case 's':           /* source register */
9010             case 't':           /* target register */
9011             case 'r':           /* both target and source */
9012             case 'v':           /* both dest and source */
9013             case 'w':           /* both dest and target */
9014             case 'E':           /* coprocessor target register */
9015             case 'G':           /* coprocessor destination register */
9016             case 'K':           /* 'rdhwr' destination register */
9017             case 'x':           /* ignore register name */
9018             case 'z':           /* must be zero register */
9019             case 'U':           /* destination register (clo/clz).  */
9020               s_reset = s;
9021               if (s[0] == '$')
9022                 {
9023
9024                   if (ISDIGIT (s[1]))
9025                     {
9026                       ++s;
9027                       regno = 0;
9028                       do
9029                         {
9030                           regno *= 10;
9031                           regno += *s - '0';
9032                           ++s;
9033                         }
9034                       while (ISDIGIT (*s));
9035                       if (regno > 31)
9036                         as_bad (_("Invalid register number (%d)"), regno);
9037                     }
9038                   else if (*args == 'E' || *args == 'G' || *args == 'K')
9039                     goto notreg;
9040                   else
9041                     {
9042                       if (s[1] == 'r' && s[2] == 'a')
9043                         {
9044                           s += 3;
9045                           regno = RA;
9046                         }
9047                       else if (s[1] == 'f' && s[2] == 'p')
9048                         {
9049                           s += 3;
9050                           regno = FP;
9051                         }
9052                       else if (s[1] == 's' && s[2] == 'p')
9053                         {
9054                           s += 3;
9055                           regno = SP;
9056                         }
9057                       else if (s[1] == 'g' && s[2] == 'p')
9058                         {
9059                           s += 3;
9060                           regno = GP;
9061                         }
9062                       else if (s[1] == 'a' && s[2] == 't')
9063                         {
9064                           s += 3;
9065                           regno = AT;
9066                         }
9067                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9068                         {
9069                           s += 4;
9070                           regno = KT0;
9071                         }
9072                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9073                         {
9074                           s += 4;
9075                           regno = KT1;
9076                         }
9077                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9078                         {
9079                           s += 5;
9080                           regno = ZERO;
9081                         }
9082                       else if (itbl_have_entries)
9083                         {
9084                           char *p, *n;
9085                           unsigned long r;
9086
9087                           p = s + 1;    /* advance past '$' */
9088                           n = itbl_get_field (&p);  /* n is name */
9089
9090                           /* See if this is a register defined in an
9091                              itbl entry.  */
9092                           if (itbl_get_reg_val (n, &r))
9093                             {
9094                               /* Get_field advances to the start of
9095                                  the next field, so we need to back
9096                                  rack to the end of the last field.  */
9097                               if (p)
9098                                 s = p - 1;
9099                               else
9100                                 s = strchr (s, '\0');
9101                               regno = r;
9102                             }
9103                           else
9104                             goto notreg;
9105                         }
9106                       else
9107                         goto notreg;
9108                     }
9109                   if (regno == AT
9110                       && ! mips_opts.noat
9111                       && *args != 'E'
9112                       && *args != 'G'
9113                       && *args != 'K')
9114                     as_warn (_("Used $at without \".set noat\""));
9115                   c = *args;
9116                   if (*s == ' ')
9117                     ++s;
9118                   if (args[1] != *s)
9119                     {
9120                       if (c == 'r' || c == 'v' || c == 'w')
9121                         {
9122                           regno = lastregno;
9123                           s = s_reset;
9124                           ++args;
9125                         }
9126                     }
9127                   /* 'z' only matches $0.  */
9128                   if (c == 'z' && regno != 0)
9129                     break;
9130
9131         /* Now that we have assembled one operand, we use the args string
9132          * to figure out where it goes in the instruction.  */
9133                   switch (c)
9134                     {
9135                     case 'r':
9136                     case 's':
9137                     case 'v':
9138                     case 'b':
9139                       ip->insn_opcode |= regno << OP_SH_RS;
9140                       break;
9141                     case 'd':
9142                     case 'G':
9143                     case 'K':
9144                       ip->insn_opcode |= regno << OP_SH_RD;
9145                       break;
9146                     case 'U':
9147                       ip->insn_opcode |= regno << OP_SH_RD;
9148                       ip->insn_opcode |= regno << OP_SH_RT;
9149                       break;
9150                     case 'w':
9151                     case 't':
9152                     case 'E':
9153                       ip->insn_opcode |= regno << OP_SH_RT;
9154                       break;
9155                     case 'x':
9156                       /* This case exists because on the r3000 trunc
9157                          expands into a macro which requires a gp
9158                          register.  On the r6000 or r4000 it is
9159                          assembled into a single instruction which
9160                          ignores the register.  Thus the insn version
9161                          is MIPS_ISA2 and uses 'x', and the macro
9162                          version is MIPS_ISA1 and uses 't'.  */
9163                       break;
9164                     case 'z':
9165                       /* This case is for the div instruction, which
9166                          acts differently if the destination argument
9167                          is $0.  This only matches $0, and is checked
9168                          outside the switch.  */
9169                       break;
9170                     case 'D':
9171                       /* Itbl operand; not yet implemented. FIXME ?? */
9172                       break;
9173                       /* What about all other operands like 'i', which
9174                          can be specified in the opcode table? */
9175                     }
9176                   lastregno = regno;
9177                   continue;
9178                 }
9179             notreg:
9180               switch (*args++)
9181                 {
9182                 case 'r':
9183                 case 'v':
9184                   ip->insn_opcode |= lastregno << OP_SH_RS;
9185                   continue;
9186                 case 'w':
9187                   ip->insn_opcode |= lastregno << OP_SH_RT;
9188                   continue;
9189                 }
9190               break;
9191
9192             case 'O':           /* MDMX alignment immediate constant.  */
9193               my_getExpression (&imm_expr, s);
9194               check_absolute_expr (ip, &imm_expr);
9195               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
9196                 {
9197                   as_warn ("Improper align amount (%ld), using low bits",
9198                            (long) imm_expr.X_add_number);
9199                   imm_expr.X_add_number &= OP_MASK_ALN;
9200                 }
9201               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
9202               imm_expr.X_op = O_absent;
9203               s = expr_end;
9204               continue;
9205
9206             case 'Q':           /* MDMX vector, element sel, or const.  */
9207               if (s[0] != '$')
9208                 {
9209                   /* MDMX Immediate.  */
9210                   my_getExpression (&imm_expr, s);
9211                   check_absolute_expr (ip, &imm_expr);
9212                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
9213                     {
9214                       as_warn (_("Invalid MDMX Immediate (%ld)"),
9215                                (long) imm_expr.X_add_number);
9216                       imm_expr.X_add_number &= OP_MASK_FT;
9217                     }
9218                   imm_expr.X_add_number &= OP_MASK_FT;
9219                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9220                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9221                   else
9222                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
9223                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
9224                   imm_expr.X_op = O_absent;
9225                   s = expr_end;
9226                   continue;
9227                 }
9228               /* Not MDMX Immediate.  Fall through.  */
9229             case 'X':           /* MDMX destination register.  */
9230             case 'Y':           /* MDMX source register.  */
9231             case 'Z':           /* MDMX target register.  */
9232               is_mdmx = 1;
9233             case 'D':           /* floating point destination register */
9234             case 'S':           /* floating point source register */
9235             case 'T':           /* floating point target register */
9236             case 'R':           /* floating point source register */
9237             case 'V':
9238             case 'W':
9239               s_reset = s;
9240               /* Accept $fN for FP and MDMX register numbers, and in
9241                  addition accept $vN for MDMX register numbers.  */
9242               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
9243                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
9244                       && ISDIGIT (s[2])))
9245                 {
9246                   s += 2;
9247                   regno = 0;
9248                   do
9249                     {
9250                       regno *= 10;
9251                       regno += *s - '0';
9252                       ++s;
9253                     }
9254                   while (ISDIGIT (*s));
9255
9256                   if (regno > 31)
9257                     as_bad (_("Invalid float register number (%d)"), regno);
9258
9259                   if ((regno & 1) != 0
9260                       && HAVE_32BIT_FPRS
9261                       && ! (strcmp (str, "mtc1") == 0
9262                             || strcmp (str, "mfc1") == 0
9263                             || strcmp (str, "lwc1") == 0
9264                             || strcmp (str, "swc1") == 0
9265                             || strcmp (str, "l.s") == 0
9266                             || strcmp (str, "s.s") == 0))
9267                     as_warn (_("Float register should be even, was %d"),
9268                              regno);
9269
9270                   c = *args;
9271                   if (*s == ' ')
9272                     ++s;
9273                   if (args[1] != *s)
9274                     {
9275                       if (c == 'V' || c == 'W')
9276                         {
9277                           regno = lastregno;
9278                           s = s_reset;
9279                           ++args;
9280                         }
9281                     }
9282                   switch (c)
9283                     {
9284                     case 'D':
9285                     case 'X':
9286                       ip->insn_opcode |= regno << OP_SH_FD;
9287                       break;
9288                     case 'V':
9289                     case 'S':
9290                     case 'Y':
9291                       ip->insn_opcode |= regno << OP_SH_FS;
9292                       break;
9293                     case 'Q':
9294                       /* This is like 'Z', but also needs to fix the MDMX
9295                          vector/scalar select bits.  Note that the
9296                          scalar immediate case is handled above.  */
9297                       if (*s == '[')
9298                         {
9299                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9300                           int max_el = (is_qh ? 3 : 7);
9301                           s++;
9302                           my_getExpression(&imm_expr, s);
9303                           check_absolute_expr (ip, &imm_expr);
9304                           s = expr_end;
9305                           if (imm_expr.X_add_number > max_el)
9306                             as_bad(_("Bad element selector %ld"),
9307                                    (long) imm_expr.X_add_number);
9308                           imm_expr.X_add_number &= max_el;
9309                           ip->insn_opcode |= (imm_expr.X_add_number
9310                                               << (OP_SH_VSEL +
9311                                                   (is_qh ? 2 : 1)));
9312                           if (*s != ']')
9313                             as_warn(_("Expecting ']' found '%s'"), s);
9314                           else
9315                             s++;
9316                         }
9317                       else
9318                         {
9319                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9320                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9321                                                 << OP_SH_VSEL);
9322                           else
9323                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9324                                                 OP_SH_VSEL);
9325                         }
9326                       /* Fall through */
9327                     case 'W':
9328                     case 'T':
9329                     case 'Z':
9330                       ip->insn_opcode |= regno << OP_SH_FT;
9331                       break;
9332                     case 'R':
9333                       ip->insn_opcode |= regno << OP_SH_FR;
9334                       break;
9335                     }
9336                   lastregno = regno;
9337                   continue;
9338                 }
9339
9340               switch (*args++)
9341                 {
9342                 case 'V':
9343                   ip->insn_opcode |= lastregno << OP_SH_FS;
9344                   continue;
9345                 case 'W':
9346                   ip->insn_opcode |= lastregno << OP_SH_FT;
9347                   continue;
9348                 }
9349               break;
9350
9351             case 'I':
9352               my_getExpression (&imm_expr, s);
9353               if (imm_expr.X_op != O_big
9354                   && imm_expr.X_op != O_constant)
9355                 insn_error = _("absolute expression required");
9356               s = expr_end;
9357               continue;
9358
9359             case 'A':
9360               my_getExpression (&offset_expr, s);
9361               *imm_reloc = BFD_RELOC_32;
9362               s = expr_end;
9363               continue;
9364
9365             case 'F':
9366             case 'L':
9367             case 'f':
9368             case 'l':
9369               {
9370                 int f64;
9371                 int using_gprs;
9372                 char *save_in;
9373                 char *err;
9374                 unsigned char temp[8];
9375                 int len;
9376                 unsigned int length;
9377                 segT seg;
9378                 subsegT subseg;
9379                 char *p;
9380
9381                 /* These only appear as the last operand in an
9382                    instruction, and every instruction that accepts
9383                    them in any variant accepts them in all variants.
9384                    This means we don't have to worry about backing out
9385                    any changes if the instruction does not match.
9386
9387                    The difference between them is the size of the
9388                    floating point constant and where it goes.  For 'F'
9389                    and 'L' the constant is 64 bits; for 'f' and 'l' it
9390                    is 32 bits.  Where the constant is placed is based
9391                    on how the MIPS assembler does things:
9392                     F -- .rdata
9393                     L -- .lit8
9394                     f -- immediate value
9395                     l -- .lit4
9396
9397                     The .lit4 and .lit8 sections are only used if
9398                     permitted by the -G argument.
9399
9400                     When generating embedded PIC code, we use the
9401                     .lit8 section but not the .lit4 section (we can do
9402                     .lit4 inline easily; we need to put .lit8
9403                     somewhere in the data segment, and using .lit8
9404                     permits the linker to eventually combine identical
9405                     .lit8 entries).
9406
9407                     The code below needs to know whether the target register
9408                     is 32 or 64 bits wide.  It relies on the fact 'f' and
9409                     'F' are used with GPR-based instructions and 'l' and
9410                     'L' are used with FPR-based instructions.  */
9411
9412                 f64 = *args == 'F' || *args == 'L';
9413                 using_gprs = *args == 'F' || *args == 'f';
9414
9415                 save_in = input_line_pointer;
9416                 input_line_pointer = s;
9417                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9418                 length = len;
9419                 s = input_line_pointer;
9420                 input_line_pointer = save_in;
9421                 if (err != NULL && *err != '\0')
9422                   {
9423                     as_bad (_("Bad floating point constant: %s"), err);
9424                     memset (temp, '\0', sizeof temp);
9425                     length = f64 ? 8 : 4;
9426                   }
9427
9428                 assert (length == (unsigned) (f64 ? 8 : 4));
9429
9430                 if (*args == 'f'
9431                     || (*args == 'l'
9432                         && (! USE_GLOBAL_POINTER_OPT
9433                             || mips_pic == EMBEDDED_PIC
9434                             || g_switch_value < 4
9435                             || (temp[0] == 0 && temp[1] == 0)
9436                             || (temp[2] == 0 && temp[3] == 0))))
9437                   {
9438                     imm_expr.X_op = O_constant;
9439                     if (! target_big_endian)
9440                       imm_expr.X_add_number = bfd_getl32 (temp);
9441                     else
9442                       imm_expr.X_add_number = bfd_getb32 (temp);
9443                   }
9444                 else if (length > 4
9445                          && ! mips_disable_float_construction
9446                          /* Constants can only be constructed in GPRs and
9447                             copied to FPRs if the GPRs are at least as wide
9448                             as the FPRs.  Force the constant into memory if
9449                             we are using 64-bit FPRs but the GPRs are only
9450                             32 bits wide.  */
9451                          && (using_gprs
9452                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9453                          && ((temp[0] == 0 && temp[1] == 0)
9454                              || (temp[2] == 0 && temp[3] == 0))
9455                          && ((temp[4] == 0 && temp[5] == 0)
9456                              || (temp[6] == 0 && temp[7] == 0)))
9457                   {
9458                     /* The value is simple enough to load with a couple of
9459                        instructions.  If using 32-bit registers, set
9460                        imm_expr to the high order 32 bits and offset_expr to
9461                        the low order 32 bits.  Otherwise, set imm_expr to
9462                        the entire 64 bit constant.  */
9463                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9464                       {
9465                         imm_expr.X_op = O_constant;
9466                         offset_expr.X_op = O_constant;
9467                         if (! target_big_endian)
9468                           {
9469                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
9470                             offset_expr.X_add_number = bfd_getl32 (temp);
9471                           }
9472                         else
9473                           {
9474                             imm_expr.X_add_number = bfd_getb32 (temp);
9475                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9476                           }
9477                         if (offset_expr.X_add_number == 0)
9478                           offset_expr.X_op = O_absent;
9479                       }
9480                     else if (sizeof (imm_expr.X_add_number) > 4)
9481                       {
9482                         imm_expr.X_op = O_constant;
9483                         if (! target_big_endian)
9484                           imm_expr.X_add_number = bfd_getl64 (temp);
9485                         else
9486                           imm_expr.X_add_number = bfd_getb64 (temp);
9487                       }
9488                     else
9489                       {
9490                         imm_expr.X_op = O_big;
9491                         imm_expr.X_add_number = 4;
9492                         if (! target_big_endian)
9493                           {
9494                             generic_bignum[0] = bfd_getl16 (temp);
9495                             generic_bignum[1] = bfd_getl16 (temp + 2);
9496                             generic_bignum[2] = bfd_getl16 (temp + 4);
9497                             generic_bignum[3] = bfd_getl16 (temp + 6);
9498                           }
9499                         else
9500                           {
9501                             generic_bignum[0] = bfd_getb16 (temp + 6);
9502                             generic_bignum[1] = bfd_getb16 (temp + 4);
9503                             generic_bignum[2] = bfd_getb16 (temp + 2);
9504                             generic_bignum[3] = bfd_getb16 (temp);
9505                           }
9506                       }
9507                   }
9508                 else
9509                   {
9510                     const char *newname;
9511                     segT new_seg;
9512
9513                     /* Switch to the right section.  */
9514                     seg = now_seg;
9515                     subseg = now_subseg;
9516                     switch (*args)
9517                       {
9518                       default: /* unused default case avoids warnings.  */
9519                       case 'L':
9520                         newname = RDATA_SECTION_NAME;
9521                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9522                             || mips_pic == EMBEDDED_PIC)
9523                           newname = ".lit8";
9524                         break;
9525                       case 'F':
9526                         if (mips_pic == EMBEDDED_PIC)
9527                           newname = ".lit8";
9528                         else
9529                           newname = RDATA_SECTION_NAME;
9530                         break;
9531                       case 'l':
9532                         assert (!USE_GLOBAL_POINTER_OPT
9533                                 || g_switch_value >= 4);
9534                         newname = ".lit4";
9535                         break;
9536                       }
9537                     new_seg = subseg_new (newname, (subsegT) 0);
9538                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9539                       bfd_set_section_flags (stdoutput, new_seg,
9540                                              (SEC_ALLOC
9541                                               | SEC_LOAD
9542                                               | SEC_READONLY
9543                                               | SEC_DATA));
9544                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9545                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9546                         && strcmp (TARGET_OS, "elf") != 0)
9547                       record_alignment (new_seg, 4);
9548                     else
9549                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9550                     if (seg == now_seg)
9551                       as_bad (_("Can't use floating point insn in this section"));
9552
9553                     /* Set the argument to the current address in the
9554                        section.  */
9555                     offset_expr.X_op = O_symbol;
9556                     offset_expr.X_add_symbol =
9557                       symbol_new ("L0\001", now_seg,
9558                                   (valueT) frag_now_fix (), frag_now);
9559                     offset_expr.X_add_number = 0;
9560
9561                     /* Put the floating point number into the section.  */
9562                     p = frag_more ((int) length);
9563                     memcpy (p, temp, length);
9564
9565                     /* Switch back to the original section.  */
9566                     subseg_set (seg, subseg);
9567                   }
9568               }
9569               continue;
9570
9571             case 'i':           /* 16 bit unsigned immediate */
9572             case 'j':           /* 16 bit signed immediate */
9573               *imm_reloc = BFD_RELOC_LO16;
9574               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9575                 {
9576                   int more;
9577                   offsetT minval, maxval;
9578
9579                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9580                           && strcmp (insn->name, insn[1].name) == 0);
9581
9582                   /* If the expression was written as an unsigned number,
9583                      only treat it as signed if there are no more
9584                      alternatives.  */
9585                   if (more
9586                       && *args == 'j'
9587                       && sizeof (imm_expr.X_add_number) <= 4
9588                       && imm_expr.X_op == O_constant
9589                       && imm_expr.X_add_number < 0
9590                       && imm_expr.X_unsigned
9591                       && HAVE_64BIT_GPRS)
9592                     break;
9593
9594                   /* For compatibility with older assemblers, we accept
9595                      0x8000-0xffff as signed 16-bit numbers when only
9596                      signed numbers are allowed.  */
9597                   if (*args == 'i')
9598                     minval = 0, maxval = 0xffff;
9599                   else if (more)
9600                     minval = -0x8000, maxval = 0x7fff;
9601                   else
9602                     minval = -0x8000, maxval = 0xffff;
9603
9604                   if (imm_expr.X_op != O_constant
9605                       || imm_expr.X_add_number < minval
9606                       || imm_expr.X_add_number > maxval)
9607                     {
9608                       if (more)
9609                         break;
9610                       if (imm_expr.X_op == O_constant
9611                           || imm_expr.X_op == O_big)
9612                         as_bad (_("expression out of range"));
9613                     }
9614                 }
9615               s = expr_end;
9616               continue;
9617
9618             case 'o':           /* 16 bit offset */
9619               /* Check whether there is only a single bracketed expression
9620                  left.  If so, it must be the base register and the
9621                  constant must be zero.  */
9622               if (*s == '(' && strchr (s + 1, '(') == 0)
9623                 {
9624                   offset_expr.X_op = O_constant;
9625                   offset_expr.X_add_number = 0;
9626                   continue;
9627                 }
9628
9629               /* If this value won't fit into a 16 bit offset, then go
9630                  find a macro that will generate the 32 bit offset
9631                  code pattern.  */
9632               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9633                   && (offset_expr.X_op != O_constant
9634                       || offset_expr.X_add_number >= 0x8000
9635                       || offset_expr.X_add_number < -0x8000))
9636                 break;
9637
9638               s = expr_end;
9639               continue;
9640
9641             case 'p':           /* pc relative offset */
9642               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9643               my_getExpression (&offset_expr, s);
9644               s = expr_end;
9645               continue;
9646
9647             case 'u':           /* upper 16 bits */
9648               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9649                   && imm_expr.X_op == O_constant
9650                   && (imm_expr.X_add_number < 0
9651                       || imm_expr.X_add_number >= 0x10000))
9652                 as_bad (_("lui expression not in range 0..65535"));
9653               s = expr_end;
9654               continue;
9655
9656             case 'a':           /* 26 bit address */
9657               my_getExpression (&offset_expr, s);
9658               s = expr_end;
9659               *offset_reloc = BFD_RELOC_MIPS_JMP;
9660               continue;
9661
9662             case 'N':           /* 3 bit branch condition code */
9663             case 'M':           /* 3 bit compare condition code */
9664               if (strncmp (s, "$fcc", 4) != 0)
9665                 break;
9666               s += 4;
9667               regno = 0;
9668               do
9669                 {
9670                   regno *= 10;
9671                   regno += *s - '0';
9672                   ++s;
9673                 }
9674               while (ISDIGIT (*s));
9675               if (regno > 7)
9676                 as_bad (_("invalid condition code register $fcc%d"), regno);
9677               if (*args == 'N')
9678                 ip->insn_opcode |= regno << OP_SH_BCC;
9679               else
9680                 ip->insn_opcode |= regno << OP_SH_CCC;
9681               continue;
9682
9683             case 'H':
9684               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9685                 s += 2;
9686               if (ISDIGIT (*s))
9687                 {
9688                   c = 0;
9689                   do
9690                     {
9691                       c *= 10;
9692                       c += *s - '0';
9693                       ++s;
9694                     }
9695                   while (ISDIGIT (*s));
9696                 }
9697               else
9698                 c = 8; /* Invalid sel value.  */
9699
9700               if (c > 7)
9701                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9702               ip->insn_opcode |= c;
9703               continue;
9704
9705             case 'e':
9706               /* Must be at least one digit.  */
9707               my_getExpression (&imm_expr, s);
9708               check_absolute_expr (ip, &imm_expr);
9709
9710               if ((unsigned long) imm_expr.X_add_number
9711                   > (unsigned long) OP_MASK_VECBYTE)
9712                 {
9713                   as_bad (_("bad byte vector index (%ld)"),
9714                            (long) imm_expr.X_add_number);
9715                   imm_expr.X_add_number = 0;
9716                 }
9717
9718               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9719               imm_expr.X_op = O_absent;
9720               s = expr_end;
9721               continue;
9722
9723             case '%':
9724               my_getExpression (&imm_expr, s);
9725               check_absolute_expr (ip, &imm_expr);
9726
9727               if ((unsigned long) imm_expr.X_add_number
9728                   > (unsigned long) OP_MASK_VECALIGN)
9729                 {
9730                   as_bad (_("bad byte vector index (%ld)"),
9731                            (long) imm_expr.X_add_number);
9732                   imm_expr.X_add_number = 0;
9733                 }
9734
9735               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9736               imm_expr.X_op = O_absent;
9737               s = expr_end;
9738               continue;
9739
9740             default:
9741               as_bad (_("bad char = '%c'\n"), *args);
9742               internalError ();
9743             }
9744           break;
9745         }
9746       /* Args don't match.  */
9747       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9748           !strcmp (insn->name, insn[1].name))
9749         {
9750           ++insn;
9751           s = argsStart;
9752           insn_error = _("illegal operands");
9753           continue;
9754         }
9755       if (save_c)
9756         *(--s) = save_c;
9757       insn_error = _("illegal operands");
9758       return;
9759     }
9760 }
9761
9762 /* This routine assembles an instruction into its binary format when
9763    assembling for the mips16.  As a side effect, it sets one of the
9764    global variables imm_reloc or offset_reloc to the type of
9765    relocation to do if one of the operands is an address expression.
9766    It also sets mips16_small and mips16_ext if the user explicitly
9767    requested a small or extended instruction.  */
9768
9769 static void
9770 mips16_ip (str, ip)
9771      char *str;
9772      struct mips_cl_insn *ip;
9773 {
9774   char *s;
9775   const char *args;
9776   struct mips_opcode *insn;
9777   char *argsstart;
9778   unsigned int regno;
9779   unsigned int lastregno = 0;
9780   char *s_reset;
9781
9782   insn_error = NULL;
9783
9784   mips16_small = FALSE;
9785   mips16_ext = FALSE;
9786
9787   for (s = str; ISLOWER (*s); ++s)
9788     ;
9789   switch (*s)
9790     {
9791     case '\0':
9792       break;
9793
9794     case ' ':
9795       *s++ = '\0';
9796       break;
9797
9798     case '.':
9799       if (s[1] == 't' && s[2] == ' ')
9800         {
9801           *s = '\0';
9802           mips16_small = TRUE;
9803           s += 3;
9804           break;
9805         }
9806       else if (s[1] == 'e' && s[2] == ' ')
9807         {
9808           *s = '\0';
9809           mips16_ext = TRUE;
9810           s += 3;
9811           break;
9812         }
9813       /* Fall through.  */
9814     default:
9815       insn_error = _("unknown opcode");
9816       return;
9817     }
9818
9819   if (mips_opts.noautoextend && ! mips16_ext)
9820     mips16_small = TRUE;
9821
9822   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9823     {
9824       insn_error = _("unrecognized opcode");
9825       return;
9826     }
9827
9828   argsstart = s;
9829   for (;;)
9830     {
9831       assert (strcmp (insn->name, str) == 0);
9832
9833       ip->insn_mo = insn;
9834       ip->insn_opcode = insn->match;
9835       ip->use_extend = FALSE;
9836       imm_expr.X_op = O_absent;
9837       imm_reloc[0] = BFD_RELOC_UNUSED;
9838       imm_reloc[1] = BFD_RELOC_UNUSED;
9839       imm_reloc[2] = BFD_RELOC_UNUSED;
9840       offset_expr.X_op = O_absent;
9841       offset_reloc[0] = BFD_RELOC_UNUSED;
9842       offset_reloc[1] = BFD_RELOC_UNUSED;
9843       offset_reloc[2] = BFD_RELOC_UNUSED;
9844       for (args = insn->args; 1; ++args)
9845         {
9846           int c;
9847
9848           if (*s == ' ')
9849             ++s;
9850
9851           /* In this switch statement we call break if we did not find
9852              a match, continue if we did find a match, or return if we
9853              are done.  */
9854
9855           c = *args;
9856           switch (c)
9857             {
9858             case '\0':
9859               if (*s == '\0')
9860                 {
9861                   /* Stuff the immediate value in now, if we can.  */
9862                   if (imm_expr.X_op == O_constant
9863                       && *imm_reloc > BFD_RELOC_UNUSED
9864                       && insn->pinfo != INSN_MACRO)
9865                     {
9866                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9867                                     imm_expr.X_add_number, TRUE, mips16_small,
9868                                     mips16_ext, &ip->insn_opcode,
9869                                     &ip->use_extend, &ip->extend);
9870                       imm_expr.X_op = O_absent;
9871                       *imm_reloc = BFD_RELOC_UNUSED;
9872                     }
9873
9874                   return;
9875                 }
9876               break;
9877
9878             case ',':
9879               if (*s++ == c)
9880                 continue;
9881               s--;
9882               switch (*++args)
9883                 {
9884                 case 'v':
9885                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9886                   continue;
9887                 case 'w':
9888                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9889                   continue;
9890                 }
9891               break;
9892
9893             case '(':
9894             case ')':
9895               if (*s++ == c)
9896                 continue;
9897               break;
9898
9899             case 'v':
9900             case 'w':
9901               if (s[0] != '$')
9902                 {
9903                   if (c == 'v')
9904                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9905                   else
9906                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9907                   ++args;
9908                   continue;
9909                 }
9910               /* Fall through.  */
9911             case 'x':
9912             case 'y':
9913             case 'z':
9914             case 'Z':
9915             case '0':
9916             case 'S':
9917             case 'R':
9918             case 'X':
9919             case 'Y':
9920               if (s[0] != '$')
9921                 break;
9922               s_reset = s;
9923               if (ISDIGIT (s[1]))
9924                 {
9925                   ++s;
9926                   regno = 0;
9927                   do
9928                     {
9929                       regno *= 10;
9930                       regno += *s - '0';
9931                       ++s;
9932                     }
9933                   while (ISDIGIT (*s));
9934                   if (regno > 31)
9935                     {
9936                       as_bad (_("invalid register number (%d)"), regno);
9937                       regno = 2;
9938                     }
9939                 }
9940               else
9941                 {
9942                   if (s[1] == 'r' && s[2] == 'a')
9943                     {
9944                       s += 3;
9945                       regno = RA;
9946                     }
9947                   else if (s[1] == 'f' && s[2] == 'p')
9948                     {
9949                       s += 3;
9950                       regno = FP;
9951                     }
9952                   else if (s[1] == 's' && s[2] == 'p')
9953                     {
9954                       s += 3;
9955                       regno = SP;
9956                     }
9957                   else if (s[1] == 'g' && s[2] == 'p')
9958                     {
9959                       s += 3;
9960                       regno = GP;
9961                     }
9962                   else if (s[1] == 'a' && s[2] == 't')
9963                     {
9964                       s += 3;
9965                       regno = AT;
9966                     }
9967                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9968                     {
9969                       s += 4;
9970                       regno = KT0;
9971                     }
9972                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9973                     {
9974                       s += 4;
9975                       regno = KT1;
9976                     }
9977                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9978                     {
9979                       s += 5;
9980                       regno = ZERO;
9981                     }
9982                   else
9983                     break;
9984                 }
9985
9986               if (*s == ' ')
9987                 ++s;
9988               if (args[1] != *s)
9989                 {
9990                   if (c == 'v' || c == 'w')
9991                     {
9992                       regno = mips16_to_32_reg_map[lastregno];
9993                       s = s_reset;
9994                       ++args;
9995                     }
9996                 }
9997
9998               switch (c)
9999                 {
10000                 case 'x':
10001                 case 'y':
10002                 case 'z':
10003                 case 'v':
10004                 case 'w':
10005                 case 'Z':
10006                   regno = mips32_to_16_reg_map[regno];
10007                   break;
10008
10009                 case '0':
10010                   if (regno != 0)
10011                     regno = ILLEGAL_REG;
10012                   break;
10013
10014                 case 'S':
10015                   if (regno != SP)
10016                     regno = ILLEGAL_REG;
10017                   break;
10018
10019                 case 'R':
10020                   if (regno != RA)
10021                     regno = ILLEGAL_REG;
10022                   break;
10023
10024                 case 'X':
10025                 case 'Y':
10026                   if (regno == AT && ! mips_opts.noat)
10027                     as_warn (_("used $at without \".set noat\""));
10028                   break;
10029
10030                 default:
10031                   internalError ();
10032                 }
10033
10034               if (regno == ILLEGAL_REG)
10035                 break;
10036
10037               switch (c)
10038                 {
10039                 case 'x':
10040                 case 'v':
10041                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
10042                   break;
10043                 case 'y':
10044                 case 'w':
10045                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
10046                   break;
10047                 case 'z':
10048                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
10049                   break;
10050                 case 'Z':
10051                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
10052                 case '0':
10053                 case 'S':
10054                 case 'R':
10055                   break;
10056                 case 'X':
10057                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
10058                   break;
10059                 case 'Y':
10060                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
10061                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
10062                   break;
10063                 default:
10064                   internalError ();
10065                 }
10066
10067               lastregno = regno;
10068               continue;
10069
10070             case 'P':
10071               if (strncmp (s, "$pc", 3) == 0)
10072                 {
10073                   s += 3;
10074                   continue;
10075                 }
10076               break;
10077
10078             case '<':
10079             case '>':
10080             case '[':
10081             case ']':
10082             case '4':
10083             case '5':
10084             case 'H':
10085             case 'W':
10086             case 'D':
10087             case 'j':
10088             case '8':
10089             case 'V':
10090             case 'C':
10091             case 'U':
10092             case 'k':
10093             case 'K':
10094               if (s[0] == '%'
10095                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
10096                 {
10097                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
10098                      and generate the appropriate reloc.  If the text
10099                      inside %gprel is not a symbol name with an
10100                      optional offset, then we generate a normal reloc
10101                      and will probably fail later.  */
10102                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
10103                   if (imm_expr.X_op == O_symbol)
10104                     {
10105                       mips16_ext = TRUE;
10106                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
10107                       s = expr_end;
10108                       ip->use_extend = TRUE;
10109                       ip->extend = 0;
10110                       continue;
10111                     }
10112                 }
10113               else
10114                 {
10115                   /* Just pick up a normal expression.  */
10116                   my_getExpression (&imm_expr, s);
10117                 }
10118
10119               if (imm_expr.X_op == O_register)
10120                 {
10121                   /* What we thought was an expression turned out to
10122                      be a register.  */
10123
10124                   if (s[0] == '(' && args[1] == '(')
10125                     {
10126                       /* It looks like the expression was omitted
10127                          before a register indirection, which means
10128                          that the expression is implicitly zero.  We
10129                          still set up imm_expr, so that we handle
10130                          explicit extensions correctly.  */
10131                       imm_expr.X_op = O_constant;
10132                       imm_expr.X_add_number = 0;
10133                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10134                       continue;
10135                     }
10136
10137                   break;
10138                 }
10139
10140               /* We need to relax this instruction.  */
10141               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10142               s = expr_end;
10143               continue;
10144
10145             case 'p':
10146             case 'q':
10147             case 'A':
10148             case 'B':
10149             case 'E':
10150               /* We use offset_reloc rather than imm_reloc for the PC
10151                  relative operands.  This lets macros with both
10152                  immediate and address operands work correctly.  */
10153               my_getExpression (&offset_expr, s);
10154
10155               if (offset_expr.X_op == O_register)
10156                 break;
10157
10158               /* We need to relax this instruction.  */
10159               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
10160               s = expr_end;
10161               continue;
10162
10163             case '6':           /* break code */
10164               my_getExpression (&imm_expr, s);
10165               check_absolute_expr (ip, &imm_expr);
10166               if ((unsigned long) imm_expr.X_add_number > 63)
10167                 {
10168                   as_warn (_("Invalid value for `%s' (%lu)"),
10169                            ip->insn_mo->name,
10170                            (unsigned long) imm_expr.X_add_number);
10171                   imm_expr.X_add_number &= 0x3f;
10172                 }
10173               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
10174               imm_expr.X_op = O_absent;
10175               s = expr_end;
10176               continue;
10177
10178             case 'a':           /* 26 bit address */
10179               my_getExpression (&offset_expr, s);
10180               s = expr_end;
10181               *offset_reloc = BFD_RELOC_MIPS16_JMP;
10182               ip->insn_opcode <<= 16;
10183               continue;
10184
10185             case 'l':           /* register list for entry macro */
10186             case 'L':           /* register list for exit macro */
10187               {
10188                 int mask;
10189
10190                 if (c == 'l')
10191                   mask = 0;
10192                 else
10193                   mask = 7 << 3;
10194                 while (*s != '\0')
10195                   {
10196                     int freg, reg1, reg2;
10197
10198                     while (*s == ' ' || *s == ',')
10199                       ++s;
10200                     if (*s != '$')
10201                       {
10202                         as_bad (_("can't parse register list"));
10203                         break;
10204                       }
10205                     ++s;
10206                     if (*s != 'f')
10207                       freg = 0;
10208                     else
10209                       {
10210                         freg = 1;
10211                         ++s;
10212                       }
10213                     reg1 = 0;
10214                     while (ISDIGIT (*s))
10215                       {
10216                         reg1 *= 10;
10217                         reg1 += *s - '0';
10218                         ++s;
10219                       }
10220                     if (*s == ' ')
10221                       ++s;
10222                     if (*s != '-')
10223                       reg2 = reg1;
10224                     else
10225                       {
10226                         ++s;
10227                         if (*s != '$')
10228                           break;
10229                         ++s;
10230                         if (freg)
10231                           {
10232                             if (*s == 'f')
10233                               ++s;
10234                             else
10235                               {
10236                                 as_bad (_("invalid register list"));
10237                                 break;
10238                               }
10239                           }
10240                         reg2 = 0;
10241                         while (ISDIGIT (*s))
10242                           {
10243                             reg2 *= 10;
10244                             reg2 += *s - '0';
10245                             ++s;
10246                           }
10247                       }
10248                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10249                       {
10250                         mask &= ~ (7 << 3);
10251                         mask |= 5 << 3;
10252                       }
10253                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10254                       {
10255                         mask &= ~ (7 << 3);
10256                         mask |= 6 << 3;
10257                       }
10258                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10259                       mask |= (reg2 - 3) << 3;
10260                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10261                       mask |= (reg2 - 15) << 1;
10262                     else if (reg1 == RA && reg2 == RA)
10263                       mask |= 1;
10264                     else
10265                       {
10266                         as_bad (_("invalid register list"));
10267                         break;
10268                       }
10269                   }
10270                 /* The mask is filled in in the opcode table for the
10271                    benefit of the disassembler.  We remove it before
10272                    applying the actual mask.  */
10273                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10274                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10275               }
10276             continue;
10277
10278             case 'e':           /* extend code */
10279               my_getExpression (&imm_expr, s);
10280               check_absolute_expr (ip, &imm_expr);
10281               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10282                 {
10283                   as_warn (_("Invalid value for `%s' (%lu)"),
10284                            ip->insn_mo->name,
10285                            (unsigned long) imm_expr.X_add_number);
10286                   imm_expr.X_add_number &= 0x7ff;
10287                 }
10288               ip->insn_opcode |= imm_expr.X_add_number;
10289               imm_expr.X_op = O_absent;
10290               s = expr_end;
10291               continue;
10292
10293             default:
10294               internalError ();
10295             }
10296           break;
10297         }
10298
10299       /* Args don't match.  */
10300       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10301           strcmp (insn->name, insn[1].name) == 0)
10302         {
10303           ++insn;
10304           s = argsstart;
10305           continue;
10306         }
10307
10308       insn_error = _("illegal operands");
10309
10310       return;
10311     }
10312 }
10313
10314 /* This structure holds information we know about a mips16 immediate
10315    argument type.  */
10316
10317 struct mips16_immed_operand
10318 {
10319   /* The type code used in the argument string in the opcode table.  */
10320   int type;
10321   /* The number of bits in the short form of the opcode.  */
10322   int nbits;
10323   /* The number of bits in the extended form of the opcode.  */
10324   int extbits;
10325   /* The amount by which the short form is shifted when it is used;
10326      for example, the sw instruction has a shift count of 2.  */
10327   int shift;
10328   /* The amount by which the short form is shifted when it is stored
10329      into the instruction code.  */
10330   int op_shift;
10331   /* Non-zero if the short form is unsigned.  */
10332   int unsp;
10333   /* Non-zero if the extended form is unsigned.  */
10334   int extu;
10335   /* Non-zero if the value is PC relative.  */
10336   int pcrel;
10337 };
10338
10339 /* The mips16 immediate operand types.  */
10340
10341 static const struct mips16_immed_operand mips16_immed_operands[] =
10342 {
10343   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10344   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10345   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10346   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10347   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10348   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10349   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10350   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10351   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10352   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10353   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10354   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10355   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10356   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10357   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10358   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10359   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10360   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10361   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10362   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10363   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10364 };
10365
10366 #define MIPS16_NUM_IMMED \
10367   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10368
10369 /* Handle a mips16 instruction with an immediate value.  This or's the
10370    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
10371    whether an extended value is needed; if one is needed, it sets
10372    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
10373    If SMALL is true, an unextended opcode was explicitly requested.
10374    If EXT is true, an extended opcode was explicitly requested.  If
10375    WARN is true, warn if EXT does not match reality.  */
10376
10377 static void
10378 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
10379               extend)
10380      char *file;
10381      unsigned int line;
10382      int type;
10383      offsetT val;
10384      bfd_boolean warn;
10385      bfd_boolean small;
10386      bfd_boolean ext;
10387      unsigned long *insn;
10388      bfd_boolean *use_extend;
10389      unsigned short *extend;
10390 {
10391   register const struct mips16_immed_operand *op;
10392   int mintiny, maxtiny;
10393   bfd_boolean needext;
10394
10395   op = mips16_immed_operands;
10396   while (op->type != type)
10397     {
10398       ++op;
10399       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10400     }
10401
10402   if (op->unsp)
10403     {
10404       if (type == '<' || type == '>' || type == '[' || type == ']')
10405         {
10406           mintiny = 1;
10407           maxtiny = 1 << op->nbits;
10408         }
10409       else
10410         {
10411           mintiny = 0;
10412           maxtiny = (1 << op->nbits) - 1;
10413         }
10414     }
10415   else
10416     {
10417       mintiny = - (1 << (op->nbits - 1));
10418       maxtiny = (1 << (op->nbits - 1)) - 1;
10419     }
10420
10421   /* Branch offsets have an implicit 0 in the lowest bit.  */
10422   if (type == 'p' || type == 'q')
10423     val /= 2;
10424
10425   if ((val & ((1 << op->shift) - 1)) != 0
10426       || val < (mintiny << op->shift)
10427       || val > (maxtiny << op->shift))
10428     needext = TRUE;
10429   else
10430     needext = FALSE;
10431
10432   if (warn && ext && ! needext)
10433     as_warn_where (file, line,
10434                    _("extended operand requested but not required"));
10435   if (small && needext)
10436     as_bad_where (file, line, _("invalid unextended operand value"));
10437
10438   if (small || (! ext && ! needext))
10439     {
10440       int insnval;
10441
10442       *use_extend = FALSE;
10443       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10444       insnval <<= op->op_shift;
10445       *insn |= insnval;
10446     }
10447   else
10448     {
10449       long minext, maxext;
10450       int extval;
10451
10452       if (op->extu)
10453         {
10454           minext = 0;
10455           maxext = (1 << op->extbits) - 1;
10456         }
10457       else
10458         {
10459           minext = - (1 << (op->extbits - 1));
10460           maxext = (1 << (op->extbits - 1)) - 1;
10461         }
10462       if (val < minext || val > maxext)
10463         as_bad_where (file, line,
10464                       _("operand value out of range for instruction"));
10465
10466       *use_extend = TRUE;
10467       if (op->extbits == 16)
10468         {
10469           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10470           val &= 0x1f;
10471         }
10472       else if (op->extbits == 15)
10473         {
10474           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10475           val &= 0xf;
10476         }
10477       else
10478         {
10479           extval = ((val & 0x1f) << 6) | (val & 0x20);
10480           val = 0;
10481         }
10482
10483       *extend = (unsigned short) extval;
10484       *insn |= val;
10485     }
10486 }
10487 \f
10488 static const struct percent_op_match
10489 {
10490   const char *str;
10491   bfd_reloc_code_real_type reloc;
10492 } percent_op[] =
10493 {
10494   {"%lo", BFD_RELOC_LO16},
10495 #ifdef OBJ_ELF
10496   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10497   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10498   {"%call16", BFD_RELOC_MIPS_CALL16},
10499   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10500   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10501   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10502   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10503   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10504   {"%got", BFD_RELOC_MIPS_GOT16},
10505   {"%gp_rel", BFD_RELOC_GPREL16},
10506   {"%half", BFD_RELOC_16},
10507   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10508   {"%higher", BFD_RELOC_MIPS_HIGHER},
10509   {"%neg", BFD_RELOC_MIPS_SUB},
10510 #endif
10511   {"%hi", BFD_RELOC_HI16_S}
10512 };
10513
10514
10515 /* Return true if *STR points to a relocation operator.  When returning true,
10516    move *STR over the operator and store its relocation code in *RELOC.
10517    Leave both *STR and *RELOC alone when returning false.  */
10518
10519 static bfd_boolean
10520 parse_relocation (str, reloc)
10521      char **str;
10522      bfd_reloc_code_real_type *reloc;
10523 {
10524   size_t i;
10525
10526   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10527     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10528       {
10529         *str += strlen (percent_op[i].str);
10530         *reloc = percent_op[i].reloc;
10531
10532         /* Check whether the output BFD supports this relocation.
10533            If not, issue an error and fall back on something safe.  */
10534         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10535           {
10536             as_bad ("relocation %s isn't supported by the current ABI",
10537                     percent_op[i].str);
10538             *reloc = BFD_RELOC_LO16;
10539           }
10540         return TRUE;
10541       }
10542   return FALSE;
10543 }
10544
10545
10546 /* Parse string STR as a 16-bit relocatable operand.  Store the
10547    expression in *EP and the relocations in the array starting
10548    at RELOC.  Return the number of relocation operators used.
10549
10550    On exit, EXPR_END points to the first character after the expression.
10551    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10552
10553 static size_t
10554 my_getSmallExpression (ep, reloc, str)
10555      expressionS *ep;
10556      bfd_reloc_code_real_type *reloc;
10557      char *str;
10558 {
10559   bfd_reloc_code_real_type reversed_reloc[3];
10560   size_t reloc_index, i;
10561   int crux_depth, str_depth;
10562   char *crux;
10563
10564   /* Search for the start of the main expression, recoding relocations
10565      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10566      of the main expression and with CRUX_DEPTH containing the number
10567      of open brackets at that point.  */
10568   reloc_index = -1;
10569   str_depth = 0;
10570   do
10571     {
10572       reloc_index++;
10573       crux = str;
10574       crux_depth = str_depth;
10575
10576       /* Skip over whitespace and brackets, keeping count of the number
10577          of brackets.  */
10578       while (*str == ' ' || *str == '\t' || *str == '(')
10579         if (*str++ == '(')
10580           str_depth++;
10581     }
10582   while (*str == '%'
10583          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10584          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10585
10586   my_getExpression (ep, crux);
10587   str = expr_end;
10588
10589   /* Match every open bracket.  */
10590   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10591     if (*str++ == ')')
10592       crux_depth--;
10593
10594   if (crux_depth > 0)
10595     as_bad ("unclosed '('");
10596
10597   expr_end = str;
10598
10599   if (reloc_index == 0)
10600     reloc[0] = BFD_RELOC_LO16;
10601   else
10602     {
10603       prev_reloc_op_frag = frag_now;
10604       for (i = 0; i < reloc_index; i++)
10605         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10606     }
10607
10608   return reloc_index;
10609 }
10610
10611 static void
10612 my_getExpression (ep, str)
10613      expressionS *ep;
10614      char *str;
10615 {
10616   char *save_in;
10617   valueT val;
10618
10619   save_in = input_line_pointer;
10620   input_line_pointer = str;
10621   expression (ep);
10622   expr_end = input_line_pointer;
10623   input_line_pointer = save_in;
10624
10625   /* If we are in mips16 mode, and this is an expression based on `.',
10626      then we bump the value of the symbol by 1 since that is how other
10627      text symbols are handled.  We don't bother to handle complex
10628      expressions, just `.' plus or minus a constant.  */
10629   if (mips_opts.mips16
10630       && ep->X_op == O_symbol
10631       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10632       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10633       && symbol_get_frag (ep->X_add_symbol) == frag_now
10634       && symbol_constant_p (ep->X_add_symbol)
10635       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10636     S_SET_VALUE (ep->X_add_symbol, val + 1);
10637 }
10638
10639 /* Turn a string in input_line_pointer into a floating point constant
10640    of type TYPE, and store the appropriate bytes in *LITP.  The number
10641    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10642    returned, or NULL on OK.  */
10643
10644 char *
10645 md_atof (type, litP, sizeP)
10646      int type;
10647      char *litP;
10648      int *sizeP;
10649 {
10650   int prec;
10651   LITTLENUM_TYPE words[4];
10652   char *t;
10653   int i;
10654
10655   switch (type)
10656     {
10657     case 'f':
10658       prec = 2;
10659       break;
10660
10661     case 'd':
10662       prec = 4;
10663       break;
10664
10665     default:
10666       *sizeP = 0;
10667       return _("bad call to md_atof");
10668     }
10669
10670   t = atof_ieee (input_line_pointer, type, words);
10671   if (t)
10672     input_line_pointer = t;
10673
10674   *sizeP = prec * 2;
10675
10676   if (! target_big_endian)
10677     {
10678       for (i = prec - 1; i >= 0; i--)
10679         {
10680           md_number_to_chars (litP, (valueT) words[i], 2);
10681           litP += 2;
10682         }
10683     }
10684   else
10685     {
10686       for (i = 0; i < prec; i++)
10687         {
10688           md_number_to_chars (litP, (valueT) words[i], 2);
10689           litP += 2;
10690         }
10691     }
10692
10693   return NULL;
10694 }
10695
10696 void
10697 md_number_to_chars (buf, val, n)
10698      char *buf;
10699      valueT val;
10700      int n;
10701 {
10702   if (target_big_endian)
10703     number_to_chars_bigendian (buf, val, n);
10704   else
10705     number_to_chars_littleendian (buf, val, n);
10706 }
10707 \f
10708 #ifdef OBJ_ELF
10709 static int support_64bit_objects(void)
10710 {
10711   const char **list, **l;
10712   int yes;
10713
10714   list = bfd_target_list ();
10715   for (l = list; *l != NULL; l++)
10716 #ifdef TE_TMIPS
10717     /* This is traditional mips */
10718     if (strcmp (*l, "elf64-tradbigmips") == 0
10719         || strcmp (*l, "elf64-tradlittlemips") == 0)
10720 #else
10721     if (strcmp (*l, "elf64-bigmips") == 0
10722         || strcmp (*l, "elf64-littlemips") == 0)
10723 #endif
10724       break;
10725   yes = (*l != NULL);
10726   free (list);
10727   return yes;
10728 }
10729 #endif /* OBJ_ELF */
10730
10731 const char *md_shortopts = "nO::g::G:";
10732
10733 struct option md_longopts[] =
10734 {
10735 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10736   {"mips0", no_argument, NULL, OPTION_MIPS1},
10737   {"mips1", no_argument, NULL, OPTION_MIPS1},
10738 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10739   {"mips2", no_argument, NULL, OPTION_MIPS2},
10740 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10741   {"mips3", no_argument, NULL, OPTION_MIPS3},
10742 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10743   {"mips4", no_argument, NULL, OPTION_MIPS4},
10744 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10745   {"mips5", no_argument, NULL, OPTION_MIPS5},
10746 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10747   {"mips32", no_argument, NULL, OPTION_MIPS32},
10748 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10749   {"mips64", no_argument, NULL, OPTION_MIPS64},
10750 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10751   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10752 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10753   {"trap", no_argument, NULL, OPTION_TRAP},
10754   {"no-break", no_argument, NULL, OPTION_TRAP},
10755 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10756   {"break", no_argument, NULL, OPTION_BREAK},
10757   {"no-trap", no_argument, NULL, OPTION_BREAK},
10758 #define OPTION_EB (OPTION_MD_BASE + 11)
10759   {"EB", no_argument, NULL, OPTION_EB},
10760 #define OPTION_EL (OPTION_MD_BASE + 12)
10761   {"EL", no_argument, NULL, OPTION_EL},
10762 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10763   {"mips16", no_argument, NULL, OPTION_MIPS16},
10764 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10765   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10766 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10767   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10768 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10769   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10770   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10771 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10772   {"mfp32", no_argument, NULL, OPTION_FP32},
10773 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10774   {"mgp32", no_argument, NULL, OPTION_GP32},
10775 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10776   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10777 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10778   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10779 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10780   {"march", required_argument, NULL, OPTION_MARCH},
10781 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10782   {"mtune", required_argument, NULL, OPTION_MTUNE},
10783 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10784   {"mfp64", no_argument, NULL, OPTION_FP64},
10785 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10786   {"m4650", no_argument, NULL, OPTION_M4650},
10787 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10788   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10789 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10790   {"m4010", no_argument, NULL, OPTION_M4010},
10791 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10792   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10793 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10794   {"m4100", no_argument, NULL, OPTION_M4100},
10795 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10796   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10797 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10798   {"m3900", no_argument, NULL, OPTION_M3900},
10799 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10800   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10801 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10802   {"mgp64", no_argument, NULL, OPTION_GP64},
10803 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10804   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10805 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10806   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10807 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10808   {"mdmx", no_argument, NULL, OPTION_MDMX},
10809 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10810   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10811 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10812 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10813   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10814   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10815 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10816 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10817   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10818   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10819 #define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10820   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10821 #ifdef OBJ_ELF
10822 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 42)
10823 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10824   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10825   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10826 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10827   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10828 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10829   {"xgot",        no_argument, NULL, OPTION_XGOT},
10830 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10831   {"mabi", required_argument, NULL, OPTION_MABI},
10832 #define OPTION_32          (OPTION_ELF_BASE + 4)
10833   {"32",          no_argument, NULL, OPTION_32},
10834 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10835   {"n32",         no_argument, NULL, OPTION_N32},
10836 #define OPTION_64          (OPTION_ELF_BASE + 6)
10837   {"64",          no_argument, NULL, OPTION_64},
10838 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10839   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10840 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10841   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10842 #endif /* OBJ_ELF */
10843   {NULL, no_argument, NULL, 0}
10844 };
10845 size_t md_longopts_size = sizeof (md_longopts);
10846
10847 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10848    NEW_VALUE.  Warn if another value was already specified.  Note:
10849    we have to defer parsing the -march and -mtune arguments in order
10850    to handle 'from-abi' correctly, since the ABI might be specified
10851    in a later argument.  */
10852
10853 static void
10854 mips_set_option_string (string_ptr, new_value)
10855      const char **string_ptr, *new_value;
10856 {
10857   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10858     as_warn (_("A different %s was already specified, is now %s"),
10859              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10860              new_value);
10861
10862   *string_ptr = new_value;
10863 }
10864
10865 int
10866 md_parse_option (c, arg)
10867      int c;
10868      char *arg;
10869 {
10870   switch (c)
10871     {
10872     case OPTION_CONSTRUCT_FLOATS:
10873       mips_disable_float_construction = 0;
10874       break;
10875
10876     case OPTION_NO_CONSTRUCT_FLOATS:
10877       mips_disable_float_construction = 1;
10878       break;
10879
10880     case OPTION_TRAP:
10881       mips_trap = 1;
10882       break;
10883
10884     case OPTION_BREAK:
10885       mips_trap = 0;
10886       break;
10887
10888     case OPTION_EB:
10889       target_big_endian = 1;
10890       break;
10891
10892     case OPTION_EL:
10893       target_big_endian = 0;
10894       break;
10895
10896     case 'n':
10897       warn_nops = 1;
10898       break;
10899
10900     case 'O':
10901       if (arg && arg[1] == '0')
10902         mips_optimize = 1;
10903       else
10904         mips_optimize = 2;
10905       break;
10906
10907     case 'g':
10908       if (arg == NULL)
10909         mips_debug = 2;
10910       else
10911         mips_debug = atoi (arg);
10912       /* When the MIPS assembler sees -g or -g2, it does not do
10913          optimizations which limit full symbolic debugging.  We take
10914          that to be equivalent to -O0.  */
10915       if (mips_debug == 2)
10916         mips_optimize = 1;
10917       break;
10918
10919     case OPTION_MIPS1:
10920       file_mips_isa = ISA_MIPS1;
10921       break;
10922
10923     case OPTION_MIPS2:
10924       file_mips_isa = ISA_MIPS2;
10925       break;
10926
10927     case OPTION_MIPS3:
10928       file_mips_isa = ISA_MIPS3;
10929       break;
10930
10931     case OPTION_MIPS4:
10932       file_mips_isa = ISA_MIPS4;
10933       break;
10934
10935     case OPTION_MIPS5:
10936       file_mips_isa = ISA_MIPS5;
10937       break;
10938
10939     case OPTION_MIPS32:
10940       file_mips_isa = ISA_MIPS32;
10941       break;
10942
10943     case OPTION_MIPS32R2:
10944       file_mips_isa = ISA_MIPS32R2;
10945       break;
10946
10947     case OPTION_MIPS64:
10948       file_mips_isa = ISA_MIPS64;
10949       break;
10950
10951     case OPTION_MTUNE:
10952       mips_set_option_string (&mips_tune_string, arg);
10953       break;
10954
10955     case OPTION_MARCH:
10956       mips_set_option_string (&mips_arch_string, arg);
10957       break;
10958
10959     case OPTION_M4650:
10960       mips_set_option_string (&mips_arch_string, "4650");
10961       mips_set_option_string (&mips_tune_string, "4650");
10962       break;
10963
10964     case OPTION_NO_M4650:
10965       break;
10966
10967     case OPTION_M4010:
10968       mips_set_option_string (&mips_arch_string, "4010");
10969       mips_set_option_string (&mips_tune_string, "4010");
10970       break;
10971
10972     case OPTION_NO_M4010:
10973       break;
10974
10975     case OPTION_M4100:
10976       mips_set_option_string (&mips_arch_string, "4100");
10977       mips_set_option_string (&mips_tune_string, "4100");
10978       break;
10979
10980     case OPTION_NO_M4100:
10981       break;
10982
10983     case OPTION_M3900:
10984       mips_set_option_string (&mips_arch_string, "3900");
10985       mips_set_option_string (&mips_tune_string, "3900");
10986       break;
10987
10988     case OPTION_NO_M3900:
10989       break;
10990
10991     case OPTION_MDMX:
10992       mips_opts.ase_mdmx = 1;
10993       break;
10994
10995     case OPTION_NO_MDMX:
10996       mips_opts.ase_mdmx = 0;
10997       break;
10998
10999     case OPTION_MIPS16:
11000       mips_opts.mips16 = 1;
11001       mips_no_prev_insn (FALSE);
11002       break;
11003
11004     case OPTION_NO_MIPS16:
11005       mips_opts.mips16 = 0;
11006       mips_no_prev_insn (FALSE);
11007       break;
11008
11009     case OPTION_MIPS3D:
11010       mips_opts.ase_mips3d = 1;
11011       break;
11012
11013     case OPTION_NO_MIPS3D:
11014       mips_opts.ase_mips3d = 0;
11015       break;
11016
11017     case OPTION_MEMBEDDED_PIC:
11018       mips_pic = EMBEDDED_PIC;
11019       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
11020         {
11021           as_bad (_("-G may not be used with embedded PIC code"));
11022           return 0;
11023         }
11024       g_switch_value = 0x7fffffff;
11025       break;
11026
11027     case OPTION_FIX_VR4122:
11028       mips_fix_4122_bugs = 1;
11029       break;
11030
11031     case OPTION_NO_FIX_VR4122:
11032       mips_fix_4122_bugs = 0;
11033       break;
11034
11035     case OPTION_RELAX_BRANCH:
11036       mips_relax_branch = 1;
11037       break;
11038
11039     case OPTION_NO_RELAX_BRANCH:
11040       mips_relax_branch = 0;
11041       break;
11042
11043 #ifdef OBJ_ELF
11044       /* When generating ELF code, we permit -KPIC and -call_shared to
11045          select SVR4_PIC, and -non_shared to select no PIC.  This is
11046          intended to be compatible with Irix 5.  */
11047     case OPTION_CALL_SHARED:
11048       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11049         {
11050           as_bad (_("-call_shared is supported only for ELF format"));
11051           return 0;
11052         }
11053       mips_pic = SVR4_PIC;
11054       mips_abicalls = TRUE;
11055       if (g_switch_seen && g_switch_value != 0)
11056         {
11057           as_bad (_("-G may not be used with SVR4 PIC code"));
11058           return 0;
11059         }
11060       g_switch_value = 0;
11061       break;
11062
11063     case OPTION_NON_SHARED:
11064       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11065         {
11066           as_bad (_("-non_shared is supported only for ELF format"));
11067           return 0;
11068         }
11069       mips_pic = NO_PIC;
11070       mips_abicalls = FALSE;
11071       break;
11072
11073       /* The -xgot option tells the assembler to use 32 offsets when
11074          accessing the got in SVR4_PIC mode.  It is for Irix
11075          compatibility.  */
11076     case OPTION_XGOT:
11077       mips_big_got = 1;
11078       break;
11079 #endif /* OBJ_ELF */
11080
11081     case 'G':
11082       if (! USE_GLOBAL_POINTER_OPT)
11083         {
11084           as_bad (_("-G is not supported for this configuration"));
11085           return 0;
11086         }
11087       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
11088         {
11089           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
11090           return 0;
11091         }
11092       else
11093         g_switch_value = atoi (arg);
11094       g_switch_seen = 1;
11095       break;
11096
11097 #ifdef OBJ_ELF
11098       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11099          and -mabi=64.  */
11100     case OPTION_32:
11101       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11102         {
11103           as_bad (_("-32 is supported for ELF format only"));
11104           return 0;
11105         }
11106       mips_abi = O32_ABI;
11107       break;
11108
11109     case OPTION_N32:
11110       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11111         {
11112           as_bad (_("-n32 is supported for ELF format only"));
11113           return 0;
11114         }
11115       mips_abi = N32_ABI;
11116       break;
11117
11118     case OPTION_64:
11119       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11120         {
11121           as_bad (_("-64 is supported for ELF format only"));
11122           return 0;
11123         }
11124       mips_abi = N64_ABI;
11125       if (! support_64bit_objects())
11126         as_fatal (_("No compiled in support for 64 bit object file format"));
11127       break;
11128 #endif /* OBJ_ELF */
11129
11130     case OPTION_GP32:
11131       file_mips_gp32 = 1;
11132       break;
11133
11134     case OPTION_GP64:
11135       file_mips_gp32 = 0;
11136       break;
11137
11138     case OPTION_FP32:
11139       file_mips_fp32 = 1;
11140       break;
11141
11142     case OPTION_FP64:
11143       file_mips_fp32 = 0;
11144       break;
11145
11146 #ifdef OBJ_ELF
11147     case OPTION_MABI:
11148       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11149         {
11150           as_bad (_("-mabi is supported for ELF format only"));
11151           return 0;
11152         }
11153       if (strcmp (arg, "32") == 0)
11154         mips_abi = O32_ABI;
11155       else if (strcmp (arg, "o64") == 0)
11156         mips_abi = O64_ABI;
11157       else if (strcmp (arg, "n32") == 0)
11158         mips_abi = N32_ABI;
11159       else if (strcmp (arg, "64") == 0)
11160         {
11161           mips_abi = N64_ABI;
11162           if (! support_64bit_objects())
11163             as_fatal (_("No compiled in support for 64 bit object file "
11164                         "format"));
11165         }
11166       else if (strcmp (arg, "eabi") == 0)
11167         mips_abi = EABI_ABI;
11168       else
11169         {
11170           as_fatal (_("invalid abi -mabi=%s"), arg);
11171           return 0;
11172         }
11173       break;
11174 #endif /* OBJ_ELF */
11175
11176     case OPTION_M7000_HILO_FIX:
11177       mips_7000_hilo_fix = TRUE;
11178       break;
11179
11180     case OPTION_MNO_7000_HILO_FIX:
11181       mips_7000_hilo_fix = FALSE;
11182       break;
11183
11184 #ifdef OBJ_ELF
11185     case OPTION_MDEBUG:
11186       mips_flag_mdebug = TRUE;
11187       break;
11188
11189     case OPTION_NO_MDEBUG:
11190       mips_flag_mdebug = FALSE;
11191       break;
11192 #endif /* OBJ_ELF */
11193
11194     default:
11195       return 0;
11196     }
11197
11198   return 1;
11199 }
11200 \f
11201 /* Set up globals to generate code for the ISA or processor
11202    described by INFO.  */
11203
11204 static void
11205 mips_set_architecture (info)
11206      const struct mips_cpu_info *info;
11207 {
11208   if (info != 0)
11209     {
11210       mips_arch_info = info;
11211       mips_arch = info->cpu;
11212       mips_opts.isa = info->isa;
11213     }
11214 }
11215
11216
11217 /* Likewise for tuning.  */
11218
11219 static void
11220 mips_set_tune (info)
11221      const struct mips_cpu_info *info;
11222 {
11223   if (info != 0)
11224     {
11225       mips_tune_info = info;
11226       mips_tune = info->cpu;
11227     }
11228 }
11229
11230
11231 void
11232 mips_after_parse_args ()
11233 {
11234   /* GP relative stuff not working for PE */
11235   if (strncmp (TARGET_OS, "pe", 2) == 0
11236       && g_switch_value != 0)
11237     {
11238       if (g_switch_seen)
11239         as_bad (_("-G not supported in this configuration."));
11240       g_switch_value = 0;
11241     }
11242
11243   if (mips_abi == NO_ABI)
11244     mips_abi = MIPS_DEFAULT_ABI;
11245
11246   /* The following code determines the architecture and register size.
11247      Similar code was added to GCC 3.3 (see override_options() in
11248      config/mips/mips.c).  The GAS and GCC code should be kept in sync
11249      as much as possible.  */
11250
11251   if (mips_arch_string != 0)
11252     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
11253
11254   if (mips_tune_string != 0)
11255     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
11256
11257   if (file_mips_isa != ISA_UNKNOWN)
11258     {
11259       /* Handle -mipsN.  At this point, file_mips_isa contains the
11260          ISA level specified by -mipsN, while mips_opts.isa contains
11261          the -march selection (if any).  */
11262       if (mips_arch_info != 0)
11263         {
11264           /* -march takes precedence over -mipsN, since it is more descriptive.
11265              There's no harm in specifying both as long as the ISA levels
11266              are the same.  */
11267           if (file_mips_isa != mips_opts.isa)
11268             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11269                     mips_cpu_info_from_isa (file_mips_isa)->name,
11270                     mips_cpu_info_from_isa (mips_opts.isa)->name);
11271         }
11272       else
11273         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
11274     }
11275
11276   if (mips_arch_info == 0)
11277     mips_set_architecture (mips_parse_cpu ("default CPU",
11278                                            MIPS_CPU_STRING_DEFAULT));
11279
11280   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11281     as_bad ("-march=%s is not compatible with the selected ABI",
11282             mips_arch_info->name);
11283
11284   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
11285   if (mips_tune_info == 0)
11286     mips_set_tune (mips_arch_info);
11287
11288   if (file_mips_gp32 >= 0)
11289     {
11290       /* The user specified the size of the integer registers.  Make sure
11291          it agrees with the ABI and ISA.  */
11292       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11293         as_bad (_("-mgp64 used with a 32-bit processor"));
11294       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11295         as_bad (_("-mgp32 used with a 64-bit ABI"));
11296       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11297         as_bad (_("-mgp64 used with a 32-bit ABI"));
11298     }
11299   else
11300     {
11301       /* Infer the integer register size from the ABI and processor.
11302          Restrict ourselves to 32-bit registers if that's all the
11303          processor has, or if the ABI cannot handle 64-bit registers.  */
11304       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11305                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11306     }
11307
11308   /* ??? GAS treats single-float processors as though they had 64-bit
11309      float registers (although it complains when double-precision
11310      instructions are used).  As things stand, saying they have 32-bit
11311      registers would lead to spurious "register must be even" messages.
11312      So here we assume float registers are always the same size as
11313      integer ones, unless the user says otherwise.  */
11314   if (file_mips_fp32 < 0)
11315     file_mips_fp32 = file_mips_gp32;
11316
11317   /* End of GCC-shared inference code.  */
11318
11319   /* ??? When do we want this flag to be set?   Who uses it?  */
11320   if (file_mips_gp32 == 1
11321       && mips_abi == NO_ABI
11322       && ISA_HAS_64BIT_REGS (mips_opts.isa))
11323     mips_32bitmode = 1;
11324
11325   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11326     as_bad (_("trap exception not supported at ISA 1"));
11327
11328   /* If the selected architecture includes support for ASEs, enable
11329      generation of code for them.  */
11330   if (mips_opts.mips16 == -1)
11331     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
11332   if (mips_opts.ase_mips3d == -1)
11333     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
11334   if (mips_opts.ase_mdmx == -1)
11335     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
11336
11337   file_mips_isa = mips_opts.isa;
11338   file_ase_mips16 = mips_opts.mips16;
11339   file_ase_mips3d = mips_opts.ase_mips3d;
11340   file_ase_mdmx = mips_opts.ase_mdmx;
11341   mips_opts.gp32 = file_mips_gp32;
11342   mips_opts.fp32 = file_mips_fp32;
11343
11344   if (mips_flag_mdebug < 0)
11345     {
11346 #ifdef OBJ_MAYBE_ECOFF
11347       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11348         mips_flag_mdebug = 1;
11349       else
11350 #endif /* OBJ_MAYBE_ECOFF */
11351         mips_flag_mdebug = 0;
11352     }
11353 }
11354 \f
11355 void
11356 mips_init_after_args ()
11357 {
11358   /* initialize opcodes */
11359   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11360   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11361 }
11362
11363 long
11364 md_pcrel_from (fixP)
11365      fixS *fixP;
11366 {
11367   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
11368       && fixP->fx_addsy != (symbolS *) NULL
11369       && ! S_IS_DEFINED (fixP->fx_addsy))
11370     return 4;
11371
11372   /* Return the address of the delay slot.  */
11373   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
11374 }
11375
11376 /* This is called before the symbol table is processed.  In order to
11377    work with gcc when using mips-tfile, we must keep all local labels.
11378    However, in other cases, we want to discard them.  If we were
11379    called with -g, but we didn't see any debugging information, it may
11380    mean that gcc is smuggling debugging information through to
11381    mips-tfile, in which case we must generate all local labels.  */
11382
11383 void
11384 mips_frob_file_before_adjust ()
11385 {
11386 #ifndef NO_ECOFF_DEBUGGING
11387   if (ECOFF_DEBUGGING
11388       && mips_debug != 0
11389       && ! ecoff_debugging_seen)
11390     flag_keep_locals = 1;
11391 #endif
11392 }
11393
11394 /* Sort any unmatched HI16_S relocs so that they immediately precede
11395    the corresponding LO reloc.  This is called before md_apply_fix3 and
11396    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
11397    explicit use of the %hi modifier.  */
11398
11399 void
11400 mips_frob_file ()
11401 {
11402   struct mips_hi_fixup *l;
11403
11404   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11405     {
11406       segment_info_type *seginfo;
11407       int pass;
11408
11409       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11410
11411       /* If a GOT16 relocation turns out to be against a global symbol,
11412          there isn't supposed to be a matching LO.  */
11413       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11414           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11415         continue;
11416
11417       /* Check quickly whether the next fixup happens to be a matching %lo.  */
11418       if (fixup_has_matching_lo_p (l->fixp))
11419         continue;
11420
11421       /* Look through the fixups for this segment for a matching %lo.
11422          When we find one, move the %hi just in front of it.  We do
11423          this in two passes.  In the first pass, we try to find a
11424          unique %lo.  In the second pass, we permit multiple %hi
11425          relocs for a single %lo (this is a GNU extension).  */
11426       seginfo = seg_info (l->seg);
11427       for (pass = 0; pass < 2; pass++)
11428         {
11429           fixS *f, *prev;
11430
11431           prev = NULL;
11432           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11433             {
11434               /* Check whether this is a %lo fixup which matches l->fixp.  */
11435               if (f->fx_r_type == BFD_RELOC_LO16
11436                   && f->fx_addsy == l->fixp->fx_addsy
11437                   && f->fx_offset == l->fixp->fx_offset
11438                   && (pass == 1
11439                       || prev == NULL
11440                       || !reloc_needs_lo_p (prev->fx_r_type)
11441                       || !fixup_has_matching_lo_p (prev)))
11442                 {
11443                   fixS **pf;
11444
11445                   /* Move l->fixp before f.  */
11446                   for (pf = &seginfo->fix_root;
11447                        *pf != l->fixp;
11448                        pf = &(*pf)->fx_next)
11449                     assert (*pf != NULL);
11450
11451                   *pf = l->fixp->fx_next;
11452
11453                   l->fixp->fx_next = f;
11454                   if (prev == NULL)
11455                     seginfo->fix_root = l->fixp;
11456                   else
11457                     prev->fx_next = l->fixp;
11458
11459                   break;
11460                 }
11461
11462               prev = f;
11463             }
11464
11465           if (f != NULL)
11466             break;
11467
11468 #if 0 /* GCC code motion plus incomplete dead code elimination
11469          can leave a %hi without a %lo.  */
11470           if (pass == 1)
11471             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11472                            _("Unmatched %%hi reloc"));
11473 #endif
11474         }
11475     }
11476 }
11477
11478 /* When generating embedded PIC code we need to use a special
11479    relocation to represent the difference of two symbols in the .text
11480    section (switch tables use a difference of this sort).  See
11481    include/coff/mips.h for details.  This macro checks whether this
11482    fixup requires the special reloc.  */
11483 #define SWITCH_TABLE(fixp) \
11484   ((fixp)->fx_r_type == BFD_RELOC_32 \
11485    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11486    && (fixp)->fx_addsy != NULL \
11487    && (fixp)->fx_subsy != NULL \
11488    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11489    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11490
11491 /* When generating embedded PIC code we must keep all PC relative
11492    relocations, in case the linker has to relax a call.  We also need
11493    to keep relocations for switch table entries.
11494
11495    We may have combined relocations without symbols in the N32/N64 ABI.
11496    We have to prevent gas from dropping them.  */
11497
11498 int
11499 mips_force_relocation (fixp)
11500      fixS *fixp;
11501 {
11502   if (generic_force_reloc (fixp))
11503     return 1;
11504
11505   if (HAVE_NEWABI
11506       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11507       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11508           || fixp->fx_r_type == BFD_RELOC_HI16_S
11509           || fixp->fx_r_type == BFD_RELOC_LO16))
11510     return 1;
11511
11512   return (mips_pic == EMBEDDED_PIC
11513           && (fixp->fx_pcrel
11514               || SWITCH_TABLE (fixp)
11515               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11516               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11517 }
11518
11519 /* This hook is called before a fix is simplified.  We don't really
11520    decide whether to skip a fix here.  Rather, we turn global symbols
11521    used as branch targets into local symbols, such that they undergo
11522    simplification.  We can only do this if the symbol is defined and
11523    it is in the same section as the branch.  If this doesn't hold, we
11524    emit a better error message than just saying the relocation is not
11525    valid for the selected object format.
11526
11527    FIXP is the fix-up we're going to try to simplify, SEG is the
11528    segment in which the fix up occurs.  The return value should be
11529    non-zero to indicate the fix-up is valid for further
11530    simplifications.  */
11531
11532 int
11533 mips_validate_fix (fixP, seg)
11534      struct fix *fixP;
11535      asection *seg;
11536 {
11537   /* There's a lot of discussion on whether it should be possible to
11538      use R_MIPS_PC16 to represent branch relocations.  The outcome
11539      seems to be that it can, but gas/bfd are very broken in creating
11540      RELA relocations for this, so for now we only accept branches to
11541      symbols in the same section.  Anything else is of dubious value,
11542      since there's no guarantee that at link time the symbol would be
11543      in range.  Even for branches to local symbols this is arguably
11544      wrong, since it we assume the symbol is not going to be
11545      overridden, which should be possible per ELF library semantics,
11546      but then, there isn't a dynamic relocation that could be used to
11547      this effect, and the target would likely be out of range as well.
11548
11549      Unfortunately, it seems that there is too much code out there
11550      that relies on branches to symbols that are global to be resolved
11551      as if they were local, like the IRIX tools do, so we do it as
11552      well, but with a warning so that people are reminded to fix their
11553      code.  If we ever get back to using R_MIPS_PC16 for branch
11554      targets, this entire block should go away (and probably the
11555      whole function).  */
11556
11557   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11558       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11559             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11560            && mips_pic != EMBEDDED_PIC)
11561           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11562       && fixP->fx_addsy)
11563     {
11564       if (! S_IS_DEFINED (fixP->fx_addsy))
11565         {
11566           as_bad_where (fixP->fx_file, fixP->fx_line,
11567                         _("Cannot branch to undefined symbol."));
11568           /* Avoid any further errors about this fixup.  */
11569           fixP->fx_done = 1;
11570         }
11571       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11572         {
11573           as_bad_where (fixP->fx_file, fixP->fx_line,
11574                         _("Cannot branch to symbol in another section."));
11575           fixP->fx_done = 1;
11576         }
11577       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11578         {
11579           symbolS *sym = fixP->fx_addsy;
11580
11581           as_warn_where (fixP->fx_file, fixP->fx_line,
11582                          _("Pretending global symbol used as branch target is local."));
11583
11584           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11585                                           S_GET_SEGMENT (sym),
11586                                           S_GET_VALUE (sym),
11587                                           symbol_get_frag (sym));
11588           copy_symbol_attributes (fixP->fx_addsy, sym);
11589           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11590           assert (symbol_resolved_p (sym));
11591           symbol_mark_resolved (fixP->fx_addsy);
11592         }
11593     }
11594
11595   return 1;
11596 }
11597
11598 #ifdef OBJ_ELF
11599 static int
11600 mips_need_elf_addend_fixup (fixP)
11601      fixS *fixP;
11602 {
11603   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11604     return 1;
11605   if (mips_pic == EMBEDDED_PIC
11606       && S_IS_WEAK (fixP->fx_addsy))
11607     return 1;
11608   if (mips_pic != EMBEDDED_PIC
11609       && (S_IS_WEAK (fixP->fx_addsy)
11610           || S_IS_EXTERNAL (fixP->fx_addsy))
11611       && !S_IS_COMMON (fixP->fx_addsy))
11612     return 1;
11613   if (((bfd_get_section_flags (stdoutput,
11614                                S_GET_SEGMENT (fixP->fx_addsy))
11615         & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11616       || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11617                    ".gnu.linkonce",
11618                    sizeof (".gnu.linkonce") - 1))
11619     return 1;
11620   return 0;
11621 }
11622 #endif
11623
11624 /* Apply a fixup to the object file.  */
11625
11626 void
11627 md_apply_fix3 (fixP, valP, seg)
11628      fixS *fixP;
11629      valueT *valP;
11630      segT seg ATTRIBUTE_UNUSED;
11631 {
11632   bfd_byte *buf;
11633   long insn;
11634   valueT value;
11635   static int previous_fx_r_type = 0;
11636
11637   /* FIXME: Maybe just return for all reloc types not listed below?
11638      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11639   if (fixP->fx_r_type == BFD_RELOC_8)
11640       return;
11641
11642   assert (fixP->fx_size == 4
11643           || fixP->fx_r_type == BFD_RELOC_16
11644           || fixP->fx_r_type == BFD_RELOC_32
11645           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11646           || fixP->fx_r_type == BFD_RELOC_HI16_S
11647           || fixP->fx_r_type == BFD_RELOC_LO16
11648           || fixP->fx_r_type == BFD_RELOC_GPREL16
11649           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11650           || fixP->fx_r_type == BFD_RELOC_GPREL32
11651           || fixP->fx_r_type == BFD_RELOC_64
11652           || fixP->fx_r_type == BFD_RELOC_CTOR
11653           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11654           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11655           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11656           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11657           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11658           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11659           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11660           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11661           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11662
11663   value = *valP;
11664
11665   /* If we aren't adjusting this fixup to be against the section
11666      symbol, we need to adjust the value.  */
11667 #ifdef OBJ_ELF
11668   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11669     {
11670       if (mips_need_elf_addend_fixup (fixP))
11671         {
11672           reloc_howto_type *howto;
11673           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11674
11675           value -= symval;
11676
11677           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11678           if (value != 0 && howto && howto->partial_inplace)
11679             {
11680               /* In this case, the bfd_install_relocation routine will
11681                  incorrectly add the symbol value back in.  We just want
11682                  the addend to appear in the object file.
11683
11684                  The condition above used to include
11685                  "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11686
11687                  However, howto can't be trusted here, because we
11688                  might change the reloc type in tc_gen_reloc.  We can
11689                  check howto->partial_inplace because that conversion
11690                  happens to preserve howto->partial_inplace; but it
11691                  does not preserve howto->pcrel_offset.  I've just
11692                  eliminated the check, because all MIPS PC-relative
11693                  relocations are marked howto->pcrel_offset.
11694
11695                  howto->pcrel_offset was originally added for
11696                  R_MIPS_PC16, which is generated for code like
11697
11698                         globl g1 .text
11699                         .text
11700                         .space 20
11701                  g1:
11702                  x:
11703                         bal g1
11704                */
11705               value -= symval;
11706
11707               /* Make sure the addend is still non-zero.  If it became zero
11708                  after the last operation, set it to a spurious value and
11709                  subtract the same value from the object file's contents.  */
11710               if (value == 0)
11711                 {
11712                   value = 8;
11713
11714                   /* The in-place addends for LO16 relocations are signed;
11715                      leave the matching HI16 in-place addends as zero.  */
11716                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11717                     {
11718                       bfd_vma contents, mask, field;
11719
11720                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11721                                                + fixP->fx_where,
11722                                                fixP->fx_size * 8,
11723                                                target_big_endian);
11724
11725                       /* MASK has bits set where the relocation should go.
11726                          FIELD is -value, shifted into the appropriate place
11727                          for this relocation.  */
11728                       mask = 1 << (howto->bitsize - 1);
11729                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11730                       field = (-value >> howto->rightshift) << howto->bitpos;
11731
11732                       bfd_put_bits ((field & mask) | (contents & ~mask),
11733                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11734                                     fixP->fx_size * 8,
11735                                     target_big_endian);
11736                     }
11737                 }
11738             }
11739         }
11740
11741       /* This code was generated using trial and error and so is
11742          fragile and not trustworthy.  If you change it, you should
11743          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11744          they still pass.  */
11745       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11746         {
11747           value += fixP->fx_frag->fr_address + fixP->fx_where;
11748
11749           /* BFD's REL handling, for MIPS, is _very_ weird.
11750              This gives the right results, but it can't possibly
11751              be the way things are supposed to work.  */
11752           if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11753               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11754             value += fixP->fx_frag->fr_address + fixP->fx_where;
11755         }
11756     }
11757 #endif
11758
11759   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11760
11761   /* We are not done if this is a composite relocation to set up gp.  */
11762   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11763       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11764            || (fixP->fx_r_type == BFD_RELOC_64
11765                && (previous_fx_r_type == BFD_RELOC_GPREL32
11766                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11767            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11768                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11769                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11770     fixP->fx_done = 1;
11771   previous_fx_r_type = fixP->fx_r_type;
11772
11773   switch (fixP->fx_r_type)
11774     {
11775     case BFD_RELOC_MIPS_JMP:
11776     case BFD_RELOC_MIPS_SHIFT5:
11777     case BFD_RELOC_MIPS_SHIFT6:
11778     case BFD_RELOC_MIPS_GOT_DISP:
11779     case BFD_RELOC_MIPS_GOT_PAGE:
11780     case BFD_RELOC_MIPS_GOT_OFST:
11781     case BFD_RELOC_MIPS_SUB:
11782     case BFD_RELOC_MIPS_INSERT_A:
11783     case BFD_RELOC_MIPS_INSERT_B:
11784     case BFD_RELOC_MIPS_DELETE:
11785     case BFD_RELOC_MIPS_HIGHEST:
11786     case BFD_RELOC_MIPS_HIGHER:
11787     case BFD_RELOC_MIPS_SCN_DISP:
11788     case BFD_RELOC_MIPS_REL16:
11789     case BFD_RELOC_MIPS_RELGOT:
11790     case BFD_RELOC_MIPS_JALR:
11791     case BFD_RELOC_HI16:
11792     case BFD_RELOC_HI16_S:
11793     case BFD_RELOC_GPREL16:
11794     case BFD_RELOC_MIPS_LITERAL:
11795     case BFD_RELOC_MIPS_CALL16:
11796     case BFD_RELOC_MIPS_GOT16:
11797     case BFD_RELOC_GPREL32:
11798     case BFD_RELOC_MIPS_GOT_HI16:
11799     case BFD_RELOC_MIPS_GOT_LO16:
11800     case BFD_RELOC_MIPS_CALL_HI16:
11801     case BFD_RELOC_MIPS_CALL_LO16:
11802     case BFD_RELOC_MIPS16_GPREL:
11803       if (fixP->fx_pcrel)
11804         as_bad_where (fixP->fx_file, fixP->fx_line,
11805                       _("Invalid PC relative reloc"));
11806       /* Nothing needed to do. The value comes from the reloc entry */
11807       break;
11808
11809     case BFD_RELOC_MIPS16_JMP:
11810       /* We currently always generate a reloc against a symbol, which
11811          means that we don't want an addend even if the symbol is
11812          defined.  */
11813       fixP->fx_addnumber = 0;
11814       break;
11815
11816     case BFD_RELOC_PCREL_HI16_S:
11817       /* The addend for this is tricky if it is internal, so we just
11818          do everything here rather than in bfd_install_relocation.  */
11819       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11820           && !fixP->fx_done
11821           && value != 0)
11822         break;
11823       if (fixP->fx_addsy
11824           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11825         {
11826           /* For an external symbol adjust by the address to make it
11827              pcrel_offset.  We use the address of the RELLO reloc
11828              which follows this one.  */
11829           value += (fixP->fx_next->fx_frag->fr_address
11830                     + fixP->fx_next->fx_where);
11831         }
11832       value = ((value + 0x8000) >> 16) & 0xffff;
11833       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11834       if (target_big_endian)
11835         buf += 2;
11836       md_number_to_chars ((char *) buf, value, 2);
11837       break;
11838
11839     case BFD_RELOC_PCREL_LO16:
11840       /* The addend for this is tricky if it is internal, so we just
11841          do everything here rather than in bfd_install_relocation.  */
11842       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11843           && !fixP->fx_done
11844           && value != 0)
11845         break;
11846       if (fixP->fx_addsy
11847           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11848         value += fixP->fx_frag->fr_address + fixP->fx_where;
11849       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11850       if (target_big_endian)
11851         buf += 2;
11852       md_number_to_chars ((char *) buf, value, 2);
11853       break;
11854
11855     case BFD_RELOC_64:
11856       /* This is handled like BFD_RELOC_32, but we output a sign
11857          extended value if we are only 32 bits.  */
11858       if (fixP->fx_done
11859           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11860         {
11861           if (8 <= sizeof (valueT))
11862             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11863                                 value, 8);
11864           else
11865             {
11866               long w1, w2;
11867               long hiv;
11868
11869               w1 = w2 = fixP->fx_where;
11870               if (target_big_endian)
11871                 w1 += 4;
11872               else
11873                 w2 += 4;
11874               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11875               if ((value & 0x80000000) != 0)
11876                 hiv = 0xffffffff;
11877               else
11878                 hiv = 0;
11879               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11880             }
11881         }
11882       break;
11883
11884     case BFD_RELOC_RVA:
11885     case BFD_RELOC_32:
11886       /* If we are deleting this reloc entry, we must fill in the
11887          value now.  This can happen if we have a .word which is not
11888          resolved when it appears but is later defined.  We also need
11889          to fill in the value if this is an embedded PIC switch table
11890          entry.  */
11891       if (fixP->fx_done
11892           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11893         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11894                             value, 4);
11895       break;
11896
11897     case BFD_RELOC_16:
11898       /* If we are deleting this reloc entry, we must fill in the
11899          value now.  */
11900       assert (fixP->fx_size == 2);
11901       if (fixP->fx_done)
11902         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11903                             value, 2);
11904       break;
11905
11906     case BFD_RELOC_LO16:
11907       /* When handling an embedded PIC switch statement, we can wind
11908          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11909       if (fixP->fx_done)
11910         {
11911           if (value + 0x8000 > 0xffff)
11912             as_bad_where (fixP->fx_file, fixP->fx_line,
11913                           _("relocation overflow"));
11914           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11915           if (target_big_endian)
11916             buf += 2;
11917           md_number_to_chars ((char *) buf, value, 2);
11918         }
11919       break;
11920
11921     case BFD_RELOC_16_PCREL_S2:
11922       if ((value & 0x3) != 0)
11923         as_bad_where (fixP->fx_file, fixP->fx_line,
11924                       _("Branch to odd address (%lx)"), (long) value);
11925
11926       /*
11927        * We need to save the bits in the instruction since fixup_segment()
11928        * might be deleting the relocation entry (i.e., a branch within
11929        * the current segment).
11930        */
11931       if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
11932         break;
11933       /* If 'value' is zero, the remaining reloc code won't actually
11934          do the store, so it must be done here.  This is probably
11935          a bug somewhere.  */
11936       if (!fixP->fx_done
11937           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11938               || fixP->fx_addsy == NULL                 /* ??? */
11939               || ! S_IS_DEFINED (fixP->fx_addsy)))
11940         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11941
11942       value = (offsetT) value >> 2;
11943
11944       /* update old instruction data */
11945       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11946       if (target_big_endian)
11947         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11948       else
11949         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11950
11951       if (value + 0x8000 <= 0xffff)
11952         insn |= value & 0xffff;
11953       else
11954         {
11955           /* The branch offset is too large.  If this is an
11956              unconditional branch, and we are not generating PIC code,
11957              we can convert it to an absolute jump instruction.  */
11958           if (mips_pic == NO_PIC
11959               && fixP->fx_done
11960               && fixP->fx_frag->fr_address >= text_section->vma
11961               && (fixP->fx_frag->fr_address
11962                   < text_section->vma + text_section->_raw_size)
11963               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11964                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11965                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11966             {
11967               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11968                 insn = 0x0c000000;      /* jal */
11969               else
11970                 insn = 0x08000000;      /* j */
11971               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11972               fixP->fx_done = 0;
11973               fixP->fx_addsy = section_symbol (text_section);
11974               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11975             }
11976           else
11977             {
11978               /* If we got here, we have branch-relaxation disabled,
11979                  and there's nothing we can do to fix this instruction
11980                  without turning it into a longer sequence.  */
11981               as_bad_where (fixP->fx_file, fixP->fx_line,
11982                             _("Branch out of range"));
11983             }
11984         }
11985
11986       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11987       break;
11988
11989     case BFD_RELOC_VTABLE_INHERIT:
11990       fixP->fx_done = 0;
11991       if (fixP->fx_addsy
11992           && !S_IS_DEFINED (fixP->fx_addsy)
11993           && !S_IS_WEAK (fixP->fx_addsy))
11994         S_SET_WEAK (fixP->fx_addsy);
11995       break;
11996
11997     case BFD_RELOC_VTABLE_ENTRY:
11998       fixP->fx_done = 0;
11999       break;
12000
12001     default:
12002       internalError ();
12003     }
12004 }
12005
12006 #if 0
12007 void
12008 printInsn (oc)
12009      unsigned long oc;
12010 {
12011   const struct mips_opcode *p;
12012   int treg, sreg, dreg, shamt;
12013   short imm;
12014   const char *args;
12015   int i;
12016
12017   for (i = 0; i < NUMOPCODES; ++i)
12018     {
12019       p = &mips_opcodes[i];
12020       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
12021         {
12022           printf ("%08lx %s\t", oc, p->name);
12023           treg = (oc >> 16) & 0x1f;
12024           sreg = (oc >> 21) & 0x1f;
12025           dreg = (oc >> 11) & 0x1f;
12026           shamt = (oc >> 6) & 0x1f;
12027           imm = oc;
12028           for (args = p->args;; ++args)
12029             {
12030               switch (*args)
12031                 {
12032                 case '\0':
12033                   printf ("\n");
12034                   break;
12035
12036                 case ',':
12037                 case '(':
12038                 case ')':
12039                   printf ("%c", *args);
12040                   continue;
12041
12042                 case 'r':
12043                   assert (treg == sreg);
12044                   printf ("$%d,$%d", treg, sreg);
12045                   continue;
12046
12047                 case 'd':
12048                 case 'G':
12049                   printf ("$%d", dreg);
12050                   continue;
12051
12052                 case 't':
12053                 case 'E':
12054                   printf ("$%d", treg);
12055                   continue;
12056
12057                 case 'k':
12058                   printf ("0x%x", treg);
12059                   continue;
12060
12061                 case 'b':
12062                 case 's':
12063                   printf ("$%d", sreg);
12064                   continue;
12065
12066                 case 'a':
12067                   printf ("0x%08lx", oc & 0x1ffffff);
12068                   continue;
12069
12070                 case 'i':
12071                 case 'j':
12072                 case 'o':
12073                 case 'u':
12074                   printf ("%d", imm);
12075                   continue;
12076
12077                 case '<':
12078                 case '>':
12079                   printf ("$%d", shamt);
12080                   continue;
12081
12082                 default:
12083                   internalError ();
12084                 }
12085               break;
12086             }
12087           return;
12088         }
12089     }
12090   printf (_("%08lx  UNDEFINED\n"), oc);
12091 }
12092 #endif
12093
12094 static symbolS *
12095 get_symbol ()
12096 {
12097   int c;
12098   char *name;
12099   symbolS *p;
12100
12101   name = input_line_pointer;
12102   c = get_symbol_end ();
12103   p = (symbolS *) symbol_find_or_make (name);
12104   *input_line_pointer = c;
12105   return p;
12106 }
12107
12108 /* Align the current frag to a given power of two.  The MIPS assembler
12109    also automatically adjusts any preceding label.  */
12110
12111 static void
12112 mips_align (to, fill, label)
12113      int to;
12114      int fill;
12115      symbolS *label;
12116 {
12117   mips_emit_delays (FALSE);
12118   frag_align (to, fill, 0);
12119   record_alignment (now_seg, to);
12120   if (label != NULL)
12121     {
12122       assert (S_GET_SEGMENT (label) == now_seg);
12123       symbol_set_frag (label, frag_now);
12124       S_SET_VALUE (label, (valueT) frag_now_fix ());
12125     }
12126 }
12127
12128 /* Align to a given power of two.  .align 0 turns off the automatic
12129    alignment used by the data creating pseudo-ops.  */
12130
12131 static void
12132 s_align (x)
12133      int x ATTRIBUTE_UNUSED;
12134 {
12135   register int temp;
12136   register long temp_fill;
12137   long max_alignment = 15;
12138
12139   /*
12140
12141     o  Note that the assembler pulls down any immediately preceeding label
12142        to the aligned address.
12143     o  It's not documented but auto alignment is reinstated by
12144        a .align pseudo instruction.
12145     o  Note also that after auto alignment is turned off the mips assembler
12146        issues an error on attempt to assemble an improperly aligned data item.
12147        We don't.
12148
12149     */
12150
12151   temp = get_absolute_expression ();
12152   if (temp > max_alignment)
12153     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
12154   else if (temp < 0)
12155     {
12156       as_warn (_("Alignment negative: 0 assumed."));
12157       temp = 0;
12158     }
12159   if (*input_line_pointer == ',')
12160     {
12161       ++input_line_pointer;
12162       temp_fill = get_absolute_expression ();
12163     }
12164   else
12165     temp_fill = 0;
12166   if (temp)
12167     {
12168       auto_align = 1;
12169       mips_align (temp, (int) temp_fill,
12170                   insn_labels != NULL ? insn_labels->label : NULL);
12171     }
12172   else
12173     {
12174       auto_align = 0;
12175     }
12176
12177   demand_empty_rest_of_line ();
12178 }
12179
12180 void
12181 mips_flush_pending_output ()
12182 {
12183   mips_emit_delays (FALSE);
12184   mips_clear_insn_labels ();
12185 }
12186
12187 static void
12188 s_change_sec (sec)
12189      int sec;
12190 {
12191   segT seg;
12192
12193   /* When generating embedded PIC code, we only use the .text, .lit8,
12194      .sdata and .sbss sections.  We change the .data and .rdata
12195      pseudo-ops to use .sdata.  */
12196   if (mips_pic == EMBEDDED_PIC
12197       && (sec == 'd' || sec == 'r'))
12198     sec = 's';
12199
12200 #ifdef OBJ_ELF
12201   /* The ELF backend needs to know that we are changing sections, so
12202      that .previous works correctly.  We could do something like check
12203      for an obj_section_change_hook macro, but that might be confusing
12204      as it would not be appropriate to use it in the section changing
12205      functions in read.c, since obj-elf.c intercepts those.  FIXME:
12206      This should be cleaner, somehow.  */
12207   obj_elf_section_change_hook ();
12208 #endif
12209
12210   mips_emit_delays (FALSE);
12211   switch (sec)
12212     {
12213     case 't':
12214       s_text (0);
12215       break;
12216     case 'd':
12217       s_data (0);
12218       break;
12219     case 'b':
12220       subseg_set (bss_section, (subsegT) get_absolute_expression ());
12221       demand_empty_rest_of_line ();
12222       break;
12223
12224     case 'r':
12225       if (USE_GLOBAL_POINTER_OPT)
12226         {
12227           seg = subseg_new (RDATA_SECTION_NAME,
12228                             (subsegT) get_absolute_expression ());
12229           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12230             {
12231               bfd_set_section_flags (stdoutput, seg,
12232                                      (SEC_ALLOC
12233                                       | SEC_LOAD
12234                                       | SEC_READONLY
12235                                       | SEC_RELOC
12236                                       | SEC_DATA));
12237               if (strcmp (TARGET_OS, "elf") != 0)
12238                 record_alignment (seg, 4);
12239             }
12240           demand_empty_rest_of_line ();
12241         }
12242       else
12243         {
12244           as_bad (_("No read only data section in this object file format"));
12245           demand_empty_rest_of_line ();
12246           return;
12247         }
12248       break;
12249
12250     case 's':
12251       if (USE_GLOBAL_POINTER_OPT)
12252         {
12253           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12254           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12255             {
12256               bfd_set_section_flags (stdoutput, seg,
12257                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
12258                                      | SEC_DATA);
12259               if (strcmp (TARGET_OS, "elf") != 0)
12260                 record_alignment (seg, 4);
12261             }
12262           demand_empty_rest_of_line ();
12263           break;
12264         }
12265       else
12266         {
12267           as_bad (_("Global pointers not supported; recompile -G 0"));
12268           demand_empty_rest_of_line ();
12269           return;
12270         }
12271     }
12272
12273   auto_align = 1;
12274 }
12275
12276 void
12277 s_change_section (ignore)
12278      int ignore ATTRIBUTE_UNUSED;
12279 {
12280 #ifdef OBJ_ELF
12281   char *section_name;
12282   char c;
12283   char next_c = 0;
12284   int section_type;
12285   int section_flag;
12286   int section_entry_size;
12287   int section_alignment;
12288
12289   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12290     return;
12291
12292   section_name = input_line_pointer;
12293   c = get_symbol_end ();
12294   if (c)
12295     next_c = *(input_line_pointer + 1);
12296
12297   /* Do we have .section Name<,"flags">?  */
12298   if (c != ',' || (c == ',' && next_c == '"'))
12299     {
12300       /* just after name is now '\0'.  */
12301       *input_line_pointer = c;
12302       input_line_pointer = section_name;
12303       obj_elf_section (ignore);
12304       return;
12305     }
12306   input_line_pointer++;
12307
12308   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
12309   if (c == ',')
12310     section_type = get_absolute_expression ();
12311   else
12312     section_type = 0;
12313   if (*input_line_pointer++ == ',')
12314     section_flag = get_absolute_expression ();
12315   else
12316     section_flag = 0;
12317   if (*input_line_pointer++ == ',')
12318     section_entry_size = get_absolute_expression ();
12319   else
12320     section_entry_size = 0;
12321   if (*input_line_pointer++ == ',')
12322     section_alignment = get_absolute_expression ();
12323   else
12324     section_alignment = 0;
12325
12326   section_name = xstrdup (section_name);
12327
12328   obj_elf_change_section (section_name, section_type, section_flag,
12329                           section_entry_size, 0, 0, 0);
12330
12331   if (now_seg->name != section_name)
12332     free (section_name);
12333 #endif /* OBJ_ELF */
12334 }
12335
12336 void
12337 mips_enable_auto_align ()
12338 {
12339   auto_align = 1;
12340 }
12341
12342 static void
12343 s_cons (log_size)
12344      int log_size;
12345 {
12346   symbolS *label;
12347
12348   label = insn_labels != NULL ? insn_labels->label : NULL;
12349   mips_emit_delays (FALSE);
12350   if (log_size > 0 && auto_align)
12351     mips_align (log_size, 0, label);
12352   mips_clear_insn_labels ();
12353   cons (1 << log_size);
12354 }
12355
12356 static void
12357 s_float_cons (type)
12358      int type;
12359 {
12360   symbolS *label;
12361
12362   label = insn_labels != NULL ? insn_labels->label : NULL;
12363
12364   mips_emit_delays (FALSE);
12365
12366   if (auto_align)
12367     {
12368       if (type == 'd')
12369         mips_align (3, 0, label);
12370       else
12371         mips_align (2, 0, label);
12372     }
12373
12374   mips_clear_insn_labels ();
12375
12376   float_cons (type);
12377 }
12378
12379 /* Handle .globl.  We need to override it because on Irix 5 you are
12380    permitted to say
12381        .globl foo .text
12382    where foo is an undefined symbol, to mean that foo should be
12383    considered to be the address of a function.  */
12384
12385 static void
12386 s_mips_globl (x)
12387      int x ATTRIBUTE_UNUSED;
12388 {
12389   char *name;
12390   int c;
12391   symbolS *symbolP;
12392   flagword flag;
12393
12394   name = input_line_pointer;
12395   c = get_symbol_end ();
12396   symbolP = symbol_find_or_make (name);
12397   *input_line_pointer = c;
12398   SKIP_WHITESPACE ();
12399
12400   /* On Irix 5, every global symbol that is not explicitly labelled as
12401      being a function is apparently labelled as being an object.  */
12402   flag = BSF_OBJECT;
12403
12404   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12405     {
12406       char *secname;
12407       asection *sec;
12408
12409       secname = input_line_pointer;
12410       c = get_symbol_end ();
12411       sec = bfd_get_section_by_name (stdoutput, secname);
12412       if (sec == NULL)
12413         as_bad (_("%s: no such section"), secname);
12414       *input_line_pointer = c;
12415
12416       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12417         flag = BSF_FUNCTION;
12418     }
12419
12420   symbol_get_bfdsym (symbolP)->flags |= flag;
12421
12422   S_SET_EXTERNAL (symbolP);
12423   demand_empty_rest_of_line ();
12424 }
12425
12426 static void
12427 s_option (x)
12428      int x ATTRIBUTE_UNUSED;
12429 {
12430   char *opt;
12431   char c;
12432
12433   opt = input_line_pointer;
12434   c = get_symbol_end ();
12435
12436   if (*opt == 'O')
12437     {
12438       /* FIXME: What does this mean?  */
12439     }
12440   else if (strncmp (opt, "pic", 3) == 0)
12441     {
12442       int i;
12443
12444       i = atoi (opt + 3);
12445       if (i == 0)
12446         mips_pic = NO_PIC;
12447       else if (i == 2)
12448         {
12449         mips_pic = SVR4_PIC;
12450           mips_abicalls = TRUE;
12451         }
12452       else
12453         as_bad (_(".option pic%d not supported"), i);
12454
12455       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12456         {
12457           if (g_switch_seen && g_switch_value != 0)
12458             as_warn (_("-G may not be used with SVR4 PIC code"));
12459           g_switch_value = 0;
12460           bfd_set_gp_size (stdoutput, 0);
12461         }
12462     }
12463   else
12464     as_warn (_("Unrecognized option \"%s\""), opt);
12465
12466   *input_line_pointer = c;
12467   demand_empty_rest_of_line ();
12468 }
12469
12470 /* This structure is used to hold a stack of .set values.  */
12471
12472 struct mips_option_stack
12473 {
12474   struct mips_option_stack *next;
12475   struct mips_set_options options;
12476 };
12477
12478 static struct mips_option_stack *mips_opts_stack;
12479
12480 /* Handle the .set pseudo-op.  */
12481
12482 static void
12483 s_mipsset (x)
12484      int x ATTRIBUTE_UNUSED;
12485 {
12486   char *name = input_line_pointer, ch;
12487
12488   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12489     ++input_line_pointer;
12490   ch = *input_line_pointer;
12491   *input_line_pointer = '\0';
12492
12493   if (strcmp (name, "reorder") == 0)
12494     {
12495       if (mips_opts.noreorder && prev_nop_frag != NULL)
12496         {
12497           /* If we still have pending nops, we can discard them.  The
12498              usual nop handling will insert any that are still
12499              needed.  */
12500           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12501                                     * (mips_opts.mips16 ? 2 : 4));
12502           prev_nop_frag = NULL;
12503         }
12504       mips_opts.noreorder = 0;
12505     }
12506   else if (strcmp (name, "noreorder") == 0)
12507     {
12508       mips_emit_delays (TRUE);
12509       mips_opts.noreorder = 1;
12510       mips_any_noreorder = 1;
12511     }
12512   else if (strcmp (name, "at") == 0)
12513     {
12514       mips_opts.noat = 0;
12515     }
12516   else if (strcmp (name, "noat") == 0)
12517     {
12518       mips_opts.noat = 1;
12519     }
12520   else if (strcmp (name, "macro") == 0)
12521     {
12522       mips_opts.warn_about_macros = 0;
12523     }
12524   else if (strcmp (name, "nomacro") == 0)
12525     {
12526       if (mips_opts.noreorder == 0)
12527         as_bad (_("`noreorder' must be set before `nomacro'"));
12528       mips_opts.warn_about_macros = 1;
12529     }
12530   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12531     {
12532       mips_opts.nomove = 0;
12533     }
12534   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12535     {
12536       mips_opts.nomove = 1;
12537     }
12538   else if (strcmp (name, "bopt") == 0)
12539     {
12540       mips_opts.nobopt = 0;
12541     }
12542   else if (strcmp (name, "nobopt") == 0)
12543     {
12544       mips_opts.nobopt = 1;
12545     }
12546   else if (strcmp (name, "mips16") == 0
12547            || strcmp (name, "MIPS-16") == 0)
12548     mips_opts.mips16 = 1;
12549   else if (strcmp (name, "nomips16") == 0
12550            || strcmp (name, "noMIPS-16") == 0)
12551     mips_opts.mips16 = 0;
12552   else if (strcmp (name, "mips3d") == 0)
12553     mips_opts.ase_mips3d = 1;
12554   else if (strcmp (name, "nomips3d") == 0)
12555     mips_opts.ase_mips3d = 0;
12556   else if (strcmp (name, "mdmx") == 0)
12557     mips_opts.ase_mdmx = 1;
12558   else if (strcmp (name, "nomdmx") == 0)
12559     mips_opts.ase_mdmx = 0;
12560   else if (strncmp (name, "mips", 4) == 0)
12561     {
12562       int reset = 0;
12563
12564       /* Permit the user to change the ISA on the fly.  Needless to
12565          say, misuse can cause serious problems.  */
12566       if (strcmp (name, "mips0") == 0)
12567         {
12568           reset = 1;
12569           mips_opts.isa = file_mips_isa;
12570         }
12571       else if (strcmp (name, "mips1") == 0)
12572         mips_opts.isa = ISA_MIPS1;
12573       else if (strcmp (name, "mips2") == 0)
12574         mips_opts.isa = ISA_MIPS2;
12575       else if (strcmp (name, "mips3") == 0)
12576         mips_opts.isa = ISA_MIPS3;
12577       else if (strcmp (name, "mips4") == 0)
12578         mips_opts.isa = ISA_MIPS4;
12579       else if (strcmp (name, "mips5") == 0)
12580         mips_opts.isa = ISA_MIPS5;
12581       else if (strcmp (name, "mips32") == 0)
12582         mips_opts.isa = ISA_MIPS32;
12583       else if (strcmp (name, "mips32r2") == 0)
12584         mips_opts.isa = ISA_MIPS32R2;
12585       else if (strcmp (name, "mips64") == 0)
12586         mips_opts.isa = ISA_MIPS64;
12587       else
12588         as_bad (_("unknown ISA level %s"), name + 4);
12589
12590       switch (mips_opts.isa)
12591         {
12592         case  0:
12593           break;
12594         case ISA_MIPS1:
12595         case ISA_MIPS2:
12596         case ISA_MIPS32:
12597         case ISA_MIPS32R2:
12598           mips_opts.gp32 = 1;
12599           mips_opts.fp32 = 1;
12600           break;
12601         case ISA_MIPS3:
12602         case ISA_MIPS4:
12603         case ISA_MIPS5:
12604         case ISA_MIPS64:
12605           mips_opts.gp32 = 0;
12606           mips_opts.fp32 = 0;
12607           break;
12608         default:
12609           as_bad (_("unknown ISA level %s"), name + 4);
12610           break;
12611         }
12612       if (reset)
12613         {
12614           mips_opts.gp32 = file_mips_gp32;
12615           mips_opts.fp32 = file_mips_fp32;
12616         }
12617     }
12618   else if (strcmp (name, "autoextend") == 0)
12619     mips_opts.noautoextend = 0;
12620   else if (strcmp (name, "noautoextend") == 0)
12621     mips_opts.noautoextend = 1;
12622   else if (strcmp (name, "push") == 0)
12623     {
12624       struct mips_option_stack *s;
12625
12626       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12627       s->next = mips_opts_stack;
12628       s->options = mips_opts;
12629       mips_opts_stack = s;
12630     }
12631   else if (strcmp (name, "pop") == 0)
12632     {
12633       struct mips_option_stack *s;
12634
12635       s = mips_opts_stack;
12636       if (s == NULL)
12637         as_bad (_(".set pop with no .set push"));
12638       else
12639         {
12640           /* If we're changing the reorder mode we need to handle
12641              delay slots correctly.  */
12642           if (s->options.noreorder && ! mips_opts.noreorder)
12643             mips_emit_delays (TRUE);
12644           else if (! s->options.noreorder && mips_opts.noreorder)
12645             {
12646               if (prev_nop_frag != NULL)
12647                 {
12648                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12649                                             * (mips_opts.mips16 ? 2 : 4));
12650                   prev_nop_frag = NULL;
12651                 }
12652             }
12653
12654           mips_opts = s->options;
12655           mips_opts_stack = s->next;
12656           free (s);
12657         }
12658     }
12659   else
12660     {
12661       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12662     }
12663   *input_line_pointer = ch;
12664   demand_empty_rest_of_line ();
12665 }
12666
12667 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12668    .option pic2.  It means to generate SVR4 PIC calls.  */
12669
12670 static void
12671 s_abicalls (ignore)
12672      int ignore ATTRIBUTE_UNUSED;
12673 {
12674   mips_pic = SVR4_PIC;
12675   mips_abicalls = TRUE;
12676   if (USE_GLOBAL_POINTER_OPT)
12677     {
12678       if (g_switch_seen && g_switch_value != 0)
12679         as_warn (_("-G may not be used with SVR4 PIC code"));
12680       g_switch_value = 0;
12681     }
12682   bfd_set_gp_size (stdoutput, 0);
12683   demand_empty_rest_of_line ();
12684 }
12685
12686 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12687    PIC code.  It sets the $gp register for the function based on the
12688    function address, which is in the register named in the argument.
12689    This uses a relocation against _gp_disp, which is handled specially
12690    by the linker.  The result is:
12691         lui     $gp,%hi(_gp_disp)
12692         addiu   $gp,$gp,%lo(_gp_disp)
12693         addu    $gp,$gp,.cpload argument
12694    The .cpload argument is normally $25 == $t9.  */
12695
12696 static void
12697 s_cpload (ignore)
12698      int ignore ATTRIBUTE_UNUSED;
12699 {
12700   expressionS ex;
12701   int icnt = 0;
12702
12703   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12704      .cpload is ignored.  */
12705   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12706     {
12707       s_ignore (0);
12708       return;
12709     }
12710
12711   /* .cpload should be in a .set noreorder section.  */
12712   if (mips_opts.noreorder == 0)
12713     as_warn (_(".cpload not in noreorder section"));
12714
12715   ex.X_op = O_symbol;
12716   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12717   ex.X_op_symbol = NULL;
12718   ex.X_add_number = 0;
12719
12720   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12721   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12722
12723   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12724   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12725                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12726
12727   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12728                mips_gp_register, mips_gp_register, tc_get_register (0));
12729
12730   demand_empty_rest_of_line ();
12731 }
12732
12733 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12734      .cpsetup $reg1, offset|$reg2, label
12735
12736    If offset is given, this results in:
12737      sd         $gp, offset($sp)
12738      lui        $gp, %hi(%neg(%gp_rel(label)))
12739      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12740      daddu      $gp, $gp, $reg1
12741
12742    If $reg2 is given, this results in:
12743      daddu      $reg2, $gp, $0
12744      lui        $gp, %hi(%neg(%gp_rel(label)))
12745      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12746      daddu      $gp, $gp, $reg1
12747    $reg1 is normally $25 == $t9.  */
12748 static void
12749 s_cpsetup (ignore)
12750      int ignore ATTRIBUTE_UNUSED;
12751 {
12752   expressionS ex_off;
12753   expressionS ex_sym;
12754   int reg1;
12755   int icnt = 0;
12756   char *f;
12757
12758   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12759      We also need NewABI support.  */
12760   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12761     {
12762       s_ignore (0);
12763       return;
12764     }
12765
12766   reg1 = tc_get_register (0);
12767   SKIP_WHITESPACE ();
12768   if (*input_line_pointer != ',')
12769     {
12770       as_bad (_("missing argument separator ',' for .cpsetup"));
12771       return;
12772     }
12773   else
12774     ++input_line_pointer;
12775   SKIP_WHITESPACE ();
12776   if (*input_line_pointer == '$')
12777     {
12778       mips_cpreturn_register = tc_get_register (0);
12779       mips_cpreturn_offset = -1;
12780     }
12781   else
12782     {
12783       mips_cpreturn_offset = get_absolute_expression ();
12784       mips_cpreturn_register = -1;
12785     }
12786   SKIP_WHITESPACE ();
12787   if (*input_line_pointer != ',')
12788     {
12789       as_bad (_("missing argument separator ',' for .cpsetup"));
12790       return;
12791     }
12792   else
12793     ++input_line_pointer;
12794   SKIP_WHITESPACE ();
12795   expression (&ex_sym);
12796
12797   if (mips_cpreturn_register == -1)
12798     {
12799       ex_off.X_op = O_constant;
12800       ex_off.X_add_symbol = NULL;
12801       ex_off.X_op_symbol = NULL;
12802       ex_off.X_add_number = mips_cpreturn_offset;
12803
12804       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12805                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12806     }
12807   else
12808     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12809                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12810
12811   /* Ensure there's room for the next two instructions, so that `f'
12812      doesn't end up with an address in the wrong frag.  */
12813   frag_grow (8);
12814   f = frag_more (0);
12815   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12816                (int) BFD_RELOC_GPREL16);
12817   fix_new (frag_now, f - frag_now->fr_literal,
12818            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12819   fix_new (frag_now, f - frag_now->fr_literal,
12820            4, NULL, 0, 0, BFD_RELOC_HI16_S);
12821
12822   f = frag_more (0);
12823   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12824                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12825   fix_new (frag_now, f - frag_now->fr_literal,
12826            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12827   fix_new (frag_now, f - frag_now->fr_literal,
12828            4, NULL, 0, 0, BFD_RELOC_LO16);
12829
12830   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12831                HAVE_64BIT_ADDRESSES ? "daddu" : "add", "d,v,t",
12832                mips_gp_register, mips_gp_register, reg1);
12833
12834   demand_empty_rest_of_line ();
12835 }
12836
12837 static void
12838 s_cplocal (ignore)
12839      int ignore ATTRIBUTE_UNUSED;
12840 {
12841   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12842    .cplocal is ignored.  */
12843   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12844     {
12845       s_ignore (0);
12846       return;
12847     }
12848
12849   mips_gp_register = tc_get_register (0);
12850   demand_empty_rest_of_line ();
12851 }
12852
12853 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12854    offset from $sp.  The offset is remembered, and after making a PIC
12855    call $gp is restored from that location.  */
12856
12857 static void
12858 s_cprestore (ignore)
12859      int ignore ATTRIBUTE_UNUSED;
12860 {
12861   expressionS ex;
12862   int icnt = 0;
12863
12864   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12865      .cprestore is ignored.  */
12866   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12867     {
12868       s_ignore (0);
12869       return;
12870     }
12871
12872   mips_cprestore_offset = get_absolute_expression ();
12873   mips_cprestore_valid = 1;
12874
12875   ex.X_op = O_constant;
12876   ex.X_add_symbol = NULL;
12877   ex.X_op_symbol = NULL;
12878   ex.X_add_number = mips_cprestore_offset;
12879
12880   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12881                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12882                                 mips_gp_register, SP);
12883
12884   demand_empty_rest_of_line ();
12885 }
12886
12887 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12888    was given in the preceeding .gpsetup, it results in:
12889      ld         $gp, offset($sp)
12890
12891    If a register $reg2 was given there, it results in:
12892      daddiu     $gp, $gp, $reg2
12893  */
12894 static void
12895 s_cpreturn (ignore)
12896      int ignore ATTRIBUTE_UNUSED;
12897 {
12898   expressionS ex;
12899   int icnt = 0;
12900
12901   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12902      We also need NewABI support.  */
12903   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12904     {
12905       s_ignore (0);
12906       return;
12907     }
12908
12909   if (mips_cpreturn_register == -1)
12910     {
12911       ex.X_op = O_constant;
12912       ex.X_add_symbol = NULL;
12913       ex.X_op_symbol = NULL;
12914       ex.X_add_number = mips_cpreturn_offset;
12915
12916       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12917                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12918     }
12919   else
12920     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12921                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12922
12923   demand_empty_rest_of_line ();
12924 }
12925
12926 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12927    code.  It sets the offset to use in gp_rel relocations.  */
12928
12929 static void
12930 s_gpvalue (ignore)
12931      int ignore ATTRIBUTE_UNUSED;
12932 {
12933   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12934      We also need NewABI support.  */
12935   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12936     {
12937       s_ignore (0);
12938       return;
12939     }
12940
12941   mips_gprel_offset = get_absolute_expression ();
12942
12943   demand_empty_rest_of_line ();
12944 }
12945
12946 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12947    code.  It generates a 32 bit GP relative reloc.  */
12948
12949 static void
12950 s_gpword (ignore)
12951      int ignore ATTRIBUTE_UNUSED;
12952 {
12953   symbolS *label;
12954   expressionS ex;
12955   char *p;
12956
12957   /* When not generating PIC code, this is treated as .word.  */
12958   if (mips_pic != SVR4_PIC)
12959     {
12960       s_cons (2);
12961       return;
12962     }
12963
12964   label = insn_labels != NULL ? insn_labels->label : NULL;
12965   mips_emit_delays (TRUE);
12966   if (auto_align)
12967     mips_align (2, 0, label);
12968   mips_clear_insn_labels ();
12969
12970   expression (&ex);
12971
12972   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12973     {
12974       as_bad (_("Unsupported use of .gpword"));
12975       ignore_rest_of_line ();
12976     }
12977
12978   p = frag_more (4);
12979   md_number_to_chars (p, (valueT) 0, 4);
12980   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12981                BFD_RELOC_GPREL32);
12982
12983   demand_empty_rest_of_line ();
12984 }
12985
12986 static void
12987 s_gpdword (ignore)
12988      int ignore ATTRIBUTE_UNUSED;
12989 {
12990   symbolS *label;
12991   expressionS ex;
12992   char *p;
12993
12994   /* When not generating PIC code, this is treated as .dword.  */
12995   if (mips_pic != SVR4_PIC)
12996     {
12997       s_cons (3);
12998       return;
12999     }
13000
13001   label = insn_labels != NULL ? insn_labels->label : NULL;
13002   mips_emit_delays (TRUE);
13003   if (auto_align)
13004     mips_align (3, 0, label);
13005   mips_clear_insn_labels ();
13006
13007   expression (&ex);
13008
13009   if (ex.X_op != O_symbol || ex.X_add_number != 0)
13010     {
13011       as_bad (_("Unsupported use of .gpdword"));
13012       ignore_rest_of_line ();
13013     }
13014
13015   p = frag_more (8);
13016   md_number_to_chars (p, (valueT) 0, 8);
13017   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
13018                BFD_RELOC_GPREL32);
13019
13020   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
13021   ex.X_op = O_absent;
13022   ex.X_add_symbol = 0;
13023   ex.X_add_number = 0;
13024   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
13025                BFD_RELOC_64);
13026
13027   demand_empty_rest_of_line ();
13028 }
13029
13030 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
13031    tables in SVR4 PIC code.  */
13032
13033 static void
13034 s_cpadd (ignore)
13035      int ignore ATTRIBUTE_UNUSED;
13036 {
13037   int icnt = 0;
13038   int reg;
13039
13040   /* This is ignored when not generating SVR4 PIC code.  */
13041   if (mips_pic != SVR4_PIC)
13042     {
13043       s_ignore (0);
13044       return;
13045     }
13046
13047   /* Add $gp to the register named as an argument.  */
13048   reg = tc_get_register (0);
13049   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
13050                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI ? "add" : "addu" : "daddu",
13051                "d,v,t", reg, reg, mips_gp_register);
13052
13053   demand_empty_rest_of_line ();
13054 }
13055
13056 /* Handle the .insn pseudo-op.  This marks instruction labels in
13057    mips16 mode.  This permits the linker to handle them specially,
13058    such as generating jalx instructions when needed.  We also make
13059    them odd for the duration of the assembly, in order to generate the
13060    right sort of code.  We will make them even in the adjust_symtab
13061    routine, while leaving them marked.  This is convenient for the
13062    debugger and the disassembler.  The linker knows to make them odd
13063    again.  */
13064
13065 static void
13066 s_insn (ignore)
13067      int ignore ATTRIBUTE_UNUSED;
13068 {
13069   mips16_mark_labels ();
13070
13071   demand_empty_rest_of_line ();
13072 }
13073
13074 /* Handle a .stabn directive.  We need these in order to mark a label
13075    as being a mips16 text label correctly.  Sometimes the compiler
13076    will emit a label, followed by a .stabn, and then switch sections.
13077    If the label and .stabn are in mips16 mode, then the label is
13078    really a mips16 text label.  */
13079
13080 static void
13081 s_mips_stab (type)
13082      int type;
13083 {
13084   if (type == 'n')
13085     mips16_mark_labels ();
13086
13087   s_stab (type);
13088 }
13089
13090 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
13091  */
13092
13093 static void
13094 s_mips_weakext (ignore)
13095      int ignore ATTRIBUTE_UNUSED;
13096 {
13097   char *name;
13098   int c;
13099   symbolS *symbolP;
13100   expressionS exp;
13101
13102   name = input_line_pointer;
13103   c = get_symbol_end ();
13104   symbolP = symbol_find_or_make (name);
13105   S_SET_WEAK (symbolP);
13106   *input_line_pointer = c;
13107
13108   SKIP_WHITESPACE ();
13109
13110   if (! is_end_of_line[(unsigned char) *input_line_pointer])
13111     {
13112       if (S_IS_DEFINED (symbolP))
13113         {
13114           as_bad ("ignoring attempt to redefine symbol %s",
13115                   S_GET_NAME (symbolP));
13116           ignore_rest_of_line ();
13117           return;
13118         }
13119
13120       if (*input_line_pointer == ',')
13121         {
13122           ++input_line_pointer;
13123           SKIP_WHITESPACE ();
13124         }
13125
13126       expression (&exp);
13127       if (exp.X_op != O_symbol)
13128         {
13129           as_bad ("bad .weakext directive");
13130           ignore_rest_of_line ();
13131           return;
13132         }
13133       symbol_set_value_expression (symbolP, &exp);
13134     }
13135
13136   demand_empty_rest_of_line ();
13137 }
13138
13139 /* Parse a register string into a number.  Called from the ECOFF code
13140    to parse .frame.  The argument is non-zero if this is the frame
13141    register, so that we can record it in mips_frame_reg.  */
13142
13143 int
13144 tc_get_register (frame)
13145      int frame;
13146 {
13147   int reg;
13148
13149   SKIP_WHITESPACE ();
13150   if (*input_line_pointer++ != '$')
13151     {
13152       as_warn (_("expected `$'"));
13153       reg = ZERO;
13154     }
13155   else if (ISDIGIT (*input_line_pointer))
13156     {
13157       reg = get_absolute_expression ();
13158       if (reg < 0 || reg >= 32)
13159         {
13160           as_warn (_("Bad register number"));
13161           reg = ZERO;
13162         }
13163     }
13164   else
13165     {
13166       if (strncmp (input_line_pointer, "ra", 2) == 0)
13167         {
13168           reg = RA;
13169           input_line_pointer += 2;
13170         }
13171       else if (strncmp (input_line_pointer, "fp", 2) == 0)
13172         {
13173           reg = FP;
13174           input_line_pointer += 2;
13175         }
13176       else if (strncmp (input_line_pointer, "sp", 2) == 0)
13177         {
13178           reg = SP;
13179           input_line_pointer += 2;
13180         }
13181       else if (strncmp (input_line_pointer, "gp", 2) == 0)
13182         {
13183           reg = GP;
13184           input_line_pointer += 2;
13185         }
13186       else if (strncmp (input_line_pointer, "at", 2) == 0)
13187         {
13188           reg = AT;
13189           input_line_pointer += 2;
13190         }
13191       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
13192         {
13193           reg = KT0;
13194           input_line_pointer += 3;
13195         }
13196       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
13197         {
13198           reg = KT1;
13199           input_line_pointer += 3;
13200         }
13201       else if (strncmp (input_line_pointer, "zero", 4) == 0)
13202         {
13203           reg = ZERO;
13204           input_line_pointer += 4;
13205         }
13206       else
13207         {
13208           as_warn (_("Unrecognized register name"));
13209           reg = ZERO;
13210           while (ISALNUM(*input_line_pointer))
13211            input_line_pointer++;
13212         }
13213     }
13214   if (frame)
13215     {
13216       mips_frame_reg = reg != 0 ? reg : SP;
13217       mips_frame_reg_valid = 1;
13218       mips_cprestore_valid = 0;
13219     }
13220   return reg;
13221 }
13222
13223 valueT
13224 md_section_align (seg, addr)
13225      asection *seg;
13226      valueT addr;
13227 {
13228   int align = bfd_get_section_alignment (stdoutput, seg);
13229
13230 #ifdef OBJ_ELF
13231   /* We don't need to align ELF sections to the full alignment.
13232      However, Irix 5 may prefer that we align them at least to a 16
13233      byte boundary.  We don't bother to align the sections if we are
13234      targeted for an embedded system.  */
13235   if (strcmp (TARGET_OS, "elf") == 0)
13236     return addr;
13237   if (align > 4)
13238     align = 4;
13239 #endif
13240
13241   return ((addr + (1 << align) - 1) & (-1 << align));
13242 }
13243
13244 /* Utility routine, called from above as well.  If called while the
13245    input file is still being read, it's only an approximation.  (For
13246    example, a symbol may later become defined which appeared to be
13247    undefined earlier.)  */
13248
13249 static int
13250 nopic_need_relax (sym, before_relaxing)
13251      symbolS *sym;
13252      int before_relaxing;
13253 {
13254   if (sym == 0)
13255     return 0;
13256
13257   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
13258     {
13259       const char *symname;
13260       int change;
13261
13262       /* Find out whether this symbol can be referenced off the $gp
13263          register.  It can be if it is smaller than the -G size or if
13264          it is in the .sdata or .sbss section.  Certain symbols can
13265          not be referenced off the $gp, although it appears as though
13266          they can.  */
13267       symname = S_GET_NAME (sym);
13268       if (symname != (const char *) NULL
13269           && (strcmp (symname, "eprol") == 0
13270               || strcmp (symname, "etext") == 0
13271               || strcmp (symname, "_gp") == 0
13272               || strcmp (symname, "edata") == 0
13273               || strcmp (symname, "_fbss") == 0
13274               || strcmp (symname, "_fdata") == 0
13275               || strcmp (symname, "_ftext") == 0
13276               || strcmp (symname, "end") == 0
13277               || strcmp (symname, "_gp_disp") == 0))
13278         change = 1;
13279       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13280                && (0
13281 #ifndef NO_ECOFF_DEBUGGING
13282                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
13283                        && (symbol_get_obj (sym)->ecoff_extern_size
13284                            <= g_switch_value))
13285 #endif
13286                    /* We must defer this decision until after the whole
13287                       file has been read, since there might be a .extern
13288                       after the first use of this symbol.  */
13289                    || (before_relaxing
13290 #ifndef NO_ECOFF_DEBUGGING
13291                        && symbol_get_obj (sym)->ecoff_extern_size == 0
13292 #endif
13293                        && S_GET_VALUE (sym) == 0)
13294                    || (S_GET_VALUE (sym) != 0
13295                        && S_GET_VALUE (sym) <= g_switch_value)))
13296         change = 0;
13297       else
13298         {
13299           const char *segname;
13300
13301           segname = segment_name (S_GET_SEGMENT (sym));
13302           assert (strcmp (segname, ".lit8") != 0
13303                   && strcmp (segname, ".lit4") != 0);
13304           change = (strcmp (segname, ".sdata") != 0
13305                     && strcmp (segname, ".sbss") != 0
13306                     && strncmp (segname, ".sdata.", 7) != 0
13307                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
13308         }
13309       return change;
13310     }
13311   else
13312     /* We are not optimizing for the $gp register.  */
13313     return 1;
13314 }
13315
13316
13317 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
13318
13319 static bfd_boolean
13320 pic_need_relax (sym, segtype)
13321      symbolS *sym;
13322      asection *segtype;
13323 {
13324   asection *symsec;
13325   bfd_boolean linkonce;
13326
13327   /* Handle the case of a symbol equated to another symbol.  */
13328   while (symbol_equated_reloc_p (sym))
13329     {
13330       symbolS *n;
13331
13332       /* It's possible to get a loop here in a badly written
13333          program.  */
13334       n = symbol_get_value_expression (sym)->X_add_symbol;
13335       if (n == sym)
13336         break;
13337       sym = n;
13338     }
13339
13340   symsec = S_GET_SEGMENT (sym);
13341
13342   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13343   linkonce = FALSE;
13344   if (symsec != segtype && ! S_IS_LOCAL (sym))
13345     {
13346       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13347           != 0)
13348         linkonce = TRUE;
13349
13350       /* The GNU toolchain uses an extension for ELF: a section
13351          beginning with the magic string .gnu.linkonce is a linkonce
13352          section.  */
13353       if (strncmp (segment_name (symsec), ".gnu.linkonce",
13354                    sizeof ".gnu.linkonce" - 1) == 0)
13355         linkonce = TRUE;
13356     }
13357
13358   /* This must duplicate the test in adjust_reloc_syms.  */
13359   return (symsec != &bfd_und_section
13360           && symsec != &bfd_abs_section
13361           && ! bfd_is_com_section (symsec)
13362           && !linkonce
13363 #ifdef OBJ_ELF
13364           /* A global or weak symbol is treated as external.  */
13365           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13366               || (! S_IS_WEAK (sym)
13367                   && (! S_IS_EXTERNAL (sym)
13368                       || mips_pic == EMBEDDED_PIC)))
13369 #endif
13370           );
13371 }
13372
13373
13374 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13375    extended opcode.  SEC is the section the frag is in.  */
13376
13377 static int
13378 mips16_extended_frag (fragp, sec, stretch)
13379      fragS *fragp;
13380      asection *sec;
13381      long stretch;
13382 {
13383   int type;
13384   register const struct mips16_immed_operand *op;
13385   offsetT val;
13386   int mintiny, maxtiny;
13387   segT symsec;
13388   fragS *sym_frag;
13389
13390   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13391     return 0;
13392   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13393     return 1;
13394
13395   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13396   op = mips16_immed_operands;
13397   while (op->type != type)
13398     {
13399       ++op;
13400       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13401     }
13402
13403   if (op->unsp)
13404     {
13405       if (type == '<' || type == '>' || type == '[' || type == ']')
13406         {
13407           mintiny = 1;
13408           maxtiny = 1 << op->nbits;
13409         }
13410       else
13411         {
13412           mintiny = 0;
13413           maxtiny = (1 << op->nbits) - 1;
13414         }
13415     }
13416   else
13417     {
13418       mintiny = - (1 << (op->nbits - 1));
13419       maxtiny = (1 << (op->nbits - 1)) - 1;
13420     }
13421
13422   sym_frag = symbol_get_frag (fragp->fr_symbol);
13423   val = S_GET_VALUE (fragp->fr_symbol);
13424   symsec = S_GET_SEGMENT (fragp->fr_symbol);
13425
13426   if (op->pcrel)
13427     {
13428       addressT addr;
13429
13430       /* We won't have the section when we are called from
13431          mips_relax_frag.  However, we will always have been called
13432          from md_estimate_size_before_relax first.  If this is a
13433          branch to a different section, we mark it as such.  If SEC is
13434          NULL, and the frag is not marked, then it must be a branch to
13435          the same section.  */
13436       if (sec == NULL)
13437         {
13438           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13439             return 1;
13440         }
13441       else
13442         {
13443           /* Must have been called from md_estimate_size_before_relax.  */
13444           if (symsec != sec)
13445             {
13446               fragp->fr_subtype =
13447                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13448
13449               /* FIXME: We should support this, and let the linker
13450                  catch branches and loads that are out of range.  */
13451               as_bad_where (fragp->fr_file, fragp->fr_line,
13452                             _("unsupported PC relative reference to different section"));
13453
13454               return 1;
13455             }
13456           if (fragp != sym_frag && sym_frag->fr_address == 0)
13457             /* Assume non-extended on the first relaxation pass.
13458                The address we have calculated will be bogus if this is
13459                a forward branch to another frag, as the forward frag
13460                will have fr_address == 0.  */
13461             return 0;
13462         }
13463
13464       /* In this case, we know for sure that the symbol fragment is in
13465          the same section.  If the relax_marker of the symbol fragment
13466          differs from the relax_marker of this fragment, we have not
13467          yet adjusted the symbol fragment fr_address.  We want to add
13468          in STRETCH in order to get a better estimate of the address.
13469          This particularly matters because of the shift bits.  */
13470       if (stretch != 0
13471           && sym_frag->relax_marker != fragp->relax_marker)
13472         {
13473           fragS *f;
13474
13475           /* Adjust stretch for any alignment frag.  Note that if have
13476              been expanding the earlier code, the symbol may be
13477              defined in what appears to be an earlier frag.  FIXME:
13478              This doesn't handle the fr_subtype field, which specifies
13479              a maximum number of bytes to skip when doing an
13480              alignment.  */
13481           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13482             {
13483               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13484                 {
13485                   if (stretch < 0)
13486                     stretch = - ((- stretch)
13487                                  & ~ ((1 << (int) f->fr_offset) - 1));
13488                   else
13489                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13490                   if (stretch == 0)
13491                     break;
13492                 }
13493             }
13494           if (f != NULL)
13495             val += stretch;
13496         }
13497
13498       addr = fragp->fr_address + fragp->fr_fix;
13499
13500       /* The base address rules are complicated.  The base address of
13501          a branch is the following instruction.  The base address of a
13502          PC relative load or add is the instruction itself, but if it
13503          is in a delay slot (in which case it can not be extended) use
13504          the address of the instruction whose delay slot it is in.  */
13505       if (type == 'p' || type == 'q')
13506         {
13507           addr += 2;
13508
13509           /* If we are currently assuming that this frag should be
13510              extended, then, the current address is two bytes
13511              higher.  */
13512           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13513             addr += 2;
13514
13515           /* Ignore the low bit in the target, since it will be set
13516              for a text label.  */
13517           if ((val & 1) != 0)
13518             --val;
13519         }
13520       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13521         addr -= 4;
13522       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13523         addr -= 2;
13524
13525       val -= addr & ~ ((1 << op->shift) - 1);
13526
13527       /* Branch offsets have an implicit 0 in the lowest bit.  */
13528       if (type == 'p' || type == 'q')
13529         val /= 2;
13530
13531       /* If any of the shifted bits are set, we must use an extended
13532          opcode.  If the address depends on the size of this
13533          instruction, this can lead to a loop, so we arrange to always
13534          use an extended opcode.  We only check this when we are in
13535          the main relaxation loop, when SEC is NULL.  */
13536       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13537         {
13538           fragp->fr_subtype =
13539             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13540           return 1;
13541         }
13542
13543       /* If we are about to mark a frag as extended because the value
13544          is precisely maxtiny + 1, then there is a chance of an
13545          infinite loop as in the following code:
13546              la $4,foo
13547              .skip      1020
13548              .align     2
13549            foo:
13550          In this case when the la is extended, foo is 0x3fc bytes
13551          away, so the la can be shrunk, but then foo is 0x400 away, so
13552          the la must be extended.  To avoid this loop, we mark the
13553          frag as extended if it was small, and is about to become
13554          extended with a value of maxtiny + 1.  */
13555       if (val == ((maxtiny + 1) << op->shift)
13556           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13557           && sec == NULL)
13558         {
13559           fragp->fr_subtype =
13560             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13561           return 1;
13562         }
13563     }
13564   else if (symsec != absolute_section && sec != NULL)
13565     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13566
13567   if ((val & ((1 << op->shift) - 1)) != 0
13568       || val < (mintiny << op->shift)
13569       || val > (maxtiny << op->shift))
13570     return 1;
13571   else
13572     return 0;
13573 }
13574
13575 /* Compute the length of a branch sequence, and adjust the
13576    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13577    worst-case length is computed, with UPDATE being used to indicate
13578    whether an unconditional (-1), branch-likely (+1) or regular (0)
13579    branch is to be computed.  */
13580 static int
13581 relaxed_branch_length (fragp, sec, update)
13582      fragS *fragp;
13583      asection *sec;
13584      int update;
13585 {
13586   bfd_boolean toofar;
13587   int length;
13588
13589   if (fragp
13590       && S_IS_DEFINED (fragp->fr_symbol)
13591       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13592     {
13593       addressT addr;
13594       offsetT val;
13595
13596       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13597
13598       addr = fragp->fr_address + fragp->fr_fix + 4;
13599
13600       val -= addr;
13601
13602       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13603     }
13604   else if (fragp)
13605     /* If the symbol is not defined or it's in a different segment,
13606        assume the user knows what's going on and emit a short
13607        branch.  */
13608     toofar = FALSE;
13609   else
13610     toofar = TRUE;
13611
13612   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13613     fragp->fr_subtype
13614       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13615                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13616                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13617                              toofar);
13618
13619   length = 4;
13620   if (toofar)
13621     {
13622       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13623         length += 8;
13624
13625       if (mips_pic != NO_PIC)
13626         {
13627           /* Additional space for PIC loading of target address.  */
13628           length += 8;
13629           if (mips_opts.isa == ISA_MIPS1)
13630             /* Additional space for $at-stabilizing nop.  */
13631             length += 4;
13632         }
13633
13634       /* If branch is conditional.  */
13635       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13636         length += 8;
13637     }
13638
13639   return length;
13640 }
13641
13642 /* Estimate the size of a frag before relaxing.  Unless this is the
13643    mips16, we are not really relaxing here, and the final size is
13644    encoded in the subtype information.  For the mips16, we have to
13645    decide whether we are using an extended opcode or not.  */
13646
13647 int
13648 md_estimate_size_before_relax (fragp, segtype)
13649      fragS *fragp;
13650      asection *segtype;
13651 {
13652   int change;
13653
13654   if (RELAX_BRANCH_P (fragp->fr_subtype))
13655     {
13656
13657       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13658
13659       return fragp->fr_var;
13660     }
13661
13662   if (RELAX_MIPS16_P (fragp->fr_subtype))
13663     /* We don't want to modify the EXTENDED bit here; it might get us
13664        into infinite loops.  We change it only in mips_relax_frag().  */
13665     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13666
13667   if (mips_pic == NO_PIC)
13668     change = nopic_need_relax (fragp->fr_symbol, 0);
13669   else if (mips_pic == SVR4_PIC)
13670     change = pic_need_relax (fragp->fr_symbol, segtype);
13671   else
13672     abort ();
13673
13674   if (change)
13675     {
13676       /* Record the offset to the first reloc in the fr_opcode field.
13677          This lets md_convert_frag and tc_gen_reloc know that the code
13678          must be expanded.  */
13679       fragp->fr_opcode = (fragp->fr_literal
13680                           + fragp->fr_fix
13681                           - RELAX_OLD (fragp->fr_subtype)
13682                           + RELAX_RELOC1 (fragp->fr_subtype));
13683       /* FIXME: This really needs as_warn_where.  */
13684       if (RELAX_WARN (fragp->fr_subtype))
13685         as_warn (_("AT used after \".set noat\" or macro used after "
13686                    "\".set nomacro\""));
13687
13688       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13689     }
13690
13691   return 0;
13692 }
13693
13694 /* This is called to see whether a reloc against a defined symbol
13695    should be converted into a reloc against a section.  Don't adjust
13696    MIPS16 jump relocations, so we don't have to worry about the format
13697    of the offset in the .o file.  Don't adjust relocations against
13698    mips16 symbols, so that the linker can find them if it needs to set
13699    up a stub.  */
13700
13701 int
13702 mips_fix_adjustable (fixp)
13703      fixS *fixp;
13704 {
13705   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13706     return 0;
13707
13708   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13709       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13710     return 0;
13711
13712   if (fixp->fx_addsy == NULL)
13713     return 1;
13714
13715 #ifdef OBJ_ELF
13716   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13717       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13718       && fixp->fx_subsy == NULL)
13719     return 0;
13720 #endif
13721
13722   return 1;
13723 }
13724
13725 /* Translate internal representation of relocation info to BFD target
13726    format.  */
13727
13728 arelent **
13729 tc_gen_reloc (section, fixp)
13730      asection *section ATTRIBUTE_UNUSED;
13731      fixS *fixp;
13732 {
13733   static arelent *retval[4];
13734   arelent *reloc;
13735   bfd_reloc_code_real_type code;
13736
13737   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13738   retval[1] = NULL;
13739
13740   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13741   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13742   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13743
13744   if (mips_pic == EMBEDDED_PIC
13745       && SWITCH_TABLE (fixp))
13746     {
13747       /* For a switch table entry we use a special reloc.  The addend
13748          is actually the difference between the reloc address and the
13749          subtrahend.  */
13750       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13751       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13752         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13753       fixp->fx_r_type = BFD_RELOC_GPREL32;
13754     }
13755   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13756     {
13757       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13758         reloc->addend = fixp->fx_addnumber;
13759       else
13760         {
13761           /* We use a special addend for an internal RELLO reloc.  */
13762           if (symbol_section_p (fixp->fx_addsy))
13763             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13764           else
13765             reloc->addend = fixp->fx_addnumber + reloc->address;
13766         }
13767     }
13768   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13769     {
13770       assert (fixp->fx_next != NULL
13771               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13772
13773       /* The reloc is relative to the RELLO; adjust the addend
13774          accordingly.  */
13775       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13776         reloc->addend = fixp->fx_next->fx_addnumber;
13777       else
13778         {
13779           /* We use a special addend for an internal RELHI reloc.  */
13780           if (symbol_section_p (fixp->fx_addsy))
13781             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13782                              + fixp->fx_next->fx_where
13783                              - S_GET_VALUE (fixp->fx_subsy));
13784           else
13785             reloc->addend = (fixp->fx_addnumber
13786                              + fixp->fx_next->fx_frag->fr_address
13787                              + fixp->fx_next->fx_where);
13788         }
13789     }
13790   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13791     reloc->addend = fixp->fx_addnumber;
13792   else
13793     {
13794       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13795         /* A gruesome hack which is a result of the gruesome gas reloc
13796            handling.  */
13797         reloc->addend = reloc->address;
13798       else
13799         reloc->addend = -reloc->address;
13800     }
13801
13802   /* If this is a variant frag, we may need to adjust the existing
13803      reloc and generate a new one.  */
13804   if (fixp->fx_frag->fr_opcode != NULL
13805       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13806            && ! HAVE_NEWABI)
13807           || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13808               && HAVE_NEWABI)
13809           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13810           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13811           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13812           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13813           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13814           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13815     )
13816     {
13817       arelent *reloc2;
13818
13819       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13820
13821       /* If this is not the last reloc in this frag, then we have two
13822          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13823          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13824          the second one handle all of them.  */
13825       if (fixp->fx_next != NULL
13826           && fixp->fx_frag == fixp->fx_next->fx_frag)
13827         {
13828           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13829                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13830                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13831                       && (fixp->fx_next->fx_r_type
13832                           == BFD_RELOC_MIPS_GOT_LO16))
13833                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13834                       && (fixp->fx_next->fx_r_type
13835                           == BFD_RELOC_MIPS_CALL_LO16)));
13836           retval[0] = NULL;
13837           return retval;
13838         }
13839
13840       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13841       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13842       reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13843       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13844       retval[2] = NULL;
13845       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13846       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13847       reloc2->address = (reloc->address
13848                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13849                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13850       reloc2->addend = fixp->fx_addnumber
13851         + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13852       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13853       assert (reloc2->howto != NULL);
13854
13855       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13856         {
13857           arelent *reloc3;
13858
13859           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13860           retval[3] = NULL;
13861           *reloc3 = *reloc2;
13862           reloc3->address += 4;
13863         }
13864
13865       if (mips_pic == NO_PIC)
13866         {
13867           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13868           fixp->fx_r_type = BFD_RELOC_HI16_S;
13869         }
13870       else if (mips_pic == SVR4_PIC)
13871         {
13872           switch (fixp->fx_r_type)
13873             {
13874             default:
13875               abort ();
13876             case BFD_RELOC_MIPS_GOT16:
13877               break;
13878             case BFD_RELOC_MIPS_GOT_LO16:
13879             case BFD_RELOC_MIPS_CALL_LO16:
13880               if (HAVE_NEWABI)
13881                 {
13882                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13883                   reloc2->howto = bfd_reloc_type_lookup
13884                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13885                 }
13886               else
13887                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13888               break;
13889             case BFD_RELOC_MIPS_CALL16:
13890             case BFD_RELOC_MIPS_GOT_OFST:
13891             case BFD_RELOC_MIPS_GOT_DISP:
13892               if (HAVE_NEWABI)
13893                 {
13894                   /* It may seem nonsensical to relax GOT_DISP to
13895                      GOT_DISP, but we're actually turning a GOT_DISP
13896                      without offset into a GOT_DISP with an offset,
13897                      getting rid of the separate addition, which we can
13898                      do when the symbol is found to be local.  */
13899                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13900                   retval[1] = NULL;
13901                 }
13902               else
13903                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13904               break;
13905             }
13906         }
13907       else
13908         abort ();
13909     }
13910
13911   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13912      entry to be used in the relocation's section offset.  */
13913   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13914     {
13915       reloc->address = reloc->addend;
13916       reloc->addend = 0;
13917     }
13918
13919   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13920      fixup_segment converted a non-PC relative reloc into a PC
13921      relative reloc.  In such a case, we need to convert the reloc
13922      code.  */
13923   code = fixp->fx_r_type;
13924   if (fixp->fx_pcrel)
13925     {
13926       switch (code)
13927         {
13928         case BFD_RELOC_8:
13929           code = BFD_RELOC_8_PCREL;
13930           break;
13931         case BFD_RELOC_16:
13932           code = BFD_RELOC_16_PCREL;
13933           break;
13934         case BFD_RELOC_32:
13935           code = BFD_RELOC_32_PCREL;
13936           break;
13937         case BFD_RELOC_64:
13938           code = BFD_RELOC_64_PCREL;
13939           break;
13940         case BFD_RELOC_8_PCREL:
13941         case BFD_RELOC_16_PCREL:
13942         case BFD_RELOC_32_PCREL:
13943         case BFD_RELOC_64_PCREL:
13944         case BFD_RELOC_16_PCREL_S2:
13945         case BFD_RELOC_PCREL_HI16_S:
13946         case BFD_RELOC_PCREL_LO16:
13947           break;
13948         default:
13949           as_bad_where (fixp->fx_file, fixp->fx_line,
13950                         _("Cannot make %s relocation PC relative"),
13951                         bfd_get_reloc_code_name (code));
13952         }
13953     }
13954
13955 #ifdef OBJ_ELF
13956   /* md_apply_fix3 has a double-subtraction hack to get
13957      bfd_install_relocation to behave nicely.  GPREL relocations are
13958      handled correctly without this hack, so undo it here.  We can't
13959      stop md_apply_fix3 from subtracting twice in the first place since
13960      the fake addend is required for variant frags above.  */
13961   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13962       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13963       && reloc->addend != 0
13964       && mips_need_elf_addend_fixup (fixp))
13965     {
13966       /* If howto->partial_inplace is false, md_apply_fix3 will only
13967          subtract it once.  */
13968       reloc_howto_type *howto;
13969
13970       howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
13971       if (howto->partial_inplace)
13972         reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13973     }
13974 #endif
13975
13976   /* To support a PC relative reloc when generating embedded PIC code
13977      for ECOFF, we use a Cygnus extension.  We check for that here to
13978      make sure that we don't let such a reloc escape normally.  */
13979   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13980        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13981       && code == BFD_RELOC_16_PCREL_S2
13982       && mips_pic != EMBEDDED_PIC)
13983     reloc->howto = NULL;
13984   else
13985     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13986
13987   if (reloc->howto == NULL)
13988     {
13989       as_bad_where (fixp->fx_file, fixp->fx_line,
13990                     _("Can not represent %s relocation in this object file format"),
13991                     bfd_get_reloc_code_name (code));
13992       retval[0] = NULL;
13993     }
13994
13995   return retval;
13996 }
13997
13998 /* Relax a machine dependent frag.  This returns the amount by which
13999    the current size of the frag should change.  */
14000
14001 int
14002 mips_relax_frag (sec, fragp, stretch)
14003      asection *sec;
14004      fragS *fragp;
14005      long stretch;
14006 {
14007   if (RELAX_BRANCH_P (fragp->fr_subtype))
14008     {
14009       offsetT old_var = fragp->fr_var;
14010
14011       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
14012
14013       return fragp->fr_var - old_var;
14014     }
14015
14016   if (! RELAX_MIPS16_P (fragp->fr_subtype))
14017     return 0;
14018
14019   if (mips16_extended_frag (fragp, NULL, stretch))
14020     {
14021       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14022         return 0;
14023       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
14024       return 2;
14025     }
14026   else
14027     {
14028       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14029         return 0;
14030       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
14031       return -2;
14032     }
14033
14034   return 0;
14035 }
14036
14037 /* Convert a machine dependent frag.  */
14038
14039 void
14040 md_convert_frag (abfd, asec, fragp)
14041      bfd *abfd ATTRIBUTE_UNUSED;
14042      segT asec;
14043      fragS *fragp;
14044 {
14045   int old, new;
14046   char *fixptr;
14047
14048   if (RELAX_BRANCH_P (fragp->fr_subtype))
14049     {
14050       bfd_byte *buf;
14051       unsigned long insn;
14052       expressionS exp;
14053       fixS *fixp;
14054
14055       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
14056
14057       if (target_big_endian)
14058         insn = bfd_getb32 (buf);
14059       else
14060         insn = bfd_getl32 (buf);
14061
14062       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
14063         {
14064           /* We generate a fixup instead of applying it right now
14065              because, if there are linker relaxations, we're going to
14066              need the relocations.  */
14067           exp.X_op = O_symbol;
14068           exp.X_add_symbol = fragp->fr_symbol;
14069           exp.X_add_number = fragp->fr_offset;
14070
14071           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14072                               4, &exp, 1,
14073                               BFD_RELOC_16_PCREL_S2);
14074           fixp->fx_file = fragp->fr_file;
14075           fixp->fx_line = fragp->fr_line;
14076
14077           md_number_to_chars ((char *)buf, insn, 4);
14078           buf += 4;
14079         }
14080       else
14081         {
14082           int i;
14083
14084           as_warn_where (fragp->fr_file, fragp->fr_line,
14085                          _("relaxed out-of-range branch into a jump"));
14086
14087           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
14088             goto uncond;
14089
14090           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14091             {
14092               /* Reverse the branch.  */
14093               switch ((insn >> 28) & 0xf)
14094                 {
14095                 case 4:
14096                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
14097                      have the condition reversed by tweaking a single
14098                      bit, and their opcodes all have 0x4???????.  */
14099                   assert ((insn & 0xf1000000) == 0x41000000);
14100                   insn ^= 0x00010000;
14101                   break;
14102
14103                 case 0:
14104                   /* bltz       0x04000000      bgez    0x04010000
14105                      bltzal     0x04100000      bgezal  0x04110000 */
14106                   assert ((insn & 0xfc0e0000) == 0x04000000);
14107                   insn ^= 0x00010000;
14108                   break;
14109
14110                 case 1:
14111                   /* beq        0x10000000      bne     0x14000000
14112                      blez       0x18000000      bgtz    0x1c000000 */
14113                   insn ^= 0x04000000;
14114                   break;
14115
14116                 default:
14117                   abort ();
14118                 }
14119             }
14120
14121           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14122             {
14123               /* Clear the and-link bit.  */
14124               assert ((insn & 0xfc1c0000) == 0x04100000);
14125
14126               /* bltzal 0x04100000      bgezal  0x04110000
14127                 bltzall 0x04120000     bgezall  0x04130000 */
14128               insn &= ~0x00100000;
14129             }
14130
14131           /* Branch over the branch (if the branch was likely) or the
14132              full jump (not likely case).  Compute the offset from the
14133              current instruction to branch to.  */
14134           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14135             i = 16;
14136           else
14137             {
14138               /* How many bytes in instructions we've already emitted?  */
14139               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14140               /* How many bytes in instructions from here to the end?  */
14141               i = fragp->fr_var - i;
14142             }
14143           /* Convert to instruction count.  */
14144           i >>= 2;
14145           /* Branch counts from the next instruction.  */
14146           i--;
14147           insn |= i;
14148           /* Branch over the jump.  */
14149           md_number_to_chars ((char *)buf, insn, 4);
14150           buf += 4;
14151
14152           /* Nop */
14153           md_number_to_chars ((char*)buf, 0, 4);
14154           buf += 4;
14155
14156           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14157             {
14158               /* beql $0, $0, 2f */
14159               insn = 0x50000000;
14160               /* Compute the PC offset from the current instruction to
14161                  the end of the variable frag.  */
14162               /* How many bytes in instructions we've already emitted?  */
14163               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14164               /* How many bytes in instructions from here to the end?  */
14165               i = fragp->fr_var - i;
14166               /* Convert to instruction count.  */
14167               i >>= 2;
14168               /* Don't decrement i, because we want to branch over the
14169                  delay slot.  */
14170
14171               insn |= i;
14172               md_number_to_chars ((char *)buf, insn, 4);
14173               buf += 4;
14174
14175               md_number_to_chars ((char *)buf, 0, 4);
14176               buf += 4;
14177             }
14178
14179         uncond:
14180           if (mips_pic == NO_PIC)
14181             {
14182               /* j or jal.  */
14183               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
14184                       ? 0x0c000000 : 0x08000000);
14185               exp.X_op = O_symbol;
14186               exp.X_add_symbol = fragp->fr_symbol;
14187               exp.X_add_number = fragp->fr_offset;
14188
14189               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14190                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
14191               fixp->fx_file = fragp->fr_file;
14192               fixp->fx_line = fragp->fr_line;
14193
14194               md_number_to_chars ((char*)buf, insn, 4);
14195               buf += 4;
14196             }
14197           else
14198             {
14199               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
14200               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
14201               exp.X_op = O_symbol;
14202               exp.X_add_symbol = fragp->fr_symbol;
14203               exp.X_add_number = fragp->fr_offset;
14204
14205               if (fragp->fr_offset)
14206                 {
14207                   exp.X_add_symbol = make_expr_symbol (&exp);
14208                   exp.X_add_number = 0;
14209                 }
14210
14211               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14212                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
14213               fixp->fx_file = fragp->fr_file;
14214               fixp->fx_line = fragp->fr_line;
14215
14216               md_number_to_chars ((char*)buf, insn, 4);
14217               buf += 4;
14218
14219               if (mips_opts.isa == ISA_MIPS1)
14220                 {
14221                   /* nop */
14222                   md_number_to_chars ((char*)buf, 0, 4);
14223                   buf += 4;
14224                 }
14225
14226               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
14227               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
14228
14229               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14230                                   4, &exp, 0, BFD_RELOC_LO16);
14231               fixp->fx_file = fragp->fr_file;
14232               fixp->fx_line = fragp->fr_line;
14233
14234               md_number_to_chars ((char*)buf, insn, 4);
14235               buf += 4;
14236
14237               /* j(al)r $at.  */
14238               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14239                 insn = 0x0020f809;
14240               else
14241                 insn = 0x00200008;
14242
14243               md_number_to_chars ((char*)buf, insn, 4);
14244               buf += 4;
14245             }
14246         }
14247
14248       assert (buf == (bfd_byte *)fragp->fr_literal
14249               + fragp->fr_fix + fragp->fr_var);
14250
14251       fragp->fr_fix += fragp->fr_var;
14252
14253       return;
14254     }
14255
14256   if (RELAX_MIPS16_P (fragp->fr_subtype))
14257     {
14258       int type;
14259       register const struct mips16_immed_operand *op;
14260       bfd_boolean small, ext;
14261       offsetT val;
14262       bfd_byte *buf;
14263       unsigned long insn;
14264       bfd_boolean use_extend;
14265       unsigned short extend;
14266
14267       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
14268       op = mips16_immed_operands;
14269       while (op->type != type)
14270         ++op;
14271
14272       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14273         {
14274           small = FALSE;
14275           ext = TRUE;
14276         }
14277       else
14278         {
14279           small = TRUE;
14280           ext = FALSE;
14281         }
14282
14283       resolve_symbol_value (fragp->fr_symbol);
14284       val = S_GET_VALUE (fragp->fr_symbol);
14285       if (op->pcrel)
14286         {
14287           addressT addr;
14288
14289           addr = fragp->fr_address + fragp->fr_fix;
14290
14291           /* The rules for the base address of a PC relative reloc are
14292              complicated; see mips16_extended_frag.  */
14293           if (type == 'p' || type == 'q')
14294             {
14295               addr += 2;
14296               if (ext)
14297                 addr += 2;
14298               /* Ignore the low bit in the target, since it will be
14299                  set for a text label.  */
14300               if ((val & 1) != 0)
14301                 --val;
14302             }
14303           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
14304             addr -= 4;
14305           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
14306             addr -= 2;
14307
14308           addr &= ~ (addressT) ((1 << op->shift) - 1);
14309           val -= addr;
14310
14311           /* Make sure the section winds up with the alignment we have
14312              assumed.  */
14313           if (op->shift > 0)
14314             record_alignment (asec, op->shift);
14315         }
14316
14317       if (ext
14318           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
14319               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
14320         as_warn_where (fragp->fr_file, fragp->fr_line,
14321                        _("extended instruction in delay slot"));
14322
14323       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
14324
14325       if (target_big_endian)
14326         insn = bfd_getb16 (buf);
14327       else
14328         insn = bfd_getl16 (buf);
14329
14330       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
14331                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
14332                     small, ext, &insn, &use_extend, &extend);
14333
14334       if (use_extend)
14335         {
14336           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
14337           fragp->fr_fix += 2;
14338           buf += 2;
14339         }
14340
14341       md_number_to_chars ((char *) buf, insn, 2);
14342       fragp->fr_fix += 2;
14343       buf += 2;
14344     }
14345   else
14346     {
14347       if (fragp->fr_opcode == NULL)
14348         return;
14349
14350       old = RELAX_OLD (fragp->fr_subtype);
14351       new = RELAX_NEW (fragp->fr_subtype);
14352       fixptr = fragp->fr_literal + fragp->fr_fix;
14353
14354       if (new > 0)
14355         memmove (fixptr - old, fixptr, new);
14356
14357       fragp->fr_fix += new - old;
14358     }
14359 }
14360
14361 #ifdef OBJ_ELF
14362
14363 /* This function is called after the relocs have been generated.
14364    We've been storing mips16 text labels as odd.  Here we convert them
14365    back to even for the convenience of the debugger.  */
14366
14367 void
14368 mips_frob_file_after_relocs ()
14369 {
14370   asymbol **syms;
14371   unsigned int count, i;
14372
14373   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
14374     return;
14375
14376   syms = bfd_get_outsymbols (stdoutput);
14377   count = bfd_get_symcount (stdoutput);
14378   for (i = 0; i < count; i++, syms++)
14379     {
14380       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
14381           && ((*syms)->value & 1) != 0)
14382         {
14383           (*syms)->value &= ~1;
14384           /* If the symbol has an odd size, it was probably computed
14385              incorrectly, so adjust that as well.  */
14386           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14387             ++elf_symbol (*syms)->internal_elf_sym.st_size;
14388         }
14389     }
14390 }
14391
14392 #endif
14393
14394 /* This function is called whenever a label is defined.  It is used
14395    when handling branch delays; if a branch has a label, we assume we
14396    can not move it.  */
14397
14398 void
14399 mips_define_label (sym)
14400      symbolS *sym;
14401 {
14402   struct insn_label_list *l;
14403
14404   if (free_insn_labels == NULL)
14405     l = (struct insn_label_list *) xmalloc (sizeof *l);
14406   else
14407     {
14408       l = free_insn_labels;
14409       free_insn_labels = l->next;
14410     }
14411
14412   l->label = sym;
14413   l->next = insn_labels;
14414   insn_labels = l;
14415 }
14416 \f
14417 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14418
14419 /* Some special processing for a MIPS ELF file.  */
14420
14421 void
14422 mips_elf_final_processing ()
14423 {
14424   /* Write out the register information.  */
14425   if (mips_abi != N64_ABI)
14426     {
14427       Elf32_RegInfo s;
14428
14429       s.ri_gprmask = mips_gprmask;
14430       s.ri_cprmask[0] = mips_cprmask[0];
14431       s.ri_cprmask[1] = mips_cprmask[1];
14432       s.ri_cprmask[2] = mips_cprmask[2];
14433       s.ri_cprmask[3] = mips_cprmask[3];
14434       /* The gp_value field is set by the MIPS ELF backend.  */
14435
14436       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14437                                        ((Elf32_External_RegInfo *)
14438                                         mips_regmask_frag));
14439     }
14440   else
14441     {
14442       Elf64_Internal_RegInfo s;
14443
14444       s.ri_gprmask = mips_gprmask;
14445       s.ri_pad = 0;
14446       s.ri_cprmask[0] = mips_cprmask[0];
14447       s.ri_cprmask[1] = mips_cprmask[1];
14448       s.ri_cprmask[2] = mips_cprmask[2];
14449       s.ri_cprmask[3] = mips_cprmask[3];
14450       /* The gp_value field is set by the MIPS ELF backend.  */
14451
14452       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14453                                        ((Elf64_External_RegInfo *)
14454                                         mips_regmask_frag));
14455     }
14456
14457   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
14458      sort of BFD interface for this.  */
14459   if (mips_any_noreorder)
14460     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14461   if (mips_pic != NO_PIC)
14462     {
14463     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14464       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14465     }
14466   if (mips_abicalls)
14467     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14468
14469   /* Set MIPS ELF flags for ASEs.  */
14470   if (file_ase_mips16)
14471     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14472 #if 0 /* XXX FIXME */
14473   if (file_ase_mips3d)
14474     elf_elfheader (stdoutput)->e_flags |= ???;
14475 #endif
14476   if (file_ase_mdmx)
14477     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14478
14479   /* Set the MIPS ELF ABI flags.  */
14480   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14481     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14482   else if (mips_abi == O64_ABI)
14483     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14484   else if (mips_abi == EABI_ABI)
14485     {
14486       if (!file_mips_gp32)
14487         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14488       else
14489         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14490     }
14491   else if (mips_abi == N32_ABI)
14492     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14493
14494   /* Nothing to do for N64_ABI.  */
14495
14496   if (mips_32bitmode)
14497     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14498 }
14499
14500 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14501 \f
14502 typedef struct proc {
14503   symbolS *isym;
14504   unsigned long reg_mask;
14505   unsigned long reg_offset;
14506   unsigned long fpreg_mask;
14507   unsigned long fpreg_offset;
14508   unsigned long frame_offset;
14509   unsigned long frame_reg;
14510   unsigned long pc_reg;
14511 } procS;
14512
14513 static procS cur_proc;
14514 static procS *cur_proc_ptr;
14515 static int numprocs;
14516
14517 /* Fill in an rs_align_code fragment.  */
14518
14519 void
14520 mips_handle_align (fragp)
14521      fragS *fragp;
14522 {
14523   if (fragp->fr_type != rs_align_code)
14524     return;
14525
14526   if (mips_opts.mips16)
14527     {
14528       static const unsigned char be_nop[] = { 0x65, 0x00 };
14529       static const unsigned char le_nop[] = { 0x00, 0x65 };
14530
14531       int bytes;
14532       char *p;
14533
14534       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14535       p = fragp->fr_literal + fragp->fr_fix;
14536
14537       if (bytes & 1)
14538         {
14539           *p++ = 0;
14540           fragp->fr_fix++;
14541         }
14542
14543       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14544       fragp->fr_var = 2;
14545     }
14546
14547   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14548 }
14549
14550 static void
14551 md_obj_begin ()
14552 {
14553 }
14554
14555 static void
14556 md_obj_end ()
14557 {
14558   /* check for premature end, nesting errors, etc */
14559   if (cur_proc_ptr)
14560     as_warn (_("missing .end at end of assembly"));
14561 }
14562
14563 static long
14564 get_number ()
14565 {
14566   int negative = 0;
14567   long val = 0;
14568
14569   if (*input_line_pointer == '-')
14570     {
14571       ++input_line_pointer;
14572       negative = 1;
14573     }
14574   if (!ISDIGIT (*input_line_pointer))
14575     as_bad (_("expected simple number"));
14576   if (input_line_pointer[0] == '0')
14577     {
14578       if (input_line_pointer[1] == 'x')
14579         {
14580           input_line_pointer += 2;
14581           while (ISXDIGIT (*input_line_pointer))
14582             {
14583               val <<= 4;
14584               val |= hex_value (*input_line_pointer++);
14585             }
14586           return negative ? -val : val;
14587         }
14588       else
14589         {
14590           ++input_line_pointer;
14591           while (ISDIGIT (*input_line_pointer))
14592             {
14593               val <<= 3;
14594               val |= *input_line_pointer++ - '0';
14595             }
14596           return negative ? -val : val;
14597         }
14598     }
14599   if (!ISDIGIT (*input_line_pointer))
14600     {
14601       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14602               *input_line_pointer, *input_line_pointer);
14603       as_warn (_("invalid number"));
14604       return -1;
14605     }
14606   while (ISDIGIT (*input_line_pointer))
14607     {
14608       val *= 10;
14609       val += *input_line_pointer++ - '0';
14610     }
14611   return negative ? -val : val;
14612 }
14613
14614 /* The .file directive; just like the usual .file directive, but there
14615    is an initial number which is the ECOFF file index.  In the non-ECOFF
14616    case .file implies DWARF-2.  */
14617
14618 static void
14619 s_mips_file (x)
14620      int x ATTRIBUTE_UNUSED;
14621 {
14622   static int first_file_directive = 0;
14623
14624   if (ECOFF_DEBUGGING)
14625     {
14626       get_number ();
14627       s_app_file (0);
14628     }
14629   else
14630     {
14631       char *filename;
14632
14633       filename = dwarf2_directive_file (0);
14634
14635       /* Versions of GCC up to 3.1 start files with a ".file"
14636          directive even for stabs output.  Make sure that this
14637          ".file" is handled.  Note that you need a version of GCC
14638          after 3.1 in order to support DWARF-2 on MIPS.  */
14639       if (filename != NULL && ! first_file_directive)
14640         {
14641           (void) new_logical_line (filename, -1);
14642           s_app_file_string (filename);
14643         }
14644       first_file_directive = 1;
14645     }
14646 }
14647
14648 /* The .loc directive, implying DWARF-2.  */
14649
14650 static void
14651 s_mips_loc (x)
14652      int x ATTRIBUTE_UNUSED;
14653 {
14654   if (!ECOFF_DEBUGGING)
14655     dwarf2_directive_loc (0);
14656 }
14657
14658 /* The .end directive.  */
14659
14660 static void
14661 s_mips_end (x)
14662      int x ATTRIBUTE_UNUSED;
14663 {
14664   symbolS *p;
14665
14666   /* Following functions need their own .frame and .cprestore directives.  */
14667   mips_frame_reg_valid = 0;
14668   mips_cprestore_valid = 0;
14669
14670   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14671     {
14672       p = get_symbol ();
14673       demand_empty_rest_of_line ();
14674     }
14675   else
14676     p = NULL;
14677
14678   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14679     as_warn (_(".end not in text section"));
14680
14681   if (!cur_proc_ptr)
14682     {
14683       as_warn (_(".end directive without a preceding .ent directive."));
14684       demand_empty_rest_of_line ();
14685       return;
14686     }
14687
14688   if (p != NULL)
14689     {
14690       assert (S_GET_NAME (p));
14691       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14692         as_warn (_(".end symbol does not match .ent symbol."));
14693
14694       if (debug_type == DEBUG_STABS)
14695         stabs_generate_asm_endfunc (S_GET_NAME (p),
14696                                     S_GET_NAME (p));
14697     }
14698   else
14699     as_warn (_(".end directive missing or unknown symbol"));
14700
14701 #ifdef OBJ_ELF
14702   /* Generate a .pdr section.  */
14703   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14704     {
14705       segT saved_seg = now_seg;
14706       subsegT saved_subseg = now_subseg;
14707       valueT dot;
14708       expressionS exp;
14709       char *fragp;
14710
14711       dot = frag_now_fix ();
14712
14713 #ifdef md_flush_pending_output
14714       md_flush_pending_output ();
14715 #endif
14716
14717       assert (pdr_seg);
14718       subseg_set (pdr_seg, 0);
14719
14720       /* Write the symbol.  */
14721       exp.X_op = O_symbol;
14722       exp.X_add_symbol = p;
14723       exp.X_add_number = 0;
14724       emit_expr (&exp, 4);
14725
14726       fragp = frag_more (7 * 4);
14727
14728       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14729       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14730       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14731       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14732       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14733       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14734       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14735
14736       subseg_set (saved_seg, saved_subseg);
14737     }
14738 #endif /* OBJ_ELF */
14739
14740   cur_proc_ptr = NULL;
14741 }
14742
14743 /* The .aent and .ent directives.  */
14744
14745 static void
14746 s_mips_ent (aent)
14747      int aent;
14748 {
14749   symbolS *symbolP;
14750
14751   symbolP = get_symbol ();
14752   if (*input_line_pointer == ',')
14753     ++input_line_pointer;
14754   SKIP_WHITESPACE ();
14755   if (ISDIGIT (*input_line_pointer)
14756       || *input_line_pointer == '-')
14757     get_number ();
14758
14759   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14760     as_warn (_(".ent or .aent not in text section."));
14761
14762   if (!aent && cur_proc_ptr)
14763     as_warn (_("missing .end"));
14764
14765   if (!aent)
14766     {
14767       /* This function needs its own .frame and .cprestore directives.  */
14768       mips_frame_reg_valid = 0;
14769       mips_cprestore_valid = 0;
14770
14771       cur_proc_ptr = &cur_proc;
14772       memset (cur_proc_ptr, '\0', sizeof (procS));
14773
14774       cur_proc_ptr->isym = symbolP;
14775
14776       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14777
14778       ++numprocs;
14779
14780       if (debug_type == DEBUG_STABS)
14781         stabs_generate_asm_func (S_GET_NAME (symbolP),
14782                                  S_GET_NAME (symbolP));
14783     }
14784
14785   demand_empty_rest_of_line ();
14786 }
14787
14788 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14789    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14790    s_mips_frame is used so that we can set the PDR information correctly.
14791    We can't use the ecoff routines because they make reference to the ecoff
14792    symbol table (in the mdebug section).  */
14793
14794 static void
14795 s_mips_frame (ignore)
14796      int ignore ATTRIBUTE_UNUSED;
14797 {
14798 #ifdef OBJ_ELF
14799   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14800     {
14801       long val;
14802
14803       if (cur_proc_ptr == (procS *) NULL)
14804         {
14805           as_warn (_(".frame outside of .ent"));
14806           demand_empty_rest_of_line ();
14807           return;
14808         }
14809
14810       cur_proc_ptr->frame_reg = tc_get_register (1);
14811
14812       SKIP_WHITESPACE ();
14813       if (*input_line_pointer++ != ','
14814           || get_absolute_expression_and_terminator (&val) != ',')
14815         {
14816           as_warn (_("Bad .frame directive"));
14817           --input_line_pointer;
14818           demand_empty_rest_of_line ();
14819           return;
14820         }
14821
14822       cur_proc_ptr->frame_offset = val;
14823       cur_proc_ptr->pc_reg = tc_get_register (0);
14824
14825       demand_empty_rest_of_line ();
14826     }
14827   else
14828 #endif /* OBJ_ELF */
14829     s_ignore (ignore);
14830 }
14831
14832 /* The .fmask and .mask directives. If the mdebug section is present
14833    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14834    embedded targets, s_mips_mask is used so that we can set the PDR
14835    information correctly. We can't use the ecoff routines because they
14836    make reference to the ecoff symbol table (in the mdebug section).  */
14837
14838 static void
14839 s_mips_mask (reg_type)
14840      char reg_type;
14841 {
14842 #ifdef OBJ_ELF
14843   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14844     {
14845       long mask, off;
14846
14847       if (cur_proc_ptr == (procS *) NULL)
14848         {
14849           as_warn (_(".mask/.fmask outside of .ent"));
14850           demand_empty_rest_of_line ();
14851           return;
14852         }
14853
14854       if (get_absolute_expression_and_terminator (&mask) != ',')
14855         {
14856           as_warn (_("Bad .mask/.fmask directive"));
14857           --input_line_pointer;
14858           demand_empty_rest_of_line ();
14859           return;
14860         }
14861
14862       off = get_absolute_expression ();
14863
14864       if (reg_type == 'F')
14865         {
14866           cur_proc_ptr->fpreg_mask = mask;
14867           cur_proc_ptr->fpreg_offset = off;
14868         }
14869       else
14870         {
14871           cur_proc_ptr->reg_mask = mask;
14872           cur_proc_ptr->reg_offset = off;
14873         }
14874
14875       demand_empty_rest_of_line ();
14876     }
14877   else
14878 #endif /* OBJ_ELF */
14879     s_ignore (reg_type);
14880 }
14881
14882 /* The .loc directive.  */
14883
14884 #if 0
14885 static void
14886 s_loc (x)
14887      int x;
14888 {
14889   symbolS *symbolP;
14890   int lineno;
14891   int addroff;
14892
14893   assert (now_seg == text_section);
14894
14895   lineno = get_number ();
14896   addroff = frag_now_fix ();
14897
14898   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14899   S_SET_TYPE (symbolP, N_SLINE);
14900   S_SET_OTHER (symbolP, 0);
14901   S_SET_DESC (symbolP, lineno);
14902   symbolP->sy_segment = now_seg;
14903 }
14904 #endif
14905
14906 /* A table describing all the processors gas knows about.  Names are
14907    matched in the order listed.
14908
14909    To ease comparison, please keep this table in the same order as
14910    gcc's mips_cpu_info_table[].  */
14911 static const struct mips_cpu_info mips_cpu_info_table[] =
14912 {
14913   /* Entries for generic ISAs */
14914   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14915   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14916   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14917   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14918   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14919   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14920   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14921   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14922
14923   /* MIPS I */
14924   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14925   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14926   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14927
14928   /* MIPS II */
14929   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14930
14931   /* MIPS III */
14932   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14933   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14934   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14935   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14936   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14937   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14938   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14939   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14940   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14941   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14942   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14943   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14944
14945   /* MIPS IV */
14946   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14947   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14948   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14949   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14950   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14951   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14952   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14953   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14954   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14955   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14956   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14957   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14958
14959   /* MIPS 32 */
14960   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14961   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14962   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14963
14964   /* MIPS 64 */
14965   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14966   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14967
14968   /* Broadcom SB-1 CPU core */
14969   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14970
14971   /* End marker */
14972   { NULL, 0, 0, 0 }
14973 };
14974
14975
14976 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14977    with a final "000" replaced by "k".  Ignore case.
14978
14979    Note: this function is shared between GCC and GAS.  */
14980
14981 static bfd_boolean
14982 mips_strict_matching_cpu_name_p (canonical, given)
14983      const char *canonical, *given;
14984 {
14985   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14986     given++, canonical++;
14987
14988   return ((*given == 0 && *canonical == 0)
14989           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14990 }
14991
14992
14993 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14994    CPU name.  We've traditionally allowed a lot of variation here.
14995
14996    Note: this function is shared between GCC and GAS.  */
14997
14998 static bfd_boolean
14999 mips_matching_cpu_name_p (canonical, given)
15000      const char *canonical, *given;
15001 {
15002   /* First see if the name matches exactly, or with a final "000"
15003      turned into "k".  */
15004   if (mips_strict_matching_cpu_name_p (canonical, given))
15005     return TRUE;
15006
15007   /* If not, try comparing based on numerical designation alone.
15008      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
15009   if (TOLOWER (*given) == 'r')
15010     given++;
15011   if (!ISDIGIT (*given))
15012     return FALSE;
15013
15014   /* Skip over some well-known prefixes in the canonical name,
15015      hoping to find a number there too.  */
15016   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
15017     canonical += 2;
15018   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
15019     canonical += 2;
15020   else if (TOLOWER (canonical[0]) == 'r')
15021     canonical += 1;
15022
15023   return mips_strict_matching_cpu_name_p (canonical, given);
15024 }
15025
15026
15027 /* Parse an option that takes the name of a processor as its argument.
15028    OPTION is the name of the option and CPU_STRING is the argument.
15029    Return the corresponding processor enumeration if the CPU_STRING is
15030    recognized, otherwise report an error and return null.
15031
15032    A similar function exists in GCC.  */
15033
15034 static const struct mips_cpu_info *
15035 mips_parse_cpu (option, cpu_string)
15036      const char *option, *cpu_string;
15037 {
15038   const struct mips_cpu_info *p;
15039
15040   /* 'from-abi' selects the most compatible architecture for the given
15041      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
15042      EABIs, we have to decide whether we're using the 32-bit or 64-bit
15043      version.  Look first at the -mgp options, if given, otherwise base
15044      the choice on MIPS_DEFAULT_64BIT.
15045
15046      Treat NO_ABI like the EABIs.  One reason to do this is that the
15047      plain 'mips' and 'mips64' configs have 'from-abi' as their default
15048      architecture.  This code picks MIPS I for 'mips' and MIPS III for
15049      'mips64', just as we did in the days before 'from-abi'.  */
15050   if (strcasecmp (cpu_string, "from-abi") == 0)
15051     {
15052       if (ABI_NEEDS_32BIT_REGS (mips_abi))
15053         return mips_cpu_info_from_isa (ISA_MIPS1);
15054
15055       if (ABI_NEEDS_64BIT_REGS (mips_abi))
15056         return mips_cpu_info_from_isa (ISA_MIPS3);
15057
15058       if (file_mips_gp32 >= 0)
15059         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
15060
15061       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
15062                                      ? ISA_MIPS3
15063                                      : ISA_MIPS1);
15064     }
15065
15066   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
15067   if (strcasecmp (cpu_string, "default") == 0)
15068     return 0;
15069
15070   for (p = mips_cpu_info_table; p->name != 0; p++)
15071     if (mips_matching_cpu_name_p (p->name, cpu_string))
15072       return p;
15073
15074   as_bad ("Bad value (%s) for %s", cpu_string, option);
15075   return 0;
15076 }
15077
15078 /* Return the canonical processor information for ISA (a member of the
15079    ISA_MIPS* enumeration).  */
15080
15081 static const struct mips_cpu_info *
15082 mips_cpu_info_from_isa (isa)
15083      int isa;
15084 {
15085   int i;
15086
15087   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15088     if (mips_cpu_info_table[i].is_isa
15089         && isa == mips_cpu_info_table[i].isa)
15090       return (&mips_cpu_info_table[i]);
15091
15092   return NULL;
15093 }
15094 \f
15095 static void
15096 show (stream, string, col_p, first_p)
15097      FILE *stream;
15098      const char *string;
15099      int *col_p;
15100      int *first_p;
15101 {
15102   if (*first_p)
15103     {
15104       fprintf (stream, "%24s", "");
15105       *col_p = 24;
15106     }
15107   else
15108     {
15109       fprintf (stream, ", ");
15110       *col_p += 2;
15111     }
15112
15113   if (*col_p + strlen (string) > 72)
15114     {
15115       fprintf (stream, "\n%24s", "");
15116       *col_p = 24;
15117     }
15118
15119   fprintf (stream, "%s", string);
15120   *col_p += strlen (string);
15121
15122   *first_p = 0;
15123 }
15124
15125 void
15126 md_show_usage (stream)
15127      FILE *stream;
15128 {
15129   int column, first;
15130   size_t i;
15131
15132   fprintf (stream, _("\
15133 MIPS options:\n\
15134 -membedded-pic          generate embedded position independent code\n\
15135 -EB                     generate big endian output\n\
15136 -EL                     generate little endian output\n\
15137 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
15138 -G NUM                  allow referencing objects up to NUM bytes\n\
15139                         implicitly with the gp register [default 8]\n"));
15140   fprintf (stream, _("\
15141 -mips1                  generate MIPS ISA I instructions\n\
15142 -mips2                  generate MIPS ISA II instructions\n\
15143 -mips3                  generate MIPS ISA III instructions\n\
15144 -mips4                  generate MIPS ISA IV instructions\n\
15145 -mips5                  generate MIPS ISA V instructions\n\
15146 -mips32                 generate MIPS32 ISA instructions\n\
15147 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
15148 -mips64                 generate MIPS64 ISA instructions\n\
15149 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
15150
15151   first = 1;
15152
15153   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15154     show (stream, mips_cpu_info_table[i].name, &column, &first);
15155   show (stream, "from-abi", &column, &first);
15156   fputc ('\n', stream);
15157
15158   fprintf (stream, _("\
15159 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
15160 -no-mCPU                don't generate code specific to CPU.\n\
15161                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
15162
15163   first = 1;
15164
15165   show (stream, "3900", &column, &first);
15166   show (stream, "4010", &column, &first);
15167   show (stream, "4100", &column, &first);
15168   show (stream, "4650", &column, &first);
15169   fputc ('\n', stream);
15170
15171   fprintf (stream, _("\
15172 -mips16                 generate mips16 instructions\n\
15173 -no-mips16              do not generate mips16 instructions\n"));
15174   fprintf (stream, _("\
15175 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
15176 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
15177 -O0                     remove unneeded NOPs, do not swap branches\n\
15178 -O                      remove unneeded NOPs and swap branches\n\
15179 -n                      warn about NOPs generated from macros\n\
15180 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
15181 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
15182 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
15183 #ifdef OBJ_ELF
15184   fprintf (stream, _("\
15185 -KPIC, -call_shared     generate SVR4 position independent code\n\
15186 -non_shared             do not generate position independent code\n\
15187 -xgot                   assume a 32 bit GOT\n\
15188 -mabi=ABI               create ABI conformant object file for:\n"));
15189
15190   first = 1;
15191
15192   show (stream, "32", &column, &first);
15193   show (stream, "o64", &column, &first);
15194   show (stream, "n32", &column, &first);
15195   show (stream, "64", &column, &first);
15196   show (stream, "eabi", &column, &first);
15197
15198   fputc ('\n', stream);
15199
15200   fprintf (stream, _("\
15201 -32                     create o32 ABI object file (default)\n\
15202 -n32                    create n32 ABI object file\n\
15203 -64                     create 64 ABI object file\n"));
15204 #endif
15205 }
15206
15207 enum dwarf2_format
15208 mips_dwarf2_format ()
15209 {
15210   if (mips_abi == N64_ABI)
15211     {
15212 #ifdef TE_IRIX
15213       return dwarf2_format_64bit_irix;
15214 #else
15215       return dwarf2_format_64bit;
15216 #endif
15217     }
15218   else
15219     return dwarf2_format_32bit;
15220 }