* config/tc-mips.h (mips_relax_frag): Take segment as argument.
[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
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too.  */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT  1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP  28
95 #define SP  29
96 #define FP  30
97 #define RA  31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format.  */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section.  */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111                             ? ".data" \
112                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113                             ? ".rdata" \
114                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115                             ? ".rdata" \
116                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117                             ? ".rodata" \
118                             : (abort (), ""))
119
120 /* The ABI to use.  */
121 enum mips_abi_level
122 {
123   NO_ABI = 0,
124   O32_ABI,
125   O64_ABI,
126   N32_ABI,
127   N64_ABI,
128   EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file.  */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* This is the set of options which may be modified by the .set
135    pseudo-op.  We use a struct so that .set push and .set pop are more
136    reliable.  */
137
138 struct mips_set_options
139 {
140   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
141      if it has not been initialized.  Changed by `.set mipsN', and the
142      -mipsN command line option, and the default CPU.  */
143   int isa;
144   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
145      if they have not been initialized.  Changed by `.set <asename>', by
146      command line options, and based on the default architecture.  */
147   int ase_mips3d;
148   int ase_mdmx;
149   /* Whether we are assembling for the mips16 processor.  0 if we are
150      not, 1 if we are, and -1 if the value has not been initialized.
151      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152      -nomips16 command line options, and the default CPU.  */
153   int mips16;
154   /* Non-zero if we should not reorder instructions.  Changed by `.set
155      reorder' and `.set noreorder'.  */
156   int noreorder;
157   /* Non-zero if we should not permit the $at ($1) register to be used
158      in instructions.  Changed by `.set at' and `.set noat'.  */
159   int noat;
160   /* Non-zero if we should warn when a macro instruction expands into
161      more than one machine instruction.  Changed by `.set nomacro' and
162      `.set macro'.  */
163   int warn_about_macros;
164   /* Non-zero if we should not move instructions.  Changed by `.set
165      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
166   int nomove;
167   /* Non-zero if we should not optimize branches by moving the target
168      of the branch into the delay slot.  Actually, we don't perform
169      this optimization anyhow.  Changed by `.set bopt' and `.set
170      nobopt'.  */
171   int nobopt;
172   /* Non-zero if we should not autoextend mips16 instructions.
173      Changed by `.set autoextend' and `.set noautoextend'.  */
174   int noautoextend;
175   /* Restrict general purpose registers and floating point registers
176      to 32 bit.  This is initially determined when -mgp32 or -mfp32
177      is passed but can changed if the assembler code uses .set mipsN.  */
178   int gp32;
179   int fp32;
180 };
181
182 /* True if -mgp32 was passed.  */
183 static int file_mips_gp32 = -1;
184
185 /* True if -mfp32 was passed.  */
186 static int file_mips_fp32 = -1;
187
188 /* This is the struct we use to hold the current set of options.  Note
189    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190    -1 to indicate that they have not been initialized.  */
191
192 static struct mips_set_options mips_opts =
193 {
194   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
195 };
196
197 /* These variables are filled in with the masks of registers used.
198    The object format code reads them and puts them in the appropriate
199    place.  */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
202
203 /* MIPS ISA we are using for this output file.  */
204 static int file_mips_isa = ISA_UNKNOWN;
205
206 /* True if -mips16 was passed or implied by arguments passed on the
207    command line (e.g., by -march).  */
208 static int file_ase_mips16;
209
210 /* True if -mips3d was passed or implied by arguments passed on the
211    command line (e.g., by -march).  */
212 static int file_ase_mips3d;
213
214 /* True if -mdmx was passed or implied by arguments passed on the
215    command line (e.g., by -march).  */
216 static int file_ase_mdmx;
217
218 /* The argument of the -march= flag.  The architecture we are assembling.  */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
222
223 /* The argument of the -mtune= flag.  The architecture for which we
224    are optimizing.  */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
228
229 /* True when generating 32-bit code for a 64-bit processor.  */
230 static int mips_32bitmode = 0;
231
232 /* Some ISA's have delay slots for instructions which read or write
233    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236    delay slot in this ISA.  The uses of this macro assume that any
237    ISA that has delay slots for one of these, has them for all.  They
238    also assume that ISAs which don't have delays for these insns, don't
239    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
240 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
241    (ISA) == ISA_MIPS1                       \
242    || (ISA) == ISA_MIPS2                    \
243    || (ISA) == ISA_MIPS3                    \
244    )
245
246 /* True if the given ABI requires 32-bit registers.  */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249 /* Likewise 64-bit registers.  */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251   ((ABI) == N32_ABI               \
252    || (ABI) == N64_ABI            \
253    || (ABI) == O64_ABI)
254
255 /*  Return true if ISA supports 64 bit gp register instructions.  */
256 #define ISA_HAS_64BIT_REGS(ISA) (    \
257    (ISA) == ISA_MIPS3                \
258    || (ISA) == ISA_MIPS4             \
259    || (ISA) == ISA_MIPS5             \
260    || (ISA) == ISA_MIPS64            \
261    )
262
263 #define HAVE_32BIT_GPRS                            \
264     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
265
266 #define HAVE_32BIT_FPRS                            \
267     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
268
269 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
271
272 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
273
274 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
275
276 /* We can only have 64bit addresses if the object file format
277    supports it.  */
278 #define HAVE_32BIT_ADDRESSES                           \
279    (HAVE_32BIT_GPRS                                    \
280     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
281          || ! HAVE_64BIT_OBJECTS)                      \
282         && mips_pic != EMBEDDED_PIC))
283
284 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
285
286 /* Return true if the given CPU supports the MIPS16 ASE.  */
287 #define CPU_HAS_MIPS16(cpu)                                             \
288    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
289     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
290
291 /* Return true if the given CPU supports the MIPS3D ASE.  */
292 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
293                                  )
294
295 /* Return true if the given CPU supports the MDMX ASE.  */
296 #define CPU_HAS_MDMX(cpu)       (false                 \
297                                  )
298
299 /* True if CPU has a dror instruction.  */
300 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
301
302 /* True if CPU has a ror instruction.  */
303 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
304
305 /* Whether the processor uses hardware interlocks to protect
306    reads from the HI and LO registers, and thus does not
307    require nops to be inserted.  */
308
309 #define hilo_interlocks (mips_arch == CPU_R4010                       \
310                          || mips_arch == CPU_VR5500                   \
311                          || mips_arch == CPU_SB1                      \
312                          )
313
314 /* Whether the processor uses hardware interlocks to protect reads
315    from the GPRs, and thus does not require nops to be inserted.  */
316 #define gpr_interlocks \
317   (mips_opts.isa != ISA_MIPS1  \
318    || mips_arch == CPU_VR5400  \
319    || mips_arch == CPU_VR5500  \
320    || mips_arch == CPU_R3900)
321
322 /* As with other "interlocks" this is used by hardware that has FP
323    (co-processor) interlocks.  */
324 /* Itbl support may require additional care here.  */
325 #define cop_interlocks (mips_arch == CPU_R4300                        \
326                         || mips_arch == CPU_VR5400                    \
327                         || mips_arch == CPU_VR5500                    \
328                         || mips_arch == CPU_SB1                       \
329                         )
330
331 /* Is this a mfhi or mflo instruction?  */
332 #define MF_HILO_INSN(PINFO) \
333           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
334
335 /* MIPS PIC level.  */
336
337 enum mips_pic_level mips_pic;
338
339 /* Warn about all NOPS that the assembler generates.  */
340 static int warn_nops = 0;
341
342 /* 1 if we should generate 32 bit offsets from the $gp register in
343    SVR4_PIC mode.  Currently has no meaning in other modes.  */
344 static int mips_big_got = 0;
345
346 /* 1 if trap instructions should used for overflow rather than break
347    instructions.  */
348 static int mips_trap = 0;
349
350 /* 1 if double width floating point constants should not be constructed
351    by assembling two single width halves into two single width floating
352    point registers which just happen to alias the double width destination
353    register.  On some architectures this aliasing can be disabled by a bit
354    in the status register, and the setting of this bit cannot be determined
355    automatically at assemble time.  */
356 static int mips_disable_float_construction;
357
358 /* Non-zero if any .set noreorder directives were used.  */
359
360 static int mips_any_noreorder;
361
362 /* Non-zero if nops should be inserted when the register referenced in
363    an mfhi/mflo instruction is read in the next two instructions.  */
364 static int mips_7000_hilo_fix;
365
366 /* The size of the small data section.  */
367 static unsigned int g_switch_value = 8;
368 /* Whether the -G option was used.  */
369 static int g_switch_seen = 0;
370
371 #define N_RMASK 0xc4
372 #define N_VFP   0xd4
373
374 /* If we can determine in advance that GP optimization won't be
375    possible, we can skip the relaxation stuff that tries to produce
376    GP-relative references.  This makes delay slot optimization work
377    better.
378
379    This function can only provide a guess, but it seems to work for
380    gcc output.  It needs to guess right for gcc, otherwise gcc
381    will put what it thinks is a GP-relative instruction in a branch
382    delay slot.
383
384    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
385    fixed it for the non-PIC mode.  KR 95/04/07  */
386 static int nopic_need_relax PARAMS ((symbolS *, int));
387
388 /* handle of the OPCODE hash table */
389 static struct hash_control *op_hash = NULL;
390
391 /* The opcode hash table we use for the mips16.  */
392 static struct hash_control *mips16_op_hash = NULL;
393
394 /* This array holds the chars that always start a comment.  If the
395     pre-processor is disabled, these aren't very useful */
396 const char comment_chars[] = "#";
397
398 /* This array holds the chars that only start a comment at the beginning of
399    a line.  If the line seems to have the form '# 123 filename'
400    .line and .file directives will appear in the pre-processed output */
401 /* Note that input_file.c hand checks for '#' at the beginning of the
402    first line of the input file.  This is because the compiler outputs
403    #NO_APP at the beginning of its output.  */
404 /* Also note that C style comments are always supported.  */
405 const char line_comment_chars[] = "#";
406
407 /* This array holds machine specific line separator characters.  */
408 const char line_separator_chars[] = ";";
409
410 /* Chars that can be used to separate mant from exp in floating point nums */
411 const char EXP_CHARS[] = "eE";
412
413 /* Chars that mean this number is a floating point constant */
414 /* As in 0f12.456 */
415 /* or    0d1.2345e12 */
416 const char FLT_CHARS[] = "rRsSfFdDxXpP";
417
418 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
419    changed in read.c .  Ideally it shouldn't have to know about it at all,
420    but nothing is ideal around here.
421  */
422
423 static char *insn_error;
424
425 static int auto_align = 1;
426
427 /* When outputting SVR4 PIC code, the assembler needs to know the
428    offset in the stack frame from which to restore the $gp register.
429    This is set by the .cprestore pseudo-op, and saved in this
430    variable.  */
431 static offsetT mips_cprestore_offset = -1;
432
433 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
434    more optimizations, it can use a register value instead of a memory-saved
435    offset and even an other register than $gp as global pointer.  */
436 static offsetT mips_cpreturn_offset = -1;
437 static int mips_cpreturn_register = -1;
438 static int mips_gp_register = GP;
439 static int mips_gprel_offset = 0;
440
441 /* Whether mips_cprestore_offset has been set in the current function
442    (or whether it has already been warned about, if not).  */
443 static int mips_cprestore_valid = 0;
444
445 /* This is the register which holds the stack frame, as set by the
446    .frame pseudo-op.  This is needed to implement .cprestore.  */
447 static int mips_frame_reg = SP;
448
449 /* Whether mips_frame_reg has been set in the current function
450    (or whether it has already been warned about, if not).  */
451 static int mips_frame_reg_valid = 0;
452
453 /* To output NOP instructions correctly, we need to keep information
454    about the previous two instructions.  */
455
456 /* Whether we are optimizing.  The default value of 2 means to remove
457    unneeded NOPs and swap branch instructions when possible.  A value
458    of 1 means to not swap branches.  A value of 0 means to always
459    insert NOPs.  */
460 static int mips_optimize = 2;
461
462 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
463    equivalent to seeing no -g option at all.  */
464 static int mips_debug = 0;
465
466 /* The previous instruction.  */
467 static struct mips_cl_insn prev_insn;
468
469 /* The instruction before prev_insn.  */
470 static struct mips_cl_insn prev_prev_insn;
471
472 /* If we don't want information for prev_insn or prev_prev_insn, we
473    point the insn_mo field at this dummy integer.  */
474 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
475
476 /* Non-zero if prev_insn is valid.  */
477 static int prev_insn_valid;
478
479 /* The frag for the previous instruction.  */
480 static struct frag *prev_insn_frag;
481
482 /* The offset into prev_insn_frag for the previous instruction.  */
483 static long prev_insn_where;
484
485 /* The reloc type for the previous instruction, if any.  */
486 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
487
488 /* The reloc for the previous instruction, if any.  */
489 static fixS *prev_insn_fixp[3];
490
491 /* Non-zero if the previous instruction was in a delay slot.  */
492 static int prev_insn_is_delay_slot;
493
494 /* Non-zero if the previous instruction was in a .set noreorder.  */
495 static int prev_insn_unreordered;
496
497 /* Non-zero if the previous instruction uses an extend opcode (if
498    mips16).  */
499 static int prev_insn_extended;
500
501 /* Non-zero if the previous previous instruction was in a .set
502    noreorder.  */
503 static int prev_prev_insn_unreordered;
504
505 /* If this is set, it points to a frag holding nop instructions which
506    were inserted before the start of a noreorder section.  If those
507    nops turn out to be unnecessary, the size of the frag can be
508    decreased.  */
509 static fragS *prev_nop_frag;
510
511 /* The number of nop instructions we created in prev_nop_frag.  */
512 static int prev_nop_frag_holds;
513
514 /* The number of nop instructions that we know we need in
515    prev_nop_frag.  */
516 static int prev_nop_frag_required;
517
518 /* The number of instructions we've seen since prev_nop_frag.  */
519 static int prev_nop_frag_since;
520
521 /* For ECOFF and ELF, relocations against symbols are done in two
522    parts, with a HI relocation and a LO relocation.  Each relocation
523    has only 16 bits of space to store an addend.  This means that in
524    order for the linker to handle carries correctly, it must be able
525    to locate both the HI and the LO relocation.  This means that the
526    relocations must appear in order in the relocation table.
527
528    In order to implement this, we keep track of each unmatched HI
529    relocation.  We then sort them so that they immediately precede the
530    corresponding LO relocation.  */
531
532 struct mips_hi_fixup
533 {
534   /* Next HI fixup.  */
535   struct mips_hi_fixup *next;
536   /* This fixup.  */
537   fixS *fixp;
538   /* The section this fixup is in.  */
539   segT seg;
540 };
541
542 /* The list of unmatched HI relocs.  */
543
544 static struct mips_hi_fixup *mips_hi_fixup_list;
545
546 /* Map normal MIPS register numbers to mips16 register numbers.  */
547
548 #define X ILLEGAL_REG
549 static const int mips32_to_16_reg_map[] =
550 {
551   X, X, 2, 3, 4, 5, 6, 7,
552   X, X, X, X, X, X, X, X,
553   0, 1, X, X, X, X, X, X,
554   X, X, X, X, X, X, X, X
555 };
556 #undef X
557
558 /* Map mips16 register numbers to normal MIPS register numbers.  */
559
560 static const unsigned int mips16_to_32_reg_map[] =
561 {
562   16, 17, 2, 3, 4, 5, 6, 7
563 };
564
565 static int mips_fix_4122_bugs;
566
567 /* We don't relax branches by default, since this causes us to expand
568    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
569    fail to compute the offset before expanding the macro to the most
570    efficient expansion.  */
571
572 static int mips_relax_branch;
573 \f
574 /* Since the MIPS does not have multiple forms of PC relative
575    instructions, we do not have to do relaxing as is done on other
576    platforms.  However, we do have to handle GP relative addressing
577    correctly, which turns out to be a similar problem.
578
579    Every macro that refers to a symbol can occur in (at least) two
580    forms, one with GP relative addressing and one without.  For
581    example, loading a global variable into a register generally uses
582    a macro instruction like this:
583      lw $4,i
584    If i can be addressed off the GP register (this is true if it is in
585    the .sbss or .sdata section, or if it is known to be smaller than
586    the -G argument) this will generate the following instruction:
587      lw $4,i($gp)
588    This instruction will use a GPREL reloc.  If i can not be addressed
589    off the GP register, the following instruction sequence will be used:
590      lui $at,i
591      lw $4,i($at)
592    In this case the first instruction will have a HI16 reloc, and the
593    second reloc will have a LO16 reloc.  Both relocs will be against
594    the symbol i.
595
596    The issue here is that we may not know whether i is GP addressable
597    until after we see the instruction that uses it.  Therefore, we
598    want to be able to choose the final instruction sequence only at
599    the end of the assembly.  This is similar to the way other
600    platforms choose the size of a PC relative instruction only at the
601    end of assembly.
602
603    When generating position independent code we do not use GP
604    addressing in quite the same way, but the issue still arises as
605    external symbols and local symbols must be handled differently.
606
607    We handle these issues by actually generating both possible
608    instruction sequences.  The longer one is put in a frag_var with
609    type rs_machine_dependent.  We encode what to do with the frag in
610    the subtype field.  We encode (1) the number of existing bytes to
611    replace, (2) the number of new bytes to use, (3) the offset from
612    the start of the existing bytes to the first reloc we must generate
613    (that is, the offset is applied from the start of the existing
614    bytes after they are replaced by the new bytes, if any), (4) the
615    offset from the start of the existing bytes to the second reloc,
616    (5) whether a third reloc is needed (the third reloc is always four
617    bytes after the second reloc), and (6) whether to warn if this
618    variant is used (this is sometimes needed if .set nomacro or .set
619    noat is in effect).  All these numbers are reasonably small.
620
621    Generating two instruction sequences must be handled carefully to
622    ensure that delay slots are handled correctly.  Fortunately, there
623    are a limited number of cases.  When the second instruction
624    sequence is generated, append_insn is directed to maintain the
625    existing delay slot information, so it continues to apply to any
626    code after the second instruction sequence.  This means that the
627    second instruction sequence must not impose any requirements not
628    required by the first instruction sequence.
629
630    These variant frags are then handled in functions called by the
631    machine independent code.  md_estimate_size_before_relax returns
632    the final size of the frag.  md_convert_frag sets up the final form
633    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
634    one if needed.  */
635 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
636   ((relax_substateT) \
637    (((old) << 23) \
638     | ((new) << 16) \
639     | (((reloc1) + 64) << 9) \
640     | (((reloc2) + 64) << 2) \
641     | ((reloc3) ? (1 << 1) : 0) \
642     | ((warn) ? 1 : 0)))
643 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
644 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
645 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
646 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
647 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
648 #define RELAX_WARN(i) ((i) & 1)
649
650 /* Branch without likely bit.  If label is out of range, we turn:
651
652         beq reg1, reg2, label
653         delay slot
654
655    into
656
657         bne reg1, reg2, 0f
658         nop
659         j label
660      0: delay slot
661
662    with the following opcode replacements:
663
664         beq <-> bne
665         blez <-> bgtz
666         bltz <-> bgez
667         bc1f <-> bc1t
668
669         bltzal <-> bgezal  (with jal label instead of j label)
670
671    Even though keeping the delay slot instruction in the delay slot of
672    the branch would be more efficient, it would be very tricky to do
673    correctly, because we'd have to introduce a variable frag *after*
674    the delay slot instruction, and expand that instead.  Let's do it
675    the easy way for now, even if the branch-not-taken case now costs
676    one additional instruction.  Out-of-range branches are not supposed
677    to be common, anyway.
678
679    Branch likely.  If label is out of range, we turn:
680
681         beql reg1, reg2, label
682         delay slot (annulled if branch not taken)
683
684    into
685
686         beql reg1, reg2, 1f
687         nop
688         beql $0, $0, 2f
689         nop
690      1: j[al] label
691         delay slot (executed only if branch taken)
692      2:
693
694    It would be possible to generate a shorter sequence by losing the
695    likely bit, generating something like:
696      
697         bne reg1, reg2, 0f
698         nop
699         j[al] label
700         delay slot (executed only if branch taken)
701      0:
702
703         beql -> bne
704         bnel -> beq
705         blezl -> bgtz
706         bgtzl -> blez
707         bltzl -> bgez
708         bgezl -> bltz
709         bc1fl -> bc1t
710         bc1tl -> bc1f
711
712         bltzall -> bgezal  (with jal label instead of j label)
713         bgezall -> bltzal  (ditto)
714
715
716    but it's not clear that it would actually improve performance.  */
717 #define RELAX_BRANCH_ENCODE(reloc_s2, uncond, likely, link, toofar) \
718   ((relax_substateT) \
719    (0xc0000000 \
720     | ((toofar) ? 1 : 0) \
721     | ((link) ? 2 : 0) \
722     | ((likely) ? 4 : 0) \
723     | ((uncond) ? 8 : 0) \
724     | ((reloc_s2) ? 16 : 0)))
725 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
726 #define RELAX_BRANCH_RELOC_S2(i) (((i) & 16) != 0)
727 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
728 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
729 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
730 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1))
731
732 /* For mips16 code, we use an entirely different form of relaxation.
733    mips16 supports two versions of most instructions which take
734    immediate values: a small one which takes some small value, and a
735    larger one which takes a 16 bit value.  Since branches also follow
736    this pattern, relaxing these values is required.
737
738    We can assemble both mips16 and normal MIPS code in a single
739    object.  Therefore, we need to support this type of relaxation at
740    the same time that we support the relaxation described above.  We
741    use the high bit of the subtype field to distinguish these cases.
742
743    The information we store for this type of relaxation is the
744    argument code found in the opcode file for this relocation, whether
745    the user explicitly requested a small or extended form, and whether
746    the relocation is in a jump or jal delay slot.  That tells us the
747    size of the value, and how it should be stored.  We also store
748    whether the fragment is considered to be extended or not.  We also
749    store whether this is known to be a branch to a different section,
750    whether we have tried to relax this frag yet, and whether we have
751    ever extended a PC relative fragment because of a shift count.  */
752 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
753   (0x80000000                                                   \
754    | ((type) & 0xff)                                            \
755    | ((small) ? 0x100 : 0)                                      \
756    | ((ext) ? 0x200 : 0)                                        \
757    | ((dslot) ? 0x400 : 0)                                      \
758    | ((jal_dslot) ? 0x800 : 0))
759 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
760 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
761 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
762 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
763 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
764 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
765 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
766 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
767 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
768 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
769 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
770 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
771
772 /* Is the given value a sign-extended 32-bit value?  */
773 #define IS_SEXT_32BIT_NUM(x)                                            \
774   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
775    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
776
777 /* Is the given value a sign-extended 16-bit value?  */
778 #define IS_SEXT_16BIT_NUM(x)                                            \
779   (((x) &~ (offsetT) 0x7fff) == 0                                       \
780    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
781
782 \f
783 /* Prototypes for static functions.  */
784
785 #ifdef __STDC__
786 #define internalError() \
787     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
788 #else
789 #define internalError() as_fatal (_("MIPS internal Error"));
790 #endif
791
792 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
793
794 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
795                                   unsigned int reg, enum mips_regclass class));
796 static int reg_needs_delay PARAMS ((unsigned int));
797 static void mips16_mark_labels PARAMS ((void));
798 static void append_insn PARAMS ((char *place,
799                                  struct mips_cl_insn * ip,
800                                  expressionS * p,
801                                  bfd_reloc_code_real_type *r,
802                                  boolean));
803 static void mips_no_prev_insn PARAMS ((int));
804 static void mips_emit_delays PARAMS ((boolean));
805 #ifdef USE_STDARG
806 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
807                                  const char *name, const char *fmt,
808                                  ...));
809 #else
810 static void macro_build ();
811 #endif
812 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
813                                         const char *, const char *,
814                                         va_list));
815 static void macro_build_jalr PARAMS ((int, expressionS *));
816 static void macro_build_lui PARAMS ((char *place, int *counter,
817                                      expressionS * ep, int regnum));
818 static void macro_build_ldst_constoffset PARAMS ((char *place, int *counter,
819                                                   expressionS * ep, const char *op,
820                                                   int valreg, int breg));
821 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
822 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
823                                          expressionS *));
824 static void load_register PARAMS ((int *, int, expressionS *, int));
825 static void load_address PARAMS ((int *, int, expressionS *, int *));
826 static void move_register PARAMS ((int *, int, int));
827 static void macro PARAMS ((struct mips_cl_insn * ip));
828 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
829 #ifdef LOSING_COMPILER
830 static void macro2 PARAMS ((struct mips_cl_insn * ip));
831 #endif
832 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
833 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
834 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
835                                   boolean, boolean, unsigned long *,
836                                   boolean *, unsigned short *));
837 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
838 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
839 static int my_getSmallExpression PARAMS ((expressionS *, char *));
840 static void my_getExpression PARAMS ((expressionS *, char *));
841 #ifdef OBJ_ELF
842 static int support_64bit_objects PARAMS((void));
843 #endif
844 static void mips_set_option_string PARAMS ((const char **, const char *));
845 static symbolS *get_symbol PARAMS ((void));
846 static void mips_align PARAMS ((int to, int fill, symbolS *label));
847 static void s_align PARAMS ((int));
848 static void s_change_sec PARAMS ((int));
849 static void s_change_section PARAMS ((int));
850 static void s_cons PARAMS ((int));
851 static void s_float_cons PARAMS ((int));
852 static void s_mips_globl PARAMS ((int));
853 static void s_option PARAMS ((int));
854 static void s_mipsset PARAMS ((int));
855 static void s_abicalls PARAMS ((int));
856 static void s_cpload PARAMS ((int));
857 static void s_cpsetup PARAMS ((int));
858 static void s_cplocal PARAMS ((int));
859 static void s_cprestore PARAMS ((int));
860 static void s_cpreturn PARAMS ((int));
861 static void s_gpvalue PARAMS ((int));
862 static void s_gpword PARAMS ((int));
863 static void s_cpadd PARAMS ((int));
864 static void s_insn PARAMS ((int));
865 static void md_obj_begin PARAMS ((void));
866 static void md_obj_end PARAMS ((void));
867 static long get_number PARAMS ((void));
868 static void s_mips_ent PARAMS ((int));
869 static void s_mips_end PARAMS ((int));
870 static void s_mips_frame PARAMS ((int));
871 static void s_mips_mask PARAMS ((int));
872 static void s_mips_stab PARAMS ((int));
873 static void s_mips_weakext PARAMS ((int));
874 static void s_mips_file PARAMS ((int));
875 static void s_mips_loc PARAMS ((int));
876 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
877 static int relaxed_branch_length (fragS *, asection *, int);
878 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
879 static void show PARAMS ((FILE *, const char *, int *, int *));
880 #ifdef OBJ_ELF
881 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
882 #endif
883
884 /* Return values of my_getSmallExpression().  */
885
886 enum small_ex_type
887 {
888   S_EX_NONE = 0,
889   S_EX_REGISTER,
890
891   /* Direct relocation creation by %percent_op().  */
892   S_EX_HALF,
893   S_EX_HI,
894   S_EX_LO,
895   S_EX_GP_REL,
896   S_EX_GOT,
897   S_EX_CALL16,
898   S_EX_GOT_DISP,
899   S_EX_GOT_PAGE,
900   S_EX_GOT_OFST,
901   S_EX_GOT_HI,
902   S_EX_GOT_LO,
903   S_EX_NEG,
904   S_EX_HIGHER,
905   S_EX_HIGHEST,
906   S_EX_CALL_HI,
907   S_EX_CALL_LO
908 };
909
910 /* Table and functions used to map between CPU/ISA names, and
911    ISA levels, and CPU numbers.  */
912
913 struct mips_cpu_info
914 {
915   const char *name;           /* CPU or ISA name.  */
916   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
917   int isa;                    /* ISA level.  */
918   int cpu;                    /* CPU number (default CPU if ISA).  */
919 };
920
921 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
922 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
923 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
924                                                         const char *));
925 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
926 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
927                                                            const char *));
928 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
929 \f
930 /* Pseudo-op table.
931
932    The following pseudo-ops from the Kane and Heinrich MIPS book
933    should be defined here, but are currently unsupported: .alias,
934    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
935
936    The following pseudo-ops from the Kane and Heinrich MIPS book are
937    specific to the type of debugging information being generated, and
938    should be defined by the object format: .aent, .begin, .bend,
939    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
940    .vreg.
941
942    The following pseudo-ops from the Kane and Heinrich MIPS book are
943    not MIPS CPU specific, but are also not specific to the object file
944    format.  This file is probably the best place to define them, but
945    they are not currently supported: .asm0, .endr, .lab, .repeat,
946    .struct.  */
947
948 static const pseudo_typeS mips_pseudo_table[] =
949 {
950   /* MIPS specific pseudo-ops.  */
951   {"option", s_option, 0},
952   {"set", s_mipsset, 0},
953   {"rdata", s_change_sec, 'r'},
954   {"sdata", s_change_sec, 's'},
955   {"livereg", s_ignore, 0},
956   {"abicalls", s_abicalls, 0},
957   {"cpload", s_cpload, 0},
958   {"cpsetup", s_cpsetup, 0},
959   {"cplocal", s_cplocal, 0},
960   {"cprestore", s_cprestore, 0},
961   {"cpreturn", s_cpreturn, 0},
962   {"gpvalue", s_gpvalue, 0},
963   {"gpword", s_gpword, 0},
964   {"cpadd", s_cpadd, 0},
965   {"insn", s_insn, 0},
966
967   /* Relatively generic pseudo-ops that happen to be used on MIPS
968      chips.  */
969   {"asciiz", stringer, 1},
970   {"bss", s_change_sec, 'b'},
971   {"err", s_err, 0},
972   {"half", s_cons, 1},
973   {"dword", s_cons, 3},
974   {"weakext", s_mips_weakext, 0},
975
976   /* These pseudo-ops are defined in read.c, but must be overridden
977      here for one reason or another.  */
978   {"align", s_align, 0},
979   {"byte", s_cons, 0},
980   {"data", s_change_sec, 'd'},
981   {"double", s_float_cons, 'd'},
982   {"float", s_float_cons, 'f'},
983   {"globl", s_mips_globl, 0},
984   {"global", s_mips_globl, 0},
985   {"hword", s_cons, 1},
986   {"int", s_cons, 2},
987   {"long", s_cons, 2},
988   {"octa", s_cons, 4},
989   {"quad", s_cons, 3},
990   {"section", s_change_section, 0},
991   {"short", s_cons, 1},
992   {"single", s_float_cons, 'f'},
993   {"stabn", s_mips_stab, 'n'},
994   {"text", s_change_sec, 't'},
995   {"word", s_cons, 2},
996
997   { "extern", ecoff_directive_extern, 0},
998
999   { NULL, NULL, 0 },
1000 };
1001
1002 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1003 {
1004   /* These pseudo-ops should be defined by the object file format.
1005      However, a.out doesn't support them, so we have versions here.  */
1006   {"aent", s_mips_ent, 1},
1007   {"bgnb", s_ignore, 0},
1008   {"end", s_mips_end, 0},
1009   {"endb", s_ignore, 0},
1010   {"ent", s_mips_ent, 0},
1011   {"file", s_mips_file, 0},
1012   {"fmask", s_mips_mask, 'F'},
1013   {"frame", s_mips_frame, 0},
1014   {"loc", s_mips_loc, 0},
1015   {"mask", s_mips_mask, 'R'},
1016   {"verstamp", s_ignore, 0},
1017   { NULL, NULL, 0 },
1018 };
1019
1020 extern void pop_insert PARAMS ((const pseudo_typeS *));
1021
1022 void
1023 mips_pop_insert ()
1024 {
1025   pop_insert (mips_pseudo_table);
1026   if (! ECOFF_DEBUGGING)
1027     pop_insert (mips_nonecoff_pseudo_table);
1028 }
1029 \f
1030 /* Symbols labelling the current insn.  */
1031
1032 struct insn_label_list
1033 {
1034   struct insn_label_list *next;
1035   symbolS *label;
1036 };
1037
1038 static struct insn_label_list *insn_labels;
1039 static struct insn_label_list *free_insn_labels;
1040
1041 static void mips_clear_insn_labels PARAMS ((void));
1042
1043 static inline void
1044 mips_clear_insn_labels ()
1045 {
1046   register struct insn_label_list **pl;
1047
1048   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1049     ;
1050   *pl = insn_labels;
1051   insn_labels = NULL;
1052 }
1053 \f
1054 static char *expr_end;
1055
1056 /* Expressions which appear in instructions.  These are set by
1057    mips_ip.  */
1058
1059 static expressionS imm_expr;
1060 static expressionS offset_expr;
1061
1062 /* Relocs associated with imm_expr and offset_expr.  */
1063
1064 static bfd_reloc_code_real_type imm_reloc[3]
1065   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1066 static bfd_reloc_code_real_type offset_reloc[3]
1067   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1068
1069 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
1070
1071 static boolean imm_unmatched_hi;
1072
1073 /* These are set by mips16_ip if an explicit extension is used.  */
1074
1075 static boolean mips16_small, mips16_ext;
1076
1077 #ifdef OBJ_ELF
1078 /* The pdr segment for per procedure frame/regmask info.  Not used for
1079    ECOFF debugging.  */
1080
1081 static segT pdr_seg;
1082 #endif
1083
1084 /* The default target format to use.  */
1085
1086 const char *
1087 mips_target_format ()
1088 {
1089   switch (OUTPUT_FLAVOR)
1090     {
1091     case bfd_target_aout_flavour:
1092       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1093     case bfd_target_ecoff_flavour:
1094       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1095     case bfd_target_coff_flavour:
1096       return "pe-mips";
1097     case bfd_target_elf_flavour:
1098 #ifdef TE_TMIPS
1099       /* This is traditional mips.  */
1100       return (target_big_endian
1101               ? (HAVE_64BIT_OBJECTS
1102                  ? "elf64-tradbigmips"
1103                  : (HAVE_NEWABI
1104                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1105               : (HAVE_64BIT_OBJECTS
1106                  ? "elf64-tradlittlemips"
1107                  : (HAVE_NEWABI
1108                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1109 #else
1110       return (target_big_endian
1111               ? (HAVE_64BIT_OBJECTS
1112                  ? "elf64-bigmips"
1113                  : (HAVE_NEWABI
1114                     ? "elf32-nbigmips" : "elf32-bigmips"))
1115               : (HAVE_64BIT_OBJECTS
1116                  ? "elf64-littlemips"
1117                  : (HAVE_NEWABI
1118                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1119 #endif
1120     default:
1121       abort ();
1122       return NULL;
1123     }
1124 }
1125
1126 /* This function is called once, at assembler startup time.  It should
1127    set up all the tables, etc. that the MD part of the assembler will need.  */
1128
1129 void
1130 md_begin ()
1131 {
1132   register const char *retval = NULL;
1133   int i = 0;
1134   int broken = 0;
1135
1136   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1137     as_warn (_("Could not set architecture and machine"));
1138
1139   op_hash = hash_new ();
1140
1141   for (i = 0; i < NUMOPCODES;)
1142     {
1143       const char *name = mips_opcodes[i].name;
1144
1145       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1146       if (retval != NULL)
1147         {
1148           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1149                    mips_opcodes[i].name, retval);
1150           /* Probably a memory allocation problem?  Give up now.  */
1151           as_fatal (_("Broken assembler.  No assembly attempted."));
1152         }
1153       do
1154         {
1155           if (mips_opcodes[i].pinfo != INSN_MACRO)
1156             {
1157               if (!validate_mips_insn (&mips_opcodes[i]))
1158                 broken = 1;
1159             }
1160           ++i;
1161         }
1162       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1163     }
1164
1165   mips16_op_hash = hash_new ();
1166
1167   i = 0;
1168   while (i < bfd_mips16_num_opcodes)
1169     {
1170       const char *name = mips16_opcodes[i].name;
1171
1172       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1173       if (retval != NULL)
1174         as_fatal (_("internal: can't hash `%s': %s"),
1175                   mips16_opcodes[i].name, retval);
1176       do
1177         {
1178           if (mips16_opcodes[i].pinfo != INSN_MACRO
1179               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1180                   != mips16_opcodes[i].match))
1181             {
1182               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1183                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1184               broken = 1;
1185             }
1186           ++i;
1187         }
1188       while (i < bfd_mips16_num_opcodes
1189              && strcmp (mips16_opcodes[i].name, name) == 0);
1190     }
1191
1192   if (broken)
1193     as_fatal (_("Broken assembler.  No assembly attempted."));
1194
1195   /* We add all the general register names to the symbol table.  This
1196      helps us detect invalid uses of them.  */
1197   for (i = 0; i < 32; i++)
1198     {
1199       char buf[5];
1200
1201       sprintf (buf, "$%d", i);
1202       symbol_table_insert (symbol_new (buf, reg_section, i,
1203                                        &zero_address_frag));
1204     }
1205   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1206                                    &zero_address_frag));
1207   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1208                                    &zero_address_frag));
1209   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1210                                    &zero_address_frag));
1211   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1212                                    &zero_address_frag));
1213   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1214                                    &zero_address_frag));
1215   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1216                                    &zero_address_frag));
1217   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1218                                    &zero_address_frag));
1219   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1220                                    &zero_address_frag));
1221   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1222                                    &zero_address_frag));
1223
1224   mips_no_prev_insn (false);
1225
1226   mips_gprmask = 0;
1227   mips_cprmask[0] = 0;
1228   mips_cprmask[1] = 0;
1229   mips_cprmask[2] = 0;
1230   mips_cprmask[3] = 0;
1231
1232   /* set the default alignment for the text section (2**2) */
1233   record_alignment (text_section, 2);
1234
1235   if (USE_GLOBAL_POINTER_OPT)
1236     bfd_set_gp_size (stdoutput, g_switch_value);
1237
1238   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1239     {
1240       /* On a native system, sections must be aligned to 16 byte
1241          boundaries.  When configured for an embedded ELF target, we
1242          don't bother.  */
1243       if (strcmp (TARGET_OS, "elf") != 0)
1244         {
1245           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1246           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1247           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1248         }
1249
1250       /* Create a .reginfo section for register masks and a .mdebug
1251          section for debugging information.  */
1252       {
1253         segT seg;
1254         subsegT subseg;
1255         flagword flags;
1256         segT sec;
1257
1258         seg = now_seg;
1259         subseg = now_subseg;
1260
1261         /* The ABI says this section should be loaded so that the
1262            running program can access it.  However, we don't load it
1263            if we are configured for an embedded target */
1264         flags = SEC_READONLY | SEC_DATA;
1265         if (strcmp (TARGET_OS, "elf") != 0)
1266           flags |= SEC_ALLOC | SEC_LOAD;
1267
1268         if (mips_abi != N64_ABI)
1269           {
1270             sec = subseg_new (".reginfo", (subsegT) 0);
1271
1272             bfd_set_section_flags (stdoutput, sec, flags);
1273             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1274
1275 #ifdef OBJ_ELF
1276             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1277 #endif
1278           }
1279         else
1280           {
1281             /* The 64-bit ABI uses a .MIPS.options section rather than
1282                .reginfo section.  */
1283             sec = subseg_new (".MIPS.options", (subsegT) 0);
1284             bfd_set_section_flags (stdoutput, sec, flags);
1285             bfd_set_section_alignment (stdoutput, sec, 3);
1286
1287 #ifdef OBJ_ELF
1288             /* Set up the option header.  */
1289             {
1290               Elf_Internal_Options opthdr;
1291               char *f;
1292
1293               opthdr.kind = ODK_REGINFO;
1294               opthdr.size = (sizeof (Elf_External_Options)
1295                              + sizeof (Elf64_External_RegInfo));
1296               opthdr.section = 0;
1297               opthdr.info = 0;
1298               f = frag_more (sizeof (Elf_External_Options));
1299               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1300                                              (Elf_External_Options *) f);
1301
1302               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1303             }
1304 #endif
1305           }
1306
1307         if (ECOFF_DEBUGGING)
1308           {
1309             sec = subseg_new (".mdebug", (subsegT) 0);
1310             (void) bfd_set_section_flags (stdoutput, sec,
1311                                           SEC_HAS_CONTENTS | SEC_READONLY);
1312             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1313           }
1314 #ifdef OBJ_ELF
1315         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1316           {
1317             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1318             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1319                                           SEC_READONLY | SEC_RELOC
1320                                           | SEC_DEBUGGING);
1321             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1322           }
1323 #endif
1324
1325         subseg_set (seg, subseg);
1326       }
1327     }
1328
1329   if (! ECOFF_DEBUGGING)
1330     md_obj_begin ();
1331 }
1332
1333 void
1334 md_mips_end ()
1335 {
1336   if (! ECOFF_DEBUGGING)
1337     md_obj_end ();
1338 }
1339
1340 void
1341 md_assemble (str)
1342      char *str;
1343 {
1344   struct mips_cl_insn insn;
1345   bfd_reloc_code_real_type unused_reloc[3]
1346     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1347
1348   imm_expr.X_op = O_absent;
1349   imm_unmatched_hi = false;
1350   offset_expr.X_op = O_absent;
1351   imm_reloc[0] = BFD_RELOC_UNUSED;
1352   imm_reloc[1] = BFD_RELOC_UNUSED;
1353   imm_reloc[2] = BFD_RELOC_UNUSED;
1354   offset_reloc[0] = BFD_RELOC_UNUSED;
1355   offset_reloc[1] = BFD_RELOC_UNUSED;
1356   offset_reloc[2] = BFD_RELOC_UNUSED;
1357
1358   if (mips_opts.mips16)
1359     mips16_ip (str, &insn);
1360   else
1361     {
1362       mips_ip (str, &insn);
1363       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1364             str, insn.insn_opcode));
1365     }
1366
1367   if (insn_error)
1368     {
1369       as_bad ("%s `%s'", insn_error, str);
1370       return;
1371     }
1372
1373   if (insn.insn_mo->pinfo == INSN_MACRO)
1374     {
1375       if (mips_opts.mips16)
1376         mips16_macro (&insn);
1377       else
1378         macro (&insn);
1379     }
1380   else
1381     {
1382       if (imm_expr.X_op != O_absent)
1383         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1384       else if (offset_expr.X_op != O_absent)
1385         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1386       else
1387         append_insn (NULL, &insn, NULL, unused_reloc, false);
1388     }
1389 }
1390
1391 /* See whether instruction IP reads register REG.  CLASS is the type
1392    of register.  */
1393
1394 static int
1395 insn_uses_reg (ip, reg, class)
1396      struct mips_cl_insn *ip;
1397      unsigned int reg;
1398      enum mips_regclass class;
1399 {
1400   if (class == MIPS16_REG)
1401     {
1402       assert (mips_opts.mips16);
1403       reg = mips16_to_32_reg_map[reg];
1404       class = MIPS_GR_REG;
1405     }
1406
1407   /* Don't report on general register ZERO, since it never changes.  */
1408   if (class == MIPS_GR_REG && reg == ZERO)
1409     return 0;
1410
1411   if (class == MIPS_FP_REG)
1412     {
1413       assert (! mips_opts.mips16);
1414       /* If we are called with either $f0 or $f1, we must check $f0.
1415          This is not optimal, because it will introduce an unnecessary
1416          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1417          need to distinguish reading both $f0 and $f1 or just one of
1418          them.  Note that we don't have to check the other way,
1419          because there is no instruction that sets both $f0 and $f1
1420          and requires a delay.  */
1421       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1422           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1423               == (reg &~ (unsigned) 1)))
1424         return 1;
1425       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1426           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1427               == (reg &~ (unsigned) 1)))
1428         return 1;
1429     }
1430   else if (! mips_opts.mips16)
1431     {
1432       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1433           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1434         return 1;
1435       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1436           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1437         return 1;
1438     }
1439   else
1440     {
1441       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1442           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1443                                     & MIPS16OP_MASK_RX)]
1444               == reg))
1445         return 1;
1446       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1447           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1448                                     & MIPS16OP_MASK_RY)]
1449               == reg))
1450         return 1;
1451       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1452           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1453                                     & MIPS16OP_MASK_MOVE32Z)]
1454               == reg))
1455         return 1;
1456       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1457         return 1;
1458       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1459         return 1;
1460       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1461         return 1;
1462       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1463           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1464               & MIPS16OP_MASK_REGR32) == reg)
1465         return 1;
1466     }
1467
1468   return 0;
1469 }
1470
1471 /* This function returns true if modifying a register requires a
1472    delay.  */
1473
1474 static int
1475 reg_needs_delay (reg)
1476      unsigned int reg;
1477 {
1478   unsigned long prev_pinfo;
1479
1480   prev_pinfo = prev_insn.insn_mo->pinfo;
1481   if (! mips_opts.noreorder
1482       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1483       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1484           || (! gpr_interlocks
1485               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1486     {
1487       /* A load from a coprocessor or from memory.  All load
1488          delays delay the use of general register rt for one
1489          instruction on the r3000.  The r6000 and r4000 use
1490          interlocks.  */
1491       /* Itbl support may require additional care here.  */
1492       know (prev_pinfo & INSN_WRITE_GPR_T);
1493       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1494         return 1;
1495     }
1496
1497   return 0;
1498 }
1499
1500 /* Mark instruction labels in mips16 mode.  This permits the linker to
1501    handle them specially, such as generating jalx instructions when
1502    needed.  We also make them odd for the duration of the assembly, in
1503    order to generate the right sort of code.  We will make them even
1504    in the adjust_symtab routine, while leaving them marked.  This is
1505    convenient for the debugger and the disassembler.  The linker knows
1506    to make them odd again.  */
1507
1508 static void
1509 mips16_mark_labels ()
1510 {
1511   if (mips_opts.mips16)
1512     {
1513       struct insn_label_list *l;
1514       valueT val;
1515
1516       for (l = insn_labels; l != NULL; l = l->next)
1517         {
1518 #ifdef OBJ_ELF
1519           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1520             S_SET_OTHER (l->label, STO_MIPS16);
1521 #endif
1522           val = S_GET_VALUE (l->label);
1523           if ((val & 1) == 0)
1524             S_SET_VALUE (l->label, val + 1);
1525         }
1526     }
1527 }
1528
1529 /* Output an instruction.  PLACE is where to put the instruction; if
1530    it is NULL, this uses frag_more to get room.  IP is the instruction
1531    information.  ADDRESS_EXPR is an operand of the instruction to be
1532    used with RELOC_TYPE.  */
1533
1534 static void
1535 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1536      char *place;
1537      struct mips_cl_insn *ip;
1538      expressionS *address_expr;
1539      bfd_reloc_code_real_type *reloc_type;
1540      boolean unmatched_hi;
1541 {
1542   register unsigned long prev_pinfo, pinfo;
1543   char *f;
1544   fixS *fixp[3];
1545   int nops = 0;
1546
1547   /* Mark instruction labels in mips16 mode.  */
1548   mips16_mark_labels ();
1549
1550   prev_pinfo = prev_insn.insn_mo->pinfo;
1551   pinfo = ip->insn_mo->pinfo;
1552
1553   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1554     {
1555       int prev_prev_nop;
1556
1557       /* If the previous insn required any delay slots, see if we need
1558          to insert a NOP or two.  There are eight kinds of possible
1559          hazards, of which an instruction can have at most one type.
1560          (1) a load from memory delay
1561          (2) a load from a coprocessor delay
1562          (3) an unconditional branch delay
1563          (4) a conditional branch delay
1564          (5) a move to coprocessor register delay
1565          (6) a load coprocessor register from memory delay
1566          (7) a coprocessor condition code delay
1567          (8) a HI/LO special register delay
1568
1569          There are a lot of optimizations we could do that we don't.
1570          In particular, we do not, in general, reorder instructions.
1571          If you use gcc with optimization, it will reorder
1572          instructions and generally do much more optimization then we
1573          do here; repeating all that work in the assembler would only
1574          benefit hand written assembly code, and does not seem worth
1575          it.  */
1576
1577       /* This is how a NOP is emitted.  */
1578 #define emit_nop()                                      \
1579   (mips_opts.mips16                                     \
1580    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1581    : md_number_to_chars (frag_more (4), 0, 4))
1582
1583       /* The previous insn might require a delay slot, depending upon
1584          the contents of the current insn.  */
1585       if (! mips_opts.mips16
1586           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1587           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1588                && ! cop_interlocks)
1589               || (! gpr_interlocks
1590                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1591         {
1592           /* A load from a coprocessor or from memory.  All load
1593              delays delay the use of general register rt for one
1594              instruction on the r3000.  The r6000 and r4000 use
1595              interlocks.  */
1596           /* Itbl support may require additional care here.  */
1597           know (prev_pinfo & INSN_WRITE_GPR_T);
1598           if (mips_optimize == 0
1599               || insn_uses_reg (ip,
1600                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1601                                  & OP_MASK_RT),
1602                                 MIPS_GR_REG))
1603             ++nops;
1604         }
1605       else if (! mips_opts.mips16
1606                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1607                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1608                     && ! cop_interlocks)
1609                    || (mips_opts.isa == ISA_MIPS1
1610                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1611         {
1612           /* A generic coprocessor delay.  The previous instruction
1613              modified a coprocessor general or control register.  If
1614              it modified a control register, we need to avoid any
1615              coprocessor instruction (this is probably not always
1616              required, but it sometimes is).  If it modified a general
1617              register, we avoid using that register.
1618
1619              On the r6000 and r4000 loading a coprocessor register
1620              from memory is interlocked, and does not require a delay.
1621
1622              This case is not handled very well.  There is no special
1623              knowledge of CP0 handling, and the coprocessors other
1624              than the floating point unit are not distinguished at
1625              all.  */
1626           /* Itbl support may require additional care here. FIXME!
1627              Need to modify this to include knowledge about
1628              user specified delays!  */
1629           if (prev_pinfo & INSN_WRITE_FPR_T)
1630             {
1631               if (mips_optimize == 0
1632                   || insn_uses_reg (ip,
1633                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1634                                      & OP_MASK_FT),
1635                                     MIPS_FP_REG))
1636                 ++nops;
1637             }
1638           else if (prev_pinfo & INSN_WRITE_FPR_S)
1639             {
1640               if (mips_optimize == 0
1641                   || insn_uses_reg (ip,
1642                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1643                                      & OP_MASK_FS),
1644                                     MIPS_FP_REG))
1645                 ++nops;
1646             }
1647           else
1648             {
1649               /* We don't know exactly what the previous instruction
1650                  does.  If the current instruction uses a coprocessor
1651                  register, we must insert a NOP.  If previous
1652                  instruction may set the condition codes, and the
1653                  current instruction uses them, we must insert two
1654                  NOPS.  */
1655               /* Itbl support may require additional care here.  */
1656               if (mips_optimize == 0
1657                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1658                       && (pinfo & INSN_READ_COND_CODE)))
1659                 nops += 2;
1660               else if (pinfo & INSN_COP)
1661                 ++nops;
1662             }
1663         }
1664       else if (! mips_opts.mips16
1665                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1666                && (prev_pinfo & INSN_WRITE_COND_CODE)
1667                && ! cop_interlocks)
1668         {
1669           /* The previous instruction sets the coprocessor condition
1670              codes, but does not require a general coprocessor delay
1671              (this means it is a floating point comparison
1672              instruction).  If this instruction uses the condition
1673              codes, we need to insert a single NOP.  */
1674           /* Itbl support may require additional care here.  */
1675           if (mips_optimize == 0
1676               || (pinfo & INSN_READ_COND_CODE))
1677             ++nops;
1678         }
1679
1680       /* If we're fixing up mfhi/mflo for the r7000 and the
1681          previous insn was an mfhi/mflo and the current insn
1682          reads the register that the mfhi/mflo wrote to, then
1683          insert two nops.  */
1684
1685       else if (mips_7000_hilo_fix
1686                && MF_HILO_INSN (prev_pinfo)
1687                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1688                                       & OP_MASK_RD),
1689                                  MIPS_GR_REG))
1690         {
1691           nops += 2;
1692         }
1693
1694       /* If we're fixing up mfhi/mflo for the r7000 and the
1695          2nd previous insn was an mfhi/mflo and the current insn
1696          reads the register that the mfhi/mflo wrote to, then
1697          insert one nop.  */
1698
1699       else if (mips_7000_hilo_fix
1700                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1701                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1702                                        & OP_MASK_RD),
1703                                     MIPS_GR_REG))
1704
1705         {
1706           ++nops;
1707         }
1708
1709       else if (prev_pinfo & INSN_READ_LO)
1710         {
1711           /* The previous instruction reads the LO register; if the
1712              current instruction writes to the LO register, we must
1713              insert two NOPS.  Some newer processors have interlocks.
1714              Also the tx39's multiply instructions can be exectuted
1715              immediatly after a read from HI/LO (without the delay),
1716              though the tx39's divide insns still do require the
1717              delay.  */
1718           if (! (hilo_interlocks
1719                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1720               && (mips_optimize == 0
1721                   || (pinfo & INSN_WRITE_LO)))
1722             nops += 2;
1723           /* Most mips16 branch insns don't have a delay slot.
1724              If a read from LO is immediately followed by a branch
1725              to a write to LO we have a read followed by a write
1726              less than 2 insns away.  We assume the target of
1727              a branch might be a write to LO, and insert a nop
1728              between a read and an immediately following branch.  */
1729           else if (mips_opts.mips16
1730                    && (mips_optimize == 0
1731                        || (pinfo & MIPS16_INSN_BRANCH)))
1732             ++nops;
1733         }
1734       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1735         {
1736           /* The previous instruction reads the HI register; if the
1737              current instruction writes to the HI register, we must
1738              insert a NOP.  Some newer processors have interlocks.
1739              Also the note tx39's multiply above.  */
1740           if (! (hilo_interlocks
1741                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1742               && (mips_optimize == 0
1743                   || (pinfo & INSN_WRITE_HI)))
1744             nops += 2;
1745           /* Most mips16 branch insns don't have a delay slot.
1746              If a read from HI is immediately followed by a branch
1747              to a write to HI we have a read followed by a write
1748              less than 2 insns away.  We assume the target of
1749              a branch might be a write to HI, and insert a nop
1750              between a read and an immediately following branch.  */
1751           else if (mips_opts.mips16
1752                    && (mips_optimize == 0
1753                        || (pinfo & MIPS16_INSN_BRANCH)))
1754             ++nops;
1755         }
1756
1757       /* If the previous instruction was in a noreorder section, then
1758          we don't want to insert the nop after all.  */
1759       /* Itbl support may require additional care here.  */
1760       if (prev_insn_unreordered)
1761         nops = 0;
1762
1763       /* There are two cases which require two intervening
1764          instructions: 1) setting the condition codes using a move to
1765          coprocessor instruction which requires a general coprocessor
1766          delay and then reading the condition codes 2) reading the HI
1767          or LO register and then writing to it (except on processors
1768          which have interlocks).  If we are not already emitting a NOP
1769          instruction, we must check for these cases compared to the
1770          instruction previous to the previous instruction.  */
1771       if ((! mips_opts.mips16
1772            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1773            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1774            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1775            && (pinfo & INSN_READ_COND_CODE)
1776            && ! cop_interlocks)
1777           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1778               && (pinfo & INSN_WRITE_LO)
1779               && ! (hilo_interlocks
1780                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1781           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1782               && (pinfo & INSN_WRITE_HI)
1783               && ! (hilo_interlocks
1784                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1785         prev_prev_nop = 1;
1786       else
1787         prev_prev_nop = 0;
1788
1789       if (prev_prev_insn_unreordered)
1790         prev_prev_nop = 0;
1791
1792       if (prev_prev_nop && nops == 0)
1793         ++nops;
1794
1795       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1796         {
1797           /* We're out of bits in pinfo, so we must resort to string
1798              ops here.  Shortcuts are selected based on opcodes being
1799              limited to the VR4122 instruction set.  */
1800           int min_nops = 0;
1801           const char *pn = prev_insn.insn_mo->name;
1802           const char *tn = ip->insn_mo->name;
1803           if (strncmp(pn, "macc", 4) == 0
1804               || strncmp(pn, "dmacc", 5) == 0)
1805             {
1806               /* Errata 21 - [D]DIV[U] after [D]MACC */
1807               if (strstr (tn, "div"))
1808                 {
1809                   min_nops = 1;
1810                 }
1811
1812               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1813               if (pn[0] == 'd' /* dmacc */
1814                   && (strncmp(tn, "dmult", 5) == 0
1815                       || strncmp(tn, "dmacc", 5) == 0))
1816                 {
1817                   min_nops = 1;
1818                 }
1819
1820               /* Errata 24 - MT{LO,HI} after [D]MACC */
1821               if (strcmp (tn, "mtlo") == 0
1822                   || strcmp (tn, "mthi") == 0)
1823                 {
1824                   min_nops = 1;
1825                 }
1826
1827             }
1828           else if (strncmp(pn, "dmult", 5) == 0
1829                    && (strncmp(tn, "dmult", 5) == 0
1830                        || strncmp(tn, "dmacc", 5) == 0))
1831             {
1832               /* Here is the rest of errata 23.  */
1833               min_nops = 1;
1834             }
1835           if (nops < min_nops)
1836             nops = min_nops;
1837         }
1838
1839       /* If we are being given a nop instruction, don't bother with
1840          one of the nops we would otherwise output.  This will only
1841          happen when a nop instruction is used with mips_optimize set
1842          to 0.  */
1843       if (nops > 0
1844           && ! mips_opts.noreorder
1845           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1846         --nops;
1847
1848       /* Now emit the right number of NOP instructions.  */
1849       if (nops > 0 && ! mips_opts.noreorder)
1850         {
1851           fragS *old_frag;
1852           unsigned long old_frag_offset;
1853           int i;
1854           struct insn_label_list *l;
1855
1856           old_frag = frag_now;
1857           old_frag_offset = frag_now_fix ();
1858
1859           for (i = 0; i < nops; i++)
1860             emit_nop ();
1861
1862           if (listing)
1863             {
1864               listing_prev_line ();
1865               /* We may be at the start of a variant frag.  In case we
1866                  are, make sure there is enough space for the frag
1867                  after the frags created by listing_prev_line.  The
1868                  argument to frag_grow here must be at least as large
1869                  as the argument to all other calls to frag_grow in
1870                  this file.  We don't have to worry about being in the
1871                  middle of a variant frag, because the variants insert
1872                  all needed nop instructions themselves.  */
1873               frag_grow (40);
1874             }
1875
1876           for (l = insn_labels; l != NULL; l = l->next)
1877             {
1878               valueT val;
1879
1880               assert (S_GET_SEGMENT (l->label) == now_seg);
1881               symbol_set_frag (l->label, frag_now);
1882               val = (valueT) frag_now_fix ();
1883               /* mips16 text labels are stored as odd.  */
1884               if (mips_opts.mips16)
1885                 ++val;
1886               S_SET_VALUE (l->label, val);
1887             }
1888
1889 #ifndef NO_ECOFF_DEBUGGING
1890           if (ECOFF_DEBUGGING)
1891             ecoff_fix_loc (old_frag, old_frag_offset);
1892 #endif
1893         }
1894       else if (prev_nop_frag != NULL)
1895         {
1896           /* We have a frag holding nops we may be able to remove.  If
1897              we don't need any nops, we can decrease the size of
1898              prev_nop_frag by the size of one instruction.  If we do
1899              need some nops, we count them in prev_nops_required.  */
1900           if (prev_nop_frag_since == 0)
1901             {
1902               if (nops == 0)
1903                 {
1904                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1905                   --prev_nop_frag_holds;
1906                 }
1907               else
1908                 prev_nop_frag_required += nops;
1909             }
1910           else
1911             {
1912               if (prev_prev_nop == 0)
1913                 {
1914                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1915                   --prev_nop_frag_holds;
1916                 }
1917               else
1918                 ++prev_nop_frag_required;
1919             }
1920
1921           if (prev_nop_frag_holds <= prev_nop_frag_required)
1922             prev_nop_frag = NULL;
1923
1924           ++prev_nop_frag_since;
1925
1926           /* Sanity check: by the time we reach the second instruction
1927              after prev_nop_frag, we should have used up all the nops
1928              one way or another.  */
1929           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1930         }
1931     }
1932
1933   if (place == NULL
1934       && address_expr
1935       && ((*reloc_type == BFD_RELOC_16_PCREL
1936            && address_expr->X_op != O_constant)
1937           || *reloc_type == BFD_RELOC_16_PCREL_S2)
1938       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1939           || pinfo & INSN_COND_BRANCH_LIKELY)
1940       && mips_relax_branch
1941       /* Don't try branch relaxation within .set nomacro, or within
1942          .set noat if we use $at for PIC computations.  If it turns
1943          out that the branch was out-of-range, we'll get an error.  */
1944       && !mips_opts.warn_about_macros
1945       && !(mips_opts.noat && mips_pic != NO_PIC)
1946       && !mips_opts.mips16)
1947     {
1948       f = frag_var (rs_machine_dependent,
1949                     relaxed_branch_length
1950                     (NULL, NULL,
1951                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1952                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1953                     RELAX_BRANCH_ENCODE
1954                     (*reloc_type == BFD_RELOC_16_PCREL_S2,
1955                      pinfo & INSN_UNCOND_BRANCH_DELAY,
1956                      pinfo & INSN_COND_BRANCH_LIKELY,
1957                      pinfo & INSN_WRITE_GPR_31,
1958                      0),
1959                     address_expr->X_add_symbol,
1960                     address_expr->X_add_number,
1961                     0);
1962       *reloc_type = BFD_RELOC_UNUSED;
1963     }
1964   else if (*reloc_type > BFD_RELOC_UNUSED)
1965     {
1966       /* We need to set up a variant frag.  */
1967       assert (mips_opts.mips16 && address_expr != NULL);
1968       f = frag_var (rs_machine_dependent, 4, 0,
1969                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1970                                          mips16_small, mips16_ext,
1971                                          (prev_pinfo
1972                                           & INSN_UNCOND_BRANCH_DELAY),
1973                                          (*prev_insn_reloc_type
1974                                           == BFD_RELOC_MIPS16_JMP)),
1975                     make_expr_symbol (address_expr), 0, NULL);
1976     }
1977   else if (place != NULL)
1978     f = place;
1979   else if (mips_opts.mips16
1980            && ! ip->use_extend
1981            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1982     {
1983       /* Make sure there is enough room to swap this instruction with
1984          a following jump instruction.  */
1985       frag_grow (6);
1986       f = frag_more (2);
1987     }
1988   else
1989     {
1990       if (mips_opts.mips16
1991           && mips_opts.noreorder
1992           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1993         as_warn (_("extended instruction in delay slot"));
1994
1995       f = frag_more (4);
1996     }
1997
1998   fixp[0] = fixp[1] = fixp[2] = NULL;
1999   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2000     {
2001       if (address_expr->X_op == O_constant)
2002         {
2003           valueT tmp;
2004
2005           switch (*reloc_type)
2006             {
2007             case BFD_RELOC_32:
2008               ip->insn_opcode |= address_expr->X_add_number;
2009               break;
2010
2011             case BFD_RELOC_MIPS_HIGHEST:
2012               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2013               tmp >>= 16;
2014               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2015               break;
2016
2017             case BFD_RELOC_MIPS_HIGHER:
2018               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2019               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2020               break;
2021
2022             case BFD_RELOC_HI16_S:
2023               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2024                                   >> 16) & 0xffff;
2025               break;
2026
2027             case BFD_RELOC_HI16:
2028               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2029               break;
2030
2031             case BFD_RELOC_LO16:
2032             case BFD_RELOC_MIPS_GOT_DISP:
2033               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2034               break;
2035
2036             case BFD_RELOC_MIPS_JMP:
2037               if ((address_expr->X_add_number & 3) != 0)
2038                 as_bad (_("jump to misaligned address (0x%lx)"),
2039                         (unsigned long) address_expr->X_add_number);
2040               if (address_expr->X_add_number & ~0xfffffff)
2041                 as_bad (_("jump address range overflow (0x%lx)"),
2042                         (unsigned long) address_expr->X_add_number);
2043               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2044               break;
2045
2046             case BFD_RELOC_MIPS16_JMP:
2047               if ((address_expr->X_add_number & 3) != 0)
2048                 as_bad (_("jump to misaligned address (0x%lx)"),
2049                         (unsigned long) address_expr->X_add_number);
2050               if (address_expr->X_add_number & ~0xfffffff)
2051                 as_bad (_("jump address range overflow (0x%lx)"),
2052                         (unsigned long) address_expr->X_add_number);
2053               ip->insn_opcode |=
2054                 (((address_expr->X_add_number & 0x7c0000) << 3)
2055                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2056                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2057               break;
2058
2059             case BFD_RELOC_16_PCREL:
2060               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2061               break;
2062
2063             case BFD_RELOC_16_PCREL_S2:
2064               goto need_reloc;
2065
2066             default:
2067               internalError ();
2068             }
2069         }
2070       else
2071         {
2072         need_reloc:
2073           /* Don't generate a reloc if we are writing into a variant frag.  */
2074           if (place == NULL)
2075             {
2076               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2077                                      address_expr,
2078                                      (*reloc_type == BFD_RELOC_16_PCREL
2079                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
2080                                      reloc_type[0]);
2081
2082               /* These relocations can have an addend that won't fit in
2083                  4 octets for 64bit assembly.  */
2084               if (HAVE_64BIT_GPRS &&
2085                   (*reloc_type == BFD_RELOC_16
2086                    || *reloc_type == BFD_RELOC_32
2087                    || *reloc_type == BFD_RELOC_MIPS_JMP
2088                    || *reloc_type == BFD_RELOC_HI16_S
2089                    || *reloc_type == BFD_RELOC_LO16
2090                    || *reloc_type == BFD_RELOC_GPREL16
2091                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2092                    || *reloc_type == BFD_RELOC_GPREL32
2093                    || *reloc_type == BFD_RELOC_64
2094                    || *reloc_type == BFD_RELOC_CTOR
2095                    || *reloc_type == BFD_RELOC_MIPS_SUB
2096                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2097                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2098                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2099                    || *reloc_type == BFD_RELOC_MIPS_REL16
2100                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2101                 fixp[0]->fx_no_overflow = 1;
2102
2103               if (unmatched_hi)
2104                 {
2105                   struct mips_hi_fixup *hi_fixup;
2106
2107                   assert (*reloc_type == BFD_RELOC_HI16_S);
2108                   hi_fixup = ((struct mips_hi_fixup *)
2109                               xmalloc (sizeof (struct mips_hi_fixup)));
2110                   hi_fixup->fixp = fixp[0];
2111                   hi_fixup->seg = now_seg;
2112                   hi_fixup->next = mips_hi_fixup_list;
2113                   mips_hi_fixup_list = hi_fixup;
2114                 }
2115
2116               if (reloc_type[1] != BFD_RELOC_UNUSED)
2117                 {
2118                   /* FIXME: This symbol can be one of
2119                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2120                   address_expr->X_op = O_absent;
2121                   address_expr->X_add_symbol = 0;
2122                   address_expr->X_add_number = 0;
2123
2124                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2125                                          4, address_expr, false,
2126                                          reloc_type[1]);
2127
2128                   /* These relocations can have an addend that won't fit in
2129                      4 octets for 64bit assembly.  */
2130                   if (HAVE_64BIT_GPRS &&
2131                       (*reloc_type == BFD_RELOC_16
2132                        || *reloc_type == BFD_RELOC_32
2133                        || *reloc_type == BFD_RELOC_MIPS_JMP
2134                        || *reloc_type == BFD_RELOC_HI16_S
2135                        || *reloc_type == BFD_RELOC_LO16
2136                        || *reloc_type == BFD_RELOC_GPREL16
2137                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2138                        || *reloc_type == BFD_RELOC_GPREL32
2139                        || *reloc_type == BFD_RELOC_64
2140                        || *reloc_type == BFD_RELOC_CTOR
2141                        || *reloc_type == BFD_RELOC_MIPS_SUB
2142                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2143                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2144                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2145                        || *reloc_type == BFD_RELOC_MIPS_REL16
2146                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2147                     fixp[1]->fx_no_overflow = 1;
2148
2149                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2150                     {
2151                       address_expr->X_op = O_absent;
2152                       address_expr->X_add_symbol = 0;
2153                       address_expr->X_add_number = 0;
2154
2155                       fixp[2] = fix_new_exp (frag_now,
2156                                              f - frag_now->fr_literal, 4,
2157                                              address_expr, false,
2158                                              reloc_type[2]);
2159
2160                       /* These relocations can have an addend that won't fit in
2161                          4 octets for 64bit assembly.  */
2162                       if (HAVE_64BIT_GPRS &&
2163                           (*reloc_type == BFD_RELOC_16
2164                            || *reloc_type == BFD_RELOC_32
2165                            || *reloc_type == BFD_RELOC_MIPS_JMP
2166                            || *reloc_type == BFD_RELOC_HI16_S
2167                            || *reloc_type == BFD_RELOC_LO16
2168                            || *reloc_type == BFD_RELOC_GPREL16
2169                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2170                            || *reloc_type == BFD_RELOC_GPREL32
2171                            || *reloc_type == BFD_RELOC_64
2172                            || *reloc_type == BFD_RELOC_CTOR
2173                            || *reloc_type == BFD_RELOC_MIPS_SUB
2174                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2175                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2176                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2177                            || *reloc_type == BFD_RELOC_MIPS_REL16
2178                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2179                         fixp[2]->fx_no_overflow = 1;
2180                     }
2181                 }
2182             }
2183         }
2184     }
2185
2186   if (! mips_opts.mips16)
2187     {
2188       md_number_to_chars (f, ip->insn_opcode, 4);
2189 #ifdef OBJ_ELF
2190       dwarf2_emit_insn (4);
2191 #endif
2192     }
2193   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2194     {
2195       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2196       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2197 #ifdef OBJ_ELF
2198       dwarf2_emit_insn (4);
2199 #endif
2200     }
2201   else
2202     {
2203       if (ip->use_extend)
2204         {
2205           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2206           f += 2;
2207         }
2208       md_number_to_chars (f, ip->insn_opcode, 2);
2209 #ifdef OBJ_ELF
2210       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2211 #endif
2212     }
2213
2214   /* Update the register mask information.  */
2215   if (! mips_opts.mips16)
2216     {
2217       if (pinfo & INSN_WRITE_GPR_D)
2218         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2219       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2220         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2221       if (pinfo & INSN_READ_GPR_S)
2222         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2223       if (pinfo & INSN_WRITE_GPR_31)
2224         mips_gprmask |= 1 << RA;
2225       if (pinfo & INSN_WRITE_FPR_D)
2226         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2227       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2228         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2229       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2230         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2231       if ((pinfo & INSN_READ_FPR_R) != 0)
2232         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2233       if (pinfo & INSN_COP)
2234         {
2235           /* We don't keep enough information to sort these cases out.
2236              The itbl support does keep this information however, although
2237              we currently don't support itbl fprmats as part of the cop
2238              instruction.  May want to add this support in the future.  */
2239         }
2240       /* Never set the bit for $0, which is always zero.  */
2241       mips_gprmask &= ~1 << 0;
2242     }
2243   else
2244     {
2245       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2246         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2247                               & MIPS16OP_MASK_RX);
2248       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2249         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2250                               & MIPS16OP_MASK_RY);
2251       if (pinfo & MIPS16_INSN_WRITE_Z)
2252         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2253                               & MIPS16OP_MASK_RZ);
2254       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2255         mips_gprmask |= 1 << TREG;
2256       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2257         mips_gprmask |= 1 << SP;
2258       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2259         mips_gprmask |= 1 << RA;
2260       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2261         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2262       if (pinfo & MIPS16_INSN_READ_Z)
2263         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2264                               & MIPS16OP_MASK_MOVE32Z);
2265       if (pinfo & MIPS16_INSN_READ_GPR_X)
2266         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2267                               & MIPS16OP_MASK_REGR32);
2268     }
2269
2270   if (place == NULL && ! mips_opts.noreorder)
2271     {
2272       /* Filling the branch delay slot is more complex.  We try to
2273          switch the branch with the previous instruction, which we can
2274          do if the previous instruction does not set up a condition
2275          that the branch tests and if the branch is not itself the
2276          target of any branch.  */
2277       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2278           || (pinfo & INSN_COND_BRANCH_DELAY))
2279         {
2280           if (mips_optimize < 2
2281               /* If we have seen .set volatile or .set nomove, don't
2282                  optimize.  */
2283               || mips_opts.nomove != 0
2284               /* If we had to emit any NOP instructions, then we
2285                  already know we can not swap.  */
2286               || nops != 0
2287               /* If we don't even know the previous insn, we can not
2288                  swap.  */
2289               || ! prev_insn_valid
2290               /* If the previous insn is already in a branch delay
2291                  slot, then we can not swap.  */
2292               || prev_insn_is_delay_slot
2293               /* If the previous previous insn was in a .set
2294                  noreorder, we can't swap.  Actually, the MIPS
2295                  assembler will swap in this situation.  However, gcc
2296                  configured -with-gnu-as will generate code like
2297                    .set noreorder
2298                    lw   $4,XXX
2299                    .set reorder
2300                    INSN
2301                    bne  $4,$0,foo
2302                  in which we can not swap the bne and INSN.  If gcc is
2303                  not configured -with-gnu-as, it does not output the
2304                  .set pseudo-ops.  We don't have to check
2305                  prev_insn_unreordered, because prev_insn_valid will
2306                  be 0 in that case.  We don't want to use
2307                  prev_prev_insn_valid, because we do want to be able
2308                  to swap at the start of a function.  */
2309               || prev_prev_insn_unreordered
2310               /* If the branch is itself the target of a branch, we
2311                  can not swap.  We cheat on this; all we check for is
2312                  whether there is a label on this instruction.  If
2313                  there are any branches to anything other than a
2314                  label, users must use .set noreorder.  */
2315               || insn_labels != NULL
2316               /* If the previous instruction is in a variant frag, we
2317                  can not do the swap.  This does not apply to the
2318                  mips16, which uses variant frags for different
2319                  purposes.  */
2320               || (! mips_opts.mips16
2321                   && prev_insn_frag->fr_type == rs_machine_dependent)
2322               /* If the branch reads the condition codes, we don't
2323                  even try to swap, because in the sequence
2324                    ctc1 $X,$31
2325                    INSN
2326                    INSN
2327                    bc1t LABEL
2328                  we can not swap, and I don't feel like handling that
2329                  case.  */
2330               || (! mips_opts.mips16
2331                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2332                   && (pinfo & INSN_READ_COND_CODE))
2333               /* We can not swap with an instruction that requires a
2334                  delay slot, becase the target of the branch might
2335                  interfere with that instruction.  */
2336               || (! mips_opts.mips16
2337                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2338                   && (prev_pinfo
2339               /* Itbl support may require additional care here.  */
2340                       & (INSN_LOAD_COPROC_DELAY
2341                          | INSN_COPROC_MOVE_DELAY
2342                          | INSN_WRITE_COND_CODE)))
2343               || (! (hilo_interlocks
2344                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2345                   && (prev_pinfo
2346                       & (INSN_READ_LO
2347                          | INSN_READ_HI)))
2348               || (! mips_opts.mips16
2349                   && ! gpr_interlocks
2350                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2351               || (! mips_opts.mips16
2352                   && mips_opts.isa == ISA_MIPS1
2353                   /* Itbl support may require additional care here.  */
2354                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2355               /* We can not swap with a branch instruction.  */
2356               || (prev_pinfo
2357                   & (INSN_UNCOND_BRANCH_DELAY
2358                      | INSN_COND_BRANCH_DELAY
2359                      | INSN_COND_BRANCH_LIKELY))
2360               /* We do not swap with a trap instruction, since it
2361                  complicates trap handlers to have the trap
2362                  instruction be in a delay slot.  */
2363               || (prev_pinfo & INSN_TRAP)
2364               /* If the branch reads a register that the previous
2365                  instruction sets, we can not swap.  */
2366               || (! mips_opts.mips16
2367                   && (prev_pinfo & INSN_WRITE_GPR_T)
2368                   && insn_uses_reg (ip,
2369                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2370                                      & OP_MASK_RT),
2371                                     MIPS_GR_REG))
2372               || (! mips_opts.mips16
2373                   && (prev_pinfo & INSN_WRITE_GPR_D)
2374                   && insn_uses_reg (ip,
2375                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2376                                      & OP_MASK_RD),
2377                                     MIPS_GR_REG))
2378               || (mips_opts.mips16
2379                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2380                        && insn_uses_reg (ip,
2381                                          ((prev_insn.insn_opcode
2382                                            >> MIPS16OP_SH_RX)
2383                                           & MIPS16OP_MASK_RX),
2384                                          MIPS16_REG))
2385                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2386                           && insn_uses_reg (ip,
2387                                             ((prev_insn.insn_opcode
2388                                               >> MIPS16OP_SH_RY)
2389                                              & MIPS16OP_MASK_RY),
2390                                             MIPS16_REG))
2391                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2392                           && insn_uses_reg (ip,
2393                                             ((prev_insn.insn_opcode
2394                                               >> MIPS16OP_SH_RZ)
2395                                              & MIPS16OP_MASK_RZ),
2396                                             MIPS16_REG))
2397                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2398                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2399                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2400                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2401                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2402                           && insn_uses_reg (ip,
2403                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2404                                                                      insn_opcode),
2405                                             MIPS_GR_REG))))
2406               /* If the branch writes a register that the previous
2407                  instruction sets, we can not swap (we know that
2408                  branches write only to RD or to $31).  */
2409               || (! mips_opts.mips16
2410                   && (prev_pinfo & INSN_WRITE_GPR_T)
2411                   && (((pinfo & INSN_WRITE_GPR_D)
2412                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2413                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2414                       || ((pinfo & INSN_WRITE_GPR_31)
2415                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2416                                & OP_MASK_RT)
2417                               == RA))))
2418               || (! mips_opts.mips16
2419                   && (prev_pinfo & INSN_WRITE_GPR_D)
2420                   && (((pinfo & INSN_WRITE_GPR_D)
2421                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2422                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2423                       || ((pinfo & INSN_WRITE_GPR_31)
2424                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2425                                & OP_MASK_RD)
2426                               == RA))))
2427               || (mips_opts.mips16
2428                   && (pinfo & MIPS16_INSN_WRITE_31)
2429                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2430                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2431                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2432                               == RA))))
2433               /* If the branch writes a register that the previous
2434                  instruction reads, we can not swap (we know that
2435                  branches only write to RD or to $31).  */
2436               || (! mips_opts.mips16
2437                   && (pinfo & INSN_WRITE_GPR_D)
2438                   && insn_uses_reg (&prev_insn,
2439                                     ((ip->insn_opcode >> OP_SH_RD)
2440                                      & OP_MASK_RD),
2441                                     MIPS_GR_REG))
2442               || (! mips_opts.mips16
2443                   && (pinfo & INSN_WRITE_GPR_31)
2444                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2445               || (mips_opts.mips16
2446                   && (pinfo & MIPS16_INSN_WRITE_31)
2447                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2448               /* If we are generating embedded PIC code, the branch
2449                  might be expanded into a sequence which uses $at, so
2450                  we can't swap with an instruction which reads it.  */
2451               || (mips_pic == EMBEDDED_PIC
2452                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2453               /* If the previous previous instruction has a load
2454                  delay, and sets a register that the branch reads, we
2455                  can not swap.  */
2456               || (! mips_opts.mips16
2457                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2458               /* Itbl support may require additional care here.  */
2459                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2460                       || (! gpr_interlocks
2461                           && (prev_prev_insn.insn_mo->pinfo
2462                               & INSN_LOAD_MEMORY_DELAY)))
2463                   && insn_uses_reg (ip,
2464                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2465                                      & OP_MASK_RT),
2466                                     MIPS_GR_REG))
2467               /* If one instruction sets a condition code and the
2468                  other one uses a condition code, we can not swap.  */
2469               || ((pinfo & INSN_READ_COND_CODE)
2470                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2471               || ((pinfo & INSN_WRITE_COND_CODE)
2472                   && (prev_pinfo & INSN_READ_COND_CODE))
2473               /* If the previous instruction uses the PC, we can not
2474                  swap.  */
2475               || (mips_opts.mips16
2476                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2477               /* If the previous instruction was extended, we can not
2478                  swap.  */
2479               || (mips_opts.mips16 && prev_insn_extended)
2480               /* If the previous instruction had a fixup in mips16
2481                  mode, we can not swap.  This normally means that the
2482                  previous instruction was a 4 byte branch anyhow.  */
2483               || (mips_opts.mips16 && prev_insn_fixp[0])
2484               /* If the previous instruction is a sync, sync.l, or
2485                  sync.p, we can not swap.  */
2486               || (prev_pinfo & INSN_SYNC))
2487             {
2488               /* We could do even better for unconditional branches to
2489                  portions of this object file; we could pick up the
2490                  instruction at the destination, put it in the delay
2491                  slot, and bump the destination address.  */
2492               emit_nop ();
2493               /* Update the previous insn information.  */
2494               prev_prev_insn = *ip;
2495               prev_insn.insn_mo = &dummy_opcode;
2496             }
2497           else
2498             {
2499               /* It looks like we can actually do the swap.  */
2500               if (! mips_opts.mips16)
2501                 {
2502                   char *prev_f;
2503                   char temp[4];
2504
2505                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2506                   memcpy (temp, prev_f, 4);
2507                   memcpy (prev_f, f, 4);
2508                   memcpy (f, temp, 4);
2509                   if (prev_insn_fixp[0])
2510                     {
2511                       prev_insn_fixp[0]->fx_frag = frag_now;
2512                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2513                     }
2514                   if (prev_insn_fixp[1])
2515                     {
2516                       prev_insn_fixp[1]->fx_frag = frag_now;
2517                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2518                     }
2519                   if (prev_insn_fixp[2])
2520                     {
2521                       prev_insn_fixp[2]->fx_frag = frag_now;
2522                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2523                     }
2524                   if (fixp[0])
2525                     {
2526                       fixp[0]->fx_frag = prev_insn_frag;
2527                       fixp[0]->fx_where = prev_insn_where;
2528                     }
2529                   if (fixp[1])
2530                     {
2531                       fixp[1]->fx_frag = prev_insn_frag;
2532                       fixp[1]->fx_where = prev_insn_where;
2533                     }
2534                   if (fixp[2])
2535                     {
2536                       fixp[2]->fx_frag = prev_insn_frag;
2537                       fixp[2]->fx_where = prev_insn_where;
2538                     }
2539                 }
2540               else
2541                 {
2542                   char *prev_f;
2543                   char temp[2];
2544
2545                   assert (prev_insn_fixp[0] == NULL);
2546                   assert (prev_insn_fixp[1] == NULL);
2547                   assert (prev_insn_fixp[2] == NULL);
2548                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2549                   memcpy (temp, prev_f, 2);
2550                   memcpy (prev_f, f, 2);
2551                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2552                     {
2553                       assert (*reloc_type == BFD_RELOC_UNUSED);
2554                       memcpy (f, temp, 2);
2555                     }
2556                   else
2557                     {
2558                       memcpy (f, f + 2, 2);
2559                       memcpy (f + 2, temp, 2);
2560                     }
2561                   if (fixp[0])
2562                     {
2563                       fixp[0]->fx_frag = prev_insn_frag;
2564                       fixp[0]->fx_where = prev_insn_where;
2565                     }
2566                   if (fixp[1])
2567                     {
2568                       fixp[1]->fx_frag = prev_insn_frag;
2569                       fixp[1]->fx_where = prev_insn_where;
2570                     }
2571                   if (fixp[2])
2572                     {
2573                       fixp[2]->fx_frag = prev_insn_frag;
2574                       fixp[2]->fx_where = prev_insn_where;
2575                     }
2576                 }
2577
2578               /* Update the previous insn information; leave prev_insn
2579                  unchanged.  */
2580               prev_prev_insn = *ip;
2581             }
2582           prev_insn_is_delay_slot = 1;
2583
2584           /* If that was an unconditional branch, forget the previous
2585              insn information.  */
2586           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2587             {
2588               prev_prev_insn.insn_mo = &dummy_opcode;
2589               prev_insn.insn_mo = &dummy_opcode;
2590             }
2591
2592           prev_insn_fixp[0] = NULL;
2593           prev_insn_fixp[1] = NULL;
2594           prev_insn_fixp[2] = NULL;
2595           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2596           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2597           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2598           prev_insn_extended = 0;
2599         }
2600       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2601         {
2602           /* We don't yet optimize a branch likely.  What we should do
2603              is look at the target, copy the instruction found there
2604              into the delay slot, and increment the branch to jump to
2605              the next instruction.  */
2606           emit_nop ();
2607           /* Update the previous insn information.  */
2608           prev_prev_insn = *ip;
2609           prev_insn.insn_mo = &dummy_opcode;
2610           prev_insn_fixp[0] = NULL;
2611           prev_insn_fixp[1] = NULL;
2612           prev_insn_fixp[2] = NULL;
2613           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2614           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2615           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2616           prev_insn_extended = 0;
2617         }
2618       else
2619         {
2620           /* Update the previous insn information.  */
2621           if (nops > 0)
2622             prev_prev_insn.insn_mo = &dummy_opcode;
2623           else
2624             prev_prev_insn = prev_insn;
2625           prev_insn = *ip;
2626
2627           /* Any time we see a branch, we always fill the delay slot
2628              immediately; since this insn is not a branch, we know it
2629              is not in a delay slot.  */
2630           prev_insn_is_delay_slot = 0;
2631
2632           prev_insn_fixp[0] = fixp[0];
2633           prev_insn_fixp[1] = fixp[1];
2634           prev_insn_fixp[2] = fixp[2];
2635           prev_insn_reloc_type[0] = reloc_type[0];
2636           prev_insn_reloc_type[1] = reloc_type[1];
2637           prev_insn_reloc_type[2] = reloc_type[2];
2638           if (mips_opts.mips16)
2639             prev_insn_extended = (ip->use_extend
2640                                   || *reloc_type > BFD_RELOC_UNUSED);
2641         }
2642
2643       prev_prev_insn_unreordered = prev_insn_unreordered;
2644       prev_insn_unreordered = 0;
2645       prev_insn_frag = frag_now;
2646       prev_insn_where = f - frag_now->fr_literal;
2647       prev_insn_valid = 1;
2648     }
2649   else if (place == NULL)
2650     {
2651       /* We need to record a bit of information even when we are not
2652          reordering, in order to determine the base address for mips16
2653          PC relative relocs.  */
2654       prev_prev_insn = prev_insn;
2655       prev_insn = *ip;
2656       prev_insn_reloc_type[0] = reloc_type[0];
2657       prev_insn_reloc_type[1] = reloc_type[1];
2658       prev_insn_reloc_type[2] = reloc_type[2];
2659       prev_prev_insn_unreordered = prev_insn_unreordered;
2660       prev_insn_unreordered = 1;
2661     }
2662
2663   /* We just output an insn, so the next one doesn't have a label.  */
2664   mips_clear_insn_labels ();
2665
2666   /* We must ensure that a fixup associated with an unmatched %hi
2667      reloc does not become a variant frag.  Otherwise, the
2668      rearrangement of %hi relocs in frob_file may confuse
2669      tc_gen_reloc.  */
2670   if (unmatched_hi)
2671     {
2672       frag_wane (frag_now);
2673       frag_new (0);
2674     }
2675 }
2676
2677 /* This function forgets that there was any previous instruction or
2678    label.  If PRESERVE is non-zero, it remembers enough information to
2679    know whether nops are needed before a noreorder section.  */
2680
2681 static void
2682 mips_no_prev_insn (preserve)
2683      int preserve;
2684 {
2685   if (! preserve)
2686     {
2687       prev_insn.insn_mo = &dummy_opcode;
2688       prev_prev_insn.insn_mo = &dummy_opcode;
2689       prev_nop_frag = NULL;
2690       prev_nop_frag_holds = 0;
2691       prev_nop_frag_required = 0;
2692       prev_nop_frag_since = 0;
2693     }
2694   prev_insn_valid = 0;
2695   prev_insn_is_delay_slot = 0;
2696   prev_insn_unreordered = 0;
2697   prev_insn_extended = 0;
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_prev_insn_unreordered = 0;
2702   mips_clear_insn_labels ();
2703 }
2704
2705 /* This function must be called whenever we turn on noreorder or emit
2706    something other than instructions.  It inserts any NOPS which might
2707    be needed by the previous instruction, and clears the information
2708    kept for the previous instructions.  The INSNS parameter is true if
2709    instructions are to follow.  */
2710
2711 static void
2712 mips_emit_delays (insns)
2713      boolean insns;
2714 {
2715   if (! mips_opts.noreorder)
2716     {
2717       int nops;
2718
2719       nops = 0;
2720       if ((! mips_opts.mips16
2721            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2722            && (! cop_interlocks
2723                && (prev_insn.insn_mo->pinfo
2724                    & (INSN_LOAD_COPROC_DELAY
2725                       | INSN_COPROC_MOVE_DELAY
2726                       | INSN_WRITE_COND_CODE))))
2727           || (! hilo_interlocks
2728               && (prev_insn.insn_mo->pinfo
2729                   & (INSN_READ_LO
2730                      | INSN_READ_HI)))
2731           || (! mips_opts.mips16
2732               && ! gpr_interlocks
2733               && (prev_insn.insn_mo->pinfo
2734                   & INSN_LOAD_MEMORY_DELAY))
2735           || (! mips_opts.mips16
2736               && mips_opts.isa == ISA_MIPS1
2737               && (prev_insn.insn_mo->pinfo
2738                   & INSN_COPROC_MEMORY_DELAY)))
2739         {
2740           /* Itbl support may require additional care here.  */
2741           ++nops;
2742           if ((! mips_opts.mips16
2743                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2744                && (! cop_interlocks
2745                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2746               || (! hilo_interlocks
2747                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2748                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2749             ++nops;
2750
2751           if (prev_insn_unreordered)
2752             nops = 0;
2753         }
2754       else if ((! mips_opts.mips16
2755                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2756                 && (! cop_interlocks
2757                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2758                || (! hilo_interlocks
2759                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2760                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2761         {
2762           /* Itbl support may require additional care here.  */
2763           if (! prev_prev_insn_unreordered)
2764             ++nops;
2765         }
2766
2767       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2768         {
2769           int min_nops = 0;
2770           const char *pn = prev_insn.insn_mo->name;
2771           if (strncmp(pn, "macc", 4) == 0
2772               || strncmp(pn, "dmacc", 5) == 0
2773               || strncmp(pn, "dmult", 5) == 0)
2774             {
2775               min_nops = 1;
2776             }
2777           if (nops < min_nops)
2778             nops = min_nops;
2779         }
2780
2781       if (nops > 0)
2782         {
2783           struct insn_label_list *l;
2784
2785           if (insns)
2786             {
2787               /* Record the frag which holds the nop instructions, so
2788                  that we can remove them if we don't need them.  */
2789               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2790               prev_nop_frag = frag_now;
2791               prev_nop_frag_holds = nops;
2792               prev_nop_frag_required = 0;
2793               prev_nop_frag_since = 0;
2794             }
2795
2796           for (; nops > 0; --nops)
2797             emit_nop ();
2798
2799           if (insns)
2800             {
2801               /* Move on to a new frag, so that it is safe to simply
2802                  decrease the size of prev_nop_frag.  */
2803               frag_wane (frag_now);
2804               frag_new (0);
2805             }
2806
2807           for (l = insn_labels; l != NULL; l = l->next)
2808             {
2809               valueT val;
2810
2811               assert (S_GET_SEGMENT (l->label) == now_seg);
2812               symbol_set_frag (l->label, frag_now);
2813               val = (valueT) frag_now_fix ();
2814               /* mips16 text labels are stored as odd.  */
2815               if (mips_opts.mips16)
2816                 ++val;
2817               S_SET_VALUE (l->label, val);
2818             }
2819         }
2820     }
2821
2822   /* Mark instruction labels in mips16 mode.  */
2823   if (insns)
2824     mips16_mark_labels ();
2825
2826   mips_no_prev_insn (insns);
2827 }
2828
2829 /* Build an instruction created by a macro expansion.  This is passed
2830    a pointer to the count of instructions created so far, an
2831    expression, the name of the instruction to build, an operand format
2832    string, and corresponding arguments.  */
2833
2834 #ifdef USE_STDARG
2835 static void
2836 macro_build (char *place,
2837              int *counter,
2838              expressionS * ep,
2839              const char *name,
2840              const char *fmt,
2841              ...)
2842 #else
2843 static void
2844 macro_build (place, counter, ep, name, fmt, va_alist)
2845      char *place;
2846      int *counter;
2847      expressionS *ep;
2848      const char *name;
2849      const char *fmt;
2850      va_dcl
2851 #endif
2852 {
2853   struct mips_cl_insn insn;
2854   bfd_reloc_code_real_type r[3];
2855   va_list args;
2856
2857 #ifdef USE_STDARG
2858   va_start (args, fmt);
2859 #else
2860   va_start (args);
2861 #endif
2862
2863   /*
2864    * If the macro is about to expand into a second instruction,
2865    * print a warning if needed. We need to pass ip as a parameter
2866    * to generate a better warning message here...
2867    */
2868   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2869     as_warn (_("Macro instruction expanded into multiple instructions"));
2870
2871   /*
2872    * If the macro is about to expand into a second instruction,
2873    * and it is in a delay slot, print a warning.
2874    */
2875   if (place == NULL
2876       && *counter == 1
2877       && mips_opts.noreorder
2878       && (prev_prev_insn.insn_mo->pinfo
2879           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2880              | INSN_COND_BRANCH_LIKELY)) != 0)
2881     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2882
2883   if (place == NULL)
2884     ++*counter;         /* bump instruction counter */
2885
2886   if (mips_opts.mips16)
2887     {
2888       mips16_macro_build (place, counter, ep, name, fmt, args);
2889       va_end (args);
2890       return;
2891     }
2892
2893   r[0] = BFD_RELOC_UNUSED;
2894   r[1] = BFD_RELOC_UNUSED;
2895   r[2] = BFD_RELOC_UNUSED;
2896   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2897   assert (insn.insn_mo);
2898   assert (strcmp (name, insn.insn_mo->name) == 0);
2899
2900   /* Search until we get a match for NAME.  */
2901   while (1)
2902     {
2903       /* It is assumed here that macros will never generate 
2904          MDMX or MIPS-3D instructions.  */
2905       if (strcmp (fmt, insn.insn_mo->args) == 0
2906           && insn.insn_mo->pinfo != INSN_MACRO
2907           && OPCODE_IS_MEMBER (insn.insn_mo,
2908                                (mips_opts.isa
2909                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2910                                mips_arch)
2911           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2912         break;
2913
2914       ++insn.insn_mo;
2915       assert (insn.insn_mo->name);
2916       assert (strcmp (name, insn.insn_mo->name) == 0);
2917     }
2918
2919   insn.insn_opcode = insn.insn_mo->match;
2920   for (;;)
2921     {
2922       switch (*fmt++)
2923         {
2924         case '\0':
2925           break;
2926
2927         case ',':
2928         case '(':
2929         case ')':
2930           continue;
2931
2932         case 't':
2933         case 'w':
2934         case 'E':
2935           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2936           continue;
2937
2938         case 'c':
2939           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2940           continue;
2941
2942         case 'T':
2943         case 'W':
2944           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2945           continue;
2946
2947         case 'd':
2948         case 'G':
2949           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2950           continue;
2951
2952         case 'U':
2953           {
2954             int tmp = va_arg (args, int);
2955
2956             insn.insn_opcode |= tmp << OP_SH_RT;
2957             insn.insn_opcode |= tmp << OP_SH_RD;
2958             continue;
2959           }
2960
2961         case 'V':
2962         case 'S':
2963           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2964           continue;
2965
2966         case 'z':
2967           continue;
2968
2969         case '<':
2970           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2971           continue;
2972
2973         case 'D':
2974           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2975           continue;
2976
2977         case 'B':
2978           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2979           continue;
2980
2981         case 'J':
2982           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2983           continue;
2984
2985         case 'q':
2986           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2987           continue;
2988
2989         case 'b':
2990         case 's':
2991         case 'r':
2992         case 'v':
2993           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2994           continue;
2995
2996         case 'i':
2997         case 'j':
2998         case 'o':
2999           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3000           assert (*r == BFD_RELOC_GPREL16
3001                   || *r == BFD_RELOC_MIPS_LITERAL
3002                   || *r == BFD_RELOC_MIPS_HIGHER
3003                   || *r == BFD_RELOC_HI16_S
3004                   || *r == BFD_RELOC_LO16
3005                   || *r == BFD_RELOC_MIPS_GOT16
3006                   || *r == BFD_RELOC_MIPS_CALL16
3007                   || *r == BFD_RELOC_MIPS_GOT_DISP
3008                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3009                   || *r == BFD_RELOC_MIPS_GOT_OFST
3010                   || *r == BFD_RELOC_MIPS_GOT_LO16
3011                   || *r == BFD_RELOC_MIPS_CALL_LO16
3012                   || (ep->X_op == O_subtract
3013                       && *r == BFD_RELOC_PCREL_LO16));
3014           continue;
3015
3016         case 'u':
3017           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3018           assert (ep != NULL
3019                   && (ep->X_op == O_constant
3020                       || (ep->X_op == O_symbol
3021                           && (*r == BFD_RELOC_MIPS_HIGHEST
3022                               || *r == BFD_RELOC_HI16_S
3023                               || *r == BFD_RELOC_HI16
3024                               || *r == BFD_RELOC_GPREL16
3025                               || *r == BFD_RELOC_MIPS_GOT_HI16
3026                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3027                       || (ep->X_op == O_subtract
3028                           && *r == BFD_RELOC_PCREL_HI16_S)));
3029           continue;
3030
3031         case 'p':
3032           assert (ep != NULL);
3033           /*
3034            * This allows macro() to pass an immediate expression for
3035            * creating short branches without creating a symbol.
3036            * Note that the expression still might come from the assembly
3037            * input, in which case the value is not checked for range nor
3038            * is a relocation entry generated (yuck).
3039            */
3040           if (ep->X_op == O_constant)
3041             {
3042               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3043               ep = NULL;
3044             }
3045           else
3046             if (mips_pic == EMBEDDED_PIC)
3047               *r = BFD_RELOC_16_PCREL_S2;
3048             else
3049               *r = BFD_RELOC_16_PCREL;
3050           continue;
3051
3052         case 'a':
3053           assert (ep != NULL);
3054           *r = BFD_RELOC_MIPS_JMP;
3055           continue;
3056
3057         case 'C':
3058           insn.insn_opcode |= va_arg (args, unsigned long);
3059           continue;
3060
3061         default:
3062           internalError ();
3063         }
3064       break;
3065     }
3066   va_end (args);
3067   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3068
3069   append_insn (place, &insn, ep, r, false);
3070 }
3071
3072 static void
3073 mips16_macro_build (place, counter, ep, name, fmt, args)
3074      char *place;
3075      int *counter ATTRIBUTE_UNUSED;
3076      expressionS *ep;
3077      const char *name;
3078      const char *fmt;
3079      va_list args;
3080 {
3081   struct mips_cl_insn insn;
3082   bfd_reloc_code_real_type r[3]
3083     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3084
3085   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3086   assert (insn.insn_mo);
3087   assert (strcmp (name, insn.insn_mo->name) == 0);
3088
3089   while (strcmp (fmt, insn.insn_mo->args) != 0
3090          || insn.insn_mo->pinfo == INSN_MACRO)
3091     {
3092       ++insn.insn_mo;
3093       assert (insn.insn_mo->name);
3094       assert (strcmp (name, insn.insn_mo->name) == 0);
3095     }
3096
3097   insn.insn_opcode = insn.insn_mo->match;
3098   insn.use_extend = false;
3099
3100   for (;;)
3101     {
3102       int c;
3103
3104       c = *fmt++;
3105       switch (c)
3106         {
3107         case '\0':
3108           break;
3109
3110         case ',':
3111         case '(':
3112         case ')':
3113           continue;
3114
3115         case 'y':
3116         case 'w':
3117           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3118           continue;
3119
3120         case 'x':
3121         case 'v':
3122           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3123           continue;
3124
3125         case 'z':
3126           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3127           continue;
3128
3129         case 'Z':
3130           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3131           continue;
3132
3133         case '0':
3134         case 'S':
3135         case 'P':
3136         case 'R':
3137           continue;
3138
3139         case 'X':
3140           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3141           continue;
3142
3143         case 'Y':
3144           {
3145             int regno;
3146
3147             regno = va_arg (args, int);
3148             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3149             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3150           }
3151           continue;
3152
3153         case '<':
3154         case '>':
3155         case '4':
3156         case '5':
3157         case 'H':
3158         case 'W':
3159         case 'D':
3160         case 'j':
3161         case '8':
3162         case 'V':
3163         case 'C':
3164         case 'U':
3165         case 'k':
3166         case 'K':
3167         case 'p':
3168         case 'q':
3169           {
3170             assert (ep != NULL);
3171
3172             if (ep->X_op != O_constant)
3173               *r = (int) BFD_RELOC_UNUSED + c;
3174             else
3175               {
3176                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3177                               false, &insn.insn_opcode, &insn.use_extend,
3178                               &insn.extend);
3179                 ep = NULL;
3180                 *r = BFD_RELOC_UNUSED;
3181               }
3182           }
3183           continue;
3184
3185         case '6':
3186           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3187           continue;
3188         }
3189
3190       break;
3191     }
3192
3193   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3194
3195   append_insn (place, &insn, ep, r, false);
3196 }
3197
3198 /*
3199  * Generate a "jalr" instruction with a relocation hint to the called
3200  * function.  This occurs in NewABI PIC code.
3201  */
3202 static void
3203 macro_build_jalr (icnt, ep)
3204      int icnt;
3205      expressionS *ep;
3206 {
3207   char *f;
3208   
3209   if (HAVE_NEWABI)
3210     {
3211       frag_grow (4);
3212       f = frag_more (0);
3213     }
3214   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3215                RA, PIC_CALL_REG);
3216   if (HAVE_NEWABI)
3217     fix_new_exp (frag_now, f - frag_now->fr_literal,
3218                  0, ep, false, BFD_RELOC_MIPS_JALR);
3219 }
3220
3221 /*
3222  * Generate a "lui" instruction.
3223  */
3224 static void
3225 macro_build_lui (place, counter, ep, regnum)
3226      char *place;
3227      int *counter;
3228      expressionS *ep;
3229      int regnum;
3230 {
3231   expressionS high_expr;
3232   struct mips_cl_insn insn;
3233   bfd_reloc_code_real_type r[3]
3234     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3235   const char *name = "lui";
3236   const char *fmt = "t,u";
3237
3238   assert (! mips_opts.mips16);
3239
3240   if (place == NULL)
3241     high_expr = *ep;
3242   else
3243     {
3244       high_expr.X_op = O_constant;
3245       high_expr.X_add_number = ep->X_add_number;
3246     }
3247
3248   if (high_expr.X_op == O_constant)
3249     {
3250       /* we can compute the instruction now without a relocation entry */
3251       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3252                                 >> 16) & 0xffff;
3253       *r = BFD_RELOC_UNUSED;
3254     }
3255   else if (! HAVE_NEWABI)
3256     {
3257       assert (ep->X_op == O_symbol);
3258       /* _gp_disp is a special case, used from s_cpload.  */
3259       assert (mips_pic == NO_PIC
3260               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3261       *r = BFD_RELOC_HI16_S;
3262     }
3263
3264   /*
3265    * If the macro is about to expand into a second instruction,
3266    * print a warning if needed. We need to pass ip as a parameter
3267    * to generate a better warning message here...
3268    */
3269   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3270     as_warn (_("Macro instruction expanded into multiple instructions"));
3271
3272   if (place == NULL)
3273     ++*counter;         /* bump instruction counter */
3274
3275   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3276   assert (insn.insn_mo);
3277   assert (strcmp (name, insn.insn_mo->name) == 0);
3278   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3279
3280   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3281   if (*r == BFD_RELOC_UNUSED)
3282     {
3283       insn.insn_opcode |= high_expr.X_add_number;
3284       append_insn (place, &insn, NULL, r, false);
3285     }
3286   else
3287     append_insn (place, &insn, &high_expr, r, false);
3288 }
3289
3290 /* Generate a sequence of instructions to do a load or store from a constant
3291    offset off of a base register (breg) into/from a target register (treg),
3292    using AT if necessary.  */
3293 static void
3294 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3295      char *place;
3296      int *counter;
3297      expressionS *ep;
3298      const char *op;
3299      int treg, breg;
3300 {
3301   assert (ep->X_op == O_constant);
3302
3303   /* Right now, this routine can only handle signed 32-bit contants.  */
3304   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3305     as_warn (_("operand overflow"));
3306
3307   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3308     {
3309       /* Signed 16-bit offset will fit in the op.  Easy!  */
3310       macro_build (place, counter, ep, op, "t,o(b)", treg,
3311                    (int) BFD_RELOC_LO16, breg);
3312     }
3313   else
3314     {
3315       /* 32-bit offset, need multiple instructions and AT, like:
3316            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3317            addu     $tempreg,$tempreg,$breg
3318            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3319          to handle the complete offset.  */
3320       macro_build_lui (place, counter, ep, AT);
3321       if (place != NULL)
3322         place += 4;
3323       macro_build (place, counter, (expressionS *) NULL,
3324                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3325                    "d,v,t", AT, AT, breg);
3326       if (place != NULL)
3327         place += 4;
3328       macro_build (place, counter, ep, op, "t,o(b)", treg,
3329                    (int) BFD_RELOC_LO16, AT);
3330
3331       if (mips_opts.noat)
3332         as_warn (_("Macro used $at after \".set noat\""));
3333     }
3334 }
3335
3336 /*                      set_at()
3337  * Generates code to set the $at register to true (one)
3338  * if reg is less than the immediate expression.
3339  */
3340 static void
3341 set_at (counter, reg, unsignedp)
3342      int *counter;
3343      int reg;
3344      int unsignedp;
3345 {
3346   if (imm_expr.X_op == O_constant
3347       && imm_expr.X_add_number >= -0x8000
3348       && imm_expr.X_add_number < 0x8000)
3349     macro_build ((char *) NULL, counter, &imm_expr,
3350                  unsignedp ? "sltiu" : "slti",
3351                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3352   else
3353     {
3354       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3355       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3356                    unsignedp ? "sltu" : "slt",
3357                    "d,v,t", AT, reg, AT);
3358     }
3359 }
3360
3361 /* Warn if an expression is not a constant.  */
3362
3363 static void
3364 check_absolute_expr (ip, ex)
3365      struct mips_cl_insn *ip;
3366      expressionS *ex;
3367 {
3368   if (ex->X_op == O_big)
3369     as_bad (_("unsupported large constant"));
3370   else if (ex->X_op != O_constant)
3371     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3372 }
3373
3374 /* Count the leading zeroes by performing a binary chop. This is a
3375    bulky bit of source, but performance is a LOT better for the
3376    majority of values than a simple loop to count the bits:
3377        for (lcnt = 0; (lcnt < 32); lcnt++)
3378          if ((v) & (1 << (31 - lcnt)))
3379            break;
3380   However it is not code size friendly, and the gain will drop a bit
3381   on certain cached systems.
3382 */
3383 #define COUNT_TOP_ZEROES(v)             \
3384   (((v) & ~0xffff) == 0                 \
3385    ? ((v) & ~0xff) == 0                 \
3386      ? ((v) & ~0xf) == 0                \
3387        ? ((v) & ~0x3) == 0              \
3388          ? ((v) & ~0x1) == 0            \
3389            ? !(v)                       \
3390              ? 32                       \
3391              : 31                       \
3392            : 30                         \
3393          : ((v) & ~0x7) == 0            \
3394            ? 29                         \
3395            : 28                         \
3396        : ((v) & ~0x3f) == 0             \
3397          ? ((v) & ~0x1f) == 0           \
3398            ? 27                         \
3399            : 26                         \
3400          : ((v) & ~0x7f) == 0           \
3401            ? 25                         \
3402            : 24                         \
3403      : ((v) & ~0xfff) == 0              \
3404        ? ((v) & ~0x3ff) == 0            \
3405          ? ((v) & ~0x1ff) == 0          \
3406            ? 23                         \
3407            : 22                         \
3408          : ((v) & ~0x7ff) == 0          \
3409            ? 21                         \
3410            : 20                         \
3411        : ((v) & ~0x3fff) == 0           \
3412          ? ((v) & ~0x1fff) == 0         \
3413            ? 19                         \
3414            : 18                         \
3415          : ((v) & ~0x7fff) == 0         \
3416            ? 17                         \
3417            : 16                         \
3418    : ((v) & ~0xffffff) == 0             \
3419      ? ((v) & ~0xfffff) == 0            \
3420        ? ((v) & ~0x3ffff) == 0          \
3421          ? ((v) & ~0x1ffff) == 0        \
3422            ? 15                         \
3423            : 14                         \
3424          : ((v) & ~0x7ffff) == 0        \
3425            ? 13                         \
3426            : 12                         \
3427        : ((v) & ~0x3fffff) == 0         \
3428          ? ((v) & ~0x1fffff) == 0       \
3429            ? 11                         \
3430            : 10                         \
3431          : ((v) & ~0x7fffff) == 0       \
3432            ? 9                          \
3433            : 8                          \
3434      : ((v) & ~0xfffffff) == 0          \
3435        ? ((v) & ~0x3ffffff) == 0        \
3436          ? ((v) & ~0x1ffffff) == 0      \
3437            ? 7                          \
3438            : 6                          \
3439          : ((v) & ~0x7ffffff) == 0      \
3440            ? 5                          \
3441            : 4                          \
3442        : ((v) & ~0x3fffffff) == 0       \
3443          ? ((v) & ~0x1fffffff) == 0     \
3444            ? 3                          \
3445            : 2                          \
3446          : ((v) & ~0x7fffffff) == 0     \
3447            ? 1                          \
3448            : 0)
3449
3450 /*                      load_register()
3451  *  This routine generates the least number of instructions neccessary to load
3452  *  an absolute expression value into a register.
3453  */
3454 static void
3455 load_register (counter, reg, ep, dbl)
3456      int *counter;
3457      int reg;
3458      expressionS *ep;
3459      int dbl;
3460 {
3461   int freg;
3462   expressionS hi32, lo32;
3463
3464   if (ep->X_op != O_big)
3465     {
3466       assert (ep->X_op == O_constant);
3467       if (ep->X_add_number < 0x8000
3468           && (ep->X_add_number >= 0
3469               || (ep->X_add_number >= -0x8000
3470                   && (! dbl
3471                       || ! ep->X_unsigned
3472                       || sizeof (ep->X_add_number) > 4))))
3473         {
3474           /* We can handle 16 bit signed values with an addiu to
3475              $zero.  No need to ever use daddiu here, since $zero and
3476              the result are always correct in 32 bit mode.  */
3477           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3478                        (int) BFD_RELOC_LO16);
3479           return;
3480         }
3481       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3482         {
3483           /* We can handle 16 bit unsigned values with an ori to
3484              $zero.  */
3485           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3486                        (int) BFD_RELOC_LO16);
3487           return;
3488         }
3489       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3490                 && (! dbl
3491                     || ! ep->X_unsigned
3492                     || sizeof (ep->X_add_number) > 4
3493                     || (ep->X_add_number & 0x80000000) == 0))
3494                || ((HAVE_32BIT_GPRS || ! dbl)
3495                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3496                || (HAVE_32BIT_GPRS
3497                    && ! dbl
3498                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3499                        == ~ (offsetT) 0xffffffff)))
3500         {
3501           /* 32 bit values require an lui.  */
3502           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3503                        (int) BFD_RELOC_HI16);
3504           if ((ep->X_add_number & 0xffff) != 0)
3505             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3506                          (int) BFD_RELOC_LO16);
3507           return;
3508         }
3509     }
3510
3511   /* The value is larger than 32 bits.  */
3512
3513   if (HAVE_32BIT_GPRS)
3514     {
3515       as_bad (_("Number (0x%lx) larger than 32 bits"),
3516               (unsigned long) ep->X_add_number);
3517       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3518                    (int) BFD_RELOC_LO16);
3519       return;
3520     }
3521
3522   if (ep->X_op != O_big)
3523     {
3524       hi32 = *ep;
3525       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3526       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3527       hi32.X_add_number &= 0xffffffff;
3528       lo32 = *ep;
3529       lo32.X_add_number &= 0xffffffff;
3530     }
3531   else
3532     {
3533       assert (ep->X_add_number > 2);
3534       if (ep->X_add_number == 3)
3535         generic_bignum[3] = 0;
3536       else if (ep->X_add_number > 4)
3537         as_bad (_("Number larger than 64 bits"));
3538       lo32.X_op = O_constant;
3539       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3540       hi32.X_op = O_constant;
3541       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3542     }
3543
3544   if (hi32.X_add_number == 0)
3545     freg = 0;
3546   else
3547     {
3548       int shift, bit;
3549       unsigned long hi, lo;
3550
3551       if (hi32.X_add_number == (offsetT) 0xffffffff)
3552         {
3553           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3554             {
3555               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3556                            reg, 0, (int) BFD_RELOC_LO16);
3557               return;
3558             }
3559           if (lo32.X_add_number & 0x80000000)
3560             {
3561               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3562                            (int) BFD_RELOC_HI16);
3563               if (lo32.X_add_number & 0xffff)
3564                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3565                              reg, reg, (int) BFD_RELOC_LO16);
3566               return;
3567             }
3568         }
3569
3570       /* Check for 16bit shifted constant.  We know that hi32 is
3571          non-zero, so start the mask on the first bit of the hi32
3572          value.  */
3573       shift = 17;
3574       do
3575         {
3576           unsigned long himask, lomask;
3577
3578           if (shift < 32)
3579             {
3580               himask = 0xffff >> (32 - shift);
3581               lomask = (0xffff << shift) & 0xffffffff;
3582             }
3583           else
3584             {
3585               himask = 0xffff << (shift - 32);
3586               lomask = 0;
3587             }
3588           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3589               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3590             {
3591               expressionS tmp;
3592
3593               tmp.X_op = O_constant;
3594               if (shift < 32)
3595                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3596                                     | (lo32.X_add_number >> shift));
3597               else
3598                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3599               macro_build ((char *) NULL, counter, &tmp,
3600                            "ori", "t,r,i", reg, 0,
3601                            (int) BFD_RELOC_LO16);
3602               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3603                            (shift >= 32) ? "dsll32" : "dsll",
3604                            "d,w,<", reg, reg,
3605                            (shift >= 32) ? shift - 32 : shift);
3606               return;
3607             }
3608           ++shift;
3609         }
3610       while (shift <= (64 - 16));
3611
3612       /* Find the bit number of the lowest one bit, and store the
3613          shifted value in hi/lo.  */
3614       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3615       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3616       if (lo != 0)
3617         {
3618           bit = 0;
3619           while ((lo & 1) == 0)
3620             {
3621               lo >>= 1;
3622               ++bit;
3623             }
3624           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3625           hi >>= bit;
3626         }
3627       else
3628         {
3629           bit = 32;
3630           while ((hi & 1) == 0)
3631             {
3632               hi >>= 1;
3633               ++bit;
3634             }
3635           lo = hi;
3636           hi = 0;
3637         }
3638
3639       /* Optimize if the shifted value is a (power of 2) - 1.  */
3640       if ((hi == 0 && ((lo + 1) & lo) == 0)
3641           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3642         {
3643           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3644           if (shift != 0)
3645             {
3646               expressionS tmp;
3647
3648               /* This instruction will set the register to be all
3649                  ones.  */
3650               tmp.X_op = O_constant;
3651               tmp.X_add_number = (offsetT) -1;
3652               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3653                            reg, 0, (int) BFD_RELOC_LO16);
3654               if (bit != 0)
3655                 {
3656                   bit += shift;
3657                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3658                                (bit >= 32) ? "dsll32" : "dsll",
3659                                "d,w,<", reg, reg,
3660                                (bit >= 32) ? bit - 32 : bit);
3661                 }
3662               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3663                            (shift >= 32) ? "dsrl32" : "dsrl",
3664                            "d,w,<", reg, reg,
3665                            (shift >= 32) ? shift - 32 : shift);
3666               return;
3667             }
3668         }
3669
3670       /* Sign extend hi32 before calling load_register, because we can
3671          generally get better code when we load a sign extended value.  */
3672       if ((hi32.X_add_number & 0x80000000) != 0)
3673         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3674       load_register (counter, reg, &hi32, 0);
3675       freg = reg;
3676     }
3677   if ((lo32.X_add_number & 0xffff0000) == 0)
3678     {
3679       if (freg != 0)
3680         {
3681           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3682                        "dsll32", "d,w,<", reg, freg, 0);
3683           freg = reg;
3684         }
3685     }
3686   else
3687     {
3688       expressionS mid16;
3689
3690       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3691         {
3692           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3693                        (int) BFD_RELOC_HI16);
3694           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3695                        "dsrl32", "d,w,<", reg, reg, 0);
3696           return;
3697         }
3698
3699       if (freg != 0)
3700         {
3701           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3702                        "d,w,<", reg, freg, 16);
3703           freg = reg;
3704         }
3705       mid16 = lo32;
3706       mid16.X_add_number >>= 16;
3707       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3708                    freg, (int) BFD_RELOC_LO16);
3709       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3710                    "d,w,<", reg, reg, 16);
3711       freg = reg;
3712     }
3713   if ((lo32.X_add_number & 0xffff) != 0)
3714     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3715                  (int) BFD_RELOC_LO16);
3716 }
3717
3718 /* Load an address into a register.  */
3719
3720 static void
3721 load_address (counter, reg, ep, used_at)
3722      int *counter;
3723      int reg;
3724      expressionS *ep;
3725      int *used_at;
3726 {
3727   char *p = NULL;
3728
3729   if (ep->X_op != O_constant
3730       && ep->X_op != O_symbol)
3731     {
3732       as_bad (_("expression too complex"));
3733       ep->X_op = O_constant;
3734     }
3735
3736   if (ep->X_op == O_constant)
3737     {
3738       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3739       return;
3740     }
3741
3742   if (mips_pic == NO_PIC)
3743     {
3744       /* If this is a reference to a GP relative symbol, we want
3745            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3746          Otherwise we want
3747            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3748            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3749          If we have an addend, we always use the latter form.
3750
3751          With 64bit address space and a usable $at we want
3752            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3753            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3754            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3755            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3756            dsll32       $reg,0
3757            daddu        $reg,$reg,$at
3758
3759          If $at is already in use, we use an path which is suboptimal
3760          on superscalar processors.
3761            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3762            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3763            dsll         $reg,16
3764            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3765            dsll         $reg,16
3766            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3767        */
3768       if (HAVE_64BIT_ADDRESSES)
3769         {
3770           /* We don't do GP optimization for now because RELAX_ENCODE can't
3771              hold the data for such large chunks.  */
3772
3773           if (*used_at == 0 && ! mips_opts.noat)
3774             {
3775               macro_build (p, counter, ep, "lui", "t,u",
3776                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3777               macro_build (p, counter, ep, "lui", "t,u",
3778                            AT, (int) BFD_RELOC_HI16_S);
3779               macro_build (p, counter, ep, "daddiu", "t,r,j",
3780                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3781               macro_build (p, counter, ep, "daddiu", "t,r,j",
3782                            AT, AT, (int) BFD_RELOC_LO16);
3783               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3784                            "d,w,<", reg, reg, 0);
3785               macro_build (p, counter, (expressionS *) NULL, "daddu",
3786                            "d,v,t", reg, reg, AT);
3787               *used_at = 1;
3788             }
3789           else
3790             {
3791               macro_build (p, counter, ep, "lui", "t,u",
3792                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3793               macro_build (p, counter, ep, "daddiu", "t,r,j",
3794                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3795               macro_build (p, counter, (expressionS *) NULL, "dsll",
3796                            "d,w,<", reg, reg, 16);
3797               macro_build (p, counter, ep, "daddiu", "t,r,j",
3798                            reg, reg, (int) BFD_RELOC_HI16_S);
3799               macro_build (p, counter, (expressionS *) NULL, "dsll",
3800                            "d,w,<", reg, reg, 16);
3801               macro_build (p, counter, ep, "daddiu", "t,r,j",
3802                            reg, reg, (int) BFD_RELOC_LO16);
3803             }
3804         }
3805       else
3806         {
3807           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3808               && ! nopic_need_relax (ep->X_add_symbol, 1))
3809             {
3810               frag_grow (20);
3811               macro_build ((char *) NULL, counter, ep,
3812                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3813                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3814               p = frag_var (rs_machine_dependent, 8, 0,
3815                             RELAX_ENCODE (4, 8, 0, 4, 0,
3816                                           mips_opts.warn_about_macros),
3817                             ep->X_add_symbol, 0, NULL);
3818             }
3819           macro_build_lui (p, counter, ep, reg);
3820           if (p != NULL)
3821             p += 4;
3822           macro_build (p, counter, ep,
3823                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3824                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3825         }
3826     }
3827   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3828     {
3829       expressionS ex;
3830
3831       /* If this is a reference to an external symbol, we want
3832            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3833          Otherwise we want
3834            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3835            nop
3836            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3837          If we have NewABI, we want
3838            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3839          If there is a constant, it must be added in after.  */
3840       ex.X_add_number = ep->X_add_number;
3841       ep->X_add_number = 0;
3842       frag_grow (20);
3843       if (HAVE_NEWABI)
3844         {
3845           macro_build ((char *) NULL, counter, ep,
3846                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3847                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3848         }
3849       else
3850         {
3851           macro_build ((char *) NULL, counter, ep,
3852                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3853                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3854           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3855           p = frag_var (rs_machine_dependent, 4, 0,
3856                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3857                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3858           macro_build (p, counter, ep,
3859                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3860                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3861         }
3862
3863       if (ex.X_add_number != 0)
3864         {
3865           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3866             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3867           ex.X_op = O_constant;
3868           macro_build ((char *) NULL, counter, &ex,
3869                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3870                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3871         }
3872     }
3873   else if (mips_pic == SVR4_PIC)
3874     {
3875       expressionS ex;
3876       int off;
3877
3878       /* This is the large GOT case.  If this is a reference to an
3879          external symbol, we want
3880            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3881            addu         $reg,$reg,$gp
3882            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3883          Otherwise, for a reference to a local symbol, we want
3884            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3885            nop
3886            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3887          If we have NewABI, we want
3888            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3889            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3890          If there is a constant, it must be added in after.  */
3891       ex.X_add_number = ep->X_add_number;
3892       ep->X_add_number = 0;
3893       if (HAVE_NEWABI)
3894         {
3895           macro_build ((char *) NULL, counter, ep,
3896                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3897                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3898           macro_build (p, counter, ep,
3899                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3900                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3901         }
3902       else
3903         {
3904           if (reg_needs_delay (mips_gp_register))
3905             off = 4;
3906           else
3907             off = 0;
3908           frag_grow (32);
3909           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3910                        (int) BFD_RELOC_MIPS_GOT_HI16);
3911           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3912                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3913                        reg, mips_gp_register);
3914           macro_build ((char *) NULL, counter, ep,
3915                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3916                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3917           p = frag_var (rs_machine_dependent, 12 + off, 0,
3918                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3919                                       mips_opts.warn_about_macros),
3920                         ep->X_add_symbol, 0, NULL);
3921           if (off > 0)
3922             {
3923               /* We need a nop before loading from $gp.  This special
3924                  check is required because the lui which starts the main
3925                  instruction stream does not refer to $gp, and so will not
3926                  insert the nop which may be required.  */
3927               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3928                 p += 4;
3929             }
3930           macro_build (p, counter, ep,
3931                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3932                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3933           p += 4;
3934           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3935           p += 4;
3936           macro_build (p, counter, ep,
3937                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3938                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3939         }
3940
3941       if (ex.X_add_number != 0)
3942         {
3943           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3944             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3945           ex.X_op = O_constant;
3946           macro_build ((char *) NULL, counter, &ex,
3947                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3948                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3949         }
3950     }
3951   else if (mips_pic == EMBEDDED_PIC)
3952     {
3953       /* We always do
3954            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3955        */
3956       macro_build ((char *) NULL, counter, ep,
3957                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3958                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3959     }
3960   else
3961     abort ();
3962 }
3963
3964 /* Move the contents of register SOURCE into register DEST.  */
3965
3966 static void
3967 move_register (counter, dest, source)
3968      int *counter;
3969      int dest;
3970      int source;
3971 {
3972   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3973                HAVE_32BIT_GPRS ? "addu" : "daddu",
3974                "d,v,t", dest, source, 0);
3975 }
3976
3977 /*
3978  *                      Build macros
3979  *   This routine implements the seemingly endless macro or synthesized
3980  * instructions and addressing modes in the mips assembly language. Many
3981  * of these macros are simple and are similar to each other. These could
3982  * probably be handled by some kind of table or grammer aproach instead of
3983  * this verbose method. Others are not simple macros but are more like
3984  * optimizing code generation.
3985  *   One interesting optimization is when several store macros appear
3986  * consecutivly that would load AT with the upper half of the same address.
3987  * The ensuing load upper instructions are ommited. This implies some kind
3988  * of global optimization. We currently only optimize within a single macro.
3989  *   For many of the load and store macros if the address is specified as a
3990  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3991  * first load register 'at' with zero and use it as the base register. The
3992  * mips assembler simply uses register $zero. Just one tiny optimization
3993  * we're missing.
3994  */
3995 static void
3996 macro (ip)
3997      struct mips_cl_insn *ip;
3998 {
3999   register int treg, sreg, dreg, breg;
4000   int tempreg;
4001   int mask;
4002   int icnt = 0;
4003   int used_at = 0;
4004   expressionS expr1;
4005   const char *s;
4006   const char *s2;
4007   const char *fmt;
4008   int likely = 0;
4009   int dbl = 0;
4010   int coproc = 0;
4011   int lr = 0;
4012   int imm = 0;
4013   offsetT maxnum;
4014   int off;
4015   bfd_reloc_code_real_type r;
4016   int hold_mips_optimize;
4017
4018   assert (! mips_opts.mips16);
4019
4020   treg = (ip->insn_opcode >> 16) & 0x1f;
4021   dreg = (ip->insn_opcode >> 11) & 0x1f;
4022   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4023   mask = ip->insn_mo->mask;
4024
4025   expr1.X_op = O_constant;
4026   expr1.X_op_symbol = NULL;
4027   expr1.X_add_symbol = NULL;
4028   expr1.X_add_number = 1;
4029
4030   switch (mask)
4031     {
4032     case M_DABS:
4033       dbl = 1;
4034     case M_ABS:
4035       /* bgez $a0,.+12
4036          move v0,$a0
4037          sub v0,$zero,$a0
4038          */
4039
4040       mips_emit_delays (true);
4041       ++mips_opts.noreorder;
4042       mips_any_noreorder = 1;
4043
4044       expr1.X_add_number = 8;
4045       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4046       if (dreg == sreg)
4047         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4048                      0);
4049       else
4050         move_register (&icnt, dreg, sreg);
4051       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4052                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4053
4054       --mips_opts.noreorder;
4055       return;
4056
4057     case M_ADD_I:
4058       s = "addi";
4059       s2 = "add";
4060       goto do_addi;
4061     case M_ADDU_I:
4062       s = "addiu";
4063       s2 = "addu";
4064       goto do_addi;
4065     case M_DADD_I:
4066       dbl = 1;
4067       s = "daddi";
4068       s2 = "dadd";
4069       goto do_addi;
4070     case M_DADDU_I:
4071       dbl = 1;
4072       s = "daddiu";
4073       s2 = "daddu";
4074     do_addi:
4075       if (imm_expr.X_op == O_constant
4076           && imm_expr.X_add_number >= -0x8000
4077           && imm_expr.X_add_number < 0x8000)
4078         {
4079           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4080                        (int) BFD_RELOC_LO16);
4081           return;
4082         }
4083       load_register (&icnt, AT, &imm_expr, dbl);
4084       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4085                    treg, sreg, AT);
4086       break;
4087
4088     case M_AND_I:
4089       s = "andi";
4090       s2 = "and";
4091       goto do_bit;
4092     case M_OR_I:
4093       s = "ori";
4094       s2 = "or";
4095       goto do_bit;
4096     case M_NOR_I:
4097       s = "";
4098       s2 = "nor";
4099       goto do_bit;
4100     case M_XOR_I:
4101       s = "xori";
4102       s2 = "xor";
4103     do_bit:
4104       if (imm_expr.X_op == O_constant
4105           && imm_expr.X_add_number >= 0
4106           && imm_expr.X_add_number < 0x10000)
4107         {
4108           if (mask != M_NOR_I)
4109             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4110                          sreg, (int) BFD_RELOC_LO16);
4111           else
4112             {
4113               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4114                            treg, sreg, (int) BFD_RELOC_LO16);
4115               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4116                            "d,v,t", treg, treg, 0);
4117             }
4118           return;
4119         }
4120
4121       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4122       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4123                    treg, sreg, AT);
4124       break;
4125
4126     case M_BEQ_I:
4127       s = "beq";
4128       goto beq_i;
4129     case M_BEQL_I:
4130       s = "beql";
4131       likely = 1;
4132       goto beq_i;
4133     case M_BNE_I:
4134       s = "bne";
4135       goto beq_i;
4136     case M_BNEL_I:
4137       s = "bnel";
4138       likely = 1;
4139     beq_i:
4140       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4141         {
4142           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4143                        0);
4144           return;
4145         }
4146       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4147       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4148       break;
4149
4150     case M_BGEL:
4151       likely = 1;
4152     case M_BGE:
4153       if (treg == 0)
4154         {
4155           macro_build ((char *) NULL, &icnt, &offset_expr,
4156                        likely ? "bgezl" : "bgez", "s,p", sreg);
4157           return;
4158         }
4159       if (sreg == 0)
4160         {
4161           macro_build ((char *) NULL, &icnt, &offset_expr,
4162                        likely ? "blezl" : "blez", "s,p", treg);
4163           return;
4164         }
4165       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4166                    AT, sreg, treg);
4167       macro_build ((char *) NULL, &icnt, &offset_expr,
4168                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4169       break;
4170
4171     case M_BGTL_I:
4172       likely = 1;
4173     case M_BGT_I:
4174       /* check for > max integer */
4175       maxnum = 0x7fffffff;
4176       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4177         {
4178           maxnum <<= 16;
4179           maxnum |= 0xffff;
4180           maxnum <<= 16;
4181           maxnum |= 0xffff;
4182         }
4183       if (imm_expr.X_op == O_constant
4184           && imm_expr.X_add_number >= maxnum
4185           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4186         {
4187         do_false:
4188           /* result is always false */
4189           if (! likely)
4190             {
4191               if (warn_nops)
4192                 as_warn (_("Branch %s is always false (nop)"),
4193                          ip->insn_mo->name);
4194               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4195                            "", 0);
4196             }
4197           else
4198             {
4199               if (warn_nops)
4200                 as_warn (_("Branch likely %s is always false"),
4201                          ip->insn_mo->name);
4202               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4203                            "s,t,p", 0, 0);
4204             }
4205           return;
4206         }
4207       if (imm_expr.X_op != O_constant)
4208         as_bad (_("Unsupported large constant"));
4209       ++imm_expr.X_add_number;
4210       /* FALLTHROUGH */
4211     case M_BGE_I:
4212     case M_BGEL_I:
4213       if (mask == M_BGEL_I)
4214         likely = 1;
4215       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4216         {
4217           macro_build ((char *) NULL, &icnt, &offset_expr,
4218                        likely ? "bgezl" : "bgez", "s,p", sreg);
4219           return;
4220         }
4221       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4222         {
4223           macro_build ((char *) NULL, &icnt, &offset_expr,
4224                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4225           return;
4226         }
4227       maxnum = 0x7fffffff;
4228       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4229         {
4230           maxnum <<= 16;
4231           maxnum |= 0xffff;
4232           maxnum <<= 16;
4233           maxnum |= 0xffff;
4234         }
4235       maxnum = - maxnum - 1;
4236       if (imm_expr.X_op == O_constant
4237           && imm_expr.X_add_number <= maxnum
4238           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4239         {
4240         do_true:
4241           /* result is always true */
4242           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4243           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4244           return;
4245         }
4246       set_at (&icnt, sreg, 0);
4247       macro_build ((char *) NULL, &icnt, &offset_expr,
4248                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4249       break;
4250
4251     case M_BGEUL:
4252       likely = 1;
4253     case M_BGEU:
4254       if (treg == 0)
4255         goto do_true;
4256       if (sreg == 0)
4257         {
4258           macro_build ((char *) NULL, &icnt, &offset_expr,
4259                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4260           return;
4261         }
4262       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4263                    "d,v,t", AT, sreg, treg);
4264       macro_build ((char *) NULL, &icnt, &offset_expr,
4265                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4266       break;
4267
4268     case M_BGTUL_I:
4269       likely = 1;
4270     case M_BGTU_I:
4271       if (sreg == 0
4272           || (HAVE_32BIT_GPRS
4273               && imm_expr.X_op == O_constant
4274               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4275         goto do_false;
4276       if (imm_expr.X_op != O_constant)
4277         as_bad (_("Unsupported large constant"));
4278       ++imm_expr.X_add_number;
4279       /* FALLTHROUGH */
4280     case M_BGEU_I:
4281     case M_BGEUL_I:
4282       if (mask == M_BGEUL_I)
4283         likely = 1;
4284       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4285         goto do_true;
4286       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4287         {
4288           macro_build ((char *) NULL, &icnt, &offset_expr,
4289                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4290           return;
4291         }
4292       set_at (&icnt, sreg, 1);
4293       macro_build ((char *) NULL, &icnt, &offset_expr,
4294                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4295       break;
4296
4297     case M_BGTL:
4298       likely = 1;
4299     case M_BGT:
4300       if (treg == 0)
4301         {
4302           macro_build ((char *) NULL, &icnt, &offset_expr,
4303                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4304           return;
4305         }
4306       if (sreg == 0)
4307         {
4308           macro_build ((char *) NULL, &icnt, &offset_expr,
4309                        likely ? "bltzl" : "bltz", "s,p", treg);
4310           return;
4311         }
4312       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4313                    AT, treg, sreg);
4314       macro_build ((char *) NULL, &icnt, &offset_expr,
4315                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4316       break;
4317
4318     case M_BGTUL:
4319       likely = 1;
4320     case M_BGTU:
4321       if (treg == 0)
4322         {
4323           macro_build ((char *) NULL, &icnt, &offset_expr,
4324                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4325           return;
4326         }
4327       if (sreg == 0)
4328         goto do_false;
4329       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4330                    "d,v,t", AT, treg, sreg);
4331       macro_build ((char *) NULL, &icnt, &offset_expr,
4332                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4333       break;
4334
4335     case M_BLEL:
4336       likely = 1;
4337     case M_BLE:
4338       if (treg == 0)
4339         {
4340           macro_build ((char *) NULL, &icnt, &offset_expr,
4341                        likely ? "blezl" : "blez", "s,p", sreg);
4342           return;
4343         }
4344       if (sreg == 0)
4345         {
4346           macro_build ((char *) NULL, &icnt, &offset_expr,
4347                        likely ? "bgezl" : "bgez", "s,p", treg);
4348           return;
4349         }
4350       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4351                    AT, treg, sreg);
4352       macro_build ((char *) NULL, &icnt, &offset_expr,
4353                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4354       break;
4355
4356     case M_BLEL_I:
4357       likely = 1;
4358     case M_BLE_I:
4359       maxnum = 0x7fffffff;
4360       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4361         {
4362           maxnum <<= 16;
4363           maxnum |= 0xffff;
4364           maxnum <<= 16;
4365           maxnum |= 0xffff;
4366         }
4367       if (imm_expr.X_op == O_constant
4368           && imm_expr.X_add_number >= maxnum
4369           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4370         goto do_true;
4371       if (imm_expr.X_op != O_constant)
4372         as_bad (_("Unsupported large constant"));
4373       ++imm_expr.X_add_number;
4374       /* FALLTHROUGH */
4375     case M_BLT_I:
4376     case M_BLTL_I:
4377       if (mask == M_BLTL_I)
4378         likely = 1;
4379       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4380         {
4381           macro_build ((char *) NULL, &icnt, &offset_expr,
4382                        likely ? "bltzl" : "bltz", "s,p", sreg);
4383           return;
4384         }
4385       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4386         {
4387           macro_build ((char *) NULL, &icnt, &offset_expr,
4388                        likely ? "blezl" : "blez", "s,p", sreg);
4389           return;
4390         }
4391       set_at (&icnt, sreg, 0);
4392       macro_build ((char *) NULL, &icnt, &offset_expr,
4393                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4394       break;
4395
4396     case M_BLEUL:
4397       likely = 1;
4398     case M_BLEU:
4399       if (treg == 0)
4400         {
4401           macro_build ((char *) NULL, &icnt, &offset_expr,
4402                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4403           return;
4404         }
4405       if (sreg == 0)
4406         goto do_true;
4407       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4408                    "d,v,t", AT, treg, sreg);
4409       macro_build ((char *) NULL, &icnt, &offset_expr,
4410                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4411       break;
4412
4413     case M_BLEUL_I:
4414       likely = 1;
4415     case M_BLEU_I:
4416       if (sreg == 0
4417           || (HAVE_32BIT_GPRS
4418               && imm_expr.X_op == O_constant
4419               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4420         goto do_true;
4421       if (imm_expr.X_op != O_constant)
4422         as_bad (_("Unsupported large constant"));
4423       ++imm_expr.X_add_number;
4424       /* FALLTHROUGH */
4425     case M_BLTU_I:
4426     case M_BLTUL_I:
4427       if (mask == M_BLTUL_I)
4428         likely = 1;
4429       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4430         goto do_false;
4431       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4432         {
4433           macro_build ((char *) NULL, &icnt, &offset_expr,
4434                        likely ? "beql" : "beq",
4435                        "s,t,p", sreg, 0);
4436           return;
4437         }
4438       set_at (&icnt, sreg, 1);
4439       macro_build ((char *) NULL, &icnt, &offset_expr,
4440                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4441       break;
4442
4443     case M_BLTL:
4444       likely = 1;
4445     case M_BLT:
4446       if (treg == 0)
4447         {
4448           macro_build ((char *) NULL, &icnt, &offset_expr,
4449                        likely ? "bltzl" : "bltz", "s,p", sreg);
4450           return;
4451         }
4452       if (sreg == 0)
4453         {
4454           macro_build ((char *) NULL, &icnt, &offset_expr,
4455                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4456           return;
4457         }
4458       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4459                    AT, sreg, treg);
4460       macro_build ((char *) NULL, &icnt, &offset_expr,
4461                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4462       break;
4463
4464     case M_BLTUL:
4465       likely = 1;
4466     case M_BLTU:
4467       if (treg == 0)
4468         goto do_false;
4469       if (sreg == 0)
4470         {
4471           macro_build ((char *) NULL, &icnt, &offset_expr,
4472                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4473           return;
4474         }
4475       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4476                    "d,v,t", AT, sreg,
4477                    treg);
4478       macro_build ((char *) NULL, &icnt, &offset_expr,
4479                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4480       break;
4481
4482     case M_DDIV_3:
4483       dbl = 1;
4484     case M_DIV_3:
4485       s = "mflo";
4486       goto do_div3;
4487     case M_DREM_3:
4488       dbl = 1;
4489     case M_REM_3:
4490       s = "mfhi";
4491     do_div3:
4492       if (treg == 0)
4493         {
4494           as_warn (_("Divide by zero."));
4495           if (mips_trap)
4496             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4497                          "s,t,q", 0, 0, 7);
4498           else
4499             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4500                          "c", 7);
4501           return;
4502         }
4503
4504       mips_emit_delays (true);
4505       ++mips_opts.noreorder;
4506       mips_any_noreorder = 1;
4507       if (mips_trap)
4508         {
4509           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4510                        "s,t,q", treg, 0, 7);
4511           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4512                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4513         }
4514       else
4515         {
4516           expr1.X_add_number = 8;
4517           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4518           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4519                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4520           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4521                        "c", 7);
4522         }
4523       expr1.X_add_number = -1;
4524       macro_build ((char *) NULL, &icnt, &expr1,
4525                    dbl ? "daddiu" : "addiu",
4526                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4527       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4528       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4529       if (dbl)
4530         {
4531           expr1.X_add_number = 1;
4532           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4533                        (int) BFD_RELOC_LO16);
4534           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4535                        "d,w,<", AT, AT, 31);
4536         }
4537       else
4538         {
4539           expr1.X_add_number = 0x80000000;
4540           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4541                        (int) BFD_RELOC_HI16);
4542         }
4543       if (mips_trap)
4544         {
4545           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4546                        "s,t,q", sreg, AT, 6);
4547           /* We want to close the noreorder block as soon as possible, so
4548              that later insns are available for delay slot filling.  */
4549           --mips_opts.noreorder;
4550         }
4551       else
4552         {
4553           expr1.X_add_number = 8;
4554           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4555           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4556                        0);
4557
4558           /* We want to close the noreorder block as soon as possible, so
4559              that later insns are available for delay slot filling.  */
4560           --mips_opts.noreorder;
4561
4562           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4563                        "c", 6);
4564         }
4565       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4566       break;
4567
4568     case M_DIV_3I:
4569       s = "div";
4570       s2 = "mflo";
4571       goto do_divi;
4572     case M_DIVU_3I:
4573       s = "divu";
4574       s2 = "mflo";
4575       goto do_divi;
4576     case M_REM_3I:
4577       s = "div";
4578       s2 = "mfhi";
4579       goto do_divi;
4580     case M_REMU_3I:
4581       s = "divu";
4582       s2 = "mfhi";
4583       goto do_divi;
4584     case M_DDIV_3I:
4585       dbl = 1;
4586       s = "ddiv";
4587       s2 = "mflo";
4588       goto do_divi;
4589     case M_DDIVU_3I:
4590       dbl = 1;
4591       s = "ddivu";
4592       s2 = "mflo";
4593       goto do_divi;
4594     case M_DREM_3I:
4595       dbl = 1;
4596       s = "ddiv";
4597       s2 = "mfhi";
4598       goto do_divi;
4599     case M_DREMU_3I:
4600       dbl = 1;
4601       s = "ddivu";
4602       s2 = "mfhi";
4603     do_divi:
4604       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4605         {
4606           as_warn (_("Divide by zero."));
4607           if (mips_trap)
4608             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4609                          "s,t,q", 0, 0, 7);
4610           else
4611             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4612                          "c", 7);
4613           return;
4614         }
4615       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4616         {
4617           if (strcmp (s2, "mflo") == 0)
4618             move_register (&icnt, dreg, sreg);
4619           else
4620             move_register (&icnt, dreg, 0);
4621           return;
4622         }
4623       if (imm_expr.X_op == O_constant
4624           && imm_expr.X_add_number == -1
4625           && s[strlen (s) - 1] != 'u')
4626         {
4627           if (strcmp (s2, "mflo") == 0)
4628             {
4629               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4630                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4631             }
4632           else
4633             move_register (&icnt, dreg, 0);
4634           return;
4635         }
4636
4637       load_register (&icnt, AT, &imm_expr, dbl);
4638       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4639                    sreg, AT);
4640       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4641       break;
4642
4643     case M_DIVU_3:
4644       s = "divu";
4645       s2 = "mflo";
4646       goto do_divu3;
4647     case M_REMU_3:
4648       s = "divu";
4649       s2 = "mfhi";
4650       goto do_divu3;
4651     case M_DDIVU_3:
4652       s = "ddivu";
4653       s2 = "mflo";
4654       goto do_divu3;
4655     case M_DREMU_3:
4656       s = "ddivu";
4657       s2 = "mfhi";
4658     do_divu3:
4659       mips_emit_delays (true);
4660       ++mips_opts.noreorder;
4661       mips_any_noreorder = 1;
4662       if (mips_trap)
4663         {
4664           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4665                        "s,t,q", treg, 0, 7);
4666           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4667                        sreg, treg);
4668           /* We want to close the noreorder block as soon as possible, so
4669              that later insns are available for delay slot filling.  */
4670           --mips_opts.noreorder;
4671         }
4672       else
4673         {
4674           expr1.X_add_number = 8;
4675           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4676           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4677                        sreg, treg);
4678
4679           /* We want to close the noreorder block as soon as possible, so
4680              that later insns are available for delay slot filling.  */
4681           --mips_opts.noreorder;
4682           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4683                        "c", 7);
4684         }
4685       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4686       return;
4687
4688     case M_DLA_AB:
4689       dbl = 1;
4690     case M_LA_AB:
4691       /* Load the address of a symbol into a register.  If breg is not
4692          zero, we then add a base register to it.  */
4693
4694       if (dbl && HAVE_32BIT_GPRS)
4695         as_warn (_("dla used to load 32-bit register"));
4696
4697       if (! dbl && HAVE_64BIT_OBJECTS)
4698         as_warn (_("la used to load 64-bit address"));
4699
4700       if (offset_expr.X_op == O_constant
4701           && offset_expr.X_add_number >= -0x8000
4702           && offset_expr.X_add_number < 0x8000)
4703         {
4704           macro_build ((char *) NULL, &icnt, &offset_expr,
4705                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4706                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4707           return;
4708         }
4709
4710       if (treg == breg)
4711         {
4712           tempreg = AT;
4713           used_at = 1;
4714         }
4715       else
4716         {
4717           tempreg = treg;
4718           used_at = 0;
4719         }
4720
4721       /* When generating embedded PIC code, we permit expressions of
4722          the form
4723            la   $treg,foo-bar
4724            la   $treg,foo-bar($breg)
4725          where bar is an address in the current section.  These are used
4726          when getting the addresses of functions.  We don't permit
4727          X_add_number to be non-zero, because if the symbol is
4728          external the relaxing code needs to know that any addend is
4729          purely the offset to X_op_symbol.  */
4730       if (mips_pic == EMBEDDED_PIC
4731           && offset_expr.X_op == O_subtract
4732           && (symbol_constant_p (offset_expr.X_op_symbol)
4733               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4734               : (symbol_equated_p (offset_expr.X_op_symbol)
4735                  && (S_GET_SEGMENT
4736                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4737                       ->X_add_symbol)
4738                      == now_seg)))
4739           && (offset_expr.X_add_number == 0
4740               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4741         {
4742           if (breg == 0)
4743             {
4744               tempreg = treg;
4745               used_at = 0;
4746               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4747                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4748             }
4749           else
4750             {
4751               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4752                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4753               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4754                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4755                            "d,v,t", tempreg, tempreg, breg);
4756             }
4757           macro_build ((char *) NULL, &icnt, &offset_expr,
4758                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4759                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4760           if (! used_at)
4761             return;
4762           break;
4763         }
4764
4765       if (offset_expr.X_op != O_symbol
4766           && offset_expr.X_op != O_constant)
4767         {
4768           as_bad (_("expression too complex"));
4769           offset_expr.X_op = O_constant;
4770         }
4771
4772       if (offset_expr.X_op == O_constant)
4773         load_register (&icnt, tempreg, &offset_expr,
4774                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4775                         ? (dbl || HAVE_64BIT_ADDRESSES)
4776                         : HAVE_64BIT_ADDRESSES));
4777       else if (mips_pic == NO_PIC)
4778         {
4779           /* If this is a reference to a GP relative symbol, we want
4780                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4781              Otherwise we want
4782                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4783                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4784              If we have a constant, we need two instructions anyhow,
4785              so we may as well always use the latter form.
4786
4787             With 64bit address space and a usable $at we want
4788               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4789               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4790               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4791               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4792               dsll32    $tempreg,0
4793               daddu     $tempreg,$tempreg,$at
4794
4795             If $at is already in use, we use an path which is suboptimal
4796             on superscalar processors.
4797               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4798               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4799               dsll      $tempreg,16
4800               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4801               dsll      $tempreg,16
4802               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4803           */
4804           char *p = NULL;
4805           if (HAVE_64BIT_ADDRESSES)
4806             {
4807               /* We don't do GP optimization for now because RELAX_ENCODE can't
4808                  hold the data for such large chunks.  */
4809
4810               if (used_at == 0 && ! mips_opts.noat)
4811                 {
4812                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4813                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4814                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4815                                AT, (int) BFD_RELOC_HI16_S);
4816                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4817                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4818                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4819                                AT, AT, (int) BFD_RELOC_LO16);
4820                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4821                                "d,w,<", tempreg, tempreg, 0);
4822                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4823                                "d,v,t", tempreg, tempreg, AT);
4824                   used_at = 1;
4825                 }
4826               else
4827                 {
4828                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4829                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4830                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4831                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4832                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4833                                tempreg, tempreg, 16);
4834                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4835                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4836                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4837                                tempreg, tempreg, 16);
4838                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4839                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4840                 }
4841             }
4842           else
4843             {
4844               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4845                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4846                 {
4847                   frag_grow (20);
4848                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4849                                "t,r,j", tempreg, mips_gp_register,
4850                                (int) BFD_RELOC_GPREL16);
4851                   p = frag_var (rs_machine_dependent, 8, 0,
4852                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4853                                               mips_opts.warn_about_macros),
4854                                 offset_expr.X_add_symbol, 0, NULL);
4855                 }
4856               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4857               if (p != NULL)
4858                 p += 4;
4859               macro_build (p, &icnt, &offset_expr, "addiu",
4860                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4861             }
4862         }
4863       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4864         {
4865           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4866
4867           /* If this is a reference to an external symbol, and there
4868              is no constant, we want
4869                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4870              or if tempreg is PIC_CALL_REG
4871                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4872              For a local symbol, we want
4873                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4874                nop
4875                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4876
4877              If we have a small constant, and this is a reference to
4878              an external symbol, we want
4879                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4880                nop
4881                addiu    $tempreg,$tempreg,<constant>
4882              For a local symbol, we want the same instruction
4883              sequence, but we output a BFD_RELOC_LO16 reloc on the
4884              addiu instruction.
4885
4886              If we have a large constant, and this is a reference to
4887              an external symbol, we want
4888                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4889                lui      $at,<hiconstant>
4890                addiu    $at,$at,<loconstant>
4891                addu     $tempreg,$tempreg,$at
4892              For a local symbol, we want the same instruction
4893              sequence, but we output a BFD_RELOC_LO16 reloc on the
4894              addiu instruction.
4895
4896              For NewABI, we want for local or external data addresses
4897                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4898              For a local function symbol, we want
4899                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4900                nop
4901                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4902            */
4903
4904           expr1.X_add_number = offset_expr.X_add_number;
4905           offset_expr.X_add_number = 0;
4906           frag_grow (32);
4907           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4908             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4909           else if (HAVE_NEWABI)
4910             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4911           macro_build ((char *) NULL, &icnt, &offset_expr,
4912                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4913                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4914           if (expr1.X_add_number == 0)
4915             {
4916               int off;
4917               char *p;
4918
4919               if (breg == 0)
4920                 off = 0;
4921               else
4922                 {
4923                   /* We're going to put in an addu instruction using
4924                      tempreg, so we may as well insert the nop right
4925                      now.  */
4926                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4927                                "nop", "");
4928                   off = 4;
4929                 }
4930               p = frag_var (rs_machine_dependent, 8 - off, 0,
4931                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4932                                           (breg == 0
4933                                            ? mips_opts.warn_about_macros
4934                                            : 0)),
4935                             offset_expr.X_add_symbol, 0, NULL);
4936               if (breg == 0)
4937                 {
4938                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4939                   p += 4;
4940                 }
4941               macro_build (p, &icnt, &expr1,
4942                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4943                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4944               /* FIXME: If breg == 0, and the next instruction uses
4945                  $tempreg, then if this variant case is used an extra
4946                  nop will be generated.  */
4947             }
4948           else if (expr1.X_add_number >= -0x8000
4949                    && expr1.X_add_number < 0x8000)
4950             {
4951               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4952                            "nop", "");
4953               macro_build ((char *) NULL, &icnt, &expr1,
4954                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4955                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4956               frag_var (rs_machine_dependent, 0, 0,
4957                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4958                         offset_expr.X_add_symbol, 0, NULL);
4959             }
4960           else
4961             {
4962               int off1;
4963
4964               /* If we are going to add in a base register, and the
4965                  target register and the base register are the same,
4966                  then we are using AT as a temporary register.  Since
4967                  we want to load the constant into AT, we add our
4968                  current AT (from the global offset table) and the
4969                  register into the register now, and pretend we were
4970                  not using a base register.  */
4971               if (breg != treg)
4972                 off1 = 0;
4973               else
4974                 {
4975                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4976                                "nop", "");
4977                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4978                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4979                                "d,v,t", treg, AT, breg);
4980                   breg = 0;
4981                   tempreg = treg;
4982                   off1 = -8;
4983                 }
4984
4985               /* Set mips_optimize around the lui instruction to avoid
4986                  inserting an unnecessary nop after the lw.  */
4987               hold_mips_optimize = mips_optimize;
4988               mips_optimize = 2;
4989               macro_build_lui (NULL, &icnt, &expr1, AT);
4990               mips_optimize = hold_mips_optimize;
4991
4992               macro_build ((char *) NULL, &icnt, &expr1,
4993                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4994                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4995               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4996                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4997                            "d,v,t", tempreg, tempreg, AT);
4998               frag_var (rs_machine_dependent, 0, 0,
4999                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5000                         offset_expr.X_add_symbol, 0, NULL);
5001               used_at = 1;
5002             }
5003         }
5004       else if (mips_pic == SVR4_PIC)
5005         {
5006           int gpdel;
5007           char *p;
5008           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5009           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5010           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5011
5012           /* This is the large GOT case.  If this is a reference to an
5013              external symbol, and there is no constant, we want
5014                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5015                addu     $tempreg,$tempreg,$gp
5016                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5017              or if tempreg is PIC_CALL_REG
5018                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5019                addu     $tempreg,$tempreg,$gp
5020                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5021              For a local symbol, we want
5022                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5023                nop
5024                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5025
5026              If we have a small constant, and this is a reference to
5027              an external symbol, we want
5028                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5029                addu     $tempreg,$tempreg,$gp
5030                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5031                nop
5032                addiu    $tempreg,$tempreg,<constant>
5033              For a local symbol, we want
5034                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5035                nop
5036                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5037
5038              If we have a large constant, and this is a reference to
5039              an external symbol, we want
5040                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5041                addu     $tempreg,$tempreg,$gp
5042                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5043                lui      $at,<hiconstant>
5044                addiu    $at,$at,<loconstant>
5045                addu     $tempreg,$tempreg,$at
5046              For a local symbol, we want
5047                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5048                lui      $at,<hiconstant>
5049                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5050                addu     $tempreg,$tempreg,$at
5051
5052              For NewABI, we want for local data addresses
5053               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5054            */
5055
5056           expr1.X_add_number = offset_expr.X_add_number;
5057           offset_expr.X_add_number = 0;
5058           frag_grow (52);
5059           if (reg_needs_delay (mips_gp_register))
5060             gpdel = 4;
5061           else
5062             gpdel = 0;
5063           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5064             {
5065               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5066               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5067             }
5068           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5069                        tempreg, lui_reloc_type);
5070           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5071                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5072                        "d,v,t", tempreg, tempreg, mips_gp_register);
5073           macro_build ((char *) NULL, &icnt, &offset_expr,
5074                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5075                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5076           if (expr1.X_add_number == 0)
5077             {
5078               int off;
5079
5080               if (breg == 0)
5081                 off = 0;
5082               else
5083                 {
5084                   /* We're going to put in an addu instruction using
5085                      tempreg, so we may as well insert the nop right
5086                      now.  */
5087                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5088                                "nop", "");
5089                   off = 4;
5090                 }
5091
5092               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5093                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5094                                           8 + gpdel, 0,
5095                                           (breg == 0
5096                                            ? mips_opts.warn_about_macros
5097                                            : 0)),
5098                             offset_expr.X_add_symbol, 0, NULL);
5099             }
5100           else if (expr1.X_add_number >= -0x8000
5101                    && expr1.X_add_number < 0x8000)
5102             {
5103               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5104                            "nop", "");
5105               macro_build ((char *) NULL, &icnt, &expr1,
5106                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5107                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5108
5109               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5110                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5111                                           (breg == 0
5112                                            ? mips_opts.warn_about_macros
5113                                            : 0)),
5114                             offset_expr.X_add_symbol, 0, NULL);
5115             }
5116           else
5117             {
5118               int adj, dreg;
5119
5120               /* If we are going to add in a base register, and the
5121                  target register and the base register are the same,
5122                  then we are using AT as a temporary register.  Since
5123                  we want to load the constant into AT, we add our
5124                  current AT (from the global offset table) and the
5125                  register into the register now, and pretend we were
5126                  not using a base register.  */
5127               if (breg != treg)
5128                 {
5129                   adj = 0;
5130                   dreg = tempreg;
5131                 }
5132               else
5133                 {
5134                   assert (tempreg == AT);
5135                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5136                                "nop", "");
5137                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5138                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5139                                "d,v,t", treg, AT, breg);
5140                   dreg = treg;
5141                   adj = 8;
5142                 }
5143
5144               /* Set mips_optimize around the lui instruction to avoid
5145                  inserting an unnecessary nop after the lw.  */
5146               hold_mips_optimize = mips_optimize;
5147               mips_optimize = 2;
5148               macro_build_lui (NULL, &icnt, &expr1, AT);
5149               mips_optimize = hold_mips_optimize;
5150
5151               macro_build ((char *) NULL, &icnt, &expr1,
5152                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5153                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5154               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5155                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5156                            "d,v,t", dreg, dreg, AT);
5157
5158               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5159                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5160                                           8 + gpdel, 0,
5161                                           (breg == 0
5162                                            ? mips_opts.warn_about_macros
5163                                            : 0)),
5164                             offset_expr.X_add_symbol, 0, NULL);
5165
5166               used_at = 1;
5167             }
5168
5169           if (gpdel > 0)
5170             {
5171               /* This is needed because this instruction uses $gp, but
5172                  the first instruction on the main stream does not.  */
5173               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5174               p += 4;
5175             }
5176
5177           if (HAVE_NEWABI)
5178             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5179           macro_build (p, &icnt, &offset_expr,
5180                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5181                        "t,o(b)", tempreg,
5182                        local_reloc_type,
5183                        mips_gp_register);
5184           p += 4;
5185           if (expr1.X_add_number == 0 && HAVE_NEWABI)
5186             {
5187               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5188             }
5189          else
5190            if (expr1.X_add_number >= -0x8000
5191               && expr1.X_add_number < 0x8000)
5192             {
5193               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5194               p += 4;
5195               macro_build (p, &icnt, &expr1,
5196                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5197                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5198               /* FIXME: If add_number is 0, and there was no base
5199                  register, the external symbol case ended with a load,
5200                  so if the symbol turns out to not be external, and
5201                  the next instruction uses tempreg, an unnecessary nop
5202                  will be inserted.  */
5203             }
5204           else
5205             {
5206               if (breg == treg)
5207                 {
5208                   /* We must add in the base register now, as in the
5209                      external symbol case.  */
5210                   assert (tempreg == AT);
5211                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5212                   p += 4;
5213                   macro_build (p, &icnt, (expressionS *) NULL,
5214                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5215                                "d,v,t", treg, AT, breg);
5216                   p += 4;
5217                   tempreg = treg;
5218                   /* We set breg to 0 because we have arranged to add
5219                      it in in both cases.  */
5220                   breg = 0;
5221                 }
5222
5223               macro_build_lui (p, &icnt, &expr1, AT);
5224               p += 4;
5225               macro_build (p, &icnt, &expr1,
5226                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5227                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5228               p += 4;
5229               macro_build (p, &icnt, (expressionS *) NULL,
5230                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5231                            "d,v,t", tempreg, tempreg, AT);
5232               p += 4;
5233             }
5234         }
5235       else if (mips_pic == EMBEDDED_PIC)
5236         {
5237           /* We use
5238                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5239              */
5240           macro_build ((char *) NULL, &icnt, &offset_expr,
5241                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5242                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5243         }
5244       else
5245         abort ();
5246
5247       if (breg != 0)
5248         {
5249           char *s;
5250
5251           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5252             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5253           else
5254             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5255
5256           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5257                        "d,v,t", treg, tempreg, breg);
5258         }
5259
5260       if (! used_at)
5261         return;
5262
5263       break;
5264
5265     case M_J_A:
5266       /* The j instruction may not be used in PIC code, since it
5267          requires an absolute address.  We convert it to a b
5268          instruction.  */
5269       if (mips_pic == NO_PIC)
5270         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5271       else
5272         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5273       return;
5274
5275       /* The jal instructions must be handled as macros because when
5276          generating PIC code they expand to multi-instruction
5277          sequences.  Normally they are simple instructions.  */
5278     case M_JAL_1:
5279       dreg = RA;
5280       /* Fall through.  */
5281     case M_JAL_2:
5282       if (mips_pic == NO_PIC
5283           || mips_pic == EMBEDDED_PIC)
5284         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5285                      "d,s", dreg, sreg);
5286       else if (mips_pic == SVR4_PIC)
5287         {
5288           if (sreg != PIC_CALL_REG)
5289             as_warn (_("MIPS PIC call to register other than $25"));
5290
5291           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5292                        "d,s", dreg, sreg);
5293           if (! HAVE_NEWABI)
5294             {
5295               if (mips_cprestore_offset < 0)
5296                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5297               else
5298                 {
5299                   if (! mips_frame_reg_valid)
5300                     {
5301                       as_warn (_("No .frame pseudo-op used in PIC code"));
5302                       /* Quiet this warning.  */
5303                       mips_frame_reg_valid = 1;
5304                     }
5305                   if (! mips_cprestore_valid)
5306                     {
5307                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5308                       /* Quiet this warning.  */
5309                       mips_cprestore_valid = 1;
5310                     }
5311                   expr1.X_add_number = mips_cprestore_offset;
5312                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5313                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5314                                                 mips_gp_register, mips_frame_reg);
5315                 }
5316             }
5317         }
5318       else
5319         abort ();
5320
5321       return;
5322
5323     case M_JAL_A:
5324       if (mips_pic == NO_PIC)
5325         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5326       else if (mips_pic == SVR4_PIC)
5327         {
5328           char *p;
5329
5330           /* If this is a reference to an external symbol, and we are
5331              using a small GOT, we want
5332                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5333                nop
5334                jalr     $ra,$25
5335                nop
5336                lw       $gp,cprestore($sp)
5337              The cprestore value is set using the .cprestore
5338              pseudo-op.  If we are using a big GOT, we want
5339                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5340                addu     $25,$25,$gp
5341                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5342                nop
5343                jalr     $ra,$25
5344                nop
5345                lw       $gp,cprestore($sp)
5346              If the symbol is not external, we want
5347                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5348                nop
5349                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5350                jalr     $ra,$25
5351                nop
5352                lw $gp,cprestore($sp)
5353              For NewABI, we want
5354                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5355                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5356            */
5357           if (HAVE_NEWABI)
5358             {
5359               macro_build ((char *) NULL, &icnt, &offset_expr,
5360                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5361                            "t,o(b)", PIC_CALL_REG,
5362                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5363               macro_build_jalr (icnt, &offset_expr);
5364             }
5365           else
5366             {
5367               frag_grow (40);
5368               if (! mips_big_got)
5369                 {
5370                   macro_build ((char *) NULL, &icnt, &offset_expr,
5371                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5372                                "t,o(b)", PIC_CALL_REG,
5373                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5374                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5375                                "nop", "");
5376                   p = frag_var (rs_machine_dependent, 4, 0,
5377                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5378                                 offset_expr.X_add_symbol, 0, NULL);
5379                 }
5380               else
5381                 {
5382                   int gpdel;
5383
5384                   if (reg_needs_delay (mips_gp_register))
5385                     gpdel = 4;
5386                   else
5387                     gpdel = 0;
5388                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5389                                "t,u", PIC_CALL_REG,
5390                                (int) BFD_RELOC_MIPS_CALL_HI16);
5391                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5392                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5393                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5394                                mips_gp_register);
5395                   macro_build ((char *) NULL, &icnt, &offset_expr,
5396                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5397                                "t,o(b)", PIC_CALL_REG,
5398                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5399                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5400                                "nop", "");
5401                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5402                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5403                                               8 + gpdel, 0, 0),
5404                                 offset_expr.X_add_symbol, 0, NULL);
5405                   if (gpdel > 0)
5406                     {
5407                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5408                       p += 4;
5409                     }
5410                   macro_build (p, &icnt, &offset_expr,
5411                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5412                                "t,o(b)", PIC_CALL_REG,
5413                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5414                   p += 4;
5415                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5416                   p += 4;
5417                 }
5418               macro_build (p, &icnt, &offset_expr,
5419                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5420                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5421                            (int) BFD_RELOC_LO16);
5422               macro_build_jalr (icnt, &offset_expr);
5423
5424               if (mips_cprestore_offset < 0)
5425                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5426               else
5427                 {
5428                   if (! mips_frame_reg_valid)
5429                     {
5430                       as_warn (_("No .frame pseudo-op used in PIC code"));
5431                       /* Quiet this warning.  */
5432                       mips_frame_reg_valid = 1;
5433                     }
5434                   if (! mips_cprestore_valid)
5435                     {
5436                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5437                       /* Quiet this warning.  */
5438                       mips_cprestore_valid = 1;
5439                     }
5440                   if (mips_opts.noreorder)
5441                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5442                                  "nop", "");
5443                   expr1.X_add_number = mips_cprestore_offset;
5444                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5445                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5446                                                 mips_gp_register, mips_frame_reg);
5447                 }
5448             }
5449         }
5450       else if (mips_pic == EMBEDDED_PIC)
5451         {
5452           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5453           /* The linker may expand the call to a longer sequence which
5454              uses $at, so we must break rather than return.  */
5455           break;
5456         }
5457       else
5458         abort ();
5459
5460       return;
5461
5462     case M_LB_AB:
5463       s = "lb";
5464       goto ld;
5465     case M_LBU_AB:
5466       s = "lbu";
5467       goto ld;
5468     case M_LH_AB:
5469       s = "lh";
5470       goto ld;
5471     case M_LHU_AB:
5472       s = "lhu";
5473       goto ld;
5474     case M_LW_AB:
5475       s = "lw";
5476       goto ld;
5477     case M_LWC0_AB:
5478       s = "lwc0";
5479       /* Itbl support may require additional care here.  */
5480       coproc = 1;
5481       goto ld;
5482     case M_LWC1_AB:
5483       s = "lwc1";
5484       /* Itbl support may require additional care here.  */
5485       coproc = 1;
5486       goto ld;
5487     case M_LWC2_AB:
5488       s = "lwc2";
5489       /* Itbl support may require additional care here.  */
5490       coproc = 1;
5491       goto ld;
5492     case M_LWC3_AB:
5493       s = "lwc3";
5494       /* Itbl support may require additional care here.  */
5495       coproc = 1;
5496       goto ld;
5497     case M_LWL_AB:
5498       s = "lwl";
5499       lr = 1;
5500       goto ld;
5501     case M_LWR_AB:
5502       s = "lwr";
5503       lr = 1;
5504       goto ld;
5505     case M_LDC1_AB:
5506       if (mips_arch == CPU_R4650)
5507         {
5508           as_bad (_("opcode not supported on this processor"));
5509           return;
5510         }
5511       s = "ldc1";
5512       /* Itbl support may require additional care here.  */
5513       coproc = 1;
5514       goto ld;
5515     case M_LDC2_AB:
5516       s = "ldc2";
5517       /* Itbl support may require additional care here.  */
5518       coproc = 1;
5519       goto ld;
5520     case M_LDC3_AB:
5521       s = "ldc3";
5522       /* Itbl support may require additional care here.  */
5523       coproc = 1;
5524       goto ld;
5525     case M_LDL_AB:
5526       s = "ldl";
5527       lr = 1;
5528       goto ld;
5529     case M_LDR_AB:
5530       s = "ldr";
5531       lr = 1;
5532       goto ld;
5533     case M_LL_AB:
5534       s = "ll";
5535       goto ld;
5536     case M_LLD_AB:
5537       s = "lld";
5538       goto ld;
5539     case M_LWU_AB:
5540       s = "lwu";
5541     ld:
5542       if (breg == treg || coproc || lr)
5543         {
5544           tempreg = AT;
5545           used_at = 1;
5546         }
5547       else
5548         {
5549           tempreg = treg;
5550           used_at = 0;
5551         }
5552       goto ld_st;
5553     case M_SB_AB:
5554       s = "sb";
5555       goto st;
5556     case M_SH_AB:
5557       s = "sh";
5558       goto st;
5559     case M_SW_AB:
5560       s = "sw";
5561       goto st;
5562     case M_SWC0_AB:
5563       s = "swc0";
5564       /* Itbl support may require additional care here.  */
5565       coproc = 1;
5566       goto st;
5567     case M_SWC1_AB:
5568       s = "swc1";
5569       /* Itbl support may require additional care here.  */
5570       coproc = 1;
5571       goto st;
5572     case M_SWC2_AB:
5573       s = "swc2";
5574       /* Itbl support may require additional care here.  */
5575       coproc = 1;
5576       goto st;
5577     case M_SWC3_AB:
5578       s = "swc3";
5579       /* Itbl support may require additional care here.  */
5580       coproc = 1;
5581       goto st;
5582     case M_SWL_AB:
5583       s = "swl";
5584       goto st;
5585     case M_SWR_AB:
5586       s = "swr";
5587       goto st;
5588     case M_SC_AB:
5589       s = "sc";
5590       goto st;
5591     case M_SCD_AB:
5592       s = "scd";
5593       goto st;
5594     case M_SDC1_AB:
5595       if (mips_arch == CPU_R4650)
5596         {
5597           as_bad (_("opcode not supported on this processor"));
5598           return;
5599         }
5600       s = "sdc1";
5601       coproc = 1;
5602       /* Itbl support may require additional care here.  */
5603       goto st;
5604     case M_SDC2_AB:
5605       s = "sdc2";
5606       /* Itbl support may require additional care here.  */
5607       coproc = 1;
5608       goto st;
5609     case M_SDC3_AB:
5610       s = "sdc3";
5611       /* Itbl support may require additional care here.  */
5612       coproc = 1;
5613       goto st;
5614     case M_SDL_AB:
5615       s = "sdl";
5616       goto st;
5617     case M_SDR_AB:
5618       s = "sdr";
5619     st:
5620       tempreg = AT;
5621       used_at = 1;
5622     ld_st:
5623       /* Itbl support may require additional care here.  */
5624       if (mask == M_LWC1_AB
5625           || mask == M_SWC1_AB
5626           || mask == M_LDC1_AB
5627           || mask == M_SDC1_AB
5628           || mask == M_L_DAB
5629           || mask == M_S_DAB)
5630         fmt = "T,o(b)";
5631       else if (coproc)
5632         fmt = "E,o(b)";
5633       else
5634         fmt = "t,o(b)";
5635
5636       /* For embedded PIC, we allow loads where the offset is calculated
5637          by subtracting a symbol in the current segment from an unknown
5638          symbol, relative to a base register, e.g.:
5639                 <op>    $treg, <sym>-<localsym>($breg)
5640          This is used by the compiler for switch statements.  */
5641       if (mips_pic == EMBEDDED_PIC
5642           && offset_expr.X_op == O_subtract
5643           && (symbol_constant_p (offset_expr.X_op_symbol)
5644               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5645               : (symbol_equated_p (offset_expr.X_op_symbol)
5646                  && (S_GET_SEGMENT
5647                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5648                       ->X_add_symbol)
5649                      == now_seg)))
5650           && breg != 0
5651           && (offset_expr.X_add_number == 0
5652               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5653         {
5654           /* For this case, we output the instructions:
5655                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5656                 addiu   $tempreg,$tempreg,$breg
5657                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5658              If the relocation would fit entirely in 16 bits, it would be
5659              nice to emit:
5660                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5661              instead, but that seems quite difficult.  */
5662           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5663                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5664           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5665                        ((bfd_arch_bits_per_address (stdoutput) == 32
5666                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5667                         ? "addu" : "daddu"),
5668                        "d,v,t", tempreg, tempreg, breg);
5669           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5670                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5671           if (! used_at)
5672             return;
5673           break;
5674         }
5675
5676       if (offset_expr.X_op != O_constant
5677           && offset_expr.X_op != O_symbol)
5678         {
5679           as_bad (_("expression too complex"));
5680           offset_expr.X_op = O_constant;
5681         }
5682
5683       /* A constant expression in PIC code can be handled just as it
5684          is in non PIC code.  */
5685       if (mips_pic == NO_PIC
5686           || offset_expr.X_op == O_constant)
5687         {
5688           char *p;
5689
5690           /* If this is a reference to a GP relative symbol, and there
5691              is no base register, we want
5692                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5693              Otherwise, if there is no base register, we want
5694                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5695                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5696              If we have a constant, we need two instructions anyhow,
5697              so we always use the latter form.
5698
5699              If we have a base register, and this is a reference to a
5700              GP relative symbol, we want
5701                addu     $tempreg,$breg,$gp
5702                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5703              Otherwise we want
5704                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5705                addu     $tempreg,$tempreg,$breg
5706                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5707              With a constant we always use the latter case.
5708
5709              With 64bit address space and no base register and $at usable,
5710              we want
5711                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5712                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5713                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5714                dsll32   $tempreg,0
5715                daddu    $tempreg,$at
5716                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5717              If we have a base register, we want
5718                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5719                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5720                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5721                daddu    $at,$breg
5722                dsll32   $tempreg,0
5723                daddu    $tempreg,$at
5724                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5725
5726              Without $at we can't generate the optimal path for superscalar
5727              processors here since this would require two temporary registers.
5728                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5729                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5730                dsll     $tempreg,16
5731                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5732                dsll     $tempreg,16
5733                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5734              If we have a base register, we want
5735                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5736                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5737                dsll     $tempreg,16
5738                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5739                dsll     $tempreg,16
5740                daddu    $tempreg,$tempreg,$breg
5741                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5742
5743              If we have 64-bit addresses, as an optimization, for
5744              addresses which are 32-bit constants (e.g. kseg0/kseg1
5745              addresses) we fall back to the 32-bit address generation
5746              mechanism since it is more efficient.  Note that due to
5747              the signed offset used by memory operations, the 32-bit
5748              range is shifted down by 32768 here.  This code should
5749              probably attempt to generate 64-bit constants more
5750              efficiently in general.
5751            */
5752           if (HAVE_64BIT_ADDRESSES
5753               && !(offset_expr.X_op == O_constant
5754                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5755             {
5756               p = NULL;
5757
5758               /* We don't do GP optimization for now because RELAX_ENCODE can't
5759                  hold the data for such large chunks.  */
5760
5761               if (used_at == 0 && ! mips_opts.noat)
5762                 {
5763                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5764                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5765                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5766                                AT, (int) BFD_RELOC_HI16_S);
5767                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5768                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5769                   if (breg != 0)
5770                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5771                                  "d,v,t", AT, AT, breg);
5772                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5773                                "d,w,<", tempreg, tempreg, 0);
5774                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5775                                "d,v,t", tempreg, tempreg, AT);
5776                   macro_build (p, &icnt, &offset_expr, s,
5777                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5778                   used_at = 1;
5779                 }
5780               else
5781                 {
5782                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5783                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5784                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5785                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5786                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5787                                "d,w,<", tempreg, tempreg, 16);
5788                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5789                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5790                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5791                                "d,w,<", tempreg, tempreg, 16);
5792                   if (breg != 0)
5793                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5794                                  "d,v,t", tempreg, tempreg, breg);
5795                   macro_build (p, &icnt, &offset_expr, s,
5796                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5797                 }
5798
5799               return;
5800             }
5801
5802           if (breg == 0)
5803             {
5804               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5805                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5806                 p = NULL;
5807               else
5808                 {
5809                   frag_grow (20);
5810                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5811                                treg, (int) BFD_RELOC_GPREL16,
5812                                mips_gp_register);
5813                   p = frag_var (rs_machine_dependent, 8, 0,
5814                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5815                                               (mips_opts.warn_about_macros
5816                                                || (used_at
5817                                                    && mips_opts.noat))),
5818                                 offset_expr.X_add_symbol, 0, NULL);
5819                   used_at = 0;
5820                 }
5821               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5822               if (p != NULL)
5823                 p += 4;
5824               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5825                            (int) BFD_RELOC_LO16, tempreg);
5826             }
5827           else
5828             {
5829               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5830                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5831                 p = NULL;
5832               else
5833                 {
5834                   frag_grow (28);
5835                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5836                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5837                                "d,v,t", tempreg, breg, mips_gp_register);
5838                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5839                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5840                   p = frag_var (rs_machine_dependent, 12, 0,
5841                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5842                                 offset_expr.X_add_symbol, 0, NULL);
5843                 }
5844               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5845               if (p != NULL)
5846                 p += 4;
5847               macro_build (p, &icnt, (expressionS *) NULL,
5848                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5849                            "d,v,t", tempreg, tempreg, breg);
5850               if (p != NULL)
5851                 p += 4;
5852               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5853                            (int) BFD_RELOC_LO16, tempreg);
5854             }
5855         }
5856       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5857         {
5858           char *p;
5859           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5860
5861           /* If this is a reference to an external symbol, we want
5862                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5863                nop
5864                <op>     $treg,0($tempreg)
5865              Otherwise we want
5866                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5867                nop
5868                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5869                <op>     $treg,0($tempreg)
5870              If we have NewABI, we want
5871                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5872              If there is a base register, we add it to $tempreg before
5873              the <op>.  If there is a constant, we stick it in the
5874              <op> instruction.  We don't handle constants larger than
5875              16 bits, because we have no way to load the upper 16 bits
5876              (actually, we could handle them for the subset of cases
5877              in which we are not using $at).  */
5878           assert (offset_expr.X_op == O_symbol);
5879           expr1.X_add_number = offset_expr.X_add_number;
5880           offset_expr.X_add_number = 0;
5881           if (HAVE_NEWABI)
5882             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5883           if (expr1.X_add_number < -0x8000
5884               || expr1.X_add_number >= 0x8000)
5885             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5886           frag_grow (20);
5887           macro_build ((char *) NULL, &icnt, &offset_expr,
5888                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5889                        (int) lw_reloc_type, mips_gp_register);
5890           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5891           p = frag_var (rs_machine_dependent, 4, 0,
5892                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5893                         offset_expr.X_add_symbol, 0, NULL);
5894           macro_build (p, &icnt, &offset_expr,
5895                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5896                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5897           if (breg != 0)
5898             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5899                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5900                          "d,v,t", tempreg, tempreg, breg);
5901           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5902                        (int) BFD_RELOC_LO16, tempreg);
5903         }
5904       else if (mips_pic == SVR4_PIC)
5905         {
5906           int gpdel;
5907           char *p;
5908
5909           /* If this is a reference to an external symbol, we want
5910                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5911                addu     $tempreg,$tempreg,$gp
5912                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5913                <op>     $treg,0($tempreg)
5914              Otherwise we want
5915                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5916                nop
5917                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5918                <op>     $treg,0($tempreg)
5919              If there is a base register, we add it to $tempreg before
5920              the <op>.  If there is a constant, we stick it in the
5921              <op> instruction.  We don't handle constants larger than
5922              16 bits, because we have no way to load the upper 16 bits
5923              (actually, we could handle them for the subset of cases
5924              in which we are not using $at).
5925
5926              For NewABI, we want
5927                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5928                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5929                <op>     $treg,0($tempreg)
5930            */
5931           assert (offset_expr.X_op == O_symbol);
5932           expr1.X_add_number = offset_expr.X_add_number;
5933           offset_expr.X_add_number = 0;
5934           if (expr1.X_add_number < -0x8000
5935               || expr1.X_add_number >= 0x8000)
5936             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5937           if (HAVE_NEWABI)
5938             {
5939               macro_build ((char *) NULL, &icnt, &offset_expr,
5940                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5941                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5942                            mips_gp_register);
5943               macro_build ((char *) NULL, &icnt, &offset_expr,
5944                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5945                            "t,r,j", tempreg, tempreg,
5946                            BFD_RELOC_MIPS_GOT_OFST);
5947               if (breg != 0)
5948                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5949                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5950                              "d,v,t", tempreg, tempreg, breg);
5951               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5952                            (int) BFD_RELOC_LO16, tempreg);
5953
5954               if (! used_at)
5955                 return;
5956
5957               break;
5958             }
5959           if (reg_needs_delay (mips_gp_register))
5960             gpdel = 4;
5961           else
5962             gpdel = 0;
5963           frag_grow (36);
5964           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5965                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5966           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5967                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5968                        "d,v,t", tempreg, tempreg, mips_gp_register);
5969           macro_build ((char *) NULL, &icnt, &offset_expr,
5970                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5971                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5972                        tempreg);
5973           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5974                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5975                         offset_expr.X_add_symbol, 0, NULL);
5976           if (gpdel > 0)
5977             {
5978               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5979               p += 4;
5980             }
5981           macro_build (p, &icnt, &offset_expr,
5982                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5983                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5984                        mips_gp_register);
5985           p += 4;
5986           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5987           p += 4;
5988           macro_build (p, &icnt, &offset_expr,
5989                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5990                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5991           if (breg != 0)
5992             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5993                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5994                          "d,v,t", tempreg, tempreg, breg);
5995           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5996                        (int) BFD_RELOC_LO16, tempreg);
5997         }
5998       else if (mips_pic == EMBEDDED_PIC)
5999         {
6000           /* If there is no base register, we want
6001                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6002              If there is a base register, we want
6003                addu     $tempreg,$breg,$gp
6004                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6005              */
6006           assert (offset_expr.X_op == O_symbol);
6007           if (breg == 0)
6008             {
6009               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6010                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6011               used_at = 0;
6012             }
6013           else
6014             {
6015               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6016                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6017                            "d,v,t", tempreg, breg, mips_gp_register);
6018               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6019                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6020             }
6021         }
6022       else
6023         abort ();
6024
6025       if (! used_at)
6026         return;
6027
6028       break;
6029
6030     case M_LI:
6031     case M_LI_S:
6032       load_register (&icnt, treg, &imm_expr, 0);
6033       return;
6034
6035     case M_DLI:
6036       load_register (&icnt, treg, &imm_expr, 1);
6037       return;
6038
6039     case M_LI_SS:
6040       if (imm_expr.X_op == O_constant)
6041         {
6042           load_register (&icnt, AT, &imm_expr, 0);
6043           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6044                        "mtc1", "t,G", AT, treg);
6045           break;
6046         }
6047       else
6048         {
6049           assert (offset_expr.X_op == O_symbol
6050                   && strcmp (segment_name (S_GET_SEGMENT
6051                                            (offset_expr.X_add_symbol)),
6052                              ".lit4") == 0
6053                   && offset_expr.X_add_number == 0);
6054           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6055                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6056           return;
6057         }
6058
6059     case M_LI_D:
6060       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6061          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6062          order 32 bits of the value and the low order 32 bits are either
6063          zero or in OFFSET_EXPR.  */
6064       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6065         {
6066           if (HAVE_64BIT_GPRS)
6067             load_register (&icnt, treg, &imm_expr, 1);
6068           else
6069             {
6070               int hreg, lreg;
6071
6072               if (target_big_endian)
6073                 {
6074                   hreg = treg;
6075                   lreg = treg + 1;
6076                 }
6077               else
6078                 {
6079                   hreg = treg + 1;
6080                   lreg = treg;
6081                 }
6082
6083               if (hreg <= 31)
6084                 load_register (&icnt, hreg, &imm_expr, 0);
6085               if (lreg <= 31)
6086                 {
6087                   if (offset_expr.X_op == O_absent)
6088                     move_register (&icnt, lreg, 0);
6089                   else
6090                     {
6091                       assert (offset_expr.X_op == O_constant);
6092                       load_register (&icnt, lreg, &offset_expr, 0);
6093                     }
6094                 }
6095             }
6096           return;
6097         }
6098
6099       /* We know that sym is in the .rdata section.  First we get the
6100          upper 16 bits of the address.  */
6101       if (mips_pic == NO_PIC)
6102         {
6103           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6104         }
6105       else if (mips_pic == SVR4_PIC)
6106         {
6107           macro_build ((char *) NULL, &icnt, &offset_expr,
6108                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6109                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6110                        mips_gp_register);
6111         }
6112       else if (mips_pic == EMBEDDED_PIC)
6113         {
6114           /* For embedded PIC we pick up the entire address off $gp in
6115              a single instruction.  */
6116           macro_build ((char *) NULL, &icnt, &offset_expr,
6117                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6118                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6119           offset_expr.X_op = O_constant;
6120           offset_expr.X_add_number = 0;
6121         }
6122       else
6123         abort ();
6124
6125       /* Now we load the register(s).  */
6126       if (HAVE_64BIT_GPRS)
6127         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6128                      treg, (int) BFD_RELOC_LO16, AT);
6129       else
6130         {
6131           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6132                        treg, (int) BFD_RELOC_LO16, AT);
6133           if (treg != RA)
6134             {
6135               /* FIXME: How in the world do we deal with the possible
6136                  overflow here?  */
6137               offset_expr.X_add_number += 4;
6138               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6139                            treg + 1, (int) BFD_RELOC_LO16, AT);
6140             }
6141         }
6142
6143       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6144          does not become a variant frag.  */
6145       frag_wane (frag_now);
6146       frag_new (0);
6147
6148       break;
6149
6150     case M_LI_DD:
6151       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6152          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6153          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6154          the value and the low order 32 bits are either zero or in
6155          OFFSET_EXPR.  */
6156       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6157         {
6158           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6159           if (HAVE_64BIT_FPRS)
6160             {
6161               assert (HAVE_64BIT_GPRS);
6162               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6163                            "dmtc1", "t,S", AT, treg);
6164             }
6165           else
6166             {
6167               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6168                            "mtc1", "t,G", AT, treg + 1);
6169               if (offset_expr.X_op == O_absent)
6170                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6171                              "mtc1", "t,G", 0, treg);
6172               else
6173                 {
6174                   assert (offset_expr.X_op == O_constant);
6175                   load_register (&icnt, AT, &offset_expr, 0);
6176                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6177                                "mtc1", "t,G", AT, treg);
6178                 }
6179             }
6180           break;
6181         }
6182
6183       assert (offset_expr.X_op == O_symbol
6184               && offset_expr.X_add_number == 0);
6185       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6186       if (strcmp (s, ".lit8") == 0)
6187         {
6188           if (mips_opts.isa != ISA_MIPS1)
6189             {
6190               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6191                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6192                            mips_gp_register);
6193               return;
6194             }
6195           breg = mips_gp_register;
6196           r = BFD_RELOC_MIPS_LITERAL;
6197           goto dob;
6198         }
6199       else
6200         {
6201           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6202           if (mips_pic == SVR4_PIC)
6203             macro_build ((char *) NULL, &icnt, &offset_expr,
6204                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6205                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6206                          mips_gp_register);
6207           else
6208             {
6209               /* FIXME: This won't work for a 64 bit address.  */
6210               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6211             }
6212
6213           if (mips_opts.isa != ISA_MIPS1)
6214             {
6215               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6216                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6217
6218               /* To avoid confusion in tc_gen_reloc, we must ensure
6219                  that this does not become a variant frag.  */
6220               frag_wane (frag_now);
6221               frag_new (0);
6222
6223               break;
6224             }
6225           breg = AT;
6226           r = BFD_RELOC_LO16;
6227           goto dob;
6228         }
6229
6230     case M_L_DOB:
6231       if (mips_arch == CPU_R4650)
6232         {
6233           as_bad (_("opcode not supported on this processor"));
6234           return;
6235         }
6236       /* Even on a big endian machine $fn comes before $fn+1.  We have
6237          to adjust when loading from memory.  */
6238       r = BFD_RELOC_LO16;
6239     dob:
6240       assert (mips_opts.isa == ISA_MIPS1);
6241       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6242                    target_big_endian ? treg + 1 : treg,
6243                    (int) r, breg);
6244       /* FIXME: A possible overflow which I don't know how to deal
6245          with.  */
6246       offset_expr.X_add_number += 4;
6247       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6248                    target_big_endian ? treg : treg + 1,
6249                    (int) r, breg);
6250
6251       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6252          does not become a variant frag.  */
6253       frag_wane (frag_now);
6254       frag_new (0);
6255
6256       if (breg != AT)
6257         return;
6258       break;
6259
6260     case M_L_DAB:
6261       /*
6262        * The MIPS assembler seems to check for X_add_number not
6263        * being double aligned and generating:
6264        *        lui     at,%hi(foo+1)
6265        *        addu    at,at,v1
6266        *        addiu   at,at,%lo(foo+1)
6267        *        lwc1    f2,0(at)
6268        *        lwc1    f3,4(at)
6269        * But, the resulting address is the same after relocation so why
6270        * generate the extra instruction?
6271        */
6272       if (mips_arch == CPU_R4650)
6273         {
6274           as_bad (_("opcode not supported on this processor"));
6275           return;
6276         }
6277       /* Itbl support may require additional care here.  */
6278       coproc = 1;
6279       if (mips_opts.isa != ISA_MIPS1)
6280         {
6281           s = "ldc1";
6282           goto ld;
6283         }
6284
6285       s = "lwc1";
6286       fmt = "T,o(b)";
6287       goto ldd_std;
6288
6289     case M_S_DAB:
6290       if (mips_arch == CPU_R4650)
6291         {
6292           as_bad (_("opcode not supported on this processor"));
6293           return;
6294         }
6295
6296       if (mips_opts.isa != ISA_MIPS1)
6297         {
6298           s = "sdc1";
6299           goto st;
6300         }
6301
6302       s = "swc1";
6303       fmt = "T,o(b)";
6304       /* Itbl support may require additional care here.  */
6305       coproc = 1;
6306       goto ldd_std;
6307
6308     case M_LD_AB:
6309       if (HAVE_64BIT_GPRS)
6310         {
6311           s = "ld";
6312           goto ld;
6313         }
6314
6315       s = "lw";
6316       fmt = "t,o(b)";
6317       goto ldd_std;
6318
6319     case M_SD_AB:
6320       if (HAVE_64BIT_GPRS)
6321         {
6322           s = "sd";
6323           goto st;
6324         }
6325
6326       s = "sw";
6327       fmt = "t,o(b)";
6328
6329     ldd_std:
6330       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6331          loads for the case of doing a pair of loads to simulate an 'ld'.
6332          This is not currently done by the compiler, and assembly coders
6333          writing embedded-pic code can cope.  */
6334
6335       if (offset_expr.X_op != O_symbol
6336           && offset_expr.X_op != O_constant)
6337         {
6338           as_bad (_("expression too complex"));
6339           offset_expr.X_op = O_constant;
6340         }
6341
6342       /* Even on a big endian machine $fn comes before $fn+1.  We have
6343          to adjust when loading from memory.  We set coproc if we must
6344          load $fn+1 first.  */
6345       /* Itbl support may require additional care here.  */
6346       if (! target_big_endian)
6347         coproc = 0;
6348
6349       if (mips_pic == NO_PIC
6350           || offset_expr.X_op == O_constant)
6351         {
6352           char *p;
6353
6354           /* If this is a reference to a GP relative symbol, we want
6355                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6356                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6357              If we have a base register, we use this
6358                addu     $at,$breg,$gp
6359                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6360                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6361              If this is not a GP relative symbol, we want
6362                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6363                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6364                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6365              If there is a base register, we add it to $at after the
6366              lui instruction.  If there is a constant, we always use
6367              the last case.  */
6368           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6369               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6370             {
6371               p = NULL;
6372               used_at = 1;
6373             }
6374           else
6375             {
6376               int off;
6377
6378               if (breg == 0)
6379                 {
6380                   frag_grow (28);
6381                   tempreg = mips_gp_register;
6382                   off = 0;
6383                   used_at = 0;
6384                 }
6385               else
6386                 {
6387                   frag_grow (36);
6388                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6389                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6390                                "d,v,t", AT, breg, mips_gp_register);
6391                   tempreg = AT;
6392                   off = 4;
6393                   used_at = 1;
6394                 }
6395
6396               /* Itbl support may require additional care here.  */
6397               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6398                            coproc ? treg + 1 : treg,
6399                            (int) BFD_RELOC_GPREL16, tempreg);
6400               offset_expr.X_add_number += 4;
6401
6402               /* Set mips_optimize to 2 to avoid inserting an
6403                  undesired nop.  */
6404               hold_mips_optimize = mips_optimize;
6405               mips_optimize = 2;
6406               /* Itbl support may require additional care here.  */
6407               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6408                            coproc ? treg : treg + 1,
6409                            (int) BFD_RELOC_GPREL16, tempreg);
6410               mips_optimize = hold_mips_optimize;
6411
6412               p = frag_var (rs_machine_dependent, 12 + off, 0,
6413                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6414                                           used_at && mips_opts.noat),
6415                             offset_expr.X_add_symbol, 0, NULL);
6416
6417               /* We just generated two relocs.  When tc_gen_reloc
6418                  handles this case, it will skip the first reloc and
6419                  handle the second.  The second reloc already has an
6420                  extra addend of 4, which we added above.  We must
6421                  subtract it out, and then subtract another 4 to make
6422                  the first reloc come out right.  The second reloc
6423                  will come out right because we are going to add 4 to
6424                  offset_expr when we build its instruction below.
6425
6426                  If we have a symbol, then we don't want to include
6427                  the offset, because it will wind up being included
6428                  when we generate the reloc.  */
6429
6430               if (offset_expr.X_op == O_constant)
6431                 offset_expr.X_add_number -= 8;
6432               else
6433                 {
6434                   offset_expr.X_add_number = -4;
6435                   offset_expr.X_op = O_constant;
6436                 }
6437             }
6438           macro_build_lui (p, &icnt, &offset_expr, AT);
6439           if (p != NULL)
6440             p += 4;
6441           if (breg != 0)
6442             {
6443               macro_build (p, &icnt, (expressionS *) NULL,
6444                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6445                            "d,v,t", AT, breg, AT);
6446               if (p != NULL)
6447                 p += 4;
6448             }
6449           /* Itbl support may require additional care here.  */
6450           macro_build (p, &icnt, &offset_expr, s, fmt,
6451                        coproc ? treg + 1 : treg,
6452                        (int) BFD_RELOC_LO16, AT);
6453           if (p != NULL)
6454             p += 4;
6455           /* FIXME: How do we handle overflow here?  */
6456           offset_expr.X_add_number += 4;
6457           /* Itbl support may require additional care here.  */
6458           macro_build (p, &icnt, &offset_expr, s, fmt,
6459                        coproc ? treg : treg + 1,
6460                        (int) BFD_RELOC_LO16, AT);
6461         }
6462       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6463         {
6464           int off;
6465
6466           /* If this is a reference to an external symbol, we want
6467                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6468                nop
6469                <op>     $treg,0($at)
6470                <op>     $treg+1,4($at)
6471              Otherwise we want
6472                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6473                nop
6474                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6475                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6476              If there is a base register we add it to $at before the
6477              lwc1 instructions.  If there is a constant we include it
6478              in the lwc1 instructions.  */
6479           used_at = 1;
6480           expr1.X_add_number = offset_expr.X_add_number;
6481           offset_expr.X_add_number = 0;
6482           if (expr1.X_add_number < -0x8000
6483               || expr1.X_add_number >= 0x8000 - 4)
6484             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6485           if (breg == 0)
6486             off = 0;
6487           else
6488             off = 4;
6489           frag_grow (24 + off);
6490           macro_build ((char *) NULL, &icnt, &offset_expr,
6491                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6492                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6493           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6494           if (breg != 0)
6495             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6496                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6497                          "d,v,t", AT, breg, AT);
6498           /* Itbl support may require additional care here.  */
6499           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6500                        coproc ? treg + 1 : treg,
6501                        (int) BFD_RELOC_LO16, AT);
6502           expr1.X_add_number += 4;
6503
6504           /* Set mips_optimize to 2 to avoid inserting an undesired
6505              nop.  */
6506           hold_mips_optimize = mips_optimize;
6507           mips_optimize = 2;
6508           /* Itbl support may require additional care here.  */
6509           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6510                        coproc ? treg : treg + 1,
6511                        (int) BFD_RELOC_LO16, AT);
6512           mips_optimize = hold_mips_optimize;
6513
6514           (void) frag_var (rs_machine_dependent, 0, 0,
6515                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6516                            offset_expr.X_add_symbol, 0, NULL);
6517         }
6518       else if (mips_pic == SVR4_PIC)
6519         {
6520           int gpdel, off;
6521           char *p;
6522
6523           /* If this is a reference to an external symbol, we want
6524                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6525                addu     $at,$at,$gp
6526                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6527                nop
6528                <op>     $treg,0($at)
6529                <op>     $treg+1,4($at)
6530              Otherwise we want
6531                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6532                nop
6533                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6534                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6535              If there is a base register we add it to $at before the
6536              lwc1 instructions.  If there is a constant we include it
6537              in the lwc1 instructions.  */
6538           used_at = 1;
6539           expr1.X_add_number = offset_expr.X_add_number;
6540           offset_expr.X_add_number = 0;
6541           if (expr1.X_add_number < -0x8000
6542               || expr1.X_add_number >= 0x8000 - 4)
6543             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6544           if (reg_needs_delay (mips_gp_register))
6545             gpdel = 4;
6546           else
6547             gpdel = 0;
6548           if (breg == 0)
6549             off = 0;
6550           else
6551             off = 4;
6552           frag_grow (56);
6553           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6554                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6555           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6556                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6557                        "d,v,t", AT, AT, mips_gp_register);
6558           macro_build ((char *) NULL, &icnt, &offset_expr,
6559                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6560                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6561           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6562           if (breg != 0)
6563             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6564                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6565                          "d,v,t", AT, breg, AT);
6566           /* Itbl support may require additional care here.  */
6567           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6568                        coproc ? treg + 1 : treg,
6569                        (int) BFD_RELOC_LO16, AT);
6570           expr1.X_add_number += 4;
6571
6572           /* Set mips_optimize to 2 to avoid inserting an undesired
6573              nop.  */
6574           hold_mips_optimize = mips_optimize;
6575           mips_optimize = 2;
6576           /* Itbl support may require additional care here.  */
6577           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6578                        coproc ? treg : treg + 1,
6579                        (int) BFD_RELOC_LO16, AT);
6580           mips_optimize = hold_mips_optimize;
6581           expr1.X_add_number -= 4;
6582
6583           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6584                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6585                                       8 + gpdel + off, 1, 0),
6586                         offset_expr.X_add_symbol, 0, NULL);
6587           if (gpdel > 0)
6588             {
6589               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6590               p += 4;
6591             }
6592           macro_build (p, &icnt, &offset_expr,
6593                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6594                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6595                        mips_gp_register);
6596           p += 4;
6597           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6598           p += 4;
6599           if (breg != 0)
6600             {
6601               macro_build (p, &icnt, (expressionS *) NULL,
6602                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6603                            "d,v,t", AT, breg, AT);
6604               p += 4;
6605             }
6606           /* Itbl support may require additional care here.  */
6607           macro_build (p, &icnt, &expr1, s, fmt,
6608                        coproc ? treg + 1 : treg,
6609                        (int) BFD_RELOC_LO16, AT);
6610           p += 4;
6611           expr1.X_add_number += 4;
6612
6613           /* Set mips_optimize to 2 to avoid inserting an undesired
6614              nop.  */
6615           hold_mips_optimize = mips_optimize;
6616           mips_optimize = 2;
6617           /* Itbl support may require additional care here.  */
6618           macro_build (p, &icnt, &expr1, s, fmt,
6619                        coproc ? treg : treg + 1,
6620                        (int) BFD_RELOC_LO16, AT);
6621           mips_optimize = hold_mips_optimize;
6622         }
6623       else if (mips_pic == EMBEDDED_PIC)
6624         {
6625           /* If there is no base register, we use
6626                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6627                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6628              If we have a base register, we use
6629                addu     $at,$breg,$gp
6630                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6631                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6632              */
6633           if (breg == 0)
6634             {
6635               tempreg = mips_gp_register;
6636               used_at = 0;
6637             }
6638           else
6639             {
6640               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6641                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6642                            "d,v,t", AT, breg, mips_gp_register);
6643               tempreg = AT;
6644               used_at = 1;
6645             }
6646
6647           /* Itbl support may require additional care here.  */
6648           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6649                        coproc ? treg + 1 : treg,
6650                        (int) BFD_RELOC_GPREL16, tempreg);
6651           offset_expr.X_add_number += 4;
6652           /* Itbl support may require additional care here.  */
6653           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6654                        coproc ? treg : treg + 1,
6655                        (int) BFD_RELOC_GPREL16, tempreg);
6656         }
6657       else
6658         abort ();
6659
6660       if (! used_at)
6661         return;
6662
6663       break;
6664
6665     case M_LD_OB:
6666       s = "lw";
6667       goto sd_ob;
6668     case M_SD_OB:
6669       s = "sw";
6670     sd_ob:
6671       assert (HAVE_32BIT_ADDRESSES);
6672       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6673                    (int) BFD_RELOC_LO16, breg);
6674       offset_expr.X_add_number += 4;
6675       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6676                    (int) BFD_RELOC_LO16, breg);
6677       return;
6678
6679    /* New code added to support COPZ instructions.
6680       This code builds table entries out of the macros in mip_opcodes.
6681       R4000 uses interlocks to handle coproc delays.
6682       Other chips (like the R3000) require nops to be inserted for delays.
6683
6684       FIXME: Currently, we require that the user handle delays.
6685       In order to fill delay slots for non-interlocked chips,
6686       we must have a way to specify delays based on the coprocessor.
6687       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6688       What are the side-effects of the cop instruction?
6689       What cache support might we have and what are its effects?
6690       Both coprocessor & memory require delays. how long???
6691       What registers are read/set/modified?
6692
6693       If an itbl is provided to interpret cop instructions,
6694       this knowledge can be encoded in the itbl spec.  */
6695
6696     case M_COP0:
6697       s = "c0";
6698       goto copz;
6699     case M_COP1:
6700       s = "c1";
6701       goto copz;
6702     case M_COP2:
6703       s = "c2";
6704       goto copz;
6705     case M_COP3:
6706       s = "c3";
6707     copz:
6708       /* For now we just do C (same as Cz).  The parameter will be
6709          stored in insn_opcode by mips_ip.  */
6710       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6711                    ip->insn_opcode);
6712       return;
6713
6714     case M_MOVE:
6715       move_register (&icnt, dreg, sreg);
6716       return;
6717
6718 #ifdef LOSING_COMPILER
6719     default:
6720       /* Try and see if this is a new itbl instruction.
6721          This code builds table entries out of the macros in mip_opcodes.
6722          FIXME: For now we just assemble the expression and pass it's
6723          value along as a 32-bit immediate.
6724          We may want to have the assembler assemble this value,
6725          so that we gain the assembler's knowledge of delay slots,
6726          symbols, etc.
6727          Would it be more efficient to use mask (id) here? */
6728       if (itbl_have_entries
6729           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6730         {
6731           s = ip->insn_mo->name;
6732           s2 = "cop3";
6733           coproc = ITBL_DECODE_PNUM (immed_expr);;
6734           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6735           return;
6736         }
6737       macro2 (ip);
6738       return;
6739     }
6740   if (mips_opts.noat)
6741     as_warn (_("Macro used $at after \".set noat\""));
6742 }
6743
6744 static void
6745 macro2 (ip)
6746      struct mips_cl_insn *ip;
6747 {
6748   register int treg, sreg, dreg, breg;
6749   int tempreg;
6750   int mask;
6751   int icnt = 0;
6752   int used_at;
6753   expressionS expr1;
6754   const char *s;
6755   const char *s2;
6756   const char *fmt;
6757   int likely = 0;
6758   int dbl = 0;
6759   int coproc = 0;
6760   int lr = 0;
6761   int imm = 0;
6762   int off;
6763   offsetT maxnum;
6764   bfd_reloc_code_real_type r;
6765   char *p;
6766
6767   treg = (ip->insn_opcode >> 16) & 0x1f;
6768   dreg = (ip->insn_opcode >> 11) & 0x1f;
6769   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6770   mask = ip->insn_mo->mask;
6771
6772   expr1.X_op = O_constant;
6773   expr1.X_op_symbol = NULL;
6774   expr1.X_add_symbol = NULL;
6775   expr1.X_add_number = 1;
6776
6777   switch (mask)
6778     {
6779 #endif /* LOSING_COMPILER */
6780
6781     case M_DMUL:
6782       dbl = 1;
6783     case M_MUL:
6784       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6785                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6786       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6787                    dreg);
6788       return;
6789
6790     case M_DMUL_I:
6791       dbl = 1;
6792     case M_MUL_I:
6793       /* The MIPS assembler some times generates shifts and adds.  I'm
6794          not trying to be that fancy. GCC should do this for us
6795          anyway.  */
6796       load_register (&icnt, AT, &imm_expr, dbl);
6797       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6798                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6799       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6800                    dreg);
6801       break;
6802
6803     case M_DMULO_I:
6804       dbl = 1;
6805     case M_MULO_I:
6806       imm = 1;
6807       goto do_mulo;
6808
6809     case M_DMULO:
6810       dbl = 1;
6811     case M_MULO:
6812     do_mulo:
6813       mips_emit_delays (true);
6814       ++mips_opts.noreorder;
6815       mips_any_noreorder = 1;
6816       if (imm)
6817         load_register (&icnt, AT, &imm_expr, dbl);
6818       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6819                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6820       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6821                    dreg);
6822       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6823                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6824       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6825                    AT);
6826       if (mips_trap)
6827         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6828                      "s,t,q", dreg, AT, 6);
6829       else
6830         {
6831           expr1.X_add_number = 8;
6832           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6833                        AT);
6834           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6835                        0);
6836           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6837                        "c", 6);
6838         }
6839       --mips_opts.noreorder;
6840       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6841       break;
6842
6843     case M_DMULOU_I:
6844       dbl = 1;
6845     case M_MULOU_I:
6846       imm = 1;
6847       goto do_mulou;
6848
6849     case M_DMULOU:
6850       dbl = 1;
6851     case M_MULOU:
6852     do_mulou:
6853       mips_emit_delays (true);
6854       ++mips_opts.noreorder;
6855       mips_any_noreorder = 1;
6856       if (imm)
6857         load_register (&icnt, AT, &imm_expr, dbl);
6858       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6859                    dbl ? "dmultu" : "multu",
6860                    "s,t", sreg, imm ? AT : treg);
6861       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6862                    AT);
6863       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6864                    dreg);
6865       if (mips_trap)
6866         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6867                      "s,t,q", AT, 0, 6);
6868       else
6869         {
6870           expr1.X_add_number = 8;
6871           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6872           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6873                        0);
6874           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6875                        "c", 6);
6876         }
6877       --mips_opts.noreorder;
6878       break;
6879
6880     case M_DROL:
6881       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6882                    "d,v,t", AT, 0, treg);
6883       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6884                    "d,t,s", AT, sreg, AT);
6885       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6886                    "d,t,s", dreg, sreg, treg);
6887       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6888                    "d,v,t", dreg, dreg, AT);
6889       break;
6890
6891     case M_ROL:
6892       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6893                    "d,v,t", AT, 0, treg);
6894       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6895                    "d,t,s", AT, sreg, AT);
6896       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6897                    "d,t,s", dreg, sreg, treg);
6898       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6899                    "d,v,t", dreg, dreg, AT);
6900       break;
6901
6902     case M_DROL_I:
6903       {
6904         unsigned int rot;
6905
6906         if (imm_expr.X_op != O_constant)
6907           as_bad (_("rotate count too large"));
6908         rot = imm_expr.X_add_number & 0x3f;
6909         if (CPU_HAS_DROR (mips_arch))
6910           {
6911             rot = (64 - rot) & 0x3f;
6912             if (rot >= 32)
6913               macro_build ((char *) NULL, &icnt, NULL, "dror32",
6914                            "d,w,<", dreg, sreg, rot - 32);
6915             else
6916               macro_build ((char *) NULL, &icnt, NULL, "dror",
6917                            "d,w,<", dreg, sreg, rot);
6918             break;
6919           }
6920         if (rot == 0)
6921           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6922                        "d,w,<", dreg, sreg, 0);
6923         else
6924           {
6925             char *l, *r;
6926
6927             l = (rot < 0x20) ? "dsll" : "dsll32";
6928             r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6929             rot &= 0x1f;
6930             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6931                          "d,w,<", AT, sreg, rot);
6932             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6933                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6934             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6935                          "d,v,t", dreg, dreg, AT);
6936           }
6937       }
6938       break;
6939
6940     case M_ROL_I:
6941       {
6942         unsigned int rot;
6943
6944         if (imm_expr.X_op != O_constant)
6945           as_bad (_("rotate count too large"));
6946         rot = imm_expr.X_add_number & 0x1f;
6947         if (CPU_HAS_ROR (mips_arch))
6948           {
6949             macro_build ((char *) NULL, &icnt, NULL, "ror",
6950                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6951             break;
6952           }
6953         if (rot == 0)
6954           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6955                        "d,w,<", dreg, sreg, 0);
6956         else
6957           {
6958             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6959                          "d,w,<", AT, sreg, rot);
6960             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6961                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6962             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6963                          "d,v,t", dreg, dreg, AT);
6964           }
6965       }
6966       break;
6967
6968     case M_DROR:
6969       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6970                    "d,v,t", AT, 0, treg);
6971       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6972                    "d,t,s", AT, sreg, AT);
6973       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6974                    "d,t,s", dreg, sreg, treg);
6975       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6976                    "d,v,t", dreg, dreg, AT);
6977       break;
6978
6979     case M_ROR:
6980       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6981                    "d,v,t", AT, 0, treg);
6982       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6983                    "d,t,s", AT, sreg, AT);
6984       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6985                    "d,t,s", dreg, sreg, treg);
6986       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6987                    "d,v,t", dreg, dreg, AT);
6988       break;
6989
6990     case M_DROR_I:
6991       {
6992         unsigned int rot;
6993
6994         if (imm_expr.X_op != O_constant)
6995           as_bad (_("rotate count too large"));
6996         rot = imm_expr.X_add_number & 0x3f;
6997         if (rot == 0)
6998           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6999                        "d,w,<", dreg, sreg, 0);
7000         else
7001           {
7002             char *l, *r;
7003
7004             r = (rot < 0x20) ? "dsrl" : "dsrl32";
7005             l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7006             rot &= 0x1f;
7007             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7008                          "d,w,<", AT, sreg, rot);
7009             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7010                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7011             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7012                          "d,v,t", dreg, dreg, AT);
7013           }
7014       }
7015       break;
7016
7017     case M_ROR_I:
7018       {
7019         unsigned int rot;
7020
7021         if (imm_expr.X_op != O_constant)
7022           as_bad (_("rotate count too large"));
7023         rot = imm_expr.X_add_number & 0x1f;
7024         if (rot == 0)
7025           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7026                        "d,w,<", dreg, sreg, 0);
7027         else
7028           {
7029             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7030                          "d,w,<", AT, sreg, rot);
7031             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7032                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7033             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7034                          "d,v,t", dreg, dreg, AT);
7035           }
7036       }
7037       break;
7038
7039     case M_S_DOB:
7040       if (mips_arch == CPU_R4650)
7041         {
7042           as_bad (_("opcode not supported on this processor"));
7043           return;
7044         }
7045       assert (mips_opts.isa == ISA_MIPS1);
7046       /* Even on a big endian machine $fn comes before $fn+1.  We have
7047          to adjust when storing to memory.  */
7048       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7049                    target_big_endian ? treg + 1 : treg,
7050                    (int) BFD_RELOC_LO16, breg);
7051       offset_expr.X_add_number += 4;
7052       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7053                    target_big_endian ? treg : treg + 1,
7054                    (int) BFD_RELOC_LO16, breg);
7055       return;
7056
7057     case M_SEQ:
7058       if (sreg == 0)
7059         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7060                      treg, (int) BFD_RELOC_LO16);
7061       else if (treg == 0)
7062         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7063                      sreg, (int) BFD_RELOC_LO16);
7064       else
7065         {
7066           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7067                        "d,v,t", dreg, sreg, treg);
7068           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7069                        dreg, (int) BFD_RELOC_LO16);
7070         }
7071       return;
7072
7073     case M_SEQ_I:
7074       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7075         {
7076           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7077                        sreg, (int) BFD_RELOC_LO16);
7078           return;
7079         }
7080       if (sreg == 0)
7081         {
7082           as_warn (_("Instruction %s: result is always false"),
7083                    ip->insn_mo->name);
7084           move_register (&icnt, dreg, 0);
7085           return;
7086         }
7087       if (imm_expr.X_op == O_constant
7088           && imm_expr.X_add_number >= 0
7089           && imm_expr.X_add_number < 0x10000)
7090         {
7091           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7092                        sreg, (int) BFD_RELOC_LO16);
7093           used_at = 0;
7094         }
7095       else if (imm_expr.X_op == O_constant
7096                && imm_expr.X_add_number > -0x8000
7097                && imm_expr.X_add_number < 0)
7098         {
7099           imm_expr.X_add_number = -imm_expr.X_add_number;
7100           macro_build ((char *) NULL, &icnt, &imm_expr,
7101                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7102                        "t,r,j", dreg, sreg,
7103                        (int) BFD_RELOC_LO16);
7104           used_at = 0;
7105         }
7106       else
7107         {
7108           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7109           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7110                        "d,v,t", dreg, sreg, AT);
7111           used_at = 1;
7112         }
7113       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7114                    (int) BFD_RELOC_LO16);
7115       if (used_at)
7116         break;
7117       return;
7118
7119     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7120       s = "slt";
7121       goto sge;
7122     case M_SGEU:
7123       s = "sltu";
7124     sge:
7125       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7126                    dreg, sreg, treg);
7127       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7128                    (int) BFD_RELOC_LO16);
7129       return;
7130
7131     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7132     case M_SGEU_I:
7133       if (imm_expr.X_op == O_constant
7134           && imm_expr.X_add_number >= -0x8000
7135           && imm_expr.X_add_number < 0x8000)
7136         {
7137           macro_build ((char *) NULL, &icnt, &imm_expr,
7138                        mask == M_SGE_I ? "slti" : "sltiu",
7139                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7140           used_at = 0;
7141         }
7142       else
7143         {
7144           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7145           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7146                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7147                        AT);
7148           used_at = 1;
7149         }
7150       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7151                    (int) BFD_RELOC_LO16);
7152       if (used_at)
7153         break;
7154       return;
7155
7156     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7157       s = "slt";
7158       goto sgt;
7159     case M_SGTU:
7160       s = "sltu";
7161     sgt:
7162       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7163                    dreg, treg, sreg);
7164       return;
7165
7166     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7167       s = "slt";
7168       goto sgti;
7169     case M_SGTU_I:
7170       s = "sltu";
7171     sgti:
7172       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7173       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7174                    dreg, AT, sreg);
7175       break;
7176
7177     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7178       s = "slt";
7179       goto sle;
7180     case M_SLEU:
7181       s = "sltu";
7182     sle:
7183       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7184                    dreg, treg, sreg);
7185       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7186                    (int) BFD_RELOC_LO16);
7187       return;
7188
7189     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7190       s = "slt";
7191       goto slei;
7192     case M_SLEU_I:
7193       s = "sltu";
7194     slei:
7195       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7196       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7197                    dreg, AT, sreg);
7198       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7199                    (int) BFD_RELOC_LO16);
7200       break;
7201
7202     case M_SLT_I:
7203       if (imm_expr.X_op == O_constant
7204           && imm_expr.X_add_number >= -0x8000
7205           && imm_expr.X_add_number < 0x8000)
7206         {
7207           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7208                        dreg, sreg, (int) BFD_RELOC_LO16);
7209           return;
7210         }
7211       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7212       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7213                    dreg, sreg, AT);
7214       break;
7215
7216     case M_SLTU_I:
7217       if (imm_expr.X_op == O_constant
7218           && imm_expr.X_add_number >= -0x8000
7219           && imm_expr.X_add_number < 0x8000)
7220         {
7221           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7222                        dreg, sreg, (int) BFD_RELOC_LO16);
7223           return;
7224         }
7225       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7226       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7227                    "d,v,t", dreg, sreg, AT);
7228       break;
7229
7230     case M_SNE:
7231       if (sreg == 0)
7232         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7233                      "d,v,t", dreg, 0, treg);
7234       else if (treg == 0)
7235         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7236                      "d,v,t", dreg, 0, sreg);
7237       else
7238         {
7239           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7240                        "d,v,t", dreg, sreg, treg);
7241           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7242                        "d,v,t", dreg, 0, dreg);
7243         }
7244       return;
7245
7246     case M_SNE_I:
7247       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7248         {
7249           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7250                        "d,v,t", dreg, 0, sreg);
7251           return;
7252         }
7253       if (sreg == 0)
7254         {
7255           as_warn (_("Instruction %s: result is always true"),
7256                    ip->insn_mo->name);
7257           macro_build ((char *) NULL, &icnt, &expr1,
7258                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7259                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7260           return;
7261         }
7262       if (imm_expr.X_op == O_constant
7263           && imm_expr.X_add_number >= 0
7264           && imm_expr.X_add_number < 0x10000)
7265         {
7266           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7267                        dreg, sreg, (int) BFD_RELOC_LO16);
7268           used_at = 0;
7269         }
7270       else if (imm_expr.X_op == O_constant
7271                && imm_expr.X_add_number > -0x8000
7272                && imm_expr.X_add_number < 0)
7273         {
7274           imm_expr.X_add_number = -imm_expr.X_add_number;
7275           macro_build ((char *) NULL, &icnt, &imm_expr,
7276                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7277                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7278           used_at = 0;
7279         }
7280       else
7281         {
7282           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7283           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7284                        "d,v,t", dreg, sreg, AT);
7285           used_at = 1;
7286         }
7287       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7288                    "d,v,t", dreg, 0, dreg);
7289       if (used_at)
7290         break;
7291       return;
7292
7293     case M_DSUB_I:
7294       dbl = 1;
7295     case M_SUB_I:
7296       if (imm_expr.X_op == O_constant
7297           && imm_expr.X_add_number > -0x8000
7298           && imm_expr.X_add_number <= 0x8000)
7299         {
7300           imm_expr.X_add_number = -imm_expr.X_add_number;
7301           macro_build ((char *) NULL, &icnt, &imm_expr,
7302                        dbl ? "daddi" : "addi",
7303                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7304           return;
7305         }
7306       load_register (&icnt, AT, &imm_expr, dbl);
7307       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7308                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7309       break;
7310
7311     case M_DSUBU_I:
7312       dbl = 1;
7313     case M_SUBU_I:
7314       if (imm_expr.X_op == O_constant
7315           && imm_expr.X_add_number > -0x8000
7316           && imm_expr.X_add_number <= 0x8000)
7317         {
7318           imm_expr.X_add_number = -imm_expr.X_add_number;
7319           macro_build ((char *) NULL, &icnt, &imm_expr,
7320                        dbl ? "daddiu" : "addiu",
7321                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7322           return;
7323         }
7324       load_register (&icnt, AT, &imm_expr, dbl);
7325       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7326                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7327       break;
7328
7329     case M_TEQ_I:
7330       s = "teq";
7331       goto trap;
7332     case M_TGE_I:
7333       s = "tge";
7334       goto trap;
7335     case M_TGEU_I:
7336       s = "tgeu";
7337       goto trap;
7338     case M_TLT_I:
7339       s = "tlt";
7340       goto trap;
7341     case M_TLTU_I:
7342       s = "tltu";
7343       goto trap;
7344     case M_TNE_I:
7345       s = "tne";
7346     trap:
7347       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7348       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7349                    AT);
7350       break;
7351
7352     case M_TRUNCWS:
7353     case M_TRUNCWD:
7354       assert (mips_opts.isa == ISA_MIPS1);
7355       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7356       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7357
7358       /*
7359        * Is the double cfc1 instruction a bug in the mips assembler;
7360        * or is there a reason for it?
7361        */
7362       mips_emit_delays (true);
7363       ++mips_opts.noreorder;
7364       mips_any_noreorder = 1;
7365       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7366                    treg, RA);
7367       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7368                    treg, RA);
7369       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7370       expr1.X_add_number = 3;
7371       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7372                    (int) BFD_RELOC_LO16);
7373       expr1.X_add_number = 2;
7374       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7375                      (int) BFD_RELOC_LO16);
7376       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7377                    AT, RA);
7378       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7379       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7380               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7381       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7382                    treg, RA);
7383       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7384       --mips_opts.noreorder;
7385       break;
7386
7387     case M_ULH:
7388       s = "lb";
7389       goto ulh;
7390     case M_ULHU:
7391       s = "lbu";
7392     ulh:
7393       if (offset_expr.X_add_number >= 0x7fff)
7394         as_bad (_("operand overflow"));
7395       /* avoid load delay */
7396       if (! target_big_endian)
7397         ++offset_expr.X_add_number;
7398       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7399                    (int) BFD_RELOC_LO16, breg);
7400       if (! target_big_endian)
7401         --offset_expr.X_add_number;
7402       else
7403         ++offset_expr.X_add_number;
7404       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7405                    (int) BFD_RELOC_LO16, breg);
7406       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7407                    treg, treg, 8);
7408       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7409                    treg, treg, AT);
7410       break;
7411
7412     case M_ULD:
7413       s = "ldl";
7414       s2 = "ldr";
7415       off = 7;
7416       goto ulw;
7417     case M_ULW:
7418       s = "lwl";
7419       s2 = "lwr";
7420       off = 3;
7421     ulw:
7422       if (offset_expr.X_add_number >= 0x8000 - off)
7423         as_bad (_("operand overflow"));
7424       if (! target_big_endian)
7425         offset_expr.X_add_number += off;
7426       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7427                    (int) BFD_RELOC_LO16, breg);
7428       if (! target_big_endian)
7429         offset_expr.X_add_number -= off;
7430       else
7431         offset_expr.X_add_number += off;
7432       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7433                    (int) BFD_RELOC_LO16, breg);
7434       return;
7435
7436     case M_ULD_A:
7437       s = "ldl";
7438       s2 = "ldr";
7439       off = 7;
7440       goto ulwa;
7441     case M_ULW_A:
7442       s = "lwl";
7443       s2 = "lwr";
7444       off = 3;
7445     ulwa:
7446       used_at = 1;
7447       load_address (&icnt, AT, &offset_expr, &used_at);
7448       if (breg != 0)
7449         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7450                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7451                      "d,v,t", AT, AT, breg);
7452       if (! target_big_endian)
7453         expr1.X_add_number = off;
7454       else
7455         expr1.X_add_number = 0;
7456       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7457                    (int) BFD_RELOC_LO16, AT);
7458       if (! target_big_endian)
7459         expr1.X_add_number = 0;
7460       else
7461         expr1.X_add_number = off;
7462       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7463                    (int) BFD_RELOC_LO16, AT);
7464       break;
7465
7466     case M_ULH_A:
7467     case M_ULHU_A:
7468       used_at = 1;
7469       load_address (&icnt, AT, &offset_expr, &used_at);
7470       if (breg != 0)
7471         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7472                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7473                      "d,v,t", AT, AT, breg);
7474       if (target_big_endian)
7475         expr1.X_add_number = 0;
7476       macro_build ((char *) NULL, &icnt, &expr1,
7477                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7478                    (int) BFD_RELOC_LO16, AT);
7479       if (target_big_endian)
7480         expr1.X_add_number = 1;
7481       else
7482         expr1.X_add_number = 0;
7483       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7484                    (int) BFD_RELOC_LO16, AT);
7485       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7486                    treg, treg, 8);
7487       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7488                    treg, treg, AT);
7489       break;
7490
7491     case M_USH:
7492       if (offset_expr.X_add_number >= 0x7fff)
7493         as_bad (_("operand overflow"));
7494       if (target_big_endian)
7495         ++offset_expr.X_add_number;
7496       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7497                    (int) BFD_RELOC_LO16, breg);
7498       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7499                    AT, treg, 8);
7500       if (target_big_endian)
7501         --offset_expr.X_add_number;
7502       else
7503         ++offset_expr.X_add_number;
7504       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7505                    (int) BFD_RELOC_LO16, breg);
7506       break;
7507
7508     case M_USD:
7509       s = "sdl";
7510       s2 = "sdr";
7511       off = 7;
7512       goto usw;
7513     case M_USW:
7514       s = "swl";
7515       s2 = "swr";
7516       off = 3;
7517     usw:
7518       if (offset_expr.X_add_number >= 0x8000 - off)
7519         as_bad (_("operand overflow"));
7520       if (! target_big_endian)
7521         offset_expr.X_add_number += off;
7522       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7523                    (int) BFD_RELOC_LO16, breg);
7524       if (! target_big_endian)
7525         offset_expr.X_add_number -= off;
7526       else
7527         offset_expr.X_add_number += off;
7528       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7529                    (int) BFD_RELOC_LO16, breg);
7530       return;
7531
7532     case M_USD_A:
7533       s = "sdl";
7534       s2 = "sdr";
7535       off = 7;
7536       goto uswa;
7537     case M_USW_A:
7538       s = "swl";
7539       s2 = "swr";
7540       off = 3;
7541     uswa:
7542       used_at = 1;
7543       load_address (&icnt, AT, &offset_expr, &used_at);
7544       if (breg != 0)
7545         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7546                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7547                      "d,v,t", AT, AT, breg);
7548       if (! target_big_endian)
7549         expr1.X_add_number = off;
7550       else
7551         expr1.X_add_number = 0;
7552       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7553                    (int) BFD_RELOC_LO16, AT);
7554       if (! target_big_endian)
7555         expr1.X_add_number = 0;
7556       else
7557         expr1.X_add_number = off;
7558       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7559                    (int) BFD_RELOC_LO16, AT);
7560       break;
7561
7562     case M_USH_A:
7563       used_at = 1;
7564       load_address (&icnt, AT, &offset_expr, &used_at);
7565       if (breg != 0)
7566         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7567                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7568                      "d,v,t", AT, AT, breg);
7569       if (! target_big_endian)
7570         expr1.X_add_number = 0;
7571       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7572                    (int) BFD_RELOC_LO16, AT);
7573       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7574                    treg, treg, 8);
7575       if (! target_big_endian)
7576         expr1.X_add_number = 1;
7577       else
7578         expr1.X_add_number = 0;
7579       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7580                    (int) BFD_RELOC_LO16, AT);
7581       if (! target_big_endian)
7582         expr1.X_add_number = 0;
7583       else
7584         expr1.X_add_number = 1;
7585       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7586                    (int) BFD_RELOC_LO16, AT);
7587       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7588                    treg, treg, 8);
7589       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7590                    treg, treg, AT);
7591       break;
7592
7593     default:
7594       /* FIXME: Check if this is one of the itbl macros, since they
7595          are added dynamically.  */
7596       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7597       break;
7598     }
7599   if (mips_opts.noat)
7600     as_warn (_("Macro used $at after \".set noat\""));
7601 }
7602
7603 /* Implement macros in mips16 mode.  */
7604
7605 static void
7606 mips16_macro (ip)
7607      struct mips_cl_insn *ip;
7608 {
7609   int mask;
7610   int xreg, yreg, zreg, tmp;
7611   int icnt;
7612   expressionS expr1;
7613   int dbl;
7614   const char *s, *s2, *s3;
7615
7616   mask = ip->insn_mo->mask;
7617
7618   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7619   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7620   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7621
7622   icnt = 0;
7623
7624   expr1.X_op = O_constant;
7625   expr1.X_op_symbol = NULL;
7626   expr1.X_add_symbol = NULL;
7627   expr1.X_add_number = 1;
7628
7629   dbl = 0;
7630
7631   switch (mask)
7632     {
7633     default:
7634       internalError ();
7635
7636     case M_DDIV_3:
7637       dbl = 1;
7638     case M_DIV_3:
7639       s = "mflo";
7640       goto do_div3;
7641     case M_DREM_3:
7642       dbl = 1;
7643     case M_REM_3:
7644       s = "mfhi";
7645     do_div3:
7646       mips_emit_delays (true);
7647       ++mips_opts.noreorder;
7648       mips_any_noreorder = 1;
7649       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7650                    dbl ? "ddiv" : "div",
7651                    "0,x,y", xreg, yreg);
7652       expr1.X_add_number = 2;
7653       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7654       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7655                    7);
7656
7657       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7658          since that causes an overflow.  We should do that as well,
7659          but I don't see how to do the comparisons without a temporary
7660          register.  */
7661       --mips_opts.noreorder;
7662       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7663       break;
7664
7665     case M_DIVU_3:
7666       s = "divu";
7667       s2 = "mflo";
7668       goto do_divu3;
7669     case M_REMU_3:
7670       s = "divu";
7671       s2 = "mfhi";
7672       goto do_divu3;
7673     case M_DDIVU_3:
7674       s = "ddivu";
7675       s2 = "mflo";
7676       goto do_divu3;
7677     case M_DREMU_3:
7678       s = "ddivu";
7679       s2 = "mfhi";
7680     do_divu3:
7681       mips_emit_delays (true);
7682       ++mips_opts.noreorder;
7683       mips_any_noreorder = 1;
7684       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7685                    xreg, yreg);
7686       expr1.X_add_number = 2;
7687       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7688       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7689                    "6", 7);
7690       --mips_opts.noreorder;
7691       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7692       break;
7693
7694     case M_DMUL:
7695       dbl = 1;
7696     case M_MUL:
7697       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7698                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7699       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7700                    zreg);
7701       return;
7702
7703     case M_DSUBU_I:
7704       dbl = 1;
7705       goto do_subu;
7706     case M_SUBU_I:
7707     do_subu:
7708       if (imm_expr.X_op != O_constant)
7709         as_bad (_("Unsupported large constant"));
7710       imm_expr.X_add_number = -imm_expr.X_add_number;
7711       macro_build ((char *) NULL, &icnt, &imm_expr,
7712                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7713       break;
7714
7715     case M_SUBU_I_2:
7716       if (imm_expr.X_op != O_constant)
7717         as_bad (_("Unsupported large constant"));
7718       imm_expr.X_add_number = -imm_expr.X_add_number;
7719       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7720                    "x,k", xreg);
7721       break;
7722
7723     case M_DSUBU_I_2:
7724       if (imm_expr.X_op != O_constant)
7725         as_bad (_("Unsupported large constant"));
7726       imm_expr.X_add_number = -imm_expr.X_add_number;
7727       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7728                    "y,j", yreg);
7729       break;
7730
7731     case M_BEQ:
7732       s = "cmp";
7733       s2 = "bteqz";
7734       goto do_branch;
7735     case M_BNE:
7736       s = "cmp";
7737       s2 = "btnez";
7738       goto do_branch;
7739     case M_BLT:
7740       s = "slt";
7741       s2 = "btnez";
7742       goto do_branch;
7743     case M_BLTU:
7744       s = "sltu";
7745       s2 = "btnez";
7746       goto do_branch;
7747     case M_BLE:
7748       s = "slt";
7749       s2 = "bteqz";
7750       goto do_reverse_branch;
7751     case M_BLEU:
7752       s = "sltu";
7753       s2 = "bteqz";
7754       goto do_reverse_branch;
7755     case M_BGE:
7756       s = "slt";
7757       s2 = "bteqz";
7758       goto do_branch;
7759     case M_BGEU:
7760       s = "sltu";
7761       s2 = "bteqz";
7762       goto do_branch;
7763     case M_BGT:
7764       s = "slt";
7765       s2 = "btnez";
7766       goto do_reverse_branch;
7767     case M_BGTU:
7768       s = "sltu";
7769       s2 = "btnez";
7770
7771     do_reverse_branch:
7772       tmp = xreg;
7773       xreg = yreg;
7774       yreg = tmp;
7775
7776     do_branch:
7777       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7778                    xreg, yreg);
7779       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7780       break;
7781
7782     case M_BEQ_I:
7783       s = "cmpi";
7784       s2 = "bteqz";
7785       s3 = "x,U";
7786       goto do_branch_i;
7787     case M_BNE_I:
7788       s = "cmpi";
7789       s2 = "btnez";
7790       s3 = "x,U";
7791       goto do_branch_i;
7792     case M_BLT_I:
7793       s = "slti";
7794       s2 = "btnez";
7795       s3 = "x,8";
7796       goto do_branch_i;
7797     case M_BLTU_I:
7798       s = "sltiu";
7799       s2 = "btnez";
7800       s3 = "x,8";
7801       goto do_branch_i;
7802     case M_BLE_I:
7803       s = "slti";
7804       s2 = "btnez";
7805       s3 = "x,8";
7806       goto do_addone_branch_i;
7807     case M_BLEU_I:
7808       s = "sltiu";
7809       s2 = "btnez";
7810       s3 = "x,8";
7811       goto do_addone_branch_i;
7812     case M_BGE_I:
7813       s = "slti";
7814       s2 = "bteqz";
7815       s3 = "x,8";
7816       goto do_branch_i;
7817     case M_BGEU_I:
7818       s = "sltiu";
7819       s2 = "bteqz";
7820       s3 = "x,8";
7821       goto do_branch_i;
7822     case M_BGT_I:
7823       s = "slti";
7824       s2 = "bteqz";
7825       s3 = "x,8";
7826       goto do_addone_branch_i;
7827     case M_BGTU_I:
7828       s = "sltiu";
7829       s2 = "bteqz";
7830       s3 = "x,8";
7831
7832     do_addone_branch_i:
7833       if (imm_expr.X_op != O_constant)
7834         as_bad (_("Unsupported large constant"));
7835       ++imm_expr.X_add_number;
7836
7837     do_branch_i:
7838       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7839       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7840       break;
7841
7842     case M_ABS:
7843       expr1.X_add_number = 0;
7844       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7845       if (xreg != yreg)
7846         move_register (&icnt, xreg, yreg);
7847       expr1.X_add_number = 2;
7848       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7849       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7850                    "neg", "x,w", xreg, xreg);
7851     }
7852 }
7853
7854 /* For consistency checking, verify that all bits are specified either
7855    by the match/mask part of the instruction definition, or by the
7856    operand list.  */
7857 static int
7858 validate_mips_insn (opc)
7859      const struct mips_opcode *opc;
7860 {
7861   const char *p = opc->args;
7862   char c;
7863   unsigned long used_bits = opc->mask;
7864
7865   if ((used_bits & opc->match) != opc->match)
7866     {
7867       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7868               opc->name, opc->args);
7869       return 0;
7870     }
7871 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7872   while (*p)
7873     switch (c = *p++)
7874       {
7875       case ',': break;
7876       case '(': break;
7877       case ')': break;
7878       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7879       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7880       case 'A': break;
7881       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7882       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7883       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7884       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7885       case 'F': break;
7886       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7887       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7888       case 'I': break;
7889       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7890       case 'L': break;
7891       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7892       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7893       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7894       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7895                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7896       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7897       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7898       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7899       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7900       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7901       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7902       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7903       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7904       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7905       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7906       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7907       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7908       case 'f': break;
7909       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7910       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7911       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7912       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7913       case 'l': break;
7914       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7915       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7916       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7917       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7918       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7919       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7920       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7921       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7922       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7923       case 'x': break;
7924       case 'z': break;
7925       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7926       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7927                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7928       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
7929       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
7930       case '[': break;
7931       case ']': break;
7932       default:
7933         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7934                 c, opc->name, opc->args);
7935         return 0;
7936       }
7937 #undef USE_BITS
7938   if (used_bits != 0xffffffff)
7939     {
7940       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7941               ~used_bits & 0xffffffff, opc->name, opc->args);
7942       return 0;
7943     }
7944   return 1;
7945 }
7946
7947 /* This routine assembles an instruction into its binary format.  As a
7948    side effect, it sets one of the global variables imm_reloc or
7949    offset_reloc to the type of relocation to do if one of the operands
7950    is an address expression.  */
7951
7952 static void
7953 mips_ip (str, ip)
7954      char *str;
7955      struct mips_cl_insn *ip;
7956 {
7957   char *s;
7958   const char *args;
7959   char c = 0;
7960   struct mips_opcode *insn;
7961   char *argsStart;
7962   unsigned int regno;
7963   unsigned int lastregno = 0;
7964   char *s_reset;
7965   char save_c = 0;
7966
7967   insn_error = NULL;
7968
7969   /* If the instruction contains a '.', we first try to match an instruction
7970      including the '.'.  Then we try again without the '.'.  */
7971   insn = NULL;
7972   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7973     continue;
7974
7975   /* If we stopped on whitespace, then replace the whitespace with null for
7976      the call to hash_find.  Save the character we replaced just in case we
7977      have to re-parse the instruction.  */
7978   if (ISSPACE (*s))
7979     {
7980       save_c = *s;
7981       *s++ = '\0';
7982     }
7983
7984   insn = (struct mips_opcode *) hash_find (op_hash, str);
7985
7986   /* If we didn't find the instruction in the opcode table, try again, but
7987      this time with just the instruction up to, but not including the
7988      first '.'.  */
7989   if (insn == NULL)
7990     {
7991       /* Restore the character we overwrite above (if any).  */
7992       if (save_c)
7993         *(--s) = save_c;
7994
7995       /* Scan up to the first '.' or whitespace.  */
7996       for (s = str;
7997            *s != '\0' && *s != '.' && !ISSPACE (*s);
7998            ++s)
7999         continue;
8000
8001       /* If we did not find a '.', then we can quit now.  */
8002       if (*s != '.')
8003         {
8004           insn_error = "unrecognized opcode";
8005           return;
8006         }
8007
8008       /* Lookup the instruction in the hash table.  */
8009       *s++ = '\0';
8010       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8011         {
8012           insn_error = "unrecognized opcode";
8013           return;
8014         }
8015     }
8016
8017   argsStart = s;
8018   for (;;)
8019     {
8020       boolean ok;
8021
8022       assert (strcmp (insn->name, str) == 0);
8023
8024       if (OPCODE_IS_MEMBER (insn,
8025                             (mips_opts.isa
8026                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8027                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8028                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8029                             mips_arch))
8030         ok = true;
8031       else
8032         ok = false;
8033
8034       if (insn->pinfo != INSN_MACRO)
8035         {
8036           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8037             ok = false;
8038         }
8039
8040       if (! ok)
8041         {
8042           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8043               && strcmp (insn->name, insn[1].name) == 0)
8044             {
8045               ++insn;
8046               continue;
8047             }
8048           else
8049             {
8050               if (!insn_error)
8051                 {
8052                   static char buf[100];
8053                   if (mips_arch_info->is_isa)
8054                     sprintf (buf,
8055                              _("opcode not supported at this ISA level (%s)"),
8056                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8057                   else
8058                     sprintf (buf,
8059                              _("opcode not supported on this processor: %s (%s)"),
8060                              mips_arch_info->name,
8061                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8062                   insn_error = buf;
8063                 }
8064               if (save_c)
8065                 *(--s) = save_c;
8066               return;
8067             }
8068         }
8069
8070       ip->insn_mo = insn;
8071       ip->insn_opcode = insn->match;
8072       insn_error = NULL;
8073       for (args = insn->args;; ++args)
8074         {
8075           int is_mdmx;
8076
8077           s += strspn (s, " \t");
8078           is_mdmx = 0;
8079           switch (*args)
8080             {
8081             case '\0':          /* end of args */
8082               if (*s == '\0')
8083                 return;
8084               break;
8085
8086             case ',':
8087               if (*s++ == *args)
8088                 continue;
8089               s--;
8090               switch (*++args)
8091                 {
8092                 case 'r':
8093                 case 'v':
8094                   ip->insn_opcode |= lastregno << OP_SH_RS;
8095                   continue;
8096
8097                 case 'w':
8098                   ip->insn_opcode |= lastregno << OP_SH_RT;
8099                   continue;
8100
8101                 case 'W':
8102                   ip->insn_opcode |= lastregno << OP_SH_FT;
8103                   continue;
8104
8105                 case 'V':
8106                   ip->insn_opcode |= lastregno << OP_SH_FS;
8107                   continue;
8108                 }
8109               break;
8110
8111             case '(':
8112               /* Handle optional base register.
8113                  Either the base register is omitted or
8114                  we must have a left paren.  */
8115               /* This is dependent on the next operand specifier
8116                  is a base register specification.  */
8117               assert (args[1] == 'b' || args[1] == '5'
8118                       || args[1] == '-' || args[1] == '4');
8119               if (*s == '\0')
8120                 return;
8121
8122             case ')':           /* these must match exactly */
8123             case '[':
8124             case ']':
8125               if (*s++ == *args)
8126                 continue;
8127               break;
8128
8129             case '<':           /* must be at least one digit */
8130               /*
8131                * According to the manual, if the shift amount is greater
8132                * than 31 or less than 0, then the shift amount should be
8133                * mod 32.  In reality the mips assembler issues an error.
8134                * We issue a warning and mask out all but the low 5 bits.
8135                */
8136               my_getExpression (&imm_expr, s);
8137               check_absolute_expr (ip, &imm_expr);
8138               if ((unsigned long) imm_expr.X_add_number > 31)
8139                 {
8140                   as_warn (_("Improper shift amount (%lu)"),
8141                            (unsigned long) imm_expr.X_add_number);
8142                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8143                 }
8144               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8145               imm_expr.X_op = O_absent;
8146               s = expr_end;
8147               continue;
8148
8149             case '>':           /* shift amount minus 32 */
8150               my_getExpression (&imm_expr, s);
8151               check_absolute_expr (ip, &imm_expr);
8152               if ((unsigned long) imm_expr.X_add_number < 32
8153                   || (unsigned long) imm_expr.X_add_number > 63)
8154                 break;
8155               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8156               imm_expr.X_op = O_absent;
8157               s = expr_end;
8158               continue;
8159
8160             case 'k':           /* cache code */
8161             case 'h':           /* prefx code */
8162               my_getExpression (&imm_expr, s);
8163               check_absolute_expr (ip, &imm_expr);
8164               if ((unsigned long) imm_expr.X_add_number > 31)
8165                 {
8166                   as_warn (_("Invalid value for `%s' (%lu)"),
8167                            ip->insn_mo->name,
8168                            (unsigned long) imm_expr.X_add_number);
8169                   imm_expr.X_add_number &= 0x1f;
8170                 }
8171               if (*args == 'k')
8172                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8173               else
8174                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8175               imm_expr.X_op = O_absent;
8176               s = expr_end;
8177               continue;
8178
8179             case 'c':           /* break code */
8180               my_getExpression (&imm_expr, s);
8181               check_absolute_expr (ip, &imm_expr);
8182               if ((unsigned long) imm_expr.X_add_number > 1023)
8183                 {
8184                   as_warn (_("Illegal break code (%lu)"),
8185                            (unsigned long) imm_expr.X_add_number);
8186                   imm_expr.X_add_number &= OP_MASK_CODE;
8187                 }
8188               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8189               imm_expr.X_op = O_absent;
8190               s = expr_end;
8191               continue;
8192
8193             case 'q':           /* lower break code */
8194               my_getExpression (&imm_expr, s);
8195               check_absolute_expr (ip, &imm_expr);
8196               if ((unsigned long) imm_expr.X_add_number > 1023)
8197                 {
8198                   as_warn (_("Illegal lower break code (%lu)"),
8199                            (unsigned long) imm_expr.X_add_number);
8200                   imm_expr.X_add_number &= OP_MASK_CODE2;
8201                 }
8202               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8203               imm_expr.X_op = O_absent;
8204               s = expr_end;
8205               continue;
8206
8207             case 'B':           /* 20-bit syscall/break code.  */
8208               my_getExpression (&imm_expr, s);
8209               check_absolute_expr (ip, &imm_expr);
8210               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8211                 as_warn (_("Illegal 20-bit code (%lu)"),
8212                          (unsigned long) imm_expr.X_add_number);
8213               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8214               imm_expr.X_op = O_absent;
8215               s = expr_end;
8216               continue;
8217
8218             case 'C':           /* Coprocessor code */
8219               my_getExpression (&imm_expr, s);
8220               check_absolute_expr (ip, &imm_expr);
8221               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8222                 {
8223                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8224                            (unsigned long) imm_expr.X_add_number);
8225                   imm_expr.X_add_number &= ((1 << 25) - 1);
8226                 }
8227               ip->insn_opcode |= imm_expr.X_add_number;
8228               imm_expr.X_op = O_absent;
8229               s = expr_end;
8230               continue;
8231
8232             case 'J':           /* 19-bit wait code.  */
8233               my_getExpression (&imm_expr, s);
8234               check_absolute_expr (ip, &imm_expr);
8235               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8236                 as_warn (_("Illegal 19-bit code (%lu)"),
8237                          (unsigned long) imm_expr.X_add_number);
8238               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8239               imm_expr.X_op = O_absent;
8240               s = expr_end;
8241               continue;
8242
8243             case 'P':           /* Performance register */
8244               my_getExpression (&imm_expr, s);
8245               check_absolute_expr (ip, &imm_expr);
8246               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8247                 {
8248                   as_warn (_("Invalid performance register (%lu)"),
8249                            (unsigned long) imm_expr.X_add_number);
8250                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8251                 }
8252               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8253               imm_expr.X_op = O_absent;
8254               s = expr_end;
8255               continue;
8256
8257             case 'b':           /* base register */
8258             case 'd':           /* destination register */
8259             case 's':           /* source register */
8260             case 't':           /* target register */
8261             case 'r':           /* both target and source */
8262             case 'v':           /* both dest and source */
8263             case 'w':           /* both dest and target */
8264             case 'E':           /* coprocessor target register */
8265             case 'G':           /* coprocessor destination register */
8266             case 'x':           /* ignore register name */
8267             case 'z':           /* must be zero register */
8268             case 'U':           /* destination register (clo/clz).  */
8269               s_reset = s;
8270               if (s[0] == '$')
8271                 {
8272
8273                   if (ISDIGIT (s[1]))
8274                     {
8275                       ++s;
8276                       regno = 0;
8277                       do
8278                         {
8279                           regno *= 10;
8280                           regno += *s - '0';
8281                           ++s;
8282                         }
8283                       while (ISDIGIT (*s));
8284                       if (regno > 31)
8285                         as_bad (_("Invalid register number (%d)"), regno);
8286                     }
8287                   else if (*args == 'E' || *args == 'G')
8288                     goto notreg;
8289                   else
8290                     {
8291                       if (s[1] == 'r' && s[2] == 'a')
8292                         {
8293                           s += 3;
8294                           regno = RA;
8295                         }
8296                       else if (s[1] == 'f' && s[2] == 'p')
8297                         {
8298                           s += 3;
8299                           regno = FP;
8300                         }
8301                       else if (s[1] == 's' && s[2] == 'p')
8302                         {
8303                           s += 3;
8304                           regno = SP;
8305                         }
8306                       else if (s[1] == 'g' && s[2] == 'p')
8307                         {
8308                           s += 3;
8309                           regno = GP;
8310                         }
8311                       else if (s[1] == 'a' && s[2] == 't')
8312                         {
8313                           s += 3;
8314                           regno = AT;
8315                         }
8316                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8317                         {
8318                           s += 4;
8319                           regno = KT0;
8320                         }
8321                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8322                         {
8323                           s += 4;
8324                           regno = KT1;
8325                         }
8326                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8327                         {
8328                           s += 5;
8329                           regno = ZERO;
8330                         }
8331                       else if (itbl_have_entries)
8332                         {
8333                           char *p, *n;
8334                           unsigned long r;
8335
8336                           p = s + 1;    /* advance past '$' */
8337                           n = itbl_get_field (&p);  /* n is name */
8338
8339                           /* See if this is a register defined in an
8340                              itbl entry.  */
8341                           if (itbl_get_reg_val (n, &r))
8342                             {
8343                               /* Get_field advances to the start of
8344                                  the next field, so we need to back
8345                                  rack to the end of the last field.  */
8346                               if (p)
8347                                 s = p - 1;
8348                               else
8349                                 s = strchr (s, '\0');
8350                               regno = r;
8351                             }
8352                           else
8353                             goto notreg;
8354                         }
8355                       else
8356                         goto notreg;
8357                     }
8358                   if (regno == AT
8359                       && ! mips_opts.noat
8360                       && *args != 'E'
8361                       && *args != 'G')
8362                     as_warn (_("Used $at without \".set noat\""));
8363                   c = *args;
8364                   if (*s == ' ')
8365                     ++s;
8366                   if (args[1] != *s)
8367                     {
8368                       if (c == 'r' || c == 'v' || c == 'w')
8369                         {
8370                           regno = lastregno;
8371                           s = s_reset;
8372                           ++args;
8373                         }
8374                     }
8375                   /* 'z' only matches $0.  */
8376                   if (c == 'z' && regno != 0)
8377                     break;
8378
8379         /* Now that we have assembled one operand, we use the args string
8380          * to figure out where it goes in the instruction.  */
8381                   switch (c)
8382                     {
8383                     case 'r':
8384                     case 's':
8385                     case 'v':
8386                     case 'b':
8387                       ip->insn_opcode |= regno << OP_SH_RS;
8388                       break;
8389                     case 'd':
8390                     case 'G':
8391                       ip->insn_opcode |= regno << OP_SH_RD;
8392                       break;
8393                     case 'U':
8394                       ip->insn_opcode |= regno << OP_SH_RD;
8395                       ip->insn_opcode |= regno << OP_SH_RT;
8396                       break;
8397                     case 'w':
8398                     case 't':
8399                     case 'E':
8400                       ip->insn_opcode |= regno << OP_SH_RT;
8401                       break;
8402                     case 'x':
8403                       /* This case exists because on the r3000 trunc
8404                          expands into a macro which requires a gp
8405                          register.  On the r6000 or r4000 it is
8406                          assembled into a single instruction which
8407                          ignores the register.  Thus the insn version
8408                          is MIPS_ISA2 and uses 'x', and the macro
8409                          version is MIPS_ISA1 and uses 't'.  */
8410                       break;
8411                     case 'z':
8412                       /* This case is for the div instruction, which
8413                          acts differently if the destination argument
8414                          is $0.  This only matches $0, and is checked
8415                          outside the switch.  */
8416                       break;
8417                     case 'D':
8418                       /* Itbl operand; not yet implemented. FIXME ?? */
8419                       break;
8420                       /* What about all other operands like 'i', which
8421                          can be specified in the opcode table? */
8422                     }
8423                   lastregno = regno;
8424                   continue;
8425                 }
8426             notreg:
8427               switch (*args++)
8428                 {
8429                 case 'r':
8430                 case 'v':
8431                   ip->insn_opcode |= lastregno << OP_SH_RS;
8432                   continue;
8433                 case 'w':
8434                   ip->insn_opcode |= lastregno << OP_SH_RT;
8435                   continue;
8436                 }
8437               break;
8438
8439             case 'O':           /* MDMX alignment immediate constant.  */
8440               my_getExpression (&imm_expr, s);
8441               check_absolute_expr (ip, &imm_expr);
8442               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8443                 {
8444                   as_warn ("Improper align amount (%ld), using low bits",
8445                            (long) imm_expr.X_add_number);
8446                   imm_expr.X_add_number &= OP_MASK_ALN;
8447                 }
8448               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8449               imm_expr.X_op = O_absent;
8450               s = expr_end;
8451               continue;
8452
8453             case 'Q':           /* MDMX vector, element sel, or const.  */
8454               if (s[0] != '$')
8455                 {
8456                   /* MDMX Immediate.  */
8457                   my_getExpression (&imm_expr, s);
8458                   check_absolute_expr (ip, &imm_expr);
8459                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8460                     {
8461                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8462                                (long) imm_expr.X_add_number);
8463                       imm_expr.X_add_number &= OP_MASK_FT;
8464                     }
8465                   imm_expr.X_add_number &= OP_MASK_FT;
8466                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8467                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8468                   else
8469                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8470                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8471                   imm_expr.X_op = O_absent;
8472                   s = expr_end;
8473                   continue;
8474                 }
8475               /* Not MDMX Immediate.  Fall through.  */
8476             case 'X':           /* MDMX destination register.  */
8477             case 'Y':           /* MDMX source register.  */
8478             case 'Z':           /* MDMX target register.  */
8479               is_mdmx = 1;
8480             case 'D':           /* floating point destination register */
8481             case 'S':           /* floating point source register */
8482             case 'T':           /* floating point target register */
8483             case 'R':           /* floating point source register */
8484             case 'V':
8485             case 'W':
8486               s_reset = s;
8487               /* Accept $fN for FP and MDMX register numbers, and in
8488                  addition accept $vN for MDMX register numbers.  */
8489               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8490                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8491                       && ISDIGIT (s[2])))
8492                 {
8493                   s += 2;
8494                   regno = 0;
8495                   do
8496                     {
8497                       regno *= 10;
8498                       regno += *s - '0';
8499                       ++s;
8500                     }
8501                   while (ISDIGIT (*s));
8502
8503                   if (regno > 31)
8504                     as_bad (_("Invalid float register number (%d)"), regno);
8505
8506                   if ((regno & 1) != 0
8507                       && HAVE_32BIT_FPRS
8508                       && ! (strcmp (str, "mtc1") == 0
8509                             || strcmp (str, "mfc1") == 0
8510                             || strcmp (str, "lwc1") == 0
8511                             || strcmp (str, "swc1") == 0
8512                             || strcmp (str, "l.s") == 0
8513                             || strcmp (str, "s.s") == 0))
8514                     as_warn (_("Float register should be even, was %d"),
8515                              regno);
8516
8517                   c = *args;
8518                   if (*s == ' ')
8519                     ++s;
8520                   if (args[1] != *s)
8521                     {
8522                       if (c == 'V' || c == 'W')
8523                         {
8524                           regno = lastregno;
8525                           s = s_reset;
8526                           ++args;
8527                         }
8528                     }
8529                   switch (c)
8530                     {
8531                     case 'D':
8532                     case 'X':
8533                       ip->insn_opcode |= regno << OP_SH_FD;
8534                       break;
8535                     case 'V':
8536                     case 'S':
8537                     case 'Y':
8538                       ip->insn_opcode |= regno << OP_SH_FS;
8539                       break;
8540                     case 'Q':
8541                       /* This is like 'Z', but also needs to fix the MDMX
8542                          vector/scalar select bits.  Note that the
8543                          scalar immediate case is handled above.  */
8544                       if (*s == '[')
8545                         {
8546                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8547                           int max_el = (is_qh ? 3 : 7);
8548                           s++;
8549                           my_getExpression(&imm_expr, s);
8550                           check_absolute_expr (ip, &imm_expr);
8551                           s = expr_end;
8552                           if (imm_expr.X_add_number > max_el)
8553                             as_bad(_("Bad element selector %ld"),
8554                                    (long) imm_expr.X_add_number);
8555                           imm_expr.X_add_number &= max_el;
8556                           ip->insn_opcode |= (imm_expr.X_add_number
8557                                               << (OP_SH_VSEL +
8558                                                   (is_qh ? 2 : 1)));
8559                           if (*s != ']')
8560                             as_warn(_("Expecting ']' found '%s'"), s);
8561                           else
8562                             s++;
8563                         }
8564                       else
8565                         {
8566                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8567                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8568                                                 << OP_SH_VSEL);
8569                           else
8570                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8571                                                 OP_SH_VSEL);
8572                         }
8573                       /* Fall through */
8574                     case 'W':
8575                     case 'T':
8576                     case 'Z':
8577                       ip->insn_opcode |= regno << OP_SH_FT;
8578                       break;
8579                     case 'R':
8580                       ip->insn_opcode |= regno << OP_SH_FR;
8581                       break;
8582                     }
8583                   lastregno = regno;
8584                   continue;
8585                 }
8586
8587               switch (*args++)
8588                 {
8589                 case 'V':
8590                   ip->insn_opcode |= lastregno << OP_SH_FS;
8591                   continue;
8592                 case 'W':
8593                   ip->insn_opcode |= lastregno << OP_SH_FT;
8594                   continue;
8595                 }
8596               break;
8597
8598             case 'I':
8599               my_getExpression (&imm_expr, s);
8600               if (imm_expr.X_op != O_big
8601                   && imm_expr.X_op != O_constant)
8602                 insn_error = _("absolute expression required");
8603               s = expr_end;
8604               continue;
8605
8606             case 'A':
8607               my_getExpression (&offset_expr, s);
8608               *imm_reloc = BFD_RELOC_32;
8609               s = expr_end;
8610               continue;
8611
8612             case 'F':
8613             case 'L':
8614             case 'f':
8615             case 'l':
8616               {
8617                 int f64;
8618                 int using_gprs;
8619                 char *save_in;
8620                 char *err;
8621                 unsigned char temp[8];
8622                 int len;
8623                 unsigned int length;
8624                 segT seg;
8625                 subsegT subseg;
8626                 char *p;
8627
8628                 /* These only appear as the last operand in an
8629                    instruction, and every instruction that accepts
8630                    them in any variant accepts them in all variants.
8631                    This means we don't have to worry about backing out
8632                    any changes if the instruction does not match.
8633
8634                    The difference between them is the size of the
8635                    floating point constant and where it goes.  For 'F'
8636                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8637                    is 32 bits.  Where the constant is placed is based
8638                    on how the MIPS assembler does things:
8639                     F -- .rdata
8640                     L -- .lit8
8641                     f -- immediate value
8642                     l -- .lit4
8643
8644                     The .lit4 and .lit8 sections are only used if
8645                     permitted by the -G argument.
8646
8647                     When generating embedded PIC code, we use the
8648                     .lit8 section but not the .lit4 section (we can do
8649                     .lit4 inline easily; we need to put .lit8
8650                     somewhere in the data segment, and using .lit8
8651                     permits the linker to eventually combine identical
8652                     .lit8 entries).
8653
8654                     The code below needs to know whether the target register
8655                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8656                     'F' are used with GPR-based instructions and 'l' and
8657                     'L' are used with FPR-based instructions.  */
8658
8659                 f64 = *args == 'F' || *args == 'L';
8660                 using_gprs = *args == 'F' || *args == 'f';
8661
8662                 save_in = input_line_pointer;
8663                 input_line_pointer = s;
8664                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8665                 length = len;
8666                 s = input_line_pointer;
8667                 input_line_pointer = save_in;
8668                 if (err != NULL && *err != '\0')
8669                   {
8670                     as_bad (_("Bad floating point constant: %s"), err);
8671                     memset (temp, '\0', sizeof temp);
8672                     length = f64 ? 8 : 4;
8673                   }
8674
8675                 assert (length == (unsigned) (f64 ? 8 : 4));
8676
8677                 if (*args == 'f'
8678                     || (*args == 'l'
8679                         && (! USE_GLOBAL_POINTER_OPT
8680                             || mips_pic == EMBEDDED_PIC
8681                             || g_switch_value < 4
8682                             || (temp[0] == 0 && temp[1] == 0)
8683                             || (temp[2] == 0 && temp[3] == 0))))
8684                   {
8685                     imm_expr.X_op = O_constant;
8686                     if (! target_big_endian)
8687                       imm_expr.X_add_number = bfd_getl32 (temp);
8688                     else
8689                       imm_expr.X_add_number = bfd_getb32 (temp);
8690                   }
8691                 else if (length > 4
8692                          && ! mips_disable_float_construction
8693                          /* Constants can only be constructed in GPRs and
8694                             copied to FPRs if the GPRs are at least as wide
8695                             as the FPRs.  Force the constant into memory if
8696                             we are using 64-bit FPRs but the GPRs are only
8697                             32 bits wide.  */
8698                          && (using_gprs
8699                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8700                          && ((temp[0] == 0 && temp[1] == 0)
8701                              || (temp[2] == 0 && temp[3] == 0))
8702                          && ((temp[4] == 0 && temp[5] == 0)
8703                              || (temp[6] == 0 && temp[7] == 0)))
8704                   {
8705                     /* The value is simple enough to load with a couple of
8706                        instructions.  If using 32-bit registers, set
8707                        imm_expr to the high order 32 bits and offset_expr to
8708                        the low order 32 bits.  Otherwise, set imm_expr to
8709                        the entire 64 bit constant.  */
8710                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8711                       {
8712                         imm_expr.X_op = O_constant;
8713                         offset_expr.X_op = O_constant;
8714                         if (! target_big_endian)
8715                           {
8716                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8717                             offset_expr.X_add_number = bfd_getl32 (temp);
8718                           }
8719                         else
8720                           {
8721                             imm_expr.X_add_number = bfd_getb32 (temp);
8722                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8723                           }
8724                         if (offset_expr.X_add_number == 0)
8725                           offset_expr.X_op = O_absent;
8726                       }
8727                     else if (sizeof (imm_expr.X_add_number) > 4)
8728                       {
8729                         imm_expr.X_op = O_constant;
8730                         if (! target_big_endian)
8731                           imm_expr.X_add_number = bfd_getl64 (temp);
8732                         else
8733                           imm_expr.X_add_number = bfd_getb64 (temp);
8734                       }
8735                     else
8736                       {
8737                         imm_expr.X_op = O_big;
8738                         imm_expr.X_add_number = 4;
8739                         if (! target_big_endian)
8740                           {
8741                             generic_bignum[0] = bfd_getl16 (temp);
8742                             generic_bignum[1] = bfd_getl16 (temp + 2);
8743                             generic_bignum[2] = bfd_getl16 (temp + 4);
8744                             generic_bignum[3] = bfd_getl16 (temp + 6);
8745                           }
8746                         else
8747                           {
8748                             generic_bignum[0] = bfd_getb16 (temp + 6);
8749                             generic_bignum[1] = bfd_getb16 (temp + 4);
8750                             generic_bignum[2] = bfd_getb16 (temp + 2);
8751                             generic_bignum[3] = bfd_getb16 (temp);
8752                           }
8753                       }
8754                   }
8755                 else
8756                   {
8757                     const char *newname;
8758                     segT new_seg;
8759
8760                     /* Switch to the right section.  */
8761                     seg = now_seg;
8762                     subseg = now_subseg;
8763                     switch (*args)
8764                       {
8765                       default: /* unused default case avoids warnings.  */
8766                       case 'L':
8767                         newname = RDATA_SECTION_NAME;
8768                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8769                             || mips_pic == EMBEDDED_PIC)
8770                           newname = ".lit8";
8771                         break;
8772                       case 'F':
8773                         if (mips_pic == EMBEDDED_PIC)
8774                           newname = ".lit8";
8775                         else
8776                           newname = RDATA_SECTION_NAME;
8777                         break;
8778                       case 'l':
8779                         assert (!USE_GLOBAL_POINTER_OPT
8780                                 || g_switch_value >= 4);
8781                         newname = ".lit4";
8782                         break;
8783                       }
8784                     new_seg = subseg_new (newname, (subsegT) 0);
8785                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8786                       bfd_set_section_flags (stdoutput, new_seg,
8787                                              (SEC_ALLOC
8788                                               | SEC_LOAD
8789                                               | SEC_READONLY
8790                                               | SEC_DATA));
8791                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8792                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8793                         && strcmp (TARGET_OS, "elf") != 0)
8794                       record_alignment (new_seg, 4);
8795                     else
8796                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8797                     if (seg == now_seg)
8798                       as_bad (_("Can't use floating point insn in this section"));
8799
8800                     /* Set the argument to the current address in the
8801                        section.  */
8802                     offset_expr.X_op = O_symbol;
8803                     offset_expr.X_add_symbol =
8804                       symbol_new ("L0\001", now_seg,
8805                                   (valueT) frag_now_fix (), frag_now);
8806                     offset_expr.X_add_number = 0;
8807
8808                     /* Put the floating point number into the section.  */
8809                     p = frag_more ((int) length);
8810                     memcpy (p, temp, length);
8811
8812                     /* Switch back to the original section.  */
8813                     subseg_set (seg, subseg);
8814                   }
8815               }
8816               continue;
8817
8818             case 'i':           /* 16 bit unsigned immediate */
8819             case 'j':           /* 16 bit signed immediate */
8820               *imm_reloc = BFD_RELOC_LO16;
8821               c = my_getSmallExpression (&imm_expr, s);
8822               if (c != S_EX_NONE)
8823                 {
8824                   if (c != S_EX_LO)
8825                     {
8826                       if (c == S_EX_HI)
8827                         {
8828                           *imm_reloc = BFD_RELOC_HI16_S;
8829                           imm_unmatched_hi = true;
8830                         }
8831 #ifdef OBJ_ELF
8832                       else if (c == S_EX_HIGHEST)
8833                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8834                       else if (c == S_EX_HIGHER)
8835                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8836                       else if (c == S_EX_GP_REL)
8837                         {
8838                           /* This occurs in NewABI only.  */
8839                           c = my_getSmallExpression (&imm_expr, s);
8840                           if (c != S_EX_NEG)
8841                             as_bad (_("bad composition of relocations"));
8842                           else
8843                             {
8844                               c = my_getSmallExpression (&imm_expr, s);
8845                               if (c != S_EX_LO)
8846                                 as_bad (_("bad composition of relocations"));
8847                               else
8848                                 {
8849                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8850                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8851                                   imm_reloc[2] = BFD_RELOC_LO16;
8852                                 }
8853                             }
8854                         }
8855 #endif
8856                       else
8857                         *imm_reloc = BFD_RELOC_HI16;
8858                     }
8859                   else if (imm_expr.X_op == O_constant)
8860                     imm_expr.X_add_number &= 0xffff;
8861                 }
8862               if (*args == 'i')
8863                 {
8864                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8865                       || ((imm_expr.X_add_number < 0
8866                            || imm_expr.X_add_number >= 0x10000)
8867                           && imm_expr.X_op == O_constant))
8868                     {
8869                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8870                           !strcmp (insn->name, insn[1].name))
8871                         break;
8872                       if (imm_expr.X_op == O_constant
8873                           || imm_expr.X_op == O_big)
8874                         as_bad (_("16 bit expression not in range 0..65535"));
8875                     }
8876                 }
8877               else
8878                 {
8879                   int more;
8880                   offsetT max;
8881
8882                   /* The upper bound should be 0x8000, but
8883                      unfortunately the MIPS assembler accepts numbers
8884                      from 0x8000 to 0xffff and sign extends them, and
8885                      we want to be compatible.  We only permit this
8886                      extended range for an instruction which does not
8887                      provide any further alternates, since those
8888                      alternates may handle other cases.  People should
8889                      use the numbers they mean, rather than relying on
8890                      a mysterious sign extension.  */
8891                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8892                           strcmp (insn->name, insn[1].name) == 0);
8893                   if (more)
8894                     max = 0x8000;
8895                   else
8896                     max = 0x10000;
8897                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8898                       || ((imm_expr.X_add_number < -0x8000
8899                            || imm_expr.X_add_number >= max)
8900                           && imm_expr.X_op == O_constant)
8901                       || (more
8902                           && imm_expr.X_add_number < 0
8903                           && HAVE_64BIT_GPRS
8904                           && imm_expr.X_unsigned
8905                           && sizeof (imm_expr.X_add_number) <= 4))
8906                     {
8907                       if (more)
8908                         break;
8909                       if (imm_expr.X_op == O_constant
8910                           || imm_expr.X_op == O_big)
8911                         as_bad (_("16 bit expression not in range -32768..32767"));
8912                     }
8913                 }
8914               s = expr_end;
8915               continue;
8916
8917             case 'o':           /* 16 bit offset */
8918               c = my_getSmallExpression (&offset_expr, s);
8919
8920               /* If this value won't fit into a 16 bit offset, then go
8921                  find a macro that will generate the 32 bit offset
8922                  code pattern.  */
8923               if (c == S_EX_NONE
8924                   && (offset_expr.X_op != O_constant
8925                       || offset_expr.X_add_number >= 0x8000
8926                       || offset_expr.X_add_number < -0x8000))
8927                 break;
8928
8929               if (c == S_EX_HI)
8930                 {
8931                   if (offset_expr.X_op != O_constant)
8932                     break;
8933                   offset_expr.X_add_number =
8934                     (offset_expr.X_add_number >> 16) & 0xffff;
8935                 }
8936               *offset_reloc = BFD_RELOC_LO16;
8937               s = expr_end;
8938               continue;
8939
8940             case 'p':           /* pc relative offset */
8941               if (mips_pic == EMBEDDED_PIC)
8942                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8943               else
8944                 *offset_reloc = BFD_RELOC_16_PCREL;
8945               my_getExpression (&offset_expr, s);
8946               s = expr_end;
8947               continue;
8948
8949             case 'u':           /* upper 16 bits */
8950               c = my_getSmallExpression (&imm_expr, s);
8951               *imm_reloc = BFD_RELOC_LO16;
8952               if (c != S_EX_NONE)
8953                 {
8954                   if (c != S_EX_LO)
8955                     {
8956                       if (c == S_EX_HI)
8957                         {
8958                           *imm_reloc = BFD_RELOC_HI16_S;
8959                           imm_unmatched_hi = true;
8960                         }
8961 #ifdef OBJ_ELF
8962                       else if (c == S_EX_HIGHEST)
8963                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8964                       else if (c == S_EX_GP_REL)
8965                         {
8966                           /* This occurs in NewABI only.  */
8967                           c = my_getSmallExpression (&imm_expr, s);
8968                           if (c != S_EX_NEG)
8969                             as_bad (_("bad composition of relocations"));
8970                           else
8971                             {
8972                               c = my_getSmallExpression (&imm_expr, s);
8973                               if (c != S_EX_HI)
8974                                 as_bad (_("bad composition of relocations"));
8975                               else
8976                                 {
8977                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8978                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8979                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8980                                 }
8981                             }
8982                         }
8983 #endif
8984                       else
8985                         *imm_reloc = BFD_RELOC_HI16;
8986                     }
8987                   else if (imm_expr.X_op == O_constant)
8988                     imm_expr.X_add_number &= 0xffff;
8989                 }
8990               else if (imm_expr.X_op == O_constant
8991                        && (imm_expr.X_add_number < 0
8992                            || imm_expr.X_add_number >= 0x10000))
8993                 as_bad (_("lui expression not in range 0..65535"));
8994               s = expr_end;
8995               continue;
8996
8997             case 'a':           /* 26 bit address */
8998               my_getExpression (&offset_expr, s);
8999               s = expr_end;
9000               *offset_reloc = BFD_RELOC_MIPS_JMP;
9001               continue;
9002
9003             case 'N':           /* 3 bit branch condition code */
9004             case 'M':           /* 3 bit compare condition code */
9005               if (strncmp (s, "$fcc", 4) != 0)
9006                 break;
9007               s += 4;
9008               regno = 0;
9009               do
9010                 {
9011                   regno *= 10;
9012                   regno += *s - '0';
9013                   ++s;
9014                 }
9015               while (ISDIGIT (*s));
9016               if (regno > 7)
9017                 as_bad (_("invalid condition code register $fcc%d"), regno);
9018               if (*args == 'N')
9019                 ip->insn_opcode |= regno << OP_SH_BCC;
9020               else
9021                 ip->insn_opcode |= regno << OP_SH_CCC;
9022               continue;
9023
9024             case 'H':
9025               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9026                 s += 2;
9027               if (ISDIGIT (*s))
9028                 {
9029                   c = 0;
9030                   do
9031                     {
9032                       c *= 10;
9033                       c += *s - '0';
9034                       ++s;
9035                     }
9036                   while (ISDIGIT (*s));
9037                 }
9038               else
9039                 c = 8; /* Invalid sel value.  */
9040
9041               if (c > 7)
9042                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9043               ip->insn_opcode |= c;
9044               continue;
9045
9046             case 'e':
9047               /* Must be at least one digit.  */
9048               my_getExpression (&imm_expr, s);
9049               check_absolute_expr (ip, &imm_expr);
9050
9051               if ((unsigned long) imm_expr.X_add_number
9052                   > (unsigned long) OP_MASK_VECBYTE)
9053                 {
9054                   as_bad (_("bad byte vector index (%ld)"),
9055                            (long) imm_expr.X_add_number);
9056                   imm_expr.X_add_number = 0;
9057                 }
9058
9059               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9060               imm_expr.X_op = O_absent;
9061               s = expr_end;
9062               continue;
9063
9064             case '%':
9065               my_getExpression (&imm_expr, s);
9066               check_absolute_expr (ip, &imm_expr);
9067
9068               if ((unsigned long) imm_expr.X_add_number
9069                   > (unsigned long) OP_MASK_VECALIGN)
9070                 {
9071                   as_bad (_("bad byte vector index (%ld)"),
9072                            (long) imm_expr.X_add_number);
9073                   imm_expr.X_add_number = 0;
9074                 }
9075
9076               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9077               imm_expr.X_op = O_absent;
9078               s = expr_end;
9079               continue;
9080
9081             default:
9082               as_bad (_("bad char = '%c'\n"), *args);
9083               internalError ();
9084             }
9085           break;
9086         }
9087       /* Args don't match.  */
9088       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9089           !strcmp (insn->name, insn[1].name))
9090         {
9091           ++insn;
9092           s = argsStart;
9093           insn_error = _("illegal operands");
9094           continue;
9095         }
9096       if (save_c)
9097         *(--s) = save_c;
9098       insn_error = _("illegal operands");
9099       return;
9100     }
9101 }
9102
9103 /* This routine assembles an instruction into its binary format when
9104    assembling for the mips16.  As a side effect, it sets one of the
9105    global variables imm_reloc or offset_reloc to the type of
9106    relocation to do if one of the operands is an address expression.
9107    It also sets mips16_small and mips16_ext if the user explicitly
9108    requested a small or extended instruction.  */
9109
9110 static void
9111 mips16_ip (str, ip)
9112      char *str;
9113      struct mips_cl_insn *ip;
9114 {
9115   char *s;
9116   const char *args;
9117   struct mips_opcode *insn;
9118   char *argsstart;
9119   unsigned int regno;
9120   unsigned int lastregno = 0;
9121   char *s_reset;
9122
9123   insn_error = NULL;
9124
9125   mips16_small = false;
9126   mips16_ext = false;
9127
9128   for (s = str; ISLOWER (*s); ++s)
9129     ;
9130   switch (*s)
9131     {
9132     case '\0':
9133       break;
9134
9135     case ' ':
9136       *s++ = '\0';
9137       break;
9138
9139     case '.':
9140       if (s[1] == 't' && s[2] == ' ')
9141         {
9142           *s = '\0';
9143           mips16_small = true;
9144           s += 3;
9145           break;
9146         }
9147       else if (s[1] == 'e' && s[2] == ' ')
9148         {
9149           *s = '\0';
9150           mips16_ext = true;
9151           s += 3;
9152           break;
9153         }
9154       /* Fall through.  */
9155     default:
9156       insn_error = _("unknown opcode");
9157       return;
9158     }
9159
9160   if (mips_opts.noautoextend && ! mips16_ext)
9161     mips16_small = true;
9162
9163   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9164     {
9165       insn_error = _("unrecognized opcode");
9166       return;
9167     }
9168
9169   argsstart = s;
9170   for (;;)
9171     {
9172       assert (strcmp (insn->name, str) == 0);
9173
9174       ip->insn_mo = insn;
9175       ip->insn_opcode = insn->match;
9176       ip->use_extend = false;
9177       imm_expr.X_op = O_absent;
9178       imm_reloc[0] = BFD_RELOC_UNUSED;
9179       imm_reloc[1] = BFD_RELOC_UNUSED;
9180       imm_reloc[2] = BFD_RELOC_UNUSED;
9181       offset_expr.X_op = O_absent;
9182       offset_reloc[0] = BFD_RELOC_UNUSED;
9183       offset_reloc[1] = BFD_RELOC_UNUSED;
9184       offset_reloc[2] = BFD_RELOC_UNUSED;
9185       for (args = insn->args; 1; ++args)
9186         {
9187           int c;
9188
9189           if (*s == ' ')
9190             ++s;
9191
9192           /* In this switch statement we call break if we did not find
9193              a match, continue if we did find a match, or return if we
9194              are done.  */
9195
9196           c = *args;
9197           switch (c)
9198             {
9199             case '\0':
9200               if (*s == '\0')
9201                 {
9202                   /* Stuff the immediate value in now, if we can.  */
9203                   if (imm_expr.X_op == O_constant
9204                       && *imm_reloc > BFD_RELOC_UNUSED
9205                       && insn->pinfo != INSN_MACRO)
9206                     {
9207                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9208                                     imm_expr.X_add_number, true, mips16_small,
9209                                     mips16_ext, &ip->insn_opcode,
9210                                     &ip->use_extend, &ip->extend);
9211                       imm_expr.X_op = O_absent;
9212                       *imm_reloc = BFD_RELOC_UNUSED;
9213                     }
9214
9215                   return;
9216                 }
9217               break;
9218
9219             case ',':
9220               if (*s++ == c)
9221                 continue;
9222               s--;
9223               switch (*++args)
9224                 {
9225                 case 'v':
9226                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9227                   continue;
9228                 case 'w':
9229                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9230                   continue;
9231                 }
9232               break;
9233
9234             case '(':
9235             case ')':
9236               if (*s++ == c)
9237                 continue;
9238               break;
9239
9240             case 'v':
9241             case 'w':
9242               if (s[0] != '$')
9243                 {
9244                   if (c == 'v')
9245                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9246                   else
9247                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9248                   ++args;
9249                   continue;
9250                 }
9251               /* Fall through.  */
9252             case 'x':
9253             case 'y':
9254             case 'z':
9255             case 'Z':
9256             case '0':
9257             case 'S':
9258             case 'R':
9259             case 'X':
9260             case 'Y':
9261               if (s[0] != '$')
9262                 break;
9263               s_reset = s;
9264               if (ISDIGIT (s[1]))
9265                 {
9266                   ++s;
9267                   regno = 0;
9268                   do
9269                     {
9270                       regno *= 10;
9271                       regno += *s - '0';
9272                       ++s;
9273                     }
9274                   while (ISDIGIT (*s));
9275                   if (regno > 31)
9276                     {
9277                       as_bad (_("invalid register number (%d)"), regno);
9278                       regno = 2;
9279                     }
9280                 }
9281               else
9282                 {
9283                   if (s[1] == 'r' && s[2] == 'a')
9284                     {
9285                       s += 3;
9286                       regno = RA;
9287                     }
9288                   else if (s[1] == 'f' && s[2] == 'p')
9289                     {
9290                       s += 3;
9291                       regno = FP;
9292                     }
9293                   else if (s[1] == 's' && s[2] == 'p')
9294                     {
9295                       s += 3;
9296                       regno = SP;
9297                     }
9298                   else if (s[1] == 'g' && s[2] == 'p')
9299                     {
9300                       s += 3;
9301                       regno = GP;
9302                     }
9303                   else if (s[1] == 'a' && s[2] == 't')
9304                     {
9305                       s += 3;
9306                       regno = AT;
9307                     }
9308                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9309                     {
9310                       s += 4;
9311                       regno = KT0;
9312                     }
9313                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9314                     {
9315                       s += 4;
9316                       regno = KT1;
9317                     }
9318                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9319                     {
9320                       s += 5;
9321                       regno = ZERO;
9322                     }
9323                   else
9324                     break;
9325                 }
9326
9327               if (*s == ' ')
9328                 ++s;
9329               if (args[1] != *s)
9330                 {
9331                   if (c == 'v' || c == 'w')
9332                     {
9333                       regno = mips16_to_32_reg_map[lastregno];
9334                       s = s_reset;
9335                       ++args;
9336                     }
9337                 }
9338
9339               switch (c)
9340                 {
9341                 case 'x':
9342                 case 'y':
9343                 case 'z':
9344                 case 'v':
9345                 case 'w':
9346                 case 'Z':
9347                   regno = mips32_to_16_reg_map[regno];
9348                   break;
9349
9350                 case '0':
9351                   if (regno != 0)
9352                     regno = ILLEGAL_REG;
9353                   break;
9354
9355                 case 'S':
9356                   if (regno != SP)
9357                     regno = ILLEGAL_REG;
9358                   break;
9359
9360                 case 'R':
9361                   if (regno != RA)
9362                     regno = ILLEGAL_REG;
9363                   break;
9364
9365                 case 'X':
9366                 case 'Y':
9367                   if (regno == AT && ! mips_opts.noat)
9368                     as_warn (_("used $at without \".set noat\""));
9369                   break;
9370
9371                 default:
9372                   internalError ();
9373                 }
9374
9375               if (regno == ILLEGAL_REG)
9376                 break;
9377
9378               switch (c)
9379                 {
9380                 case 'x':
9381                 case 'v':
9382                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9383                   break;
9384                 case 'y':
9385                 case 'w':
9386                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9387                   break;
9388                 case 'z':
9389                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9390                   break;
9391                 case 'Z':
9392                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9393                 case '0':
9394                 case 'S':
9395                 case 'R':
9396                   break;
9397                 case 'X':
9398                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9399                   break;
9400                 case 'Y':
9401                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9402                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9403                   break;
9404                 default:
9405                   internalError ();
9406                 }
9407
9408               lastregno = regno;
9409               continue;
9410
9411             case 'P':
9412               if (strncmp (s, "$pc", 3) == 0)
9413                 {
9414                   s += 3;
9415                   continue;
9416                 }
9417               break;
9418
9419             case '<':
9420             case '>':
9421             case '[':
9422             case ']':
9423             case '4':
9424             case '5':
9425             case 'H':
9426             case 'W':
9427             case 'D':
9428             case 'j':
9429             case '8':
9430             case 'V':
9431             case 'C':
9432             case 'U':
9433             case 'k':
9434             case 'K':
9435               if (s[0] == '%'
9436                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9437                 {
9438                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9439                      and generate the appropriate reloc.  If the text
9440                      inside %gprel is not a symbol name with an
9441                      optional offset, then we generate a normal reloc
9442                      and will probably fail later.  */
9443                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9444                   if (imm_expr.X_op == O_symbol)
9445                     {
9446                       mips16_ext = true;
9447                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9448                       s = expr_end;
9449                       ip->use_extend = true;
9450                       ip->extend = 0;
9451                       continue;
9452                     }
9453                 }
9454               else
9455                 {
9456                   /* Just pick up a normal expression.  */
9457                   my_getExpression (&imm_expr, s);
9458                 }
9459
9460               if (imm_expr.X_op == O_register)
9461                 {
9462                   /* What we thought was an expression turned out to
9463                      be a register.  */
9464
9465                   if (s[0] == '(' && args[1] == '(')
9466                     {
9467                       /* It looks like the expression was omitted
9468                          before a register indirection, which means
9469                          that the expression is implicitly zero.  We
9470                          still set up imm_expr, so that we handle
9471                          explicit extensions correctly.  */
9472                       imm_expr.X_op = O_constant;
9473                       imm_expr.X_add_number = 0;
9474                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9475                       continue;
9476                     }
9477
9478                   break;
9479                 }
9480
9481               /* We need to relax this instruction.  */
9482               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9483               s = expr_end;
9484               continue;
9485
9486             case 'p':
9487             case 'q':
9488             case 'A':
9489             case 'B':
9490             case 'E':
9491               /* We use offset_reloc rather than imm_reloc for the PC
9492                  relative operands.  This lets macros with both
9493                  immediate and address operands work correctly.  */
9494               my_getExpression (&offset_expr, s);
9495
9496               if (offset_expr.X_op == O_register)
9497                 break;
9498
9499               /* We need to relax this instruction.  */
9500               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9501               s = expr_end;
9502               continue;
9503
9504             case '6':           /* break code */
9505               my_getExpression (&imm_expr, s);
9506               check_absolute_expr (ip, &imm_expr);
9507               if ((unsigned long) imm_expr.X_add_number > 63)
9508                 {
9509                   as_warn (_("Invalid value for `%s' (%lu)"),
9510                            ip->insn_mo->name,
9511                            (unsigned long) imm_expr.X_add_number);
9512                   imm_expr.X_add_number &= 0x3f;
9513                 }
9514               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9515               imm_expr.X_op = O_absent;
9516               s = expr_end;
9517               continue;
9518
9519             case 'a':           /* 26 bit address */
9520               my_getExpression (&offset_expr, s);
9521               s = expr_end;
9522               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9523               ip->insn_opcode <<= 16;
9524               continue;
9525
9526             case 'l':           /* register list for entry macro */
9527             case 'L':           /* register list for exit macro */
9528               {
9529                 int mask;
9530
9531                 if (c == 'l')
9532                   mask = 0;
9533                 else
9534                   mask = 7 << 3;
9535                 while (*s != '\0')
9536                   {
9537                     int freg, reg1, reg2;
9538
9539                     while (*s == ' ' || *s == ',')
9540                       ++s;
9541                     if (*s != '$')
9542                       {
9543                         as_bad (_("can't parse register list"));
9544                         break;
9545                       }
9546                     ++s;
9547                     if (*s != 'f')
9548                       freg = 0;
9549                     else
9550                       {
9551                         freg = 1;
9552                         ++s;
9553                       }
9554                     reg1 = 0;
9555                     while (ISDIGIT (*s))
9556                       {
9557                         reg1 *= 10;
9558                         reg1 += *s - '0';
9559                         ++s;
9560                       }
9561                     if (*s == ' ')
9562                       ++s;
9563                     if (*s != '-')
9564                       reg2 = reg1;
9565                     else
9566                       {
9567                         ++s;
9568                         if (*s != '$')
9569                           break;
9570                         ++s;
9571                         if (freg)
9572                           {
9573                             if (*s == 'f')
9574                               ++s;
9575                             else
9576                               {
9577                                 as_bad (_("invalid register list"));
9578                                 break;
9579                               }
9580                           }
9581                         reg2 = 0;
9582                         while (ISDIGIT (*s))
9583                           {
9584                             reg2 *= 10;
9585                             reg2 += *s - '0';
9586                             ++s;
9587                           }
9588                       }
9589                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9590                       {
9591                         mask &= ~ (7 << 3);
9592                         mask |= 5 << 3;
9593                       }
9594                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9595                       {
9596                         mask &= ~ (7 << 3);
9597                         mask |= 6 << 3;
9598                       }
9599                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9600                       mask |= (reg2 - 3) << 3;
9601                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9602                       mask |= (reg2 - 15) << 1;
9603                     else if (reg1 == RA && reg2 == RA)
9604                       mask |= 1;
9605                     else
9606                       {
9607                         as_bad (_("invalid register list"));
9608                         break;
9609                       }
9610                   }
9611                 /* The mask is filled in in the opcode table for the
9612                    benefit of the disassembler.  We remove it before
9613                    applying the actual mask.  */
9614                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9615                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9616               }
9617             continue;
9618
9619             case 'e':           /* extend code */
9620               my_getExpression (&imm_expr, s);
9621               check_absolute_expr (ip, &imm_expr);
9622               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9623                 {
9624                   as_warn (_("Invalid value for `%s' (%lu)"),
9625                            ip->insn_mo->name,
9626                            (unsigned long) imm_expr.X_add_number);
9627                   imm_expr.X_add_number &= 0x7ff;
9628                 }
9629               ip->insn_opcode |= imm_expr.X_add_number;
9630               imm_expr.X_op = O_absent;
9631               s = expr_end;
9632               continue;
9633
9634             default:
9635               internalError ();
9636             }
9637           break;
9638         }
9639
9640       /* Args don't match.  */
9641       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9642           strcmp (insn->name, insn[1].name) == 0)
9643         {
9644           ++insn;
9645           s = argsstart;
9646           continue;
9647         }
9648
9649       insn_error = _("illegal operands");
9650
9651       return;
9652     }
9653 }
9654
9655 /* This structure holds information we know about a mips16 immediate
9656    argument type.  */
9657
9658 struct mips16_immed_operand
9659 {
9660   /* The type code used in the argument string in the opcode table.  */
9661   int type;
9662   /* The number of bits in the short form of the opcode.  */
9663   int nbits;
9664   /* The number of bits in the extended form of the opcode.  */
9665   int extbits;
9666   /* The amount by which the short form is shifted when it is used;
9667      for example, the sw instruction has a shift count of 2.  */
9668   int shift;
9669   /* The amount by which the short form is shifted when it is stored
9670      into the instruction code.  */
9671   int op_shift;
9672   /* Non-zero if the short form is unsigned.  */
9673   int unsp;
9674   /* Non-zero if the extended form is unsigned.  */
9675   int extu;
9676   /* Non-zero if the value is PC relative.  */
9677   int pcrel;
9678 };
9679
9680 /* The mips16 immediate operand types.  */
9681
9682 static const struct mips16_immed_operand mips16_immed_operands[] =
9683 {
9684   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9685   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9686   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9687   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9688   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9689   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9690   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9691   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9692   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9693   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9694   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9695   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9696   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9697   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9698   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9699   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9700   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9701   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9702   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9703   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9704   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9705 };
9706
9707 #define MIPS16_NUM_IMMED \
9708   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9709
9710 /* Handle a mips16 instruction with an immediate value.  This or's the
9711    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9712    whether an extended value is needed; if one is needed, it sets
9713    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9714    If SMALL is true, an unextended opcode was explicitly requested.
9715    If EXT is true, an extended opcode was explicitly requested.  If
9716    WARN is true, warn if EXT does not match reality.  */
9717
9718 static void
9719 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9720               extend)
9721      char *file;
9722      unsigned int line;
9723      int type;
9724      offsetT val;
9725      boolean warn;
9726      boolean small;
9727      boolean ext;
9728      unsigned long *insn;
9729      boolean *use_extend;
9730      unsigned short *extend;
9731 {
9732   register const struct mips16_immed_operand *op;
9733   int mintiny, maxtiny;
9734   boolean needext;
9735
9736   op = mips16_immed_operands;
9737   while (op->type != type)
9738     {
9739       ++op;
9740       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9741     }
9742
9743   if (op->unsp)
9744     {
9745       if (type == '<' || type == '>' || type == '[' || type == ']')
9746         {
9747           mintiny = 1;
9748           maxtiny = 1 << op->nbits;
9749         }
9750       else
9751         {
9752           mintiny = 0;
9753           maxtiny = (1 << op->nbits) - 1;
9754         }
9755     }
9756   else
9757     {
9758       mintiny = - (1 << (op->nbits - 1));
9759       maxtiny = (1 << (op->nbits - 1)) - 1;
9760     }
9761
9762   /* Branch offsets have an implicit 0 in the lowest bit.  */
9763   if (type == 'p' || type == 'q')
9764     val /= 2;
9765
9766   if ((val & ((1 << op->shift) - 1)) != 0
9767       || val < (mintiny << op->shift)
9768       || val > (maxtiny << op->shift))
9769     needext = true;
9770   else
9771     needext = false;
9772
9773   if (warn && ext && ! needext)
9774     as_warn_where (file, line,
9775                    _("extended operand requested but not required"));
9776   if (small && needext)
9777     as_bad_where (file, line, _("invalid unextended operand value"));
9778
9779   if (small || (! ext && ! needext))
9780     {
9781       int insnval;
9782
9783       *use_extend = false;
9784       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9785       insnval <<= op->op_shift;
9786       *insn |= insnval;
9787     }
9788   else
9789     {
9790       long minext, maxext;
9791       int extval;
9792
9793       if (op->extu)
9794         {
9795           minext = 0;
9796           maxext = (1 << op->extbits) - 1;
9797         }
9798       else
9799         {
9800           minext = - (1 << (op->extbits - 1));
9801           maxext = (1 << (op->extbits - 1)) - 1;
9802         }
9803       if (val < minext || val > maxext)
9804         as_bad_where (file, line,
9805                       _("operand value out of range for instruction"));
9806
9807       *use_extend = true;
9808       if (op->extbits == 16)
9809         {
9810           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9811           val &= 0x1f;
9812         }
9813       else if (op->extbits == 15)
9814         {
9815           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9816           val &= 0xf;
9817         }
9818       else
9819         {
9820           extval = ((val & 0x1f) << 6) | (val & 0x20);
9821           val = 0;
9822         }
9823
9824       *extend = (unsigned short) extval;
9825       *insn |= val;
9826     }
9827 }
9828 \f
9829 static struct percent_op_match
9830 {
9831    const char *str;
9832    const enum small_ex_type type;
9833 } percent_op[] =
9834 {
9835   {"%lo", S_EX_LO},
9836 #ifdef OBJ_ELF
9837   {"%call_hi", S_EX_CALL_HI},
9838   {"%call_lo", S_EX_CALL_LO},
9839   {"%call16", S_EX_CALL16},
9840   {"%got_disp", S_EX_GOT_DISP},
9841   {"%got_page", S_EX_GOT_PAGE},
9842   {"%got_ofst", S_EX_GOT_OFST},
9843   {"%got_hi", S_EX_GOT_HI},
9844   {"%got_lo", S_EX_GOT_LO},
9845   {"%got", S_EX_GOT},
9846   {"%gp_rel", S_EX_GP_REL},
9847   {"%half", S_EX_HALF},
9848   {"%highest", S_EX_HIGHEST},
9849   {"%higher", S_EX_HIGHER},
9850   {"%neg", S_EX_NEG},
9851 #endif
9852   {"%hi", S_EX_HI}
9853 };
9854
9855 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9856    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9857    can be nested, this is handled by blanking the innermost, parsing the
9858    rest by subsequent calls.  */
9859
9860 static int
9861 my_getSmallParser (str, len, nestlevel)
9862      char **str;
9863      unsigned int *len;
9864      int *nestlevel;
9865 {
9866   *len = 0;
9867   *str += strspn (*str, " \t");
9868   /* Check for expression in parentheses.  */
9869   if (**str == '(')
9870     {
9871       char *b = *str + 1 + strspn (*str + 1, " \t");
9872       char *e;
9873
9874       /* Check for base register.  */
9875       if (b[0] == '$')
9876         {
9877           if (strchr (b, ')')
9878               && (e = b + strcspn (b, ") \t"))
9879               && e - b > 1 && e - b < 4)
9880             {
9881               if ((e - b == 3
9882                    && ((b[1] == 'f' && b[2] == 'p')
9883                        || (b[1] == 's' && b[2] == 'p')
9884                        || (b[1] == 'g' && b[2] == 'p')
9885                        || (b[1] == 'a' && b[2] == 't')
9886                        || (ISDIGIT (b[1])
9887                            && ISDIGIT (b[2]))))
9888                   || (ISDIGIT (b[1])))
9889                 {
9890                   *len = strcspn (*str, ")") + 1;
9891                   return S_EX_REGISTER;
9892                 }
9893             }
9894         }
9895       /* Check for percent_op (in parentheses).  */
9896       else if (b[0] == '%')
9897         {
9898           *str = b;
9899           return my_getPercentOp (str, len, nestlevel);
9900         }
9901
9902       /* Some other expression in the parentheses, which can contain
9903          parentheses itself. Attempt to find the matching one.  */
9904       {
9905         int pcnt = 1;
9906         char *s;
9907
9908         *len = 1;
9909         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9910           {
9911             if (*s == '(')
9912               ++pcnt;
9913             else if (*s == ')')
9914               --pcnt;
9915           }
9916       }
9917     }
9918   /* Check for percent_op (outside of parentheses).  */
9919   else if (*str[0] == '%')
9920     return my_getPercentOp (str, len, nestlevel);
9921
9922   /* Any other expression.  */
9923   return S_EX_NONE;
9924 }
9925
9926 static int
9927 my_getPercentOp (str, len, nestlevel)
9928      char **str;
9929      unsigned int *len;
9930      int *nestlevel;
9931 {
9932   char *tmp = *str + 1;
9933   unsigned int i = 0;
9934
9935   while (ISALPHA (*tmp) || *tmp == '_')
9936     {
9937       *tmp = TOLOWER (*tmp);
9938       tmp++;
9939     }
9940   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9941     {
9942       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9943         i++;
9944       else
9945         {
9946           int type = percent_op[i].type;
9947
9948           /* Only %hi and %lo are allowed for OldABI.  */
9949           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9950             return S_EX_NONE;
9951
9952           *len = strlen (percent_op[i].str);
9953           ++(*nestlevel);
9954           return type;
9955         }
9956     }
9957   return S_EX_NONE;
9958 }
9959
9960 static int
9961 my_getSmallExpression (ep, str)
9962      expressionS *ep;
9963      char *str;
9964 {
9965   static char *oldstr = NULL;
9966   int c = S_EX_NONE;
9967   int oldc;
9968   int nestlevel = -1;
9969   unsigned int len;
9970
9971   /* Don't update oldstr if the last call had nested percent_op's. We need
9972      it to parse the outer ones later.  */
9973   if (! oldstr)
9974     oldstr = str;
9975
9976   do
9977     {
9978       oldc = c;
9979       c = my_getSmallParser (&str, &len, &nestlevel);
9980       if (c != S_EX_NONE && c != S_EX_REGISTER)
9981         str += len;
9982     }
9983   while (c != S_EX_NONE && c != S_EX_REGISTER);
9984
9985   if (nestlevel >= 0)
9986     {
9987       /* A percent_op was encountered.  Don't try to get an expression if
9988          it is already blanked out.  */
9989       if (*(str + strspn (str + 1, " )")) != ')')
9990         {
9991           char save;
9992
9993           /* Let my_getExpression() stop at the closing parenthesis.  */
9994           save = *(str + len);
9995           *(str + len) = '\0';
9996           my_getExpression (ep, str);
9997           *(str + len) = save;
9998         }
9999       if (nestlevel > 0)
10000         {
10001           /* Blank out including the % sign and the proper matching
10002              parenthesis.  */
10003           int pcnt = 1;
10004           char *s = strrchr (oldstr, '%');
10005           char *end;
10006
10007           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
10008             {
10009               if (*end == '(')
10010                 ++pcnt;
10011               else if (*end == ')')
10012                 --pcnt;
10013             }
10014
10015           memset (s, ' ', end - s);
10016           str = oldstr;
10017         }
10018       else
10019         expr_end = str + len;
10020
10021       c = oldc;
10022     }
10023   else if (c == S_EX_NONE)
10024     {
10025       my_getExpression (ep, str);
10026     }
10027   else if (c == S_EX_REGISTER)
10028     {
10029       ep->X_op = O_constant;
10030       expr_end = str;
10031       ep->X_add_symbol = NULL;
10032       ep->X_op_symbol = NULL;
10033       ep->X_add_number = 0;
10034     }
10035   else
10036     {
10037       as_fatal (_("internal error"));
10038     }
10039
10040   if (nestlevel <= 0)
10041     /* All percent_op's have been handled.  */
10042     oldstr = NULL;
10043
10044   return c;
10045 }
10046
10047 static void
10048 my_getExpression (ep, str)
10049      expressionS *ep;
10050      char *str;
10051 {
10052   char *save_in;
10053   valueT val;
10054
10055   save_in = input_line_pointer;
10056   input_line_pointer = str;
10057   expression (ep);
10058   expr_end = input_line_pointer;
10059   input_line_pointer = save_in;
10060
10061   /* If we are in mips16 mode, and this is an expression based on `.',
10062      then we bump the value of the symbol by 1 since that is how other
10063      text symbols are handled.  We don't bother to handle complex
10064      expressions, just `.' plus or minus a constant.  */
10065   if (mips_opts.mips16
10066       && ep->X_op == O_symbol
10067       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10068       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10069       && symbol_get_frag (ep->X_add_symbol) == frag_now
10070       && symbol_constant_p (ep->X_add_symbol)
10071       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10072     S_SET_VALUE (ep->X_add_symbol, val + 1);
10073 }
10074
10075 /* Turn a string in input_line_pointer into a floating point constant
10076    of type TYPE, and store the appropriate bytes in *LITP.  The number
10077    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10078    returned, or NULL on OK.  */
10079
10080 char *
10081 md_atof (type, litP, sizeP)
10082      int type;
10083      char *litP;
10084      int *sizeP;
10085 {
10086   int prec;
10087   LITTLENUM_TYPE words[4];
10088   char *t;
10089   int i;
10090
10091   switch (type)
10092     {
10093     case 'f':
10094       prec = 2;
10095       break;
10096
10097     case 'd':
10098       prec = 4;
10099       break;
10100
10101     default:
10102       *sizeP = 0;
10103       return _("bad call to md_atof");
10104     }
10105
10106   t = atof_ieee (input_line_pointer, type, words);
10107   if (t)
10108     input_line_pointer = t;
10109
10110   *sizeP = prec * 2;
10111
10112   if (! target_big_endian)
10113     {
10114       for (i = prec - 1; i >= 0; i--)
10115         {
10116           md_number_to_chars (litP, (valueT) words[i], 2);
10117           litP += 2;
10118         }
10119     }
10120   else
10121     {
10122       for (i = 0; i < prec; i++)
10123         {
10124           md_number_to_chars (litP, (valueT) words[i], 2);
10125           litP += 2;
10126         }
10127     }
10128
10129   return NULL;
10130 }
10131
10132 void
10133 md_number_to_chars (buf, val, n)
10134      char *buf;
10135      valueT val;
10136      int n;
10137 {
10138   if (target_big_endian)
10139     number_to_chars_bigendian (buf, val, n);
10140   else
10141     number_to_chars_littleendian (buf, val, n);
10142 }
10143 \f
10144 #ifdef OBJ_ELF
10145 static int support_64bit_objects(void)
10146 {
10147   const char **list, **l;
10148
10149   list = bfd_target_list ();
10150   for (l = list; *l != NULL; l++)
10151 #ifdef TE_TMIPS
10152     /* This is traditional mips */
10153     if (strcmp (*l, "elf64-tradbigmips") == 0
10154         || strcmp (*l, "elf64-tradlittlemips") == 0)
10155 #else
10156     if (strcmp (*l, "elf64-bigmips") == 0
10157         || strcmp (*l, "elf64-littlemips") == 0)
10158 #endif
10159       break;
10160   free (list);
10161   return (*l != NULL);
10162 }
10163 #endif /* OBJ_ELF */
10164
10165 const char *md_shortopts = "nO::g::G:";
10166
10167 struct option md_longopts[] =
10168 {
10169 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10170   {"mips0", no_argument, NULL, OPTION_MIPS1},
10171   {"mips1", no_argument, NULL, OPTION_MIPS1},
10172 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10173   {"mips2", no_argument, NULL, OPTION_MIPS2},
10174 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10175   {"mips3", no_argument, NULL, OPTION_MIPS3},
10176 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10177   {"mips4", no_argument, NULL, OPTION_MIPS4},
10178 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10179   {"mips5", no_argument, NULL, OPTION_MIPS5},
10180 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10181   {"mips32", no_argument, NULL, OPTION_MIPS32},
10182 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10183   {"mips64", no_argument, NULL, OPTION_MIPS64},
10184 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10185   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10186 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10187   {"trap", no_argument, NULL, OPTION_TRAP},
10188   {"no-break", no_argument, NULL, OPTION_TRAP},
10189 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10190   {"break", no_argument, NULL, OPTION_BREAK},
10191   {"no-trap", no_argument, NULL, OPTION_BREAK},
10192 #define OPTION_EB (OPTION_MD_BASE + 11)
10193   {"EB", no_argument, NULL, OPTION_EB},
10194 #define OPTION_EL (OPTION_MD_BASE + 12)
10195   {"EL", no_argument, NULL, OPTION_EL},
10196 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10197   {"mips16", no_argument, NULL, OPTION_MIPS16},
10198 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10199   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10200 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10201   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10202 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10203   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10204   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10205 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10206   {"mfp32", no_argument, NULL, OPTION_FP32},
10207 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10208   {"mgp32", no_argument, NULL, OPTION_GP32},
10209 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10210   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10211 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10212   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10213 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10214   {"march", required_argument, NULL, OPTION_MARCH},
10215 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10216   {"mtune", required_argument, NULL, OPTION_MTUNE},
10217 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10218   {"mfp64", no_argument, NULL, OPTION_FP64},
10219 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10220   {"m4650", no_argument, NULL, OPTION_M4650},
10221 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10222   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10223 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10224   {"m4010", no_argument, NULL, OPTION_M4010},
10225 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10226   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10227 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10228   {"m4100", no_argument, NULL, OPTION_M4100},
10229 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10230   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10231 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10232   {"m3900", no_argument, NULL, OPTION_M3900},
10233 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10234   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10235 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10236   {"mgp64", no_argument, NULL, OPTION_GP64},
10237 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10238   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10239 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10240   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10241 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10242   {"mdmx", no_argument, NULL, OPTION_MDMX},
10243 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10244   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10245 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10246 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10247   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10248   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10249 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10250 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10251   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10252   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10253 #ifdef OBJ_ELF
10254 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 41)
10255 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10256   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10257   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10258 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10259   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10260 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10261   {"xgot",        no_argument, NULL, OPTION_XGOT},
10262 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10263   {"mabi", required_argument, NULL, OPTION_MABI},
10264 #define OPTION_32          (OPTION_ELF_BASE + 4)
10265   {"32",          no_argument, NULL, OPTION_32},
10266 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10267   {"n32",         no_argument, NULL, OPTION_N32},
10268 #define OPTION_64          (OPTION_ELF_BASE + 6)
10269   {"64",          no_argument, NULL, OPTION_64},
10270 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10271   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10272 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10273   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10274 #endif /* OBJ_ELF */
10275   {NULL, no_argument, NULL, 0}
10276 };
10277 size_t md_longopts_size = sizeof (md_longopts);
10278
10279 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10280    NEW_VALUE.  Warn if another value was already specified.  Note:
10281    we have to defer parsing the -march and -mtune arguments in order
10282    to handle 'from-abi' correctly, since the ABI might be specified
10283    in a later argument.  */
10284
10285 static void
10286 mips_set_option_string (string_ptr, new_value)
10287      const char **string_ptr, *new_value;
10288 {
10289   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10290     as_warn (_("A different %s was already specified, is now %s"),
10291              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10292              new_value);
10293
10294   *string_ptr = new_value;
10295 }
10296
10297 int
10298 md_parse_option (c, arg)
10299      int c;
10300      char *arg;
10301 {
10302   switch (c)
10303     {
10304     case OPTION_CONSTRUCT_FLOATS:
10305       mips_disable_float_construction = 0;
10306       break;
10307
10308     case OPTION_NO_CONSTRUCT_FLOATS:
10309       mips_disable_float_construction = 1;
10310       break;
10311
10312     case OPTION_TRAP:
10313       mips_trap = 1;
10314       break;
10315
10316     case OPTION_BREAK:
10317       mips_trap = 0;
10318       break;
10319
10320     case OPTION_EB:
10321       target_big_endian = 1;
10322       break;
10323
10324     case OPTION_EL:
10325       target_big_endian = 0;
10326       break;
10327
10328     case 'n':
10329       warn_nops = 1;
10330       break;
10331
10332     case 'O':
10333       if (arg && arg[1] == '0')
10334         mips_optimize = 1;
10335       else
10336         mips_optimize = 2;
10337       break;
10338
10339     case 'g':
10340       if (arg == NULL)
10341         mips_debug = 2;
10342       else
10343         mips_debug = atoi (arg);
10344       /* When the MIPS assembler sees -g or -g2, it does not do
10345          optimizations which limit full symbolic debugging.  We take
10346          that to be equivalent to -O0.  */
10347       if (mips_debug == 2)
10348         mips_optimize = 1;
10349       break;
10350
10351     case OPTION_MIPS1:
10352       file_mips_isa = ISA_MIPS1;
10353       break;
10354
10355     case OPTION_MIPS2:
10356       file_mips_isa = ISA_MIPS2;
10357       break;
10358
10359     case OPTION_MIPS3:
10360       file_mips_isa = ISA_MIPS3;
10361       break;
10362
10363     case OPTION_MIPS4:
10364       file_mips_isa = ISA_MIPS4;
10365       break;
10366
10367     case OPTION_MIPS5:
10368       file_mips_isa = ISA_MIPS5;
10369       break;
10370
10371     case OPTION_MIPS32:
10372       file_mips_isa = ISA_MIPS32;
10373       break;
10374
10375     case OPTION_MIPS64:
10376       file_mips_isa = ISA_MIPS64;
10377       break;
10378
10379     case OPTION_MTUNE:
10380       mips_set_option_string (&mips_tune_string, arg);
10381       break;
10382
10383     case OPTION_MARCH:
10384       mips_set_option_string (&mips_arch_string, arg);
10385       break;
10386
10387     case OPTION_M4650:
10388       mips_set_option_string (&mips_arch_string, "4650");
10389       mips_set_option_string (&mips_tune_string, "4650");
10390       break;
10391
10392     case OPTION_NO_M4650:
10393       break;
10394
10395     case OPTION_M4010:
10396       mips_set_option_string (&mips_arch_string, "4010");
10397       mips_set_option_string (&mips_tune_string, "4010");
10398       break;
10399
10400     case OPTION_NO_M4010:
10401       break;
10402
10403     case OPTION_M4100:
10404       mips_set_option_string (&mips_arch_string, "4100");
10405       mips_set_option_string (&mips_tune_string, "4100");
10406       break;
10407
10408     case OPTION_NO_M4100:
10409       break;
10410
10411     case OPTION_M3900:
10412       mips_set_option_string (&mips_arch_string, "3900");
10413       mips_set_option_string (&mips_tune_string, "3900");
10414       break;
10415
10416     case OPTION_NO_M3900:
10417       break;
10418
10419     case OPTION_MDMX:
10420       mips_opts.ase_mdmx = 1;
10421       break;
10422
10423     case OPTION_NO_MDMX:
10424       mips_opts.ase_mdmx = 0;
10425       break;
10426
10427     case OPTION_MIPS16:
10428       mips_opts.mips16 = 1;
10429       mips_no_prev_insn (false);
10430       break;
10431
10432     case OPTION_NO_MIPS16:
10433       mips_opts.mips16 = 0;
10434       mips_no_prev_insn (false);
10435       break;
10436
10437     case OPTION_MIPS3D:
10438       mips_opts.ase_mips3d = 1;
10439       break;
10440
10441     case OPTION_NO_MIPS3D:
10442       mips_opts.ase_mips3d = 0;
10443       break;
10444
10445     case OPTION_MEMBEDDED_PIC:
10446       mips_pic = EMBEDDED_PIC;
10447       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10448         {
10449           as_bad (_("-G may not be used with embedded PIC code"));
10450           return 0;
10451         }
10452       g_switch_value = 0x7fffffff;
10453       break;
10454
10455     case OPTION_FIX_VR4122:
10456       mips_fix_4122_bugs = 1;
10457       break;
10458
10459     case OPTION_NO_FIX_VR4122:
10460       mips_fix_4122_bugs = 0;
10461       break;
10462
10463     case OPTION_RELAX_BRANCH:
10464       mips_relax_branch = 1;
10465       break;
10466
10467     case OPTION_NO_RELAX_BRANCH:
10468       mips_relax_branch = 0;
10469       break;
10470
10471 #ifdef OBJ_ELF
10472       /* When generating ELF code, we permit -KPIC and -call_shared to
10473          select SVR4_PIC, and -non_shared to select no PIC.  This is
10474          intended to be compatible with Irix 5.  */
10475     case OPTION_CALL_SHARED:
10476       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10477         {
10478           as_bad (_("-call_shared is supported only for ELF format"));
10479           return 0;
10480         }
10481       mips_pic = SVR4_PIC;
10482       if (g_switch_seen && g_switch_value != 0)
10483         {
10484           as_bad (_("-G may not be used with SVR4 PIC code"));
10485           return 0;
10486         }
10487       g_switch_value = 0;
10488       break;
10489
10490     case OPTION_NON_SHARED:
10491       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10492         {
10493           as_bad (_("-non_shared is supported only for ELF format"));
10494           return 0;
10495         }
10496       mips_pic = NO_PIC;
10497       break;
10498
10499       /* The -xgot option tells the assembler to use 32 offsets when
10500          accessing the got in SVR4_PIC mode.  It is for Irix
10501          compatibility.  */
10502     case OPTION_XGOT:
10503       mips_big_got = 1;
10504       break;
10505 #endif /* OBJ_ELF */
10506
10507     case 'G':
10508       if (! USE_GLOBAL_POINTER_OPT)
10509         {
10510           as_bad (_("-G is not supported for this configuration"));
10511           return 0;
10512         }
10513       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10514         {
10515           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10516           return 0;
10517         }
10518       else
10519         g_switch_value = atoi (arg);
10520       g_switch_seen = 1;
10521       break;
10522
10523 #ifdef OBJ_ELF
10524       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10525          and -mabi=64.  */
10526     case OPTION_32:
10527       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10528         {
10529           as_bad (_("-32 is supported for ELF format only"));
10530           return 0;
10531         }
10532       mips_abi = O32_ABI;
10533       break;
10534
10535     case OPTION_N32:
10536       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10537         {
10538           as_bad (_("-n32 is supported for ELF format only"));
10539           return 0;
10540         }
10541       mips_abi = N32_ABI;
10542       break;
10543
10544     case OPTION_64:
10545       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10546         {
10547           as_bad (_("-64 is supported for ELF format only"));
10548           return 0;
10549         }
10550       mips_abi = N64_ABI;
10551       if (! support_64bit_objects())
10552         as_fatal (_("No compiled in support for 64 bit object file format"));
10553       break;
10554 #endif /* OBJ_ELF */
10555
10556     case OPTION_GP32:
10557       file_mips_gp32 = 1;
10558       break;
10559
10560     case OPTION_GP64:
10561       file_mips_gp32 = 0;
10562       break;
10563
10564     case OPTION_FP32:
10565       file_mips_fp32 = 1;
10566       break;
10567
10568     case OPTION_FP64:
10569       file_mips_fp32 = 0;
10570       break;
10571
10572 #ifdef OBJ_ELF
10573     case OPTION_MABI:
10574       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10575         {
10576           as_bad (_("-mabi is supported for ELF format only"));
10577           return 0;
10578         }
10579       if (strcmp (arg, "32") == 0)
10580         mips_abi = O32_ABI;
10581       else if (strcmp (arg, "o64") == 0)
10582         mips_abi = O64_ABI;
10583       else if (strcmp (arg, "n32") == 0)
10584         mips_abi = N32_ABI;
10585       else if (strcmp (arg, "64") == 0)
10586         {
10587           mips_abi = N64_ABI;
10588           if (! support_64bit_objects())
10589             as_fatal (_("No compiled in support for 64 bit object file "
10590                         "format"));
10591         }
10592       else if (strcmp (arg, "eabi") == 0)
10593         mips_abi = EABI_ABI;
10594       else
10595         {
10596           as_fatal (_("invalid abi -mabi=%s"), arg);
10597           return 0;
10598         }
10599       break;
10600 #endif /* OBJ_ELF */
10601
10602     case OPTION_M7000_HILO_FIX:
10603       mips_7000_hilo_fix = true;
10604       break;
10605
10606     case OPTION_MNO_7000_HILO_FIX:
10607       mips_7000_hilo_fix = false;
10608       break;
10609
10610 #ifdef OBJ_ELF
10611     case OPTION_MDEBUG:
10612       mips_flag_mdebug = true;
10613       break;
10614
10615     case OPTION_NO_MDEBUG:
10616       mips_flag_mdebug = false;
10617       break;
10618 #endif /* OBJ_ELF */
10619
10620     default:
10621       return 0;
10622     }
10623
10624   return 1;
10625 }
10626 \f
10627 /* Set up globals to generate code for the ISA or processor
10628    described by INFO.  */
10629
10630 static void
10631 mips_set_architecture (info)
10632      const struct mips_cpu_info *info;
10633 {
10634   if (info != 0)
10635     {
10636       mips_arch_info = info;
10637       mips_arch = info->cpu;
10638       mips_opts.isa = info->isa;
10639     }
10640 }
10641
10642
10643 /* Likewise for tuning.  */
10644
10645 static void
10646 mips_set_tune (info)
10647      const struct mips_cpu_info *info;
10648 {
10649   if (info != 0)
10650     {
10651       mips_tune_info = info;
10652       mips_tune = info->cpu;
10653     }
10654 }
10655
10656
10657 void
10658 mips_after_parse_args ()
10659 {
10660   /* GP relative stuff not working for PE */
10661   if (strncmp (TARGET_OS, "pe", 2) == 0
10662       && g_switch_value != 0)
10663     {
10664       if (g_switch_seen)
10665         as_bad (_("-G not supported in this configuration."));
10666       g_switch_value = 0;
10667     }
10668
10669   /* The following code determines the architecture and register size.
10670      Similar code was added to GCC 3.3 (see override_options() in
10671      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10672      as much as possible.  */
10673
10674   if (mips_arch_string != 0)
10675     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10676
10677   if (mips_tune_string != 0)
10678     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10679
10680   if (file_mips_isa != ISA_UNKNOWN)
10681     {
10682       /* Handle -mipsN.  At this point, file_mips_isa contains the
10683          ISA level specified by -mipsN, while mips_opts.isa contains
10684          the -march selection (if any).  */
10685       if (mips_arch_info != 0)
10686         {
10687           /* -march takes precedence over -mipsN, since it is more descriptive.
10688              There's no harm in specifying both as long as the ISA levels
10689              are the same.  */
10690           if (file_mips_isa != mips_opts.isa)
10691             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10692                     mips_cpu_info_from_isa (file_mips_isa)->name,
10693                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10694         }
10695       else
10696         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10697     }
10698
10699   if (mips_arch_info == 0)
10700     mips_set_architecture (mips_parse_cpu ("default CPU",
10701                                            MIPS_CPU_STRING_DEFAULT));
10702
10703   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10704     as_bad ("-march=%s is not compatible with the selected ABI",
10705             mips_arch_info->name);
10706
10707   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10708   if (mips_tune_info == 0)
10709     mips_set_tune (mips_arch_info);
10710
10711   if (file_mips_gp32 >= 0)
10712     {
10713       /* The user specified the size of the integer registers.  Make sure
10714          it agrees with the ABI and ISA.  */
10715       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10716         as_bad (_("-mgp64 used with a 32-bit processor"));
10717       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10718         as_bad (_("-mgp32 used with a 64-bit ABI"));
10719       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10720         as_bad (_("-mgp64 used with a 32-bit ABI"));
10721     }
10722   else
10723     {
10724       /* Infer the integer register size from the ABI and processor.
10725          Restrict ourselves to 32-bit registers if that's all the
10726          processor has, or if the ABI cannot handle 64-bit registers.  */
10727       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10728                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10729     }
10730
10731   /* ??? GAS treats single-float processors as though they had 64-bit
10732      float registers (although it complains when double-precision
10733      instructions are used).  As things stand, saying they have 32-bit
10734      registers would lead to spurious "register must be even" messages.
10735      So here we assume float registers are always the same size as
10736      integer ones, unless the user says otherwise.  */
10737   if (file_mips_fp32 < 0)
10738     file_mips_fp32 = file_mips_gp32;
10739
10740   /* End of GCC-shared inference code.  */
10741
10742   /* ??? When do we want this flag to be set?   Who uses it?  */
10743   if (file_mips_gp32 == 1
10744       && mips_abi == NO_ABI
10745       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10746     mips_32bitmode = 1;
10747
10748   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10749     as_bad (_("trap exception not supported at ISA 1"));
10750
10751   /* If the selected architecture includes support for ASEs, enable
10752      generation of code for them.  */
10753   if (mips_opts.mips16 == -1)
10754     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10755   if (mips_opts.ase_mips3d == -1)
10756     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10757   if (mips_opts.ase_mdmx == -1)
10758     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10759
10760   file_mips_isa = mips_opts.isa;
10761   file_ase_mips16 = mips_opts.mips16;
10762   file_ase_mips3d = mips_opts.ase_mips3d;
10763   file_ase_mdmx = mips_opts.ase_mdmx;
10764   mips_opts.gp32 = file_mips_gp32;
10765   mips_opts.fp32 = file_mips_fp32;
10766
10767   if (mips_flag_mdebug < 0)
10768     {
10769 #ifdef OBJ_MAYBE_ECOFF
10770       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10771         mips_flag_mdebug = 1;
10772       else
10773 #endif /* OBJ_MAYBE_ECOFF */
10774         mips_flag_mdebug = 0;
10775     }
10776 }
10777 \f
10778 void
10779 mips_init_after_args ()
10780 {
10781   /* initialize opcodes */
10782   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10783   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10784 }
10785
10786 long
10787 md_pcrel_from (fixP)
10788      fixS *fixP;
10789 {
10790   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10791       && fixP->fx_addsy != (symbolS *) NULL
10792       && ! S_IS_DEFINED (fixP->fx_addsy))
10793     {
10794       /* This makes a branch to an undefined symbol be a branch to the
10795          current location.  */
10796       if (mips_pic == EMBEDDED_PIC)
10797         return 4;
10798       else
10799         return 1;
10800     }
10801
10802   /* Return the address of the delay slot.  */
10803   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10804 }
10805
10806 /* This is called before the symbol table is processed.  In order to
10807    work with gcc when using mips-tfile, we must keep all local labels.
10808    However, in other cases, we want to discard them.  If we were
10809    called with -g, but we didn't see any debugging information, it may
10810    mean that gcc is smuggling debugging information through to
10811    mips-tfile, in which case we must generate all local labels.  */
10812
10813 void
10814 mips_frob_file_before_adjust ()
10815 {
10816 #ifndef NO_ECOFF_DEBUGGING
10817   if (ECOFF_DEBUGGING
10818       && mips_debug != 0
10819       && ! ecoff_debugging_seen)
10820     flag_keep_locals = 1;
10821 #endif
10822 }
10823
10824 /* Sort any unmatched HI16_S relocs so that they immediately precede
10825    the corresponding LO reloc.  This is called before md_apply_fix3 and
10826    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10827    explicit use of the %hi modifier.  */
10828
10829 void
10830 mips_frob_file ()
10831 {
10832   struct mips_hi_fixup *l;
10833
10834   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10835     {
10836       segment_info_type *seginfo;
10837       int pass;
10838
10839       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10840
10841       /* Check quickly whether the next fixup happens to be a matching
10842          %lo.  */
10843       if (l->fixp->fx_next != NULL
10844           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10845           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10846           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10847         continue;
10848
10849       /* Look through the fixups for this segment for a matching %lo.
10850          When we find one, move the %hi just in front of it.  We do
10851          this in two passes.  In the first pass, we try to find a
10852          unique %lo.  In the second pass, we permit multiple %hi
10853          relocs for a single %lo (this is a GNU extension).  */
10854       seginfo = seg_info (l->seg);
10855       for (pass = 0; pass < 2; pass++)
10856         {
10857           fixS *f, *prev;
10858
10859           prev = NULL;
10860           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10861             {
10862               /* Check whether this is a %lo fixup which matches l->fixp.  */
10863               if (f->fx_r_type == BFD_RELOC_LO16
10864                   && f->fx_addsy == l->fixp->fx_addsy
10865                   && f->fx_offset == l->fixp->fx_offset
10866                   && (pass == 1
10867                       || prev == NULL
10868                       || prev->fx_r_type != BFD_RELOC_HI16_S
10869                       || prev->fx_addsy != f->fx_addsy
10870                       || prev->fx_offset !=  f->fx_offset))
10871                 {
10872                   fixS **pf;
10873
10874                   /* Move l->fixp before f.  */
10875                   for (pf = &seginfo->fix_root;
10876                        *pf != l->fixp;
10877                        pf = &(*pf)->fx_next)
10878                     assert (*pf != NULL);
10879
10880                   *pf = l->fixp->fx_next;
10881
10882                   l->fixp->fx_next = f;
10883                   if (prev == NULL)
10884                     seginfo->fix_root = l->fixp;
10885                   else
10886                     prev->fx_next = l->fixp;
10887
10888                   break;
10889                 }
10890
10891               prev = f;
10892             }
10893
10894           if (f != NULL)
10895             break;
10896
10897 #if 0 /* GCC code motion plus incomplete dead code elimination
10898          can leave a %hi without a %lo.  */
10899           if (pass == 1)
10900             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10901                            _("Unmatched %%hi reloc"));
10902 #endif
10903         }
10904     }
10905 }
10906
10907 /* When generating embedded PIC code we need to use a special
10908    relocation to represent the difference of two symbols in the .text
10909    section (switch tables use a difference of this sort).  See
10910    include/coff/mips.h for details.  This macro checks whether this
10911    fixup requires the special reloc.  */
10912 #define SWITCH_TABLE(fixp) \
10913   ((fixp)->fx_r_type == BFD_RELOC_32 \
10914    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10915    && (fixp)->fx_addsy != NULL \
10916    && (fixp)->fx_subsy != NULL \
10917    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10918    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10919
10920 /* When generating embedded PIC code we must keep all PC relative
10921    relocations, in case the linker has to relax a call.  We also need
10922    to keep relocations for switch table entries.
10923
10924    We may have combined relocations without symbols in the N32/N64 ABI.
10925    We have to prevent gas from dropping them.  */
10926
10927 int
10928 mips_force_relocation (fixp)
10929      fixS *fixp;
10930 {
10931   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10932       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10933       || S_FORCE_RELOC (fixp->fx_addsy))
10934     return 1;
10935
10936   if (HAVE_NEWABI
10937       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10938       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10939           || fixp->fx_r_type == BFD_RELOC_HI16_S
10940           || fixp->fx_r_type == BFD_RELOC_LO16))
10941     return 1;
10942
10943   return (mips_pic == EMBEDDED_PIC
10944           && (fixp->fx_pcrel
10945               || SWITCH_TABLE (fixp)
10946               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10947               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10948 }
10949
10950 #ifdef OBJ_ELF
10951 static int
10952 mips_need_elf_addend_fixup (fixP)
10953      fixS *fixP;
10954 {
10955   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10956     return 1;
10957   if (mips_pic == EMBEDDED_PIC
10958       && S_IS_WEAK (fixP->fx_addsy))
10959     return 1;
10960   if (mips_pic != EMBEDDED_PIC
10961       && (S_IS_WEAK (fixP->fx_addsy)
10962           || S_IS_EXTERNAL (fixP->fx_addsy))
10963       && !S_IS_COMMON (fixP->fx_addsy))
10964     return 1;
10965   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10966       && (((bfd_get_section_flags (stdoutput,
10967                                    S_GET_SEGMENT (fixP->fx_addsy))
10968             & SEC_LINK_ONCE) != 0)
10969           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10970                        ".gnu.linkonce",
10971                        sizeof (".gnu.linkonce") - 1)))
10972     return 1;
10973   return 0;
10974 }
10975 #endif
10976
10977 /* Apply a fixup to the object file.  */
10978
10979 void
10980 md_apply_fix3 (fixP, valP, seg)
10981      fixS *fixP;
10982      valueT *valP;
10983      segT seg ATTRIBUTE_UNUSED;
10984 {
10985   bfd_byte *buf;
10986   long insn;
10987   valueT value;
10988   static int previous_fx_r_type = 0;
10989
10990   /* FIXME: Maybe just return for all reloc types not listed below?
10991      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
10992   if (fixP->fx_r_type == BFD_RELOC_8)
10993       return;
10994
10995   assert (fixP->fx_size == 4
10996           || fixP->fx_r_type == BFD_RELOC_16
10997           || fixP->fx_r_type == BFD_RELOC_32
10998           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10999           || fixP->fx_r_type == BFD_RELOC_HI16_S
11000           || fixP->fx_r_type == BFD_RELOC_LO16
11001           || fixP->fx_r_type == BFD_RELOC_GPREL16
11002           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11003           || fixP->fx_r_type == BFD_RELOC_GPREL32
11004           || fixP->fx_r_type == BFD_RELOC_64
11005           || fixP->fx_r_type == BFD_RELOC_CTOR
11006           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11007           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11008           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11009           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11010           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11011           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11012           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11013           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11014           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11015
11016   value = *valP;
11017
11018   /* If we aren't adjusting this fixup to be against the section
11019      symbol, we need to adjust the value.  */
11020 #ifdef OBJ_ELF
11021   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11022     {
11023       if (mips_need_elf_addend_fixup (fixP))
11024         {
11025           reloc_howto_type *howto;
11026           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11027
11028           value -= symval;
11029
11030           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11031           if (value != 0 && howto->partial_inplace
11032               && (! fixP->fx_pcrel || howto->pcrel_offset))
11033             {
11034               /* In this case, the bfd_install_relocation routine will
11035                  incorrectly add the symbol value back in.  We just want
11036                  the addend to appear in the object file.
11037                  
11038                  howto->pcrel_offset is added for R_MIPS_PC16, which is
11039                  generated for code like
11040                  
11041                         globl g1 .text
11042                         .text
11043                         .space 20
11044                  g1:
11045                  x:
11046                         bal g1
11047                */
11048               value -= symval;
11049
11050               /* Make sure the addend is still non-zero.  If it became zero
11051                  after the last operation, set it to a spurious value and
11052                  subtract the same value from the object file's contents.  */
11053               if (value == 0)
11054                 {
11055                   value = 8;
11056
11057                   /* The in-place addends for LO16 relocations are signed;
11058                      leave the matching HI16 in-place addends as zero.  */
11059                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11060                     {
11061                       bfd_vma contents, mask, field;
11062
11063                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11064                                                + fixP->fx_where,
11065                                                fixP->fx_size * 8,
11066                                                target_big_endian);
11067
11068                       /* MASK has bits set where the relocation should go.
11069                          FIELD is -value, shifted into the appropriate place
11070                          for this relocation.  */
11071                       mask = 1 << (howto->bitsize - 1);
11072                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11073                       field = (-value >> howto->rightshift) << howto->bitpos;
11074
11075                       bfd_put_bits ((field & mask) | (contents & ~mask),
11076                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11077                                     fixP->fx_size * 8,
11078                                     target_big_endian);
11079                     }
11080                 }
11081             }
11082         }
11083
11084       /* This code was generated using trial and error and so is
11085          fragile and not trustworthy.  If you change it, you should
11086          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11087          they still pass.  */
11088       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11089         {
11090           value += fixP->fx_frag->fr_address + fixP->fx_where;
11091
11092           /* BFD's REL handling, for MIPS, is _very_ weird.
11093              This gives the right results, but it can't possibly
11094              be the way things are supposed to work.  */
11095           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
11096                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
11097               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11098             value += fixP->fx_frag->fr_address + fixP->fx_where;
11099         }
11100     }
11101 #endif
11102
11103   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11104
11105   /* We are not done if this is a composite relocation to set up gp.  */
11106   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11107       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11108            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11109                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11110                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11111     fixP->fx_done = 1;
11112   previous_fx_r_type = fixP->fx_r_type;
11113
11114   switch (fixP->fx_r_type)
11115     {
11116     case BFD_RELOC_MIPS_JMP:
11117     case BFD_RELOC_MIPS_SHIFT5:
11118     case BFD_RELOC_MIPS_SHIFT6:
11119     case BFD_RELOC_MIPS_GOT_DISP:
11120     case BFD_RELOC_MIPS_GOT_PAGE:
11121     case BFD_RELOC_MIPS_GOT_OFST:
11122     case BFD_RELOC_MIPS_SUB:
11123     case BFD_RELOC_MIPS_INSERT_A:
11124     case BFD_RELOC_MIPS_INSERT_B:
11125     case BFD_RELOC_MIPS_DELETE:
11126     case BFD_RELOC_MIPS_HIGHEST:
11127     case BFD_RELOC_MIPS_HIGHER:
11128     case BFD_RELOC_MIPS_SCN_DISP:
11129     case BFD_RELOC_MIPS_REL16:
11130     case BFD_RELOC_MIPS_RELGOT:
11131     case BFD_RELOC_MIPS_JALR:
11132     case BFD_RELOC_HI16:
11133     case BFD_RELOC_HI16_S:
11134     case BFD_RELOC_GPREL16:
11135     case BFD_RELOC_MIPS_LITERAL:
11136     case BFD_RELOC_MIPS_CALL16:
11137     case BFD_RELOC_MIPS_GOT16:
11138     case BFD_RELOC_GPREL32:
11139     case BFD_RELOC_MIPS_GOT_HI16:
11140     case BFD_RELOC_MIPS_GOT_LO16:
11141     case BFD_RELOC_MIPS_CALL_HI16:
11142     case BFD_RELOC_MIPS_CALL_LO16:
11143     case BFD_RELOC_MIPS16_GPREL:
11144       if (fixP->fx_pcrel)
11145         as_bad_where (fixP->fx_file, fixP->fx_line,
11146                       _("Invalid PC relative reloc"));
11147       /* Nothing needed to do. The value comes from the reloc entry */
11148       break;
11149
11150     case BFD_RELOC_MIPS16_JMP:
11151       /* We currently always generate a reloc against a symbol, which
11152          means that we don't want an addend even if the symbol is
11153          defined.  */
11154       fixP->fx_addnumber = 0;
11155       break;
11156
11157     case BFD_RELOC_PCREL_HI16_S:
11158       /* The addend for this is tricky if it is internal, so we just
11159          do everything here rather than in bfd_install_relocation.  */
11160       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11161           && !fixP->fx_done
11162           && value != 0)
11163         break;
11164       if (fixP->fx_addsy
11165           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11166         {
11167           /* For an external symbol adjust by the address to make it
11168              pcrel_offset.  We use the address of the RELLO reloc
11169              which follows this one.  */
11170           value += (fixP->fx_next->fx_frag->fr_address
11171                     + fixP->fx_next->fx_where);
11172         }
11173       value = ((value + 0x8000) >> 16) & 0xffff;
11174       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11175       if (target_big_endian)
11176         buf += 2;
11177       md_number_to_chars ((char *) buf, value, 2);
11178       break;
11179
11180     case BFD_RELOC_PCREL_LO16:
11181       /* The addend for this is tricky if it is internal, so we just
11182          do everything here rather than in bfd_install_relocation.  */
11183       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11184           && !fixP->fx_done
11185           && value != 0)
11186         break;
11187       if (fixP->fx_addsy
11188           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11189         value += fixP->fx_frag->fr_address + fixP->fx_where;
11190       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11191       if (target_big_endian)
11192         buf += 2;
11193       md_number_to_chars ((char *) buf, value, 2);
11194       break;
11195
11196     case BFD_RELOC_64:
11197       /* This is handled like BFD_RELOC_32, but we output a sign
11198          extended value if we are only 32 bits.  */
11199       if (fixP->fx_done
11200           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11201         {
11202           if (8 <= sizeof (valueT))
11203             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11204                                 value, 8);
11205           else
11206             {
11207               long w1, w2;
11208               long hiv;
11209
11210               w1 = w2 = fixP->fx_where;
11211               if (target_big_endian)
11212                 w1 += 4;
11213               else
11214                 w2 += 4;
11215               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11216               if ((value & 0x80000000) != 0)
11217                 hiv = 0xffffffff;
11218               else
11219                 hiv = 0;
11220               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11221             }
11222         }
11223       break;
11224
11225     case BFD_RELOC_RVA:
11226     case BFD_RELOC_32:
11227       /* If we are deleting this reloc entry, we must fill in the
11228          value now.  This can happen if we have a .word which is not
11229          resolved when it appears but is later defined.  We also need
11230          to fill in the value if this is an embedded PIC switch table
11231          entry.  */
11232       if (fixP->fx_done
11233           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11234         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11235                             value, 4);
11236       break;
11237
11238     case BFD_RELOC_16:
11239       /* If we are deleting this reloc entry, we must fill in the
11240          value now.  */
11241       assert (fixP->fx_size == 2);
11242       if (fixP->fx_done)
11243         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11244                             value, 2);
11245       break;
11246
11247     case BFD_RELOC_LO16:
11248       /* When handling an embedded PIC switch statement, we can wind
11249          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11250       if (fixP->fx_done)
11251         {
11252           if (value + 0x8000 > 0xffff)
11253             as_bad_where (fixP->fx_file, fixP->fx_line,
11254                           _("relocation overflow"));
11255           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11256           if (target_big_endian)
11257             buf += 2;
11258           md_number_to_chars ((char *) buf, value, 2);
11259         }
11260       break;
11261
11262     case BFD_RELOC_16_PCREL_S2:
11263       if ((value & 0x3) != 0)
11264         as_bad_where (fixP->fx_file, fixP->fx_line,
11265                       _("Branch to odd address (%lx)"), (long) value);
11266
11267       /* Fall through.  */
11268
11269     case BFD_RELOC_16_PCREL:
11270       /*
11271        * We need to save the bits in the instruction since fixup_segment()
11272        * might be deleting the relocation entry (i.e., a branch within
11273        * the current segment).
11274        */
11275       if (!fixP->fx_done && value != 0)
11276         break;
11277       /* If 'value' is zero, the remaining reloc code won't actually
11278          do the store, so it must be done here.  This is probably
11279          a bug somewhere.  */
11280       if (!fixP->fx_done
11281           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11282               || fixP->fx_addsy == NULL                 /* ??? */
11283               || ! S_IS_DEFINED (fixP->fx_addsy)))
11284         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11285
11286       value = (offsetT) value >> 2;
11287
11288       /* update old instruction data */
11289       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11290       if (target_big_endian)
11291         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11292       else
11293         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11294
11295       if (value + 0x8000 <= 0xffff)
11296         insn |= value & 0xffff;
11297       else
11298         {
11299           /* The branch offset is too large.  If this is an
11300              unconditional branch, and we are not generating PIC code,
11301              we can convert it to an absolute jump instruction.  */
11302           if (mips_pic == NO_PIC
11303               && fixP->fx_done
11304               && fixP->fx_frag->fr_address >= text_section->vma
11305               && (fixP->fx_frag->fr_address
11306                   < text_section->vma + text_section->_raw_size)
11307               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11308                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11309                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11310             {
11311               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11312                 insn = 0x0c000000;      /* jal */
11313               else
11314                 insn = 0x08000000;      /* j */
11315               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11316               fixP->fx_done = 0;
11317               fixP->fx_addsy = section_symbol (text_section);
11318               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11319             }
11320           else
11321             {
11322               /* If we got here, we have branch-relaxation disabled,
11323                  and there's nothing we can do to fix this instruction
11324                  without turning it into a longer sequence.  */
11325               as_bad_where (fixP->fx_file, fixP->fx_line,
11326                             _("Branch out of range"));
11327             }
11328         }
11329
11330       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11331       break;
11332
11333     case BFD_RELOC_VTABLE_INHERIT:
11334       fixP->fx_done = 0;
11335       if (fixP->fx_addsy
11336           && !S_IS_DEFINED (fixP->fx_addsy)
11337           && !S_IS_WEAK (fixP->fx_addsy))
11338         S_SET_WEAK (fixP->fx_addsy);
11339       break;
11340
11341     case BFD_RELOC_VTABLE_ENTRY:
11342       fixP->fx_done = 0;
11343       break;
11344
11345     default:
11346       internalError ();
11347     }
11348 }
11349
11350 #if 0
11351 void
11352 printInsn (oc)
11353      unsigned long oc;
11354 {
11355   const struct mips_opcode *p;
11356   int treg, sreg, dreg, shamt;
11357   short imm;
11358   const char *args;
11359   int i;
11360
11361   for (i = 0; i < NUMOPCODES; ++i)
11362     {
11363       p = &mips_opcodes[i];
11364       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11365         {
11366           printf ("%08lx %s\t", oc, p->name);
11367           treg = (oc >> 16) & 0x1f;
11368           sreg = (oc >> 21) & 0x1f;
11369           dreg = (oc >> 11) & 0x1f;
11370           shamt = (oc >> 6) & 0x1f;
11371           imm = oc;
11372           for (args = p->args;; ++args)
11373             {
11374               switch (*args)
11375                 {
11376                 case '\0':
11377                   printf ("\n");
11378                   break;
11379
11380                 case ',':
11381                 case '(':
11382                 case ')':
11383                   printf ("%c", *args);
11384                   continue;
11385
11386                 case 'r':
11387                   assert (treg == sreg);
11388                   printf ("$%d,$%d", treg, sreg);
11389                   continue;
11390
11391                 case 'd':
11392                 case 'G':
11393                   printf ("$%d", dreg);
11394                   continue;
11395
11396                 case 't':
11397                 case 'E':
11398                   printf ("$%d", treg);
11399                   continue;
11400
11401                 case 'k':
11402                   printf ("0x%x", treg);
11403                   continue;
11404
11405                 case 'b':
11406                 case 's':
11407                   printf ("$%d", sreg);
11408                   continue;
11409
11410                 case 'a':
11411                   printf ("0x%08lx", oc & 0x1ffffff);
11412                   continue;
11413
11414                 case 'i':
11415                 case 'j':
11416                 case 'o':
11417                 case 'u':
11418                   printf ("%d", imm);
11419                   continue;
11420
11421                 case '<':
11422                 case '>':
11423                   printf ("$%d", shamt);
11424                   continue;
11425
11426                 default:
11427                   internalError ();
11428                 }
11429               break;
11430             }
11431           return;
11432         }
11433     }
11434   printf (_("%08lx  UNDEFINED\n"), oc);
11435 }
11436 #endif
11437
11438 static symbolS *
11439 get_symbol ()
11440 {
11441   int c;
11442   char *name;
11443   symbolS *p;
11444
11445   name = input_line_pointer;
11446   c = get_symbol_end ();
11447   p = (symbolS *) symbol_find_or_make (name);
11448   *input_line_pointer = c;
11449   return p;
11450 }
11451
11452 /* Align the current frag to a given power of two.  The MIPS assembler
11453    also automatically adjusts any preceding label.  */
11454
11455 static void
11456 mips_align (to, fill, label)
11457      int to;
11458      int fill;
11459      symbolS *label;
11460 {
11461   mips_emit_delays (false);
11462   frag_align (to, fill, 0);
11463   record_alignment (now_seg, to);
11464   if (label != NULL)
11465     {
11466       assert (S_GET_SEGMENT (label) == now_seg);
11467       symbol_set_frag (label, frag_now);
11468       S_SET_VALUE (label, (valueT) frag_now_fix ());
11469     }
11470 }
11471
11472 /* Align to a given power of two.  .align 0 turns off the automatic
11473    alignment used by the data creating pseudo-ops.  */
11474
11475 static void
11476 s_align (x)
11477      int x ATTRIBUTE_UNUSED;
11478 {
11479   register int temp;
11480   register long temp_fill;
11481   long max_alignment = 15;
11482
11483   /*
11484
11485     o  Note that the assembler pulls down any immediately preceeding label
11486        to the aligned address.
11487     o  It's not documented but auto alignment is reinstated by
11488        a .align pseudo instruction.
11489     o  Note also that after auto alignment is turned off the mips assembler
11490        issues an error on attempt to assemble an improperly aligned data item.
11491        We don't.
11492
11493     */
11494
11495   temp = get_absolute_expression ();
11496   if (temp > max_alignment)
11497     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11498   else if (temp < 0)
11499     {
11500       as_warn (_("Alignment negative: 0 assumed."));
11501       temp = 0;
11502     }
11503   if (*input_line_pointer == ',')
11504     {
11505       ++input_line_pointer;
11506       temp_fill = get_absolute_expression ();
11507     }
11508   else
11509     temp_fill = 0;
11510   if (temp)
11511     {
11512       auto_align = 1;
11513       mips_align (temp, (int) temp_fill,
11514                   insn_labels != NULL ? insn_labels->label : NULL);
11515     }
11516   else
11517     {
11518       auto_align = 0;
11519     }
11520
11521   demand_empty_rest_of_line ();
11522 }
11523
11524 void
11525 mips_flush_pending_output ()
11526 {
11527   mips_emit_delays (false);
11528   mips_clear_insn_labels ();
11529 }
11530
11531 static void
11532 s_change_sec (sec)
11533      int sec;
11534 {
11535   segT seg;
11536
11537   /* When generating embedded PIC code, we only use the .text, .lit8,
11538      .sdata and .sbss sections.  We change the .data and .rdata
11539      pseudo-ops to use .sdata.  */
11540   if (mips_pic == EMBEDDED_PIC
11541       && (sec == 'd' || sec == 'r'))
11542     sec = 's';
11543
11544 #ifdef OBJ_ELF
11545   /* The ELF backend needs to know that we are changing sections, so
11546      that .previous works correctly.  We could do something like check
11547      for an obj_section_change_hook macro, but that might be confusing
11548      as it would not be appropriate to use it in the section changing
11549      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11550      This should be cleaner, somehow.  */
11551   obj_elf_section_change_hook ();
11552 #endif
11553
11554   mips_emit_delays (false);
11555   switch (sec)
11556     {
11557     case 't':
11558       s_text (0);
11559       break;
11560     case 'd':
11561       s_data (0);
11562       break;
11563     case 'b':
11564       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11565       demand_empty_rest_of_line ();
11566       break;
11567
11568     case 'r':
11569       if (USE_GLOBAL_POINTER_OPT)
11570         {
11571           seg = subseg_new (RDATA_SECTION_NAME,
11572                             (subsegT) get_absolute_expression ());
11573           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11574             {
11575               bfd_set_section_flags (stdoutput, seg,
11576                                      (SEC_ALLOC
11577                                       | SEC_LOAD
11578                                       | SEC_READONLY
11579                                       | SEC_RELOC
11580                                       | SEC_DATA));
11581               if (strcmp (TARGET_OS, "elf") != 0)
11582                 record_alignment (seg, 4);
11583             }
11584           demand_empty_rest_of_line ();
11585         }
11586       else
11587         {
11588           as_bad (_("No read only data section in this object file format"));
11589           demand_empty_rest_of_line ();
11590           return;
11591         }
11592       break;
11593
11594     case 's':
11595       if (USE_GLOBAL_POINTER_OPT)
11596         {
11597           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11598           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11599             {
11600               bfd_set_section_flags (stdoutput, seg,
11601                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11602                                      | SEC_DATA);
11603               if (strcmp (TARGET_OS, "elf") != 0)
11604                 record_alignment (seg, 4);
11605             }
11606           demand_empty_rest_of_line ();
11607           break;
11608         }
11609       else
11610         {
11611           as_bad (_("Global pointers not supported; recompile -G 0"));
11612           demand_empty_rest_of_line ();
11613           return;
11614         }
11615     }
11616
11617   auto_align = 1;
11618 }
11619   
11620 void
11621 s_change_section (ignore)
11622      int ignore ATTRIBUTE_UNUSED;
11623 {
11624 #ifdef OBJ_ELF
11625   char *section_name;
11626   char c;
11627   char next_c;
11628   int section_type;
11629   int section_flag;
11630   int section_entry_size;
11631   int section_alignment;
11632   
11633   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11634     return;
11635
11636   section_name = input_line_pointer;
11637   c = get_symbol_end ();
11638   next_c = *(input_line_pointer + 1);
11639
11640   /* Do we have .section Name<,"flags">?  */
11641   if (c != ',' || (c == ',' && next_c == '"'))
11642     {
11643       /* just after name is now '\0'.  */
11644       *input_line_pointer = c;
11645       input_line_pointer = section_name;
11646       obj_elf_section (ignore);
11647       return;
11648     }
11649   input_line_pointer++;
11650
11651   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11652   if (c == ',')
11653     section_type = get_absolute_expression ();
11654   else
11655     section_type = 0;
11656   if (*input_line_pointer++ == ',')
11657     section_flag = get_absolute_expression ();
11658   else
11659     section_flag = 0;
11660   if (*input_line_pointer++ == ',')
11661     section_entry_size = get_absolute_expression ();
11662   else
11663     section_entry_size = 0;
11664   if (*input_line_pointer++ == ',')
11665     section_alignment = get_absolute_expression ();
11666   else
11667     section_alignment = 0;
11668
11669   obj_elf_change_section (section_name, section_type, section_flag,
11670                           section_entry_size, 0, 0, 0);
11671 #endif /* OBJ_ELF */
11672 }
11673
11674 void
11675 mips_enable_auto_align ()
11676 {
11677   auto_align = 1;
11678 }
11679
11680 static void
11681 s_cons (log_size)
11682      int log_size;
11683 {
11684   symbolS *label;
11685
11686   label = insn_labels != NULL ? insn_labels->label : NULL;
11687   mips_emit_delays (false);
11688   if (log_size > 0 && auto_align)
11689     mips_align (log_size, 0, label);
11690   mips_clear_insn_labels ();
11691   cons (1 << log_size);
11692 }
11693
11694 static void
11695 s_float_cons (type)
11696      int type;
11697 {
11698   symbolS *label;
11699
11700   label = insn_labels != NULL ? insn_labels->label : NULL;
11701
11702   mips_emit_delays (false);
11703
11704   if (auto_align)
11705     {
11706       if (type == 'd')
11707         mips_align (3, 0, label);
11708       else
11709         mips_align (2, 0, label);
11710     }
11711
11712   mips_clear_insn_labels ();
11713
11714   float_cons (type);
11715 }
11716
11717 /* Handle .globl.  We need to override it because on Irix 5 you are
11718    permitted to say
11719        .globl foo .text
11720    where foo is an undefined symbol, to mean that foo should be
11721    considered to be the address of a function.  */
11722
11723 static void
11724 s_mips_globl (x)
11725      int x ATTRIBUTE_UNUSED;
11726 {
11727   char *name;
11728   int c;
11729   symbolS *symbolP;
11730   flagword flag;
11731
11732   name = input_line_pointer;
11733   c = get_symbol_end ();
11734   symbolP = symbol_find_or_make (name);
11735   *input_line_pointer = c;
11736   SKIP_WHITESPACE ();
11737
11738   /* On Irix 5, every global symbol that is not explicitly labelled as
11739      being a function is apparently labelled as being an object.  */
11740   flag = BSF_OBJECT;
11741
11742   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11743     {
11744       char *secname;
11745       asection *sec;
11746
11747       secname = input_line_pointer;
11748       c = get_symbol_end ();
11749       sec = bfd_get_section_by_name (stdoutput, secname);
11750       if (sec == NULL)
11751         as_bad (_("%s: no such section"), secname);
11752       *input_line_pointer = c;
11753
11754       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11755         flag = BSF_FUNCTION;
11756     }
11757
11758   symbol_get_bfdsym (symbolP)->flags |= flag;
11759
11760   S_SET_EXTERNAL (symbolP);
11761   demand_empty_rest_of_line ();
11762 }
11763
11764 static void
11765 s_option (x)
11766      int x ATTRIBUTE_UNUSED;
11767 {
11768   char *opt;
11769   char c;
11770
11771   opt = input_line_pointer;
11772   c = get_symbol_end ();
11773
11774   if (*opt == 'O')
11775     {
11776       /* FIXME: What does this mean?  */
11777     }
11778   else if (strncmp (opt, "pic", 3) == 0)
11779     {
11780       int i;
11781
11782       i = atoi (opt + 3);
11783       if (i == 0)
11784         mips_pic = NO_PIC;
11785       else if (i == 2)
11786         mips_pic = SVR4_PIC;
11787       else
11788         as_bad (_(".option pic%d not supported"), i);
11789
11790       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11791         {
11792           if (g_switch_seen && g_switch_value != 0)
11793             as_warn (_("-G may not be used with SVR4 PIC code"));
11794           g_switch_value = 0;
11795           bfd_set_gp_size (stdoutput, 0);
11796         }
11797     }
11798   else
11799     as_warn (_("Unrecognized option \"%s\""), opt);
11800
11801   *input_line_pointer = c;
11802   demand_empty_rest_of_line ();
11803 }
11804
11805 /* This structure is used to hold a stack of .set values.  */
11806
11807 struct mips_option_stack
11808 {
11809   struct mips_option_stack *next;
11810   struct mips_set_options options;
11811 };
11812
11813 static struct mips_option_stack *mips_opts_stack;
11814
11815 /* Handle the .set pseudo-op.  */
11816
11817 static void
11818 s_mipsset (x)
11819      int x ATTRIBUTE_UNUSED;
11820 {
11821   char *name = input_line_pointer, ch;
11822
11823   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11824     ++input_line_pointer;
11825   ch = *input_line_pointer;
11826   *input_line_pointer = '\0';
11827
11828   if (strcmp (name, "reorder") == 0)
11829     {
11830       if (mips_opts.noreorder && prev_nop_frag != NULL)
11831         {
11832           /* If we still have pending nops, we can discard them.  The
11833              usual nop handling will insert any that are still
11834              needed.  */
11835           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11836                                     * (mips_opts.mips16 ? 2 : 4));
11837           prev_nop_frag = NULL;
11838         }
11839       mips_opts.noreorder = 0;
11840     }
11841   else if (strcmp (name, "noreorder") == 0)
11842     {
11843       mips_emit_delays (true);
11844       mips_opts.noreorder = 1;
11845       mips_any_noreorder = 1;
11846     }
11847   else if (strcmp (name, "at") == 0)
11848     {
11849       mips_opts.noat = 0;
11850     }
11851   else if (strcmp (name, "noat") == 0)
11852     {
11853       mips_opts.noat = 1;
11854     }
11855   else if (strcmp (name, "macro") == 0)
11856     {
11857       mips_opts.warn_about_macros = 0;
11858     }
11859   else if (strcmp (name, "nomacro") == 0)
11860     {
11861       if (mips_opts.noreorder == 0)
11862         as_bad (_("`noreorder' must be set before `nomacro'"));
11863       mips_opts.warn_about_macros = 1;
11864     }
11865   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11866     {
11867       mips_opts.nomove = 0;
11868     }
11869   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11870     {
11871       mips_opts.nomove = 1;
11872     }
11873   else if (strcmp (name, "bopt") == 0)
11874     {
11875       mips_opts.nobopt = 0;
11876     }
11877   else if (strcmp (name, "nobopt") == 0)
11878     {
11879       mips_opts.nobopt = 1;
11880     }
11881   else if (strcmp (name, "mips16") == 0
11882            || strcmp (name, "MIPS-16") == 0)
11883     mips_opts.mips16 = 1;
11884   else if (strcmp (name, "nomips16") == 0
11885            || strcmp (name, "noMIPS-16") == 0)
11886     mips_opts.mips16 = 0;
11887   else if (strcmp (name, "mips3d") == 0)
11888     mips_opts.ase_mips3d = 1;
11889   else if (strcmp (name, "nomips3d") == 0)
11890     mips_opts.ase_mips3d = 0;
11891   else if (strcmp (name, "mdmx") == 0)
11892     mips_opts.ase_mdmx = 1;
11893   else if (strcmp (name, "nomdmx") == 0)
11894     mips_opts.ase_mdmx = 0;
11895   else if (strncmp (name, "mips", 4) == 0)
11896     {
11897       int isa;
11898
11899       /* Permit the user to change the ISA on the fly.  Needless to
11900          say, misuse can cause serious problems.  */
11901       isa = atoi (name + 4);
11902       switch (isa)
11903         {
11904         case  0:
11905           mips_opts.gp32 = file_mips_gp32;
11906           mips_opts.fp32 = file_mips_fp32;
11907           break;
11908         case  1:
11909         case  2:
11910         case 32:
11911           mips_opts.gp32 = 1;
11912           mips_opts.fp32 = 1;
11913           break;
11914         case  3:
11915         case  4:
11916         case  5:
11917         case 64:
11918           mips_opts.gp32 = 0;
11919           mips_opts.fp32 = 0;
11920           break;
11921         default:
11922           as_bad (_("unknown ISA level %s"), name + 4);
11923           break;
11924         }
11925
11926       switch (isa)
11927         {
11928         case  0: mips_opts.isa = file_mips_isa;   break;
11929         case  1: mips_opts.isa = ISA_MIPS1;       break;
11930         case  2: mips_opts.isa = ISA_MIPS2;       break;
11931         case  3: mips_opts.isa = ISA_MIPS3;       break;
11932         case  4: mips_opts.isa = ISA_MIPS4;       break;
11933         case  5: mips_opts.isa = ISA_MIPS5;       break;
11934         case 32: mips_opts.isa = ISA_MIPS32;      break;
11935         case 64: mips_opts.isa = ISA_MIPS64;      break;
11936         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11937         }
11938     }
11939   else if (strcmp (name, "autoextend") == 0)
11940     mips_opts.noautoextend = 0;
11941   else if (strcmp (name, "noautoextend") == 0)
11942     mips_opts.noautoextend = 1;
11943   else if (strcmp (name, "push") == 0)
11944     {
11945       struct mips_option_stack *s;
11946
11947       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11948       s->next = mips_opts_stack;
11949       s->options = mips_opts;
11950       mips_opts_stack = s;
11951     }
11952   else if (strcmp (name, "pop") == 0)
11953     {
11954       struct mips_option_stack *s;
11955
11956       s = mips_opts_stack;
11957       if (s == NULL)
11958         as_bad (_(".set pop with no .set push"));
11959       else
11960         {
11961           /* If we're changing the reorder mode we need to handle
11962              delay slots correctly.  */
11963           if (s->options.noreorder && ! mips_opts.noreorder)
11964             mips_emit_delays (true);
11965           else if (! s->options.noreorder && mips_opts.noreorder)
11966             {
11967               if (prev_nop_frag != NULL)
11968                 {
11969                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11970                                             * (mips_opts.mips16 ? 2 : 4));
11971                   prev_nop_frag = NULL;
11972                 }
11973             }
11974
11975           mips_opts = s->options;
11976           mips_opts_stack = s->next;
11977           free (s);
11978         }
11979     }
11980   else
11981     {
11982       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11983     }
11984   *input_line_pointer = ch;
11985   demand_empty_rest_of_line ();
11986 }
11987
11988 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11989    .option pic2.  It means to generate SVR4 PIC calls.  */
11990
11991 static void
11992 s_abicalls (ignore)
11993      int ignore ATTRIBUTE_UNUSED;
11994 {
11995   mips_pic = SVR4_PIC;
11996   if (USE_GLOBAL_POINTER_OPT)
11997     {
11998       if (g_switch_seen && g_switch_value != 0)
11999         as_warn (_("-G may not be used with SVR4 PIC code"));
12000       g_switch_value = 0;
12001     }
12002   bfd_set_gp_size (stdoutput, 0);
12003   demand_empty_rest_of_line ();
12004 }
12005
12006 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12007    PIC code.  It sets the $gp register for the function based on the
12008    function address, which is in the register named in the argument.
12009    This uses a relocation against _gp_disp, which is handled specially
12010    by the linker.  The result is:
12011         lui     $gp,%hi(_gp_disp)
12012         addiu   $gp,$gp,%lo(_gp_disp)
12013         addu    $gp,$gp,.cpload argument
12014    The .cpload argument is normally $25 == $t9.  */
12015
12016 static void
12017 s_cpload (ignore)
12018      int ignore ATTRIBUTE_UNUSED;
12019 {
12020   expressionS ex;
12021   int icnt = 0;
12022
12023   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12024      .cpload is ignored.  */
12025   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12026     {
12027       s_ignore (0);
12028       return;
12029     }
12030
12031   /* .cpload should be in a .set noreorder section.  */
12032   if (mips_opts.noreorder == 0)
12033     as_warn (_(".cpload not in noreorder section"));
12034
12035   ex.X_op = O_symbol;
12036   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12037   ex.X_op_symbol = NULL;
12038   ex.X_add_number = 0;
12039
12040   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12041   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12042
12043   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12044   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12045                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12046
12047   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12048                mips_gp_register, mips_gp_register, tc_get_register (0));
12049
12050   demand_empty_rest_of_line ();
12051 }
12052
12053 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12054      .cpsetup $reg1, offset|$reg2, label
12055
12056    If offset is given, this results in:
12057      sd         $gp, offset($sp)
12058      lui        $gp, %hi(%neg(%gp_rel(label)))
12059      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12060      daddu      $gp, $gp, $reg1
12061
12062    If $reg2 is given, this results in:
12063      daddu      $reg2, $gp, $0
12064      lui        $gp, %hi(%neg(%gp_rel(label)))
12065      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12066      daddu      $gp, $gp, $reg1
12067    $reg1 is normally $25 == $t9.  */
12068 static void
12069 s_cpsetup (ignore)
12070      int ignore ATTRIBUTE_UNUSED;
12071 {
12072   expressionS ex_off;
12073   expressionS ex_sym;
12074   int reg1;
12075   int icnt = 0;
12076   char *f;
12077
12078   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12079      We also need NewABI support.  */
12080   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12081     {
12082       s_ignore (0);
12083       return;
12084     }
12085
12086   reg1 = tc_get_register (0);
12087   SKIP_WHITESPACE ();
12088   if (*input_line_pointer != ',')
12089     {
12090       as_bad (_("missing argument separator ',' for .cpsetup"));
12091       return;
12092     }
12093   else
12094     ++input_line_pointer;
12095   SKIP_WHITESPACE ();
12096   if (*input_line_pointer == '$')
12097     {
12098       mips_cpreturn_register = tc_get_register (0);
12099       mips_cpreturn_offset = -1;
12100     }
12101   else
12102     {
12103       mips_cpreturn_offset = get_absolute_expression ();
12104       mips_cpreturn_register = -1;
12105     }
12106   SKIP_WHITESPACE ();
12107   if (*input_line_pointer != ',')
12108     {
12109       as_bad (_("missing argument separator ',' for .cpsetup"));
12110       return;
12111     }
12112   else
12113     ++input_line_pointer;
12114   SKIP_WHITESPACE ();
12115   expression (&ex_sym);
12116
12117   if (mips_cpreturn_register == -1)
12118     {
12119       ex_off.X_op = O_constant;
12120       ex_off.X_add_symbol = NULL;
12121       ex_off.X_op_symbol = NULL;
12122       ex_off.X_add_number = mips_cpreturn_offset;
12123
12124       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12125                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12126     }
12127   else
12128     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12129                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12130
12131   /* Ensure there's room for the next two instructions, so that `f'
12132      doesn't end up with an address in the wrong frag.  */
12133   frag_grow (8);
12134   f = frag_more (0);
12135   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12136                (int) BFD_RELOC_GPREL16);
12137   fix_new (frag_now, f - frag_now->fr_literal,
12138            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12139   fix_new (frag_now, f - frag_now->fr_literal,
12140            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12141
12142   f = frag_more (0);
12143   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12144                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12145   fix_new (frag_now, f - frag_now->fr_literal,
12146            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12147   fix_new (frag_now, f - frag_now->fr_literal,
12148            0, NULL, 0, 0, BFD_RELOC_LO16);
12149
12150   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12151                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12152                mips_gp_register, mips_gp_register, reg1);
12153
12154   demand_empty_rest_of_line ();
12155 }
12156
12157 static void
12158 s_cplocal (ignore)
12159      int ignore ATTRIBUTE_UNUSED;
12160 {
12161   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12162    .cplocal is ignored.  */
12163   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12164     {
12165       s_ignore (0);
12166       return;
12167     }
12168
12169   mips_gp_register = tc_get_register (0);
12170   demand_empty_rest_of_line ();
12171 }
12172
12173 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12174    offset from $sp.  The offset is remembered, and after making a PIC
12175    call $gp is restored from that location.  */
12176
12177 static void
12178 s_cprestore (ignore)
12179      int ignore ATTRIBUTE_UNUSED;
12180 {
12181   expressionS ex;
12182   int icnt = 0;
12183
12184   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12185      .cprestore is ignored.  */
12186   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12187     {
12188       s_ignore (0);
12189       return;
12190     }
12191
12192   mips_cprestore_offset = get_absolute_expression ();
12193   mips_cprestore_valid = 1;
12194
12195   ex.X_op = O_constant;
12196   ex.X_add_symbol = NULL;
12197   ex.X_op_symbol = NULL;
12198   ex.X_add_number = mips_cprestore_offset;
12199
12200   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12201                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12202                                 mips_gp_register, SP);
12203
12204   demand_empty_rest_of_line ();
12205 }
12206
12207 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12208    was given in the preceeding .gpsetup, it results in:
12209      ld         $gp, offset($sp)
12210
12211    If a register $reg2 was given there, it results in:
12212      daddiu     $gp, $gp, $reg2
12213  */
12214 static void
12215 s_cpreturn (ignore)
12216      int ignore ATTRIBUTE_UNUSED;
12217 {
12218   expressionS ex;
12219   int icnt = 0;
12220
12221   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12222      We also need NewABI support.  */
12223   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12224     {
12225       s_ignore (0);
12226       return;
12227     }
12228
12229   if (mips_cpreturn_register == -1)
12230     {
12231       ex.X_op = O_constant;
12232       ex.X_add_symbol = NULL;
12233       ex.X_op_symbol = NULL;
12234       ex.X_add_number = mips_cpreturn_offset;
12235
12236       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12237                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12238     }
12239   else
12240     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12241                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12242
12243   demand_empty_rest_of_line ();
12244 }
12245
12246 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12247    code.  It sets the offset to use in gp_rel relocations.  */
12248
12249 static void
12250 s_gpvalue (ignore)
12251      int ignore ATTRIBUTE_UNUSED;
12252 {
12253   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12254      We also need NewABI support.  */
12255   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12256     {
12257       s_ignore (0);
12258       return;
12259     }
12260
12261   mips_gprel_offset = get_absolute_expression ();
12262
12263   demand_empty_rest_of_line ();
12264 }
12265
12266 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12267    code.  It generates a 32 bit GP relative reloc.  */
12268
12269 static void
12270 s_gpword (ignore)
12271      int ignore ATTRIBUTE_UNUSED;
12272 {
12273   symbolS *label;
12274   expressionS ex;
12275   char *p;
12276
12277   /* When not generating PIC code, this is treated as .word.  */
12278   if (mips_pic != SVR4_PIC)
12279     {
12280       s_cons (2);
12281       return;
12282     }
12283
12284   label = insn_labels != NULL ? insn_labels->label : NULL;
12285   mips_emit_delays (true);
12286   if (auto_align)
12287     mips_align (2, 0, label);
12288   mips_clear_insn_labels ();
12289
12290   expression (&ex);
12291
12292   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12293     {
12294       as_bad (_("Unsupported use of .gpword"));
12295       ignore_rest_of_line ();
12296     }
12297
12298   p = frag_more (4);
12299   md_number_to_chars (p, (valueT) 0, 4);
12300   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12301                BFD_RELOC_GPREL32);
12302
12303   demand_empty_rest_of_line ();
12304 }
12305
12306 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12307    tables in SVR4 PIC code.  */
12308
12309 static void
12310 s_cpadd (ignore)
12311      int ignore ATTRIBUTE_UNUSED;
12312 {
12313   int icnt = 0;
12314   int reg;
12315
12316   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12317      code.  */
12318   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12319     {
12320       s_ignore (0);
12321       return;
12322     }
12323
12324   /* Add $gp to the register named as an argument.  */
12325   reg = tc_get_register (0);
12326   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12327                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12328                "d,v,t", reg, reg, mips_gp_register);
12329
12330   demand_empty_rest_of_line ();
12331 }
12332
12333 /* Handle the .insn pseudo-op.  This marks instruction labels in
12334    mips16 mode.  This permits the linker to handle them specially,
12335    such as generating jalx instructions when needed.  We also make
12336    them odd for the duration of the assembly, in order to generate the
12337    right sort of code.  We will make them even in the adjust_symtab
12338    routine, while leaving them marked.  This is convenient for the
12339    debugger and the disassembler.  The linker knows to make them odd
12340    again.  */
12341
12342 static void
12343 s_insn (ignore)
12344      int ignore ATTRIBUTE_UNUSED;
12345 {
12346   mips16_mark_labels ();
12347
12348   demand_empty_rest_of_line ();
12349 }
12350
12351 /* Handle a .stabn directive.  We need these in order to mark a label
12352    as being a mips16 text label correctly.  Sometimes the compiler
12353    will emit a label, followed by a .stabn, and then switch sections.
12354    If the label and .stabn are in mips16 mode, then the label is
12355    really a mips16 text label.  */
12356
12357 static void
12358 s_mips_stab (type)
12359      int type;
12360 {
12361   if (type == 'n')
12362     mips16_mark_labels ();
12363
12364   s_stab (type);
12365 }
12366
12367 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12368  */
12369
12370 static void
12371 s_mips_weakext (ignore)
12372      int ignore ATTRIBUTE_UNUSED;
12373 {
12374   char *name;
12375   int c;
12376   symbolS *symbolP;
12377   expressionS exp;
12378
12379   name = input_line_pointer;
12380   c = get_symbol_end ();
12381   symbolP = symbol_find_or_make (name);
12382   S_SET_WEAK (symbolP);
12383   *input_line_pointer = c;
12384
12385   SKIP_WHITESPACE ();
12386
12387   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12388     {
12389       if (S_IS_DEFINED (symbolP))
12390         {
12391           as_bad ("ignoring attempt to redefine symbol %s",
12392                   S_GET_NAME (symbolP));
12393           ignore_rest_of_line ();
12394           return;
12395         }
12396
12397       if (*input_line_pointer == ',')
12398         {
12399           ++input_line_pointer;
12400           SKIP_WHITESPACE ();
12401         }
12402
12403       expression (&exp);
12404       if (exp.X_op != O_symbol)
12405         {
12406           as_bad ("bad .weakext directive");
12407           ignore_rest_of_line ();
12408           return;
12409         }
12410       symbol_set_value_expression (symbolP, &exp);
12411     }
12412
12413   demand_empty_rest_of_line ();
12414 }
12415
12416 /* Parse a register string into a number.  Called from the ECOFF code
12417    to parse .frame.  The argument is non-zero if this is the frame
12418    register, so that we can record it in mips_frame_reg.  */
12419
12420 int
12421 tc_get_register (frame)
12422      int frame;
12423 {
12424   int reg;
12425
12426   SKIP_WHITESPACE ();
12427   if (*input_line_pointer++ != '$')
12428     {
12429       as_warn (_("expected `$'"));
12430       reg = ZERO;
12431     }
12432   else if (ISDIGIT (*input_line_pointer))
12433     {
12434       reg = get_absolute_expression ();
12435       if (reg < 0 || reg >= 32)
12436         {
12437           as_warn (_("Bad register number"));
12438           reg = ZERO;
12439         }
12440     }
12441   else
12442     {
12443       if (strncmp (input_line_pointer, "ra", 2) == 0)
12444         {
12445           reg = RA;
12446           input_line_pointer += 2;
12447         }
12448       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12449         {
12450           reg = FP;
12451           input_line_pointer += 2;
12452         }
12453       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12454         {
12455           reg = SP;
12456           input_line_pointer += 2;
12457         }
12458       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12459         {
12460           reg = GP;
12461           input_line_pointer += 2;
12462         }
12463       else if (strncmp (input_line_pointer, "at", 2) == 0)
12464         {
12465           reg = AT;
12466           input_line_pointer += 2;
12467         }
12468       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12469         {
12470           reg = KT0;
12471           input_line_pointer += 3;
12472         }
12473       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12474         {
12475           reg = KT1;
12476           input_line_pointer += 3;
12477         }
12478       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12479         {
12480           reg = ZERO;
12481           input_line_pointer += 4;
12482         }
12483       else
12484         {
12485           as_warn (_("Unrecognized register name"));
12486           reg = ZERO;
12487           while (ISALNUM(*input_line_pointer))
12488            input_line_pointer++;
12489         }
12490     }
12491   if (frame)
12492     {
12493       mips_frame_reg = reg != 0 ? reg : SP;
12494       mips_frame_reg_valid = 1;
12495       mips_cprestore_valid = 0;
12496     }
12497   return reg;
12498 }
12499
12500 valueT
12501 md_section_align (seg, addr)
12502      asection *seg;
12503      valueT addr;
12504 {
12505   int align = bfd_get_section_alignment (stdoutput, seg);
12506
12507 #ifdef OBJ_ELF
12508   /* We don't need to align ELF sections to the full alignment.
12509      However, Irix 5 may prefer that we align them at least to a 16
12510      byte boundary.  We don't bother to align the sections if we are
12511      targeted for an embedded system.  */
12512   if (strcmp (TARGET_OS, "elf") == 0)
12513     return addr;
12514   if (align > 4)
12515     align = 4;
12516 #endif
12517
12518   return ((addr + (1 << align) - 1) & (-1 << align));
12519 }
12520
12521 /* Utility routine, called from above as well.  If called while the
12522    input file is still being read, it's only an approximation.  (For
12523    example, a symbol may later become defined which appeared to be
12524    undefined earlier.)  */
12525
12526 static int
12527 nopic_need_relax (sym, before_relaxing)
12528      symbolS *sym;
12529      int before_relaxing;
12530 {
12531   if (sym == 0)
12532     return 0;
12533
12534   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12535     {
12536       const char *symname;
12537       int change;
12538
12539       /* Find out whether this symbol can be referenced off the $gp
12540          register.  It can be if it is smaller than the -G size or if
12541          it is in the .sdata or .sbss section.  Certain symbols can
12542          not be referenced off the $gp, although it appears as though
12543          they can.  */
12544       symname = S_GET_NAME (sym);
12545       if (symname != (const char *) NULL
12546           && (strcmp (symname, "eprol") == 0
12547               || strcmp (symname, "etext") == 0
12548               || strcmp (symname, "_gp") == 0
12549               || strcmp (symname, "edata") == 0
12550               || strcmp (symname, "_fbss") == 0
12551               || strcmp (symname, "_fdata") == 0
12552               || strcmp (symname, "_ftext") == 0
12553               || strcmp (symname, "end") == 0
12554               || strcmp (symname, "_gp_disp") == 0))
12555         change = 1;
12556       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12557                && (0
12558 #ifndef NO_ECOFF_DEBUGGING
12559                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12560                        && (symbol_get_obj (sym)->ecoff_extern_size
12561                            <= g_switch_value))
12562 #endif
12563                    /* We must defer this decision until after the whole
12564                       file has been read, since there might be a .extern
12565                       after the first use of this symbol.  */
12566                    || (before_relaxing
12567 #ifndef NO_ECOFF_DEBUGGING
12568                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12569 #endif
12570                        && S_GET_VALUE (sym) == 0)
12571                    || (S_GET_VALUE (sym) != 0
12572                        && S_GET_VALUE (sym) <= g_switch_value)))
12573         change = 0;
12574       else
12575         {
12576           const char *segname;
12577
12578           segname = segment_name (S_GET_SEGMENT (sym));
12579           assert (strcmp (segname, ".lit8") != 0
12580                   && strcmp (segname, ".lit4") != 0);
12581           change = (strcmp (segname, ".sdata") != 0
12582                     && strcmp (segname, ".sbss") != 0
12583                     && strncmp (segname, ".sdata.", 7) != 0
12584                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12585         }
12586       return change;
12587     }
12588   else
12589     /* We are not optimizing for the $gp register.  */
12590     return 1;
12591 }
12592
12593 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12594    extended opcode.  SEC is the section the frag is in.  */
12595
12596 static int
12597 mips16_extended_frag (fragp, sec, stretch)
12598      fragS *fragp;
12599      asection *sec;
12600      long stretch;
12601 {
12602   int type;
12603   register const struct mips16_immed_operand *op;
12604   offsetT val;
12605   int mintiny, maxtiny;
12606   segT symsec;
12607   fragS *sym_frag;
12608
12609   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12610     return 0;
12611   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12612     return 1;
12613
12614   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12615   op = mips16_immed_operands;
12616   while (op->type != type)
12617     {
12618       ++op;
12619       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12620     }
12621
12622   if (op->unsp)
12623     {
12624       if (type == '<' || type == '>' || type == '[' || type == ']')
12625         {
12626           mintiny = 1;
12627           maxtiny = 1 << op->nbits;
12628         }
12629       else
12630         {
12631           mintiny = 0;
12632           maxtiny = (1 << op->nbits) - 1;
12633         }
12634     }
12635   else
12636     {
12637       mintiny = - (1 << (op->nbits - 1));
12638       maxtiny = (1 << (op->nbits - 1)) - 1;
12639     }
12640
12641   sym_frag = symbol_get_frag (fragp->fr_symbol);
12642   val = S_GET_VALUE (fragp->fr_symbol);
12643   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12644
12645   if (op->pcrel)
12646     {
12647       addressT addr;
12648
12649       /* We won't have the section when we are called from
12650          mips_relax_frag.  However, we will always have been called
12651          from md_estimate_size_before_relax first.  If this is a
12652          branch to a different section, we mark it as such.  If SEC is
12653          NULL, and the frag is not marked, then it must be a branch to
12654          the same section.  */
12655       if (sec == NULL)
12656         {
12657           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12658             return 1;
12659         }
12660       else
12661         {
12662           /* Must have been called from md_estimate_size_before_relax.  */
12663           if (symsec != sec)
12664             {
12665               fragp->fr_subtype =
12666                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12667
12668               /* FIXME: We should support this, and let the linker
12669                  catch branches and loads that are out of range.  */
12670               as_bad_where (fragp->fr_file, fragp->fr_line,
12671                             _("unsupported PC relative reference to different section"));
12672
12673               return 1;
12674             }
12675           if (fragp != sym_frag && sym_frag->fr_address == 0)
12676             /* Assume non-extended on the first relaxation pass.
12677                The address we have calculated will be bogus if this is
12678                a forward branch to another frag, as the forward frag
12679                will have fr_address == 0.  */
12680             return 0;
12681         }
12682
12683       /* In this case, we know for sure that the symbol fragment is in
12684          the same section.  If the relax_marker of the symbol fragment
12685          differs from the relax_marker of this fragment, we have not
12686          yet adjusted the symbol fragment fr_address.  We want to add
12687          in STRETCH in order to get a better estimate of the address.
12688          This particularly matters because of the shift bits.  */
12689       if (stretch != 0
12690           && sym_frag->relax_marker != fragp->relax_marker)
12691         {
12692           fragS *f;
12693
12694           /* Adjust stretch for any alignment frag.  Note that if have
12695              been expanding the earlier code, the symbol may be
12696              defined in what appears to be an earlier frag.  FIXME:
12697              This doesn't handle the fr_subtype field, which specifies
12698              a maximum number of bytes to skip when doing an
12699              alignment.  */
12700           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12701             {
12702               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12703                 {
12704                   if (stretch < 0)
12705                     stretch = - ((- stretch)
12706                                  & ~ ((1 << (int) f->fr_offset) - 1));
12707                   else
12708                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12709                   if (stretch == 0)
12710                     break;
12711                 }
12712             }
12713           if (f != NULL)
12714             val += stretch;
12715         }
12716
12717       addr = fragp->fr_address + fragp->fr_fix;
12718
12719       /* The base address rules are complicated.  The base address of
12720          a branch is the following instruction.  The base address of a
12721          PC relative load or add is the instruction itself, but if it
12722          is in a delay slot (in which case it can not be extended) use
12723          the address of the instruction whose delay slot it is in.  */
12724       if (type == 'p' || type == 'q')
12725         {
12726           addr += 2;
12727
12728           /* If we are currently assuming that this frag should be
12729              extended, then, the current address is two bytes
12730              higher.  */
12731           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12732             addr += 2;
12733
12734           /* Ignore the low bit in the target, since it will be set
12735              for a text label.  */
12736           if ((val & 1) != 0)
12737             --val;
12738         }
12739       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12740         addr -= 4;
12741       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12742         addr -= 2;
12743
12744       val -= addr & ~ ((1 << op->shift) - 1);
12745
12746       /* Branch offsets have an implicit 0 in the lowest bit.  */
12747       if (type == 'p' || type == 'q')
12748         val /= 2;
12749
12750       /* If any of the shifted bits are set, we must use an extended
12751          opcode.  If the address depends on the size of this
12752          instruction, this can lead to a loop, so we arrange to always
12753          use an extended opcode.  We only check this when we are in
12754          the main relaxation loop, when SEC is NULL.  */
12755       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12756         {
12757           fragp->fr_subtype =
12758             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12759           return 1;
12760         }
12761
12762       /* If we are about to mark a frag as extended because the value
12763          is precisely maxtiny + 1, then there is a chance of an
12764          infinite loop as in the following code:
12765              la $4,foo
12766              .skip      1020
12767              .align     2
12768            foo:
12769          In this case when the la is extended, foo is 0x3fc bytes
12770          away, so the la can be shrunk, but then foo is 0x400 away, so
12771          the la must be extended.  To avoid this loop, we mark the
12772          frag as extended if it was small, and is about to become
12773          extended with a value of maxtiny + 1.  */
12774       if (val == ((maxtiny + 1) << op->shift)
12775           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12776           && sec == NULL)
12777         {
12778           fragp->fr_subtype =
12779             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12780           return 1;
12781         }
12782     }
12783   else if (symsec != absolute_section && sec != NULL)
12784     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12785
12786   if ((val & ((1 << op->shift) - 1)) != 0
12787       || val < (mintiny << op->shift)
12788       || val > (maxtiny << op->shift))
12789     return 1;
12790   else
12791     return 0;
12792 }
12793
12794 /* Compute the length of a branch sequence, and adjust the
12795    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
12796    worst-case length is computed, with UPDATE being used to indicate
12797    whether an unconditional (-1), branch-likely (+1) or regular (0)
12798    branch is to be computed.  */
12799 static int
12800 relaxed_branch_length (fragp, sec, update)
12801      fragS *fragp;
12802      asection *sec;
12803      int update;
12804 {
12805   boolean toofar;
12806   int length;
12807
12808   if (fragp
12809       && S_IS_DEFINED (fragp->fr_symbol)
12810       && sec == S_GET_SEGMENT (fragp->fr_symbol))
12811     {
12812       addressT addr;
12813       offsetT val;
12814
12815       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12816
12817       addr = fragp->fr_address + fragp->fr_fix + 4;
12818
12819       val -= addr;
12820
12821       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12822     }
12823   else if (fragp)
12824     /* If the symbol is not defined or it's in a different segment,
12825        assume the user knows what's going on and emit a short
12826        branch.  */
12827     toofar = false;
12828   else
12829     toofar = true;
12830
12831   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12832     fragp->fr_subtype
12833       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype),
12834                              RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12835                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12836                              RELAX_BRANCH_LINK (fragp->fr_subtype),
12837                              toofar);
12838
12839   length = 4;
12840   if (toofar)
12841     {
12842       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12843         length += 8;
12844
12845       if (mips_pic != NO_PIC)
12846         {
12847           /* Additional space for PIC loading of target address.  */
12848           length += 8;
12849           if (mips_opts.isa == ISA_MIPS1)
12850             /* Additional space for $at-stabilizing nop.  */
12851             length += 4;
12852         }
12853
12854       /* If branch is conditional.  */
12855       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12856         length += 8;
12857     }
12858   
12859   return length;
12860 }
12861
12862 /* Estimate the size of a frag before relaxing.  Unless this is the
12863    mips16, we are not really relaxing here, and the final size is
12864    encoded in the subtype information.  For the mips16, we have to
12865    decide whether we are using an extended opcode or not.  */
12866
12867 int
12868 md_estimate_size_before_relax (fragp, segtype)
12869      fragS *fragp;
12870      asection *segtype;
12871 {
12872   int change = 0;
12873   boolean linkonce = false;
12874
12875   if (RELAX_BRANCH_P (fragp->fr_subtype))
12876     {
12877
12878       fragp->fr_var = relaxed_branch_length (fragp, segtype, false);
12879       
12880       return fragp->fr_var;
12881     }
12882
12883   if (RELAX_MIPS16_P (fragp->fr_subtype))
12884     /* We don't want to modify the EXTENDED bit here; it might get us
12885        into infinite loops.  We change it only in mips_relax_frag().  */
12886     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12887
12888   if (mips_pic == NO_PIC)
12889     {
12890       change = nopic_need_relax (fragp->fr_symbol, 0);
12891     }
12892   else if (mips_pic == SVR4_PIC)
12893     {
12894       symbolS *sym;
12895       asection *symsec;
12896
12897       sym = fragp->fr_symbol;
12898
12899       /* Handle the case of a symbol equated to another symbol.  */
12900       while (symbol_equated_reloc_p (sym))
12901         {
12902           symbolS *n;
12903
12904           /* It's possible to get a loop here in a badly written
12905              program.  */
12906           n = symbol_get_value_expression (sym)->X_add_symbol;
12907           if (n == sym)
12908             break;
12909           sym = n;
12910         }
12911
12912       symsec = S_GET_SEGMENT (sym);
12913
12914       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12915       if (symsec != segtype && ! S_IS_LOCAL (sym))
12916         {
12917           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12918               != 0)
12919             linkonce = true;
12920
12921           /* The GNU toolchain uses an extension for ELF: a section
12922              beginning with the magic string .gnu.linkonce is a linkonce
12923              section.  */
12924           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12925                        sizeof ".gnu.linkonce" - 1) == 0)
12926             linkonce = true;
12927         }
12928
12929       /* This must duplicate the test in adjust_reloc_syms.  */
12930       change = (symsec != &bfd_und_section
12931                 && symsec != &bfd_abs_section
12932                 && ! bfd_is_com_section (symsec)
12933                 && !linkonce
12934 #ifdef OBJ_ELF
12935                 /* A global or weak symbol is treated as external.  */
12936                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12937                     || (! S_IS_WEAK (sym)
12938                         && (! S_IS_EXTERNAL (sym)
12939                             || mips_pic == EMBEDDED_PIC)))
12940 #endif
12941                 );
12942     }
12943   else
12944     abort ();
12945
12946   if (change)
12947     {
12948       /* Record the offset to the first reloc in the fr_opcode field.
12949          This lets md_convert_frag and tc_gen_reloc know that the code
12950          must be expanded.  */
12951       fragp->fr_opcode = (fragp->fr_literal
12952                           + fragp->fr_fix
12953                           - RELAX_OLD (fragp->fr_subtype)
12954                           + RELAX_RELOC1 (fragp->fr_subtype));
12955       /* FIXME: This really needs as_warn_where.  */
12956       if (RELAX_WARN (fragp->fr_subtype))
12957         as_warn (_("AT used after \".set noat\" or macro used after "
12958                    "\".set nomacro\""));
12959
12960       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12961     }
12962
12963   return 0;
12964 }
12965
12966 /* This is called to see whether a reloc against a defined symbol
12967    should be converted into a reloc against a section.  Don't adjust
12968    MIPS16 jump relocations, so we don't have to worry about the format
12969    of the offset in the .o file.  Don't adjust relocations against
12970    mips16 symbols, so that the linker can find them if it needs to set
12971    up a stub.  */
12972
12973 int
12974 mips_fix_adjustable (fixp)
12975      fixS *fixp;
12976 {
12977   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12978     return 0;
12979
12980   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12981       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12982     return 0;
12983
12984   if (fixp->fx_addsy == NULL)
12985     return 1;
12986
12987 #ifdef OBJ_ELF
12988   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12989       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12990       && fixp->fx_subsy == NULL)
12991     return 0;
12992 #endif
12993
12994   return 1;
12995 }
12996
12997 /* Translate internal representation of relocation info to BFD target
12998    format.  */
12999
13000 arelent **
13001 tc_gen_reloc (section, fixp)
13002      asection *section ATTRIBUTE_UNUSED;
13003      fixS *fixp;
13004 {
13005   static arelent *retval[4];
13006   arelent *reloc;
13007   bfd_reloc_code_real_type code;
13008
13009   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13010   retval[1] = NULL;
13011
13012   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13013   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13014   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13015
13016   if (mips_pic == EMBEDDED_PIC
13017       && SWITCH_TABLE (fixp))
13018     {
13019       /* For a switch table entry we use a special reloc.  The addend
13020          is actually the difference between the reloc address and the
13021          subtrahend.  */
13022       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13023       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13024         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13025       fixp->fx_r_type = BFD_RELOC_GPREL32;
13026     }
13027   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13028     {
13029       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13030         reloc->addend = fixp->fx_addnumber;
13031       else
13032         {
13033           /* We use a special addend for an internal RELLO reloc.  */
13034           if (symbol_section_p (fixp->fx_addsy))
13035             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13036           else
13037             reloc->addend = fixp->fx_addnumber + reloc->address;
13038         }
13039     }
13040   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13041     {
13042       assert (fixp->fx_next != NULL
13043               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13044
13045       /* The reloc is relative to the RELLO; adjust the addend
13046          accordingly.  */
13047       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13048         reloc->addend = fixp->fx_next->fx_addnumber;
13049       else
13050         {
13051           /* We use a special addend for an internal RELHI reloc.  */
13052           if (symbol_section_p (fixp->fx_addsy))
13053             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13054                              + fixp->fx_next->fx_where
13055                              - S_GET_VALUE (fixp->fx_subsy));
13056           else
13057             reloc->addend = (fixp->fx_addnumber
13058                              + fixp->fx_next->fx_frag->fr_address
13059                              + fixp->fx_next->fx_where);
13060         }
13061     }
13062   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13063     reloc->addend = fixp->fx_addnumber;
13064   else
13065     {
13066       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13067         /* A gruesome hack which is a result of the gruesome gas reloc
13068            handling.  */
13069         reloc->addend = reloc->address;
13070       else
13071         reloc->addend = -reloc->address;
13072     }
13073
13074   /* If this is a variant frag, we may need to adjust the existing
13075      reloc and generate a new one.  */
13076   if (fixp->fx_frag->fr_opcode != NULL
13077       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13078            && ! HAVE_NEWABI)
13079           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13080           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13081           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13082           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13083           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13084           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13085     )
13086     {
13087       arelent *reloc2;
13088
13089       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13090
13091       /* If this is not the last reloc in this frag, then we have two
13092          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13093          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13094          the second one handle all of them.  */
13095       if (fixp->fx_next != NULL
13096           && fixp->fx_frag == fixp->fx_next->fx_frag)
13097         {
13098           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13099                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13100                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13101                       && (fixp->fx_next->fx_r_type
13102                           == BFD_RELOC_MIPS_GOT_LO16))
13103                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13104                       && (fixp->fx_next->fx_r_type
13105                           == BFD_RELOC_MIPS_CALL_LO16)));
13106           retval[0] = NULL;
13107           return retval;
13108         }
13109
13110       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13111       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13112       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13113       retval[2] = NULL;
13114       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13115       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13116       reloc2->address = (reloc->address
13117                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13118                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13119       reloc2->addend = fixp->fx_addnumber;
13120       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13121       assert (reloc2->howto != NULL);
13122
13123       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13124         {
13125           arelent *reloc3;
13126
13127           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13128           retval[3] = NULL;
13129           *reloc3 = *reloc2;
13130           reloc3->address += 4;
13131         }
13132
13133       if (mips_pic == NO_PIC)
13134         {
13135           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13136           fixp->fx_r_type = BFD_RELOC_HI16_S;
13137         }
13138       else if (mips_pic == SVR4_PIC)
13139         {
13140           switch (fixp->fx_r_type)
13141             {
13142             default:
13143               abort ();
13144             case BFD_RELOC_MIPS_GOT16:
13145               break;
13146             case BFD_RELOC_MIPS_GOT_LO16:
13147             case BFD_RELOC_MIPS_CALL_LO16:
13148               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13149               break;
13150             case BFD_RELOC_MIPS_CALL16:
13151               if (HAVE_NEWABI)
13152                 {
13153                   /* BFD_RELOC_MIPS_GOT16;*/
13154                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13155                   reloc2->howto = bfd_reloc_type_lookup
13156                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13157                 }
13158               else
13159                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13160               break;
13161             }
13162         }
13163       else
13164         abort ();
13165
13166       /* newabi uses R_MIPS_GOT_DISP for local symbols */
13167       if (HAVE_NEWABI && BFD_RELOC_MIPS_GOT_LO16)
13168         {
13169           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13170           retval[1] = NULL;
13171         }
13172     }
13173
13174   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13175      entry to be used in the relocation's section offset.  */
13176   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13177     {
13178       reloc->address = reloc->addend;
13179       reloc->addend = 0;
13180     }
13181
13182   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13183      fixup_segment converted a non-PC relative reloc into a PC
13184      relative reloc.  In such a case, we need to convert the reloc
13185      code.  */
13186   code = fixp->fx_r_type;
13187   if (fixp->fx_pcrel)
13188     {
13189       switch (code)
13190         {
13191         case BFD_RELOC_8:
13192           code = BFD_RELOC_8_PCREL;
13193           break;
13194         case BFD_RELOC_16:
13195           code = BFD_RELOC_16_PCREL;
13196           break;
13197         case BFD_RELOC_32:
13198           code = BFD_RELOC_32_PCREL;
13199           break;
13200         case BFD_RELOC_64:
13201           code = BFD_RELOC_64_PCREL;
13202           break;
13203         case BFD_RELOC_8_PCREL:
13204         case BFD_RELOC_16_PCREL:
13205         case BFD_RELOC_32_PCREL:
13206         case BFD_RELOC_64_PCREL:
13207         case BFD_RELOC_16_PCREL_S2:
13208         case BFD_RELOC_PCREL_HI16_S:
13209         case BFD_RELOC_PCREL_LO16:
13210           break;
13211         default:
13212           as_bad_where (fixp->fx_file, fixp->fx_line,
13213                         _("Cannot make %s relocation PC relative"),
13214                         bfd_get_reloc_code_name (code));
13215         }
13216     }
13217
13218 #ifdef OBJ_ELF
13219   /* md_apply_fix3 has a double-subtraction hack to get
13220      bfd_install_relocation to behave nicely.  GPREL relocations are
13221      handled correctly without this hack, so undo it here.  We can't
13222      stop md_apply_fix3 from subtracting twice in the first place since
13223      the fake addend is required for variant frags above.  */
13224   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13225       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13226       && reloc->addend != 0
13227       && mips_need_elf_addend_fixup (fixp))
13228     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13229 #endif
13230
13231   /* To support a PC relative reloc when generating embedded PIC code
13232      for ECOFF, we use a Cygnus extension.  We check for that here to
13233      make sure that we don't let such a reloc escape normally.  */
13234   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13235        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13236       && code == BFD_RELOC_16_PCREL_S2
13237       && mips_pic != EMBEDDED_PIC)
13238     reloc->howto = NULL;
13239   else
13240     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13241
13242   if (reloc->howto == NULL)
13243     {
13244       as_bad_where (fixp->fx_file, fixp->fx_line,
13245                     _("Can not represent %s relocation in this object file format"),
13246                     bfd_get_reloc_code_name (code));
13247       retval[0] = NULL;
13248     }
13249
13250   return retval;
13251 }
13252
13253 /* Relax a machine dependent frag.  This returns the amount by which
13254    the current size of the frag should change.  */
13255
13256 int
13257 mips_relax_frag (sec, fragp, stretch)
13258      asection *sec;
13259      fragS *fragp;
13260      long stretch;
13261 {
13262   if (RELAX_BRANCH_P (fragp->fr_subtype))
13263     {
13264       offsetT old_var = fragp->fr_var;
13265       
13266       fragp->fr_var = relaxed_branch_length (fragp, sec, true);
13267
13268       return fragp->fr_var - old_var;
13269     }
13270
13271   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13272     return 0;
13273
13274   if (mips16_extended_frag (fragp, NULL, stretch))
13275     {
13276       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13277         return 0;
13278       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13279       return 2;
13280     }
13281   else
13282     {
13283       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13284         return 0;
13285       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13286       return -2;
13287     }
13288
13289   return 0;
13290 }
13291
13292 /* Convert a machine dependent frag.  */
13293
13294 void
13295 md_convert_frag (abfd, asec, fragp)
13296      bfd *abfd ATTRIBUTE_UNUSED;
13297      segT asec;
13298      fragS *fragp;
13299 {
13300   int old, new;
13301   char *fixptr;
13302
13303   if (RELAX_BRANCH_P (fragp->fr_subtype))
13304     {
13305       bfd_byte *buf;
13306       unsigned long insn;
13307       expressionS exp;
13308       fixS *fixp;
13309       
13310       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13311
13312       if (target_big_endian)
13313         insn = bfd_getb32 (buf);
13314       else
13315         insn = bfd_getl32 (buf);
13316           
13317       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13318         {
13319           /* We generate a fixup instead of applying it right now
13320              because, if there are linker relaxations, we're going to
13321              need the relocations.  */
13322           exp.X_op = O_symbol;
13323           exp.X_add_symbol = fragp->fr_symbol;
13324           exp.X_add_number = fragp->fr_offset;
13325
13326           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13327                               4, &exp, 1,
13328                               RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype)
13329                               ? BFD_RELOC_16_PCREL_S2
13330                               : BFD_RELOC_16_PCREL);
13331           fixp->fx_file = fragp->fr_file;
13332           fixp->fx_line = fragp->fr_line;
13333           
13334           md_number_to_chars ((char *)buf, insn, 4);
13335           buf += 4;
13336         }
13337       else
13338         {
13339           int i;
13340
13341           as_warn_where (fragp->fr_file, fragp->fr_line,
13342                          _("relaxed out-of-range branch into a jump"));
13343
13344           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13345             goto uncond;
13346
13347           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13348             {
13349               /* Reverse the branch.  */
13350               switch ((insn >> 28) & 0xf)
13351                 {
13352                 case 4:
13353                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13354                      have the condition reversed by tweaking a single
13355                      bit, and their opcodes all have 0x4???????.  */
13356                   assert ((insn & 0xf1000000) == 0x41000000);
13357                   insn ^= 0x00010000;
13358                   break;
13359
13360                 case 0:
13361                   /* bltz       0x04000000      bgez    0x04010000
13362                      bltzal     0x04100000      bgezal  0x04110000 */
13363                   assert ((insn & 0xfc0e0000) == 0x04000000);
13364                   insn ^= 0x00010000;
13365                   break;
13366                   
13367                 case 1:
13368                   /* beq        0x10000000      bne     0x14000000
13369                      blez       0x18000000      bgtz    0x1c000000 */
13370                   insn ^= 0x04000000;
13371                   break;
13372
13373                 default:
13374                   abort ();
13375                 }
13376             }
13377
13378           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13379             {
13380               /* Clear the and-link bit.  */
13381               assert ((insn & 0xfc1c0000) == 0x04100000);
13382
13383               /* bltzal 0x04100000      bgezal  0x04110000
13384                 bltzall 0x04120000     bgezall  0x04130000 */
13385               insn &= ~0x00100000;
13386             }
13387
13388           /* Branch over the branch (if the branch was likely) or the
13389              full jump (not likely case).  Compute the offset from the
13390              current instruction to branch to.  */
13391           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13392             i = 16;
13393           else
13394             {
13395               /* How many bytes in instructions we've already emitted?  */
13396               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13397               /* How many bytes in instructions from here to the end?  */
13398               i = fragp->fr_var - i;
13399             }
13400           /* Convert to instruction count.  */
13401           i >>= 2;
13402           /* Branch counts from the next instruction.  */
13403           i--; 
13404           insn |= i;
13405           /* Branch over the jump.  */
13406           md_number_to_chars ((char *)buf, insn, 4);
13407           buf += 4;
13408
13409           /* Nop */
13410           md_number_to_chars ((char*)buf, 0, 4);
13411           buf += 4;
13412
13413           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13414             {
13415               /* beql $0, $0, 2f */
13416               insn = 0x50000000;
13417               /* Compute the PC offset from the current instruction to
13418                  the end of the variable frag.  */
13419               /* How many bytes in instructions we've already emitted?  */
13420               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13421               /* How many bytes in instructions from here to the end?  */
13422               i = fragp->fr_var - i;
13423               /* Convert to instruction count.  */
13424               i >>= 2;
13425               /* Don't decrement i, because we want to branch over the
13426                  delay slot.  */
13427
13428               insn |= i;
13429               md_number_to_chars ((char *)buf, insn, 4);
13430               buf += 4;
13431
13432               md_number_to_chars ((char *)buf, 0, 4);
13433               buf += 4;
13434             }
13435
13436         uncond:
13437           if (mips_pic == NO_PIC)
13438             {
13439               /* j or jal.  */
13440               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13441                       ? 0x0c000000 : 0x08000000);
13442               exp.X_op = O_symbol;
13443               exp.X_add_symbol = fragp->fr_symbol;
13444               exp.X_add_number = fragp->fr_offset;
13445
13446               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13447                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13448               fixp->fx_file = fragp->fr_file;
13449               fixp->fx_line = fragp->fr_line;
13450
13451               md_number_to_chars ((char*)buf, insn, 4);
13452               buf += 4;
13453             }
13454           else
13455             {
13456               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13457               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13458               exp.X_op = O_symbol;
13459               exp.X_add_symbol = fragp->fr_symbol;
13460               exp.X_add_number = fragp->fr_offset;
13461
13462               if (fragp->fr_offset)
13463                 {
13464                   exp.X_add_symbol = make_expr_symbol (&exp);
13465                   exp.X_add_number = 0;
13466                 }
13467
13468               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13469                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13470               fixp->fx_file = fragp->fr_file;
13471               fixp->fx_line = fragp->fr_line;
13472
13473               md_number_to_chars ((char*)buf, insn, 4);
13474               buf += 4;
13475               
13476               if (mips_opts.isa == ISA_MIPS1)
13477                 {
13478                   /* nop */
13479                   md_number_to_chars ((char*)buf, 0, 4);
13480                   buf += 4;
13481                 }
13482
13483               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13484               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13485
13486               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13487                                   4, &exp, 0, BFD_RELOC_LO16);
13488               fixp->fx_file = fragp->fr_file;
13489               fixp->fx_line = fragp->fr_line;
13490               
13491               md_number_to_chars ((char*)buf, insn, 4);
13492               buf += 4;
13493
13494               /* j(al)r $at.  */
13495               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13496                 insn = 0x0020f809;
13497               else
13498                 insn = 0x00200008;
13499
13500               md_number_to_chars ((char*)buf, insn, 4);
13501               buf += 4;
13502             }
13503         }
13504
13505       assert (buf == (bfd_byte *)fragp->fr_literal
13506               + fragp->fr_fix + fragp->fr_var);
13507
13508       fragp->fr_fix += fragp->fr_var;
13509
13510       return;
13511     }
13512
13513   if (RELAX_MIPS16_P (fragp->fr_subtype))
13514     {
13515       int type;
13516       register const struct mips16_immed_operand *op;
13517       boolean small, ext;
13518       offsetT val;
13519       bfd_byte *buf;
13520       unsigned long insn;
13521       boolean use_extend;
13522       unsigned short extend;
13523
13524       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13525       op = mips16_immed_operands;
13526       while (op->type != type)
13527         ++op;
13528
13529       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13530         {
13531           small = false;
13532           ext = true;
13533         }
13534       else
13535         {
13536           small = true;
13537           ext = false;
13538         }
13539
13540       resolve_symbol_value (fragp->fr_symbol);
13541       val = S_GET_VALUE (fragp->fr_symbol);
13542       if (op->pcrel)
13543         {
13544           addressT addr;
13545
13546           addr = fragp->fr_address + fragp->fr_fix;
13547
13548           /* The rules for the base address of a PC relative reloc are
13549              complicated; see mips16_extended_frag.  */
13550           if (type == 'p' || type == 'q')
13551             {
13552               addr += 2;
13553               if (ext)
13554                 addr += 2;
13555               /* Ignore the low bit in the target, since it will be
13556                  set for a text label.  */
13557               if ((val & 1) != 0)
13558                 --val;
13559             }
13560           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13561             addr -= 4;
13562           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13563             addr -= 2;
13564
13565           addr &= ~ (addressT) ((1 << op->shift) - 1);
13566           val -= addr;
13567
13568           /* Make sure the section winds up with the alignment we have
13569              assumed.  */
13570           if (op->shift > 0)
13571             record_alignment (asec, op->shift);
13572         }
13573
13574       if (ext
13575           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13576               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13577         as_warn_where (fragp->fr_file, fragp->fr_line,
13578                        _("extended instruction in delay slot"));
13579
13580       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13581
13582       if (target_big_endian)
13583         insn = bfd_getb16 (buf);
13584       else
13585         insn = bfd_getl16 (buf);
13586
13587       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13588                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13589                     small, ext, &insn, &use_extend, &extend);
13590
13591       if (use_extend)
13592         {
13593           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13594           fragp->fr_fix += 2;
13595           buf += 2;
13596         }
13597
13598       md_number_to_chars ((char *) buf, insn, 2);
13599       fragp->fr_fix += 2;
13600       buf += 2;
13601     }
13602   else
13603     {
13604       if (fragp->fr_opcode == NULL)
13605         return;
13606
13607       old = RELAX_OLD (fragp->fr_subtype);
13608       new = RELAX_NEW (fragp->fr_subtype);
13609       fixptr = fragp->fr_literal + fragp->fr_fix;
13610
13611       if (new > 0)
13612         memcpy (fixptr - old, fixptr, new);
13613
13614       fragp->fr_fix += new - old;
13615     }
13616 }
13617
13618 #ifdef OBJ_ELF
13619
13620 /* This function is called after the relocs have been generated.
13621    We've been storing mips16 text labels as odd.  Here we convert them
13622    back to even for the convenience of the debugger.  */
13623
13624 void
13625 mips_frob_file_after_relocs ()
13626 {
13627   asymbol **syms;
13628   unsigned int count, i;
13629
13630   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13631     return;
13632
13633   syms = bfd_get_outsymbols (stdoutput);
13634   count = bfd_get_symcount (stdoutput);
13635   for (i = 0; i < count; i++, syms++)
13636     {
13637       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13638           && ((*syms)->value & 1) != 0)
13639         {
13640           (*syms)->value &= ~1;
13641           /* If the symbol has an odd size, it was probably computed
13642              incorrectly, so adjust that as well.  */
13643           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13644             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13645         }
13646     }
13647 }
13648
13649 #endif
13650
13651 /* This function is called whenever a label is defined.  It is used
13652    when handling branch delays; if a branch has a label, we assume we
13653    can not move it.  */
13654
13655 void
13656 mips_define_label (sym)
13657      symbolS *sym;
13658 {
13659   struct insn_label_list *l;
13660
13661   if (free_insn_labels == NULL)
13662     l = (struct insn_label_list *) xmalloc (sizeof *l);
13663   else
13664     {
13665       l = free_insn_labels;
13666       free_insn_labels = l->next;
13667     }
13668
13669   l->label = sym;
13670   l->next = insn_labels;
13671   insn_labels = l;
13672 }
13673 \f
13674 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13675
13676 /* Some special processing for a MIPS ELF file.  */
13677
13678 void
13679 mips_elf_final_processing ()
13680 {
13681   /* Write out the register information.  */
13682   if (mips_abi != N64_ABI)
13683     {
13684       Elf32_RegInfo s;
13685
13686       s.ri_gprmask = mips_gprmask;
13687       s.ri_cprmask[0] = mips_cprmask[0];
13688       s.ri_cprmask[1] = mips_cprmask[1];
13689       s.ri_cprmask[2] = mips_cprmask[2];
13690       s.ri_cprmask[3] = mips_cprmask[3];
13691       /* The gp_value field is set by the MIPS ELF backend.  */
13692
13693       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13694                                        ((Elf32_External_RegInfo *)
13695                                         mips_regmask_frag));
13696     }
13697   else
13698     {
13699       Elf64_Internal_RegInfo s;
13700
13701       s.ri_gprmask = mips_gprmask;
13702       s.ri_pad = 0;
13703       s.ri_cprmask[0] = mips_cprmask[0];
13704       s.ri_cprmask[1] = mips_cprmask[1];
13705       s.ri_cprmask[2] = mips_cprmask[2];
13706       s.ri_cprmask[3] = mips_cprmask[3];
13707       /* The gp_value field is set by the MIPS ELF backend.  */
13708
13709       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13710                                        ((Elf64_External_RegInfo *)
13711                                         mips_regmask_frag));
13712     }
13713
13714   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13715      sort of BFD interface for this.  */
13716   if (mips_any_noreorder)
13717     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13718   if (mips_pic != NO_PIC)
13719     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13720
13721   /* Set MIPS ELF flags for ASEs.  */
13722   if (file_ase_mips16)
13723     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13724 #if 0 /* XXX FIXME */
13725   if (file_ase_mips3d)
13726     elf_elfheader (stdoutput)->e_flags |= ???;
13727 #endif
13728   if (file_ase_mdmx)
13729     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13730
13731   /* Set the MIPS ELF ABI flags.  */
13732   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13733     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13734   else if (mips_abi == O64_ABI)
13735     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13736   else if (mips_abi == EABI_ABI)
13737     {
13738       if (!file_mips_gp32)
13739         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13740       else
13741         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13742     }
13743   else if (mips_abi == N32_ABI)
13744     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13745
13746   /* Nothing to do for N64_ABI.  */
13747
13748   if (mips_32bitmode)
13749     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13750 }
13751
13752 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13753 \f
13754 typedef struct proc {
13755   symbolS *isym;
13756   unsigned long reg_mask;
13757   unsigned long reg_offset;
13758   unsigned long fpreg_mask;
13759   unsigned long fpreg_offset;
13760   unsigned long frame_offset;
13761   unsigned long frame_reg;
13762   unsigned long pc_reg;
13763 } procS;
13764
13765 static procS cur_proc;
13766 static procS *cur_proc_ptr;
13767 static int numprocs;
13768
13769 /* Fill in an rs_align_code fragment.  */
13770
13771 void
13772 mips_handle_align (fragp)
13773      fragS *fragp;
13774 {
13775   if (fragp->fr_type != rs_align_code)
13776     return;
13777
13778   if (mips_opts.mips16)
13779     {
13780       static const unsigned char be_nop[] = { 0x65, 0x00 };
13781       static const unsigned char le_nop[] = { 0x00, 0x65 };
13782
13783       int bytes;
13784       char *p;
13785
13786       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13787       p = fragp->fr_literal + fragp->fr_fix;
13788
13789       if (bytes & 1)
13790         {
13791           *p++ = 0;
13792           fragp->fr_fix++;
13793         }
13794
13795       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13796       fragp->fr_var = 2;
13797     }
13798
13799   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13800 }
13801
13802 static void
13803 md_obj_begin ()
13804 {
13805 }
13806
13807 static void
13808 md_obj_end ()
13809 {
13810   /* check for premature end, nesting errors, etc */
13811   if (cur_proc_ptr)
13812     as_warn (_("missing .end at end of assembly"));
13813 }
13814
13815 static long
13816 get_number ()
13817 {
13818   int negative = 0;
13819   long val = 0;
13820
13821   if (*input_line_pointer == '-')
13822     {
13823       ++input_line_pointer;
13824       negative = 1;
13825     }
13826   if (!ISDIGIT (*input_line_pointer))
13827     as_bad (_("expected simple number"));
13828   if (input_line_pointer[0] == '0')
13829     {
13830       if (input_line_pointer[1] == 'x')
13831         {
13832           input_line_pointer += 2;
13833           while (ISXDIGIT (*input_line_pointer))
13834             {
13835               val <<= 4;
13836               val |= hex_value (*input_line_pointer++);
13837             }
13838           return negative ? -val : val;
13839         }
13840       else
13841         {
13842           ++input_line_pointer;
13843           while (ISDIGIT (*input_line_pointer))
13844             {
13845               val <<= 3;
13846               val |= *input_line_pointer++ - '0';
13847             }
13848           return negative ? -val : val;
13849         }
13850     }
13851   if (!ISDIGIT (*input_line_pointer))
13852     {
13853       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13854               *input_line_pointer, *input_line_pointer);
13855       as_warn (_("invalid number"));
13856       return -1;
13857     }
13858   while (ISDIGIT (*input_line_pointer))
13859     {
13860       val *= 10;
13861       val += *input_line_pointer++ - '0';
13862     }
13863   return negative ? -val : val;
13864 }
13865
13866 /* The .file directive; just like the usual .file directive, but there
13867    is an initial number which is the ECOFF file index.  In the non-ECOFF
13868    case .file implies DWARF-2.  */
13869
13870 static void
13871 s_mips_file (x)
13872      int x ATTRIBUTE_UNUSED;
13873 {
13874   static int first_file_directive = 0;
13875
13876   if (ECOFF_DEBUGGING)
13877     {
13878       get_number ();
13879       s_app_file (0);
13880     }
13881   else
13882     {
13883       char *filename;
13884
13885       filename = dwarf2_directive_file (0);
13886
13887       /* Versions of GCC up to 3.1 start files with a ".file"
13888          directive even for stabs output.  Make sure that this
13889          ".file" is handled.  Note that you need a version of GCC
13890          after 3.1 in order to support DWARF-2 on MIPS.  */
13891       if (filename != NULL && ! first_file_directive)
13892         {
13893           (void) new_logical_line (filename, -1);
13894           s_app_file_string (filename);
13895         }
13896       first_file_directive = 1;
13897     }
13898 }
13899
13900 /* The .loc directive, implying DWARF-2.  */
13901
13902 static void
13903 s_mips_loc (x)
13904      int x ATTRIBUTE_UNUSED;
13905 {
13906   if (!ECOFF_DEBUGGING)
13907     dwarf2_directive_loc (0);
13908 }
13909
13910 /* The .end directive.  */
13911
13912 static void
13913 s_mips_end (x)
13914      int x ATTRIBUTE_UNUSED;
13915 {
13916   symbolS *p;
13917   int maybe_text;
13918
13919   /* Following functions need their own .frame and .cprestore directives.  */
13920   mips_frame_reg_valid = 0;
13921   mips_cprestore_valid = 0;
13922
13923   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13924     {
13925       p = get_symbol ();
13926       demand_empty_rest_of_line ();
13927     }
13928   else
13929     p = NULL;
13930
13931 #ifdef BFD_ASSEMBLER
13932   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13933     maybe_text = 1;
13934   else
13935     maybe_text = 0;
13936 #else
13937   if (now_seg != data_section && now_seg != bss_section)
13938     maybe_text = 1;
13939   else
13940     maybe_text = 0;
13941 #endif
13942
13943   if (!maybe_text)
13944     as_warn (_(".end not in text section"));
13945
13946   if (!cur_proc_ptr)
13947     {
13948       as_warn (_(".end directive without a preceding .ent directive."));
13949       demand_empty_rest_of_line ();
13950       return;
13951     }
13952
13953   if (p != NULL)
13954     {
13955       assert (S_GET_NAME (p));
13956       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13957         as_warn (_(".end symbol does not match .ent symbol."));
13958
13959       if (debug_type == DEBUG_STABS)
13960         stabs_generate_asm_endfunc (S_GET_NAME (p),
13961                                     S_GET_NAME (p));
13962     }
13963   else
13964     as_warn (_(".end directive missing or unknown symbol"));
13965
13966 #ifdef OBJ_ELF
13967   /* Generate a .pdr section.  */
13968   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13969     {
13970       segT saved_seg = now_seg;
13971       subsegT saved_subseg = now_subseg;
13972       valueT dot;
13973       expressionS exp;
13974       char *fragp;
13975
13976       dot = frag_now_fix ();
13977
13978 #ifdef md_flush_pending_output
13979       md_flush_pending_output ();
13980 #endif
13981
13982       assert (pdr_seg);
13983       subseg_set (pdr_seg, 0);
13984
13985       /* Write the symbol.  */
13986       exp.X_op = O_symbol;
13987       exp.X_add_symbol = p;
13988       exp.X_add_number = 0;
13989       emit_expr (&exp, 4);
13990
13991       fragp = frag_more (7 * 4);
13992
13993       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13994       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13995       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13996       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13997       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13998       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13999       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14000
14001       subseg_set (saved_seg, saved_subseg);
14002     }
14003 #endif /* OBJ_ELF */
14004
14005   cur_proc_ptr = NULL;
14006 }
14007
14008 /* The .aent and .ent directives.  */
14009
14010 static void
14011 s_mips_ent (aent)
14012      int aent;
14013 {
14014   symbolS *symbolP;
14015   int maybe_text;
14016
14017   symbolP = get_symbol ();
14018   if (*input_line_pointer == ',')
14019     ++input_line_pointer;
14020   SKIP_WHITESPACE ();
14021   if (ISDIGIT (*input_line_pointer)
14022       || *input_line_pointer == '-')
14023     get_number ();
14024
14025 #ifdef BFD_ASSEMBLER
14026   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14027     maybe_text = 1;
14028   else
14029     maybe_text = 0;
14030 #else
14031   if (now_seg != data_section && now_seg != bss_section)
14032     maybe_text = 1;
14033   else
14034     maybe_text = 0;
14035 #endif
14036
14037   if (!maybe_text)
14038     as_warn (_(".ent or .aent not in text section."));
14039
14040   if (!aent && cur_proc_ptr)
14041     as_warn (_("missing .end"));
14042
14043   if (!aent)
14044     {
14045       /* This function needs its own .frame and .cprestore directives.  */
14046       mips_frame_reg_valid = 0;
14047       mips_cprestore_valid = 0;
14048
14049       cur_proc_ptr = &cur_proc;
14050       memset (cur_proc_ptr, '\0', sizeof (procS));
14051
14052       cur_proc_ptr->isym = symbolP;
14053
14054       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14055
14056       ++numprocs;
14057
14058       if (debug_type == DEBUG_STABS)
14059         stabs_generate_asm_func (S_GET_NAME (symbolP),
14060                                  S_GET_NAME (symbolP));
14061     }
14062
14063   demand_empty_rest_of_line ();
14064 }
14065
14066 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14067    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14068    s_mips_frame is used so that we can set the PDR information correctly.
14069    We can't use the ecoff routines because they make reference to the ecoff
14070    symbol table (in the mdebug section).  */
14071
14072 static void
14073 s_mips_frame (ignore)
14074      int ignore ATTRIBUTE_UNUSED;
14075 {
14076 #ifdef OBJ_ELF
14077   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14078     {
14079       long val;
14080
14081       if (cur_proc_ptr == (procS *) NULL)
14082         {
14083           as_warn (_(".frame outside of .ent"));
14084           demand_empty_rest_of_line ();
14085           return;
14086         }
14087
14088       cur_proc_ptr->frame_reg = tc_get_register (1);
14089
14090       SKIP_WHITESPACE ();
14091       if (*input_line_pointer++ != ','
14092           || get_absolute_expression_and_terminator (&val) != ',')
14093         {
14094           as_warn (_("Bad .frame directive"));
14095           --input_line_pointer;
14096           demand_empty_rest_of_line ();
14097           return;
14098         }
14099
14100       cur_proc_ptr->frame_offset = val;
14101       cur_proc_ptr->pc_reg = tc_get_register (0);
14102
14103       demand_empty_rest_of_line ();
14104     }
14105   else
14106 #endif /* OBJ_ELF */
14107     s_ignore (ignore);
14108 }
14109
14110 /* The .fmask and .mask directives. If the mdebug section is present
14111    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14112    embedded targets, s_mips_mask is used so that we can set the PDR
14113    information correctly. We can't use the ecoff routines because they
14114    make reference to the ecoff symbol table (in the mdebug section).  */
14115
14116 static void
14117 s_mips_mask (reg_type)
14118      char reg_type;
14119 {
14120 #ifdef OBJ_ELF
14121   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14122     {
14123       long mask, off;
14124
14125       if (cur_proc_ptr == (procS *) NULL)
14126         {
14127           as_warn (_(".mask/.fmask outside of .ent"));
14128           demand_empty_rest_of_line ();
14129           return;
14130         }
14131
14132       if (get_absolute_expression_and_terminator (&mask) != ',')
14133         {
14134           as_warn (_("Bad .mask/.fmask directive"));
14135           --input_line_pointer;
14136           demand_empty_rest_of_line ();
14137           return;
14138         }
14139
14140       off = get_absolute_expression ();
14141
14142       if (reg_type == 'F')
14143         {
14144           cur_proc_ptr->fpreg_mask = mask;
14145           cur_proc_ptr->fpreg_offset = off;
14146         }
14147       else
14148         {
14149           cur_proc_ptr->reg_mask = mask;
14150           cur_proc_ptr->reg_offset = off;
14151         }
14152
14153       demand_empty_rest_of_line ();
14154     }
14155   else
14156 #endif /* OBJ_ELF */
14157     s_ignore (reg_type);
14158 }
14159
14160 /* The .loc directive.  */
14161
14162 #if 0
14163 static void
14164 s_loc (x)
14165      int x;
14166 {
14167   symbolS *symbolP;
14168   int lineno;
14169   int addroff;
14170
14171   assert (now_seg == text_section);
14172
14173   lineno = get_number ();
14174   addroff = frag_now_fix ();
14175
14176   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14177   S_SET_TYPE (symbolP, N_SLINE);
14178   S_SET_OTHER (symbolP, 0);
14179   S_SET_DESC (symbolP, lineno);
14180   symbolP->sy_segment = now_seg;
14181 }
14182 #endif
14183
14184 /* A table describing all the processors gas knows about.  Names are
14185    matched in the order listed.
14186
14187    To ease comparison, please keep this table in the same order as
14188    gcc's mips_cpu_info_table[].  */
14189 static const struct mips_cpu_info mips_cpu_info_table[] =
14190 {
14191   /* Entries for generic ISAs */
14192   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14193   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14194   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14195   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14196   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14197   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14198   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14199
14200   /* MIPS I */
14201   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14202   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14203   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14204
14205   /* MIPS II */
14206   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14207
14208   /* MIPS III */
14209   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14210   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14211   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14212   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14213   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14214   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14215   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14216   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14217   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14218   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14219   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14220   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14221
14222   /* MIPS IV */
14223   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14224   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14225   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14226   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14227   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14228   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14229   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14230   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14231   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14232   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14233   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14234   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14235
14236   /* MIPS 32 */
14237   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14238   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14239   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14240
14241   /* MIPS 64 */
14242   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14243   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14244
14245   /* Broadcom SB-1 CPU core */
14246   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14247
14248   /* End marker */
14249   { NULL, 0, 0, 0 }
14250 };
14251
14252
14253 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14254    with a final "000" replaced by "k".  Ignore case.
14255
14256    Note: this function is shared between GCC and GAS.  */
14257
14258 static boolean
14259 mips_strict_matching_cpu_name_p (canonical, given)
14260      const char *canonical, *given;
14261 {
14262   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14263     given++, canonical++;
14264
14265   return ((*given == 0 && *canonical == 0)
14266           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14267 }
14268
14269
14270 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14271    CPU name.  We've traditionally allowed a lot of variation here.
14272
14273    Note: this function is shared between GCC and GAS.  */
14274
14275 static boolean
14276 mips_matching_cpu_name_p (canonical, given)
14277      const char *canonical, *given;
14278 {
14279   /* First see if the name matches exactly, or with a final "000"
14280      turned into "k".  */
14281   if (mips_strict_matching_cpu_name_p (canonical, given))
14282     return true;
14283
14284   /* If not, try comparing based on numerical designation alone.
14285      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14286   if (TOLOWER (*given) == 'r')
14287     given++;
14288   if (!ISDIGIT (*given))
14289     return false;
14290
14291   /* Skip over some well-known prefixes in the canonical name,
14292      hoping to find a number there too.  */
14293   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14294     canonical += 2;
14295   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14296     canonical += 2;
14297   else if (TOLOWER (canonical[0]) == 'r')
14298     canonical += 1;
14299
14300   return mips_strict_matching_cpu_name_p (canonical, given);
14301 }
14302
14303
14304 /* Parse an option that takes the name of a processor as its argument.
14305    OPTION is the name of the option and CPU_STRING is the argument.
14306    Return the corresponding processor enumeration if the CPU_STRING is
14307    recognized, otherwise report an error and return null.
14308
14309    A similar function exists in GCC.  */
14310
14311 static const struct mips_cpu_info *
14312 mips_parse_cpu (option, cpu_string)
14313      const char *option, *cpu_string;
14314 {
14315   const struct mips_cpu_info *p;
14316
14317   /* 'from-abi' selects the most compatible architecture for the given
14318      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14319      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14320      version.  Look first at the -mgp options, if given, otherwise base
14321      the choice on MIPS_DEFAULT_64BIT.
14322
14323      Treat NO_ABI like the EABIs.  One reason to do this is that the
14324      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14325      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14326      'mips64', just as we did in the days before 'from-abi'.  */
14327   if (strcasecmp (cpu_string, "from-abi") == 0)
14328     {
14329       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14330         return mips_cpu_info_from_isa (ISA_MIPS1);
14331
14332       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14333         return mips_cpu_info_from_isa (ISA_MIPS3);
14334
14335       if (file_mips_gp32 >= 0)
14336         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14337
14338       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14339                                      ? ISA_MIPS3
14340                                      : ISA_MIPS1);
14341     }
14342
14343   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14344   if (strcasecmp (cpu_string, "default") == 0)
14345     return 0;
14346
14347   for (p = mips_cpu_info_table; p->name != 0; p++)
14348     if (mips_matching_cpu_name_p (p->name, cpu_string))
14349       return p;
14350
14351   as_bad ("Bad value (%s) for %s", cpu_string, option);
14352   return 0;
14353 }
14354
14355 /* Return the canonical processor information for ISA (a member of the
14356    ISA_MIPS* enumeration).  */
14357
14358 static const struct mips_cpu_info *
14359 mips_cpu_info_from_isa (isa)
14360      int isa;
14361 {
14362   int i;
14363
14364   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14365     if (mips_cpu_info_table[i].is_isa
14366         && isa == mips_cpu_info_table[i].isa)
14367       return (&mips_cpu_info_table[i]);
14368
14369   return NULL;
14370 }
14371 \f
14372 static void
14373 show (stream, string, col_p, first_p)
14374      FILE *stream;
14375      const char *string;
14376      int *col_p;
14377      int *first_p;
14378 {
14379   if (*first_p)
14380     {
14381       fprintf (stream, "%24s", "");
14382       *col_p = 24;
14383     }
14384   else
14385     {
14386       fprintf (stream, ", ");
14387       *col_p += 2;
14388     }
14389
14390   if (*col_p + strlen (string) > 72)
14391     {
14392       fprintf (stream, "\n%24s", "");
14393       *col_p = 24;
14394     }
14395
14396   fprintf (stream, "%s", string);
14397   *col_p += strlen (string);
14398
14399   *first_p = 0;
14400 }
14401
14402 void
14403 md_show_usage (stream)
14404      FILE *stream;
14405 {
14406   int column, first;
14407   size_t i;
14408
14409   fprintf (stream, _("\
14410 MIPS options:\n\
14411 -membedded-pic          generate embedded position independent code\n\
14412 -EB                     generate big endian output\n\
14413 -EL                     generate little endian output\n\
14414 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14415 -G NUM                  allow referencing objects up to NUM bytes\n\
14416                         implicitly with the gp register [default 8]\n"));
14417   fprintf (stream, _("\
14418 -mips1                  generate MIPS ISA I instructions\n\
14419 -mips2                  generate MIPS ISA II instructions\n\
14420 -mips3                  generate MIPS ISA III instructions\n\
14421 -mips4                  generate MIPS ISA IV instructions\n\
14422 -mips5                  generate MIPS ISA V instructions\n\
14423 -mips32                 generate MIPS32 ISA instructions\n\
14424 -mips64                 generate MIPS64 ISA instructions\n\
14425 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14426
14427   first = 1;
14428
14429   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14430     show (stream, mips_cpu_info_table[i].name, &column, &first);
14431   show (stream, "from-abi", &column, &first);
14432   fputc ('\n', stream);
14433
14434   fprintf (stream, _("\
14435 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14436 -no-mCPU                don't generate code specific to CPU.\n\
14437                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14438
14439   first = 1;
14440
14441   show (stream, "3900", &column, &first);
14442   show (stream, "4010", &column, &first);
14443   show (stream, "4100", &column, &first);
14444   show (stream, "4650", &column, &first);
14445   fputc ('\n', stream);
14446
14447   fprintf (stream, _("\
14448 -mips16                 generate mips16 instructions\n\
14449 -no-mips16              do not generate mips16 instructions\n"));
14450   fprintf (stream, _("\
14451 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14452 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14453 -O0                     remove unneeded NOPs, do not swap branches\n\
14454 -O                      remove unneeded NOPs and swap branches\n\
14455 -n                      warn about NOPs generated from macros\n\
14456 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14457 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14458 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14459 #ifdef OBJ_ELF
14460   fprintf (stream, _("\
14461 -KPIC, -call_shared     generate SVR4 position independent code\n\
14462 -non_shared             do not generate position independent code\n\
14463 -xgot                   assume a 32 bit GOT\n\
14464 -mabi=ABI               create ABI conformant object file for:\n"));
14465
14466   first = 1;
14467
14468   show (stream, "32", &column, &first);
14469   show (stream, "o64", &column, &first);
14470   show (stream, "n32", &column, &first);
14471   show (stream, "64", &column, &first);
14472   show (stream, "eabi", &column, &first);
14473
14474   fputc ('\n', stream);
14475
14476   fprintf (stream, _("\
14477 -32                     create o32 ABI object file (default)\n\
14478 -n32                    create n32 ABI object file\n\
14479 -64                     create 64 ABI object file\n"));
14480 #endif
14481 }