* config/tc-mips.c (HAVE_64BIT_ADDRESS_CONSTANTS): Remove.
[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 #include <stdarg.h>
32
33 #include "opcode/mips.h"
34 #include "itbl-ops.h"
35 #include "dwarf2dbg.h"
36
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too.  */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about.  */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections.  Off by default on IRIX: the native
78    linker doesn't know about and discards them, but relocations against them
79    remain, leading to rld crashes.  */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define AT  1
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP  28
99 #define SP  29
100 #define FP  30
101 #define RA  31
102
103 #define ILLEGAL_REG (32)
104
105 /* Allow override of standard little-endian ECOFF format.  */
106
107 #ifndef ECOFF_LITTLE_FORMAT
108 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109 #endif
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section.  */
114 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
115                             ? ".data" \
116                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
117                             ? ".rdata" \
118                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
119                             ? ".rdata" \
120                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
121                             ? ".rodata" \
122                             : (abort (), ""))
123
124 /* The ABI to use.  */
125 enum mips_abi_level
126 {
127   NO_ABI = 0,
128   O32_ABI,
129   O64_ABI,
130   N32_ABI,
131   N64_ABI,
132   EABI_ABI
133 };
134
135 /* MIPS ABI we are using for this output file.  */
136 static enum mips_abi_level mips_abi = NO_ABI;
137
138 /* Whether or not we have code that can call pic code.  */
139 int mips_abicalls = FALSE;
140
141 /* This is the set of options which may be modified by the .set
142    pseudo-op.  We use a struct so that .set push and .set pop are more
143    reliable.  */
144
145 struct mips_set_options
146 {
147   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
148      if it has not been initialized.  Changed by `.set mipsN', and the
149      -mipsN command line option, and the default CPU.  */
150   int isa;
151   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
152      if they have not been initialized.  Changed by `.set <asename>', by
153      command line options, and based on the default architecture.  */
154   int ase_mips3d;
155   int ase_mdmx;
156   /* Whether we are assembling for the mips16 processor.  0 if we are
157      not, 1 if we are, and -1 if the value has not been initialized.
158      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
159      -nomips16 command line options, and the default CPU.  */
160   int mips16;
161   /* Non-zero if we should not reorder instructions.  Changed by `.set
162      reorder' and `.set noreorder'.  */
163   int noreorder;
164   /* Non-zero if we should not permit the $at ($1) register to be used
165      in instructions.  Changed by `.set at' and `.set noat'.  */
166   int noat;
167   /* Non-zero if we should warn when a macro instruction expands into
168      more than one machine instruction.  Changed by `.set nomacro' and
169      `.set macro'.  */
170   int warn_about_macros;
171   /* Non-zero if we should not move instructions.  Changed by `.set
172      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
173   int nomove;
174   /* Non-zero if we should not optimize branches by moving the target
175      of the branch into the delay slot.  Actually, we don't perform
176      this optimization anyhow.  Changed by `.set bopt' and `.set
177      nobopt'.  */
178   int nobopt;
179   /* Non-zero if we should not autoextend mips16 instructions.
180      Changed by `.set autoextend' and `.set noautoextend'.  */
181   int noautoextend;
182   /* Restrict general purpose registers and floating point registers
183      to 32 bit.  This is initially determined when -mgp32 or -mfp32
184      is passed but can changed if the assembler code uses .set mipsN.  */
185   int gp32;
186   int fp32;
187   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
188      command line option, and the default CPU.  */
189   int arch;
190 };
191
192 /* True if -mgp32 was passed.  */
193 static int file_mips_gp32 = -1;
194
195 /* True if -mfp32 was passed.  */
196 static int file_mips_fp32 = -1;
197
198 /* This is the struct we use to hold the current set of options.  Note
199    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
200    -1 to indicate that they have not been initialized.  */
201
202 static struct mips_set_options mips_opts =
203 {
204   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
205 };
206
207 /* These variables are filled in with the masks of registers used.
208    The object format code reads them and puts them in the appropriate
209    place.  */
210 unsigned long mips_gprmask;
211 unsigned long mips_cprmask[4];
212
213 /* MIPS ISA we are using for this output file.  */
214 static int file_mips_isa = ISA_UNKNOWN;
215
216 /* True if -mips16 was passed or implied by arguments passed on the
217    command line (e.g., by -march).  */
218 static int file_ase_mips16;
219
220 /* True if -mips3d was passed or implied by arguments passed on the
221    command line (e.g., by -march).  */
222 static int file_ase_mips3d;
223
224 /* True if -mdmx was passed or implied by arguments passed on the
225    command line (e.g., by -march).  */
226 static int file_ase_mdmx;
227
228 /* The argument of the -march= flag.  The architecture we are assembling.  */
229 static int file_mips_arch = CPU_UNKNOWN;
230 static const char *mips_arch_string;
231
232 /* The argument of the -mtune= flag.  The architecture for which we
233    are optimizing.  */
234 static int mips_tune = CPU_UNKNOWN;
235 static const char *mips_tune_string;
236
237 /* True when generating 32-bit code for a 64-bit processor.  */
238 static int mips_32bitmode = 0;
239
240 /* Some ISA's have delay slots for instructions which read or write
241    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
242    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
243    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
244    delay slot in this ISA.  The uses of this macro assume that any
245    ISA that has delay slots for one of these, has them for all.  They
246    also assume that ISAs which don't have delays for these insns, don't
247    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
248 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
249    (ISA) == ISA_MIPS1                       \
250    || (ISA) == ISA_MIPS2                    \
251    || (ISA) == ISA_MIPS3                    \
252    )
253
254 /* True if the given ABI requires 32-bit registers.  */
255 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
256
257 /* Likewise 64-bit registers.  */
258 #define ABI_NEEDS_64BIT_REGS(ABI) \
259   ((ABI) == N32_ABI               \
260    || (ABI) == N64_ABI            \
261    || (ABI) == O64_ABI)
262
263 /*  Return true if ISA supports 64 bit gp register instructions.  */
264 #define ISA_HAS_64BIT_REGS(ISA) (    \
265    (ISA) == ISA_MIPS3                \
266    || (ISA) == ISA_MIPS4             \
267    || (ISA) == ISA_MIPS5             \
268    || (ISA) == ISA_MIPS64            \
269    )
270
271 /* Return true if ISA supports 64-bit right rotate (dror et al.)
272    instructions.  */
273 #define ISA_HAS_DROR(ISA) (     \
274    0                            \
275    )
276
277 /* Return true if ISA supports 32-bit right rotate (ror et al.)
278    instructions.  */
279 #define ISA_HAS_ROR(ISA) (      \
280    (ISA) == ISA_MIPS32R2        \
281    )
282
283 #define HAVE_32BIT_GPRS                            \
284     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
285
286 #define HAVE_32BIT_FPRS                            \
287     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
288
289 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
290 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
291
292 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
293
294 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
295
296 /* We can only have 64bit addresses if the object file format
297    supports it.  */
298 #define HAVE_32BIT_ADDRESSES                           \
299    (HAVE_32BIT_GPRS                                    \
300     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
301          || ! HAVE_64BIT_OBJECTS)                      \
302         && mips_pic != EMBEDDED_PIC))
303
304 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
305
306 /* Addresses are loaded in different ways, depending on the address size
307    in use.  The n32 ABI Documentation also mandates the use of additions
308    with overflow checking, but existing implementations don't follow it.  */
309 #define ADDRESS_ADD_INSN                                                \
310    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
311
312 #define ADDRESS_ADDI_INSN                                               \
313    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
314
315 #define ADDRESS_LOAD_INSN                                               \
316    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
317
318 #define ADDRESS_STORE_INSN                                              \
319    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
320
321 /* Return true if the given CPU supports the MIPS16 ASE.  */
322 #define CPU_HAS_MIPS16(cpu)                                             \
323    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
324     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
325
326 /* Return true if the given CPU supports the MIPS3D ASE.  */
327 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
328                                  )
329
330 /* Return true if the given CPU supports the MDMX ASE.  */
331 #define CPU_HAS_MDMX(cpu)       (FALSE                 \
332                                  )
333
334 /* True if CPU has a dror instruction.  */
335 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
336
337 /* True if CPU has a ror instruction.  */
338 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
339
340 /* Whether the processor uses hardware interlocks to protect
341    reads from the HI and LO registers, and thus does not
342    require nops to be inserted.  */
343
344 #define hilo_interlocks (mips_opts.arch == CPU_R4010                       \
345                          || mips_opts.arch == CPU_VR5500                   \
346                          || mips_opts.arch == CPU_RM7000                   \
347                          || mips_opts.arch == CPU_SB1                      \
348                          )
349
350 /* Whether the processor uses hardware interlocks to protect reads
351    from the GPRs, and thus does not require nops to be inserted.  */
352 #define gpr_interlocks \
353   (mips_opts.isa != ISA_MIPS1  \
354    || mips_opts.arch == CPU_VR5400  \
355    || mips_opts.arch == CPU_VR5500  \
356    || mips_opts.arch == CPU_R3900)
357
358 /* As with other "interlocks" this is used by hardware that has FP
359    (co-processor) interlocks.  */
360 /* Itbl support may require additional care here.  */
361 #define cop_interlocks (mips_opts.arch == CPU_R4300                        \
362                         || mips_opts.arch == CPU_VR5400                    \
363                         || mips_opts.arch == CPU_VR5500                    \
364                         || mips_opts.arch == CPU_SB1                       \
365                         )
366
367 /* Is this a mfhi or mflo instruction?  */
368 #define MF_HILO_INSN(PINFO) \
369           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
370
371 /* MIPS PIC level.  */
372
373 enum mips_pic_level mips_pic;
374
375 /* Warn about all NOPS that the assembler generates.  */
376 static int warn_nops = 0;
377
378 /* 1 if we should generate 32 bit offsets from the $gp register in
379    SVR4_PIC mode.  Currently has no meaning in other modes.  */
380 static int mips_big_got = 0;
381
382 /* 1 if trap instructions should used for overflow rather than break
383    instructions.  */
384 static int mips_trap = 0;
385
386 /* 1 if double width floating point constants should not be constructed
387    by assembling two single width halves into two single width floating
388    point registers which just happen to alias the double width destination
389    register.  On some architectures this aliasing can be disabled by a bit
390    in the status register, and the setting of this bit cannot be determined
391    automatically at assemble time.  */
392 static int mips_disable_float_construction;
393
394 /* Non-zero if any .set noreorder directives were used.  */
395
396 static int mips_any_noreorder;
397
398 /* Non-zero if nops should be inserted when the register referenced in
399    an mfhi/mflo instruction is read in the next two instructions.  */
400 static int mips_7000_hilo_fix;
401
402 /* The size of the small data section.  */
403 static unsigned int g_switch_value = 8;
404 /* Whether the -G option was used.  */
405 static int g_switch_seen = 0;
406
407 #define N_RMASK 0xc4
408 #define N_VFP   0xd4
409
410 /* If we can determine in advance that GP optimization won't be
411    possible, we can skip the relaxation stuff that tries to produce
412    GP-relative references.  This makes delay slot optimization work
413    better.
414
415    This function can only provide a guess, but it seems to work for
416    gcc output.  It needs to guess right for gcc, otherwise gcc
417    will put what it thinks is a GP-relative instruction in a branch
418    delay slot.
419
420    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
421    fixed it for the non-PIC mode.  KR 95/04/07  */
422 static int nopic_need_relax (symbolS *, int);
423
424 /* handle of the OPCODE hash table */
425 static struct hash_control *op_hash = NULL;
426
427 /* The opcode hash table we use for the mips16.  */
428 static struct hash_control *mips16_op_hash = NULL;
429
430 /* This array holds the chars that always start a comment.  If the
431     pre-processor is disabled, these aren't very useful */
432 const char comment_chars[] = "#";
433
434 /* This array holds the chars that only start a comment at the beginning of
435    a line.  If the line seems to have the form '# 123 filename'
436    .line and .file directives will appear in the pre-processed output */
437 /* Note that input_file.c hand checks for '#' at the beginning of the
438    first line of the input file.  This is because the compiler outputs
439    #NO_APP at the beginning of its output.  */
440 /* Also note that C style comments are always supported.  */
441 const char line_comment_chars[] = "#";
442
443 /* This array holds machine specific line separator characters.  */
444 const char line_separator_chars[] = ";";
445
446 /* Chars that can be used to separate mant from exp in floating point nums */
447 const char EXP_CHARS[] = "eE";
448
449 /* Chars that mean this number is a floating point constant */
450 /* As in 0f12.456 */
451 /* or    0d1.2345e12 */
452 const char FLT_CHARS[] = "rRsSfFdDxXpP";
453
454 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
455    changed in read.c .  Ideally it shouldn't have to know about it at all,
456    but nothing is ideal around here.
457  */
458
459 static char *insn_error;
460
461 static int auto_align = 1;
462
463 /* When outputting SVR4 PIC code, the assembler needs to know the
464    offset in the stack frame from which to restore the $gp register.
465    This is set by the .cprestore pseudo-op, and saved in this
466    variable.  */
467 static offsetT mips_cprestore_offset = -1;
468
469 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
470    more optimizations, it can use a register value instead of a memory-saved
471    offset and even an other register than $gp as global pointer.  */
472 static offsetT mips_cpreturn_offset = -1;
473 static int mips_cpreturn_register = -1;
474 static int mips_gp_register = GP;
475 static int mips_gprel_offset = 0;
476
477 /* Whether mips_cprestore_offset has been set in the current function
478    (or whether it has already been warned about, if not).  */
479 static int mips_cprestore_valid = 0;
480
481 /* This is the register which holds the stack frame, as set by the
482    .frame pseudo-op.  This is needed to implement .cprestore.  */
483 static int mips_frame_reg = SP;
484
485 /* Whether mips_frame_reg has been set in the current function
486    (or whether it has already been warned about, if not).  */
487 static int mips_frame_reg_valid = 0;
488
489 /* To output NOP instructions correctly, we need to keep information
490    about the previous two instructions.  */
491
492 /* Whether we are optimizing.  The default value of 2 means to remove
493    unneeded NOPs and swap branch instructions when possible.  A value
494    of 1 means to not swap branches.  A value of 0 means to always
495    insert NOPs.  */
496 static int mips_optimize = 2;
497
498 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
499    equivalent to seeing no -g option at all.  */
500 static int mips_debug = 0;
501
502 /* The previous instruction.  */
503 static struct mips_cl_insn prev_insn;
504
505 /* The instruction before prev_insn.  */
506 static struct mips_cl_insn prev_prev_insn;
507
508 /* If we don't want information for prev_insn or prev_prev_insn, we
509    point the insn_mo field at this dummy integer.  */
510 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
511
512 /* Non-zero if prev_insn is valid.  */
513 static int prev_insn_valid;
514
515 /* The frag for the previous instruction.  */
516 static struct frag *prev_insn_frag;
517
518 /* The offset into prev_insn_frag for the previous instruction.  */
519 static long prev_insn_where;
520
521 /* The reloc type for the previous instruction, if any.  */
522 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
523
524 /* The reloc for the previous instruction, if any.  */
525 static fixS *prev_insn_fixp[3];
526
527 /* Non-zero if the previous instruction was in a delay slot.  */
528 static int prev_insn_is_delay_slot;
529
530 /* Non-zero if the previous instruction was in a .set noreorder.  */
531 static int prev_insn_unreordered;
532
533 /* Non-zero if the previous instruction uses an extend opcode (if
534    mips16).  */
535 static int prev_insn_extended;
536
537 /* Non-zero if the previous previous instruction was in a .set
538    noreorder.  */
539 static int prev_prev_insn_unreordered;
540
541 /* If this is set, it points to a frag holding nop instructions which
542    were inserted before the start of a noreorder section.  If those
543    nops turn out to be unnecessary, the size of the frag can be
544    decreased.  */
545 static fragS *prev_nop_frag;
546
547 /* The number of nop instructions we created in prev_nop_frag.  */
548 static int prev_nop_frag_holds;
549
550 /* The number of nop instructions that we know we need in
551    prev_nop_frag.  */
552 static int prev_nop_frag_required;
553
554 /* The number of instructions we've seen since prev_nop_frag.  */
555 static int prev_nop_frag_since;
556
557 /* For ECOFF and ELF, relocations against symbols are done in two
558    parts, with a HI relocation and a LO relocation.  Each relocation
559    has only 16 bits of space to store an addend.  This means that in
560    order for the linker to handle carries correctly, it must be able
561    to locate both the HI and the LO relocation.  This means that the
562    relocations must appear in order in the relocation table.
563
564    In order to implement this, we keep track of each unmatched HI
565    relocation.  We then sort them so that they immediately precede the
566    corresponding LO relocation.  */
567
568 struct mips_hi_fixup
569 {
570   /* Next HI fixup.  */
571   struct mips_hi_fixup *next;
572   /* This fixup.  */
573   fixS *fixp;
574   /* The section this fixup is in.  */
575   segT seg;
576 };
577
578 /* The list of unmatched HI relocs.  */
579
580 static struct mips_hi_fixup *mips_hi_fixup_list;
581
582 /* The frag containing the last explicit relocation operator.
583    Null if explicit relocations have not been used.  */
584
585 static fragS *prev_reloc_op_frag;
586
587 /* Map normal MIPS register numbers to mips16 register numbers.  */
588
589 #define X ILLEGAL_REG
590 static const int mips32_to_16_reg_map[] =
591 {
592   X, X, 2, 3, 4, 5, 6, 7,
593   X, X, X, X, X, X, X, X,
594   0, 1, X, X, X, X, X, X,
595   X, X, X, X, X, X, X, X
596 };
597 #undef X
598
599 /* Map mips16 register numbers to normal MIPS register numbers.  */
600
601 static const unsigned int mips16_to_32_reg_map[] =
602 {
603   16, 17, 2, 3, 4, 5, 6, 7
604 };
605
606 static int mips_fix_4122_bugs;
607
608 /* We don't relax branches by default, since this causes us to expand
609    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
610    fail to compute the offset before expanding the macro to the most
611    efficient expansion.  */
612
613 static int mips_relax_branch;
614 \f
615 /* Since the MIPS does not have multiple forms of PC relative
616    instructions, we do not have to do relaxing as is done on other
617    platforms.  However, we do have to handle GP relative addressing
618    correctly, which turns out to be a similar problem.
619
620    Every macro that refers to a symbol can occur in (at least) two
621    forms, one with GP relative addressing and one without.  For
622    example, loading a global variable into a register generally uses
623    a macro instruction like this:
624      lw $4,i
625    If i can be addressed off the GP register (this is true if it is in
626    the .sbss or .sdata section, or if it is known to be smaller than
627    the -G argument) this will generate the following instruction:
628      lw $4,i($gp)
629    This instruction will use a GPREL reloc.  If i can not be addressed
630    off the GP register, the following instruction sequence will be used:
631      lui $at,i
632      lw $4,i($at)
633    In this case the first instruction will have a HI16 reloc, and the
634    second reloc will have a LO16 reloc.  Both relocs will be against
635    the symbol i.
636
637    The issue here is that we may not know whether i is GP addressable
638    until after we see the instruction that uses it.  Therefore, we
639    want to be able to choose the final instruction sequence only at
640    the end of the assembly.  This is similar to the way other
641    platforms choose the size of a PC relative instruction only at the
642    end of assembly.
643
644    When generating position independent code we do not use GP
645    addressing in quite the same way, but the issue still arises as
646    external symbols and local symbols must be handled differently.
647
648    We handle these issues by actually generating both possible
649    instruction sequences.  The longer one is put in a frag_var with
650    type rs_machine_dependent.  We encode what to do with the frag in
651    the subtype field.  We encode (1) the number of existing bytes to
652    replace, (2) the number of new bytes to use, (3) the offset from
653    the start of the existing bytes to the first reloc we must generate
654    (that is, the offset is applied from the start of the existing
655    bytes after they are replaced by the new bytes, if any), (4) the
656    offset from the start of the existing bytes to the second reloc,
657    (5) whether a third reloc is needed (the third reloc is always four
658    bytes after the second reloc), and (6) whether to warn if this
659    variant is used (this is sometimes needed if .set nomacro or .set
660    noat is in effect).  All these numbers are reasonably small.
661
662    Generating two instruction sequences must be handled carefully to
663    ensure that delay slots are handled correctly.  Fortunately, there
664    are a limited number of cases.  When the second instruction
665    sequence is generated, append_insn is directed to maintain the
666    existing delay slot information, so it continues to apply to any
667    code after the second instruction sequence.  This means that the
668    second instruction sequence must not impose any requirements not
669    required by the first instruction sequence.
670
671    These variant frags are then handled in functions called by the
672    machine independent code.  md_estimate_size_before_relax returns
673    the final size of the frag.  md_convert_frag sets up the final form
674    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
675    one if needed.  */
676 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
677   ((relax_substateT) \
678    (((old) << 23) \
679     | ((new) << 16) \
680     | (((reloc1) + 64) << 9) \
681     | (((reloc2) + 64) << 2) \
682     | ((reloc3) ? (1 << 1) : 0) \
683     | ((warn) ? 1 : 0)))
684 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
685 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
686 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
687 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
688 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
689 #define RELAX_WARN(i) ((i) & 1)
690
691 /* Branch without likely bit.  If label is out of range, we turn:
692
693         beq reg1, reg2, label
694         delay slot
695
696    into
697
698         bne reg1, reg2, 0f
699         nop
700         j label
701      0: delay slot
702
703    with the following opcode replacements:
704
705         beq <-> bne
706         blez <-> bgtz
707         bltz <-> bgez
708         bc1f <-> bc1t
709
710         bltzal <-> bgezal  (with jal label instead of j label)
711
712    Even though keeping the delay slot instruction in the delay slot of
713    the branch would be more efficient, it would be very tricky to do
714    correctly, because we'd have to introduce a variable frag *after*
715    the delay slot instruction, and expand that instead.  Let's do it
716    the easy way for now, even if the branch-not-taken case now costs
717    one additional instruction.  Out-of-range branches are not supposed
718    to be common, anyway.
719
720    Branch likely.  If label is out of range, we turn:
721
722         beql reg1, reg2, label
723         delay slot (annulled if branch not taken)
724
725    into
726
727         beql reg1, reg2, 1f
728         nop
729         beql $0, $0, 2f
730         nop
731      1: j[al] label
732         delay slot (executed only if branch taken)
733      2:
734
735    It would be possible to generate a shorter sequence by losing the
736    likely bit, generating something like:
737
738         bne reg1, reg2, 0f
739         nop
740         j[al] label
741         delay slot (executed only if branch taken)
742      0:
743
744         beql -> bne
745         bnel -> beq
746         blezl -> bgtz
747         bgtzl -> blez
748         bltzl -> bgez
749         bgezl -> bltz
750         bc1fl -> bc1t
751         bc1tl -> bc1f
752
753         bltzall -> bgezal  (with jal label instead of j label)
754         bgezall -> bltzal  (ditto)
755
756
757    but it's not clear that it would actually improve performance.  */
758 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
759   ((relax_substateT) \
760    (0xc0000000 \
761     | ((toofar) ? 1 : 0) \
762     | ((link) ? 2 : 0) \
763     | ((likely) ? 4 : 0) \
764     | ((uncond) ? 8 : 0)))
765 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
766 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
767 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
768 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
769 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
770
771 /* For mips16 code, we use an entirely different form of relaxation.
772    mips16 supports two versions of most instructions which take
773    immediate values: a small one which takes some small value, and a
774    larger one which takes a 16 bit value.  Since branches also follow
775    this pattern, relaxing these values is required.
776
777    We can assemble both mips16 and normal MIPS code in a single
778    object.  Therefore, we need to support this type of relaxation at
779    the same time that we support the relaxation described above.  We
780    use the high bit of the subtype field to distinguish these cases.
781
782    The information we store for this type of relaxation is the
783    argument code found in the opcode file for this relocation, whether
784    the user explicitly requested a small or extended form, and whether
785    the relocation is in a jump or jal delay slot.  That tells us the
786    size of the value, and how it should be stored.  We also store
787    whether the fragment is considered to be extended or not.  We also
788    store whether this is known to be a branch to a different section,
789    whether we have tried to relax this frag yet, and whether we have
790    ever extended a PC relative fragment because of a shift count.  */
791 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
792   (0x80000000                                                   \
793    | ((type) & 0xff)                                            \
794    | ((small) ? 0x100 : 0)                                      \
795    | ((ext) ? 0x200 : 0)                                        \
796    | ((dslot) ? 0x400 : 0)                                      \
797    | ((jal_dslot) ? 0x800 : 0))
798 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
799 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
800 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
801 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
802 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
803 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
804 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
805 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
806 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
807 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
808 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
809 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
810
811 /* Is the given value a sign-extended 32-bit value?  */
812 #define IS_SEXT_32BIT_NUM(x)                                            \
813   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
814    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
815
816 /* Is the given value a sign-extended 16-bit value?  */
817 #define IS_SEXT_16BIT_NUM(x)                                            \
818   (((x) &~ (offsetT) 0x7fff) == 0                                       \
819    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
820
821 \f
822 /* Prototypes for static functions.  */
823
824 #define internalError()                                                 \
825     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
826
827 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
828
829 static void append_insn
830   (char *place, struct mips_cl_insn *ip, expressionS *p,
831    bfd_reloc_code_real_type *r);
832 static void mips_no_prev_insn (int);
833 static void mips16_macro_build
834   (char *, int *, expressionS *, const char *, const char *, va_list);
835 static void load_register (int *, int, expressionS *, int);
836 static void macro (struct mips_cl_insn * ip);
837 static void mips16_macro (struct mips_cl_insn * ip);
838 #ifdef LOSING_COMPILER
839 static void macro2 (struct mips_cl_insn * ip);
840 #endif
841 static void mips_ip (char *str, struct mips_cl_insn * ip);
842 static void mips16_ip (char *str, struct mips_cl_insn * ip);
843 static void mips16_immed
844   (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
845    unsigned long *, bfd_boolean *, unsigned short *);
846 static size_t my_getSmallExpression
847   (expressionS *, bfd_reloc_code_real_type *, char *);
848 static void my_getExpression (expressionS *, char *);
849 static void s_align (int);
850 static void s_change_sec (int);
851 static void s_change_section (int);
852 static void s_cons (int);
853 static void s_float_cons (int);
854 static void s_mips_globl (int);
855 static void s_option (int);
856 static void s_mipsset (int);
857 static void s_abicalls (int);
858 static void s_cpload (int);
859 static void s_cpsetup (int);
860 static void s_cplocal (int);
861 static void s_cprestore (int);
862 static void s_cpreturn (int);
863 static void s_gpvalue (int);
864 static void s_gpword (int);
865 static void s_gpdword (int);
866 static void s_cpadd (int);
867 static void s_insn (int);
868 static void md_obj_begin (void);
869 static void md_obj_end (void);
870 static void s_mips_ent (int);
871 static void s_mips_end (int);
872 static void s_mips_frame (int);
873 static void s_mips_mask (int reg_type);
874 static void s_mips_stab (int);
875 static void s_mips_weakext (int);
876 static void s_mips_file (int);
877 static void s_mips_loc (int);
878 static bfd_boolean pic_need_relax (symbolS *, asection *);
879 static int relaxed_branch_length (fragS *, asection *, int);
880 static int validate_mips_insn (const struct mips_opcode *);
881
882 /* Table and functions used to map between CPU/ISA names, and
883    ISA levels, and CPU numbers.  */
884
885 struct mips_cpu_info
886 {
887   const char *name;           /* CPU or ISA name.  */
888   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
889   int isa;                    /* ISA level.  */
890   int cpu;                    /* CPU number (default CPU if ISA).  */
891 };
892
893 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
894 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
895 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
896 \f
897 /* Pseudo-op table.
898
899    The following pseudo-ops from the Kane and Heinrich MIPS book
900    should be defined here, but are currently unsupported: .alias,
901    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
902
903    The following pseudo-ops from the Kane and Heinrich MIPS book are
904    specific to the type of debugging information being generated, and
905    should be defined by the object format: .aent, .begin, .bend,
906    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
907    .vreg.
908
909    The following pseudo-ops from the Kane and Heinrich MIPS book are
910    not MIPS CPU specific, but are also not specific to the object file
911    format.  This file is probably the best place to define them, but
912    they are not currently supported: .asm0, .endr, .lab, .repeat,
913    .struct.  */
914
915 static const pseudo_typeS mips_pseudo_table[] =
916 {
917   /* MIPS specific pseudo-ops.  */
918   {"option", s_option, 0},
919   {"set", s_mipsset, 0},
920   {"rdata", s_change_sec, 'r'},
921   {"sdata", s_change_sec, 's'},
922   {"livereg", s_ignore, 0},
923   {"abicalls", s_abicalls, 0},
924   {"cpload", s_cpload, 0},
925   {"cpsetup", s_cpsetup, 0},
926   {"cplocal", s_cplocal, 0},
927   {"cprestore", s_cprestore, 0},
928   {"cpreturn", s_cpreturn, 0},
929   {"gpvalue", s_gpvalue, 0},
930   {"gpword", s_gpword, 0},
931   {"gpdword", s_gpdword, 0},
932   {"cpadd", s_cpadd, 0},
933   {"insn", s_insn, 0},
934
935   /* Relatively generic pseudo-ops that happen to be used on MIPS
936      chips.  */
937   {"asciiz", stringer, 1},
938   {"bss", s_change_sec, 'b'},
939   {"err", s_err, 0},
940   {"half", s_cons, 1},
941   {"dword", s_cons, 3},
942   {"weakext", s_mips_weakext, 0},
943
944   /* These pseudo-ops are defined in read.c, but must be overridden
945      here for one reason or another.  */
946   {"align", s_align, 0},
947   {"byte", s_cons, 0},
948   {"data", s_change_sec, 'd'},
949   {"double", s_float_cons, 'd'},
950   {"float", s_float_cons, 'f'},
951   {"globl", s_mips_globl, 0},
952   {"global", s_mips_globl, 0},
953   {"hword", s_cons, 1},
954   {"int", s_cons, 2},
955   {"long", s_cons, 2},
956   {"octa", s_cons, 4},
957   {"quad", s_cons, 3},
958   {"section", s_change_section, 0},
959   {"short", s_cons, 1},
960   {"single", s_float_cons, 'f'},
961   {"stabn", s_mips_stab, 'n'},
962   {"text", s_change_sec, 't'},
963   {"word", s_cons, 2},
964
965   { "extern", ecoff_directive_extern, 0},
966
967   { NULL, NULL, 0 },
968 };
969
970 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
971 {
972   /* These pseudo-ops should be defined by the object file format.
973      However, a.out doesn't support them, so we have versions here.  */
974   {"aent", s_mips_ent, 1},
975   {"bgnb", s_ignore, 0},
976   {"end", s_mips_end, 0},
977   {"endb", s_ignore, 0},
978   {"ent", s_mips_ent, 0},
979   {"file", s_mips_file, 0},
980   {"fmask", s_mips_mask, 'F'},
981   {"frame", s_mips_frame, 0},
982   {"loc", s_mips_loc, 0},
983   {"mask", s_mips_mask, 'R'},
984   {"verstamp", s_ignore, 0},
985   { NULL, NULL, 0 },
986 };
987
988 extern void pop_insert (const pseudo_typeS *);
989
990 void
991 mips_pop_insert (void)
992 {
993   pop_insert (mips_pseudo_table);
994   if (! ECOFF_DEBUGGING)
995     pop_insert (mips_nonecoff_pseudo_table);
996 }
997 \f
998 /* Symbols labelling the current insn.  */
999
1000 struct insn_label_list
1001 {
1002   struct insn_label_list *next;
1003   symbolS *label;
1004 };
1005
1006 static struct insn_label_list *insn_labels;
1007 static struct insn_label_list *free_insn_labels;
1008
1009 static void mips_clear_insn_labels (void);
1010
1011 static inline void
1012 mips_clear_insn_labels (void)
1013 {
1014   register struct insn_label_list **pl;
1015
1016   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1017     ;
1018   *pl = insn_labels;
1019   insn_labels = NULL;
1020 }
1021 \f
1022 static char *expr_end;
1023
1024 /* Expressions which appear in instructions.  These are set by
1025    mips_ip.  */
1026
1027 static expressionS imm_expr;
1028 static expressionS offset_expr;
1029
1030 /* Relocs associated with imm_expr and offset_expr.  */
1031
1032 static bfd_reloc_code_real_type imm_reloc[3]
1033   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1034 static bfd_reloc_code_real_type offset_reloc[3]
1035   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1036
1037 /* These are set by mips16_ip if an explicit extension is used.  */
1038
1039 static bfd_boolean mips16_small, mips16_ext;
1040
1041 #ifdef OBJ_ELF
1042 /* The pdr segment for per procedure frame/regmask info.  Not used for
1043    ECOFF debugging.  */
1044
1045 static segT pdr_seg;
1046 #endif
1047
1048 /* The default target format to use.  */
1049
1050 const char *
1051 mips_target_format (void)
1052 {
1053   switch (OUTPUT_FLAVOR)
1054     {
1055     case bfd_target_aout_flavour:
1056       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1057     case bfd_target_ecoff_flavour:
1058       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1059     case bfd_target_coff_flavour:
1060       return "pe-mips";
1061     case bfd_target_elf_flavour:
1062 #ifdef TE_TMIPS
1063       /* This is traditional mips.  */
1064       return (target_big_endian
1065               ? (HAVE_64BIT_OBJECTS
1066                  ? "elf64-tradbigmips"
1067                  : (HAVE_NEWABI
1068                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1069               : (HAVE_64BIT_OBJECTS
1070                  ? "elf64-tradlittlemips"
1071                  : (HAVE_NEWABI
1072                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1073 #else
1074       return (target_big_endian
1075               ? (HAVE_64BIT_OBJECTS
1076                  ? "elf64-bigmips"
1077                  : (HAVE_NEWABI
1078                     ? "elf32-nbigmips" : "elf32-bigmips"))
1079               : (HAVE_64BIT_OBJECTS
1080                  ? "elf64-littlemips"
1081                  : (HAVE_NEWABI
1082                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1083 #endif
1084     default:
1085       abort ();
1086       return NULL;
1087     }
1088 }
1089
1090 /* This function is called once, at assembler startup time.  It should
1091    set up all the tables, etc. that the MD part of the assembler will need.  */
1092
1093 void
1094 md_begin (void)
1095 {
1096   register const char *retval = NULL;
1097   int i = 0;
1098   int broken = 0;
1099
1100   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1101     as_warn (_("Could not set architecture and machine"));
1102
1103   op_hash = hash_new ();
1104
1105   for (i = 0; i < NUMOPCODES;)
1106     {
1107       const char *name = mips_opcodes[i].name;
1108
1109       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1110       if (retval != NULL)
1111         {
1112           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1113                    mips_opcodes[i].name, retval);
1114           /* Probably a memory allocation problem?  Give up now.  */
1115           as_fatal (_("Broken assembler.  No assembly attempted."));
1116         }
1117       do
1118         {
1119           if (mips_opcodes[i].pinfo != INSN_MACRO)
1120             {
1121               if (!validate_mips_insn (&mips_opcodes[i]))
1122                 broken = 1;
1123             }
1124           ++i;
1125         }
1126       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1127     }
1128
1129   mips16_op_hash = hash_new ();
1130
1131   i = 0;
1132   while (i < bfd_mips16_num_opcodes)
1133     {
1134       const char *name = mips16_opcodes[i].name;
1135
1136       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1137       if (retval != NULL)
1138         as_fatal (_("internal: can't hash `%s': %s"),
1139                   mips16_opcodes[i].name, retval);
1140       do
1141         {
1142           if (mips16_opcodes[i].pinfo != INSN_MACRO
1143               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1144                   != mips16_opcodes[i].match))
1145             {
1146               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1147                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1148               broken = 1;
1149             }
1150           ++i;
1151         }
1152       while (i < bfd_mips16_num_opcodes
1153              && strcmp (mips16_opcodes[i].name, name) == 0);
1154     }
1155
1156   if (broken)
1157     as_fatal (_("Broken assembler.  No assembly attempted."));
1158
1159   /* We add all the general register names to the symbol table.  This
1160      helps us detect invalid uses of them.  */
1161   for (i = 0; i < 32; i++)
1162     {
1163       char buf[5];
1164
1165       sprintf (buf, "$%d", i);
1166       symbol_table_insert (symbol_new (buf, reg_section, i,
1167                                        &zero_address_frag));
1168     }
1169   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1170                                    &zero_address_frag));
1171   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1172                                    &zero_address_frag));
1173   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1174                                    &zero_address_frag));
1175   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1176                                    &zero_address_frag));
1177   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1178                                    &zero_address_frag));
1179   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1180                                    &zero_address_frag));
1181   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1182                                    &zero_address_frag));
1183   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1184                                    &zero_address_frag));
1185   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1186                                    &zero_address_frag));
1187
1188   /* If we don't add these register names to the symbol table, they
1189      may end up being added as regular symbols by operand(), and then
1190      make it to the object file as undefined in case they're not
1191      regarded as local symbols.  They're local in o32, since `$' is a
1192      local symbol prefix, but not in n32 or n64.  */
1193   for (i = 0; i < 8; i++)
1194     {
1195       char buf[6];
1196
1197       sprintf (buf, "$fcc%i", i);
1198       symbol_table_insert (symbol_new (buf, reg_section, -1,
1199                                        &zero_address_frag));
1200     }
1201
1202   mips_no_prev_insn (FALSE);
1203
1204   mips_gprmask = 0;
1205   mips_cprmask[0] = 0;
1206   mips_cprmask[1] = 0;
1207   mips_cprmask[2] = 0;
1208   mips_cprmask[3] = 0;
1209
1210   /* set the default alignment for the text section (2**2) */
1211   record_alignment (text_section, 2);
1212
1213   if (USE_GLOBAL_POINTER_OPT)
1214     bfd_set_gp_size (stdoutput, g_switch_value);
1215
1216   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1217     {
1218       /* On a native system, sections must be aligned to 16 byte
1219          boundaries.  When configured for an embedded ELF target, we
1220          don't bother.  */
1221       if (strcmp (TARGET_OS, "elf") != 0)
1222         {
1223           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1224           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1225           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1226         }
1227
1228       /* Create a .reginfo section for register masks and a .mdebug
1229          section for debugging information.  */
1230       {
1231         segT seg;
1232         subsegT subseg;
1233         flagword flags;
1234         segT sec;
1235
1236         seg = now_seg;
1237         subseg = now_subseg;
1238
1239         /* The ABI says this section should be loaded so that the
1240            running program can access it.  However, we don't load it
1241            if we are configured for an embedded target */
1242         flags = SEC_READONLY | SEC_DATA;
1243         if (strcmp (TARGET_OS, "elf") != 0)
1244           flags |= SEC_ALLOC | SEC_LOAD;
1245
1246         if (mips_abi != N64_ABI)
1247           {
1248             sec = subseg_new (".reginfo", (subsegT) 0);
1249
1250             bfd_set_section_flags (stdoutput, sec, flags);
1251             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1252
1253 #ifdef OBJ_ELF
1254             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1255 #endif
1256           }
1257         else
1258           {
1259             /* The 64-bit ABI uses a .MIPS.options section rather than
1260                .reginfo section.  */
1261             sec = subseg_new (".MIPS.options", (subsegT) 0);
1262             bfd_set_section_flags (stdoutput, sec, flags);
1263             bfd_set_section_alignment (stdoutput, sec, 3);
1264
1265 #ifdef OBJ_ELF
1266             /* Set up the option header.  */
1267             {
1268               Elf_Internal_Options opthdr;
1269               char *f;
1270
1271               opthdr.kind = ODK_REGINFO;
1272               opthdr.size = (sizeof (Elf_External_Options)
1273                              + sizeof (Elf64_External_RegInfo));
1274               opthdr.section = 0;
1275               opthdr.info = 0;
1276               f = frag_more (sizeof (Elf_External_Options));
1277               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1278                                              (Elf_External_Options *) f);
1279
1280               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1281             }
1282 #endif
1283           }
1284
1285         if (ECOFF_DEBUGGING)
1286           {
1287             sec = subseg_new (".mdebug", (subsegT) 0);
1288             (void) bfd_set_section_flags (stdoutput, sec,
1289                                           SEC_HAS_CONTENTS | SEC_READONLY);
1290             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1291           }
1292 #ifdef OBJ_ELF
1293         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1294           {
1295             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1296             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1297                                           SEC_READONLY | SEC_RELOC
1298                                           | SEC_DEBUGGING);
1299             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1300           }
1301 #endif
1302
1303         subseg_set (seg, subseg);
1304       }
1305     }
1306
1307   if (! ECOFF_DEBUGGING)
1308     md_obj_begin ();
1309 }
1310
1311 void
1312 md_mips_end (void)
1313 {
1314   if (! ECOFF_DEBUGGING)
1315     md_obj_end ();
1316 }
1317
1318 void
1319 md_assemble (char *str)
1320 {
1321   struct mips_cl_insn insn;
1322   bfd_reloc_code_real_type unused_reloc[3]
1323     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1324
1325   imm_expr.X_op = O_absent;
1326   offset_expr.X_op = O_absent;
1327   imm_reloc[0] = BFD_RELOC_UNUSED;
1328   imm_reloc[1] = BFD_RELOC_UNUSED;
1329   imm_reloc[2] = BFD_RELOC_UNUSED;
1330   offset_reloc[0] = BFD_RELOC_UNUSED;
1331   offset_reloc[1] = BFD_RELOC_UNUSED;
1332   offset_reloc[2] = BFD_RELOC_UNUSED;
1333
1334   if (mips_opts.mips16)
1335     mips16_ip (str, &insn);
1336   else
1337     {
1338       mips_ip (str, &insn);
1339       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1340             str, insn.insn_opcode));
1341     }
1342
1343   if (insn_error)
1344     {
1345       as_bad ("%s `%s'", insn_error, str);
1346       return;
1347     }
1348
1349   if (insn.insn_mo->pinfo == INSN_MACRO)
1350     {
1351       if (mips_opts.mips16)
1352         mips16_macro (&insn);
1353       else
1354         macro (&insn);
1355     }
1356   else
1357     {
1358       if (imm_expr.X_op != O_absent)
1359         append_insn (NULL, &insn, &imm_expr, imm_reloc);
1360       else if (offset_expr.X_op != O_absent)
1361         append_insn (NULL, &insn, &offset_expr, offset_reloc);
1362       else
1363         append_insn (NULL, &insn, NULL, unused_reloc);
1364     }
1365 }
1366
1367 /* Return true if the given relocation might need a matching %lo().
1368    Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1369    applied to local symbols.  */
1370
1371 static inline bfd_boolean
1372 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1373 {
1374   return (reloc == BFD_RELOC_HI16_S
1375           || reloc == BFD_RELOC_MIPS_GOT16);
1376 }
1377
1378 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1379    relocation.  */
1380
1381 static inline bfd_boolean
1382 fixup_has_matching_lo_p (fixS *fixp)
1383 {
1384   return (fixp->fx_next != NULL
1385           && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1386           && fixp->fx_addsy == fixp->fx_next->fx_addsy
1387           && fixp->fx_offset == fixp->fx_next->fx_offset);
1388 }
1389
1390 /* See whether instruction IP reads register REG.  CLASS is the type
1391    of register.  */
1392
1393 static int
1394 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1395                enum mips_regclass class)
1396 {
1397   if (class == MIPS16_REG)
1398     {
1399       assert (mips_opts.mips16);
1400       reg = mips16_to_32_reg_map[reg];
1401       class = MIPS_GR_REG;
1402     }
1403
1404   /* Don't report on general register ZERO, since it never changes.  */
1405   if (class == MIPS_GR_REG && reg == ZERO)
1406     return 0;
1407
1408   if (class == MIPS_FP_REG)
1409     {
1410       assert (! mips_opts.mips16);
1411       /* If we are called with either $f0 or $f1, we must check $f0.
1412          This is not optimal, because it will introduce an unnecessary
1413          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1414          need to distinguish reading both $f0 and $f1 or just one of
1415          them.  Note that we don't have to check the other way,
1416          because there is no instruction that sets both $f0 and $f1
1417          and requires a delay.  */
1418       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1419           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1420               == (reg &~ (unsigned) 1)))
1421         return 1;
1422       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1423           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1424               == (reg &~ (unsigned) 1)))
1425         return 1;
1426     }
1427   else if (! mips_opts.mips16)
1428     {
1429       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1430           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1431         return 1;
1432       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1433           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1434         return 1;
1435     }
1436   else
1437     {
1438       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1439           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1440                                     & MIPS16OP_MASK_RX)]
1441               == reg))
1442         return 1;
1443       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1444           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1445                                     & MIPS16OP_MASK_RY)]
1446               == reg))
1447         return 1;
1448       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1449           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1450                                     & MIPS16OP_MASK_MOVE32Z)]
1451               == reg))
1452         return 1;
1453       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1454         return 1;
1455       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1456         return 1;
1457       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1458         return 1;
1459       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1460           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1461               & MIPS16OP_MASK_REGR32) == reg)
1462         return 1;
1463     }
1464
1465   return 0;
1466 }
1467
1468 /* This function returns true if modifying a register requires a
1469    delay.  */
1470
1471 static int
1472 reg_needs_delay (unsigned int reg)
1473 {
1474   unsigned long prev_pinfo;
1475
1476   prev_pinfo = prev_insn.insn_mo->pinfo;
1477   if (! mips_opts.noreorder
1478       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1479       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1480           || (! gpr_interlocks
1481               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1482     {
1483       /* A load from a coprocessor or from memory.  All load
1484          delays delay the use of general register rt for one
1485          instruction on the r3000.  The r6000 and r4000 use
1486          interlocks.  */
1487       /* Itbl support may require additional care here.  */
1488       know (prev_pinfo & INSN_WRITE_GPR_T);
1489       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1490         return 1;
1491     }
1492
1493   return 0;
1494 }
1495
1496 /* Mark instruction labels in mips16 mode.  This permits the linker to
1497    handle them specially, such as generating jalx instructions when
1498    needed.  We also make them odd for the duration of the assembly, in
1499    order to generate the right sort of code.  We will make them even
1500    in the adjust_symtab routine, while leaving them marked.  This is
1501    convenient for the debugger and the disassembler.  The linker knows
1502    to make them odd again.  */
1503
1504 static void
1505 mips16_mark_labels (void)
1506 {
1507   if (mips_opts.mips16)
1508     {
1509       struct insn_label_list *l;
1510       valueT val;
1511
1512       for (l = insn_labels; l != NULL; l = l->next)
1513         {
1514 #ifdef OBJ_ELF
1515           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1516             S_SET_OTHER (l->label, STO_MIPS16);
1517 #endif
1518           val = S_GET_VALUE (l->label);
1519           if ((val & 1) == 0)
1520             S_SET_VALUE (l->label, val + 1);
1521         }
1522     }
1523 }
1524
1525 /* Output an instruction.  PLACE is where to put the instruction; if
1526    it is NULL, this uses frag_more to get room.  IP is the instruction
1527    information.  ADDRESS_EXPR is an operand of the instruction to be
1528    used with RELOC_TYPE.  */
1529
1530 static void
1531 append_insn (char *place, struct mips_cl_insn *ip, expressionS *address_expr,
1532              bfd_reloc_code_real_type *reloc_type)
1533 {
1534   register unsigned long prev_pinfo, pinfo;
1535   char *f;
1536   fixS *fixp[3];
1537   int nops = 0;
1538   bfd_boolean force_new_frag = FALSE;
1539
1540   /* Mark instruction labels in mips16 mode.  */
1541   mips16_mark_labels ();
1542
1543   prev_pinfo = prev_insn.insn_mo->pinfo;
1544   pinfo = ip->insn_mo->pinfo;
1545
1546   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1547     {
1548       int prev_prev_nop;
1549
1550       /* If the previous insn required any delay slots, see if we need
1551          to insert a NOP or two.  There are eight kinds of possible
1552          hazards, of which an instruction can have at most one type.
1553          (1) a load from memory delay
1554          (2) a load from a coprocessor delay
1555          (3) an unconditional branch delay
1556          (4) a conditional branch delay
1557          (5) a move to coprocessor register delay
1558          (6) a load coprocessor register from memory delay
1559          (7) a coprocessor condition code delay
1560          (8) a HI/LO special register delay
1561
1562          There are a lot of optimizations we could do that we don't.
1563          In particular, we do not, in general, reorder instructions.
1564          If you use gcc with optimization, it will reorder
1565          instructions and generally do much more optimization then we
1566          do here; repeating all that work in the assembler would only
1567          benefit hand written assembly code, and does not seem worth
1568          it.  */
1569
1570       /* This is how a NOP is emitted.  */
1571 #define emit_nop()                                      \
1572   (mips_opts.mips16                                     \
1573    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1574    : md_number_to_chars (frag_more (4), 0, 4))
1575
1576       /* The previous insn might require a delay slot, depending upon
1577          the contents of the current insn.  */
1578       if (! mips_opts.mips16
1579           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1580           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1581                && ! cop_interlocks)
1582               || (! gpr_interlocks
1583                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1584         {
1585           /* A load from a coprocessor or from memory.  All load
1586              delays delay the use of general register rt for one
1587              instruction on the r3000.  The r6000 and r4000 use
1588              interlocks.  */
1589           /* Itbl support may require additional care here.  */
1590           know (prev_pinfo & INSN_WRITE_GPR_T);
1591           if (mips_optimize == 0
1592               || insn_uses_reg (ip,
1593                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1594                                  & OP_MASK_RT),
1595                                 MIPS_GR_REG))
1596             ++nops;
1597         }
1598       else if (! mips_opts.mips16
1599                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1600                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1601                     && ! cop_interlocks)
1602                    || (mips_opts.isa == ISA_MIPS1
1603                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1604         {
1605           /* A generic coprocessor delay.  The previous instruction
1606              modified a coprocessor general or control register.  If
1607              it modified a control register, we need to avoid any
1608              coprocessor instruction (this is probably not always
1609              required, but it sometimes is).  If it modified a general
1610              register, we avoid using that register.
1611
1612              On the r6000 and r4000 loading a coprocessor register
1613              from memory is interlocked, and does not require a delay.
1614
1615              This case is not handled very well.  There is no special
1616              knowledge of CP0 handling, and the coprocessors other
1617              than the floating point unit are not distinguished at
1618              all.  */
1619           /* Itbl support may require additional care here. FIXME!
1620              Need to modify this to include knowledge about
1621              user specified delays!  */
1622           if (prev_pinfo & INSN_WRITE_FPR_T)
1623             {
1624               if (mips_optimize == 0
1625                   || insn_uses_reg (ip,
1626                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1627                                      & OP_MASK_FT),
1628                                     MIPS_FP_REG))
1629                 ++nops;
1630             }
1631           else if (prev_pinfo & INSN_WRITE_FPR_S)
1632             {
1633               if (mips_optimize == 0
1634                   || insn_uses_reg (ip,
1635                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1636                                      & OP_MASK_FS),
1637                                     MIPS_FP_REG))
1638                 ++nops;
1639             }
1640           else
1641             {
1642               /* We don't know exactly what the previous instruction
1643                  does.  If the current instruction uses a coprocessor
1644                  register, we must insert a NOP.  If previous
1645                  instruction may set the condition codes, and the
1646                  current instruction uses them, we must insert two
1647                  NOPS.  */
1648               /* Itbl support may require additional care here.  */
1649               if (mips_optimize == 0
1650                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1651                       && (pinfo & INSN_READ_COND_CODE)))
1652                 nops += 2;
1653               else if (pinfo & INSN_COP)
1654                 ++nops;
1655             }
1656         }
1657       else if (! mips_opts.mips16
1658                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1659                && (prev_pinfo & INSN_WRITE_COND_CODE)
1660                && ! cop_interlocks)
1661         {
1662           /* The previous instruction sets the coprocessor condition
1663              codes, but does not require a general coprocessor delay
1664              (this means it is a floating point comparison
1665              instruction).  If this instruction uses the condition
1666              codes, we need to insert a single NOP.  */
1667           /* Itbl support may require additional care here.  */
1668           if (mips_optimize == 0
1669               || (pinfo & INSN_READ_COND_CODE))
1670             ++nops;
1671         }
1672
1673       /* If we're fixing up mfhi/mflo for the r7000 and the
1674          previous insn was an mfhi/mflo and the current insn
1675          reads the register that the mfhi/mflo wrote to, then
1676          insert two nops.  */
1677
1678       else if (mips_7000_hilo_fix
1679                && MF_HILO_INSN (prev_pinfo)
1680                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1681                                       & OP_MASK_RD),
1682                                  MIPS_GR_REG))
1683         {
1684           nops += 2;
1685         }
1686
1687       /* If we're fixing up mfhi/mflo for the r7000 and the
1688          2nd previous insn was an mfhi/mflo and the current insn
1689          reads the register that the mfhi/mflo wrote to, then
1690          insert one nop.  */
1691
1692       else if (mips_7000_hilo_fix
1693                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1694                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1695                                        & OP_MASK_RD),
1696                                     MIPS_GR_REG))
1697
1698         {
1699           ++nops;
1700         }
1701
1702       else if (prev_pinfo & INSN_READ_LO)
1703         {
1704           /* The previous instruction reads the LO register; if the
1705              current instruction writes to the LO register, we must
1706              insert two NOPS.  Some newer processors have interlocks.
1707              Also the tx39's multiply instructions can be exectuted
1708              immediatly after a read from HI/LO (without the delay),
1709              though the tx39's divide insns still do require the
1710              delay.  */
1711           if (! (hilo_interlocks
1712                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1713               && (mips_optimize == 0
1714                   || (pinfo & INSN_WRITE_LO)))
1715             nops += 2;
1716           /* Most mips16 branch insns don't have a delay slot.
1717              If a read from LO is immediately followed by a branch
1718              to a write to LO we have a read followed by a write
1719              less than 2 insns away.  We assume the target of
1720              a branch might be a write to LO, and insert a nop
1721              between a read and an immediately following branch.  */
1722           else if (mips_opts.mips16
1723                    && (mips_optimize == 0
1724                        || (pinfo & MIPS16_INSN_BRANCH)))
1725             ++nops;
1726         }
1727       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1728         {
1729           /* The previous instruction reads the HI register; if the
1730              current instruction writes to the HI register, we must
1731              insert a NOP.  Some newer processors have interlocks.
1732              Also the note tx39's multiply above.  */
1733           if (! (hilo_interlocks
1734                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1735               && (mips_optimize == 0
1736                   || (pinfo & INSN_WRITE_HI)))
1737             nops += 2;
1738           /* Most mips16 branch insns don't have a delay slot.
1739              If a read from HI is immediately followed by a branch
1740              to a write to HI we have a read followed by a write
1741              less than 2 insns away.  We assume the target of
1742              a branch might be a write to HI, and insert a nop
1743              between a read and an immediately following branch.  */
1744           else if (mips_opts.mips16
1745                    && (mips_optimize == 0
1746                        || (pinfo & MIPS16_INSN_BRANCH)))
1747             ++nops;
1748         }
1749
1750       /* If the previous instruction was in a noreorder section, then
1751          we don't want to insert the nop after all.  */
1752       /* Itbl support may require additional care here.  */
1753       if (prev_insn_unreordered)
1754         nops = 0;
1755
1756       /* There are two cases which require two intervening
1757          instructions: 1) setting the condition codes using a move to
1758          coprocessor instruction which requires a general coprocessor
1759          delay and then reading the condition codes 2) reading the HI
1760          or LO register and then writing to it (except on processors
1761          which have interlocks).  If we are not already emitting a NOP
1762          instruction, we must check for these cases compared to the
1763          instruction previous to the previous instruction.  */
1764       if ((! mips_opts.mips16
1765            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1766            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1767            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1768            && (pinfo & INSN_READ_COND_CODE)
1769            && ! cop_interlocks)
1770           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1771               && (pinfo & INSN_WRITE_LO)
1772               && ! (hilo_interlocks
1773                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1774           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1775               && (pinfo & INSN_WRITE_HI)
1776               && ! (hilo_interlocks
1777                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1778         prev_prev_nop = 1;
1779       else
1780         prev_prev_nop = 0;
1781
1782       if (prev_prev_insn_unreordered)
1783         prev_prev_nop = 0;
1784
1785       if (prev_prev_nop && nops == 0)
1786         ++nops;
1787
1788       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1789         {
1790           /* We're out of bits in pinfo, so we must resort to string
1791              ops here.  Shortcuts are selected based on opcodes being
1792              limited to the VR4122 instruction set.  */
1793           int min_nops = 0;
1794           const char *pn = prev_insn.insn_mo->name;
1795           const char *tn = ip->insn_mo->name;
1796           if (strncmp(pn, "macc", 4) == 0
1797               || strncmp(pn, "dmacc", 5) == 0)
1798             {
1799               /* Errata 21 - [D]DIV[U] after [D]MACC */
1800               if (strstr (tn, "div"))
1801                 {
1802                   min_nops = 1;
1803                 }
1804
1805               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1806               if (pn[0] == 'd' /* dmacc */
1807                   && (strncmp(tn, "dmult", 5) == 0
1808                       || strncmp(tn, "dmacc", 5) == 0))
1809                 {
1810                   min_nops = 1;
1811                 }
1812
1813               /* Errata 24 - MT{LO,HI} after [D]MACC */
1814               if (strcmp (tn, "mtlo") == 0
1815                   || strcmp (tn, "mthi") == 0)
1816                 {
1817                   min_nops = 1;
1818                 }
1819
1820             }
1821           else if (strncmp(pn, "dmult", 5) == 0
1822                    && (strncmp(tn, "dmult", 5) == 0
1823                        || strncmp(tn, "dmacc", 5) == 0))
1824             {
1825               /* Here is the rest of errata 23.  */
1826               min_nops = 1;
1827             }
1828           if (nops < min_nops)
1829             nops = min_nops;
1830         }
1831
1832       /* If we are being given a nop instruction, don't bother with
1833          one of the nops we would otherwise output.  This will only
1834          happen when a nop instruction is used with mips_optimize set
1835          to 0.  */
1836       if (nops > 0
1837           && ! mips_opts.noreorder
1838           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1839         --nops;
1840
1841       /* Now emit the right number of NOP instructions.  */
1842       if (nops > 0 && ! mips_opts.noreorder)
1843         {
1844           fragS *old_frag;
1845           unsigned long old_frag_offset;
1846           int i;
1847           struct insn_label_list *l;
1848
1849           old_frag = frag_now;
1850           old_frag_offset = frag_now_fix ();
1851
1852           for (i = 0; i < nops; i++)
1853             emit_nop ();
1854
1855           if (listing)
1856             {
1857               listing_prev_line ();
1858               /* We may be at the start of a variant frag.  In case we
1859                  are, make sure there is enough space for the frag
1860                  after the frags created by listing_prev_line.  The
1861                  argument to frag_grow here must be at least as large
1862                  as the argument to all other calls to frag_grow in
1863                  this file.  We don't have to worry about being in the
1864                  middle of a variant frag, because the variants insert
1865                  all needed nop instructions themselves.  */
1866               frag_grow (40);
1867             }
1868
1869           for (l = insn_labels; l != NULL; l = l->next)
1870             {
1871               valueT val;
1872
1873               assert (S_GET_SEGMENT (l->label) == now_seg);
1874               symbol_set_frag (l->label, frag_now);
1875               val = (valueT) frag_now_fix ();
1876               /* mips16 text labels are stored as odd.  */
1877               if (mips_opts.mips16)
1878                 ++val;
1879               S_SET_VALUE (l->label, val);
1880             }
1881
1882 #ifndef NO_ECOFF_DEBUGGING
1883           if (ECOFF_DEBUGGING)
1884             ecoff_fix_loc (old_frag, old_frag_offset);
1885 #endif
1886         }
1887       else if (prev_nop_frag != NULL)
1888         {
1889           /* We have a frag holding nops we may be able to remove.  If
1890              we don't need any nops, we can decrease the size of
1891              prev_nop_frag by the size of one instruction.  If we do
1892              need some nops, we count them in prev_nops_required.  */
1893           if (prev_nop_frag_since == 0)
1894             {
1895               if (nops == 0)
1896                 {
1897                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1898                   --prev_nop_frag_holds;
1899                 }
1900               else
1901                 prev_nop_frag_required += nops;
1902             }
1903           else
1904             {
1905               if (prev_prev_nop == 0)
1906                 {
1907                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1908                   --prev_nop_frag_holds;
1909                 }
1910               else
1911                 ++prev_nop_frag_required;
1912             }
1913
1914           if (prev_nop_frag_holds <= prev_nop_frag_required)
1915             prev_nop_frag = NULL;
1916
1917           ++prev_nop_frag_since;
1918
1919           /* Sanity check: by the time we reach the second instruction
1920              after prev_nop_frag, we should have used up all the nops
1921              one way or another.  */
1922           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1923         }
1924     }
1925
1926   if (place == NULL
1927       && address_expr
1928       && *reloc_type == BFD_RELOC_16_PCREL_S2
1929       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1930           || pinfo & INSN_COND_BRANCH_LIKELY)
1931       && mips_relax_branch
1932       /* Don't try branch relaxation within .set nomacro, or within
1933          .set noat if we use $at for PIC computations.  If it turns
1934          out that the branch was out-of-range, we'll get an error.  */
1935       && !mips_opts.warn_about_macros
1936       && !(mips_opts.noat && mips_pic != NO_PIC)
1937       && !mips_opts.mips16)
1938     {
1939       f = frag_var (rs_machine_dependent,
1940                     relaxed_branch_length
1941                     (NULL, NULL,
1942                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1943                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1944                     RELAX_BRANCH_ENCODE
1945                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
1946                      pinfo & INSN_COND_BRANCH_LIKELY,
1947                      pinfo & INSN_WRITE_GPR_31,
1948                      0),
1949                     address_expr->X_add_symbol,
1950                     address_expr->X_add_number,
1951                     0);
1952       *reloc_type = BFD_RELOC_UNUSED;
1953     }
1954   else if (*reloc_type > BFD_RELOC_UNUSED)
1955     {
1956       /* We need to set up a variant frag.  */
1957       assert (mips_opts.mips16 && address_expr != NULL);
1958       f = frag_var (rs_machine_dependent, 4, 0,
1959                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1960                                          mips16_small, mips16_ext,
1961                                          (prev_pinfo
1962                                           & INSN_UNCOND_BRANCH_DELAY),
1963                                          (*prev_insn_reloc_type
1964                                           == BFD_RELOC_MIPS16_JMP)),
1965                     make_expr_symbol (address_expr), 0, NULL);
1966     }
1967   else if (place != NULL)
1968     f = place;
1969   else if (mips_opts.mips16
1970            && ! ip->use_extend
1971            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1972     {
1973       /* Make sure there is enough room to swap this instruction with
1974          a following jump instruction.  */
1975       frag_grow (6);
1976       f = frag_more (2);
1977     }
1978   else
1979     {
1980       if (mips_opts.mips16
1981           && mips_opts.noreorder
1982           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1983         as_warn (_("extended instruction in delay slot"));
1984
1985       f = frag_more (4);
1986     }
1987
1988   fixp[0] = fixp[1] = fixp[2] = NULL;
1989   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1990     {
1991       if (address_expr->X_op == O_constant)
1992         {
1993           valueT tmp;
1994
1995           switch (*reloc_type)
1996             {
1997             case BFD_RELOC_32:
1998               ip->insn_opcode |= address_expr->X_add_number;
1999               break;
2000
2001             case BFD_RELOC_MIPS_HIGHEST:
2002               tmp = (address_expr->X_add_number
2003                      + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
2004               tmp >>= 16;
2005               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2006               break;
2007
2008             case BFD_RELOC_MIPS_HIGHER:
2009               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2010               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2011               break;
2012
2013             case BFD_RELOC_HI16_S:
2014               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2015                                   >> 16) & 0xffff;
2016               break;
2017
2018             case BFD_RELOC_HI16:
2019               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2020               break;
2021
2022             case BFD_RELOC_LO16:
2023             case BFD_RELOC_MIPS_GOT_DISP:
2024               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2025               break;
2026
2027             case BFD_RELOC_MIPS_JMP:
2028               if ((address_expr->X_add_number & 3) != 0)
2029                 as_bad (_("jump to misaligned address (0x%lx)"),
2030                         (unsigned long) address_expr->X_add_number);
2031               if (address_expr->X_add_number & ~0xfffffff)
2032                 as_bad (_("jump address range overflow (0x%lx)"),
2033                         (unsigned long) address_expr->X_add_number);
2034               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2035               break;
2036
2037             case BFD_RELOC_MIPS16_JMP:
2038               if ((address_expr->X_add_number & 3) != 0)
2039                 as_bad (_("jump to misaligned address (0x%lx)"),
2040                         (unsigned long) address_expr->X_add_number);
2041               if (address_expr->X_add_number & ~0xfffffff)
2042                 as_bad (_("jump address range overflow (0x%lx)"),
2043                         (unsigned long) address_expr->X_add_number);
2044               ip->insn_opcode |=
2045                 (((address_expr->X_add_number & 0x7c0000) << 3)
2046                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2047                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2048               break;
2049
2050             case BFD_RELOC_16_PCREL_S2:
2051               goto need_reloc;
2052
2053             default:
2054               internalError ();
2055             }
2056         }
2057       else
2058         {
2059         need_reloc:
2060           /* Don't generate a reloc if we are writing into a variant frag.  */
2061           if (place == NULL)
2062             {
2063               reloc_howto_type *howto;
2064               int i;
2065
2066               /* In a compound relocation, it is the final (outermost)
2067                  operator that determines the relocated field.  */
2068               for (i = 1; i < 3; i++)
2069                 if (reloc_type[i] == BFD_RELOC_UNUSED)
2070                   break;
2071
2072               howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2073               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2074                                      bfd_get_reloc_size(howto),
2075                                      address_expr,
2076                                      reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2077                                      reloc_type[0]);
2078
2079               /* These relocations can have an addend that won't fit in
2080                  4 octets for 64bit assembly.  */
2081               if (HAVE_64BIT_GPRS
2082                   && ! howto->partial_inplace
2083                   && (reloc_type[0] == BFD_RELOC_16
2084                       || reloc_type[0] == BFD_RELOC_32
2085                       || reloc_type[0] == BFD_RELOC_MIPS_JMP
2086                       || reloc_type[0] == BFD_RELOC_HI16_S
2087                       || reloc_type[0] == BFD_RELOC_LO16
2088                       || reloc_type[0] == BFD_RELOC_GPREL16
2089                       || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2090                       || reloc_type[0] == BFD_RELOC_GPREL32
2091                       || reloc_type[0] == BFD_RELOC_64
2092                       || reloc_type[0] == BFD_RELOC_CTOR
2093                       || reloc_type[0] == BFD_RELOC_MIPS_SUB
2094                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2095                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2096                       || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2097                       || reloc_type[0] == BFD_RELOC_MIPS_REL16
2098                       || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2099                 fixp[0]->fx_no_overflow = 1;
2100
2101               if (reloc_needs_lo_p (*reloc_type))
2102                 {
2103                   struct mips_hi_fixup *hi_fixup;
2104
2105                   /* Reuse the last entry if it already has a matching %lo.  */
2106                   hi_fixup = mips_hi_fixup_list;
2107                   if (hi_fixup == 0
2108                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2109                     {
2110                       hi_fixup = ((struct mips_hi_fixup *)
2111                                   xmalloc (sizeof (struct mips_hi_fixup)));
2112                       hi_fixup->next = mips_hi_fixup_list;
2113                       mips_hi_fixup_list = hi_fixup;
2114                     }
2115                   hi_fixup->fixp = fixp[0];
2116                   hi_fixup->seg = now_seg;
2117                 }
2118
2119               /* Add fixups for the second and third relocations, if given.
2120                  Note that the ABI allows the second relocation to be
2121                  against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
2122                  moment we only use RSS_UNDEF, but we could add support
2123                  for the others if it ever becomes necessary.  */
2124               for (i = 1; i < 3; i++)
2125                 if (reloc_type[i] != BFD_RELOC_UNUSED)
2126                   {
2127                     address_expr->X_op = O_absent;
2128                     address_expr->X_add_symbol = 0;
2129                     address_expr->X_add_number = 0;
2130
2131                     fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2132                                            fixp[0]->fx_size, address_expr,
2133                                            FALSE, reloc_type[i]);
2134                   }
2135             }
2136         }
2137     }
2138
2139   if (! mips_opts.mips16)
2140     {
2141       md_number_to_chars (f, ip->insn_opcode, 4);
2142 #ifdef OBJ_ELF
2143       dwarf2_emit_insn (4);
2144 #endif
2145     }
2146   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2147     {
2148       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2149       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2150 #ifdef OBJ_ELF
2151       dwarf2_emit_insn (4);
2152 #endif
2153     }
2154   else
2155     {
2156       if (ip->use_extend)
2157         {
2158           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2159           f += 2;
2160         }
2161       md_number_to_chars (f, ip->insn_opcode, 2);
2162 #ifdef OBJ_ELF
2163       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2164 #endif
2165     }
2166
2167   /* Update the register mask information.  */
2168   if (! mips_opts.mips16)
2169     {
2170       if (pinfo & INSN_WRITE_GPR_D)
2171         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2172       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2173         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2174       if (pinfo & INSN_READ_GPR_S)
2175         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2176       if (pinfo & INSN_WRITE_GPR_31)
2177         mips_gprmask |= 1 << RA;
2178       if (pinfo & INSN_WRITE_FPR_D)
2179         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2180       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2181         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2182       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2183         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2184       if ((pinfo & INSN_READ_FPR_R) != 0)
2185         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2186       if (pinfo & INSN_COP)
2187         {
2188           /* We don't keep enough information to sort these cases out.
2189              The itbl support does keep this information however, although
2190              we currently don't support itbl fprmats as part of the cop
2191              instruction.  May want to add this support in the future.  */
2192         }
2193       /* Never set the bit for $0, which is always zero.  */
2194       mips_gprmask &= ~1 << 0;
2195     }
2196   else
2197     {
2198       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2199         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2200                               & MIPS16OP_MASK_RX);
2201       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2202         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2203                               & MIPS16OP_MASK_RY);
2204       if (pinfo & MIPS16_INSN_WRITE_Z)
2205         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2206                               & MIPS16OP_MASK_RZ);
2207       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2208         mips_gprmask |= 1 << TREG;
2209       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2210         mips_gprmask |= 1 << SP;
2211       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2212         mips_gprmask |= 1 << RA;
2213       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2214         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2215       if (pinfo & MIPS16_INSN_READ_Z)
2216         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2217                               & MIPS16OP_MASK_MOVE32Z);
2218       if (pinfo & MIPS16_INSN_READ_GPR_X)
2219         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2220                               & MIPS16OP_MASK_REGR32);
2221     }
2222
2223   if (place == NULL && ! mips_opts.noreorder)
2224     {
2225       /* Filling the branch delay slot is more complex.  We try to
2226          switch the branch with the previous instruction, which we can
2227          do if the previous instruction does not set up a condition
2228          that the branch tests and if the branch is not itself the
2229          target of any branch.  */
2230       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2231           || (pinfo & INSN_COND_BRANCH_DELAY))
2232         {
2233           if (mips_optimize < 2
2234               /* If we have seen .set volatile or .set nomove, don't
2235                  optimize.  */
2236               || mips_opts.nomove != 0
2237               /* If we had to emit any NOP instructions, then we
2238                  already know we can not swap.  */
2239               || nops != 0
2240               /* If we don't even know the previous insn, we can not
2241                  swap.  */
2242               || ! prev_insn_valid
2243               /* If the previous insn is already in a branch delay
2244                  slot, then we can not swap.  */
2245               || prev_insn_is_delay_slot
2246               /* If the previous previous insn was in a .set
2247                  noreorder, we can't swap.  Actually, the MIPS
2248                  assembler will swap in this situation.  However, gcc
2249                  configured -with-gnu-as will generate code like
2250                    .set noreorder
2251                    lw   $4,XXX
2252                    .set reorder
2253                    INSN
2254                    bne  $4,$0,foo
2255                  in which we can not swap the bne and INSN.  If gcc is
2256                  not configured -with-gnu-as, it does not output the
2257                  .set pseudo-ops.  We don't have to check
2258                  prev_insn_unreordered, because prev_insn_valid will
2259                  be 0 in that case.  We don't want to use
2260                  prev_prev_insn_valid, because we do want to be able
2261                  to swap at the start of a function.  */
2262               || prev_prev_insn_unreordered
2263               /* If the branch is itself the target of a branch, we
2264                  can not swap.  We cheat on this; all we check for is
2265                  whether there is a label on this instruction.  If
2266                  there are any branches to anything other than a
2267                  label, users must use .set noreorder.  */
2268               || insn_labels != NULL
2269               /* If the previous instruction is in a variant frag, we
2270                  can not do the swap.  This does not apply to the
2271                  mips16, which uses variant frags for different
2272                  purposes.  */
2273               || (! mips_opts.mips16
2274                   && prev_insn_frag->fr_type == rs_machine_dependent)
2275               /* If the branch reads the condition codes, we don't
2276                  even try to swap, because in the sequence
2277                    ctc1 $X,$31
2278                    INSN
2279                    INSN
2280                    bc1t LABEL
2281                  we can not swap, and I don't feel like handling that
2282                  case.  */
2283               || (! mips_opts.mips16
2284                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2285                   && (pinfo & INSN_READ_COND_CODE))
2286               /* We can not swap with an instruction that requires a
2287                  delay slot, becase the target of the branch might
2288                  interfere with that instruction.  */
2289               || (! mips_opts.mips16
2290                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2291                   && (prev_pinfo
2292               /* Itbl support may require additional care here.  */
2293                       & (INSN_LOAD_COPROC_DELAY
2294                          | INSN_COPROC_MOVE_DELAY
2295                          | INSN_WRITE_COND_CODE)))
2296               || (! (hilo_interlocks
2297                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2298                   && (prev_pinfo
2299                       & (INSN_READ_LO
2300                          | INSN_READ_HI)))
2301               || (! mips_opts.mips16
2302                   && ! gpr_interlocks
2303                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2304               || (! mips_opts.mips16
2305                   && mips_opts.isa == ISA_MIPS1
2306                   /* Itbl support may require additional care here.  */
2307                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2308               /* We can not swap with a branch instruction.  */
2309               || (prev_pinfo
2310                   & (INSN_UNCOND_BRANCH_DELAY
2311                      | INSN_COND_BRANCH_DELAY
2312                      | INSN_COND_BRANCH_LIKELY))
2313               /* We do not swap with a trap instruction, since it
2314                  complicates trap handlers to have the trap
2315                  instruction be in a delay slot.  */
2316               || (prev_pinfo & INSN_TRAP)
2317               /* If the branch reads a register that the previous
2318                  instruction sets, we can not swap.  */
2319               || (! mips_opts.mips16
2320                   && (prev_pinfo & INSN_WRITE_GPR_T)
2321                   && insn_uses_reg (ip,
2322                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2323                                      & OP_MASK_RT),
2324                                     MIPS_GR_REG))
2325               || (! mips_opts.mips16
2326                   && (prev_pinfo & INSN_WRITE_GPR_D)
2327                   && insn_uses_reg (ip,
2328                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2329                                      & OP_MASK_RD),
2330                                     MIPS_GR_REG))
2331               || (mips_opts.mips16
2332                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2333                        && insn_uses_reg (ip,
2334                                          ((prev_insn.insn_opcode
2335                                            >> MIPS16OP_SH_RX)
2336                                           & MIPS16OP_MASK_RX),
2337                                          MIPS16_REG))
2338                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2339                           && insn_uses_reg (ip,
2340                                             ((prev_insn.insn_opcode
2341                                               >> MIPS16OP_SH_RY)
2342                                              & MIPS16OP_MASK_RY),
2343                                             MIPS16_REG))
2344                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2345                           && insn_uses_reg (ip,
2346                                             ((prev_insn.insn_opcode
2347                                               >> MIPS16OP_SH_RZ)
2348                                              & MIPS16OP_MASK_RZ),
2349                                             MIPS16_REG))
2350                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2351                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2352                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2353                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2354                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2355                           && insn_uses_reg (ip,
2356                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2357                                                                      insn_opcode),
2358                                             MIPS_GR_REG))))
2359               /* If the branch writes a register that the previous
2360                  instruction sets, we can not swap (we know that
2361                  branches write only to RD or to $31).  */
2362               || (! mips_opts.mips16
2363                   && (prev_pinfo & INSN_WRITE_GPR_T)
2364                   && (((pinfo & INSN_WRITE_GPR_D)
2365                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2366                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2367                       || ((pinfo & INSN_WRITE_GPR_31)
2368                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2369                                & OP_MASK_RT)
2370                               == RA))))
2371               || (! mips_opts.mips16
2372                   && (prev_pinfo & INSN_WRITE_GPR_D)
2373                   && (((pinfo & INSN_WRITE_GPR_D)
2374                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2375                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2376                       || ((pinfo & INSN_WRITE_GPR_31)
2377                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2378                                & OP_MASK_RD)
2379                               == RA))))
2380               || (mips_opts.mips16
2381                   && (pinfo & MIPS16_INSN_WRITE_31)
2382                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2383                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2384                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2385                               == RA))))
2386               /* If the branch writes a register that the previous
2387                  instruction reads, we can not swap (we know that
2388                  branches only write to RD or to $31).  */
2389               || (! mips_opts.mips16
2390                   && (pinfo & INSN_WRITE_GPR_D)
2391                   && insn_uses_reg (&prev_insn,
2392                                     ((ip->insn_opcode >> OP_SH_RD)
2393                                      & OP_MASK_RD),
2394                                     MIPS_GR_REG))
2395               || (! mips_opts.mips16
2396                   && (pinfo & INSN_WRITE_GPR_31)
2397                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2398               || (mips_opts.mips16
2399                   && (pinfo & MIPS16_INSN_WRITE_31)
2400                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2401               /* If we are generating embedded PIC code, the branch
2402                  might be expanded into a sequence which uses $at, so
2403                  we can't swap with an instruction which reads it.  */
2404               || (mips_pic == EMBEDDED_PIC
2405                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2406               /* If the previous previous instruction has a load
2407                  delay, and sets a register that the branch reads, we
2408                  can not swap.  */
2409               || (! mips_opts.mips16
2410                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2411               /* Itbl support may require additional care here.  */
2412                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2413                       || (! gpr_interlocks
2414                           && (prev_prev_insn.insn_mo->pinfo
2415                               & INSN_LOAD_MEMORY_DELAY)))
2416                   && insn_uses_reg (ip,
2417                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2418                                      & OP_MASK_RT),
2419                                     MIPS_GR_REG))
2420               /* If one instruction sets a condition code and the
2421                  other one uses a condition code, we can not swap.  */
2422               || ((pinfo & INSN_READ_COND_CODE)
2423                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2424               || ((pinfo & INSN_WRITE_COND_CODE)
2425                   && (prev_pinfo & INSN_READ_COND_CODE))
2426               /* If the previous instruction uses the PC, we can not
2427                  swap.  */
2428               || (mips_opts.mips16
2429                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2430               /* If the previous instruction was extended, we can not
2431                  swap.  */
2432               || (mips_opts.mips16 && prev_insn_extended)
2433               /* If the previous instruction had a fixup in mips16
2434                  mode, we can not swap.  This normally means that the
2435                  previous instruction was a 4 byte branch anyhow.  */
2436               || (mips_opts.mips16 && prev_insn_fixp[0])
2437               /* If the previous instruction is a sync, sync.l, or
2438                  sync.p, we can not swap.  */
2439               || (prev_pinfo & INSN_SYNC))
2440             {
2441               /* We could do even better for unconditional branches to
2442                  portions of this object file; we could pick up the
2443                  instruction at the destination, put it in the delay
2444                  slot, and bump the destination address.  */
2445               emit_nop ();
2446               /* Update the previous insn information.  */
2447               prev_prev_insn = *ip;
2448               prev_insn.insn_mo = &dummy_opcode;
2449             }
2450           else
2451             {
2452               /* It looks like we can actually do the swap.  */
2453               if (! mips_opts.mips16)
2454                 {
2455                   char *prev_f;
2456                   char temp[4];
2457
2458                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2459                   memcpy (temp, prev_f, 4);
2460                   memcpy (prev_f, f, 4);
2461                   memcpy (f, temp, 4);
2462                   if (prev_insn_fixp[0])
2463                     {
2464                       prev_insn_fixp[0]->fx_frag = frag_now;
2465                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2466                     }
2467                   if (prev_insn_fixp[1])
2468                     {
2469                       prev_insn_fixp[1]->fx_frag = frag_now;
2470                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2471                     }
2472                   if (prev_insn_fixp[2])
2473                     {
2474                       prev_insn_fixp[2]->fx_frag = frag_now;
2475                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2476                     }
2477                   if (prev_insn_fixp[0] && HAVE_NEWABI
2478                       && prev_insn_frag != frag_now
2479                       && (prev_insn_fixp[0]->fx_r_type
2480                           == BFD_RELOC_MIPS_GOT_DISP
2481                           || (prev_insn_fixp[0]->fx_r_type
2482                               == BFD_RELOC_MIPS_CALL16)))
2483                     {
2484                       /* To avoid confusion in tc_gen_reloc, we must
2485                          ensure that this does not become a variant
2486                          frag.  */
2487                       force_new_frag = TRUE;
2488                     }
2489                   if (fixp[0])
2490                     {
2491                       fixp[0]->fx_frag = prev_insn_frag;
2492                       fixp[0]->fx_where = prev_insn_where;
2493                     }
2494                   if (fixp[1])
2495                     {
2496                       fixp[1]->fx_frag = prev_insn_frag;
2497                       fixp[1]->fx_where = prev_insn_where;
2498                     }
2499                   if (fixp[2])
2500                     {
2501                       fixp[2]->fx_frag = prev_insn_frag;
2502                       fixp[2]->fx_where = prev_insn_where;
2503                     }
2504                 }
2505               else
2506                 {
2507                   char *prev_f;
2508                   char temp[2];
2509
2510                   assert (prev_insn_fixp[0] == NULL);
2511                   assert (prev_insn_fixp[1] == NULL);
2512                   assert (prev_insn_fixp[2] == NULL);
2513                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2514                   memcpy (temp, prev_f, 2);
2515                   memcpy (prev_f, f, 2);
2516                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2517                     {
2518                       assert (*reloc_type == BFD_RELOC_UNUSED);
2519                       memcpy (f, temp, 2);
2520                     }
2521                   else
2522                     {
2523                       memcpy (f, f + 2, 2);
2524                       memcpy (f + 2, temp, 2);
2525                     }
2526                   if (fixp[0])
2527                     {
2528                       fixp[0]->fx_frag = prev_insn_frag;
2529                       fixp[0]->fx_where = prev_insn_where;
2530                     }
2531                   if (fixp[1])
2532                     {
2533                       fixp[1]->fx_frag = prev_insn_frag;
2534                       fixp[1]->fx_where = prev_insn_where;
2535                     }
2536                   if (fixp[2])
2537                     {
2538                       fixp[2]->fx_frag = prev_insn_frag;
2539                       fixp[2]->fx_where = prev_insn_where;
2540                     }
2541                 }
2542
2543               /* Update the previous insn information; leave prev_insn
2544                  unchanged.  */
2545               prev_prev_insn = *ip;
2546             }
2547           prev_insn_is_delay_slot = 1;
2548
2549           /* If that was an unconditional branch, forget the previous
2550              insn information.  */
2551           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2552             {
2553               prev_prev_insn.insn_mo = &dummy_opcode;
2554               prev_insn.insn_mo = &dummy_opcode;
2555             }
2556
2557           prev_insn_fixp[0] = NULL;
2558           prev_insn_fixp[1] = NULL;
2559           prev_insn_fixp[2] = NULL;
2560           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2561           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2562           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2563           prev_insn_extended = 0;
2564         }
2565       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2566         {
2567           /* We don't yet optimize a branch likely.  What we should do
2568              is look at the target, copy the instruction found there
2569              into the delay slot, and increment the branch to jump to
2570              the next instruction.  */
2571           emit_nop ();
2572           /* Update the previous insn information.  */
2573           prev_prev_insn = *ip;
2574           prev_insn.insn_mo = &dummy_opcode;
2575           prev_insn_fixp[0] = NULL;
2576           prev_insn_fixp[1] = NULL;
2577           prev_insn_fixp[2] = NULL;
2578           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2579           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2580           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2581           prev_insn_extended = 0;
2582         }
2583       else
2584         {
2585           /* Update the previous insn information.  */
2586           if (nops > 0)
2587             prev_prev_insn.insn_mo = &dummy_opcode;
2588           else
2589             prev_prev_insn = prev_insn;
2590           prev_insn = *ip;
2591
2592           /* Any time we see a branch, we always fill the delay slot
2593              immediately; since this insn is not a branch, we know it
2594              is not in a delay slot.  */
2595           prev_insn_is_delay_slot = 0;
2596
2597           prev_insn_fixp[0] = fixp[0];
2598           prev_insn_fixp[1] = fixp[1];
2599           prev_insn_fixp[2] = fixp[2];
2600           prev_insn_reloc_type[0] = reloc_type[0];
2601           prev_insn_reloc_type[1] = reloc_type[1];
2602           prev_insn_reloc_type[2] = reloc_type[2];
2603           if (mips_opts.mips16)
2604             prev_insn_extended = (ip->use_extend
2605                                   || *reloc_type > BFD_RELOC_UNUSED);
2606         }
2607
2608       prev_prev_insn_unreordered = prev_insn_unreordered;
2609       prev_insn_unreordered = 0;
2610       prev_insn_frag = frag_now;
2611       prev_insn_where = f - frag_now->fr_literal;
2612       prev_insn_valid = 1;
2613     }
2614   else if (place == NULL)
2615     {
2616       /* We need to record a bit of information even when we are not
2617          reordering, in order to determine the base address for mips16
2618          PC relative relocs.  */
2619       prev_prev_insn = prev_insn;
2620       prev_insn = *ip;
2621       prev_insn_reloc_type[0] = reloc_type[0];
2622       prev_insn_reloc_type[1] = reloc_type[1];
2623       prev_insn_reloc_type[2] = reloc_type[2];
2624       prev_prev_insn_unreordered = prev_insn_unreordered;
2625       prev_insn_unreordered = 1;
2626     }
2627
2628   /* We just output an insn, so the next one doesn't have a label.  */
2629   mips_clear_insn_labels ();
2630
2631   /* We must ensure that the frag to which an instruction that was
2632      moved from a non-variant frag doesn't become a variant frag,
2633      otherwise tc_gen_reloc may get confused.  */
2634   if (force_new_frag)
2635     {
2636       frag_wane (frag_now);
2637       frag_new (0);
2638     }
2639 }
2640
2641 /* This function forgets that there was any previous instruction or
2642    label.  If PRESERVE is non-zero, it remembers enough information to
2643    know whether nops are needed before a noreorder section.  */
2644
2645 static void
2646 mips_no_prev_insn (int preserve)
2647 {
2648   if (! preserve)
2649     {
2650       prev_insn.insn_mo = &dummy_opcode;
2651       prev_prev_insn.insn_mo = &dummy_opcode;
2652       prev_nop_frag = NULL;
2653       prev_nop_frag_holds = 0;
2654       prev_nop_frag_required = 0;
2655       prev_nop_frag_since = 0;
2656     }
2657   prev_insn_valid = 0;
2658   prev_insn_is_delay_slot = 0;
2659   prev_insn_unreordered = 0;
2660   prev_insn_extended = 0;
2661   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2662   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2663   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2664   prev_prev_insn_unreordered = 0;
2665   mips_clear_insn_labels ();
2666 }
2667
2668 /* This function must be called whenever we turn on noreorder or emit
2669    something other than instructions.  It inserts any NOPS which might
2670    be needed by the previous instruction, and clears the information
2671    kept for the previous instructions.  The INSNS parameter is true if
2672    instructions are to follow.  */
2673
2674 static void
2675 mips_emit_delays (bfd_boolean insns)
2676 {
2677   if (! mips_opts.noreorder)
2678     {
2679       int nops;
2680
2681       nops = 0;
2682       if ((! mips_opts.mips16
2683            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2684            && (! cop_interlocks
2685                && (prev_insn.insn_mo->pinfo
2686                    & (INSN_LOAD_COPROC_DELAY
2687                       | INSN_COPROC_MOVE_DELAY
2688                       | INSN_WRITE_COND_CODE))))
2689           || (! hilo_interlocks
2690               && (prev_insn.insn_mo->pinfo
2691                   & (INSN_READ_LO
2692                      | INSN_READ_HI)))
2693           || (! mips_opts.mips16
2694               && ! gpr_interlocks
2695               && (prev_insn.insn_mo->pinfo
2696                   & INSN_LOAD_MEMORY_DELAY))
2697           || (! mips_opts.mips16
2698               && mips_opts.isa == ISA_MIPS1
2699               && (prev_insn.insn_mo->pinfo
2700                   & INSN_COPROC_MEMORY_DELAY)))
2701         {
2702           /* Itbl support may require additional care here.  */
2703           ++nops;
2704           if ((! mips_opts.mips16
2705                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2706                && (! cop_interlocks
2707                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2708               || (! hilo_interlocks
2709                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2710                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2711             ++nops;
2712
2713           if (prev_insn_unreordered)
2714             nops = 0;
2715         }
2716       else if ((! mips_opts.mips16
2717                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2718                 && (! cop_interlocks
2719                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2720                || (! hilo_interlocks
2721                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2722                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2723         {
2724           /* Itbl support may require additional care here.  */
2725           if (! prev_prev_insn_unreordered)
2726             ++nops;
2727         }
2728
2729       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2730         {
2731           int min_nops = 0;
2732           const char *pn = prev_insn.insn_mo->name;
2733           if (strncmp(pn, "macc", 4) == 0
2734               || strncmp(pn, "dmacc", 5) == 0
2735               || strncmp(pn, "dmult", 5) == 0)
2736             {
2737               min_nops = 1;
2738             }
2739           if (nops < min_nops)
2740             nops = min_nops;
2741         }
2742
2743       if (nops > 0)
2744         {
2745           struct insn_label_list *l;
2746
2747           if (insns)
2748             {
2749               /* Record the frag which holds the nop instructions, so
2750                  that we can remove them if we don't need them.  */
2751               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2752               prev_nop_frag = frag_now;
2753               prev_nop_frag_holds = nops;
2754               prev_nop_frag_required = 0;
2755               prev_nop_frag_since = 0;
2756             }
2757
2758           for (; nops > 0; --nops)
2759             emit_nop ();
2760
2761           if (insns)
2762             {
2763               /* Move on to a new frag, so that it is safe to simply
2764                  decrease the size of prev_nop_frag.  */
2765               frag_wane (frag_now);
2766               frag_new (0);
2767             }
2768
2769           for (l = insn_labels; l != NULL; l = l->next)
2770             {
2771               valueT val;
2772
2773               assert (S_GET_SEGMENT (l->label) == now_seg);
2774               symbol_set_frag (l->label, frag_now);
2775               val = (valueT) frag_now_fix ();
2776               /* mips16 text labels are stored as odd.  */
2777               if (mips_opts.mips16)
2778                 ++val;
2779               S_SET_VALUE (l->label, val);
2780             }
2781         }
2782     }
2783
2784   /* Mark instruction labels in mips16 mode.  */
2785   if (insns)
2786     mips16_mark_labels ();
2787
2788   mips_no_prev_insn (insns);
2789 }
2790
2791 /* Build an instruction created by a macro expansion.  This is passed
2792    a pointer to the count of instructions created so far, an
2793    expression, the name of the instruction to build, an operand format
2794    string, and corresponding arguments.  */
2795
2796 static void
2797 macro_build (char *place, int *counter, expressionS *ep, const char *name,
2798              const char *fmt, ...)
2799 {
2800   struct mips_cl_insn insn;
2801   bfd_reloc_code_real_type r[3];
2802   va_list args;
2803
2804   va_start (args, fmt);
2805
2806   /*
2807    * If the macro is about to expand into a second instruction,
2808    * print a warning if needed. We need to pass ip as a parameter
2809    * to generate a better warning message here...
2810    */
2811   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2812     as_warn (_("Macro instruction expanded into multiple instructions"));
2813
2814   /*
2815    * If the macro is about to expand into a second instruction,
2816    * and it is in a delay slot, print a warning.
2817    */
2818   if (place == NULL
2819       && *counter == 1
2820       && mips_opts.noreorder
2821       && (prev_prev_insn.insn_mo->pinfo
2822           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2823              | INSN_COND_BRANCH_LIKELY)) != 0)
2824     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2825
2826   if (place == NULL)
2827     ++*counter;         /* bump instruction counter */
2828
2829   if (mips_opts.mips16)
2830     {
2831       mips16_macro_build (place, counter, ep, name, fmt, args);
2832       va_end (args);
2833       return;
2834     }
2835
2836   r[0] = BFD_RELOC_UNUSED;
2837   r[1] = BFD_RELOC_UNUSED;
2838   r[2] = BFD_RELOC_UNUSED;
2839   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2840   assert (insn.insn_mo);
2841   assert (strcmp (name, insn.insn_mo->name) == 0);
2842
2843   /* Search until we get a match for NAME.  */
2844   while (1)
2845     {
2846       /* It is assumed here that macros will never generate
2847          MDMX or MIPS-3D instructions.  */
2848       if (strcmp (fmt, insn.insn_mo->args) == 0
2849           && insn.insn_mo->pinfo != INSN_MACRO
2850           && OPCODE_IS_MEMBER (insn.insn_mo,
2851                                (mips_opts.isa
2852                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2853                                mips_opts.arch)
2854           && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2855         break;
2856
2857       ++insn.insn_mo;
2858       assert (insn.insn_mo->name);
2859       assert (strcmp (name, insn.insn_mo->name) == 0);
2860     }
2861
2862   insn.insn_opcode = insn.insn_mo->match;
2863   for (;;)
2864     {
2865       switch (*fmt++)
2866         {
2867         case '\0':
2868           break;
2869
2870         case ',':
2871         case '(':
2872         case ')':
2873           continue;
2874
2875         case 't':
2876         case 'w':
2877         case 'E':
2878           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2879           continue;
2880
2881         case 'c':
2882           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2883           continue;
2884
2885         case 'T':
2886         case 'W':
2887           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2888           continue;
2889
2890         case 'd':
2891         case 'G':
2892         case 'K':
2893           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2894           continue;
2895
2896         case 'U':
2897           {
2898             int tmp = va_arg (args, int);
2899
2900             insn.insn_opcode |= tmp << OP_SH_RT;
2901             insn.insn_opcode |= tmp << OP_SH_RD;
2902             continue;
2903           }
2904
2905         case 'V':
2906         case 'S':
2907           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2908           continue;
2909
2910         case 'z':
2911           continue;
2912
2913         case '<':
2914           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2915           continue;
2916
2917         case 'D':
2918           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2919           continue;
2920
2921         case 'B':
2922           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2923           continue;
2924
2925         case 'J':
2926           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2927           continue;
2928
2929         case 'q':
2930           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2931           continue;
2932
2933         case 'b':
2934         case 's':
2935         case 'r':
2936         case 'v':
2937           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2938           continue;
2939
2940         case 'i':
2941         case 'j':
2942         case 'o':
2943           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2944           assert (*r == BFD_RELOC_GPREL16
2945                   || *r == BFD_RELOC_MIPS_LITERAL
2946                   || *r == BFD_RELOC_MIPS_HIGHER
2947                   || *r == BFD_RELOC_HI16_S
2948                   || *r == BFD_RELOC_LO16
2949                   || *r == BFD_RELOC_MIPS_GOT16
2950                   || *r == BFD_RELOC_MIPS_CALL16
2951                   || *r == BFD_RELOC_MIPS_GOT_DISP
2952                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2953                   || *r == BFD_RELOC_MIPS_GOT_OFST
2954                   || *r == BFD_RELOC_MIPS_GOT_LO16
2955                   || *r == BFD_RELOC_MIPS_CALL_LO16
2956                   || (ep->X_op == O_subtract
2957                       && *r == BFD_RELOC_PCREL_LO16));
2958           continue;
2959
2960         case 'u':
2961           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2962           assert (ep != NULL
2963                   && (ep->X_op == O_constant
2964                       || (ep->X_op == O_symbol
2965                           && (*r == BFD_RELOC_MIPS_HIGHEST
2966                               || *r == BFD_RELOC_HI16_S
2967                               || *r == BFD_RELOC_HI16
2968                               || *r == BFD_RELOC_GPREL16
2969                               || *r == BFD_RELOC_MIPS_GOT_HI16
2970                               || *r == BFD_RELOC_MIPS_CALL_HI16))
2971                       || (ep->X_op == O_subtract
2972                           && *r == BFD_RELOC_PCREL_HI16_S)));
2973           continue;
2974
2975         case 'p':
2976           assert (ep != NULL);
2977           /*
2978            * This allows macro() to pass an immediate expression for
2979            * creating short branches without creating a symbol.
2980            * Note that the expression still might come from the assembly
2981            * input, in which case the value is not checked for range nor
2982            * is a relocation entry generated (yuck).
2983            */
2984           if (ep->X_op == O_constant)
2985             {
2986               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2987               ep = NULL;
2988             }
2989           else
2990             *r = BFD_RELOC_16_PCREL_S2;
2991           continue;
2992
2993         case 'a':
2994           assert (ep != NULL);
2995           *r = BFD_RELOC_MIPS_JMP;
2996           continue;
2997
2998         case 'C':
2999           insn.insn_opcode |= va_arg (args, unsigned long);
3000           continue;
3001
3002         default:
3003           internalError ();
3004         }
3005       break;
3006     }
3007   va_end (args);
3008   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3009
3010   append_insn (place, &insn, ep, r);
3011 }
3012
3013 static void
3014 mips16_macro_build (char *place, int *counter ATTRIBUTE_UNUSED,
3015                     expressionS *ep, const char *name, const char *fmt,
3016                     va_list args)
3017 {
3018   struct mips_cl_insn insn;
3019   bfd_reloc_code_real_type r[3]
3020     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3021
3022   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3023   assert (insn.insn_mo);
3024   assert (strcmp (name, insn.insn_mo->name) == 0);
3025
3026   while (strcmp (fmt, insn.insn_mo->args) != 0
3027          || insn.insn_mo->pinfo == INSN_MACRO)
3028     {
3029       ++insn.insn_mo;
3030       assert (insn.insn_mo->name);
3031       assert (strcmp (name, insn.insn_mo->name) == 0);
3032     }
3033
3034   insn.insn_opcode = insn.insn_mo->match;
3035   insn.use_extend = FALSE;
3036
3037   for (;;)
3038     {
3039       int c;
3040
3041       c = *fmt++;
3042       switch (c)
3043         {
3044         case '\0':
3045           break;
3046
3047         case ',':
3048         case '(':
3049         case ')':
3050           continue;
3051
3052         case 'y':
3053         case 'w':
3054           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3055           continue;
3056
3057         case 'x':
3058         case 'v':
3059           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3060           continue;
3061
3062         case 'z':
3063           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3064           continue;
3065
3066         case 'Z':
3067           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3068           continue;
3069
3070         case '0':
3071         case 'S':
3072         case 'P':
3073         case 'R':
3074           continue;
3075
3076         case 'X':
3077           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3078           continue;
3079
3080         case 'Y':
3081           {
3082             int regno;
3083
3084             regno = va_arg (args, int);
3085             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3086             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3087           }
3088           continue;
3089
3090         case '<':
3091         case '>':
3092         case '4':
3093         case '5':
3094         case 'H':
3095         case 'W':
3096         case 'D':
3097         case 'j':
3098         case '8':
3099         case 'V':
3100         case 'C':
3101         case 'U':
3102         case 'k':
3103         case 'K':
3104         case 'p':
3105         case 'q':
3106           {
3107             assert (ep != NULL);
3108
3109             if (ep->X_op != O_constant)
3110               *r = (int) BFD_RELOC_UNUSED + c;
3111             else
3112               {
3113                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3114                               FALSE, &insn.insn_opcode, &insn.use_extend,
3115                               &insn.extend);
3116                 ep = NULL;
3117                 *r = BFD_RELOC_UNUSED;
3118               }
3119           }
3120           continue;
3121
3122         case '6':
3123           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3124           continue;
3125         }
3126
3127       break;
3128     }
3129
3130   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3131
3132   append_insn (place, &insn, ep, r);
3133 }
3134
3135 /*
3136  * Generate a "jalr" instruction with a relocation hint to the called
3137  * function.  This occurs in NewABI PIC code.
3138  */
3139 static void
3140 macro_build_jalr (int icnt, expressionS *ep)
3141 {
3142   char *f = NULL;
3143
3144   if (HAVE_NEWABI)
3145     {
3146       frag_grow (4);
3147       f = frag_more (0);
3148     }
3149   macro_build (NULL, &icnt, NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3150   if (HAVE_NEWABI)
3151     fix_new_exp (frag_now, f - frag_now->fr_literal,
3152                  4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3153 }
3154
3155 /*
3156  * Generate a "lui" instruction.
3157  */
3158 static void
3159 macro_build_lui (char *place, int *counter, expressionS *ep, int regnum)
3160 {
3161   expressionS high_expr;
3162   struct mips_cl_insn insn;
3163   bfd_reloc_code_real_type r[3]
3164     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3165   const char *name = "lui";
3166   const char *fmt = "t,u";
3167
3168   assert (! mips_opts.mips16);
3169
3170   if (place == NULL)
3171     high_expr = *ep;
3172   else
3173     {
3174       high_expr.X_op = O_constant;
3175       high_expr.X_add_number = ep->X_add_number;
3176     }
3177
3178   if (high_expr.X_op == O_constant)
3179     {
3180       /* we can compute the instruction now without a relocation entry */
3181       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3182                                 >> 16) & 0xffff;
3183       *r = BFD_RELOC_UNUSED;
3184     }
3185   else
3186     {
3187       assert (ep->X_op == O_symbol);
3188       /* _gp_disp is a special case, used from s_cpload.  */
3189       assert (mips_pic == NO_PIC
3190               || (! HAVE_NEWABI
3191                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3192       *r = BFD_RELOC_HI16_S;
3193     }
3194
3195   /*
3196    * If the macro is about to expand into a second instruction,
3197    * print a warning if needed. We need to pass ip as a parameter
3198    * to generate a better warning message here...
3199    */
3200   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3201     as_warn (_("Macro instruction expanded into multiple instructions"));
3202
3203   if (place == NULL)
3204     ++*counter;         /* bump instruction counter */
3205
3206   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3207   assert (insn.insn_mo);
3208   assert (strcmp (name, insn.insn_mo->name) == 0);
3209   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3210
3211   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3212   if (*r == BFD_RELOC_UNUSED)
3213     {
3214       insn.insn_opcode |= high_expr.X_add_number;
3215       append_insn (place, &insn, NULL, r);
3216     }
3217   else
3218     append_insn (place, &insn, &high_expr, r);
3219 }
3220
3221 /* Generate a sequence of instructions to do a load or store from a constant
3222    offset off of a base register (breg) into/from a target register (treg),
3223    using AT if necessary.  */
3224 static void
3225 macro_build_ldst_constoffset (char *place, int *counter, expressionS *ep,
3226                               const char *op, int treg, int breg, int dbl)
3227 {
3228   assert (ep->X_op == O_constant);
3229
3230   /* Sign-extending 32-bit constants makes their handling easier.  */
3231   if (! dbl)
3232     ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3233                         - 0x80000000);
3234
3235   /* Right now, this routine can only handle signed 32-bit contants.  */
3236   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3237     as_warn (_("operand overflow"));
3238
3239   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3240     {
3241       /* Signed 16-bit offset will fit in the op.  Easy!  */
3242       macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3243                    breg);
3244     }
3245   else
3246     {
3247       /* 32-bit offset, need multiple instructions and AT, like:
3248            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3249            addu     $tempreg,$tempreg,$breg
3250            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3251          to handle the complete offset.  */
3252       macro_build_lui (place, counter, ep, AT);
3253       if (place != NULL)
3254         place += 4;
3255       macro_build (place, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT,
3256                    breg);
3257       if (place != NULL)
3258         place += 4;
3259       macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3260                    AT);
3261
3262       if (mips_opts.noat)
3263         as_warn (_("Macro used $at after \".set noat\""));
3264     }
3265 }
3266
3267 /*                      set_at()
3268  * Generates code to set the $at register to true (one)
3269  * if reg is less than the immediate expression.
3270  */
3271 static void
3272 set_at (int *counter, int reg, int unsignedp)
3273 {
3274   if (imm_expr.X_op == O_constant
3275       && imm_expr.X_add_number >= -0x8000
3276       && imm_expr.X_add_number < 0x8000)
3277     macro_build (NULL, counter, &imm_expr, unsignedp ? "sltiu" : "slti",
3278                  "t,r,j", AT, reg, BFD_RELOC_LO16);
3279   else
3280     {
3281       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3282       macro_build (NULL, counter, NULL, unsignedp ? "sltu" : "slt",
3283                    "d,v,t", AT, reg, AT);
3284     }
3285 }
3286
3287 /* Warn if an expression is not a constant.  */
3288
3289 static void
3290 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3291 {
3292   if (ex->X_op == O_big)
3293     as_bad (_("unsupported large constant"));
3294   else if (ex->X_op != O_constant)
3295     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3296 }
3297
3298 /* Count the leading zeroes by performing a binary chop. This is a
3299    bulky bit of source, but performance is a LOT better for the
3300    majority of values than a simple loop to count the bits:
3301        for (lcnt = 0; (lcnt < 32); lcnt++)
3302          if ((v) & (1 << (31 - lcnt)))
3303            break;
3304   However it is not code size friendly, and the gain will drop a bit
3305   on certain cached systems.
3306 */
3307 #define COUNT_TOP_ZEROES(v)             \
3308   (((v) & ~0xffff) == 0                 \
3309    ? ((v) & ~0xff) == 0                 \
3310      ? ((v) & ~0xf) == 0                \
3311        ? ((v) & ~0x3) == 0              \
3312          ? ((v) & ~0x1) == 0            \
3313            ? !(v)                       \
3314              ? 32                       \
3315              : 31                       \
3316            : 30                         \
3317          : ((v) & ~0x7) == 0            \
3318            ? 29                         \
3319            : 28                         \
3320        : ((v) & ~0x3f) == 0             \
3321          ? ((v) & ~0x1f) == 0           \
3322            ? 27                         \
3323            : 26                         \
3324          : ((v) & ~0x7f) == 0           \
3325            ? 25                         \
3326            : 24                         \
3327      : ((v) & ~0xfff) == 0              \
3328        ? ((v) & ~0x3ff) == 0            \
3329          ? ((v) & ~0x1ff) == 0          \
3330            ? 23                         \
3331            : 22                         \
3332          : ((v) & ~0x7ff) == 0          \
3333            ? 21                         \
3334            : 20                         \
3335        : ((v) & ~0x3fff) == 0           \
3336          ? ((v) & ~0x1fff) == 0         \
3337            ? 19                         \
3338            : 18                         \
3339          : ((v) & ~0x7fff) == 0         \
3340            ? 17                         \
3341            : 16                         \
3342    : ((v) & ~0xffffff) == 0             \
3343      ? ((v) & ~0xfffff) == 0            \
3344        ? ((v) & ~0x3ffff) == 0          \
3345          ? ((v) & ~0x1ffff) == 0        \
3346            ? 15                         \
3347            : 14                         \
3348          : ((v) & ~0x7ffff) == 0        \
3349            ? 13                         \
3350            : 12                         \
3351        : ((v) & ~0x3fffff) == 0         \
3352          ? ((v) & ~0x1fffff) == 0       \
3353            ? 11                         \
3354            : 10                         \
3355          : ((v) & ~0x7fffff) == 0       \
3356            ? 9                          \
3357            : 8                          \
3358      : ((v) & ~0xfffffff) == 0          \
3359        ? ((v) & ~0x3ffffff) == 0        \
3360          ? ((v) & ~0x1ffffff) == 0      \
3361            ? 7                          \
3362            : 6                          \
3363          : ((v) & ~0x7ffffff) == 0      \
3364            ? 5                          \
3365            : 4                          \
3366        : ((v) & ~0x3fffffff) == 0       \
3367          ? ((v) & ~0x1fffffff) == 0     \
3368            ? 3                          \
3369            : 2                          \
3370          : ((v) & ~0x7fffffff) == 0     \
3371            ? 1                          \
3372            : 0)
3373
3374 /*                      load_register()
3375  *  This routine generates the least number of instructions neccessary to load
3376  *  an absolute expression value into a register.
3377  */
3378 static void
3379 load_register (int *counter, int reg, expressionS *ep, int dbl)
3380 {
3381   int freg;
3382   expressionS hi32, lo32;
3383
3384   if (ep->X_op != O_big)
3385     {
3386       assert (ep->X_op == O_constant);
3387
3388       /* Sign-extending 32-bit constants makes their handling easier.  */
3389       if (! dbl)
3390         ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3391                             - 0x80000000);
3392
3393       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3394         {
3395           /* We can handle 16 bit signed values with an addiu to
3396              $zero.  No need to ever use daddiu here, since $zero and
3397              the result are always correct in 32 bit mode.  */
3398           macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3399                        BFD_RELOC_LO16);
3400           return;
3401         }
3402       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3403         {
3404           /* We can handle 16 bit unsigned values with an ori to
3405              $zero.  */
3406           macro_build (NULL, counter, ep, "ori", "t,r,i", reg, 0,
3407                        BFD_RELOC_LO16);
3408           return;
3409         }
3410       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3411         {
3412           /* 32 bit values require an lui.  */
3413           macro_build (NULL, counter, ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3414           if ((ep->X_add_number & 0xffff) != 0)
3415             macro_build (NULL, counter, ep, "ori", "t,r,i", reg, reg,
3416                          BFD_RELOC_LO16);
3417           return;
3418         }
3419     }
3420
3421   /* The value is larger than 32 bits.  */
3422
3423   if (HAVE_32BIT_GPRS)
3424     {
3425       as_bad (_("Number (0x%lx) larger than 32 bits"),
3426               (unsigned long) ep->X_add_number);
3427       macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3428                    BFD_RELOC_LO16);
3429       return;
3430     }
3431
3432   if (ep->X_op != O_big)
3433     {
3434       hi32 = *ep;
3435       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3436       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3437       hi32.X_add_number &= 0xffffffff;
3438       lo32 = *ep;
3439       lo32.X_add_number &= 0xffffffff;
3440     }
3441   else
3442     {
3443       assert (ep->X_add_number > 2);
3444       if (ep->X_add_number == 3)
3445         generic_bignum[3] = 0;
3446       else if (ep->X_add_number > 4)
3447         as_bad (_("Number larger than 64 bits"));
3448       lo32.X_op = O_constant;
3449       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3450       hi32.X_op = O_constant;
3451       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3452     }
3453
3454   if (hi32.X_add_number == 0)
3455     freg = 0;
3456   else
3457     {
3458       int shift, bit;
3459       unsigned long hi, lo;
3460
3461       if (hi32.X_add_number == (offsetT) 0xffffffff)
3462         {
3463           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3464             {
3465               macro_build (NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
3466                            BFD_RELOC_LO16);
3467               return;
3468             }
3469           if (lo32.X_add_number & 0x80000000)
3470             {
3471               macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3472                            BFD_RELOC_HI16);
3473               if (lo32.X_add_number & 0xffff)
3474                 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
3475                              BFD_RELOC_LO16);
3476               return;
3477             }
3478         }
3479
3480       /* Check for 16bit shifted constant.  We know that hi32 is
3481          non-zero, so start the mask on the first bit of the hi32
3482          value.  */
3483       shift = 17;
3484       do
3485         {
3486           unsigned long himask, lomask;
3487
3488           if (shift < 32)
3489             {
3490               himask = 0xffff >> (32 - shift);
3491               lomask = (0xffff << shift) & 0xffffffff;
3492             }
3493           else
3494             {
3495               himask = 0xffff << (shift - 32);
3496               lomask = 0;
3497             }
3498           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3499               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3500             {
3501               expressionS tmp;
3502
3503               tmp.X_op = O_constant;
3504               if (shift < 32)
3505                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3506                                     | (lo32.X_add_number >> shift));
3507               else
3508                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3509               macro_build (NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
3510                            BFD_RELOC_LO16);
3511               macro_build (NULL, counter, NULL,
3512                            (shift >= 32) ? "dsll32" : "dsll",
3513                            "d,w,<", reg, reg,
3514                            (shift >= 32) ? shift - 32 : shift);
3515               return;
3516             }
3517           ++shift;
3518         }
3519       while (shift <= (64 - 16));
3520
3521       /* Find the bit number of the lowest one bit, and store the
3522          shifted value in hi/lo.  */
3523       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3524       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3525       if (lo != 0)
3526         {
3527           bit = 0;
3528           while ((lo & 1) == 0)
3529             {
3530               lo >>= 1;
3531               ++bit;
3532             }
3533           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3534           hi >>= bit;
3535         }
3536       else
3537         {
3538           bit = 32;
3539           while ((hi & 1) == 0)
3540             {
3541               hi >>= 1;
3542               ++bit;
3543             }
3544           lo = hi;
3545           hi = 0;
3546         }
3547
3548       /* Optimize if the shifted value is a (power of 2) - 1.  */
3549       if ((hi == 0 && ((lo + 1) & lo) == 0)
3550           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3551         {
3552           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3553           if (shift != 0)
3554             {
3555               expressionS tmp;
3556
3557               /* This instruction will set the register to be all
3558                  ones.  */
3559               tmp.X_op = O_constant;
3560               tmp.X_add_number = (offsetT) -1;
3561               macro_build (NULL, counter, &tmp, "addiu", "t,r,j", reg, 0,
3562                            BFD_RELOC_LO16);
3563               if (bit != 0)
3564                 {
3565                   bit += shift;
3566                   macro_build (NULL, counter, NULL,
3567                                (bit >= 32) ? "dsll32" : "dsll",
3568                                "d,w,<", reg, reg,
3569                                (bit >= 32) ? bit - 32 : bit);
3570                 }
3571               macro_build (NULL, counter, NULL,
3572                            (shift >= 32) ? "dsrl32" : "dsrl",
3573                            "d,w,<", reg, reg,
3574                            (shift >= 32) ? shift - 32 : shift);
3575               return;
3576             }
3577         }
3578
3579       /* Sign extend hi32 before calling load_register, because we can
3580          generally get better code when we load a sign extended value.  */
3581       if ((hi32.X_add_number & 0x80000000) != 0)
3582         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3583       load_register (counter, reg, &hi32, 0);
3584       freg = reg;
3585     }
3586   if ((lo32.X_add_number & 0xffff0000) == 0)
3587     {
3588       if (freg != 0)
3589         {
3590           macro_build (NULL, counter, NULL, "dsll32", "d,w,<", reg, freg, 0);
3591           freg = reg;
3592         }
3593     }
3594   else
3595     {
3596       expressionS mid16;
3597
3598       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3599         {
3600           macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3601                        BFD_RELOC_HI16);
3602           macro_build (NULL, counter, NULL, "dsrl32", "d,w,<", reg, reg, 0);
3603           return;
3604         }
3605
3606       if (freg != 0)
3607         {
3608           macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, freg, 16);
3609           freg = reg;
3610         }
3611       mid16 = lo32;
3612       mid16.X_add_number >>= 16;
3613       macro_build (NULL, counter, &mid16, "ori", "t,r,i", reg, freg,
3614                    BFD_RELOC_LO16);
3615       macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3616       freg = reg;
3617     }
3618   if ((lo32.X_add_number & 0xffff) != 0)
3619     macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3620                  BFD_RELOC_LO16);
3621 }
3622
3623 /* Load an address into a register.  */
3624
3625 static void
3626 load_address (int *counter, int reg, expressionS *ep, int *used_at)
3627 {
3628   char *p = NULL;
3629
3630   if (ep->X_op != O_constant
3631       && ep->X_op != O_symbol)
3632     {
3633       as_bad (_("expression too complex"));
3634       ep->X_op = O_constant;
3635     }
3636
3637   if (ep->X_op == O_constant)
3638     {
3639       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3640       return;
3641     }
3642
3643   if (mips_pic == NO_PIC)
3644     {
3645       /* If this is a reference to a GP relative symbol, we want
3646            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3647          Otherwise we want
3648            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3649            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3650          If we have an addend, we always use the latter form.
3651
3652          With 64bit address space and a usable $at we want
3653            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3654            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3655            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3656            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3657            dsll32       $reg,0
3658            daddu        $reg,$reg,$at
3659
3660          If $at is already in use, we use a path which is suboptimal
3661          on superscalar processors.
3662            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3663            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3664            dsll         $reg,16
3665            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3666            dsll         $reg,16
3667            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3668        */
3669       if (HAVE_64BIT_ADDRESSES)
3670         {
3671           /* We don't do GP optimization for now because RELAX_ENCODE can't
3672              hold the data for such large chunks.  */
3673
3674           if (*used_at == 0 && ! mips_opts.noat)
3675             {
3676               macro_build (p, counter, ep, "lui", "t,u",
3677                            reg, BFD_RELOC_MIPS_HIGHEST);
3678               macro_build (p, counter, ep, "lui", "t,u",
3679                            AT, BFD_RELOC_HI16_S);
3680               macro_build (p, counter, ep, "daddiu", "t,r,j",
3681                            reg, reg, BFD_RELOC_MIPS_HIGHER);
3682               macro_build (p, counter, ep, "daddiu", "t,r,j",
3683                            AT, AT, BFD_RELOC_LO16);
3684               macro_build (p, counter, NULL, "dsll32", "d,w,<", reg, reg, 0);
3685               macro_build (p, counter, NULL, "daddu", "d,v,t", reg, reg, AT);
3686               *used_at = 1;
3687             }
3688           else
3689             {
3690               macro_build (p, counter, ep, "lui", "t,u",
3691                            reg, BFD_RELOC_MIPS_HIGHEST);
3692               macro_build (p, counter, ep, "daddiu", "t,r,j",
3693                            reg, reg, BFD_RELOC_MIPS_HIGHER);
3694               macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3695               macro_build (p, counter, ep, "daddiu", "t,r,j",
3696                            reg, reg, BFD_RELOC_HI16_S);
3697               macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3698               macro_build (p, counter, ep, "daddiu", "t,r,j",
3699                            reg, reg, BFD_RELOC_LO16);
3700             }
3701         }
3702       else
3703         {
3704           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3705               && ! nopic_need_relax (ep->X_add_symbol, 1))
3706             {
3707               frag_grow (20);
3708               macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3709                            mips_gp_register, BFD_RELOC_GPREL16);
3710               p = frag_var (rs_machine_dependent, 8, 0,
3711                             RELAX_ENCODE (4, 8, 0, 4, 0,
3712                                           mips_opts.warn_about_macros),
3713                             ep->X_add_symbol, 0, NULL);
3714             }
3715           macro_build_lui (p, counter, ep, reg);
3716           if (p != NULL)
3717             p += 4;
3718           macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3719                        BFD_RELOC_LO16);
3720         }
3721     }
3722   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3723     {
3724       expressionS ex;
3725
3726       /* If this is a reference to an external symbol, we want
3727            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3728          Otherwise we want
3729            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3730            nop
3731            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3732          If there is a constant, it must be added in after.
3733
3734          If we have NewABI, we want
3735            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
3736          unless we're referencing a global symbol with a non-zero
3737          offset, in which case cst must be added separately.  */
3738       if (HAVE_NEWABI)
3739         {
3740           frag_grow (12);
3741
3742           if (ep->X_add_number)
3743             {
3744               frag_now->tc_frag_data.tc_fr_offset =
3745                 ex.X_add_number = ep->X_add_number;
3746               ep->X_add_number = 0;
3747               macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)",
3748                            reg, BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3749               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3750                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3751               ex.X_op = O_constant;
3752               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3753                            reg, reg, BFD_RELOC_LO16);
3754               p = frag_var (rs_machine_dependent, 8, 0,
3755                             RELAX_ENCODE (8, 4, 0, 0, 0,
3756                                           mips_opts.warn_about_macros),
3757                             ep->X_add_symbol, 0, NULL);
3758               ep->X_add_number = ex.X_add_number;
3759             }
3760
3761           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3762                        BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3763
3764           if (! p)
3765             {
3766               /* To avoid confusion in tc_gen_reloc, we must ensure
3767                  that this does not become a variant frag.  */
3768               frag_wane (frag_now);
3769               frag_new (0);
3770             }
3771         }
3772       else
3773         {
3774           ex.X_add_number = ep->X_add_number;
3775           ep->X_add_number = 0;
3776           frag_grow (20);
3777           macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3778                        BFD_RELOC_MIPS_GOT16,
3779                        mips_gp_register);
3780           macro_build (NULL, counter, NULL, "nop", "");
3781           p = frag_var (rs_machine_dependent, 4, 0,
3782                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3783                         ep->X_add_symbol, 0, NULL);
3784           macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3785                        BFD_RELOC_LO16);
3786
3787           if (ex.X_add_number != 0)
3788             {
3789               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3790                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3791               ex.X_op = O_constant;
3792               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3793                            reg, reg, BFD_RELOC_LO16);
3794             }
3795         }
3796     }
3797   else if (mips_pic == SVR4_PIC)
3798     {
3799       expressionS ex;
3800       int off;
3801
3802       /* This is the large GOT case.  If this is a reference to an
3803          external symbol, we want
3804            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3805            addu         $reg,$reg,$gp
3806            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3807
3808          Otherwise, for a reference to a local symbol in old ABI, we want
3809            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3810            nop
3811            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3812          If there is a constant, it must be added in after.
3813
3814          In the NewABI, for local symbols, with or without offsets, we want:
3815            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3816            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3817       */
3818       if (HAVE_NEWABI)
3819         {
3820           frag_grow (24);
3821
3822           frag_now->tc_frag_data.tc_fr_offset =
3823             ex.X_add_number = ep->X_add_number;
3824           ep->X_add_number = 0;
3825           macro_build (NULL, counter, ep, "lui", "t,u", reg,
3826                        BFD_RELOC_MIPS_GOT_HI16);
3827           macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3828                        reg, mips_gp_register);
3829           macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3830                        BFD_RELOC_MIPS_GOT_LO16, reg);
3831           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3832             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3833           else if (ex.X_add_number)
3834             {
3835               ex.X_op = O_constant;
3836               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3837                            reg, reg, BFD_RELOC_LO16);
3838             }
3839
3840           ep->X_add_number = ex.X_add_number;
3841           p = frag_var (rs_machine_dependent, 8, 0,
3842                         RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
3843                                       mips_opts.warn_about_macros),
3844                         ep->X_add_symbol, 0, NULL);
3845           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3846                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3847           macro_build (p + 4, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3848                        reg, BFD_RELOC_MIPS_GOT_OFST);
3849         }
3850       else
3851         {
3852           ex.X_add_number = ep->X_add_number;
3853           ep->X_add_number = 0;
3854           if (reg_needs_delay (mips_gp_register))
3855             off = 4;
3856           else
3857             off = 0;
3858           frag_grow (32);
3859           macro_build (NULL, counter, ep, "lui", "t,u", reg,
3860                        BFD_RELOC_MIPS_GOT_HI16);
3861           macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3862                        reg, mips_gp_register);
3863           macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3864                        BFD_RELOC_MIPS_GOT_LO16, reg);
3865           p = frag_var (rs_machine_dependent, 12 + off, 0,
3866                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3867                                       mips_opts.warn_about_macros),
3868                         ep->X_add_symbol, 0, NULL);
3869           if (off > 0)
3870             {
3871               /* We need a nop before loading from $gp.  This special
3872                  check is required because the lui which starts the main
3873                  instruction stream does not refer to $gp, and so will not
3874                  insert the nop which may be required.  */
3875               macro_build (p, counter, NULL, "nop", "");
3876                 p += 4;
3877             }
3878           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3879                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
3880           p += 4;
3881           macro_build (p, counter, NULL, "nop", "");
3882           p += 4;
3883           macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3884                        BFD_RELOC_LO16);
3885
3886           if (ex.X_add_number != 0)
3887             {
3888               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3889                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3890               ex.X_op = O_constant;
3891               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3892                            reg, reg, BFD_RELOC_LO16);
3893             }
3894         }
3895     }
3896   else if (mips_pic == EMBEDDED_PIC)
3897     {
3898       /* We always do
3899            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3900        */
3901       macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3902                    mips_gp_register, BFD_RELOC_GPREL16);
3903     }
3904   else
3905     abort ();
3906 }
3907
3908 /* Move the contents of register SOURCE into register DEST.  */
3909
3910 static void
3911 move_register (int *counter, int dest, int source)
3912 {
3913   macro_build (NULL, counter, NULL, HAVE_32BIT_GPRS ? "addu" : "daddu",
3914                "d,v,t", dest, source, 0);
3915 }
3916
3917 /*
3918  *                      Build macros
3919  *   This routine implements the seemingly endless macro or synthesized
3920  * instructions and addressing modes in the mips assembly language. Many
3921  * of these macros are simple and are similar to each other. These could
3922  * probably be handled by some kind of table or grammer aproach instead of
3923  * this verbose method. Others are not simple macros but are more like
3924  * optimizing code generation.
3925  *   One interesting optimization is when several store macros appear
3926  * consecutivly that would load AT with the upper half of the same address.
3927  * The ensuing load upper instructions are ommited. This implies some kind
3928  * of global optimization. We currently only optimize within a single macro.
3929  *   For many of the load and store macros if the address is specified as a
3930  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3931  * first load register 'at' with zero and use it as the base register. The
3932  * mips assembler simply uses register $zero. Just one tiny optimization
3933  * we're missing.
3934  */
3935 static void
3936 macro (struct mips_cl_insn *ip)
3937 {
3938   register int treg, sreg, dreg, breg;
3939   int tempreg;
3940   int mask;
3941   int icnt = 0;
3942   int used_at = 0;
3943   expressionS expr1;
3944   const char *s;
3945   const char *s2;
3946   const char *fmt;
3947   int likely = 0;
3948   int dbl = 0;
3949   int coproc = 0;
3950   int lr = 0;
3951   int imm = 0;
3952   offsetT maxnum;
3953   int off;
3954   bfd_reloc_code_real_type r;
3955   int hold_mips_optimize;
3956
3957   assert (! mips_opts.mips16);
3958
3959   treg = (ip->insn_opcode >> 16) & 0x1f;
3960   dreg = (ip->insn_opcode >> 11) & 0x1f;
3961   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3962   mask = ip->insn_mo->mask;
3963
3964   expr1.X_op = O_constant;
3965   expr1.X_op_symbol = NULL;
3966   expr1.X_add_symbol = NULL;
3967   expr1.X_add_number = 1;
3968
3969   /* Umatched fixups should not be put in the same frag as a relaxable
3970      macro.  For example, suppose we have:
3971
3972         lui $4,%hi(l1)          # 1
3973         la $5,l2                # 2
3974         addiu $4,$4,%lo(l1)     # 3
3975
3976      If instructions 1 and 2 were put in the same frag, md_frob_file would
3977      move the fixup for #1 after the fixups for the "unrelaxed" version of
3978      #2.  This would confuse tc_gen_reloc, which expects the relocations
3979      for #2 to be the last for that frag.
3980
3981      Also, if tc_gen_reloc sees certain relocations in a variant frag,
3982      it assumes that they belong to a relaxable macro.  We mustn't put
3983      other uses of such relocations into a variant frag.
3984
3985      To avoid both problems, finish the current frag it contains a
3986      %reloc() operator.  The macro then goes into a new frag.  */
3987   if (prev_reloc_op_frag == frag_now)
3988     {
3989       frag_wane (frag_now);
3990       frag_new (0);
3991     }
3992
3993   switch (mask)
3994     {
3995     case M_DABS:
3996       dbl = 1;
3997     case M_ABS:
3998       /* bgez $a0,.+12
3999          move v0,$a0
4000          sub v0,$zero,$a0
4001          */
4002
4003       mips_emit_delays (TRUE);
4004       ++mips_opts.noreorder;
4005       mips_any_noreorder = 1;
4006
4007       expr1.X_add_number = 8;
4008       macro_build (NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4009       if (dreg == sreg)
4010         macro_build (NULL, &icnt, NULL, "nop", "", 0);
4011       else
4012         move_register (&icnt, dreg, sreg);
4013       macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0,
4014                    sreg);
4015
4016       --mips_opts.noreorder;
4017       return;
4018
4019     case M_ADD_I:
4020       s = "addi";
4021       s2 = "add";
4022       goto do_addi;
4023     case M_ADDU_I:
4024       s = "addiu";
4025       s2 = "addu";
4026       goto do_addi;
4027     case M_DADD_I:
4028       dbl = 1;
4029       s = "daddi";
4030       s2 = "dadd";
4031       goto do_addi;
4032     case M_DADDU_I:
4033       dbl = 1;
4034       s = "daddiu";
4035       s2 = "daddu";
4036     do_addi:
4037       if (imm_expr.X_op == O_constant
4038           && imm_expr.X_add_number >= -0x8000
4039           && imm_expr.X_add_number < 0x8000)
4040         {
4041           macro_build (NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4042                        BFD_RELOC_LO16);
4043           return;
4044         }
4045       load_register (&icnt, AT, &imm_expr, dbl);
4046       macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4047       break;
4048
4049     case M_AND_I:
4050       s = "andi";
4051       s2 = "and";
4052       goto do_bit;
4053     case M_OR_I:
4054       s = "ori";
4055       s2 = "or";
4056       goto do_bit;
4057     case M_NOR_I:
4058       s = "";
4059       s2 = "nor";
4060       goto do_bit;
4061     case M_XOR_I:
4062       s = "xori";
4063       s2 = "xor";
4064     do_bit:
4065       if (imm_expr.X_op == O_constant
4066           && imm_expr.X_add_number >= 0
4067           && imm_expr.X_add_number < 0x10000)
4068         {
4069           if (mask != M_NOR_I)
4070             macro_build (NULL, &icnt, &imm_expr, s, "t,r,i", treg, sreg,
4071                          BFD_RELOC_LO16);
4072           else
4073             {
4074               macro_build (NULL, &icnt, &imm_expr, "ori", "t,r,i", treg, sreg,
4075                            BFD_RELOC_LO16);
4076               macro_build (NULL, &icnt, NULL, "nor", "d,v,t", treg, treg, 0);
4077             }
4078           return;
4079         }
4080
4081       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4082       macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4083       break;
4084
4085     case M_BEQ_I:
4086       s = "beq";
4087       goto beq_i;
4088     case M_BEQL_I:
4089       s = "beql";
4090       likely = 1;
4091       goto beq_i;
4092     case M_BNE_I:
4093       s = "bne";
4094       goto beq_i;
4095     case M_BNEL_I:
4096       s = "bnel";
4097       likely = 1;
4098     beq_i:
4099       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4100         {
4101           macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, 0);
4102           return;
4103         }
4104       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4105       macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4106       break;
4107
4108     case M_BGEL:
4109       likely = 1;
4110     case M_BGE:
4111       if (treg == 0)
4112         {
4113           macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4114                        "s,p", sreg);
4115           return;
4116         }
4117       if (sreg == 0)
4118         {
4119           macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4120                        "s,p", treg);
4121           return;
4122         }
4123       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4124       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4125                    "s,t,p", AT, 0);
4126       break;
4127
4128     case M_BGTL_I:
4129       likely = 1;
4130     case M_BGT_I:
4131       /* check for > max integer */
4132       maxnum = 0x7fffffff;
4133       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4134         {
4135           maxnum <<= 16;
4136           maxnum |= 0xffff;
4137           maxnum <<= 16;
4138           maxnum |= 0xffff;
4139         }
4140       if (imm_expr.X_op == O_constant
4141           && imm_expr.X_add_number >= maxnum
4142           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4143         {
4144         do_false:
4145           /* result is always false */
4146           if (! likely)
4147             {
4148               if (warn_nops)
4149                 as_warn (_("Branch %s is always false (nop)"),
4150                          ip->insn_mo->name);
4151               macro_build (NULL, &icnt, NULL, "nop", "", 0);
4152             }
4153           else
4154             {
4155               if (warn_nops)
4156                 as_warn (_("Branch likely %s is always false"),
4157                          ip->insn_mo->name);
4158               macro_build (NULL, &icnt, &offset_expr, "bnel", "s,t,p", 0, 0);
4159             }
4160           return;
4161         }
4162       if (imm_expr.X_op != O_constant)
4163         as_bad (_("Unsupported large constant"));
4164       ++imm_expr.X_add_number;
4165       /* FALLTHROUGH */
4166     case M_BGE_I:
4167     case M_BGEL_I:
4168       if (mask == M_BGEL_I)
4169         likely = 1;
4170       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4171         {
4172           macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4173                        "s,p", sreg);
4174           return;
4175         }
4176       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4177         {
4178           macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4179                        "s,p", sreg);
4180           return;
4181         }
4182       maxnum = 0x7fffffff;
4183       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4184         {
4185           maxnum <<= 16;
4186           maxnum |= 0xffff;
4187           maxnum <<= 16;
4188           maxnum |= 0xffff;
4189         }
4190       maxnum = - maxnum - 1;
4191       if (imm_expr.X_op == O_constant
4192           && imm_expr.X_add_number <= maxnum
4193           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4194         {
4195         do_true:
4196           /* result is always true */
4197           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4198           macro_build (NULL, &icnt, &offset_expr, "b", "p");
4199           return;
4200         }
4201       set_at (&icnt, sreg, 0);
4202       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4203                    "s,t,p", AT, 0);
4204       break;
4205
4206     case M_BGEUL:
4207       likely = 1;
4208     case M_BGEU:
4209       if (treg == 0)
4210         goto do_true;
4211       if (sreg == 0)
4212         {
4213           macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4214                        "s,t,p", 0, treg);
4215           return;
4216         }
4217       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4218       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4219                    "s,t,p", AT, 0);
4220       break;
4221
4222     case M_BGTUL_I:
4223       likely = 1;
4224     case M_BGTU_I:
4225       if (sreg == 0
4226           || (HAVE_32BIT_GPRS
4227               && imm_expr.X_op == O_constant
4228               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4229         goto do_false;
4230       if (imm_expr.X_op != O_constant)
4231         as_bad (_("Unsupported large constant"));
4232       ++imm_expr.X_add_number;
4233       /* FALLTHROUGH */
4234     case M_BGEU_I:
4235     case M_BGEUL_I:
4236       if (mask == M_BGEUL_I)
4237         likely = 1;
4238       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4239         goto do_true;
4240       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4241         {
4242           macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4243                        "s,t,p", sreg, 0);
4244           return;
4245         }
4246       set_at (&icnt, sreg, 1);
4247       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4248                    "s,t,p", AT, 0);
4249       break;
4250
4251     case M_BGTL:
4252       likely = 1;
4253     case M_BGT:
4254       if (treg == 0)
4255         {
4256           macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4257                        "s,p", sreg);
4258           return;
4259         }
4260       if (sreg == 0)
4261         {
4262           macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4263                        "s,p", treg);
4264           return;
4265         }
4266       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4267       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4268                    "s,t,p", AT, 0);
4269       break;
4270
4271     case M_BGTUL:
4272       likely = 1;
4273     case M_BGTU:
4274       if (treg == 0)
4275         {
4276           macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4277                        "s,t,p", sreg, 0);
4278           return;
4279         }
4280       if (sreg == 0)
4281         goto do_false;
4282       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4283       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4284                    "s,t,p", AT, 0);
4285       break;
4286
4287     case M_BLEL:
4288       likely = 1;
4289     case M_BLE:
4290       if (treg == 0)
4291         {
4292           macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4293                        "s,p", sreg);
4294           return;
4295         }
4296       if (sreg == 0)
4297         {
4298           macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4299                        "s,p", treg);
4300           return;
4301         }
4302       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4303       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4304                    "s,t,p", AT, 0);
4305       break;
4306
4307     case M_BLEL_I:
4308       likely = 1;
4309     case M_BLE_I:
4310       maxnum = 0x7fffffff;
4311       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4312         {
4313           maxnum <<= 16;
4314           maxnum |= 0xffff;
4315           maxnum <<= 16;
4316           maxnum |= 0xffff;
4317         }
4318       if (imm_expr.X_op == O_constant
4319           && imm_expr.X_add_number >= maxnum
4320           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4321         goto do_true;
4322       if (imm_expr.X_op != O_constant)
4323         as_bad (_("Unsupported large constant"));
4324       ++imm_expr.X_add_number;
4325       /* FALLTHROUGH */
4326     case M_BLT_I:
4327     case M_BLTL_I:
4328       if (mask == M_BLTL_I)
4329         likely = 1;
4330       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4331         {
4332           macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4333                        "s,p", sreg);
4334           return;
4335         }
4336       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4337         {
4338           macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4339                        "s,p", sreg);
4340           return;
4341         }
4342       set_at (&icnt, sreg, 0);
4343       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4344                    "s,t,p", AT, 0);
4345       break;
4346
4347     case M_BLEUL:
4348       likely = 1;
4349     case M_BLEU:
4350       if (treg == 0)
4351         {
4352           macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4353                        "s,t,p", sreg, 0);
4354           return;
4355         }
4356       if (sreg == 0)
4357         goto do_true;
4358       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4359       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4360                    "s,t,p", AT, 0);
4361       break;
4362
4363     case M_BLEUL_I:
4364       likely = 1;
4365     case M_BLEU_I:
4366       if (sreg == 0
4367           || (HAVE_32BIT_GPRS
4368               && imm_expr.X_op == O_constant
4369               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4370         goto do_true;
4371       if (imm_expr.X_op != O_constant)
4372         as_bad (_("Unsupported large constant"));
4373       ++imm_expr.X_add_number;
4374       /* FALLTHROUGH */
4375     case M_BLTU_I:
4376     case M_BLTUL_I:
4377       if (mask == M_BLTUL_I)
4378         likely = 1;
4379       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4380         goto do_false;
4381       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4382         {
4383           macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4384                        "s,t,p", sreg, 0);
4385           return;
4386         }
4387       set_at (&icnt, sreg, 1);
4388       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4389                    "s,t,p", AT, 0);
4390       break;
4391
4392     case M_BLTL:
4393       likely = 1;
4394     case M_BLT:
4395       if (treg == 0)
4396         {
4397           macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4398                        "s,p", sreg);
4399           return;
4400         }
4401       if (sreg == 0)
4402         {
4403           macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4404                        "s,p", treg);
4405           return;
4406         }
4407       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4408       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4409                    "s,t,p", AT, 0);
4410       break;
4411
4412     case M_BLTUL:
4413       likely = 1;
4414     case M_BLTU:
4415       if (treg == 0)
4416         goto do_false;
4417       if (sreg == 0)
4418         {
4419           macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4420                        "s,t,p", 0, treg);
4421           return;
4422         }
4423       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4424       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4425                    "s,t,p", AT, 0);
4426       break;
4427
4428     case M_DDIV_3:
4429       dbl = 1;
4430     case M_DIV_3:
4431       s = "mflo";
4432       goto do_div3;
4433     case M_DREM_3:
4434       dbl = 1;
4435     case M_REM_3:
4436       s = "mfhi";
4437     do_div3:
4438       if (treg == 0)
4439         {
4440           as_warn (_("Divide by zero."));
4441           if (mips_trap)
4442             macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4443           else
4444             macro_build (NULL, &icnt, NULL, "break", "c", 7);
4445           return;
4446         }
4447
4448       mips_emit_delays (TRUE);
4449       ++mips_opts.noreorder;
4450       mips_any_noreorder = 1;
4451       if (mips_trap)
4452         {
4453           macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4454           macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4455                        sreg, treg);
4456         }
4457       else
4458         {
4459           expr1.X_add_number = 8;
4460           macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4461           macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4462                        sreg, treg);
4463           macro_build (NULL, &icnt,NULL, "break", "c", 7);
4464         }
4465       expr1.X_add_number = -1;
4466       macro_build (NULL, &icnt, &expr1, dbl ? "daddiu" : "addiu", "t,r,j",
4467                    AT, 0, BFD_RELOC_LO16);
4468       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4469       macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4470       if (dbl)
4471         {
4472           expr1.X_add_number = 1;
4473           macro_build (NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4474                        BFD_RELOC_LO16);
4475           macro_build (NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT, 31);
4476         }
4477       else
4478         {
4479           expr1.X_add_number = 0x80000000;
4480           macro_build (NULL, &icnt, &expr1, "lui", "t,u", AT,
4481                        BFD_RELOC_HI16);
4482         }
4483       if (mips_trap)
4484         {
4485           macro_build (NULL, &icnt, NULL, "teq", "s,t,q", sreg, AT, 6);
4486           /* We want to close the noreorder block as soon as possible, so
4487              that later insns are available for delay slot filling.  */
4488           --mips_opts.noreorder;
4489         }
4490       else
4491         {
4492           expr1.X_add_number = 8;
4493           macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4494           macro_build (NULL, &icnt, NULL, "nop", "", 0);
4495
4496           /* We want to close the noreorder block as soon as possible, so
4497              that later insns are available for delay slot filling.  */
4498           --mips_opts.noreorder;
4499
4500           macro_build (NULL, &icnt, NULL, "break", "c", 6);
4501         }
4502       macro_build (NULL, &icnt, NULL, s, "d", dreg);
4503       break;
4504
4505     case M_DIV_3I:
4506       s = "div";
4507       s2 = "mflo";
4508       goto do_divi;
4509     case M_DIVU_3I:
4510       s = "divu";
4511       s2 = "mflo";
4512       goto do_divi;
4513     case M_REM_3I:
4514       s = "div";
4515       s2 = "mfhi";
4516       goto do_divi;
4517     case M_REMU_3I:
4518       s = "divu";
4519       s2 = "mfhi";
4520       goto do_divi;
4521     case M_DDIV_3I:
4522       dbl = 1;
4523       s = "ddiv";
4524       s2 = "mflo";
4525       goto do_divi;
4526     case M_DDIVU_3I:
4527       dbl = 1;
4528       s = "ddivu";
4529       s2 = "mflo";
4530       goto do_divi;
4531     case M_DREM_3I:
4532       dbl = 1;
4533       s = "ddiv";
4534       s2 = "mfhi";
4535       goto do_divi;
4536     case M_DREMU_3I:
4537       dbl = 1;
4538       s = "ddivu";
4539       s2 = "mfhi";
4540     do_divi:
4541       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4542         {
4543           as_warn (_("Divide by zero."));
4544           if (mips_trap)
4545             macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4546           else
4547             macro_build (NULL, &icnt, NULL, "break", "c", 7);
4548           return;
4549         }
4550       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4551         {
4552           if (strcmp (s2, "mflo") == 0)
4553             move_register (&icnt, dreg, sreg);
4554           else
4555             move_register (&icnt, dreg, 0);
4556           return;
4557         }
4558       if (imm_expr.X_op == O_constant
4559           && imm_expr.X_add_number == -1
4560           && s[strlen (s) - 1] != 'u')
4561         {
4562           if (strcmp (s2, "mflo") == 0)
4563             {
4564               macro_build (NULL, &icnt, NULL, dbl ? "dneg" : "neg", "d,w",
4565                            dreg, sreg);
4566             }
4567           else
4568             move_register (&icnt, dreg, 0);
4569           return;
4570         }
4571
4572       load_register (&icnt, AT, &imm_expr, dbl);
4573       macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4574       macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4575       break;
4576
4577     case M_DIVU_3:
4578       s = "divu";
4579       s2 = "mflo";
4580       goto do_divu3;
4581     case M_REMU_3:
4582       s = "divu";
4583       s2 = "mfhi";
4584       goto do_divu3;
4585     case M_DDIVU_3:
4586       s = "ddivu";
4587       s2 = "mflo";
4588       goto do_divu3;
4589     case M_DREMU_3:
4590       s = "ddivu";
4591       s2 = "mfhi";
4592     do_divu3:
4593       mips_emit_delays (TRUE);
4594       ++mips_opts.noreorder;
4595       mips_any_noreorder = 1;
4596       if (mips_trap)
4597         {
4598           macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4599           macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4600           /* We want to close the noreorder block as soon as possible, so
4601              that later insns are available for delay slot filling.  */
4602           --mips_opts.noreorder;
4603         }
4604       else
4605         {
4606           expr1.X_add_number = 8;
4607           macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4608           macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4609
4610           /* We want to close the noreorder block as soon as possible, so
4611              that later insns are available for delay slot filling.  */
4612           --mips_opts.noreorder;
4613           macro_build (NULL, &icnt, NULL, "break", "c", 7);
4614         }
4615       macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4616       return;
4617
4618     case M_DLA_AB:
4619       dbl = 1;
4620     case M_LA_AB:
4621       /* Load the address of a symbol into a register.  If breg is not
4622          zero, we then add a base register to it.  */
4623
4624       if (dbl && HAVE_32BIT_GPRS)
4625         as_warn (_("dla used to load 32-bit register"));
4626
4627       if (! dbl && HAVE_64BIT_OBJECTS)
4628         as_warn (_("la used to load 64-bit address"));
4629
4630       if (offset_expr.X_op == O_constant
4631           && offset_expr.X_add_number >= -0x8000
4632           && offset_expr.X_add_number < 0x8000)
4633         {
4634           macro_build (NULL, &icnt, &offset_expr,
4635                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4636                        "t,r,j", treg, sreg, BFD_RELOC_LO16);
4637           return;
4638         }
4639
4640       if (treg == breg)
4641         {
4642           tempreg = AT;
4643           used_at = 1;
4644         }
4645       else
4646         {
4647           tempreg = treg;
4648           used_at = 0;
4649         }
4650
4651       /* When generating embedded PIC code, we permit expressions of
4652          the form
4653            la   $treg,foo-bar
4654            la   $treg,foo-bar($breg)
4655          where bar is an address in the current section.  These are used
4656          when getting the addresses of functions.  We don't permit
4657          X_add_number to be non-zero, because if the symbol is
4658          external the relaxing code needs to know that any addend is
4659          purely the offset to X_op_symbol.  */
4660       if (mips_pic == EMBEDDED_PIC
4661           && offset_expr.X_op == O_subtract
4662           && (symbol_constant_p (offset_expr.X_op_symbol)
4663               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4664               : (symbol_equated_p (offset_expr.X_op_symbol)
4665                  && (S_GET_SEGMENT
4666                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4667                       ->X_add_symbol)
4668                      == now_seg)))
4669           && (offset_expr.X_add_number == 0
4670               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4671         {
4672           if (breg == 0)
4673             {
4674               tempreg = treg;
4675               used_at = 0;
4676               macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4677                            BFD_RELOC_PCREL_HI16_S);
4678             }
4679           else
4680             {
4681               macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4682                            BFD_RELOC_PCREL_HI16_S);
4683               macro_build (NULL, &icnt, NULL,
4684                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4685                            "d,v,t", tempreg, tempreg, breg);
4686             }
4687           macro_build (NULL, &icnt, &offset_expr,
4688                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4689                        "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
4690           if (! used_at)
4691             return;
4692           break;
4693         }
4694
4695       if (offset_expr.X_op != O_symbol
4696           && offset_expr.X_op != O_constant)
4697         {
4698           as_bad (_("expression too complex"));
4699           offset_expr.X_op = O_constant;
4700         }
4701
4702       if (offset_expr.X_op == O_constant)
4703         load_register (&icnt, tempreg, &offset_expr,
4704                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4705                         ? (dbl || HAVE_64BIT_ADDRESSES)
4706                         : HAVE_64BIT_ADDRESSES));
4707       else if (mips_pic == NO_PIC)
4708         {
4709           /* If this is a reference to a GP relative symbol, we want
4710                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4711              Otherwise we want
4712                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4713                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4714              If we have a constant, we need two instructions anyhow,
4715              so we may as well always use the latter form.
4716
4717             With 64bit address space and a usable $at we want
4718               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4719               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4720               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4721               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4722               dsll32    $tempreg,0
4723               daddu     $tempreg,$tempreg,$at
4724
4725             If $at is already in use, we use a path which is suboptimal
4726             on superscalar processors.
4727               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4728               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4729               dsll      $tempreg,16
4730               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4731               dsll      $tempreg,16
4732               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4733           */
4734           char *p = NULL;
4735           if (HAVE_64BIT_ADDRESSES)
4736             {
4737               /* We don't do GP optimization for now because RELAX_ENCODE can't
4738                  hold the data for such large chunks.  */
4739
4740               if (used_at == 0 && ! mips_opts.noat)
4741                 {
4742                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4743                                tempreg, BFD_RELOC_MIPS_HIGHEST);
4744                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4745                                AT, BFD_RELOC_HI16_S);
4746                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4747                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4748                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4749                                AT, AT, BFD_RELOC_LO16);
4750                   macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
4751                                tempreg, tempreg, 0);
4752                   macro_build (p, &icnt, NULL, "daddu", "d,v,t",
4753                                tempreg, tempreg, AT);
4754                   used_at = 1;
4755                 }
4756               else
4757                 {
4758                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4759                                tempreg, BFD_RELOC_MIPS_HIGHEST);
4760                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4761                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4762                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4763                                tempreg, tempreg, 16);
4764                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4765                                tempreg, tempreg, BFD_RELOC_HI16_S);
4766                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4767                                tempreg, tempreg, 16);
4768                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4769                                tempreg, tempreg, BFD_RELOC_LO16);
4770                 }
4771             }
4772           else
4773             {
4774               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4775                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4776                 {
4777                   frag_grow (20);
4778                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4779                                "t,r,j", tempreg, mips_gp_register,
4780                                BFD_RELOC_GPREL16);
4781                   p = frag_var (rs_machine_dependent, 8, 0,
4782                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4783                                               mips_opts.warn_about_macros),
4784                                 offset_expr.X_add_symbol, 0, NULL);
4785                 }
4786               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4787               if (p != NULL)
4788                 p += 4;
4789               macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4790                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4791             }
4792         }
4793       else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4794         {
4795           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4796
4797           /* If this is a reference to an external symbol, and there
4798              is no constant, we want
4799                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4800              or if tempreg is PIC_CALL_REG
4801                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4802              For a local symbol, we want
4803                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4804                nop
4805                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4806
4807              If we have a small constant, and this is a reference to
4808              an external symbol, we want
4809                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4810                nop
4811                addiu    $tempreg,$tempreg,<constant>
4812              For a local symbol, we want the same instruction
4813              sequence, but we output a BFD_RELOC_LO16 reloc on the
4814              addiu instruction.
4815
4816              If we have a large constant, and this is a reference to
4817              an external symbol, we want
4818                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4819                lui      $at,<hiconstant>
4820                addiu    $at,$at,<loconstant>
4821                addu     $tempreg,$tempreg,$at
4822              For a local symbol, we want the same instruction
4823              sequence, but we output a BFD_RELOC_LO16 reloc on the
4824              addiu instruction.
4825            */
4826
4827           expr1.X_add_number = offset_expr.X_add_number;
4828           offset_expr.X_add_number = 0;
4829           frag_grow (32);
4830           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4831             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4832           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
4833                        tempreg, lw_reloc_type, mips_gp_register);
4834           if (expr1.X_add_number == 0)
4835             {
4836               int off;
4837               char *p;
4838
4839               if (breg == 0)
4840                 off = 0;
4841               else
4842                 {
4843                   /* We're going to put in an addu instruction using
4844                      tempreg, so we may as well insert the nop right
4845                      now.  */
4846                   macro_build (NULL, &icnt, NULL, "nop", "");
4847                   off = 4;
4848                 }
4849               p = frag_var (rs_machine_dependent, 8 - off, 0,
4850                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4851                                           (breg == 0
4852                                            ? mips_opts.warn_about_macros
4853                                            : 0)),
4854                             offset_expr.X_add_symbol, 0, NULL);
4855               if (breg == 0)
4856                 {
4857                   macro_build (p, &icnt, NULL, "nop", "");
4858                   p += 4;
4859                 }
4860               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
4861                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4862               /* FIXME: If breg == 0, and the next instruction uses
4863                  $tempreg, then if this variant case is used an extra
4864                  nop will be generated.  */
4865             }
4866           else if (expr1.X_add_number >= -0x8000
4867                    && expr1.X_add_number < 0x8000)
4868             {
4869               macro_build (NULL, &icnt, NULL, "nop", "");
4870               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
4871                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4872               frag_var (rs_machine_dependent, 0, 0,
4873                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4874                         offset_expr.X_add_symbol, 0, NULL);
4875             }
4876           else
4877             {
4878               int off1;
4879
4880               /* If we are going to add in a base register, and the
4881                  target register and the base register are the same,
4882                  then we are using AT as a temporary register.  Since
4883                  we want to load the constant into AT, we add our
4884                  current AT (from the global offset table) and the
4885                  register into the register now, and pretend we were
4886                  not using a base register.  */
4887               if (breg != treg)
4888                 off1 = 0;
4889               else
4890                 {
4891                   macro_build (NULL, &icnt, NULL, "nop", "");
4892                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
4893                                treg, AT, breg);
4894                   breg = 0;
4895                   tempreg = treg;
4896                   off1 = -8;
4897                 }
4898
4899               /* Set mips_optimize around the lui instruction to avoid
4900                  inserting an unnecessary nop after the lw.  */
4901               hold_mips_optimize = mips_optimize;
4902               mips_optimize = 2;
4903               macro_build_lui (NULL, &icnt, &expr1, AT);
4904               mips_optimize = hold_mips_optimize;
4905
4906               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
4907                            AT, AT, BFD_RELOC_LO16);
4908               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
4909                            tempreg, tempreg, AT);
4910               frag_var (rs_machine_dependent, 0, 0,
4911                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4912                         offset_expr.X_add_symbol, 0, NULL);
4913               used_at = 1;
4914             }
4915         }
4916       else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
4917         {
4918           char *p = NULL;
4919           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4920           int adj = 0;
4921
4922           /* If this is a reference to an external, and there is no
4923              constant, or local symbol (*), with or without a
4924              constant, we want
4925                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4926              or if tempreg is PIC_CALL_REG
4927                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4928
4929              If we have a small constant, and this is a reference to
4930              an external symbol, we want
4931                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4932                addiu    $tempreg,$tempreg,<constant>
4933
4934              If we have a large constant, and this is a reference to
4935              an external symbol, we want
4936                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4937                lui      $at,<hiconstant>
4938                addiu    $at,$at,<loconstant>
4939                addu     $tempreg,$tempreg,$at
4940
4941              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
4942              local symbols, even though it introduces an additional
4943              instruction.  */
4944
4945           frag_grow (28);
4946           if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
4947             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4948           if (offset_expr.X_add_number)
4949             {
4950               frag_now->tc_frag_data.tc_fr_offset =
4951                 expr1.X_add_number = offset_expr.X_add_number;
4952               offset_expr.X_add_number = 0;
4953
4954               macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
4955                            "t,o(b)", tempreg, lw_reloc_type,
4956                            mips_gp_register);
4957
4958               if (expr1.X_add_number >= -0x8000
4959                   && expr1.X_add_number < 0x8000)
4960                 {
4961                   macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
4962                                "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4963                   p = frag_var (rs_machine_dependent, 4, 0,
4964                                 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
4965                                 offset_expr.X_add_symbol, 0, NULL);
4966                 }
4967               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
4968                 {
4969                   int dreg;
4970
4971                   /* If we are going to add in a base register, and the
4972                      target register and the base register are the same,
4973                      then we are using AT as a temporary register.  Since
4974                      we want to load the constant into AT, we add our
4975                      current AT (from the global offset table) and the
4976                      register into the register now, and pretend we were
4977                      not using a base register.  */
4978                   if (breg != treg)
4979                     dreg = tempreg;
4980                   else
4981                     {
4982                       assert (tempreg == AT);
4983                       macro_build (NULL, &icnt,NULL, ADDRESS_ADD_INSN,
4984                                    "d,v,t", treg, AT, breg);
4985                       dreg = treg;
4986                       adj = 4;
4987                     }
4988
4989                   macro_build_lui (NULL, &icnt, &expr1, AT);
4990                   macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
4991                                "t,r,j", AT, AT, BFD_RELOC_LO16);
4992                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
4993                                dreg, dreg, AT);
4994
4995                   p = frag_var (rs_machine_dependent, 4 + adj, 0,
4996                                 RELAX_ENCODE (16 + adj, 4 + adj,
4997                                               0, 0, 0, 0),
4998                                 offset_expr.X_add_symbol, 0, NULL);
4999
5000                   used_at = 1;
5001                 }
5002               else
5003                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5004
5005               offset_expr.X_add_number = expr1.X_add_number;
5006
5007               macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5008                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_DISP,
5009                            mips_gp_register);
5010               if (adj)
5011                 {
5012                   macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5013                                treg, tempreg, breg);
5014                   breg = 0;
5015                   tempreg = treg;
5016                 }
5017             }
5018           else
5019             {
5020               macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5021                            "t,o(b)", tempreg, lw_reloc_type,
5022                            mips_gp_register);
5023               if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5024                 p = frag_var (rs_machine_dependent, 0, 0,
5025                               RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5026                               offset_expr.X_add_symbol, 0, NULL);
5027             }
5028
5029           if (! p)
5030             {
5031               /* To avoid confusion in tc_gen_reloc, we must ensure
5032                  that this does not become a variant frag.  */
5033               frag_wane (frag_now);
5034               frag_new (0);
5035             }
5036         }
5037       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5038         {
5039           int gpdel;
5040           char *p;
5041           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5042           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5043           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5044
5045           /* This is the large GOT case.  If this is a reference to an
5046              external symbol, and there is no constant, we want
5047                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5048                addu     $tempreg,$tempreg,$gp
5049                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5050              or if tempreg is PIC_CALL_REG
5051                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5052                addu     $tempreg,$tempreg,$gp
5053                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5054              For a local symbol, we want
5055                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5056                nop
5057                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5058
5059              If we have a small constant, and this is a reference to
5060              an external symbol, we want
5061                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5062                addu     $tempreg,$tempreg,$gp
5063                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5064                nop
5065                addiu    $tempreg,$tempreg,<constant>
5066              For a local symbol, we want
5067                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5068                nop
5069                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5070
5071              If we have a large constant, and this is a reference to
5072              an external symbol, we want
5073                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5074                addu     $tempreg,$tempreg,$gp
5075                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5076                lui      $at,<hiconstant>
5077                addiu    $at,$at,<loconstant>
5078                addu     $tempreg,$tempreg,$at
5079              For a local symbol, we want
5080                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5081                lui      $at,<hiconstant>
5082                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5083                addu     $tempreg,$tempreg,$at
5084           */
5085
5086           expr1.X_add_number = offset_expr.X_add_number;
5087           offset_expr.X_add_number = 0;
5088           frag_grow (52);
5089           if (reg_needs_delay (mips_gp_register))
5090             gpdel = 4;
5091           else
5092             gpdel = 0;
5093           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5094             {
5095               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5096               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5097             }
5098           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5099                        tempreg, lui_reloc_type);
5100           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5101                        tempreg, tempreg, mips_gp_register);
5102           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5103                        tempreg, lw_reloc_type, tempreg);
5104           if (expr1.X_add_number == 0)
5105             {
5106               int off;
5107
5108               if (breg == 0)
5109                 off = 0;
5110               else
5111                 {
5112                   /* We're going to put in an addu instruction using
5113                      tempreg, so we may as well insert the nop right
5114                      now.  */
5115                   macro_build (NULL, &icnt, NULL, "nop", "");
5116                   off = 4;
5117                 }
5118
5119               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5120                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5121                                           8 + gpdel, 0,
5122                                           (breg == 0
5123                                            ? mips_opts.warn_about_macros
5124                                            : 0)),
5125                             offset_expr.X_add_symbol, 0, NULL);
5126             }
5127           else if (expr1.X_add_number >= -0x8000
5128                    && expr1.X_add_number < 0x8000)
5129             {
5130               macro_build (NULL, &icnt, NULL, "nop", "");
5131               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5132                            tempreg, tempreg, BFD_RELOC_LO16);
5133
5134               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5135                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5136                                           (breg == 0
5137                                            ? mips_opts.warn_about_macros
5138                                            : 0)),
5139                             offset_expr.X_add_symbol, 0, NULL);
5140             }
5141           else
5142             {
5143               int adj, dreg;
5144
5145               /* If we are going to add in a base register, and the
5146                  target register and the base register are the same,
5147                  then we are using AT as a temporary register.  Since
5148                  we want to load the constant into AT, we add our
5149                  current AT (from the global offset table) and the
5150                  register into the register now, and pretend we were
5151                  not using a base register.  */
5152               if (breg != treg)
5153                 {
5154                   adj = 0;
5155                   dreg = tempreg;
5156                 }
5157               else
5158                 {
5159                   assert (tempreg == AT);
5160                   macro_build (NULL, &icnt, NULL, "nop", "");
5161                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5162                                treg, AT, breg);
5163                   dreg = treg;
5164                   adj = 8;
5165                 }
5166
5167               /* Set mips_optimize around the lui instruction to avoid
5168                  inserting an unnecessary nop after the lw.  */
5169               hold_mips_optimize = mips_optimize;
5170               mips_optimize = 2;
5171               macro_build_lui (NULL, &icnt, &expr1, AT);
5172               mips_optimize = hold_mips_optimize;
5173
5174               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5175                            AT, AT, BFD_RELOC_LO16);
5176               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5177                            dreg, dreg, AT);
5178
5179               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5180                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5181                                           8 + gpdel, 0,
5182                                           (breg == 0
5183                                            ? mips_opts.warn_about_macros
5184                                            : 0)),
5185                             offset_expr.X_add_symbol, 0, NULL);
5186
5187               used_at = 1;
5188             }
5189
5190           if (gpdel > 0)
5191             {
5192               /* This is needed because this instruction uses $gp, but
5193                  the first instruction on the main stream does not.  */
5194               macro_build (p, &icnt, NULL, "nop", "");
5195               p += 4;
5196             }
5197
5198           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5199                        tempreg, local_reloc_type, mips_gp_register);
5200           p += 4;
5201           if (expr1.X_add_number >= -0x8000
5202               && expr1.X_add_number < 0x8000)
5203             {
5204               macro_build (p, &icnt, NULL, "nop", "");
5205               p += 4;
5206               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5207                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5208               /* FIXME: If add_number is 0, and there was no base
5209                  register, the external symbol case ended with a load,
5210                  so if the symbol turns out to not be external, and
5211                  the next instruction uses tempreg, an unnecessary nop
5212                  will be inserted.  */
5213             }
5214           else
5215             {
5216               if (breg == treg)
5217                 {
5218                   /* We must add in the base register now, as in the
5219                      external symbol case.  */
5220                   assert (tempreg == AT);
5221                   macro_build (p, &icnt, NULL, "nop", "");
5222                   p += 4;
5223                   macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5224                                treg, AT, breg);
5225                   p += 4;
5226                   tempreg = treg;
5227                   /* We set breg to 0 because we have arranged to add
5228                      it in in both cases.  */
5229                   breg = 0;
5230                 }
5231
5232               macro_build_lui (p, &icnt, &expr1, AT);
5233               p += 4;
5234               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5235                            AT, AT, BFD_RELOC_LO16);
5236               p += 4;
5237               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5238                            tempreg, tempreg, AT);
5239               p += 4;
5240             }
5241         }
5242       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5243         {
5244           char *p = NULL;
5245           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5246           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5247           int adj = 0;
5248
5249           /* This is the large GOT case.  If this is a reference to an
5250              external symbol, and there is no constant, we want
5251                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5252                add      $tempreg,$tempreg,$gp
5253                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5254              or if tempreg is PIC_CALL_REG
5255                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5256                add      $tempreg,$tempreg,$gp
5257                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5258
5259              If we have a small constant, and this is a reference to
5260              an external symbol, we want
5261                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5262                add      $tempreg,$tempreg,$gp
5263                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5264                addi     $tempreg,$tempreg,<constant>
5265
5266              If we have a large constant, and this is a reference to
5267              an external symbol, we want
5268                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5269                addu     $tempreg,$tempreg,$gp
5270                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5271                lui      $at,<hiconstant>
5272                addi     $at,$at,<loconstant>
5273                add      $tempreg,$tempreg,$at
5274
5275              If we have NewABI, and we know it's a local symbol, we want
5276                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
5277                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
5278              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
5279
5280           frag_grow (40);
5281
5282           frag_now->tc_frag_data.tc_fr_offset =
5283             expr1.X_add_number = offset_expr.X_add_number;
5284           offset_expr.X_add_number = 0;
5285
5286           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5287             {
5288               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5289               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5290             }
5291           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5292                        tempreg, lui_reloc_type);
5293           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5294                        tempreg, tempreg, mips_gp_register);
5295           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5296                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5297
5298           if (expr1.X_add_number == 0)
5299             {
5300               p = frag_var (rs_machine_dependent, 8, 0,
5301                             RELAX_ENCODE (12, 8, 0, 4, 0,
5302                                           mips_opts.warn_about_macros),
5303                             offset_expr.X_add_symbol, 0, NULL);
5304             }
5305           else if (expr1.X_add_number >= -0x8000
5306                    && expr1.X_add_number < 0x8000)
5307             {
5308               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5309                            tempreg, tempreg, BFD_RELOC_LO16);
5310               p = frag_var (rs_machine_dependent, 8, 0,
5311                             RELAX_ENCODE (16, 8, 0, 4, 0,
5312                                           mips_opts.warn_about_macros),
5313                             offset_expr.X_add_symbol, 0, NULL);
5314             }
5315           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5316             {
5317               int dreg;
5318
5319               /* If we are going to add in a base register, and the
5320                  target register and the base register are the same,
5321                  then we are using AT as a temporary register.  Since
5322                  we want to load the constant into AT, we add our
5323                  current AT (from the global offset table) and the
5324                  register into the register now, and pretend we were
5325                  not using a base register.  */
5326               if (breg != treg)
5327                 dreg = tempreg;
5328               else
5329                 {
5330                   assert (tempreg == AT);
5331                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5332                                treg, AT, breg);
5333                   dreg = treg;
5334                   adj = 4;
5335                 }
5336
5337               /* Set mips_optimize around the lui instruction to avoid
5338                  inserting an unnecessary nop after the lw.  */
5339               macro_build_lui (NULL, &icnt, &expr1, AT);
5340               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5341                            "t,r,j", AT, AT, BFD_RELOC_LO16);
5342               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5343                            dreg, dreg, AT);
5344
5345               p = frag_var (rs_machine_dependent, 8 + adj, 0,
5346                             RELAX_ENCODE (24 + adj, 8 + adj,
5347                                           0, 4, 0,
5348                                           (breg == 0
5349                                            ? mips_opts.warn_about_macros
5350                                            : 0)),
5351                             offset_expr.X_add_symbol, 0, NULL);
5352
5353               used_at = 1;
5354             }
5355           else
5356             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5357
5358           offset_expr.X_add_number = expr1.X_add_number;
5359           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5360                        tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5361           macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5362                        tempreg, tempreg, BFD_RELOC_MIPS_GOT_OFST);
5363           if (adj)
5364             {
5365               macro_build (p + 8, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5366                            treg, tempreg, breg);
5367               breg = 0;
5368               tempreg = treg;
5369             }
5370         }
5371       else if (mips_pic == EMBEDDED_PIC)
5372         {
5373           /* We use
5374                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5375              */
5376           macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5377                        tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5378         }
5379       else
5380         abort ();
5381
5382       if (breg != 0)
5383         {
5384           char *s;
5385
5386           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5387             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5388           else
5389             s = ADDRESS_ADD_INSN;
5390
5391           macro_build (NULL, &icnt, NULL, s, "d,v,t", treg, tempreg, breg);
5392         }
5393
5394       if (! used_at)
5395         return;
5396
5397       break;
5398
5399     case M_J_A:
5400       /* The j instruction may not be used in PIC code, since it
5401          requires an absolute address.  We convert it to a b
5402          instruction.  */
5403       if (mips_pic == NO_PIC)
5404         macro_build (NULL, &icnt, &offset_expr, "j", "a");
5405       else
5406         macro_build (NULL, &icnt, &offset_expr, "b", "p");
5407       return;
5408
5409       /* The jal instructions must be handled as macros because when
5410          generating PIC code they expand to multi-instruction
5411          sequences.  Normally they are simple instructions.  */
5412     case M_JAL_1:
5413       dreg = RA;
5414       /* Fall through.  */
5415     case M_JAL_2:
5416       if (mips_pic == NO_PIC
5417           || mips_pic == EMBEDDED_PIC)
5418         macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5419       else if (mips_pic == SVR4_PIC)
5420         {
5421           if (sreg != PIC_CALL_REG)
5422             as_warn (_("MIPS PIC call to register other than $25"));
5423
5424           macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5425           if (! HAVE_NEWABI)
5426             {
5427               if (mips_cprestore_offset < 0)
5428                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5429               else
5430                 {
5431                   if (! mips_frame_reg_valid)
5432                     {
5433                       as_warn (_("No .frame pseudo-op used in PIC code"));
5434                       /* Quiet this warning.  */
5435                       mips_frame_reg_valid = 1;
5436                     }
5437                   if (! mips_cprestore_valid)
5438                     {
5439                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5440                       /* Quiet this warning.  */
5441                       mips_cprestore_valid = 1;
5442                     }
5443                   expr1.X_add_number = mips_cprestore_offset;
5444                   macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5445                                                 ADDRESS_LOAD_INSN,
5446                                                 mips_gp_register,
5447                                                 mips_frame_reg,
5448                                                 HAVE_64BIT_ADDRESSES);
5449                 }
5450             }
5451         }
5452       else
5453         abort ();
5454
5455       return;
5456
5457     case M_JAL_A:
5458       if (mips_pic == NO_PIC)
5459         macro_build (NULL, &icnt, &offset_expr, "jal", "a");
5460       else if (mips_pic == SVR4_PIC)
5461         {
5462           char *p;
5463
5464           /* If this is a reference to an external symbol, and we are
5465              using a small GOT, we want
5466                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5467                nop
5468                jalr     $ra,$25
5469                nop
5470                lw       $gp,cprestore($sp)
5471              The cprestore value is set using the .cprestore
5472              pseudo-op.  If we are using a big GOT, we want
5473                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5474                addu     $25,$25,$gp
5475                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5476                nop
5477                jalr     $ra,$25
5478                nop
5479                lw       $gp,cprestore($sp)
5480              If the symbol is not external, we want
5481                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5482                nop
5483                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5484                jalr     $ra,$25
5485                nop
5486                lw $gp,cprestore($sp)
5487
5488              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5489              sequences above, minus nops, unless the symbol is local,
5490              which enables us to use GOT_PAGE/GOT_OFST (big got) or
5491              GOT_DISP.  */
5492           if (HAVE_NEWABI)
5493             {
5494               if (! mips_big_got)
5495                 {
5496                   frag_grow (4);
5497                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5498                                "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5499                                mips_gp_register);
5500                   frag_var (rs_machine_dependent, 0, 0,
5501                             RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5502                             offset_expr.X_add_symbol, 0, NULL);
5503                 }
5504               else
5505                 {
5506                   frag_grow (20);
5507                   macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5508                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5509                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5510                                PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5511                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5512                                "t,o(b)", PIC_CALL_REG,
5513                                BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5514                   p = frag_var (rs_machine_dependent, 8, 0,
5515                                 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5516                                 offset_expr.X_add_symbol, 0, NULL);
5517                   macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5518                                "t,o(b)", PIC_CALL_REG,
5519                                BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5520                   macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5521                                "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5522                                BFD_RELOC_MIPS_GOT_OFST);
5523                 }
5524
5525               macro_build_jalr (icnt, &offset_expr);
5526             }
5527           else
5528             {
5529               frag_grow (40);
5530               if (! mips_big_got)
5531                 {
5532                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5533                                "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5534                                mips_gp_register);
5535                   macro_build (NULL, &icnt, NULL, "nop", "");
5536                   p = frag_var (rs_machine_dependent, 4, 0,
5537                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5538                                 offset_expr.X_add_symbol, 0, NULL);
5539                 }
5540               else
5541                 {
5542                   int gpdel;
5543
5544                   if (reg_needs_delay (mips_gp_register))
5545                     gpdel = 4;
5546                   else
5547                     gpdel = 0;
5548                   macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5549                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5550                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5551                                PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5552                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5553                                "t,o(b)", PIC_CALL_REG,
5554                                BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5555                   macro_build (NULL, &icnt, NULL, "nop", "");
5556                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5557                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5558                                               8 + gpdel, 0, 0),
5559                                 offset_expr.X_add_symbol, 0, NULL);
5560                   if (gpdel > 0)
5561                     {
5562                       macro_build (p, &icnt, NULL, "nop", "");
5563                       p += 4;
5564                     }
5565                   macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5566                                "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5567                                mips_gp_register);
5568                   p += 4;
5569                   macro_build (p, &icnt, NULL, "nop", "");
5570                   p += 4;
5571                 }
5572               macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5573                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5574                            BFD_RELOC_LO16);
5575               macro_build_jalr (icnt, &offset_expr);
5576
5577               if (mips_cprestore_offset < 0)
5578                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5579               else
5580                 {
5581                   if (! mips_frame_reg_valid)
5582                     {
5583                       as_warn (_("No .frame pseudo-op used in PIC code"));
5584                       /* Quiet this warning.  */
5585                       mips_frame_reg_valid = 1;
5586                     }
5587                   if (! mips_cprestore_valid)
5588                     {
5589                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5590                       /* Quiet this warning.  */
5591                       mips_cprestore_valid = 1;
5592                     }
5593                   if (mips_opts.noreorder)
5594                     macro_build (NULL, &icnt, NULL, "nop", "");
5595                   expr1.X_add_number = mips_cprestore_offset;
5596                   macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5597                                                 ADDRESS_LOAD_INSN,
5598                                                 mips_gp_register,
5599                                                 mips_frame_reg,
5600                                                 HAVE_64BIT_ADDRESSES);
5601                 }
5602             }
5603         }
5604       else if (mips_pic == EMBEDDED_PIC)
5605         {
5606           macro_build (NULL, &icnt, &offset_expr, "bal", "p");
5607           /* The linker may expand the call to a longer sequence which
5608              uses $at, so we must break rather than return.  */
5609           break;
5610         }
5611       else
5612         abort ();
5613
5614       return;
5615
5616     case M_LB_AB:
5617       s = "lb";
5618       goto ld;
5619     case M_LBU_AB:
5620       s = "lbu";
5621       goto ld;
5622     case M_LH_AB:
5623       s = "lh";
5624       goto ld;
5625     case M_LHU_AB:
5626       s = "lhu";
5627       goto ld;
5628     case M_LW_AB:
5629       s = "lw";
5630       goto ld;
5631     case M_LWC0_AB:
5632       s = "lwc0";
5633       /* Itbl support may require additional care here.  */
5634       coproc = 1;
5635       goto ld;
5636     case M_LWC1_AB:
5637       s = "lwc1";
5638       /* Itbl support may require additional care here.  */
5639       coproc = 1;
5640       goto ld;
5641     case M_LWC2_AB:
5642       s = "lwc2";
5643       /* Itbl support may require additional care here.  */
5644       coproc = 1;
5645       goto ld;
5646     case M_LWC3_AB:
5647       s = "lwc3";
5648       /* Itbl support may require additional care here.  */
5649       coproc = 1;
5650       goto ld;
5651     case M_LWL_AB:
5652       s = "lwl";
5653       lr = 1;
5654       goto ld;
5655     case M_LWR_AB:
5656       s = "lwr";
5657       lr = 1;
5658       goto ld;
5659     case M_LDC1_AB:
5660       if (mips_opts.arch == CPU_R4650)
5661         {
5662           as_bad (_("opcode not supported on this processor"));
5663           return;
5664         }
5665       s = "ldc1";
5666       /* Itbl support may require additional care here.  */
5667       coproc = 1;
5668       goto ld;
5669     case M_LDC2_AB:
5670       s = "ldc2";
5671       /* Itbl support may require additional care here.  */
5672       coproc = 1;
5673       goto ld;
5674     case M_LDC3_AB:
5675       s = "ldc3";
5676       /* Itbl support may require additional care here.  */
5677       coproc = 1;
5678       goto ld;
5679     case M_LDL_AB:
5680       s = "ldl";
5681       lr = 1;
5682       goto ld;
5683     case M_LDR_AB:
5684       s = "ldr";
5685       lr = 1;
5686       goto ld;
5687     case M_LL_AB:
5688       s = "ll";
5689       goto ld;
5690     case M_LLD_AB:
5691       s = "lld";
5692       goto ld;
5693     case M_LWU_AB:
5694       s = "lwu";
5695     ld:
5696       if (breg == treg || coproc || lr)
5697         {
5698           tempreg = AT;
5699           used_at = 1;
5700         }
5701       else
5702         {
5703           tempreg = treg;
5704           used_at = 0;
5705         }
5706       goto ld_st;
5707     case M_SB_AB:
5708       s = "sb";
5709       goto st;
5710     case M_SH_AB:
5711       s = "sh";
5712       goto st;
5713     case M_SW_AB:
5714       s = "sw";
5715       goto st;
5716     case M_SWC0_AB:
5717       s = "swc0";
5718       /* Itbl support may require additional care here.  */
5719       coproc = 1;
5720       goto st;
5721     case M_SWC1_AB:
5722       s = "swc1";
5723       /* Itbl support may require additional care here.  */
5724       coproc = 1;
5725       goto st;
5726     case M_SWC2_AB:
5727       s = "swc2";
5728       /* Itbl support may require additional care here.  */
5729       coproc = 1;
5730       goto st;
5731     case M_SWC3_AB:
5732       s = "swc3";
5733       /* Itbl support may require additional care here.  */
5734       coproc = 1;
5735       goto st;
5736     case M_SWL_AB:
5737       s = "swl";
5738       goto st;
5739     case M_SWR_AB:
5740       s = "swr";
5741       goto st;
5742     case M_SC_AB:
5743       s = "sc";
5744       goto st;
5745     case M_SCD_AB:
5746       s = "scd";
5747       goto st;
5748     case M_SDC1_AB:
5749       if (mips_opts.arch == CPU_R4650)
5750         {
5751           as_bad (_("opcode not supported on this processor"));
5752           return;
5753         }
5754       s = "sdc1";
5755       coproc = 1;
5756       /* Itbl support may require additional care here.  */
5757       goto st;
5758     case M_SDC2_AB:
5759       s = "sdc2";
5760       /* Itbl support may require additional care here.  */
5761       coproc = 1;
5762       goto st;
5763     case M_SDC3_AB:
5764       s = "sdc3";
5765       /* Itbl support may require additional care here.  */
5766       coproc = 1;
5767       goto st;
5768     case M_SDL_AB:
5769       s = "sdl";
5770       goto st;
5771     case M_SDR_AB:
5772       s = "sdr";
5773     st:
5774       tempreg = AT;
5775       used_at = 1;
5776     ld_st:
5777       /* Itbl support may require additional care here.  */
5778       if (mask == M_LWC1_AB
5779           || mask == M_SWC1_AB
5780           || mask == M_LDC1_AB
5781           || mask == M_SDC1_AB
5782           || mask == M_L_DAB
5783           || mask == M_S_DAB)
5784         fmt = "T,o(b)";
5785       else if (coproc)
5786         fmt = "E,o(b)";
5787       else
5788         fmt = "t,o(b)";
5789
5790       /* Sign-extending 32-bit constants makes their handling easier.
5791          The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5792          described below.  */
5793       if ((! HAVE_64BIT_ADDRESSES
5794            && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
5795           && (offset_expr.X_op == O_constant))
5796         offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5797                                      ^ 0x80000000) - 0x80000000);
5798
5799       /* For embedded PIC, we allow loads where the offset is calculated
5800          by subtracting a symbol in the current segment from an unknown
5801          symbol, relative to a base register, e.g.:
5802                 <op>    $treg, <sym>-<localsym>($breg)
5803          This is used by the compiler for switch statements.  */
5804       if (mips_pic == EMBEDDED_PIC
5805           && offset_expr.X_op == O_subtract
5806           && (symbol_constant_p (offset_expr.X_op_symbol)
5807               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5808               : (symbol_equated_p (offset_expr.X_op_symbol)
5809                  && (S_GET_SEGMENT
5810                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5811                       ->X_add_symbol)
5812                      == now_seg)))
5813           && breg != 0
5814           && (offset_expr.X_add_number == 0
5815               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5816         {
5817           /* For this case, we output the instructions:
5818                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5819                 addiu   $tempreg,$tempreg,$breg
5820                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5821              If the relocation would fit entirely in 16 bits, it would be
5822              nice to emit:
5823                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5824              instead, but that seems quite difficult.  */
5825           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
5826                        BFD_RELOC_PCREL_HI16_S);
5827           macro_build (NULL, &icnt, NULL,
5828                        ((bfd_arch_bits_per_address (stdoutput) == 32
5829                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5830                         ? "addu" : "daddu"),
5831                        "d,v,t", tempreg, tempreg, breg);
5832           macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
5833                        BFD_RELOC_PCREL_LO16, tempreg);
5834           if (! used_at)
5835             return;
5836           break;
5837         }
5838
5839       if (offset_expr.X_op != O_constant
5840           && offset_expr.X_op != O_symbol)
5841         {
5842           as_bad (_("expression too complex"));
5843           offset_expr.X_op = O_constant;
5844         }
5845
5846       /* A constant expression in PIC code can be handled just as it
5847          is in non PIC code.  */
5848       if (mips_pic == NO_PIC
5849           || offset_expr.X_op == O_constant)
5850         {
5851           char *p;
5852
5853           /* If this is a reference to a GP relative symbol, and there
5854              is no base register, we want
5855                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5856              Otherwise, if there is no base register, we want
5857                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5858                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5859              If we have a constant, we need two instructions anyhow,
5860              so we always use the latter form.
5861
5862              If we have a base register, and this is a reference to a
5863              GP relative symbol, we want
5864                addu     $tempreg,$breg,$gp
5865                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5866              Otherwise we want
5867                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5868                addu     $tempreg,$tempreg,$breg
5869                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5870              With a constant we always use the latter case.
5871
5872              With 64bit address space and no base register and $at usable,
5873              we want
5874                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5875                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5876                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5877                dsll32   $tempreg,0
5878                daddu    $tempreg,$at
5879                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5880              If we have a base register, we want
5881                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5882                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5883                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5884                daddu    $at,$breg
5885                dsll32   $tempreg,0
5886                daddu    $tempreg,$at
5887                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5888
5889              Without $at we can't generate the optimal path for superscalar
5890              processors here since this would require two temporary registers.
5891                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5892                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5893                dsll     $tempreg,16
5894                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5895                dsll     $tempreg,16
5896                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5897              If we have a base register, we want
5898                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5899                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5900                dsll     $tempreg,16
5901                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5902                dsll     $tempreg,16
5903                daddu    $tempreg,$tempreg,$breg
5904                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5905
5906              If we have 64-bit addresses, as an optimization, for
5907              addresses which are 32-bit constants (e.g. kseg0/kseg1
5908              addresses) we fall back to the 32-bit address generation
5909              mechanism since it is more efficient.  Note that due to
5910              the signed offset used by memory operations, the 32-bit
5911              range is shifted down by 32768 here.  This code should
5912              probably attempt to generate 64-bit constants more
5913              efficiently in general.
5914
5915              As an extension for architectures with 64-bit registers,
5916              we don't truncate 64-bit addresses given as literal
5917              constants down to 32 bits, to support existing practice
5918              in the mips64 Linux (the kernel), that compiles source
5919              files with -mabi=64, assembling them as o32 or n32 (with
5920              -Wa,-32 or -Wa,-n32).  This is not beautiful, but since
5921              the whole kernel is loaded into a memory region that is
5922              addressible with sign-extended 32-bit addresses, it is
5923              wasteful to compute the upper 32 bits of every
5924              non-literal address, that takes more space and time.
5925              Some day this should probably be implemented as an
5926              assembler option, such that the kernel doesn't have to
5927              use such ugly hacks, even though it will still have to
5928              end up converting the binary to ELF32 for a number of
5929              platforms whose boot loaders don't support ELF64
5930              binaries.  */
5931           if ((HAVE_64BIT_ADDRESSES
5932                && ! (offset_expr.X_op == O_constant
5933                      && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5934               || (HAVE_64BIT_GPRS
5935                   && offset_expr.X_op == O_constant
5936                   && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5937             {
5938               p = NULL;
5939
5940               /* We don't do GP optimization for now because RELAX_ENCODE can't
5941                  hold the data for such large chunks.  */
5942
5943               if (used_at == 0 && ! mips_opts.noat)
5944                 {
5945                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5946                                tempreg, BFD_RELOC_MIPS_HIGHEST);
5947                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5948                                AT, BFD_RELOC_HI16_S);
5949                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5950                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5951                   if (breg != 0)
5952                     macro_build (p, &icnt, NULL, "daddu", "d,v,t",
5953                                  AT, AT, breg);
5954                   macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
5955                                tempreg, tempreg, 0);
5956                   macro_build (p, &icnt, NULL, "daddu", "d,v,t",
5957                                tempreg, tempreg, AT);
5958                   macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5959                                BFD_RELOC_LO16, tempreg);
5960                   used_at = 1;
5961                 }
5962               else
5963                 {
5964                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5965                                tempreg, BFD_RELOC_MIPS_HIGHEST);
5966                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5967                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5968                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
5969                                tempreg, tempreg, 16);
5970                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5971                                tempreg, tempreg, BFD_RELOC_HI16_S);
5972                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
5973                                tempreg, tempreg, 16);
5974                   if (breg != 0)
5975                     macro_build (p, &icnt, NULL, "daddu", "d,v,t",
5976                                  tempreg, tempreg, breg);
5977                   macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5978                                BFD_RELOC_LO16, tempreg);
5979                 }
5980
5981               return;
5982             }
5983
5984           if (offset_expr.X_op == O_constant
5985               && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5986             as_bad (_("load/store address overflow (max 32 bits)"));
5987
5988           if (breg == 0)
5989             {
5990               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5991                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5992                 p = NULL;
5993               else
5994                 {
5995                   frag_grow (20);
5996                   macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
5997                                BFD_RELOC_GPREL16, mips_gp_register);
5998                   p = frag_var (rs_machine_dependent, 8, 0,
5999                                 RELAX_ENCODE (4, 8, 0, 4, 0,
6000                                               (mips_opts.warn_about_macros
6001                                                || (used_at
6002                                                    && mips_opts.noat))),
6003                                 offset_expr.X_add_symbol, 0, NULL);
6004                   used_at = 0;
6005                 }
6006               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6007               if (p != NULL)
6008                 p += 4;
6009               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6010                            BFD_RELOC_LO16, tempreg);
6011             }
6012           else
6013             {
6014               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6015                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6016                 p = NULL;
6017               else
6018                 {
6019                   frag_grow (28);
6020                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6021                                tempreg, breg, mips_gp_register);
6022                   macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6023                                BFD_RELOC_GPREL16, tempreg);
6024                   p = frag_var (rs_machine_dependent, 12, 0,
6025                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6026                                 offset_expr.X_add_symbol, 0, NULL);
6027                 }
6028               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6029               if (p != NULL)
6030                 p += 4;
6031               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6032                            tempreg, tempreg, breg);
6033               if (p != NULL)
6034                 p += 4;
6035               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6036                            BFD_RELOC_LO16, tempreg);
6037             }
6038         }
6039       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6040         {
6041           char *p;
6042           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6043
6044           /* If this is a reference to an external symbol, we want
6045                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6046                nop
6047                <op>     $treg,0($tempreg)
6048              Otherwise we want
6049                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6050                nop
6051                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6052                <op>     $treg,0($tempreg)
6053
6054              For NewABI, we want
6055                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6056                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
6057
6058              If there is a base register, we add it to $tempreg before
6059              the <op>.  If there is a constant, we stick it in the
6060              <op> instruction.  We don't handle constants larger than
6061              16 bits, because we have no way to load the upper 16 bits
6062              (actually, we could handle them for the subset of cases
6063              in which we are not using $at).  */
6064           assert (offset_expr.X_op == O_symbol);
6065           if (HAVE_NEWABI)
6066             {
6067               macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6068                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6069                            mips_gp_register);
6070               if (breg != 0)
6071                 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6072                              tempreg, tempreg, breg);
6073               macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6074                            BFD_RELOC_MIPS_GOT_OFST, tempreg);
6075
6076               if (! used_at)
6077                 return;
6078
6079               break;
6080             }
6081           expr1.X_add_number = offset_expr.X_add_number;
6082           offset_expr.X_add_number = 0;
6083           if (expr1.X_add_number < -0x8000
6084               || expr1.X_add_number >= 0x8000)
6085             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6086           frag_grow (20);
6087           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6088                        tempreg, lw_reloc_type, mips_gp_register);
6089           macro_build (NULL, &icnt, NULL, "nop", "");
6090           p = frag_var (rs_machine_dependent, 4, 0,
6091                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6092                         offset_expr.X_add_symbol, 0, NULL);
6093           macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
6094                        "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
6095           if (breg != 0)
6096             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6097                          tempreg, tempreg, breg);
6098           macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6099                        tempreg);
6100         }
6101       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6102         {
6103           int gpdel;
6104           char *p;
6105
6106           /* If this is a reference to an external symbol, we want
6107                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6108                addu     $tempreg,$tempreg,$gp
6109                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6110                <op>     $treg,0($tempreg)
6111              Otherwise we want
6112                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6113                nop
6114                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6115                <op>     $treg,0($tempreg)
6116              If there is a base register, we add it to $tempreg before
6117              the <op>.  If there is a constant, we stick it in the
6118              <op> instruction.  We don't handle constants larger than
6119              16 bits, because we have no way to load the upper 16 bits
6120              (actually, we could handle them for the subset of cases
6121              in which we are not using $at).  */
6122           assert (offset_expr.X_op == O_symbol);
6123           expr1.X_add_number = offset_expr.X_add_number;
6124           offset_expr.X_add_number = 0;
6125           if (expr1.X_add_number < -0x8000
6126               || expr1.X_add_number >= 0x8000)
6127             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6128           if (reg_needs_delay (mips_gp_register))
6129             gpdel = 4;
6130           else
6131             gpdel = 0;
6132           frag_grow (36);
6133           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6134                        BFD_RELOC_MIPS_GOT_HI16);
6135           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6136                        tempreg, tempreg, mips_gp_register);
6137           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6138                        tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6139           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6140                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6141                         offset_expr.X_add_symbol, 0, NULL);
6142           if (gpdel > 0)
6143             {
6144               macro_build (p, &icnt, NULL, "nop", "");
6145               p += 4;
6146             }
6147           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6148                        tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6149           p += 4;
6150           macro_build (p, &icnt, NULL, "nop", "");
6151           p += 4;
6152           macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6153                        tempreg, tempreg, BFD_RELOC_LO16);
6154           if (breg != 0)
6155             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6156                          tempreg, tempreg, breg);
6157           macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6158                        tempreg);
6159         }
6160       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6161         {
6162           char *p;
6163           int bregsz = breg != 0 ? 4 : 0;
6164
6165           /* If this is a reference to an external symbol, we want
6166                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6167                add      $tempreg,$tempreg,$gp
6168                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6169                <op>     $treg,<ofst>($tempreg)
6170              Otherwise, for local symbols, we want:
6171                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6172                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
6173           assert (offset_expr.X_op == O_symbol);
6174           frag_now->tc_frag_data.tc_fr_offset =
6175             expr1.X_add_number = offset_expr.X_add_number;
6176           offset_expr.X_add_number = 0;
6177           if (expr1.X_add_number < -0x8000
6178               || expr1.X_add_number >= 0x8000)
6179             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6180           frag_grow (36);
6181           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6182                        BFD_RELOC_MIPS_GOT_HI16);
6183           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6184                        tempreg, tempreg, mips_gp_register);
6185           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6186                        tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6187           if (breg != 0)
6188             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6189                          tempreg, tempreg, breg);
6190           macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6191                        tempreg);
6192
6193           offset_expr.X_add_number = expr1.X_add_number;
6194           p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6195                         RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6196                                       0, 4 + bregsz, 0, 0),
6197                         offset_expr.X_add_symbol, 0, NULL);
6198           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6199                        tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6200           if (breg != 0)
6201             macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6202                          tempreg, tempreg, breg);
6203           macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6204                        BFD_RELOC_MIPS_GOT_OFST, tempreg);
6205         }
6206       else if (mips_pic == EMBEDDED_PIC)
6207         {
6208           /* If there is no base register, we want
6209                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6210              If there is a base register, we want
6211                addu     $tempreg,$breg,$gp
6212                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6213              */
6214           assert (offset_expr.X_op == O_symbol);
6215           if (breg == 0)
6216             {
6217               macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6218                            BFD_RELOC_GPREL16, mips_gp_register);
6219               used_at = 0;
6220             }
6221           else
6222             {
6223               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6224                            tempreg, breg, mips_gp_register);
6225               macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6226                            BFD_RELOC_GPREL16, tempreg);
6227             }
6228         }
6229       else
6230         abort ();
6231
6232       if (! used_at)
6233         return;
6234
6235       break;
6236
6237     case M_LI:
6238     case M_LI_S:
6239       load_register (&icnt, treg, &imm_expr, 0);
6240       return;
6241
6242     case M_DLI:
6243       load_register (&icnt, treg, &imm_expr, 1);
6244       return;
6245
6246     case M_LI_SS:
6247       if (imm_expr.X_op == O_constant)
6248         {
6249           load_register (&icnt, AT, &imm_expr, 0);
6250           macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6251           break;
6252         }
6253       else
6254         {
6255           assert (offset_expr.X_op == O_symbol
6256                   && strcmp (segment_name (S_GET_SEGMENT
6257                                            (offset_expr.X_add_symbol)),
6258                              ".lit4") == 0
6259                   && offset_expr.X_add_number == 0);
6260           macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)", treg,
6261                        BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6262           return;
6263         }
6264
6265     case M_LI_D:
6266       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6267          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6268          order 32 bits of the value and the low order 32 bits are either
6269          zero or in OFFSET_EXPR.  */
6270       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6271         {
6272           if (HAVE_64BIT_GPRS)
6273             load_register (&icnt, treg, &imm_expr, 1);
6274           else
6275             {
6276               int hreg, lreg;
6277
6278               if (target_big_endian)
6279                 {
6280                   hreg = treg;
6281                   lreg = treg + 1;
6282                 }
6283               else
6284                 {
6285                   hreg = treg + 1;
6286                   lreg = treg;
6287                 }
6288
6289               if (hreg <= 31)
6290                 load_register (&icnt, hreg, &imm_expr, 0);
6291               if (lreg <= 31)
6292                 {
6293                   if (offset_expr.X_op == O_absent)
6294                     move_register (&icnt, lreg, 0);
6295                   else
6296                     {
6297                       assert (offset_expr.X_op == O_constant);
6298                       load_register (&icnt, lreg, &offset_expr, 0);
6299                     }
6300                 }
6301             }
6302           return;
6303         }
6304
6305       /* We know that sym is in the .rdata section.  First we get the
6306          upper 16 bits of the address.  */
6307       if (mips_pic == NO_PIC)
6308         {
6309           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6310         }
6311       else if (mips_pic == SVR4_PIC)
6312         {
6313           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6314                        AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6315         }
6316       else if (mips_pic == EMBEDDED_PIC)
6317         {
6318           /* For embedded PIC we pick up the entire address off $gp in
6319              a single instruction.  */
6320           macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6321                        AT, mips_gp_register, BFD_RELOC_GPREL16);
6322           offset_expr.X_op = O_constant;
6323           offset_expr.X_add_number = 0;
6324         }
6325       else
6326         abort ();
6327
6328       /* Now we load the register(s).  */
6329       if (HAVE_64BIT_GPRS)
6330         macro_build (NULL, &icnt, &offset_expr, "ld", "t,o(b)", treg,
6331                      BFD_RELOC_LO16, AT);
6332       else
6333         {
6334           macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)", treg,
6335                        BFD_RELOC_LO16, AT);
6336           if (treg != RA)
6337             {
6338               /* FIXME: How in the world do we deal with the possible
6339                  overflow here?  */
6340               offset_expr.X_add_number += 4;
6341               macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6342                            treg + 1, BFD_RELOC_LO16, AT);
6343             }
6344         }
6345
6346       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6347          does not become a variant frag.  */
6348       frag_wane (frag_now);
6349       frag_new (0);
6350
6351       break;
6352
6353     case M_LI_DD:
6354       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6355          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6356          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6357          the value and the low order 32 bits are either zero or in
6358          OFFSET_EXPR.  */
6359       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6360         {
6361           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6362           if (HAVE_64BIT_FPRS)
6363             {
6364               assert (HAVE_64BIT_GPRS);
6365               macro_build (NULL, &icnt, NULL, "dmtc1", "t,S", AT, treg);
6366             }
6367           else
6368             {
6369               macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg + 1);
6370               if (offset_expr.X_op == O_absent)
6371                 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", 0, treg);
6372               else
6373                 {
6374                   assert (offset_expr.X_op == O_constant);
6375                   load_register (&icnt, AT, &offset_expr, 0);
6376                   macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6377                 }
6378             }
6379           break;
6380         }
6381
6382       assert (offset_expr.X_op == O_symbol
6383               && offset_expr.X_add_number == 0);
6384       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6385       if (strcmp (s, ".lit8") == 0)
6386         {
6387           if (mips_opts.isa != ISA_MIPS1)
6388             {
6389               macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6390                            BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6391               return;
6392             }
6393           breg = mips_gp_register;
6394           r = BFD_RELOC_MIPS_LITERAL;
6395           goto dob;
6396         }
6397       else
6398         {
6399           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6400           if (mips_pic == SVR4_PIC)
6401             macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6402                          "t,o(b)", AT, BFD_RELOC_MIPS_GOT16,
6403                          mips_gp_register);
6404           else
6405             {
6406               /* FIXME: This won't work for a 64 bit address.  */
6407               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6408             }
6409
6410           if (mips_opts.isa != ISA_MIPS1)
6411             {
6412               macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6413                            BFD_RELOC_LO16, AT);
6414
6415               /* To avoid confusion in tc_gen_reloc, we must ensure
6416                  that this does not become a variant frag.  */
6417               frag_wane (frag_now);
6418               frag_new (0);
6419
6420               break;
6421             }
6422           breg = AT;
6423           r = BFD_RELOC_LO16;
6424           goto dob;
6425         }
6426
6427     case M_L_DOB:
6428       if (mips_opts.arch == CPU_R4650)
6429         {
6430           as_bad (_("opcode not supported on this processor"));
6431           return;
6432         }
6433       /* Even on a big endian machine $fn comes before $fn+1.  We have
6434          to adjust when loading from memory.  */
6435       r = BFD_RELOC_LO16;
6436     dob:
6437       assert (mips_opts.isa == ISA_MIPS1);
6438       macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6439                    target_big_endian ? treg + 1 : treg, r, breg);
6440       /* FIXME: A possible overflow which I don't know how to deal
6441          with.  */
6442       offset_expr.X_add_number += 4;
6443       macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6444                    target_big_endian ? treg : treg + 1, r, breg);
6445
6446       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6447          does not become a variant frag.  */
6448       frag_wane (frag_now);
6449       frag_new (0);
6450
6451       if (breg != AT)
6452         return;
6453       break;
6454
6455     case M_L_DAB:
6456       /*
6457        * The MIPS assembler seems to check for X_add_number not
6458        * being double aligned and generating:
6459        *        lui     at,%hi(foo+1)
6460        *        addu    at,at,v1
6461        *        addiu   at,at,%lo(foo+1)
6462        *        lwc1    f2,0(at)
6463        *        lwc1    f3,4(at)
6464        * But, the resulting address is the same after relocation so why
6465        * generate the extra instruction?
6466        */
6467       if (mips_opts.arch == CPU_R4650)
6468         {
6469           as_bad (_("opcode not supported on this processor"));
6470           return;
6471         }
6472       /* Itbl support may require additional care here.  */
6473       coproc = 1;
6474       if (mips_opts.isa != ISA_MIPS1)
6475         {
6476           s = "ldc1";
6477           goto ld;
6478         }
6479
6480       s = "lwc1";
6481       fmt = "T,o(b)";
6482       goto ldd_std;
6483
6484     case M_S_DAB:
6485       if (mips_opts.arch == CPU_R4650)
6486         {
6487           as_bad (_("opcode not supported on this processor"));
6488           return;
6489         }
6490
6491       if (mips_opts.isa != ISA_MIPS1)
6492         {
6493           s = "sdc1";
6494           goto st;
6495         }
6496
6497       s = "swc1";
6498       fmt = "T,o(b)";
6499       /* Itbl support may require additional care here.  */
6500       coproc = 1;
6501       goto ldd_std;
6502
6503     case M_LD_AB:
6504       if (HAVE_64BIT_GPRS)
6505         {
6506           s = "ld";
6507           goto ld;
6508         }
6509
6510       s = "lw";
6511       fmt = "t,o(b)";
6512       goto ldd_std;
6513
6514     case M_SD_AB:
6515       if (HAVE_64BIT_GPRS)
6516         {
6517           s = "sd";
6518           goto st;
6519         }
6520
6521       s = "sw";
6522       fmt = "t,o(b)";
6523
6524     ldd_std:
6525       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6526          loads for the case of doing a pair of loads to simulate an 'ld'.
6527          This is not currently done by the compiler, and assembly coders
6528          writing embedded-pic code can cope.  */
6529
6530       if (offset_expr.X_op != O_symbol
6531           && offset_expr.X_op != O_constant)
6532         {
6533           as_bad (_("expression too complex"));
6534           offset_expr.X_op = O_constant;
6535         }
6536
6537       /* Even on a big endian machine $fn comes before $fn+1.  We have
6538          to adjust when loading from memory.  We set coproc if we must
6539          load $fn+1 first.  */
6540       /* Itbl support may require additional care here.  */
6541       if (! target_big_endian)
6542         coproc = 0;
6543
6544       if (mips_pic == NO_PIC
6545           || offset_expr.X_op == O_constant)
6546         {
6547           char *p;
6548
6549           /* If this is a reference to a GP relative symbol, we want
6550                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6551                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6552              If we have a base register, we use this
6553                addu     $at,$breg,$gp
6554                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6555                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6556              If this is not a GP relative symbol, we want
6557                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6558                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6559                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6560              If there is a base register, we add it to $at after the
6561              lui instruction.  If there is a constant, we always use
6562              the last case.  */
6563           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6564               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6565             {
6566               p = NULL;
6567               used_at = 1;
6568             }
6569           else
6570             {
6571               int off;
6572
6573               if (breg == 0)
6574                 {
6575                   frag_grow (28);
6576                   tempreg = mips_gp_register;
6577                   off = 0;
6578                   used_at = 0;
6579                 }
6580               else
6581                 {
6582                   frag_grow (36);
6583                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6584                                AT, breg, mips_gp_register);
6585                   tempreg = AT;
6586                   off = 4;
6587                   used_at = 1;
6588                 }
6589
6590               /* Itbl support may require additional care here.  */
6591               macro_build (NULL, &icnt, &offset_expr, s, fmt,
6592                            coproc ? treg + 1 : treg,
6593                            BFD_RELOC_GPREL16, tempreg);
6594               offset_expr.X_add_number += 4;
6595
6596               /* Set mips_optimize to 2 to avoid inserting an
6597                  undesired nop.  */
6598               hold_mips_optimize = mips_optimize;
6599               mips_optimize = 2;
6600               /* Itbl support may require additional care here.  */
6601               macro_build (NULL, &icnt, &offset_expr, s, fmt,
6602                            coproc ? treg : treg + 1,
6603                            BFD_RELOC_GPREL16, tempreg);
6604               mips_optimize = hold_mips_optimize;
6605
6606               p = frag_var (rs_machine_dependent, 12 + off, 0,
6607                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6608                                           used_at && mips_opts.noat),
6609                             offset_expr.X_add_symbol, 0, NULL);
6610
6611               /* We just generated two relocs.  When tc_gen_reloc
6612                  handles this case, it will skip the first reloc and
6613                  handle the second.  The second reloc already has an
6614                  extra addend of 4, which we added above.  We must
6615                  subtract it out, and then subtract another 4 to make
6616                  the first reloc come out right.  The second reloc
6617                  will come out right because we are going to add 4 to
6618                  offset_expr when we build its instruction below.
6619
6620                  If we have a symbol, then we don't want to include
6621                  the offset, because it will wind up being included
6622                  when we generate the reloc.  */
6623
6624               if (offset_expr.X_op == O_constant)
6625                 offset_expr.X_add_number -= 8;
6626               else
6627                 {
6628                   offset_expr.X_add_number = -4;
6629                   offset_expr.X_op = O_constant;
6630                 }
6631             }
6632           macro_build_lui (p, &icnt, &offset_expr, AT);
6633           if (p != NULL)
6634             p += 4;
6635           if (breg != 0)
6636             {
6637               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6638                            AT, breg, AT);
6639               if (p != NULL)
6640                 p += 4;
6641             }
6642           /* Itbl support may require additional care here.  */
6643           macro_build (p, &icnt, &offset_expr, s, fmt,
6644                        coproc ? treg + 1 : treg,
6645                        BFD_RELOC_LO16, AT);
6646           if (p != NULL)
6647             p += 4;
6648           /* FIXME: How do we handle overflow here?  */
6649           offset_expr.X_add_number += 4;
6650           /* Itbl support may require additional care here.  */
6651           macro_build (p, &icnt, &offset_expr, s, fmt,
6652                        coproc ? treg : treg + 1,
6653                        BFD_RELOC_LO16, AT);
6654         }
6655       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6656         {
6657           int off;
6658
6659           /* If this is a reference to an external symbol, we want
6660                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6661                nop
6662                <op>     $treg,0($at)
6663                <op>     $treg+1,4($at)
6664              Otherwise we want
6665                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6666                nop
6667                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6668                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6669              If there is a base register we add it to $at before the
6670              lwc1 instructions.  If there is a constant we include it
6671              in the lwc1 instructions.  */
6672           used_at = 1;
6673           expr1.X_add_number = offset_expr.X_add_number;
6674           offset_expr.X_add_number = 0;
6675           if (expr1.X_add_number < -0x8000
6676               || expr1.X_add_number >= 0x8000 - 4)
6677             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6678           if (breg == 0)
6679             off = 0;
6680           else
6681             off = 4;
6682           frag_grow (24 + off);
6683           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6684                        AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6685           macro_build (NULL, &icnt, NULL, "nop", "");
6686           if (breg != 0)
6687             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6688                          AT, breg, AT);
6689           /* Itbl support may require additional care here.  */
6690           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6691                        BFD_RELOC_LO16, AT);
6692           expr1.X_add_number += 4;
6693
6694           /* Set mips_optimize to 2 to avoid inserting an undesired
6695              nop.  */
6696           hold_mips_optimize = mips_optimize;
6697           mips_optimize = 2;
6698           /* Itbl support may require additional care here.  */
6699           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6700                        BFD_RELOC_LO16, AT);
6701           mips_optimize = hold_mips_optimize;
6702
6703           (void) frag_var (rs_machine_dependent, 0, 0,
6704                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6705                            offset_expr.X_add_symbol, 0, NULL);
6706         }
6707       else if (mips_pic == SVR4_PIC)
6708         {
6709           int gpdel, off;
6710           char *p;
6711
6712           /* If this is a reference to an external symbol, we want
6713                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6714                addu     $at,$at,$gp
6715                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6716                nop
6717                <op>     $treg,0($at)
6718                <op>     $treg+1,4($at)
6719              Otherwise we want
6720                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6721                nop
6722                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6723                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6724              If there is a base register we add it to $at before the
6725              lwc1 instructions.  If there is a constant we include it
6726              in the lwc1 instructions.  */
6727           used_at = 1;
6728           expr1.X_add_number = offset_expr.X_add_number;
6729           offset_expr.X_add_number = 0;
6730           if (expr1.X_add_number < -0x8000
6731               || expr1.X_add_number >= 0x8000 - 4)
6732             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6733           if (reg_needs_delay (mips_gp_register))
6734             gpdel = 4;
6735           else
6736             gpdel = 0;
6737           if (breg == 0)
6738             off = 0;
6739           else
6740             off = 4;
6741           frag_grow (56);
6742           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", AT,
6743                        BFD_RELOC_MIPS_GOT_HI16);
6744           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6745                        AT, AT, mips_gp_register);
6746           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6747                        AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6748           macro_build (NULL, &icnt, NULL, "nop", "");
6749           if (breg != 0)
6750             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6751                          AT, breg, AT);
6752           /* Itbl support may require additional care here.  */
6753           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6754                        BFD_RELOC_LO16, AT);
6755           expr1.X_add_number += 4;
6756
6757           /* Set mips_optimize to 2 to avoid inserting an undesired
6758              nop.  */
6759           hold_mips_optimize = mips_optimize;
6760           mips_optimize = 2;
6761           /* Itbl support may require additional care here.  */
6762           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6763                        BFD_RELOC_LO16, AT);
6764           mips_optimize = hold_mips_optimize;
6765           expr1.X_add_number -= 4;
6766
6767           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6768                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6769                                       8 + gpdel + off, 1, 0),
6770                         offset_expr.X_add_symbol, 0, NULL);
6771           if (gpdel > 0)
6772             {
6773               macro_build (p, &icnt, NULL, "nop", "");
6774               p += 4;
6775             }
6776           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6777                        AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6778           p += 4;
6779           macro_build (p, &icnt, NULL, "nop", "");
6780           p += 4;
6781           if (breg != 0)
6782             {
6783               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6784                            AT, breg, AT);
6785               p += 4;
6786             }
6787           /* Itbl support may require additional care here.  */
6788           macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6789                        BFD_RELOC_LO16, AT);
6790           p += 4;
6791           expr1.X_add_number += 4;
6792
6793           /* Set mips_optimize to 2 to avoid inserting an undesired
6794              nop.  */
6795           hold_mips_optimize = mips_optimize;
6796           mips_optimize = 2;
6797           /* Itbl support may require additional care here.  */
6798           macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6799                        BFD_RELOC_LO16, AT);
6800           mips_optimize = hold_mips_optimize;
6801         }
6802       else if (mips_pic == EMBEDDED_PIC)
6803         {
6804           /* If there is no base register, we use
6805                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6806                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6807              If we have a base register, we use
6808                addu     $at,$breg,$gp
6809                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6810                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6811              */
6812           if (breg == 0)
6813             {
6814               tempreg = mips_gp_register;
6815               used_at = 0;
6816             }
6817           else
6818             {
6819               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6820                            AT, breg, mips_gp_register);
6821               tempreg = AT;
6822               used_at = 1;
6823             }
6824
6825           /* Itbl support may require additional care here.  */
6826           macro_build (NULL, &icnt, &offset_expr, s, fmt,
6827                        coproc ? treg + 1 : treg,
6828                        BFD_RELOC_GPREL16, tempreg);
6829           offset_expr.X_add_number += 4;
6830           /* Itbl support may require additional care here.  */
6831           macro_build (NULL, &icnt, &offset_expr, s, fmt,
6832                        coproc ? treg : treg + 1,
6833                        BFD_RELOC_GPREL16, tempreg);
6834         }
6835       else
6836         abort ();
6837
6838       if (! used_at)
6839         return;
6840
6841       break;
6842
6843     case M_LD_OB:
6844       s = "lw";
6845       goto sd_ob;
6846     case M_SD_OB:
6847       s = "sw";
6848     sd_ob:
6849       assert (HAVE_32BIT_ADDRESSES);
6850       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6851                    BFD_RELOC_LO16, breg);
6852       offset_expr.X_add_number += 4;
6853       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6854                    BFD_RELOC_LO16, breg);
6855       return;
6856
6857    /* New code added to support COPZ instructions.
6858       This code builds table entries out of the macros in mip_opcodes.
6859       R4000 uses interlocks to handle coproc delays.
6860       Other chips (like the R3000) require nops to be inserted for delays.
6861
6862       FIXME: Currently, we require that the user handle delays.
6863       In order to fill delay slots for non-interlocked chips,
6864       we must have a way to specify delays based on the coprocessor.
6865       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6866       What are the side-effects of the cop instruction?
6867       What cache support might we have and what are its effects?
6868       Both coprocessor & memory require delays. how long???
6869       What registers are read/set/modified?
6870
6871       If an itbl is provided to interpret cop instructions,
6872       this knowledge can be encoded in the itbl spec.  */
6873
6874     case M_COP0:
6875       s = "c0";
6876       goto copz;
6877     case M_COP1:
6878       s = "c1";
6879       goto copz;
6880     case M_COP2:
6881       s = "c2";
6882       goto copz;
6883     case M_COP3:
6884       s = "c3";
6885     copz:
6886       /* For now we just do C (same as Cz).  The parameter will be
6887          stored in insn_opcode by mips_ip.  */
6888       macro_build (NULL, &icnt, NULL, s, "C", ip->insn_opcode);
6889       return;
6890
6891     case M_MOVE:
6892       move_register (&icnt, dreg, sreg);
6893       return;
6894
6895 #ifdef LOSING_COMPILER
6896     default:
6897       /* Try and see if this is a new itbl instruction.
6898          This code builds table entries out of the macros in mip_opcodes.
6899          FIXME: For now we just assemble the expression and pass it's
6900          value along as a 32-bit immediate.
6901          We may want to have the assembler assemble this value,
6902          so that we gain the assembler's knowledge of delay slots,
6903          symbols, etc.
6904          Would it be more efficient to use mask (id) here? */
6905       if (itbl_have_entries
6906           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6907         {
6908           s = ip->insn_mo->name;
6909           s2 = "cop3";
6910           coproc = ITBL_DECODE_PNUM (immed_expr);;
6911           macro_build (NULL, &icnt, &immed_expr, s, "C");
6912           return;
6913         }
6914       macro2 (ip);
6915       return;
6916     }
6917   if (mips_opts.noat)
6918     as_warn (_("Macro used $at after \".set noat\""));
6919 }
6920
6921 static void
6922 macro2 (struct mips_cl_insn *ip)
6923 {
6924   register int treg, sreg, dreg, breg;
6925   int tempreg;
6926   int mask;
6927   int icnt = 0;
6928   int used_at;
6929   expressionS expr1;
6930   const char *s;
6931   const char *s2;
6932   const char *fmt;
6933   int likely = 0;
6934   int dbl = 0;
6935   int coproc = 0;
6936   int lr = 0;
6937   int imm = 0;
6938   int off;
6939   offsetT maxnum;
6940   bfd_reloc_code_real_type r;
6941   char *p;
6942
6943   treg = (ip->insn_opcode >> 16) & 0x1f;
6944   dreg = (ip->insn_opcode >> 11) & 0x1f;
6945   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6946   mask = ip->insn_mo->mask;
6947
6948   expr1.X_op = O_constant;
6949   expr1.X_op_symbol = NULL;
6950   expr1.X_add_symbol = NULL;
6951   expr1.X_add_number = 1;
6952
6953   switch (mask)
6954     {
6955 #endif /* LOSING_COMPILER */
6956
6957     case M_DMUL:
6958       dbl = 1;
6959     case M_MUL:
6960       macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
6961                    sreg, treg);
6962       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
6963       return;
6964
6965     case M_DMUL_I:
6966       dbl = 1;
6967     case M_MUL_I:
6968       /* The MIPS assembler some times generates shifts and adds.  I'm
6969          not trying to be that fancy. GCC should do this for us
6970          anyway.  */
6971       load_register (&icnt, AT, &imm_expr, dbl);
6972       macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
6973                    sreg, AT);
6974       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
6975       break;
6976
6977     case M_DMULO_I:
6978       dbl = 1;
6979     case M_MULO_I:
6980       imm = 1;
6981       goto do_mulo;
6982
6983     case M_DMULO:
6984       dbl = 1;
6985     case M_MULO:
6986     do_mulo:
6987       mips_emit_delays (TRUE);
6988       ++mips_opts.noreorder;
6989       mips_any_noreorder = 1;
6990       if (imm)
6991         load_register (&icnt, AT, &imm_expr, dbl);
6992       macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
6993                    sreg, imm ? AT : treg);
6994       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
6995       macro_build (NULL, &icnt, NULL, dbl ? "dsra32" : "sra", "d,w,<",
6996                    dreg, dreg, RA);
6997       macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
6998       if (mips_trap)
6999         macro_build (NULL, &icnt, NULL, "tne", "s,t,q", dreg, AT, 6);
7000       else
7001         {
7002           expr1.X_add_number = 8;
7003           macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
7004           macro_build (NULL, &icnt, NULL, "nop", "", 0);
7005           macro_build (NULL, &icnt, NULL, "break", "c", 6);
7006         }
7007       --mips_opts.noreorder;
7008       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7009       break;
7010
7011     case M_DMULOU_I:
7012       dbl = 1;
7013     case M_MULOU_I:
7014       imm = 1;
7015       goto do_mulou;
7016
7017     case M_DMULOU:
7018       dbl = 1;
7019     case M_MULOU:
7020     do_mulou:
7021       mips_emit_delays (TRUE);
7022       ++mips_opts.noreorder;
7023       mips_any_noreorder = 1;
7024       if (imm)
7025         load_register (&icnt, AT, &imm_expr, dbl);
7026       macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7027                    sreg, imm ? AT : treg);
7028       macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7029       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7030       if (mips_trap)
7031         macro_build (NULL, &icnt, NULL, "tne", "s,t,q", AT, 0, 6);
7032       else
7033         {
7034           expr1.X_add_number = 8;
7035           macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7036           macro_build (NULL, &icnt, NULL, "nop", "", 0);
7037           macro_build (NULL, &icnt, NULL, "break", "c", 6);
7038         }
7039       --mips_opts.noreorder;
7040       break;
7041
7042     case M_DROL:
7043       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7044         {
7045           if (dreg == sreg)
7046             {
7047               tempreg = AT;
7048               used_at = 1;
7049             }
7050           else
7051             {
7052               tempreg = dreg;
7053               used_at = 0;
7054             }
7055           macro_build (NULL, &icnt, NULL, "dnegu", "d,w", tempreg, treg);
7056           macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg,
7057                        tempreg);
7058           if (used_at)
7059             break;
7060           return;
7061         }
7062       macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
7063       macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7064       macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7065       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7066       break;
7067
7068     case M_ROL:
7069       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7070         {
7071           if (dreg == sreg)
7072             {
7073               tempreg = AT;
7074               used_at = 1;
7075             }
7076           else
7077             {
7078               tempreg = dreg;
7079               used_at = 0;
7080             }
7081           macro_build (NULL, &icnt, NULL, "negu", "d,w", tempreg, treg);
7082           macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg,
7083                        tempreg);
7084           if (used_at)
7085             break;
7086           return;
7087         }
7088       macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7089       macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
7090       macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg, treg);
7091       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7092       break;
7093
7094     case M_DROL_I:
7095       {
7096         unsigned int rot;
7097         char *l, *r;
7098
7099         if (imm_expr.X_op != O_constant)
7100           as_bad (_("Improper rotate count"));
7101         rot = imm_expr.X_add_number & 0x3f;
7102         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7103           {
7104             rot = (64 - rot) & 0x3f;
7105             if (rot >= 32)
7106               macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7107                            dreg, sreg, rot - 32);
7108             else
7109               macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7110                            dreg, sreg, rot);
7111             return;
7112           }
7113         if (rot == 0)
7114           {
7115             macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7116             return;
7117           }
7118         l = (rot < 0x20) ? "dsll" : "dsll32";
7119         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7120         rot &= 0x1f;
7121         macro_build (NULL, &icnt, NULL, l, "d,w,<", AT, sreg, rot);
7122         macro_build (NULL, &icnt, NULL, r, "d,w,<", dreg, sreg,
7123                      (0x20 - rot) & 0x1f);
7124         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7125       }
7126       break;
7127
7128     case M_ROL_I:
7129       {
7130         unsigned int rot;
7131
7132         if (imm_expr.X_op != O_constant)
7133           as_bad (_("Improper rotate count"));
7134         rot = imm_expr.X_add_number & 0x1f;
7135         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7136           {
7137             macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg,
7138                          (32 - rot) & 0x1f);
7139             return;
7140           }
7141         if (rot == 0)
7142           {
7143             macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7144             return;
7145           }
7146         macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg, rot);
7147         macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
7148                      (0x20 - rot) & 0x1f);
7149         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7150       }
7151       break;
7152
7153     case M_DROR:
7154       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7155         {
7156           macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg, treg);
7157           return;
7158         }
7159       macro_build (NULL, &icnt,NULL, "dsubu", "d,v,t", AT, 0, treg);
7160       macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", AT, sreg, AT);
7161       macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7162       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7163       break;
7164
7165     case M_ROR:
7166       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7167         {
7168           macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg, treg);
7169           return;
7170         }
7171       macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7172       macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
7173       macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg, treg);
7174       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7175       break;
7176
7177     case M_DROR_I:
7178       {
7179         unsigned int rot;
7180         char *l, *r;
7181
7182         if (imm_expr.X_op != O_constant)
7183           as_bad (_("Improper rotate count"));
7184         rot = imm_expr.X_add_number & 0x3f;
7185         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7186           {
7187             if (rot >= 32)
7188               macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7189                            dreg, sreg, rot - 32);
7190             else
7191               macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7192                            dreg, sreg, rot);
7193             return;
7194           }
7195         if (rot == 0)
7196           {
7197             macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7198             return;
7199           }
7200         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7201         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7202         rot &= 0x1f;
7203         macro_build ( NULL, &icnt,NULL, r, "d,w,<", AT, sreg, rot);
7204         macro_build (NULL, &icnt, NULL, l, "d,w,<", dreg, sreg,
7205                      (0x20 - rot) & 0x1f);
7206         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7207       }
7208       break;
7209
7210     case M_ROR_I:
7211       {
7212         unsigned int rot;
7213
7214         if (imm_expr.X_op != O_constant)
7215           as_bad (_("Improper rotate count"));
7216         rot = imm_expr.X_add_number & 0x1f;
7217         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7218           {
7219             macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg, rot);
7220             return;
7221           }
7222         if (rot == 0)
7223           {
7224             macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7225             return;
7226           }
7227         macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg, rot);
7228         macro_build (NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
7229                      (0x20 - rot) & 0x1f);
7230         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7231       }
7232       break;
7233
7234     case M_S_DOB:
7235       if (mips_opts.arch == CPU_R4650)
7236         {
7237           as_bad (_("opcode not supported on this processor"));
7238           return;
7239         }
7240       assert (mips_opts.isa == ISA_MIPS1);
7241       /* Even on a big endian machine $fn comes before $fn+1.  We have
7242          to adjust when storing to memory.  */
7243       macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7244                    target_big_endian ? treg + 1 : treg,
7245                    BFD_RELOC_LO16, breg);
7246       offset_expr.X_add_number += 4;
7247       macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7248                    target_big_endian ? treg : treg + 1,
7249                    BFD_RELOC_LO16, breg);
7250       return;
7251
7252     case M_SEQ:
7253       if (sreg == 0)
7254         macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, treg,
7255                      BFD_RELOC_LO16);
7256       else if (treg == 0)
7257         macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7258                      BFD_RELOC_LO16);
7259       else
7260         {
7261           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7262           macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7263                        BFD_RELOC_LO16);
7264         }
7265       return;
7266
7267     case M_SEQ_I:
7268       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7269         {
7270           macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7271                        BFD_RELOC_LO16);
7272           return;
7273         }
7274       if (sreg == 0)
7275         {
7276           as_warn (_("Instruction %s: result is always false"),
7277                    ip->insn_mo->name);
7278           move_register (&icnt, dreg, 0);
7279           return;
7280         }
7281       if (imm_expr.X_op == O_constant
7282           && imm_expr.X_add_number >= 0
7283           && imm_expr.X_add_number < 0x10000)
7284         {
7285           macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7286                        BFD_RELOC_LO16);
7287           used_at = 0;
7288         }
7289       else if (imm_expr.X_op == O_constant
7290                && imm_expr.X_add_number > -0x8000
7291                && imm_expr.X_add_number < 0)
7292         {
7293           imm_expr.X_add_number = -imm_expr.X_add_number;
7294           macro_build (NULL, &icnt, &imm_expr,
7295                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7296                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7297           used_at = 0;
7298         }
7299       else
7300         {
7301           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7302           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7303           used_at = 1;
7304         }
7305       macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7306                    BFD_RELOC_LO16);
7307       if (used_at)
7308         break;
7309       return;
7310
7311     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7312       s = "slt";
7313       goto sge;
7314     case M_SGEU:
7315       s = "sltu";
7316     sge:
7317       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
7318       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7319                    BFD_RELOC_LO16);
7320       return;
7321
7322     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7323     case M_SGEU_I:
7324       if (imm_expr.X_op == O_constant
7325           && imm_expr.X_add_number >= -0x8000
7326           && imm_expr.X_add_number < 0x8000)
7327         {
7328           macro_build (NULL, &icnt, &imm_expr,
7329                        mask == M_SGE_I ? "slti" : "sltiu",
7330                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7331           used_at = 0;
7332         }
7333       else
7334         {
7335           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7336           macro_build (NULL, &icnt, NULL, mask == M_SGE_I ? "slt" : "sltu",
7337                        "d,v,t", dreg, sreg, AT);
7338           used_at = 1;
7339         }
7340       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7341                    BFD_RELOC_LO16);
7342       if (used_at)
7343         break;
7344       return;
7345
7346     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7347       s = "slt";
7348       goto sgt;
7349     case M_SGTU:
7350       s = "sltu";
7351     sgt:
7352       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7353       return;
7354
7355     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7356       s = "slt";
7357       goto sgti;
7358     case M_SGTU_I:
7359       s = "sltu";
7360     sgti:
7361       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7362       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7363       break;
7364
7365     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7366       s = "slt";
7367       goto sle;
7368     case M_SLEU:
7369       s = "sltu";
7370     sle:
7371       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7372       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7373                    BFD_RELOC_LO16);
7374       return;
7375
7376     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7377       s = "slt";
7378       goto slei;
7379     case M_SLEU_I:
7380       s = "sltu";
7381     slei:
7382       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7383       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7384       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7385                    BFD_RELOC_LO16);
7386       break;
7387
7388     case M_SLT_I:
7389       if (imm_expr.X_op == O_constant
7390           && imm_expr.X_add_number >= -0x8000
7391           && imm_expr.X_add_number < 0x8000)
7392         {
7393           macro_build (NULL, &icnt, &imm_expr, "slti", "t,r,j", dreg, sreg,
7394                        BFD_RELOC_LO16);
7395           return;
7396         }
7397       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7398       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
7399       break;
7400
7401     case M_SLTU_I:
7402       if (imm_expr.X_op == O_constant
7403           && imm_expr.X_add_number >= -0x8000
7404           && imm_expr.X_add_number < 0x8000)
7405         {
7406           macro_build (NULL, &icnt, &imm_expr, "sltiu", "t,r,j", dreg, sreg,
7407                        BFD_RELOC_LO16);
7408           return;
7409         }
7410       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7411       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg, AT);
7412       break;
7413
7414     case M_SNE:
7415       if (sreg == 0)
7416         macro_build (NULL, &icnt,NULL, "sltu","d,v,t", dreg, 0, treg);
7417       else if (treg == 0)
7418         macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7419       else
7420         {
7421           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7422           macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7423         }
7424       return;
7425
7426     case M_SNE_I:
7427       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7428         {
7429           macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7430           return;
7431         }
7432       if (sreg == 0)
7433         {
7434           as_warn (_("Instruction %s: result is always true"),
7435                    ip->insn_mo->name);
7436           macro_build (NULL, &icnt, &expr1,
7437                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7438                        "t,r,j", dreg, 0, BFD_RELOC_LO16);
7439           return;
7440         }
7441       if (imm_expr.X_op == O_constant
7442           && imm_expr.X_add_number >= 0
7443           && imm_expr.X_add_number < 0x10000)
7444         {
7445           macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7446                        BFD_RELOC_LO16);
7447           used_at = 0;
7448         }
7449       else if (imm_expr.X_op == O_constant
7450                && imm_expr.X_add_number > -0x8000
7451                && imm_expr.X_add_number < 0)
7452         {
7453           imm_expr.X_add_number = -imm_expr.X_add_number;
7454           macro_build (NULL, &icnt, &imm_expr,
7455                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7456                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7457           used_at = 0;
7458         }
7459       else
7460         {
7461           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7462           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7463           used_at = 1;
7464         }
7465       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7466       if (used_at)
7467         break;
7468       return;
7469
7470     case M_DSUB_I:
7471       dbl = 1;
7472     case M_SUB_I:
7473       if (imm_expr.X_op == O_constant
7474           && imm_expr.X_add_number > -0x8000
7475           && imm_expr.X_add_number <= 0x8000)
7476         {
7477           imm_expr.X_add_number = -imm_expr.X_add_number;
7478           macro_build (NULL, &icnt, &imm_expr, dbl ? "daddi" : "addi",
7479                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7480           return;
7481         }
7482       load_register (&icnt, AT, &imm_expr, dbl);
7483       macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t",
7484                    dreg, sreg, AT);
7485       break;
7486
7487     case M_DSUBU_I:
7488       dbl = 1;
7489     case M_SUBU_I:
7490       if (imm_expr.X_op == O_constant
7491           && imm_expr.X_add_number > -0x8000
7492           && imm_expr.X_add_number <= 0x8000)
7493         {
7494           imm_expr.X_add_number = -imm_expr.X_add_number;
7495           macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu",
7496                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7497           return;
7498         }
7499       load_register (&icnt, AT, &imm_expr, dbl);
7500       macro_build (NULL, &icnt, NULL, dbl ? "dsubu" : "subu", "d,v,t",
7501                    dreg, sreg, AT);
7502       break;
7503
7504     case M_TEQ_I:
7505       s = "teq";
7506       goto trap;
7507     case M_TGE_I:
7508       s = "tge";
7509       goto trap;
7510     case M_TGEU_I:
7511       s = "tgeu";
7512       goto trap;
7513     case M_TLT_I:
7514       s = "tlt";
7515       goto trap;
7516     case M_TLTU_I:
7517       s = "tltu";
7518       goto trap;
7519     case M_TNE_I:
7520       s = "tne";
7521     trap:
7522       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7523       macro_build (NULL, &icnt, NULL, s, "s,t", sreg, AT);
7524       break;
7525
7526     case M_TRUNCWS:
7527     case M_TRUNCWD:
7528       assert (mips_opts.isa == ISA_MIPS1);
7529       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7530       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7531
7532       /*
7533        * Is the double cfc1 instruction a bug in the mips assembler;
7534        * or is there a reason for it?
7535        */
7536       mips_emit_delays (TRUE);
7537       ++mips_opts.noreorder;
7538       mips_any_noreorder = 1;
7539       macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7540       macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7541       macro_build (NULL, &icnt, NULL, "nop", "");
7542       expr1.X_add_number = 3;
7543       macro_build (NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7544                    BFD_RELOC_LO16);
7545       expr1.X_add_number = 2;
7546       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7547                    BFD_RELOC_LO16);
7548       macro_build (NULL, &icnt, NULL, "ctc1", "t,G", AT, RA);
7549       macro_build (NULL, &icnt, NULL, "nop", "");
7550       macro_build (NULL, &icnt, NULL,
7551                    mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s",
7552                    "D,S", dreg, sreg);
7553       macro_build (NULL, &icnt, NULL, "ctc1", "t,G", treg, RA);
7554       macro_build (NULL, &icnt, NULL, "nop", "");
7555       --mips_opts.noreorder;
7556       break;
7557
7558     case M_ULH:
7559       s = "lb";
7560       goto ulh;
7561     case M_ULHU:
7562       s = "lbu";
7563     ulh:
7564       if (offset_expr.X_add_number >= 0x7fff)
7565         as_bad (_("operand overflow"));
7566       if (! target_big_endian)
7567         ++offset_expr.X_add_number;
7568       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7569                    BFD_RELOC_LO16, breg);
7570       if (! target_big_endian)
7571         --offset_expr.X_add_number;
7572       else
7573         ++offset_expr.X_add_number;
7574       macro_build (NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7575                    BFD_RELOC_LO16, breg);
7576       macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, AT, 8);
7577       macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7578       break;
7579
7580     case M_ULD:
7581       s = "ldl";
7582       s2 = "ldr";
7583       off = 7;
7584       goto ulw;
7585     case M_ULW:
7586       s = "lwl";
7587       s2 = "lwr";
7588       off = 3;
7589     ulw:
7590       if (offset_expr.X_add_number >= 0x8000 - off)
7591         as_bad (_("operand overflow"));
7592       if (treg != breg)
7593         tempreg = treg;
7594       else
7595         tempreg = AT;
7596       if (! target_big_endian)
7597         offset_expr.X_add_number += off;
7598       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7599                    BFD_RELOC_LO16, breg);
7600       if (! target_big_endian)
7601         offset_expr.X_add_number -= off;
7602       else
7603         offset_expr.X_add_number += off;
7604       macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7605                    BFD_RELOC_LO16, breg);
7606
7607       /* If necessary, move the result in tempreg the final destination.  */
7608       if (treg == tempreg)
7609         return;
7610       /* Protect second load's delay slot.  */
7611       if (!gpr_interlocks)
7612         macro_build (NULL, &icnt, NULL, "nop", "");
7613       move_register (&icnt, treg, tempreg);
7614       break;
7615
7616     case M_ULD_A:
7617       s = "ldl";
7618       s2 = "ldr";
7619       off = 7;
7620       goto ulwa;
7621     case M_ULW_A:
7622       s = "lwl";
7623       s2 = "lwr";
7624       off = 3;
7625     ulwa:
7626       used_at = 1;
7627       load_address (&icnt, AT, &offset_expr, &used_at);
7628       if (breg != 0)
7629         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7630                      AT, AT, breg);
7631       if (! target_big_endian)
7632         expr1.X_add_number = off;
7633       else
7634         expr1.X_add_number = 0;
7635       macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7636                    BFD_RELOC_LO16, AT);
7637       if (! target_big_endian)
7638         expr1.X_add_number = 0;
7639       else
7640         expr1.X_add_number = off;
7641       macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7642                    BFD_RELOC_LO16, AT);
7643       break;
7644
7645     case M_ULH_A:
7646     case M_ULHU_A:
7647       used_at = 1;
7648       load_address (&icnt, AT, &offset_expr, &used_at);
7649       if (breg != 0)
7650         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7651                      AT, AT, breg);
7652       if (target_big_endian)
7653         expr1.X_add_number = 0;
7654       macro_build (NULL, &icnt, &expr1,
7655                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7656                    treg, BFD_RELOC_LO16, AT);
7657       if (target_big_endian)
7658         expr1.X_add_number = 1;
7659       else
7660         expr1.X_add_number = 0;
7661       macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)",
7662                    AT, BFD_RELOC_LO16, AT);
7663       macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7664       macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7665       break;
7666
7667     case M_USH:
7668       if (offset_expr.X_add_number >= 0x7fff)
7669         as_bad (_("operand overflow"));
7670       if (target_big_endian)
7671         ++offset_expr.X_add_number;
7672       macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7673                    BFD_RELOC_LO16, breg);
7674       macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
7675       if (target_big_endian)
7676         --offset_expr.X_add_number;
7677       else
7678         ++offset_expr.X_add_number;
7679       macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7680                    BFD_RELOC_LO16, breg);
7681       break;
7682
7683     case M_USD:
7684       s = "sdl";
7685       s2 = "sdr";
7686       off = 7;
7687       goto usw;
7688     case M_USW:
7689       s = "swl";
7690       s2 = "swr";
7691       off = 3;
7692     usw:
7693       if (offset_expr.X_add_number >= 0x8000 - off)
7694         as_bad (_("operand overflow"));
7695       if (! target_big_endian)
7696         offset_expr.X_add_number += off;
7697       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7698                    BFD_RELOC_LO16, breg);
7699       if (! target_big_endian)
7700         offset_expr.X_add_number -= off;
7701       else
7702         offset_expr.X_add_number += off;
7703       macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7704                    BFD_RELOC_LO16, breg);
7705       return;
7706
7707     case M_USD_A:
7708       s = "sdl";
7709       s2 = "sdr";
7710       off = 7;
7711       goto uswa;
7712     case M_USW_A:
7713       s = "swl";
7714       s2 = "swr";
7715       off = 3;
7716     uswa:
7717       used_at = 1;
7718       load_address (&icnt, AT, &offset_expr, &used_at);
7719       if (breg != 0)
7720         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7721                      AT, AT, breg);
7722       if (! target_big_endian)
7723         expr1.X_add_number = off;
7724       else
7725         expr1.X_add_number = 0;
7726       macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7727                    BFD_RELOC_LO16, AT);
7728       if (! target_big_endian)
7729         expr1.X_add_number = 0;
7730       else
7731         expr1.X_add_number = off;
7732       macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7733                    BFD_RELOC_LO16, AT);
7734       break;
7735
7736     case M_USH_A:
7737       used_at = 1;
7738       load_address (&icnt, AT, &offset_expr, &used_at);
7739       if (breg != 0)
7740         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7741                      AT, AT, breg);
7742       if (! target_big_endian)
7743         expr1.X_add_number = 0;
7744       macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7745                    BFD_RELOC_LO16, AT);
7746       macro_build (NULL, &icnt, NULL, "srl", "d,w,<", treg, treg, 8);
7747       if (! target_big_endian)
7748         expr1.X_add_number = 1;
7749       else
7750         expr1.X_add_number = 0;
7751       macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7752                    BFD_RELOC_LO16, AT);
7753       if (! target_big_endian)
7754         expr1.X_add_number = 0;
7755       else
7756         expr1.X_add_number = 1;
7757       macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7758                    BFD_RELOC_LO16, AT);
7759       macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7760       macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7761       break;
7762
7763     default:
7764       /* FIXME: Check if this is one of the itbl macros, since they
7765          are added dynamically.  */
7766       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7767       break;
7768     }
7769   if (mips_opts.noat)
7770     as_warn (_("Macro used $at after \".set noat\""));
7771 }
7772
7773 /* Implement macros in mips16 mode.  */
7774
7775 static void
7776 mips16_macro (struct mips_cl_insn *ip)
7777 {
7778   int mask;
7779   int xreg, yreg, zreg, tmp;
7780   int icnt;
7781   expressionS expr1;
7782   int dbl;
7783   const char *s, *s2, *s3;
7784
7785   mask = ip->insn_mo->mask;
7786
7787   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7788   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7789   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7790
7791   icnt = 0;
7792
7793   expr1.X_op = O_constant;
7794   expr1.X_op_symbol = NULL;
7795   expr1.X_add_symbol = NULL;
7796   expr1.X_add_number = 1;
7797
7798   dbl = 0;
7799
7800   switch (mask)
7801     {
7802     default:
7803       internalError ();
7804
7805     case M_DDIV_3:
7806       dbl = 1;
7807     case M_DIV_3:
7808       s = "mflo";
7809       goto do_div3;
7810     case M_DREM_3:
7811       dbl = 1;
7812     case M_REM_3:
7813       s = "mfhi";
7814     do_div3:
7815       mips_emit_delays (TRUE);
7816       ++mips_opts.noreorder;
7817       mips_any_noreorder = 1;
7818       macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "0,x,y",
7819                    xreg, yreg);
7820       expr1.X_add_number = 2;
7821       macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7822       macro_build (NULL, &icnt, NULL, "break", "6", 7);
7823
7824       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7825          since that causes an overflow.  We should do that as well,
7826          but I don't see how to do the comparisons without a temporary
7827          register.  */
7828       --mips_opts.noreorder;
7829       macro_build (NULL, &icnt, NULL, s, "x", zreg);
7830       break;
7831
7832     case M_DIVU_3:
7833       s = "divu";
7834       s2 = "mflo";
7835       goto do_divu3;
7836     case M_REMU_3:
7837       s = "divu";
7838       s2 = "mfhi";
7839       goto do_divu3;
7840     case M_DDIVU_3:
7841       s = "ddivu";
7842       s2 = "mflo";
7843       goto do_divu3;
7844     case M_DREMU_3:
7845       s = "ddivu";
7846       s2 = "mfhi";
7847     do_divu3:
7848       mips_emit_delays (TRUE);
7849       ++mips_opts.noreorder;
7850       mips_any_noreorder = 1;
7851       macro_build (NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
7852       expr1.X_add_number = 2;
7853       macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7854       macro_build (NULL, &icnt, NULL, "break", "6", 7);
7855       --mips_opts.noreorder;
7856       macro_build (NULL, &icnt, NULL, s2, "x", zreg);
7857       break;
7858
7859     case M_DMUL:
7860       dbl = 1;
7861     case M_MUL:
7862       macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "x,y",
7863                    xreg, yreg);
7864       macro_build (NULL, &icnt, NULL, "mflo", "x", zreg);
7865       return;
7866
7867     case M_DSUBU_I:
7868       dbl = 1;
7869       goto do_subu;
7870     case M_SUBU_I:
7871     do_subu:
7872       if (imm_expr.X_op != O_constant)
7873         as_bad (_("Unsupported large constant"));
7874       imm_expr.X_add_number = -imm_expr.X_add_number;
7875       macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu", "y,x,4",
7876                    yreg, xreg);
7877       break;
7878
7879     case M_SUBU_I_2:
7880       if (imm_expr.X_op != O_constant)
7881         as_bad (_("Unsupported large constant"));
7882       imm_expr.X_add_number = -imm_expr.X_add_number;
7883       macro_build (NULL, &icnt, &imm_expr, "addiu", "x,k", xreg);
7884       break;
7885
7886     case M_DSUBU_I_2:
7887       if (imm_expr.X_op != O_constant)
7888         as_bad (_("Unsupported large constant"));
7889       imm_expr.X_add_number = -imm_expr.X_add_number;
7890       macro_build (NULL, &icnt, &imm_expr, "daddiu", "y,j", yreg);
7891       break;
7892
7893     case M_BEQ:
7894       s = "cmp";
7895       s2 = "bteqz";
7896       goto do_branch;
7897     case M_BNE:
7898       s = "cmp";
7899       s2 = "btnez";
7900       goto do_branch;
7901     case M_BLT:
7902       s = "slt";
7903       s2 = "btnez";
7904       goto do_branch;
7905     case M_BLTU:
7906       s = "sltu";
7907       s2 = "btnez";
7908       goto do_branch;
7909     case M_BLE:
7910       s = "slt";
7911       s2 = "bteqz";
7912       goto do_reverse_branch;
7913     case M_BLEU:
7914       s = "sltu";
7915       s2 = "bteqz";
7916       goto do_reverse_branch;
7917     case M_BGE:
7918       s = "slt";
7919       s2 = "bteqz";
7920       goto do_branch;
7921     case M_BGEU:
7922       s = "sltu";
7923       s2 = "bteqz";
7924       goto do_branch;
7925     case M_BGT:
7926       s = "slt";
7927       s2 = "btnez";
7928       goto do_reverse_branch;
7929     case M_BGTU:
7930       s = "sltu";
7931       s2 = "btnez";
7932
7933     do_reverse_branch:
7934       tmp = xreg;
7935       xreg = yreg;
7936       yreg = tmp;
7937
7938     do_branch:
7939       macro_build (NULL, &icnt, NULL, s, "x,y", xreg, yreg);
7940       macro_build (NULL, &icnt, &offset_expr, s2, "p");
7941       break;
7942
7943     case M_BEQ_I:
7944       s = "cmpi";
7945       s2 = "bteqz";
7946       s3 = "x,U";
7947       goto do_branch_i;
7948     case M_BNE_I:
7949       s = "cmpi";
7950       s2 = "btnez";
7951       s3 = "x,U";
7952       goto do_branch_i;
7953     case M_BLT_I:
7954       s = "slti";
7955       s2 = "btnez";
7956       s3 = "x,8";
7957       goto do_branch_i;
7958     case M_BLTU_I:
7959       s = "sltiu";
7960       s2 = "btnez";
7961       s3 = "x,8";
7962       goto do_branch_i;
7963     case M_BLE_I:
7964       s = "slti";
7965       s2 = "btnez";
7966       s3 = "x,8";
7967       goto do_addone_branch_i;
7968     case M_BLEU_I:
7969       s = "sltiu";
7970       s2 = "btnez";
7971       s3 = "x,8";
7972       goto do_addone_branch_i;
7973     case M_BGE_I:
7974       s = "slti";
7975       s2 = "bteqz";
7976       s3 = "x,8";
7977       goto do_branch_i;
7978     case M_BGEU_I:
7979       s = "sltiu";
7980       s2 = "bteqz";
7981       s3 = "x,8";
7982       goto do_branch_i;
7983     case M_BGT_I:
7984       s = "slti";
7985       s2 = "bteqz";
7986       s3 = "x,8";
7987       goto do_addone_branch_i;
7988     case M_BGTU_I:
7989       s = "sltiu";
7990       s2 = "bteqz";
7991       s3 = "x,8";
7992
7993     do_addone_branch_i:
7994       if (imm_expr.X_op != O_constant)
7995         as_bad (_("Unsupported large constant"));
7996       ++imm_expr.X_add_number;
7997
7998     do_branch_i:
7999       macro_build (NULL, &icnt, &imm_expr, s, s3, xreg);
8000       macro_build (NULL, &icnt, &offset_expr, s2, "p");
8001       break;
8002
8003     case M_ABS:
8004       expr1.X_add_number = 0;
8005       macro_build (NULL, &icnt, &expr1, "slti", "x,8", yreg);
8006       if (xreg != yreg)
8007         move_register (&icnt, xreg, yreg);
8008       expr1.X_add_number = 2;
8009       macro_build (NULL, &icnt, &expr1, "bteqz", "p");
8010       macro_build (NULL, &icnt, NULL, "neg", "x,w", xreg, xreg);
8011     }
8012 }
8013
8014 /* For consistency checking, verify that all bits are specified either
8015    by the match/mask part of the instruction definition, or by the
8016    operand list.  */
8017 static int
8018 validate_mips_insn (const struct mips_opcode *opc)
8019 {
8020   const char *p = opc->args;
8021   char c;
8022   unsigned long used_bits = opc->mask;
8023
8024   if ((used_bits & opc->match) != opc->match)
8025     {
8026       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8027               opc->name, opc->args);
8028       return 0;
8029     }
8030 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8031   while (*p)
8032     switch (c = *p++)
8033       {
8034       case ',': break;
8035       case '(': break;
8036       case ')': break;
8037       case '+':
8038         switch (c = *p++)
8039           {
8040           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8041           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8042           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8043           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8044                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8045           default:
8046             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8047                     c, opc->name, opc->args);
8048             return 0;
8049           }
8050         break;
8051       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8052       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8053       case 'A': break;
8054       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8055       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8056       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8057       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8058       case 'F': break;
8059       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8060       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8061       case 'I': break;
8062       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8063       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8064       case 'L': break;
8065       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8066       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8067       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8068       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8069                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8070       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8071       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8072       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8073       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8074       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8075       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8076       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8077       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8078       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8079       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8080       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8081       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8082       case 'f': break;
8083       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8084       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8085       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8086       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8087       case 'l': break;
8088       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8089       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8090       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8091       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8092       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8093       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8094       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8095       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8096       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8097       case 'x': break;
8098       case 'z': break;
8099       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8100       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8101                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8102       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8103       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8104       case '[': break;
8105       case ']': break;
8106       default:
8107         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8108                 c, opc->name, opc->args);
8109         return 0;
8110       }
8111 #undef USE_BITS
8112   if (used_bits != 0xffffffff)
8113     {
8114       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8115               ~used_bits & 0xffffffff, opc->name, opc->args);
8116       return 0;
8117     }
8118   return 1;
8119 }
8120
8121 /* This routine assembles an instruction into its binary format.  As a
8122    side effect, it sets one of the global variables imm_reloc or
8123    offset_reloc to the type of relocation to do if one of the operands
8124    is an address expression.  */
8125
8126 static void
8127 mips_ip (char *str, struct mips_cl_insn *ip)
8128 {
8129   char *s;
8130   const char *args;
8131   char c = 0;
8132   struct mips_opcode *insn;
8133   char *argsStart;
8134   unsigned int regno;
8135   unsigned int lastregno = 0;
8136   unsigned int lastpos = 0;
8137   unsigned int limlo, limhi;
8138   char *s_reset;
8139   char save_c = 0;
8140
8141   insn_error = NULL;
8142
8143   /* If the instruction contains a '.', we first try to match an instruction
8144      including the '.'.  Then we try again without the '.'.  */
8145   insn = NULL;
8146   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8147     continue;
8148
8149   /* If we stopped on whitespace, then replace the whitespace with null for
8150      the call to hash_find.  Save the character we replaced just in case we
8151      have to re-parse the instruction.  */
8152   if (ISSPACE (*s))
8153     {
8154       save_c = *s;
8155       *s++ = '\0';
8156     }
8157
8158   insn = (struct mips_opcode *) hash_find (op_hash, str);
8159
8160   /* If we didn't find the instruction in the opcode table, try again, but
8161      this time with just the instruction up to, but not including the
8162      first '.'.  */
8163   if (insn == NULL)
8164     {
8165       /* Restore the character we overwrite above (if any).  */
8166       if (save_c)
8167         *(--s) = save_c;
8168
8169       /* Scan up to the first '.' or whitespace.  */
8170       for (s = str;
8171            *s != '\0' && *s != '.' && !ISSPACE (*s);
8172            ++s)
8173         continue;
8174
8175       /* If we did not find a '.', then we can quit now.  */
8176       if (*s != '.')
8177         {
8178           insn_error = "unrecognized opcode";
8179           return;
8180         }
8181
8182       /* Lookup the instruction in the hash table.  */
8183       *s++ = '\0';
8184       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8185         {
8186           insn_error = "unrecognized opcode";
8187           return;
8188         }
8189     }
8190
8191   argsStart = s;
8192   for (;;)
8193     {
8194       bfd_boolean ok;
8195
8196       assert (strcmp (insn->name, str) == 0);
8197
8198       if (OPCODE_IS_MEMBER (insn,
8199                             (mips_opts.isa
8200                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8201                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8202                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8203                             mips_opts.arch))
8204         ok = TRUE;
8205       else
8206         ok = FALSE;
8207
8208       if (insn->pinfo != INSN_MACRO)
8209         {
8210           if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8211             ok = FALSE;
8212         }
8213
8214       if (! ok)
8215         {
8216           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8217               && strcmp (insn->name, insn[1].name) == 0)
8218             {
8219               ++insn;
8220               continue;
8221             }
8222           else
8223             {
8224               if (!insn_error)
8225                 {
8226                   static char buf[100];
8227                   sprintf (buf,
8228                            _("opcode not supported on this processor: %s (%s)"),
8229                            mips_cpu_info_from_arch (mips_opts.arch)->name,
8230                            mips_cpu_info_from_isa (mips_opts.isa)->name);
8231                   insn_error = buf;
8232                 }
8233               if (save_c)
8234                 *(--s) = save_c;
8235               return;
8236             }
8237         }
8238
8239       ip->insn_mo = insn;
8240       ip->insn_opcode = insn->match;
8241       insn_error = NULL;
8242       for (args = insn->args;; ++args)
8243         {
8244           int is_mdmx;
8245
8246           s += strspn (s, " \t");
8247           is_mdmx = 0;
8248           switch (*args)
8249             {
8250             case '\0':          /* end of args */
8251               if (*s == '\0')
8252                 return;
8253               break;
8254
8255             case ',':
8256               if (*s++ == *args)
8257                 continue;
8258               s--;
8259               switch (*++args)
8260                 {
8261                 case 'r':
8262                 case 'v':
8263                   ip->insn_opcode |= lastregno << OP_SH_RS;
8264                   continue;
8265
8266                 case 'w':
8267                   ip->insn_opcode |= lastregno << OP_SH_RT;
8268                   continue;
8269
8270                 case 'W':
8271                   ip->insn_opcode |= lastregno << OP_SH_FT;
8272                   continue;
8273
8274                 case 'V':
8275                   ip->insn_opcode |= lastregno << OP_SH_FS;
8276                   continue;
8277                 }
8278               break;
8279
8280             case '(':
8281               /* Handle optional base register.
8282                  Either the base register is omitted or
8283                  we must have a left paren.  */
8284               /* This is dependent on the next operand specifier
8285                  is a base register specification.  */
8286               assert (args[1] == 'b' || args[1] == '5'
8287                       || args[1] == '-' || args[1] == '4');
8288               if (*s == '\0')
8289                 return;
8290
8291             case ')':           /* these must match exactly */
8292             case '[':
8293             case ']':
8294               if (*s++ == *args)
8295                 continue;
8296               break;
8297
8298             case '+':           /* Opcode extension character.  */
8299               switch (*++args)
8300                 {
8301                 case 'A':               /* ins/ext position, becomes LSB.  */
8302                   limlo = 0;
8303                   limhi = 31;
8304                   my_getExpression (&imm_expr, s);
8305                   check_absolute_expr (ip, &imm_expr);
8306                   if ((unsigned long) imm_expr.X_add_number < limlo
8307                       || (unsigned long) imm_expr.X_add_number > limhi)
8308                     {
8309                       as_bad (_("Improper position (%lu)"),
8310                               (unsigned long) imm_expr.X_add_number);
8311                       imm_expr.X_add_number = limlo;
8312                     }
8313                   lastpos = imm_expr.X_add_number;
8314                   ip->insn_opcode |= (imm_expr.X_add_number
8315                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8316                   imm_expr.X_op = O_absent;
8317                   s = expr_end;
8318                   continue;
8319
8320                 case 'B':               /* ins size, becomes MSB.  */
8321                   limlo = 1;
8322                   limhi = 32;
8323                   my_getExpression (&imm_expr, s);
8324                   check_absolute_expr (ip, &imm_expr);
8325                   /* Check for negative input so that small negative numbers
8326                      will not succeed incorrectly.  The checks against
8327                      (pos+size) transitively check "size" itself,
8328                      assuming that "pos" is reasonable.  */
8329                   if ((long) imm_expr.X_add_number < 0
8330                       || ((unsigned long) imm_expr.X_add_number
8331                           + lastpos) < limlo
8332                       || ((unsigned long) imm_expr.X_add_number
8333                           + lastpos) > limhi)
8334                     {
8335                       as_bad (_("Improper insert size (%lu, position %lu)"),
8336                               (unsigned long) imm_expr.X_add_number,
8337                               (unsigned long) lastpos);
8338                       imm_expr.X_add_number = limlo - lastpos;
8339                     }
8340                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8341                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8342                   imm_expr.X_op = O_absent;
8343                   s = expr_end;
8344                   continue;
8345
8346                 case 'C':               /* ext size, becomes MSBD.  */
8347                   limlo = 1;
8348                   limhi = 32;
8349                   my_getExpression (&imm_expr, s);
8350                   check_absolute_expr (ip, &imm_expr);
8351                   /* Check for negative input so that small negative numbers
8352                      will not succeed incorrectly.  The checks against
8353                      (pos+size) transitively check "size" itself,
8354                      assuming that "pos" is reasonable.  */
8355                   if ((long) imm_expr.X_add_number < 0
8356                       || ((unsigned long) imm_expr.X_add_number
8357                           + lastpos) < limlo
8358                       || ((unsigned long) imm_expr.X_add_number
8359                           + lastpos) > limhi)
8360                     {
8361                       as_bad (_("Improper extract size (%lu, position %lu)"),
8362                               (unsigned long) imm_expr.X_add_number,
8363                               (unsigned long) lastpos);
8364                       imm_expr.X_add_number = limlo - lastpos;
8365                     }
8366                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8367                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8368                   imm_expr.X_op = O_absent;
8369                   s = expr_end;
8370                   continue;
8371
8372                 case 'D':
8373                   /* +D is for disassembly only; never match.  */
8374                   break;
8375
8376                 default:
8377                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8378                     *args, insn->name, insn->args);
8379                   /* Further processing is fruitless.  */
8380                   return;
8381                 }
8382               break;
8383
8384             case '<':           /* must be at least one digit */
8385               /*
8386                * According to the manual, if the shift amount is greater
8387                * than 31 or less than 0, then the shift amount should be
8388                * mod 32.  In reality the mips assembler issues an error.
8389                * We issue a warning and mask out all but the low 5 bits.
8390                */
8391               my_getExpression (&imm_expr, s);
8392               check_absolute_expr (ip, &imm_expr);
8393               if ((unsigned long) imm_expr.X_add_number > 31)
8394                 {
8395                   as_warn (_("Improper shift amount (%lu)"),
8396                            (unsigned long) imm_expr.X_add_number);
8397                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8398                 }
8399               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8400               imm_expr.X_op = O_absent;
8401               s = expr_end;
8402               continue;
8403
8404             case '>':           /* shift amount minus 32 */
8405               my_getExpression (&imm_expr, s);
8406               check_absolute_expr (ip, &imm_expr);
8407               if ((unsigned long) imm_expr.X_add_number < 32
8408                   || (unsigned long) imm_expr.X_add_number > 63)
8409                 break;
8410               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8411               imm_expr.X_op = O_absent;
8412               s = expr_end;
8413               continue;
8414
8415             case 'k':           /* cache code */
8416             case 'h':           /* prefx code */
8417               my_getExpression (&imm_expr, s);
8418               check_absolute_expr (ip, &imm_expr);
8419               if ((unsigned long) imm_expr.X_add_number > 31)
8420                 {
8421                   as_warn (_("Invalid value for `%s' (%lu)"),
8422                            ip->insn_mo->name,
8423                            (unsigned long) imm_expr.X_add_number);
8424                   imm_expr.X_add_number &= 0x1f;
8425                 }
8426               if (*args == 'k')
8427                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8428               else
8429                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8430               imm_expr.X_op = O_absent;
8431               s = expr_end;
8432               continue;
8433
8434             case 'c':           /* break code */
8435               my_getExpression (&imm_expr, s);
8436               check_absolute_expr (ip, &imm_expr);
8437               if ((unsigned long) imm_expr.X_add_number > 1023)
8438                 {
8439                   as_warn (_("Illegal break code (%lu)"),
8440                            (unsigned long) imm_expr.X_add_number);
8441                   imm_expr.X_add_number &= OP_MASK_CODE;
8442                 }
8443               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8444               imm_expr.X_op = O_absent;
8445               s = expr_end;
8446               continue;
8447
8448             case 'q':           /* lower break code */
8449               my_getExpression (&imm_expr, s);
8450               check_absolute_expr (ip, &imm_expr);
8451               if ((unsigned long) imm_expr.X_add_number > 1023)
8452                 {
8453                   as_warn (_("Illegal lower break code (%lu)"),
8454                            (unsigned long) imm_expr.X_add_number);
8455                   imm_expr.X_add_number &= OP_MASK_CODE2;
8456                 }
8457               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8458               imm_expr.X_op = O_absent;
8459               s = expr_end;
8460               continue;
8461
8462             case 'B':           /* 20-bit syscall/break code.  */
8463               my_getExpression (&imm_expr, s);
8464               check_absolute_expr (ip, &imm_expr);
8465               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8466                 as_warn (_("Illegal 20-bit code (%lu)"),
8467                          (unsigned long) imm_expr.X_add_number);
8468               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8469               imm_expr.X_op = O_absent;
8470               s = expr_end;
8471               continue;
8472
8473             case 'C':           /* Coprocessor code */
8474               my_getExpression (&imm_expr, s);
8475               check_absolute_expr (ip, &imm_expr);
8476               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8477                 {
8478                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8479                            (unsigned long) imm_expr.X_add_number);
8480                   imm_expr.X_add_number &= ((1 << 25) - 1);
8481                 }
8482               ip->insn_opcode |= imm_expr.X_add_number;
8483               imm_expr.X_op = O_absent;
8484               s = expr_end;
8485               continue;
8486
8487             case 'J':           /* 19-bit wait code.  */
8488               my_getExpression (&imm_expr, s);
8489               check_absolute_expr (ip, &imm_expr);
8490               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8491                 as_warn (_("Illegal 19-bit code (%lu)"),
8492                          (unsigned long) imm_expr.X_add_number);
8493               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8494               imm_expr.X_op = O_absent;
8495               s = expr_end;
8496               continue;
8497
8498             case 'P':           /* Performance register */
8499               my_getExpression (&imm_expr, s);
8500               check_absolute_expr (ip, &imm_expr);
8501               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8502                 {
8503                   as_warn (_("Invalid performance register (%lu)"),
8504                            (unsigned long) imm_expr.X_add_number);
8505                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8506                 }
8507               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8508               imm_expr.X_op = O_absent;
8509               s = expr_end;
8510               continue;
8511
8512             case 'b':           /* base register */
8513             case 'd':           /* destination register */
8514             case 's':           /* source register */
8515             case 't':           /* target register */
8516             case 'r':           /* both target and source */
8517             case 'v':           /* both dest and source */
8518             case 'w':           /* both dest and target */
8519             case 'E':           /* coprocessor target register */
8520             case 'G':           /* coprocessor destination register */
8521             case 'K':           /* 'rdhwr' destination register */
8522             case 'x':           /* ignore register name */
8523             case 'z':           /* must be zero register */
8524             case 'U':           /* destination register (clo/clz).  */
8525               s_reset = s;
8526               if (s[0] == '$')
8527                 {
8528
8529                   if (ISDIGIT (s[1]))
8530                     {
8531                       ++s;
8532                       regno = 0;
8533                       do
8534                         {
8535                           regno *= 10;
8536                           regno += *s - '0';
8537                           ++s;
8538                         }
8539                       while (ISDIGIT (*s));
8540                       if (regno > 31)
8541                         as_bad (_("Invalid register number (%d)"), regno);
8542                     }
8543                   else if (*args == 'E' || *args == 'G' || *args == 'K')
8544                     goto notreg;
8545                   else
8546                     {
8547                       if (s[1] == 'r' && s[2] == 'a')
8548                         {
8549                           s += 3;
8550                           regno = RA;
8551                         }
8552                       else if (s[1] == 'f' && s[2] == 'p')
8553                         {
8554                           s += 3;
8555                           regno = FP;
8556                         }
8557                       else if (s[1] == 's' && s[2] == 'p')
8558                         {
8559                           s += 3;
8560                           regno = SP;
8561                         }
8562                       else if (s[1] == 'g' && s[2] == 'p')
8563                         {
8564                           s += 3;
8565                           regno = GP;
8566                         }
8567                       else if (s[1] == 'a' && s[2] == 't')
8568                         {
8569                           s += 3;
8570                           regno = AT;
8571                         }
8572                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8573                         {
8574                           s += 4;
8575                           regno = KT0;
8576                         }
8577                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8578                         {
8579                           s += 4;
8580                           regno = KT1;
8581                         }
8582                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8583                         {
8584                           s += 5;
8585                           regno = ZERO;
8586                         }
8587                       else if (itbl_have_entries)
8588                         {
8589                           char *p, *n;
8590                           unsigned long r;
8591
8592                           p = s + 1;    /* advance past '$' */
8593                           n = itbl_get_field (&p);  /* n is name */
8594
8595                           /* See if this is a register defined in an
8596                              itbl entry.  */
8597                           if (itbl_get_reg_val (n, &r))
8598                             {
8599                               /* Get_field advances to the start of
8600                                  the next field, so we need to back
8601                                  rack to the end of the last field.  */
8602                               if (p)
8603                                 s = p - 1;
8604                               else
8605                                 s = strchr (s, '\0');
8606                               regno = r;
8607                             }
8608                           else
8609                             goto notreg;
8610                         }
8611                       else
8612                         goto notreg;
8613                     }
8614                   if (regno == AT
8615                       && ! mips_opts.noat
8616                       && *args != 'E'
8617                       && *args != 'G'
8618                       && *args != 'K')
8619                     as_warn (_("Used $at without \".set noat\""));
8620                   c = *args;
8621                   if (*s == ' ')
8622                     ++s;
8623                   if (args[1] != *s)
8624                     {
8625                       if (c == 'r' || c == 'v' || c == 'w')
8626                         {
8627                           regno = lastregno;
8628                           s = s_reset;
8629                           ++args;
8630                         }
8631                     }
8632                   /* 'z' only matches $0.  */
8633                   if (c == 'z' && regno != 0)
8634                     break;
8635
8636         /* Now that we have assembled one operand, we use the args string
8637          * to figure out where it goes in the instruction.  */
8638                   switch (c)
8639                     {
8640                     case 'r':
8641                     case 's':
8642                     case 'v':
8643                     case 'b':
8644                       ip->insn_opcode |= regno << OP_SH_RS;
8645                       break;
8646                     case 'd':
8647                     case 'G':
8648                     case 'K':
8649                       ip->insn_opcode |= regno << OP_SH_RD;
8650                       break;
8651                     case 'U':
8652                       ip->insn_opcode |= regno << OP_SH_RD;
8653                       ip->insn_opcode |= regno << OP_SH_RT;
8654                       break;
8655                     case 'w':
8656                     case 't':
8657                     case 'E':
8658                       ip->insn_opcode |= regno << OP_SH_RT;
8659                       break;
8660                     case 'x':
8661                       /* This case exists because on the r3000 trunc
8662                          expands into a macro which requires a gp
8663                          register.  On the r6000 or r4000 it is
8664                          assembled into a single instruction which
8665                          ignores the register.  Thus the insn version
8666                          is MIPS_ISA2 and uses 'x', and the macro
8667                          version is MIPS_ISA1 and uses 't'.  */
8668                       break;
8669                     case 'z':
8670                       /* This case is for the div instruction, which
8671                          acts differently if the destination argument
8672                          is $0.  This only matches $0, and is checked
8673                          outside the switch.  */
8674                       break;
8675                     case 'D':
8676                       /* Itbl operand; not yet implemented. FIXME ?? */
8677                       break;
8678                       /* What about all other operands like 'i', which
8679                          can be specified in the opcode table? */
8680                     }
8681                   lastregno = regno;
8682                   continue;
8683                 }
8684             notreg:
8685               switch (*args++)
8686                 {
8687                 case 'r':
8688                 case 'v':
8689                   ip->insn_opcode |= lastregno << OP_SH_RS;
8690                   continue;
8691                 case 'w':
8692                   ip->insn_opcode |= lastregno << OP_SH_RT;
8693                   continue;
8694                 }
8695               break;
8696
8697             case 'O':           /* MDMX alignment immediate constant.  */
8698               my_getExpression (&imm_expr, s);
8699               check_absolute_expr (ip, &imm_expr);
8700               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8701                 {
8702                   as_warn ("Improper align amount (%ld), using low bits",
8703                            (long) imm_expr.X_add_number);
8704                   imm_expr.X_add_number &= OP_MASK_ALN;
8705                 }
8706               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8707               imm_expr.X_op = O_absent;
8708               s = expr_end;
8709               continue;
8710
8711             case 'Q':           /* MDMX vector, element sel, or const.  */
8712               if (s[0] != '$')
8713                 {
8714                   /* MDMX Immediate.  */
8715                   my_getExpression (&imm_expr, s);
8716                   check_absolute_expr (ip, &imm_expr);
8717                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8718                     {
8719                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8720                                (long) imm_expr.X_add_number);
8721                       imm_expr.X_add_number &= OP_MASK_FT;
8722                     }
8723                   imm_expr.X_add_number &= OP_MASK_FT;
8724                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8725                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8726                   else
8727                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8728                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8729                   imm_expr.X_op = O_absent;
8730                   s = expr_end;
8731                   continue;
8732                 }
8733               /* Not MDMX Immediate.  Fall through.  */
8734             case 'X':           /* MDMX destination register.  */
8735             case 'Y':           /* MDMX source register.  */
8736             case 'Z':           /* MDMX target register.  */
8737               is_mdmx = 1;
8738             case 'D':           /* floating point destination register */
8739             case 'S':           /* floating point source register */
8740             case 'T':           /* floating point target register */
8741             case 'R':           /* floating point source register */
8742             case 'V':
8743             case 'W':
8744               s_reset = s;
8745               /* Accept $fN for FP and MDMX register numbers, and in
8746                  addition accept $vN for MDMX register numbers.  */
8747               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8748                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8749                       && ISDIGIT (s[2])))
8750                 {
8751                   s += 2;
8752                   regno = 0;
8753                   do
8754                     {
8755                       regno *= 10;
8756                       regno += *s - '0';
8757                       ++s;
8758                     }
8759                   while (ISDIGIT (*s));
8760
8761                   if (regno > 31)
8762                     as_bad (_("Invalid float register number (%d)"), regno);
8763
8764                   if ((regno & 1) != 0
8765                       && HAVE_32BIT_FPRS
8766                       && ! (strcmp (str, "mtc1") == 0
8767                             || strcmp (str, "mfc1") == 0
8768                             || strcmp (str, "lwc1") == 0
8769                             || strcmp (str, "swc1") == 0
8770                             || strcmp (str, "l.s") == 0
8771                             || strcmp (str, "s.s") == 0))
8772                     as_warn (_("Float register should be even, was %d"),
8773                              regno);
8774
8775                   c = *args;
8776                   if (*s == ' ')
8777                     ++s;
8778                   if (args[1] != *s)
8779                     {
8780                       if (c == 'V' || c == 'W')
8781                         {
8782                           regno = lastregno;
8783                           s = s_reset;
8784                           ++args;
8785                         }
8786                     }
8787                   switch (c)
8788                     {
8789                     case 'D':
8790                     case 'X':
8791                       ip->insn_opcode |= regno << OP_SH_FD;
8792                       break;
8793                     case 'V':
8794                     case 'S':
8795                     case 'Y':
8796                       ip->insn_opcode |= regno << OP_SH_FS;
8797                       break;
8798                     case 'Q':
8799                       /* This is like 'Z', but also needs to fix the MDMX
8800                          vector/scalar select bits.  Note that the
8801                          scalar immediate case is handled above.  */
8802                       if (*s == '[')
8803                         {
8804                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8805                           int max_el = (is_qh ? 3 : 7);
8806                           s++;
8807                           my_getExpression(&imm_expr, s);
8808                           check_absolute_expr (ip, &imm_expr);
8809                           s = expr_end;
8810                           if (imm_expr.X_add_number > max_el)
8811                             as_bad(_("Bad element selector %ld"),
8812                                    (long) imm_expr.X_add_number);
8813                           imm_expr.X_add_number &= max_el;
8814                           ip->insn_opcode |= (imm_expr.X_add_number
8815                                               << (OP_SH_VSEL +
8816                                                   (is_qh ? 2 : 1)));
8817                           if (*s != ']')
8818                             as_warn(_("Expecting ']' found '%s'"), s);
8819                           else
8820                             s++;
8821                         }
8822                       else
8823                         {
8824                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8825                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8826                                                 << OP_SH_VSEL);
8827                           else
8828                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8829                                                 OP_SH_VSEL);
8830                         }
8831                       /* Fall through */
8832                     case 'W':
8833                     case 'T':
8834                     case 'Z':
8835                       ip->insn_opcode |= regno << OP_SH_FT;
8836                       break;
8837                     case 'R':
8838                       ip->insn_opcode |= regno << OP_SH_FR;
8839                       break;
8840                     }
8841                   lastregno = regno;
8842                   continue;
8843                 }
8844
8845               switch (*args++)
8846                 {
8847                 case 'V':
8848                   ip->insn_opcode |= lastregno << OP_SH_FS;
8849                   continue;
8850                 case 'W':
8851                   ip->insn_opcode |= lastregno << OP_SH_FT;
8852                   continue;
8853                 }
8854               break;
8855
8856             case 'I':
8857               my_getExpression (&imm_expr, s);
8858               if (imm_expr.X_op != O_big
8859                   && imm_expr.X_op != O_constant)
8860                 insn_error = _("absolute expression required");
8861               s = expr_end;
8862               continue;
8863
8864             case 'A':
8865               my_getExpression (&offset_expr, s);
8866               *imm_reloc = BFD_RELOC_32;
8867               s = expr_end;
8868               continue;
8869
8870             case 'F':
8871             case 'L':
8872             case 'f':
8873             case 'l':
8874               {
8875                 int f64;
8876                 int using_gprs;
8877                 char *save_in;
8878                 char *err;
8879                 unsigned char temp[8];
8880                 int len;
8881                 unsigned int length;
8882                 segT seg;
8883                 subsegT subseg;
8884                 char *p;
8885
8886                 /* These only appear as the last operand in an
8887                    instruction, and every instruction that accepts
8888                    them in any variant accepts them in all variants.
8889                    This means we don't have to worry about backing out
8890                    any changes if the instruction does not match.
8891
8892                    The difference between them is the size of the
8893                    floating point constant and where it goes.  For 'F'
8894                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8895                    is 32 bits.  Where the constant is placed is based
8896                    on how the MIPS assembler does things:
8897                     F -- .rdata
8898                     L -- .lit8
8899                     f -- immediate value
8900                     l -- .lit4
8901
8902                     The .lit4 and .lit8 sections are only used if
8903                     permitted by the -G argument.
8904
8905                     When generating embedded PIC code, we use the
8906                     .lit8 section but not the .lit4 section (we can do
8907                     .lit4 inline easily; we need to put .lit8
8908                     somewhere in the data segment, and using .lit8
8909                     permits the linker to eventually combine identical
8910                     .lit8 entries).
8911
8912                     The code below needs to know whether the target register
8913                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8914                     'F' are used with GPR-based instructions and 'l' and
8915                     'L' are used with FPR-based instructions.  */
8916
8917                 f64 = *args == 'F' || *args == 'L';
8918                 using_gprs = *args == 'F' || *args == 'f';
8919
8920                 save_in = input_line_pointer;
8921                 input_line_pointer = s;
8922                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8923                 length = len;
8924                 s = input_line_pointer;
8925                 input_line_pointer = save_in;
8926                 if (err != NULL && *err != '\0')
8927                   {
8928                     as_bad (_("Bad floating point constant: %s"), err);
8929                     memset (temp, '\0', sizeof temp);
8930                     length = f64 ? 8 : 4;
8931                   }
8932
8933                 assert (length == (unsigned) (f64 ? 8 : 4));
8934
8935                 if (*args == 'f'
8936                     || (*args == 'l'
8937                         && (! USE_GLOBAL_POINTER_OPT
8938                             || mips_pic == EMBEDDED_PIC
8939                             || g_switch_value < 4
8940                             || (temp[0] == 0 && temp[1] == 0)
8941                             || (temp[2] == 0 && temp[3] == 0))))
8942                   {
8943                     imm_expr.X_op = O_constant;
8944                     if (! target_big_endian)
8945                       imm_expr.X_add_number = bfd_getl32 (temp);
8946                     else
8947                       imm_expr.X_add_number = bfd_getb32 (temp);
8948                   }
8949                 else if (length > 4
8950                          && ! mips_disable_float_construction
8951                          /* Constants can only be constructed in GPRs and
8952                             copied to FPRs if the GPRs are at least as wide
8953                             as the FPRs.  Force the constant into memory if
8954                             we are using 64-bit FPRs but the GPRs are only
8955                             32 bits wide.  */
8956                          && (using_gprs
8957                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8958                          && ((temp[0] == 0 && temp[1] == 0)
8959                              || (temp[2] == 0 && temp[3] == 0))
8960                          && ((temp[4] == 0 && temp[5] == 0)
8961                              || (temp[6] == 0 && temp[7] == 0)))
8962                   {
8963                     /* The value is simple enough to load with a couple of
8964                        instructions.  If using 32-bit registers, set
8965                        imm_expr to the high order 32 bits and offset_expr to
8966                        the low order 32 bits.  Otherwise, set imm_expr to
8967                        the entire 64 bit constant.  */
8968                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8969                       {
8970                         imm_expr.X_op = O_constant;
8971                         offset_expr.X_op = O_constant;
8972                         if (! target_big_endian)
8973                           {
8974                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8975                             offset_expr.X_add_number = bfd_getl32 (temp);
8976                           }
8977                         else
8978                           {
8979                             imm_expr.X_add_number = bfd_getb32 (temp);
8980                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8981                           }
8982                         if (offset_expr.X_add_number == 0)
8983                           offset_expr.X_op = O_absent;
8984                       }
8985                     else if (sizeof (imm_expr.X_add_number) > 4)
8986                       {
8987                         imm_expr.X_op = O_constant;
8988                         if (! target_big_endian)
8989                           imm_expr.X_add_number = bfd_getl64 (temp);
8990                         else
8991                           imm_expr.X_add_number = bfd_getb64 (temp);
8992                       }
8993                     else
8994                       {
8995                         imm_expr.X_op = O_big;
8996                         imm_expr.X_add_number = 4;
8997                         if (! target_big_endian)
8998                           {
8999                             generic_bignum[0] = bfd_getl16 (temp);
9000                             generic_bignum[1] = bfd_getl16 (temp + 2);
9001                             generic_bignum[2] = bfd_getl16 (temp + 4);
9002                             generic_bignum[3] = bfd_getl16 (temp + 6);
9003                           }
9004                         else
9005                           {
9006                             generic_bignum[0] = bfd_getb16 (temp + 6);
9007                             generic_bignum[1] = bfd_getb16 (temp + 4);
9008                             generic_bignum[2] = bfd_getb16 (temp + 2);
9009                             generic_bignum[3] = bfd_getb16 (temp);
9010                           }
9011                       }
9012                   }
9013                 else
9014                   {
9015                     const char *newname;
9016                     segT new_seg;
9017
9018                     /* Switch to the right section.  */
9019                     seg = now_seg;
9020                     subseg = now_subseg;
9021                     switch (*args)
9022                       {
9023                       default: /* unused default case avoids warnings.  */
9024                       case 'L':
9025                         newname = RDATA_SECTION_NAME;
9026                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9027                             || mips_pic == EMBEDDED_PIC)
9028                           newname = ".lit8";
9029                         break;
9030                       case 'F':
9031                         if (mips_pic == EMBEDDED_PIC)
9032                           newname = ".lit8";
9033                         else
9034                           newname = RDATA_SECTION_NAME;
9035                         break;
9036                       case 'l':
9037                         assert (!USE_GLOBAL_POINTER_OPT
9038                                 || g_switch_value >= 4);
9039                         newname = ".lit4";
9040                         break;
9041                       }
9042                     new_seg = subseg_new (newname, (subsegT) 0);
9043                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9044                       bfd_set_section_flags (stdoutput, new_seg,
9045                                              (SEC_ALLOC
9046                                               | SEC_LOAD
9047                                               | SEC_READONLY
9048                                               | SEC_DATA));
9049                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9050                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9051                         && strcmp (TARGET_OS, "elf") != 0)
9052                       record_alignment (new_seg, 4);
9053                     else
9054                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9055                     if (seg == now_seg)
9056                       as_bad (_("Can't use floating point insn in this section"));
9057
9058                     /* Set the argument to the current address in the
9059                        section.  */
9060                     offset_expr.X_op = O_symbol;
9061                     offset_expr.X_add_symbol =
9062                       symbol_new ("L0\001", now_seg,
9063                                   (valueT) frag_now_fix (), frag_now);
9064                     offset_expr.X_add_number = 0;
9065
9066                     /* Put the floating point number into the section.  */
9067                     p = frag_more ((int) length);
9068                     memcpy (p, temp, length);
9069
9070                     /* Switch back to the original section.  */
9071                     subseg_set (seg, subseg);
9072                   }
9073               }
9074               continue;
9075
9076             case 'i':           /* 16 bit unsigned immediate */
9077             case 'j':           /* 16 bit signed immediate */
9078               *imm_reloc = BFD_RELOC_LO16;
9079               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9080                 {
9081                   int more;
9082                   offsetT minval, maxval;
9083
9084                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9085                           && strcmp (insn->name, insn[1].name) == 0);
9086
9087                   /* If the expression was written as an unsigned number,
9088                      only treat it as signed if there are no more
9089                      alternatives.  */
9090                   if (more
9091                       && *args == 'j'
9092                       && sizeof (imm_expr.X_add_number) <= 4
9093                       && imm_expr.X_op == O_constant
9094                       && imm_expr.X_add_number < 0
9095                       && imm_expr.X_unsigned
9096                       && HAVE_64BIT_GPRS)
9097                     break;
9098
9099                   /* For compatibility with older assemblers, we accept
9100                      0x8000-0xffff as signed 16-bit numbers when only
9101                      signed numbers are allowed.  */
9102                   if (*args == 'i')
9103                     minval = 0, maxval = 0xffff;
9104                   else if (more)
9105                     minval = -0x8000, maxval = 0x7fff;
9106                   else
9107                     minval = -0x8000, maxval = 0xffff;
9108
9109                   if (imm_expr.X_op != O_constant
9110                       || imm_expr.X_add_number < minval
9111                       || imm_expr.X_add_number > maxval)
9112                     {
9113                       if (more)
9114                         break;
9115                       if (imm_expr.X_op == O_constant
9116                           || imm_expr.X_op == O_big)
9117                         as_bad (_("expression out of range"));
9118                     }
9119                 }
9120               s = expr_end;
9121               continue;
9122
9123             case 'o':           /* 16 bit offset */
9124               /* Check whether there is only a single bracketed expression
9125                  left.  If so, it must be the base register and the
9126                  constant must be zero.  */
9127               if (*s == '(' && strchr (s + 1, '(') == 0)
9128                 {
9129                   offset_expr.X_op = O_constant;
9130                   offset_expr.X_add_number = 0;
9131                   continue;
9132                 }
9133
9134               /* If this value won't fit into a 16 bit offset, then go
9135                  find a macro that will generate the 32 bit offset
9136                  code pattern.  */
9137               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9138                   && (offset_expr.X_op != O_constant
9139                       || offset_expr.X_add_number >= 0x8000
9140                       || offset_expr.X_add_number < -0x8000))
9141                 break;
9142
9143               s = expr_end;
9144               continue;
9145
9146             case 'p':           /* pc relative offset */
9147               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9148               my_getExpression (&offset_expr, s);
9149               s = expr_end;
9150               continue;
9151
9152             case 'u':           /* upper 16 bits */
9153               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9154                   && imm_expr.X_op == O_constant
9155                   && (imm_expr.X_add_number < 0
9156                       || imm_expr.X_add_number >= 0x10000))
9157                 as_bad (_("lui expression not in range 0..65535"));
9158               s = expr_end;
9159               continue;
9160
9161             case 'a':           /* 26 bit address */
9162               my_getExpression (&offset_expr, s);
9163               s = expr_end;
9164               *offset_reloc = BFD_RELOC_MIPS_JMP;
9165               continue;
9166
9167             case 'N':           /* 3 bit branch condition code */
9168             case 'M':           /* 3 bit compare condition code */
9169               if (strncmp (s, "$fcc", 4) != 0)
9170                 break;
9171               s += 4;
9172               regno = 0;
9173               do
9174                 {
9175                   regno *= 10;
9176                   regno += *s - '0';
9177                   ++s;
9178                 }
9179               while (ISDIGIT (*s));
9180               if (regno > 7)
9181                 as_bad (_("invalid condition code register $fcc%d"), regno);
9182               if (*args == 'N')
9183                 ip->insn_opcode |= regno << OP_SH_BCC;
9184               else
9185                 ip->insn_opcode |= regno << OP_SH_CCC;
9186               continue;
9187
9188             case 'H':
9189               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9190                 s += 2;
9191               if (ISDIGIT (*s))
9192                 {
9193                   c = 0;
9194                   do
9195                     {
9196                       c *= 10;
9197                       c += *s - '0';
9198                       ++s;
9199                     }
9200                   while (ISDIGIT (*s));
9201                 }
9202               else
9203                 c = 8; /* Invalid sel value.  */
9204
9205               if (c > 7)
9206                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9207               ip->insn_opcode |= c;
9208               continue;
9209
9210             case 'e':
9211               /* Must be at least one digit.  */
9212               my_getExpression (&imm_expr, s);
9213               check_absolute_expr (ip, &imm_expr);
9214
9215               if ((unsigned long) imm_expr.X_add_number
9216                   > (unsigned long) OP_MASK_VECBYTE)
9217                 {
9218                   as_bad (_("bad byte vector index (%ld)"),
9219                            (long) imm_expr.X_add_number);
9220                   imm_expr.X_add_number = 0;
9221                 }
9222
9223               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9224               imm_expr.X_op = O_absent;
9225               s = expr_end;
9226               continue;
9227
9228             case '%':
9229               my_getExpression (&imm_expr, s);
9230               check_absolute_expr (ip, &imm_expr);
9231
9232               if ((unsigned long) imm_expr.X_add_number
9233                   > (unsigned long) OP_MASK_VECALIGN)
9234                 {
9235                   as_bad (_("bad byte vector index (%ld)"),
9236                            (long) imm_expr.X_add_number);
9237                   imm_expr.X_add_number = 0;
9238                 }
9239
9240               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9241               imm_expr.X_op = O_absent;
9242               s = expr_end;
9243               continue;
9244
9245             default:
9246               as_bad (_("bad char = '%c'\n"), *args);
9247               internalError ();
9248             }
9249           break;
9250         }
9251       /* Args don't match.  */
9252       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9253           !strcmp (insn->name, insn[1].name))
9254         {
9255           ++insn;
9256           s = argsStart;
9257           insn_error = _("illegal operands");
9258           continue;
9259         }
9260       if (save_c)
9261         *(--s) = save_c;
9262       insn_error = _("illegal operands");
9263       return;
9264     }
9265 }
9266
9267 /* This routine assembles an instruction into its binary format when
9268    assembling for the mips16.  As a side effect, it sets one of the
9269    global variables imm_reloc or offset_reloc to the type of
9270    relocation to do if one of the operands is an address expression.
9271    It also sets mips16_small and mips16_ext if the user explicitly
9272    requested a small or extended instruction.  */
9273
9274 static void
9275 mips16_ip (char *str, struct mips_cl_insn *ip)
9276 {
9277   char *s;
9278   const char *args;
9279   struct mips_opcode *insn;
9280   char *argsstart;
9281   unsigned int regno;
9282   unsigned int lastregno = 0;
9283   char *s_reset;
9284
9285   insn_error = NULL;
9286
9287   mips16_small = FALSE;
9288   mips16_ext = FALSE;
9289
9290   for (s = str; ISLOWER (*s); ++s)
9291     ;
9292   switch (*s)
9293     {
9294     case '\0':
9295       break;
9296
9297     case ' ':
9298       *s++ = '\0';
9299       break;
9300
9301     case '.':
9302       if (s[1] == 't' && s[2] == ' ')
9303         {
9304           *s = '\0';
9305           mips16_small = TRUE;
9306           s += 3;
9307           break;
9308         }
9309       else if (s[1] == 'e' && s[2] == ' ')
9310         {
9311           *s = '\0';
9312           mips16_ext = TRUE;
9313           s += 3;
9314           break;
9315         }
9316       /* Fall through.  */
9317     default:
9318       insn_error = _("unknown opcode");
9319       return;
9320     }
9321
9322   if (mips_opts.noautoextend && ! mips16_ext)
9323     mips16_small = TRUE;
9324
9325   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9326     {
9327       insn_error = _("unrecognized opcode");
9328       return;
9329     }
9330
9331   argsstart = s;
9332   for (;;)
9333     {
9334       assert (strcmp (insn->name, str) == 0);
9335
9336       ip->insn_mo = insn;
9337       ip->insn_opcode = insn->match;
9338       ip->use_extend = FALSE;
9339       imm_expr.X_op = O_absent;
9340       imm_reloc[0] = BFD_RELOC_UNUSED;
9341       imm_reloc[1] = BFD_RELOC_UNUSED;
9342       imm_reloc[2] = BFD_RELOC_UNUSED;
9343       offset_expr.X_op = O_absent;
9344       offset_reloc[0] = BFD_RELOC_UNUSED;
9345       offset_reloc[1] = BFD_RELOC_UNUSED;
9346       offset_reloc[2] = BFD_RELOC_UNUSED;
9347       for (args = insn->args; 1; ++args)
9348         {
9349           int c;
9350
9351           if (*s == ' ')
9352             ++s;
9353
9354           /* In this switch statement we call break if we did not find
9355              a match, continue if we did find a match, or return if we
9356              are done.  */
9357
9358           c = *args;
9359           switch (c)
9360             {
9361             case '\0':
9362               if (*s == '\0')
9363                 {
9364                   /* Stuff the immediate value in now, if we can.  */
9365                   if (imm_expr.X_op == O_constant
9366                       && *imm_reloc > BFD_RELOC_UNUSED
9367                       && insn->pinfo != INSN_MACRO)
9368                     {
9369                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9370                                     imm_expr.X_add_number, TRUE, mips16_small,
9371                                     mips16_ext, &ip->insn_opcode,
9372                                     &ip->use_extend, &ip->extend);
9373                       imm_expr.X_op = O_absent;
9374                       *imm_reloc = BFD_RELOC_UNUSED;
9375                     }
9376
9377                   return;
9378                 }
9379               break;
9380
9381             case ',':
9382               if (*s++ == c)
9383                 continue;
9384               s--;
9385               switch (*++args)
9386                 {
9387                 case 'v':
9388                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9389                   continue;
9390                 case 'w':
9391                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9392                   continue;
9393                 }
9394               break;
9395
9396             case '(':
9397             case ')':
9398               if (*s++ == c)
9399                 continue;
9400               break;
9401
9402             case 'v':
9403             case 'w':
9404               if (s[0] != '$')
9405                 {
9406                   if (c == 'v')
9407                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9408                   else
9409                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9410                   ++args;
9411                   continue;
9412                 }
9413               /* Fall through.  */
9414             case 'x':
9415             case 'y':
9416             case 'z':
9417             case 'Z':
9418             case '0':
9419             case 'S':
9420             case 'R':
9421             case 'X':
9422             case 'Y':
9423               if (s[0] != '$')
9424                 break;
9425               s_reset = s;
9426               if (ISDIGIT (s[1]))
9427                 {
9428                   ++s;
9429                   regno = 0;
9430                   do
9431                     {
9432                       regno *= 10;
9433                       regno += *s - '0';
9434                       ++s;
9435                     }
9436                   while (ISDIGIT (*s));
9437                   if (regno > 31)
9438                     {
9439                       as_bad (_("invalid register number (%d)"), regno);
9440                       regno = 2;
9441                     }
9442                 }
9443               else
9444                 {
9445                   if (s[1] == 'r' && s[2] == 'a')
9446                     {
9447                       s += 3;
9448                       regno = RA;
9449                     }
9450                   else if (s[1] == 'f' && s[2] == 'p')
9451                     {
9452                       s += 3;
9453                       regno = FP;
9454                     }
9455                   else if (s[1] == 's' && s[2] == 'p')
9456                     {
9457                       s += 3;
9458                       regno = SP;
9459                     }
9460                   else if (s[1] == 'g' && s[2] == 'p')
9461                     {
9462                       s += 3;
9463                       regno = GP;
9464                     }
9465                   else if (s[1] == 'a' && s[2] == 't')
9466                     {
9467                       s += 3;
9468                       regno = AT;
9469                     }
9470                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9471                     {
9472                       s += 4;
9473                       regno = KT0;
9474                     }
9475                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9476                     {
9477                       s += 4;
9478                       regno = KT1;
9479                     }
9480                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9481                     {
9482                       s += 5;
9483                       regno = ZERO;
9484                     }
9485                   else
9486                     break;
9487                 }
9488
9489               if (*s == ' ')
9490                 ++s;
9491               if (args[1] != *s)
9492                 {
9493                   if (c == 'v' || c == 'w')
9494                     {
9495                       regno = mips16_to_32_reg_map[lastregno];
9496                       s = s_reset;
9497                       ++args;
9498                     }
9499                 }
9500
9501               switch (c)
9502                 {
9503                 case 'x':
9504                 case 'y':
9505                 case 'z':
9506                 case 'v':
9507                 case 'w':
9508                 case 'Z':
9509                   regno = mips32_to_16_reg_map[regno];
9510                   break;
9511
9512                 case '0':
9513                   if (regno != 0)
9514                     regno = ILLEGAL_REG;
9515                   break;
9516
9517                 case 'S':
9518                   if (regno != SP)
9519                     regno = ILLEGAL_REG;
9520                   break;
9521
9522                 case 'R':
9523                   if (regno != RA)
9524                     regno = ILLEGAL_REG;
9525                   break;
9526
9527                 case 'X':
9528                 case 'Y':
9529                   if (regno == AT && ! mips_opts.noat)
9530                     as_warn (_("used $at without \".set noat\""));
9531                   break;
9532
9533                 default:
9534                   internalError ();
9535                 }
9536
9537               if (regno == ILLEGAL_REG)
9538                 break;
9539
9540               switch (c)
9541                 {
9542                 case 'x':
9543                 case 'v':
9544                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9545                   break;
9546                 case 'y':
9547                 case 'w':
9548                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9549                   break;
9550                 case 'z':
9551                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9552                   break;
9553                 case 'Z':
9554                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9555                 case '0':
9556                 case 'S':
9557                 case 'R':
9558                   break;
9559                 case 'X':
9560                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9561                   break;
9562                 case 'Y':
9563                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9564                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9565                   break;
9566                 default:
9567                   internalError ();
9568                 }
9569
9570               lastregno = regno;
9571               continue;
9572
9573             case 'P':
9574               if (strncmp (s, "$pc", 3) == 0)
9575                 {
9576                   s += 3;
9577                   continue;
9578                 }
9579               break;
9580
9581             case '<':
9582             case '>':
9583             case '[':
9584             case ']':
9585             case '4':
9586             case '5':
9587             case 'H':
9588             case 'W':
9589             case 'D':
9590             case 'j':
9591             case '8':
9592             case 'V':
9593             case 'C':
9594             case 'U':
9595             case 'k':
9596             case 'K':
9597               if (s[0] == '%'
9598                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9599                 {
9600                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9601                      and generate the appropriate reloc.  If the text
9602                      inside %gprel is not a symbol name with an
9603                      optional offset, then we generate a normal reloc
9604                      and will probably fail later.  */
9605                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9606                   if (imm_expr.X_op == O_symbol)
9607                     {
9608                       mips16_ext = TRUE;
9609                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9610                       s = expr_end;
9611                       ip->use_extend = TRUE;
9612                       ip->extend = 0;
9613                       continue;
9614                     }
9615                 }
9616               else
9617                 {
9618                   /* Just pick up a normal expression.  */
9619                   my_getExpression (&imm_expr, s);
9620                 }
9621
9622               if (imm_expr.X_op == O_register)
9623                 {
9624                   /* What we thought was an expression turned out to
9625                      be a register.  */
9626
9627                   if (s[0] == '(' && args[1] == '(')
9628                     {
9629                       /* It looks like the expression was omitted
9630                          before a register indirection, which means
9631                          that the expression is implicitly zero.  We
9632                          still set up imm_expr, so that we handle
9633                          explicit extensions correctly.  */
9634                       imm_expr.X_op = O_constant;
9635                       imm_expr.X_add_number = 0;
9636                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9637                       continue;
9638                     }
9639
9640                   break;
9641                 }
9642
9643               /* We need to relax this instruction.  */
9644               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9645               s = expr_end;
9646               continue;
9647
9648             case 'p':
9649             case 'q':
9650             case 'A':
9651             case 'B':
9652             case 'E':
9653               /* We use offset_reloc rather than imm_reloc for the PC
9654                  relative operands.  This lets macros with both
9655                  immediate and address operands work correctly.  */
9656               my_getExpression (&offset_expr, s);
9657
9658               if (offset_expr.X_op == O_register)
9659                 break;
9660
9661               /* We need to relax this instruction.  */
9662               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9663               s = expr_end;
9664               continue;
9665
9666             case '6':           /* break code */
9667               my_getExpression (&imm_expr, s);
9668               check_absolute_expr (ip, &imm_expr);
9669               if ((unsigned long) imm_expr.X_add_number > 63)
9670                 {
9671                   as_warn (_("Invalid value for `%s' (%lu)"),
9672                            ip->insn_mo->name,
9673                            (unsigned long) imm_expr.X_add_number);
9674                   imm_expr.X_add_number &= 0x3f;
9675                 }
9676               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9677               imm_expr.X_op = O_absent;
9678               s = expr_end;
9679               continue;
9680
9681             case 'a':           /* 26 bit address */
9682               my_getExpression (&offset_expr, s);
9683               s = expr_end;
9684               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9685               ip->insn_opcode <<= 16;
9686               continue;
9687
9688             case 'l':           /* register list for entry macro */
9689             case 'L':           /* register list for exit macro */
9690               {
9691                 int mask;
9692
9693                 if (c == 'l')
9694                   mask = 0;
9695                 else
9696                   mask = 7 << 3;
9697                 while (*s != '\0')
9698                   {
9699                     int freg, reg1, reg2;
9700
9701                     while (*s == ' ' || *s == ',')
9702                       ++s;
9703                     if (*s != '$')
9704                       {
9705                         as_bad (_("can't parse register list"));
9706                         break;
9707                       }
9708                     ++s;
9709                     if (*s != 'f')
9710                       freg = 0;
9711                     else
9712                       {
9713                         freg = 1;
9714                         ++s;
9715                       }
9716                     reg1 = 0;
9717                     while (ISDIGIT (*s))
9718                       {
9719                         reg1 *= 10;
9720                         reg1 += *s - '0';
9721                         ++s;
9722                       }
9723                     if (*s == ' ')
9724                       ++s;
9725                     if (*s != '-')
9726                       reg2 = reg1;
9727                     else
9728                       {
9729                         ++s;
9730                         if (*s != '$')
9731                           break;
9732                         ++s;
9733                         if (freg)
9734                           {
9735                             if (*s == 'f')
9736                               ++s;
9737                             else
9738                               {
9739                                 as_bad (_("invalid register list"));
9740                                 break;
9741                               }
9742                           }
9743                         reg2 = 0;
9744                         while (ISDIGIT (*s))
9745                           {
9746                             reg2 *= 10;
9747                             reg2 += *s - '0';
9748                             ++s;
9749                           }
9750                       }
9751                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9752                       {
9753                         mask &= ~ (7 << 3);
9754                         mask |= 5 << 3;
9755                       }
9756                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9757                       {
9758                         mask &= ~ (7 << 3);
9759                         mask |= 6 << 3;
9760                       }
9761                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9762                       mask |= (reg2 - 3) << 3;
9763                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9764                       mask |= (reg2 - 15) << 1;
9765                     else if (reg1 == RA && reg2 == RA)
9766                       mask |= 1;
9767                     else
9768                       {
9769                         as_bad (_("invalid register list"));
9770                         break;
9771                       }
9772                   }
9773                 /* The mask is filled in in the opcode table for the
9774                    benefit of the disassembler.  We remove it before
9775                    applying the actual mask.  */
9776                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9777                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9778               }
9779             continue;
9780
9781             case 'e':           /* extend code */
9782               my_getExpression (&imm_expr, s);
9783               check_absolute_expr (ip, &imm_expr);
9784               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9785                 {
9786                   as_warn (_("Invalid value for `%s' (%lu)"),
9787                            ip->insn_mo->name,
9788                            (unsigned long) imm_expr.X_add_number);
9789                   imm_expr.X_add_number &= 0x7ff;
9790                 }
9791               ip->insn_opcode |= imm_expr.X_add_number;
9792               imm_expr.X_op = O_absent;
9793               s = expr_end;
9794               continue;
9795
9796             default:
9797               internalError ();
9798             }
9799           break;
9800         }
9801
9802       /* Args don't match.  */
9803       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9804           strcmp (insn->name, insn[1].name) == 0)
9805         {
9806           ++insn;
9807           s = argsstart;
9808           continue;
9809         }
9810
9811       insn_error = _("illegal operands");
9812
9813       return;
9814     }
9815 }
9816
9817 /* This structure holds information we know about a mips16 immediate
9818    argument type.  */
9819
9820 struct mips16_immed_operand
9821 {
9822   /* The type code used in the argument string in the opcode table.  */
9823   int type;
9824   /* The number of bits in the short form of the opcode.  */
9825   int nbits;
9826   /* The number of bits in the extended form of the opcode.  */
9827   int extbits;
9828   /* The amount by which the short form is shifted when it is used;
9829      for example, the sw instruction has a shift count of 2.  */
9830   int shift;
9831   /* The amount by which the short form is shifted when it is stored
9832      into the instruction code.  */
9833   int op_shift;
9834   /* Non-zero if the short form is unsigned.  */
9835   int unsp;
9836   /* Non-zero if the extended form is unsigned.  */
9837   int extu;
9838   /* Non-zero if the value is PC relative.  */
9839   int pcrel;
9840 };
9841
9842 /* The mips16 immediate operand types.  */
9843
9844 static const struct mips16_immed_operand mips16_immed_operands[] =
9845 {
9846   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9847   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9848   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9849   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9850   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9851   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9852   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9853   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9854   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9855   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9856   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9857   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9858   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9859   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9860   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9861   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9862   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9863   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9864   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9865   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9866   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9867 };
9868
9869 #define MIPS16_NUM_IMMED \
9870   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9871
9872 /* Handle a mips16 instruction with an immediate value.  This or's the
9873    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9874    whether an extended value is needed; if one is needed, it sets
9875    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9876    If SMALL is true, an unextended opcode was explicitly requested.
9877    If EXT is true, an extended opcode was explicitly requested.  If
9878    WARN is true, warn if EXT does not match reality.  */
9879
9880 static void
9881 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9882               bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9883               unsigned long *insn, bfd_boolean *use_extend,
9884               unsigned short *extend)
9885 {
9886   register const struct mips16_immed_operand *op;
9887   int mintiny, maxtiny;
9888   bfd_boolean needext;
9889
9890   op = mips16_immed_operands;
9891   while (op->type != type)
9892     {
9893       ++op;
9894       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9895     }
9896
9897   if (op->unsp)
9898     {
9899       if (type == '<' || type == '>' || type == '[' || type == ']')
9900         {
9901           mintiny = 1;
9902           maxtiny = 1 << op->nbits;
9903         }
9904       else
9905         {
9906           mintiny = 0;
9907           maxtiny = (1 << op->nbits) - 1;
9908         }
9909     }
9910   else
9911     {
9912       mintiny = - (1 << (op->nbits - 1));
9913       maxtiny = (1 << (op->nbits - 1)) - 1;
9914     }
9915
9916   /* Branch offsets have an implicit 0 in the lowest bit.  */
9917   if (type == 'p' || type == 'q')
9918     val /= 2;
9919
9920   if ((val & ((1 << op->shift) - 1)) != 0
9921       || val < (mintiny << op->shift)
9922       || val > (maxtiny << op->shift))
9923     needext = TRUE;
9924   else
9925     needext = FALSE;
9926
9927   if (warn && ext && ! needext)
9928     as_warn_where (file, line,
9929                    _("extended operand requested but not required"));
9930   if (small && needext)
9931     as_bad_where (file, line, _("invalid unextended operand value"));
9932
9933   if (small || (! ext && ! needext))
9934     {
9935       int insnval;
9936
9937       *use_extend = FALSE;
9938       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9939       insnval <<= op->op_shift;
9940       *insn |= insnval;
9941     }
9942   else
9943     {
9944       long minext, maxext;
9945       int extval;
9946
9947       if (op->extu)
9948         {
9949           minext = 0;
9950           maxext = (1 << op->extbits) - 1;
9951         }
9952       else
9953         {
9954           minext = - (1 << (op->extbits - 1));
9955           maxext = (1 << (op->extbits - 1)) - 1;
9956         }
9957       if (val < minext || val > maxext)
9958         as_bad_where (file, line,
9959                       _("operand value out of range for instruction"));
9960
9961       *use_extend = TRUE;
9962       if (op->extbits == 16)
9963         {
9964           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9965           val &= 0x1f;
9966         }
9967       else if (op->extbits == 15)
9968         {
9969           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9970           val &= 0xf;
9971         }
9972       else
9973         {
9974           extval = ((val & 0x1f) << 6) | (val & 0x20);
9975           val = 0;
9976         }
9977
9978       *extend = (unsigned short) extval;
9979       *insn |= val;
9980     }
9981 }
9982 \f
9983 static const struct percent_op_match
9984 {
9985   const char *str;
9986   bfd_reloc_code_real_type reloc;
9987 } percent_op[] =
9988 {
9989   {"%lo", BFD_RELOC_LO16},
9990 #ifdef OBJ_ELF
9991   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9992   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9993   {"%call16", BFD_RELOC_MIPS_CALL16},
9994   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9995   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9996   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9997   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9998   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9999   {"%got", BFD_RELOC_MIPS_GOT16},
10000   {"%gp_rel", BFD_RELOC_GPREL16},
10001   {"%half", BFD_RELOC_16},
10002   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10003   {"%higher", BFD_RELOC_MIPS_HIGHER},
10004   {"%neg", BFD_RELOC_MIPS_SUB},
10005 #endif
10006   {"%hi", BFD_RELOC_HI16_S}
10007 };
10008
10009
10010 /* Return true if *STR points to a relocation operator.  When returning true,
10011    move *STR over the operator and store its relocation code in *RELOC.
10012    Leave both *STR and *RELOC alone when returning false.  */
10013
10014 static bfd_boolean
10015 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
10016 {
10017   size_t i;
10018
10019   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10020     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10021       {
10022         *str += strlen (percent_op[i].str);
10023         *reloc = percent_op[i].reloc;
10024
10025         /* Check whether the output BFD supports this relocation.
10026            If not, issue an error and fall back on something safe.  */
10027         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10028           {
10029             as_bad ("relocation %s isn't supported by the current ABI",
10030                     percent_op[i].str);
10031             *reloc = BFD_RELOC_LO16;
10032           }
10033         return TRUE;
10034       }
10035   return FALSE;
10036 }
10037
10038
10039 /* Parse string STR as a 16-bit relocatable operand.  Store the
10040    expression in *EP and the relocations in the array starting
10041    at RELOC.  Return the number of relocation operators used.
10042
10043    On exit, EXPR_END points to the first character after the expression.
10044    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10045
10046 static size_t
10047 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10048                        char *str)
10049 {
10050   bfd_reloc_code_real_type reversed_reloc[3];
10051   size_t reloc_index, i;
10052   int crux_depth, str_depth;
10053   char *crux;
10054
10055   /* Search for the start of the main expression, recoding relocations
10056      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10057      of the main expression and with CRUX_DEPTH containing the number
10058      of open brackets at that point.  */
10059   reloc_index = -1;
10060   str_depth = 0;
10061   do
10062     {
10063       reloc_index++;
10064       crux = str;
10065       crux_depth = str_depth;
10066
10067       /* Skip over whitespace and brackets, keeping count of the number
10068          of brackets.  */
10069       while (*str == ' ' || *str == '\t' || *str == '(')
10070         if (*str++ == '(')
10071           str_depth++;
10072     }
10073   while (*str == '%'
10074          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10075          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10076
10077   my_getExpression (ep, crux);
10078   str = expr_end;
10079
10080   /* Match every open bracket.  */
10081   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10082     if (*str++ == ')')
10083       crux_depth--;
10084
10085   if (crux_depth > 0)
10086     as_bad ("unclosed '('");
10087
10088   expr_end = str;
10089
10090   if (reloc_index == 0)
10091     reloc[0] = BFD_RELOC_LO16;
10092   else
10093     {
10094       prev_reloc_op_frag = frag_now;
10095       for (i = 0; i < reloc_index; i++)
10096         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10097     }
10098
10099   return reloc_index;
10100 }
10101
10102 static void
10103 my_getExpression (expressionS *ep, char *str)
10104 {
10105   char *save_in;
10106   valueT val;
10107
10108   save_in = input_line_pointer;
10109   input_line_pointer = str;
10110   expression (ep);
10111   expr_end = input_line_pointer;
10112   input_line_pointer = save_in;
10113
10114   /* If we are in mips16 mode, and this is an expression based on `.',
10115      then we bump the value of the symbol by 1 since that is how other
10116      text symbols are handled.  We don't bother to handle complex
10117      expressions, just `.' plus or minus a constant.  */
10118   if (mips_opts.mips16
10119       && ep->X_op == O_symbol
10120       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10121       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10122       && symbol_get_frag (ep->X_add_symbol) == frag_now
10123       && symbol_constant_p (ep->X_add_symbol)
10124       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10125     S_SET_VALUE (ep->X_add_symbol, val + 1);
10126 }
10127
10128 /* Turn a string in input_line_pointer into a floating point constant
10129    of type TYPE, and store the appropriate bytes in *LITP.  The number
10130    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10131    returned, or NULL on OK.  */
10132
10133 char *
10134 md_atof (int type, char *litP, int *sizeP)
10135 {
10136   int prec;
10137   LITTLENUM_TYPE words[4];
10138   char *t;
10139   int i;
10140
10141   switch (type)
10142     {
10143     case 'f':
10144       prec = 2;
10145       break;
10146
10147     case 'd':
10148       prec = 4;
10149       break;
10150
10151     default:
10152       *sizeP = 0;
10153       return _("bad call to md_atof");
10154     }
10155
10156   t = atof_ieee (input_line_pointer, type, words);
10157   if (t)
10158     input_line_pointer = t;
10159
10160   *sizeP = prec * 2;
10161
10162   if (! target_big_endian)
10163     {
10164       for (i = prec - 1; i >= 0; i--)
10165         {
10166           md_number_to_chars (litP, words[i], 2);
10167           litP += 2;
10168         }
10169     }
10170   else
10171     {
10172       for (i = 0; i < prec; i++)
10173         {
10174           md_number_to_chars (litP, words[i], 2);
10175           litP += 2;
10176         }
10177     }
10178
10179   return NULL;
10180 }
10181
10182 void
10183 md_number_to_chars (char *buf, valueT val, int n)
10184 {
10185   if (target_big_endian)
10186     number_to_chars_bigendian (buf, val, n);
10187   else
10188     number_to_chars_littleendian (buf, val, n);
10189 }
10190 \f
10191 #ifdef OBJ_ELF
10192 static int support_64bit_objects(void)
10193 {
10194   const char **list, **l;
10195   int yes;
10196
10197   list = bfd_target_list ();
10198   for (l = list; *l != NULL; l++)
10199 #ifdef TE_TMIPS
10200     /* This is traditional mips */
10201     if (strcmp (*l, "elf64-tradbigmips") == 0
10202         || strcmp (*l, "elf64-tradlittlemips") == 0)
10203 #else
10204     if (strcmp (*l, "elf64-bigmips") == 0
10205         || strcmp (*l, "elf64-littlemips") == 0)
10206 #endif
10207       break;
10208   yes = (*l != NULL);
10209   free (list);
10210   return yes;
10211 }
10212 #endif /* OBJ_ELF */
10213
10214 const char *md_shortopts = "nO::g::G:";
10215
10216 struct option md_longopts[] =
10217 {
10218   /* Options which specify architecture.  */
10219 #define OPTION_ARCH_BASE    (OPTION_MD_BASE)
10220 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10221   {"march", required_argument, NULL, OPTION_MARCH},
10222 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10223   {"mtune", required_argument, NULL, OPTION_MTUNE},
10224 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10225   {"mips0", no_argument, NULL, OPTION_MIPS1},
10226   {"mips1", no_argument, NULL, OPTION_MIPS1},
10227 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10228   {"mips2", no_argument, NULL, OPTION_MIPS2},
10229 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10230   {"mips3", no_argument, NULL, OPTION_MIPS3},
10231 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10232   {"mips4", no_argument, NULL, OPTION_MIPS4},
10233 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10234   {"mips5", no_argument, NULL, OPTION_MIPS5},
10235 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10236   {"mips32", no_argument, NULL, OPTION_MIPS32},
10237 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10238   {"mips64", no_argument, NULL, OPTION_MIPS64},
10239 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10240   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10241
10242   /* Options which specify Application Specific Extensions (ASEs).  */
10243 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 10)
10244 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10245   {"mips16", no_argument, NULL, OPTION_MIPS16},
10246 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10247   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10248 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10249   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10250 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10251   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10252 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10253   {"mdmx", no_argument, NULL, OPTION_MDMX},
10254 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10255   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10256
10257   /* Old-style architecture options.  Don't add more of these.  */
10258 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10259 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10260   {"m4650", no_argument, NULL, OPTION_M4650},
10261 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10262   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10263 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10264   {"m4010", no_argument, NULL, OPTION_M4010},
10265 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10266   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10267 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10268   {"m4100", no_argument, NULL, OPTION_M4100},
10269 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10270   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10271 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10272   {"m3900", no_argument, NULL, OPTION_M3900},
10273 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10274   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10275
10276   /* Options which enable bug fixes.  */
10277 #define OPTION_FIX_BASE    (OPTION_COMPAT_ARCH_BASE + 8)
10278 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10279   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10280 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10281   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10282   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10283 #define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10284 #define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10285   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10286   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10287
10288   /* Miscellaneous options.  */
10289 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10290 #define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
10291   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10292 #define OPTION_TRAP (OPTION_MISC_BASE + 1)
10293   {"trap", no_argument, NULL, OPTION_TRAP},
10294   {"no-break", no_argument, NULL, OPTION_TRAP},
10295 #define OPTION_BREAK (OPTION_MISC_BASE + 2)
10296   {"break", no_argument, NULL, OPTION_BREAK},
10297   {"no-trap", no_argument, NULL, OPTION_BREAK},
10298 #define OPTION_EB (OPTION_MISC_BASE + 3)
10299   {"EB", no_argument, NULL, OPTION_EB},
10300 #define OPTION_EL (OPTION_MISC_BASE + 4)
10301   {"EL", no_argument, NULL, OPTION_EL},
10302 #define OPTION_FP32 (OPTION_MISC_BASE + 5)
10303   {"mfp32", no_argument, NULL, OPTION_FP32},
10304 #define OPTION_GP32 (OPTION_MISC_BASE + 6)
10305   {"mgp32", no_argument, NULL, OPTION_GP32},
10306 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10307   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10308 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
10309   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10310 #define OPTION_FP64 (OPTION_MISC_BASE + 9)
10311   {"mfp64", no_argument, NULL, OPTION_FP64},
10312 #define OPTION_GP64 (OPTION_MISC_BASE + 10)
10313   {"mgp64", no_argument, NULL, OPTION_GP64},
10314 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10315 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
10316   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10317   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10318
10319   /* ELF-specific options.  */
10320 #ifdef OBJ_ELF
10321 #define OPTION_ELF_BASE    (OPTION_MISC_BASE + 13)
10322 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10323   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10324   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10325 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10326   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10327 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10328   {"xgot",        no_argument, NULL, OPTION_XGOT},
10329 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10330   {"mabi", required_argument, NULL, OPTION_MABI},
10331 #define OPTION_32          (OPTION_ELF_BASE + 4)
10332   {"32",          no_argument, NULL, OPTION_32},
10333 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10334   {"n32",         no_argument, NULL, OPTION_N32},
10335 #define OPTION_64          (OPTION_ELF_BASE + 6)
10336   {"64",          no_argument, NULL, OPTION_64},
10337 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10338   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10339 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10340   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10341 #define OPTION_PDR         (OPTION_ELF_BASE + 9)
10342   {"mpdr", no_argument, NULL, OPTION_PDR},
10343 #define OPTION_NO_PDR      (OPTION_ELF_BASE + 10)
10344   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10345 #endif /* OBJ_ELF */
10346
10347   {NULL, no_argument, NULL, 0}
10348 };
10349 size_t md_longopts_size = sizeof (md_longopts);
10350
10351 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10352    NEW_VALUE.  Warn if another value was already specified.  Note:
10353    we have to defer parsing the -march and -mtune arguments in order
10354    to handle 'from-abi' correctly, since the ABI might be specified
10355    in a later argument.  */
10356
10357 static void
10358 mips_set_option_string (const char **string_ptr, const char *new_value)
10359 {
10360   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10361     as_warn (_("A different %s was already specified, is now %s"),
10362              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10363              new_value);
10364
10365   *string_ptr = new_value;
10366 }
10367
10368 int
10369 md_parse_option (int c, char *arg)
10370 {
10371   switch (c)
10372     {
10373     case OPTION_CONSTRUCT_FLOATS:
10374       mips_disable_float_construction = 0;
10375       break;
10376
10377     case OPTION_NO_CONSTRUCT_FLOATS:
10378       mips_disable_float_construction = 1;
10379       break;
10380
10381     case OPTION_TRAP:
10382       mips_trap = 1;
10383       break;
10384
10385     case OPTION_BREAK:
10386       mips_trap = 0;
10387       break;
10388
10389     case OPTION_EB:
10390       target_big_endian = 1;
10391       break;
10392
10393     case OPTION_EL:
10394       target_big_endian = 0;
10395       break;
10396
10397     case 'n':
10398       warn_nops = 1;
10399       break;
10400
10401     case 'O':
10402       if (arg && arg[1] == '0')
10403         mips_optimize = 1;
10404       else
10405         mips_optimize = 2;
10406       break;
10407
10408     case 'g':
10409       if (arg == NULL)
10410         mips_debug = 2;
10411       else
10412         mips_debug = atoi (arg);
10413       /* When the MIPS assembler sees -g or -g2, it does not do
10414          optimizations which limit full symbolic debugging.  We take
10415          that to be equivalent to -O0.  */
10416       if (mips_debug == 2)
10417         mips_optimize = 1;
10418       break;
10419
10420     case OPTION_MIPS1:
10421       file_mips_isa = ISA_MIPS1;
10422       break;
10423
10424     case OPTION_MIPS2:
10425       file_mips_isa = ISA_MIPS2;
10426       break;
10427
10428     case OPTION_MIPS3:
10429       file_mips_isa = ISA_MIPS3;
10430       break;
10431
10432     case OPTION_MIPS4:
10433       file_mips_isa = ISA_MIPS4;
10434       break;
10435
10436     case OPTION_MIPS5:
10437       file_mips_isa = ISA_MIPS5;
10438       break;
10439
10440     case OPTION_MIPS32:
10441       file_mips_isa = ISA_MIPS32;
10442       break;
10443
10444     case OPTION_MIPS32R2:
10445       file_mips_isa = ISA_MIPS32R2;
10446       break;
10447
10448     case OPTION_MIPS64:
10449       file_mips_isa = ISA_MIPS64;
10450       break;
10451
10452     case OPTION_MTUNE:
10453       mips_set_option_string (&mips_tune_string, arg);
10454       break;
10455
10456     case OPTION_MARCH:
10457       mips_set_option_string (&mips_arch_string, arg);
10458       break;
10459
10460     case OPTION_M4650:
10461       mips_set_option_string (&mips_arch_string, "4650");
10462       mips_set_option_string (&mips_tune_string, "4650");
10463       break;
10464
10465     case OPTION_NO_M4650:
10466       break;
10467
10468     case OPTION_M4010:
10469       mips_set_option_string (&mips_arch_string, "4010");
10470       mips_set_option_string (&mips_tune_string, "4010");
10471       break;
10472
10473     case OPTION_NO_M4010:
10474       break;
10475
10476     case OPTION_M4100:
10477       mips_set_option_string (&mips_arch_string, "4100");
10478       mips_set_option_string (&mips_tune_string, "4100");
10479       break;
10480
10481     case OPTION_NO_M4100:
10482       break;
10483
10484     case OPTION_M3900:
10485       mips_set_option_string (&mips_arch_string, "3900");
10486       mips_set_option_string (&mips_tune_string, "3900");
10487       break;
10488
10489     case OPTION_NO_M3900:
10490       break;
10491
10492     case OPTION_MDMX:
10493       mips_opts.ase_mdmx = 1;
10494       break;
10495
10496     case OPTION_NO_MDMX:
10497       mips_opts.ase_mdmx = 0;
10498       break;
10499
10500     case OPTION_MIPS16:
10501       mips_opts.mips16 = 1;
10502       mips_no_prev_insn (FALSE);
10503       break;
10504
10505     case OPTION_NO_MIPS16:
10506       mips_opts.mips16 = 0;
10507       mips_no_prev_insn (FALSE);
10508       break;
10509
10510     case OPTION_MIPS3D:
10511       mips_opts.ase_mips3d = 1;
10512       break;
10513
10514     case OPTION_NO_MIPS3D:
10515       mips_opts.ase_mips3d = 0;
10516       break;
10517
10518     case OPTION_MEMBEDDED_PIC:
10519       mips_pic = EMBEDDED_PIC;
10520       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10521         {
10522           as_bad (_("-G may not be used with embedded PIC code"));
10523           return 0;
10524         }
10525       g_switch_value = 0x7fffffff;
10526       break;
10527
10528     case OPTION_FIX_VR4122:
10529       mips_fix_4122_bugs = 1;
10530       break;
10531
10532     case OPTION_NO_FIX_VR4122:
10533       mips_fix_4122_bugs = 0;
10534       break;
10535
10536     case OPTION_RELAX_BRANCH:
10537       mips_relax_branch = 1;
10538       break;
10539
10540     case OPTION_NO_RELAX_BRANCH:
10541       mips_relax_branch = 0;
10542       break;
10543
10544 #ifdef OBJ_ELF
10545       /* When generating ELF code, we permit -KPIC and -call_shared to
10546          select SVR4_PIC, and -non_shared to select no PIC.  This is
10547          intended to be compatible with Irix 5.  */
10548     case OPTION_CALL_SHARED:
10549       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10550         {
10551           as_bad (_("-call_shared is supported only for ELF format"));
10552           return 0;
10553         }
10554       mips_pic = SVR4_PIC;
10555       mips_abicalls = TRUE;
10556       if (g_switch_seen && g_switch_value != 0)
10557         {
10558           as_bad (_("-G may not be used with SVR4 PIC code"));
10559           return 0;
10560         }
10561       g_switch_value = 0;
10562       break;
10563
10564     case OPTION_NON_SHARED:
10565       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10566         {
10567           as_bad (_("-non_shared is supported only for ELF format"));
10568           return 0;
10569         }
10570       mips_pic = NO_PIC;
10571       mips_abicalls = FALSE;
10572       break;
10573
10574       /* The -xgot option tells the assembler to use 32 offsets when
10575          accessing the got in SVR4_PIC mode.  It is for Irix
10576          compatibility.  */
10577     case OPTION_XGOT:
10578       mips_big_got = 1;
10579       break;
10580 #endif /* OBJ_ELF */
10581
10582     case 'G':
10583       if (! USE_GLOBAL_POINTER_OPT)
10584         {
10585           as_bad (_("-G is not supported for this configuration"));
10586           return 0;
10587         }
10588       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10589         {
10590           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10591           return 0;
10592         }
10593       else
10594         g_switch_value = atoi (arg);
10595       g_switch_seen = 1;
10596       break;
10597
10598 #ifdef OBJ_ELF
10599       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10600          and -mabi=64.  */
10601     case OPTION_32:
10602       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10603         {
10604           as_bad (_("-32 is supported for ELF format only"));
10605           return 0;
10606         }
10607       mips_abi = O32_ABI;
10608       break;
10609
10610     case OPTION_N32:
10611       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10612         {
10613           as_bad (_("-n32 is supported for ELF format only"));
10614           return 0;
10615         }
10616       mips_abi = N32_ABI;
10617       break;
10618
10619     case OPTION_64:
10620       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10621         {
10622           as_bad (_("-64 is supported for ELF format only"));
10623           return 0;
10624         }
10625       mips_abi = N64_ABI;
10626       if (! support_64bit_objects())
10627         as_fatal (_("No compiled in support for 64 bit object file format"));
10628       break;
10629 #endif /* OBJ_ELF */
10630
10631     case OPTION_GP32:
10632       file_mips_gp32 = 1;
10633       break;
10634
10635     case OPTION_GP64:
10636       file_mips_gp32 = 0;
10637       break;
10638
10639     case OPTION_FP32:
10640       file_mips_fp32 = 1;
10641       break;
10642
10643     case OPTION_FP64:
10644       file_mips_fp32 = 0;
10645       break;
10646
10647 #ifdef OBJ_ELF
10648     case OPTION_MABI:
10649       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10650         {
10651           as_bad (_("-mabi is supported for ELF format only"));
10652           return 0;
10653         }
10654       if (strcmp (arg, "32") == 0)
10655         mips_abi = O32_ABI;
10656       else if (strcmp (arg, "o64") == 0)
10657         mips_abi = O64_ABI;
10658       else if (strcmp (arg, "n32") == 0)
10659         mips_abi = N32_ABI;
10660       else if (strcmp (arg, "64") == 0)
10661         {
10662           mips_abi = N64_ABI;
10663           if (! support_64bit_objects())
10664             as_fatal (_("No compiled in support for 64 bit object file "
10665                         "format"));
10666         }
10667       else if (strcmp (arg, "eabi") == 0)
10668         mips_abi = EABI_ABI;
10669       else
10670         {
10671           as_fatal (_("invalid abi -mabi=%s"), arg);
10672           return 0;
10673         }
10674       break;
10675 #endif /* OBJ_ELF */
10676
10677     case OPTION_M7000_HILO_FIX:
10678       mips_7000_hilo_fix = TRUE;
10679       break;
10680
10681     case OPTION_MNO_7000_HILO_FIX:
10682       mips_7000_hilo_fix = FALSE;
10683       break;
10684
10685 #ifdef OBJ_ELF
10686     case OPTION_MDEBUG:
10687       mips_flag_mdebug = TRUE;
10688       break;
10689
10690     case OPTION_NO_MDEBUG:
10691       mips_flag_mdebug = FALSE;
10692       break;
10693
10694     case OPTION_PDR:
10695       mips_flag_pdr = TRUE;
10696       break;
10697
10698     case OPTION_NO_PDR:
10699       mips_flag_pdr = FALSE;
10700       break;
10701 #endif /* OBJ_ELF */
10702
10703     default:
10704       return 0;
10705     }
10706
10707   return 1;
10708 }
10709 \f
10710 /* Set up globals to generate code for the ISA or processor
10711    described by INFO.  */
10712
10713 static void
10714 mips_set_architecture (const struct mips_cpu_info *info)
10715 {
10716   if (info != 0)
10717     {
10718       file_mips_arch = info->cpu;
10719       mips_opts.arch = info->cpu;
10720       mips_opts.isa = info->isa;
10721     }
10722 }
10723
10724
10725 /* Likewise for tuning.  */
10726
10727 static void
10728 mips_set_tune (const struct mips_cpu_info *info)
10729 {
10730   if (info != 0)
10731     mips_tune = info->cpu;
10732 }
10733
10734
10735 void
10736 mips_after_parse_args (void)
10737 {
10738   const struct mips_cpu_info *arch_info = 0;
10739   const struct mips_cpu_info *tune_info = 0;
10740
10741   /* GP relative stuff not working for PE */
10742   if (strncmp (TARGET_OS, "pe", 2) == 0
10743       && g_switch_value != 0)
10744     {
10745       if (g_switch_seen)
10746         as_bad (_("-G not supported in this configuration."));
10747       g_switch_value = 0;
10748     }
10749
10750   if (mips_abi == NO_ABI)
10751     mips_abi = MIPS_DEFAULT_ABI;
10752
10753   /* The following code determines the architecture and register size.
10754      Similar code was added to GCC 3.3 (see override_options() in
10755      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10756      as much as possible.  */
10757
10758   if (mips_arch_string != 0)
10759     arch_info = mips_parse_cpu ("-march", mips_arch_string);
10760
10761   if (file_mips_isa != ISA_UNKNOWN)
10762     {
10763       /* Handle -mipsN.  At this point, file_mips_isa contains the
10764          ISA level specified by -mipsN, while arch_info->isa contains
10765          the -march selection (if any).  */
10766       if (arch_info != 0)
10767         {
10768           /* -march takes precedence over -mipsN, since it is more descriptive.
10769              There's no harm in specifying both as long as the ISA levels
10770              are the same.  */
10771           if (file_mips_isa != arch_info->isa)
10772             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10773                     mips_cpu_info_from_isa (file_mips_isa)->name,
10774                     mips_cpu_info_from_isa (arch_info->isa)->name);
10775         }
10776       else
10777         arch_info = mips_cpu_info_from_isa (file_mips_isa);
10778     }
10779
10780   if (arch_info == 0)
10781     arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10782
10783   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10784     as_bad ("-march=%s is not compatible with the selected ABI",
10785             arch_info->name);
10786
10787   mips_set_architecture (arch_info);
10788
10789   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
10790   if (mips_tune_string != 0)
10791     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10792
10793   if (tune_info == 0)
10794     mips_set_tune (arch_info);
10795   else
10796     mips_set_tune (tune_info);
10797
10798   if (file_mips_gp32 >= 0)
10799     {
10800       /* The user specified the size of the integer registers.  Make sure
10801          it agrees with the ABI and ISA.  */
10802       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10803         as_bad (_("-mgp64 used with a 32-bit processor"));
10804       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10805         as_bad (_("-mgp32 used with a 64-bit ABI"));
10806       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10807         as_bad (_("-mgp64 used with a 32-bit ABI"));
10808     }
10809   else
10810     {
10811       /* Infer the integer register size from the ABI and processor.
10812          Restrict ourselves to 32-bit registers if that's all the
10813          processor has, or if the ABI cannot handle 64-bit registers.  */
10814       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10815                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10816     }
10817
10818   /* ??? GAS treats single-float processors as though they had 64-bit
10819      float registers (although it complains when double-precision
10820      instructions are used).  As things stand, saying they have 32-bit
10821      registers would lead to spurious "register must be even" messages.
10822      So here we assume float registers are always the same size as
10823      integer ones, unless the user says otherwise.  */
10824   if (file_mips_fp32 < 0)
10825     file_mips_fp32 = file_mips_gp32;
10826
10827   /* End of GCC-shared inference code.  */
10828
10829   /* This flag is set when we have a 64-bit capable CPU but use only
10830      32-bit wide registers.  Note that EABI does not use it.  */
10831   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10832       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10833           || mips_abi == O32_ABI))
10834     mips_32bitmode = 1;
10835
10836   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10837     as_bad (_("trap exception not supported at ISA 1"));
10838
10839   /* If the selected architecture includes support for ASEs, enable
10840      generation of code for them.  */
10841   if (mips_opts.mips16 == -1)
10842     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10843   if (mips_opts.ase_mips3d == -1)
10844     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10845   if (mips_opts.ase_mdmx == -1)
10846     mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10847
10848   file_mips_isa = mips_opts.isa;
10849   file_ase_mips16 = mips_opts.mips16;
10850   file_ase_mips3d = mips_opts.ase_mips3d;
10851   file_ase_mdmx = mips_opts.ase_mdmx;
10852   mips_opts.gp32 = file_mips_gp32;
10853   mips_opts.fp32 = file_mips_fp32;
10854
10855   if (mips_flag_mdebug < 0)
10856     {
10857 #ifdef OBJ_MAYBE_ECOFF
10858       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10859         mips_flag_mdebug = 1;
10860       else
10861 #endif /* OBJ_MAYBE_ECOFF */
10862         mips_flag_mdebug = 0;
10863     }
10864 }
10865 \f
10866 void
10867 mips_init_after_args (void)
10868 {
10869   /* initialize opcodes */
10870   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10871   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10872 }
10873
10874 long
10875 md_pcrel_from (fixS *fixP)
10876 {
10877   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10878   switch (fixP->fx_r_type)
10879     {
10880     case BFD_RELOC_16_PCREL_S2:
10881     case BFD_RELOC_MIPS_JMP:
10882       /* Return the address of the delay slot.  */
10883       return addr + 4;
10884     default:
10885       return addr;
10886     }
10887 }
10888
10889 /* This is called before the symbol table is processed.  In order to
10890    work with gcc when using mips-tfile, we must keep all local labels.
10891    However, in other cases, we want to discard them.  If we were
10892    called with -g, but we didn't see any debugging information, it may
10893    mean that gcc is smuggling debugging information through to
10894    mips-tfile, in which case we must generate all local labels.  */
10895
10896 void
10897 mips_frob_file_before_adjust (void)
10898 {
10899 #ifndef NO_ECOFF_DEBUGGING
10900   if (ECOFF_DEBUGGING
10901       && mips_debug != 0
10902       && ! ecoff_debugging_seen)
10903     flag_keep_locals = 1;
10904 #endif
10905 }
10906
10907 /* Sort any unmatched HI16_S relocs so that they immediately precede
10908    the corresponding LO reloc.  This is called before md_apply_fix3 and
10909    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10910    explicit use of the %hi modifier.  */
10911
10912 void
10913 mips_frob_file (void)
10914 {
10915   struct mips_hi_fixup *l;
10916
10917   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10918     {
10919       segment_info_type *seginfo;
10920       int pass;
10921
10922       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10923
10924       /* If a GOT16 relocation turns out to be against a global symbol,
10925          there isn't supposed to be a matching LO.  */
10926       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10927           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10928         continue;
10929
10930       /* Check quickly whether the next fixup happens to be a matching %lo.  */
10931       if (fixup_has_matching_lo_p (l->fixp))
10932         continue;
10933
10934       /* Look through the fixups for this segment for a matching %lo.
10935          When we find one, move the %hi just in front of it.  We do
10936          this in two passes.  In the first pass, we try to find a
10937          unique %lo.  In the second pass, we permit multiple %hi
10938          relocs for a single %lo (this is a GNU extension).  */
10939       seginfo = seg_info (l->seg);
10940       for (pass = 0; pass < 2; pass++)
10941         {
10942           fixS *f, *prev;
10943
10944           prev = NULL;
10945           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10946             {
10947               /* Check whether this is a %lo fixup which matches l->fixp.  */
10948               if (f->fx_r_type == BFD_RELOC_LO16
10949                   && f->fx_addsy == l->fixp->fx_addsy
10950                   && f->fx_offset == l->fixp->fx_offset
10951                   && (pass == 1
10952                       || prev == NULL
10953                       || !reloc_needs_lo_p (prev->fx_r_type)
10954                       || !fixup_has_matching_lo_p (prev)))
10955                 {
10956                   fixS **pf;
10957
10958                   /* Move l->fixp before f.  */
10959                   for (pf = &seginfo->fix_root;
10960                        *pf != l->fixp;
10961                        pf = &(*pf)->fx_next)
10962                     assert (*pf != NULL);
10963
10964                   *pf = l->fixp->fx_next;
10965
10966                   l->fixp->fx_next = f;
10967                   if (prev == NULL)
10968                     seginfo->fix_root = l->fixp;
10969                   else
10970                     prev->fx_next = l->fixp;
10971
10972                   break;
10973                 }
10974
10975               prev = f;
10976             }
10977
10978           if (f != NULL)
10979             break;
10980
10981 #if 0 /* GCC code motion plus incomplete dead code elimination
10982          can leave a %hi without a %lo.  */
10983           if (pass == 1)
10984             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10985                            _("Unmatched %%hi reloc"));
10986 #endif
10987         }
10988     }
10989 }
10990
10991 /* When generating embedded PIC code we need to use a special
10992    relocation to represent the difference of two symbols in the .text
10993    section (switch tables use a difference of this sort).  See
10994    include/coff/mips.h for details.  This macro checks whether this
10995    fixup requires the special reloc.  */
10996 #define SWITCH_TABLE(fixp) \
10997   ((fixp)->fx_r_type == BFD_RELOC_32 \
10998    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10999    && (fixp)->fx_addsy != NULL \
11000    && (fixp)->fx_subsy != NULL \
11001    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11002    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11003
11004 /* When generating embedded PIC code we must keep all PC relative
11005    relocations, in case the linker has to relax a call.  We also need
11006    to keep relocations for switch table entries.
11007
11008    We may have combined relocations without symbols in the N32/N64 ABI.
11009    We have to prevent gas from dropping them.  */
11010
11011 int
11012 mips_force_relocation (fixS *fixp)
11013 {
11014   if (generic_force_reloc (fixp))
11015     return 1;
11016
11017   if (HAVE_NEWABI
11018       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11019       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11020           || fixp->fx_r_type == BFD_RELOC_HI16_S
11021           || fixp->fx_r_type == BFD_RELOC_LO16))
11022     return 1;
11023
11024   return (mips_pic == EMBEDDED_PIC
11025           && (fixp->fx_pcrel
11026               || SWITCH_TABLE (fixp)
11027               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11028               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11029 }
11030
11031 /* This hook is called before a fix is simplified.  We don't really
11032    decide whether to skip a fix here.  Rather, we turn global symbols
11033    used as branch targets into local symbols, such that they undergo
11034    simplification.  We can only do this if the symbol is defined and
11035    it is in the same section as the branch.  If this doesn't hold, we
11036    emit a better error message than just saying the relocation is not
11037    valid for the selected object format.
11038
11039    FIXP is the fix-up we're going to try to simplify, SEG is the
11040    segment in which the fix up occurs.  The return value should be
11041    non-zero to indicate the fix-up is valid for further
11042    simplifications.  */
11043
11044 int
11045 mips_validate_fix (struct fix *fixP, asection *seg)
11046 {
11047   /* There's a lot of discussion on whether it should be possible to
11048      use R_MIPS_PC16 to represent branch relocations.  The outcome
11049      seems to be that it can, but gas/bfd are very broken in creating
11050      RELA relocations for this, so for now we only accept branches to
11051      symbols in the same section.  Anything else is of dubious value,
11052      since there's no guarantee that at link time the symbol would be
11053      in range.  Even for branches to local symbols this is arguably
11054      wrong, since it we assume the symbol is not going to be
11055      overridden, which should be possible per ELF library semantics,
11056      but then, there isn't a dynamic relocation that could be used to
11057      this effect, and the target would likely be out of range as well.
11058
11059      Unfortunately, it seems that there is too much code out there
11060      that relies on branches to symbols that are global to be resolved
11061      as if they were local, like the IRIX tools do, so we do it as
11062      well, but with a warning so that people are reminded to fix their
11063      code.  If we ever get back to using R_MIPS_PC16 for branch
11064      targets, this entire block should go away (and probably the
11065      whole function).  */
11066
11067   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11068       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11069             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11070            && mips_pic != EMBEDDED_PIC)
11071           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11072       && fixP->fx_addsy)
11073     {
11074       if (! S_IS_DEFINED (fixP->fx_addsy))
11075         {
11076           as_bad_where (fixP->fx_file, fixP->fx_line,
11077                         _("Cannot branch to undefined symbol."));
11078           /* Avoid any further errors about this fixup.  */
11079           fixP->fx_done = 1;
11080         }
11081       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11082         {
11083           as_bad_where (fixP->fx_file, fixP->fx_line,
11084                         _("Cannot branch to symbol in another section."));
11085           fixP->fx_done = 1;
11086         }
11087       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11088         {
11089           symbolS *sym = fixP->fx_addsy;
11090
11091           if (mips_pic == SVR4_PIC)
11092             as_warn_where (fixP->fx_file, fixP->fx_line,
11093                            _("Pretending global symbol used as branch target is local."));
11094
11095           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11096                                           S_GET_SEGMENT (sym),
11097                                           S_GET_VALUE (sym),
11098                                           symbol_get_frag (sym));
11099           copy_symbol_attributes (fixP->fx_addsy, sym);
11100           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11101           assert (symbol_resolved_p (sym));
11102           symbol_mark_resolved (fixP->fx_addsy);
11103         }
11104     }
11105
11106   return 1;
11107 }
11108
11109 #ifdef OBJ_ELF
11110 static int
11111 mips_need_elf_addend_fixup (fixS *fixP)
11112 {
11113   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11114     return 1;
11115   if (mips_pic == EMBEDDED_PIC
11116       && S_IS_WEAK (fixP->fx_addsy))
11117     return 1;
11118   if (mips_pic != EMBEDDED_PIC
11119       && (S_IS_WEAK (fixP->fx_addsy)
11120           || S_IS_EXTERNAL (fixP->fx_addsy))
11121       && !S_IS_COMMON (fixP->fx_addsy))
11122     return 1;
11123   if (((bfd_get_section_flags (stdoutput,
11124                                S_GET_SEGMENT (fixP->fx_addsy))
11125         & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11126       || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11127                    ".gnu.linkonce",
11128                    sizeof (".gnu.linkonce") - 1))
11129     return 1;
11130   return 0;
11131 }
11132 #endif
11133
11134 /* Apply a fixup to the object file.  */
11135
11136 void
11137 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11138 {
11139   bfd_byte *buf;
11140   long insn;
11141   static int previous_fx_r_type = 0;
11142   reloc_howto_type *howto;
11143
11144   /* We ignore generic BFD relocations we don't know about.  */
11145   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11146   if (! howto)
11147     return;
11148
11149   assert (fixP->fx_size == 4
11150           || fixP->fx_r_type == BFD_RELOC_16
11151           || fixP->fx_r_type == BFD_RELOC_64
11152           || fixP->fx_r_type == BFD_RELOC_CTOR
11153           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11154           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11155           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11156
11157   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11158
11159   /* If we aren't adjusting this fixup to be against the section
11160      symbol, we need to adjust the value.  */
11161 #ifdef OBJ_ELF
11162   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11163     {
11164       if (mips_need_elf_addend_fixup (fixP)
11165           && howto->partial_inplace
11166           && fixP->fx_r_type != BFD_RELOC_GPREL16
11167           && fixP->fx_r_type != BFD_RELOC_GPREL32
11168           && fixP->fx_r_type != BFD_RELOC_MIPS16_GPREL)
11169         {
11170           /* In this case, the bfd_install_relocation routine will
11171              incorrectly add the symbol value back in.  We just want
11172              the addend to appear in the object file.
11173
11174              The condition above used to include
11175              "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11176
11177              However, howto can't be trusted here, because we
11178              might change the reloc type in tc_gen_reloc.  We can
11179              check howto->partial_inplace because that conversion
11180              happens to preserve howto->partial_inplace; but it
11181              does not preserve howto->pcrel_offset.  I've just
11182              eliminated the check, because all MIPS PC-relative
11183              relocations are marked howto->pcrel_offset.
11184
11185              howto->pcrel_offset was originally added for
11186              R_MIPS_PC16, which is generated for code like
11187
11188                     globl g1 .text
11189                     .text
11190                     .space 20
11191              g1:
11192              x:
11193                     bal g1
11194            */
11195           *valP -= S_GET_VALUE (fixP->fx_addsy);
11196         }
11197
11198       /* This code was generated using trial and error and so is
11199          fragile and not trustworthy.  If you change it, you should
11200          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11201          they still pass.  */
11202       if (fixP->fx_pcrel)
11203         {
11204           *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11205
11206           /* BFD's REL handling, for MIPS, is _very_ weird.
11207              This gives the right results, but it can't possibly
11208              be the way things are supposed to work.  */
11209           *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11210         }
11211     }
11212 #endif
11213
11214   /* We are not done if this is a composite relocation to set up gp.  */
11215   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11216       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11217            || (fixP->fx_r_type == BFD_RELOC_64
11218                && (previous_fx_r_type == BFD_RELOC_GPREL32
11219                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11220            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11221                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11222                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11223     fixP->fx_done = 1;
11224   previous_fx_r_type = fixP->fx_r_type;
11225
11226   switch (fixP->fx_r_type)
11227     {
11228     case BFD_RELOC_MIPS_JMP:
11229     case BFD_RELOC_MIPS_SHIFT5:
11230     case BFD_RELOC_MIPS_SHIFT6:
11231     case BFD_RELOC_MIPS_GOT_DISP:
11232     case BFD_RELOC_MIPS_GOT_PAGE:
11233     case BFD_RELOC_MIPS_GOT_OFST:
11234     case BFD_RELOC_MIPS_SUB:
11235     case BFD_RELOC_MIPS_INSERT_A:
11236     case BFD_RELOC_MIPS_INSERT_B:
11237     case BFD_RELOC_MIPS_DELETE:
11238     case BFD_RELOC_MIPS_HIGHEST:
11239     case BFD_RELOC_MIPS_HIGHER:
11240     case BFD_RELOC_MIPS_SCN_DISP:
11241     case BFD_RELOC_MIPS_REL16:
11242     case BFD_RELOC_MIPS_RELGOT:
11243     case BFD_RELOC_MIPS_JALR:
11244     case BFD_RELOC_HI16:
11245     case BFD_RELOC_HI16_S:
11246     case BFD_RELOC_GPREL16:
11247     case BFD_RELOC_MIPS_LITERAL:
11248     case BFD_RELOC_MIPS_CALL16:
11249     case BFD_RELOC_MIPS_GOT16:
11250     case BFD_RELOC_GPREL32:
11251     case BFD_RELOC_MIPS_GOT_HI16:
11252     case BFD_RELOC_MIPS_GOT_LO16:
11253     case BFD_RELOC_MIPS_CALL_HI16:
11254     case BFD_RELOC_MIPS_CALL_LO16:
11255     case BFD_RELOC_MIPS16_GPREL:
11256       if (fixP->fx_pcrel)
11257         as_bad_where (fixP->fx_file, fixP->fx_line,
11258                       _("Invalid PC relative reloc"));
11259       /* Nothing needed to do. The value comes from the reloc entry */
11260       break;
11261
11262     case BFD_RELOC_MIPS16_JMP:
11263       /* We currently always generate a reloc against a symbol, which
11264          means that we don't want an addend even if the symbol is
11265          defined.  */
11266       *valP = 0;
11267       break;
11268
11269     case BFD_RELOC_PCREL_HI16_S:
11270       /* The addend for this is tricky if it is internal, so we just
11271          do everything here rather than in bfd_install_relocation.  */
11272       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11273         break;
11274       if (fixP->fx_addsy
11275           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11276         {
11277           /* For an external symbol adjust by the address to make it
11278              pcrel_offset.  We use the address of the RELLO reloc
11279              which follows this one.  */
11280           *valP += (fixP->fx_next->fx_frag->fr_address
11281                     + fixP->fx_next->fx_where);
11282         }
11283       *valP = ((*valP + 0x8000) >> 16) & 0xffff;
11284       if (target_big_endian)
11285         buf += 2;
11286       md_number_to_chars (buf, *valP, 2);
11287       break;
11288
11289     case BFD_RELOC_PCREL_LO16:
11290       /* The addend for this is tricky if it is internal, so we just
11291          do everything here rather than in bfd_install_relocation.  */
11292       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11293         break;
11294       if (fixP->fx_addsy
11295           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11296         *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11297       if (target_big_endian)
11298         buf += 2;
11299       md_number_to_chars (buf, *valP, 2);
11300       break;
11301
11302     case BFD_RELOC_64:
11303       /* This is handled like BFD_RELOC_32, but we output a sign
11304          extended value if we are only 32 bits.  */
11305       if (fixP->fx_done
11306           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11307         {
11308           if (8 <= sizeof (valueT))
11309             md_number_to_chars (buf, *valP, 8);
11310           else
11311             {
11312               valueT hiv;
11313
11314               if ((*valP & 0x80000000) != 0)
11315                 hiv = 0xffffffff;
11316               else
11317                 hiv = 0;
11318               md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11319                                   *valP, 4);
11320               md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11321                                   hiv, 4);
11322             }
11323         }
11324       break;
11325
11326     case BFD_RELOC_RVA:
11327     case BFD_RELOC_32:
11328       /* If we are deleting this reloc entry, we must fill in the
11329          value now.  This can happen if we have a .word which is not
11330          resolved when it appears but is later defined.  We also need
11331          to fill in the value if this is an embedded PIC switch table
11332          entry.  */
11333       if (fixP->fx_done
11334           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11335         md_number_to_chars (buf, *valP, 4);
11336       break;
11337
11338     case BFD_RELOC_16:
11339       /* If we are deleting this reloc entry, we must fill in the
11340          value now.  */
11341       assert (fixP->fx_size == 2);
11342       if (fixP->fx_done)
11343         md_number_to_chars (buf, *valP, 2);
11344       break;
11345
11346     case BFD_RELOC_LO16:
11347       /* When handling an embedded PIC switch statement, we can wind
11348          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11349       if (fixP->fx_done)
11350         {
11351           if (*valP + 0x8000 > 0xffff)
11352             as_bad_where (fixP->fx_file, fixP->fx_line,
11353                           _("relocation overflow"));
11354           if (target_big_endian)
11355             buf += 2;
11356           md_number_to_chars (buf, *valP, 2);
11357         }
11358       break;
11359
11360     case BFD_RELOC_16_PCREL_S2:
11361       if ((*valP & 0x3) != 0)
11362         as_bad_where (fixP->fx_file, fixP->fx_line,
11363                       _("Branch to odd address (%lx)"), (long) *valP);
11364
11365       /*
11366        * We need to save the bits in the instruction since fixup_segment()
11367        * might be deleting the relocation entry (i.e., a branch within
11368        * the current segment).
11369        */
11370       if (! fixP->fx_done)
11371         break;
11372
11373       /* update old instruction data */
11374       if (target_big_endian)
11375         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11376       else
11377         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11378
11379       if (*valP + 0x20000 <= 0x3ffff)
11380         {
11381           insn |= (*valP >> 2) & 0xffff;
11382           md_number_to_chars (buf, insn, 4);
11383         }
11384       else if (mips_pic == NO_PIC
11385                && fixP->fx_done
11386                && fixP->fx_frag->fr_address >= text_section->vma
11387                && (fixP->fx_frag->fr_address
11388                    < text_section->vma + text_section->_raw_size)
11389                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
11390                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
11391                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11392         {
11393           /* The branch offset is too large.  If this is an
11394              unconditional branch, and we are not generating PIC code,
11395              we can convert it to an absolute jump instruction.  */
11396           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
11397             insn = 0x0c000000;  /* jal */
11398           else
11399             insn = 0x08000000;  /* j */
11400           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11401           fixP->fx_done = 0;
11402           fixP->fx_addsy = section_symbol (text_section);
11403           *valP += md_pcrel_from (fixP);
11404           md_number_to_chars (buf, insn, 4);
11405         }
11406       else
11407         {
11408           /* If we got here, we have branch-relaxation disabled,
11409              and there's nothing we can do to fix this instruction
11410              without turning it into a longer sequence.  */
11411           as_bad_where (fixP->fx_file, fixP->fx_line,
11412                         _("Branch out of range"));
11413         }
11414       break;
11415
11416     case BFD_RELOC_VTABLE_INHERIT:
11417       fixP->fx_done = 0;
11418       if (fixP->fx_addsy
11419           && !S_IS_DEFINED (fixP->fx_addsy)
11420           && !S_IS_WEAK (fixP->fx_addsy))
11421         S_SET_WEAK (fixP->fx_addsy);
11422       break;
11423
11424     case BFD_RELOC_VTABLE_ENTRY:
11425       fixP->fx_done = 0;
11426       break;
11427
11428     default:
11429       internalError ();
11430     }
11431
11432   /* Remember value for tc_gen_reloc.  */
11433   fixP->fx_addnumber = *valP;
11434 }
11435
11436 #if 0
11437 void
11438 printInsn (unsigned long oc)
11439 {
11440   const struct mips_opcode *p;
11441   int treg, sreg, dreg, shamt;
11442   short imm;
11443   const char *args;
11444   int i;
11445
11446   for (i = 0; i < NUMOPCODES; ++i)
11447     {
11448       p = &mips_opcodes[i];
11449       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11450         {
11451           printf ("%08lx %s\t", oc, p->name);
11452           treg = (oc >> 16) & 0x1f;
11453           sreg = (oc >> 21) & 0x1f;
11454           dreg = (oc >> 11) & 0x1f;
11455           shamt = (oc >> 6) & 0x1f;
11456           imm = oc;
11457           for (args = p->args;; ++args)
11458             {
11459               switch (*args)
11460                 {
11461                 case '\0':
11462                   printf ("\n");
11463                   break;
11464
11465                 case ',':
11466                 case '(':
11467                 case ')':
11468                   printf ("%c", *args);
11469                   continue;
11470
11471                 case 'r':
11472                   assert (treg == sreg);
11473                   printf ("$%d,$%d", treg, sreg);
11474                   continue;
11475
11476                 case 'd':
11477                 case 'G':
11478                   printf ("$%d", dreg);
11479                   continue;
11480
11481                 case 't':
11482                 case 'E':
11483                   printf ("$%d", treg);
11484                   continue;
11485
11486                 case 'k':
11487                   printf ("0x%x", treg);
11488                   continue;
11489
11490                 case 'b':
11491                 case 's':
11492                   printf ("$%d", sreg);
11493                   continue;
11494
11495                 case 'a':
11496                   printf ("0x%08lx", oc & 0x1ffffff);
11497                   continue;
11498
11499                 case 'i':
11500                 case 'j':
11501                 case 'o':
11502                 case 'u':
11503                   printf ("%d", imm);
11504                   continue;
11505
11506                 case '<':
11507                 case '>':
11508                   printf ("$%d", shamt);
11509                   continue;
11510
11511                 default:
11512                   internalError ();
11513                 }
11514               break;
11515             }
11516           return;
11517         }
11518     }
11519   printf (_("%08lx  UNDEFINED\n"), oc);
11520 }
11521 #endif
11522
11523 static symbolS *
11524 get_symbol (void)
11525 {
11526   int c;
11527   char *name;
11528   symbolS *p;
11529
11530   name = input_line_pointer;
11531   c = get_symbol_end ();
11532   p = (symbolS *) symbol_find_or_make (name);
11533   *input_line_pointer = c;
11534   return p;
11535 }
11536
11537 /* Align the current frag to a given power of two.  The MIPS assembler
11538    also automatically adjusts any preceding label.  */
11539
11540 static void
11541 mips_align (int to, int fill, symbolS *label)
11542 {
11543   mips_emit_delays (FALSE);
11544   frag_align (to, fill, 0);
11545   record_alignment (now_seg, to);
11546   if (label != NULL)
11547     {
11548       assert (S_GET_SEGMENT (label) == now_seg);
11549       symbol_set_frag (label, frag_now);
11550       S_SET_VALUE (label, (valueT) frag_now_fix ());
11551     }
11552 }
11553
11554 /* Align to a given power of two.  .align 0 turns off the automatic
11555    alignment used by the data creating pseudo-ops.  */
11556
11557 static void
11558 s_align (int x ATTRIBUTE_UNUSED)
11559 {
11560   register int temp;
11561   register long temp_fill;
11562   long max_alignment = 15;
11563
11564   /*
11565
11566     o  Note that the assembler pulls down any immediately preceeding label
11567        to the aligned address.
11568     o  It's not documented but auto alignment is reinstated by
11569        a .align pseudo instruction.
11570     o  Note also that after auto alignment is turned off the mips assembler
11571        issues an error on attempt to assemble an improperly aligned data item.
11572        We don't.
11573
11574     */
11575
11576   temp = get_absolute_expression ();
11577   if (temp > max_alignment)
11578     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11579   else if (temp < 0)
11580     {
11581       as_warn (_("Alignment negative: 0 assumed."));
11582       temp = 0;
11583     }
11584   if (*input_line_pointer == ',')
11585     {
11586       ++input_line_pointer;
11587       temp_fill = get_absolute_expression ();
11588     }
11589   else
11590     temp_fill = 0;
11591   if (temp)
11592     {
11593       auto_align = 1;
11594       mips_align (temp, (int) temp_fill,
11595                   insn_labels != NULL ? insn_labels->label : NULL);
11596     }
11597   else
11598     {
11599       auto_align = 0;
11600     }
11601
11602   demand_empty_rest_of_line ();
11603 }
11604
11605 void
11606 mips_flush_pending_output (void)
11607 {
11608   mips_emit_delays (FALSE);
11609   mips_clear_insn_labels ();
11610 }
11611
11612 static void
11613 s_change_sec (int sec)
11614 {
11615   segT seg;
11616
11617   /* When generating embedded PIC code, we only use the .text, .lit8,
11618      .sdata and .sbss sections.  We change the .data and .rdata
11619      pseudo-ops to use .sdata.  */
11620   if (mips_pic == EMBEDDED_PIC
11621       && (sec == 'd' || sec == 'r'))
11622     sec = 's';
11623
11624 #ifdef OBJ_ELF
11625   /* The ELF backend needs to know that we are changing sections, so
11626      that .previous works correctly.  We could do something like check
11627      for an obj_section_change_hook macro, but that might be confusing
11628      as it would not be appropriate to use it in the section changing
11629      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11630      This should be cleaner, somehow.  */
11631   obj_elf_section_change_hook ();
11632 #endif
11633
11634   mips_emit_delays (FALSE);
11635   switch (sec)
11636     {
11637     case 't':
11638       s_text (0);
11639       break;
11640     case 'd':
11641       s_data (0);
11642       break;
11643     case 'b':
11644       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11645       demand_empty_rest_of_line ();
11646       break;
11647
11648     case 'r':
11649       if (USE_GLOBAL_POINTER_OPT)
11650         {
11651           seg = subseg_new (RDATA_SECTION_NAME,
11652                             (subsegT) get_absolute_expression ());
11653           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11654             {
11655               bfd_set_section_flags (stdoutput, seg,
11656                                      (SEC_ALLOC
11657                                       | SEC_LOAD
11658                                       | SEC_READONLY
11659                                       | SEC_RELOC
11660                                       | SEC_DATA));
11661               if (strcmp (TARGET_OS, "elf") != 0)
11662                 record_alignment (seg, 4);
11663             }
11664           demand_empty_rest_of_line ();
11665         }
11666       else
11667         {
11668           as_bad (_("No read only data section in this object file format"));
11669           demand_empty_rest_of_line ();
11670           return;
11671         }
11672       break;
11673
11674     case 's':
11675       if (USE_GLOBAL_POINTER_OPT)
11676         {
11677           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11678           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11679             {
11680               bfd_set_section_flags (stdoutput, seg,
11681                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11682                                      | SEC_DATA);
11683               if (strcmp (TARGET_OS, "elf") != 0)
11684                 record_alignment (seg, 4);
11685             }
11686           demand_empty_rest_of_line ();
11687           break;
11688         }
11689       else
11690         {
11691           as_bad (_("Global pointers not supported; recompile -G 0"));
11692           demand_empty_rest_of_line ();
11693           return;
11694         }
11695     }
11696
11697   auto_align = 1;
11698 }
11699
11700 void
11701 s_change_section (int ignore ATTRIBUTE_UNUSED)
11702 {
11703 #ifdef OBJ_ELF
11704   char *section_name;
11705   char c;
11706   char next_c = 0;
11707   int section_type;
11708   int section_flag;
11709   int section_entry_size;
11710   int section_alignment;
11711
11712   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11713     return;
11714
11715   section_name = input_line_pointer;
11716   c = get_symbol_end ();
11717   if (c)
11718     next_c = *(input_line_pointer + 1);
11719
11720   /* Do we have .section Name<,"flags">?  */
11721   if (c != ',' || (c == ',' && next_c == '"'))
11722     {
11723       /* just after name is now '\0'.  */
11724       *input_line_pointer = c;
11725       input_line_pointer = section_name;
11726       obj_elf_section (ignore);
11727       return;
11728     }
11729   input_line_pointer++;
11730
11731   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11732   if (c == ',')
11733     section_type = get_absolute_expression ();
11734   else
11735     section_type = 0;
11736   if (*input_line_pointer++ == ',')
11737     section_flag = get_absolute_expression ();
11738   else
11739     section_flag = 0;
11740   if (*input_line_pointer++ == ',')
11741     section_entry_size = get_absolute_expression ();
11742   else
11743     section_entry_size = 0;
11744   if (*input_line_pointer++ == ',')
11745     section_alignment = get_absolute_expression ();
11746   else
11747     section_alignment = 0;
11748
11749   section_name = xstrdup (section_name);
11750
11751   obj_elf_change_section (section_name, section_type, section_flag,
11752                           section_entry_size, 0, 0, 0);
11753
11754   if (now_seg->name != section_name)
11755     free (section_name);
11756 #endif /* OBJ_ELF */
11757 }
11758
11759 void
11760 mips_enable_auto_align (void)
11761 {
11762   auto_align = 1;
11763 }
11764
11765 static void
11766 s_cons (int log_size)
11767 {
11768   symbolS *label;
11769
11770   label = insn_labels != NULL ? insn_labels->label : NULL;
11771   mips_emit_delays (FALSE);
11772   if (log_size > 0 && auto_align)
11773     mips_align (log_size, 0, label);
11774   mips_clear_insn_labels ();
11775   cons (1 << log_size);
11776 }
11777
11778 static void
11779 s_float_cons (int type)
11780 {
11781   symbolS *label;
11782
11783   label = insn_labels != NULL ? insn_labels->label : NULL;
11784
11785   mips_emit_delays (FALSE);
11786
11787   if (auto_align)
11788     {
11789       if (type == 'd')
11790         mips_align (3, 0, label);
11791       else
11792         mips_align (2, 0, label);
11793     }
11794
11795   mips_clear_insn_labels ();
11796
11797   float_cons (type);
11798 }
11799
11800 /* Handle .globl.  We need to override it because on Irix 5 you are
11801    permitted to say
11802        .globl foo .text
11803    where foo is an undefined symbol, to mean that foo should be
11804    considered to be the address of a function.  */
11805
11806 static void
11807 s_mips_globl (int x ATTRIBUTE_UNUSED)
11808 {
11809   char *name;
11810   int c;
11811   symbolS *symbolP;
11812   flagword flag;
11813
11814   name = input_line_pointer;
11815   c = get_symbol_end ();
11816   symbolP = symbol_find_or_make (name);
11817   *input_line_pointer = c;
11818   SKIP_WHITESPACE ();
11819
11820   /* On Irix 5, every global symbol that is not explicitly labelled as
11821      being a function is apparently labelled as being an object.  */
11822   flag = BSF_OBJECT;
11823
11824   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11825     {
11826       char *secname;
11827       asection *sec;
11828
11829       secname = input_line_pointer;
11830       c = get_symbol_end ();
11831       sec = bfd_get_section_by_name (stdoutput, secname);
11832       if (sec == NULL)
11833         as_bad (_("%s: no such section"), secname);
11834       *input_line_pointer = c;
11835
11836       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11837         flag = BSF_FUNCTION;
11838     }
11839
11840   symbol_get_bfdsym (symbolP)->flags |= flag;
11841
11842   S_SET_EXTERNAL (symbolP);
11843   demand_empty_rest_of_line ();
11844 }
11845
11846 static void
11847 s_option (int x ATTRIBUTE_UNUSED)
11848 {
11849   char *opt;
11850   char c;
11851
11852   opt = input_line_pointer;
11853   c = get_symbol_end ();
11854
11855   if (*opt == 'O')
11856     {
11857       /* FIXME: What does this mean?  */
11858     }
11859   else if (strncmp (opt, "pic", 3) == 0)
11860     {
11861       int i;
11862
11863       i = atoi (opt + 3);
11864       if (i == 0)
11865         mips_pic = NO_PIC;
11866       else if (i == 2)
11867         {
11868         mips_pic = SVR4_PIC;
11869           mips_abicalls = TRUE;
11870         }
11871       else
11872         as_bad (_(".option pic%d not supported"), i);
11873
11874       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11875         {
11876           if (g_switch_seen && g_switch_value != 0)
11877             as_warn (_("-G may not be used with SVR4 PIC code"));
11878           g_switch_value = 0;
11879           bfd_set_gp_size (stdoutput, 0);
11880         }
11881     }
11882   else
11883     as_warn (_("Unrecognized option \"%s\""), opt);
11884
11885   *input_line_pointer = c;
11886   demand_empty_rest_of_line ();
11887 }
11888
11889 /* This structure is used to hold a stack of .set values.  */
11890
11891 struct mips_option_stack
11892 {
11893   struct mips_option_stack *next;
11894   struct mips_set_options options;
11895 };
11896
11897 static struct mips_option_stack *mips_opts_stack;
11898
11899 /* Handle the .set pseudo-op.  */
11900
11901 static void
11902 s_mipsset (int x ATTRIBUTE_UNUSED)
11903 {
11904   char *name = input_line_pointer, ch;
11905
11906   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11907     ++input_line_pointer;
11908   ch = *input_line_pointer;
11909   *input_line_pointer = '\0';
11910
11911   if (strcmp (name, "reorder") == 0)
11912     {
11913       if (mips_opts.noreorder && prev_nop_frag != NULL)
11914         {
11915           /* If we still have pending nops, we can discard them.  The
11916              usual nop handling will insert any that are still
11917              needed.  */
11918           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11919                                     * (mips_opts.mips16 ? 2 : 4));
11920           prev_nop_frag = NULL;
11921         }
11922       mips_opts.noreorder = 0;
11923     }
11924   else if (strcmp (name, "noreorder") == 0)
11925     {
11926       mips_emit_delays (TRUE);
11927       mips_opts.noreorder = 1;
11928       mips_any_noreorder = 1;
11929     }
11930   else if (strcmp (name, "at") == 0)
11931     {
11932       mips_opts.noat = 0;
11933     }
11934   else if (strcmp (name, "noat") == 0)
11935     {
11936       mips_opts.noat = 1;
11937     }
11938   else if (strcmp (name, "macro") == 0)
11939     {
11940       mips_opts.warn_about_macros = 0;
11941     }
11942   else if (strcmp (name, "nomacro") == 0)
11943     {
11944       if (mips_opts.noreorder == 0)
11945         as_bad (_("`noreorder' must be set before `nomacro'"));
11946       mips_opts.warn_about_macros = 1;
11947     }
11948   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11949     {
11950       mips_opts.nomove = 0;
11951     }
11952   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11953     {
11954       mips_opts.nomove = 1;
11955     }
11956   else if (strcmp (name, "bopt") == 0)
11957     {
11958       mips_opts.nobopt = 0;
11959     }
11960   else if (strcmp (name, "nobopt") == 0)
11961     {
11962       mips_opts.nobopt = 1;
11963     }
11964   else if (strcmp (name, "mips16") == 0
11965            || strcmp (name, "MIPS-16") == 0)
11966     mips_opts.mips16 = 1;
11967   else if (strcmp (name, "nomips16") == 0
11968            || strcmp (name, "noMIPS-16") == 0)
11969     mips_opts.mips16 = 0;
11970   else if (strcmp (name, "mips3d") == 0)
11971     mips_opts.ase_mips3d = 1;
11972   else if (strcmp (name, "nomips3d") == 0)
11973     mips_opts.ase_mips3d = 0;
11974   else if (strcmp (name, "mdmx") == 0)
11975     mips_opts.ase_mdmx = 1;
11976   else if (strcmp (name, "nomdmx") == 0)
11977     mips_opts.ase_mdmx = 0;
11978   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11979     {
11980       int reset = 0;
11981
11982       /* Permit the user to change the ISA and architecture on the fly.
11983          Needless to say, misuse can cause serious problems.  */
11984       if (strcmp (name, "mips0") == 0)
11985         {
11986           reset = 1;
11987           mips_opts.isa = file_mips_isa;
11988         }
11989       else if (strcmp (name, "mips1") == 0)
11990         mips_opts.isa = ISA_MIPS1;
11991       else if (strcmp (name, "mips2") == 0)
11992         mips_opts.isa = ISA_MIPS2;
11993       else if (strcmp (name, "mips3") == 0)
11994         mips_opts.isa = ISA_MIPS3;
11995       else if (strcmp (name, "mips4") == 0)
11996         mips_opts.isa = ISA_MIPS4;
11997       else if (strcmp (name, "mips5") == 0)
11998         mips_opts.isa = ISA_MIPS5;
11999       else if (strcmp (name, "mips32") == 0)
12000         mips_opts.isa = ISA_MIPS32;
12001       else if (strcmp (name, "mips32r2") == 0)
12002         mips_opts.isa = ISA_MIPS32R2;
12003       else if (strcmp (name, "mips64") == 0)
12004         mips_opts.isa = ISA_MIPS64;
12005       else if (strcmp (name, "arch=default") == 0)
12006         {
12007           reset = 1;
12008           mips_opts.arch = file_mips_arch;
12009           mips_opts.isa = file_mips_isa;
12010         }
12011       else if (strncmp (name, "arch=", 5) == 0)
12012         {
12013           const struct mips_cpu_info *p;
12014
12015           p = mips_parse_cpu("internal use", name + 5);
12016           if (!p)
12017             as_bad (_("unknown architecture %s"), name + 5);
12018           else
12019             {
12020               mips_opts.arch = p->cpu;
12021               mips_opts.isa = p->isa;
12022             }
12023         }
12024       else
12025         as_bad (_("unknown ISA level %s"), name + 4);
12026
12027       switch (mips_opts.isa)
12028         {
12029         case  0:
12030           break;
12031         case ISA_MIPS1:
12032         case ISA_MIPS2:
12033         case ISA_MIPS32:
12034         case ISA_MIPS32R2:
12035           mips_opts.gp32 = 1;
12036           mips_opts.fp32 = 1;
12037           break;
12038         case ISA_MIPS3:
12039         case ISA_MIPS4:
12040         case ISA_MIPS5:
12041         case ISA_MIPS64:
12042           mips_opts.gp32 = 0;
12043           mips_opts.fp32 = 0;
12044           break;
12045         default:
12046           as_bad (_("unknown ISA level %s"), name + 4);
12047           break;
12048         }
12049       if (reset)
12050         {
12051           mips_opts.gp32 = file_mips_gp32;
12052           mips_opts.fp32 = file_mips_fp32;
12053         }
12054     }
12055   else if (strcmp (name, "autoextend") == 0)
12056     mips_opts.noautoextend = 0;
12057   else if (strcmp (name, "noautoextend") == 0)
12058     mips_opts.noautoextend = 1;
12059   else if (strcmp (name, "push") == 0)
12060     {
12061       struct mips_option_stack *s;
12062
12063       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12064       s->next = mips_opts_stack;
12065       s->options = mips_opts;
12066       mips_opts_stack = s;
12067     }
12068   else if (strcmp (name, "pop") == 0)
12069     {
12070       struct mips_option_stack *s;
12071
12072       s = mips_opts_stack;
12073       if (s == NULL)
12074         as_bad (_(".set pop with no .set push"));
12075       else
12076         {
12077           /* If we're changing the reorder mode we need to handle
12078              delay slots correctly.  */
12079           if (s->options.noreorder && ! mips_opts.noreorder)
12080             mips_emit_delays (TRUE);
12081           else if (! s->options.noreorder && mips_opts.noreorder)
12082             {
12083               if (prev_nop_frag != NULL)
12084                 {
12085                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12086                                             * (mips_opts.mips16 ? 2 : 4));
12087                   prev_nop_frag = NULL;
12088                 }
12089             }
12090
12091           mips_opts = s->options;
12092           mips_opts_stack = s->next;
12093           free (s);
12094         }
12095     }
12096   else
12097     {
12098       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12099     }
12100   *input_line_pointer = ch;
12101   demand_empty_rest_of_line ();
12102 }
12103
12104 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12105    .option pic2.  It means to generate SVR4 PIC calls.  */
12106
12107 static void
12108 s_abicalls (int ignore ATTRIBUTE_UNUSED)
12109 {
12110   mips_pic = SVR4_PIC;
12111   mips_abicalls = TRUE;
12112   if (USE_GLOBAL_POINTER_OPT)
12113     {
12114       if (g_switch_seen && g_switch_value != 0)
12115         as_warn (_("-G may not be used with SVR4 PIC code"));
12116       g_switch_value = 0;
12117     }
12118   bfd_set_gp_size (stdoutput, 0);
12119   demand_empty_rest_of_line ();
12120 }
12121
12122 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12123    PIC code.  It sets the $gp register for the function based on the
12124    function address, which is in the register named in the argument.
12125    This uses a relocation against _gp_disp, which is handled specially
12126    by the linker.  The result is:
12127         lui     $gp,%hi(_gp_disp)
12128         addiu   $gp,$gp,%lo(_gp_disp)
12129         addu    $gp,$gp,.cpload argument
12130    The .cpload argument is normally $25 == $t9.  */
12131
12132 static void
12133 s_cpload (int ignore ATTRIBUTE_UNUSED)
12134 {
12135   expressionS ex;
12136   int icnt = 0;
12137
12138   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12139      .cpload is ignored.  */
12140   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12141     {
12142       s_ignore (0);
12143       return;
12144     }
12145
12146   /* .cpload should be in a .set noreorder section.  */
12147   if (mips_opts.noreorder == 0)
12148     as_warn (_(".cpload not in noreorder section"));
12149
12150   ex.X_op = O_symbol;
12151   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12152   ex.X_op_symbol = NULL;
12153   ex.X_add_number = 0;
12154
12155   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12156   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12157
12158   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12159   macro_build (NULL, &icnt, &ex, "addiu", "t,r,j", mips_gp_register,
12160                mips_gp_register, BFD_RELOC_LO16);
12161
12162   macro_build (NULL, &icnt, NULL, "addu", "d,v,t", mips_gp_register,
12163                mips_gp_register, tc_get_register (0));
12164
12165   demand_empty_rest_of_line ();
12166 }
12167
12168 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12169      .cpsetup $reg1, offset|$reg2, label
12170
12171    If offset is given, this results in:
12172      sd         $gp, offset($sp)
12173      lui        $gp, %hi(%neg(%gp_rel(label)))
12174      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12175      daddu      $gp, $gp, $reg1
12176
12177    If $reg2 is given, this results in:
12178      daddu      $reg2, $gp, $0
12179      lui        $gp, %hi(%neg(%gp_rel(label)))
12180      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12181      daddu      $gp, $gp, $reg1
12182    $reg1 is normally $25 == $t9.  */
12183 static void
12184 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12185 {
12186   expressionS ex_off;
12187   expressionS ex_sym;
12188   int reg1;
12189   int icnt = 0;
12190   char *f;
12191
12192   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12193      We also need NewABI support.  */
12194   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12195     {
12196       s_ignore (0);
12197       return;
12198     }
12199
12200   reg1 = tc_get_register (0);
12201   SKIP_WHITESPACE ();
12202   if (*input_line_pointer != ',')
12203     {
12204       as_bad (_("missing argument separator ',' for .cpsetup"));
12205       return;
12206     }
12207   else
12208     ++input_line_pointer;
12209   SKIP_WHITESPACE ();
12210   if (*input_line_pointer == '$')
12211     {
12212       mips_cpreturn_register = tc_get_register (0);
12213       mips_cpreturn_offset = -1;
12214     }
12215   else
12216     {
12217       mips_cpreturn_offset = get_absolute_expression ();
12218       mips_cpreturn_register = -1;
12219     }
12220   SKIP_WHITESPACE ();
12221   if (*input_line_pointer != ',')
12222     {
12223       as_bad (_("missing argument separator ',' for .cpsetup"));
12224       return;
12225     }
12226   else
12227     ++input_line_pointer;
12228   SKIP_WHITESPACE ();
12229   expression (&ex_sym);
12230
12231   if (mips_cpreturn_register == -1)
12232     {
12233       ex_off.X_op = O_constant;
12234       ex_off.X_add_symbol = NULL;
12235       ex_off.X_op_symbol = NULL;
12236       ex_off.X_add_number = mips_cpreturn_offset;
12237
12238       macro_build (NULL, &icnt, &ex_off, "sd", "t,o(b)", mips_gp_register,
12239                    BFD_RELOC_LO16, SP);
12240     }
12241   else
12242     macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_cpreturn_register,
12243                  mips_gp_register, 0);
12244
12245   /* Ensure there's room for the next two instructions, so that `f'
12246      doesn't end up with an address in the wrong frag.  */
12247   frag_grow (8);
12248   f = frag_more (0);
12249   macro_build (NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12250                BFD_RELOC_GPREL16);
12251   fix_new (frag_now, f - frag_now->fr_literal,
12252            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12253   fix_new (frag_now, f - frag_now->fr_literal,
12254            4, NULL, 0, 0, BFD_RELOC_HI16_S);
12255
12256   f = frag_more (0);
12257   macro_build (NULL, &icnt, &ex_sym, "addiu", "t,r,j", mips_gp_register,
12258                mips_gp_register, BFD_RELOC_GPREL16);
12259   fix_new (frag_now, f - frag_now->fr_literal,
12260            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12261   fix_new (frag_now, f - frag_now->fr_literal,
12262            4, NULL, 0, 0, BFD_RELOC_LO16);
12263
12264   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12265                mips_gp_register, reg1);
12266
12267   demand_empty_rest_of_line ();
12268 }
12269
12270 static void
12271 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12272 {
12273   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12274    .cplocal is ignored.  */
12275   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12276     {
12277       s_ignore (0);
12278       return;
12279     }
12280
12281   mips_gp_register = tc_get_register (0);
12282   demand_empty_rest_of_line ();
12283 }
12284
12285 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12286    offset from $sp.  The offset is remembered, and after making a PIC
12287    call $gp is restored from that location.  */
12288
12289 static void
12290 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12291 {
12292   expressionS ex;
12293   int icnt = 0;
12294
12295   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12296      .cprestore is ignored.  */
12297   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12298     {
12299       s_ignore (0);
12300       return;
12301     }
12302
12303   mips_cprestore_offset = get_absolute_expression ();
12304   mips_cprestore_valid = 1;
12305
12306   ex.X_op = O_constant;
12307   ex.X_add_symbol = NULL;
12308   ex.X_op_symbol = NULL;
12309   ex.X_add_number = mips_cprestore_offset;
12310
12311   macro_build_ldst_constoffset (NULL, &icnt, &ex, ADDRESS_STORE_INSN,
12312                                 mips_gp_register, SP, HAVE_64BIT_ADDRESSES);
12313
12314   demand_empty_rest_of_line ();
12315 }
12316
12317 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12318    was given in the preceeding .gpsetup, it results in:
12319      ld         $gp, offset($sp)
12320
12321    If a register $reg2 was given there, it results in:
12322      daddiu     $gp, $gp, $reg2
12323  */
12324 static void
12325 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12326 {
12327   expressionS ex;
12328   int icnt = 0;
12329
12330   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12331      We also need NewABI support.  */
12332   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12333     {
12334       s_ignore (0);
12335       return;
12336     }
12337
12338   if (mips_cpreturn_register == -1)
12339     {
12340       ex.X_op = O_constant;
12341       ex.X_add_symbol = NULL;
12342       ex.X_op_symbol = NULL;
12343       ex.X_add_number = mips_cpreturn_offset;
12344
12345       macro_build (NULL, &icnt, &ex, "ld", "t,o(b)", mips_gp_register,
12346                    BFD_RELOC_LO16, SP);
12347     }
12348   else
12349     macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_gp_register,
12350                  mips_cpreturn_register, 0);
12351
12352   demand_empty_rest_of_line ();
12353 }
12354
12355 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12356    code.  It sets the offset to use in gp_rel relocations.  */
12357
12358 static void
12359 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12360 {
12361   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12362      We also need NewABI support.  */
12363   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12364     {
12365       s_ignore (0);
12366       return;
12367     }
12368
12369   mips_gprel_offset = get_absolute_expression ();
12370
12371   demand_empty_rest_of_line ();
12372 }
12373
12374 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12375    code.  It generates a 32 bit GP relative reloc.  */
12376
12377 static void
12378 s_gpword (int ignore ATTRIBUTE_UNUSED)
12379 {
12380   symbolS *label;
12381   expressionS ex;
12382   char *p;
12383
12384   /* When not generating PIC code, this is treated as .word.  */
12385   if (mips_pic != SVR4_PIC)
12386     {
12387       s_cons (2);
12388       return;
12389     }
12390
12391   label = insn_labels != NULL ? insn_labels->label : NULL;
12392   mips_emit_delays (TRUE);
12393   if (auto_align)
12394     mips_align (2, 0, label);
12395   mips_clear_insn_labels ();
12396
12397   expression (&ex);
12398
12399   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12400     {
12401       as_bad (_("Unsupported use of .gpword"));
12402       ignore_rest_of_line ();
12403     }
12404
12405   p = frag_more (4);
12406   md_number_to_chars (p, 0, 4);
12407   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12408                BFD_RELOC_GPREL32);
12409
12410   demand_empty_rest_of_line ();
12411 }
12412
12413 static void
12414 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12415 {
12416   symbolS *label;
12417   expressionS ex;
12418   char *p;
12419
12420   /* When not generating PIC code, this is treated as .dword.  */
12421   if (mips_pic != SVR4_PIC)
12422     {
12423       s_cons (3);
12424       return;
12425     }
12426
12427   label = insn_labels != NULL ? insn_labels->label : NULL;
12428   mips_emit_delays (TRUE);
12429   if (auto_align)
12430     mips_align (3, 0, label);
12431   mips_clear_insn_labels ();
12432
12433   expression (&ex);
12434
12435   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12436     {
12437       as_bad (_("Unsupported use of .gpdword"));
12438       ignore_rest_of_line ();
12439     }
12440
12441   p = frag_more (8);
12442   md_number_to_chars (p, 0, 8);
12443   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12444                BFD_RELOC_GPREL32);
12445
12446   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12447   ex.X_op = O_absent;
12448   ex.X_add_symbol = 0;
12449   ex.X_add_number = 0;
12450   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12451                BFD_RELOC_64);
12452
12453   demand_empty_rest_of_line ();
12454 }
12455
12456 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12457    tables in SVR4 PIC code.  */
12458
12459 static void
12460 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12461 {
12462   int icnt = 0;
12463   int reg;
12464
12465   /* This is ignored when not generating SVR4 PIC code.  */
12466   if (mips_pic != SVR4_PIC)
12467     {
12468       s_ignore (0);
12469       return;
12470     }
12471
12472   /* Add $gp to the register named as an argument.  */
12473   reg = tc_get_register (0);
12474   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
12475                reg, reg, mips_gp_register);
12476
12477   demand_empty_rest_of_line ();
12478 }
12479
12480 /* Handle the .insn pseudo-op.  This marks instruction labels in
12481    mips16 mode.  This permits the linker to handle them specially,
12482    such as generating jalx instructions when needed.  We also make
12483    them odd for the duration of the assembly, in order to generate the
12484    right sort of code.  We will make them even in the adjust_symtab
12485    routine, while leaving them marked.  This is convenient for the
12486    debugger and the disassembler.  The linker knows to make them odd
12487    again.  */
12488
12489 static void
12490 s_insn (int ignore ATTRIBUTE_UNUSED)
12491 {
12492   mips16_mark_labels ();
12493
12494   demand_empty_rest_of_line ();
12495 }
12496
12497 /* Handle a .stabn directive.  We need these in order to mark a label
12498    as being a mips16 text label correctly.  Sometimes the compiler
12499    will emit a label, followed by a .stabn, and then switch sections.
12500    If the label and .stabn are in mips16 mode, then the label is
12501    really a mips16 text label.  */
12502
12503 static void
12504 s_mips_stab (int type)
12505 {
12506   if (type == 'n')
12507     mips16_mark_labels ();
12508
12509   s_stab (type);
12510 }
12511
12512 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12513  */
12514
12515 static void
12516 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12517 {
12518   char *name;
12519   int c;
12520   symbolS *symbolP;
12521   expressionS exp;
12522
12523   name = input_line_pointer;
12524   c = get_symbol_end ();
12525   symbolP = symbol_find_or_make (name);
12526   S_SET_WEAK (symbolP);
12527   *input_line_pointer = c;
12528
12529   SKIP_WHITESPACE ();
12530
12531   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12532     {
12533       if (S_IS_DEFINED (symbolP))
12534         {
12535           as_bad ("ignoring attempt to redefine symbol %s",
12536                   S_GET_NAME (symbolP));
12537           ignore_rest_of_line ();
12538           return;
12539         }
12540
12541       if (*input_line_pointer == ',')
12542         {
12543           ++input_line_pointer;
12544           SKIP_WHITESPACE ();
12545         }
12546
12547       expression (&exp);
12548       if (exp.X_op != O_symbol)
12549         {
12550           as_bad ("bad .weakext directive");
12551           ignore_rest_of_line ();
12552           return;
12553         }
12554       symbol_set_value_expression (symbolP, &exp);
12555     }
12556
12557   demand_empty_rest_of_line ();
12558 }
12559
12560 /* Parse a register string into a number.  Called from the ECOFF code
12561    to parse .frame.  The argument is non-zero if this is the frame
12562    register, so that we can record it in mips_frame_reg.  */
12563
12564 int
12565 tc_get_register (int frame)
12566 {
12567   int reg;
12568
12569   SKIP_WHITESPACE ();
12570   if (*input_line_pointer++ != '$')
12571     {
12572       as_warn (_("expected `$'"));
12573       reg = ZERO;
12574     }
12575   else if (ISDIGIT (*input_line_pointer))
12576     {
12577       reg = get_absolute_expression ();
12578       if (reg < 0 || reg >= 32)
12579         {
12580           as_warn (_("Bad register number"));
12581           reg = ZERO;
12582         }
12583     }
12584   else
12585     {
12586       if (strncmp (input_line_pointer, "ra", 2) == 0)
12587         {
12588           reg = RA;
12589           input_line_pointer += 2;
12590         }
12591       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12592         {
12593           reg = FP;
12594           input_line_pointer += 2;
12595         }
12596       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12597         {
12598           reg = SP;
12599           input_line_pointer += 2;
12600         }
12601       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12602         {
12603           reg = GP;
12604           input_line_pointer += 2;
12605         }
12606       else if (strncmp (input_line_pointer, "at", 2) == 0)
12607         {
12608           reg = AT;
12609           input_line_pointer += 2;
12610         }
12611       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12612         {
12613           reg = KT0;
12614           input_line_pointer += 3;
12615         }
12616       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12617         {
12618           reg = KT1;
12619           input_line_pointer += 3;
12620         }
12621       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12622         {
12623           reg = ZERO;
12624           input_line_pointer += 4;
12625         }
12626       else
12627         {
12628           as_warn (_("Unrecognized register name"));
12629           reg = ZERO;
12630           while (ISALNUM(*input_line_pointer))
12631            input_line_pointer++;
12632         }
12633     }
12634   if (frame)
12635     {
12636       mips_frame_reg = reg != 0 ? reg : SP;
12637       mips_frame_reg_valid = 1;
12638       mips_cprestore_valid = 0;
12639     }
12640   return reg;
12641 }
12642
12643 valueT
12644 md_section_align (asection *seg, valueT addr)
12645 {
12646   int align = bfd_get_section_alignment (stdoutput, seg);
12647
12648 #ifdef OBJ_ELF
12649   /* We don't need to align ELF sections to the full alignment.
12650      However, Irix 5 may prefer that we align them at least to a 16
12651      byte boundary.  We don't bother to align the sections if we are
12652      targeted for an embedded system.  */
12653   if (strcmp (TARGET_OS, "elf") == 0)
12654     return addr;
12655   if (align > 4)
12656     align = 4;
12657 #endif
12658
12659   return ((addr + (1 << align) - 1) & (-1 << align));
12660 }
12661
12662 /* Utility routine, called from above as well.  If called while the
12663    input file is still being read, it's only an approximation.  (For
12664    example, a symbol may later become defined which appeared to be
12665    undefined earlier.)  */
12666
12667 static int
12668 nopic_need_relax (symbolS *sym, int before_relaxing)
12669 {
12670   if (sym == 0)
12671     return 0;
12672
12673   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12674     {
12675       const char *symname;
12676       int change;
12677
12678       /* Find out whether this symbol can be referenced off the $gp
12679          register.  It can be if it is smaller than the -G size or if
12680          it is in the .sdata or .sbss section.  Certain symbols can
12681          not be referenced off the $gp, although it appears as though
12682          they can.  */
12683       symname = S_GET_NAME (sym);
12684       if (symname != (const char *) NULL
12685           && (strcmp (symname, "eprol") == 0
12686               || strcmp (symname, "etext") == 0
12687               || strcmp (symname, "_gp") == 0
12688               || strcmp (symname, "edata") == 0
12689               || strcmp (symname, "_fbss") == 0
12690               || strcmp (symname, "_fdata") == 0
12691               || strcmp (symname, "_ftext") == 0
12692               || strcmp (symname, "end") == 0
12693               || strcmp (symname, "_gp_disp") == 0))
12694         change = 1;
12695       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12696                && (0
12697 #ifndef NO_ECOFF_DEBUGGING
12698                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12699                        && (symbol_get_obj (sym)->ecoff_extern_size
12700                            <= g_switch_value))
12701 #endif
12702                    /* We must defer this decision until after the whole
12703                       file has been read, since there might be a .extern
12704                       after the first use of this symbol.  */
12705                    || (before_relaxing
12706 #ifndef NO_ECOFF_DEBUGGING
12707                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12708 #endif
12709                        && S_GET_VALUE (sym) == 0)
12710                    || (S_GET_VALUE (sym) != 0
12711                        && S_GET_VALUE (sym) <= g_switch_value)))
12712         change = 0;
12713       else
12714         {
12715           const char *segname;
12716
12717           segname = segment_name (S_GET_SEGMENT (sym));
12718           assert (strcmp (segname, ".lit8") != 0
12719                   && strcmp (segname, ".lit4") != 0);
12720           change = (strcmp (segname, ".sdata") != 0
12721                     && strcmp (segname, ".sbss") != 0
12722                     && strncmp (segname, ".sdata.", 7) != 0
12723                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12724         }
12725       return change;
12726     }
12727   else
12728     /* We are not optimizing for the $gp register.  */
12729     return 1;
12730 }
12731
12732
12733 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
12734
12735 static bfd_boolean
12736 pic_need_relax (symbolS *sym, asection *segtype)
12737 {
12738   asection *symsec;
12739   bfd_boolean linkonce;
12740
12741   /* Handle the case of a symbol equated to another symbol.  */
12742   while (symbol_equated_reloc_p (sym))
12743     {
12744       symbolS *n;
12745
12746       /* It's possible to get a loop here in a badly written
12747          program.  */
12748       n = symbol_get_value_expression (sym)->X_add_symbol;
12749       if (n == sym)
12750         break;
12751       sym = n;
12752     }
12753
12754   symsec = S_GET_SEGMENT (sym);
12755
12756   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12757   linkonce = FALSE;
12758   if (symsec != segtype && ! S_IS_LOCAL (sym))
12759     {
12760       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12761           != 0)
12762         linkonce = TRUE;
12763
12764       /* The GNU toolchain uses an extension for ELF: a section
12765          beginning with the magic string .gnu.linkonce is a linkonce
12766          section.  */
12767       if (strncmp (segment_name (symsec), ".gnu.linkonce",
12768                    sizeof ".gnu.linkonce" - 1) == 0)
12769         linkonce = TRUE;
12770     }
12771
12772   /* This must duplicate the test in adjust_reloc_syms.  */
12773   return (symsec != &bfd_und_section
12774           && symsec != &bfd_abs_section
12775           && ! bfd_is_com_section (symsec)
12776           && !linkonce
12777 #ifdef OBJ_ELF
12778           /* A global or weak symbol is treated as external.  */
12779           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12780               || (! S_IS_WEAK (sym)
12781                   && (! S_IS_EXTERNAL (sym)
12782                       || mips_pic == EMBEDDED_PIC)))
12783 #endif
12784           );
12785 }
12786
12787
12788 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12789    extended opcode.  SEC is the section the frag is in.  */
12790
12791 static int
12792 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12793 {
12794   int type;
12795   register const struct mips16_immed_operand *op;
12796   offsetT val;
12797   int mintiny, maxtiny;
12798   segT symsec;
12799   fragS *sym_frag;
12800
12801   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12802     return 0;
12803   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12804     return 1;
12805
12806   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12807   op = mips16_immed_operands;
12808   while (op->type != type)
12809     {
12810       ++op;
12811       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12812     }
12813
12814   if (op->unsp)
12815     {
12816       if (type == '<' || type == '>' || type == '[' || type == ']')
12817         {
12818           mintiny = 1;
12819           maxtiny = 1 << op->nbits;
12820         }
12821       else
12822         {
12823           mintiny = 0;
12824           maxtiny = (1 << op->nbits) - 1;
12825         }
12826     }
12827   else
12828     {
12829       mintiny = - (1 << (op->nbits - 1));
12830       maxtiny = (1 << (op->nbits - 1)) - 1;
12831     }
12832
12833   sym_frag = symbol_get_frag (fragp->fr_symbol);
12834   val = S_GET_VALUE (fragp->fr_symbol);
12835   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12836
12837   if (op->pcrel)
12838     {
12839       addressT addr;
12840
12841       /* We won't have the section when we are called from
12842          mips_relax_frag.  However, we will always have been called
12843          from md_estimate_size_before_relax first.  If this is a
12844          branch to a different section, we mark it as such.  If SEC is
12845          NULL, and the frag is not marked, then it must be a branch to
12846          the same section.  */
12847       if (sec == NULL)
12848         {
12849           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12850             return 1;
12851         }
12852       else
12853         {
12854           /* Must have been called from md_estimate_size_before_relax.  */
12855           if (symsec != sec)
12856             {
12857               fragp->fr_subtype =
12858                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12859
12860               /* FIXME: We should support this, and let the linker
12861                  catch branches and loads that are out of range.  */
12862               as_bad_where (fragp->fr_file, fragp->fr_line,
12863                             _("unsupported PC relative reference to different section"));
12864
12865               return 1;
12866             }
12867           if (fragp != sym_frag && sym_frag->fr_address == 0)
12868             /* Assume non-extended on the first relaxation pass.
12869                The address we have calculated will be bogus if this is
12870                a forward branch to another frag, as the forward frag
12871                will have fr_address == 0.  */
12872             return 0;
12873         }
12874
12875       /* In this case, we know for sure that the symbol fragment is in
12876          the same section.  If the relax_marker of the symbol fragment
12877          differs from the relax_marker of this fragment, we have not
12878          yet adjusted the symbol fragment fr_address.  We want to add
12879          in STRETCH in order to get a better estimate of the address.
12880          This particularly matters because of the shift bits.  */
12881       if (stretch != 0
12882           && sym_frag->relax_marker != fragp->relax_marker)
12883         {
12884           fragS *f;
12885
12886           /* Adjust stretch for any alignment frag.  Note that if have
12887              been expanding the earlier code, the symbol may be
12888              defined in what appears to be an earlier frag.  FIXME:
12889              This doesn't handle the fr_subtype field, which specifies
12890              a maximum number of bytes to skip when doing an
12891              alignment.  */
12892           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12893             {
12894               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12895                 {
12896                   if (stretch < 0)
12897                     stretch = - ((- stretch)
12898                                  & ~ ((1 << (int) f->fr_offset) - 1));
12899                   else
12900                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12901                   if (stretch == 0)
12902                     break;
12903                 }
12904             }
12905           if (f != NULL)
12906             val += stretch;
12907         }
12908
12909       addr = fragp->fr_address + fragp->fr_fix;
12910
12911       /* The base address rules are complicated.  The base address of
12912          a branch is the following instruction.  The base address of a
12913          PC relative load or add is the instruction itself, but if it
12914          is in a delay slot (in which case it can not be extended) use
12915          the address of the instruction whose delay slot it is in.  */
12916       if (type == 'p' || type == 'q')
12917         {
12918           addr += 2;
12919
12920           /* If we are currently assuming that this frag should be
12921              extended, then, the current address is two bytes
12922              higher.  */
12923           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12924             addr += 2;
12925
12926           /* Ignore the low bit in the target, since it will be set
12927              for a text label.  */
12928           if ((val & 1) != 0)
12929             --val;
12930         }
12931       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12932         addr -= 4;
12933       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12934         addr -= 2;
12935
12936       val -= addr & ~ ((1 << op->shift) - 1);
12937
12938       /* Branch offsets have an implicit 0 in the lowest bit.  */
12939       if (type == 'p' || type == 'q')
12940         val /= 2;
12941
12942       /* If any of the shifted bits are set, we must use an extended
12943          opcode.  If the address depends on the size of this
12944          instruction, this can lead to a loop, so we arrange to always
12945          use an extended opcode.  We only check this when we are in
12946          the main relaxation loop, when SEC is NULL.  */
12947       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12948         {
12949           fragp->fr_subtype =
12950             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12951           return 1;
12952         }
12953
12954       /* If we are about to mark a frag as extended because the value
12955          is precisely maxtiny + 1, then there is a chance of an
12956          infinite loop as in the following code:
12957              la $4,foo
12958              .skip      1020
12959              .align     2
12960            foo:
12961          In this case when the la is extended, foo is 0x3fc bytes
12962          away, so the la can be shrunk, but then foo is 0x400 away, so
12963          the la must be extended.  To avoid this loop, we mark the
12964          frag as extended if it was small, and is about to become
12965          extended with a value of maxtiny + 1.  */
12966       if (val == ((maxtiny + 1) << op->shift)
12967           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12968           && sec == NULL)
12969         {
12970           fragp->fr_subtype =
12971             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12972           return 1;
12973         }
12974     }
12975   else if (symsec != absolute_section && sec != NULL)
12976     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12977
12978   if ((val & ((1 << op->shift) - 1)) != 0
12979       || val < (mintiny << op->shift)
12980       || val > (maxtiny << op->shift))
12981     return 1;
12982   else
12983     return 0;
12984 }
12985
12986 /* Compute the length of a branch sequence, and adjust the
12987    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
12988    worst-case length is computed, with UPDATE being used to indicate
12989    whether an unconditional (-1), branch-likely (+1) or regular (0)
12990    branch is to be computed.  */
12991 static int
12992 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12993 {
12994   bfd_boolean toofar;
12995   int length;
12996
12997   if (fragp
12998       && S_IS_DEFINED (fragp->fr_symbol)
12999       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13000     {
13001       addressT addr;
13002       offsetT val;
13003
13004       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13005
13006       addr = fragp->fr_address + fragp->fr_fix + 4;
13007
13008       val -= addr;
13009
13010       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13011     }
13012   else if (fragp)
13013     /* If the symbol is not defined or it's in a different segment,
13014        assume the user knows what's going on and emit a short
13015        branch.  */
13016     toofar = FALSE;
13017   else
13018     toofar = TRUE;
13019
13020   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13021     fragp->fr_subtype
13022       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13023                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13024                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13025                              toofar);
13026
13027   length = 4;
13028   if (toofar)
13029     {
13030       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13031         length += 8;
13032
13033       if (mips_pic != NO_PIC)
13034         {
13035           /* Additional space for PIC loading of target address.  */
13036           length += 8;
13037           if (mips_opts.isa == ISA_MIPS1)
13038             /* Additional space for $at-stabilizing nop.  */
13039             length += 4;
13040         }
13041
13042       /* If branch is conditional.  */
13043       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13044         length += 8;
13045     }
13046
13047   return length;
13048 }
13049
13050 /* Estimate the size of a frag before relaxing.  Unless this is the
13051    mips16, we are not really relaxing here, and the final size is
13052    encoded in the subtype information.  For the mips16, we have to
13053    decide whether we are using an extended opcode or not.  */
13054
13055 int
13056 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
13057 {
13058   int change;
13059
13060   if (RELAX_BRANCH_P (fragp->fr_subtype))
13061     {
13062
13063       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13064
13065       return fragp->fr_var;
13066     }
13067
13068   if (RELAX_MIPS16_P (fragp->fr_subtype))
13069     /* We don't want to modify the EXTENDED bit here; it might get us
13070        into infinite loops.  We change it only in mips_relax_frag().  */
13071     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13072
13073   if (mips_pic == NO_PIC)
13074     change = nopic_need_relax (fragp->fr_symbol, 0);
13075   else if (mips_pic == SVR4_PIC)
13076     change = pic_need_relax (fragp->fr_symbol, segtype);
13077   else
13078     abort ();
13079
13080   if (change)
13081     {
13082       /* Record the offset to the first reloc in the fr_opcode field.
13083          This lets md_convert_frag and tc_gen_reloc know that the code
13084          must be expanded.  */
13085       fragp->fr_opcode = (fragp->fr_literal
13086                           + fragp->fr_fix
13087                           - RELAX_OLD (fragp->fr_subtype)
13088                           + RELAX_RELOC1 (fragp->fr_subtype));
13089       /* FIXME: This really needs as_warn_where.  */
13090       if (RELAX_WARN (fragp->fr_subtype))
13091         as_warn (_("AT used after \".set noat\" or macro used after "
13092                    "\".set nomacro\""));
13093
13094       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13095     }
13096
13097   return 0;
13098 }
13099
13100 /* This is called to see whether a reloc against a defined symbol
13101    should be converted into a reloc against a section.  Don't adjust
13102    MIPS16 jump relocations, so we don't have to worry about the format
13103    of the offset in the .o file.  Don't adjust relocations against
13104    mips16 symbols, so that the linker can find them if it needs to set
13105    up a stub.  */
13106
13107 int
13108 mips_fix_adjustable (fixS *fixp)
13109 {
13110   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13111     return 0;
13112
13113   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13114       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13115     return 0;
13116
13117   if (fixp->fx_addsy == NULL)
13118     return 1;
13119
13120 #ifdef OBJ_ELF
13121   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13122       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13123       && fixp->fx_subsy == NULL)
13124     return 0;
13125 #endif
13126
13127   return 1;
13128 }
13129
13130 /* Translate internal representation of relocation info to BFD target
13131    format.  */
13132
13133 arelent **
13134 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
13135 {
13136   static arelent *retval[4];
13137   arelent *reloc;
13138   bfd_reloc_code_real_type code;
13139
13140   memset (retval, 0, sizeof(retval));
13141   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13142   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13143   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13144   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13145
13146   if (mips_pic == EMBEDDED_PIC
13147       && SWITCH_TABLE (fixp))
13148     {
13149       /* For a switch table entry we use a special reloc.  The addend
13150          is actually the difference between the reloc address and the
13151          subtrahend.  */
13152       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13153       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13154         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13155       fixp->fx_r_type = BFD_RELOC_GPREL32;
13156     }
13157   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13158     {
13159       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13160         reloc->addend = fixp->fx_addnumber;
13161       else
13162         {
13163           /* We use a special addend for an internal RELLO reloc.  */
13164           if (symbol_section_p (fixp->fx_addsy))
13165             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13166           else
13167             reloc->addend = fixp->fx_addnumber + reloc->address;
13168         }
13169     }
13170   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13171     {
13172       assert (fixp->fx_next != NULL
13173               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13174
13175       /* The reloc is relative to the RELLO; adjust the addend
13176          accordingly.  */
13177       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13178         reloc->addend = fixp->fx_next->fx_addnumber;
13179       else
13180         {
13181           /* We use a special addend for an internal RELHI reloc.  */
13182           if (symbol_section_p (fixp->fx_addsy))
13183             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13184                              + fixp->fx_next->fx_where
13185                              - S_GET_VALUE (fixp->fx_subsy));
13186           else
13187             reloc->addend = (fixp->fx_addnumber
13188                              + fixp->fx_next->fx_frag->fr_address
13189                              + fixp->fx_next->fx_where);
13190         }
13191     }
13192   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13193     reloc->addend = fixp->fx_addnumber;
13194   else
13195     {
13196       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13197         /* A gruesome hack which is a result of the gruesome gas reloc
13198            handling.  */
13199         reloc->addend = reloc->address;
13200       else
13201         reloc->addend = -reloc->address;
13202     }
13203
13204   /* If this is a variant frag, we may need to adjust the existing
13205      reloc and generate a new one.  */
13206   if (fixp->fx_frag->fr_opcode != NULL
13207       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13208            && ! HAVE_NEWABI)
13209           || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13210               && HAVE_NEWABI)
13211           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13212           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13213           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13214           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13215           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13216           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13217     )
13218     {
13219       arelent *reloc2;
13220
13221       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13222
13223       /* If this is not the last reloc in this frag, then we have two
13224          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13225          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13226          the second one handle all of them.  */
13227       if (fixp->fx_next != NULL
13228           && fixp->fx_frag == fixp->fx_next->fx_frag)
13229         {
13230           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13231                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13232                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13233                       && (fixp->fx_next->fx_r_type
13234                           == BFD_RELOC_MIPS_GOT_LO16))
13235                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13236                       && (fixp->fx_next->fx_r_type
13237                           == BFD_RELOC_MIPS_CALL_LO16)));
13238           retval[0] = NULL;
13239           return retval;
13240         }
13241
13242       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13243       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13244       reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13245       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13246       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13247       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13248       reloc2->address = (reloc->address
13249                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13250                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13251       reloc2->addend = fixp->fx_addnumber - S_GET_VALUE (fixp->fx_addsy)
13252         + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13253       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13254       assert (reloc2->howto != NULL);
13255
13256       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13257         {
13258           arelent *reloc3;
13259
13260           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13261           *reloc3 = *reloc2;
13262           reloc3->address += 4;
13263         }
13264
13265       if (mips_pic == NO_PIC)
13266         {
13267           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13268           fixp->fx_r_type = BFD_RELOC_HI16_S;
13269         }
13270       else if (mips_pic == SVR4_PIC)
13271         {
13272           switch (fixp->fx_r_type)
13273             {
13274             default:
13275               abort ();
13276             case BFD_RELOC_MIPS_GOT16:
13277               break;
13278             case BFD_RELOC_MIPS_GOT_LO16:
13279             case BFD_RELOC_MIPS_CALL_LO16:
13280               if (HAVE_NEWABI)
13281                 {
13282                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13283                   reloc2->howto = bfd_reloc_type_lookup
13284                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13285                 }
13286               else
13287                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13288               break;
13289             case BFD_RELOC_MIPS_CALL16:
13290             case BFD_RELOC_MIPS_GOT_OFST:
13291             case BFD_RELOC_MIPS_GOT_DISP:
13292               if (HAVE_NEWABI)
13293                 {
13294                   /* It may seem nonsensical to relax GOT_DISP to
13295                      GOT_DISP, but we're actually turning a GOT_DISP
13296                      without offset into a GOT_DISP with an offset,
13297                      getting rid of the separate addition, which we can
13298                      do when the symbol is found to be local.  */
13299                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13300                   retval[1] = NULL;
13301                 }
13302               else
13303                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13304               break;
13305             }
13306         }
13307       else
13308         abort ();
13309     }
13310
13311   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13312      entry to be used in the relocation's section offset.  */
13313   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13314     {
13315       reloc->address = reloc->addend;
13316       reloc->addend = 0;
13317     }
13318
13319   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13320      fixup_segment converted a non-PC relative reloc into a PC
13321      relative reloc.  In such a case, we need to convert the reloc
13322      code.  */
13323   code = fixp->fx_r_type;
13324   if (fixp->fx_pcrel)
13325     {
13326       switch (code)
13327         {
13328         case BFD_RELOC_8:
13329           code = BFD_RELOC_8_PCREL;
13330           break;
13331         case BFD_RELOC_16:
13332           code = BFD_RELOC_16_PCREL;
13333           break;
13334         case BFD_RELOC_32:
13335           code = BFD_RELOC_32_PCREL;
13336           break;
13337         case BFD_RELOC_64:
13338           code = BFD_RELOC_64_PCREL;
13339           break;
13340         case BFD_RELOC_8_PCREL:
13341         case BFD_RELOC_16_PCREL:
13342         case BFD_RELOC_32_PCREL:
13343         case BFD_RELOC_64_PCREL:
13344         case BFD_RELOC_16_PCREL_S2:
13345         case BFD_RELOC_PCREL_HI16_S:
13346         case BFD_RELOC_PCREL_LO16:
13347           break;
13348         default:
13349           as_bad_where (fixp->fx_file, fixp->fx_line,
13350                         _("Cannot make %s relocation PC relative"),
13351                         bfd_get_reloc_code_name (code));
13352         }
13353     }
13354
13355   /* To support a PC relative reloc when generating embedded PIC code
13356      for ECOFF, we use a Cygnus extension.  We check for that here to
13357      make sure that we don't let such a reloc escape normally.  */
13358   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13359        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13360       && code == BFD_RELOC_16_PCREL_S2
13361       && mips_pic != EMBEDDED_PIC)
13362     reloc->howto = NULL;
13363   else
13364     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13365
13366   if (reloc->howto == NULL)
13367     {
13368       as_bad_where (fixp->fx_file, fixp->fx_line,
13369                     _("Can not represent %s relocation in this object file format"),
13370                     bfd_get_reloc_code_name (code));
13371       retval[0] = NULL;
13372     }
13373
13374   return retval;
13375 }
13376
13377 /* Relax a machine dependent frag.  This returns the amount by which
13378    the current size of the frag should change.  */
13379
13380 int
13381 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13382 {
13383   if (RELAX_BRANCH_P (fragp->fr_subtype))
13384     {
13385       offsetT old_var = fragp->fr_var;
13386
13387       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13388
13389       return fragp->fr_var - old_var;
13390     }
13391
13392   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13393     return 0;
13394
13395   if (mips16_extended_frag (fragp, NULL, stretch))
13396     {
13397       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13398         return 0;
13399       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13400       return 2;
13401     }
13402   else
13403     {
13404       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13405         return 0;
13406       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13407       return -2;
13408     }
13409
13410   return 0;
13411 }
13412
13413 /* Convert a machine dependent frag.  */
13414
13415 void
13416 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13417 {
13418   int old, new;
13419   char *fixptr;
13420
13421   if (RELAX_BRANCH_P (fragp->fr_subtype))
13422     {
13423       bfd_byte *buf;
13424       unsigned long insn;
13425       expressionS exp;
13426       fixS *fixp;
13427
13428       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13429
13430       if (target_big_endian)
13431         insn = bfd_getb32 (buf);
13432       else
13433         insn = bfd_getl32 (buf);
13434
13435       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13436         {
13437           /* We generate a fixup instead of applying it right now
13438              because, if there are linker relaxations, we're going to
13439              need the relocations.  */
13440           exp.X_op = O_symbol;
13441           exp.X_add_symbol = fragp->fr_symbol;
13442           exp.X_add_number = fragp->fr_offset;
13443
13444           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13445                               4, &exp, 1,
13446                               BFD_RELOC_16_PCREL_S2);
13447           fixp->fx_file = fragp->fr_file;
13448           fixp->fx_line = fragp->fr_line;
13449
13450           md_number_to_chars (buf, insn, 4);
13451           buf += 4;
13452         }
13453       else
13454         {
13455           int i;
13456
13457           as_warn_where (fragp->fr_file, fragp->fr_line,
13458                          _("relaxed out-of-range branch into a jump"));
13459
13460           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13461             goto uncond;
13462
13463           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13464             {
13465               /* Reverse the branch.  */
13466               switch ((insn >> 28) & 0xf)
13467                 {
13468                 case 4:
13469                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13470                      have the condition reversed by tweaking a single
13471                      bit, and their opcodes all have 0x4???????.  */
13472                   assert ((insn & 0xf1000000) == 0x41000000);
13473                   insn ^= 0x00010000;
13474                   break;
13475
13476                 case 0:
13477                   /* bltz       0x04000000      bgez    0x04010000
13478                      bltzal     0x04100000      bgezal  0x04110000 */
13479                   assert ((insn & 0xfc0e0000) == 0x04000000);
13480                   insn ^= 0x00010000;
13481                   break;
13482
13483                 case 1:
13484                   /* beq        0x10000000      bne     0x14000000
13485                      blez       0x18000000      bgtz    0x1c000000 */
13486                   insn ^= 0x04000000;
13487                   break;
13488
13489                 default:
13490                   abort ();
13491                 }
13492             }
13493
13494           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13495             {
13496               /* Clear the and-link bit.  */
13497               assert ((insn & 0xfc1c0000) == 0x04100000);
13498
13499               /* bltzal 0x04100000      bgezal  0x04110000
13500                 bltzall 0x04120000     bgezall  0x04130000 */
13501               insn &= ~0x00100000;
13502             }
13503
13504           /* Branch over the branch (if the branch was likely) or the
13505              full jump (not likely case).  Compute the offset from the
13506              current instruction to branch to.  */
13507           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13508             i = 16;
13509           else
13510             {
13511               /* How many bytes in instructions we've already emitted?  */
13512               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13513               /* How many bytes in instructions from here to the end?  */
13514               i = fragp->fr_var - i;
13515             }
13516           /* Convert to instruction count.  */
13517           i >>= 2;
13518           /* Branch counts from the next instruction.  */
13519           i--;
13520           insn |= i;
13521           /* Branch over the jump.  */
13522           md_number_to_chars (buf, insn, 4);
13523           buf += 4;
13524
13525           /* Nop */
13526           md_number_to_chars (buf, 0, 4);
13527           buf += 4;
13528
13529           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13530             {
13531               /* beql $0, $0, 2f */
13532               insn = 0x50000000;
13533               /* Compute the PC offset from the current instruction to
13534                  the end of the variable frag.  */
13535               /* How many bytes in instructions we've already emitted?  */
13536               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13537               /* How many bytes in instructions from here to the end?  */
13538               i = fragp->fr_var - i;
13539               /* Convert to instruction count.  */
13540               i >>= 2;
13541               /* Don't decrement i, because we want to branch over the
13542                  delay slot.  */
13543
13544               insn |= i;
13545               md_number_to_chars (buf, insn, 4);
13546               buf += 4;
13547
13548               md_number_to_chars (buf, 0, 4);
13549               buf += 4;
13550             }
13551
13552         uncond:
13553           if (mips_pic == NO_PIC)
13554             {
13555               /* j or jal.  */
13556               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13557                       ? 0x0c000000 : 0x08000000);
13558               exp.X_op = O_symbol;
13559               exp.X_add_symbol = fragp->fr_symbol;
13560               exp.X_add_number = fragp->fr_offset;
13561
13562               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13563                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13564               fixp->fx_file = fragp->fr_file;
13565               fixp->fx_line = fragp->fr_line;
13566
13567               md_number_to_chars (buf, insn, 4);
13568               buf += 4;
13569             }
13570           else
13571             {
13572               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13573               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13574               exp.X_op = O_symbol;
13575               exp.X_add_symbol = fragp->fr_symbol;
13576               exp.X_add_number = fragp->fr_offset;
13577
13578               if (fragp->fr_offset)
13579                 {
13580                   exp.X_add_symbol = make_expr_symbol (&exp);
13581                   exp.X_add_number = 0;
13582                 }
13583
13584               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13585                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13586               fixp->fx_file = fragp->fr_file;
13587               fixp->fx_line = fragp->fr_line;
13588
13589               md_number_to_chars (buf, insn, 4);
13590               buf += 4;
13591
13592               if (mips_opts.isa == ISA_MIPS1)
13593                 {
13594                   /* nop */
13595                   md_number_to_chars (buf, 0, 4);
13596                   buf += 4;
13597                 }
13598
13599               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13600               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13601
13602               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13603                                   4, &exp, 0, BFD_RELOC_LO16);
13604               fixp->fx_file = fragp->fr_file;
13605               fixp->fx_line = fragp->fr_line;
13606
13607               md_number_to_chars (buf, insn, 4);
13608               buf += 4;
13609
13610               /* j(al)r $at.  */
13611               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13612                 insn = 0x0020f809;
13613               else
13614                 insn = 0x00200008;
13615
13616               md_number_to_chars (buf, insn, 4);
13617               buf += 4;
13618             }
13619         }
13620
13621       assert (buf == (bfd_byte *)fragp->fr_literal
13622               + fragp->fr_fix + fragp->fr_var);
13623
13624       fragp->fr_fix += fragp->fr_var;
13625
13626       return;
13627     }
13628
13629   if (RELAX_MIPS16_P (fragp->fr_subtype))
13630     {
13631       int type;
13632       register const struct mips16_immed_operand *op;
13633       bfd_boolean small, ext;
13634       offsetT val;
13635       bfd_byte *buf;
13636       unsigned long insn;
13637       bfd_boolean use_extend;
13638       unsigned short extend;
13639
13640       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13641       op = mips16_immed_operands;
13642       while (op->type != type)
13643         ++op;
13644
13645       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13646         {
13647           small = FALSE;
13648           ext = TRUE;
13649         }
13650       else
13651         {
13652           small = TRUE;
13653           ext = FALSE;
13654         }
13655
13656       resolve_symbol_value (fragp->fr_symbol);
13657       val = S_GET_VALUE (fragp->fr_symbol);
13658       if (op->pcrel)
13659         {
13660           addressT addr;
13661
13662           addr = fragp->fr_address + fragp->fr_fix;
13663
13664           /* The rules for the base address of a PC relative reloc are
13665              complicated; see mips16_extended_frag.  */
13666           if (type == 'p' || type == 'q')
13667             {
13668               addr += 2;
13669               if (ext)
13670                 addr += 2;
13671               /* Ignore the low bit in the target, since it will be
13672                  set for a text label.  */
13673               if ((val & 1) != 0)
13674                 --val;
13675             }
13676           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13677             addr -= 4;
13678           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13679             addr -= 2;
13680
13681           addr &= ~ (addressT) ((1 << op->shift) - 1);
13682           val -= addr;
13683
13684           /* Make sure the section winds up with the alignment we have
13685              assumed.  */
13686           if (op->shift > 0)
13687             record_alignment (asec, op->shift);
13688         }
13689
13690       if (ext
13691           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13692               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13693         as_warn_where (fragp->fr_file, fragp->fr_line,
13694                        _("extended instruction in delay slot"));
13695
13696       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13697
13698       if (target_big_endian)
13699         insn = bfd_getb16 (buf);
13700       else
13701         insn = bfd_getl16 (buf);
13702
13703       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13704                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13705                     small, ext, &insn, &use_extend, &extend);
13706
13707       if (use_extend)
13708         {
13709           md_number_to_chars (buf, 0xf000 | extend, 2);
13710           fragp->fr_fix += 2;
13711           buf += 2;
13712         }
13713
13714       md_number_to_chars (buf, insn, 2);
13715       fragp->fr_fix += 2;
13716       buf += 2;
13717     }
13718   else
13719     {
13720       if (fragp->fr_opcode == NULL)
13721         return;
13722
13723       old = RELAX_OLD (fragp->fr_subtype);
13724       new = RELAX_NEW (fragp->fr_subtype);
13725       fixptr = fragp->fr_literal + fragp->fr_fix;
13726
13727       if (new > 0)
13728         memmove (fixptr - old, fixptr, new);
13729
13730       fragp->fr_fix += new - old;
13731     }
13732 }
13733
13734 #ifdef OBJ_ELF
13735
13736 /* This function is called after the relocs have been generated.
13737    We've been storing mips16 text labels as odd.  Here we convert them
13738    back to even for the convenience of the debugger.  */
13739
13740 void
13741 mips_frob_file_after_relocs (void)
13742 {
13743   asymbol **syms;
13744   unsigned int count, i;
13745
13746   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13747     return;
13748
13749   syms = bfd_get_outsymbols (stdoutput);
13750   count = bfd_get_symcount (stdoutput);
13751   for (i = 0; i < count; i++, syms++)
13752     {
13753       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13754           && ((*syms)->value & 1) != 0)
13755         {
13756           (*syms)->value &= ~1;
13757           /* If the symbol has an odd size, it was probably computed
13758              incorrectly, so adjust that as well.  */
13759           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13760             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13761         }
13762     }
13763 }
13764
13765 #endif
13766
13767 /* This function is called whenever a label is defined.  It is used
13768    when handling branch delays; if a branch has a label, we assume we
13769    can not move it.  */
13770
13771 void
13772 mips_define_label (symbolS *sym)
13773 {
13774   struct insn_label_list *l;
13775
13776   if (free_insn_labels == NULL)
13777     l = (struct insn_label_list *) xmalloc (sizeof *l);
13778   else
13779     {
13780       l = free_insn_labels;
13781       free_insn_labels = l->next;
13782     }
13783
13784   l->label = sym;
13785   l->next = insn_labels;
13786   insn_labels = l;
13787 }
13788 \f
13789 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13790
13791 /* Some special processing for a MIPS ELF file.  */
13792
13793 void
13794 mips_elf_final_processing (void)
13795 {
13796   /* Write out the register information.  */
13797   if (mips_abi != N64_ABI)
13798     {
13799       Elf32_RegInfo s;
13800
13801       s.ri_gprmask = mips_gprmask;
13802       s.ri_cprmask[0] = mips_cprmask[0];
13803       s.ri_cprmask[1] = mips_cprmask[1];
13804       s.ri_cprmask[2] = mips_cprmask[2];
13805       s.ri_cprmask[3] = mips_cprmask[3];
13806       /* The gp_value field is set by the MIPS ELF backend.  */
13807
13808       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13809                                        ((Elf32_External_RegInfo *)
13810                                         mips_regmask_frag));
13811     }
13812   else
13813     {
13814       Elf64_Internal_RegInfo s;
13815
13816       s.ri_gprmask = mips_gprmask;
13817       s.ri_pad = 0;
13818       s.ri_cprmask[0] = mips_cprmask[0];
13819       s.ri_cprmask[1] = mips_cprmask[1];
13820       s.ri_cprmask[2] = mips_cprmask[2];
13821       s.ri_cprmask[3] = mips_cprmask[3];
13822       /* The gp_value field is set by the MIPS ELF backend.  */
13823
13824       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13825                                        ((Elf64_External_RegInfo *)
13826                                         mips_regmask_frag));
13827     }
13828
13829   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13830      sort of BFD interface for this.  */
13831   if (mips_any_noreorder)
13832     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13833   if (mips_pic != NO_PIC)
13834     {
13835     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13836       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13837     }
13838   if (mips_abicalls)
13839     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13840
13841   /* Set MIPS ELF flags for ASEs.  */
13842   if (file_ase_mips16)
13843     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13844 #if 0 /* XXX FIXME */
13845   if (file_ase_mips3d)
13846     elf_elfheader (stdoutput)->e_flags |= ???;
13847 #endif
13848   if (file_ase_mdmx)
13849     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13850
13851   /* Set the MIPS ELF ABI flags.  */
13852   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13853     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13854   else if (mips_abi == O64_ABI)
13855     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13856   else if (mips_abi == EABI_ABI)
13857     {
13858       if (!file_mips_gp32)
13859         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13860       else
13861         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13862     }
13863   else if (mips_abi == N32_ABI)
13864     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13865
13866   /* Nothing to do for N64_ABI.  */
13867
13868   if (mips_32bitmode)
13869     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13870 }
13871
13872 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13873 \f
13874 typedef struct proc {
13875   symbolS *isym;
13876   unsigned long reg_mask;
13877   unsigned long reg_offset;
13878   unsigned long fpreg_mask;
13879   unsigned long fpreg_offset;
13880   unsigned long frame_offset;
13881   unsigned long frame_reg;
13882   unsigned long pc_reg;
13883 } procS;
13884
13885 static procS cur_proc;
13886 static procS *cur_proc_ptr;
13887 static int numprocs;
13888
13889 /* Fill in an rs_align_code fragment.  */
13890
13891 void
13892 mips_handle_align (fragS *fragp)
13893 {
13894   if (fragp->fr_type != rs_align_code)
13895     return;
13896
13897   if (mips_opts.mips16)
13898     {
13899       static const unsigned char be_nop[] = { 0x65, 0x00 };
13900       static const unsigned char le_nop[] = { 0x00, 0x65 };
13901
13902       int bytes;
13903       char *p;
13904
13905       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13906       p = fragp->fr_literal + fragp->fr_fix;
13907
13908       if (bytes & 1)
13909         {
13910           *p++ = 0;
13911           fragp->fr_fix++;
13912         }
13913
13914       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13915       fragp->fr_var = 2;
13916     }
13917
13918   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13919 }
13920
13921 static void
13922 md_obj_begin (void)
13923 {
13924 }
13925
13926 static void
13927 md_obj_end (void)
13928 {
13929   /* check for premature end, nesting errors, etc */
13930   if (cur_proc_ptr)
13931     as_warn (_("missing .end at end of assembly"));
13932 }
13933
13934 static long
13935 get_number (void)
13936 {
13937   int negative = 0;
13938   long val = 0;
13939
13940   if (*input_line_pointer == '-')
13941     {
13942       ++input_line_pointer;
13943       negative = 1;
13944     }
13945   if (!ISDIGIT (*input_line_pointer))
13946     as_bad (_("expected simple number"));
13947   if (input_line_pointer[0] == '0')
13948     {
13949       if (input_line_pointer[1] == 'x')
13950         {
13951           input_line_pointer += 2;
13952           while (ISXDIGIT (*input_line_pointer))
13953             {
13954               val <<= 4;
13955               val |= hex_value (*input_line_pointer++);
13956             }
13957           return negative ? -val : val;
13958         }
13959       else
13960         {
13961           ++input_line_pointer;
13962           while (ISDIGIT (*input_line_pointer))
13963             {
13964               val <<= 3;
13965               val |= *input_line_pointer++ - '0';
13966             }
13967           return negative ? -val : val;
13968         }
13969     }
13970   if (!ISDIGIT (*input_line_pointer))
13971     {
13972       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13973               *input_line_pointer, *input_line_pointer);
13974       as_warn (_("invalid number"));
13975       return -1;
13976     }
13977   while (ISDIGIT (*input_line_pointer))
13978     {
13979       val *= 10;
13980       val += *input_line_pointer++ - '0';
13981     }
13982   return negative ? -val : val;
13983 }
13984
13985 /* The .file directive; just like the usual .file directive, but there
13986    is an initial number which is the ECOFF file index.  In the non-ECOFF
13987    case .file implies DWARF-2.  */
13988
13989 static void
13990 s_mips_file (int x ATTRIBUTE_UNUSED)
13991 {
13992   static int first_file_directive = 0;
13993
13994   if (ECOFF_DEBUGGING)
13995     {
13996       get_number ();
13997       s_app_file (0);
13998     }
13999   else
14000     {
14001       char *filename;
14002
14003       filename = dwarf2_directive_file (0);
14004
14005       /* Versions of GCC up to 3.1 start files with a ".file"
14006          directive even for stabs output.  Make sure that this
14007          ".file" is handled.  Note that you need a version of GCC
14008          after 3.1 in order to support DWARF-2 on MIPS.  */
14009       if (filename != NULL && ! first_file_directive)
14010         {
14011           (void) new_logical_line (filename, -1);
14012           s_app_file_string (filename);
14013         }
14014       first_file_directive = 1;
14015     }
14016 }
14017
14018 /* The .loc directive, implying DWARF-2.  */
14019
14020 static void
14021 s_mips_loc (int x ATTRIBUTE_UNUSED)
14022 {
14023   if (!ECOFF_DEBUGGING)
14024     dwarf2_directive_loc (0);
14025 }
14026
14027 /* The .end directive.  */
14028
14029 static void
14030 s_mips_end (int x ATTRIBUTE_UNUSED)
14031 {
14032   symbolS *p;
14033
14034   /* Following functions need their own .frame and .cprestore directives.  */
14035   mips_frame_reg_valid = 0;
14036   mips_cprestore_valid = 0;
14037
14038   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14039     {
14040       p = get_symbol ();
14041       demand_empty_rest_of_line ();
14042     }
14043   else
14044     p = NULL;
14045
14046   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14047     as_warn (_(".end not in text section"));
14048
14049   if (!cur_proc_ptr)
14050     {
14051       as_warn (_(".end directive without a preceding .ent directive."));
14052       demand_empty_rest_of_line ();
14053       return;
14054     }
14055
14056   if (p != NULL)
14057     {
14058       assert (S_GET_NAME (p));
14059       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14060         as_warn (_(".end symbol does not match .ent symbol."));
14061
14062       if (debug_type == DEBUG_STABS)
14063         stabs_generate_asm_endfunc (S_GET_NAME (p),
14064                                     S_GET_NAME (p));
14065     }
14066   else
14067     as_warn (_(".end directive missing or unknown symbol"));
14068
14069 #ifdef OBJ_ELF
14070   /* Generate a .pdr section.  */
14071   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14072       && mips_flag_pdr)
14073     {
14074       segT saved_seg = now_seg;
14075       subsegT saved_subseg = now_subseg;
14076       valueT dot;
14077       expressionS exp;
14078       char *fragp;
14079
14080       dot = frag_now_fix ();
14081
14082 #ifdef md_flush_pending_output
14083       md_flush_pending_output ();
14084 #endif
14085
14086       assert (pdr_seg);
14087       subseg_set (pdr_seg, 0);
14088
14089       /* Write the symbol.  */
14090       exp.X_op = O_symbol;
14091       exp.X_add_symbol = p;
14092       exp.X_add_number = 0;
14093       emit_expr (&exp, 4);
14094
14095       fragp = frag_more (7 * 4);
14096
14097       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14098       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14099       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14100       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14101       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14102       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14103       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
14104
14105       subseg_set (saved_seg, saved_subseg);
14106     }
14107 #endif /* OBJ_ELF */
14108
14109   cur_proc_ptr = NULL;
14110 }
14111
14112 /* The .aent and .ent directives.  */
14113
14114 static void
14115 s_mips_ent (int aent)
14116 {
14117   symbolS *symbolP;
14118
14119   symbolP = get_symbol ();
14120   if (*input_line_pointer == ',')
14121     ++input_line_pointer;
14122   SKIP_WHITESPACE ();
14123   if (ISDIGIT (*input_line_pointer)
14124       || *input_line_pointer == '-')
14125     get_number ();
14126
14127   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14128     as_warn (_(".ent or .aent not in text section."));
14129
14130   if (!aent && cur_proc_ptr)
14131     as_warn (_("missing .end"));
14132
14133   if (!aent)
14134     {
14135       /* This function needs its own .frame and .cprestore directives.  */
14136       mips_frame_reg_valid = 0;
14137       mips_cprestore_valid = 0;
14138
14139       cur_proc_ptr = &cur_proc;
14140       memset (cur_proc_ptr, '\0', sizeof (procS));
14141
14142       cur_proc_ptr->isym = symbolP;
14143
14144       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14145
14146       ++numprocs;
14147
14148       if (debug_type == DEBUG_STABS)
14149         stabs_generate_asm_func (S_GET_NAME (symbolP),
14150                                  S_GET_NAME (symbolP));
14151     }
14152
14153   demand_empty_rest_of_line ();
14154 }
14155
14156 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14157    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14158    s_mips_frame is used so that we can set the PDR information correctly.
14159    We can't use the ecoff routines because they make reference to the ecoff
14160    symbol table (in the mdebug section).  */
14161
14162 static void
14163 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
14164 {
14165 #ifdef OBJ_ELF
14166   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14167     {
14168       long val;
14169
14170       if (cur_proc_ptr == (procS *) NULL)
14171         {
14172           as_warn (_(".frame outside of .ent"));
14173           demand_empty_rest_of_line ();
14174           return;
14175         }
14176
14177       cur_proc_ptr->frame_reg = tc_get_register (1);
14178
14179       SKIP_WHITESPACE ();
14180       if (*input_line_pointer++ != ','
14181           || get_absolute_expression_and_terminator (&val) != ',')
14182         {
14183           as_warn (_("Bad .frame directive"));
14184           --input_line_pointer;
14185           demand_empty_rest_of_line ();
14186           return;
14187         }
14188
14189       cur_proc_ptr->frame_offset = val;
14190       cur_proc_ptr->pc_reg = tc_get_register (0);
14191
14192       demand_empty_rest_of_line ();
14193     }
14194   else
14195 #endif /* OBJ_ELF */
14196     s_ignore (ignore);
14197 }
14198
14199 /* The .fmask and .mask directives. If the mdebug section is present
14200    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14201    embedded targets, s_mips_mask is used so that we can set the PDR
14202    information correctly. We can't use the ecoff routines because they
14203    make reference to the ecoff symbol table (in the mdebug section).  */
14204
14205 static void
14206 s_mips_mask (int reg_type)
14207 {
14208 #ifdef OBJ_ELF
14209   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14210     {
14211       long mask, off;
14212
14213       if (cur_proc_ptr == (procS *) NULL)
14214         {
14215           as_warn (_(".mask/.fmask outside of .ent"));
14216           demand_empty_rest_of_line ();
14217           return;
14218         }
14219
14220       if (get_absolute_expression_and_terminator (&mask) != ',')
14221         {
14222           as_warn (_("Bad .mask/.fmask directive"));
14223           --input_line_pointer;
14224           demand_empty_rest_of_line ();
14225           return;
14226         }
14227
14228       off = get_absolute_expression ();
14229
14230       if (reg_type == 'F')
14231         {
14232           cur_proc_ptr->fpreg_mask = mask;
14233           cur_proc_ptr->fpreg_offset = off;
14234         }
14235       else
14236         {
14237           cur_proc_ptr->reg_mask = mask;
14238           cur_proc_ptr->reg_offset = off;
14239         }
14240
14241       demand_empty_rest_of_line ();
14242     }
14243   else
14244 #endif /* OBJ_ELF */
14245     s_ignore (reg_type);
14246 }
14247
14248 /* The .loc directive.  */
14249
14250 #if 0
14251 static void
14252 s_loc (int x)
14253 {
14254   symbolS *symbolP;
14255   int lineno;
14256   int addroff;
14257
14258   assert (now_seg == text_section);
14259
14260   lineno = get_number ();
14261   addroff = frag_now_fix ();
14262
14263   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14264   S_SET_TYPE (symbolP, N_SLINE);
14265   S_SET_OTHER (symbolP, 0);
14266   S_SET_DESC (symbolP, lineno);
14267   symbolP->sy_segment = now_seg;
14268 }
14269 #endif
14270
14271 /* A table describing all the processors gas knows about.  Names are
14272    matched in the order listed.
14273
14274    To ease comparison, please keep this table in the same order as
14275    gcc's mips_cpu_info_table[].  */
14276 static const struct mips_cpu_info mips_cpu_info_table[] =
14277 {
14278   /* Entries for generic ISAs */
14279   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14280   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14281   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14282   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14283   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14284   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14285   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14286   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14287
14288   /* MIPS I */
14289   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14290   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14291   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14292
14293   /* MIPS II */
14294   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14295
14296   /* MIPS III */
14297   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14298   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14299   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14300   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14301   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14302   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14303   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14304   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14305   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14306   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14307   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14308   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14309
14310   /* MIPS IV */
14311   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14312   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14313   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14314   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14315   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14316   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14317   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14318   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14319   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14320   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14321   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14322   { "rm7000",         0,      ISA_MIPS4,      CPU_RM7000 },
14323   { "rm9000",         0,      ISA_MIPS4,      CPU_RM7000 },
14324
14325   /* MIPS 32 */
14326   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32 },
14327   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14328   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14329
14330   /* MIPS 64 */
14331   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14332   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14333
14334   /* Broadcom SB-1 CPU core */
14335   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14336
14337   /* End marker */
14338   { NULL, 0, 0, 0 }
14339 };
14340
14341
14342 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14343    with a final "000" replaced by "k".  Ignore case.
14344
14345    Note: this function is shared between GCC and GAS.  */
14346
14347 static bfd_boolean
14348 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14349 {
14350   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14351     given++, canonical++;
14352
14353   return ((*given == 0 && *canonical == 0)
14354           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14355 }
14356
14357
14358 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14359    CPU name.  We've traditionally allowed a lot of variation here.
14360
14361    Note: this function is shared between GCC and GAS.  */
14362
14363 static bfd_boolean
14364 mips_matching_cpu_name_p (const char *canonical, const char *given)
14365 {
14366   /* First see if the name matches exactly, or with a final "000"
14367      turned into "k".  */
14368   if (mips_strict_matching_cpu_name_p (canonical, given))
14369     return TRUE;
14370
14371   /* If not, try comparing based on numerical designation alone.
14372      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14373   if (TOLOWER (*given) == 'r')
14374     given++;
14375   if (!ISDIGIT (*given))
14376     return FALSE;
14377
14378   /* Skip over some well-known prefixes in the canonical name,
14379      hoping to find a number there too.  */
14380   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14381     canonical += 2;
14382   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14383     canonical += 2;
14384   else if (TOLOWER (canonical[0]) == 'r')
14385     canonical += 1;
14386
14387   return mips_strict_matching_cpu_name_p (canonical, given);
14388 }
14389
14390
14391 /* Parse an option that takes the name of a processor as its argument.
14392    OPTION is the name of the option and CPU_STRING is the argument.
14393    Return the corresponding processor enumeration if the CPU_STRING is
14394    recognized, otherwise report an error and return null.
14395
14396    A similar function exists in GCC.  */
14397
14398 static const struct mips_cpu_info *
14399 mips_parse_cpu (const char *option, const char *cpu_string)
14400 {
14401   const struct mips_cpu_info *p;
14402
14403   /* 'from-abi' selects the most compatible architecture for the given
14404      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14405      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14406      version.  Look first at the -mgp options, if given, otherwise base
14407      the choice on MIPS_DEFAULT_64BIT.
14408
14409      Treat NO_ABI like the EABIs.  One reason to do this is that the
14410      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14411      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14412      'mips64', just as we did in the days before 'from-abi'.  */
14413   if (strcasecmp (cpu_string, "from-abi") == 0)
14414     {
14415       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14416         return mips_cpu_info_from_isa (ISA_MIPS1);
14417
14418       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14419         return mips_cpu_info_from_isa (ISA_MIPS3);
14420
14421       if (file_mips_gp32 >= 0)
14422         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14423
14424       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14425                                      ? ISA_MIPS3
14426                                      : ISA_MIPS1);
14427     }
14428
14429   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14430   if (strcasecmp (cpu_string, "default") == 0)
14431     return 0;
14432
14433   for (p = mips_cpu_info_table; p->name != 0; p++)
14434     if (mips_matching_cpu_name_p (p->name, cpu_string))
14435       return p;
14436
14437   as_bad ("Bad value (%s) for %s", cpu_string, option);
14438   return 0;
14439 }
14440
14441 /* Return the canonical processor information for ISA (a member of the
14442    ISA_MIPS* enumeration).  */
14443
14444 static const struct mips_cpu_info *
14445 mips_cpu_info_from_isa (int isa)
14446 {
14447   int i;
14448
14449   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14450     if (mips_cpu_info_table[i].is_isa
14451         && isa == mips_cpu_info_table[i].isa)
14452       return (&mips_cpu_info_table[i]);
14453
14454   return NULL;
14455 }
14456
14457 static const struct mips_cpu_info *
14458 mips_cpu_info_from_arch (int arch)
14459 {
14460   int i;
14461
14462   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14463     if (arch == mips_cpu_info_table[i].cpu)
14464       return (&mips_cpu_info_table[i]);
14465
14466   return NULL;
14467 }
14468 \f
14469 static void
14470 show (FILE *stream, const char *string, int *col_p, int *first_p)
14471 {
14472   if (*first_p)
14473     {
14474       fprintf (stream, "%24s", "");
14475       *col_p = 24;
14476     }
14477   else
14478     {
14479       fprintf (stream, ", ");
14480       *col_p += 2;
14481     }
14482
14483   if (*col_p + strlen (string) > 72)
14484     {
14485       fprintf (stream, "\n%24s", "");
14486       *col_p = 24;
14487     }
14488
14489   fprintf (stream, "%s", string);
14490   *col_p += strlen (string);
14491
14492   *first_p = 0;
14493 }
14494
14495 void
14496 md_show_usage (FILE *stream)
14497 {
14498   int column, first;
14499   size_t i;
14500
14501   fprintf (stream, _("\
14502 MIPS options:\n\
14503 -membedded-pic          generate embedded position independent code\n\
14504 -EB                     generate big endian output\n\
14505 -EL                     generate little endian output\n\
14506 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14507 -G NUM                  allow referencing objects up to NUM bytes\n\
14508                         implicitly with the gp register [default 8]\n"));
14509   fprintf (stream, _("\
14510 -mips1                  generate MIPS ISA I instructions\n\
14511 -mips2                  generate MIPS ISA II instructions\n\
14512 -mips3                  generate MIPS ISA III instructions\n\
14513 -mips4                  generate MIPS ISA IV instructions\n\
14514 -mips5                  generate MIPS ISA V instructions\n\
14515 -mips32                 generate MIPS32 ISA instructions\n\
14516 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
14517 -mips64                 generate MIPS64 ISA instructions\n\
14518 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14519
14520   first = 1;
14521
14522   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14523     show (stream, mips_cpu_info_table[i].name, &column, &first);
14524   show (stream, "from-abi", &column, &first);
14525   fputc ('\n', stream);
14526
14527   fprintf (stream, _("\
14528 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14529 -no-mCPU                don't generate code specific to CPU.\n\
14530                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14531
14532   first = 1;
14533
14534   show (stream, "3900", &column, &first);
14535   show (stream, "4010", &column, &first);
14536   show (stream, "4100", &column, &first);
14537   show (stream, "4650", &column, &first);
14538   fputc ('\n', stream);
14539
14540   fprintf (stream, _("\
14541 -mips16                 generate mips16 instructions\n\
14542 -no-mips16              do not generate mips16 instructions\n"));
14543   fprintf (stream, _("\
14544 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14545 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14546 -O0                     remove unneeded NOPs, do not swap branches\n\
14547 -O                      remove unneeded NOPs and swap branches\n\
14548 -n                      warn about NOPs generated from macros\n\
14549 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14550 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14551 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14552 #ifdef OBJ_ELF
14553   fprintf (stream, _("\
14554 -KPIC, -call_shared     generate SVR4 position independent code\n\
14555 -non_shared             do not generate position independent code\n\
14556 -xgot                   assume a 32 bit GOT\n\
14557 -mpdr, -mno-pdr         enable/disable creation of .pdr sections\n\
14558 -mabi=ABI               create ABI conformant object file for:\n"));
14559
14560   first = 1;
14561
14562   show (stream, "32", &column, &first);
14563   show (stream, "o64", &column, &first);
14564   show (stream, "n32", &column, &first);
14565   show (stream, "64", &column, &first);
14566   show (stream, "eabi", &column, &first);
14567
14568   fputc ('\n', stream);
14569
14570   fprintf (stream, _("\
14571 -32                     create o32 ABI object file (default)\n\
14572 -n32                    create n32 ABI object file\n\
14573 -64                     create 64 ABI object file\n"));
14574 #endif
14575 }
14576
14577 enum dwarf2_format
14578 mips_dwarf2_format (void)
14579 {
14580   if (mips_abi == N64_ABI)
14581     {
14582 #ifdef TE_IRIX
14583       return dwarf2_format_64bit_irix;
14584 #else
14585       return dwarf2_format_64bit;
14586 #endif
14587     }
14588   else
14589     return dwarf2_format_32bit;
14590 }