* config/tc-mips.c (macro): Put back `+ 0x8000' in test for 64-bit
[platform/upstream/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too.  */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT  1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP  28
95 #define SP  29
96 #define FP  30
97 #define RA  31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format.  */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section.  */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111                             ? ".data" \
112                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113                             ? ".rdata" \
114                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115                             ? ".rdata" \
116                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117                             ? ".rodata" \
118                             : (abort (), ""))
119
120 /* The ABI to use.  */
121 enum mips_abi_level
122 {
123   NO_ABI = 0,
124   O32_ABI,
125   O64_ABI,
126   N32_ABI,
127   N64_ABI,
128   EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file.  */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* This is the set of options which may be modified by the .set
135    pseudo-op.  We use a struct so that .set push and .set pop are more
136    reliable.  */
137
138 struct mips_set_options
139 {
140   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
141      if it has not been initialized.  Changed by `.set mipsN', and the
142      -mipsN command line option, and the default CPU.  */
143   int isa;
144   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
145      if they have not been initialized.  Changed by `.set <asename>', by
146      command line options, and based on the default architecture.  */
147   int ase_mips3d;
148   int ase_mdmx;
149   /* Whether we are assembling for the mips16 processor.  0 if we are
150      not, 1 if we are, and -1 if the value has not been initialized.
151      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152      -nomips16 command line options, and the default CPU.  */
153   int mips16;
154   /* Non-zero if we should not reorder instructions.  Changed by `.set
155      reorder' and `.set noreorder'.  */
156   int noreorder;
157   /* Non-zero if we should not permit the $at ($1) register to be used
158      in instructions.  Changed by `.set at' and `.set noat'.  */
159   int noat;
160   /* Non-zero if we should warn when a macro instruction expands into
161      more than one machine instruction.  Changed by `.set nomacro' and
162      `.set macro'.  */
163   int warn_about_macros;
164   /* Non-zero if we should not move instructions.  Changed by `.set
165      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
166   int nomove;
167   /* Non-zero if we should not optimize branches by moving the target
168      of the branch into the delay slot.  Actually, we don't perform
169      this optimization anyhow.  Changed by `.set bopt' and `.set
170      nobopt'.  */
171   int nobopt;
172   /* Non-zero if we should not autoextend mips16 instructions.
173      Changed by `.set autoextend' and `.set noautoextend'.  */
174   int noautoextend;
175   /* Restrict general purpose registers and floating point registers
176      to 32 bit.  This is initially determined when -mgp32 or -mfp32
177      is passed but can changed if the assembler code uses .set mipsN.  */
178   int gp32;
179   int fp32;
180 };
181
182 /* True if -mgp32 was passed.  */
183 static int file_mips_gp32 = -1;
184
185 /* True if -mfp32 was passed.  */
186 static int file_mips_fp32 = -1;
187
188 /* This is the struct we use to hold the current set of options.  Note
189    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190    -1 to indicate that they have not been initialized.  */
191
192 static struct mips_set_options mips_opts =
193 {
194   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
195 };
196
197 /* These variables are filled in with the masks of registers used.
198    The object format code reads them and puts them in the appropriate
199    place.  */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
202
203 /* MIPS ISA we are using for this output file.  */
204 static int file_mips_isa = ISA_UNKNOWN;
205
206 /* True if -mips16 was passed or implied by arguments passed on the
207    command line (e.g., by -march).  */
208 static int file_ase_mips16;
209
210 /* True if -mips3d was passed or implied by arguments passed on the
211    command line (e.g., by -march).  */
212 static int file_ase_mips3d;
213
214 /* True if -mdmx was passed or implied by arguments passed on the
215    command line (e.g., by -march).  */
216 static int file_ase_mdmx;
217
218 /* The argument of the -march= flag.  The architecture we are assembling.  */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
222
223 /* The argument of the -mtune= flag.  The architecture for which we
224    are optimizing.  */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
228
229 /* True when generating 32-bit code for a 64-bit processor.  */
230 static int mips_32bitmode = 0;
231
232 /* Some ISA's have delay slots for instructions which read or write
233    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236    delay slot in this ISA.  The uses of this macro assume that any
237    ISA that has delay slots for one of these, has them for all.  They
238    also assume that ISAs which don't have delays for these insns, don't
239    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
240 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
241    (ISA) == ISA_MIPS1                       \
242    || (ISA) == ISA_MIPS2                    \
243    || (ISA) == ISA_MIPS3                    \
244    )
245
246 /* True if the given ABI requires 32-bit registers.  */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249 /* Likewise 64-bit registers.  */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251   ((ABI) == N32_ABI               \
252    || (ABI) == N64_ABI            \
253    || (ABI) == O64_ABI)
254
255 /*  Return true if ISA supports 64 bit gp register instructions.  */
256 #define ISA_HAS_64BIT_REGS(ISA) (    \
257    (ISA) == ISA_MIPS3                \
258    || (ISA) == ISA_MIPS4             \
259    || (ISA) == ISA_MIPS5             \
260    || (ISA) == ISA_MIPS64            \
261    )
262
263 /* Return true if ISA supports 64-bit right rotate (dror et al.)
264    instructions.  */
265 #define ISA_HAS_DROR(ISA) (     \
266    0                            \
267    )
268
269 /* Return true if ISA supports 32-bit right rotate (ror et al.)
270    instructions.  */
271 #define ISA_HAS_ROR(ISA) (      \
272    (ISA) == ISA_MIPS32R2        \
273    )
274
275 #define HAVE_32BIT_GPRS                            \
276     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
277
278 #define HAVE_32BIT_FPRS                            \
279     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
280
281 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
282 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
283
284 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
285
286 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
287
288 /* We can only have 64bit addresses if the object file format
289    supports it.  */
290 #define HAVE_32BIT_ADDRESSES                           \
291    (HAVE_32BIT_GPRS                                    \
292     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
293          || ! HAVE_64BIT_OBJECTS)                      \
294         && mips_pic != EMBEDDED_PIC))
295
296 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
297 #define HAVE_64BIT_ADDRESS_CONSTANTS (HAVE_64BIT_ADDRESSES \
298                                       || HAVE_64BIT_GPRS)
299
300 /* Return true if the given CPU supports the MIPS16 ASE.  */
301 #define CPU_HAS_MIPS16(cpu)                                             \
302    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
303     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
304
305 /* Return true if the given CPU supports the MIPS3D ASE.  */
306 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
307                                  )
308
309 /* Return true if the given CPU supports the MDMX ASE.  */
310 #define CPU_HAS_MDMX(cpu)       (FALSE                 \
311                                  )
312
313 /* True if CPU has a dror instruction.  */
314 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
315
316 /* True if CPU has a ror instruction.  */
317 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
318
319 /* Whether the processor uses hardware interlocks to protect
320    reads from the HI and LO registers, and thus does not
321    require nops to be inserted.  */
322
323 #define hilo_interlocks (mips_arch == CPU_R4010                       \
324                          || mips_arch == CPU_VR5500                   \
325                          || mips_arch == CPU_SB1                      \
326                          )
327
328 /* Whether the processor uses hardware interlocks to protect reads
329    from the GPRs, and thus does not require nops to be inserted.  */
330 #define gpr_interlocks \
331   (mips_opts.isa != ISA_MIPS1  \
332    || mips_arch == CPU_VR5400  \
333    || mips_arch == CPU_VR5500  \
334    || mips_arch == CPU_R3900)
335
336 /* As with other "interlocks" this is used by hardware that has FP
337    (co-processor) interlocks.  */
338 /* Itbl support may require additional care here.  */
339 #define cop_interlocks (mips_arch == CPU_R4300                        \
340                         || mips_arch == CPU_VR5400                    \
341                         || mips_arch == CPU_VR5500                    \
342                         || mips_arch == CPU_SB1                       \
343                         )
344
345 /* Is this a mfhi or mflo instruction?  */
346 #define MF_HILO_INSN(PINFO) \
347           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
348
349 /* MIPS PIC level.  */
350
351 enum mips_pic_level mips_pic;
352
353 /* Warn about all NOPS that the assembler generates.  */
354 static int warn_nops = 0;
355
356 /* 1 if we should generate 32 bit offsets from the $gp register in
357    SVR4_PIC mode.  Currently has no meaning in other modes.  */
358 static int mips_big_got = 0;
359
360 /* 1 if trap instructions should used for overflow rather than break
361    instructions.  */
362 static int mips_trap = 0;
363
364 /* 1 if double width floating point constants should not be constructed
365    by assembling two single width halves into two single width floating
366    point registers which just happen to alias the double width destination
367    register.  On some architectures this aliasing can be disabled by a bit
368    in the status register, and the setting of this bit cannot be determined
369    automatically at assemble time.  */
370 static int mips_disable_float_construction;
371
372 /* Non-zero if any .set noreorder directives were used.  */
373
374 static int mips_any_noreorder;
375
376 /* Non-zero if nops should be inserted when the register referenced in
377    an mfhi/mflo instruction is read in the next two instructions.  */
378 static int mips_7000_hilo_fix;
379
380 /* The size of the small data section.  */
381 static unsigned int g_switch_value = 8;
382 /* Whether the -G option was used.  */
383 static int g_switch_seen = 0;
384
385 #define N_RMASK 0xc4
386 #define N_VFP   0xd4
387
388 /* If we can determine in advance that GP optimization won't be
389    possible, we can skip the relaxation stuff that tries to produce
390    GP-relative references.  This makes delay slot optimization work
391    better.
392
393    This function can only provide a guess, but it seems to work for
394    gcc output.  It needs to guess right for gcc, otherwise gcc
395    will put what it thinks is a GP-relative instruction in a branch
396    delay slot.
397
398    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
399    fixed it for the non-PIC mode.  KR 95/04/07  */
400 static int nopic_need_relax PARAMS ((symbolS *, int));
401
402 /* handle of the OPCODE hash table */
403 static struct hash_control *op_hash = NULL;
404
405 /* The opcode hash table we use for the mips16.  */
406 static struct hash_control *mips16_op_hash = NULL;
407
408 /* This array holds the chars that always start a comment.  If the
409     pre-processor is disabled, these aren't very useful */
410 const char comment_chars[] = "#";
411
412 /* This array holds the chars that only start a comment at the beginning of
413    a line.  If the line seems to have the form '# 123 filename'
414    .line and .file directives will appear in the pre-processed output */
415 /* Note that input_file.c hand checks for '#' at the beginning of the
416    first line of the input file.  This is because the compiler outputs
417    #NO_APP at the beginning of its output.  */
418 /* Also note that C style comments are always supported.  */
419 const char line_comment_chars[] = "#";
420
421 /* This array holds machine specific line separator characters.  */
422 const char line_separator_chars[] = ";";
423
424 /* Chars that can be used to separate mant from exp in floating point nums */
425 const char EXP_CHARS[] = "eE";
426
427 /* Chars that mean this number is a floating point constant */
428 /* As in 0f12.456 */
429 /* or    0d1.2345e12 */
430 const char FLT_CHARS[] = "rRsSfFdDxXpP";
431
432 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
433    changed in read.c .  Ideally it shouldn't have to know about it at all,
434    but nothing is ideal around here.
435  */
436
437 static char *insn_error;
438
439 static int auto_align = 1;
440
441 /* When outputting SVR4 PIC code, the assembler needs to know the
442    offset in the stack frame from which to restore the $gp register.
443    This is set by the .cprestore pseudo-op, and saved in this
444    variable.  */
445 static offsetT mips_cprestore_offset = -1;
446
447 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
448    more optimizations, it can use a register value instead of a memory-saved
449    offset and even an other register than $gp as global pointer.  */
450 static offsetT mips_cpreturn_offset = -1;
451 static int mips_cpreturn_register = -1;
452 static int mips_gp_register = GP;
453 static int mips_gprel_offset = 0;
454
455 /* Whether mips_cprestore_offset has been set in the current function
456    (or whether it has already been warned about, if not).  */
457 static int mips_cprestore_valid = 0;
458
459 /* This is the register which holds the stack frame, as set by the
460    .frame pseudo-op.  This is needed to implement .cprestore.  */
461 static int mips_frame_reg = SP;
462
463 /* Whether mips_frame_reg has been set in the current function
464    (or whether it has already been warned about, if not).  */
465 static int mips_frame_reg_valid = 0;
466
467 /* To output NOP instructions correctly, we need to keep information
468    about the previous two instructions.  */
469
470 /* Whether we are optimizing.  The default value of 2 means to remove
471    unneeded NOPs and swap branch instructions when possible.  A value
472    of 1 means to not swap branches.  A value of 0 means to always
473    insert NOPs.  */
474 static int mips_optimize = 2;
475
476 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
477    equivalent to seeing no -g option at all.  */
478 static int mips_debug = 0;
479
480 /* The previous instruction.  */
481 static struct mips_cl_insn prev_insn;
482
483 /* The instruction before prev_insn.  */
484 static struct mips_cl_insn prev_prev_insn;
485
486 /* If we don't want information for prev_insn or prev_prev_insn, we
487    point the insn_mo field at this dummy integer.  */
488 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
489
490 /* Non-zero if prev_insn is valid.  */
491 static int prev_insn_valid;
492
493 /* The frag for the previous instruction.  */
494 static struct frag *prev_insn_frag;
495
496 /* The offset into prev_insn_frag for the previous instruction.  */
497 static long prev_insn_where;
498
499 /* The reloc type for the previous instruction, if any.  */
500 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
501
502 /* The reloc for the previous instruction, if any.  */
503 static fixS *prev_insn_fixp[3];
504
505 /* Non-zero if the previous instruction was in a delay slot.  */
506 static int prev_insn_is_delay_slot;
507
508 /* Non-zero if the previous instruction was in a .set noreorder.  */
509 static int prev_insn_unreordered;
510
511 /* Non-zero if the previous instruction uses an extend opcode (if
512    mips16).  */
513 static int prev_insn_extended;
514
515 /* Non-zero if the previous previous instruction was in a .set
516    noreorder.  */
517 static int prev_prev_insn_unreordered;
518
519 /* If this is set, it points to a frag holding nop instructions which
520    were inserted before the start of a noreorder section.  If those
521    nops turn out to be unnecessary, the size of the frag can be
522    decreased.  */
523 static fragS *prev_nop_frag;
524
525 /* The number of nop instructions we created in prev_nop_frag.  */
526 static int prev_nop_frag_holds;
527
528 /* The number of nop instructions that we know we need in
529    prev_nop_frag.  */
530 static int prev_nop_frag_required;
531
532 /* The number of instructions we've seen since prev_nop_frag.  */
533 static int prev_nop_frag_since;
534
535 /* For ECOFF and ELF, relocations against symbols are done in two
536    parts, with a HI relocation and a LO relocation.  Each relocation
537    has only 16 bits of space to store an addend.  This means that in
538    order for the linker to handle carries correctly, it must be able
539    to locate both the HI and the LO relocation.  This means that the
540    relocations must appear in order in the relocation table.
541
542    In order to implement this, we keep track of each unmatched HI
543    relocation.  We then sort them so that they immediately precede the
544    corresponding LO relocation.  */
545
546 struct mips_hi_fixup
547 {
548   /* Next HI fixup.  */
549   struct mips_hi_fixup *next;
550   /* This fixup.  */
551   fixS *fixp;
552   /* The section this fixup is in.  */
553   segT seg;
554 };
555
556 /* The list of unmatched HI relocs.  */
557
558 static struct mips_hi_fixup *mips_hi_fixup_list;
559
560 /* The frag containing the last explicit relocation operator.
561    Null if explicit relocations have not been used.  */
562
563 static fragS *prev_reloc_op_frag;
564
565 /* Map normal MIPS register numbers to mips16 register numbers.  */
566
567 #define X ILLEGAL_REG
568 static const int mips32_to_16_reg_map[] =
569 {
570   X, X, 2, 3, 4, 5, 6, 7,
571   X, X, X, X, X, X, X, X,
572   0, 1, X, X, X, X, X, X,
573   X, X, X, X, X, X, X, X
574 };
575 #undef X
576
577 /* Map mips16 register numbers to normal MIPS register numbers.  */
578
579 static const unsigned int mips16_to_32_reg_map[] =
580 {
581   16, 17, 2, 3, 4, 5, 6, 7
582 };
583
584 static int mips_fix_4122_bugs;
585
586 /* We don't relax branches by default, since this causes us to expand
587    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
588    fail to compute the offset before expanding the macro to the most
589    efficient expansion.  */
590
591 static int mips_relax_branch;
592 \f
593 /* Since the MIPS does not have multiple forms of PC relative
594    instructions, we do not have to do relaxing as is done on other
595    platforms.  However, we do have to handle GP relative addressing
596    correctly, which turns out to be a similar problem.
597
598    Every macro that refers to a symbol can occur in (at least) two
599    forms, one with GP relative addressing and one without.  For
600    example, loading a global variable into a register generally uses
601    a macro instruction like this:
602      lw $4,i
603    If i can be addressed off the GP register (this is true if it is in
604    the .sbss or .sdata section, or if it is known to be smaller than
605    the -G argument) this will generate the following instruction:
606      lw $4,i($gp)
607    This instruction will use a GPREL reloc.  If i can not be addressed
608    off the GP register, the following instruction sequence will be used:
609      lui $at,i
610      lw $4,i($at)
611    In this case the first instruction will have a HI16 reloc, and the
612    second reloc will have a LO16 reloc.  Both relocs will be against
613    the symbol i.
614
615    The issue here is that we may not know whether i is GP addressable
616    until after we see the instruction that uses it.  Therefore, we
617    want to be able to choose the final instruction sequence only at
618    the end of the assembly.  This is similar to the way other
619    platforms choose the size of a PC relative instruction only at the
620    end of assembly.
621
622    When generating position independent code we do not use GP
623    addressing in quite the same way, but the issue still arises as
624    external symbols and local symbols must be handled differently.
625
626    We handle these issues by actually generating both possible
627    instruction sequences.  The longer one is put in a frag_var with
628    type rs_machine_dependent.  We encode what to do with the frag in
629    the subtype field.  We encode (1) the number of existing bytes to
630    replace, (2) the number of new bytes to use, (3) the offset from
631    the start of the existing bytes to the first reloc we must generate
632    (that is, the offset is applied from the start of the existing
633    bytes after they are replaced by the new bytes, if any), (4) the
634    offset from the start of the existing bytes to the second reloc,
635    (5) whether a third reloc is needed (the third reloc is always four
636    bytes after the second reloc), and (6) whether to warn if this
637    variant is used (this is sometimes needed if .set nomacro or .set
638    noat is in effect).  All these numbers are reasonably small.
639
640    Generating two instruction sequences must be handled carefully to
641    ensure that delay slots are handled correctly.  Fortunately, there
642    are a limited number of cases.  When the second instruction
643    sequence is generated, append_insn is directed to maintain the
644    existing delay slot information, so it continues to apply to any
645    code after the second instruction sequence.  This means that the
646    second instruction sequence must not impose any requirements not
647    required by the first instruction sequence.
648
649    These variant frags are then handled in functions called by the
650    machine independent code.  md_estimate_size_before_relax returns
651    the final size of the frag.  md_convert_frag sets up the final form
652    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
653    one if needed.  */
654 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
655   ((relax_substateT) \
656    (((old) << 23) \
657     | ((new) << 16) \
658     | (((reloc1) + 64) << 9) \
659     | (((reloc2) + 64) << 2) \
660     | ((reloc3) ? (1 << 1) : 0) \
661     | ((warn) ? 1 : 0)))
662 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
663 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
664 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
665 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
666 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
667 #define RELAX_WARN(i) ((i) & 1)
668
669 /* Branch without likely bit.  If label is out of range, we turn:
670
671         beq reg1, reg2, label
672         delay slot
673
674    into
675
676         bne reg1, reg2, 0f
677         nop
678         j label
679      0: delay slot
680
681    with the following opcode replacements:
682
683         beq <-> bne
684         blez <-> bgtz
685         bltz <-> bgez
686         bc1f <-> bc1t
687
688         bltzal <-> bgezal  (with jal label instead of j label)
689
690    Even though keeping the delay slot instruction in the delay slot of
691    the branch would be more efficient, it would be very tricky to do
692    correctly, because we'd have to introduce a variable frag *after*
693    the delay slot instruction, and expand that instead.  Let's do it
694    the easy way for now, even if the branch-not-taken case now costs
695    one additional instruction.  Out-of-range branches are not supposed
696    to be common, anyway.
697
698    Branch likely.  If label is out of range, we turn:
699
700         beql reg1, reg2, label
701         delay slot (annulled if branch not taken)
702
703    into
704
705         beql reg1, reg2, 1f
706         nop
707         beql $0, $0, 2f
708         nop
709      1: j[al] label
710         delay slot (executed only if branch taken)
711      2:
712
713    It would be possible to generate a shorter sequence by losing the
714    likely bit, generating something like:
715
716         bne reg1, reg2, 0f
717         nop
718         j[al] label
719         delay slot (executed only if branch taken)
720      0:
721
722         beql -> bne
723         bnel -> beq
724         blezl -> bgtz
725         bgtzl -> blez
726         bltzl -> bgez
727         bgezl -> bltz
728         bc1fl -> bc1t
729         bc1tl -> bc1f
730
731         bltzall -> bgezal  (with jal label instead of j label)
732         bgezall -> bltzal  (ditto)
733
734
735    but it's not clear that it would actually improve performance.  */
736 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
737   ((relax_substateT) \
738    (0xc0000000 \
739     | ((toofar) ? 1 : 0) \
740     | ((link) ? 2 : 0) \
741     | ((likely) ? 4 : 0) \
742     | ((uncond) ? 8 : 0)))
743 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
744 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
745 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
746 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
747 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
748
749 /* For mips16 code, we use an entirely different form of relaxation.
750    mips16 supports two versions of most instructions which take
751    immediate values: a small one which takes some small value, and a
752    larger one which takes a 16 bit value.  Since branches also follow
753    this pattern, relaxing these values is required.
754
755    We can assemble both mips16 and normal MIPS code in a single
756    object.  Therefore, we need to support this type of relaxation at
757    the same time that we support the relaxation described above.  We
758    use the high bit of the subtype field to distinguish these cases.
759
760    The information we store for this type of relaxation is the
761    argument code found in the opcode file for this relocation, whether
762    the user explicitly requested a small or extended form, and whether
763    the relocation is in a jump or jal delay slot.  That tells us the
764    size of the value, and how it should be stored.  We also store
765    whether the fragment is considered to be extended or not.  We also
766    store whether this is known to be a branch to a different section,
767    whether we have tried to relax this frag yet, and whether we have
768    ever extended a PC relative fragment because of a shift count.  */
769 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
770   (0x80000000                                                   \
771    | ((type) & 0xff)                                            \
772    | ((small) ? 0x100 : 0)                                      \
773    | ((ext) ? 0x200 : 0)                                        \
774    | ((dslot) ? 0x400 : 0)                                      \
775    | ((jal_dslot) ? 0x800 : 0))
776 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
777 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
778 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
779 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
780 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
781 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
782 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
783 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
784 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
785 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
786 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
787 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
788
789 /* Is the given value a sign-extended 32-bit value?  */
790 #define IS_SEXT_32BIT_NUM(x)                                            \
791   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
792    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
793
794 /* Is the given value a sign-extended 16-bit value?  */
795 #define IS_SEXT_16BIT_NUM(x)                                            \
796   (((x) &~ (offsetT) 0x7fff) == 0                                       \
797    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
798
799 \f
800 /* Prototypes for static functions.  */
801
802 #ifdef __STDC__
803 #define internalError() \
804     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
805 #else
806 #define internalError() as_fatal (_("MIPS internal Error"));
807 #endif
808
809 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
810
811 static inline bfd_boolean reloc_needs_lo_p
812   PARAMS ((bfd_reloc_code_real_type));
813 static inline bfd_boolean fixup_has_matching_lo_p
814   PARAMS ((fixS *));
815 static int insn_uses_reg
816   PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
817            enum mips_regclass class));
818 static int reg_needs_delay
819   PARAMS ((unsigned int));
820 static void mips16_mark_labels
821   PARAMS ((void));
822 static void append_insn
823   PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
824            bfd_reloc_code_real_type *r));
825 static void mips_no_prev_insn
826   PARAMS ((int));
827 static void mips_emit_delays
828   PARAMS ((bfd_boolean));
829 #ifdef USE_STDARG
830 static void macro_build
831   PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
832            const char *fmt, ...));
833 #else
834 static void macro_build ();
835 #endif
836 static void mips16_macro_build
837   PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
838 static void macro_build_jalr
839   PARAMS ((int, expressionS *));
840 static void macro_build_lui
841   PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
842 static void macro_build_ldst_constoffset
843   PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
844            int valreg, int breg));
845 static void set_at
846   PARAMS ((int *counter, int reg, int unsignedp));
847 static void check_absolute_expr
848   PARAMS ((struct mips_cl_insn * ip, expressionS *));
849 static void load_register
850   PARAMS ((int *, int, expressionS *, int));
851 static void load_address
852   PARAMS ((int *, int, expressionS *, int *));
853 static void move_register
854   PARAMS ((int *, int, int));
855 static void macro
856   PARAMS ((struct mips_cl_insn * ip));
857 static void mips16_macro
858   PARAMS ((struct mips_cl_insn * ip));
859 #ifdef LOSING_COMPILER
860 static void macro2
861   PARAMS ((struct mips_cl_insn * ip));
862 #endif
863 static void mips_ip
864   PARAMS ((char *str, struct mips_cl_insn * ip));
865 static void mips16_ip
866   PARAMS ((char *str, struct mips_cl_insn * ip));
867 static void mips16_immed
868   PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
869            bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
870 static bfd_boolean parse_relocation
871   PARAMS ((char **, bfd_reloc_code_real_type *));
872 static size_t my_getSmallExpression
873   PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
874 static void my_getExpression
875   PARAMS ((expressionS *, char *));
876 #ifdef OBJ_ELF
877 static int support_64bit_objects
878   PARAMS((void));
879 #endif
880 static void mips_set_option_string
881   PARAMS ((const char **, const char *));
882 static symbolS *get_symbol
883   PARAMS ((void));
884 static void mips_align
885   PARAMS ((int to, int fill, symbolS *label));
886 static void s_align
887   PARAMS ((int));
888 static void s_change_sec
889   PARAMS ((int));
890 static void s_change_section
891   PARAMS ((int));
892 static void s_cons
893   PARAMS ((int));
894 static void s_float_cons
895   PARAMS ((int));
896 static void s_mips_globl
897   PARAMS ((int));
898 static void s_option
899   PARAMS ((int));
900 static void s_mipsset
901   PARAMS ((int));
902 static void s_abicalls
903   PARAMS ((int));
904 static void s_cpload
905   PARAMS ((int));
906 static void s_cpsetup
907   PARAMS ((int));
908 static void s_cplocal
909   PARAMS ((int));
910 static void s_cprestore
911   PARAMS ((int));
912 static void s_cpreturn
913   PARAMS ((int));
914 static void s_gpvalue
915   PARAMS ((int));
916 static void s_gpword
917   PARAMS ((int));
918 static void s_gpdword
919   PARAMS ((int));
920 static void s_cpadd
921   PARAMS ((int));
922 static void s_insn
923   PARAMS ((int));
924 static void md_obj_begin
925   PARAMS ((void));
926 static void md_obj_end
927   PARAMS ((void));
928 static long get_number
929   PARAMS ((void));
930 static void s_mips_ent
931   PARAMS ((int));
932 static void s_mips_end
933   PARAMS ((int));
934 static void s_mips_frame
935   PARAMS ((int));
936 static void s_mips_mask
937   PARAMS ((int));
938 static void s_mips_stab
939   PARAMS ((int));
940 static void s_mips_weakext
941   PARAMS ((int));
942 static void s_mips_file
943   PARAMS ((int));
944 static void s_mips_loc
945   PARAMS ((int));
946 static bfd_boolean pic_need_relax
947   PARAMS ((symbolS *, asection *));
948 static int mips16_extended_frag
949   PARAMS ((fragS *, asection *, long));
950 static int relaxed_branch_length (fragS *, asection *, int);
951 static int validate_mips_insn
952   PARAMS ((const struct mips_opcode *));
953 static void show
954   PARAMS ((FILE *, const char *, int *, int *));
955 #ifdef OBJ_ELF
956 static int mips_need_elf_addend_fixup
957   PARAMS ((fixS *));
958 #endif
959
960 /* Table and functions used to map between CPU/ISA names, and
961    ISA levels, and CPU numbers.  */
962
963 struct mips_cpu_info
964 {
965   const char *name;           /* CPU or ISA name.  */
966   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
967   int isa;                    /* ISA level.  */
968   int cpu;                    /* CPU number (default CPU if ISA).  */
969 };
970
971 static void mips_set_architecture
972   PARAMS ((const struct mips_cpu_info *));
973 static void mips_set_tune
974   PARAMS ((const struct mips_cpu_info *));
975 static bfd_boolean mips_strict_matching_cpu_name_p
976   PARAMS ((const char *, const char *));
977 static bfd_boolean mips_matching_cpu_name_p
978   PARAMS ((const char *, const char *));
979 static const struct mips_cpu_info *mips_parse_cpu
980   PARAMS ((const char *, const char *));
981 static const struct mips_cpu_info *mips_cpu_info_from_isa
982   PARAMS ((int));
983 \f
984 /* Pseudo-op table.
985
986    The following pseudo-ops from the Kane and Heinrich MIPS book
987    should be defined here, but are currently unsupported: .alias,
988    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
989
990    The following pseudo-ops from the Kane and Heinrich MIPS book are
991    specific to the type of debugging information being generated, and
992    should be defined by the object format: .aent, .begin, .bend,
993    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
994    .vreg.
995
996    The following pseudo-ops from the Kane and Heinrich MIPS book are
997    not MIPS CPU specific, but are also not specific to the object file
998    format.  This file is probably the best place to define them, but
999    they are not currently supported: .asm0, .endr, .lab, .repeat,
1000    .struct.  */
1001
1002 static const pseudo_typeS mips_pseudo_table[] =
1003 {
1004   /* MIPS specific pseudo-ops.  */
1005   {"option", s_option, 0},
1006   {"set", s_mipsset, 0},
1007   {"rdata", s_change_sec, 'r'},
1008   {"sdata", s_change_sec, 's'},
1009   {"livereg", s_ignore, 0},
1010   {"abicalls", s_abicalls, 0},
1011   {"cpload", s_cpload, 0},
1012   {"cpsetup", s_cpsetup, 0},
1013   {"cplocal", s_cplocal, 0},
1014   {"cprestore", s_cprestore, 0},
1015   {"cpreturn", s_cpreturn, 0},
1016   {"gpvalue", s_gpvalue, 0},
1017   {"gpword", s_gpword, 0},
1018   {"gpdword", s_gpdword, 0},
1019   {"cpadd", s_cpadd, 0},
1020   {"insn", s_insn, 0},
1021
1022   /* Relatively generic pseudo-ops that happen to be used on MIPS
1023      chips.  */
1024   {"asciiz", stringer, 1},
1025   {"bss", s_change_sec, 'b'},
1026   {"err", s_err, 0},
1027   {"half", s_cons, 1},
1028   {"dword", s_cons, 3},
1029   {"weakext", s_mips_weakext, 0},
1030
1031   /* These pseudo-ops are defined in read.c, but must be overridden
1032      here for one reason or another.  */
1033   {"align", s_align, 0},
1034   {"byte", s_cons, 0},
1035   {"data", s_change_sec, 'd'},
1036   {"double", s_float_cons, 'd'},
1037   {"float", s_float_cons, 'f'},
1038   {"globl", s_mips_globl, 0},
1039   {"global", s_mips_globl, 0},
1040   {"hword", s_cons, 1},
1041   {"int", s_cons, 2},
1042   {"long", s_cons, 2},
1043   {"octa", s_cons, 4},
1044   {"quad", s_cons, 3},
1045   {"section", s_change_section, 0},
1046   {"short", s_cons, 1},
1047   {"single", s_float_cons, 'f'},
1048   {"stabn", s_mips_stab, 'n'},
1049   {"text", s_change_sec, 't'},
1050   {"word", s_cons, 2},
1051
1052   { "extern", ecoff_directive_extern, 0},
1053
1054   { NULL, NULL, 0 },
1055 };
1056
1057 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1058 {
1059   /* These pseudo-ops should be defined by the object file format.
1060      However, a.out doesn't support them, so we have versions here.  */
1061   {"aent", s_mips_ent, 1},
1062   {"bgnb", s_ignore, 0},
1063   {"end", s_mips_end, 0},
1064   {"endb", s_ignore, 0},
1065   {"ent", s_mips_ent, 0},
1066   {"file", s_mips_file, 0},
1067   {"fmask", s_mips_mask, 'F'},
1068   {"frame", s_mips_frame, 0},
1069   {"loc", s_mips_loc, 0},
1070   {"mask", s_mips_mask, 'R'},
1071   {"verstamp", s_ignore, 0},
1072   { NULL, NULL, 0 },
1073 };
1074
1075 extern void pop_insert PARAMS ((const pseudo_typeS *));
1076
1077 void
1078 mips_pop_insert ()
1079 {
1080   pop_insert (mips_pseudo_table);
1081   if (! ECOFF_DEBUGGING)
1082     pop_insert (mips_nonecoff_pseudo_table);
1083 }
1084 \f
1085 /* Symbols labelling the current insn.  */
1086
1087 struct insn_label_list
1088 {
1089   struct insn_label_list *next;
1090   symbolS *label;
1091 };
1092
1093 static struct insn_label_list *insn_labels;
1094 static struct insn_label_list *free_insn_labels;
1095
1096 static void mips_clear_insn_labels PARAMS ((void));
1097
1098 static inline void
1099 mips_clear_insn_labels ()
1100 {
1101   register struct insn_label_list **pl;
1102
1103   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1104     ;
1105   *pl = insn_labels;
1106   insn_labels = NULL;
1107 }
1108 \f
1109 static char *expr_end;
1110
1111 /* Expressions which appear in instructions.  These are set by
1112    mips_ip.  */
1113
1114 static expressionS imm_expr;
1115 static expressionS offset_expr;
1116
1117 /* Relocs associated with imm_expr and offset_expr.  */
1118
1119 static bfd_reloc_code_real_type imm_reloc[3]
1120   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1121 static bfd_reloc_code_real_type offset_reloc[3]
1122   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1123
1124 /* These are set by mips16_ip if an explicit extension is used.  */
1125
1126 static bfd_boolean mips16_small, mips16_ext;
1127
1128 #ifdef OBJ_ELF
1129 /* The pdr segment for per procedure frame/regmask info.  Not used for
1130    ECOFF debugging.  */
1131
1132 static segT pdr_seg;
1133 #endif
1134
1135 /* The default target format to use.  */
1136
1137 const char *
1138 mips_target_format ()
1139 {
1140   switch (OUTPUT_FLAVOR)
1141     {
1142     case bfd_target_aout_flavour:
1143       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1144     case bfd_target_ecoff_flavour:
1145       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1146     case bfd_target_coff_flavour:
1147       return "pe-mips";
1148     case bfd_target_elf_flavour:
1149 #ifdef TE_TMIPS
1150       /* This is traditional mips.  */
1151       return (target_big_endian
1152               ? (HAVE_64BIT_OBJECTS
1153                  ? "elf64-tradbigmips"
1154                  : (HAVE_NEWABI
1155                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1156               : (HAVE_64BIT_OBJECTS
1157                  ? "elf64-tradlittlemips"
1158                  : (HAVE_NEWABI
1159                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1160 #else
1161       return (target_big_endian
1162               ? (HAVE_64BIT_OBJECTS
1163                  ? "elf64-bigmips"
1164                  : (HAVE_NEWABI
1165                     ? "elf32-nbigmips" : "elf32-bigmips"))
1166               : (HAVE_64BIT_OBJECTS
1167                  ? "elf64-littlemips"
1168                  : (HAVE_NEWABI
1169                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1170 #endif
1171     default:
1172       abort ();
1173       return NULL;
1174     }
1175 }
1176
1177 /* This function is called once, at assembler startup time.  It should
1178    set up all the tables, etc. that the MD part of the assembler will need.  */
1179
1180 void
1181 md_begin ()
1182 {
1183   register const char *retval = NULL;
1184   int i = 0;
1185   int broken = 0;
1186
1187   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1188     as_warn (_("Could not set architecture and machine"));
1189
1190   op_hash = hash_new ();
1191
1192   for (i = 0; i < NUMOPCODES;)
1193     {
1194       const char *name = mips_opcodes[i].name;
1195
1196       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1197       if (retval != NULL)
1198         {
1199           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1200                    mips_opcodes[i].name, retval);
1201           /* Probably a memory allocation problem?  Give up now.  */
1202           as_fatal (_("Broken assembler.  No assembly attempted."));
1203         }
1204       do
1205         {
1206           if (mips_opcodes[i].pinfo != INSN_MACRO)
1207             {
1208               if (!validate_mips_insn (&mips_opcodes[i]))
1209                 broken = 1;
1210             }
1211           ++i;
1212         }
1213       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1214     }
1215
1216   mips16_op_hash = hash_new ();
1217
1218   i = 0;
1219   while (i < bfd_mips16_num_opcodes)
1220     {
1221       const char *name = mips16_opcodes[i].name;
1222
1223       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1224       if (retval != NULL)
1225         as_fatal (_("internal: can't hash `%s': %s"),
1226                   mips16_opcodes[i].name, retval);
1227       do
1228         {
1229           if (mips16_opcodes[i].pinfo != INSN_MACRO
1230               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1231                   != mips16_opcodes[i].match))
1232             {
1233               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1234                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1235               broken = 1;
1236             }
1237           ++i;
1238         }
1239       while (i < bfd_mips16_num_opcodes
1240              && strcmp (mips16_opcodes[i].name, name) == 0);
1241     }
1242
1243   if (broken)
1244     as_fatal (_("Broken assembler.  No assembly attempted."));
1245
1246   /* We add all the general register names to the symbol table.  This
1247      helps us detect invalid uses of them.  */
1248   for (i = 0; i < 32; i++)
1249     {
1250       char buf[5];
1251
1252       sprintf (buf, "$%d", i);
1253       symbol_table_insert (symbol_new (buf, reg_section, i,
1254                                        &zero_address_frag));
1255     }
1256   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1257                                    &zero_address_frag));
1258   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1259                                    &zero_address_frag));
1260   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1261                                    &zero_address_frag));
1262   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1263                                    &zero_address_frag));
1264   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1265                                    &zero_address_frag));
1266   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1267                                    &zero_address_frag));
1268   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1269                                    &zero_address_frag));
1270   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1271                                    &zero_address_frag));
1272   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1273                                    &zero_address_frag));
1274
1275   /* If we don't add these register names to the symbol table, they
1276      may end up being added as regular symbols by operand(), and then
1277      make it to the object file as undefined in case they're not
1278      regarded as local symbols.  They're local in o32, since `$' is a
1279      local symbol prefix, but not in n32 or n64.  */
1280   for (i = 0; i < 8; i++)
1281     {
1282       char buf[6];
1283
1284       sprintf (buf, "$fcc%i", i);
1285       symbol_table_insert (symbol_new (buf, reg_section, -1,
1286                                        &zero_address_frag));
1287     }
1288
1289   mips_no_prev_insn (FALSE);
1290
1291   mips_gprmask = 0;
1292   mips_cprmask[0] = 0;
1293   mips_cprmask[1] = 0;
1294   mips_cprmask[2] = 0;
1295   mips_cprmask[3] = 0;
1296
1297   /* set the default alignment for the text section (2**2) */
1298   record_alignment (text_section, 2);
1299
1300   if (USE_GLOBAL_POINTER_OPT)
1301     bfd_set_gp_size (stdoutput, g_switch_value);
1302
1303   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1304     {
1305       /* On a native system, sections must be aligned to 16 byte
1306          boundaries.  When configured for an embedded ELF target, we
1307          don't bother.  */
1308       if (strcmp (TARGET_OS, "elf") != 0)
1309         {
1310           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1311           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1312           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1313         }
1314
1315       /* Create a .reginfo section for register masks and a .mdebug
1316          section for debugging information.  */
1317       {
1318         segT seg;
1319         subsegT subseg;
1320         flagword flags;
1321         segT sec;
1322
1323         seg = now_seg;
1324         subseg = now_subseg;
1325
1326         /* The ABI says this section should be loaded so that the
1327            running program can access it.  However, we don't load it
1328            if we are configured for an embedded target */
1329         flags = SEC_READONLY | SEC_DATA;
1330         if (strcmp (TARGET_OS, "elf") != 0)
1331           flags |= SEC_ALLOC | SEC_LOAD;
1332
1333         if (mips_abi != N64_ABI)
1334           {
1335             sec = subseg_new (".reginfo", (subsegT) 0);
1336
1337             bfd_set_section_flags (stdoutput, sec, flags);
1338             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1339
1340 #ifdef OBJ_ELF
1341             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1342 #endif
1343           }
1344         else
1345           {
1346             /* The 64-bit ABI uses a .MIPS.options section rather than
1347                .reginfo section.  */
1348             sec = subseg_new (".MIPS.options", (subsegT) 0);
1349             bfd_set_section_flags (stdoutput, sec, flags);
1350             bfd_set_section_alignment (stdoutput, sec, 3);
1351
1352 #ifdef OBJ_ELF
1353             /* Set up the option header.  */
1354             {
1355               Elf_Internal_Options opthdr;
1356               char *f;
1357
1358               opthdr.kind = ODK_REGINFO;
1359               opthdr.size = (sizeof (Elf_External_Options)
1360                              + sizeof (Elf64_External_RegInfo));
1361               opthdr.section = 0;
1362               opthdr.info = 0;
1363               f = frag_more (sizeof (Elf_External_Options));
1364               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1365                                              (Elf_External_Options *) f);
1366
1367               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1368             }
1369 #endif
1370           }
1371
1372         if (ECOFF_DEBUGGING)
1373           {
1374             sec = subseg_new (".mdebug", (subsegT) 0);
1375             (void) bfd_set_section_flags (stdoutput, sec,
1376                                           SEC_HAS_CONTENTS | SEC_READONLY);
1377             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1378           }
1379 #ifdef OBJ_ELF
1380         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1381           {
1382             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1383             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1384                                           SEC_READONLY | SEC_RELOC
1385                                           | SEC_DEBUGGING);
1386             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1387           }
1388 #endif
1389
1390         subseg_set (seg, subseg);
1391       }
1392     }
1393
1394   if (! ECOFF_DEBUGGING)
1395     md_obj_begin ();
1396 }
1397
1398 void
1399 md_mips_end ()
1400 {
1401   if (! ECOFF_DEBUGGING)
1402     md_obj_end ();
1403 }
1404
1405 void
1406 md_assemble (str)
1407      char *str;
1408 {
1409   struct mips_cl_insn insn;
1410   bfd_reloc_code_real_type unused_reloc[3]
1411     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1412
1413   imm_expr.X_op = O_absent;
1414   offset_expr.X_op = O_absent;
1415   imm_reloc[0] = BFD_RELOC_UNUSED;
1416   imm_reloc[1] = BFD_RELOC_UNUSED;
1417   imm_reloc[2] = BFD_RELOC_UNUSED;
1418   offset_reloc[0] = BFD_RELOC_UNUSED;
1419   offset_reloc[1] = BFD_RELOC_UNUSED;
1420   offset_reloc[2] = BFD_RELOC_UNUSED;
1421
1422   if (mips_opts.mips16)
1423     mips16_ip (str, &insn);
1424   else
1425     {
1426       mips_ip (str, &insn);
1427       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1428             str, insn.insn_opcode));
1429     }
1430
1431   if (insn_error)
1432     {
1433       as_bad ("%s `%s'", insn_error, str);
1434       return;
1435     }
1436
1437   if (insn.insn_mo->pinfo == INSN_MACRO)
1438     {
1439       if (mips_opts.mips16)
1440         mips16_macro (&insn);
1441       else
1442         macro (&insn);
1443     }
1444   else
1445     {
1446       if (imm_expr.X_op != O_absent)
1447         append_insn (NULL, &insn, &imm_expr, imm_reloc);
1448       else if (offset_expr.X_op != O_absent)
1449         append_insn (NULL, &insn, &offset_expr, offset_reloc);
1450       else
1451         append_insn (NULL, &insn, NULL, unused_reloc);
1452     }
1453 }
1454
1455 /* Return true if the given relocation might need a matching %lo().
1456    Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1457    applied to local symbols.  */
1458
1459 static inline bfd_boolean
1460 reloc_needs_lo_p (reloc)
1461      bfd_reloc_code_real_type reloc;
1462 {
1463   return (reloc == BFD_RELOC_HI16_S
1464           || reloc == BFD_RELOC_MIPS_GOT16);
1465 }
1466
1467 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1468    relocation.  */
1469
1470 static inline bfd_boolean
1471 fixup_has_matching_lo_p (fixp)
1472      fixS *fixp;
1473 {
1474   return (fixp->fx_next != NULL
1475           && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1476           && fixp->fx_addsy == fixp->fx_next->fx_addsy
1477           && fixp->fx_offset == fixp->fx_next->fx_offset);
1478 }
1479
1480 /* See whether instruction IP reads register REG.  CLASS is the type
1481    of register.  */
1482
1483 static int
1484 insn_uses_reg (ip, reg, class)
1485      struct mips_cl_insn *ip;
1486      unsigned int reg;
1487      enum mips_regclass class;
1488 {
1489   if (class == MIPS16_REG)
1490     {
1491       assert (mips_opts.mips16);
1492       reg = mips16_to_32_reg_map[reg];
1493       class = MIPS_GR_REG;
1494     }
1495
1496   /* Don't report on general register ZERO, since it never changes.  */
1497   if (class == MIPS_GR_REG && reg == ZERO)
1498     return 0;
1499
1500   if (class == MIPS_FP_REG)
1501     {
1502       assert (! mips_opts.mips16);
1503       /* If we are called with either $f0 or $f1, we must check $f0.
1504          This is not optimal, because it will introduce an unnecessary
1505          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1506          need to distinguish reading both $f0 and $f1 or just one of
1507          them.  Note that we don't have to check the other way,
1508          because there is no instruction that sets both $f0 and $f1
1509          and requires a delay.  */
1510       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1511           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1512               == (reg &~ (unsigned) 1)))
1513         return 1;
1514       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1515           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1516               == (reg &~ (unsigned) 1)))
1517         return 1;
1518     }
1519   else if (! mips_opts.mips16)
1520     {
1521       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1522           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1523         return 1;
1524       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1525           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1526         return 1;
1527     }
1528   else
1529     {
1530       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1531           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1532                                     & MIPS16OP_MASK_RX)]
1533               == reg))
1534         return 1;
1535       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1536           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1537                                     & MIPS16OP_MASK_RY)]
1538               == reg))
1539         return 1;
1540       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1541           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1542                                     & MIPS16OP_MASK_MOVE32Z)]
1543               == reg))
1544         return 1;
1545       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1546         return 1;
1547       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1548         return 1;
1549       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1550         return 1;
1551       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1552           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1553               & MIPS16OP_MASK_REGR32) == reg)
1554         return 1;
1555     }
1556
1557   return 0;
1558 }
1559
1560 /* This function returns true if modifying a register requires a
1561    delay.  */
1562
1563 static int
1564 reg_needs_delay (reg)
1565      unsigned int reg;
1566 {
1567   unsigned long prev_pinfo;
1568
1569   prev_pinfo = prev_insn.insn_mo->pinfo;
1570   if (! mips_opts.noreorder
1571       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1572       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1573           || (! gpr_interlocks
1574               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1575     {
1576       /* A load from a coprocessor or from memory.  All load
1577          delays delay the use of general register rt for one
1578          instruction on the r3000.  The r6000 and r4000 use
1579          interlocks.  */
1580       /* Itbl support may require additional care here.  */
1581       know (prev_pinfo & INSN_WRITE_GPR_T);
1582       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1583         return 1;
1584     }
1585
1586   return 0;
1587 }
1588
1589 /* Mark instruction labels in mips16 mode.  This permits the linker to
1590    handle them specially, such as generating jalx instructions when
1591    needed.  We also make them odd for the duration of the assembly, in
1592    order to generate the right sort of code.  We will make them even
1593    in the adjust_symtab routine, while leaving them marked.  This is
1594    convenient for the debugger and the disassembler.  The linker knows
1595    to make them odd again.  */
1596
1597 static void
1598 mips16_mark_labels ()
1599 {
1600   if (mips_opts.mips16)
1601     {
1602       struct insn_label_list *l;
1603       valueT val;
1604
1605       for (l = insn_labels; l != NULL; l = l->next)
1606         {
1607 #ifdef OBJ_ELF
1608           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1609             S_SET_OTHER (l->label, STO_MIPS16);
1610 #endif
1611           val = S_GET_VALUE (l->label);
1612           if ((val & 1) == 0)
1613             S_SET_VALUE (l->label, val + 1);
1614         }
1615     }
1616 }
1617
1618 /* Output an instruction.  PLACE is where to put the instruction; if
1619    it is NULL, this uses frag_more to get room.  IP is the instruction
1620    information.  ADDRESS_EXPR is an operand of the instruction to be
1621    used with RELOC_TYPE.  */
1622
1623 static void
1624 append_insn (place, ip, address_expr, reloc_type)
1625      char *place;
1626      struct mips_cl_insn *ip;
1627      expressionS *address_expr;
1628      bfd_reloc_code_real_type *reloc_type;
1629 {
1630   register unsigned long prev_pinfo, pinfo;
1631   char *f;
1632   fixS *fixp[3];
1633   int nops = 0;
1634
1635   /* Mark instruction labels in mips16 mode.  */
1636   mips16_mark_labels ();
1637
1638   prev_pinfo = prev_insn.insn_mo->pinfo;
1639   pinfo = ip->insn_mo->pinfo;
1640
1641   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1642     {
1643       int prev_prev_nop;
1644
1645       /* If the previous insn required any delay slots, see if we need
1646          to insert a NOP or two.  There are eight kinds of possible
1647          hazards, of which an instruction can have at most one type.
1648          (1) a load from memory delay
1649          (2) a load from a coprocessor delay
1650          (3) an unconditional branch delay
1651          (4) a conditional branch delay
1652          (5) a move to coprocessor register delay
1653          (6) a load coprocessor register from memory delay
1654          (7) a coprocessor condition code delay
1655          (8) a HI/LO special register delay
1656
1657          There are a lot of optimizations we could do that we don't.
1658          In particular, we do not, in general, reorder instructions.
1659          If you use gcc with optimization, it will reorder
1660          instructions and generally do much more optimization then we
1661          do here; repeating all that work in the assembler would only
1662          benefit hand written assembly code, and does not seem worth
1663          it.  */
1664
1665       /* This is how a NOP is emitted.  */
1666 #define emit_nop()                                      \
1667   (mips_opts.mips16                                     \
1668    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1669    : md_number_to_chars (frag_more (4), 0, 4))
1670
1671       /* The previous insn might require a delay slot, depending upon
1672          the contents of the current insn.  */
1673       if (! mips_opts.mips16
1674           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1675           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1676                && ! cop_interlocks)
1677               || (! gpr_interlocks
1678                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1679         {
1680           /* A load from a coprocessor or from memory.  All load
1681              delays delay the use of general register rt for one
1682              instruction on the r3000.  The r6000 and r4000 use
1683              interlocks.  */
1684           /* Itbl support may require additional care here.  */
1685           know (prev_pinfo & INSN_WRITE_GPR_T);
1686           if (mips_optimize == 0
1687               || insn_uses_reg (ip,
1688                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1689                                  & OP_MASK_RT),
1690                                 MIPS_GR_REG))
1691             ++nops;
1692         }
1693       else if (! mips_opts.mips16
1694                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1695                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1696                     && ! cop_interlocks)
1697                    || (mips_opts.isa == ISA_MIPS1
1698                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1699         {
1700           /* A generic coprocessor delay.  The previous instruction
1701              modified a coprocessor general or control register.  If
1702              it modified a control register, we need to avoid any
1703              coprocessor instruction (this is probably not always
1704              required, but it sometimes is).  If it modified a general
1705              register, we avoid using that register.
1706
1707              On the r6000 and r4000 loading a coprocessor register
1708              from memory is interlocked, and does not require a delay.
1709
1710              This case is not handled very well.  There is no special
1711              knowledge of CP0 handling, and the coprocessors other
1712              than the floating point unit are not distinguished at
1713              all.  */
1714           /* Itbl support may require additional care here. FIXME!
1715              Need to modify this to include knowledge about
1716              user specified delays!  */
1717           if (prev_pinfo & INSN_WRITE_FPR_T)
1718             {
1719               if (mips_optimize == 0
1720                   || insn_uses_reg (ip,
1721                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1722                                      & OP_MASK_FT),
1723                                     MIPS_FP_REG))
1724                 ++nops;
1725             }
1726           else if (prev_pinfo & INSN_WRITE_FPR_S)
1727             {
1728               if (mips_optimize == 0
1729                   || insn_uses_reg (ip,
1730                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1731                                      & OP_MASK_FS),
1732                                     MIPS_FP_REG))
1733                 ++nops;
1734             }
1735           else
1736             {
1737               /* We don't know exactly what the previous instruction
1738                  does.  If the current instruction uses a coprocessor
1739                  register, we must insert a NOP.  If previous
1740                  instruction may set the condition codes, and the
1741                  current instruction uses them, we must insert two
1742                  NOPS.  */
1743               /* Itbl support may require additional care here.  */
1744               if (mips_optimize == 0
1745                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1746                       && (pinfo & INSN_READ_COND_CODE)))
1747                 nops += 2;
1748               else if (pinfo & INSN_COP)
1749                 ++nops;
1750             }
1751         }
1752       else if (! mips_opts.mips16
1753                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1754                && (prev_pinfo & INSN_WRITE_COND_CODE)
1755                && ! cop_interlocks)
1756         {
1757           /* The previous instruction sets the coprocessor condition
1758              codes, but does not require a general coprocessor delay
1759              (this means it is a floating point comparison
1760              instruction).  If this instruction uses the condition
1761              codes, we need to insert a single NOP.  */
1762           /* Itbl support may require additional care here.  */
1763           if (mips_optimize == 0
1764               || (pinfo & INSN_READ_COND_CODE))
1765             ++nops;
1766         }
1767
1768       /* If we're fixing up mfhi/mflo for the r7000 and the
1769          previous insn was an mfhi/mflo and the current insn
1770          reads the register that the mfhi/mflo wrote to, then
1771          insert two nops.  */
1772
1773       else if (mips_7000_hilo_fix
1774                && MF_HILO_INSN (prev_pinfo)
1775                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1776                                       & OP_MASK_RD),
1777                                  MIPS_GR_REG))
1778         {
1779           nops += 2;
1780         }
1781
1782       /* If we're fixing up mfhi/mflo for the r7000 and the
1783          2nd previous insn was an mfhi/mflo and the current insn
1784          reads the register that the mfhi/mflo wrote to, then
1785          insert one nop.  */
1786
1787       else if (mips_7000_hilo_fix
1788                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1789                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1790                                        & OP_MASK_RD),
1791                                     MIPS_GR_REG))
1792
1793         {
1794           ++nops;
1795         }
1796
1797       else if (prev_pinfo & INSN_READ_LO)
1798         {
1799           /* The previous instruction reads the LO register; if the
1800              current instruction writes to the LO register, we must
1801              insert two NOPS.  Some newer processors have interlocks.
1802              Also the tx39's multiply instructions can be exectuted
1803              immediatly after a read from HI/LO (without the delay),
1804              though the tx39's divide insns still do require the
1805              delay.  */
1806           if (! (hilo_interlocks
1807                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1808               && (mips_optimize == 0
1809                   || (pinfo & INSN_WRITE_LO)))
1810             nops += 2;
1811           /* Most mips16 branch insns don't have a delay slot.
1812              If a read from LO is immediately followed by a branch
1813              to a write to LO we have a read followed by a write
1814              less than 2 insns away.  We assume the target of
1815              a branch might be a write to LO, and insert a nop
1816              between a read and an immediately following branch.  */
1817           else if (mips_opts.mips16
1818                    && (mips_optimize == 0
1819                        || (pinfo & MIPS16_INSN_BRANCH)))
1820             ++nops;
1821         }
1822       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1823         {
1824           /* The previous instruction reads the HI register; if the
1825              current instruction writes to the HI register, we must
1826              insert a NOP.  Some newer processors have interlocks.
1827              Also the note tx39's multiply above.  */
1828           if (! (hilo_interlocks
1829                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1830               && (mips_optimize == 0
1831                   || (pinfo & INSN_WRITE_HI)))
1832             nops += 2;
1833           /* Most mips16 branch insns don't have a delay slot.
1834              If a read from HI is immediately followed by a branch
1835              to a write to HI we have a read followed by a write
1836              less than 2 insns away.  We assume the target of
1837              a branch might be a write to HI, and insert a nop
1838              between a read and an immediately following branch.  */
1839           else if (mips_opts.mips16
1840                    && (mips_optimize == 0
1841                        || (pinfo & MIPS16_INSN_BRANCH)))
1842             ++nops;
1843         }
1844
1845       /* If the previous instruction was in a noreorder section, then
1846          we don't want to insert the nop after all.  */
1847       /* Itbl support may require additional care here.  */
1848       if (prev_insn_unreordered)
1849         nops = 0;
1850
1851       /* There are two cases which require two intervening
1852          instructions: 1) setting the condition codes using a move to
1853          coprocessor instruction which requires a general coprocessor
1854          delay and then reading the condition codes 2) reading the HI
1855          or LO register and then writing to it (except on processors
1856          which have interlocks).  If we are not already emitting a NOP
1857          instruction, we must check for these cases compared to the
1858          instruction previous to the previous instruction.  */
1859       if ((! mips_opts.mips16
1860            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1861            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1862            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1863            && (pinfo & INSN_READ_COND_CODE)
1864            && ! cop_interlocks)
1865           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1866               && (pinfo & INSN_WRITE_LO)
1867               && ! (hilo_interlocks
1868                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1869           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1870               && (pinfo & INSN_WRITE_HI)
1871               && ! (hilo_interlocks
1872                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1873         prev_prev_nop = 1;
1874       else
1875         prev_prev_nop = 0;
1876
1877       if (prev_prev_insn_unreordered)
1878         prev_prev_nop = 0;
1879
1880       if (prev_prev_nop && nops == 0)
1881         ++nops;
1882
1883       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1884         {
1885           /* We're out of bits in pinfo, so we must resort to string
1886              ops here.  Shortcuts are selected based on opcodes being
1887              limited to the VR4122 instruction set.  */
1888           int min_nops = 0;
1889           const char *pn = prev_insn.insn_mo->name;
1890           const char *tn = ip->insn_mo->name;
1891           if (strncmp(pn, "macc", 4) == 0
1892               || strncmp(pn, "dmacc", 5) == 0)
1893             {
1894               /* Errata 21 - [D]DIV[U] after [D]MACC */
1895               if (strstr (tn, "div"))
1896                 {
1897                   min_nops = 1;
1898                 }
1899
1900               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1901               if (pn[0] == 'd' /* dmacc */
1902                   && (strncmp(tn, "dmult", 5) == 0
1903                       || strncmp(tn, "dmacc", 5) == 0))
1904                 {
1905                   min_nops = 1;
1906                 }
1907
1908               /* Errata 24 - MT{LO,HI} after [D]MACC */
1909               if (strcmp (tn, "mtlo") == 0
1910                   || strcmp (tn, "mthi") == 0)
1911                 {
1912                   min_nops = 1;
1913                 }
1914
1915             }
1916           else if (strncmp(pn, "dmult", 5) == 0
1917                    && (strncmp(tn, "dmult", 5) == 0
1918                        || strncmp(tn, "dmacc", 5) == 0))
1919             {
1920               /* Here is the rest of errata 23.  */
1921               min_nops = 1;
1922             }
1923           if (nops < min_nops)
1924             nops = min_nops;
1925         }
1926
1927       /* If we are being given a nop instruction, don't bother with
1928          one of the nops we would otherwise output.  This will only
1929          happen when a nop instruction is used with mips_optimize set
1930          to 0.  */
1931       if (nops > 0
1932           && ! mips_opts.noreorder
1933           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1934         --nops;
1935
1936       /* Now emit the right number of NOP instructions.  */
1937       if (nops > 0 && ! mips_opts.noreorder)
1938         {
1939           fragS *old_frag;
1940           unsigned long old_frag_offset;
1941           int i;
1942           struct insn_label_list *l;
1943
1944           old_frag = frag_now;
1945           old_frag_offset = frag_now_fix ();
1946
1947           for (i = 0; i < nops; i++)
1948             emit_nop ();
1949
1950           if (listing)
1951             {
1952               listing_prev_line ();
1953               /* We may be at the start of a variant frag.  In case we
1954                  are, make sure there is enough space for the frag
1955                  after the frags created by listing_prev_line.  The
1956                  argument to frag_grow here must be at least as large
1957                  as the argument to all other calls to frag_grow in
1958                  this file.  We don't have to worry about being in the
1959                  middle of a variant frag, because the variants insert
1960                  all needed nop instructions themselves.  */
1961               frag_grow (40);
1962             }
1963
1964           for (l = insn_labels; l != NULL; l = l->next)
1965             {
1966               valueT val;
1967
1968               assert (S_GET_SEGMENT (l->label) == now_seg);
1969               symbol_set_frag (l->label, frag_now);
1970               val = (valueT) frag_now_fix ();
1971               /* mips16 text labels are stored as odd.  */
1972               if (mips_opts.mips16)
1973                 ++val;
1974               S_SET_VALUE (l->label, val);
1975             }
1976
1977 #ifndef NO_ECOFF_DEBUGGING
1978           if (ECOFF_DEBUGGING)
1979             ecoff_fix_loc (old_frag, old_frag_offset);
1980 #endif
1981         }
1982       else if (prev_nop_frag != NULL)
1983         {
1984           /* We have a frag holding nops we may be able to remove.  If
1985              we don't need any nops, we can decrease the size of
1986              prev_nop_frag by the size of one instruction.  If we do
1987              need some nops, we count them in prev_nops_required.  */
1988           if (prev_nop_frag_since == 0)
1989             {
1990               if (nops == 0)
1991                 {
1992                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1993                   --prev_nop_frag_holds;
1994                 }
1995               else
1996                 prev_nop_frag_required += nops;
1997             }
1998           else
1999             {
2000               if (prev_prev_nop == 0)
2001                 {
2002                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2003                   --prev_nop_frag_holds;
2004                 }
2005               else
2006                 ++prev_nop_frag_required;
2007             }
2008
2009           if (prev_nop_frag_holds <= prev_nop_frag_required)
2010             prev_nop_frag = NULL;
2011
2012           ++prev_nop_frag_since;
2013
2014           /* Sanity check: by the time we reach the second instruction
2015              after prev_nop_frag, we should have used up all the nops
2016              one way or another.  */
2017           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2018         }
2019     }
2020
2021   if (place == NULL
2022       && address_expr
2023       && *reloc_type == BFD_RELOC_16_PCREL_S2
2024       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2025           || pinfo & INSN_COND_BRANCH_LIKELY)
2026       && mips_relax_branch
2027       /* Don't try branch relaxation within .set nomacro, or within
2028          .set noat if we use $at for PIC computations.  If it turns
2029          out that the branch was out-of-range, we'll get an error.  */
2030       && !mips_opts.warn_about_macros
2031       && !(mips_opts.noat && mips_pic != NO_PIC)
2032       && !mips_opts.mips16)
2033     {
2034       f = frag_var (rs_machine_dependent,
2035                     relaxed_branch_length
2036                     (NULL, NULL,
2037                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2038                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2039                     RELAX_BRANCH_ENCODE
2040                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
2041                      pinfo & INSN_COND_BRANCH_LIKELY,
2042                      pinfo & INSN_WRITE_GPR_31,
2043                      0),
2044                     address_expr->X_add_symbol,
2045                     address_expr->X_add_number,
2046                     0);
2047       *reloc_type = BFD_RELOC_UNUSED;
2048     }
2049   else if (*reloc_type > BFD_RELOC_UNUSED)
2050     {
2051       /* We need to set up a variant frag.  */
2052       assert (mips_opts.mips16 && address_expr != NULL);
2053       f = frag_var (rs_machine_dependent, 4, 0,
2054                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2055                                          mips16_small, mips16_ext,
2056                                          (prev_pinfo
2057                                           & INSN_UNCOND_BRANCH_DELAY),
2058                                          (*prev_insn_reloc_type
2059                                           == BFD_RELOC_MIPS16_JMP)),
2060                     make_expr_symbol (address_expr), 0, NULL);
2061     }
2062   else if (place != NULL)
2063     f = place;
2064   else if (mips_opts.mips16
2065            && ! ip->use_extend
2066            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2067     {
2068       /* Make sure there is enough room to swap this instruction with
2069          a following jump instruction.  */
2070       frag_grow (6);
2071       f = frag_more (2);
2072     }
2073   else
2074     {
2075       if (mips_opts.mips16
2076           && mips_opts.noreorder
2077           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2078         as_warn (_("extended instruction in delay slot"));
2079
2080       f = frag_more (4);
2081     }
2082
2083   fixp[0] = fixp[1] = fixp[2] = NULL;
2084   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2085     {
2086       if (address_expr->X_op == O_constant)
2087         {
2088           valueT tmp;
2089
2090           switch (*reloc_type)
2091             {
2092             case BFD_RELOC_32:
2093               ip->insn_opcode |= address_expr->X_add_number;
2094               break;
2095
2096             case BFD_RELOC_MIPS_HIGHEST:
2097               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2098               tmp >>= 16;
2099               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2100               break;
2101
2102             case BFD_RELOC_MIPS_HIGHER:
2103               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2104               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2105               break;
2106
2107             case BFD_RELOC_HI16_S:
2108               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2109                                   >> 16) & 0xffff;
2110               break;
2111
2112             case BFD_RELOC_HI16:
2113               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2114               break;
2115
2116             case BFD_RELOC_LO16:
2117             case BFD_RELOC_MIPS_GOT_DISP:
2118               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2119               break;
2120
2121             case BFD_RELOC_MIPS_JMP:
2122               if ((address_expr->X_add_number & 3) != 0)
2123                 as_bad (_("jump to misaligned address (0x%lx)"),
2124                         (unsigned long) address_expr->X_add_number);
2125               if (address_expr->X_add_number & ~0xfffffff)
2126                 as_bad (_("jump address range overflow (0x%lx)"),
2127                         (unsigned long) address_expr->X_add_number);
2128               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2129               break;
2130
2131             case BFD_RELOC_MIPS16_JMP:
2132               if ((address_expr->X_add_number & 3) != 0)
2133                 as_bad (_("jump to misaligned address (0x%lx)"),
2134                         (unsigned long) address_expr->X_add_number);
2135               if (address_expr->X_add_number & ~0xfffffff)
2136                 as_bad (_("jump address range overflow (0x%lx)"),
2137                         (unsigned long) address_expr->X_add_number);
2138               ip->insn_opcode |=
2139                 (((address_expr->X_add_number & 0x7c0000) << 3)
2140                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2141                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2142               break;
2143
2144             case BFD_RELOC_16_PCREL_S2:
2145               goto need_reloc;
2146
2147             default:
2148               internalError ();
2149             }
2150         }
2151       else
2152         {
2153         need_reloc:
2154           /* Don't generate a reloc if we are writing into a variant frag.  */
2155           if (place == NULL)
2156             {
2157               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2158                                      address_expr,
2159                                      *reloc_type == BFD_RELOC_16_PCREL_S2,
2160                                      reloc_type[0]);
2161
2162               /* These relocations can have an addend that won't fit in
2163                  4 octets for 64bit assembly.  */
2164               if (HAVE_64BIT_GPRS &&
2165                   (*reloc_type == BFD_RELOC_16
2166                    || *reloc_type == BFD_RELOC_32
2167                    || *reloc_type == BFD_RELOC_MIPS_JMP
2168                    || *reloc_type == BFD_RELOC_HI16_S
2169                    || *reloc_type == BFD_RELOC_LO16
2170                    || *reloc_type == BFD_RELOC_GPREL16
2171                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2172                    || *reloc_type == BFD_RELOC_GPREL32
2173                    || *reloc_type == BFD_RELOC_64
2174                    || *reloc_type == BFD_RELOC_CTOR
2175                    || *reloc_type == BFD_RELOC_MIPS_SUB
2176                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2177                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2178                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2179                    || *reloc_type == BFD_RELOC_MIPS_REL16
2180                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2181                 fixp[0]->fx_no_overflow = 1;
2182
2183               if (reloc_needs_lo_p (*reloc_type))
2184                 {
2185                   struct mips_hi_fixup *hi_fixup;
2186
2187                   /* Reuse the last entry if it already has a matching %lo.  */
2188                   hi_fixup = mips_hi_fixup_list;
2189                   if (hi_fixup == 0
2190                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2191                     {
2192                       hi_fixup = ((struct mips_hi_fixup *)
2193                                   xmalloc (sizeof (struct mips_hi_fixup)));
2194                       hi_fixup->next = mips_hi_fixup_list;
2195                       mips_hi_fixup_list = hi_fixup;
2196                     }
2197                   hi_fixup->fixp = fixp[0];
2198                   hi_fixup->seg = now_seg;
2199                 }
2200
2201               if (reloc_type[1] != BFD_RELOC_UNUSED)
2202                 {
2203                   /* FIXME: This symbol can be one of
2204                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2205                   address_expr->X_op = O_absent;
2206                   address_expr->X_add_symbol = 0;
2207                   address_expr->X_add_number = 0;
2208
2209                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2210                                          4, address_expr, FALSE,
2211                                          reloc_type[1]);
2212
2213                   /* These relocations can have an addend that won't fit in
2214                      4 octets for 64bit assembly.  */
2215                   if (HAVE_64BIT_GPRS &&
2216                       (*reloc_type == BFD_RELOC_16
2217                        || *reloc_type == BFD_RELOC_32
2218                        || *reloc_type == BFD_RELOC_MIPS_JMP
2219                        || *reloc_type == BFD_RELOC_HI16_S
2220                        || *reloc_type == BFD_RELOC_LO16
2221                        || *reloc_type == BFD_RELOC_GPREL16
2222                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2223                        || *reloc_type == BFD_RELOC_GPREL32
2224                        || *reloc_type == BFD_RELOC_64
2225                        || *reloc_type == BFD_RELOC_CTOR
2226                        || *reloc_type == BFD_RELOC_MIPS_SUB
2227                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2228                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2229                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2230                        || *reloc_type == BFD_RELOC_MIPS_REL16
2231                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2232                     fixp[1]->fx_no_overflow = 1;
2233
2234                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2235                     {
2236                       address_expr->X_op = O_absent;
2237                       address_expr->X_add_symbol = 0;
2238                       address_expr->X_add_number = 0;
2239
2240                       fixp[2] = fix_new_exp (frag_now,
2241                                              f - frag_now->fr_literal, 4,
2242                                              address_expr, FALSE,
2243                                              reloc_type[2]);
2244
2245                       /* These relocations can have an addend that won't fit in
2246                          4 octets for 64bit assembly.  */
2247                       if (HAVE_64BIT_GPRS &&
2248                           (*reloc_type == BFD_RELOC_16
2249                            || *reloc_type == BFD_RELOC_32
2250                            || *reloc_type == BFD_RELOC_MIPS_JMP
2251                            || *reloc_type == BFD_RELOC_HI16_S
2252                            || *reloc_type == BFD_RELOC_LO16
2253                            || *reloc_type == BFD_RELOC_GPREL16
2254                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2255                            || *reloc_type == BFD_RELOC_GPREL32
2256                            || *reloc_type == BFD_RELOC_64
2257                            || *reloc_type == BFD_RELOC_CTOR
2258                            || *reloc_type == BFD_RELOC_MIPS_SUB
2259                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2260                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2261                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2262                            || *reloc_type == BFD_RELOC_MIPS_REL16
2263                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2264                         fixp[2]->fx_no_overflow = 1;
2265                     }
2266                 }
2267             }
2268         }
2269     }
2270
2271   if (! mips_opts.mips16)
2272     {
2273       md_number_to_chars (f, ip->insn_opcode, 4);
2274 #ifdef OBJ_ELF
2275       dwarf2_emit_insn (4);
2276 #endif
2277     }
2278   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2279     {
2280       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2281       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2282 #ifdef OBJ_ELF
2283       dwarf2_emit_insn (4);
2284 #endif
2285     }
2286   else
2287     {
2288       if (ip->use_extend)
2289         {
2290           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2291           f += 2;
2292         }
2293       md_number_to_chars (f, ip->insn_opcode, 2);
2294 #ifdef OBJ_ELF
2295       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2296 #endif
2297     }
2298
2299   /* Update the register mask information.  */
2300   if (! mips_opts.mips16)
2301     {
2302       if (pinfo & INSN_WRITE_GPR_D)
2303         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2304       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2305         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2306       if (pinfo & INSN_READ_GPR_S)
2307         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2308       if (pinfo & INSN_WRITE_GPR_31)
2309         mips_gprmask |= 1 << RA;
2310       if (pinfo & INSN_WRITE_FPR_D)
2311         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2312       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2313         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2314       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2315         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2316       if ((pinfo & INSN_READ_FPR_R) != 0)
2317         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2318       if (pinfo & INSN_COP)
2319         {
2320           /* We don't keep enough information to sort these cases out.
2321              The itbl support does keep this information however, although
2322              we currently don't support itbl fprmats as part of the cop
2323              instruction.  May want to add this support in the future.  */
2324         }
2325       /* Never set the bit for $0, which is always zero.  */
2326       mips_gprmask &= ~1 << 0;
2327     }
2328   else
2329     {
2330       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2331         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2332                               & MIPS16OP_MASK_RX);
2333       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2334         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2335                               & MIPS16OP_MASK_RY);
2336       if (pinfo & MIPS16_INSN_WRITE_Z)
2337         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2338                               & MIPS16OP_MASK_RZ);
2339       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2340         mips_gprmask |= 1 << TREG;
2341       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2342         mips_gprmask |= 1 << SP;
2343       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2344         mips_gprmask |= 1 << RA;
2345       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2346         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2347       if (pinfo & MIPS16_INSN_READ_Z)
2348         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2349                               & MIPS16OP_MASK_MOVE32Z);
2350       if (pinfo & MIPS16_INSN_READ_GPR_X)
2351         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2352                               & MIPS16OP_MASK_REGR32);
2353     }
2354
2355   if (place == NULL && ! mips_opts.noreorder)
2356     {
2357       /* Filling the branch delay slot is more complex.  We try to
2358          switch the branch with the previous instruction, which we can
2359          do if the previous instruction does not set up a condition
2360          that the branch tests and if the branch is not itself the
2361          target of any branch.  */
2362       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2363           || (pinfo & INSN_COND_BRANCH_DELAY))
2364         {
2365           if (mips_optimize < 2
2366               /* If we have seen .set volatile or .set nomove, don't
2367                  optimize.  */
2368               || mips_opts.nomove != 0
2369               /* If we had to emit any NOP instructions, then we
2370                  already know we can not swap.  */
2371               || nops != 0
2372               /* If we don't even know the previous insn, we can not
2373                  swap.  */
2374               || ! prev_insn_valid
2375               /* If the previous insn is already in a branch delay
2376                  slot, then we can not swap.  */
2377               || prev_insn_is_delay_slot
2378               /* If the previous previous insn was in a .set
2379                  noreorder, we can't swap.  Actually, the MIPS
2380                  assembler will swap in this situation.  However, gcc
2381                  configured -with-gnu-as will generate code like
2382                    .set noreorder
2383                    lw   $4,XXX
2384                    .set reorder
2385                    INSN
2386                    bne  $4,$0,foo
2387                  in which we can not swap the bne and INSN.  If gcc is
2388                  not configured -with-gnu-as, it does not output the
2389                  .set pseudo-ops.  We don't have to check
2390                  prev_insn_unreordered, because prev_insn_valid will
2391                  be 0 in that case.  We don't want to use
2392                  prev_prev_insn_valid, because we do want to be able
2393                  to swap at the start of a function.  */
2394               || prev_prev_insn_unreordered
2395               /* If the branch is itself the target of a branch, we
2396                  can not swap.  We cheat on this; all we check for is
2397                  whether there is a label on this instruction.  If
2398                  there are any branches to anything other than a
2399                  label, users must use .set noreorder.  */
2400               || insn_labels != NULL
2401               /* If the previous instruction is in a variant frag, we
2402                  can not do the swap.  This does not apply to the
2403                  mips16, which uses variant frags for different
2404                  purposes.  */
2405               || (! mips_opts.mips16
2406                   && prev_insn_frag->fr_type == rs_machine_dependent)
2407               /* If the branch reads the condition codes, we don't
2408                  even try to swap, because in the sequence
2409                    ctc1 $X,$31
2410                    INSN
2411                    INSN
2412                    bc1t LABEL
2413                  we can not swap, and I don't feel like handling that
2414                  case.  */
2415               || (! mips_opts.mips16
2416                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2417                   && (pinfo & INSN_READ_COND_CODE))
2418               /* We can not swap with an instruction that requires a
2419                  delay slot, becase the target of the branch might
2420                  interfere with that instruction.  */
2421               || (! mips_opts.mips16
2422                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2423                   && (prev_pinfo
2424               /* Itbl support may require additional care here.  */
2425                       & (INSN_LOAD_COPROC_DELAY
2426                          | INSN_COPROC_MOVE_DELAY
2427                          | INSN_WRITE_COND_CODE)))
2428               || (! (hilo_interlocks
2429                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2430                   && (prev_pinfo
2431                       & (INSN_READ_LO
2432                          | INSN_READ_HI)))
2433               || (! mips_opts.mips16
2434                   && ! gpr_interlocks
2435                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2436               || (! mips_opts.mips16
2437                   && mips_opts.isa == ISA_MIPS1
2438                   /* Itbl support may require additional care here.  */
2439                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2440               /* We can not swap with a branch instruction.  */
2441               || (prev_pinfo
2442                   & (INSN_UNCOND_BRANCH_DELAY
2443                      | INSN_COND_BRANCH_DELAY
2444                      | INSN_COND_BRANCH_LIKELY))
2445               /* We do not swap with a trap instruction, since it
2446                  complicates trap handlers to have the trap
2447                  instruction be in a delay slot.  */
2448               || (prev_pinfo & INSN_TRAP)
2449               /* If the branch reads a register that the previous
2450                  instruction sets, we can not swap.  */
2451               || (! mips_opts.mips16
2452                   && (prev_pinfo & INSN_WRITE_GPR_T)
2453                   && insn_uses_reg (ip,
2454                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2455                                      & OP_MASK_RT),
2456                                     MIPS_GR_REG))
2457               || (! mips_opts.mips16
2458                   && (prev_pinfo & INSN_WRITE_GPR_D)
2459                   && insn_uses_reg (ip,
2460                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2461                                      & OP_MASK_RD),
2462                                     MIPS_GR_REG))
2463               || (mips_opts.mips16
2464                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2465                        && insn_uses_reg (ip,
2466                                          ((prev_insn.insn_opcode
2467                                            >> MIPS16OP_SH_RX)
2468                                           & MIPS16OP_MASK_RX),
2469                                          MIPS16_REG))
2470                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2471                           && insn_uses_reg (ip,
2472                                             ((prev_insn.insn_opcode
2473                                               >> MIPS16OP_SH_RY)
2474                                              & MIPS16OP_MASK_RY),
2475                                             MIPS16_REG))
2476                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2477                           && insn_uses_reg (ip,
2478                                             ((prev_insn.insn_opcode
2479                                               >> MIPS16OP_SH_RZ)
2480                                              & MIPS16OP_MASK_RZ),
2481                                             MIPS16_REG))
2482                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2483                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2484                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2485                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2486                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2487                           && insn_uses_reg (ip,
2488                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2489                                                                      insn_opcode),
2490                                             MIPS_GR_REG))))
2491               /* If the branch writes a register that the previous
2492                  instruction sets, we can not swap (we know that
2493                  branches write only to RD or to $31).  */
2494               || (! mips_opts.mips16
2495                   && (prev_pinfo & INSN_WRITE_GPR_T)
2496                   && (((pinfo & INSN_WRITE_GPR_D)
2497                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2498                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2499                       || ((pinfo & INSN_WRITE_GPR_31)
2500                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2501                                & OP_MASK_RT)
2502                               == RA))))
2503               || (! mips_opts.mips16
2504                   && (prev_pinfo & INSN_WRITE_GPR_D)
2505                   && (((pinfo & INSN_WRITE_GPR_D)
2506                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2507                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2508                       || ((pinfo & INSN_WRITE_GPR_31)
2509                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2510                                & OP_MASK_RD)
2511                               == RA))))
2512               || (mips_opts.mips16
2513                   && (pinfo & MIPS16_INSN_WRITE_31)
2514                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2515                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2516                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2517                               == RA))))
2518               /* If the branch writes a register that the previous
2519                  instruction reads, we can not swap (we know that
2520                  branches only write to RD or to $31).  */
2521               || (! mips_opts.mips16
2522                   && (pinfo & INSN_WRITE_GPR_D)
2523                   && insn_uses_reg (&prev_insn,
2524                                     ((ip->insn_opcode >> OP_SH_RD)
2525                                      & OP_MASK_RD),
2526                                     MIPS_GR_REG))
2527               || (! mips_opts.mips16
2528                   && (pinfo & INSN_WRITE_GPR_31)
2529                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2530               || (mips_opts.mips16
2531                   && (pinfo & MIPS16_INSN_WRITE_31)
2532                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2533               /* If we are generating embedded PIC code, the branch
2534                  might be expanded into a sequence which uses $at, so
2535                  we can't swap with an instruction which reads it.  */
2536               || (mips_pic == EMBEDDED_PIC
2537                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2538               /* If the previous previous instruction has a load
2539                  delay, and sets a register that the branch reads, we
2540                  can not swap.  */
2541               || (! mips_opts.mips16
2542                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2543               /* Itbl support may require additional care here.  */
2544                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2545                       || (! gpr_interlocks
2546                           && (prev_prev_insn.insn_mo->pinfo
2547                               & INSN_LOAD_MEMORY_DELAY)))
2548                   && insn_uses_reg (ip,
2549                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2550                                      & OP_MASK_RT),
2551                                     MIPS_GR_REG))
2552               /* If one instruction sets a condition code and the
2553                  other one uses a condition code, we can not swap.  */
2554               || ((pinfo & INSN_READ_COND_CODE)
2555                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2556               || ((pinfo & INSN_WRITE_COND_CODE)
2557                   && (prev_pinfo & INSN_READ_COND_CODE))
2558               /* If the previous instruction uses the PC, we can not
2559                  swap.  */
2560               || (mips_opts.mips16
2561                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2562               /* If the previous instruction was extended, we can not
2563                  swap.  */
2564               || (mips_opts.mips16 && prev_insn_extended)
2565               /* If the previous instruction had a fixup in mips16
2566                  mode, we can not swap.  This normally means that the
2567                  previous instruction was a 4 byte branch anyhow.  */
2568               || (mips_opts.mips16 && prev_insn_fixp[0])
2569               /* If the previous instruction is a sync, sync.l, or
2570                  sync.p, we can not swap.  */
2571               || (prev_pinfo & INSN_SYNC))
2572             {
2573               /* We could do even better for unconditional branches to
2574                  portions of this object file; we could pick up the
2575                  instruction at the destination, put it in the delay
2576                  slot, and bump the destination address.  */
2577               emit_nop ();
2578               /* Update the previous insn information.  */
2579               prev_prev_insn = *ip;
2580               prev_insn.insn_mo = &dummy_opcode;
2581             }
2582           else
2583             {
2584               /* It looks like we can actually do the swap.  */
2585               if (! mips_opts.mips16)
2586                 {
2587                   char *prev_f;
2588                   char temp[4];
2589
2590                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2591                   memcpy (temp, prev_f, 4);
2592                   memcpy (prev_f, f, 4);
2593                   memcpy (f, temp, 4);
2594                   if (prev_insn_fixp[0])
2595                     {
2596                       prev_insn_fixp[0]->fx_frag = frag_now;
2597                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2598                     }
2599                   if (prev_insn_fixp[1])
2600                     {
2601                       prev_insn_fixp[1]->fx_frag = frag_now;
2602                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2603                     }
2604                   if (prev_insn_fixp[2])
2605                     {
2606                       prev_insn_fixp[2]->fx_frag = frag_now;
2607                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2608                     }
2609                   if (fixp[0])
2610                     {
2611                       fixp[0]->fx_frag = prev_insn_frag;
2612                       fixp[0]->fx_where = prev_insn_where;
2613                     }
2614                   if (fixp[1])
2615                     {
2616                       fixp[1]->fx_frag = prev_insn_frag;
2617                       fixp[1]->fx_where = prev_insn_where;
2618                     }
2619                   if (fixp[2])
2620                     {
2621                       fixp[2]->fx_frag = prev_insn_frag;
2622                       fixp[2]->fx_where = prev_insn_where;
2623                     }
2624                 }
2625               else
2626                 {
2627                   char *prev_f;
2628                   char temp[2];
2629
2630                   assert (prev_insn_fixp[0] == NULL);
2631                   assert (prev_insn_fixp[1] == NULL);
2632                   assert (prev_insn_fixp[2] == NULL);
2633                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2634                   memcpy (temp, prev_f, 2);
2635                   memcpy (prev_f, f, 2);
2636                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2637                     {
2638                       assert (*reloc_type == BFD_RELOC_UNUSED);
2639                       memcpy (f, temp, 2);
2640                     }
2641                   else
2642                     {
2643                       memcpy (f, f + 2, 2);
2644                       memcpy (f + 2, temp, 2);
2645                     }
2646                   if (fixp[0])
2647                     {
2648                       fixp[0]->fx_frag = prev_insn_frag;
2649                       fixp[0]->fx_where = prev_insn_where;
2650                     }
2651                   if (fixp[1])
2652                     {
2653                       fixp[1]->fx_frag = prev_insn_frag;
2654                       fixp[1]->fx_where = prev_insn_where;
2655                     }
2656                   if (fixp[2])
2657                     {
2658                       fixp[2]->fx_frag = prev_insn_frag;
2659                       fixp[2]->fx_where = prev_insn_where;
2660                     }
2661                 }
2662
2663               /* Update the previous insn information; leave prev_insn
2664                  unchanged.  */
2665               prev_prev_insn = *ip;
2666             }
2667           prev_insn_is_delay_slot = 1;
2668
2669           /* If that was an unconditional branch, forget the previous
2670              insn information.  */
2671           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2672             {
2673               prev_prev_insn.insn_mo = &dummy_opcode;
2674               prev_insn.insn_mo = &dummy_opcode;
2675             }
2676
2677           prev_insn_fixp[0] = NULL;
2678           prev_insn_fixp[1] = NULL;
2679           prev_insn_fixp[2] = NULL;
2680           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2681           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2682           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2683           prev_insn_extended = 0;
2684         }
2685       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2686         {
2687           /* We don't yet optimize a branch likely.  What we should do
2688              is look at the target, copy the instruction found there
2689              into the delay slot, and increment the branch to jump to
2690              the next instruction.  */
2691           emit_nop ();
2692           /* Update the previous insn information.  */
2693           prev_prev_insn = *ip;
2694           prev_insn.insn_mo = &dummy_opcode;
2695           prev_insn_fixp[0] = NULL;
2696           prev_insn_fixp[1] = NULL;
2697           prev_insn_fixp[2] = NULL;
2698           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2699           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2700           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2701           prev_insn_extended = 0;
2702         }
2703       else
2704         {
2705           /* Update the previous insn information.  */
2706           if (nops > 0)
2707             prev_prev_insn.insn_mo = &dummy_opcode;
2708           else
2709             prev_prev_insn = prev_insn;
2710           prev_insn = *ip;
2711
2712           /* Any time we see a branch, we always fill the delay slot
2713              immediately; since this insn is not a branch, we know it
2714              is not in a delay slot.  */
2715           prev_insn_is_delay_slot = 0;
2716
2717           prev_insn_fixp[0] = fixp[0];
2718           prev_insn_fixp[1] = fixp[1];
2719           prev_insn_fixp[2] = fixp[2];
2720           prev_insn_reloc_type[0] = reloc_type[0];
2721           prev_insn_reloc_type[1] = reloc_type[1];
2722           prev_insn_reloc_type[2] = reloc_type[2];
2723           if (mips_opts.mips16)
2724             prev_insn_extended = (ip->use_extend
2725                                   || *reloc_type > BFD_RELOC_UNUSED);
2726         }
2727
2728       prev_prev_insn_unreordered = prev_insn_unreordered;
2729       prev_insn_unreordered = 0;
2730       prev_insn_frag = frag_now;
2731       prev_insn_where = f - frag_now->fr_literal;
2732       prev_insn_valid = 1;
2733     }
2734   else if (place == NULL)
2735     {
2736       /* We need to record a bit of information even when we are not
2737          reordering, in order to determine the base address for mips16
2738          PC relative relocs.  */
2739       prev_prev_insn = prev_insn;
2740       prev_insn = *ip;
2741       prev_insn_reloc_type[0] = reloc_type[0];
2742       prev_insn_reloc_type[1] = reloc_type[1];
2743       prev_insn_reloc_type[2] = reloc_type[2];
2744       prev_prev_insn_unreordered = prev_insn_unreordered;
2745       prev_insn_unreordered = 1;
2746     }
2747
2748   /* We just output an insn, so the next one doesn't have a label.  */
2749   mips_clear_insn_labels ();
2750 }
2751
2752 /* This function forgets that there was any previous instruction or
2753    label.  If PRESERVE is non-zero, it remembers enough information to
2754    know whether nops are needed before a noreorder section.  */
2755
2756 static void
2757 mips_no_prev_insn (preserve)
2758      int preserve;
2759 {
2760   if (! preserve)
2761     {
2762       prev_insn.insn_mo = &dummy_opcode;
2763       prev_prev_insn.insn_mo = &dummy_opcode;
2764       prev_nop_frag = NULL;
2765       prev_nop_frag_holds = 0;
2766       prev_nop_frag_required = 0;
2767       prev_nop_frag_since = 0;
2768     }
2769   prev_insn_valid = 0;
2770   prev_insn_is_delay_slot = 0;
2771   prev_insn_unreordered = 0;
2772   prev_insn_extended = 0;
2773   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2774   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2775   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2776   prev_prev_insn_unreordered = 0;
2777   mips_clear_insn_labels ();
2778 }
2779
2780 /* This function must be called whenever we turn on noreorder or emit
2781    something other than instructions.  It inserts any NOPS which might
2782    be needed by the previous instruction, and clears the information
2783    kept for the previous instructions.  The INSNS parameter is true if
2784    instructions are to follow.  */
2785
2786 static void
2787 mips_emit_delays (insns)
2788      bfd_boolean insns;
2789 {
2790   if (! mips_opts.noreorder)
2791     {
2792       int nops;
2793
2794       nops = 0;
2795       if ((! mips_opts.mips16
2796            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2797            && (! cop_interlocks
2798                && (prev_insn.insn_mo->pinfo
2799                    & (INSN_LOAD_COPROC_DELAY
2800                       | INSN_COPROC_MOVE_DELAY
2801                       | INSN_WRITE_COND_CODE))))
2802           || (! hilo_interlocks
2803               && (prev_insn.insn_mo->pinfo
2804                   & (INSN_READ_LO
2805                      | INSN_READ_HI)))
2806           || (! mips_opts.mips16
2807               && ! gpr_interlocks
2808               && (prev_insn.insn_mo->pinfo
2809                   & INSN_LOAD_MEMORY_DELAY))
2810           || (! mips_opts.mips16
2811               && mips_opts.isa == ISA_MIPS1
2812               && (prev_insn.insn_mo->pinfo
2813                   & INSN_COPROC_MEMORY_DELAY)))
2814         {
2815           /* Itbl support may require additional care here.  */
2816           ++nops;
2817           if ((! mips_opts.mips16
2818                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2819                && (! cop_interlocks
2820                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2821               || (! hilo_interlocks
2822                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2823                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2824             ++nops;
2825
2826           if (prev_insn_unreordered)
2827             nops = 0;
2828         }
2829       else if ((! mips_opts.mips16
2830                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2831                 && (! cop_interlocks
2832                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2833                || (! hilo_interlocks
2834                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2835                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2836         {
2837           /* Itbl support may require additional care here.  */
2838           if (! prev_prev_insn_unreordered)
2839             ++nops;
2840         }
2841
2842       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2843         {
2844           int min_nops = 0;
2845           const char *pn = prev_insn.insn_mo->name;
2846           if (strncmp(pn, "macc", 4) == 0
2847               || strncmp(pn, "dmacc", 5) == 0
2848               || strncmp(pn, "dmult", 5) == 0)
2849             {
2850               min_nops = 1;
2851             }
2852           if (nops < min_nops)
2853             nops = min_nops;
2854         }
2855
2856       if (nops > 0)
2857         {
2858           struct insn_label_list *l;
2859
2860           if (insns)
2861             {
2862               /* Record the frag which holds the nop instructions, so
2863                  that we can remove them if we don't need them.  */
2864               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2865               prev_nop_frag = frag_now;
2866               prev_nop_frag_holds = nops;
2867               prev_nop_frag_required = 0;
2868               prev_nop_frag_since = 0;
2869             }
2870
2871           for (; nops > 0; --nops)
2872             emit_nop ();
2873
2874           if (insns)
2875             {
2876               /* Move on to a new frag, so that it is safe to simply
2877                  decrease the size of prev_nop_frag.  */
2878               frag_wane (frag_now);
2879               frag_new (0);
2880             }
2881
2882           for (l = insn_labels; l != NULL; l = l->next)
2883             {
2884               valueT val;
2885
2886               assert (S_GET_SEGMENT (l->label) == now_seg);
2887               symbol_set_frag (l->label, frag_now);
2888               val = (valueT) frag_now_fix ();
2889               /* mips16 text labels are stored as odd.  */
2890               if (mips_opts.mips16)
2891                 ++val;
2892               S_SET_VALUE (l->label, val);
2893             }
2894         }
2895     }
2896
2897   /* Mark instruction labels in mips16 mode.  */
2898   if (insns)
2899     mips16_mark_labels ();
2900
2901   mips_no_prev_insn (insns);
2902 }
2903
2904 /* Build an instruction created by a macro expansion.  This is passed
2905    a pointer to the count of instructions created so far, an
2906    expression, the name of the instruction to build, an operand format
2907    string, and corresponding arguments.  */
2908
2909 #ifdef USE_STDARG
2910 static void
2911 macro_build (char *place,
2912              int *counter,
2913              expressionS * ep,
2914              const char *name,
2915              const char *fmt,
2916              ...)
2917 #else
2918 static void
2919 macro_build (place, counter, ep, name, fmt, va_alist)
2920      char *place;
2921      int *counter;
2922      expressionS *ep;
2923      const char *name;
2924      const char *fmt;
2925      va_dcl
2926 #endif
2927 {
2928   struct mips_cl_insn insn;
2929   bfd_reloc_code_real_type r[3];
2930   va_list args;
2931
2932 #ifdef USE_STDARG
2933   va_start (args, fmt);
2934 #else
2935   va_start (args);
2936 #endif
2937
2938   /*
2939    * If the macro is about to expand into a second instruction,
2940    * print a warning if needed. We need to pass ip as a parameter
2941    * to generate a better warning message here...
2942    */
2943   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2944     as_warn (_("Macro instruction expanded into multiple instructions"));
2945
2946   /*
2947    * If the macro is about to expand into a second instruction,
2948    * and it is in a delay slot, print a warning.
2949    */
2950   if (place == NULL
2951       && *counter == 1
2952       && mips_opts.noreorder
2953       && (prev_prev_insn.insn_mo->pinfo
2954           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2955              | INSN_COND_BRANCH_LIKELY)) != 0)
2956     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2957
2958   if (place == NULL)
2959     ++*counter;         /* bump instruction counter */
2960
2961   if (mips_opts.mips16)
2962     {
2963       mips16_macro_build (place, counter, ep, name, fmt, args);
2964       va_end (args);
2965       return;
2966     }
2967
2968   r[0] = BFD_RELOC_UNUSED;
2969   r[1] = BFD_RELOC_UNUSED;
2970   r[2] = BFD_RELOC_UNUSED;
2971   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2972   assert (insn.insn_mo);
2973   assert (strcmp (name, insn.insn_mo->name) == 0);
2974
2975   /* Search until we get a match for NAME.  */
2976   while (1)
2977     {
2978       /* It is assumed here that macros will never generate
2979          MDMX or MIPS-3D instructions.  */
2980       if (strcmp (fmt, insn.insn_mo->args) == 0
2981           && insn.insn_mo->pinfo != INSN_MACRO
2982           && OPCODE_IS_MEMBER (insn.insn_mo,
2983                                (mips_opts.isa
2984                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2985                                mips_arch)
2986           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2987         break;
2988
2989       ++insn.insn_mo;
2990       assert (insn.insn_mo->name);
2991       assert (strcmp (name, insn.insn_mo->name) == 0);
2992     }
2993
2994   insn.insn_opcode = insn.insn_mo->match;
2995   for (;;)
2996     {
2997       switch (*fmt++)
2998         {
2999         case '\0':
3000           break;
3001
3002         case ',':
3003         case '(':
3004         case ')':
3005           continue;
3006
3007         case 't':
3008         case 'w':
3009         case 'E':
3010           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3011           continue;
3012
3013         case 'c':
3014           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3015           continue;
3016
3017         case 'T':
3018         case 'W':
3019           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3020           continue;
3021
3022         case 'd':
3023         case 'G':
3024         case 'K':
3025           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3026           continue;
3027
3028         case 'U':
3029           {
3030             int tmp = va_arg (args, int);
3031
3032             insn.insn_opcode |= tmp << OP_SH_RT;
3033             insn.insn_opcode |= tmp << OP_SH_RD;
3034             continue;
3035           }
3036
3037         case 'V':
3038         case 'S':
3039           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3040           continue;
3041
3042         case 'z':
3043           continue;
3044
3045         case '<':
3046           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3047           continue;
3048
3049         case 'D':
3050           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3051           continue;
3052
3053         case 'B':
3054           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3055           continue;
3056
3057         case 'J':
3058           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3059           continue;
3060
3061         case 'q':
3062           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3063           continue;
3064
3065         case 'b':
3066         case 's':
3067         case 'r':
3068         case 'v':
3069           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3070           continue;
3071
3072         case 'i':
3073         case 'j':
3074         case 'o':
3075           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3076           assert (*r == BFD_RELOC_GPREL16
3077                   || *r == BFD_RELOC_MIPS_LITERAL
3078                   || *r == BFD_RELOC_MIPS_HIGHER
3079                   || *r == BFD_RELOC_HI16_S
3080                   || *r == BFD_RELOC_LO16
3081                   || *r == BFD_RELOC_MIPS_GOT16
3082                   || *r == BFD_RELOC_MIPS_CALL16
3083                   || *r == BFD_RELOC_MIPS_GOT_DISP
3084                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3085                   || *r == BFD_RELOC_MIPS_GOT_OFST
3086                   || *r == BFD_RELOC_MIPS_GOT_LO16
3087                   || *r == BFD_RELOC_MIPS_CALL_LO16
3088                   || (ep->X_op == O_subtract
3089                       && *r == BFD_RELOC_PCREL_LO16));
3090           continue;
3091
3092         case 'u':
3093           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3094           assert (ep != NULL
3095                   && (ep->X_op == O_constant
3096                       || (ep->X_op == O_symbol
3097                           && (*r == BFD_RELOC_MIPS_HIGHEST
3098                               || *r == BFD_RELOC_HI16_S
3099                               || *r == BFD_RELOC_HI16
3100                               || *r == BFD_RELOC_GPREL16
3101                               || *r == BFD_RELOC_MIPS_GOT_HI16
3102                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3103                       || (ep->X_op == O_subtract
3104                           && *r == BFD_RELOC_PCREL_HI16_S)));
3105           continue;
3106
3107         case 'p':
3108           assert (ep != NULL);
3109           /*
3110            * This allows macro() to pass an immediate expression for
3111            * creating short branches without creating a symbol.
3112            * Note that the expression still might come from the assembly
3113            * input, in which case the value is not checked for range nor
3114            * is a relocation entry generated (yuck).
3115            */
3116           if (ep->X_op == O_constant)
3117             {
3118               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3119               ep = NULL;
3120             }
3121           else
3122             *r = BFD_RELOC_16_PCREL_S2;
3123           continue;
3124
3125         case 'a':
3126           assert (ep != NULL);
3127           *r = BFD_RELOC_MIPS_JMP;
3128           continue;
3129
3130         case 'C':
3131           insn.insn_opcode |= va_arg (args, unsigned long);
3132           continue;
3133
3134         default:
3135           internalError ();
3136         }
3137       break;
3138     }
3139   va_end (args);
3140   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3141
3142   append_insn (place, &insn, ep, r);
3143 }
3144
3145 static void
3146 mips16_macro_build (place, counter, ep, name, fmt, args)
3147      char *place;
3148      int *counter ATTRIBUTE_UNUSED;
3149      expressionS *ep;
3150      const char *name;
3151      const char *fmt;
3152      va_list args;
3153 {
3154   struct mips_cl_insn insn;
3155   bfd_reloc_code_real_type r[3]
3156     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3157
3158   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3159   assert (insn.insn_mo);
3160   assert (strcmp (name, insn.insn_mo->name) == 0);
3161
3162   while (strcmp (fmt, insn.insn_mo->args) != 0
3163          || insn.insn_mo->pinfo == INSN_MACRO)
3164     {
3165       ++insn.insn_mo;
3166       assert (insn.insn_mo->name);
3167       assert (strcmp (name, insn.insn_mo->name) == 0);
3168     }
3169
3170   insn.insn_opcode = insn.insn_mo->match;
3171   insn.use_extend = FALSE;
3172
3173   for (;;)
3174     {
3175       int c;
3176
3177       c = *fmt++;
3178       switch (c)
3179         {
3180         case '\0':
3181           break;
3182
3183         case ',':
3184         case '(':
3185         case ')':
3186           continue;
3187
3188         case 'y':
3189         case 'w':
3190           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3191           continue;
3192
3193         case 'x':
3194         case 'v':
3195           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3196           continue;
3197
3198         case 'z':
3199           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3200           continue;
3201
3202         case 'Z':
3203           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3204           continue;
3205
3206         case '0':
3207         case 'S':
3208         case 'P':
3209         case 'R':
3210           continue;
3211
3212         case 'X':
3213           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3214           continue;
3215
3216         case 'Y':
3217           {
3218             int regno;
3219
3220             regno = va_arg (args, int);
3221             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3222             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3223           }
3224           continue;
3225
3226         case '<':
3227         case '>':
3228         case '4':
3229         case '5':
3230         case 'H':
3231         case 'W':
3232         case 'D':
3233         case 'j':
3234         case '8':
3235         case 'V':
3236         case 'C':
3237         case 'U':
3238         case 'k':
3239         case 'K':
3240         case 'p':
3241         case 'q':
3242           {
3243             assert (ep != NULL);
3244
3245             if (ep->X_op != O_constant)
3246               *r = (int) BFD_RELOC_UNUSED + c;
3247             else
3248               {
3249                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3250                               FALSE, &insn.insn_opcode, &insn.use_extend,
3251                               &insn.extend);
3252                 ep = NULL;
3253                 *r = BFD_RELOC_UNUSED;
3254               }
3255           }
3256           continue;
3257
3258         case '6':
3259           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3260           continue;
3261         }
3262
3263       break;
3264     }
3265
3266   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3267
3268   append_insn (place, &insn, ep, r);
3269 }
3270
3271 /*
3272  * Generate a "jalr" instruction with a relocation hint to the called
3273  * function.  This occurs in NewABI PIC code.
3274  */
3275 static void
3276 macro_build_jalr (icnt, ep)
3277      int icnt;
3278      expressionS *ep;
3279 {
3280   char *f;
3281
3282   if (HAVE_NEWABI)
3283     {
3284       frag_grow (4);
3285       f = frag_more (0);
3286     }
3287   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3288                RA, PIC_CALL_REG);
3289   if (HAVE_NEWABI)
3290     fix_new_exp (frag_now, f - frag_now->fr_literal,
3291                  0, ep, FALSE, BFD_RELOC_MIPS_JALR);
3292 }
3293
3294 /*
3295  * Generate a "lui" instruction.
3296  */
3297 static void
3298 macro_build_lui (place, counter, ep, regnum)
3299      char *place;
3300      int *counter;
3301      expressionS *ep;
3302      int regnum;
3303 {
3304   expressionS high_expr;
3305   struct mips_cl_insn insn;
3306   bfd_reloc_code_real_type r[3]
3307     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3308   const char *name = "lui";
3309   const char *fmt = "t,u";
3310
3311   assert (! mips_opts.mips16);
3312
3313   if (place == NULL)
3314     high_expr = *ep;
3315   else
3316     {
3317       high_expr.X_op = O_constant;
3318       high_expr.X_add_number = ep->X_add_number;
3319     }
3320
3321   if (high_expr.X_op == O_constant)
3322     {
3323       /* we can compute the instruction now without a relocation entry */
3324       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3325                                 >> 16) & 0xffff;
3326       *r = BFD_RELOC_UNUSED;
3327     }
3328   else
3329     {
3330       assert (ep->X_op == O_symbol);
3331       /* _gp_disp is a special case, used from s_cpload.  */
3332       assert (mips_pic == NO_PIC
3333               || (! HAVE_NEWABI
3334                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3335       *r = BFD_RELOC_HI16_S;
3336     }
3337
3338   /*
3339    * If the macro is about to expand into a second instruction,
3340    * print a warning if needed. We need to pass ip as a parameter
3341    * to generate a better warning message here...
3342    */
3343   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3344     as_warn (_("Macro instruction expanded into multiple instructions"));
3345
3346   if (place == NULL)
3347     ++*counter;         /* bump instruction counter */
3348
3349   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3350   assert (insn.insn_mo);
3351   assert (strcmp (name, insn.insn_mo->name) == 0);
3352   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3353
3354   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3355   if (*r == BFD_RELOC_UNUSED)
3356     {
3357       insn.insn_opcode |= high_expr.X_add_number;
3358       append_insn (place, &insn, NULL, r);
3359     }
3360   else
3361     append_insn (place, &insn, &high_expr, r);
3362 }
3363
3364 /* Generate a sequence of instructions to do a load or store from a constant
3365    offset off of a base register (breg) into/from a target register (treg),
3366    using AT if necessary.  */
3367 static void
3368 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3369      char *place;
3370      int *counter;
3371      expressionS *ep;
3372      const char *op;
3373      int treg, breg;
3374 {
3375   assert (ep->X_op == O_constant);
3376
3377   /* Right now, this routine can only handle signed 32-bit contants.  */
3378   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3379     as_warn (_("operand overflow"));
3380
3381   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3382     {
3383       /* Signed 16-bit offset will fit in the op.  Easy!  */
3384       macro_build (place, counter, ep, op, "t,o(b)", treg,
3385                    (int) BFD_RELOC_LO16, breg);
3386     }
3387   else
3388     {
3389       /* 32-bit offset, need multiple instructions and AT, like:
3390            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3391            addu     $tempreg,$tempreg,$breg
3392            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3393          to handle the complete offset.  */
3394       macro_build_lui (place, counter, ep, AT);
3395       if (place != NULL)
3396         place += 4;
3397       macro_build (place, counter, (expressionS *) NULL,
3398                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3399                    "d,v,t", AT, AT, breg);
3400       if (place != NULL)
3401         place += 4;
3402       macro_build (place, counter, ep, op, "t,o(b)", treg,
3403                    (int) BFD_RELOC_LO16, AT);
3404
3405       if (mips_opts.noat)
3406         as_warn (_("Macro used $at after \".set noat\""));
3407     }
3408 }
3409
3410 /*                      set_at()
3411  * Generates code to set the $at register to true (one)
3412  * if reg is less than the immediate expression.
3413  */
3414 static void
3415 set_at (counter, reg, unsignedp)
3416      int *counter;
3417      int reg;
3418      int unsignedp;
3419 {
3420   if (imm_expr.X_op == O_constant
3421       && imm_expr.X_add_number >= -0x8000
3422       && imm_expr.X_add_number < 0x8000)
3423     macro_build ((char *) NULL, counter, &imm_expr,
3424                  unsignedp ? "sltiu" : "slti",
3425                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3426   else
3427     {
3428       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3429       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3430                    unsignedp ? "sltu" : "slt",
3431                    "d,v,t", AT, reg, AT);
3432     }
3433 }
3434
3435 /* Warn if an expression is not a constant.  */
3436
3437 static void
3438 check_absolute_expr (ip, ex)
3439      struct mips_cl_insn *ip;
3440      expressionS *ex;
3441 {
3442   if (ex->X_op == O_big)
3443     as_bad (_("unsupported large constant"));
3444   else if (ex->X_op != O_constant)
3445     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3446 }
3447
3448 /* Count the leading zeroes by performing a binary chop. This is a
3449    bulky bit of source, but performance is a LOT better for the
3450    majority of values than a simple loop to count the bits:
3451        for (lcnt = 0; (lcnt < 32); lcnt++)
3452          if ((v) & (1 << (31 - lcnt)))
3453            break;
3454   However it is not code size friendly, and the gain will drop a bit
3455   on certain cached systems.
3456 */
3457 #define COUNT_TOP_ZEROES(v)             \
3458   (((v) & ~0xffff) == 0                 \
3459    ? ((v) & ~0xff) == 0                 \
3460      ? ((v) & ~0xf) == 0                \
3461        ? ((v) & ~0x3) == 0              \
3462          ? ((v) & ~0x1) == 0            \
3463            ? !(v)                       \
3464              ? 32                       \
3465              : 31                       \
3466            : 30                         \
3467          : ((v) & ~0x7) == 0            \
3468            ? 29                         \
3469            : 28                         \
3470        : ((v) & ~0x3f) == 0             \
3471          ? ((v) & ~0x1f) == 0           \
3472            ? 27                         \
3473            : 26                         \
3474          : ((v) & ~0x7f) == 0           \
3475            ? 25                         \
3476            : 24                         \
3477      : ((v) & ~0xfff) == 0              \
3478        ? ((v) & ~0x3ff) == 0            \
3479          ? ((v) & ~0x1ff) == 0          \
3480            ? 23                         \
3481            : 22                         \
3482          : ((v) & ~0x7ff) == 0          \
3483            ? 21                         \
3484            : 20                         \
3485        : ((v) & ~0x3fff) == 0           \
3486          ? ((v) & ~0x1fff) == 0         \
3487            ? 19                         \
3488            : 18                         \
3489          : ((v) & ~0x7fff) == 0         \
3490            ? 17                         \
3491            : 16                         \
3492    : ((v) & ~0xffffff) == 0             \
3493      ? ((v) & ~0xfffff) == 0            \
3494        ? ((v) & ~0x3ffff) == 0          \
3495          ? ((v) & ~0x1ffff) == 0        \
3496            ? 15                         \
3497            : 14                         \
3498          : ((v) & ~0x7ffff) == 0        \
3499            ? 13                         \
3500            : 12                         \
3501        : ((v) & ~0x3fffff) == 0         \
3502          ? ((v) & ~0x1fffff) == 0       \
3503            ? 11                         \
3504            : 10                         \
3505          : ((v) & ~0x7fffff) == 0       \
3506            ? 9                          \
3507            : 8                          \
3508      : ((v) & ~0xfffffff) == 0          \
3509        ? ((v) & ~0x3ffffff) == 0        \
3510          ? ((v) & ~0x1ffffff) == 0      \
3511            ? 7                          \
3512            : 6                          \
3513          : ((v) & ~0x7ffffff) == 0      \
3514            ? 5                          \
3515            : 4                          \
3516        : ((v) & ~0x3fffffff) == 0       \
3517          ? ((v) & ~0x1fffffff) == 0     \
3518            ? 3                          \
3519            : 2                          \
3520          : ((v) & ~0x7fffffff) == 0     \
3521            ? 1                          \
3522            : 0)
3523
3524 /*                      load_register()
3525  *  This routine generates the least number of instructions neccessary to load
3526  *  an absolute expression value into a register.
3527  */
3528 static void
3529 load_register (counter, reg, ep, dbl)
3530      int *counter;
3531      int reg;
3532      expressionS *ep;
3533      int dbl;
3534 {
3535   int freg;
3536   expressionS hi32, lo32;
3537
3538   if (ep->X_op != O_big)
3539     {
3540       assert (ep->X_op == O_constant);
3541       if (ep->X_add_number < 0x8000
3542           && (ep->X_add_number >= 0
3543               || (ep->X_add_number >= -0x8000
3544                   && (! dbl
3545                       || ! ep->X_unsigned
3546                       || sizeof (ep->X_add_number) > 4))))
3547         {
3548           /* We can handle 16 bit signed values with an addiu to
3549              $zero.  No need to ever use daddiu here, since $zero and
3550              the result are always correct in 32 bit mode.  */
3551           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3552                        (int) BFD_RELOC_LO16);
3553           return;
3554         }
3555       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3556         {
3557           /* We can handle 16 bit unsigned values with an ori to
3558              $zero.  */
3559           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3560                        (int) BFD_RELOC_LO16);
3561           return;
3562         }
3563       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3564                 && (! dbl
3565                     || ! ep->X_unsigned
3566                     || sizeof (ep->X_add_number) > 4
3567                     || (ep->X_add_number & 0x80000000) == 0))
3568                || ((HAVE_32BIT_GPRS || ! dbl)
3569                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3570                || (HAVE_32BIT_GPRS
3571                    && ! dbl
3572                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3573                        == ~ (offsetT) 0xffffffff)))
3574         {
3575           /* 32 bit values require an lui.  */
3576           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3577                        (int) BFD_RELOC_HI16);
3578           if ((ep->X_add_number & 0xffff) != 0)
3579             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3580                          (int) BFD_RELOC_LO16);
3581           return;
3582         }
3583     }
3584
3585   /* The value is larger than 32 bits.  */
3586
3587   if (HAVE_32BIT_GPRS)
3588     {
3589       as_bad (_("Number (0x%lx) larger than 32 bits"),
3590               (unsigned long) ep->X_add_number);
3591       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3592                    (int) BFD_RELOC_LO16);
3593       return;
3594     }
3595
3596   if (ep->X_op != O_big)
3597     {
3598       hi32 = *ep;
3599       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3600       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3601       hi32.X_add_number &= 0xffffffff;
3602       lo32 = *ep;
3603       lo32.X_add_number &= 0xffffffff;
3604     }
3605   else
3606     {
3607       assert (ep->X_add_number > 2);
3608       if (ep->X_add_number == 3)
3609         generic_bignum[3] = 0;
3610       else if (ep->X_add_number > 4)
3611         as_bad (_("Number larger than 64 bits"));
3612       lo32.X_op = O_constant;
3613       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3614       hi32.X_op = O_constant;
3615       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3616     }
3617
3618   if (hi32.X_add_number == 0)
3619     freg = 0;
3620   else
3621     {
3622       int shift, bit;
3623       unsigned long hi, lo;
3624
3625       if (hi32.X_add_number == (offsetT) 0xffffffff)
3626         {
3627           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3628             {
3629               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3630                            reg, 0, (int) BFD_RELOC_LO16);
3631               return;
3632             }
3633           if (lo32.X_add_number & 0x80000000)
3634             {
3635               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3636                            (int) BFD_RELOC_HI16);
3637               if (lo32.X_add_number & 0xffff)
3638                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3639                              reg, reg, (int) BFD_RELOC_LO16);
3640               return;
3641             }
3642         }
3643
3644       /* Check for 16bit shifted constant.  We know that hi32 is
3645          non-zero, so start the mask on the first bit of the hi32
3646          value.  */
3647       shift = 17;
3648       do
3649         {
3650           unsigned long himask, lomask;
3651
3652           if (shift < 32)
3653             {
3654               himask = 0xffff >> (32 - shift);
3655               lomask = (0xffff << shift) & 0xffffffff;
3656             }
3657           else
3658             {
3659               himask = 0xffff << (shift - 32);
3660               lomask = 0;
3661             }
3662           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3663               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3664             {
3665               expressionS tmp;
3666
3667               tmp.X_op = O_constant;
3668               if (shift < 32)
3669                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3670                                     | (lo32.X_add_number >> shift));
3671               else
3672                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3673               macro_build ((char *) NULL, counter, &tmp,
3674                            "ori", "t,r,i", reg, 0,
3675                            (int) BFD_RELOC_LO16);
3676               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3677                            (shift >= 32) ? "dsll32" : "dsll",
3678                            "d,w,<", reg, reg,
3679                            (shift >= 32) ? shift - 32 : shift);
3680               return;
3681             }
3682           ++shift;
3683         }
3684       while (shift <= (64 - 16));
3685
3686       /* Find the bit number of the lowest one bit, and store the
3687          shifted value in hi/lo.  */
3688       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3689       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3690       if (lo != 0)
3691         {
3692           bit = 0;
3693           while ((lo & 1) == 0)
3694             {
3695               lo >>= 1;
3696               ++bit;
3697             }
3698           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3699           hi >>= bit;
3700         }
3701       else
3702         {
3703           bit = 32;
3704           while ((hi & 1) == 0)
3705             {
3706               hi >>= 1;
3707               ++bit;
3708             }
3709           lo = hi;
3710           hi = 0;
3711         }
3712
3713       /* Optimize if the shifted value is a (power of 2) - 1.  */
3714       if ((hi == 0 && ((lo + 1) & lo) == 0)
3715           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3716         {
3717           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3718           if (shift != 0)
3719             {
3720               expressionS tmp;
3721
3722               /* This instruction will set the register to be all
3723                  ones.  */
3724               tmp.X_op = O_constant;
3725               tmp.X_add_number = (offsetT) -1;
3726               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3727                            reg, 0, (int) BFD_RELOC_LO16);
3728               if (bit != 0)
3729                 {
3730                   bit += shift;
3731                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3732                                (bit >= 32) ? "dsll32" : "dsll",
3733                                "d,w,<", reg, reg,
3734                                (bit >= 32) ? bit - 32 : bit);
3735                 }
3736               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3737                            (shift >= 32) ? "dsrl32" : "dsrl",
3738                            "d,w,<", reg, reg,
3739                            (shift >= 32) ? shift - 32 : shift);
3740               return;
3741             }
3742         }
3743
3744       /* Sign extend hi32 before calling load_register, because we can
3745          generally get better code when we load a sign extended value.  */
3746       if ((hi32.X_add_number & 0x80000000) != 0)
3747         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3748       load_register (counter, reg, &hi32, 0);
3749       freg = reg;
3750     }
3751   if ((lo32.X_add_number & 0xffff0000) == 0)
3752     {
3753       if (freg != 0)
3754         {
3755           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3756                        "dsll32", "d,w,<", reg, freg, 0);
3757           freg = reg;
3758         }
3759     }
3760   else
3761     {
3762       expressionS mid16;
3763
3764       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3765         {
3766           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3767                        (int) BFD_RELOC_HI16);
3768           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3769                        "dsrl32", "d,w,<", reg, reg, 0);
3770           return;
3771         }
3772
3773       if (freg != 0)
3774         {
3775           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3776                        "d,w,<", reg, freg, 16);
3777           freg = reg;
3778         }
3779       mid16 = lo32;
3780       mid16.X_add_number >>= 16;
3781       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3782                    freg, (int) BFD_RELOC_LO16);
3783       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3784                    "d,w,<", reg, reg, 16);
3785       freg = reg;
3786     }
3787   if ((lo32.X_add_number & 0xffff) != 0)
3788     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3789                  (int) BFD_RELOC_LO16);
3790 }
3791
3792 /* Load an address into a register.  */
3793
3794 static void
3795 load_address (counter, reg, ep, used_at)
3796      int *counter;
3797      int reg;
3798      expressionS *ep;
3799      int *used_at;
3800 {
3801   char *p = NULL;
3802
3803   if (ep->X_op != O_constant
3804       && ep->X_op != O_symbol)
3805     {
3806       as_bad (_("expression too complex"));
3807       ep->X_op = O_constant;
3808     }
3809
3810   if (ep->X_op == O_constant)
3811     {
3812       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3813       return;
3814     }
3815
3816   if (mips_pic == NO_PIC)
3817     {
3818       /* If this is a reference to a GP relative symbol, we want
3819            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3820          Otherwise we want
3821            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3822            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3823          If we have an addend, we always use the latter form.
3824
3825          With 64bit address space and a usable $at we want
3826            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3827            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3828            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3829            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3830            dsll32       $reg,0
3831            daddu        $reg,$reg,$at
3832
3833          If $at is already in use, we use a path which is suboptimal
3834          on superscalar processors.
3835            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3836            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3837            dsll         $reg,16
3838            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3839            dsll         $reg,16
3840            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3841        */
3842       if (HAVE_64BIT_ADDRESSES)
3843         {
3844           /* We don't do GP optimization for now because RELAX_ENCODE can't
3845              hold the data for such large chunks.  */
3846
3847           if (*used_at == 0 && ! mips_opts.noat)
3848             {
3849               macro_build (p, counter, ep, "lui", "t,u",
3850                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3851               macro_build (p, counter, ep, "lui", "t,u",
3852                            AT, (int) BFD_RELOC_HI16_S);
3853               macro_build (p, counter, ep, "daddiu", "t,r,j",
3854                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3855               macro_build (p, counter, ep, "daddiu", "t,r,j",
3856                            AT, AT, (int) BFD_RELOC_LO16);
3857               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3858                            "d,w,<", reg, reg, 0);
3859               macro_build (p, counter, (expressionS *) NULL, "daddu",
3860                            "d,v,t", reg, reg, AT);
3861               *used_at = 1;
3862             }
3863           else
3864             {
3865               macro_build (p, counter, ep, "lui", "t,u",
3866                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3867               macro_build (p, counter, ep, "daddiu", "t,r,j",
3868                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3869               macro_build (p, counter, (expressionS *) NULL, "dsll",
3870                            "d,w,<", reg, reg, 16);
3871               macro_build (p, counter, ep, "daddiu", "t,r,j",
3872                            reg, reg, (int) BFD_RELOC_HI16_S);
3873               macro_build (p, counter, (expressionS *) NULL, "dsll",
3874                            "d,w,<", reg, reg, 16);
3875               macro_build (p, counter, ep, "daddiu", "t,r,j",
3876                            reg, reg, (int) BFD_RELOC_LO16);
3877             }
3878         }
3879       else
3880         {
3881           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3882               && ! nopic_need_relax (ep->X_add_symbol, 1))
3883             {
3884               frag_grow (20);
3885               macro_build ((char *) NULL, counter, ep,
3886                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3887                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3888               p = frag_var (rs_machine_dependent, 8, 0,
3889                             RELAX_ENCODE (4, 8, 0, 4, 0,
3890                                           mips_opts.warn_about_macros),
3891                             ep->X_add_symbol, 0, NULL);
3892             }
3893           macro_build_lui (p, counter, ep, reg);
3894           if (p != NULL)
3895             p += 4;
3896           macro_build (p, counter, ep,
3897                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3898                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3899         }
3900     }
3901   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3902     {
3903       expressionS ex;
3904
3905       /* If this is a reference to an external symbol, we want
3906            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3907          Otherwise we want
3908            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3909            nop
3910            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3911          If we have NewABI, we want
3912            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3913          If there is a constant, it must be added in after.  */
3914       ex.X_add_number = ep->X_add_number;
3915       ep->X_add_number = 0;
3916       frag_grow (20);
3917       if (HAVE_NEWABI)
3918         {
3919           macro_build ((char *) NULL, counter, ep,
3920                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3921                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3922         }
3923       else
3924         {
3925           macro_build ((char *) NULL, counter, ep,
3926                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3927                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3928           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3929           p = frag_var (rs_machine_dependent, 4, 0,
3930                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3931                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3932           macro_build (p, counter, ep,
3933                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3934                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3935         }
3936
3937       if (ex.X_add_number != 0)
3938         {
3939           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3940             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3941           ex.X_op = O_constant;
3942           macro_build ((char *) NULL, counter, &ex,
3943                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3944                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3945         }
3946     }
3947   else if (mips_pic == SVR4_PIC)
3948     {
3949       expressionS ex;
3950       int off;
3951
3952       /* This is the large GOT case.  If this is a reference to an
3953          external symbol, we want
3954            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3955            addu         $reg,$reg,$gp
3956            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3957          Otherwise, for a reference to a local symbol, we want
3958            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3959            nop
3960            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3961          If we have NewABI, we want
3962            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3963            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3964          If there is a constant, it must be added in after.  */
3965       ex.X_add_number = ep->X_add_number;
3966       ep->X_add_number = 0;
3967       if (HAVE_NEWABI)
3968         {
3969           macro_build ((char *) NULL, counter, ep,
3970                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3971                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3972           macro_build (p, counter, ep,
3973                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3974                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3975         }
3976       else
3977         {
3978           if (reg_needs_delay (mips_gp_register))
3979             off = 4;
3980           else
3981             off = 0;
3982           frag_grow (32);
3983           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3984                        (int) BFD_RELOC_MIPS_GOT_HI16);
3985           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3986                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3987                        reg, mips_gp_register);
3988           macro_build ((char *) NULL, counter, ep,
3989                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3990                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3991           p = frag_var (rs_machine_dependent, 12 + off, 0,
3992                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3993                                       mips_opts.warn_about_macros),
3994                         ep->X_add_symbol, 0, NULL);
3995           if (off > 0)
3996             {
3997               /* We need a nop before loading from $gp.  This special
3998                  check is required because the lui which starts the main
3999                  instruction stream does not refer to $gp, and so will not
4000                  insert the nop which may be required.  */
4001               macro_build (p, counter, (expressionS *) NULL, "nop", "");
4002                 p += 4;
4003             }
4004           macro_build (p, counter, ep,
4005                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4006                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4007           p += 4;
4008           macro_build (p, counter, (expressionS *) NULL, "nop", "");
4009           p += 4;
4010           macro_build (p, counter, ep,
4011                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4012                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4013         }
4014
4015       if (ex.X_add_number != 0)
4016         {
4017           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4018             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4019           ex.X_op = O_constant;
4020           macro_build ((char *) NULL, counter, &ex,
4021                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4022                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4023         }
4024     }
4025   else if (mips_pic == EMBEDDED_PIC)
4026     {
4027       /* We always do
4028            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
4029        */
4030       macro_build ((char *) NULL, counter, ep,
4031                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4032                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4033     }
4034   else
4035     abort ();
4036 }
4037
4038 /* Move the contents of register SOURCE into register DEST.  */
4039
4040 static void
4041 move_register (counter, dest, source)
4042      int *counter;
4043      int dest;
4044      int source;
4045 {
4046   macro_build ((char *) NULL, counter, (expressionS *) NULL,
4047                HAVE_32BIT_GPRS ? "addu" : "daddu",
4048                "d,v,t", dest, source, 0);
4049 }
4050
4051 /*
4052  *                      Build macros
4053  *   This routine implements the seemingly endless macro or synthesized
4054  * instructions and addressing modes in the mips assembly language. Many
4055  * of these macros are simple and are similar to each other. These could
4056  * probably be handled by some kind of table or grammer aproach instead of
4057  * this verbose method. Others are not simple macros but are more like
4058  * optimizing code generation.
4059  *   One interesting optimization is when several store macros appear
4060  * consecutivly that would load AT with the upper half of the same address.
4061  * The ensuing load upper instructions are ommited. This implies some kind
4062  * of global optimization. We currently only optimize within a single macro.
4063  *   For many of the load and store macros if the address is specified as a
4064  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4065  * first load register 'at' with zero and use it as the base register. The
4066  * mips assembler simply uses register $zero. Just one tiny optimization
4067  * we're missing.
4068  */
4069 static void
4070 macro (ip)
4071      struct mips_cl_insn *ip;
4072 {
4073   register int treg, sreg, dreg, breg;
4074   int tempreg;
4075   int mask;
4076   int icnt = 0;
4077   int used_at = 0;
4078   expressionS expr1;
4079   const char *s;
4080   const char *s2;
4081   const char *fmt;
4082   int likely = 0;
4083   int dbl = 0;
4084   int coproc = 0;
4085   int lr = 0;
4086   int imm = 0;
4087   offsetT maxnum;
4088   int off;
4089   bfd_reloc_code_real_type r;
4090   int hold_mips_optimize;
4091
4092   assert (! mips_opts.mips16);
4093
4094   treg = (ip->insn_opcode >> 16) & 0x1f;
4095   dreg = (ip->insn_opcode >> 11) & 0x1f;
4096   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4097   mask = ip->insn_mo->mask;
4098
4099   expr1.X_op = O_constant;
4100   expr1.X_op_symbol = NULL;
4101   expr1.X_add_symbol = NULL;
4102   expr1.X_add_number = 1;
4103
4104   /* Umatched fixups should not be put in the same frag as a relaxable
4105      macro.  For example, suppose we have:
4106
4107         lui $4,%hi(l1)          # 1
4108         la $5,l2                # 2
4109         addiu $4,$4,%lo(l1)     # 3
4110
4111      If instructions 1 and 2 were put in the same frag, md_frob_file would
4112      move the fixup for #1 after the fixups for the "unrelaxed" version of
4113      #2.  This would confuse tc_gen_reloc, which expects the relocations
4114      for #2 to be the last for that frag.
4115
4116      Also, if tc_gen_reloc sees certain relocations in a variant frag,
4117      it assumes that they belong to a relaxable macro.  We mustn't put
4118      other uses of such relocations into a variant frag.
4119
4120      To avoid both problems, finish the current frag it contains a
4121      %reloc() operator.  The macro then goes into a new frag.  */
4122   if (prev_reloc_op_frag == frag_now)
4123     {
4124       frag_wane (frag_now);
4125       frag_new (0);
4126     }
4127
4128   switch (mask)
4129     {
4130     case M_DABS:
4131       dbl = 1;
4132     case M_ABS:
4133       /* bgez $a0,.+12
4134          move v0,$a0
4135          sub v0,$zero,$a0
4136          */
4137
4138       mips_emit_delays (TRUE);
4139       ++mips_opts.noreorder;
4140       mips_any_noreorder = 1;
4141
4142       expr1.X_add_number = 8;
4143       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4144       if (dreg == sreg)
4145         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4146                      0);
4147       else
4148         move_register (&icnt, dreg, sreg);
4149       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4150                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4151
4152       --mips_opts.noreorder;
4153       return;
4154
4155     case M_ADD_I:
4156       s = "addi";
4157       s2 = "add";
4158       goto do_addi;
4159     case M_ADDU_I:
4160       s = "addiu";
4161       s2 = "addu";
4162       goto do_addi;
4163     case M_DADD_I:
4164       dbl = 1;
4165       s = "daddi";
4166       s2 = "dadd";
4167       goto do_addi;
4168     case M_DADDU_I:
4169       dbl = 1;
4170       s = "daddiu";
4171       s2 = "daddu";
4172     do_addi:
4173       if (imm_expr.X_op == O_constant
4174           && imm_expr.X_add_number >= -0x8000
4175           && imm_expr.X_add_number < 0x8000)
4176         {
4177           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4178                        (int) BFD_RELOC_LO16);
4179           return;
4180         }
4181       load_register (&icnt, AT, &imm_expr, dbl);
4182       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4183                    treg, sreg, AT);
4184       break;
4185
4186     case M_AND_I:
4187       s = "andi";
4188       s2 = "and";
4189       goto do_bit;
4190     case M_OR_I:
4191       s = "ori";
4192       s2 = "or";
4193       goto do_bit;
4194     case M_NOR_I:
4195       s = "";
4196       s2 = "nor";
4197       goto do_bit;
4198     case M_XOR_I:
4199       s = "xori";
4200       s2 = "xor";
4201     do_bit:
4202       if (imm_expr.X_op == O_constant
4203           && imm_expr.X_add_number >= 0
4204           && imm_expr.X_add_number < 0x10000)
4205         {
4206           if (mask != M_NOR_I)
4207             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4208                          sreg, (int) BFD_RELOC_LO16);
4209           else
4210             {
4211               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4212                            treg, sreg, (int) BFD_RELOC_LO16);
4213               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4214                            "d,v,t", treg, treg, 0);
4215             }
4216           return;
4217         }
4218
4219       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4220       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4221                    treg, sreg, AT);
4222       break;
4223
4224     case M_BEQ_I:
4225       s = "beq";
4226       goto beq_i;
4227     case M_BEQL_I:
4228       s = "beql";
4229       likely = 1;
4230       goto beq_i;
4231     case M_BNE_I:
4232       s = "bne";
4233       goto beq_i;
4234     case M_BNEL_I:
4235       s = "bnel";
4236       likely = 1;
4237     beq_i:
4238       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4239         {
4240           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4241                        0);
4242           return;
4243         }
4244       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4245       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4246       break;
4247
4248     case M_BGEL:
4249       likely = 1;
4250     case M_BGE:
4251       if (treg == 0)
4252         {
4253           macro_build ((char *) NULL, &icnt, &offset_expr,
4254                        likely ? "bgezl" : "bgez", "s,p", sreg);
4255           return;
4256         }
4257       if (sreg == 0)
4258         {
4259           macro_build ((char *) NULL, &icnt, &offset_expr,
4260                        likely ? "blezl" : "blez", "s,p", treg);
4261           return;
4262         }
4263       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4264                    AT, sreg, treg);
4265       macro_build ((char *) NULL, &icnt, &offset_expr,
4266                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4267       break;
4268
4269     case M_BGTL_I:
4270       likely = 1;
4271     case M_BGT_I:
4272       /* check for > max integer */
4273       maxnum = 0x7fffffff;
4274       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4275         {
4276           maxnum <<= 16;
4277           maxnum |= 0xffff;
4278           maxnum <<= 16;
4279           maxnum |= 0xffff;
4280         }
4281       if (imm_expr.X_op == O_constant
4282           && imm_expr.X_add_number >= maxnum
4283           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4284         {
4285         do_false:
4286           /* result is always false */
4287           if (! likely)
4288             {
4289               if (warn_nops)
4290                 as_warn (_("Branch %s is always false (nop)"),
4291                          ip->insn_mo->name);
4292               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4293                            "", 0);
4294             }
4295           else
4296             {
4297               if (warn_nops)
4298                 as_warn (_("Branch likely %s is always false"),
4299                          ip->insn_mo->name);
4300               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4301                            "s,t,p", 0, 0);
4302             }
4303           return;
4304         }
4305       if (imm_expr.X_op != O_constant)
4306         as_bad (_("Unsupported large constant"));
4307       ++imm_expr.X_add_number;
4308       /* FALLTHROUGH */
4309     case M_BGE_I:
4310     case M_BGEL_I:
4311       if (mask == M_BGEL_I)
4312         likely = 1;
4313       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4314         {
4315           macro_build ((char *) NULL, &icnt, &offset_expr,
4316                        likely ? "bgezl" : "bgez", "s,p", sreg);
4317           return;
4318         }
4319       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4320         {
4321           macro_build ((char *) NULL, &icnt, &offset_expr,
4322                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4323           return;
4324         }
4325       maxnum = 0x7fffffff;
4326       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4327         {
4328           maxnum <<= 16;
4329           maxnum |= 0xffff;
4330           maxnum <<= 16;
4331           maxnum |= 0xffff;
4332         }
4333       maxnum = - maxnum - 1;
4334       if (imm_expr.X_op == O_constant
4335           && imm_expr.X_add_number <= maxnum
4336           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4337         {
4338         do_true:
4339           /* result is always true */
4340           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4341           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4342           return;
4343         }
4344       set_at (&icnt, sreg, 0);
4345       macro_build ((char *) NULL, &icnt, &offset_expr,
4346                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4347       break;
4348
4349     case M_BGEUL:
4350       likely = 1;
4351     case M_BGEU:
4352       if (treg == 0)
4353         goto do_true;
4354       if (sreg == 0)
4355         {
4356           macro_build ((char *) NULL, &icnt, &offset_expr,
4357                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4358           return;
4359         }
4360       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4361                    "d,v,t", AT, sreg, treg);
4362       macro_build ((char *) NULL, &icnt, &offset_expr,
4363                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4364       break;
4365
4366     case M_BGTUL_I:
4367       likely = 1;
4368     case M_BGTU_I:
4369       if (sreg == 0
4370           || (HAVE_32BIT_GPRS
4371               && imm_expr.X_op == O_constant
4372               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4373         goto do_false;
4374       if (imm_expr.X_op != O_constant)
4375         as_bad (_("Unsupported large constant"));
4376       ++imm_expr.X_add_number;
4377       /* FALLTHROUGH */
4378     case M_BGEU_I:
4379     case M_BGEUL_I:
4380       if (mask == M_BGEUL_I)
4381         likely = 1;
4382       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4383         goto do_true;
4384       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4385         {
4386           macro_build ((char *) NULL, &icnt, &offset_expr,
4387                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4388           return;
4389         }
4390       set_at (&icnt, sreg, 1);
4391       macro_build ((char *) NULL, &icnt, &offset_expr,
4392                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4393       break;
4394
4395     case M_BGTL:
4396       likely = 1;
4397     case M_BGT:
4398       if (treg == 0)
4399         {
4400           macro_build ((char *) NULL, &icnt, &offset_expr,
4401                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4402           return;
4403         }
4404       if (sreg == 0)
4405         {
4406           macro_build ((char *) NULL, &icnt, &offset_expr,
4407                        likely ? "bltzl" : "bltz", "s,p", treg);
4408           return;
4409         }
4410       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4411                    AT, treg, sreg);
4412       macro_build ((char *) NULL, &icnt, &offset_expr,
4413                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4414       break;
4415
4416     case M_BGTUL:
4417       likely = 1;
4418     case M_BGTU:
4419       if (treg == 0)
4420         {
4421           macro_build ((char *) NULL, &icnt, &offset_expr,
4422                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4423           return;
4424         }
4425       if (sreg == 0)
4426         goto do_false;
4427       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4428                    "d,v,t", AT, treg, sreg);
4429       macro_build ((char *) NULL, &icnt, &offset_expr,
4430                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4431       break;
4432
4433     case M_BLEL:
4434       likely = 1;
4435     case M_BLE:
4436       if (treg == 0)
4437         {
4438           macro_build ((char *) NULL, &icnt, &offset_expr,
4439                        likely ? "blezl" : "blez", "s,p", sreg);
4440           return;
4441         }
4442       if (sreg == 0)
4443         {
4444           macro_build ((char *) NULL, &icnt, &offset_expr,
4445                        likely ? "bgezl" : "bgez", "s,p", treg);
4446           return;
4447         }
4448       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4449                    AT, treg, sreg);
4450       macro_build ((char *) NULL, &icnt, &offset_expr,
4451                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4452       break;
4453
4454     case M_BLEL_I:
4455       likely = 1;
4456     case M_BLE_I:
4457       maxnum = 0x7fffffff;
4458       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4459         {
4460           maxnum <<= 16;
4461           maxnum |= 0xffff;
4462           maxnum <<= 16;
4463           maxnum |= 0xffff;
4464         }
4465       if (imm_expr.X_op == O_constant
4466           && imm_expr.X_add_number >= maxnum
4467           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4468         goto do_true;
4469       if (imm_expr.X_op != O_constant)
4470         as_bad (_("Unsupported large constant"));
4471       ++imm_expr.X_add_number;
4472       /* FALLTHROUGH */
4473     case M_BLT_I:
4474     case M_BLTL_I:
4475       if (mask == M_BLTL_I)
4476         likely = 1;
4477       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4478         {
4479           macro_build ((char *) NULL, &icnt, &offset_expr,
4480                        likely ? "bltzl" : "bltz", "s,p", sreg);
4481           return;
4482         }
4483       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4484         {
4485           macro_build ((char *) NULL, &icnt, &offset_expr,
4486                        likely ? "blezl" : "blez", "s,p", sreg);
4487           return;
4488         }
4489       set_at (&icnt, sreg, 0);
4490       macro_build ((char *) NULL, &icnt, &offset_expr,
4491                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4492       break;
4493
4494     case M_BLEUL:
4495       likely = 1;
4496     case M_BLEU:
4497       if (treg == 0)
4498         {
4499           macro_build ((char *) NULL, &icnt, &offset_expr,
4500                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4501           return;
4502         }
4503       if (sreg == 0)
4504         goto do_true;
4505       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4506                    "d,v,t", AT, treg, sreg);
4507       macro_build ((char *) NULL, &icnt, &offset_expr,
4508                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4509       break;
4510
4511     case M_BLEUL_I:
4512       likely = 1;
4513     case M_BLEU_I:
4514       if (sreg == 0
4515           || (HAVE_32BIT_GPRS
4516               && imm_expr.X_op == O_constant
4517               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4518         goto do_true;
4519       if (imm_expr.X_op != O_constant)
4520         as_bad (_("Unsupported large constant"));
4521       ++imm_expr.X_add_number;
4522       /* FALLTHROUGH */
4523     case M_BLTU_I:
4524     case M_BLTUL_I:
4525       if (mask == M_BLTUL_I)
4526         likely = 1;
4527       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4528         goto do_false;
4529       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4530         {
4531           macro_build ((char *) NULL, &icnt, &offset_expr,
4532                        likely ? "beql" : "beq",
4533                        "s,t,p", sreg, 0);
4534           return;
4535         }
4536       set_at (&icnt, sreg, 1);
4537       macro_build ((char *) NULL, &icnt, &offset_expr,
4538                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4539       break;
4540
4541     case M_BLTL:
4542       likely = 1;
4543     case M_BLT:
4544       if (treg == 0)
4545         {
4546           macro_build ((char *) NULL, &icnt, &offset_expr,
4547                        likely ? "bltzl" : "bltz", "s,p", sreg);
4548           return;
4549         }
4550       if (sreg == 0)
4551         {
4552           macro_build ((char *) NULL, &icnt, &offset_expr,
4553                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4554           return;
4555         }
4556       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4557                    AT, sreg, treg);
4558       macro_build ((char *) NULL, &icnt, &offset_expr,
4559                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4560       break;
4561
4562     case M_BLTUL:
4563       likely = 1;
4564     case M_BLTU:
4565       if (treg == 0)
4566         goto do_false;
4567       if (sreg == 0)
4568         {
4569           macro_build ((char *) NULL, &icnt, &offset_expr,
4570                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4571           return;
4572         }
4573       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4574                    "d,v,t", AT, sreg,
4575                    treg);
4576       macro_build ((char *) NULL, &icnt, &offset_expr,
4577                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4578       break;
4579
4580     case M_DDIV_3:
4581       dbl = 1;
4582     case M_DIV_3:
4583       s = "mflo";
4584       goto do_div3;
4585     case M_DREM_3:
4586       dbl = 1;
4587     case M_REM_3:
4588       s = "mfhi";
4589     do_div3:
4590       if (treg == 0)
4591         {
4592           as_warn (_("Divide by zero."));
4593           if (mips_trap)
4594             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4595                          "s,t,q", 0, 0, 7);
4596           else
4597             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4598                          "c", 7);
4599           return;
4600         }
4601
4602       mips_emit_delays (TRUE);
4603       ++mips_opts.noreorder;
4604       mips_any_noreorder = 1;
4605       if (mips_trap)
4606         {
4607           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4608                        "s,t,q", treg, 0, 7);
4609           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4610                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4611         }
4612       else
4613         {
4614           expr1.X_add_number = 8;
4615           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4616           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4617                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4618           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4619                        "c", 7);
4620         }
4621       expr1.X_add_number = -1;
4622       macro_build ((char *) NULL, &icnt, &expr1,
4623                    dbl ? "daddiu" : "addiu",
4624                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4625       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4626       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4627       if (dbl)
4628         {
4629           expr1.X_add_number = 1;
4630           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4631                        (int) BFD_RELOC_LO16);
4632           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4633                        "d,w,<", AT, AT, 31);
4634         }
4635       else
4636         {
4637           expr1.X_add_number = 0x80000000;
4638           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4639                        (int) BFD_RELOC_HI16);
4640         }
4641       if (mips_trap)
4642         {
4643           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4644                        "s,t,q", sreg, AT, 6);
4645           /* We want to close the noreorder block as soon as possible, so
4646              that later insns are available for delay slot filling.  */
4647           --mips_opts.noreorder;
4648         }
4649       else
4650         {
4651           expr1.X_add_number = 8;
4652           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4653           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4654                        0);
4655
4656           /* We want to close the noreorder block as soon as possible, so
4657              that later insns are available for delay slot filling.  */
4658           --mips_opts.noreorder;
4659
4660           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4661                        "c", 6);
4662         }
4663       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4664       break;
4665
4666     case M_DIV_3I:
4667       s = "div";
4668       s2 = "mflo";
4669       goto do_divi;
4670     case M_DIVU_3I:
4671       s = "divu";
4672       s2 = "mflo";
4673       goto do_divi;
4674     case M_REM_3I:
4675       s = "div";
4676       s2 = "mfhi";
4677       goto do_divi;
4678     case M_REMU_3I:
4679       s = "divu";
4680       s2 = "mfhi";
4681       goto do_divi;
4682     case M_DDIV_3I:
4683       dbl = 1;
4684       s = "ddiv";
4685       s2 = "mflo";
4686       goto do_divi;
4687     case M_DDIVU_3I:
4688       dbl = 1;
4689       s = "ddivu";
4690       s2 = "mflo";
4691       goto do_divi;
4692     case M_DREM_3I:
4693       dbl = 1;
4694       s = "ddiv";
4695       s2 = "mfhi";
4696       goto do_divi;
4697     case M_DREMU_3I:
4698       dbl = 1;
4699       s = "ddivu";
4700       s2 = "mfhi";
4701     do_divi:
4702       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4703         {
4704           as_warn (_("Divide by zero."));
4705           if (mips_trap)
4706             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4707                          "s,t,q", 0, 0, 7);
4708           else
4709             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4710                          "c", 7);
4711           return;
4712         }
4713       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4714         {
4715           if (strcmp (s2, "mflo") == 0)
4716             move_register (&icnt, dreg, sreg);
4717           else
4718             move_register (&icnt, dreg, 0);
4719           return;
4720         }
4721       if (imm_expr.X_op == O_constant
4722           && imm_expr.X_add_number == -1
4723           && s[strlen (s) - 1] != 'u')
4724         {
4725           if (strcmp (s2, "mflo") == 0)
4726             {
4727               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4728                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4729             }
4730           else
4731             move_register (&icnt, dreg, 0);
4732           return;
4733         }
4734
4735       load_register (&icnt, AT, &imm_expr, dbl);
4736       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4737                    sreg, AT);
4738       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4739       break;
4740
4741     case M_DIVU_3:
4742       s = "divu";
4743       s2 = "mflo";
4744       goto do_divu3;
4745     case M_REMU_3:
4746       s = "divu";
4747       s2 = "mfhi";
4748       goto do_divu3;
4749     case M_DDIVU_3:
4750       s = "ddivu";
4751       s2 = "mflo";
4752       goto do_divu3;
4753     case M_DREMU_3:
4754       s = "ddivu";
4755       s2 = "mfhi";
4756     do_divu3:
4757       mips_emit_delays (TRUE);
4758       ++mips_opts.noreorder;
4759       mips_any_noreorder = 1;
4760       if (mips_trap)
4761         {
4762           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4763                        "s,t,q", treg, 0, 7);
4764           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4765                        sreg, treg);
4766           /* We want to close the noreorder block as soon as possible, so
4767              that later insns are available for delay slot filling.  */
4768           --mips_opts.noreorder;
4769         }
4770       else
4771         {
4772           expr1.X_add_number = 8;
4773           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4774           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4775                        sreg, treg);
4776
4777           /* We want to close the noreorder block as soon as possible, so
4778              that later insns are available for delay slot filling.  */
4779           --mips_opts.noreorder;
4780           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4781                        "c", 7);
4782         }
4783       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4784       return;
4785
4786     case M_DLA_AB:
4787       dbl = 1;
4788     case M_LA_AB:
4789       /* Load the address of a symbol into a register.  If breg is not
4790          zero, we then add a base register to it.  */
4791
4792       if (dbl && HAVE_32BIT_GPRS)
4793         as_warn (_("dla used to load 32-bit register"));
4794
4795       if (! dbl && HAVE_64BIT_OBJECTS)
4796         as_warn (_("la used to load 64-bit address"));
4797
4798       if (offset_expr.X_op == O_constant
4799           && offset_expr.X_add_number >= -0x8000
4800           && offset_expr.X_add_number < 0x8000)
4801         {
4802           macro_build ((char *) NULL, &icnt, &offset_expr,
4803                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4804                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4805           return;
4806         }
4807
4808       if (treg == breg)
4809         {
4810           tempreg = AT;
4811           used_at = 1;
4812         }
4813       else
4814         {
4815           tempreg = treg;
4816           used_at = 0;
4817         }
4818
4819       /* When generating embedded PIC code, we permit expressions of
4820          the form
4821            la   $treg,foo-bar
4822            la   $treg,foo-bar($breg)
4823          where bar is an address in the current section.  These are used
4824          when getting the addresses of functions.  We don't permit
4825          X_add_number to be non-zero, because if the symbol is
4826          external the relaxing code needs to know that any addend is
4827          purely the offset to X_op_symbol.  */
4828       if (mips_pic == EMBEDDED_PIC
4829           && offset_expr.X_op == O_subtract
4830           && (symbol_constant_p (offset_expr.X_op_symbol)
4831               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4832               : (symbol_equated_p (offset_expr.X_op_symbol)
4833                  && (S_GET_SEGMENT
4834                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4835                       ->X_add_symbol)
4836                      == now_seg)))
4837           && (offset_expr.X_add_number == 0
4838               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4839         {
4840           if (breg == 0)
4841             {
4842               tempreg = treg;
4843               used_at = 0;
4844               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4845                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4846             }
4847           else
4848             {
4849               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4850                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4851               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4852                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4853                            "d,v,t", tempreg, tempreg, breg);
4854             }
4855           macro_build ((char *) NULL, &icnt, &offset_expr,
4856                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4857                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4858           if (! used_at)
4859             return;
4860           break;
4861         }
4862
4863       if (offset_expr.X_op != O_symbol
4864           && offset_expr.X_op != O_constant)
4865         {
4866           as_bad (_("expression too complex"));
4867           offset_expr.X_op = O_constant;
4868         }
4869
4870       if (offset_expr.X_op == O_constant)
4871         load_register (&icnt, tempreg, &offset_expr,
4872                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4873                         ? (dbl || HAVE_64BIT_ADDRESSES)
4874                         : HAVE_64BIT_ADDRESSES));
4875       else if (mips_pic == NO_PIC)
4876         {
4877           /* If this is a reference to a GP relative symbol, we want
4878                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4879              Otherwise we want
4880                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4881                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4882              If we have a constant, we need two instructions anyhow,
4883              so we may as well always use the latter form.
4884
4885             With 64bit address space and a usable $at we want
4886               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4887               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4888               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4889               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4890               dsll32    $tempreg,0
4891               daddu     $tempreg,$tempreg,$at
4892
4893             If $at is already in use, we use a path which is suboptimal
4894             on superscalar processors.
4895               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4896               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4897               dsll      $tempreg,16
4898               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4899               dsll      $tempreg,16
4900               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4901           */
4902           char *p = NULL;
4903           if (HAVE_64BIT_ADDRESSES)
4904             {
4905               /* We don't do GP optimization for now because RELAX_ENCODE can't
4906                  hold the data for such large chunks.  */
4907
4908               if (used_at == 0 && ! mips_opts.noat)
4909                 {
4910                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4911                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4912                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4913                                AT, (int) BFD_RELOC_HI16_S);
4914                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4915                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4916                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4917                                AT, AT, (int) BFD_RELOC_LO16);
4918                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4919                                "d,w,<", tempreg, tempreg, 0);
4920                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4921                                "d,v,t", tempreg, tempreg, AT);
4922                   used_at = 1;
4923                 }
4924               else
4925                 {
4926                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4927                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4928                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4929                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4930                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4931                                tempreg, tempreg, 16);
4932                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4933                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4934                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4935                                tempreg, tempreg, 16);
4936                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4937                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4938                 }
4939             }
4940           else
4941             {
4942               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4943                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4944                 {
4945                   frag_grow (20);
4946                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4947                                "t,r,j", tempreg, mips_gp_register,
4948                                (int) BFD_RELOC_GPREL16);
4949                   p = frag_var (rs_machine_dependent, 8, 0,
4950                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4951                                               mips_opts.warn_about_macros),
4952                                 offset_expr.X_add_symbol, 0, NULL);
4953                 }
4954               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4955               if (p != NULL)
4956                 p += 4;
4957               macro_build (p, &icnt, &offset_expr, "addiu",
4958                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4959             }
4960         }
4961       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4962         {
4963           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4964
4965           /* If this is a reference to an external symbol, and there
4966              is no constant, we want
4967                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4968              or if tempreg is PIC_CALL_REG
4969                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4970              For a local symbol, we want
4971                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4972                nop
4973                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4974
4975              If we have a small constant, and this is a reference to
4976              an external symbol, we want
4977                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4978                nop
4979                addiu    $tempreg,$tempreg,<constant>
4980              For a local symbol, we want the same instruction
4981              sequence, but we output a BFD_RELOC_LO16 reloc on the
4982              addiu instruction.
4983
4984              If we have a large constant, and this is a reference to
4985              an external symbol, we want
4986                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4987                lui      $at,<hiconstant>
4988                addiu    $at,$at,<loconstant>
4989                addu     $tempreg,$tempreg,$at
4990              For a local symbol, we want the same instruction
4991              sequence, but we output a BFD_RELOC_LO16 reloc on the
4992              addiu instruction.
4993
4994              For NewABI, we want for local or external data addresses
4995                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4996              For a local function symbol, we want
4997                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4998                nop
4999                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5000            */
5001
5002           expr1.X_add_number = offset_expr.X_add_number;
5003           offset_expr.X_add_number = 0;
5004           frag_grow (32);
5005           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5006             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5007           else if (HAVE_NEWABI)
5008             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5009           macro_build ((char *) NULL, &icnt, &offset_expr,
5010                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5011                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
5012           if (expr1.X_add_number == 0)
5013             {
5014               int off;
5015               char *p;
5016
5017               if (breg == 0)
5018                 off = 0;
5019               else
5020                 {
5021                   /* We're going to put in an addu instruction using
5022                      tempreg, so we may as well insert the nop right
5023                      now.  */
5024                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5025                                "nop", "");
5026                   off = 4;
5027                 }
5028               p = frag_var (rs_machine_dependent, 8 - off, 0,
5029                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5030                                           (breg == 0
5031                                            ? mips_opts.warn_about_macros
5032                                            : 0)),
5033                             offset_expr.X_add_symbol, 0, NULL);
5034               if (breg == 0)
5035                 {
5036                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5037                   p += 4;
5038                 }
5039               macro_build (p, &icnt, &expr1,
5040                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5041                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5042               /* FIXME: If breg == 0, and the next instruction uses
5043                  $tempreg, then if this variant case is used an extra
5044                  nop will be generated.  */
5045             }
5046           else if (expr1.X_add_number >= -0x8000
5047                    && expr1.X_add_number < 0x8000)
5048             {
5049               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5050                            "nop", "");
5051               macro_build ((char *) NULL, &icnt, &expr1,
5052                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5053                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5054               frag_var (rs_machine_dependent, 0, 0,
5055                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5056                         offset_expr.X_add_symbol, 0, NULL);
5057             }
5058           else
5059             {
5060               int off1;
5061
5062               /* If we are going to add in a base register, and the
5063                  target register and the base register are the same,
5064                  then we are using AT as a temporary register.  Since
5065                  we want to load the constant into AT, we add our
5066                  current AT (from the global offset table) and the
5067                  register into the register now, and pretend we were
5068                  not using a base register.  */
5069               if (breg != treg)
5070                 off1 = 0;
5071               else
5072                 {
5073                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5074                                "nop", "");
5075                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5076                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5077                                "d,v,t", treg, AT, breg);
5078                   breg = 0;
5079                   tempreg = treg;
5080                   off1 = -8;
5081                 }
5082
5083               /* Set mips_optimize around the lui instruction to avoid
5084                  inserting an unnecessary nop after the lw.  */
5085               hold_mips_optimize = mips_optimize;
5086               mips_optimize = 2;
5087               macro_build_lui (NULL, &icnt, &expr1, AT);
5088               mips_optimize = hold_mips_optimize;
5089
5090               macro_build ((char *) NULL, &icnt, &expr1,
5091                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5092                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5093               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5094                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5095                            "d,v,t", tempreg, tempreg, AT);
5096               frag_var (rs_machine_dependent, 0, 0,
5097                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5098                         offset_expr.X_add_symbol, 0, NULL);
5099               used_at = 1;
5100             }
5101         }
5102       else if (mips_pic == SVR4_PIC)
5103         {
5104           int gpdel;
5105           char *p;
5106           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5107           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5108           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5109
5110           /* This is the large GOT case.  If this is a reference to an
5111              external symbol, and there is no constant, we want
5112                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5113                addu     $tempreg,$tempreg,$gp
5114                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5115              or if tempreg is PIC_CALL_REG
5116                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5117                addu     $tempreg,$tempreg,$gp
5118                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5119              For a local symbol, we want
5120                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5121                nop
5122                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5123
5124              If we have a small constant, and this is a reference to
5125              an external symbol, we want
5126                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5127                addu     $tempreg,$tempreg,$gp
5128                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5129                nop
5130                addiu    $tempreg,$tempreg,<constant>
5131              For a local symbol, we want
5132                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5133                nop
5134                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5135
5136              If we have a large constant, and this is a reference to
5137              an external symbol, we want
5138                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5139                addu     $tempreg,$tempreg,$gp
5140                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5141                lui      $at,<hiconstant>
5142                addiu    $at,$at,<loconstant>
5143                addu     $tempreg,$tempreg,$at
5144              For a local symbol, we want
5145                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5146                lui      $at,<hiconstant>
5147                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5148                addu     $tempreg,$tempreg,$at
5149
5150              For NewABI, we want for local data addresses
5151               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5152            */
5153
5154           expr1.X_add_number = offset_expr.X_add_number;
5155           offset_expr.X_add_number = 0;
5156           frag_grow (52);
5157           if (reg_needs_delay (mips_gp_register))
5158             gpdel = 4;
5159           else
5160             gpdel = 0;
5161           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5162             {
5163               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5164               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5165             }
5166           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5167                        tempreg, lui_reloc_type);
5168           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5169                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5170                        "d,v,t", tempreg, tempreg, mips_gp_register);
5171           macro_build ((char *) NULL, &icnt, &offset_expr,
5172                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5173                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5174           if (expr1.X_add_number == 0)
5175             {
5176               int off;
5177
5178               if (breg == 0)
5179                 off = 0;
5180               else
5181                 {
5182                   /* We're going to put in an addu instruction using
5183                      tempreg, so we may as well insert the nop right
5184                      now.  */
5185                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5186                                "nop", "");
5187                   off = 4;
5188                 }
5189
5190               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5191                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5192                                           8 + gpdel, 0,
5193                                           (breg == 0
5194                                            ? mips_opts.warn_about_macros
5195                                            : 0)),
5196                             offset_expr.X_add_symbol, 0, NULL);
5197             }
5198           else if (expr1.X_add_number >= -0x8000
5199                    && expr1.X_add_number < 0x8000)
5200             {
5201               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5202                            "nop", "");
5203               macro_build ((char *) NULL, &icnt, &expr1,
5204                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5205                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5206
5207               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5208                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5209                                           (breg == 0
5210                                            ? mips_opts.warn_about_macros
5211                                            : 0)),
5212                             offset_expr.X_add_symbol, 0, NULL);
5213             }
5214           else
5215             {
5216               int adj, dreg;
5217
5218               /* If we are going to add in a base register, and the
5219                  target register and the base register are the same,
5220                  then we are using AT as a temporary register.  Since
5221                  we want to load the constant into AT, we add our
5222                  current AT (from the global offset table) and the
5223                  register into the register now, and pretend we were
5224                  not using a base register.  */
5225               if (breg != treg)
5226                 {
5227                   adj = 0;
5228                   dreg = tempreg;
5229                 }
5230               else
5231                 {
5232                   assert (tempreg == AT);
5233                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5234                                "nop", "");
5235                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5236                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5237                                "d,v,t", treg, AT, breg);
5238                   dreg = treg;
5239                   adj = 8;
5240                 }
5241
5242               /* Set mips_optimize around the lui instruction to avoid
5243                  inserting an unnecessary nop after the lw.  */
5244               hold_mips_optimize = mips_optimize;
5245               mips_optimize = 2;
5246               macro_build_lui (NULL, &icnt, &expr1, AT);
5247               mips_optimize = hold_mips_optimize;
5248
5249               macro_build ((char *) NULL, &icnt, &expr1,
5250                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5251                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5252               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5253                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5254                            "d,v,t", dreg, dreg, AT);
5255
5256               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5257                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5258                                           8 + gpdel, 0,
5259                                           (breg == 0
5260                                            ? mips_opts.warn_about_macros
5261                                            : 0)),
5262                             offset_expr.X_add_symbol, 0, NULL);
5263
5264               used_at = 1;
5265             }
5266
5267           if (gpdel > 0)
5268             {
5269               /* This is needed because this instruction uses $gp, but
5270                  the first instruction on the main stream does not.  */
5271               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5272               p += 4;
5273             }
5274
5275           if (HAVE_NEWABI)
5276             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5277           macro_build (p, &icnt, &offset_expr,
5278                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5279                        "t,o(b)", tempreg,
5280                        local_reloc_type,
5281                        mips_gp_register);
5282           p += 4;
5283           if (expr1.X_add_number == 0 && HAVE_NEWABI)
5284             {
5285               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5286             }
5287          else
5288            if (expr1.X_add_number >= -0x8000
5289               && expr1.X_add_number < 0x8000)
5290             {
5291               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5292               p += 4;
5293               macro_build (p, &icnt, &expr1,
5294                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5295                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5296               /* FIXME: If add_number is 0, and there was no base
5297                  register, the external symbol case ended with a load,
5298                  so if the symbol turns out to not be external, and
5299                  the next instruction uses tempreg, an unnecessary nop
5300                  will be inserted.  */
5301             }
5302           else
5303             {
5304               if (breg == treg)
5305                 {
5306                   /* We must add in the base register now, as in the
5307                      external symbol case.  */
5308                   assert (tempreg == AT);
5309                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5310                   p += 4;
5311                   macro_build (p, &icnt, (expressionS *) NULL,
5312                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5313                                "d,v,t", treg, AT, breg);
5314                   p += 4;
5315                   tempreg = treg;
5316                   /* We set breg to 0 because we have arranged to add
5317                      it in in both cases.  */
5318                   breg = 0;
5319                 }
5320
5321               macro_build_lui (p, &icnt, &expr1, AT);
5322               p += 4;
5323               macro_build (p, &icnt, &expr1,
5324                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5325                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5326               p += 4;
5327               macro_build (p, &icnt, (expressionS *) NULL,
5328                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5329                            "d,v,t", tempreg, tempreg, AT);
5330               p += 4;
5331             }
5332         }
5333       else if (mips_pic == EMBEDDED_PIC)
5334         {
5335           /* We use
5336                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5337              */
5338           macro_build ((char *) NULL, &icnt, &offset_expr,
5339                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5340                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5341         }
5342       else
5343         abort ();
5344
5345       if (breg != 0)
5346         {
5347           char *s;
5348
5349           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5350             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5351           else
5352             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5353
5354           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5355                        "d,v,t", treg, tempreg, breg);
5356         }
5357
5358       if (! used_at)
5359         return;
5360
5361       break;
5362
5363     case M_J_A:
5364       /* The j instruction may not be used in PIC code, since it
5365          requires an absolute address.  We convert it to a b
5366          instruction.  */
5367       if (mips_pic == NO_PIC)
5368         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5369       else
5370         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5371       return;
5372
5373       /* The jal instructions must be handled as macros because when
5374          generating PIC code they expand to multi-instruction
5375          sequences.  Normally they are simple instructions.  */
5376     case M_JAL_1:
5377       dreg = RA;
5378       /* Fall through.  */
5379     case M_JAL_2:
5380       if (mips_pic == NO_PIC
5381           || mips_pic == EMBEDDED_PIC)
5382         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5383                      "d,s", dreg, sreg);
5384       else if (mips_pic == SVR4_PIC)
5385         {
5386           if (sreg != PIC_CALL_REG)
5387             as_warn (_("MIPS PIC call to register other than $25"));
5388
5389           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5390                        "d,s", dreg, sreg);
5391           if (! HAVE_NEWABI)
5392             {
5393               if (mips_cprestore_offset < 0)
5394                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5395               else
5396                 {
5397                   if (! mips_frame_reg_valid)
5398                     {
5399                       as_warn (_("No .frame pseudo-op used in PIC code"));
5400                       /* Quiet this warning.  */
5401                       mips_frame_reg_valid = 1;
5402                     }
5403                   if (! mips_cprestore_valid)
5404                     {
5405                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5406                       /* Quiet this warning.  */
5407                       mips_cprestore_valid = 1;
5408                     }
5409                   expr1.X_add_number = mips_cprestore_offset;
5410                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5411                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5412                                                 mips_gp_register, mips_frame_reg);
5413                 }
5414             }
5415         }
5416       else
5417         abort ();
5418
5419       return;
5420
5421     case M_JAL_A:
5422       if (mips_pic == NO_PIC)
5423         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5424       else if (mips_pic == SVR4_PIC)
5425         {
5426           char *p;
5427
5428           /* If this is a reference to an external symbol, and we are
5429              using a small GOT, we want
5430                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5431                nop
5432                jalr     $ra,$25
5433                nop
5434                lw       $gp,cprestore($sp)
5435              The cprestore value is set using the .cprestore
5436              pseudo-op.  If we are using a big GOT, we want
5437                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5438                addu     $25,$25,$gp
5439                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5440                nop
5441                jalr     $ra,$25
5442                nop
5443                lw       $gp,cprestore($sp)
5444              If the symbol is not external, we want
5445                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5446                nop
5447                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5448                jalr     $ra,$25
5449                nop
5450                lw $gp,cprestore($sp)
5451              For NewABI, we want
5452                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5453                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5454            */
5455           if (HAVE_NEWABI)
5456             {
5457               macro_build ((char *) NULL, &icnt, &offset_expr,
5458                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5459                            "t,o(b)", PIC_CALL_REG,
5460                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5461               macro_build_jalr (icnt, &offset_expr);
5462             }
5463           else
5464             {
5465               frag_grow (40);
5466               if (! mips_big_got)
5467                 {
5468                   macro_build ((char *) NULL, &icnt, &offset_expr,
5469                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5470                                "t,o(b)", PIC_CALL_REG,
5471                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5472                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5473                                "nop", "");
5474                   p = frag_var (rs_machine_dependent, 4, 0,
5475                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5476                                 offset_expr.X_add_symbol, 0, NULL);
5477                 }
5478               else
5479                 {
5480                   int gpdel;
5481
5482                   if (reg_needs_delay (mips_gp_register))
5483                     gpdel = 4;
5484                   else
5485                     gpdel = 0;
5486                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5487                                "t,u", PIC_CALL_REG,
5488                                (int) BFD_RELOC_MIPS_CALL_HI16);
5489                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5490                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5491                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5492                                mips_gp_register);
5493                   macro_build ((char *) NULL, &icnt, &offset_expr,
5494                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5495                                "t,o(b)", PIC_CALL_REG,
5496                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5497                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5498                                "nop", "");
5499                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5500                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5501                                               8 + gpdel, 0, 0),
5502                                 offset_expr.X_add_symbol, 0, NULL);
5503                   if (gpdel > 0)
5504                     {
5505                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5506                       p += 4;
5507                     }
5508                   macro_build (p, &icnt, &offset_expr,
5509                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5510                                "t,o(b)", PIC_CALL_REG,
5511                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5512                   p += 4;
5513                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5514                   p += 4;
5515                 }
5516               macro_build (p, &icnt, &offset_expr,
5517                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5518                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5519                            (int) BFD_RELOC_LO16);
5520               macro_build_jalr (icnt, &offset_expr);
5521
5522               if (mips_cprestore_offset < 0)
5523                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5524               else
5525                 {
5526                   if (! mips_frame_reg_valid)
5527                     {
5528                       as_warn (_("No .frame pseudo-op used in PIC code"));
5529                       /* Quiet this warning.  */
5530                       mips_frame_reg_valid = 1;
5531                     }
5532                   if (! mips_cprestore_valid)
5533                     {
5534                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5535                       /* Quiet this warning.  */
5536                       mips_cprestore_valid = 1;
5537                     }
5538                   if (mips_opts.noreorder)
5539                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5540                                  "nop", "");
5541                   expr1.X_add_number = mips_cprestore_offset;
5542                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5543                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5544                                                 mips_gp_register, mips_frame_reg);
5545                 }
5546             }
5547         }
5548       else if (mips_pic == EMBEDDED_PIC)
5549         {
5550           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5551           /* The linker may expand the call to a longer sequence which
5552              uses $at, so we must break rather than return.  */
5553           break;
5554         }
5555       else
5556         abort ();
5557
5558       return;
5559
5560     case M_LB_AB:
5561       s = "lb";
5562       goto ld;
5563     case M_LBU_AB:
5564       s = "lbu";
5565       goto ld;
5566     case M_LH_AB:
5567       s = "lh";
5568       goto ld;
5569     case M_LHU_AB:
5570       s = "lhu";
5571       goto ld;
5572     case M_LW_AB:
5573       s = "lw";
5574       goto ld;
5575     case M_LWC0_AB:
5576       s = "lwc0";
5577       /* Itbl support may require additional care here.  */
5578       coproc = 1;
5579       goto ld;
5580     case M_LWC1_AB:
5581       s = "lwc1";
5582       /* Itbl support may require additional care here.  */
5583       coproc = 1;
5584       goto ld;
5585     case M_LWC2_AB:
5586       s = "lwc2";
5587       /* Itbl support may require additional care here.  */
5588       coproc = 1;
5589       goto ld;
5590     case M_LWC3_AB:
5591       s = "lwc3";
5592       /* Itbl support may require additional care here.  */
5593       coproc = 1;
5594       goto ld;
5595     case M_LWL_AB:
5596       s = "lwl";
5597       lr = 1;
5598       goto ld;
5599     case M_LWR_AB:
5600       s = "lwr";
5601       lr = 1;
5602       goto ld;
5603     case M_LDC1_AB:
5604       if (mips_arch == CPU_R4650)
5605         {
5606           as_bad (_("opcode not supported on this processor"));
5607           return;
5608         }
5609       s = "ldc1";
5610       /* Itbl support may require additional care here.  */
5611       coproc = 1;
5612       goto ld;
5613     case M_LDC2_AB:
5614       s = "ldc2";
5615       /* Itbl support may require additional care here.  */
5616       coproc = 1;
5617       goto ld;
5618     case M_LDC3_AB:
5619       s = "ldc3";
5620       /* Itbl support may require additional care here.  */
5621       coproc = 1;
5622       goto ld;
5623     case M_LDL_AB:
5624       s = "ldl";
5625       lr = 1;
5626       goto ld;
5627     case M_LDR_AB:
5628       s = "ldr";
5629       lr = 1;
5630       goto ld;
5631     case M_LL_AB:
5632       s = "ll";
5633       goto ld;
5634     case M_LLD_AB:
5635       s = "lld";
5636       goto ld;
5637     case M_LWU_AB:
5638       s = "lwu";
5639     ld:
5640       if (breg == treg || coproc || lr)
5641         {
5642           tempreg = AT;
5643           used_at = 1;
5644         }
5645       else
5646         {
5647           tempreg = treg;
5648           used_at = 0;
5649         }
5650       goto ld_st;
5651     case M_SB_AB:
5652       s = "sb";
5653       goto st;
5654     case M_SH_AB:
5655       s = "sh";
5656       goto st;
5657     case M_SW_AB:
5658       s = "sw";
5659       goto st;
5660     case M_SWC0_AB:
5661       s = "swc0";
5662       /* Itbl support may require additional care here.  */
5663       coproc = 1;
5664       goto st;
5665     case M_SWC1_AB:
5666       s = "swc1";
5667       /* Itbl support may require additional care here.  */
5668       coproc = 1;
5669       goto st;
5670     case M_SWC2_AB:
5671       s = "swc2";
5672       /* Itbl support may require additional care here.  */
5673       coproc = 1;
5674       goto st;
5675     case M_SWC3_AB:
5676       s = "swc3";
5677       /* Itbl support may require additional care here.  */
5678       coproc = 1;
5679       goto st;
5680     case M_SWL_AB:
5681       s = "swl";
5682       goto st;
5683     case M_SWR_AB:
5684       s = "swr";
5685       goto st;
5686     case M_SC_AB:
5687       s = "sc";
5688       goto st;
5689     case M_SCD_AB:
5690       s = "scd";
5691       goto st;
5692     case M_SDC1_AB:
5693       if (mips_arch == CPU_R4650)
5694         {
5695           as_bad (_("opcode not supported on this processor"));
5696           return;
5697         }
5698       s = "sdc1";
5699       coproc = 1;
5700       /* Itbl support may require additional care here.  */
5701       goto st;
5702     case M_SDC2_AB:
5703       s = "sdc2";
5704       /* Itbl support may require additional care here.  */
5705       coproc = 1;
5706       goto st;
5707     case M_SDC3_AB:
5708       s = "sdc3";
5709       /* Itbl support may require additional care here.  */
5710       coproc = 1;
5711       goto st;
5712     case M_SDL_AB:
5713       s = "sdl";
5714       goto st;
5715     case M_SDR_AB:
5716       s = "sdr";
5717     st:
5718       tempreg = AT;
5719       used_at = 1;
5720     ld_st:
5721       /* Itbl support may require additional care here.  */
5722       if (mask == M_LWC1_AB
5723           || mask == M_SWC1_AB
5724           || mask == M_LDC1_AB
5725           || mask == M_SDC1_AB
5726           || mask == M_L_DAB
5727           || mask == M_S_DAB)
5728         fmt = "T,o(b)";
5729       else if (coproc)
5730         fmt = "E,o(b)";
5731       else
5732         fmt = "t,o(b)";
5733
5734       /* For embedded PIC, we allow loads where the offset is calculated
5735          by subtracting a symbol in the current segment from an unknown
5736          symbol, relative to a base register, e.g.:
5737                 <op>    $treg, <sym>-<localsym>($breg)
5738          This is used by the compiler for switch statements.  */
5739       if (mips_pic == EMBEDDED_PIC
5740           && offset_expr.X_op == O_subtract
5741           && (symbol_constant_p (offset_expr.X_op_symbol)
5742               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5743               : (symbol_equated_p (offset_expr.X_op_symbol)
5744                  && (S_GET_SEGMENT
5745                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5746                       ->X_add_symbol)
5747                      == now_seg)))
5748           && breg != 0
5749           && (offset_expr.X_add_number == 0
5750               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5751         {
5752           /* For this case, we output the instructions:
5753                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5754                 addiu   $tempreg,$tempreg,$breg
5755                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5756              If the relocation would fit entirely in 16 bits, it would be
5757              nice to emit:
5758                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5759              instead, but that seems quite difficult.  */
5760           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5761                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5762           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5763                        ((bfd_arch_bits_per_address (stdoutput) == 32
5764                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5765                         ? "addu" : "daddu"),
5766                        "d,v,t", tempreg, tempreg, breg);
5767           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5768                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5769           if (! used_at)
5770             return;
5771           break;
5772         }
5773
5774       if (offset_expr.X_op != O_constant
5775           && offset_expr.X_op != O_symbol)
5776         {
5777           as_bad (_("expression too complex"));
5778           offset_expr.X_op = O_constant;
5779         }
5780
5781       /* A constant expression in PIC code can be handled just as it
5782          is in non PIC code.  */
5783       if (mips_pic == NO_PIC
5784           || offset_expr.X_op == O_constant)
5785         {
5786           char *p;
5787
5788           /* If this is a reference to a GP relative symbol, and there
5789              is no base register, we want
5790                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5791              Otherwise, if there is no base register, we want
5792                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5793                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5794              If we have a constant, we need two instructions anyhow,
5795              so we always use the latter form.
5796
5797              If we have a base register, and this is a reference to a
5798              GP relative symbol, we want
5799                addu     $tempreg,$breg,$gp
5800                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5801              Otherwise we want
5802                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5803                addu     $tempreg,$tempreg,$breg
5804                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5805              With a constant we always use the latter case.
5806
5807              With 64bit address space and no base register and $at usable,
5808              we want
5809                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5810                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5811                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5812                dsll32   $tempreg,0
5813                daddu    $tempreg,$at
5814                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5815              If we have a base register, we want
5816                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5817                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5818                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5819                daddu    $at,$breg
5820                dsll32   $tempreg,0
5821                daddu    $tempreg,$at
5822                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5823
5824              Without $at we can't generate the optimal path for superscalar
5825              processors here since this would require two temporary registers.
5826                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5827                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5828                dsll     $tempreg,16
5829                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5830                dsll     $tempreg,16
5831                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5832              If we have a base register, we want
5833                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5834                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5835                dsll     $tempreg,16
5836                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5837                dsll     $tempreg,16
5838                daddu    $tempreg,$tempreg,$breg
5839                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5840
5841              If we have 64-bit addresses, as an optimization, for
5842              addresses which are 32-bit constants (e.g. kseg0/kseg1
5843              addresses) we fall back to the 32-bit address generation
5844              mechanism since it is more efficient.  Note that due to
5845              the signed offset used by memory operations, the 32-bit
5846              range is shifted down by 32768 here.  This code should
5847              probably attempt to generate 64-bit constants more
5848              efficiently in general.
5849            */
5850           if ((offset_expr.X_op != O_constant && HAVE_64BIT_ADDRESSES)
5851               || (offset_expr.X_op == O_constant
5852                   && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)
5853                   && HAVE_64BIT_ADDRESS_CONSTANTS))
5854             {
5855               p = NULL;
5856
5857               /* We don't do GP optimization for now because RELAX_ENCODE can't
5858                  hold the data for such large chunks.  */
5859
5860               if (used_at == 0 && ! mips_opts.noat)
5861                 {
5862                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5863                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5864                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5865                                AT, (int) BFD_RELOC_HI16_S);
5866                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5867                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5868                   if (breg != 0)
5869                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5870                                  "d,v,t", AT, AT, breg);
5871                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5872                                "d,w,<", tempreg, tempreg, 0);
5873                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5874                                "d,v,t", tempreg, tempreg, AT);
5875                   macro_build (p, &icnt, &offset_expr, s,
5876                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5877                   used_at = 1;
5878                 }
5879               else
5880                 {
5881                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5882                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5883                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5884                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5885                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5886                                "d,w,<", tempreg, tempreg, 16);
5887                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5888                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5889                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5890                                "d,w,<", tempreg, tempreg, 16);
5891                   if (breg != 0)
5892                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5893                                  "d,v,t", tempreg, tempreg, breg);
5894                   macro_build (p, &icnt, &offset_expr, s,
5895                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5896                 }
5897
5898               return;
5899             }
5900           else if (offset_expr.X_op == O_constant
5901                    && !HAVE_64BIT_ADDRESS_CONSTANTS
5902                    && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5903             as_bad (_("load/store address overflow (max 32 bits)"));
5904
5905           if (breg == 0)
5906             {
5907               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5908                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5909                 p = NULL;
5910               else
5911                 {
5912                   frag_grow (20);
5913                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5914                                treg, (int) BFD_RELOC_GPREL16,
5915                                mips_gp_register);
5916                   p = frag_var (rs_machine_dependent, 8, 0,
5917                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5918                                               (mips_opts.warn_about_macros
5919                                                || (used_at
5920                                                    && mips_opts.noat))),
5921                                 offset_expr.X_add_symbol, 0, NULL);
5922                   used_at = 0;
5923                 }
5924               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5925               if (p != NULL)
5926                 p += 4;
5927               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5928                            (int) BFD_RELOC_LO16, tempreg);
5929             }
5930           else
5931             {
5932               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5933                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5934                 p = NULL;
5935               else
5936                 {
5937                   frag_grow (28);
5938                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5939                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5940                                "d,v,t", tempreg, breg, mips_gp_register);
5941                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5942                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5943                   p = frag_var (rs_machine_dependent, 12, 0,
5944                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5945                                 offset_expr.X_add_symbol, 0, NULL);
5946                 }
5947               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5948               if (p != NULL)
5949                 p += 4;
5950               macro_build (p, &icnt, (expressionS *) NULL,
5951                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5952                            "d,v,t", tempreg, tempreg, breg);
5953               if (p != NULL)
5954                 p += 4;
5955               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5956                            (int) BFD_RELOC_LO16, tempreg);
5957             }
5958         }
5959       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5960         {
5961           char *p;
5962           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5963
5964           /* If this is a reference to an external symbol, we want
5965                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5966                nop
5967                <op>     $treg,0($tempreg)
5968              Otherwise we want
5969                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5970                nop
5971                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5972                <op>     $treg,0($tempreg)
5973              If we have NewABI, we want
5974                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5975              If there is a base register, we add it to $tempreg before
5976              the <op>.  If there is a constant, we stick it in the
5977              <op> instruction.  We don't handle constants larger than
5978              16 bits, because we have no way to load the upper 16 bits
5979              (actually, we could handle them for the subset of cases
5980              in which we are not using $at).  */
5981           assert (offset_expr.X_op == O_symbol);
5982           expr1.X_add_number = offset_expr.X_add_number;
5983           offset_expr.X_add_number = 0;
5984           if (HAVE_NEWABI)
5985             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5986           if (expr1.X_add_number < -0x8000
5987               || expr1.X_add_number >= 0x8000)
5988             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5989           frag_grow (20);
5990           macro_build ((char *) NULL, &icnt, &offset_expr,
5991                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5992                        (int) lw_reloc_type, mips_gp_register);
5993           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5994           p = frag_var (rs_machine_dependent, 4, 0,
5995                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5996                         offset_expr.X_add_symbol, 0, NULL);
5997           macro_build (p, &icnt, &offset_expr,
5998                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5999                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6000           if (breg != 0)
6001             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6002                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6003                          "d,v,t", tempreg, tempreg, breg);
6004           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6005                        (int) BFD_RELOC_LO16, tempreg);
6006         }
6007       else if (mips_pic == SVR4_PIC)
6008         {
6009           int gpdel;
6010           char *p;
6011
6012           /* If this is a reference to an external symbol, we want
6013                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6014                addu     $tempreg,$tempreg,$gp
6015                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6016                <op>     $treg,0($tempreg)
6017              Otherwise we want
6018                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6019                nop
6020                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6021                <op>     $treg,0($tempreg)
6022              If there is a base register, we add it to $tempreg before
6023              the <op>.  If there is a constant, we stick it in the
6024              <op> instruction.  We don't handle constants larger than
6025              16 bits, because we have no way to load the upper 16 bits
6026              (actually, we could handle them for the subset of cases
6027              in which we are not using $at).
6028
6029              For NewABI, we want
6030                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6031                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
6032                <op>     $treg,0($tempreg)
6033            */
6034           assert (offset_expr.X_op == O_symbol);
6035           expr1.X_add_number = offset_expr.X_add_number;
6036           offset_expr.X_add_number = 0;
6037           if (expr1.X_add_number < -0x8000
6038               || expr1.X_add_number >= 0x8000)
6039             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6040           if (HAVE_NEWABI)
6041             {
6042               macro_build ((char *) NULL, &icnt, &offset_expr,
6043                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6044                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6045                            mips_gp_register);
6046               macro_build ((char *) NULL, &icnt, &offset_expr,
6047                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6048                            "t,r,j", tempreg, tempreg,
6049                            BFD_RELOC_MIPS_GOT_OFST);
6050               if (breg != 0)
6051                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6052                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6053                              "d,v,t", tempreg, tempreg, breg);
6054               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6055                            (int) BFD_RELOC_LO16, tempreg);
6056
6057               if (! used_at)
6058                 return;
6059
6060               break;
6061             }
6062           if (reg_needs_delay (mips_gp_register))
6063             gpdel = 4;
6064           else
6065             gpdel = 0;
6066           frag_grow (36);
6067           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6068                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6069           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6070                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6071                        "d,v,t", tempreg, tempreg, mips_gp_register);
6072           macro_build ((char *) NULL, &icnt, &offset_expr,
6073                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6074                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6075                        tempreg);
6076           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6077                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6078                         offset_expr.X_add_symbol, 0, NULL);
6079           if (gpdel > 0)
6080             {
6081               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6082               p += 4;
6083             }
6084           macro_build (p, &icnt, &offset_expr,
6085                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6086                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6087                        mips_gp_register);
6088           p += 4;
6089           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6090           p += 4;
6091           macro_build (p, &icnt, &offset_expr,
6092                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6093                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6094           if (breg != 0)
6095             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6096                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6097                          "d,v,t", tempreg, tempreg, breg);
6098           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6099                        (int) BFD_RELOC_LO16, tempreg);
6100         }
6101       else if (mips_pic == EMBEDDED_PIC)
6102         {
6103           /* If there is no base register, we want
6104                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6105              If there is a base register, we want
6106                addu     $tempreg,$breg,$gp
6107                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6108              */
6109           assert (offset_expr.X_op == O_symbol);
6110           if (breg == 0)
6111             {
6112               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6113                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6114               used_at = 0;
6115             }
6116           else
6117             {
6118               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6119                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6120                            "d,v,t", tempreg, breg, mips_gp_register);
6121               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6122                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6123             }
6124         }
6125       else
6126         abort ();
6127
6128       if (! used_at)
6129         return;
6130
6131       break;
6132
6133     case M_LI:
6134     case M_LI_S:
6135       load_register (&icnt, treg, &imm_expr, 0);
6136       return;
6137
6138     case M_DLI:
6139       load_register (&icnt, treg, &imm_expr, 1);
6140       return;
6141
6142     case M_LI_SS:
6143       if (imm_expr.X_op == O_constant)
6144         {
6145           load_register (&icnt, AT, &imm_expr, 0);
6146           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6147                        "mtc1", "t,G", AT, treg);
6148           break;
6149         }
6150       else
6151         {
6152           assert (offset_expr.X_op == O_symbol
6153                   && strcmp (segment_name (S_GET_SEGMENT
6154                                            (offset_expr.X_add_symbol)),
6155                              ".lit4") == 0
6156                   && offset_expr.X_add_number == 0);
6157           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6158                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6159           return;
6160         }
6161
6162     case M_LI_D:
6163       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6164          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6165          order 32 bits of the value and the low order 32 bits are either
6166          zero or in OFFSET_EXPR.  */
6167       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6168         {
6169           if (HAVE_64BIT_GPRS)
6170             load_register (&icnt, treg, &imm_expr, 1);
6171           else
6172             {
6173               int hreg, lreg;
6174
6175               if (target_big_endian)
6176                 {
6177                   hreg = treg;
6178                   lreg = treg + 1;
6179                 }
6180               else
6181                 {
6182                   hreg = treg + 1;
6183                   lreg = treg;
6184                 }
6185
6186               if (hreg <= 31)
6187                 load_register (&icnt, hreg, &imm_expr, 0);
6188               if (lreg <= 31)
6189                 {
6190                   if (offset_expr.X_op == O_absent)
6191                     move_register (&icnt, lreg, 0);
6192                   else
6193                     {
6194                       assert (offset_expr.X_op == O_constant);
6195                       load_register (&icnt, lreg, &offset_expr, 0);
6196                     }
6197                 }
6198             }
6199           return;
6200         }
6201
6202       /* We know that sym is in the .rdata section.  First we get the
6203          upper 16 bits of the address.  */
6204       if (mips_pic == NO_PIC)
6205         {
6206           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6207         }
6208       else if (mips_pic == SVR4_PIC)
6209         {
6210           macro_build ((char *) NULL, &icnt, &offset_expr,
6211                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6212                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6213                        mips_gp_register);
6214         }
6215       else if (mips_pic == EMBEDDED_PIC)
6216         {
6217           /* For embedded PIC we pick up the entire address off $gp in
6218              a single instruction.  */
6219           macro_build ((char *) NULL, &icnt, &offset_expr,
6220                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6221                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6222           offset_expr.X_op = O_constant;
6223           offset_expr.X_add_number = 0;
6224         }
6225       else
6226         abort ();
6227
6228       /* Now we load the register(s).  */
6229       if (HAVE_64BIT_GPRS)
6230         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6231                      treg, (int) BFD_RELOC_LO16, AT);
6232       else
6233         {
6234           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6235                        treg, (int) BFD_RELOC_LO16, AT);
6236           if (treg != RA)
6237             {
6238               /* FIXME: How in the world do we deal with the possible
6239                  overflow here?  */
6240               offset_expr.X_add_number += 4;
6241               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6242                            treg + 1, (int) BFD_RELOC_LO16, AT);
6243             }
6244         }
6245
6246       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6247          does not become a variant frag.  */
6248       frag_wane (frag_now);
6249       frag_new (0);
6250
6251       break;
6252
6253     case M_LI_DD:
6254       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6255          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6256          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6257          the value and the low order 32 bits are either zero or in
6258          OFFSET_EXPR.  */
6259       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6260         {
6261           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6262           if (HAVE_64BIT_FPRS)
6263             {
6264               assert (HAVE_64BIT_GPRS);
6265               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6266                            "dmtc1", "t,S", AT, treg);
6267             }
6268           else
6269             {
6270               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6271                            "mtc1", "t,G", AT, treg + 1);
6272               if (offset_expr.X_op == O_absent)
6273                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6274                              "mtc1", "t,G", 0, treg);
6275               else
6276                 {
6277                   assert (offset_expr.X_op == O_constant);
6278                   load_register (&icnt, AT, &offset_expr, 0);
6279                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6280                                "mtc1", "t,G", AT, treg);
6281                 }
6282             }
6283           break;
6284         }
6285
6286       assert (offset_expr.X_op == O_symbol
6287               && offset_expr.X_add_number == 0);
6288       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6289       if (strcmp (s, ".lit8") == 0)
6290         {
6291           if (mips_opts.isa != ISA_MIPS1)
6292             {
6293               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6294                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6295                            mips_gp_register);
6296               return;
6297             }
6298           breg = mips_gp_register;
6299           r = BFD_RELOC_MIPS_LITERAL;
6300           goto dob;
6301         }
6302       else
6303         {
6304           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6305           if (mips_pic == SVR4_PIC)
6306             macro_build ((char *) NULL, &icnt, &offset_expr,
6307                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6308                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6309                          mips_gp_register);
6310           else
6311             {
6312               /* FIXME: This won't work for a 64 bit address.  */
6313               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6314             }
6315
6316           if (mips_opts.isa != ISA_MIPS1)
6317             {
6318               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6319                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6320
6321               /* To avoid confusion in tc_gen_reloc, we must ensure
6322                  that this does not become a variant frag.  */
6323               frag_wane (frag_now);
6324               frag_new (0);
6325
6326               break;
6327             }
6328           breg = AT;
6329           r = BFD_RELOC_LO16;
6330           goto dob;
6331         }
6332
6333     case M_L_DOB:
6334       if (mips_arch == CPU_R4650)
6335         {
6336           as_bad (_("opcode not supported on this processor"));
6337           return;
6338         }
6339       /* Even on a big endian machine $fn comes before $fn+1.  We have
6340          to adjust when loading from memory.  */
6341       r = BFD_RELOC_LO16;
6342     dob:
6343       assert (mips_opts.isa == ISA_MIPS1);
6344       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6345                    target_big_endian ? treg + 1 : treg,
6346                    (int) r, breg);
6347       /* FIXME: A possible overflow which I don't know how to deal
6348          with.  */
6349       offset_expr.X_add_number += 4;
6350       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6351                    target_big_endian ? treg : treg + 1,
6352                    (int) r, breg);
6353
6354       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6355          does not become a variant frag.  */
6356       frag_wane (frag_now);
6357       frag_new (0);
6358
6359       if (breg != AT)
6360         return;
6361       break;
6362
6363     case M_L_DAB:
6364       /*
6365        * The MIPS assembler seems to check for X_add_number not
6366        * being double aligned and generating:
6367        *        lui     at,%hi(foo+1)
6368        *        addu    at,at,v1
6369        *        addiu   at,at,%lo(foo+1)
6370        *        lwc1    f2,0(at)
6371        *        lwc1    f3,4(at)
6372        * But, the resulting address is the same after relocation so why
6373        * generate the extra instruction?
6374        */
6375       if (mips_arch == CPU_R4650)
6376         {
6377           as_bad (_("opcode not supported on this processor"));
6378           return;
6379         }
6380       /* Itbl support may require additional care here.  */
6381       coproc = 1;
6382       if (mips_opts.isa != ISA_MIPS1)
6383         {
6384           s = "ldc1";
6385           goto ld;
6386         }
6387
6388       s = "lwc1";
6389       fmt = "T,o(b)";
6390       goto ldd_std;
6391
6392     case M_S_DAB:
6393       if (mips_arch == CPU_R4650)
6394         {
6395           as_bad (_("opcode not supported on this processor"));
6396           return;
6397         }
6398
6399       if (mips_opts.isa != ISA_MIPS1)
6400         {
6401           s = "sdc1";
6402           goto st;
6403         }
6404
6405       s = "swc1";
6406       fmt = "T,o(b)";
6407       /* Itbl support may require additional care here.  */
6408       coproc = 1;
6409       goto ldd_std;
6410
6411     case M_LD_AB:
6412       if (HAVE_64BIT_GPRS)
6413         {
6414           s = "ld";
6415           goto ld;
6416         }
6417
6418       s = "lw";
6419       fmt = "t,o(b)";
6420       goto ldd_std;
6421
6422     case M_SD_AB:
6423       if (HAVE_64BIT_GPRS)
6424         {
6425           s = "sd";
6426           goto st;
6427         }
6428
6429       s = "sw";
6430       fmt = "t,o(b)";
6431
6432     ldd_std:
6433       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6434          loads for the case of doing a pair of loads to simulate an 'ld'.
6435          This is not currently done by the compiler, and assembly coders
6436          writing embedded-pic code can cope.  */
6437
6438       if (offset_expr.X_op != O_symbol
6439           && offset_expr.X_op != O_constant)
6440         {
6441           as_bad (_("expression too complex"));
6442           offset_expr.X_op = O_constant;
6443         }
6444
6445       /* Even on a big endian machine $fn comes before $fn+1.  We have
6446          to adjust when loading from memory.  We set coproc if we must
6447          load $fn+1 first.  */
6448       /* Itbl support may require additional care here.  */
6449       if (! target_big_endian)
6450         coproc = 0;
6451
6452       if (mips_pic == NO_PIC
6453           || offset_expr.X_op == O_constant)
6454         {
6455           char *p;
6456
6457           /* If this is a reference to a GP relative symbol, we want
6458                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6459                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6460              If we have a base register, we use this
6461                addu     $at,$breg,$gp
6462                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6463                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6464              If this is not a GP relative symbol, we want
6465                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6466                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6467                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6468              If there is a base register, we add it to $at after the
6469              lui instruction.  If there is a constant, we always use
6470              the last case.  */
6471           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6472               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6473             {
6474               p = NULL;
6475               used_at = 1;
6476             }
6477           else
6478             {
6479               int off;
6480
6481               if (breg == 0)
6482                 {
6483                   frag_grow (28);
6484                   tempreg = mips_gp_register;
6485                   off = 0;
6486                   used_at = 0;
6487                 }
6488               else
6489                 {
6490                   frag_grow (36);
6491                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6492                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6493                                "d,v,t", AT, breg, mips_gp_register);
6494                   tempreg = AT;
6495                   off = 4;
6496                   used_at = 1;
6497                 }
6498
6499               /* Itbl support may require additional care here.  */
6500               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6501                            coproc ? treg + 1 : treg,
6502                            (int) BFD_RELOC_GPREL16, tempreg);
6503               offset_expr.X_add_number += 4;
6504
6505               /* Set mips_optimize to 2 to avoid inserting an
6506                  undesired nop.  */
6507               hold_mips_optimize = mips_optimize;
6508               mips_optimize = 2;
6509               /* Itbl support may require additional care here.  */
6510               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6511                            coproc ? treg : treg + 1,
6512                            (int) BFD_RELOC_GPREL16, tempreg);
6513               mips_optimize = hold_mips_optimize;
6514
6515               p = frag_var (rs_machine_dependent, 12 + off, 0,
6516                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6517                                           used_at && mips_opts.noat),
6518                             offset_expr.X_add_symbol, 0, NULL);
6519
6520               /* We just generated two relocs.  When tc_gen_reloc
6521                  handles this case, it will skip the first reloc and
6522                  handle the second.  The second reloc already has an
6523                  extra addend of 4, which we added above.  We must
6524                  subtract it out, and then subtract another 4 to make
6525                  the first reloc come out right.  The second reloc
6526                  will come out right because we are going to add 4 to
6527                  offset_expr when we build its instruction below.
6528
6529                  If we have a symbol, then we don't want to include
6530                  the offset, because it will wind up being included
6531                  when we generate the reloc.  */
6532
6533               if (offset_expr.X_op == O_constant)
6534                 offset_expr.X_add_number -= 8;
6535               else
6536                 {
6537                   offset_expr.X_add_number = -4;
6538                   offset_expr.X_op = O_constant;
6539                 }
6540             }
6541           macro_build_lui (p, &icnt, &offset_expr, AT);
6542           if (p != NULL)
6543             p += 4;
6544           if (breg != 0)
6545             {
6546               macro_build (p, &icnt, (expressionS *) NULL,
6547                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6548                            "d,v,t", AT, breg, AT);
6549               if (p != NULL)
6550                 p += 4;
6551             }
6552           /* Itbl support may require additional care here.  */
6553           macro_build (p, &icnt, &offset_expr, s, fmt,
6554                        coproc ? treg + 1 : treg,
6555                        (int) BFD_RELOC_LO16, AT);
6556           if (p != NULL)
6557             p += 4;
6558           /* FIXME: How do we handle overflow here?  */
6559           offset_expr.X_add_number += 4;
6560           /* Itbl support may require additional care here.  */
6561           macro_build (p, &icnt, &offset_expr, s, fmt,
6562                        coproc ? treg : treg + 1,
6563                        (int) BFD_RELOC_LO16, AT);
6564         }
6565       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6566         {
6567           int off;
6568
6569           /* If this is a reference to an external symbol, we want
6570                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6571                nop
6572                <op>     $treg,0($at)
6573                <op>     $treg+1,4($at)
6574              Otherwise we want
6575                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6576                nop
6577                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6578                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6579              If there is a base register we add it to $at before the
6580              lwc1 instructions.  If there is a constant we include it
6581              in the lwc1 instructions.  */
6582           used_at = 1;
6583           expr1.X_add_number = offset_expr.X_add_number;
6584           offset_expr.X_add_number = 0;
6585           if (expr1.X_add_number < -0x8000
6586               || expr1.X_add_number >= 0x8000 - 4)
6587             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6588           if (breg == 0)
6589             off = 0;
6590           else
6591             off = 4;
6592           frag_grow (24 + off);
6593           macro_build ((char *) NULL, &icnt, &offset_expr,
6594                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6595                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6596           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6597           if (breg != 0)
6598             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6599                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6600                          "d,v,t", AT, breg, AT);
6601           /* Itbl support may require additional care here.  */
6602           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6603                        coproc ? treg + 1 : treg,
6604                        (int) BFD_RELOC_LO16, AT);
6605           expr1.X_add_number += 4;
6606
6607           /* Set mips_optimize to 2 to avoid inserting an undesired
6608              nop.  */
6609           hold_mips_optimize = mips_optimize;
6610           mips_optimize = 2;
6611           /* Itbl support may require additional care here.  */
6612           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6613                        coproc ? treg : treg + 1,
6614                        (int) BFD_RELOC_LO16, AT);
6615           mips_optimize = hold_mips_optimize;
6616
6617           (void) frag_var (rs_machine_dependent, 0, 0,
6618                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6619                            offset_expr.X_add_symbol, 0, NULL);
6620         }
6621       else if (mips_pic == SVR4_PIC)
6622         {
6623           int gpdel, off;
6624           char *p;
6625
6626           /* If this is a reference to an external symbol, we want
6627                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6628                addu     $at,$at,$gp
6629                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6630                nop
6631                <op>     $treg,0($at)
6632                <op>     $treg+1,4($at)
6633              Otherwise we want
6634                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6635                nop
6636                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6637                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6638              If there is a base register we add it to $at before the
6639              lwc1 instructions.  If there is a constant we include it
6640              in the lwc1 instructions.  */
6641           used_at = 1;
6642           expr1.X_add_number = offset_expr.X_add_number;
6643           offset_expr.X_add_number = 0;
6644           if (expr1.X_add_number < -0x8000
6645               || expr1.X_add_number >= 0x8000 - 4)
6646             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6647           if (reg_needs_delay (mips_gp_register))
6648             gpdel = 4;
6649           else
6650             gpdel = 0;
6651           if (breg == 0)
6652             off = 0;
6653           else
6654             off = 4;
6655           frag_grow (56);
6656           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6657                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6658           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6659                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6660                        "d,v,t", AT, AT, mips_gp_register);
6661           macro_build ((char *) NULL, &icnt, &offset_expr,
6662                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6663                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6664           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6665           if (breg != 0)
6666             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6667                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6668                          "d,v,t", AT, breg, AT);
6669           /* Itbl support may require additional care here.  */
6670           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6671                        coproc ? treg + 1 : treg,
6672                        (int) BFD_RELOC_LO16, AT);
6673           expr1.X_add_number += 4;
6674
6675           /* Set mips_optimize to 2 to avoid inserting an undesired
6676              nop.  */
6677           hold_mips_optimize = mips_optimize;
6678           mips_optimize = 2;
6679           /* Itbl support may require additional care here.  */
6680           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6681                        coproc ? treg : treg + 1,
6682                        (int) BFD_RELOC_LO16, AT);
6683           mips_optimize = hold_mips_optimize;
6684           expr1.X_add_number -= 4;
6685
6686           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6687                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6688                                       8 + gpdel + off, 1, 0),
6689                         offset_expr.X_add_symbol, 0, NULL);
6690           if (gpdel > 0)
6691             {
6692               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6693               p += 4;
6694             }
6695           macro_build (p, &icnt, &offset_expr,
6696                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6697                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6698                        mips_gp_register);
6699           p += 4;
6700           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6701           p += 4;
6702           if (breg != 0)
6703             {
6704               macro_build (p, &icnt, (expressionS *) NULL,
6705                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6706                            "d,v,t", AT, breg, AT);
6707               p += 4;
6708             }
6709           /* Itbl support may require additional care here.  */
6710           macro_build (p, &icnt, &expr1, s, fmt,
6711                        coproc ? treg + 1 : treg,
6712                        (int) BFD_RELOC_LO16, AT);
6713           p += 4;
6714           expr1.X_add_number += 4;
6715
6716           /* Set mips_optimize to 2 to avoid inserting an undesired
6717              nop.  */
6718           hold_mips_optimize = mips_optimize;
6719           mips_optimize = 2;
6720           /* Itbl support may require additional care here.  */
6721           macro_build (p, &icnt, &expr1, s, fmt,
6722                        coproc ? treg : treg + 1,
6723                        (int) BFD_RELOC_LO16, AT);
6724           mips_optimize = hold_mips_optimize;
6725         }
6726       else if (mips_pic == EMBEDDED_PIC)
6727         {
6728           /* If there is no base register, we use
6729                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6730                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6731              If we have a base register, we use
6732                addu     $at,$breg,$gp
6733                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6734                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6735              */
6736           if (breg == 0)
6737             {
6738               tempreg = mips_gp_register;
6739               used_at = 0;
6740             }
6741           else
6742             {
6743               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6744                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6745                            "d,v,t", AT, breg, mips_gp_register);
6746               tempreg = AT;
6747               used_at = 1;
6748             }
6749
6750           /* Itbl support may require additional care here.  */
6751           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6752                        coproc ? treg + 1 : treg,
6753                        (int) BFD_RELOC_GPREL16, tempreg);
6754           offset_expr.X_add_number += 4;
6755           /* Itbl support may require additional care here.  */
6756           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6757                        coproc ? treg : treg + 1,
6758                        (int) BFD_RELOC_GPREL16, tempreg);
6759         }
6760       else
6761         abort ();
6762
6763       if (! used_at)
6764         return;
6765
6766       break;
6767
6768     case M_LD_OB:
6769       s = "lw";
6770       goto sd_ob;
6771     case M_SD_OB:
6772       s = "sw";
6773     sd_ob:
6774       assert (HAVE_32BIT_ADDRESSES);
6775       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6776                    (int) BFD_RELOC_LO16, breg);
6777       offset_expr.X_add_number += 4;
6778       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6779                    (int) BFD_RELOC_LO16, breg);
6780       return;
6781
6782    /* New code added to support COPZ instructions.
6783       This code builds table entries out of the macros in mip_opcodes.
6784       R4000 uses interlocks to handle coproc delays.
6785       Other chips (like the R3000) require nops to be inserted for delays.
6786
6787       FIXME: Currently, we require that the user handle delays.
6788       In order to fill delay slots for non-interlocked chips,
6789       we must have a way to specify delays based on the coprocessor.
6790       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6791       What are the side-effects of the cop instruction?
6792       What cache support might we have and what are its effects?
6793       Both coprocessor & memory require delays. how long???
6794       What registers are read/set/modified?
6795
6796       If an itbl is provided to interpret cop instructions,
6797       this knowledge can be encoded in the itbl spec.  */
6798
6799     case M_COP0:
6800       s = "c0";
6801       goto copz;
6802     case M_COP1:
6803       s = "c1";
6804       goto copz;
6805     case M_COP2:
6806       s = "c2";
6807       goto copz;
6808     case M_COP3:
6809       s = "c3";
6810     copz:
6811       /* For now we just do C (same as Cz).  The parameter will be
6812          stored in insn_opcode by mips_ip.  */
6813       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6814                    ip->insn_opcode);
6815       return;
6816
6817     case M_MOVE:
6818       move_register (&icnt, dreg, sreg);
6819       return;
6820
6821 #ifdef LOSING_COMPILER
6822     default:
6823       /* Try and see if this is a new itbl instruction.
6824          This code builds table entries out of the macros in mip_opcodes.
6825          FIXME: For now we just assemble the expression and pass it's
6826          value along as a 32-bit immediate.
6827          We may want to have the assembler assemble this value,
6828          so that we gain the assembler's knowledge of delay slots,
6829          symbols, etc.
6830          Would it be more efficient to use mask (id) here? */
6831       if (itbl_have_entries
6832           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6833         {
6834           s = ip->insn_mo->name;
6835           s2 = "cop3";
6836           coproc = ITBL_DECODE_PNUM (immed_expr);;
6837           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6838           return;
6839         }
6840       macro2 (ip);
6841       return;
6842     }
6843   if (mips_opts.noat)
6844     as_warn (_("Macro used $at after \".set noat\""));
6845 }
6846
6847 static void
6848 macro2 (ip)
6849      struct mips_cl_insn *ip;
6850 {
6851   register int treg, sreg, dreg, breg;
6852   int tempreg;
6853   int mask;
6854   int icnt = 0;
6855   int used_at;
6856   expressionS expr1;
6857   const char *s;
6858   const char *s2;
6859   const char *fmt;
6860   int likely = 0;
6861   int dbl = 0;
6862   int coproc = 0;
6863   int lr = 0;
6864   int imm = 0;
6865   int off;
6866   offsetT maxnum;
6867   bfd_reloc_code_real_type r;
6868   char *p;
6869
6870   treg = (ip->insn_opcode >> 16) & 0x1f;
6871   dreg = (ip->insn_opcode >> 11) & 0x1f;
6872   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6873   mask = ip->insn_mo->mask;
6874
6875   expr1.X_op = O_constant;
6876   expr1.X_op_symbol = NULL;
6877   expr1.X_add_symbol = NULL;
6878   expr1.X_add_number = 1;
6879
6880   switch (mask)
6881     {
6882 #endif /* LOSING_COMPILER */
6883
6884     case M_DMUL:
6885       dbl = 1;
6886     case M_MUL:
6887       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6888                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6889       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6890                    dreg);
6891       return;
6892
6893     case M_DMUL_I:
6894       dbl = 1;
6895     case M_MUL_I:
6896       /* The MIPS assembler some times generates shifts and adds.  I'm
6897          not trying to be that fancy. GCC should do this for us
6898          anyway.  */
6899       load_register (&icnt, AT, &imm_expr, dbl);
6900       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6901                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6902       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6903                    dreg);
6904       break;
6905
6906     case M_DMULO_I:
6907       dbl = 1;
6908     case M_MULO_I:
6909       imm = 1;
6910       goto do_mulo;
6911
6912     case M_DMULO:
6913       dbl = 1;
6914     case M_MULO:
6915     do_mulo:
6916       mips_emit_delays (TRUE);
6917       ++mips_opts.noreorder;
6918       mips_any_noreorder = 1;
6919       if (imm)
6920         load_register (&icnt, AT, &imm_expr, dbl);
6921       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6922                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6923       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6924                    dreg);
6925       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6926                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6927       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6928                    AT);
6929       if (mips_trap)
6930         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6931                      "s,t,q", dreg, AT, 6);
6932       else
6933         {
6934           expr1.X_add_number = 8;
6935           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6936                        AT);
6937           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6938                        0);
6939           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6940                        "c", 6);
6941         }
6942       --mips_opts.noreorder;
6943       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6944       break;
6945
6946     case M_DMULOU_I:
6947       dbl = 1;
6948     case M_MULOU_I:
6949       imm = 1;
6950       goto do_mulou;
6951
6952     case M_DMULOU:
6953       dbl = 1;
6954     case M_MULOU:
6955     do_mulou:
6956       mips_emit_delays (TRUE);
6957       ++mips_opts.noreorder;
6958       mips_any_noreorder = 1;
6959       if (imm)
6960         load_register (&icnt, AT, &imm_expr, dbl);
6961       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6962                    dbl ? "dmultu" : "multu",
6963                    "s,t", sreg, imm ? AT : treg);
6964       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6965                    AT);
6966       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6967                    dreg);
6968       if (mips_trap)
6969         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6970                      "s,t,q", AT, 0, 6);
6971       else
6972         {
6973           expr1.X_add_number = 8;
6974           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6975           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6976                        0);
6977           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6978                        "c", 6);
6979         }
6980       --mips_opts.noreorder;
6981       break;
6982
6983     case M_DROL:
6984       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
6985         {
6986           if (dreg == sreg)
6987             {
6988               tempreg = AT;
6989               used_at = 1;
6990             }
6991           else
6992             {
6993               tempreg = dreg;
6994               used_at = 0;
6995             }
6996           macro_build ((char *) NULL, &icnt, NULL, "dnegu",
6997                        "d,w", tempreg, treg);
6998           macro_build ((char *) NULL, &icnt, NULL, "drorv",
6999                        "d,t,s", dreg, sreg, tempreg);
7000           if (used_at)
7001             break;
7002           return;
7003         }
7004       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7005                    "d,v,t", AT, 0, treg);
7006       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7007                    "d,t,s", AT, sreg, AT);
7008       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7009                    "d,t,s", dreg, sreg, treg);
7010       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7011                    "d,v,t", dreg, dreg, AT);
7012       break;
7013
7014     case M_ROL:
7015       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7016         {
7017           if (dreg == sreg)
7018             {
7019               tempreg = AT;
7020               used_at = 1;
7021             }
7022           else
7023             {
7024               tempreg = dreg;
7025               used_at = 0;
7026             }
7027           macro_build ((char *) NULL, &icnt, NULL, "negu",
7028                        "d,w", tempreg, treg);
7029           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7030                        "d,t,s", dreg, sreg, tempreg);
7031           if (used_at)
7032             break;
7033           return;
7034         }
7035       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7036                    "d,v,t", AT, 0, treg);
7037       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7038                    "d,t,s", AT, sreg, AT);
7039       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7040                    "d,t,s", dreg, sreg, treg);
7041       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7042                    "d,v,t", dreg, dreg, AT);
7043       break;
7044
7045     case M_DROL_I:
7046       {
7047         unsigned int rot;
7048         char *l, *r;
7049
7050         if (imm_expr.X_op != O_constant)
7051           as_bad (_("Improper rotate count"));
7052         rot = imm_expr.X_add_number & 0x3f;
7053         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7054           {
7055             rot = (64 - rot) & 0x3f;
7056             if (rot >= 32)
7057               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7058                            "d,w,<", dreg, sreg, rot - 32);
7059             else
7060               macro_build ((char *) NULL, &icnt, NULL, "dror",
7061                            "d,w,<", dreg, sreg, rot);
7062             return;
7063           }
7064         if (rot == 0)
7065           {
7066             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7067                          "d,w,<", dreg, sreg, 0);
7068             return;
7069           }
7070         l = (rot < 0x20) ? "dsll" : "dsll32";
7071         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7072         rot &= 0x1f;
7073         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7074                      "d,w,<", AT, sreg, rot);
7075         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7076                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7077         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7078                      "d,v,t", dreg, dreg, AT);
7079       }
7080       break;
7081
7082     case M_ROL_I:
7083       {
7084         unsigned int rot;
7085
7086         if (imm_expr.X_op != O_constant)
7087           as_bad (_("Improper rotate count"));
7088         rot = imm_expr.X_add_number & 0x1f;
7089         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7090           {
7091             macro_build ((char *) NULL, &icnt, NULL, "ror",
7092                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7093             return;
7094           }
7095         if (rot == 0)
7096           {
7097             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7098                          "d,w,<", dreg, sreg, 0);
7099             return;
7100           }
7101         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7102                      "d,w,<", AT, sreg, rot);
7103         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7104                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7105         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7106                      "d,v,t", dreg, dreg, AT);
7107       }
7108       break;
7109
7110     case M_DROR:
7111       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7112         {
7113           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7114                        "d,t,s", dreg, sreg, treg);
7115           return;
7116         }
7117       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7118                    "d,v,t", AT, 0, treg);
7119       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7120                    "d,t,s", AT, sreg, AT);
7121       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7122                    "d,t,s", dreg, sreg, treg);
7123       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7124                    "d,v,t", dreg, dreg, AT);
7125       break;
7126
7127     case M_ROR:
7128       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7129         {
7130           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7131                        "d,t,s", dreg, sreg, treg);
7132           return;
7133         }
7134       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7135                    "d,v,t", AT, 0, treg);
7136       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7137                    "d,t,s", AT, sreg, AT);
7138       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7139                    "d,t,s", dreg, sreg, treg);
7140       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7141                    "d,v,t", dreg, dreg, AT);
7142       break;
7143
7144     case M_DROR_I:
7145       {
7146         unsigned int rot;
7147         char *l, *r;
7148
7149         if (imm_expr.X_op != O_constant)
7150           as_bad (_("Improper rotate count"));
7151         rot = imm_expr.X_add_number & 0x3f;
7152         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7153           {
7154             if (rot >= 32)
7155               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7156                            "d,w,<", dreg, sreg, rot - 32);
7157             else
7158               macro_build ((char *) NULL, &icnt, NULL, "dror",
7159                            "d,w,<", dreg, sreg, rot);
7160             return;
7161           }
7162         if (rot == 0)
7163           {
7164             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7165                          "d,w,<", dreg, sreg, 0);
7166             return;
7167           }
7168         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7169         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7170         rot &= 0x1f;
7171         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7172                      "d,w,<", AT, sreg, rot);
7173         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7174                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7175         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7176                      "d,v,t", dreg, dreg, AT);
7177       }
7178       break;
7179
7180     case M_ROR_I:
7181       {
7182         unsigned int rot;
7183
7184         if (imm_expr.X_op != O_constant)
7185           as_bad (_("Improper rotate count"));
7186         rot = imm_expr.X_add_number & 0x1f;
7187         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7188           {
7189             macro_build ((char *) NULL, &icnt, NULL, "ror",
7190                          "d,w,<", dreg, sreg, rot);
7191             return;
7192           }
7193         if (rot == 0)
7194           {
7195             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7196                          "d,w,<", dreg, sreg, 0);
7197             return;
7198           }
7199         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7200                      "d,w,<", AT, sreg, rot);
7201         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7202                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7203         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7204                      "d,v,t", dreg, dreg, AT);
7205       }
7206       break;
7207
7208     case M_S_DOB:
7209       if (mips_arch == CPU_R4650)
7210         {
7211           as_bad (_("opcode not supported on this processor"));
7212           return;
7213         }
7214       assert (mips_opts.isa == ISA_MIPS1);
7215       /* Even on a big endian machine $fn comes before $fn+1.  We have
7216          to adjust when storing to memory.  */
7217       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7218                    target_big_endian ? treg + 1 : treg,
7219                    (int) BFD_RELOC_LO16, breg);
7220       offset_expr.X_add_number += 4;
7221       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7222                    target_big_endian ? treg : treg + 1,
7223                    (int) BFD_RELOC_LO16, breg);
7224       return;
7225
7226     case M_SEQ:
7227       if (sreg == 0)
7228         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7229                      treg, (int) BFD_RELOC_LO16);
7230       else if (treg == 0)
7231         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7232                      sreg, (int) BFD_RELOC_LO16);
7233       else
7234         {
7235           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7236                        "d,v,t", dreg, sreg, treg);
7237           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7238                        dreg, (int) BFD_RELOC_LO16);
7239         }
7240       return;
7241
7242     case M_SEQ_I:
7243       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7244         {
7245           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7246                        sreg, (int) BFD_RELOC_LO16);
7247           return;
7248         }
7249       if (sreg == 0)
7250         {
7251           as_warn (_("Instruction %s: result is always false"),
7252                    ip->insn_mo->name);
7253           move_register (&icnt, dreg, 0);
7254           return;
7255         }
7256       if (imm_expr.X_op == O_constant
7257           && imm_expr.X_add_number >= 0
7258           && imm_expr.X_add_number < 0x10000)
7259         {
7260           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7261                        sreg, (int) BFD_RELOC_LO16);
7262           used_at = 0;
7263         }
7264       else if (imm_expr.X_op == O_constant
7265                && imm_expr.X_add_number > -0x8000
7266                && imm_expr.X_add_number < 0)
7267         {
7268           imm_expr.X_add_number = -imm_expr.X_add_number;
7269           macro_build ((char *) NULL, &icnt, &imm_expr,
7270                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7271                        "t,r,j", dreg, sreg,
7272                        (int) BFD_RELOC_LO16);
7273           used_at = 0;
7274         }
7275       else
7276         {
7277           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7278           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7279                        "d,v,t", dreg, sreg, AT);
7280           used_at = 1;
7281         }
7282       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7283                    (int) BFD_RELOC_LO16);
7284       if (used_at)
7285         break;
7286       return;
7287
7288     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7289       s = "slt";
7290       goto sge;
7291     case M_SGEU:
7292       s = "sltu";
7293     sge:
7294       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7295                    dreg, sreg, treg);
7296       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7297                    (int) BFD_RELOC_LO16);
7298       return;
7299
7300     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7301     case M_SGEU_I:
7302       if (imm_expr.X_op == O_constant
7303           && imm_expr.X_add_number >= -0x8000
7304           && imm_expr.X_add_number < 0x8000)
7305         {
7306           macro_build ((char *) NULL, &icnt, &imm_expr,
7307                        mask == M_SGE_I ? "slti" : "sltiu",
7308                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7309           used_at = 0;
7310         }
7311       else
7312         {
7313           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7314           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7315                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7316                        AT);
7317           used_at = 1;
7318         }
7319       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7320                    (int) BFD_RELOC_LO16);
7321       if (used_at)
7322         break;
7323       return;
7324
7325     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7326       s = "slt";
7327       goto sgt;
7328     case M_SGTU:
7329       s = "sltu";
7330     sgt:
7331       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7332                    dreg, treg, sreg);
7333       return;
7334
7335     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7336       s = "slt";
7337       goto sgti;
7338     case M_SGTU_I:
7339       s = "sltu";
7340     sgti:
7341       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7342       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7343                    dreg, AT, sreg);
7344       break;
7345
7346     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7347       s = "slt";
7348       goto sle;
7349     case M_SLEU:
7350       s = "sltu";
7351     sle:
7352       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7353                    dreg, treg, sreg);
7354       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7355                    (int) BFD_RELOC_LO16);
7356       return;
7357
7358     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7359       s = "slt";
7360       goto slei;
7361     case M_SLEU_I:
7362       s = "sltu";
7363     slei:
7364       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7365       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7366                    dreg, AT, sreg);
7367       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7368                    (int) BFD_RELOC_LO16);
7369       break;
7370
7371     case M_SLT_I:
7372       if (imm_expr.X_op == O_constant
7373           && imm_expr.X_add_number >= -0x8000
7374           && imm_expr.X_add_number < 0x8000)
7375         {
7376           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7377                        dreg, sreg, (int) BFD_RELOC_LO16);
7378           return;
7379         }
7380       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7381       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7382                    dreg, sreg, AT);
7383       break;
7384
7385     case M_SLTU_I:
7386       if (imm_expr.X_op == O_constant
7387           && imm_expr.X_add_number >= -0x8000
7388           && imm_expr.X_add_number < 0x8000)
7389         {
7390           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7391                        dreg, sreg, (int) BFD_RELOC_LO16);
7392           return;
7393         }
7394       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7395       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7396                    "d,v,t", dreg, sreg, AT);
7397       break;
7398
7399     case M_SNE:
7400       if (sreg == 0)
7401         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7402                      "d,v,t", dreg, 0, treg);
7403       else if (treg == 0)
7404         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7405                      "d,v,t", dreg, 0, sreg);
7406       else
7407         {
7408           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7409                        "d,v,t", dreg, sreg, treg);
7410           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7411                        "d,v,t", dreg, 0, dreg);
7412         }
7413       return;
7414
7415     case M_SNE_I:
7416       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7417         {
7418           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7419                        "d,v,t", dreg, 0, sreg);
7420           return;
7421         }
7422       if (sreg == 0)
7423         {
7424           as_warn (_("Instruction %s: result is always true"),
7425                    ip->insn_mo->name);
7426           macro_build ((char *) NULL, &icnt, &expr1,
7427                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7428                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7429           return;
7430         }
7431       if (imm_expr.X_op == O_constant
7432           && imm_expr.X_add_number >= 0
7433           && imm_expr.X_add_number < 0x10000)
7434         {
7435           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7436                        dreg, sreg, (int) BFD_RELOC_LO16);
7437           used_at = 0;
7438         }
7439       else if (imm_expr.X_op == O_constant
7440                && imm_expr.X_add_number > -0x8000
7441                && imm_expr.X_add_number < 0)
7442         {
7443           imm_expr.X_add_number = -imm_expr.X_add_number;
7444           macro_build ((char *) NULL, &icnt, &imm_expr,
7445                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7446                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7447           used_at = 0;
7448         }
7449       else
7450         {
7451           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7452           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7453                        "d,v,t", dreg, sreg, AT);
7454           used_at = 1;
7455         }
7456       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7457                    "d,v,t", dreg, 0, dreg);
7458       if (used_at)
7459         break;
7460       return;
7461
7462     case M_DSUB_I:
7463       dbl = 1;
7464     case M_SUB_I:
7465       if (imm_expr.X_op == O_constant
7466           && imm_expr.X_add_number > -0x8000
7467           && imm_expr.X_add_number <= 0x8000)
7468         {
7469           imm_expr.X_add_number = -imm_expr.X_add_number;
7470           macro_build ((char *) NULL, &icnt, &imm_expr,
7471                        dbl ? "daddi" : "addi",
7472                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7473           return;
7474         }
7475       load_register (&icnt, AT, &imm_expr, dbl);
7476       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7477                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7478       break;
7479
7480     case M_DSUBU_I:
7481       dbl = 1;
7482     case M_SUBU_I:
7483       if (imm_expr.X_op == O_constant
7484           && imm_expr.X_add_number > -0x8000
7485           && imm_expr.X_add_number <= 0x8000)
7486         {
7487           imm_expr.X_add_number = -imm_expr.X_add_number;
7488           macro_build ((char *) NULL, &icnt, &imm_expr,
7489                        dbl ? "daddiu" : "addiu",
7490                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7491           return;
7492         }
7493       load_register (&icnt, AT, &imm_expr, dbl);
7494       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7495                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7496       break;
7497
7498     case M_TEQ_I:
7499       s = "teq";
7500       goto trap;
7501     case M_TGE_I:
7502       s = "tge";
7503       goto trap;
7504     case M_TGEU_I:
7505       s = "tgeu";
7506       goto trap;
7507     case M_TLT_I:
7508       s = "tlt";
7509       goto trap;
7510     case M_TLTU_I:
7511       s = "tltu";
7512       goto trap;
7513     case M_TNE_I:
7514       s = "tne";
7515     trap:
7516       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7517       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7518                    AT);
7519       break;
7520
7521     case M_TRUNCWS:
7522     case M_TRUNCWD:
7523       assert (mips_opts.isa == ISA_MIPS1);
7524       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7525       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7526
7527       /*
7528        * Is the double cfc1 instruction a bug in the mips assembler;
7529        * or is there a reason for it?
7530        */
7531       mips_emit_delays (TRUE);
7532       ++mips_opts.noreorder;
7533       mips_any_noreorder = 1;
7534       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7535                    treg, RA);
7536       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7537                    treg, RA);
7538       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7539       expr1.X_add_number = 3;
7540       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7541                    (int) BFD_RELOC_LO16);
7542       expr1.X_add_number = 2;
7543       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7544                      (int) BFD_RELOC_LO16);
7545       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7546                    AT, RA);
7547       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7548       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7549               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7550       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7551                    treg, RA);
7552       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7553       --mips_opts.noreorder;
7554       break;
7555
7556     case M_ULH:
7557       s = "lb";
7558       goto ulh;
7559     case M_ULHU:
7560       s = "lbu";
7561     ulh:
7562       if (offset_expr.X_add_number >= 0x7fff)
7563         as_bad (_("operand overflow"));
7564       if (! target_big_endian)
7565         ++offset_expr.X_add_number;
7566       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7567                    (int) BFD_RELOC_LO16, breg);
7568       if (! target_big_endian)
7569         --offset_expr.X_add_number;
7570       else
7571         ++offset_expr.X_add_number;
7572       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7573                    (int) BFD_RELOC_LO16, breg);
7574       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7575                    AT, AT, 8);
7576       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7577                    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 ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7599                    (int) 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 ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7605                    (int) 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 ((char *) NULL, &icnt, (expressionS *) 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 ((char *) NULL, &icnt, (expressionS *) NULL,
7630                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7631                      "d,v,t", AT, AT, breg);
7632       if (! target_big_endian)
7633         expr1.X_add_number = off;
7634       else
7635         expr1.X_add_number = 0;
7636       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7637                    (int) BFD_RELOC_LO16, AT);
7638       if (! target_big_endian)
7639         expr1.X_add_number = 0;
7640       else
7641         expr1.X_add_number = off;
7642       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7643                    (int) BFD_RELOC_LO16, AT);
7644       break;
7645
7646     case M_ULH_A:
7647     case M_ULHU_A:
7648       used_at = 1;
7649       load_address (&icnt, AT, &offset_expr, &used_at);
7650       if (breg != 0)
7651         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7652                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7653                      "d,v,t", AT, AT, breg);
7654       if (target_big_endian)
7655         expr1.X_add_number = 0;
7656       macro_build ((char *) NULL, &icnt, &expr1,
7657                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7658                    (int) BFD_RELOC_LO16, AT);
7659       if (target_big_endian)
7660         expr1.X_add_number = 1;
7661       else
7662         expr1.X_add_number = 0;
7663       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7664                    (int) BFD_RELOC_LO16, AT);
7665       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7666                    treg, treg, 8);
7667       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7668                    treg, treg, AT);
7669       break;
7670
7671     case M_USH:
7672       if (offset_expr.X_add_number >= 0x7fff)
7673         as_bad (_("operand overflow"));
7674       if (target_big_endian)
7675         ++offset_expr.X_add_number;
7676       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7677                    (int) BFD_RELOC_LO16, breg);
7678       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7679                    AT, treg, 8);
7680       if (target_big_endian)
7681         --offset_expr.X_add_number;
7682       else
7683         ++offset_expr.X_add_number;
7684       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7685                    (int) BFD_RELOC_LO16, breg);
7686       break;
7687
7688     case M_USD:
7689       s = "sdl";
7690       s2 = "sdr";
7691       off = 7;
7692       goto usw;
7693     case M_USW:
7694       s = "swl";
7695       s2 = "swr";
7696       off = 3;
7697     usw:
7698       if (offset_expr.X_add_number >= 0x8000 - off)
7699         as_bad (_("operand overflow"));
7700       if (! target_big_endian)
7701         offset_expr.X_add_number += off;
7702       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7703                    (int) BFD_RELOC_LO16, breg);
7704       if (! target_big_endian)
7705         offset_expr.X_add_number -= off;
7706       else
7707         offset_expr.X_add_number += off;
7708       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7709                    (int) BFD_RELOC_LO16, breg);
7710       return;
7711
7712     case M_USD_A:
7713       s = "sdl";
7714       s2 = "sdr";
7715       off = 7;
7716       goto uswa;
7717     case M_USW_A:
7718       s = "swl";
7719       s2 = "swr";
7720       off = 3;
7721     uswa:
7722       used_at = 1;
7723       load_address (&icnt, AT, &offset_expr, &used_at);
7724       if (breg != 0)
7725         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7726                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7727                      "d,v,t", AT, AT, breg);
7728       if (! target_big_endian)
7729         expr1.X_add_number = off;
7730       else
7731         expr1.X_add_number = 0;
7732       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7733                    (int) BFD_RELOC_LO16, AT);
7734       if (! target_big_endian)
7735         expr1.X_add_number = 0;
7736       else
7737         expr1.X_add_number = off;
7738       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7739                    (int) BFD_RELOC_LO16, AT);
7740       break;
7741
7742     case M_USH_A:
7743       used_at = 1;
7744       load_address (&icnt, AT, &offset_expr, &used_at);
7745       if (breg != 0)
7746         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7747                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7748                      "d,v,t", AT, AT, breg);
7749       if (! target_big_endian)
7750         expr1.X_add_number = 0;
7751       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7752                    (int) BFD_RELOC_LO16, AT);
7753       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7754                    treg, treg, 8);
7755       if (! target_big_endian)
7756         expr1.X_add_number = 1;
7757       else
7758         expr1.X_add_number = 0;
7759       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7760                    (int) BFD_RELOC_LO16, AT);
7761       if (! target_big_endian)
7762         expr1.X_add_number = 0;
7763       else
7764         expr1.X_add_number = 1;
7765       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7766                    (int) BFD_RELOC_LO16, AT);
7767       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7768                    treg, treg, 8);
7769       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7770                    treg, treg, AT);
7771       break;
7772
7773     default:
7774       /* FIXME: Check if this is one of the itbl macros, since they
7775          are added dynamically.  */
7776       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7777       break;
7778     }
7779   if (mips_opts.noat)
7780     as_warn (_("Macro used $at after \".set noat\""));
7781 }
7782
7783 /* Implement macros in mips16 mode.  */
7784
7785 static void
7786 mips16_macro (ip)
7787      struct mips_cl_insn *ip;
7788 {
7789   int mask;
7790   int xreg, yreg, zreg, tmp;
7791   int icnt;
7792   expressionS expr1;
7793   int dbl;
7794   const char *s, *s2, *s3;
7795
7796   mask = ip->insn_mo->mask;
7797
7798   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7799   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7800   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7801
7802   icnt = 0;
7803
7804   expr1.X_op = O_constant;
7805   expr1.X_op_symbol = NULL;
7806   expr1.X_add_symbol = NULL;
7807   expr1.X_add_number = 1;
7808
7809   dbl = 0;
7810
7811   switch (mask)
7812     {
7813     default:
7814       internalError ();
7815
7816     case M_DDIV_3:
7817       dbl = 1;
7818     case M_DIV_3:
7819       s = "mflo";
7820       goto do_div3;
7821     case M_DREM_3:
7822       dbl = 1;
7823     case M_REM_3:
7824       s = "mfhi";
7825     do_div3:
7826       mips_emit_delays (TRUE);
7827       ++mips_opts.noreorder;
7828       mips_any_noreorder = 1;
7829       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7830                    dbl ? "ddiv" : "div",
7831                    "0,x,y", xreg, yreg);
7832       expr1.X_add_number = 2;
7833       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7834       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7835                    7);
7836
7837       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7838          since that causes an overflow.  We should do that as well,
7839          but I don't see how to do the comparisons without a temporary
7840          register.  */
7841       --mips_opts.noreorder;
7842       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7843       break;
7844
7845     case M_DIVU_3:
7846       s = "divu";
7847       s2 = "mflo";
7848       goto do_divu3;
7849     case M_REMU_3:
7850       s = "divu";
7851       s2 = "mfhi";
7852       goto do_divu3;
7853     case M_DDIVU_3:
7854       s = "ddivu";
7855       s2 = "mflo";
7856       goto do_divu3;
7857     case M_DREMU_3:
7858       s = "ddivu";
7859       s2 = "mfhi";
7860     do_divu3:
7861       mips_emit_delays (TRUE);
7862       ++mips_opts.noreorder;
7863       mips_any_noreorder = 1;
7864       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7865                    xreg, yreg);
7866       expr1.X_add_number = 2;
7867       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7868       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7869                    "6", 7);
7870       --mips_opts.noreorder;
7871       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7872       break;
7873
7874     case M_DMUL:
7875       dbl = 1;
7876     case M_MUL:
7877       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7878                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7879       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7880                    zreg);
7881       return;
7882
7883     case M_DSUBU_I:
7884       dbl = 1;
7885       goto do_subu;
7886     case M_SUBU_I:
7887     do_subu:
7888       if (imm_expr.X_op != O_constant)
7889         as_bad (_("Unsupported large constant"));
7890       imm_expr.X_add_number = -imm_expr.X_add_number;
7891       macro_build ((char *) NULL, &icnt, &imm_expr,
7892                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7893       break;
7894
7895     case M_SUBU_I_2:
7896       if (imm_expr.X_op != O_constant)
7897         as_bad (_("Unsupported large constant"));
7898       imm_expr.X_add_number = -imm_expr.X_add_number;
7899       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7900                    "x,k", xreg);
7901       break;
7902
7903     case M_DSUBU_I_2:
7904       if (imm_expr.X_op != O_constant)
7905         as_bad (_("Unsupported large constant"));
7906       imm_expr.X_add_number = -imm_expr.X_add_number;
7907       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7908                    "y,j", yreg);
7909       break;
7910
7911     case M_BEQ:
7912       s = "cmp";
7913       s2 = "bteqz";
7914       goto do_branch;
7915     case M_BNE:
7916       s = "cmp";
7917       s2 = "btnez";
7918       goto do_branch;
7919     case M_BLT:
7920       s = "slt";
7921       s2 = "btnez";
7922       goto do_branch;
7923     case M_BLTU:
7924       s = "sltu";
7925       s2 = "btnez";
7926       goto do_branch;
7927     case M_BLE:
7928       s = "slt";
7929       s2 = "bteqz";
7930       goto do_reverse_branch;
7931     case M_BLEU:
7932       s = "sltu";
7933       s2 = "bteqz";
7934       goto do_reverse_branch;
7935     case M_BGE:
7936       s = "slt";
7937       s2 = "bteqz";
7938       goto do_branch;
7939     case M_BGEU:
7940       s = "sltu";
7941       s2 = "bteqz";
7942       goto do_branch;
7943     case M_BGT:
7944       s = "slt";
7945       s2 = "btnez";
7946       goto do_reverse_branch;
7947     case M_BGTU:
7948       s = "sltu";
7949       s2 = "btnez";
7950
7951     do_reverse_branch:
7952       tmp = xreg;
7953       xreg = yreg;
7954       yreg = tmp;
7955
7956     do_branch:
7957       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7958                    xreg, yreg);
7959       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7960       break;
7961
7962     case M_BEQ_I:
7963       s = "cmpi";
7964       s2 = "bteqz";
7965       s3 = "x,U";
7966       goto do_branch_i;
7967     case M_BNE_I:
7968       s = "cmpi";
7969       s2 = "btnez";
7970       s3 = "x,U";
7971       goto do_branch_i;
7972     case M_BLT_I:
7973       s = "slti";
7974       s2 = "btnez";
7975       s3 = "x,8";
7976       goto do_branch_i;
7977     case M_BLTU_I:
7978       s = "sltiu";
7979       s2 = "btnez";
7980       s3 = "x,8";
7981       goto do_branch_i;
7982     case M_BLE_I:
7983       s = "slti";
7984       s2 = "btnez";
7985       s3 = "x,8";
7986       goto do_addone_branch_i;
7987     case M_BLEU_I:
7988       s = "sltiu";
7989       s2 = "btnez";
7990       s3 = "x,8";
7991       goto do_addone_branch_i;
7992     case M_BGE_I:
7993       s = "slti";
7994       s2 = "bteqz";
7995       s3 = "x,8";
7996       goto do_branch_i;
7997     case M_BGEU_I:
7998       s = "sltiu";
7999       s2 = "bteqz";
8000       s3 = "x,8";
8001       goto do_branch_i;
8002     case M_BGT_I:
8003       s = "slti";
8004       s2 = "bteqz";
8005       s3 = "x,8";
8006       goto do_addone_branch_i;
8007     case M_BGTU_I:
8008       s = "sltiu";
8009       s2 = "bteqz";
8010       s3 = "x,8";
8011
8012     do_addone_branch_i:
8013       if (imm_expr.X_op != O_constant)
8014         as_bad (_("Unsupported large constant"));
8015       ++imm_expr.X_add_number;
8016
8017     do_branch_i:
8018       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8019       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8020       break;
8021
8022     case M_ABS:
8023       expr1.X_add_number = 0;
8024       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
8025       if (xreg != yreg)
8026         move_register (&icnt, xreg, yreg);
8027       expr1.X_add_number = 2;
8028       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8029       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8030                    "neg", "x,w", xreg, xreg);
8031     }
8032 }
8033
8034 /* For consistency checking, verify that all bits are specified either
8035    by the match/mask part of the instruction definition, or by the
8036    operand list.  */
8037 static int
8038 validate_mips_insn (opc)
8039      const struct mips_opcode *opc;
8040 {
8041   const char *p = opc->args;
8042   char c;
8043   unsigned long used_bits = opc->mask;
8044
8045   if ((used_bits & opc->match) != opc->match)
8046     {
8047       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8048               opc->name, opc->args);
8049       return 0;
8050     }
8051 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8052   while (*p)
8053     switch (c = *p++)
8054       {
8055       case ',': break;
8056       case '(': break;
8057       case ')': break;
8058       case '+':
8059         switch (c = *p++)
8060           {
8061           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8062           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8063           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8064           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8065                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8066           default:
8067             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8068                     c, opc->name, opc->args);
8069             return 0;
8070           }
8071         break;
8072       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8073       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8074       case 'A': break;
8075       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8076       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8077       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8078       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8079       case 'F': break;
8080       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8081       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8082       case 'I': break;
8083       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8084       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8085       case 'L': break;
8086       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8087       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8088       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8089       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8090                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8091       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8092       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8093       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8094       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8095       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8096       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8097       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8098       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8099       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8100       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8101       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8102       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8103       case 'f': break;
8104       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8105       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8106       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8107       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8108       case 'l': break;
8109       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8110       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8111       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8112       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8113       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8114       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8115       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8116       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8117       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8118       case 'x': break;
8119       case 'z': break;
8120       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8121       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8122                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8123       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8124       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8125       case '[': break;
8126       case ']': break;
8127       default:
8128         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8129                 c, opc->name, opc->args);
8130         return 0;
8131       }
8132 #undef USE_BITS
8133   if (used_bits != 0xffffffff)
8134     {
8135       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8136               ~used_bits & 0xffffffff, opc->name, opc->args);
8137       return 0;
8138     }
8139   return 1;
8140 }
8141
8142 /* This routine assembles an instruction into its binary format.  As a
8143    side effect, it sets one of the global variables imm_reloc or
8144    offset_reloc to the type of relocation to do if one of the operands
8145    is an address expression.  */
8146
8147 static void
8148 mips_ip (str, ip)
8149      char *str;
8150      struct mips_cl_insn *ip;
8151 {
8152   char *s;
8153   const char *args;
8154   char c = 0;
8155   struct mips_opcode *insn;
8156   char *argsStart;
8157   unsigned int regno;
8158   unsigned int lastregno = 0;
8159   unsigned int lastpos = 0;
8160   unsigned int limlo, limhi;
8161   char *s_reset;
8162   char save_c = 0;
8163
8164   insn_error = NULL;
8165
8166   /* If the instruction contains a '.', we first try to match an instruction
8167      including the '.'.  Then we try again without the '.'.  */
8168   insn = NULL;
8169   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8170     continue;
8171
8172   /* If we stopped on whitespace, then replace the whitespace with null for
8173      the call to hash_find.  Save the character we replaced just in case we
8174      have to re-parse the instruction.  */
8175   if (ISSPACE (*s))
8176     {
8177       save_c = *s;
8178       *s++ = '\0';
8179     }
8180
8181   insn = (struct mips_opcode *) hash_find (op_hash, str);
8182
8183   /* If we didn't find the instruction in the opcode table, try again, but
8184      this time with just the instruction up to, but not including the
8185      first '.'.  */
8186   if (insn == NULL)
8187     {
8188       /* Restore the character we overwrite above (if any).  */
8189       if (save_c)
8190         *(--s) = save_c;
8191
8192       /* Scan up to the first '.' or whitespace.  */
8193       for (s = str;
8194            *s != '\0' && *s != '.' && !ISSPACE (*s);
8195            ++s)
8196         continue;
8197
8198       /* If we did not find a '.', then we can quit now.  */
8199       if (*s != '.')
8200         {
8201           insn_error = "unrecognized opcode";
8202           return;
8203         }
8204
8205       /* Lookup the instruction in the hash table.  */
8206       *s++ = '\0';
8207       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8208         {
8209           insn_error = "unrecognized opcode";
8210           return;
8211         }
8212     }
8213
8214   argsStart = s;
8215   for (;;)
8216     {
8217       bfd_boolean ok;
8218
8219       assert (strcmp (insn->name, str) == 0);
8220
8221       if (OPCODE_IS_MEMBER (insn,
8222                             (mips_opts.isa
8223                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8224                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8225                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8226                             mips_arch))
8227         ok = TRUE;
8228       else
8229         ok = FALSE;
8230
8231       if (insn->pinfo != INSN_MACRO)
8232         {
8233           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8234             ok = FALSE;
8235         }
8236
8237       if (! ok)
8238         {
8239           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8240               && strcmp (insn->name, insn[1].name) == 0)
8241             {
8242               ++insn;
8243               continue;
8244             }
8245           else
8246             {
8247               if (!insn_error)
8248                 {
8249                   static char buf[100];
8250                   if (mips_arch_info->is_isa)
8251                     sprintf (buf,
8252                              _("opcode not supported at this ISA level (%s)"),
8253                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8254                   else
8255                     sprintf (buf,
8256                              _("opcode not supported on this processor: %s (%s)"),
8257                              mips_arch_info->name,
8258                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8259                   insn_error = buf;
8260                 }
8261               if (save_c)
8262                 *(--s) = save_c;
8263               return;
8264             }
8265         }
8266
8267       ip->insn_mo = insn;
8268       ip->insn_opcode = insn->match;
8269       insn_error = NULL;
8270       for (args = insn->args;; ++args)
8271         {
8272           int is_mdmx;
8273
8274           s += strspn (s, " \t");
8275           is_mdmx = 0;
8276           switch (*args)
8277             {
8278             case '\0':          /* end of args */
8279               if (*s == '\0')
8280                 return;
8281               break;
8282
8283             case ',':
8284               if (*s++ == *args)
8285                 continue;
8286               s--;
8287               switch (*++args)
8288                 {
8289                 case 'r':
8290                 case 'v':
8291                   ip->insn_opcode |= lastregno << OP_SH_RS;
8292                   continue;
8293
8294                 case 'w':
8295                   ip->insn_opcode |= lastregno << OP_SH_RT;
8296                   continue;
8297
8298                 case 'W':
8299                   ip->insn_opcode |= lastregno << OP_SH_FT;
8300                   continue;
8301
8302                 case 'V':
8303                   ip->insn_opcode |= lastregno << OP_SH_FS;
8304                   continue;
8305                 }
8306               break;
8307
8308             case '(':
8309               /* Handle optional base register.
8310                  Either the base register is omitted or
8311                  we must have a left paren.  */
8312               /* This is dependent on the next operand specifier
8313                  is a base register specification.  */
8314               assert (args[1] == 'b' || args[1] == '5'
8315                       || args[1] == '-' || args[1] == '4');
8316               if (*s == '\0')
8317                 return;
8318
8319             case ')':           /* these must match exactly */
8320             case '[':
8321             case ']':
8322               if (*s++ == *args)
8323                 continue;
8324               break;
8325
8326             case '+':           /* Opcode extension character.  */
8327               switch (*++args)
8328                 {
8329                 case 'A':               /* ins/ext position, becomes LSB.  */
8330                   limlo = 0;
8331                   limhi = 31;
8332                   my_getExpression (&imm_expr, s);
8333                   check_absolute_expr (ip, &imm_expr);
8334                   if ((unsigned long) imm_expr.X_add_number < limlo
8335                       || (unsigned long) imm_expr.X_add_number > limhi)
8336                     {
8337                       as_bad (_("Improper position (%lu)"),
8338                               (unsigned long) imm_expr.X_add_number);
8339                       imm_expr.X_add_number = limlo;
8340                     }
8341                   lastpos = imm_expr.X_add_number;
8342                   ip->insn_opcode |= (imm_expr.X_add_number
8343                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8344                   imm_expr.X_op = O_absent;
8345                   s = expr_end;
8346                   continue;
8347
8348                 case 'B':               /* ins size, becomes MSB.  */
8349                   limlo = 1;
8350                   limhi = 32;
8351                   my_getExpression (&imm_expr, s);
8352                   check_absolute_expr (ip, &imm_expr);
8353                   /* Check for negative input so that small negative numbers
8354                      will not succeed incorrectly.  The checks against
8355                      (pos+size) transitively check "size" itself,
8356                      assuming that "pos" is reasonable.  */
8357                   if ((long) imm_expr.X_add_number < 0
8358                       || ((unsigned long) imm_expr.X_add_number
8359                           + lastpos) < limlo
8360                       || ((unsigned long) imm_expr.X_add_number
8361                           + lastpos) > limhi)
8362                     {
8363                       as_bad (_("Improper insert size (%lu, position %lu)"),
8364                               (unsigned long) imm_expr.X_add_number,
8365                               (unsigned long) lastpos);
8366                       imm_expr.X_add_number = limlo - lastpos;
8367                     }
8368                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8369                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8370                   imm_expr.X_op = O_absent;
8371                   s = expr_end;
8372                   continue;
8373
8374                 case 'C':               /* ext size, becomes MSBD.  */
8375                   limlo = 1;
8376                   limhi = 32;
8377                   my_getExpression (&imm_expr, s);
8378                   check_absolute_expr (ip, &imm_expr);
8379                   /* Check for negative input so that small negative numbers
8380                      will not succeed incorrectly.  The checks against
8381                      (pos+size) transitively check "size" itself,
8382                      assuming that "pos" is reasonable.  */
8383                   if ((long) imm_expr.X_add_number < 0
8384                       || ((unsigned long) imm_expr.X_add_number
8385                           + lastpos) < limlo
8386                       || ((unsigned long) imm_expr.X_add_number
8387                           + lastpos) > limhi)
8388                     {
8389                       as_bad (_("Improper extract size (%lu, position %lu)"),
8390                               (unsigned long) imm_expr.X_add_number,
8391                               (unsigned long) lastpos);
8392                       imm_expr.X_add_number = limlo - lastpos;
8393                     }
8394                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8395                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8396                   imm_expr.X_op = O_absent;
8397                   s = expr_end;
8398                   continue;
8399
8400                 case 'D':
8401                   /* +D is for disassembly only; never match.  */
8402                   break;
8403
8404                 default:
8405                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8406                     *args, insn->name, insn->args);
8407                   /* Further processing is fruitless.  */
8408                   return;
8409                 }
8410               break;
8411
8412             case '<':           /* must be at least one digit */
8413               /*
8414                * According to the manual, if the shift amount is greater
8415                * than 31 or less than 0, then the shift amount should be
8416                * mod 32.  In reality the mips assembler issues an error.
8417                * We issue a warning and mask out all but the low 5 bits.
8418                */
8419               my_getExpression (&imm_expr, s);
8420               check_absolute_expr (ip, &imm_expr);
8421               if ((unsigned long) imm_expr.X_add_number > 31)
8422                 {
8423                   as_warn (_("Improper shift amount (%lu)"),
8424                            (unsigned long) imm_expr.X_add_number);
8425                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8426                 }
8427               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8428               imm_expr.X_op = O_absent;
8429               s = expr_end;
8430               continue;
8431
8432             case '>':           /* shift amount minus 32 */
8433               my_getExpression (&imm_expr, s);
8434               check_absolute_expr (ip, &imm_expr);
8435               if ((unsigned long) imm_expr.X_add_number < 32
8436                   || (unsigned long) imm_expr.X_add_number > 63)
8437                 break;
8438               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8439               imm_expr.X_op = O_absent;
8440               s = expr_end;
8441               continue;
8442
8443             case 'k':           /* cache code */
8444             case 'h':           /* prefx code */
8445               my_getExpression (&imm_expr, s);
8446               check_absolute_expr (ip, &imm_expr);
8447               if ((unsigned long) imm_expr.X_add_number > 31)
8448                 {
8449                   as_warn (_("Invalid value for `%s' (%lu)"),
8450                            ip->insn_mo->name,
8451                            (unsigned long) imm_expr.X_add_number);
8452                   imm_expr.X_add_number &= 0x1f;
8453                 }
8454               if (*args == 'k')
8455                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8456               else
8457                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8458               imm_expr.X_op = O_absent;
8459               s = expr_end;
8460               continue;
8461
8462             case 'c':           /* break code */
8463               my_getExpression (&imm_expr, s);
8464               check_absolute_expr (ip, &imm_expr);
8465               if ((unsigned long) imm_expr.X_add_number > 1023)
8466                 {
8467                   as_warn (_("Illegal break code (%lu)"),
8468                            (unsigned long) imm_expr.X_add_number);
8469                   imm_expr.X_add_number &= OP_MASK_CODE;
8470                 }
8471               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8472               imm_expr.X_op = O_absent;
8473               s = expr_end;
8474               continue;
8475
8476             case 'q':           /* lower break code */
8477               my_getExpression (&imm_expr, s);
8478               check_absolute_expr (ip, &imm_expr);
8479               if ((unsigned long) imm_expr.X_add_number > 1023)
8480                 {
8481                   as_warn (_("Illegal lower break code (%lu)"),
8482                            (unsigned long) imm_expr.X_add_number);
8483                   imm_expr.X_add_number &= OP_MASK_CODE2;
8484                 }
8485               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8486               imm_expr.X_op = O_absent;
8487               s = expr_end;
8488               continue;
8489
8490             case 'B':           /* 20-bit syscall/break code.  */
8491               my_getExpression (&imm_expr, s);
8492               check_absolute_expr (ip, &imm_expr);
8493               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8494                 as_warn (_("Illegal 20-bit code (%lu)"),
8495                          (unsigned long) imm_expr.X_add_number);
8496               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8497               imm_expr.X_op = O_absent;
8498               s = expr_end;
8499               continue;
8500
8501             case 'C':           /* Coprocessor code */
8502               my_getExpression (&imm_expr, s);
8503               check_absolute_expr (ip, &imm_expr);
8504               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8505                 {
8506                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8507                            (unsigned long) imm_expr.X_add_number);
8508                   imm_expr.X_add_number &= ((1 << 25) - 1);
8509                 }
8510               ip->insn_opcode |= imm_expr.X_add_number;
8511               imm_expr.X_op = O_absent;
8512               s = expr_end;
8513               continue;
8514
8515             case 'J':           /* 19-bit wait code.  */
8516               my_getExpression (&imm_expr, s);
8517               check_absolute_expr (ip, &imm_expr);
8518               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8519                 as_warn (_("Illegal 19-bit code (%lu)"),
8520                          (unsigned long) imm_expr.X_add_number);
8521               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8522               imm_expr.X_op = O_absent;
8523               s = expr_end;
8524               continue;
8525
8526             case 'P':           /* Performance register */
8527               my_getExpression (&imm_expr, s);
8528               check_absolute_expr (ip, &imm_expr);
8529               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8530                 {
8531                   as_warn (_("Invalid performance register (%lu)"),
8532                            (unsigned long) imm_expr.X_add_number);
8533                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8534                 }
8535               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8536               imm_expr.X_op = O_absent;
8537               s = expr_end;
8538               continue;
8539
8540             case 'b':           /* base register */
8541             case 'd':           /* destination register */
8542             case 's':           /* source register */
8543             case 't':           /* target register */
8544             case 'r':           /* both target and source */
8545             case 'v':           /* both dest and source */
8546             case 'w':           /* both dest and target */
8547             case 'E':           /* coprocessor target register */
8548             case 'G':           /* coprocessor destination register */
8549             case 'K':           /* 'rdhwr' destination register */
8550             case 'x':           /* ignore register name */
8551             case 'z':           /* must be zero register */
8552             case 'U':           /* destination register (clo/clz).  */
8553               s_reset = s;
8554               if (s[0] == '$')
8555                 {
8556
8557                   if (ISDIGIT (s[1]))
8558                     {
8559                       ++s;
8560                       regno = 0;
8561                       do
8562                         {
8563                           regno *= 10;
8564                           regno += *s - '0';
8565                           ++s;
8566                         }
8567                       while (ISDIGIT (*s));
8568                       if (regno > 31)
8569                         as_bad (_("Invalid register number (%d)"), regno);
8570                     }
8571                   else if (*args == 'E' || *args == 'G' || *args == 'K')
8572                     goto notreg;
8573                   else
8574                     {
8575                       if (s[1] == 'r' && s[2] == 'a')
8576                         {
8577                           s += 3;
8578                           regno = RA;
8579                         }
8580                       else if (s[1] == 'f' && s[2] == 'p')
8581                         {
8582                           s += 3;
8583                           regno = FP;
8584                         }
8585                       else if (s[1] == 's' && s[2] == 'p')
8586                         {
8587                           s += 3;
8588                           regno = SP;
8589                         }
8590                       else if (s[1] == 'g' && s[2] == 'p')
8591                         {
8592                           s += 3;
8593                           regno = GP;
8594                         }
8595                       else if (s[1] == 'a' && s[2] == 't')
8596                         {
8597                           s += 3;
8598                           regno = AT;
8599                         }
8600                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8601                         {
8602                           s += 4;
8603                           regno = KT0;
8604                         }
8605                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8606                         {
8607                           s += 4;
8608                           regno = KT1;
8609                         }
8610                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8611                         {
8612                           s += 5;
8613                           regno = ZERO;
8614                         }
8615                       else if (itbl_have_entries)
8616                         {
8617                           char *p, *n;
8618                           unsigned long r;
8619
8620                           p = s + 1;    /* advance past '$' */
8621                           n = itbl_get_field (&p);  /* n is name */
8622
8623                           /* See if this is a register defined in an
8624                              itbl entry.  */
8625                           if (itbl_get_reg_val (n, &r))
8626                             {
8627                               /* Get_field advances to the start of
8628                                  the next field, so we need to back
8629                                  rack to the end of the last field.  */
8630                               if (p)
8631                                 s = p - 1;
8632                               else
8633                                 s = strchr (s, '\0');
8634                               regno = r;
8635                             }
8636                           else
8637                             goto notreg;
8638                         }
8639                       else
8640                         goto notreg;
8641                     }
8642                   if (regno == AT
8643                       && ! mips_opts.noat
8644                       && *args != 'E'
8645                       && *args != 'G'
8646                       && *args != 'K')
8647                     as_warn (_("Used $at without \".set noat\""));
8648                   c = *args;
8649                   if (*s == ' ')
8650                     ++s;
8651                   if (args[1] != *s)
8652                     {
8653                       if (c == 'r' || c == 'v' || c == 'w')
8654                         {
8655                           regno = lastregno;
8656                           s = s_reset;
8657                           ++args;
8658                         }
8659                     }
8660                   /* 'z' only matches $0.  */
8661                   if (c == 'z' && regno != 0)
8662                     break;
8663
8664         /* Now that we have assembled one operand, we use the args string
8665          * to figure out where it goes in the instruction.  */
8666                   switch (c)
8667                     {
8668                     case 'r':
8669                     case 's':
8670                     case 'v':
8671                     case 'b':
8672                       ip->insn_opcode |= regno << OP_SH_RS;
8673                       break;
8674                     case 'd':
8675                     case 'G':
8676                     case 'K':
8677                       ip->insn_opcode |= regno << OP_SH_RD;
8678                       break;
8679                     case 'U':
8680                       ip->insn_opcode |= regno << OP_SH_RD;
8681                       ip->insn_opcode |= regno << OP_SH_RT;
8682                       break;
8683                     case 'w':
8684                     case 't':
8685                     case 'E':
8686                       ip->insn_opcode |= regno << OP_SH_RT;
8687                       break;
8688                     case 'x':
8689                       /* This case exists because on the r3000 trunc
8690                          expands into a macro which requires a gp
8691                          register.  On the r6000 or r4000 it is
8692                          assembled into a single instruction which
8693                          ignores the register.  Thus the insn version
8694                          is MIPS_ISA2 and uses 'x', and the macro
8695                          version is MIPS_ISA1 and uses 't'.  */
8696                       break;
8697                     case 'z':
8698                       /* This case is for the div instruction, which
8699                          acts differently if the destination argument
8700                          is $0.  This only matches $0, and is checked
8701                          outside the switch.  */
8702                       break;
8703                     case 'D':
8704                       /* Itbl operand; not yet implemented. FIXME ?? */
8705                       break;
8706                       /* What about all other operands like 'i', which
8707                          can be specified in the opcode table? */
8708                     }
8709                   lastregno = regno;
8710                   continue;
8711                 }
8712             notreg:
8713               switch (*args++)
8714                 {
8715                 case 'r':
8716                 case 'v':
8717                   ip->insn_opcode |= lastregno << OP_SH_RS;
8718                   continue;
8719                 case 'w':
8720                   ip->insn_opcode |= lastregno << OP_SH_RT;
8721                   continue;
8722                 }
8723               break;
8724
8725             case 'O':           /* MDMX alignment immediate constant.  */
8726               my_getExpression (&imm_expr, s);
8727               check_absolute_expr (ip, &imm_expr);
8728               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8729                 {
8730                   as_warn ("Improper align amount (%ld), using low bits",
8731                            (long) imm_expr.X_add_number);
8732                   imm_expr.X_add_number &= OP_MASK_ALN;
8733                 }
8734               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8735               imm_expr.X_op = O_absent;
8736               s = expr_end;
8737               continue;
8738
8739             case 'Q':           /* MDMX vector, element sel, or const.  */
8740               if (s[0] != '$')
8741                 {
8742                   /* MDMX Immediate.  */
8743                   my_getExpression (&imm_expr, s);
8744                   check_absolute_expr (ip, &imm_expr);
8745                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8746                     {
8747                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8748                                (long) imm_expr.X_add_number);
8749                       imm_expr.X_add_number &= OP_MASK_FT;
8750                     }
8751                   imm_expr.X_add_number &= OP_MASK_FT;
8752                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8753                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8754                   else
8755                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8756                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8757                   imm_expr.X_op = O_absent;
8758                   s = expr_end;
8759                   continue;
8760                 }
8761               /* Not MDMX Immediate.  Fall through.  */
8762             case 'X':           /* MDMX destination register.  */
8763             case 'Y':           /* MDMX source register.  */
8764             case 'Z':           /* MDMX target register.  */
8765               is_mdmx = 1;
8766             case 'D':           /* floating point destination register */
8767             case 'S':           /* floating point source register */
8768             case 'T':           /* floating point target register */
8769             case 'R':           /* floating point source register */
8770             case 'V':
8771             case 'W':
8772               s_reset = s;
8773               /* Accept $fN for FP and MDMX register numbers, and in
8774                  addition accept $vN for MDMX register numbers.  */
8775               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8776                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8777                       && ISDIGIT (s[2])))
8778                 {
8779                   s += 2;
8780                   regno = 0;
8781                   do
8782                     {
8783                       regno *= 10;
8784                       regno += *s - '0';
8785                       ++s;
8786                     }
8787                   while (ISDIGIT (*s));
8788
8789                   if (regno > 31)
8790                     as_bad (_("Invalid float register number (%d)"), regno);
8791
8792                   if ((regno & 1) != 0
8793                       && HAVE_32BIT_FPRS
8794                       && ! (strcmp (str, "mtc1") == 0
8795                             || strcmp (str, "mfc1") == 0
8796                             || strcmp (str, "lwc1") == 0
8797                             || strcmp (str, "swc1") == 0
8798                             || strcmp (str, "l.s") == 0
8799                             || strcmp (str, "s.s") == 0))
8800                     as_warn (_("Float register should be even, was %d"),
8801                              regno);
8802
8803                   c = *args;
8804                   if (*s == ' ')
8805                     ++s;
8806                   if (args[1] != *s)
8807                     {
8808                       if (c == 'V' || c == 'W')
8809                         {
8810                           regno = lastregno;
8811                           s = s_reset;
8812                           ++args;
8813                         }
8814                     }
8815                   switch (c)
8816                     {
8817                     case 'D':
8818                     case 'X':
8819                       ip->insn_opcode |= regno << OP_SH_FD;
8820                       break;
8821                     case 'V':
8822                     case 'S':
8823                     case 'Y':
8824                       ip->insn_opcode |= regno << OP_SH_FS;
8825                       break;
8826                     case 'Q':
8827                       /* This is like 'Z', but also needs to fix the MDMX
8828                          vector/scalar select bits.  Note that the
8829                          scalar immediate case is handled above.  */
8830                       if (*s == '[')
8831                         {
8832                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8833                           int max_el = (is_qh ? 3 : 7);
8834                           s++;
8835                           my_getExpression(&imm_expr, s);
8836                           check_absolute_expr (ip, &imm_expr);
8837                           s = expr_end;
8838                           if (imm_expr.X_add_number > max_el)
8839                             as_bad(_("Bad element selector %ld"),
8840                                    (long) imm_expr.X_add_number);
8841                           imm_expr.X_add_number &= max_el;
8842                           ip->insn_opcode |= (imm_expr.X_add_number
8843                                               << (OP_SH_VSEL +
8844                                                   (is_qh ? 2 : 1)));
8845                           if (*s != ']')
8846                             as_warn(_("Expecting ']' found '%s'"), s);
8847                           else
8848                             s++;
8849                         }
8850                       else
8851                         {
8852                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8853                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8854                                                 << OP_SH_VSEL);
8855                           else
8856                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8857                                                 OP_SH_VSEL);
8858                         }
8859                       /* Fall through */
8860                     case 'W':
8861                     case 'T':
8862                     case 'Z':
8863                       ip->insn_opcode |= regno << OP_SH_FT;
8864                       break;
8865                     case 'R':
8866                       ip->insn_opcode |= regno << OP_SH_FR;
8867                       break;
8868                     }
8869                   lastregno = regno;
8870                   continue;
8871                 }
8872
8873               switch (*args++)
8874                 {
8875                 case 'V':
8876                   ip->insn_opcode |= lastregno << OP_SH_FS;
8877                   continue;
8878                 case 'W':
8879                   ip->insn_opcode |= lastregno << OP_SH_FT;
8880                   continue;
8881                 }
8882               break;
8883
8884             case 'I':
8885               my_getExpression (&imm_expr, s);
8886               if (imm_expr.X_op != O_big
8887                   && imm_expr.X_op != O_constant)
8888                 insn_error = _("absolute expression required");
8889               s = expr_end;
8890               continue;
8891
8892             case 'A':
8893               my_getExpression (&offset_expr, s);
8894               *imm_reloc = BFD_RELOC_32;
8895               s = expr_end;
8896               continue;
8897
8898             case 'F':
8899             case 'L':
8900             case 'f':
8901             case 'l':
8902               {
8903                 int f64;
8904                 int using_gprs;
8905                 char *save_in;
8906                 char *err;
8907                 unsigned char temp[8];
8908                 int len;
8909                 unsigned int length;
8910                 segT seg;
8911                 subsegT subseg;
8912                 char *p;
8913
8914                 /* These only appear as the last operand in an
8915                    instruction, and every instruction that accepts
8916                    them in any variant accepts them in all variants.
8917                    This means we don't have to worry about backing out
8918                    any changes if the instruction does not match.
8919
8920                    The difference between them is the size of the
8921                    floating point constant and where it goes.  For 'F'
8922                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8923                    is 32 bits.  Where the constant is placed is based
8924                    on how the MIPS assembler does things:
8925                     F -- .rdata
8926                     L -- .lit8
8927                     f -- immediate value
8928                     l -- .lit4
8929
8930                     The .lit4 and .lit8 sections are only used if
8931                     permitted by the -G argument.
8932
8933                     When generating embedded PIC code, we use the
8934                     .lit8 section but not the .lit4 section (we can do
8935                     .lit4 inline easily; we need to put .lit8
8936                     somewhere in the data segment, and using .lit8
8937                     permits the linker to eventually combine identical
8938                     .lit8 entries).
8939
8940                     The code below needs to know whether the target register
8941                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8942                     'F' are used with GPR-based instructions and 'l' and
8943                     'L' are used with FPR-based instructions.  */
8944
8945                 f64 = *args == 'F' || *args == 'L';
8946                 using_gprs = *args == 'F' || *args == 'f';
8947
8948                 save_in = input_line_pointer;
8949                 input_line_pointer = s;
8950                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8951                 length = len;
8952                 s = input_line_pointer;
8953                 input_line_pointer = save_in;
8954                 if (err != NULL && *err != '\0')
8955                   {
8956                     as_bad (_("Bad floating point constant: %s"), err);
8957                     memset (temp, '\0', sizeof temp);
8958                     length = f64 ? 8 : 4;
8959                   }
8960
8961                 assert (length == (unsigned) (f64 ? 8 : 4));
8962
8963                 if (*args == 'f'
8964                     || (*args == 'l'
8965                         && (! USE_GLOBAL_POINTER_OPT
8966                             || mips_pic == EMBEDDED_PIC
8967                             || g_switch_value < 4
8968                             || (temp[0] == 0 && temp[1] == 0)
8969                             || (temp[2] == 0 && temp[3] == 0))))
8970                   {
8971                     imm_expr.X_op = O_constant;
8972                     if (! target_big_endian)
8973                       imm_expr.X_add_number = bfd_getl32 (temp);
8974                     else
8975                       imm_expr.X_add_number = bfd_getb32 (temp);
8976                   }
8977                 else if (length > 4
8978                          && ! mips_disable_float_construction
8979                          /* Constants can only be constructed in GPRs and
8980                             copied to FPRs if the GPRs are at least as wide
8981                             as the FPRs.  Force the constant into memory if
8982                             we are using 64-bit FPRs but the GPRs are only
8983                             32 bits wide.  */
8984                          && (using_gprs
8985                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8986                          && ((temp[0] == 0 && temp[1] == 0)
8987                              || (temp[2] == 0 && temp[3] == 0))
8988                          && ((temp[4] == 0 && temp[5] == 0)
8989                              || (temp[6] == 0 && temp[7] == 0)))
8990                   {
8991                     /* The value is simple enough to load with a couple of
8992                        instructions.  If using 32-bit registers, set
8993                        imm_expr to the high order 32 bits and offset_expr to
8994                        the low order 32 bits.  Otherwise, set imm_expr to
8995                        the entire 64 bit constant.  */
8996                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8997                       {
8998                         imm_expr.X_op = O_constant;
8999                         offset_expr.X_op = O_constant;
9000                         if (! target_big_endian)
9001                           {
9002                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
9003                             offset_expr.X_add_number = bfd_getl32 (temp);
9004                           }
9005                         else
9006                           {
9007                             imm_expr.X_add_number = bfd_getb32 (temp);
9008                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9009                           }
9010                         if (offset_expr.X_add_number == 0)
9011                           offset_expr.X_op = O_absent;
9012                       }
9013                     else if (sizeof (imm_expr.X_add_number) > 4)
9014                       {
9015                         imm_expr.X_op = O_constant;
9016                         if (! target_big_endian)
9017                           imm_expr.X_add_number = bfd_getl64 (temp);
9018                         else
9019                           imm_expr.X_add_number = bfd_getb64 (temp);
9020                       }
9021                     else
9022                       {
9023                         imm_expr.X_op = O_big;
9024                         imm_expr.X_add_number = 4;
9025                         if (! target_big_endian)
9026                           {
9027                             generic_bignum[0] = bfd_getl16 (temp);
9028                             generic_bignum[1] = bfd_getl16 (temp + 2);
9029                             generic_bignum[2] = bfd_getl16 (temp + 4);
9030                             generic_bignum[3] = bfd_getl16 (temp + 6);
9031                           }
9032                         else
9033                           {
9034                             generic_bignum[0] = bfd_getb16 (temp + 6);
9035                             generic_bignum[1] = bfd_getb16 (temp + 4);
9036                             generic_bignum[2] = bfd_getb16 (temp + 2);
9037                             generic_bignum[3] = bfd_getb16 (temp);
9038                           }
9039                       }
9040                   }
9041                 else
9042                   {
9043                     const char *newname;
9044                     segT new_seg;
9045
9046                     /* Switch to the right section.  */
9047                     seg = now_seg;
9048                     subseg = now_subseg;
9049                     switch (*args)
9050                       {
9051                       default: /* unused default case avoids warnings.  */
9052                       case 'L':
9053                         newname = RDATA_SECTION_NAME;
9054                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9055                             || mips_pic == EMBEDDED_PIC)
9056                           newname = ".lit8";
9057                         break;
9058                       case 'F':
9059                         if (mips_pic == EMBEDDED_PIC)
9060                           newname = ".lit8";
9061                         else
9062                           newname = RDATA_SECTION_NAME;
9063                         break;
9064                       case 'l':
9065                         assert (!USE_GLOBAL_POINTER_OPT
9066                                 || g_switch_value >= 4);
9067                         newname = ".lit4";
9068                         break;
9069                       }
9070                     new_seg = subseg_new (newname, (subsegT) 0);
9071                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9072                       bfd_set_section_flags (stdoutput, new_seg,
9073                                              (SEC_ALLOC
9074                                               | SEC_LOAD
9075                                               | SEC_READONLY
9076                                               | SEC_DATA));
9077                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9078                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9079                         && strcmp (TARGET_OS, "elf") != 0)
9080                       record_alignment (new_seg, 4);
9081                     else
9082                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9083                     if (seg == now_seg)
9084                       as_bad (_("Can't use floating point insn in this section"));
9085
9086                     /* Set the argument to the current address in the
9087                        section.  */
9088                     offset_expr.X_op = O_symbol;
9089                     offset_expr.X_add_symbol =
9090                       symbol_new ("L0\001", now_seg,
9091                                   (valueT) frag_now_fix (), frag_now);
9092                     offset_expr.X_add_number = 0;
9093
9094                     /* Put the floating point number into the section.  */
9095                     p = frag_more ((int) length);
9096                     memcpy (p, temp, length);
9097
9098                     /* Switch back to the original section.  */
9099                     subseg_set (seg, subseg);
9100                   }
9101               }
9102               continue;
9103
9104             case 'i':           /* 16 bit unsigned immediate */
9105             case 'j':           /* 16 bit signed immediate */
9106               *imm_reloc = BFD_RELOC_LO16;
9107               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9108                 {
9109                   int more;
9110                   offsetT minval, maxval;
9111
9112                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9113                           && strcmp (insn->name, insn[1].name) == 0);
9114
9115                   /* If the expression was written as an unsigned number,
9116                      only treat it as signed if there are no more
9117                      alternatives.  */
9118                   if (more
9119                       && *args == 'j'
9120                       && sizeof (imm_expr.X_add_number) <= 4
9121                       && imm_expr.X_op == O_constant
9122                       && imm_expr.X_add_number < 0
9123                       && imm_expr.X_unsigned
9124                       && HAVE_64BIT_GPRS)
9125                     break;
9126
9127                   /* For compatibility with older assemblers, we accept
9128                      0x8000-0xffff as signed 16-bit numbers when only
9129                      signed numbers are allowed.  */
9130                   if (*args == 'i')
9131                     minval = 0, maxval = 0xffff;
9132                   else if (more)
9133                     minval = -0x8000, maxval = 0x7fff;
9134                   else
9135                     minval = -0x8000, maxval = 0xffff;
9136
9137                   if (imm_expr.X_op != O_constant
9138                       || imm_expr.X_add_number < minval
9139                       || imm_expr.X_add_number > maxval)
9140                     {
9141                       if (more)
9142                         break;
9143                       if (imm_expr.X_op == O_constant
9144                           || imm_expr.X_op == O_big)
9145                         as_bad (_("expression out of range"));
9146                     }
9147                 }
9148               s = expr_end;
9149               continue;
9150
9151             case 'o':           /* 16 bit offset */
9152               /* Check whether there is only a single bracketed expression
9153                  left.  If so, it must be the base register and the
9154                  constant must be zero.  */
9155               if (*s == '(' && strchr (s + 1, '(') == 0)
9156                 {
9157                   offset_expr.X_op = O_constant;
9158                   offset_expr.X_add_number = 0;
9159                   continue;
9160                 }
9161
9162               /* If this value won't fit into a 16 bit offset, then go
9163                  find a macro that will generate the 32 bit offset
9164                  code pattern.  */
9165               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9166                   && (offset_expr.X_op != O_constant
9167                       || offset_expr.X_add_number >= 0x8000
9168                       || offset_expr.X_add_number < -0x8000))
9169                 break;
9170
9171               s = expr_end;
9172               continue;
9173
9174             case 'p':           /* pc relative offset */
9175               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9176               my_getExpression (&offset_expr, s);
9177               s = expr_end;
9178               continue;
9179
9180             case 'u':           /* upper 16 bits */
9181               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9182                   && imm_expr.X_op == O_constant
9183                   && (imm_expr.X_add_number < 0
9184                       || imm_expr.X_add_number >= 0x10000))
9185                 as_bad (_("lui expression not in range 0..65535"));
9186               s = expr_end;
9187               continue;
9188
9189             case 'a':           /* 26 bit address */
9190               my_getExpression (&offset_expr, s);
9191               s = expr_end;
9192               *offset_reloc = BFD_RELOC_MIPS_JMP;
9193               continue;
9194
9195             case 'N':           /* 3 bit branch condition code */
9196             case 'M':           /* 3 bit compare condition code */
9197               if (strncmp (s, "$fcc", 4) != 0)
9198                 break;
9199               s += 4;
9200               regno = 0;
9201               do
9202                 {
9203                   regno *= 10;
9204                   regno += *s - '0';
9205                   ++s;
9206                 }
9207               while (ISDIGIT (*s));
9208               if (regno > 7)
9209                 as_bad (_("invalid condition code register $fcc%d"), regno);
9210               if (*args == 'N')
9211                 ip->insn_opcode |= regno << OP_SH_BCC;
9212               else
9213                 ip->insn_opcode |= regno << OP_SH_CCC;
9214               continue;
9215
9216             case 'H':
9217               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9218                 s += 2;
9219               if (ISDIGIT (*s))
9220                 {
9221                   c = 0;
9222                   do
9223                     {
9224                       c *= 10;
9225                       c += *s - '0';
9226                       ++s;
9227                     }
9228                   while (ISDIGIT (*s));
9229                 }
9230               else
9231                 c = 8; /* Invalid sel value.  */
9232
9233               if (c > 7)
9234                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9235               ip->insn_opcode |= c;
9236               continue;
9237
9238             case 'e':
9239               /* Must be at least one digit.  */
9240               my_getExpression (&imm_expr, s);
9241               check_absolute_expr (ip, &imm_expr);
9242
9243               if ((unsigned long) imm_expr.X_add_number
9244                   > (unsigned long) OP_MASK_VECBYTE)
9245                 {
9246                   as_bad (_("bad byte vector index (%ld)"),
9247                            (long) imm_expr.X_add_number);
9248                   imm_expr.X_add_number = 0;
9249                 }
9250
9251               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9252               imm_expr.X_op = O_absent;
9253               s = expr_end;
9254               continue;
9255
9256             case '%':
9257               my_getExpression (&imm_expr, s);
9258               check_absolute_expr (ip, &imm_expr);
9259
9260               if ((unsigned long) imm_expr.X_add_number
9261                   > (unsigned long) OP_MASK_VECALIGN)
9262                 {
9263                   as_bad (_("bad byte vector index (%ld)"),
9264                            (long) imm_expr.X_add_number);
9265                   imm_expr.X_add_number = 0;
9266                 }
9267
9268               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9269               imm_expr.X_op = O_absent;
9270               s = expr_end;
9271               continue;
9272
9273             default:
9274               as_bad (_("bad char = '%c'\n"), *args);
9275               internalError ();
9276             }
9277           break;
9278         }
9279       /* Args don't match.  */
9280       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9281           !strcmp (insn->name, insn[1].name))
9282         {
9283           ++insn;
9284           s = argsStart;
9285           insn_error = _("illegal operands");
9286           continue;
9287         }
9288       if (save_c)
9289         *(--s) = save_c;
9290       insn_error = _("illegal operands");
9291       return;
9292     }
9293 }
9294
9295 /* This routine assembles an instruction into its binary format when
9296    assembling for the mips16.  As a side effect, it sets one of the
9297    global variables imm_reloc or offset_reloc to the type of
9298    relocation to do if one of the operands is an address expression.
9299    It also sets mips16_small and mips16_ext if the user explicitly
9300    requested a small or extended instruction.  */
9301
9302 static void
9303 mips16_ip (str, ip)
9304      char *str;
9305      struct mips_cl_insn *ip;
9306 {
9307   char *s;
9308   const char *args;
9309   struct mips_opcode *insn;
9310   char *argsstart;
9311   unsigned int regno;
9312   unsigned int lastregno = 0;
9313   char *s_reset;
9314
9315   insn_error = NULL;
9316
9317   mips16_small = FALSE;
9318   mips16_ext = FALSE;
9319
9320   for (s = str; ISLOWER (*s); ++s)
9321     ;
9322   switch (*s)
9323     {
9324     case '\0':
9325       break;
9326
9327     case ' ':
9328       *s++ = '\0';
9329       break;
9330
9331     case '.':
9332       if (s[1] == 't' && s[2] == ' ')
9333         {
9334           *s = '\0';
9335           mips16_small = TRUE;
9336           s += 3;
9337           break;
9338         }
9339       else if (s[1] == 'e' && s[2] == ' ')
9340         {
9341           *s = '\0';
9342           mips16_ext = TRUE;
9343           s += 3;
9344           break;
9345         }
9346       /* Fall through.  */
9347     default:
9348       insn_error = _("unknown opcode");
9349       return;
9350     }
9351
9352   if (mips_opts.noautoextend && ! mips16_ext)
9353     mips16_small = TRUE;
9354
9355   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9356     {
9357       insn_error = _("unrecognized opcode");
9358       return;
9359     }
9360
9361   argsstart = s;
9362   for (;;)
9363     {
9364       assert (strcmp (insn->name, str) == 0);
9365
9366       ip->insn_mo = insn;
9367       ip->insn_opcode = insn->match;
9368       ip->use_extend = FALSE;
9369       imm_expr.X_op = O_absent;
9370       imm_reloc[0] = BFD_RELOC_UNUSED;
9371       imm_reloc[1] = BFD_RELOC_UNUSED;
9372       imm_reloc[2] = BFD_RELOC_UNUSED;
9373       offset_expr.X_op = O_absent;
9374       offset_reloc[0] = BFD_RELOC_UNUSED;
9375       offset_reloc[1] = BFD_RELOC_UNUSED;
9376       offset_reloc[2] = BFD_RELOC_UNUSED;
9377       for (args = insn->args; 1; ++args)
9378         {
9379           int c;
9380
9381           if (*s == ' ')
9382             ++s;
9383
9384           /* In this switch statement we call break if we did not find
9385              a match, continue if we did find a match, or return if we
9386              are done.  */
9387
9388           c = *args;
9389           switch (c)
9390             {
9391             case '\0':
9392               if (*s == '\0')
9393                 {
9394                   /* Stuff the immediate value in now, if we can.  */
9395                   if (imm_expr.X_op == O_constant
9396                       && *imm_reloc > BFD_RELOC_UNUSED
9397                       && insn->pinfo != INSN_MACRO)
9398                     {
9399                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9400                                     imm_expr.X_add_number, TRUE, mips16_small,
9401                                     mips16_ext, &ip->insn_opcode,
9402                                     &ip->use_extend, &ip->extend);
9403                       imm_expr.X_op = O_absent;
9404                       *imm_reloc = BFD_RELOC_UNUSED;
9405                     }
9406
9407                   return;
9408                 }
9409               break;
9410
9411             case ',':
9412               if (*s++ == c)
9413                 continue;
9414               s--;
9415               switch (*++args)
9416                 {
9417                 case 'v':
9418                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9419                   continue;
9420                 case 'w':
9421                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9422                   continue;
9423                 }
9424               break;
9425
9426             case '(':
9427             case ')':
9428               if (*s++ == c)
9429                 continue;
9430               break;
9431
9432             case 'v':
9433             case 'w':
9434               if (s[0] != '$')
9435                 {
9436                   if (c == 'v')
9437                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9438                   else
9439                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9440                   ++args;
9441                   continue;
9442                 }
9443               /* Fall through.  */
9444             case 'x':
9445             case 'y':
9446             case 'z':
9447             case 'Z':
9448             case '0':
9449             case 'S':
9450             case 'R':
9451             case 'X':
9452             case 'Y':
9453               if (s[0] != '$')
9454                 break;
9455               s_reset = s;
9456               if (ISDIGIT (s[1]))
9457                 {
9458                   ++s;
9459                   regno = 0;
9460                   do
9461                     {
9462                       regno *= 10;
9463                       regno += *s - '0';
9464                       ++s;
9465                     }
9466                   while (ISDIGIT (*s));
9467                   if (regno > 31)
9468                     {
9469                       as_bad (_("invalid register number (%d)"), regno);
9470                       regno = 2;
9471                     }
9472                 }
9473               else
9474                 {
9475                   if (s[1] == 'r' && s[2] == 'a')
9476                     {
9477                       s += 3;
9478                       regno = RA;
9479                     }
9480                   else if (s[1] == 'f' && s[2] == 'p')
9481                     {
9482                       s += 3;
9483                       regno = FP;
9484                     }
9485                   else if (s[1] == 's' && s[2] == 'p')
9486                     {
9487                       s += 3;
9488                       regno = SP;
9489                     }
9490                   else if (s[1] == 'g' && s[2] == 'p')
9491                     {
9492                       s += 3;
9493                       regno = GP;
9494                     }
9495                   else if (s[1] == 'a' && s[2] == 't')
9496                     {
9497                       s += 3;
9498                       regno = AT;
9499                     }
9500                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9501                     {
9502                       s += 4;
9503                       regno = KT0;
9504                     }
9505                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9506                     {
9507                       s += 4;
9508                       regno = KT1;
9509                     }
9510                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9511                     {
9512                       s += 5;
9513                       regno = ZERO;
9514                     }
9515                   else
9516                     break;
9517                 }
9518
9519               if (*s == ' ')
9520                 ++s;
9521               if (args[1] != *s)
9522                 {
9523                   if (c == 'v' || c == 'w')
9524                     {
9525                       regno = mips16_to_32_reg_map[lastregno];
9526                       s = s_reset;
9527                       ++args;
9528                     }
9529                 }
9530
9531               switch (c)
9532                 {
9533                 case 'x':
9534                 case 'y':
9535                 case 'z':
9536                 case 'v':
9537                 case 'w':
9538                 case 'Z':
9539                   regno = mips32_to_16_reg_map[regno];
9540                   break;
9541
9542                 case '0':
9543                   if (regno != 0)
9544                     regno = ILLEGAL_REG;
9545                   break;
9546
9547                 case 'S':
9548                   if (regno != SP)
9549                     regno = ILLEGAL_REG;
9550                   break;
9551
9552                 case 'R':
9553                   if (regno != RA)
9554                     regno = ILLEGAL_REG;
9555                   break;
9556
9557                 case 'X':
9558                 case 'Y':
9559                   if (regno == AT && ! mips_opts.noat)
9560                     as_warn (_("used $at without \".set noat\""));
9561                   break;
9562
9563                 default:
9564                   internalError ();
9565                 }
9566
9567               if (regno == ILLEGAL_REG)
9568                 break;
9569
9570               switch (c)
9571                 {
9572                 case 'x':
9573                 case 'v':
9574                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9575                   break;
9576                 case 'y':
9577                 case 'w':
9578                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9579                   break;
9580                 case 'z':
9581                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9582                   break;
9583                 case 'Z':
9584                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9585                 case '0':
9586                 case 'S':
9587                 case 'R':
9588                   break;
9589                 case 'X':
9590                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9591                   break;
9592                 case 'Y':
9593                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9594                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9595                   break;
9596                 default:
9597                   internalError ();
9598                 }
9599
9600               lastregno = regno;
9601               continue;
9602
9603             case 'P':
9604               if (strncmp (s, "$pc", 3) == 0)
9605                 {
9606                   s += 3;
9607                   continue;
9608                 }
9609               break;
9610
9611             case '<':
9612             case '>':
9613             case '[':
9614             case ']':
9615             case '4':
9616             case '5':
9617             case 'H':
9618             case 'W':
9619             case 'D':
9620             case 'j':
9621             case '8':
9622             case 'V':
9623             case 'C':
9624             case 'U':
9625             case 'k':
9626             case 'K':
9627               if (s[0] == '%'
9628                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9629                 {
9630                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9631                      and generate the appropriate reloc.  If the text
9632                      inside %gprel is not a symbol name with an
9633                      optional offset, then we generate a normal reloc
9634                      and will probably fail later.  */
9635                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9636                   if (imm_expr.X_op == O_symbol)
9637                     {
9638                       mips16_ext = TRUE;
9639                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9640                       s = expr_end;
9641                       ip->use_extend = TRUE;
9642                       ip->extend = 0;
9643                       continue;
9644                     }
9645                 }
9646               else
9647                 {
9648                   /* Just pick up a normal expression.  */
9649                   my_getExpression (&imm_expr, s);
9650                 }
9651
9652               if (imm_expr.X_op == O_register)
9653                 {
9654                   /* What we thought was an expression turned out to
9655                      be a register.  */
9656
9657                   if (s[0] == '(' && args[1] == '(')
9658                     {
9659                       /* It looks like the expression was omitted
9660                          before a register indirection, which means
9661                          that the expression is implicitly zero.  We
9662                          still set up imm_expr, so that we handle
9663                          explicit extensions correctly.  */
9664                       imm_expr.X_op = O_constant;
9665                       imm_expr.X_add_number = 0;
9666                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9667                       continue;
9668                     }
9669
9670                   break;
9671                 }
9672
9673               /* We need to relax this instruction.  */
9674               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9675               s = expr_end;
9676               continue;
9677
9678             case 'p':
9679             case 'q':
9680             case 'A':
9681             case 'B':
9682             case 'E':
9683               /* We use offset_reloc rather than imm_reloc for the PC
9684                  relative operands.  This lets macros with both
9685                  immediate and address operands work correctly.  */
9686               my_getExpression (&offset_expr, s);
9687
9688               if (offset_expr.X_op == O_register)
9689                 break;
9690
9691               /* We need to relax this instruction.  */
9692               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9693               s = expr_end;
9694               continue;
9695
9696             case '6':           /* break code */
9697               my_getExpression (&imm_expr, s);
9698               check_absolute_expr (ip, &imm_expr);
9699               if ((unsigned long) imm_expr.X_add_number > 63)
9700                 {
9701                   as_warn (_("Invalid value for `%s' (%lu)"),
9702                            ip->insn_mo->name,
9703                            (unsigned long) imm_expr.X_add_number);
9704                   imm_expr.X_add_number &= 0x3f;
9705                 }
9706               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9707               imm_expr.X_op = O_absent;
9708               s = expr_end;
9709               continue;
9710
9711             case 'a':           /* 26 bit address */
9712               my_getExpression (&offset_expr, s);
9713               s = expr_end;
9714               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9715               ip->insn_opcode <<= 16;
9716               continue;
9717
9718             case 'l':           /* register list for entry macro */
9719             case 'L':           /* register list for exit macro */
9720               {
9721                 int mask;
9722
9723                 if (c == 'l')
9724                   mask = 0;
9725                 else
9726                   mask = 7 << 3;
9727                 while (*s != '\0')
9728                   {
9729                     int freg, reg1, reg2;
9730
9731                     while (*s == ' ' || *s == ',')
9732                       ++s;
9733                     if (*s != '$')
9734                       {
9735                         as_bad (_("can't parse register list"));
9736                         break;
9737                       }
9738                     ++s;
9739                     if (*s != 'f')
9740                       freg = 0;
9741                     else
9742                       {
9743                         freg = 1;
9744                         ++s;
9745                       }
9746                     reg1 = 0;
9747                     while (ISDIGIT (*s))
9748                       {
9749                         reg1 *= 10;
9750                         reg1 += *s - '0';
9751                         ++s;
9752                       }
9753                     if (*s == ' ')
9754                       ++s;
9755                     if (*s != '-')
9756                       reg2 = reg1;
9757                     else
9758                       {
9759                         ++s;
9760                         if (*s != '$')
9761                           break;
9762                         ++s;
9763                         if (freg)
9764                           {
9765                             if (*s == 'f')
9766                               ++s;
9767                             else
9768                               {
9769                                 as_bad (_("invalid register list"));
9770                                 break;
9771                               }
9772                           }
9773                         reg2 = 0;
9774                         while (ISDIGIT (*s))
9775                           {
9776                             reg2 *= 10;
9777                             reg2 += *s - '0';
9778                             ++s;
9779                           }
9780                       }
9781                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9782                       {
9783                         mask &= ~ (7 << 3);
9784                         mask |= 5 << 3;
9785                       }
9786                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9787                       {
9788                         mask &= ~ (7 << 3);
9789                         mask |= 6 << 3;
9790                       }
9791                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9792                       mask |= (reg2 - 3) << 3;
9793                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9794                       mask |= (reg2 - 15) << 1;
9795                     else if (reg1 == RA && reg2 == RA)
9796                       mask |= 1;
9797                     else
9798                       {
9799                         as_bad (_("invalid register list"));
9800                         break;
9801                       }
9802                   }
9803                 /* The mask is filled in in the opcode table for the
9804                    benefit of the disassembler.  We remove it before
9805                    applying the actual mask.  */
9806                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9807                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9808               }
9809             continue;
9810
9811             case 'e':           /* extend code */
9812               my_getExpression (&imm_expr, s);
9813               check_absolute_expr (ip, &imm_expr);
9814               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9815                 {
9816                   as_warn (_("Invalid value for `%s' (%lu)"),
9817                            ip->insn_mo->name,
9818                            (unsigned long) imm_expr.X_add_number);
9819                   imm_expr.X_add_number &= 0x7ff;
9820                 }
9821               ip->insn_opcode |= imm_expr.X_add_number;
9822               imm_expr.X_op = O_absent;
9823               s = expr_end;
9824               continue;
9825
9826             default:
9827               internalError ();
9828             }
9829           break;
9830         }
9831
9832       /* Args don't match.  */
9833       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9834           strcmp (insn->name, insn[1].name) == 0)
9835         {
9836           ++insn;
9837           s = argsstart;
9838           continue;
9839         }
9840
9841       insn_error = _("illegal operands");
9842
9843       return;
9844     }
9845 }
9846
9847 /* This structure holds information we know about a mips16 immediate
9848    argument type.  */
9849
9850 struct mips16_immed_operand
9851 {
9852   /* The type code used in the argument string in the opcode table.  */
9853   int type;
9854   /* The number of bits in the short form of the opcode.  */
9855   int nbits;
9856   /* The number of bits in the extended form of the opcode.  */
9857   int extbits;
9858   /* The amount by which the short form is shifted when it is used;
9859      for example, the sw instruction has a shift count of 2.  */
9860   int shift;
9861   /* The amount by which the short form is shifted when it is stored
9862      into the instruction code.  */
9863   int op_shift;
9864   /* Non-zero if the short form is unsigned.  */
9865   int unsp;
9866   /* Non-zero if the extended form is unsigned.  */
9867   int extu;
9868   /* Non-zero if the value is PC relative.  */
9869   int pcrel;
9870 };
9871
9872 /* The mips16 immediate operand types.  */
9873
9874 static const struct mips16_immed_operand mips16_immed_operands[] =
9875 {
9876   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9877   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9878   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9879   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9880   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9881   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9882   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9883   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9884   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9885   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9886   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9887   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9888   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9889   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9890   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9891   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9892   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9893   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9894   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9895   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9896   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9897 };
9898
9899 #define MIPS16_NUM_IMMED \
9900   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9901
9902 /* Handle a mips16 instruction with an immediate value.  This or's the
9903    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9904    whether an extended value is needed; if one is needed, it sets
9905    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9906    If SMALL is true, an unextended opcode was explicitly requested.
9907    If EXT is true, an extended opcode was explicitly requested.  If
9908    WARN is true, warn if EXT does not match reality.  */
9909
9910 static void
9911 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9912               extend)
9913      char *file;
9914      unsigned int line;
9915      int type;
9916      offsetT val;
9917      bfd_boolean warn;
9918      bfd_boolean small;
9919      bfd_boolean ext;
9920      unsigned long *insn;
9921      bfd_boolean *use_extend;
9922      unsigned short *extend;
9923 {
9924   register const struct mips16_immed_operand *op;
9925   int mintiny, maxtiny;
9926   bfd_boolean needext;
9927
9928   op = mips16_immed_operands;
9929   while (op->type != type)
9930     {
9931       ++op;
9932       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9933     }
9934
9935   if (op->unsp)
9936     {
9937       if (type == '<' || type == '>' || type == '[' || type == ']')
9938         {
9939           mintiny = 1;
9940           maxtiny = 1 << op->nbits;
9941         }
9942       else
9943         {
9944           mintiny = 0;
9945           maxtiny = (1 << op->nbits) - 1;
9946         }
9947     }
9948   else
9949     {
9950       mintiny = - (1 << (op->nbits - 1));
9951       maxtiny = (1 << (op->nbits - 1)) - 1;
9952     }
9953
9954   /* Branch offsets have an implicit 0 in the lowest bit.  */
9955   if (type == 'p' || type == 'q')
9956     val /= 2;
9957
9958   if ((val & ((1 << op->shift) - 1)) != 0
9959       || val < (mintiny << op->shift)
9960       || val > (maxtiny << op->shift))
9961     needext = TRUE;
9962   else
9963     needext = FALSE;
9964
9965   if (warn && ext && ! needext)
9966     as_warn_where (file, line,
9967                    _("extended operand requested but not required"));
9968   if (small && needext)
9969     as_bad_where (file, line, _("invalid unextended operand value"));
9970
9971   if (small || (! ext && ! needext))
9972     {
9973       int insnval;
9974
9975       *use_extend = FALSE;
9976       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9977       insnval <<= op->op_shift;
9978       *insn |= insnval;
9979     }
9980   else
9981     {
9982       long minext, maxext;
9983       int extval;
9984
9985       if (op->extu)
9986         {
9987           minext = 0;
9988           maxext = (1 << op->extbits) - 1;
9989         }
9990       else
9991         {
9992           minext = - (1 << (op->extbits - 1));
9993           maxext = (1 << (op->extbits - 1)) - 1;
9994         }
9995       if (val < minext || val > maxext)
9996         as_bad_where (file, line,
9997                       _("operand value out of range for instruction"));
9998
9999       *use_extend = TRUE;
10000       if (op->extbits == 16)
10001         {
10002           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10003           val &= 0x1f;
10004         }
10005       else if (op->extbits == 15)
10006         {
10007           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10008           val &= 0xf;
10009         }
10010       else
10011         {
10012           extval = ((val & 0x1f) << 6) | (val & 0x20);
10013           val = 0;
10014         }
10015
10016       *extend = (unsigned short) extval;
10017       *insn |= val;
10018     }
10019 }
10020 \f
10021 static const struct percent_op_match
10022 {
10023   const char *str;
10024   bfd_reloc_code_real_type reloc;
10025 } percent_op[] =
10026 {
10027   {"%lo", BFD_RELOC_LO16},
10028 #ifdef OBJ_ELF
10029   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10030   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10031   {"%call16", BFD_RELOC_MIPS_CALL16},
10032   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10033   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10034   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10035   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10036   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10037   {"%got", BFD_RELOC_MIPS_GOT16},
10038   {"%gp_rel", BFD_RELOC_GPREL16},
10039   {"%half", BFD_RELOC_16},
10040   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10041   {"%higher", BFD_RELOC_MIPS_HIGHER},
10042   {"%neg", BFD_RELOC_MIPS_SUB},
10043 #endif
10044   {"%hi", BFD_RELOC_HI16_S}
10045 };
10046
10047
10048 /* Return true if *STR points to a relocation operator.  When returning true,
10049    move *STR over the operator and store its relocation code in *RELOC.
10050    Leave both *STR and *RELOC alone when returning false.  */
10051
10052 static bfd_boolean
10053 parse_relocation (str, reloc)
10054      char **str;
10055      bfd_reloc_code_real_type *reloc;
10056 {
10057   size_t i;
10058
10059   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10060     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10061       {
10062         *str += strlen (percent_op[i].str);
10063         *reloc = percent_op[i].reloc;
10064
10065         /* Check whether the output BFD supports this relocation.
10066            If not, issue an error and fall back on something safe.  */
10067         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10068           {
10069             as_bad ("relocation %s isn't supported by the current ABI",
10070                     percent_op[i].str);
10071             *reloc = BFD_RELOC_LO16;
10072           }
10073         return TRUE;
10074       }
10075   return FALSE;
10076 }
10077
10078
10079 /* Parse string STR as a 16-bit relocatable operand.  Store the
10080    expression in *EP and the relocations in the array starting
10081    at RELOC.  Return the number of relocation operators used.
10082
10083    On exit, EXPR_END points to the first character after the expression.
10084    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10085
10086 static size_t
10087 my_getSmallExpression (ep, reloc, str)
10088      expressionS *ep;
10089      bfd_reloc_code_real_type *reloc;
10090      char *str;
10091 {
10092   bfd_reloc_code_real_type reversed_reloc[3];
10093   size_t reloc_index, i;
10094   int crux_depth, str_depth;
10095   char *crux;
10096
10097   /* Search for the start of the main expression, recoding relocations
10098      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10099      of the main expression and with CRUX_DEPTH containing the number
10100      of open brackets at that point.  */
10101   reloc_index = -1;
10102   str_depth = 0;
10103   do
10104     {
10105       reloc_index++;
10106       crux = str;
10107       crux_depth = str_depth;
10108
10109       /* Skip over whitespace and brackets, keeping count of the number
10110          of brackets.  */
10111       while (*str == ' ' || *str == '\t' || *str == '(')
10112         if (*str++ == '(')
10113           str_depth++;
10114     }
10115   while (*str == '%'
10116          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10117          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10118
10119   my_getExpression (ep, crux);
10120   str = expr_end;
10121
10122   /* Match every open bracket.  */
10123   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10124     if (*str++ == ')')
10125       crux_depth--;
10126
10127   if (crux_depth > 0)
10128     as_bad ("unclosed '('");
10129
10130   expr_end = str;
10131
10132   if (reloc_index == 0)
10133     reloc[0] = BFD_RELOC_LO16;
10134   else
10135     {
10136       prev_reloc_op_frag = frag_now;
10137       for (i = 0; i < reloc_index; i++)
10138         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10139     }
10140
10141   return reloc_index;
10142 }
10143
10144 static void
10145 my_getExpression (ep, str)
10146      expressionS *ep;
10147      char *str;
10148 {
10149   char *save_in;
10150   valueT val;
10151
10152   save_in = input_line_pointer;
10153   input_line_pointer = str;
10154   expression (ep);
10155   expr_end = input_line_pointer;
10156   input_line_pointer = save_in;
10157
10158   /* If we are in mips16 mode, and this is an expression based on `.',
10159      then we bump the value of the symbol by 1 since that is how other
10160      text symbols are handled.  We don't bother to handle complex
10161      expressions, just `.' plus or minus a constant.  */
10162   if (mips_opts.mips16
10163       && ep->X_op == O_symbol
10164       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10165       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10166       && symbol_get_frag (ep->X_add_symbol) == frag_now
10167       && symbol_constant_p (ep->X_add_symbol)
10168       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10169     S_SET_VALUE (ep->X_add_symbol, val + 1);
10170 }
10171
10172 /* Turn a string in input_line_pointer into a floating point constant
10173    of type TYPE, and store the appropriate bytes in *LITP.  The number
10174    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10175    returned, or NULL on OK.  */
10176
10177 char *
10178 md_atof (type, litP, sizeP)
10179      int type;
10180      char *litP;
10181      int *sizeP;
10182 {
10183   int prec;
10184   LITTLENUM_TYPE words[4];
10185   char *t;
10186   int i;
10187
10188   switch (type)
10189     {
10190     case 'f':
10191       prec = 2;
10192       break;
10193
10194     case 'd':
10195       prec = 4;
10196       break;
10197
10198     default:
10199       *sizeP = 0;
10200       return _("bad call to md_atof");
10201     }
10202
10203   t = atof_ieee (input_line_pointer, type, words);
10204   if (t)
10205     input_line_pointer = t;
10206
10207   *sizeP = prec * 2;
10208
10209   if (! target_big_endian)
10210     {
10211       for (i = prec - 1; i >= 0; i--)
10212         {
10213           md_number_to_chars (litP, (valueT) words[i], 2);
10214           litP += 2;
10215         }
10216     }
10217   else
10218     {
10219       for (i = 0; i < prec; i++)
10220         {
10221           md_number_to_chars (litP, (valueT) words[i], 2);
10222           litP += 2;
10223         }
10224     }
10225
10226   return NULL;
10227 }
10228
10229 void
10230 md_number_to_chars (buf, val, n)
10231      char *buf;
10232      valueT val;
10233      int n;
10234 {
10235   if (target_big_endian)
10236     number_to_chars_bigendian (buf, val, n);
10237   else
10238     number_to_chars_littleendian (buf, val, n);
10239 }
10240 \f
10241 #ifdef OBJ_ELF
10242 static int support_64bit_objects(void)
10243 {
10244   const char **list, **l;
10245   int yes;
10246
10247   list = bfd_target_list ();
10248   for (l = list; *l != NULL; l++)
10249 #ifdef TE_TMIPS
10250     /* This is traditional mips */
10251     if (strcmp (*l, "elf64-tradbigmips") == 0
10252         || strcmp (*l, "elf64-tradlittlemips") == 0)
10253 #else
10254     if (strcmp (*l, "elf64-bigmips") == 0
10255         || strcmp (*l, "elf64-littlemips") == 0)
10256 #endif
10257       break;
10258   yes = (*l != NULL);
10259   free (list);
10260   return yes;
10261 }
10262 #endif /* OBJ_ELF */
10263
10264 const char *md_shortopts = "nO::g::G:";
10265
10266 struct option md_longopts[] =
10267 {
10268 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10269   {"mips0", no_argument, NULL, OPTION_MIPS1},
10270   {"mips1", no_argument, NULL, OPTION_MIPS1},
10271 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10272   {"mips2", no_argument, NULL, OPTION_MIPS2},
10273 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10274   {"mips3", no_argument, NULL, OPTION_MIPS3},
10275 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10276   {"mips4", no_argument, NULL, OPTION_MIPS4},
10277 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10278   {"mips5", no_argument, NULL, OPTION_MIPS5},
10279 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10280   {"mips32", no_argument, NULL, OPTION_MIPS32},
10281 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10282   {"mips64", no_argument, NULL, OPTION_MIPS64},
10283 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10284   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10285 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10286   {"trap", no_argument, NULL, OPTION_TRAP},
10287   {"no-break", no_argument, NULL, OPTION_TRAP},
10288 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10289   {"break", no_argument, NULL, OPTION_BREAK},
10290   {"no-trap", no_argument, NULL, OPTION_BREAK},
10291 #define OPTION_EB (OPTION_MD_BASE + 11)
10292   {"EB", no_argument, NULL, OPTION_EB},
10293 #define OPTION_EL (OPTION_MD_BASE + 12)
10294   {"EL", no_argument, NULL, OPTION_EL},
10295 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10296   {"mips16", no_argument, NULL, OPTION_MIPS16},
10297 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10298   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10299 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10300   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10301 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10302   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10303   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10304 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10305   {"mfp32", no_argument, NULL, OPTION_FP32},
10306 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10307   {"mgp32", no_argument, NULL, OPTION_GP32},
10308 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10309   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10310 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10311   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10312 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10313   {"march", required_argument, NULL, OPTION_MARCH},
10314 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10315   {"mtune", required_argument, NULL, OPTION_MTUNE},
10316 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10317   {"mfp64", no_argument, NULL, OPTION_FP64},
10318 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10319   {"m4650", no_argument, NULL, OPTION_M4650},
10320 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10321   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10322 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10323   {"m4010", no_argument, NULL, OPTION_M4010},
10324 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10325   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10326 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10327   {"m4100", no_argument, NULL, OPTION_M4100},
10328 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10329   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10330 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10331   {"m3900", no_argument, NULL, OPTION_M3900},
10332 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10333   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10334 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10335   {"mgp64", no_argument, NULL, OPTION_GP64},
10336 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10337   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10338 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10339   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10340 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10341   {"mdmx", no_argument, NULL, OPTION_MDMX},
10342 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10343   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10344 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10345 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10346   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10347   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10348 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10349 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10350   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10351   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10352 #define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10353   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10354 #ifdef OBJ_ELF
10355 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 42)
10356 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10357   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10358   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10359 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10360   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10361 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10362   {"xgot",        no_argument, NULL, OPTION_XGOT},
10363 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10364   {"mabi", required_argument, NULL, OPTION_MABI},
10365 #define OPTION_32          (OPTION_ELF_BASE + 4)
10366   {"32",          no_argument, NULL, OPTION_32},
10367 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10368   {"n32",         no_argument, NULL, OPTION_N32},
10369 #define OPTION_64          (OPTION_ELF_BASE + 6)
10370   {"64",          no_argument, NULL, OPTION_64},
10371 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10372   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10373 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10374   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10375 #endif /* OBJ_ELF */
10376   {NULL, no_argument, NULL, 0}
10377 };
10378 size_t md_longopts_size = sizeof (md_longopts);
10379
10380 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10381    NEW_VALUE.  Warn if another value was already specified.  Note:
10382    we have to defer parsing the -march and -mtune arguments in order
10383    to handle 'from-abi' correctly, since the ABI might be specified
10384    in a later argument.  */
10385
10386 static void
10387 mips_set_option_string (string_ptr, new_value)
10388      const char **string_ptr, *new_value;
10389 {
10390   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10391     as_warn (_("A different %s was already specified, is now %s"),
10392              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10393              new_value);
10394
10395   *string_ptr = new_value;
10396 }
10397
10398 int
10399 md_parse_option (c, arg)
10400      int c;
10401      char *arg;
10402 {
10403   switch (c)
10404     {
10405     case OPTION_CONSTRUCT_FLOATS:
10406       mips_disable_float_construction = 0;
10407       break;
10408
10409     case OPTION_NO_CONSTRUCT_FLOATS:
10410       mips_disable_float_construction = 1;
10411       break;
10412
10413     case OPTION_TRAP:
10414       mips_trap = 1;
10415       break;
10416
10417     case OPTION_BREAK:
10418       mips_trap = 0;
10419       break;
10420
10421     case OPTION_EB:
10422       target_big_endian = 1;
10423       break;
10424
10425     case OPTION_EL:
10426       target_big_endian = 0;
10427       break;
10428
10429     case 'n':
10430       warn_nops = 1;
10431       break;
10432
10433     case 'O':
10434       if (arg && arg[1] == '0')
10435         mips_optimize = 1;
10436       else
10437         mips_optimize = 2;
10438       break;
10439
10440     case 'g':
10441       if (arg == NULL)
10442         mips_debug = 2;
10443       else
10444         mips_debug = atoi (arg);
10445       /* When the MIPS assembler sees -g or -g2, it does not do
10446          optimizations which limit full symbolic debugging.  We take
10447          that to be equivalent to -O0.  */
10448       if (mips_debug == 2)
10449         mips_optimize = 1;
10450       break;
10451
10452     case OPTION_MIPS1:
10453       file_mips_isa = ISA_MIPS1;
10454       break;
10455
10456     case OPTION_MIPS2:
10457       file_mips_isa = ISA_MIPS2;
10458       break;
10459
10460     case OPTION_MIPS3:
10461       file_mips_isa = ISA_MIPS3;
10462       break;
10463
10464     case OPTION_MIPS4:
10465       file_mips_isa = ISA_MIPS4;
10466       break;
10467
10468     case OPTION_MIPS5:
10469       file_mips_isa = ISA_MIPS5;
10470       break;
10471
10472     case OPTION_MIPS32:
10473       file_mips_isa = ISA_MIPS32;
10474       break;
10475
10476     case OPTION_MIPS32R2:
10477       file_mips_isa = ISA_MIPS32R2;
10478       break;
10479
10480     case OPTION_MIPS64:
10481       file_mips_isa = ISA_MIPS64;
10482       break;
10483
10484     case OPTION_MTUNE:
10485       mips_set_option_string (&mips_tune_string, arg);
10486       break;
10487
10488     case OPTION_MARCH:
10489       mips_set_option_string (&mips_arch_string, arg);
10490       break;
10491
10492     case OPTION_M4650:
10493       mips_set_option_string (&mips_arch_string, "4650");
10494       mips_set_option_string (&mips_tune_string, "4650");
10495       break;
10496
10497     case OPTION_NO_M4650:
10498       break;
10499
10500     case OPTION_M4010:
10501       mips_set_option_string (&mips_arch_string, "4010");
10502       mips_set_option_string (&mips_tune_string, "4010");
10503       break;
10504
10505     case OPTION_NO_M4010:
10506       break;
10507
10508     case OPTION_M4100:
10509       mips_set_option_string (&mips_arch_string, "4100");
10510       mips_set_option_string (&mips_tune_string, "4100");
10511       break;
10512
10513     case OPTION_NO_M4100:
10514       break;
10515
10516     case OPTION_M3900:
10517       mips_set_option_string (&mips_arch_string, "3900");
10518       mips_set_option_string (&mips_tune_string, "3900");
10519       break;
10520
10521     case OPTION_NO_M3900:
10522       break;
10523
10524     case OPTION_MDMX:
10525       mips_opts.ase_mdmx = 1;
10526       break;
10527
10528     case OPTION_NO_MDMX:
10529       mips_opts.ase_mdmx = 0;
10530       break;
10531
10532     case OPTION_MIPS16:
10533       mips_opts.mips16 = 1;
10534       mips_no_prev_insn (FALSE);
10535       break;
10536
10537     case OPTION_NO_MIPS16:
10538       mips_opts.mips16 = 0;
10539       mips_no_prev_insn (FALSE);
10540       break;
10541
10542     case OPTION_MIPS3D:
10543       mips_opts.ase_mips3d = 1;
10544       break;
10545
10546     case OPTION_NO_MIPS3D:
10547       mips_opts.ase_mips3d = 0;
10548       break;
10549
10550     case OPTION_MEMBEDDED_PIC:
10551       mips_pic = EMBEDDED_PIC;
10552       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10553         {
10554           as_bad (_("-G may not be used with embedded PIC code"));
10555           return 0;
10556         }
10557       g_switch_value = 0x7fffffff;
10558       break;
10559
10560     case OPTION_FIX_VR4122:
10561       mips_fix_4122_bugs = 1;
10562       break;
10563
10564     case OPTION_NO_FIX_VR4122:
10565       mips_fix_4122_bugs = 0;
10566       break;
10567
10568     case OPTION_RELAX_BRANCH:
10569       mips_relax_branch = 1;
10570       break;
10571
10572     case OPTION_NO_RELAX_BRANCH:
10573       mips_relax_branch = 0;
10574       break;
10575
10576 #ifdef OBJ_ELF
10577       /* When generating ELF code, we permit -KPIC and -call_shared to
10578          select SVR4_PIC, and -non_shared to select no PIC.  This is
10579          intended to be compatible with Irix 5.  */
10580     case OPTION_CALL_SHARED:
10581       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10582         {
10583           as_bad (_("-call_shared is supported only for ELF format"));
10584           return 0;
10585         }
10586       mips_pic = SVR4_PIC;
10587       if (g_switch_seen && g_switch_value != 0)
10588         {
10589           as_bad (_("-G may not be used with SVR4 PIC code"));
10590           return 0;
10591         }
10592       g_switch_value = 0;
10593       break;
10594
10595     case OPTION_NON_SHARED:
10596       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10597         {
10598           as_bad (_("-non_shared is supported only for ELF format"));
10599           return 0;
10600         }
10601       mips_pic = NO_PIC;
10602       break;
10603
10604       /* The -xgot option tells the assembler to use 32 offsets when
10605          accessing the got in SVR4_PIC mode.  It is for Irix
10606          compatibility.  */
10607     case OPTION_XGOT:
10608       mips_big_got = 1;
10609       break;
10610 #endif /* OBJ_ELF */
10611
10612     case 'G':
10613       if (! USE_GLOBAL_POINTER_OPT)
10614         {
10615           as_bad (_("-G is not supported for this configuration"));
10616           return 0;
10617         }
10618       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10619         {
10620           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10621           return 0;
10622         }
10623       else
10624         g_switch_value = atoi (arg);
10625       g_switch_seen = 1;
10626       break;
10627
10628 #ifdef OBJ_ELF
10629       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10630          and -mabi=64.  */
10631     case OPTION_32:
10632       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10633         {
10634           as_bad (_("-32 is supported for ELF format only"));
10635           return 0;
10636         }
10637       mips_abi = O32_ABI;
10638       break;
10639
10640     case OPTION_N32:
10641       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10642         {
10643           as_bad (_("-n32 is supported for ELF format only"));
10644           return 0;
10645         }
10646       mips_abi = N32_ABI;
10647       break;
10648
10649     case OPTION_64:
10650       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10651         {
10652           as_bad (_("-64 is supported for ELF format only"));
10653           return 0;
10654         }
10655       mips_abi = N64_ABI;
10656       if (! support_64bit_objects())
10657         as_fatal (_("No compiled in support for 64 bit object file format"));
10658       break;
10659 #endif /* OBJ_ELF */
10660
10661     case OPTION_GP32:
10662       file_mips_gp32 = 1;
10663       break;
10664
10665     case OPTION_GP64:
10666       file_mips_gp32 = 0;
10667       break;
10668
10669     case OPTION_FP32:
10670       file_mips_fp32 = 1;
10671       break;
10672
10673     case OPTION_FP64:
10674       file_mips_fp32 = 0;
10675       break;
10676
10677 #ifdef OBJ_ELF
10678     case OPTION_MABI:
10679       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10680         {
10681           as_bad (_("-mabi is supported for ELF format only"));
10682           return 0;
10683         }
10684       if (strcmp (arg, "32") == 0)
10685         mips_abi = O32_ABI;
10686       else if (strcmp (arg, "o64") == 0)
10687         mips_abi = O64_ABI;
10688       else if (strcmp (arg, "n32") == 0)
10689         mips_abi = N32_ABI;
10690       else if (strcmp (arg, "64") == 0)
10691         {
10692           mips_abi = N64_ABI;
10693           if (! support_64bit_objects())
10694             as_fatal (_("No compiled in support for 64 bit object file "
10695                         "format"));
10696         }
10697       else if (strcmp (arg, "eabi") == 0)
10698         mips_abi = EABI_ABI;
10699       else
10700         {
10701           as_fatal (_("invalid abi -mabi=%s"), arg);
10702           return 0;
10703         }
10704       break;
10705 #endif /* OBJ_ELF */
10706
10707     case OPTION_M7000_HILO_FIX:
10708       mips_7000_hilo_fix = TRUE;
10709       break;
10710
10711     case OPTION_MNO_7000_HILO_FIX:
10712       mips_7000_hilo_fix = FALSE;
10713       break;
10714
10715 #ifdef OBJ_ELF
10716     case OPTION_MDEBUG:
10717       mips_flag_mdebug = TRUE;
10718       break;
10719
10720     case OPTION_NO_MDEBUG:
10721       mips_flag_mdebug = FALSE;
10722       break;
10723 #endif /* OBJ_ELF */
10724
10725     default:
10726       return 0;
10727     }
10728
10729   return 1;
10730 }
10731 \f
10732 /* Set up globals to generate code for the ISA or processor
10733    described by INFO.  */
10734
10735 static void
10736 mips_set_architecture (info)
10737      const struct mips_cpu_info *info;
10738 {
10739   if (info != 0)
10740     {
10741       mips_arch_info = info;
10742       mips_arch = info->cpu;
10743       mips_opts.isa = info->isa;
10744     }
10745 }
10746
10747
10748 /* Likewise for tuning.  */
10749
10750 static void
10751 mips_set_tune (info)
10752      const struct mips_cpu_info *info;
10753 {
10754   if (info != 0)
10755     {
10756       mips_tune_info = info;
10757       mips_tune = info->cpu;
10758     }
10759 }
10760
10761
10762 void
10763 mips_after_parse_args ()
10764 {
10765   /* GP relative stuff not working for PE */
10766   if (strncmp (TARGET_OS, "pe", 2) == 0
10767       && g_switch_value != 0)
10768     {
10769       if (g_switch_seen)
10770         as_bad (_("-G not supported in this configuration."));
10771       g_switch_value = 0;
10772     }
10773
10774   /* The following code determines the architecture and register size.
10775      Similar code was added to GCC 3.3 (see override_options() in
10776      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10777      as much as possible.  */
10778
10779   if (mips_arch_string != 0)
10780     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10781
10782   if (mips_tune_string != 0)
10783     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10784
10785   if (file_mips_isa != ISA_UNKNOWN)
10786     {
10787       /* Handle -mipsN.  At this point, file_mips_isa contains the
10788          ISA level specified by -mipsN, while mips_opts.isa contains
10789          the -march selection (if any).  */
10790       if (mips_arch_info != 0)
10791         {
10792           /* -march takes precedence over -mipsN, since it is more descriptive.
10793              There's no harm in specifying both as long as the ISA levels
10794              are the same.  */
10795           if (file_mips_isa != mips_opts.isa)
10796             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10797                     mips_cpu_info_from_isa (file_mips_isa)->name,
10798                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10799         }
10800       else
10801         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10802     }
10803
10804   if (mips_arch_info == 0)
10805     mips_set_architecture (mips_parse_cpu ("default CPU",
10806                                            MIPS_CPU_STRING_DEFAULT));
10807
10808   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10809     as_bad ("-march=%s is not compatible with the selected ABI",
10810             mips_arch_info->name);
10811
10812   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10813   if (mips_tune_info == 0)
10814     mips_set_tune (mips_arch_info);
10815
10816   if (file_mips_gp32 >= 0)
10817     {
10818       /* The user specified the size of the integer registers.  Make sure
10819          it agrees with the ABI and ISA.  */
10820       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10821         as_bad (_("-mgp64 used with a 32-bit processor"));
10822       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10823         as_bad (_("-mgp32 used with a 64-bit ABI"));
10824       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10825         as_bad (_("-mgp64 used with a 32-bit ABI"));
10826     }
10827   else
10828     {
10829       /* Infer the integer register size from the ABI and processor.
10830          Restrict ourselves to 32-bit registers if that's all the
10831          processor has, or if the ABI cannot handle 64-bit registers.  */
10832       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10833                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10834     }
10835
10836   /* ??? GAS treats single-float processors as though they had 64-bit
10837      float registers (although it complains when double-precision
10838      instructions are used).  As things stand, saying they have 32-bit
10839      registers would lead to spurious "register must be even" messages.
10840      So here we assume float registers are always the same size as
10841      integer ones, unless the user says otherwise.  */
10842   if (file_mips_fp32 < 0)
10843     file_mips_fp32 = file_mips_gp32;
10844
10845   /* End of GCC-shared inference code.  */
10846
10847   /* ??? When do we want this flag to be set?   Who uses it?  */
10848   if (file_mips_gp32 == 1
10849       && mips_abi == NO_ABI
10850       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10851     mips_32bitmode = 1;
10852
10853   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10854     as_bad (_("trap exception not supported at ISA 1"));
10855
10856   /* If the selected architecture includes support for ASEs, enable
10857      generation of code for them.  */
10858   if (mips_opts.mips16 == -1)
10859     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10860   if (mips_opts.ase_mips3d == -1)
10861     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10862   if (mips_opts.ase_mdmx == -1)
10863     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10864
10865   file_mips_isa = mips_opts.isa;
10866   file_ase_mips16 = mips_opts.mips16;
10867   file_ase_mips3d = mips_opts.ase_mips3d;
10868   file_ase_mdmx = mips_opts.ase_mdmx;
10869   mips_opts.gp32 = file_mips_gp32;
10870   mips_opts.fp32 = file_mips_fp32;
10871
10872   if (mips_flag_mdebug < 0)
10873     {
10874 #ifdef OBJ_MAYBE_ECOFF
10875       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10876         mips_flag_mdebug = 1;
10877       else
10878 #endif /* OBJ_MAYBE_ECOFF */
10879         mips_flag_mdebug = 0;
10880     }
10881 }
10882 \f
10883 void
10884 mips_init_after_args ()
10885 {
10886   /* initialize opcodes */
10887   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10888   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10889 }
10890
10891 long
10892 md_pcrel_from (fixP)
10893      fixS *fixP;
10894 {
10895   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10896       && fixP->fx_addsy != (symbolS *) NULL
10897       && ! S_IS_DEFINED (fixP->fx_addsy))
10898     return 4;
10899
10900   /* Return the address of the delay slot.  */
10901   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10902 }
10903
10904 /* This is called before the symbol table is processed.  In order to
10905    work with gcc when using mips-tfile, we must keep all local labels.
10906    However, in other cases, we want to discard them.  If we were
10907    called with -g, but we didn't see any debugging information, it may
10908    mean that gcc is smuggling debugging information through to
10909    mips-tfile, in which case we must generate all local labels.  */
10910
10911 void
10912 mips_frob_file_before_adjust ()
10913 {
10914 #ifndef NO_ECOFF_DEBUGGING
10915   if (ECOFF_DEBUGGING
10916       && mips_debug != 0
10917       && ! ecoff_debugging_seen)
10918     flag_keep_locals = 1;
10919 #endif
10920 }
10921
10922 /* Sort any unmatched HI16_S relocs so that they immediately precede
10923    the corresponding LO reloc.  This is called before md_apply_fix3 and
10924    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10925    explicit use of the %hi modifier.  */
10926
10927 void
10928 mips_frob_file ()
10929 {
10930   struct mips_hi_fixup *l;
10931
10932   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10933     {
10934       segment_info_type *seginfo;
10935       int pass;
10936
10937       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10938
10939       /* If a GOT16 relocation turns out to be against a global symbol,
10940          there isn't supposed to be a matching LO.  */
10941       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10942           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10943         continue;
10944
10945       /* Check quickly whether the next fixup happens to be a matching %lo.  */
10946       if (fixup_has_matching_lo_p (l->fixp))
10947         continue;
10948
10949       /* Look through the fixups for this segment for a matching %lo.
10950          When we find one, move the %hi just in front of it.  We do
10951          this in two passes.  In the first pass, we try to find a
10952          unique %lo.  In the second pass, we permit multiple %hi
10953          relocs for a single %lo (this is a GNU extension).  */
10954       seginfo = seg_info (l->seg);
10955       for (pass = 0; pass < 2; pass++)
10956         {
10957           fixS *f, *prev;
10958
10959           prev = NULL;
10960           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10961             {
10962               /* Check whether this is a %lo fixup which matches l->fixp.  */
10963               if (f->fx_r_type == BFD_RELOC_LO16
10964                   && f->fx_addsy == l->fixp->fx_addsy
10965                   && f->fx_offset == l->fixp->fx_offset
10966                   && (pass == 1
10967                       || prev == NULL
10968                       || !reloc_needs_lo_p (prev->fx_r_type)
10969                       || !fixup_has_matching_lo_p (prev)))
10970                 {
10971                   fixS **pf;
10972
10973                   /* Move l->fixp before f.  */
10974                   for (pf = &seginfo->fix_root;
10975                        *pf != l->fixp;
10976                        pf = &(*pf)->fx_next)
10977                     assert (*pf != NULL);
10978
10979                   *pf = l->fixp->fx_next;
10980
10981                   l->fixp->fx_next = f;
10982                   if (prev == NULL)
10983                     seginfo->fix_root = l->fixp;
10984                   else
10985                     prev->fx_next = l->fixp;
10986
10987                   break;
10988                 }
10989
10990               prev = f;
10991             }
10992
10993           if (f != NULL)
10994             break;
10995
10996 #if 0 /* GCC code motion plus incomplete dead code elimination
10997          can leave a %hi without a %lo.  */
10998           if (pass == 1)
10999             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11000                            _("Unmatched %%hi reloc"));
11001 #endif
11002         }
11003     }
11004 }
11005
11006 /* When generating embedded PIC code we need to use a special
11007    relocation to represent the difference of two symbols in the .text
11008    section (switch tables use a difference of this sort).  See
11009    include/coff/mips.h for details.  This macro checks whether this
11010    fixup requires the special reloc.  */
11011 #define SWITCH_TABLE(fixp) \
11012   ((fixp)->fx_r_type == BFD_RELOC_32 \
11013    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11014    && (fixp)->fx_addsy != NULL \
11015    && (fixp)->fx_subsy != NULL \
11016    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11017    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11018
11019 /* When generating embedded PIC code we must keep all PC relative
11020    relocations, in case the linker has to relax a call.  We also need
11021    to keep relocations for switch table entries.
11022
11023    We may have combined relocations without symbols in the N32/N64 ABI.
11024    We have to prevent gas from dropping them.  */
11025
11026 int
11027 mips_force_relocation (fixp)
11028      fixS *fixp;
11029 {
11030   if (generic_force_reloc (fixp))
11031     return 1;
11032
11033   if (HAVE_NEWABI
11034       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11035       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11036           || fixp->fx_r_type == BFD_RELOC_HI16_S
11037           || fixp->fx_r_type == BFD_RELOC_LO16))
11038     return 1;
11039
11040   return (mips_pic == EMBEDDED_PIC
11041           && (fixp->fx_pcrel
11042               || SWITCH_TABLE (fixp)
11043               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11044               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11045 }
11046
11047 /* This hook is called before a fix is simplified.  We don't really
11048    decide whether to skip a fix here.  Rather, we turn global symbols
11049    used as branch targets into local symbols, such that they undergo
11050    simplification.  We can only do this if the symbol is defined and
11051    it is in the same section as the branch.  If this doesn't hold, we
11052    emit a better error message than just saying the relocation is not
11053    valid for the selected object format.
11054
11055    FIXP is the fix-up we're going to try to simplify, SEG is the
11056    segment in which the fix up occurs.  The return value should be
11057    non-zero to indicate the fix-up is valid for further
11058    simplifications.  */
11059
11060 int
11061 mips_validate_fix (fixP, seg)
11062      struct fix *fixP;
11063      asection *seg;
11064 {
11065   /* There's a lot of discussion on whether it should be possible to
11066      use R_MIPS_PC16 to represent branch relocations.  The outcome
11067      seems to be that it can, but gas/bfd are very broken in creating
11068      RELA relocations for this, so for now we only accept branches to
11069      symbols in the same section.  Anything else is of dubious value,
11070      since there's no guarantee that at link time the symbol would be
11071      in range.  Even for branches to local symbols this is arguably
11072      wrong, since it we assume the symbol is not going to be
11073      overridden, which should be possible per ELF library semantics,
11074      but then, there isn't a dynamic relocation that could be used to
11075      this effect, and the target would likely be out of range as well.
11076
11077      Unfortunately, it seems that there is too much code out there
11078      that relies on branches to symbols that are global to be resolved
11079      as if they were local, like the IRIX tools do, so we do it as
11080      well, but with a warning so that people are reminded to fix their
11081      code.  If we ever get back to using R_MIPS_PC16 for branch
11082      targets, this entire block should go away (and probably the
11083      whole function).  */
11084
11085   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11086       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11087             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11088            && mips_pic != EMBEDDED_PIC)
11089           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11090       && fixP->fx_addsy)
11091     {
11092       if (! S_IS_DEFINED (fixP->fx_addsy))
11093         {
11094           as_bad_where (fixP->fx_file, fixP->fx_line,
11095                         _("Cannot branch to undefined symbol."));
11096           /* Avoid any further errors about this fixup.  */
11097           fixP->fx_done = 1;
11098         }
11099       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11100         {
11101           as_bad_where (fixP->fx_file, fixP->fx_line,
11102                         _("Cannot branch to symbol in another section."));
11103           fixP->fx_done = 1;
11104         }
11105       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11106         {
11107           symbolS *sym = fixP->fx_addsy;
11108
11109           as_warn_where (fixP->fx_file, fixP->fx_line,
11110                          _("Pretending global symbol used as branch target is local."));
11111
11112           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11113                                           S_GET_SEGMENT (sym),
11114                                           S_GET_VALUE (sym),
11115                                           symbol_get_frag (sym));
11116           copy_symbol_attributes (fixP->fx_addsy, sym);
11117           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11118           assert (symbol_resolved_p (sym));
11119           symbol_mark_resolved (fixP->fx_addsy);
11120         }
11121     }
11122
11123   return 1;
11124 }
11125
11126 #ifdef OBJ_ELF
11127 static int
11128 mips_need_elf_addend_fixup (fixP)
11129      fixS *fixP;
11130 {
11131   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11132     return 1;
11133   if (mips_pic == EMBEDDED_PIC
11134       && S_IS_WEAK (fixP->fx_addsy))
11135     return 1;
11136   if (mips_pic != EMBEDDED_PIC
11137       && (S_IS_WEAK (fixP->fx_addsy)
11138           || S_IS_EXTERNAL (fixP->fx_addsy))
11139       && !S_IS_COMMON (fixP->fx_addsy))
11140     return 1;
11141   if (symbol_used_in_reloc_p (fixP->fx_addsy)
11142       && (((bfd_get_section_flags (stdoutput,
11143                                    S_GET_SEGMENT (fixP->fx_addsy))
11144             & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11145           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11146                        ".gnu.linkonce",
11147                        sizeof (".gnu.linkonce") - 1)))
11148     return 1;
11149   return 0;
11150 }
11151 #endif
11152
11153 /* Apply a fixup to the object file.  */
11154
11155 void
11156 md_apply_fix3 (fixP, valP, seg)
11157      fixS *fixP;
11158      valueT *valP;
11159      segT seg ATTRIBUTE_UNUSED;
11160 {
11161   bfd_byte *buf;
11162   long insn;
11163   valueT value;
11164   static int previous_fx_r_type = 0;
11165
11166   /* FIXME: Maybe just return for all reloc types not listed below?
11167      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11168   if (fixP->fx_r_type == BFD_RELOC_8)
11169       return;
11170
11171   assert (fixP->fx_size == 4
11172           || fixP->fx_r_type == BFD_RELOC_16
11173           || fixP->fx_r_type == BFD_RELOC_32
11174           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11175           || fixP->fx_r_type == BFD_RELOC_HI16_S
11176           || fixP->fx_r_type == BFD_RELOC_LO16
11177           || fixP->fx_r_type == BFD_RELOC_GPREL16
11178           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11179           || fixP->fx_r_type == BFD_RELOC_GPREL32
11180           || fixP->fx_r_type == BFD_RELOC_64
11181           || fixP->fx_r_type == BFD_RELOC_CTOR
11182           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11183           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11184           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11185           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11186           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11187           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11188           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11189           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11190           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11191
11192   value = *valP;
11193
11194   /* If we aren't adjusting this fixup to be against the section
11195      symbol, we need to adjust the value.  */
11196 #ifdef OBJ_ELF
11197   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11198     {
11199       if (mips_need_elf_addend_fixup (fixP))
11200         {
11201           reloc_howto_type *howto;
11202           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11203
11204           value -= symval;
11205
11206           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11207           if (value != 0 && howto && howto->partial_inplace
11208               && (! fixP->fx_pcrel || howto->pcrel_offset))
11209             {
11210               /* In this case, the bfd_install_relocation routine will
11211                  incorrectly add the symbol value back in.  We just want
11212                  the addend to appear in the object file.
11213
11214                  howto->pcrel_offset is added for R_MIPS_PC16, which is
11215                  generated for code like
11216
11217                         globl g1 .text
11218                         .text
11219                         .space 20
11220                  g1:
11221                  x:
11222                         bal g1
11223                */
11224               value -= symval;
11225
11226               /* Make sure the addend is still non-zero.  If it became zero
11227                  after the last operation, set it to a spurious value and
11228                  subtract the same value from the object file's contents.  */
11229               if (value == 0)
11230                 {
11231                   value = 8;
11232
11233                   /* The in-place addends for LO16 relocations are signed;
11234                      leave the matching HI16 in-place addends as zero.  */
11235                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11236                     {
11237                       bfd_vma contents, mask, field;
11238
11239                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11240                                                + fixP->fx_where,
11241                                                fixP->fx_size * 8,
11242                                                target_big_endian);
11243
11244                       /* MASK has bits set where the relocation should go.
11245                          FIELD is -value, shifted into the appropriate place
11246                          for this relocation.  */
11247                       mask = 1 << (howto->bitsize - 1);
11248                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11249                       field = (-value >> howto->rightshift) << howto->bitpos;
11250
11251                       bfd_put_bits ((field & mask) | (contents & ~mask),
11252                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11253                                     fixP->fx_size * 8,
11254                                     target_big_endian);
11255                     }
11256                 }
11257             }
11258         }
11259
11260       /* This code was generated using trial and error and so is
11261          fragile and not trustworthy.  If you change it, you should
11262          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11263          they still pass.  */
11264       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11265         {
11266           value += fixP->fx_frag->fr_address + fixP->fx_where;
11267
11268           /* BFD's REL handling, for MIPS, is _very_ weird.
11269              This gives the right results, but it can't possibly
11270              be the way things are supposed to work.  */
11271           if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11272               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11273             value += fixP->fx_frag->fr_address + fixP->fx_where;
11274         }
11275     }
11276 #endif
11277
11278   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11279
11280   /* We are not done if this is a composite relocation to set up gp.  */
11281   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11282       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11283            || (fixP->fx_r_type == BFD_RELOC_64
11284                && (previous_fx_r_type == BFD_RELOC_GPREL32
11285                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11286            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11287                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11288                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11289     fixP->fx_done = 1;
11290   previous_fx_r_type = fixP->fx_r_type;
11291
11292   switch (fixP->fx_r_type)
11293     {
11294     case BFD_RELOC_MIPS_JMP:
11295     case BFD_RELOC_MIPS_SHIFT5:
11296     case BFD_RELOC_MIPS_SHIFT6:
11297     case BFD_RELOC_MIPS_GOT_DISP:
11298     case BFD_RELOC_MIPS_GOT_PAGE:
11299     case BFD_RELOC_MIPS_GOT_OFST:
11300     case BFD_RELOC_MIPS_SUB:
11301     case BFD_RELOC_MIPS_INSERT_A:
11302     case BFD_RELOC_MIPS_INSERT_B:
11303     case BFD_RELOC_MIPS_DELETE:
11304     case BFD_RELOC_MIPS_HIGHEST:
11305     case BFD_RELOC_MIPS_HIGHER:
11306     case BFD_RELOC_MIPS_SCN_DISP:
11307     case BFD_RELOC_MIPS_REL16:
11308     case BFD_RELOC_MIPS_RELGOT:
11309     case BFD_RELOC_MIPS_JALR:
11310     case BFD_RELOC_HI16:
11311     case BFD_RELOC_HI16_S:
11312     case BFD_RELOC_GPREL16:
11313     case BFD_RELOC_MIPS_LITERAL:
11314     case BFD_RELOC_MIPS_CALL16:
11315     case BFD_RELOC_MIPS_GOT16:
11316     case BFD_RELOC_GPREL32:
11317     case BFD_RELOC_MIPS_GOT_HI16:
11318     case BFD_RELOC_MIPS_GOT_LO16:
11319     case BFD_RELOC_MIPS_CALL_HI16:
11320     case BFD_RELOC_MIPS_CALL_LO16:
11321     case BFD_RELOC_MIPS16_GPREL:
11322       if (fixP->fx_pcrel)
11323         as_bad_where (fixP->fx_file, fixP->fx_line,
11324                       _("Invalid PC relative reloc"));
11325       /* Nothing needed to do. The value comes from the reloc entry */
11326       break;
11327
11328     case BFD_RELOC_MIPS16_JMP:
11329       /* We currently always generate a reloc against a symbol, which
11330          means that we don't want an addend even if the symbol is
11331          defined.  */
11332       fixP->fx_addnumber = 0;
11333       break;
11334
11335     case BFD_RELOC_PCREL_HI16_S:
11336       /* The addend for this is tricky if it is internal, so we just
11337          do everything here rather than in bfd_install_relocation.  */
11338       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11339           && !fixP->fx_done
11340           && value != 0)
11341         break;
11342       if (fixP->fx_addsy
11343           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11344         {
11345           /* For an external symbol adjust by the address to make it
11346              pcrel_offset.  We use the address of the RELLO reloc
11347              which follows this one.  */
11348           value += (fixP->fx_next->fx_frag->fr_address
11349                     + fixP->fx_next->fx_where);
11350         }
11351       value = ((value + 0x8000) >> 16) & 0xffff;
11352       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11353       if (target_big_endian)
11354         buf += 2;
11355       md_number_to_chars ((char *) buf, value, 2);
11356       break;
11357
11358     case BFD_RELOC_PCREL_LO16:
11359       /* The addend for this is tricky if it is internal, so we just
11360          do everything here rather than in bfd_install_relocation.  */
11361       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11362           && !fixP->fx_done
11363           && value != 0)
11364         break;
11365       if (fixP->fx_addsy
11366           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11367         value += fixP->fx_frag->fr_address + fixP->fx_where;
11368       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11369       if (target_big_endian)
11370         buf += 2;
11371       md_number_to_chars ((char *) buf, value, 2);
11372       break;
11373
11374     case BFD_RELOC_64:
11375       /* This is handled like BFD_RELOC_32, but we output a sign
11376          extended value if we are only 32 bits.  */
11377       if (fixP->fx_done
11378           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11379         {
11380           if (8 <= sizeof (valueT))
11381             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11382                                 value, 8);
11383           else
11384             {
11385               long w1, w2;
11386               long hiv;
11387
11388               w1 = w2 = fixP->fx_where;
11389               if (target_big_endian)
11390                 w1 += 4;
11391               else
11392                 w2 += 4;
11393               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11394               if ((value & 0x80000000) != 0)
11395                 hiv = 0xffffffff;
11396               else
11397                 hiv = 0;
11398               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11399             }
11400         }
11401       break;
11402
11403     case BFD_RELOC_RVA:
11404     case BFD_RELOC_32:
11405       /* If we are deleting this reloc entry, we must fill in the
11406          value now.  This can happen if we have a .word which is not
11407          resolved when it appears but is later defined.  We also need
11408          to fill in the value if this is an embedded PIC switch table
11409          entry.  */
11410       if (fixP->fx_done
11411           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11412         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11413                             value, 4);
11414       break;
11415
11416     case BFD_RELOC_16:
11417       /* If we are deleting this reloc entry, we must fill in the
11418          value now.  */
11419       assert (fixP->fx_size == 2);
11420       if (fixP->fx_done)
11421         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11422                             value, 2);
11423       break;
11424
11425     case BFD_RELOC_LO16:
11426       /* When handling an embedded PIC switch statement, we can wind
11427          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11428       if (fixP->fx_done)
11429         {
11430           if (value + 0x8000 > 0xffff)
11431             as_bad_where (fixP->fx_file, fixP->fx_line,
11432                           _("relocation overflow"));
11433           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11434           if (target_big_endian)
11435             buf += 2;
11436           md_number_to_chars ((char *) buf, value, 2);
11437         }
11438       break;
11439
11440     case BFD_RELOC_16_PCREL_S2:
11441       if ((value & 0x3) != 0)
11442         as_bad_where (fixP->fx_file, fixP->fx_line,
11443                       _("Branch to odd address (%lx)"), (long) value);
11444
11445       /*
11446        * We need to save the bits in the instruction since fixup_segment()
11447        * might be deleting the relocation entry (i.e., a branch within
11448        * the current segment).
11449        */
11450       if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
11451         break;
11452       /* If 'value' is zero, the remaining reloc code won't actually
11453          do the store, so it must be done here.  This is probably
11454          a bug somewhere.  */
11455       if (!fixP->fx_done
11456           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11457               || fixP->fx_addsy == NULL                 /* ??? */
11458               || ! S_IS_DEFINED (fixP->fx_addsy)))
11459         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11460
11461       value = (offsetT) value >> 2;
11462
11463       /* update old instruction data */
11464       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11465       if (target_big_endian)
11466         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11467       else
11468         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11469
11470       if (value + 0x8000 <= 0xffff)
11471         insn |= value & 0xffff;
11472       else
11473         {
11474           /* The branch offset is too large.  If this is an
11475              unconditional branch, and we are not generating PIC code,
11476              we can convert it to an absolute jump instruction.  */
11477           if (mips_pic == NO_PIC
11478               && fixP->fx_done
11479               && fixP->fx_frag->fr_address >= text_section->vma
11480               && (fixP->fx_frag->fr_address
11481                   < text_section->vma + text_section->_raw_size)
11482               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11483                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11484                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11485             {
11486               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11487                 insn = 0x0c000000;      /* jal */
11488               else
11489                 insn = 0x08000000;      /* j */
11490               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11491               fixP->fx_done = 0;
11492               fixP->fx_addsy = section_symbol (text_section);
11493               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11494             }
11495           else
11496             {
11497               /* If we got here, we have branch-relaxation disabled,
11498                  and there's nothing we can do to fix this instruction
11499                  without turning it into a longer sequence.  */
11500               as_bad_where (fixP->fx_file, fixP->fx_line,
11501                             _("Branch out of range"));
11502             }
11503         }
11504
11505       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11506       break;
11507
11508     case BFD_RELOC_VTABLE_INHERIT:
11509       fixP->fx_done = 0;
11510       if (fixP->fx_addsy
11511           && !S_IS_DEFINED (fixP->fx_addsy)
11512           && !S_IS_WEAK (fixP->fx_addsy))
11513         S_SET_WEAK (fixP->fx_addsy);
11514       break;
11515
11516     case BFD_RELOC_VTABLE_ENTRY:
11517       fixP->fx_done = 0;
11518       break;
11519
11520     default:
11521       internalError ();
11522     }
11523 }
11524
11525 #if 0
11526 void
11527 printInsn (oc)
11528      unsigned long oc;
11529 {
11530   const struct mips_opcode *p;
11531   int treg, sreg, dreg, shamt;
11532   short imm;
11533   const char *args;
11534   int i;
11535
11536   for (i = 0; i < NUMOPCODES; ++i)
11537     {
11538       p = &mips_opcodes[i];
11539       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11540         {
11541           printf ("%08lx %s\t", oc, p->name);
11542           treg = (oc >> 16) & 0x1f;
11543           sreg = (oc >> 21) & 0x1f;
11544           dreg = (oc >> 11) & 0x1f;
11545           shamt = (oc >> 6) & 0x1f;
11546           imm = oc;
11547           for (args = p->args;; ++args)
11548             {
11549               switch (*args)
11550                 {
11551                 case '\0':
11552                   printf ("\n");
11553                   break;
11554
11555                 case ',':
11556                 case '(':
11557                 case ')':
11558                   printf ("%c", *args);
11559                   continue;
11560
11561                 case 'r':
11562                   assert (treg == sreg);
11563                   printf ("$%d,$%d", treg, sreg);
11564                   continue;
11565
11566                 case 'd':
11567                 case 'G':
11568                   printf ("$%d", dreg);
11569                   continue;
11570
11571                 case 't':
11572                 case 'E':
11573                   printf ("$%d", treg);
11574                   continue;
11575
11576                 case 'k':
11577                   printf ("0x%x", treg);
11578                   continue;
11579
11580                 case 'b':
11581                 case 's':
11582                   printf ("$%d", sreg);
11583                   continue;
11584
11585                 case 'a':
11586                   printf ("0x%08lx", oc & 0x1ffffff);
11587                   continue;
11588
11589                 case 'i':
11590                 case 'j':
11591                 case 'o':
11592                 case 'u':
11593                   printf ("%d", imm);
11594                   continue;
11595
11596                 case '<':
11597                 case '>':
11598                   printf ("$%d", shamt);
11599                   continue;
11600
11601                 default:
11602                   internalError ();
11603                 }
11604               break;
11605             }
11606           return;
11607         }
11608     }
11609   printf (_("%08lx  UNDEFINED\n"), oc);
11610 }
11611 #endif
11612
11613 static symbolS *
11614 get_symbol ()
11615 {
11616   int c;
11617   char *name;
11618   symbolS *p;
11619
11620   name = input_line_pointer;
11621   c = get_symbol_end ();
11622   p = (symbolS *) symbol_find_or_make (name);
11623   *input_line_pointer = c;
11624   return p;
11625 }
11626
11627 /* Align the current frag to a given power of two.  The MIPS assembler
11628    also automatically adjusts any preceding label.  */
11629
11630 static void
11631 mips_align (to, fill, label)
11632      int to;
11633      int fill;
11634      symbolS *label;
11635 {
11636   mips_emit_delays (FALSE);
11637   frag_align (to, fill, 0);
11638   record_alignment (now_seg, to);
11639   if (label != NULL)
11640     {
11641       assert (S_GET_SEGMENT (label) == now_seg);
11642       symbol_set_frag (label, frag_now);
11643       S_SET_VALUE (label, (valueT) frag_now_fix ());
11644     }
11645 }
11646
11647 /* Align to a given power of two.  .align 0 turns off the automatic
11648    alignment used by the data creating pseudo-ops.  */
11649
11650 static void
11651 s_align (x)
11652      int x ATTRIBUTE_UNUSED;
11653 {
11654   register int temp;
11655   register long temp_fill;
11656   long max_alignment = 15;
11657
11658   /*
11659
11660     o  Note that the assembler pulls down any immediately preceeding label
11661        to the aligned address.
11662     o  It's not documented but auto alignment is reinstated by
11663        a .align pseudo instruction.
11664     o  Note also that after auto alignment is turned off the mips assembler
11665        issues an error on attempt to assemble an improperly aligned data item.
11666        We don't.
11667
11668     */
11669
11670   temp = get_absolute_expression ();
11671   if (temp > max_alignment)
11672     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11673   else if (temp < 0)
11674     {
11675       as_warn (_("Alignment negative: 0 assumed."));
11676       temp = 0;
11677     }
11678   if (*input_line_pointer == ',')
11679     {
11680       ++input_line_pointer;
11681       temp_fill = get_absolute_expression ();
11682     }
11683   else
11684     temp_fill = 0;
11685   if (temp)
11686     {
11687       auto_align = 1;
11688       mips_align (temp, (int) temp_fill,
11689                   insn_labels != NULL ? insn_labels->label : NULL);
11690     }
11691   else
11692     {
11693       auto_align = 0;
11694     }
11695
11696   demand_empty_rest_of_line ();
11697 }
11698
11699 void
11700 mips_flush_pending_output ()
11701 {
11702   mips_emit_delays (FALSE);
11703   mips_clear_insn_labels ();
11704 }
11705
11706 static void
11707 s_change_sec (sec)
11708      int sec;
11709 {
11710   segT seg;
11711
11712   /* When generating embedded PIC code, we only use the .text, .lit8,
11713      .sdata and .sbss sections.  We change the .data and .rdata
11714      pseudo-ops to use .sdata.  */
11715   if (mips_pic == EMBEDDED_PIC
11716       && (sec == 'd' || sec == 'r'))
11717     sec = 's';
11718
11719 #ifdef OBJ_ELF
11720   /* The ELF backend needs to know that we are changing sections, so
11721      that .previous works correctly.  We could do something like check
11722      for an obj_section_change_hook macro, but that might be confusing
11723      as it would not be appropriate to use it in the section changing
11724      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11725      This should be cleaner, somehow.  */
11726   obj_elf_section_change_hook ();
11727 #endif
11728
11729   mips_emit_delays (FALSE);
11730   switch (sec)
11731     {
11732     case 't':
11733       s_text (0);
11734       break;
11735     case 'd':
11736       s_data (0);
11737       break;
11738     case 'b':
11739       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11740       demand_empty_rest_of_line ();
11741       break;
11742
11743     case 'r':
11744       if (USE_GLOBAL_POINTER_OPT)
11745         {
11746           seg = subseg_new (RDATA_SECTION_NAME,
11747                             (subsegT) get_absolute_expression ());
11748           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11749             {
11750               bfd_set_section_flags (stdoutput, seg,
11751                                      (SEC_ALLOC
11752                                       | SEC_LOAD
11753                                       | SEC_READONLY
11754                                       | SEC_RELOC
11755                                       | SEC_DATA));
11756               if (strcmp (TARGET_OS, "elf") != 0)
11757                 record_alignment (seg, 4);
11758             }
11759           demand_empty_rest_of_line ();
11760         }
11761       else
11762         {
11763           as_bad (_("No read only data section in this object file format"));
11764           demand_empty_rest_of_line ();
11765           return;
11766         }
11767       break;
11768
11769     case 's':
11770       if (USE_GLOBAL_POINTER_OPT)
11771         {
11772           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11773           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11774             {
11775               bfd_set_section_flags (stdoutput, seg,
11776                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11777                                      | SEC_DATA);
11778               if (strcmp (TARGET_OS, "elf") != 0)
11779                 record_alignment (seg, 4);
11780             }
11781           demand_empty_rest_of_line ();
11782           break;
11783         }
11784       else
11785         {
11786           as_bad (_("Global pointers not supported; recompile -G 0"));
11787           demand_empty_rest_of_line ();
11788           return;
11789         }
11790     }
11791
11792   auto_align = 1;
11793 }
11794
11795 void
11796 s_change_section (ignore)
11797      int ignore ATTRIBUTE_UNUSED;
11798 {
11799 #ifdef OBJ_ELF
11800   char *section_name;
11801   char c;
11802   char next_c;
11803   int section_type;
11804   int section_flag;
11805   int section_entry_size;
11806   int section_alignment;
11807
11808   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11809     return;
11810
11811   section_name = input_line_pointer;
11812   c = get_symbol_end ();
11813   if (c)
11814     next_c = *(input_line_pointer + 1);
11815
11816   /* Do we have .section Name<,"flags">?  */
11817   if (c != ',' || (c == ',' && next_c == '"'))
11818     {
11819       /* just after name is now '\0'.  */
11820       *input_line_pointer = c;
11821       input_line_pointer = section_name;
11822       obj_elf_section (ignore);
11823       return;
11824     }
11825   input_line_pointer++;
11826
11827   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11828   if (c == ',')
11829     section_type = get_absolute_expression ();
11830   else
11831     section_type = 0;
11832   if (*input_line_pointer++ == ',')
11833     section_flag = get_absolute_expression ();
11834   else
11835     section_flag = 0;
11836   if (*input_line_pointer++ == ',')
11837     section_entry_size = get_absolute_expression ();
11838   else
11839     section_entry_size = 0;
11840   if (*input_line_pointer++ == ',')
11841     section_alignment = get_absolute_expression ();
11842   else
11843     section_alignment = 0;
11844
11845   section_name = xstrdup (section_name);
11846
11847   obj_elf_change_section (section_name, section_type, section_flag,
11848                           section_entry_size, 0, 0, 0);
11849
11850   if (now_seg->name != section_name)
11851     free (section_name);
11852 #endif /* OBJ_ELF */
11853 }
11854
11855 void
11856 mips_enable_auto_align ()
11857 {
11858   auto_align = 1;
11859 }
11860
11861 static void
11862 s_cons (log_size)
11863      int log_size;
11864 {
11865   symbolS *label;
11866
11867   label = insn_labels != NULL ? insn_labels->label : NULL;
11868   mips_emit_delays (FALSE);
11869   if (log_size > 0 && auto_align)
11870     mips_align (log_size, 0, label);
11871   mips_clear_insn_labels ();
11872   cons (1 << log_size);
11873 }
11874
11875 static void
11876 s_float_cons (type)
11877      int type;
11878 {
11879   symbolS *label;
11880
11881   label = insn_labels != NULL ? insn_labels->label : NULL;
11882
11883   mips_emit_delays (FALSE);
11884
11885   if (auto_align)
11886     {
11887       if (type == 'd')
11888         mips_align (3, 0, label);
11889       else
11890         mips_align (2, 0, label);
11891     }
11892
11893   mips_clear_insn_labels ();
11894
11895   float_cons (type);
11896 }
11897
11898 /* Handle .globl.  We need to override it because on Irix 5 you are
11899    permitted to say
11900        .globl foo .text
11901    where foo is an undefined symbol, to mean that foo should be
11902    considered to be the address of a function.  */
11903
11904 static void
11905 s_mips_globl (x)
11906      int x ATTRIBUTE_UNUSED;
11907 {
11908   char *name;
11909   int c;
11910   symbolS *symbolP;
11911   flagword flag;
11912
11913   name = input_line_pointer;
11914   c = get_symbol_end ();
11915   symbolP = symbol_find_or_make (name);
11916   *input_line_pointer = c;
11917   SKIP_WHITESPACE ();
11918
11919   /* On Irix 5, every global symbol that is not explicitly labelled as
11920      being a function is apparently labelled as being an object.  */
11921   flag = BSF_OBJECT;
11922
11923   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11924     {
11925       char *secname;
11926       asection *sec;
11927
11928       secname = input_line_pointer;
11929       c = get_symbol_end ();
11930       sec = bfd_get_section_by_name (stdoutput, secname);
11931       if (sec == NULL)
11932         as_bad (_("%s: no such section"), secname);
11933       *input_line_pointer = c;
11934
11935       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11936         flag = BSF_FUNCTION;
11937     }
11938
11939   symbol_get_bfdsym (symbolP)->flags |= flag;
11940
11941   S_SET_EXTERNAL (symbolP);
11942   demand_empty_rest_of_line ();
11943 }
11944
11945 static void
11946 s_option (x)
11947      int x ATTRIBUTE_UNUSED;
11948 {
11949   char *opt;
11950   char c;
11951
11952   opt = input_line_pointer;
11953   c = get_symbol_end ();
11954
11955   if (*opt == 'O')
11956     {
11957       /* FIXME: What does this mean?  */
11958     }
11959   else if (strncmp (opt, "pic", 3) == 0)
11960     {
11961       int i;
11962
11963       i = atoi (opt + 3);
11964       if (i == 0)
11965         mips_pic = NO_PIC;
11966       else if (i == 2)
11967         mips_pic = SVR4_PIC;
11968       else
11969         as_bad (_(".option pic%d not supported"), i);
11970
11971       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11972         {
11973           if (g_switch_seen && g_switch_value != 0)
11974             as_warn (_("-G may not be used with SVR4 PIC code"));
11975           g_switch_value = 0;
11976           bfd_set_gp_size (stdoutput, 0);
11977         }
11978     }
11979   else
11980     as_warn (_("Unrecognized option \"%s\""), opt);
11981
11982   *input_line_pointer = c;
11983   demand_empty_rest_of_line ();
11984 }
11985
11986 /* This structure is used to hold a stack of .set values.  */
11987
11988 struct mips_option_stack
11989 {
11990   struct mips_option_stack *next;
11991   struct mips_set_options options;
11992 };
11993
11994 static struct mips_option_stack *mips_opts_stack;
11995
11996 /* Handle the .set pseudo-op.  */
11997
11998 static void
11999 s_mipsset (x)
12000      int x ATTRIBUTE_UNUSED;
12001 {
12002   char *name = input_line_pointer, ch;
12003
12004   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12005     ++input_line_pointer;
12006   ch = *input_line_pointer;
12007   *input_line_pointer = '\0';
12008
12009   if (strcmp (name, "reorder") == 0)
12010     {
12011       if (mips_opts.noreorder && prev_nop_frag != NULL)
12012         {
12013           /* If we still have pending nops, we can discard them.  The
12014              usual nop handling will insert any that are still
12015              needed.  */
12016           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12017                                     * (mips_opts.mips16 ? 2 : 4));
12018           prev_nop_frag = NULL;
12019         }
12020       mips_opts.noreorder = 0;
12021     }
12022   else if (strcmp (name, "noreorder") == 0)
12023     {
12024       mips_emit_delays (TRUE);
12025       mips_opts.noreorder = 1;
12026       mips_any_noreorder = 1;
12027     }
12028   else if (strcmp (name, "at") == 0)
12029     {
12030       mips_opts.noat = 0;
12031     }
12032   else if (strcmp (name, "noat") == 0)
12033     {
12034       mips_opts.noat = 1;
12035     }
12036   else if (strcmp (name, "macro") == 0)
12037     {
12038       mips_opts.warn_about_macros = 0;
12039     }
12040   else if (strcmp (name, "nomacro") == 0)
12041     {
12042       if (mips_opts.noreorder == 0)
12043         as_bad (_("`noreorder' must be set before `nomacro'"));
12044       mips_opts.warn_about_macros = 1;
12045     }
12046   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12047     {
12048       mips_opts.nomove = 0;
12049     }
12050   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12051     {
12052       mips_opts.nomove = 1;
12053     }
12054   else if (strcmp (name, "bopt") == 0)
12055     {
12056       mips_opts.nobopt = 0;
12057     }
12058   else if (strcmp (name, "nobopt") == 0)
12059     {
12060       mips_opts.nobopt = 1;
12061     }
12062   else if (strcmp (name, "mips16") == 0
12063            || strcmp (name, "MIPS-16") == 0)
12064     mips_opts.mips16 = 1;
12065   else if (strcmp (name, "nomips16") == 0
12066            || strcmp (name, "noMIPS-16") == 0)
12067     mips_opts.mips16 = 0;
12068   else if (strcmp (name, "mips3d") == 0)
12069     mips_opts.ase_mips3d = 1;
12070   else if (strcmp (name, "nomips3d") == 0)
12071     mips_opts.ase_mips3d = 0;
12072   else if (strcmp (name, "mdmx") == 0)
12073     mips_opts.ase_mdmx = 1;
12074   else if (strcmp (name, "nomdmx") == 0)
12075     mips_opts.ase_mdmx = 0;
12076   else if (strncmp (name, "mips", 4) == 0)
12077     {
12078       int reset = 0;
12079
12080       /* Permit the user to change the ISA on the fly.  Needless to
12081          say, misuse can cause serious problems.  */
12082       if (strcmp (name, "mips0") == 0)
12083         {
12084           reset = 1;
12085           mips_opts.isa = file_mips_isa;
12086         }
12087       else if (strcmp (name, "mips1") == 0)
12088         mips_opts.isa = ISA_MIPS1;
12089       else if (strcmp (name, "mips2") == 0)
12090         mips_opts.isa = ISA_MIPS2;
12091       else if (strcmp (name, "mips3") == 0)
12092         mips_opts.isa = ISA_MIPS3;
12093       else if (strcmp (name, "mips4") == 0)
12094         mips_opts.isa = ISA_MIPS4;
12095       else if (strcmp (name, "mips5") == 0)
12096         mips_opts.isa = ISA_MIPS5;
12097       else if (strcmp (name, "mips32") == 0)
12098         mips_opts.isa = ISA_MIPS32;
12099       else if (strcmp (name, "mips32r2") == 0)
12100         mips_opts.isa = ISA_MIPS32R2;
12101       else if (strcmp (name, "mips64") == 0)
12102         mips_opts.isa = ISA_MIPS64;
12103       else
12104         as_bad (_("unknown ISA level %s"), name + 4);
12105
12106       switch (mips_opts.isa)
12107         {
12108         case  0:
12109           break;
12110         case ISA_MIPS1:
12111         case ISA_MIPS2:
12112         case ISA_MIPS32:
12113         case ISA_MIPS32R2:
12114           mips_opts.gp32 = 1;
12115           mips_opts.fp32 = 1;
12116           break;
12117         case ISA_MIPS3:
12118         case ISA_MIPS4:
12119         case ISA_MIPS5:
12120         case ISA_MIPS64:
12121           mips_opts.gp32 = 0;
12122           mips_opts.fp32 = 0;
12123           break;
12124         default:
12125           as_bad (_("unknown ISA level %s"), name + 4);
12126           break;
12127         }
12128       if (reset)
12129         {
12130           mips_opts.gp32 = file_mips_gp32;
12131           mips_opts.fp32 = file_mips_fp32;
12132         }
12133     }
12134   else if (strcmp (name, "autoextend") == 0)
12135     mips_opts.noautoextend = 0;
12136   else if (strcmp (name, "noautoextend") == 0)
12137     mips_opts.noautoextend = 1;
12138   else if (strcmp (name, "push") == 0)
12139     {
12140       struct mips_option_stack *s;
12141
12142       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12143       s->next = mips_opts_stack;
12144       s->options = mips_opts;
12145       mips_opts_stack = s;
12146     }
12147   else if (strcmp (name, "pop") == 0)
12148     {
12149       struct mips_option_stack *s;
12150
12151       s = mips_opts_stack;
12152       if (s == NULL)
12153         as_bad (_(".set pop with no .set push"));
12154       else
12155         {
12156           /* If we're changing the reorder mode we need to handle
12157              delay slots correctly.  */
12158           if (s->options.noreorder && ! mips_opts.noreorder)
12159             mips_emit_delays (TRUE);
12160           else if (! s->options.noreorder && mips_opts.noreorder)
12161             {
12162               if (prev_nop_frag != NULL)
12163                 {
12164                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12165                                             * (mips_opts.mips16 ? 2 : 4));
12166                   prev_nop_frag = NULL;
12167                 }
12168             }
12169
12170           mips_opts = s->options;
12171           mips_opts_stack = s->next;
12172           free (s);
12173         }
12174     }
12175   else
12176     {
12177       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12178     }
12179   *input_line_pointer = ch;
12180   demand_empty_rest_of_line ();
12181 }
12182
12183 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12184    .option pic2.  It means to generate SVR4 PIC calls.  */
12185
12186 static void
12187 s_abicalls (ignore)
12188      int ignore ATTRIBUTE_UNUSED;
12189 {
12190   mips_pic = SVR4_PIC;
12191   if (USE_GLOBAL_POINTER_OPT)
12192     {
12193       if (g_switch_seen && g_switch_value != 0)
12194         as_warn (_("-G may not be used with SVR4 PIC code"));
12195       g_switch_value = 0;
12196     }
12197   bfd_set_gp_size (stdoutput, 0);
12198   demand_empty_rest_of_line ();
12199 }
12200
12201 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12202    PIC code.  It sets the $gp register for the function based on the
12203    function address, which is in the register named in the argument.
12204    This uses a relocation against _gp_disp, which is handled specially
12205    by the linker.  The result is:
12206         lui     $gp,%hi(_gp_disp)
12207         addiu   $gp,$gp,%lo(_gp_disp)
12208         addu    $gp,$gp,.cpload argument
12209    The .cpload argument is normally $25 == $t9.  */
12210
12211 static void
12212 s_cpload (ignore)
12213      int ignore ATTRIBUTE_UNUSED;
12214 {
12215   expressionS ex;
12216   int icnt = 0;
12217
12218   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12219      .cpload is ignored.  */
12220   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12221     {
12222       s_ignore (0);
12223       return;
12224     }
12225
12226   /* .cpload should be in a .set noreorder section.  */
12227   if (mips_opts.noreorder == 0)
12228     as_warn (_(".cpload not in noreorder section"));
12229
12230   ex.X_op = O_symbol;
12231   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12232   ex.X_op_symbol = NULL;
12233   ex.X_add_number = 0;
12234
12235   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12236   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12237
12238   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12239   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12240                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12241
12242   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12243                mips_gp_register, mips_gp_register, tc_get_register (0));
12244
12245   demand_empty_rest_of_line ();
12246 }
12247
12248 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12249      .cpsetup $reg1, offset|$reg2, label
12250
12251    If offset is given, this results in:
12252      sd         $gp, offset($sp)
12253      lui        $gp, %hi(%neg(%gp_rel(label)))
12254      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12255      daddu      $gp, $gp, $reg1
12256
12257    If $reg2 is given, this results in:
12258      daddu      $reg2, $gp, $0
12259      lui        $gp, %hi(%neg(%gp_rel(label)))
12260      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12261      daddu      $gp, $gp, $reg1
12262    $reg1 is normally $25 == $t9.  */
12263 static void
12264 s_cpsetup (ignore)
12265      int ignore ATTRIBUTE_UNUSED;
12266 {
12267   expressionS ex_off;
12268   expressionS ex_sym;
12269   int reg1;
12270   int icnt = 0;
12271   char *f;
12272
12273   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12274      We also need NewABI support.  */
12275   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12276     {
12277       s_ignore (0);
12278       return;
12279     }
12280
12281   reg1 = tc_get_register (0);
12282   SKIP_WHITESPACE ();
12283   if (*input_line_pointer != ',')
12284     {
12285       as_bad (_("missing argument separator ',' for .cpsetup"));
12286       return;
12287     }
12288   else
12289     ++input_line_pointer;
12290   SKIP_WHITESPACE ();
12291   if (*input_line_pointer == '$')
12292     {
12293       mips_cpreturn_register = tc_get_register (0);
12294       mips_cpreturn_offset = -1;
12295     }
12296   else
12297     {
12298       mips_cpreturn_offset = get_absolute_expression ();
12299       mips_cpreturn_register = -1;
12300     }
12301   SKIP_WHITESPACE ();
12302   if (*input_line_pointer != ',')
12303     {
12304       as_bad (_("missing argument separator ',' for .cpsetup"));
12305       return;
12306     }
12307   else
12308     ++input_line_pointer;
12309   SKIP_WHITESPACE ();
12310   expression (&ex_sym);
12311
12312   if (mips_cpreturn_register == -1)
12313     {
12314       ex_off.X_op = O_constant;
12315       ex_off.X_add_symbol = NULL;
12316       ex_off.X_op_symbol = NULL;
12317       ex_off.X_add_number = mips_cpreturn_offset;
12318
12319       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12320                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12321     }
12322   else
12323     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12324                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12325
12326   /* Ensure there's room for the next two instructions, so that `f'
12327      doesn't end up with an address in the wrong frag.  */
12328   frag_grow (8);
12329   f = frag_more (0);
12330   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12331                (int) BFD_RELOC_GPREL16);
12332   fix_new (frag_now, f - frag_now->fr_literal,
12333            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12334   fix_new (frag_now, f - frag_now->fr_literal,
12335            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12336
12337   f = frag_more (0);
12338   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12339                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12340   fix_new (frag_now, f - frag_now->fr_literal,
12341            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12342   fix_new (frag_now, f - frag_now->fr_literal,
12343            0, NULL, 0, 0, BFD_RELOC_LO16);
12344
12345   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12346                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12347                mips_gp_register, mips_gp_register, reg1);
12348
12349   demand_empty_rest_of_line ();
12350 }
12351
12352 static void
12353 s_cplocal (ignore)
12354      int ignore ATTRIBUTE_UNUSED;
12355 {
12356   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12357    .cplocal is ignored.  */
12358   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12359     {
12360       s_ignore (0);
12361       return;
12362     }
12363
12364   mips_gp_register = tc_get_register (0);
12365   demand_empty_rest_of_line ();
12366 }
12367
12368 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12369    offset from $sp.  The offset is remembered, and after making a PIC
12370    call $gp is restored from that location.  */
12371
12372 static void
12373 s_cprestore (ignore)
12374      int ignore ATTRIBUTE_UNUSED;
12375 {
12376   expressionS ex;
12377   int icnt = 0;
12378
12379   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12380      .cprestore is ignored.  */
12381   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12382     {
12383       s_ignore (0);
12384       return;
12385     }
12386
12387   mips_cprestore_offset = get_absolute_expression ();
12388   mips_cprestore_valid = 1;
12389
12390   ex.X_op = O_constant;
12391   ex.X_add_symbol = NULL;
12392   ex.X_op_symbol = NULL;
12393   ex.X_add_number = mips_cprestore_offset;
12394
12395   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12396                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12397                                 mips_gp_register, SP);
12398
12399   demand_empty_rest_of_line ();
12400 }
12401
12402 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12403    was given in the preceeding .gpsetup, it results in:
12404      ld         $gp, offset($sp)
12405
12406    If a register $reg2 was given there, it results in:
12407      daddiu     $gp, $gp, $reg2
12408  */
12409 static void
12410 s_cpreturn (ignore)
12411      int ignore ATTRIBUTE_UNUSED;
12412 {
12413   expressionS ex;
12414   int icnt = 0;
12415
12416   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12417      We also need NewABI support.  */
12418   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12419     {
12420       s_ignore (0);
12421       return;
12422     }
12423
12424   if (mips_cpreturn_register == -1)
12425     {
12426       ex.X_op = O_constant;
12427       ex.X_add_symbol = NULL;
12428       ex.X_op_symbol = NULL;
12429       ex.X_add_number = mips_cpreturn_offset;
12430
12431       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12432                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12433     }
12434   else
12435     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12436                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12437
12438   demand_empty_rest_of_line ();
12439 }
12440
12441 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12442    code.  It sets the offset to use in gp_rel relocations.  */
12443
12444 static void
12445 s_gpvalue (ignore)
12446      int ignore ATTRIBUTE_UNUSED;
12447 {
12448   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12449      We also need NewABI support.  */
12450   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12451     {
12452       s_ignore (0);
12453       return;
12454     }
12455
12456   mips_gprel_offset = get_absolute_expression ();
12457
12458   demand_empty_rest_of_line ();
12459 }
12460
12461 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12462    code.  It generates a 32 bit GP relative reloc.  */
12463
12464 static void
12465 s_gpword (ignore)
12466      int ignore ATTRIBUTE_UNUSED;
12467 {
12468   symbolS *label;
12469   expressionS ex;
12470   char *p;
12471
12472   /* When not generating PIC code, this is treated as .word.  */
12473   if (mips_pic != SVR4_PIC)
12474     {
12475       s_cons (2);
12476       return;
12477     }
12478
12479   label = insn_labels != NULL ? insn_labels->label : NULL;
12480   mips_emit_delays (TRUE);
12481   if (auto_align)
12482     mips_align (2, 0, label);
12483   mips_clear_insn_labels ();
12484
12485   expression (&ex);
12486
12487   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12488     {
12489       as_bad (_("Unsupported use of .gpword"));
12490       ignore_rest_of_line ();
12491     }
12492
12493   p = frag_more (4);
12494   md_number_to_chars (p, (valueT) 0, 4);
12495   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12496                BFD_RELOC_GPREL32);
12497
12498   demand_empty_rest_of_line ();
12499 }
12500
12501 static void
12502 s_gpdword (ignore)
12503      int ignore ATTRIBUTE_UNUSED;
12504 {
12505   symbolS *label;
12506   expressionS ex;
12507   char *p;
12508
12509   /* When not generating PIC code, this is treated as .dword.  */
12510   if (mips_pic != SVR4_PIC)
12511     {
12512       s_cons (3);
12513       return;
12514     }
12515
12516   label = insn_labels != NULL ? insn_labels->label : NULL;
12517   mips_emit_delays (TRUE);
12518   if (auto_align)
12519     mips_align (3, 0, label);
12520   mips_clear_insn_labels ();
12521
12522   expression (&ex);
12523
12524   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12525     {
12526       as_bad (_("Unsupported use of .gpdword"));
12527       ignore_rest_of_line ();
12528     }
12529
12530   p = frag_more (8);
12531   md_number_to_chars (p, (valueT) 0, 8);
12532   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12533                BFD_RELOC_GPREL32);
12534
12535   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12536   ex.X_op = O_absent;
12537   ex.X_add_symbol = 0;
12538   ex.X_add_number = 0;
12539   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12540                BFD_RELOC_64);
12541
12542   demand_empty_rest_of_line ();
12543 }
12544
12545 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12546    tables in SVR4 PIC code.  */
12547
12548 static void
12549 s_cpadd (ignore)
12550      int ignore ATTRIBUTE_UNUSED;
12551 {
12552   int icnt = 0;
12553   int reg;
12554
12555   /* This is ignored when not generating SVR4 PIC code.  */
12556   if (mips_pic != SVR4_PIC)
12557     {
12558       s_ignore (0);
12559       return;
12560     }
12561
12562   /* Add $gp to the register named as an argument.  */
12563   reg = tc_get_register (0);
12564   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12565                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12566                "d,v,t", reg, reg, mips_gp_register);
12567
12568   demand_empty_rest_of_line ();
12569 }
12570
12571 /* Handle the .insn pseudo-op.  This marks instruction labels in
12572    mips16 mode.  This permits the linker to handle them specially,
12573    such as generating jalx instructions when needed.  We also make
12574    them odd for the duration of the assembly, in order to generate the
12575    right sort of code.  We will make them even in the adjust_symtab
12576    routine, while leaving them marked.  This is convenient for the
12577    debugger and the disassembler.  The linker knows to make them odd
12578    again.  */
12579
12580 static void
12581 s_insn (ignore)
12582      int ignore ATTRIBUTE_UNUSED;
12583 {
12584   mips16_mark_labels ();
12585
12586   demand_empty_rest_of_line ();
12587 }
12588
12589 /* Handle a .stabn directive.  We need these in order to mark a label
12590    as being a mips16 text label correctly.  Sometimes the compiler
12591    will emit a label, followed by a .stabn, and then switch sections.
12592    If the label and .stabn are in mips16 mode, then the label is
12593    really a mips16 text label.  */
12594
12595 static void
12596 s_mips_stab (type)
12597      int type;
12598 {
12599   if (type == 'n')
12600     mips16_mark_labels ();
12601
12602   s_stab (type);
12603 }
12604
12605 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12606  */
12607
12608 static void
12609 s_mips_weakext (ignore)
12610      int ignore ATTRIBUTE_UNUSED;
12611 {
12612   char *name;
12613   int c;
12614   symbolS *symbolP;
12615   expressionS exp;
12616
12617   name = input_line_pointer;
12618   c = get_symbol_end ();
12619   symbolP = symbol_find_or_make (name);
12620   S_SET_WEAK (symbolP);
12621   *input_line_pointer = c;
12622
12623   SKIP_WHITESPACE ();
12624
12625   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12626     {
12627       if (S_IS_DEFINED (symbolP))
12628         {
12629           as_bad ("ignoring attempt to redefine symbol %s",
12630                   S_GET_NAME (symbolP));
12631           ignore_rest_of_line ();
12632           return;
12633         }
12634
12635       if (*input_line_pointer == ',')
12636         {
12637           ++input_line_pointer;
12638           SKIP_WHITESPACE ();
12639         }
12640
12641       expression (&exp);
12642       if (exp.X_op != O_symbol)
12643         {
12644           as_bad ("bad .weakext directive");
12645           ignore_rest_of_line ();
12646           return;
12647         }
12648       symbol_set_value_expression (symbolP, &exp);
12649     }
12650
12651   demand_empty_rest_of_line ();
12652 }
12653
12654 /* Parse a register string into a number.  Called from the ECOFF code
12655    to parse .frame.  The argument is non-zero if this is the frame
12656    register, so that we can record it in mips_frame_reg.  */
12657
12658 int
12659 tc_get_register (frame)
12660      int frame;
12661 {
12662   int reg;
12663
12664   SKIP_WHITESPACE ();
12665   if (*input_line_pointer++ != '$')
12666     {
12667       as_warn (_("expected `$'"));
12668       reg = ZERO;
12669     }
12670   else if (ISDIGIT (*input_line_pointer))
12671     {
12672       reg = get_absolute_expression ();
12673       if (reg < 0 || reg >= 32)
12674         {
12675           as_warn (_("Bad register number"));
12676           reg = ZERO;
12677         }
12678     }
12679   else
12680     {
12681       if (strncmp (input_line_pointer, "ra", 2) == 0)
12682         {
12683           reg = RA;
12684           input_line_pointer += 2;
12685         }
12686       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12687         {
12688           reg = FP;
12689           input_line_pointer += 2;
12690         }
12691       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12692         {
12693           reg = SP;
12694           input_line_pointer += 2;
12695         }
12696       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12697         {
12698           reg = GP;
12699           input_line_pointer += 2;
12700         }
12701       else if (strncmp (input_line_pointer, "at", 2) == 0)
12702         {
12703           reg = AT;
12704           input_line_pointer += 2;
12705         }
12706       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12707         {
12708           reg = KT0;
12709           input_line_pointer += 3;
12710         }
12711       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12712         {
12713           reg = KT1;
12714           input_line_pointer += 3;
12715         }
12716       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12717         {
12718           reg = ZERO;
12719           input_line_pointer += 4;
12720         }
12721       else
12722         {
12723           as_warn (_("Unrecognized register name"));
12724           reg = ZERO;
12725           while (ISALNUM(*input_line_pointer))
12726            input_line_pointer++;
12727         }
12728     }
12729   if (frame)
12730     {
12731       mips_frame_reg = reg != 0 ? reg : SP;
12732       mips_frame_reg_valid = 1;
12733       mips_cprestore_valid = 0;
12734     }
12735   return reg;
12736 }
12737
12738 valueT
12739 md_section_align (seg, addr)
12740      asection *seg;
12741      valueT addr;
12742 {
12743   int align = bfd_get_section_alignment (stdoutput, seg);
12744
12745 #ifdef OBJ_ELF
12746   /* We don't need to align ELF sections to the full alignment.
12747      However, Irix 5 may prefer that we align them at least to a 16
12748      byte boundary.  We don't bother to align the sections if we are
12749      targeted for an embedded system.  */
12750   if (strcmp (TARGET_OS, "elf") == 0)
12751     return addr;
12752   if (align > 4)
12753     align = 4;
12754 #endif
12755
12756   return ((addr + (1 << align) - 1) & (-1 << align));
12757 }
12758
12759 /* Utility routine, called from above as well.  If called while the
12760    input file is still being read, it's only an approximation.  (For
12761    example, a symbol may later become defined which appeared to be
12762    undefined earlier.)  */
12763
12764 static int
12765 nopic_need_relax (sym, before_relaxing)
12766      symbolS *sym;
12767      int before_relaxing;
12768 {
12769   if (sym == 0)
12770     return 0;
12771
12772   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12773     {
12774       const char *symname;
12775       int change;
12776
12777       /* Find out whether this symbol can be referenced off the $gp
12778          register.  It can be if it is smaller than the -G size or if
12779          it is in the .sdata or .sbss section.  Certain symbols can
12780          not be referenced off the $gp, although it appears as though
12781          they can.  */
12782       symname = S_GET_NAME (sym);
12783       if (symname != (const char *) NULL
12784           && (strcmp (symname, "eprol") == 0
12785               || strcmp (symname, "etext") == 0
12786               || strcmp (symname, "_gp") == 0
12787               || strcmp (symname, "edata") == 0
12788               || strcmp (symname, "_fbss") == 0
12789               || strcmp (symname, "_fdata") == 0
12790               || strcmp (symname, "_ftext") == 0
12791               || strcmp (symname, "end") == 0
12792               || strcmp (symname, "_gp_disp") == 0))
12793         change = 1;
12794       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12795                && !S_IS_EXTERN (sym)
12796                && (0
12797 #ifndef NO_ECOFF_DEBUGGING
12798                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12799                        && (symbol_get_obj (sym)->ecoff_extern_size
12800                            <= g_switch_value))
12801 #endif
12802                    /* We must defer this decision until after the whole
12803                       file has been read, since there might be a .extern
12804                       after the first use of this symbol.  */
12805                    || (before_relaxing
12806 #ifndef NO_ECOFF_DEBUGGING
12807                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12808 #endif
12809                        && S_GET_VALUE (sym) == 0)
12810                    || (S_GET_VALUE (sym) != 0
12811                        && S_GET_VALUE (sym) <= g_switch_value)))
12812         change = 0;
12813       else
12814         {
12815           const char *segname;
12816
12817           segname = segment_name (S_GET_SEGMENT (sym));
12818           assert (strcmp (segname, ".lit8") != 0
12819                   && strcmp (segname, ".lit4") != 0);
12820           change = (strcmp (segname, ".sdata") != 0
12821                     && strcmp (segname, ".sbss") != 0
12822                     && strncmp (segname, ".sdata.", 7) != 0
12823                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12824         }
12825       return change;
12826     }
12827   else
12828     /* We are not optimizing for the $gp register.  */
12829     return 1;
12830 }
12831
12832
12833 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
12834
12835 static bfd_boolean
12836 pic_need_relax (sym, segtype)
12837      symbolS *sym;
12838      asection *segtype;
12839 {
12840   asection *symsec;
12841   bfd_boolean linkonce;
12842
12843   /* Handle the case of a symbol equated to another symbol.  */
12844   while (symbol_equated_reloc_p (sym))
12845     {
12846       symbolS *n;
12847
12848       /* It's possible to get a loop here in a badly written
12849          program.  */
12850       n = symbol_get_value_expression (sym)->X_add_symbol;
12851       if (n == sym)
12852         break;
12853       sym = n;
12854     }
12855
12856   symsec = S_GET_SEGMENT (sym);
12857
12858   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12859   linkonce = FALSE;
12860   if (symsec != segtype && ! S_IS_LOCAL (sym))
12861     {
12862       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12863           != 0)
12864         linkonce = TRUE;
12865
12866       /* The GNU toolchain uses an extension for ELF: a section
12867          beginning with the magic string .gnu.linkonce is a linkonce
12868          section.  */
12869       if (strncmp (segment_name (symsec), ".gnu.linkonce",
12870                    sizeof ".gnu.linkonce" - 1) == 0)
12871         linkonce = TRUE;
12872     }
12873
12874   /* This must duplicate the test in adjust_reloc_syms.  */
12875   return (symsec != &bfd_und_section
12876           && symsec != &bfd_abs_section
12877           && ! bfd_is_com_section (symsec)
12878           && !linkonce
12879 #ifdef OBJ_ELF
12880           /* A global or weak symbol is treated as external.  */
12881           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12882               || (! S_IS_WEAK (sym)
12883                   && (! S_IS_EXTERNAL (sym)
12884                       || mips_pic == EMBEDDED_PIC)))
12885 #endif
12886           );
12887 }
12888
12889
12890 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12891    extended opcode.  SEC is the section the frag is in.  */
12892
12893 static int
12894 mips16_extended_frag (fragp, sec, stretch)
12895      fragS *fragp;
12896      asection *sec;
12897      long stretch;
12898 {
12899   int type;
12900   register const struct mips16_immed_operand *op;
12901   offsetT val;
12902   int mintiny, maxtiny;
12903   segT symsec;
12904   fragS *sym_frag;
12905
12906   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12907     return 0;
12908   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12909     return 1;
12910
12911   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12912   op = mips16_immed_operands;
12913   while (op->type != type)
12914     {
12915       ++op;
12916       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12917     }
12918
12919   if (op->unsp)
12920     {
12921       if (type == '<' || type == '>' || type == '[' || type == ']')
12922         {
12923           mintiny = 1;
12924           maxtiny = 1 << op->nbits;
12925         }
12926       else
12927         {
12928           mintiny = 0;
12929           maxtiny = (1 << op->nbits) - 1;
12930         }
12931     }
12932   else
12933     {
12934       mintiny = - (1 << (op->nbits - 1));
12935       maxtiny = (1 << (op->nbits - 1)) - 1;
12936     }
12937
12938   sym_frag = symbol_get_frag (fragp->fr_symbol);
12939   val = S_GET_VALUE (fragp->fr_symbol);
12940   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12941
12942   if (op->pcrel)
12943     {
12944       addressT addr;
12945
12946       /* We won't have the section when we are called from
12947          mips_relax_frag.  However, we will always have been called
12948          from md_estimate_size_before_relax first.  If this is a
12949          branch to a different section, we mark it as such.  If SEC is
12950          NULL, and the frag is not marked, then it must be a branch to
12951          the same section.  */
12952       if (sec == NULL)
12953         {
12954           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12955             return 1;
12956         }
12957       else
12958         {
12959           /* Must have been called from md_estimate_size_before_relax.  */
12960           if (symsec != sec)
12961             {
12962               fragp->fr_subtype =
12963                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12964
12965               /* FIXME: We should support this, and let the linker
12966                  catch branches and loads that are out of range.  */
12967               as_bad_where (fragp->fr_file, fragp->fr_line,
12968                             _("unsupported PC relative reference to different section"));
12969
12970               return 1;
12971             }
12972           if (fragp != sym_frag && sym_frag->fr_address == 0)
12973             /* Assume non-extended on the first relaxation pass.
12974                The address we have calculated will be bogus if this is
12975                a forward branch to another frag, as the forward frag
12976                will have fr_address == 0.  */
12977             return 0;
12978         }
12979
12980       /* In this case, we know for sure that the symbol fragment is in
12981          the same section.  If the relax_marker of the symbol fragment
12982          differs from the relax_marker of this fragment, we have not
12983          yet adjusted the symbol fragment fr_address.  We want to add
12984          in STRETCH in order to get a better estimate of the address.
12985          This particularly matters because of the shift bits.  */
12986       if (stretch != 0
12987           && sym_frag->relax_marker != fragp->relax_marker)
12988         {
12989           fragS *f;
12990
12991           /* Adjust stretch for any alignment frag.  Note that if have
12992              been expanding the earlier code, the symbol may be
12993              defined in what appears to be an earlier frag.  FIXME:
12994              This doesn't handle the fr_subtype field, which specifies
12995              a maximum number of bytes to skip when doing an
12996              alignment.  */
12997           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12998             {
12999               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13000                 {
13001                   if (stretch < 0)
13002                     stretch = - ((- stretch)
13003                                  & ~ ((1 << (int) f->fr_offset) - 1));
13004                   else
13005                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13006                   if (stretch == 0)
13007                     break;
13008                 }
13009             }
13010           if (f != NULL)
13011             val += stretch;
13012         }
13013
13014       addr = fragp->fr_address + fragp->fr_fix;
13015
13016       /* The base address rules are complicated.  The base address of
13017          a branch is the following instruction.  The base address of a
13018          PC relative load or add is the instruction itself, but if it
13019          is in a delay slot (in which case it can not be extended) use
13020          the address of the instruction whose delay slot it is in.  */
13021       if (type == 'p' || type == 'q')
13022         {
13023           addr += 2;
13024
13025           /* If we are currently assuming that this frag should be
13026              extended, then, the current address is two bytes
13027              higher.  */
13028           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13029             addr += 2;
13030
13031           /* Ignore the low bit in the target, since it will be set
13032              for a text label.  */
13033           if ((val & 1) != 0)
13034             --val;
13035         }
13036       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13037         addr -= 4;
13038       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13039         addr -= 2;
13040
13041       val -= addr & ~ ((1 << op->shift) - 1);
13042
13043       /* Branch offsets have an implicit 0 in the lowest bit.  */
13044       if (type == 'p' || type == 'q')
13045         val /= 2;
13046
13047       /* If any of the shifted bits are set, we must use an extended
13048          opcode.  If the address depends on the size of this
13049          instruction, this can lead to a loop, so we arrange to always
13050          use an extended opcode.  We only check this when we are in
13051          the main relaxation loop, when SEC is NULL.  */
13052       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13053         {
13054           fragp->fr_subtype =
13055             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13056           return 1;
13057         }
13058
13059       /* If we are about to mark a frag as extended because the value
13060          is precisely maxtiny + 1, then there is a chance of an
13061          infinite loop as in the following code:
13062              la $4,foo
13063              .skip      1020
13064              .align     2
13065            foo:
13066          In this case when the la is extended, foo is 0x3fc bytes
13067          away, so the la can be shrunk, but then foo is 0x400 away, so
13068          the la must be extended.  To avoid this loop, we mark the
13069          frag as extended if it was small, and is about to become
13070          extended with a value of maxtiny + 1.  */
13071       if (val == ((maxtiny + 1) << op->shift)
13072           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13073           && sec == NULL)
13074         {
13075           fragp->fr_subtype =
13076             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13077           return 1;
13078         }
13079     }
13080   else if (symsec != absolute_section && sec != NULL)
13081     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13082
13083   if ((val & ((1 << op->shift) - 1)) != 0
13084       || val < (mintiny << op->shift)
13085       || val > (maxtiny << op->shift))
13086     return 1;
13087   else
13088     return 0;
13089 }
13090
13091 /* Compute the length of a branch sequence, and adjust the
13092    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13093    worst-case length is computed, with UPDATE being used to indicate
13094    whether an unconditional (-1), branch-likely (+1) or regular (0)
13095    branch is to be computed.  */
13096 static int
13097 relaxed_branch_length (fragp, sec, update)
13098      fragS *fragp;
13099      asection *sec;
13100      int update;
13101 {
13102   bfd_boolean toofar;
13103   int length;
13104
13105   if (fragp
13106       && S_IS_DEFINED (fragp->fr_symbol)
13107       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13108     {
13109       addressT addr;
13110       offsetT val;
13111
13112       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13113
13114       addr = fragp->fr_address + fragp->fr_fix + 4;
13115
13116       val -= addr;
13117
13118       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13119     }
13120   else if (fragp)
13121     /* If the symbol is not defined or it's in a different segment,
13122        assume the user knows what's going on and emit a short
13123        branch.  */
13124     toofar = FALSE;
13125   else
13126     toofar = TRUE;
13127
13128   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13129     fragp->fr_subtype
13130       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13131                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13132                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13133                              toofar);
13134
13135   length = 4;
13136   if (toofar)
13137     {
13138       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13139         length += 8;
13140
13141       if (mips_pic != NO_PIC)
13142         {
13143           /* Additional space for PIC loading of target address.  */
13144           length += 8;
13145           if (mips_opts.isa == ISA_MIPS1)
13146             /* Additional space for $at-stabilizing nop.  */
13147             length += 4;
13148         }
13149
13150       /* If branch is conditional.  */
13151       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13152         length += 8;
13153     }
13154
13155   return length;
13156 }
13157
13158 /* Estimate the size of a frag before relaxing.  Unless this is the
13159    mips16, we are not really relaxing here, and the final size is
13160    encoded in the subtype information.  For the mips16, we have to
13161    decide whether we are using an extended opcode or not.  */
13162
13163 int
13164 md_estimate_size_before_relax (fragp, segtype)
13165      fragS *fragp;
13166      asection *segtype;
13167 {
13168   int change;
13169
13170   if (RELAX_BRANCH_P (fragp->fr_subtype))
13171     {
13172
13173       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13174
13175       return fragp->fr_var;
13176     }
13177
13178   if (RELAX_MIPS16_P (fragp->fr_subtype))
13179     /* We don't want to modify the EXTENDED bit here; it might get us
13180        into infinite loops.  We change it only in mips_relax_frag().  */
13181     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13182
13183   if (mips_pic == NO_PIC)
13184     change = nopic_need_relax (fragp->fr_symbol, 0);
13185   else if (mips_pic == SVR4_PIC)
13186     change = pic_need_relax (fragp->fr_symbol, segtype);
13187   else
13188     abort ();
13189
13190   if (change)
13191     {
13192       /* Record the offset to the first reloc in the fr_opcode field.
13193          This lets md_convert_frag and tc_gen_reloc know that the code
13194          must be expanded.  */
13195       fragp->fr_opcode = (fragp->fr_literal
13196                           + fragp->fr_fix
13197                           - RELAX_OLD (fragp->fr_subtype)
13198                           + RELAX_RELOC1 (fragp->fr_subtype));
13199       /* FIXME: This really needs as_warn_where.  */
13200       if (RELAX_WARN (fragp->fr_subtype))
13201         as_warn (_("AT used after \".set noat\" or macro used after "
13202                    "\".set nomacro\""));
13203
13204       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13205     }
13206
13207   return 0;
13208 }
13209
13210 /* This is called to see whether a reloc against a defined symbol
13211    should be converted into a reloc against a section.  Don't adjust
13212    MIPS16 jump relocations, so we don't have to worry about the format
13213    of the offset in the .o file.  Don't adjust relocations against
13214    mips16 symbols, so that the linker can find them if it needs to set
13215    up a stub.  */
13216
13217 int
13218 mips_fix_adjustable (fixp)
13219      fixS *fixp;
13220 {
13221   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13222     return 0;
13223
13224   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13225       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13226     return 0;
13227
13228   if (fixp->fx_addsy == NULL)
13229     return 1;
13230
13231 #ifdef OBJ_ELF
13232   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13233       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13234       && fixp->fx_subsy == NULL)
13235     return 0;
13236 #endif
13237
13238   return 1;
13239 }
13240
13241 /* Translate internal representation of relocation info to BFD target
13242    format.  */
13243
13244 arelent **
13245 tc_gen_reloc (section, fixp)
13246      asection *section ATTRIBUTE_UNUSED;
13247      fixS *fixp;
13248 {
13249   static arelent *retval[4];
13250   arelent *reloc;
13251   bfd_reloc_code_real_type code;
13252
13253   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13254   retval[1] = NULL;
13255
13256   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13257   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13258   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13259
13260   if (mips_pic == EMBEDDED_PIC
13261       && SWITCH_TABLE (fixp))
13262     {
13263       /* For a switch table entry we use a special reloc.  The addend
13264          is actually the difference between the reloc address and the
13265          subtrahend.  */
13266       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13267       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13268         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13269       fixp->fx_r_type = BFD_RELOC_GPREL32;
13270     }
13271   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13272     {
13273       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13274         reloc->addend = fixp->fx_addnumber;
13275       else
13276         {
13277           /* We use a special addend for an internal RELLO reloc.  */
13278           if (symbol_section_p (fixp->fx_addsy))
13279             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13280           else
13281             reloc->addend = fixp->fx_addnumber + reloc->address;
13282         }
13283     }
13284   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13285     {
13286       assert (fixp->fx_next != NULL
13287               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13288
13289       /* The reloc is relative to the RELLO; adjust the addend
13290          accordingly.  */
13291       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13292         reloc->addend = fixp->fx_next->fx_addnumber;
13293       else
13294         {
13295           /* We use a special addend for an internal RELHI reloc.  */
13296           if (symbol_section_p (fixp->fx_addsy))
13297             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13298                              + fixp->fx_next->fx_where
13299                              - S_GET_VALUE (fixp->fx_subsy));
13300           else
13301             reloc->addend = (fixp->fx_addnumber
13302                              + fixp->fx_next->fx_frag->fr_address
13303                              + fixp->fx_next->fx_where);
13304         }
13305     }
13306   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13307     reloc->addend = fixp->fx_addnumber;
13308   else
13309     {
13310       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13311         /* A gruesome hack which is a result of the gruesome gas reloc
13312            handling.  */
13313         reloc->addend = reloc->address;
13314       else
13315         reloc->addend = -reloc->address;
13316     }
13317
13318   /* If this is a variant frag, we may need to adjust the existing
13319      reloc and generate a new one.  */
13320   if (fixp->fx_frag->fr_opcode != NULL
13321       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13322            && ! HAVE_NEWABI)
13323           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13324           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13325           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13326           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13327           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13328           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13329     )
13330     {
13331       arelent *reloc2;
13332
13333       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13334
13335       /* If this is not the last reloc in this frag, then we have two
13336          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13337          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13338          the second one handle all of them.  */
13339       if (fixp->fx_next != NULL
13340           && fixp->fx_frag == fixp->fx_next->fx_frag)
13341         {
13342           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13343                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13344                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13345                       && (fixp->fx_next->fx_r_type
13346                           == BFD_RELOC_MIPS_GOT_LO16))
13347                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13348                       && (fixp->fx_next->fx_r_type
13349                           == BFD_RELOC_MIPS_CALL_LO16)));
13350           retval[0] = NULL;
13351           return retval;
13352         }
13353
13354       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13355       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13356       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13357       retval[2] = NULL;
13358       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13359       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13360       reloc2->address = (reloc->address
13361                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13362                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13363       reloc2->addend = fixp->fx_addnumber;
13364       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13365       assert (reloc2->howto != NULL);
13366
13367       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13368         {
13369           arelent *reloc3;
13370
13371           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13372           retval[3] = NULL;
13373           *reloc3 = *reloc2;
13374           reloc3->address += 4;
13375         }
13376
13377       if (mips_pic == NO_PIC)
13378         {
13379           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13380           fixp->fx_r_type = BFD_RELOC_HI16_S;
13381         }
13382       else if (mips_pic == SVR4_PIC)
13383         {
13384           switch (fixp->fx_r_type)
13385             {
13386             default:
13387               abort ();
13388             case BFD_RELOC_MIPS_GOT16:
13389               break;
13390             case BFD_RELOC_MIPS_GOT_LO16:
13391             case BFD_RELOC_MIPS_CALL_LO16:
13392               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13393               break;
13394             case BFD_RELOC_MIPS_CALL16:
13395               if (HAVE_NEWABI)
13396                 {
13397                   /* BFD_RELOC_MIPS_GOT16;*/
13398                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13399                   reloc2->howto = bfd_reloc_type_lookup
13400                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13401                 }
13402               else
13403                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13404               break;
13405             }
13406         }
13407       else
13408         abort ();
13409
13410       /* newabi uses R_MIPS_GOT_DISP for local symbols */
13411       if (HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16)
13412         {
13413           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13414           retval[1] = NULL;
13415         }
13416     }
13417
13418   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13419      entry to be used in the relocation's section offset.  */
13420   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13421     {
13422       reloc->address = reloc->addend;
13423       reloc->addend = 0;
13424     }
13425
13426   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13427      fixup_segment converted a non-PC relative reloc into a PC
13428      relative reloc.  In such a case, we need to convert the reloc
13429      code.  */
13430   code = fixp->fx_r_type;
13431   if (fixp->fx_pcrel)
13432     {
13433       switch (code)
13434         {
13435         case BFD_RELOC_8:
13436           code = BFD_RELOC_8_PCREL;
13437           break;
13438         case BFD_RELOC_16:
13439           code = BFD_RELOC_16_PCREL;
13440           break;
13441         case BFD_RELOC_32:
13442           code = BFD_RELOC_32_PCREL;
13443           break;
13444         case BFD_RELOC_64:
13445           code = BFD_RELOC_64_PCREL;
13446           break;
13447         case BFD_RELOC_8_PCREL:
13448         case BFD_RELOC_16_PCREL:
13449         case BFD_RELOC_32_PCREL:
13450         case BFD_RELOC_64_PCREL:
13451         case BFD_RELOC_16_PCREL_S2:
13452         case BFD_RELOC_PCREL_HI16_S:
13453         case BFD_RELOC_PCREL_LO16:
13454           break;
13455         default:
13456           as_bad_where (fixp->fx_file, fixp->fx_line,
13457                         _("Cannot make %s relocation PC relative"),
13458                         bfd_get_reloc_code_name (code));
13459         }
13460     }
13461
13462 #ifdef OBJ_ELF
13463   /* md_apply_fix3 has a double-subtraction hack to get
13464      bfd_install_relocation to behave nicely.  GPREL relocations are
13465      handled correctly without this hack, so undo it here.  We can't
13466      stop md_apply_fix3 from subtracting twice in the first place since
13467      the fake addend is required for variant frags above.  */
13468   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13469       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13470       && reloc->addend != 0
13471       && mips_need_elf_addend_fixup (fixp))
13472     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13473 #endif
13474
13475   /* To support a PC relative reloc when generating embedded PIC code
13476      for ECOFF, we use a Cygnus extension.  We check for that here to
13477      make sure that we don't let such a reloc escape normally.  */
13478   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13479        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13480       && code == BFD_RELOC_16_PCREL_S2
13481       && mips_pic != EMBEDDED_PIC)
13482     reloc->howto = NULL;
13483   else
13484     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13485
13486   if (reloc->howto == NULL)
13487     {
13488       as_bad_where (fixp->fx_file, fixp->fx_line,
13489                     _("Can not represent %s relocation in this object file format"),
13490                     bfd_get_reloc_code_name (code));
13491       retval[0] = NULL;
13492     }
13493
13494   return retval;
13495 }
13496
13497 /* Relax a machine dependent frag.  This returns the amount by which
13498    the current size of the frag should change.  */
13499
13500 int
13501 mips_relax_frag (sec, fragp, stretch)
13502      asection *sec;
13503      fragS *fragp;
13504      long stretch;
13505 {
13506   if (RELAX_BRANCH_P (fragp->fr_subtype))
13507     {
13508       offsetT old_var = fragp->fr_var;
13509
13510       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13511
13512       return fragp->fr_var - old_var;
13513     }
13514
13515   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13516     return 0;
13517
13518   if (mips16_extended_frag (fragp, NULL, stretch))
13519     {
13520       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13521         return 0;
13522       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13523       return 2;
13524     }
13525   else
13526     {
13527       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13528         return 0;
13529       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13530       return -2;
13531     }
13532
13533   return 0;
13534 }
13535
13536 /* Convert a machine dependent frag.  */
13537
13538 void
13539 md_convert_frag (abfd, asec, fragp)
13540      bfd *abfd ATTRIBUTE_UNUSED;
13541      segT asec;
13542      fragS *fragp;
13543 {
13544   int old, new;
13545   char *fixptr;
13546
13547   if (RELAX_BRANCH_P (fragp->fr_subtype))
13548     {
13549       bfd_byte *buf;
13550       unsigned long insn;
13551       expressionS exp;
13552       fixS *fixp;
13553
13554       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13555
13556       if (target_big_endian)
13557         insn = bfd_getb32 (buf);
13558       else
13559         insn = bfd_getl32 (buf);
13560
13561       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13562         {
13563           /* We generate a fixup instead of applying it right now
13564              because, if there are linker relaxations, we're going to
13565              need the relocations.  */
13566           exp.X_op = O_symbol;
13567           exp.X_add_symbol = fragp->fr_symbol;
13568           exp.X_add_number = fragp->fr_offset;
13569
13570           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13571                               4, &exp, 1,
13572                               BFD_RELOC_16_PCREL_S2);
13573           fixp->fx_file = fragp->fr_file;
13574           fixp->fx_line = fragp->fr_line;
13575
13576           md_number_to_chars ((char *)buf, insn, 4);
13577           buf += 4;
13578         }
13579       else
13580         {
13581           int i;
13582
13583           as_warn_where (fragp->fr_file, fragp->fr_line,
13584                          _("relaxed out-of-range branch into a jump"));
13585
13586           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13587             goto uncond;
13588
13589           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13590             {
13591               /* Reverse the branch.  */
13592               switch ((insn >> 28) & 0xf)
13593                 {
13594                 case 4:
13595                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13596                      have the condition reversed by tweaking a single
13597                      bit, and their opcodes all have 0x4???????.  */
13598                   assert ((insn & 0xf1000000) == 0x41000000);
13599                   insn ^= 0x00010000;
13600                   break;
13601
13602                 case 0:
13603                   /* bltz       0x04000000      bgez    0x04010000
13604                      bltzal     0x04100000      bgezal  0x04110000 */
13605                   assert ((insn & 0xfc0e0000) == 0x04000000);
13606                   insn ^= 0x00010000;
13607                   break;
13608
13609                 case 1:
13610                   /* beq        0x10000000      bne     0x14000000
13611                      blez       0x18000000      bgtz    0x1c000000 */
13612                   insn ^= 0x04000000;
13613                   break;
13614
13615                 default:
13616                   abort ();
13617                 }
13618             }
13619
13620           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13621             {
13622               /* Clear the and-link bit.  */
13623               assert ((insn & 0xfc1c0000) == 0x04100000);
13624
13625               /* bltzal 0x04100000      bgezal  0x04110000
13626                 bltzall 0x04120000     bgezall  0x04130000 */
13627               insn &= ~0x00100000;
13628             }
13629
13630           /* Branch over the branch (if the branch was likely) or the
13631              full jump (not likely case).  Compute the offset from the
13632              current instruction to branch to.  */
13633           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13634             i = 16;
13635           else
13636             {
13637               /* How many bytes in instructions we've already emitted?  */
13638               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13639               /* How many bytes in instructions from here to the end?  */
13640               i = fragp->fr_var - i;
13641             }
13642           /* Convert to instruction count.  */
13643           i >>= 2;
13644           /* Branch counts from the next instruction.  */
13645           i--;
13646           insn |= i;
13647           /* Branch over the jump.  */
13648           md_number_to_chars ((char *)buf, insn, 4);
13649           buf += 4;
13650
13651           /* Nop */
13652           md_number_to_chars ((char*)buf, 0, 4);
13653           buf += 4;
13654
13655           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13656             {
13657               /* beql $0, $0, 2f */
13658               insn = 0x50000000;
13659               /* Compute the PC offset from the current instruction to
13660                  the end of the variable frag.  */
13661               /* How many bytes in instructions we've already emitted?  */
13662               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13663               /* How many bytes in instructions from here to the end?  */
13664               i = fragp->fr_var - i;
13665               /* Convert to instruction count.  */
13666               i >>= 2;
13667               /* Don't decrement i, because we want to branch over the
13668                  delay slot.  */
13669
13670               insn |= i;
13671               md_number_to_chars ((char *)buf, insn, 4);
13672               buf += 4;
13673
13674               md_number_to_chars ((char *)buf, 0, 4);
13675               buf += 4;
13676             }
13677
13678         uncond:
13679           if (mips_pic == NO_PIC)
13680             {
13681               /* j or jal.  */
13682               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13683                       ? 0x0c000000 : 0x08000000);
13684               exp.X_op = O_symbol;
13685               exp.X_add_symbol = fragp->fr_symbol;
13686               exp.X_add_number = fragp->fr_offset;
13687
13688               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13689                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13690               fixp->fx_file = fragp->fr_file;
13691               fixp->fx_line = fragp->fr_line;
13692
13693               md_number_to_chars ((char*)buf, insn, 4);
13694               buf += 4;
13695             }
13696           else
13697             {
13698               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13699               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13700               exp.X_op = O_symbol;
13701               exp.X_add_symbol = fragp->fr_symbol;
13702               exp.X_add_number = fragp->fr_offset;
13703
13704               if (fragp->fr_offset)
13705                 {
13706                   exp.X_add_symbol = make_expr_symbol (&exp);
13707                   exp.X_add_number = 0;
13708                 }
13709
13710               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13711                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13712               fixp->fx_file = fragp->fr_file;
13713               fixp->fx_line = fragp->fr_line;
13714
13715               md_number_to_chars ((char*)buf, insn, 4);
13716               buf += 4;
13717
13718               if (mips_opts.isa == ISA_MIPS1)
13719                 {
13720                   /* nop */
13721                   md_number_to_chars ((char*)buf, 0, 4);
13722                   buf += 4;
13723                 }
13724
13725               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13726               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13727
13728               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13729                                   4, &exp, 0, BFD_RELOC_LO16);
13730               fixp->fx_file = fragp->fr_file;
13731               fixp->fx_line = fragp->fr_line;
13732
13733               md_number_to_chars ((char*)buf, insn, 4);
13734               buf += 4;
13735
13736               /* j(al)r $at.  */
13737               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13738                 insn = 0x0020f809;
13739               else
13740                 insn = 0x00200008;
13741
13742               md_number_to_chars ((char*)buf, insn, 4);
13743               buf += 4;
13744             }
13745         }
13746
13747       assert (buf == (bfd_byte *)fragp->fr_literal
13748               + fragp->fr_fix + fragp->fr_var);
13749
13750       fragp->fr_fix += fragp->fr_var;
13751
13752       return;
13753     }
13754
13755   if (RELAX_MIPS16_P (fragp->fr_subtype))
13756     {
13757       int type;
13758       register const struct mips16_immed_operand *op;
13759       bfd_boolean small, ext;
13760       offsetT val;
13761       bfd_byte *buf;
13762       unsigned long insn;
13763       bfd_boolean use_extend;
13764       unsigned short extend;
13765
13766       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13767       op = mips16_immed_operands;
13768       while (op->type != type)
13769         ++op;
13770
13771       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13772         {
13773           small = FALSE;
13774           ext = TRUE;
13775         }
13776       else
13777         {
13778           small = TRUE;
13779           ext = FALSE;
13780         }
13781
13782       resolve_symbol_value (fragp->fr_symbol);
13783       val = S_GET_VALUE (fragp->fr_symbol);
13784       if (op->pcrel)
13785         {
13786           addressT addr;
13787
13788           addr = fragp->fr_address + fragp->fr_fix;
13789
13790           /* The rules for the base address of a PC relative reloc are
13791              complicated; see mips16_extended_frag.  */
13792           if (type == 'p' || type == 'q')
13793             {
13794               addr += 2;
13795               if (ext)
13796                 addr += 2;
13797               /* Ignore the low bit in the target, since it will be
13798                  set for a text label.  */
13799               if ((val & 1) != 0)
13800                 --val;
13801             }
13802           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13803             addr -= 4;
13804           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13805             addr -= 2;
13806
13807           addr &= ~ (addressT) ((1 << op->shift) - 1);
13808           val -= addr;
13809
13810           /* Make sure the section winds up with the alignment we have
13811              assumed.  */
13812           if (op->shift > 0)
13813             record_alignment (asec, op->shift);
13814         }
13815
13816       if (ext
13817           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13818               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13819         as_warn_where (fragp->fr_file, fragp->fr_line,
13820                        _("extended instruction in delay slot"));
13821
13822       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13823
13824       if (target_big_endian)
13825         insn = bfd_getb16 (buf);
13826       else
13827         insn = bfd_getl16 (buf);
13828
13829       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13830                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13831                     small, ext, &insn, &use_extend, &extend);
13832
13833       if (use_extend)
13834         {
13835           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13836           fragp->fr_fix += 2;
13837           buf += 2;
13838         }
13839
13840       md_number_to_chars ((char *) buf, insn, 2);
13841       fragp->fr_fix += 2;
13842       buf += 2;
13843     }
13844   else
13845     {
13846       if (fragp->fr_opcode == NULL)
13847         return;
13848
13849       old = RELAX_OLD (fragp->fr_subtype);
13850       new = RELAX_NEW (fragp->fr_subtype);
13851       fixptr = fragp->fr_literal + fragp->fr_fix;
13852
13853       if (new > 0)
13854         memcpy (fixptr - old, fixptr, new);
13855
13856       fragp->fr_fix += new - old;
13857     }
13858 }
13859
13860 #ifdef OBJ_ELF
13861
13862 /* This function is called after the relocs have been generated.
13863    We've been storing mips16 text labels as odd.  Here we convert them
13864    back to even for the convenience of the debugger.  */
13865
13866 void
13867 mips_frob_file_after_relocs ()
13868 {
13869   asymbol **syms;
13870   unsigned int count, i;
13871
13872   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13873     return;
13874
13875   syms = bfd_get_outsymbols (stdoutput);
13876   count = bfd_get_symcount (stdoutput);
13877   for (i = 0; i < count; i++, syms++)
13878     {
13879       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13880           && ((*syms)->value & 1) != 0)
13881         {
13882           (*syms)->value &= ~1;
13883           /* If the symbol has an odd size, it was probably computed
13884              incorrectly, so adjust that as well.  */
13885           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13886             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13887         }
13888     }
13889 }
13890
13891 #endif
13892
13893 /* This function is called whenever a label is defined.  It is used
13894    when handling branch delays; if a branch has a label, we assume we
13895    can not move it.  */
13896
13897 void
13898 mips_define_label (sym)
13899      symbolS *sym;
13900 {
13901   struct insn_label_list *l;
13902
13903   if (free_insn_labels == NULL)
13904     l = (struct insn_label_list *) xmalloc (sizeof *l);
13905   else
13906     {
13907       l = free_insn_labels;
13908       free_insn_labels = l->next;
13909     }
13910
13911   l->label = sym;
13912   l->next = insn_labels;
13913   insn_labels = l;
13914 }
13915 \f
13916 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13917
13918 /* Some special processing for a MIPS ELF file.  */
13919
13920 void
13921 mips_elf_final_processing ()
13922 {
13923   /* Write out the register information.  */
13924   if (mips_abi != N64_ABI)
13925     {
13926       Elf32_RegInfo s;
13927
13928       s.ri_gprmask = mips_gprmask;
13929       s.ri_cprmask[0] = mips_cprmask[0];
13930       s.ri_cprmask[1] = mips_cprmask[1];
13931       s.ri_cprmask[2] = mips_cprmask[2];
13932       s.ri_cprmask[3] = mips_cprmask[3];
13933       /* The gp_value field is set by the MIPS ELF backend.  */
13934
13935       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13936                                        ((Elf32_External_RegInfo *)
13937                                         mips_regmask_frag));
13938     }
13939   else
13940     {
13941       Elf64_Internal_RegInfo s;
13942
13943       s.ri_gprmask = mips_gprmask;
13944       s.ri_pad = 0;
13945       s.ri_cprmask[0] = mips_cprmask[0];
13946       s.ri_cprmask[1] = mips_cprmask[1];
13947       s.ri_cprmask[2] = mips_cprmask[2];
13948       s.ri_cprmask[3] = mips_cprmask[3];
13949       /* The gp_value field is set by the MIPS ELF backend.  */
13950
13951       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13952                                        ((Elf64_External_RegInfo *)
13953                                         mips_regmask_frag));
13954     }
13955
13956   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13957      sort of BFD interface for this.  */
13958   if (mips_any_noreorder)
13959     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13960   if (mips_pic != NO_PIC)
13961     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13962
13963   /* Set MIPS ELF flags for ASEs.  */
13964   if (file_ase_mips16)
13965     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13966 #if 0 /* XXX FIXME */
13967   if (file_ase_mips3d)
13968     elf_elfheader (stdoutput)->e_flags |= ???;
13969 #endif
13970   if (file_ase_mdmx)
13971     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13972
13973   /* Set the MIPS ELF ABI flags.  */
13974   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13975     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13976   else if (mips_abi == O64_ABI)
13977     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13978   else if (mips_abi == EABI_ABI)
13979     {
13980       if (!file_mips_gp32)
13981         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13982       else
13983         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13984     }
13985   else if (mips_abi == N32_ABI)
13986     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13987
13988   /* Nothing to do for N64_ABI.  */
13989
13990   if (mips_32bitmode)
13991     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13992 }
13993
13994 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13995 \f
13996 typedef struct proc {
13997   symbolS *isym;
13998   unsigned long reg_mask;
13999   unsigned long reg_offset;
14000   unsigned long fpreg_mask;
14001   unsigned long fpreg_offset;
14002   unsigned long frame_offset;
14003   unsigned long frame_reg;
14004   unsigned long pc_reg;
14005 } procS;
14006
14007 static procS cur_proc;
14008 static procS *cur_proc_ptr;
14009 static int numprocs;
14010
14011 /* Fill in an rs_align_code fragment.  */
14012
14013 void
14014 mips_handle_align (fragp)
14015      fragS *fragp;
14016 {
14017   if (fragp->fr_type != rs_align_code)
14018     return;
14019
14020   if (mips_opts.mips16)
14021     {
14022       static const unsigned char be_nop[] = { 0x65, 0x00 };
14023       static const unsigned char le_nop[] = { 0x00, 0x65 };
14024
14025       int bytes;
14026       char *p;
14027
14028       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14029       p = fragp->fr_literal + fragp->fr_fix;
14030
14031       if (bytes & 1)
14032         {
14033           *p++ = 0;
14034           fragp->fr_fix++;
14035         }
14036
14037       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14038       fragp->fr_var = 2;
14039     }
14040
14041   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14042 }
14043
14044 static void
14045 md_obj_begin ()
14046 {
14047 }
14048
14049 static void
14050 md_obj_end ()
14051 {
14052   /* check for premature end, nesting errors, etc */
14053   if (cur_proc_ptr)
14054     as_warn (_("missing .end at end of assembly"));
14055 }
14056
14057 static long
14058 get_number ()
14059 {
14060   int negative = 0;
14061   long val = 0;
14062
14063   if (*input_line_pointer == '-')
14064     {
14065       ++input_line_pointer;
14066       negative = 1;
14067     }
14068   if (!ISDIGIT (*input_line_pointer))
14069     as_bad (_("expected simple number"));
14070   if (input_line_pointer[0] == '0')
14071     {
14072       if (input_line_pointer[1] == 'x')
14073         {
14074           input_line_pointer += 2;
14075           while (ISXDIGIT (*input_line_pointer))
14076             {
14077               val <<= 4;
14078               val |= hex_value (*input_line_pointer++);
14079             }
14080           return negative ? -val : val;
14081         }
14082       else
14083         {
14084           ++input_line_pointer;
14085           while (ISDIGIT (*input_line_pointer))
14086             {
14087               val <<= 3;
14088               val |= *input_line_pointer++ - '0';
14089             }
14090           return negative ? -val : val;
14091         }
14092     }
14093   if (!ISDIGIT (*input_line_pointer))
14094     {
14095       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14096               *input_line_pointer, *input_line_pointer);
14097       as_warn (_("invalid number"));
14098       return -1;
14099     }
14100   while (ISDIGIT (*input_line_pointer))
14101     {
14102       val *= 10;
14103       val += *input_line_pointer++ - '0';
14104     }
14105   return negative ? -val : val;
14106 }
14107
14108 /* The .file directive; just like the usual .file directive, but there
14109    is an initial number which is the ECOFF file index.  In the non-ECOFF
14110    case .file implies DWARF-2.  */
14111
14112 static void
14113 s_mips_file (x)
14114      int x ATTRIBUTE_UNUSED;
14115 {
14116   static int first_file_directive = 0;
14117
14118   if (ECOFF_DEBUGGING)
14119     {
14120       get_number ();
14121       s_app_file (0);
14122     }
14123   else
14124     {
14125       char *filename;
14126
14127       filename = dwarf2_directive_file (0);
14128
14129       /* Versions of GCC up to 3.1 start files with a ".file"
14130          directive even for stabs output.  Make sure that this
14131          ".file" is handled.  Note that you need a version of GCC
14132          after 3.1 in order to support DWARF-2 on MIPS.  */
14133       if (filename != NULL && ! first_file_directive)
14134         {
14135           (void) new_logical_line (filename, -1);
14136           s_app_file_string (filename);
14137         }
14138       first_file_directive = 1;
14139     }
14140 }
14141
14142 /* The .loc directive, implying DWARF-2.  */
14143
14144 static void
14145 s_mips_loc (x)
14146      int x ATTRIBUTE_UNUSED;
14147 {
14148   if (!ECOFF_DEBUGGING)
14149     dwarf2_directive_loc (0);
14150 }
14151
14152 /* The .end directive.  */
14153
14154 static void
14155 s_mips_end (x)
14156      int x ATTRIBUTE_UNUSED;
14157 {
14158   symbolS *p;
14159
14160   /* Following functions need their own .frame and .cprestore directives.  */
14161   mips_frame_reg_valid = 0;
14162   mips_cprestore_valid = 0;
14163
14164   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14165     {
14166       p = get_symbol ();
14167       demand_empty_rest_of_line ();
14168     }
14169   else
14170     p = NULL;
14171
14172   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14173     as_warn (_(".end not in text section"));
14174
14175   if (!cur_proc_ptr)
14176     {
14177       as_warn (_(".end directive without a preceding .ent directive."));
14178       demand_empty_rest_of_line ();
14179       return;
14180     }
14181
14182   if (p != NULL)
14183     {
14184       assert (S_GET_NAME (p));
14185       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14186         as_warn (_(".end symbol does not match .ent symbol."));
14187
14188       if (debug_type == DEBUG_STABS)
14189         stabs_generate_asm_endfunc (S_GET_NAME (p),
14190                                     S_GET_NAME (p));
14191     }
14192   else
14193     as_warn (_(".end directive missing or unknown symbol"));
14194
14195 #ifdef OBJ_ELF
14196   /* Generate a .pdr section.  */
14197   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14198     {
14199       segT saved_seg = now_seg;
14200       subsegT saved_subseg = now_subseg;
14201       valueT dot;
14202       expressionS exp;
14203       char *fragp;
14204
14205       dot = frag_now_fix ();
14206
14207 #ifdef md_flush_pending_output
14208       md_flush_pending_output ();
14209 #endif
14210
14211       assert (pdr_seg);
14212       subseg_set (pdr_seg, 0);
14213
14214       /* Write the symbol.  */
14215       exp.X_op = O_symbol;
14216       exp.X_add_symbol = p;
14217       exp.X_add_number = 0;
14218       emit_expr (&exp, 4);
14219
14220       fragp = frag_more (7 * 4);
14221
14222       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14223       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14224       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14225       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14226       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14227       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14228       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14229
14230       subseg_set (saved_seg, saved_subseg);
14231     }
14232 #endif /* OBJ_ELF */
14233
14234   cur_proc_ptr = NULL;
14235 }
14236
14237 /* The .aent and .ent directives.  */
14238
14239 static void
14240 s_mips_ent (aent)
14241      int aent;
14242 {
14243   symbolS *symbolP;
14244
14245   symbolP = get_symbol ();
14246   if (*input_line_pointer == ',')
14247     ++input_line_pointer;
14248   SKIP_WHITESPACE ();
14249   if (ISDIGIT (*input_line_pointer)
14250       || *input_line_pointer == '-')
14251     get_number ();
14252
14253   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14254     as_warn (_(".ent or .aent not in text section."));
14255
14256   if (!aent && cur_proc_ptr)
14257     as_warn (_("missing .end"));
14258
14259   if (!aent)
14260     {
14261       /* This function needs its own .frame and .cprestore directives.  */
14262       mips_frame_reg_valid = 0;
14263       mips_cprestore_valid = 0;
14264
14265       cur_proc_ptr = &cur_proc;
14266       memset (cur_proc_ptr, '\0', sizeof (procS));
14267
14268       cur_proc_ptr->isym = symbolP;
14269
14270       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14271
14272       ++numprocs;
14273
14274       if (debug_type == DEBUG_STABS)
14275         stabs_generate_asm_func (S_GET_NAME (symbolP),
14276                                  S_GET_NAME (symbolP));
14277     }
14278
14279   demand_empty_rest_of_line ();
14280 }
14281
14282 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14283    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14284    s_mips_frame is used so that we can set the PDR information correctly.
14285    We can't use the ecoff routines because they make reference to the ecoff
14286    symbol table (in the mdebug section).  */
14287
14288 static void
14289 s_mips_frame (ignore)
14290      int ignore ATTRIBUTE_UNUSED;
14291 {
14292 #ifdef OBJ_ELF
14293   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14294     {
14295       long val;
14296
14297       if (cur_proc_ptr == (procS *) NULL)
14298         {
14299           as_warn (_(".frame outside of .ent"));
14300           demand_empty_rest_of_line ();
14301           return;
14302         }
14303
14304       cur_proc_ptr->frame_reg = tc_get_register (1);
14305
14306       SKIP_WHITESPACE ();
14307       if (*input_line_pointer++ != ','
14308           || get_absolute_expression_and_terminator (&val) != ',')
14309         {
14310           as_warn (_("Bad .frame directive"));
14311           --input_line_pointer;
14312           demand_empty_rest_of_line ();
14313           return;
14314         }
14315
14316       cur_proc_ptr->frame_offset = val;
14317       cur_proc_ptr->pc_reg = tc_get_register (0);
14318
14319       demand_empty_rest_of_line ();
14320     }
14321   else
14322 #endif /* OBJ_ELF */
14323     s_ignore (ignore);
14324 }
14325
14326 /* The .fmask and .mask directives. If the mdebug section is present
14327    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14328    embedded targets, s_mips_mask is used so that we can set the PDR
14329    information correctly. We can't use the ecoff routines because they
14330    make reference to the ecoff symbol table (in the mdebug section).  */
14331
14332 static void
14333 s_mips_mask (reg_type)
14334      char reg_type;
14335 {
14336 #ifdef OBJ_ELF
14337   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14338     {
14339       long mask, off;
14340
14341       if (cur_proc_ptr == (procS *) NULL)
14342         {
14343           as_warn (_(".mask/.fmask outside of .ent"));
14344           demand_empty_rest_of_line ();
14345           return;
14346         }
14347
14348       if (get_absolute_expression_and_terminator (&mask) != ',')
14349         {
14350           as_warn (_("Bad .mask/.fmask directive"));
14351           --input_line_pointer;
14352           demand_empty_rest_of_line ();
14353           return;
14354         }
14355
14356       off = get_absolute_expression ();
14357
14358       if (reg_type == 'F')
14359         {
14360           cur_proc_ptr->fpreg_mask = mask;
14361           cur_proc_ptr->fpreg_offset = off;
14362         }
14363       else
14364         {
14365           cur_proc_ptr->reg_mask = mask;
14366           cur_proc_ptr->reg_offset = off;
14367         }
14368
14369       demand_empty_rest_of_line ();
14370     }
14371   else
14372 #endif /* OBJ_ELF */
14373     s_ignore (reg_type);
14374 }
14375
14376 /* The .loc directive.  */
14377
14378 #if 0
14379 static void
14380 s_loc (x)
14381      int x;
14382 {
14383   symbolS *symbolP;
14384   int lineno;
14385   int addroff;
14386
14387   assert (now_seg == text_section);
14388
14389   lineno = get_number ();
14390   addroff = frag_now_fix ();
14391
14392   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14393   S_SET_TYPE (symbolP, N_SLINE);
14394   S_SET_OTHER (symbolP, 0);
14395   S_SET_DESC (symbolP, lineno);
14396   symbolP->sy_segment = now_seg;
14397 }
14398 #endif
14399
14400 /* A table describing all the processors gas knows about.  Names are
14401    matched in the order listed.
14402
14403    To ease comparison, please keep this table in the same order as
14404    gcc's mips_cpu_info_table[].  */
14405 static const struct mips_cpu_info mips_cpu_info_table[] =
14406 {
14407   /* Entries for generic ISAs */
14408   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14409   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14410   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14411   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14412   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14413   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14414   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14415   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14416
14417   /* MIPS I */
14418   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14419   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14420   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14421
14422   /* MIPS II */
14423   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14424
14425   /* MIPS III */
14426   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14427   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14428   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14429   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14430   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14431   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14432   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14433   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14434   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14435   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14436   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14437   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14438
14439   /* MIPS IV */
14440   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14441   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14442   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14443   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14444   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14445   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14446   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14447   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14448   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14449   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14450   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14451   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14452
14453   /* MIPS 32 */
14454   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14455   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14456   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14457
14458   /* MIPS 64 */
14459   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14460   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14461
14462   /* Broadcom SB-1 CPU core */
14463   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14464
14465   /* End marker */
14466   { NULL, 0, 0, 0 }
14467 };
14468
14469
14470 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14471    with a final "000" replaced by "k".  Ignore case.
14472
14473    Note: this function is shared between GCC and GAS.  */
14474
14475 static bfd_boolean
14476 mips_strict_matching_cpu_name_p (canonical, given)
14477      const char *canonical, *given;
14478 {
14479   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14480     given++, canonical++;
14481
14482   return ((*given == 0 && *canonical == 0)
14483           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14484 }
14485
14486
14487 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14488    CPU name.  We've traditionally allowed a lot of variation here.
14489
14490    Note: this function is shared between GCC and GAS.  */
14491
14492 static bfd_boolean
14493 mips_matching_cpu_name_p (canonical, given)
14494      const char *canonical, *given;
14495 {
14496   /* First see if the name matches exactly, or with a final "000"
14497      turned into "k".  */
14498   if (mips_strict_matching_cpu_name_p (canonical, given))
14499     return TRUE;
14500
14501   /* If not, try comparing based on numerical designation alone.
14502      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14503   if (TOLOWER (*given) == 'r')
14504     given++;
14505   if (!ISDIGIT (*given))
14506     return FALSE;
14507
14508   /* Skip over some well-known prefixes in the canonical name,
14509      hoping to find a number there too.  */
14510   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14511     canonical += 2;
14512   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14513     canonical += 2;
14514   else if (TOLOWER (canonical[0]) == 'r')
14515     canonical += 1;
14516
14517   return mips_strict_matching_cpu_name_p (canonical, given);
14518 }
14519
14520
14521 /* Parse an option that takes the name of a processor as its argument.
14522    OPTION is the name of the option and CPU_STRING is the argument.
14523    Return the corresponding processor enumeration if the CPU_STRING is
14524    recognized, otherwise report an error and return null.
14525
14526    A similar function exists in GCC.  */
14527
14528 static const struct mips_cpu_info *
14529 mips_parse_cpu (option, cpu_string)
14530      const char *option, *cpu_string;
14531 {
14532   const struct mips_cpu_info *p;
14533
14534   /* 'from-abi' selects the most compatible architecture for the given
14535      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14536      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14537      version.  Look first at the -mgp options, if given, otherwise base
14538      the choice on MIPS_DEFAULT_64BIT.
14539
14540      Treat NO_ABI like the EABIs.  One reason to do this is that the
14541      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14542      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14543      'mips64', just as we did in the days before 'from-abi'.  */
14544   if (strcasecmp (cpu_string, "from-abi") == 0)
14545     {
14546       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14547         return mips_cpu_info_from_isa (ISA_MIPS1);
14548
14549       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14550         return mips_cpu_info_from_isa (ISA_MIPS3);
14551
14552       if (file_mips_gp32 >= 0)
14553         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14554
14555       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14556                                      ? ISA_MIPS3
14557                                      : ISA_MIPS1);
14558     }
14559
14560   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14561   if (strcasecmp (cpu_string, "default") == 0)
14562     return 0;
14563
14564   for (p = mips_cpu_info_table; p->name != 0; p++)
14565     if (mips_matching_cpu_name_p (p->name, cpu_string))
14566       return p;
14567
14568   as_bad ("Bad value (%s) for %s", cpu_string, option);
14569   return 0;
14570 }
14571
14572 /* Return the canonical processor information for ISA (a member of the
14573    ISA_MIPS* enumeration).  */
14574
14575 static const struct mips_cpu_info *
14576 mips_cpu_info_from_isa (isa)
14577      int isa;
14578 {
14579   int i;
14580
14581   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14582     if (mips_cpu_info_table[i].is_isa
14583         && isa == mips_cpu_info_table[i].isa)
14584       return (&mips_cpu_info_table[i]);
14585
14586   return NULL;
14587 }
14588 \f
14589 static void
14590 show (stream, string, col_p, first_p)
14591      FILE *stream;
14592      const char *string;
14593      int *col_p;
14594      int *first_p;
14595 {
14596   if (*first_p)
14597     {
14598       fprintf (stream, "%24s", "");
14599       *col_p = 24;
14600     }
14601   else
14602     {
14603       fprintf (stream, ", ");
14604       *col_p += 2;
14605     }
14606
14607   if (*col_p + strlen (string) > 72)
14608     {
14609       fprintf (stream, "\n%24s", "");
14610       *col_p = 24;
14611     }
14612
14613   fprintf (stream, "%s", string);
14614   *col_p += strlen (string);
14615
14616   *first_p = 0;
14617 }
14618
14619 void
14620 md_show_usage (stream)
14621      FILE *stream;
14622 {
14623   int column, first;
14624   size_t i;
14625
14626   fprintf (stream, _("\
14627 MIPS options:\n\
14628 -membedded-pic          generate embedded position independent code\n\
14629 -EB                     generate big endian output\n\
14630 -EL                     generate little endian output\n\
14631 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14632 -G NUM                  allow referencing objects up to NUM bytes\n\
14633                         implicitly with the gp register [default 8]\n"));
14634   fprintf (stream, _("\
14635 -mips1                  generate MIPS ISA I instructions\n\
14636 -mips2                  generate MIPS ISA II instructions\n\
14637 -mips3                  generate MIPS ISA III instructions\n\
14638 -mips4                  generate MIPS ISA IV instructions\n\
14639 -mips5                  generate MIPS ISA V instructions\n\
14640 -mips32                 generate MIPS32 ISA instructions\n\
14641 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
14642 -mips64                 generate MIPS64 ISA instructions\n\
14643 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14644
14645   first = 1;
14646
14647   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14648     show (stream, mips_cpu_info_table[i].name, &column, &first);
14649   show (stream, "from-abi", &column, &first);
14650   fputc ('\n', stream);
14651
14652   fprintf (stream, _("\
14653 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14654 -no-mCPU                don't generate code specific to CPU.\n\
14655                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14656
14657   first = 1;
14658
14659   show (stream, "3900", &column, &first);
14660   show (stream, "4010", &column, &first);
14661   show (stream, "4100", &column, &first);
14662   show (stream, "4650", &column, &first);
14663   fputc ('\n', stream);
14664
14665   fprintf (stream, _("\
14666 -mips16                 generate mips16 instructions\n\
14667 -no-mips16              do not generate mips16 instructions\n"));
14668   fprintf (stream, _("\
14669 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14670 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14671 -O0                     remove unneeded NOPs, do not swap branches\n\
14672 -O                      remove unneeded NOPs and swap branches\n\
14673 -n                      warn about NOPs generated from macros\n\
14674 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14675 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14676 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14677 #ifdef OBJ_ELF
14678   fprintf (stream, _("\
14679 -KPIC, -call_shared     generate SVR4 position independent code\n\
14680 -non_shared             do not generate position independent code\n\
14681 -xgot                   assume a 32 bit GOT\n\
14682 -mabi=ABI               create ABI conformant object file for:\n"));
14683
14684   first = 1;
14685
14686   show (stream, "32", &column, &first);
14687   show (stream, "o64", &column, &first);
14688   show (stream, "n32", &column, &first);
14689   show (stream, "64", &column, &first);
14690   show (stream, "eabi", &column, &first);
14691
14692   fputc ('\n', stream);
14693
14694   fprintf (stream, _("\
14695 -32                     create o32 ABI object file (default)\n\
14696 -n32                    create n32 ABI object file\n\
14697 -64                     create 64 ABI object file\n"));
14698 #endif
14699 }
14700
14701 enum dwarf2_format
14702 mips_dwarf2_format ()
14703 {
14704   if (mips_abi == N64_ABI)
14705     {
14706 #ifdef TE_IRIX
14707       return dwarf2_format_64bit_irix;
14708 #else
14709       return dwarf2_format_64bit;
14710 #endif
14711     }
14712   else
14713     return dwarf2_format_32bit;
14714 }