* config/tc-mips.c (s_cpsetup): Fix comment.
[external/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
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 #ifdef OBJ_MAYBE_ELF
48 /* Clean up namespace so we can include obj-elf.h too.  */
49 static int mips_output_flavor PARAMS ((void));
50 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
51 #undef OBJ_PROCESS_STAB
52 #undef OUTPUT_FLAVOR
53 #undef S_GET_ALIGN
54 #undef S_GET_SIZE
55 #undef S_SET_ALIGN
56 #undef S_SET_SIZE
57 #undef obj_frob_file
58 #undef obj_frob_file_after_relocs
59 #undef obj_frob_symbol
60 #undef obj_pop_insert
61 #undef obj_sec_sym_ok_for_reloc
62 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
63
64 #include "obj-elf.h"
65 /* Fix any of them that we actually care about.  */
66 #undef OUTPUT_FLAVOR
67 #define OUTPUT_FLAVOR mips_output_flavor()
68 #endif
69
70 #if defined (OBJ_ELF)
71 #include "elf/mips.h"
72 #endif
73
74 #ifndef ECOFF_DEBUGGING
75 #define NO_ECOFF_DEBUGGING
76 #define ECOFF_DEBUGGING 0
77 #endif
78
79 #include "ecoff.h"
80
81 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
82 static char *mips_regmask_frag;
83 #endif
84
85 #define AT  1
86 #define TREG 24
87 #define PIC_CALL_REG 25
88 #define KT0 26
89 #define KT1 27
90 #define GP  28
91 #define SP  29
92 #define FP  30
93 #define RA  31
94
95 #define ILLEGAL_REG (32)
96
97 /* Allow override of standard little-endian ECOFF format.  */
98
99 #ifndef ECOFF_LITTLE_FORMAT
100 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
101 #endif
102
103 extern int target_big_endian;
104
105 /* The name of the readonly data section.  */
106 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
107                             ? ".data" \
108                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
109                             ? ".rdata" \
110                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
111                             ? ".rdata" \
112                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
113                             ? ".rodata" \
114                             : (abort (), ""))
115
116 /* The ABI to use.  */
117 enum mips_abi_level
118 {
119   NO_ABI = 0,
120   O32_ABI,
121   O64_ABI,
122   N32_ABI,
123   N64_ABI,
124   EABI_ABI
125 };
126
127 /* MIPS ABI we are using for this output file.  */
128 static enum mips_abi_level file_mips_abi = NO_ABI;
129
130 /* This is the set of options which may be modified by the .set
131    pseudo-op.  We use a struct so that .set push and .set pop are more
132    reliable.  */
133
134 struct mips_set_options
135 {
136   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
137      if it has not been initialized.  Changed by `.set mipsN', and the
138      -mipsN command line option, and the default CPU.  */
139   int isa;
140   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
141      if they have not been initialized.  Changed by `.set <asename>', by
142      command line options, and based on the default architecture.  */
143   int ase_mips3d;
144   int ase_mdmx;
145   /* Whether we are assembling for the mips16 processor.  0 if we are
146      not, 1 if we are, and -1 if the value has not been initialized.
147      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
148      -nomips16 command line options, and the default CPU.  */
149   int mips16;
150   /* Non-zero if we should not reorder instructions.  Changed by `.set
151      reorder' and `.set noreorder'.  */
152   int noreorder;
153   /* Non-zero if we should not permit the $at ($1) register to be used
154      in instructions.  Changed by `.set at' and `.set noat'.  */
155   int noat;
156   /* Non-zero if we should warn when a macro instruction expands into
157      more than one machine instruction.  Changed by `.set nomacro' and
158      `.set macro'.  */
159   int warn_about_macros;
160   /* Non-zero if we should not move instructions.  Changed by `.set
161      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
162   int nomove;
163   /* Non-zero if we should not optimize branches by moving the target
164      of the branch into the delay slot.  Actually, we don't perform
165      this optimization anyhow.  Changed by `.set bopt' and `.set
166      nobopt'.  */
167   int nobopt;
168   /* Non-zero if we should not autoextend mips16 instructions.
169      Changed by `.set autoextend' and `.set noautoextend'.  */
170   int noautoextend;
171   /* Restrict general purpose registers and floating point registers
172      to 32 bit.  This is initially determined when -mgp32 or -mfp32
173      is passed but can changed if the assembler code uses .set mipsN.  */
174   int gp32;
175   int fp32;
176   /* The ABI currently in use. This is changed by .set mipsN to loosen
177      restrictions and doesn't affect the whole file.  */
178   enum mips_abi_level abi;
179 };
180
181 /* True if -mgp32 was passed.  */
182 static int file_mips_gp32 = -1;
183
184 /* True if -mfp32 was passed.  */
185 static int file_mips_fp32 = -1;
186
187 /* This is the struct we use to hold the current set of options.  Note
188    that we must set the isa field to ISA_UNKNOWN and the mips16 field to
189    -1 to indicate that they have not been initialized.  */
190
191 static struct mips_set_options mips_opts =
192 {
193   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, NO_ABI
194 };
195
196 /* These variables are filled in with the masks of registers used.
197    The object format code reads them and puts them in the appropriate
198    place.  */
199 unsigned long mips_gprmask;
200 unsigned long mips_cprmask[4];
201
202 /* MIPS ISA we are using for this output file.  */
203 static int file_mips_isa = ISA_UNKNOWN;
204
205 /* True if -mips3d was passed or implied by arguments passed on the
206    command line (e.g., by -march).  */
207 static int file_ase_mips3d;
208
209 /* True if -mdmx was passed or implied by arguments passed on the
210    command line (e.g., by -march).  */
211 static int file_ase_mdmx;
212
213 /* The argument of the -mcpu= flag.  Historical for code generation.  */
214 static int mips_cpu = CPU_UNKNOWN;
215
216 /* The argument of the -march= flag.  The architecture we are assembling.  */
217 static int mips_arch = CPU_UNKNOWN;
218
219 /* The argument of the -mtune= flag.  The architecture for which we
220    are optimizing.  */
221 static int mips_tune = CPU_UNKNOWN;
222
223 /* Whether we should mark the file EABI64 or EABI32.  */
224 static int mips_eabi64 = 0;
225
226 /* If they asked for mips1 or mips2 and a cpu that is
227    mips3 or greater, then mark the object file 32BITMODE.  */
228 static int mips_32bitmode = 0;
229
230 /* Some ISA's have delay slots for instructions which read or write
231    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
232    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
233    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
234    delay slot in this ISA.  The uses of this macro assume that any
235    ISA that has delay slots for one of these, has them for all.  They
236    also assume that ISAs which don't have delays for these insns, don't
237    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
238 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
239    (ISA) == ISA_MIPS1                       \
240    || (ISA) == ISA_MIPS2                    \
241    || (ISA) == ISA_MIPS3                    \
242    )
243
244 /*  Return true if ISA supports 64 bit gp register instructions.  */
245 #define ISA_HAS_64BIT_REGS(ISA) (    \
246    (ISA) == ISA_MIPS3                \
247    || (ISA) == ISA_MIPS4             \
248    || (ISA) == ISA_MIPS5             \
249    || (ISA) == ISA_MIPS64            \
250    )
251
252 #define HAVE_32BIT_GPRS                            \
253     (mips_opts.gp32                                \
254      || mips_opts.abi == O32_ABI                   \
255      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
256
257 #define HAVE_32BIT_FPRS                            \
258     (mips_opts.fp32                                \
259      || mips_opts.abi == O32_ABI                   \
260      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
261
262 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
263 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
264
265 #define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
266
267 #define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
268
269 /* We can only have 64bit addresses if the object file format
270    supports it.  */
271 #define HAVE_32BIT_ADDRESSES                           \
272    (HAVE_32BIT_GPRS                                    \
273     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
274          || ! HAVE_64BIT_OBJECTS)                      \
275         && mips_pic != EMBEDDED_PIC))
276
277 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
278
279 /* Return true if the given CPU supports the MIPS3D ASE.  */
280 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
281                                  )
282
283 /* Return true if the given CPU supports the MDMX ASE.  */
284 #define CPU_HAS_MDMX(cpu)       (0                    \
285                                  )
286
287 /* Whether the processor uses hardware interlocks to protect
288    reads from the HI and LO registers, and thus does not
289    require nops to be inserted.  */
290
291 #define hilo_interlocks (mips_arch == CPU_R4010                       \
292                          || mips_arch == CPU_SB1                      \
293                          )
294
295 /* Whether the processor uses hardware interlocks to protect reads
296    from the GPRs, and thus does not require nops to be inserted.  */
297 #define gpr_interlocks \
298   (mips_opts.isa != ISA_MIPS1  \
299    || mips_arch == CPU_R3900)
300
301 /* As with other "interlocks" this is used by hardware that has FP
302    (co-processor) interlocks.  */
303 /* Itbl support may require additional care here.  */
304 #define cop_interlocks (mips_arch == CPU_R4300                        \
305                         || mips_arch == CPU_SB1                       \
306                         )
307
308 /* Is this a mfhi or mflo instruction?  */
309 #define MF_HILO_INSN(PINFO) \
310           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
311
312 /* MIPS PIC level.  */
313
314 enum mips_pic_level
315 {
316   /* Do not generate PIC code.  */
317   NO_PIC,
318
319   /* Generate PIC code as in Irix 4.  This is not implemented, and I'm
320      not sure what it is supposed to do.  */
321   IRIX4_PIC,
322
323   /* Generate PIC code as in the SVR4 MIPS ABI.  */
324   SVR4_PIC,
325
326   /* Generate PIC code without using a global offset table: the data
327      segment has a maximum size of 64K, all data references are off
328      the $gp register, and all text references are PC relative.  This
329      is used on some embedded systems.  */
330   EMBEDDED_PIC
331 };
332
333 static enum mips_pic_level mips_pic;
334
335 /* Warn about all NOPS that the assembler generates.  */
336 static int warn_nops = 0;
337
338 /* 1 if we should generate 32 bit offsets from the $gp register in
339    SVR4_PIC mode.  Currently has no meaning in other modes.  */
340 static int mips_big_got = 0;
341
342 /* 1 if trap instructions should used for overflow rather than break
343    instructions.  */
344 static int mips_trap = 0;
345
346 /* 1 if double width floating point constants should not be constructed
347    by assembling two single width halves into two single width floating
348    point registers which just happen to alias the double width destination
349    register.  On some architectures this aliasing can be disabled by a bit
350    in the status register, and the setting of this bit cannot be determined
351    automatically at assemble time.  */
352 static int mips_disable_float_construction;
353
354 /* Non-zero if any .set noreorder directives were used.  */
355
356 static int mips_any_noreorder;
357
358 /* Non-zero if nops should be inserted when the register referenced in
359    an mfhi/mflo instruction is read in the next two instructions.  */
360 static int mips_7000_hilo_fix;
361
362 /* The size of the small data section.  */
363 static unsigned int g_switch_value = 8;
364 /* Whether the -G option was used.  */
365 static int g_switch_seen = 0;
366
367 #define N_RMASK 0xc4
368 #define N_VFP   0xd4
369
370 /* If we can determine in advance that GP optimization won't be
371    possible, we can skip the relaxation stuff that tries to produce
372    GP-relative references.  This makes delay slot optimization work
373    better.
374
375    This function can only provide a guess, but it seems to work for
376    gcc output.  It needs to guess right for gcc, otherwise gcc
377    will put what it thinks is a GP-relative instruction in a branch
378    delay slot.
379
380    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
381    fixed it for the non-PIC mode.  KR 95/04/07  */
382 static int nopic_need_relax PARAMS ((symbolS *, int));
383
384 /* handle of the OPCODE hash table */
385 static struct hash_control *op_hash = NULL;
386
387 /* The opcode hash table we use for the mips16.  */
388 static struct hash_control *mips16_op_hash = NULL;
389
390 /* This array holds the chars that always start a comment.  If the
391     pre-processor is disabled, these aren't very useful */
392 const char comment_chars[] = "#";
393
394 /* This array holds the chars that only start a comment at the beginning of
395    a line.  If the line seems to have the form '# 123 filename'
396    .line and .file directives will appear in the pre-processed output */
397 /* Note that input_file.c hand checks for '#' at the beginning of the
398    first line of the input file.  This is because the compiler outputs
399    #NO_APP at the beginning of its output.  */
400 /* Also note that C style comments are always supported.  */
401 const char line_comment_chars[] = "#";
402
403 /* This array holds machine specific line separator characters.  */
404 const char line_separator_chars[] = ";";
405
406 /* Chars that can be used to separate mant from exp in floating point nums */
407 const char EXP_CHARS[] = "eE";
408
409 /* Chars that mean this number is a floating point constant */
410 /* As in 0f12.456 */
411 /* or    0d1.2345e12 */
412 const char FLT_CHARS[] = "rRsSfFdDxXpP";
413
414 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
415    changed in read.c .  Ideally it shouldn't have to know about it at all,
416    but nothing is ideal around here.
417  */
418
419 static char *insn_error;
420
421 static int auto_align = 1;
422
423 /* When outputting SVR4 PIC code, the assembler needs to know the
424    offset in the stack frame from which to restore the $gp register.
425    This is set by the .cprestore pseudo-op, and saved in this
426    variable.  */
427 static offsetT mips_cprestore_offset = -1;
428
429 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
430    more optimizations, it can use a register value instead of a memory-saved
431    offset and even an other register than $gp as global pointer.  */
432 static offsetT mips_cpreturn_offset = -1;
433 static int mips_cpreturn_register = -1;
434 static int mips_gp_register = GP;
435 static int mips_gprel_offset = 0;
436
437 /* Whether mips_cprestore_offset has been set in the current function
438    (or whether it has already been warned about, if not).  */
439 static int mips_cprestore_valid = 0;
440
441 /* This is the register which holds the stack frame, as set by the
442    .frame pseudo-op.  This is needed to implement .cprestore.  */
443 static int mips_frame_reg = SP;
444
445 /* Whether mips_frame_reg has been set in the current function
446    (or whether it has already been warned about, if not).  */
447 static int mips_frame_reg_valid = 0;
448
449 /* To output NOP instructions correctly, we need to keep information
450    about the previous two instructions.  */
451
452 /* Whether we are optimizing.  The default value of 2 means to remove
453    unneeded NOPs and swap branch instructions when possible.  A value
454    of 1 means to not swap branches.  A value of 0 means to always
455    insert NOPs.  */
456 static int mips_optimize = 2;
457
458 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
459    equivalent to seeing no -g option at all.  */
460 static int mips_debug = 0;
461
462 /* The previous instruction.  */
463 static struct mips_cl_insn prev_insn;
464
465 /* The instruction before prev_insn.  */
466 static struct mips_cl_insn prev_prev_insn;
467
468 /* If we don't want information for prev_insn or prev_prev_insn, we
469    point the insn_mo field at this dummy integer.  */
470 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
471
472 /* Non-zero if prev_insn is valid.  */
473 static int prev_insn_valid;
474
475 /* The frag for the previous instruction.  */
476 static struct frag *prev_insn_frag;
477
478 /* The offset into prev_insn_frag for the previous instruction.  */
479 static long prev_insn_where;
480
481 /* The reloc type for the previous instruction, if any.  */
482 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
483
484 /* The reloc for the previous instruction, if any.  */
485 static fixS *prev_insn_fixp[3];
486
487 /* Non-zero if the previous instruction was in a delay slot.  */
488 static int prev_insn_is_delay_slot;
489
490 /* Non-zero if the previous instruction was in a .set noreorder.  */
491 static int prev_insn_unreordered;
492
493 /* Non-zero if the previous instruction uses an extend opcode (if
494    mips16).  */
495 static int prev_insn_extended;
496
497 /* Non-zero if the previous previous instruction was in a .set
498    noreorder.  */
499 static int prev_prev_insn_unreordered;
500
501 /* If this is set, it points to a frag holding nop instructions which
502    were inserted before the start of a noreorder section.  If those
503    nops turn out to be unnecessary, the size of the frag can be
504    decreased.  */
505 static fragS *prev_nop_frag;
506
507 /* The number of nop instructions we created in prev_nop_frag.  */
508 static int prev_nop_frag_holds;
509
510 /* The number of nop instructions that we know we need in
511    prev_nop_frag.  */
512 static int prev_nop_frag_required;
513
514 /* The number of instructions we've seen since prev_nop_frag.  */
515 static int prev_nop_frag_since;
516
517 /* For ECOFF and ELF, relocations against symbols are done in two
518    parts, with a HI relocation and a LO relocation.  Each relocation
519    has only 16 bits of space to store an addend.  This means that in
520    order for the linker to handle carries correctly, it must be able
521    to locate both the HI and the LO relocation.  This means that the
522    relocations must appear in order in the relocation table.
523
524    In order to implement this, we keep track of each unmatched HI
525    relocation.  We then sort them so that they immediately precede the
526    corresponding LO relocation.  */
527
528 struct mips_hi_fixup
529 {
530   /* Next HI fixup.  */
531   struct mips_hi_fixup *next;
532   /* This fixup.  */
533   fixS *fixp;
534   /* The section this fixup is in.  */
535   segT seg;
536 };
537
538 /* The list of unmatched HI relocs.  */
539
540 static struct mips_hi_fixup *mips_hi_fixup_list;
541
542 /* Map normal MIPS register numbers to mips16 register numbers.  */
543
544 #define X ILLEGAL_REG
545 static const int mips32_to_16_reg_map[] =
546 {
547   X, X, 2, 3, 4, 5, 6, 7,
548   X, X, X, X, X, X, X, X,
549   0, 1, X, X, X, X, X, X,
550   X, X, X, X, X, X, X, X
551 };
552 #undef X
553
554 /* Map mips16 register numbers to normal MIPS register numbers.  */
555
556 static const unsigned int mips16_to_32_reg_map[] =
557 {
558   16, 17, 2, 3, 4, 5, 6, 7
559 };
560 \f
561 /* Since the MIPS does not have multiple forms of PC relative
562    instructions, we do not have to do relaxing as is done on other
563    platforms.  However, we do have to handle GP relative addressing
564    correctly, which turns out to be a similar problem.
565
566    Every macro that refers to a symbol can occur in (at least) two
567    forms, one with GP relative addressing and one without.  For
568    example, loading a global variable into a register generally uses
569    a macro instruction like this:
570      lw $4,i
571    If i can be addressed off the GP register (this is true if it is in
572    the .sbss or .sdata section, or if it is known to be smaller than
573    the -G argument) this will generate the following instruction:
574      lw $4,i($gp)
575    This instruction will use a GPREL reloc.  If i can not be addressed
576    off the GP register, the following instruction sequence will be used:
577      lui $at,i
578      lw $4,i($at)
579    In this case the first instruction will have a HI16 reloc, and the
580    second reloc will have a LO16 reloc.  Both relocs will be against
581    the symbol i.
582
583    The issue here is that we may not know whether i is GP addressable
584    until after we see the instruction that uses it.  Therefore, we
585    want to be able to choose the final instruction sequence only at
586    the end of the assembly.  This is similar to the way other
587    platforms choose the size of a PC relative instruction only at the
588    end of assembly.
589
590    When generating position independent code we do not use GP
591    addressing in quite the same way, but the issue still arises as
592    external symbols and local symbols must be handled differently.
593
594    We handle these issues by actually generating both possible
595    instruction sequences.  The longer one is put in a frag_var with
596    type rs_machine_dependent.  We encode what to do with the frag in
597    the subtype field.  We encode (1) the number of existing bytes to
598    replace, (2) the number of new bytes to use, (3) the offset from
599    the start of the existing bytes to the first reloc we must generate
600    (that is, the offset is applied from the start of the existing
601    bytes after they are replaced by the new bytes, if any), (4) the
602    offset from the start of the existing bytes to the second reloc,
603    (5) whether a third reloc is needed (the third reloc is always four
604    bytes after the second reloc), and (6) whether to warn if this
605    variant is used (this is sometimes needed if .set nomacro or .set
606    noat is in effect).  All these numbers are reasonably small.
607
608    Generating two instruction sequences must be handled carefully to
609    ensure that delay slots are handled correctly.  Fortunately, there
610    are a limited number of cases.  When the second instruction
611    sequence is generated, append_insn is directed to maintain the
612    existing delay slot information, so it continues to apply to any
613    code after the second instruction sequence.  This means that the
614    second instruction sequence must not impose any requirements not
615    required by the first instruction sequence.
616
617    These variant frags are then handled in functions called by the
618    machine independent code.  md_estimate_size_before_relax returns
619    the final size of the frag.  md_convert_frag sets up the final form
620    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
621    one if needed.  */
622 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
623   ((relax_substateT) \
624    (((old) << 23) \
625     | ((new) << 16) \
626     | (((reloc1) + 64) << 9) \
627     | (((reloc2) + 64) << 2) \
628     | ((reloc3) ? (1 << 1) : 0) \
629     | ((warn) ? 1 : 0)))
630 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
631 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
632 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
633 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
634 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
635 #define RELAX_WARN(i) ((i) & 1)
636
637 /* For mips16 code, we use an entirely different form of relaxation.
638    mips16 supports two versions of most instructions which take
639    immediate values: a small one which takes some small value, and a
640    larger one which takes a 16 bit value.  Since branches also follow
641    this pattern, relaxing these values is required.
642
643    We can assemble both mips16 and normal MIPS code in a single
644    object.  Therefore, we need to support this type of relaxation at
645    the same time that we support the relaxation described above.  We
646    use the high bit of the subtype field to distinguish these cases.
647
648    The information we store for this type of relaxation is the
649    argument code found in the opcode file for this relocation, whether
650    the user explicitly requested a small or extended form, and whether
651    the relocation is in a jump or jal delay slot.  That tells us the
652    size of the value, and how it should be stored.  We also store
653    whether the fragment is considered to be extended or not.  We also
654    store whether this is known to be a branch to a different section,
655    whether we have tried to relax this frag yet, and whether we have
656    ever extended a PC relative fragment because of a shift count.  */
657 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
658   (0x80000000                                                   \
659    | ((type) & 0xff)                                            \
660    | ((small) ? 0x100 : 0)                                      \
661    | ((ext) ? 0x200 : 0)                                        \
662    | ((dslot) ? 0x400 : 0)                                      \
663    | ((jal_dslot) ? 0x800 : 0))
664 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
665 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
666 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
667 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
668 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
669 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
670 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
671 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
672 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
673 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
674 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
675 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
676 \f
677 /* Prototypes for static functions.  */
678
679 #ifdef __STDC__
680 #define internalError() \
681     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
682 #else
683 #define internalError() as_fatal (_("MIPS internal Error"));
684 #endif
685
686 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
687
688 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
689                                   unsigned int reg, enum mips_regclass class));
690 static int reg_needs_delay PARAMS ((unsigned int));
691 static void mips16_mark_labels PARAMS ((void));
692 static void append_insn PARAMS ((char *place,
693                                  struct mips_cl_insn * ip,
694                                  expressionS * p,
695                                  bfd_reloc_code_real_type *r,
696                                  boolean));
697 static void mips_no_prev_insn PARAMS ((int));
698 static void mips_emit_delays PARAMS ((boolean));
699 #ifdef USE_STDARG
700 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
701                                  const char *name, const char *fmt,
702                                  ...));
703 #else
704 static void macro_build ();
705 #endif
706 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
707                                         const char *, const char *,
708                                         va_list));
709 static void macro_build_jalr PARAMS ((int, expressionS *));
710 static void macro_build_lui PARAMS ((char *place, int *counter,
711                                      expressionS * ep, int regnum));
712 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
713 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
714                                          expressionS *));
715 static void load_register PARAMS ((int *, int, expressionS *, int));
716 static void load_address PARAMS ((int *, int, expressionS *, int *));
717 static void move_register PARAMS ((int *, int, int));
718 static void macro PARAMS ((struct mips_cl_insn * ip));
719 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
720 #ifdef LOSING_COMPILER
721 static void macro2 PARAMS ((struct mips_cl_insn * ip));
722 #endif
723 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
724 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
725 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
726                                   boolean, boolean, unsigned long *,
727                                   boolean *, unsigned short *));
728 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
729 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
730 static int my_getSmallExpression PARAMS ((expressionS *, char *));
731 static void my_getExpression PARAMS ((expressionS *, char *));
732 #ifdef OBJ_ELF
733 static int support_64bit_objects PARAMS((void));
734 #endif
735 static symbolS *get_symbol PARAMS ((void));
736 static void mips_align PARAMS ((int to, int fill, symbolS *label));
737 static void s_align PARAMS ((int));
738 static void s_change_sec PARAMS ((int));
739 static void s_cons PARAMS ((int));
740 static void s_float_cons PARAMS ((int));
741 static void s_mips_globl PARAMS ((int));
742 static void s_option PARAMS ((int));
743 static void s_mipsset PARAMS ((int));
744 static void s_abicalls PARAMS ((int));
745 static void s_cpload PARAMS ((int));
746 static void s_cpsetup PARAMS ((int));
747 static void s_cplocal PARAMS ((int));
748 static void s_cprestore PARAMS ((int));
749 static void s_cpreturn PARAMS ((int));
750 static void s_gpvalue PARAMS ((int));
751 static void s_gpword PARAMS ((int));
752 static void s_cpadd PARAMS ((int));
753 static void s_insn PARAMS ((int));
754 static void md_obj_begin PARAMS ((void));
755 static void md_obj_end PARAMS ((void));
756 static long get_number PARAMS ((void));
757 static void s_mips_ent PARAMS ((int));
758 static void s_mips_end PARAMS ((int));
759 static void s_mips_frame PARAMS ((int));
760 static void s_mips_mask PARAMS ((int));
761 static void s_mips_stab PARAMS ((int));
762 static void s_mips_weakext PARAMS ((int));
763 static void s_file PARAMS ((int));
764 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
765 static const char *mips_isa_to_str PARAMS ((int));
766 static const char *mips_cpu_to_str PARAMS ((int));
767 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
768 static void show PARAMS ((FILE *, char *, int *, int *));
769 #ifdef OBJ_ELF
770 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
771 #endif
772
773 /* Return values of my_getSmallExpression().  */
774
775 enum small_ex_type
776 {
777   S_EX_NONE = 0,
778   S_EX_REGISTER,
779
780   /* Direct relocation creation by %percent_op().  */
781   S_EX_HALF,
782   S_EX_HI,
783   S_EX_LO,
784   S_EX_GP_REL,
785   S_EX_GOT,
786   S_EX_CALL16,
787   S_EX_GOT_DISP,
788   S_EX_GOT_PAGE,
789   S_EX_GOT_OFST,
790   S_EX_GOT_HI,
791   S_EX_GOT_LO,
792   S_EX_NEG,
793   S_EX_HIGHER,
794   S_EX_HIGHEST,
795   S_EX_CALL_HI,
796   S_EX_CALL_LO
797 };
798
799 /* Table and functions used to map between CPU/ISA names, and
800    ISA levels, and CPU numbers.  */
801
802 struct mips_cpu_info
803 {
804   const char *name;           /* CPU or ISA name.  */
805   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
806   int isa;                    /* ISA level.  */
807   int cpu;                    /* CPU number (default CPU if ISA).  */
808 };
809
810 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
811 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
812 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
813 \f
814 /* Pseudo-op table.
815
816    The following pseudo-ops from the Kane and Heinrich MIPS book
817    should be defined here, but are currently unsupported: .alias,
818    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
819
820    The following pseudo-ops from the Kane and Heinrich MIPS book are
821    specific to the type of debugging information being generated, and
822    should be defined by the object format: .aent, .begin, .bend,
823    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
824    .vreg.
825
826    The following pseudo-ops from the Kane and Heinrich MIPS book are
827    not MIPS CPU specific, but are also not specific to the object file
828    format.  This file is probably the best place to define them, but
829    they are not currently supported: .asm0, .endr, .lab, .repeat,
830    .struct.  */
831
832 static const pseudo_typeS mips_pseudo_table[] =
833 {
834   /* MIPS specific pseudo-ops.  */
835   {"option", s_option, 0},
836   {"set", s_mipsset, 0},
837   {"rdata", s_change_sec, 'r'},
838   {"sdata", s_change_sec, 's'},
839   {"livereg", s_ignore, 0},
840   {"abicalls", s_abicalls, 0},
841   {"cpload", s_cpload, 0},
842   {"cpsetup", s_cpsetup, 0},
843   {"cplocal", s_cplocal, 0},
844   {"cprestore", s_cprestore, 0},
845   {"cpreturn", s_cpreturn, 0},
846   {"gpvalue", s_gpvalue, 0},
847   {"gpword", s_gpword, 0},
848   {"cpadd", s_cpadd, 0},
849   {"insn", s_insn, 0},
850
851   /* Relatively generic pseudo-ops that happen to be used on MIPS
852      chips.  */
853   {"asciiz", stringer, 1},
854   {"bss", s_change_sec, 'b'},
855   {"err", s_err, 0},
856   {"half", s_cons, 1},
857   {"dword", s_cons, 3},
858   {"weakext", s_mips_weakext, 0},
859
860   /* These pseudo-ops are defined in read.c, but must be overridden
861      here for one reason or another.  */
862   {"align", s_align, 0},
863   {"byte", s_cons, 0},
864   {"data", s_change_sec, 'd'},
865   {"double", s_float_cons, 'd'},
866   {"float", s_float_cons, 'f'},
867   {"globl", s_mips_globl, 0},
868   {"global", s_mips_globl, 0},
869   {"hword", s_cons, 1},
870   {"int", s_cons, 2},
871   {"long", s_cons, 2},
872   {"octa", s_cons, 4},
873   {"quad", s_cons, 3},
874   {"short", s_cons, 1},
875   {"single", s_float_cons, 'f'},
876   {"stabn", s_mips_stab, 'n'},
877   {"text", s_change_sec, 't'},
878   {"word", s_cons, 2},
879
880 #ifdef MIPS_STABS_ELF
881   { "extern", ecoff_directive_extern, 0},
882 #endif
883
884   { NULL, NULL, 0 },
885 };
886
887 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
888 {
889   /* These pseudo-ops should be defined by the object file format.
890      However, a.out doesn't support them, so we have versions here.  */
891   {"aent", s_mips_ent, 1},
892   {"bgnb", s_ignore, 0},
893   {"end", s_mips_end, 0},
894   {"endb", s_ignore, 0},
895   {"ent", s_mips_ent, 0},
896   {"file", s_file, 0},
897   {"fmask", s_mips_mask, 'F'},
898   {"frame", s_mips_frame, 0},
899   {"loc", s_ignore, 0},
900   {"mask", s_mips_mask, 'R'},
901   {"verstamp", s_ignore, 0},
902   { NULL, NULL, 0 },
903 };
904
905 extern void pop_insert PARAMS ((const pseudo_typeS *));
906
907 void
908 mips_pop_insert ()
909 {
910   pop_insert (mips_pseudo_table);
911   if (! ECOFF_DEBUGGING)
912     pop_insert (mips_nonecoff_pseudo_table);
913 }
914 \f
915 /* Symbols labelling the current insn.  */
916
917 struct insn_label_list
918 {
919   struct insn_label_list *next;
920   symbolS *label;
921 };
922
923 static struct insn_label_list *insn_labels;
924 static struct insn_label_list *free_insn_labels;
925
926 static void mips_clear_insn_labels PARAMS ((void));
927
928 static inline void
929 mips_clear_insn_labels ()
930 {
931   register struct insn_label_list **pl;
932
933   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
934     ;
935   *pl = insn_labels;
936   insn_labels = NULL;
937 }
938 \f
939 static char *expr_end;
940
941 /* Expressions which appear in instructions.  These are set by
942    mips_ip.  */
943
944 static expressionS imm_expr;
945 static expressionS offset_expr;
946
947 /* Relocs associated with imm_expr and offset_expr.  */
948
949 static bfd_reloc_code_real_type imm_reloc[3]
950   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
951 static bfd_reloc_code_real_type offset_reloc[3]
952   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
953
954 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
955
956 static boolean imm_unmatched_hi;
957
958 /* These are set by mips16_ip if an explicit extension is used.  */
959
960 static boolean mips16_small, mips16_ext;
961
962 #ifdef MIPS_STABS_ELF
963 /* The pdr segment for per procedure frame/regmask info */
964
965 static segT pdr_seg;
966 #endif
967
968 static const char *
969 mips_isa_to_str (isa)
970      int isa;
971 {
972   const struct mips_cpu_info *ci;
973   static char s[20];
974
975   ci = mips_cpu_info_from_isa (isa);
976   if (ci != NULL)
977     return (ci->name);
978
979   sprintf (s, "ISA#%d", isa);
980   return s;
981 }
982
983 static const char *
984 mips_cpu_to_str (cpu)
985      int cpu;
986 {
987   const struct mips_cpu_info *ci;
988   static char s[16];
989
990   ci = mips_cpu_info_from_cpu (cpu);
991   if (ci != NULL)
992     return (ci->name);
993
994   sprintf (s, "CPU#%d", cpu);
995   return s;
996 }
997
998 /* The default target format to use.  */
999
1000 const char *
1001 mips_target_format ()
1002 {
1003   switch (OUTPUT_FLAVOR)
1004     {
1005     case bfd_target_aout_flavour:
1006       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1007     case bfd_target_ecoff_flavour:
1008       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1009     case bfd_target_coff_flavour:
1010       return "pe-mips";
1011     case bfd_target_elf_flavour:
1012 #ifdef TE_TMIPS
1013       /* This is traditional mips */
1014       return (target_big_endian
1015               ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
1016                  : "elf32-tradbigmips")
1017               : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
1018                  : "elf32-tradlittlemips"));
1019 #else
1020       return (target_big_endian
1021               ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
1022               : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1023                  : "elf32-littlemips"));
1024 #endif
1025     default:
1026       abort ();
1027       return NULL;
1028     }
1029 }
1030
1031 /* This function is called once, at assembler startup time.  It should
1032    set up all the tables, etc. that the MD part of the assembler will need.  */
1033
1034 void
1035 md_begin ()
1036 {
1037   register const char *retval = NULL;
1038   int i = 0;
1039   const char *cpu;
1040   char *a = NULL;
1041   int broken = 0;
1042   int mips_isa_from_cpu;
1043   int target_cpu_had_mips16 = 0;
1044   const struct mips_cpu_info *ci;
1045
1046   /* GP relative stuff not working for PE */
1047   if (strncmp (TARGET_OS, "pe", 2) == 0
1048       && g_switch_value != 0)
1049     {
1050       if (g_switch_seen)
1051         as_bad (_("-G not supported in this configuration."));
1052       g_switch_value = 0;
1053     }
1054
1055   cpu = TARGET_CPU;
1056   if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1057     {
1058       a = xmalloc (sizeof TARGET_CPU);
1059       strcpy (a, TARGET_CPU);
1060       a[(sizeof TARGET_CPU) - 3] = '\0';
1061       cpu = a;
1062     }
1063
1064   if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
1065     {
1066       target_cpu_had_mips16 = 1;
1067       cpu += sizeof "mips16" - 1;
1068     }
1069
1070   if (mips_opts.mips16 < 0)
1071     mips_opts.mips16 = target_cpu_had_mips16;
1072
1073   /* Backward compatibility for historic -mcpu= option.  Check for
1074      incompatible options, warn if -mcpu is used.  */
1075   if (mips_cpu != CPU_UNKNOWN
1076       && mips_arch != CPU_UNKNOWN
1077       && mips_cpu != mips_arch)
1078     {
1079       as_fatal (_("The -mcpu option can't be used together with -march. "
1080                   "Use -mtune instead of -mcpu."));
1081     }
1082
1083   if (mips_cpu != CPU_UNKNOWN
1084       && mips_tune != CPU_UNKNOWN
1085       && mips_cpu != mips_tune)
1086     {
1087       as_fatal (_("The -mcpu option can't be used together with -mtune. "
1088                   "Use -march instead of -mcpu."));
1089     }
1090
1091 #if 1
1092   /* For backward compatibility, let -mipsN set various defaults.  */
1093   /* This code should go away, to be replaced with something rather more
1094      draconian.  Until GCC 3.1 has been released for some reasonable
1095      amount of time, however, we need to support this.  */
1096   if (mips_opts.isa != ISA_UNKNOWN)
1097     {
1098       /* Translate -mipsN to the appropriate settings of file_mips_gp32
1099          and file_mips_fp32.  Tag binaries as using the mipsN ISA.  */
1100       if (file_mips_gp32 < 0)
1101         {
1102           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1103             file_mips_gp32 = 0;
1104           else
1105             file_mips_gp32 = 1;
1106         }
1107       if (file_mips_fp32 < 0)
1108         {
1109           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1110             file_mips_fp32 = 0;
1111           else
1112             file_mips_fp32 = 1;
1113         }
1114
1115       ci = mips_cpu_info_from_isa (mips_opts.isa);
1116       assert (ci != NULL);
1117       /* -mipsN has higher priority than -mcpu but lower than -march.  */
1118       if (mips_arch == CPU_UNKNOWN)
1119         mips_arch = ci->cpu;
1120
1121       /* Default mips_abi.  */
1122       if (mips_opts.abi == NO_ABI)
1123         {
1124           if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
1125             mips_opts.abi = O32_ABI;
1126           else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
1127             mips_opts.abi = O64_ABI;
1128         }
1129     }
1130
1131   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1132     {
1133       ci = mips_cpu_info_from_cpu (mips_cpu);
1134       assert (ci != NULL);
1135       mips_arch = ci->cpu;
1136       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
1137                  "-mtune instead."));
1138     }
1139
1140   /* Set tune from -mcpu, not from -mipsN.  */
1141   if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1142     {
1143       ci = mips_cpu_info_from_cpu (mips_cpu);
1144       assert (ci != NULL);
1145       mips_tune = ci->cpu;
1146     }
1147
1148   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1149      specified on the command line, or some other value if one was.
1150      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1151      the command line, or will be set otherwise if one was.  */
1152
1153   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1154     /* Handled above.  */;
1155 #else
1156   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1157     {
1158       ci = mips_cpu_info_from_cpu (mips_cpu);
1159       assert (ci != NULL);
1160       mips_arch = ci->cpu;
1161       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
1162                  "-mtune instead."));
1163     }
1164
1165   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1166      specified on the command line, or some other value if one was.
1167      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1168      the command line, or will be set otherwise if one was.  */
1169
1170   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1171     {
1172       /* We have to check if the isa is the default isa of arch.  Otherwise
1173          we'll get invalid object file headers.  */
1174       ci = mips_cpu_info_from_cpu (mips_arch);
1175       assert (ci != NULL);
1176       if (mips_opts.isa != ci->isa)
1177         {
1178           /* This really should be an error instead of a warning, but old
1179              compilers only have -mcpu which sets both arch and tune.  For
1180              now, we discard arch and preserve tune.  */
1181           as_warn (_("The -march option is incompatible to -mipsN and "
1182                      "therefore ignored."));
1183           if (mips_tune == CPU_UNKNOWN)
1184             mips_tune = mips_arch;
1185           ci = mips_cpu_info_from_isa (mips_opts.isa);
1186           assert (ci != NULL);
1187           mips_arch = ci->cpu;
1188         }
1189     }
1190 #endif
1191   else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
1192     {
1193       /* We have ARCH, we need ISA.  */
1194       ci = mips_cpu_info_from_cpu (mips_arch);
1195       assert (ci != NULL);
1196       mips_opts.isa = ci->isa;
1197     }
1198   else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1199     {
1200       /* We have ISA, we need default ARCH.  */
1201       ci = mips_cpu_info_from_isa (mips_opts.isa);
1202       assert (ci != NULL);
1203       mips_arch = ci->cpu;
1204     }
1205   else
1206     {
1207       /* We need to set both ISA and ARCH from target cpu.  */
1208       ci = mips_cpu_info_from_name (cpu);
1209       if (ci == NULL)
1210         ci = mips_cpu_info_from_cpu (CPU_R3000);
1211       assert (ci != NULL);
1212       mips_opts.isa = ci->isa;
1213       mips_arch = ci->cpu;
1214     }
1215
1216   if (mips_tune == CPU_UNKNOWN)
1217     mips_tune = mips_arch;
1218
1219   ci = mips_cpu_info_from_cpu (mips_arch);
1220   assert (ci != NULL);
1221   mips_isa_from_cpu = ci->isa;
1222
1223   /* End of TARGET_CPU processing, get rid of malloced memory
1224      if necessary.  */
1225   cpu = NULL;
1226   if (a != NULL)
1227     {
1228       free (a);
1229       a = NULL;
1230     }
1231
1232   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
1233     as_bad (_("trap exception not supported at ISA 1"));
1234
1235   /* Set the EABI kind based on the ISA before the user gets
1236      to change the ISA with directives.  This isn't really
1237      the best, but then neither is basing the abi on the isa.  */
1238   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
1239       && mips_opts.abi == EABI_ABI)
1240     mips_eabi64 = 1;
1241
1242   /* If they asked for mips1 or mips2 and a cpu that is
1243      mips3 or greater, then mark the object file 32BITMODE.  */
1244   if (mips_isa_from_cpu != ISA_UNKNOWN
1245       && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1246       && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1247     mips_32bitmode = 1;
1248
1249   /* If the selected architecture includes support for ASEs, enable
1250      generation of code for them.  */
1251   if (mips_opts.ase_mips3d == -1 && CPU_HAS_MIPS3D (mips_arch))
1252     mips_opts.ase_mips3d = 1;
1253   if (mips_opts.ase_mdmx == -1 && CPU_HAS_MDMX (mips_arch))
1254     mips_opts.ase_mdmx = 1;
1255
1256   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1257     as_warn (_("Could not set architecture and machine"));
1258
1259   if (file_mips_gp32 < 0)
1260     file_mips_gp32 = 0;
1261   if (file_mips_fp32 < 0)
1262     file_mips_fp32 = 0;
1263
1264   file_mips_isa = mips_opts.isa;
1265   file_mips_abi = mips_opts.abi;
1266   file_ase_mips3d = mips_opts.ase_mips3d;
1267   file_ase_mdmx = mips_opts.ase_mdmx;
1268   mips_opts.gp32 = file_mips_gp32;
1269   mips_opts.fp32 = file_mips_fp32;
1270
1271   if (HAVE_NEWABI)
1272     mips_big_got = 1;
1273
1274   op_hash = hash_new ();
1275
1276   for (i = 0; i < NUMOPCODES;)
1277     {
1278       const char *name = mips_opcodes[i].name;
1279
1280       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1281       if (retval != NULL)
1282         {
1283           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1284                    mips_opcodes[i].name, retval);
1285           /* Probably a memory allocation problem?  Give up now.  */
1286           as_fatal (_("Broken assembler.  No assembly attempted."));
1287         }
1288       do
1289         {
1290           if (mips_opcodes[i].pinfo != INSN_MACRO)
1291             {
1292               if (!validate_mips_insn (&mips_opcodes[i]))
1293                 broken = 1;
1294             }
1295           ++i;
1296         }
1297       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1298     }
1299
1300   mips16_op_hash = hash_new ();
1301
1302   i = 0;
1303   while (i < bfd_mips16_num_opcodes)
1304     {
1305       const char *name = mips16_opcodes[i].name;
1306
1307       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1308       if (retval != NULL)
1309         as_fatal (_("internal: can't hash `%s': %s"),
1310                   mips16_opcodes[i].name, retval);
1311       do
1312         {
1313           if (mips16_opcodes[i].pinfo != INSN_MACRO
1314               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1315                   != mips16_opcodes[i].match))
1316             {
1317               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1318                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1319               broken = 1;
1320             }
1321           ++i;
1322         }
1323       while (i < bfd_mips16_num_opcodes
1324              && strcmp (mips16_opcodes[i].name, name) == 0);
1325     }
1326
1327   if (broken)
1328     as_fatal (_("Broken assembler.  No assembly attempted."));
1329
1330   /* We add all the general register names to the symbol table.  This
1331      helps us detect invalid uses of them.  */
1332   for (i = 0; i < 32; i++)
1333     {
1334       char buf[5];
1335
1336       sprintf (buf, "$%d", i);
1337       symbol_table_insert (symbol_new (buf, reg_section, i,
1338                                        &zero_address_frag));
1339     }
1340   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1341                                    &zero_address_frag));
1342   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1343                                    &zero_address_frag));
1344   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1345                                    &zero_address_frag));
1346   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1347                                    &zero_address_frag));
1348   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1349                                    &zero_address_frag));
1350   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1351                                    &zero_address_frag));
1352   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1353                                    &zero_address_frag));
1354   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1355                                    &zero_address_frag));
1356
1357   mips_no_prev_insn (false);
1358
1359   mips_gprmask = 0;
1360   mips_cprmask[0] = 0;
1361   mips_cprmask[1] = 0;
1362   mips_cprmask[2] = 0;
1363   mips_cprmask[3] = 0;
1364
1365   /* set the default alignment for the text section (2**2) */
1366   record_alignment (text_section, 2);
1367
1368   if (USE_GLOBAL_POINTER_OPT)
1369     bfd_set_gp_size (stdoutput, g_switch_value);
1370
1371   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1372     {
1373       /* On a native system, sections must be aligned to 16 byte
1374          boundaries.  When configured for an embedded ELF target, we
1375          don't bother.  */
1376       if (strcmp (TARGET_OS, "elf") != 0)
1377         {
1378           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1379           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1380           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1381         }
1382
1383       /* Create a .reginfo section for register masks and a .mdebug
1384          section for debugging information.  */
1385       {
1386         segT seg;
1387         subsegT subseg;
1388         flagword flags;
1389         segT sec;
1390
1391         seg = now_seg;
1392         subseg = now_subseg;
1393
1394         /* The ABI says this section should be loaded so that the
1395            running program can access it.  However, we don't load it
1396            if we are configured for an embedded target */
1397         flags = SEC_READONLY | SEC_DATA;
1398         if (strcmp (TARGET_OS, "elf") != 0)
1399           flags |= SEC_ALLOC | SEC_LOAD;
1400
1401         if (file_mips_abi != N64_ABI)
1402           {
1403             sec = subseg_new (".reginfo", (subsegT) 0);
1404
1405             bfd_set_section_flags (stdoutput, sec, flags);
1406             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1407
1408 #ifdef OBJ_ELF
1409             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1410 #endif
1411           }
1412         else
1413           {
1414             /* The 64-bit ABI uses a .MIPS.options section rather than
1415                .reginfo section.  */
1416             sec = subseg_new (".MIPS.options", (subsegT) 0);
1417             bfd_set_section_flags (stdoutput, sec, flags);
1418             bfd_set_section_alignment (stdoutput, sec, 3);
1419
1420 #ifdef OBJ_ELF
1421             /* Set up the option header.  */
1422             {
1423               Elf_Internal_Options opthdr;
1424               char *f;
1425
1426               opthdr.kind = ODK_REGINFO;
1427               opthdr.size = (sizeof (Elf_External_Options)
1428                              + sizeof (Elf64_External_RegInfo));
1429               opthdr.section = 0;
1430               opthdr.info = 0;
1431               f = frag_more (sizeof (Elf_External_Options));
1432               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1433                                              (Elf_External_Options *) f);
1434
1435               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1436             }
1437 #endif
1438           }
1439
1440         if (ECOFF_DEBUGGING)
1441           {
1442             sec = subseg_new (".mdebug", (subsegT) 0);
1443             (void) bfd_set_section_flags (stdoutput, sec,
1444                                           SEC_HAS_CONTENTS | SEC_READONLY);
1445             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1446           }
1447
1448 #ifdef MIPS_STABS_ELF
1449         pdr_seg = subseg_new (".pdr", (subsegT) 0);
1450         (void) bfd_set_section_flags (stdoutput, pdr_seg,
1451                              SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1452         (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1453 #endif
1454
1455         subseg_set (seg, subseg);
1456       }
1457     }
1458
1459   if (! ECOFF_DEBUGGING)
1460     md_obj_begin ();
1461 }
1462
1463 void
1464 md_mips_end ()
1465 {
1466   if (! ECOFF_DEBUGGING)
1467     md_obj_end ();
1468 }
1469
1470 void
1471 md_assemble (str)
1472      char *str;
1473 {
1474   struct mips_cl_insn insn;
1475   bfd_reloc_code_real_type unused_reloc[3]
1476     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1477
1478   imm_expr.X_op = O_absent;
1479   imm_unmatched_hi = false;
1480   offset_expr.X_op = O_absent;
1481   imm_reloc[0] = BFD_RELOC_UNUSED;
1482   imm_reloc[1] = BFD_RELOC_UNUSED;
1483   imm_reloc[2] = BFD_RELOC_UNUSED;
1484   offset_reloc[0] = BFD_RELOC_UNUSED;
1485   offset_reloc[1] = BFD_RELOC_UNUSED;
1486   offset_reloc[2] = BFD_RELOC_UNUSED;
1487
1488   if (mips_opts.mips16)
1489     mips16_ip (str, &insn);
1490   else
1491     {
1492       mips_ip (str, &insn);
1493       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1494             str, insn.insn_opcode));
1495     }
1496
1497   if (insn_error)
1498     {
1499       as_bad ("%s `%s'", insn_error, str);
1500       return;
1501     }
1502
1503   if (insn.insn_mo->pinfo == INSN_MACRO)
1504     {
1505       if (mips_opts.mips16)
1506         mips16_macro (&insn);
1507       else
1508         macro (&insn);
1509     }
1510   else
1511     {
1512       if (imm_expr.X_op != O_absent)
1513         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1514       else if (offset_expr.X_op != O_absent)
1515         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1516       else
1517         append_insn (NULL, &insn, NULL, unused_reloc, false);
1518     }
1519 }
1520
1521 /* See whether instruction IP reads register REG.  CLASS is the type
1522    of register.  */
1523
1524 static int
1525 insn_uses_reg (ip, reg, class)
1526      struct mips_cl_insn *ip;
1527      unsigned int reg;
1528      enum mips_regclass class;
1529 {
1530   if (class == MIPS16_REG)
1531     {
1532       assert (mips_opts.mips16);
1533       reg = mips16_to_32_reg_map[reg];
1534       class = MIPS_GR_REG;
1535     }
1536
1537   /* Don't report on general register 0, since it never changes.  */
1538   if (class == MIPS_GR_REG && reg == 0)
1539     return 0;
1540
1541   if (class == MIPS_FP_REG)
1542     {
1543       assert (! mips_opts.mips16);
1544       /* If we are called with either $f0 or $f1, we must check $f0.
1545          This is not optimal, because it will introduce an unnecessary
1546          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1547          need to distinguish reading both $f0 and $f1 or just one of
1548          them.  Note that we don't have to check the other way,
1549          because there is no instruction that sets both $f0 and $f1
1550          and requires a delay.  */
1551       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1552           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1553               == (reg &~ (unsigned) 1)))
1554         return 1;
1555       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1556           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1557               == (reg &~ (unsigned) 1)))
1558         return 1;
1559     }
1560   else if (! mips_opts.mips16)
1561     {
1562       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1563           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1564         return 1;
1565       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1566           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1567         return 1;
1568     }
1569   else
1570     {
1571       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1572           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1573                                     & MIPS16OP_MASK_RX)]
1574               == reg))
1575         return 1;
1576       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1577           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1578                                     & MIPS16OP_MASK_RY)]
1579               == reg))
1580         return 1;
1581       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1582           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1583                                     & MIPS16OP_MASK_MOVE32Z)]
1584               == reg))
1585         return 1;
1586       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1587         return 1;
1588       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1589         return 1;
1590       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1591         return 1;
1592       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1593           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1594               & MIPS16OP_MASK_REGR32) == reg)
1595         return 1;
1596     }
1597
1598   return 0;
1599 }
1600
1601 /* This function returns true if modifying a register requires a
1602    delay.  */
1603
1604 static int
1605 reg_needs_delay (reg)
1606      unsigned int reg;
1607 {
1608   unsigned long prev_pinfo;
1609
1610   prev_pinfo = prev_insn.insn_mo->pinfo;
1611   if (! mips_opts.noreorder
1612       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1613       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1614           || (! gpr_interlocks
1615               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1616     {
1617       /* A load from a coprocessor or from memory.  All load
1618          delays delay the use of general register rt for one
1619          instruction on the r3000.  The r6000 and r4000 use
1620          interlocks.  */
1621       /* Itbl support may require additional care here.  */
1622       know (prev_pinfo & INSN_WRITE_GPR_T);
1623       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1624         return 1;
1625     }
1626
1627   return 0;
1628 }
1629
1630 /* Mark instruction labels in mips16 mode.  This permits the linker to
1631    handle them specially, such as generating jalx instructions when
1632    needed.  We also make them odd for the duration of the assembly, in
1633    order to generate the right sort of code.  We will make them even
1634    in the adjust_symtab routine, while leaving them marked.  This is
1635    convenient for the debugger and the disassembler.  The linker knows
1636    to make them odd again.  */
1637
1638 static void
1639 mips16_mark_labels ()
1640 {
1641   if (mips_opts.mips16)
1642     {
1643       struct insn_label_list *l;
1644       valueT val;
1645
1646       for (l = insn_labels; l != NULL; l = l->next)
1647         {
1648 #ifdef OBJ_ELF
1649           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1650             S_SET_OTHER (l->label, STO_MIPS16);
1651 #endif
1652           val = S_GET_VALUE (l->label);
1653           if ((val & 1) == 0)
1654             S_SET_VALUE (l->label, val + 1);
1655         }
1656     }
1657 }
1658
1659 /* Output an instruction.  PLACE is where to put the instruction; if
1660    it is NULL, this uses frag_more to get room.  IP is the instruction
1661    information.  ADDRESS_EXPR is an operand of the instruction to be
1662    used with RELOC_TYPE.  */
1663
1664 static void
1665 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1666      char *place;
1667      struct mips_cl_insn *ip;
1668      expressionS *address_expr;
1669      bfd_reloc_code_real_type *reloc_type;
1670      boolean unmatched_hi;
1671 {
1672   register unsigned long prev_pinfo, pinfo;
1673   char *f;
1674   fixS *fixp[3];
1675   int nops = 0;
1676
1677   /* Mark instruction labels in mips16 mode.  */
1678   mips16_mark_labels ();
1679
1680   prev_pinfo = prev_insn.insn_mo->pinfo;
1681   pinfo = ip->insn_mo->pinfo;
1682
1683   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1684     {
1685       int prev_prev_nop;
1686
1687       /* If the previous insn required any delay slots, see if we need
1688          to insert a NOP or two.  There are eight kinds of possible
1689          hazards, of which an instruction can have at most one type.
1690          (1) a load from memory delay
1691          (2) a load from a coprocessor delay
1692          (3) an unconditional branch delay
1693          (4) a conditional branch delay
1694          (5) a move to coprocessor register delay
1695          (6) a load coprocessor register from memory delay
1696          (7) a coprocessor condition code delay
1697          (8) a HI/LO special register delay
1698
1699          There are a lot of optimizations we could do that we don't.
1700          In particular, we do not, in general, reorder instructions.
1701          If you use gcc with optimization, it will reorder
1702          instructions and generally do much more optimization then we
1703          do here; repeating all that work in the assembler would only
1704          benefit hand written assembly code, and does not seem worth
1705          it.  */
1706
1707       /* This is how a NOP is emitted.  */
1708 #define emit_nop()                                      \
1709   (mips_opts.mips16                                     \
1710    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1711    : md_number_to_chars (frag_more (4), 0, 4))
1712
1713       /* The previous insn might require a delay slot, depending upon
1714          the contents of the current insn.  */
1715       if (! mips_opts.mips16
1716           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1717           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1718                && ! cop_interlocks)
1719               || (! gpr_interlocks
1720                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1721         {
1722           /* A load from a coprocessor or from memory.  All load
1723              delays delay the use of general register rt for one
1724              instruction on the r3000.  The r6000 and r4000 use
1725              interlocks.  */
1726           /* Itbl support may require additional care here.  */
1727           know (prev_pinfo & INSN_WRITE_GPR_T);
1728           if (mips_optimize == 0
1729               || insn_uses_reg (ip,
1730                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1731                                  & OP_MASK_RT),
1732                                 MIPS_GR_REG))
1733             ++nops;
1734         }
1735       else if (! mips_opts.mips16
1736                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1737                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1738                     && ! cop_interlocks)
1739                    || (mips_opts.isa == ISA_MIPS1
1740                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1741         {
1742           /* A generic coprocessor delay.  The previous instruction
1743              modified a coprocessor general or control register.  If
1744              it modified a control register, we need to avoid any
1745              coprocessor instruction (this is probably not always
1746              required, but it sometimes is).  If it modified a general
1747              register, we avoid using that register.
1748
1749              On the r6000 and r4000 loading a coprocessor register
1750              from memory is interlocked, and does not require a delay.
1751
1752              This case is not handled very well.  There is no special
1753              knowledge of CP0 handling, and the coprocessors other
1754              than the floating point unit are not distinguished at
1755              all.  */
1756           /* Itbl support may require additional care here. FIXME!
1757              Need to modify this to include knowledge about
1758              user specified delays!  */
1759           if (prev_pinfo & INSN_WRITE_FPR_T)
1760             {
1761               if (mips_optimize == 0
1762                   || insn_uses_reg (ip,
1763                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1764                                      & OP_MASK_FT),
1765                                     MIPS_FP_REG))
1766                 ++nops;
1767             }
1768           else if (prev_pinfo & INSN_WRITE_FPR_S)
1769             {
1770               if (mips_optimize == 0
1771                   || insn_uses_reg (ip,
1772                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1773                                      & OP_MASK_FS),
1774                                     MIPS_FP_REG))
1775                 ++nops;
1776             }
1777           else
1778             {
1779               /* We don't know exactly what the previous instruction
1780                  does.  If the current instruction uses a coprocessor
1781                  register, we must insert a NOP.  If previous
1782                  instruction may set the condition codes, and the
1783                  current instruction uses them, we must insert two
1784                  NOPS.  */
1785               /* Itbl support may require additional care here.  */
1786               if (mips_optimize == 0
1787                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1788                       && (pinfo & INSN_READ_COND_CODE)))
1789                 nops += 2;
1790               else if (pinfo & INSN_COP)
1791                 ++nops;
1792             }
1793         }
1794       else if (! mips_opts.mips16
1795                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1796                && (prev_pinfo & INSN_WRITE_COND_CODE)
1797                && ! cop_interlocks)
1798         {
1799           /* The previous instruction sets the coprocessor condition
1800              codes, but does not require a general coprocessor delay
1801              (this means it is a floating point comparison
1802              instruction).  If this instruction uses the condition
1803              codes, we need to insert a single NOP.  */
1804           /* Itbl support may require additional care here.  */
1805           if (mips_optimize == 0
1806               || (pinfo & INSN_READ_COND_CODE))
1807             ++nops;
1808         }
1809
1810       /* If we're fixing up mfhi/mflo for the r7000 and the
1811          previous insn was an mfhi/mflo and the current insn
1812          reads the register that the mfhi/mflo wrote to, then
1813          insert two nops.  */
1814
1815       else if (mips_7000_hilo_fix
1816                && MF_HILO_INSN (prev_pinfo)
1817                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1818                                       & OP_MASK_RD),
1819                                  MIPS_GR_REG))
1820         {
1821           nops += 2;
1822         }
1823
1824       /* If we're fixing up mfhi/mflo for the r7000 and the
1825          2nd previous insn was an mfhi/mflo and the current insn
1826          reads the register that the mfhi/mflo wrote to, then
1827          insert one nop.  */
1828
1829       else if (mips_7000_hilo_fix
1830                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1831                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1832                                        & OP_MASK_RD),
1833                                     MIPS_GR_REG))
1834
1835         {
1836           ++nops;
1837         }
1838
1839       else if (prev_pinfo & INSN_READ_LO)
1840         {
1841           /* The previous instruction reads the LO register; if the
1842              current instruction writes to the LO register, we must
1843              insert two NOPS.  Some newer processors have interlocks.
1844              Also the tx39's multiply instructions can be exectuted
1845              immediatly after a read from HI/LO (without the delay),
1846              though the tx39's divide insns still do require the
1847              delay.  */
1848           if (! (hilo_interlocks
1849                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1850               && (mips_optimize == 0
1851                   || (pinfo & INSN_WRITE_LO)))
1852             nops += 2;
1853           /* Most mips16 branch insns don't have a delay slot.
1854              If a read from LO is immediately followed by a branch
1855              to a write to LO we have a read followed by a write
1856              less than 2 insns away.  We assume the target of
1857              a branch might be a write to LO, and insert a nop
1858              between a read and an immediately following branch.  */
1859           else if (mips_opts.mips16
1860                    && (mips_optimize == 0
1861                        || (pinfo & MIPS16_INSN_BRANCH)))
1862             ++nops;
1863         }
1864       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1865         {
1866           /* The previous instruction reads the HI register; if the
1867              current instruction writes to the HI register, we must
1868              insert a NOP.  Some newer processors have interlocks.
1869              Also the note tx39's multiply above.  */
1870           if (! (hilo_interlocks
1871                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1872               && (mips_optimize == 0
1873                   || (pinfo & INSN_WRITE_HI)))
1874             nops += 2;
1875           /* Most mips16 branch insns don't have a delay slot.
1876              If a read from HI is immediately followed by a branch
1877              to a write to HI we have a read followed by a write
1878              less than 2 insns away.  We assume the target of
1879              a branch might be a write to HI, and insert a nop
1880              between a read and an immediately following branch.  */
1881           else if (mips_opts.mips16
1882                    && (mips_optimize == 0
1883                        || (pinfo & MIPS16_INSN_BRANCH)))
1884             ++nops;
1885         }
1886
1887       /* If the previous instruction was in a noreorder section, then
1888          we don't want to insert the nop after all.  */
1889       /* Itbl support may require additional care here.  */
1890       if (prev_insn_unreordered)
1891         nops = 0;
1892
1893       /* There are two cases which require two intervening
1894          instructions: 1) setting the condition codes using a move to
1895          coprocessor instruction which requires a general coprocessor
1896          delay and then reading the condition codes 2) reading the HI
1897          or LO register and then writing to it (except on processors
1898          which have interlocks).  If we are not already emitting a NOP
1899          instruction, we must check for these cases compared to the
1900          instruction previous to the previous instruction.  */
1901       if ((! mips_opts.mips16
1902            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1903            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1904            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1905            && (pinfo & INSN_READ_COND_CODE)
1906            && ! cop_interlocks)
1907           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1908               && (pinfo & INSN_WRITE_LO)
1909               && ! (hilo_interlocks
1910                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1911           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1912               && (pinfo & INSN_WRITE_HI)
1913               && ! (hilo_interlocks
1914                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1915         prev_prev_nop = 1;
1916       else
1917         prev_prev_nop = 0;
1918
1919       if (prev_prev_insn_unreordered)
1920         prev_prev_nop = 0;
1921
1922       if (prev_prev_nop && nops == 0)
1923         ++nops;
1924
1925       /* If we are being given a nop instruction, don't bother with
1926          one of the nops we would otherwise output.  This will only
1927          happen when a nop instruction is used with mips_optimize set
1928          to 0.  */
1929       if (nops > 0
1930           && ! mips_opts.noreorder
1931           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1932         --nops;
1933
1934       /* Now emit the right number of NOP instructions.  */
1935       if (nops > 0 && ! mips_opts.noreorder)
1936         {
1937           fragS *old_frag;
1938           unsigned long old_frag_offset;
1939           int i;
1940           struct insn_label_list *l;
1941
1942           old_frag = frag_now;
1943           old_frag_offset = frag_now_fix ();
1944
1945           for (i = 0; i < nops; i++)
1946             emit_nop ();
1947
1948           if (listing)
1949             {
1950               listing_prev_line ();
1951               /* We may be at the start of a variant frag.  In case we
1952                  are, make sure there is enough space for the frag
1953                  after the frags created by listing_prev_line.  The
1954                  argument to frag_grow here must be at least as large
1955                  as the argument to all other calls to frag_grow in
1956                  this file.  We don't have to worry about being in the
1957                  middle of a variant frag, because the variants insert
1958                  all needed nop instructions themselves.  */
1959               frag_grow (40);
1960             }
1961
1962           for (l = insn_labels; l != NULL; l = l->next)
1963             {
1964               valueT val;
1965
1966               assert (S_GET_SEGMENT (l->label) == now_seg);
1967               symbol_set_frag (l->label, frag_now);
1968               val = (valueT) frag_now_fix ();
1969               /* mips16 text labels are stored as odd.  */
1970               if (mips_opts.mips16)
1971                 ++val;
1972               S_SET_VALUE (l->label, val);
1973             }
1974
1975 #ifndef NO_ECOFF_DEBUGGING
1976           if (ECOFF_DEBUGGING)
1977             ecoff_fix_loc (old_frag, old_frag_offset);
1978 #endif
1979         }
1980       else if (prev_nop_frag != NULL)
1981         {
1982           /* We have a frag holding nops we may be able to remove.  If
1983              we don't need any nops, we can decrease the size of
1984              prev_nop_frag by the size of one instruction.  If we do
1985              need some nops, we count them in prev_nops_required.  */
1986           if (prev_nop_frag_since == 0)
1987             {
1988               if (nops == 0)
1989                 {
1990                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1991                   --prev_nop_frag_holds;
1992                 }
1993               else
1994                 prev_nop_frag_required += nops;
1995             }
1996           else
1997             {
1998               if (prev_prev_nop == 0)
1999                 {
2000                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2001                   --prev_nop_frag_holds;
2002                 }
2003               else
2004                 ++prev_nop_frag_required;
2005             }
2006
2007           if (prev_nop_frag_holds <= prev_nop_frag_required)
2008             prev_nop_frag = NULL;
2009
2010           ++prev_nop_frag_since;
2011
2012           /* Sanity check: by the time we reach the second instruction
2013              after prev_nop_frag, we should have used up all the nops
2014              one way or another.  */
2015           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2016         }
2017     }
2018
2019   if (*reloc_type > BFD_RELOC_UNUSED)
2020     {
2021       /* We need to set up a variant frag.  */
2022       assert (mips_opts.mips16 && address_expr != NULL);
2023       f = frag_var (rs_machine_dependent, 4, 0,
2024                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2025                                          mips16_small, mips16_ext,
2026                                          (prev_pinfo
2027                                           & INSN_UNCOND_BRANCH_DELAY),
2028                                          (*prev_insn_reloc_type
2029                                           == BFD_RELOC_MIPS16_JMP)),
2030                     make_expr_symbol (address_expr), 0, NULL);
2031     }
2032   else if (place != NULL)
2033     f = place;
2034   else if (mips_opts.mips16
2035            && ! ip->use_extend
2036            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2037     {
2038       /* Make sure there is enough room to swap this instruction with
2039          a following jump instruction.  */
2040       frag_grow (6);
2041       f = frag_more (2);
2042     }
2043   else
2044     {
2045       if (mips_opts.mips16
2046           && mips_opts.noreorder
2047           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2048         as_warn (_("extended instruction in delay slot"));
2049
2050       f = frag_more (4);
2051     }
2052
2053   fixp[0] = fixp[1] = fixp[2] = NULL;
2054   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2055     {
2056       if (address_expr->X_op == O_constant)
2057         {
2058           valueT tmp;
2059
2060           switch (*reloc_type)
2061             {
2062             case BFD_RELOC_32:
2063               ip->insn_opcode |= address_expr->X_add_number;
2064               break;
2065
2066             case BFD_RELOC_MIPS_HIGHEST:
2067               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2068               tmp >>= 16;
2069               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2070               break;
2071
2072             case BFD_RELOC_MIPS_HIGHER:
2073               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2074               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2075               break;
2076
2077             case BFD_RELOC_HI16_S:
2078               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2079                                   >> 16) & 0xffff;
2080               break;
2081
2082             case BFD_RELOC_HI16:
2083               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2084               break;
2085
2086             case BFD_RELOC_LO16:
2087               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2088               break;
2089
2090             case BFD_RELOC_MIPS_JMP:
2091               if ((address_expr->X_add_number & 3) != 0)
2092                 as_bad (_("jump to misaligned address (0x%lx)"),
2093                         (unsigned long) address_expr->X_add_number);
2094               if (address_expr->X_add_number & ~0xfffffff
2095                   || address_expr->X_add_number > 0x7fffffc)
2096                 as_bad (_("jump address range overflow (0x%lx)"),
2097                         (unsigned long) address_expr->X_add_number);
2098               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2099               break;
2100
2101             case BFD_RELOC_MIPS16_JMP:
2102               if ((address_expr->X_add_number & 3) != 0)
2103                 as_bad (_("jump to misaligned address (0x%lx)"),
2104                         (unsigned long) address_expr->X_add_number);
2105               if (address_expr->X_add_number & ~0xfffffff
2106                   || address_expr->X_add_number > 0x7fffffc)
2107                 as_bad (_("jump address range overflow (0x%lx)"),
2108                         (unsigned long) address_expr->X_add_number);
2109               ip->insn_opcode |=
2110                 (((address_expr->X_add_number & 0x7c0000) << 3)
2111                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2112                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2113               break;
2114
2115             case BFD_RELOC_16_PCREL:
2116               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2117               break;
2118
2119             case BFD_RELOC_16_PCREL_S2:
2120               goto need_reloc;
2121
2122             default:
2123               internalError ();
2124             }
2125         }
2126       else
2127         {
2128         need_reloc:
2129           /* Don't generate a reloc if we are writing into a variant frag.  */
2130           if (place == NULL)
2131             {
2132               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2133                                      address_expr,
2134                                      (*reloc_type == BFD_RELOC_16_PCREL
2135                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
2136                                      reloc_type[0]);
2137
2138               /* These relocations can have an addend that won't fit in
2139                  4 octets for 64bit assembly.  */
2140               if (HAVE_64BIT_GPRS &&
2141                   (*reloc_type == BFD_RELOC_16
2142                    || *reloc_type == BFD_RELOC_32
2143                    || *reloc_type == BFD_RELOC_MIPS_JMP
2144                    || *reloc_type == BFD_RELOC_HI16_S
2145                    || *reloc_type == BFD_RELOC_LO16
2146                    || *reloc_type == BFD_RELOC_GPREL16
2147                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2148                    || *reloc_type == BFD_RELOC_GPREL32
2149                    || *reloc_type == BFD_RELOC_64
2150                    || *reloc_type == BFD_RELOC_CTOR
2151                    || *reloc_type == BFD_RELOC_MIPS_SUB
2152                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2153                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2154                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2155                    || *reloc_type == BFD_RELOC_MIPS_REL16
2156                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2157                 fixp[0]->fx_no_overflow = 1;
2158
2159               if (unmatched_hi)
2160                 {
2161                   struct mips_hi_fixup *hi_fixup;
2162
2163                   assert (*reloc_type == BFD_RELOC_HI16_S);
2164                   hi_fixup = ((struct mips_hi_fixup *)
2165                               xmalloc (sizeof (struct mips_hi_fixup)));
2166                   hi_fixup->fixp = fixp[0];
2167                   hi_fixup->seg = now_seg;
2168                   hi_fixup->next = mips_hi_fixup_list;
2169                   mips_hi_fixup_list = hi_fixup;
2170                 }
2171
2172               if (reloc_type[1] != BFD_RELOC_UNUSED)
2173                 {
2174                   /* FIXME: This symbol can be one of
2175                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2176                   address_expr->X_op = O_absent;
2177                   address_expr->X_add_symbol = 0;
2178                   address_expr->X_add_number = 0;
2179
2180                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2181                                          4, address_expr, false,
2182                                          reloc_type[1]);
2183
2184                   /* These relocations can have an addend that won't fit in
2185                      4 octets for 64bit assembly.  */
2186                   if (HAVE_64BIT_GPRS &&
2187                       (*reloc_type == BFD_RELOC_16
2188                        || *reloc_type == BFD_RELOC_32
2189                        || *reloc_type == BFD_RELOC_MIPS_JMP
2190                        || *reloc_type == BFD_RELOC_HI16_S
2191                        || *reloc_type == BFD_RELOC_LO16
2192                        || *reloc_type == BFD_RELOC_GPREL16
2193                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2194                        || *reloc_type == BFD_RELOC_GPREL32
2195                        || *reloc_type == BFD_RELOC_64
2196                        || *reloc_type == BFD_RELOC_CTOR
2197                        || *reloc_type == BFD_RELOC_MIPS_SUB
2198                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2199                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2200                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2201                        || *reloc_type == BFD_RELOC_MIPS_REL16
2202                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2203                     fixp[1]->fx_no_overflow = 1;
2204
2205                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2206                     {
2207                       address_expr->X_op = O_absent;
2208                       address_expr->X_add_symbol = 0;
2209                       address_expr->X_add_number = 0;
2210
2211                       fixp[2] = fix_new_exp (frag_now,
2212                                              f - frag_now->fr_literal, 4,
2213                                              address_expr, false,
2214                                              reloc_type[2]);
2215
2216                       /* These relocations can have an addend that won't fit in
2217                          4 octets for 64bit assembly.  */
2218                       if (HAVE_64BIT_GPRS &&
2219                           (*reloc_type == BFD_RELOC_16
2220                            || *reloc_type == BFD_RELOC_32
2221                            || *reloc_type == BFD_RELOC_MIPS_JMP
2222                            || *reloc_type == BFD_RELOC_HI16_S
2223                            || *reloc_type == BFD_RELOC_LO16
2224                            || *reloc_type == BFD_RELOC_GPREL16
2225                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2226                            || *reloc_type == BFD_RELOC_GPREL32
2227                            || *reloc_type == BFD_RELOC_64
2228                            || *reloc_type == BFD_RELOC_CTOR
2229                            || *reloc_type == BFD_RELOC_MIPS_SUB
2230                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2231                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2232                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2233                            || *reloc_type == BFD_RELOC_MIPS_REL16
2234                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2235                         fixp[2]->fx_no_overflow = 1;
2236                     }
2237                 }
2238             }
2239         }
2240     }
2241
2242   if (! mips_opts.mips16)
2243     md_number_to_chars (f, ip->insn_opcode, 4);
2244   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2245     {
2246       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2247       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2248     }
2249   else
2250     {
2251       if (ip->use_extend)
2252         {
2253           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2254           f += 2;
2255         }
2256       md_number_to_chars (f, ip->insn_opcode, 2);
2257     }
2258
2259   /* Update the register mask information.  */
2260   if (! mips_opts.mips16)
2261     {
2262       if (pinfo & INSN_WRITE_GPR_D)
2263         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2264       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2265         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2266       if (pinfo & INSN_READ_GPR_S)
2267         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2268       if (pinfo & INSN_WRITE_GPR_31)
2269         mips_gprmask |= 1 << RA;
2270       if (pinfo & INSN_WRITE_FPR_D)
2271         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2272       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2273         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2274       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2275         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2276       if ((pinfo & INSN_READ_FPR_R) != 0)
2277         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2278       if (pinfo & INSN_COP)
2279         {
2280           /* We don't keep enough information to sort these cases out.
2281              The itbl support does keep this information however, although
2282              we currently don't support itbl fprmats as part of the cop
2283              instruction.  May want to add this support in the future.  */
2284         }
2285       /* Never set the bit for $0, which is always zero.  */
2286       mips_gprmask &= ~1 << 0;
2287     }
2288   else
2289     {
2290       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2291         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2292                               & MIPS16OP_MASK_RX);
2293       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2294         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2295                               & MIPS16OP_MASK_RY);
2296       if (pinfo & MIPS16_INSN_WRITE_Z)
2297         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2298                               & MIPS16OP_MASK_RZ);
2299       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2300         mips_gprmask |= 1 << TREG;
2301       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2302         mips_gprmask |= 1 << SP;
2303       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2304         mips_gprmask |= 1 << RA;
2305       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2306         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2307       if (pinfo & MIPS16_INSN_READ_Z)
2308         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2309                               & MIPS16OP_MASK_MOVE32Z);
2310       if (pinfo & MIPS16_INSN_READ_GPR_X)
2311         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2312                               & MIPS16OP_MASK_REGR32);
2313     }
2314
2315   if (place == NULL && ! mips_opts.noreorder)
2316     {
2317       /* Filling the branch delay slot is more complex.  We try to
2318          switch the branch with the previous instruction, which we can
2319          do if the previous instruction does not set up a condition
2320          that the branch tests and if the branch is not itself the
2321          target of any branch.  */
2322       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2323           || (pinfo & INSN_COND_BRANCH_DELAY))
2324         {
2325           if (mips_optimize < 2
2326               /* If we have seen .set volatile or .set nomove, don't
2327                  optimize.  */
2328               || mips_opts.nomove != 0
2329               /* If we had to emit any NOP instructions, then we
2330                  already know we can not swap.  */
2331               || nops != 0
2332               /* If we don't even know the previous insn, we can not
2333                  swap.  */
2334               || ! prev_insn_valid
2335               /* If the previous insn is already in a branch delay
2336                  slot, then we can not swap.  */
2337               || prev_insn_is_delay_slot
2338               /* If the previous previous insn was in a .set
2339                  noreorder, we can't swap.  Actually, the MIPS
2340                  assembler will swap in this situation.  However, gcc
2341                  configured -with-gnu-as will generate code like
2342                    .set noreorder
2343                    lw   $4,XXX
2344                    .set reorder
2345                    INSN
2346                    bne  $4,$0,foo
2347                  in which we can not swap the bne and INSN.  If gcc is
2348                  not configured -with-gnu-as, it does not output the
2349                  .set pseudo-ops.  We don't have to check
2350                  prev_insn_unreordered, because prev_insn_valid will
2351                  be 0 in that case.  We don't want to use
2352                  prev_prev_insn_valid, because we do want to be able
2353                  to swap at the start of a function.  */
2354               || prev_prev_insn_unreordered
2355               /* If the branch is itself the target of a branch, we
2356                  can not swap.  We cheat on this; all we check for is
2357                  whether there is a label on this instruction.  If
2358                  there are any branches to anything other than a
2359                  label, users must use .set noreorder.  */
2360               || insn_labels != NULL
2361               /* If the previous instruction is in a variant frag, we
2362                  can not do the swap.  This does not apply to the
2363                  mips16, which uses variant frags for different
2364                  purposes.  */
2365               || (! mips_opts.mips16
2366                   && prev_insn_frag->fr_type == rs_machine_dependent)
2367               /* If the branch reads the condition codes, we don't
2368                  even try to swap, because in the sequence
2369                    ctc1 $X,$31
2370                    INSN
2371                    INSN
2372                    bc1t LABEL
2373                  we can not swap, and I don't feel like handling that
2374                  case.  */
2375               || (! mips_opts.mips16
2376                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2377                   && (pinfo & INSN_READ_COND_CODE))
2378               /* We can not swap with an instruction that requires a
2379                  delay slot, becase the target of the branch might
2380                  interfere with that instruction.  */
2381               || (! mips_opts.mips16
2382                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2383                   && (prev_pinfo
2384               /* Itbl support may require additional care here.  */
2385                       & (INSN_LOAD_COPROC_DELAY
2386                          | INSN_COPROC_MOVE_DELAY
2387                          | INSN_WRITE_COND_CODE)))
2388               || (! (hilo_interlocks
2389                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2390                   && (prev_pinfo
2391                       & (INSN_READ_LO
2392                          | INSN_READ_HI)))
2393               || (! mips_opts.mips16
2394                   && ! gpr_interlocks
2395                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2396               || (! mips_opts.mips16
2397                   && mips_opts.isa == ISA_MIPS1
2398                   /* Itbl support may require additional care here.  */
2399                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2400               /* We can not swap with a branch instruction.  */
2401               || (prev_pinfo
2402                   & (INSN_UNCOND_BRANCH_DELAY
2403                      | INSN_COND_BRANCH_DELAY
2404                      | INSN_COND_BRANCH_LIKELY))
2405               /* We do not swap with a trap instruction, since it
2406                  complicates trap handlers to have the trap
2407                  instruction be in a delay slot.  */
2408               || (prev_pinfo & INSN_TRAP)
2409               /* If the branch reads a register that the previous
2410                  instruction sets, we can not swap.  */
2411               || (! mips_opts.mips16
2412                   && (prev_pinfo & INSN_WRITE_GPR_T)
2413                   && insn_uses_reg (ip,
2414                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2415                                      & OP_MASK_RT),
2416                                     MIPS_GR_REG))
2417               || (! mips_opts.mips16
2418                   && (prev_pinfo & INSN_WRITE_GPR_D)
2419                   && insn_uses_reg (ip,
2420                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2421                                      & OP_MASK_RD),
2422                                     MIPS_GR_REG))
2423               || (mips_opts.mips16
2424                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2425                        && insn_uses_reg (ip,
2426                                          ((prev_insn.insn_opcode
2427                                            >> MIPS16OP_SH_RX)
2428                                           & MIPS16OP_MASK_RX),
2429                                          MIPS16_REG))
2430                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2431                           && insn_uses_reg (ip,
2432                                             ((prev_insn.insn_opcode
2433                                               >> MIPS16OP_SH_RY)
2434                                              & MIPS16OP_MASK_RY),
2435                                             MIPS16_REG))
2436                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2437                           && insn_uses_reg (ip,
2438                                             ((prev_insn.insn_opcode
2439                                               >> MIPS16OP_SH_RZ)
2440                                              & MIPS16OP_MASK_RZ),
2441                                             MIPS16_REG))
2442                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2443                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2444                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2445                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2446                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2447                           && insn_uses_reg (ip,
2448                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2449                                                                      insn_opcode),
2450                                             MIPS_GR_REG))))
2451               /* If the branch writes a register that the previous
2452                  instruction sets, we can not swap (we know that
2453                  branches write only to RD or to $31).  */
2454               || (! mips_opts.mips16
2455                   && (prev_pinfo & INSN_WRITE_GPR_T)
2456                   && (((pinfo & INSN_WRITE_GPR_D)
2457                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2458                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2459                       || ((pinfo & INSN_WRITE_GPR_31)
2460                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2461                                & OP_MASK_RT)
2462                               == RA))))
2463               || (! mips_opts.mips16
2464                   && (prev_pinfo & INSN_WRITE_GPR_D)
2465                   && (((pinfo & INSN_WRITE_GPR_D)
2466                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2467                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2468                       || ((pinfo & INSN_WRITE_GPR_31)
2469                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2470                                & OP_MASK_RD)
2471                               == RA))))
2472               || (mips_opts.mips16
2473                   && (pinfo & MIPS16_INSN_WRITE_31)
2474                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2475                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2476                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2477                               == RA))))
2478               /* If the branch writes a register that the previous
2479                  instruction reads, we can not swap (we know that
2480                  branches only write to RD or to $31).  */
2481               || (! mips_opts.mips16
2482                   && (pinfo & INSN_WRITE_GPR_D)
2483                   && insn_uses_reg (&prev_insn,
2484                                     ((ip->insn_opcode >> OP_SH_RD)
2485                                      & OP_MASK_RD),
2486                                     MIPS_GR_REG))
2487               || (! mips_opts.mips16
2488                   && (pinfo & INSN_WRITE_GPR_31)
2489                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2490               || (mips_opts.mips16
2491                   && (pinfo & MIPS16_INSN_WRITE_31)
2492                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2493               /* If we are generating embedded PIC code, the branch
2494                  might be expanded into a sequence which uses $at, so
2495                  we can't swap with an instruction which reads it.  */
2496               || (mips_pic == EMBEDDED_PIC
2497                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2498               /* If the previous previous instruction has a load
2499                  delay, and sets a register that the branch reads, we
2500                  can not swap.  */
2501               || (! mips_opts.mips16
2502                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2503               /* Itbl support may require additional care here.  */
2504                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2505                       || (! gpr_interlocks
2506                           && (prev_prev_insn.insn_mo->pinfo
2507                               & INSN_LOAD_MEMORY_DELAY)))
2508                   && insn_uses_reg (ip,
2509                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2510                                      & OP_MASK_RT),
2511                                     MIPS_GR_REG))
2512               /* If one instruction sets a condition code and the
2513                  other one uses a condition code, we can not swap.  */
2514               || ((pinfo & INSN_READ_COND_CODE)
2515                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2516               || ((pinfo & INSN_WRITE_COND_CODE)
2517                   && (prev_pinfo & INSN_READ_COND_CODE))
2518               /* If the previous instruction uses the PC, we can not
2519                  swap.  */
2520               || (mips_opts.mips16
2521                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2522               /* If the previous instruction was extended, we can not
2523                  swap.  */
2524               || (mips_opts.mips16 && prev_insn_extended)
2525               /* If the previous instruction had a fixup in mips16
2526                  mode, we can not swap.  This normally means that the
2527                  previous instruction was a 4 byte branch anyhow.  */
2528               || (mips_opts.mips16 && prev_insn_fixp[0])
2529               /* If the previous instruction is a sync, sync.l, or
2530                  sync.p, we can not swap.  */
2531               || (prev_pinfo & INSN_SYNC))
2532             {
2533               /* We could do even better for unconditional branches to
2534                  portions of this object file; we could pick up the
2535                  instruction at the destination, put it in the delay
2536                  slot, and bump the destination address.  */
2537               emit_nop ();
2538               /* Update the previous insn information.  */
2539               prev_prev_insn = *ip;
2540               prev_insn.insn_mo = &dummy_opcode;
2541             }
2542           else
2543             {
2544               /* It looks like we can actually do the swap.  */
2545               if (! mips_opts.mips16)
2546                 {
2547                   char *prev_f;
2548                   char temp[4];
2549
2550                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2551                   memcpy (temp, prev_f, 4);
2552                   memcpy (prev_f, f, 4);
2553                   memcpy (f, temp, 4);
2554                   if (prev_insn_fixp[0])
2555                     {
2556                       prev_insn_fixp[0]->fx_frag = frag_now;
2557                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2558                     }
2559                   if (prev_insn_fixp[1])
2560                     {
2561                       prev_insn_fixp[1]->fx_frag = frag_now;
2562                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2563                     }
2564                   if (prev_insn_fixp[2])
2565                     {
2566                       prev_insn_fixp[2]->fx_frag = frag_now;
2567                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2568                     }
2569                   if (fixp[0])
2570                     {
2571                       fixp[0]->fx_frag = prev_insn_frag;
2572                       fixp[0]->fx_where = prev_insn_where;
2573                     }
2574                   if (fixp[1])
2575                     {
2576                       fixp[1]->fx_frag = prev_insn_frag;
2577                       fixp[1]->fx_where = prev_insn_where;
2578                     }
2579                   if (fixp[2])
2580                     {
2581                       fixp[2]->fx_frag = prev_insn_frag;
2582                       fixp[2]->fx_where = prev_insn_where;
2583                     }
2584                 }
2585               else
2586                 {
2587                   char *prev_f;
2588                   char temp[2];
2589
2590                   assert (prev_insn_fixp[0] == NULL);
2591                   assert (prev_insn_fixp[1] == NULL);
2592                   assert (prev_insn_fixp[2] == NULL);
2593                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2594                   memcpy (temp, prev_f, 2);
2595                   memcpy (prev_f, f, 2);
2596                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2597                     {
2598                       assert (*reloc_type == BFD_RELOC_UNUSED);
2599                       memcpy (f, temp, 2);
2600                     }
2601                   else
2602                     {
2603                       memcpy (f, f + 2, 2);
2604                       memcpy (f + 2, temp, 2);
2605                     }
2606                   if (fixp[0])
2607                     {
2608                       fixp[0]->fx_frag = prev_insn_frag;
2609                       fixp[0]->fx_where = prev_insn_where;
2610                     }
2611                   if (fixp[1])
2612                     {
2613                       fixp[1]->fx_frag = prev_insn_frag;
2614                       fixp[1]->fx_where = prev_insn_where;
2615                     }
2616                   if (fixp[2])
2617                     {
2618                       fixp[2]->fx_frag = prev_insn_frag;
2619                       fixp[2]->fx_where = prev_insn_where;
2620                     }
2621                 }
2622
2623               /* Update the previous insn information; leave prev_insn
2624                  unchanged.  */
2625               prev_prev_insn = *ip;
2626             }
2627           prev_insn_is_delay_slot = 1;
2628
2629           /* If that was an unconditional branch, forget the previous
2630              insn information.  */
2631           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2632             {
2633               prev_prev_insn.insn_mo = &dummy_opcode;
2634               prev_insn.insn_mo = &dummy_opcode;
2635             }
2636
2637           prev_insn_fixp[0] = NULL;
2638           prev_insn_fixp[1] = NULL;
2639           prev_insn_fixp[2] = NULL;
2640           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2641           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2642           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2643           prev_insn_extended = 0;
2644         }
2645       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2646         {
2647           /* We don't yet optimize a branch likely.  What we should do
2648              is look at the target, copy the instruction found there
2649              into the delay slot, and increment the branch to jump to
2650              the next instruction.  */
2651           emit_nop ();
2652           /* Update the previous insn information.  */
2653           prev_prev_insn = *ip;
2654           prev_insn.insn_mo = &dummy_opcode;
2655           prev_insn_fixp[0] = NULL;
2656           prev_insn_fixp[1] = NULL;
2657           prev_insn_fixp[2] = NULL;
2658           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2659           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2660           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2661           prev_insn_extended = 0;
2662         }
2663       else
2664         {
2665           /* Update the previous insn information.  */
2666           if (nops > 0)
2667             prev_prev_insn.insn_mo = &dummy_opcode;
2668           else
2669             prev_prev_insn = prev_insn;
2670           prev_insn = *ip;
2671
2672           /* Any time we see a branch, we always fill the delay slot
2673              immediately; since this insn is not a branch, we know it
2674              is not in a delay slot.  */
2675           prev_insn_is_delay_slot = 0;
2676
2677           prev_insn_fixp[0] = fixp[0];
2678           prev_insn_fixp[1] = fixp[1];
2679           prev_insn_fixp[2] = fixp[2];
2680           prev_insn_reloc_type[0] = reloc_type[0];
2681           prev_insn_reloc_type[1] = reloc_type[1];
2682           prev_insn_reloc_type[2] = reloc_type[2];
2683           if (mips_opts.mips16)
2684             prev_insn_extended = (ip->use_extend
2685                                   || *reloc_type > BFD_RELOC_UNUSED);
2686         }
2687
2688       prev_prev_insn_unreordered = prev_insn_unreordered;
2689       prev_insn_unreordered = 0;
2690       prev_insn_frag = frag_now;
2691       prev_insn_where = f - frag_now->fr_literal;
2692       prev_insn_valid = 1;
2693     }
2694   else if (place == NULL)
2695     {
2696       /* We need to record a bit of information even when we are not
2697          reordering, in order to determine the base address for mips16
2698          PC relative relocs.  */
2699       prev_prev_insn = prev_insn;
2700       prev_insn = *ip;
2701       prev_insn_reloc_type[0] = reloc_type[0];
2702       prev_insn_reloc_type[1] = reloc_type[1];
2703       prev_insn_reloc_type[2] = reloc_type[2];
2704       prev_prev_insn_unreordered = prev_insn_unreordered;
2705       prev_insn_unreordered = 1;
2706     }
2707
2708   /* We just output an insn, so the next one doesn't have a label.  */
2709   mips_clear_insn_labels ();
2710
2711   /* We must ensure that a fixup associated with an unmatched %hi
2712      reloc does not become a variant frag.  Otherwise, the
2713      rearrangement of %hi relocs in frob_file may confuse
2714      tc_gen_reloc.  */
2715   if (unmatched_hi)
2716     {
2717       frag_wane (frag_now);
2718       frag_new (0);
2719     }
2720 }
2721
2722 /* This function forgets that there was any previous instruction or
2723    label.  If PRESERVE is non-zero, it remembers enough information to
2724    know whether nops are needed before a noreorder section.  */
2725
2726 static void
2727 mips_no_prev_insn (preserve)
2728      int preserve;
2729 {
2730   if (! preserve)
2731     {
2732       prev_insn.insn_mo = &dummy_opcode;
2733       prev_prev_insn.insn_mo = &dummy_opcode;
2734       prev_nop_frag = NULL;
2735       prev_nop_frag_holds = 0;
2736       prev_nop_frag_required = 0;
2737       prev_nop_frag_since = 0;
2738     }
2739   prev_insn_valid = 0;
2740   prev_insn_is_delay_slot = 0;
2741   prev_insn_unreordered = 0;
2742   prev_insn_extended = 0;
2743   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2744   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2745   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2746   prev_prev_insn_unreordered = 0;
2747   mips_clear_insn_labels ();
2748 }
2749
2750 /* This function must be called whenever we turn on noreorder or emit
2751    something other than instructions.  It inserts any NOPS which might
2752    be needed by the previous instruction, and clears the information
2753    kept for the previous instructions.  The INSNS parameter is true if
2754    instructions are to follow.  */
2755
2756 static void
2757 mips_emit_delays (insns)
2758      boolean insns;
2759 {
2760   if (! mips_opts.noreorder)
2761     {
2762       int nops;
2763
2764       nops = 0;
2765       if ((! mips_opts.mips16
2766            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2767            && (! cop_interlocks
2768                && (prev_insn.insn_mo->pinfo
2769                    & (INSN_LOAD_COPROC_DELAY
2770                       | INSN_COPROC_MOVE_DELAY
2771                       | INSN_WRITE_COND_CODE))))
2772           || (! hilo_interlocks
2773               && (prev_insn.insn_mo->pinfo
2774                   & (INSN_READ_LO
2775                      | INSN_READ_HI)))
2776           || (! mips_opts.mips16
2777               && ! gpr_interlocks
2778               && (prev_insn.insn_mo->pinfo
2779                   & INSN_LOAD_MEMORY_DELAY))
2780           || (! mips_opts.mips16
2781               && mips_opts.isa == ISA_MIPS1
2782               && (prev_insn.insn_mo->pinfo
2783                   & INSN_COPROC_MEMORY_DELAY)))
2784         {
2785           /* Itbl support may require additional care here.  */
2786           ++nops;
2787           if ((! mips_opts.mips16
2788                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2789                && (! cop_interlocks
2790                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2791               || (! hilo_interlocks
2792                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2793                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2794             ++nops;
2795
2796           if (prev_insn_unreordered)
2797             nops = 0;
2798         }
2799       else if ((! mips_opts.mips16
2800                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2801                 && (! cop_interlocks
2802                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2803                || (! hilo_interlocks
2804                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2805                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2806         {
2807           /* Itbl support may require additional care here.  */
2808           if (! prev_prev_insn_unreordered)
2809             ++nops;
2810         }
2811
2812       if (nops > 0)
2813         {
2814           struct insn_label_list *l;
2815
2816           if (insns)
2817             {
2818               /* Record the frag which holds the nop instructions, so
2819                  that we can remove them if we don't need them.  */
2820               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2821               prev_nop_frag = frag_now;
2822               prev_nop_frag_holds = nops;
2823               prev_nop_frag_required = 0;
2824               prev_nop_frag_since = 0;
2825             }
2826
2827           for (; nops > 0; --nops)
2828             emit_nop ();
2829
2830           if (insns)
2831             {
2832               /* Move on to a new frag, so that it is safe to simply
2833                  decrease the size of prev_nop_frag.  */
2834               frag_wane (frag_now);
2835               frag_new (0);
2836             }
2837
2838           for (l = insn_labels; l != NULL; l = l->next)
2839             {
2840               valueT val;
2841
2842               assert (S_GET_SEGMENT (l->label) == now_seg);
2843               symbol_set_frag (l->label, frag_now);
2844               val = (valueT) frag_now_fix ();
2845               /* mips16 text labels are stored as odd.  */
2846               if (mips_opts.mips16)
2847                 ++val;
2848               S_SET_VALUE (l->label, val);
2849             }
2850         }
2851     }
2852
2853   /* Mark instruction labels in mips16 mode.  */
2854   if (insns)
2855     mips16_mark_labels ();
2856
2857   mips_no_prev_insn (insns);
2858 }
2859
2860 /* Build an instruction created by a macro expansion.  This is passed
2861    a pointer to the count of instructions created so far, an
2862    expression, the name of the instruction to build, an operand format
2863    string, and corresponding arguments.  */
2864
2865 #ifdef USE_STDARG
2866 static void
2867 macro_build (char *place,
2868              int *counter,
2869              expressionS * ep,
2870              const char *name,
2871              const char *fmt,
2872              ...)
2873 #else
2874 static void
2875 macro_build (place, counter, ep, name, fmt, va_alist)
2876      char *place;
2877      int *counter;
2878      expressionS *ep;
2879      const char *name;
2880      const char *fmt;
2881      va_dcl
2882 #endif
2883 {
2884   struct mips_cl_insn insn;
2885   bfd_reloc_code_real_type r[3];
2886   va_list args;
2887
2888 #ifdef USE_STDARG
2889   va_start (args, fmt);
2890 #else
2891   va_start (args);
2892 #endif
2893
2894   /*
2895    * If the macro is about to expand into a second instruction,
2896    * print a warning if needed. We need to pass ip as a parameter
2897    * to generate a better warning message here...
2898    */
2899   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2900     as_warn (_("Macro instruction expanded into multiple instructions"));
2901
2902   /*
2903    * If the macro is about to expand into a second instruction,
2904    * and it is in a delay slot, print a warning.
2905    */
2906   if (place == NULL
2907       && *counter == 1
2908       && mips_opts.noreorder
2909       && (prev_prev_insn.insn_mo->pinfo
2910           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2911              | INSN_COND_BRANCH_LIKELY)) != 0)
2912     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2913
2914   if (place == NULL)
2915     ++*counter;         /* bump instruction counter */
2916
2917   if (mips_opts.mips16)
2918     {
2919       mips16_macro_build (place, counter, ep, name, fmt, args);
2920       va_end (args);
2921       return;
2922     }
2923
2924   r[0] = BFD_RELOC_UNUSED;
2925   r[1] = BFD_RELOC_UNUSED;
2926   r[2] = BFD_RELOC_UNUSED;
2927   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2928   assert (insn.insn_mo);
2929   assert (strcmp (name, insn.insn_mo->name) == 0);
2930
2931   /* Search until we get a match for NAME.  */
2932   while (1)
2933     {
2934       /* It is assumed here that macros will never generate 
2935          MDMX or MIPS-3D instructions.  */
2936       if (strcmp (fmt, insn.insn_mo->args) == 0
2937           && insn.insn_mo->pinfo != INSN_MACRO
2938           && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2939           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2940         break;
2941
2942       ++insn.insn_mo;
2943       assert (insn.insn_mo->name);
2944       assert (strcmp (name, insn.insn_mo->name) == 0);
2945     }
2946
2947   insn.insn_opcode = insn.insn_mo->match;
2948   for (;;)
2949     {
2950       switch (*fmt++)
2951         {
2952         case '\0':
2953           break;
2954
2955         case ',':
2956         case '(':
2957         case ')':
2958           continue;
2959
2960         case 't':
2961         case 'w':
2962         case 'E':
2963           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2964           continue;
2965
2966         case 'c':
2967           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2968           continue;
2969
2970         case 'T':
2971         case 'W':
2972           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2973           continue;
2974
2975         case 'd':
2976         case 'G':
2977           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2978           continue;
2979
2980         case 'U':
2981           {
2982             int tmp = va_arg (args, int);
2983
2984             insn.insn_opcode |= tmp << OP_SH_RT;
2985             insn.insn_opcode |= tmp << OP_SH_RD;
2986             continue;
2987           }
2988
2989         case 'V':
2990         case 'S':
2991           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2992           continue;
2993
2994         case 'z':
2995           continue;
2996
2997         case '<':
2998           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2999           continue;
3000
3001         case 'D':
3002           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3003           continue;
3004
3005         case 'B':
3006           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3007           continue;
3008
3009         case 'J':
3010           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3011           continue;
3012
3013         case 'q':
3014           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3015           continue;
3016
3017         case 'b':
3018         case 's':
3019         case 'r':
3020         case 'v':
3021           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3022           continue;
3023
3024         case 'i':
3025         case 'j':
3026         case 'o':
3027           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3028           assert (*r == BFD_RELOC_GPREL16
3029                   || *r == BFD_RELOC_MIPS_LITERAL
3030                   || *r == BFD_RELOC_MIPS_HIGHER
3031                   || *r == BFD_RELOC_HI16_S
3032                   || *r == BFD_RELOC_LO16
3033                   || *r == BFD_RELOC_MIPS_GOT16
3034                   || *r == BFD_RELOC_MIPS_CALL16
3035                   || *r == BFD_RELOC_MIPS_GOT_DISP
3036                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3037                   || *r == BFD_RELOC_MIPS_GOT_OFST
3038                   || *r == BFD_RELOC_MIPS_GOT_LO16
3039                   || *r == BFD_RELOC_MIPS_CALL_LO16
3040                   || (ep->X_op == O_subtract
3041                       && *r == BFD_RELOC_PCREL_LO16));
3042           continue;
3043
3044         case 'u':
3045           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3046           assert (ep != NULL
3047                   && (ep->X_op == O_constant
3048                       || (ep->X_op == O_symbol
3049                           && (*r == BFD_RELOC_MIPS_HIGHEST
3050                               || *r == BFD_RELOC_HI16_S
3051                               || *r == BFD_RELOC_HI16
3052                               || *r == BFD_RELOC_GPREL16
3053                               || *r == BFD_RELOC_MIPS_GOT_HI16
3054                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3055                       || (ep->X_op == O_subtract
3056                           && *r == BFD_RELOC_PCREL_HI16_S)));
3057           continue;
3058
3059         case 'p':
3060           assert (ep != NULL);
3061           /*
3062            * This allows macro() to pass an immediate expression for
3063            * creating short branches without creating a symbol.
3064            * Note that the expression still might come from the assembly
3065            * input, in which case the value is not checked for range nor
3066            * is a relocation entry generated (yuck).
3067            */
3068           if (ep->X_op == O_constant)
3069             {
3070               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3071               ep = NULL;
3072             }
3073           else
3074             if (mips_pic == EMBEDDED_PIC)
3075               *r = BFD_RELOC_16_PCREL_S2;
3076             else
3077               *r = BFD_RELOC_16_PCREL;
3078           continue;
3079
3080         case 'a':
3081           assert (ep != NULL);
3082           *r = BFD_RELOC_MIPS_JMP;
3083           continue;
3084
3085         case 'C':
3086           insn.insn_opcode |= va_arg (args, unsigned long);
3087           continue;
3088
3089         default:
3090           internalError ();
3091         }
3092       break;
3093     }
3094   va_end (args);
3095   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3096
3097   append_insn (place, &insn, ep, r, false);
3098 }
3099
3100 static void
3101 mips16_macro_build (place, counter, ep, name, fmt, args)
3102      char *place;
3103      int *counter ATTRIBUTE_UNUSED;
3104      expressionS *ep;
3105      const char *name;
3106      const char *fmt;
3107      va_list args;
3108 {
3109   struct mips_cl_insn insn;
3110   bfd_reloc_code_real_type r[3]
3111     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3112
3113   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3114   assert (insn.insn_mo);
3115   assert (strcmp (name, insn.insn_mo->name) == 0);
3116
3117   while (strcmp (fmt, insn.insn_mo->args) != 0
3118          || insn.insn_mo->pinfo == INSN_MACRO)
3119     {
3120       ++insn.insn_mo;
3121       assert (insn.insn_mo->name);
3122       assert (strcmp (name, insn.insn_mo->name) == 0);
3123     }
3124
3125   insn.insn_opcode = insn.insn_mo->match;
3126   insn.use_extend = false;
3127
3128   for (;;)
3129     {
3130       int c;
3131
3132       c = *fmt++;
3133       switch (c)
3134         {
3135         case '\0':
3136           break;
3137
3138         case ',':
3139         case '(':
3140         case ')':
3141           continue;
3142
3143         case 'y':
3144         case 'w':
3145           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3146           continue;
3147
3148         case 'x':
3149         case 'v':
3150           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3151           continue;
3152
3153         case 'z':
3154           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3155           continue;
3156
3157         case 'Z':
3158           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3159           continue;
3160
3161         case '0':
3162         case 'S':
3163         case 'P':
3164         case 'R':
3165           continue;
3166
3167         case 'X':
3168           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3169           continue;
3170
3171         case 'Y':
3172           {
3173             int regno;
3174
3175             regno = va_arg (args, int);
3176             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3177             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3178           }
3179           continue;
3180
3181         case '<':
3182         case '>':
3183         case '4':
3184         case '5':
3185         case 'H':
3186         case 'W':
3187         case 'D':
3188         case 'j':
3189         case '8':
3190         case 'V':
3191         case 'C':
3192         case 'U':
3193         case 'k':
3194         case 'K':
3195         case 'p':
3196         case 'q':
3197           {
3198             assert (ep != NULL);
3199
3200             if (ep->X_op != O_constant)
3201               *r = (int) BFD_RELOC_UNUSED + c;
3202             else
3203               {
3204                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3205                               false, &insn.insn_opcode, &insn.use_extend,
3206                               &insn.extend);
3207                 ep = NULL;
3208                 *r = BFD_RELOC_UNUSED;
3209               }
3210           }
3211           continue;
3212
3213         case '6':
3214           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3215           continue;
3216         }
3217
3218       break;
3219     }
3220
3221   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3222
3223   append_insn (place, &insn, ep, r, false);
3224 }
3225
3226 /*
3227  * Generate a "jalr" instruction with a relocation hint to the called
3228  * function.  This occurs in NewABI PIC code.
3229  */
3230 static void
3231 macro_build_jalr (icnt, ep)
3232      int icnt;
3233      expressionS *ep;
3234 {
3235   if (HAVE_NEWABI)
3236     frag_more (0);
3237   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3238                RA, PIC_CALL_REG);
3239   if (HAVE_NEWABI)
3240     fix_new_exp (frag_now, 0, 0, ep, false, BFD_RELOC_MIPS_JALR);
3241 }
3242
3243 /*
3244  * Generate a "lui" instruction.
3245  */
3246 static void
3247 macro_build_lui (place, counter, ep, regnum)
3248      char *place;
3249      int *counter;
3250      expressionS *ep;
3251      int regnum;
3252 {
3253   expressionS high_expr;
3254   struct mips_cl_insn insn;
3255   bfd_reloc_code_real_type r[3]
3256     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3257   CONST char *name = "lui";
3258   CONST char *fmt = "t,u";
3259
3260   assert (! mips_opts.mips16);
3261
3262   if (place == NULL)
3263     high_expr = *ep;
3264   else
3265     {
3266       high_expr.X_op = O_constant;
3267       high_expr.X_add_number = ep->X_add_number;
3268     }
3269
3270   if (high_expr.X_op == O_constant)
3271     {
3272       /* we can compute the instruction now without a relocation entry */
3273       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3274                                 >> 16) & 0xffff;
3275       *r = BFD_RELOC_UNUSED;
3276     }
3277   else if (! HAVE_NEWABI)
3278     {
3279       assert (ep->X_op == O_symbol);
3280       /* _gp_disp is a special case, used from s_cpload.  */
3281       assert (mips_pic == NO_PIC
3282               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3283       *r = BFD_RELOC_HI16_S;
3284     }
3285
3286   /*
3287    * If the macro is about to expand into a second instruction,
3288    * print a warning if needed. We need to pass ip as a parameter
3289    * to generate a better warning message here...
3290    */
3291   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3292     as_warn (_("Macro instruction expanded into multiple instructions"));
3293
3294   if (place == NULL)
3295     ++*counter;         /* bump instruction counter */
3296
3297   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3298   assert (insn.insn_mo);
3299   assert (strcmp (name, insn.insn_mo->name) == 0);
3300   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3301
3302   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3303   if (*r == BFD_RELOC_UNUSED)
3304     {
3305       insn.insn_opcode |= high_expr.X_add_number;
3306       append_insn (place, &insn, NULL, r, false);
3307     }
3308   else
3309     append_insn (place, &insn, &high_expr, r, false);
3310 }
3311
3312 /*                      set_at()
3313  * Generates code to set the $at register to true (one)
3314  * if reg is less than the immediate expression.
3315  */
3316 static void
3317 set_at (counter, reg, unsignedp)
3318      int *counter;
3319      int reg;
3320      int unsignedp;
3321 {
3322   if (imm_expr.X_op == O_constant
3323       && imm_expr.X_add_number >= -0x8000
3324       && imm_expr.X_add_number < 0x8000)
3325     macro_build ((char *) NULL, counter, &imm_expr,
3326                  unsignedp ? "sltiu" : "slti",
3327                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3328   else
3329     {
3330       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3331       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3332                    unsignedp ? "sltu" : "slt",
3333                    "d,v,t", AT, reg, AT);
3334     }
3335 }
3336
3337 /* Warn if an expression is not a constant.  */
3338
3339 static void
3340 check_absolute_expr (ip, ex)
3341      struct mips_cl_insn *ip;
3342      expressionS *ex;
3343 {
3344   if (ex->X_op == O_big)
3345     as_bad (_("unsupported large constant"));
3346   else if (ex->X_op != O_constant)
3347     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3348 }
3349
3350 /* Count the leading zeroes by performing a binary chop. This is a
3351    bulky bit of source, but performance is a LOT better for the
3352    majority of values than a simple loop to count the bits:
3353        for (lcnt = 0; (lcnt < 32); lcnt++)
3354          if ((v) & (1 << (31 - lcnt)))
3355            break;
3356   However it is not code size friendly, and the gain will drop a bit
3357   on certain cached systems.
3358 */
3359 #define COUNT_TOP_ZEROES(v)             \
3360   (((v) & ~0xffff) == 0                 \
3361    ? ((v) & ~0xff) == 0                 \
3362      ? ((v) & ~0xf) == 0                \
3363        ? ((v) & ~0x3) == 0              \
3364          ? ((v) & ~0x1) == 0            \
3365            ? !(v)                       \
3366              ? 32                       \
3367              : 31                       \
3368            : 30                         \
3369          : ((v) & ~0x7) == 0            \
3370            ? 29                         \
3371            : 28                         \
3372        : ((v) & ~0x3f) == 0             \
3373          ? ((v) & ~0x1f) == 0           \
3374            ? 27                         \
3375            : 26                         \
3376          : ((v) & ~0x7f) == 0           \
3377            ? 25                         \
3378            : 24                         \
3379      : ((v) & ~0xfff) == 0              \
3380        ? ((v) & ~0x3ff) == 0            \
3381          ? ((v) & ~0x1ff) == 0          \
3382            ? 23                         \
3383            : 22                         \
3384          : ((v) & ~0x7ff) == 0          \
3385            ? 21                         \
3386            : 20                         \
3387        : ((v) & ~0x3fff) == 0           \
3388          ? ((v) & ~0x1fff) == 0         \
3389            ? 19                         \
3390            : 18                         \
3391          : ((v) & ~0x7fff) == 0         \
3392            ? 17                         \
3393            : 16                         \
3394    : ((v) & ~0xffffff) == 0             \
3395      ? ((v) & ~0xfffff) == 0            \
3396        ? ((v) & ~0x3ffff) == 0          \
3397          ? ((v) & ~0x1ffff) == 0        \
3398            ? 15                         \
3399            : 14                         \
3400          : ((v) & ~0x7ffff) == 0        \
3401            ? 13                         \
3402            : 12                         \
3403        : ((v) & ~0x3fffff) == 0         \
3404          ? ((v) & ~0x1fffff) == 0       \
3405            ? 11                         \
3406            : 10                         \
3407          : ((v) & ~0x7fffff) == 0       \
3408            ? 9                          \
3409            : 8                          \
3410      : ((v) & ~0xfffffff) == 0          \
3411        ? ((v) & ~0x3ffffff) == 0        \
3412          ? ((v) & ~0x1ffffff) == 0      \
3413            ? 7                          \
3414            : 6                          \
3415          : ((v) & ~0x7ffffff) == 0      \
3416            ? 5                          \
3417            : 4                          \
3418        : ((v) & ~0x3fffffff) == 0       \
3419          ? ((v) & ~0x1fffffff) == 0     \
3420            ? 3                          \
3421            : 2                          \
3422          : ((v) & ~0x7fffffff) == 0     \
3423            ? 1                          \
3424            : 0)
3425
3426 /* Is the given value a sign-extended 32-bit value?  */
3427 #define IS_SEXT_32BIT_NUM(x)                                            \
3428   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
3429    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3430
3431 /*                      load_register()
3432  *  This routine generates the least number of instructions neccessary to load
3433  *  an absolute expression value into a register.
3434  */
3435 static void
3436 load_register (counter, reg, ep, dbl)
3437      int *counter;
3438      int reg;
3439      expressionS *ep;
3440      int dbl;
3441 {
3442   int freg;
3443   expressionS hi32, lo32;
3444
3445   if (ep->X_op != O_big)
3446     {
3447       assert (ep->X_op == O_constant);
3448       if (ep->X_add_number < 0x8000
3449           && (ep->X_add_number >= 0
3450               || (ep->X_add_number >= -0x8000
3451                   && (! dbl
3452                       || ! ep->X_unsigned
3453                       || sizeof (ep->X_add_number) > 4))))
3454         {
3455           /* We can handle 16 bit signed values with an addiu to
3456              $zero.  No need to ever use daddiu here, since $zero and
3457              the result are always correct in 32 bit mode.  */
3458           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3459                        (int) BFD_RELOC_LO16);
3460           return;
3461         }
3462       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3463         {
3464           /* We can handle 16 bit unsigned values with an ori to
3465              $zero.  */
3466           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3467                        (int) BFD_RELOC_LO16);
3468           return;
3469         }
3470       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3471                 && (! dbl
3472                     || ! ep->X_unsigned
3473                     || sizeof (ep->X_add_number) > 4
3474                     || (ep->X_add_number & 0x80000000) == 0))
3475                || ((HAVE_32BIT_GPRS || ! dbl)
3476                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3477                || (HAVE_32BIT_GPRS
3478                    && ! dbl
3479                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3480                        == ~ (offsetT) 0xffffffff)))
3481         {
3482           /* 32 bit values require an lui.  */
3483           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3484                        (int) BFD_RELOC_HI16);
3485           if ((ep->X_add_number & 0xffff) != 0)
3486             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3487                          (int) BFD_RELOC_LO16);
3488           return;
3489         }
3490     }
3491
3492   /* The value is larger than 32 bits.  */
3493
3494   if (HAVE_32BIT_GPRS)
3495     {
3496       as_bad (_("Number (0x%lx) larger than 32 bits"),
3497               (unsigned long) ep->X_add_number);
3498       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3499                    (int) BFD_RELOC_LO16);
3500       return;
3501     }
3502
3503   if (ep->X_op != O_big)
3504     {
3505       hi32 = *ep;
3506       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3507       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3508       hi32.X_add_number &= 0xffffffff;
3509       lo32 = *ep;
3510       lo32.X_add_number &= 0xffffffff;
3511     }
3512   else
3513     {
3514       assert (ep->X_add_number > 2);
3515       if (ep->X_add_number == 3)
3516         generic_bignum[3] = 0;
3517       else if (ep->X_add_number > 4)
3518         as_bad (_("Number larger than 64 bits"));
3519       lo32.X_op = O_constant;
3520       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3521       hi32.X_op = O_constant;
3522       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3523     }
3524
3525   if (hi32.X_add_number == 0)
3526     freg = 0;
3527   else
3528     {
3529       int shift, bit;
3530       unsigned long hi, lo;
3531
3532       if (hi32.X_add_number == (offsetT) 0xffffffff)
3533         {
3534           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3535             {
3536               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3537                            reg, 0, (int) BFD_RELOC_LO16);
3538               return;
3539             }
3540           if (lo32.X_add_number & 0x80000000)
3541             {
3542               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3543                            (int) BFD_RELOC_HI16);
3544               if (lo32.X_add_number & 0xffff)
3545                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3546                              reg, reg, (int) BFD_RELOC_LO16);
3547               return;
3548             }
3549         }
3550
3551       /* Check for 16bit shifted constant.  We know that hi32 is
3552          non-zero, so start the mask on the first bit of the hi32
3553          value.  */
3554       shift = 17;
3555       do
3556         {
3557           unsigned long himask, lomask;
3558
3559           if (shift < 32)
3560             {
3561               himask = 0xffff >> (32 - shift);
3562               lomask = (0xffff << shift) & 0xffffffff;
3563             }
3564           else
3565             {
3566               himask = 0xffff << (shift - 32);
3567               lomask = 0;
3568             }
3569           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3570               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3571             {
3572               expressionS tmp;
3573
3574               tmp.X_op = O_constant;
3575               if (shift < 32)
3576                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3577                                     | (lo32.X_add_number >> shift));
3578               else
3579                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3580               macro_build ((char *) NULL, counter, &tmp,
3581                            "ori", "t,r,i", reg, 0,
3582                            (int) BFD_RELOC_LO16);
3583               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3584                            (shift >= 32) ? "dsll32" : "dsll",
3585                            "d,w,<", reg, reg,
3586                            (shift >= 32) ? shift - 32 : shift);
3587               return;
3588             }
3589           ++shift;
3590         }
3591       while (shift <= (64 - 16));
3592
3593       /* Find the bit number of the lowest one bit, and store the
3594          shifted value in hi/lo.  */
3595       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3596       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3597       if (lo != 0)
3598         {
3599           bit = 0;
3600           while ((lo & 1) == 0)
3601             {
3602               lo >>= 1;
3603               ++bit;
3604             }
3605           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3606           hi >>= bit;
3607         }
3608       else
3609         {
3610           bit = 32;
3611           while ((hi & 1) == 0)
3612             {
3613               hi >>= 1;
3614               ++bit;
3615             }
3616           lo = hi;
3617           hi = 0;
3618         }
3619
3620       /* Optimize if the shifted value is a (power of 2) - 1.  */
3621       if ((hi == 0 && ((lo + 1) & lo) == 0)
3622           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3623         {
3624           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3625           if (shift != 0)
3626             {
3627               expressionS tmp;
3628
3629               /* This instruction will set the register to be all
3630                  ones.  */
3631               tmp.X_op = O_constant;
3632               tmp.X_add_number = (offsetT) -1;
3633               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3634                            reg, 0, (int) BFD_RELOC_LO16);
3635               if (bit != 0)
3636                 {
3637                   bit += shift;
3638                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3639                                (bit >= 32) ? "dsll32" : "dsll",
3640                                "d,w,<", reg, reg,
3641                                (bit >= 32) ? bit - 32 : bit);
3642                 }
3643               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3644                            (shift >= 32) ? "dsrl32" : "dsrl",
3645                            "d,w,<", reg, reg,
3646                            (shift >= 32) ? shift - 32 : shift);
3647               return;
3648             }
3649         }
3650
3651       /* Sign extend hi32 before calling load_register, because we can
3652          generally get better code when we load a sign extended value.  */
3653       if ((hi32.X_add_number & 0x80000000) != 0)
3654         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3655       load_register (counter, reg, &hi32, 0);
3656       freg = reg;
3657     }
3658   if ((lo32.X_add_number & 0xffff0000) == 0)
3659     {
3660       if (freg != 0)
3661         {
3662           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3663                        "dsll32", "d,w,<", reg, freg, 0);
3664           freg = reg;
3665         }
3666     }
3667   else
3668     {
3669       expressionS mid16;
3670
3671       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3672         {
3673           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3674                        (int) BFD_RELOC_HI16);
3675           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3676                        "dsrl32", "d,w,<", reg, reg, 0);
3677           return;
3678         }
3679
3680       if (freg != 0)
3681         {
3682           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3683                        "d,w,<", reg, freg, 16);
3684           freg = reg;
3685         }
3686       mid16 = lo32;
3687       mid16.X_add_number >>= 16;
3688       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3689                    freg, (int) BFD_RELOC_LO16);
3690       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3691                    "d,w,<", reg, reg, 16);
3692       freg = reg;
3693     }
3694   if ((lo32.X_add_number & 0xffff) != 0)
3695     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3696                  (int) BFD_RELOC_LO16);
3697 }
3698
3699 /* Load an address into a register.  */
3700
3701 static void
3702 load_address (counter, reg, ep, used_at)
3703      int *counter;
3704      int reg;
3705      expressionS *ep;
3706      int *used_at;
3707 {
3708   char *p = NULL;
3709
3710   if (ep->X_op != O_constant
3711       && ep->X_op != O_symbol)
3712     {
3713       as_bad (_("expression too complex"));
3714       ep->X_op = O_constant;
3715     }
3716
3717   if (ep->X_op == O_constant)
3718     {
3719       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3720       return;
3721     }
3722
3723   if (mips_pic == NO_PIC)
3724     {
3725       /* If this is a reference to a GP relative symbol, we want
3726            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3727          Otherwise we want
3728            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3729            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3730          If we have an addend, we always use the latter form.
3731
3732          With 64bit address space and a usable $at we want
3733            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3734            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3735            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3736            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3737            dsll32       $reg,0
3738            dadd         $reg,$reg,$at
3739
3740          If $at is already in use, we use an path which is suboptimal
3741          on superscalar processors.
3742            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3743            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3744            dsll         $reg,16
3745            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3746            dsll         $reg,16
3747            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3748        */
3749       if (HAVE_64BIT_ADDRESSES)
3750         {
3751           /* We don't do GP optimization for now because RELAX_ENCODE can't
3752              hold the data for such large chunks.  */
3753
3754           if (*used_at == 0)
3755             {
3756               macro_build (p, counter, ep, "lui", "t,u",
3757                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3758               macro_build (p, counter, ep, "lui", "t,u",
3759                            AT, (int) BFD_RELOC_HI16_S);
3760               macro_build (p, counter, ep, "daddiu", "t,r,j",
3761                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3762               macro_build (p, counter, ep, "daddiu", "t,r,j",
3763                            AT, AT, (int) BFD_RELOC_LO16);
3764               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3765                            "d,w,<", reg, reg, 0);
3766               macro_build (p, counter, (expressionS *) NULL, "dadd",
3767                            "d,v,t", reg, reg, AT);
3768               *used_at = 1;
3769             }
3770           else
3771             {
3772               macro_build (p, counter, ep, "lui", "t,u",
3773                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3774               macro_build (p, counter, ep, "daddiu", "t,r,j",
3775                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3776               macro_build (p, counter, (expressionS *) NULL, "dsll",
3777                            "d,w,<", reg, reg, 16);
3778               macro_build (p, counter, ep, "daddiu", "t,r,j",
3779                            reg, reg, (int) BFD_RELOC_HI16_S);
3780               macro_build (p, counter, (expressionS *) NULL, "dsll",
3781                            "d,w,<", reg, reg, 16);
3782               macro_build (p, counter, ep, "daddiu", "t,r,j",
3783                            reg, reg, (int) BFD_RELOC_LO16);
3784             }
3785         }
3786       else
3787         {
3788           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3789               && ! nopic_need_relax (ep->X_add_symbol, 1))
3790             {
3791               frag_grow (20);
3792               macro_build ((char *) NULL, counter, ep,
3793                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3794                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3795               p = frag_var (rs_machine_dependent, 8, 0,
3796                             RELAX_ENCODE (4, 8, 0, 4, 0,
3797                                           mips_opts.warn_about_macros),
3798                             ep->X_add_symbol, 0, NULL);
3799             }
3800           macro_build_lui (p, counter, ep, reg);
3801           if (p != NULL)
3802             p += 4;
3803           macro_build (p, counter, ep,
3804                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3805                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3806         }
3807     }
3808   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3809     {
3810       expressionS ex;
3811
3812       /* If this is a reference to an external symbol, we want
3813            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3814          Otherwise we want
3815            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3816            nop
3817            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3818          If there is a constant, it must be added in after.  */
3819       ex.X_add_number = ep->X_add_number;
3820       ep->X_add_number = 0;
3821       frag_grow (20);
3822       macro_build ((char *) NULL, counter, ep,
3823                    HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3824                    reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3825       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3826       p = frag_var (rs_machine_dependent, 4, 0,
3827                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3828                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3829       macro_build (p, counter, ep,
3830                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3831                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3832       if (ex.X_add_number != 0)
3833         {
3834           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3835             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3836           ex.X_op = O_constant;
3837           macro_build ((char *) NULL, counter, &ex,
3838                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3839                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3840         }
3841     }
3842   else if (mips_pic == SVR4_PIC)
3843     {
3844       expressionS ex;
3845       int off;
3846
3847       /* This is the large GOT case.  If this is a reference to an
3848          external symbol, we want
3849            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3850            addu         $reg,$reg,$gp
3851            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3852          Otherwise, for a reference to a local symbol, we want
3853            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3854            nop
3855            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3856          If we have NewABI, we want
3857            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3858            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3859          If there is a constant, it must be added in after.  */
3860       ex.X_add_number = ep->X_add_number;
3861       ep->X_add_number = 0;
3862       if (HAVE_NEWABI)
3863         {
3864           macro_build ((char *) NULL, counter, ep,
3865                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3866                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3867           macro_build (p, counter, ep,
3868                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3869                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3870         }
3871       else
3872         {
3873           if (reg_needs_delay (mips_gp_register))
3874             off = 4;
3875           else
3876             off = 0;
3877           frag_grow (32);
3878           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3879                        (int) BFD_RELOC_MIPS_GOT_HI16);
3880           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3881                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3882                        reg, mips_gp_register);
3883           macro_build ((char *) NULL, counter, ep,
3884                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3885                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3886           p = frag_var (rs_machine_dependent, 12 + off, 0,
3887                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3888                                       mips_opts.warn_about_macros),
3889                         ep->X_add_symbol, 0, NULL);
3890           if (off > 0)
3891             {
3892               /* We need a nop before loading from $gp.  This special
3893                  check is required because the lui which starts the main
3894                  instruction stream does not refer to $gp, and so will not
3895                  insert the nop which may be required.  */
3896               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3897                 p += 4;
3898             }
3899           macro_build (p, counter, ep,
3900                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3901                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3902           p += 4;
3903           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3904           p += 4;
3905           macro_build (p, counter, ep,
3906                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3907                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3908         }
3909
3910       if (ex.X_add_number != 0)
3911         {
3912           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3913             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3914           ex.X_op = O_constant;
3915           macro_build ((char *) NULL, counter, &ex,
3916                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3917                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3918         }
3919     }
3920   else if (mips_pic == EMBEDDED_PIC)
3921     {
3922       /* We always do
3923            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3924        */
3925       macro_build ((char *) NULL, counter, ep,
3926                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3927                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3928     }
3929   else
3930     abort ();
3931 }
3932
3933 /* Move the contents of register SOURCE into register DEST.  */
3934
3935 static void
3936 move_register (counter, dest, source)
3937      int *counter;
3938      int dest;
3939      int source;
3940 {
3941   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3942                HAVE_32BIT_GPRS ? "addu" : "daddu",
3943                "d,v,t", dest, source, 0);
3944 }
3945
3946 /*
3947  *                      Build macros
3948  *   This routine implements the seemingly endless macro or synthesized
3949  * instructions and addressing modes in the mips assembly language. Many
3950  * of these macros are simple and are similar to each other. These could
3951  * probably be handled by some kind of table or grammer aproach instead of
3952  * this verbose method. Others are not simple macros but are more like
3953  * optimizing code generation.
3954  *   One interesting optimization is when several store macros appear
3955  * consecutivly that would load AT with the upper half of the same address.
3956  * The ensuing load upper instructions are ommited. This implies some kind
3957  * of global optimization. We currently only optimize within a single macro.
3958  *   For many of the load and store macros if the address is specified as a
3959  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3960  * first load register 'at' with zero and use it as the base register. The
3961  * mips assembler simply uses register $zero. Just one tiny optimization
3962  * we're missing.
3963  */
3964 static void
3965 macro (ip)
3966      struct mips_cl_insn *ip;
3967 {
3968   register int treg, sreg, dreg, breg;
3969   int tempreg;
3970   int mask;
3971   int icnt = 0;
3972   int used_at = 0;
3973   expressionS expr1;
3974   const char *s;
3975   const char *s2;
3976   const char *fmt;
3977   int likely = 0;
3978   int dbl = 0;
3979   int coproc = 0;
3980   int lr = 0;
3981   int imm = 0;
3982   offsetT maxnum;
3983   int off;
3984   bfd_reloc_code_real_type r;
3985   int hold_mips_optimize;
3986
3987   assert (! mips_opts.mips16);
3988
3989   treg = (ip->insn_opcode >> 16) & 0x1f;
3990   dreg = (ip->insn_opcode >> 11) & 0x1f;
3991   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3992   mask = ip->insn_mo->mask;
3993
3994   expr1.X_op = O_constant;
3995   expr1.X_op_symbol = NULL;
3996   expr1.X_add_symbol = NULL;
3997   expr1.X_add_number = 1;
3998
3999   switch (mask)
4000     {
4001     case M_DABS:
4002       dbl = 1;
4003     case M_ABS:
4004       /* bgez $a0,.+12
4005          move v0,$a0
4006          sub v0,$zero,$a0
4007          */
4008
4009       mips_emit_delays (true);
4010       ++mips_opts.noreorder;
4011       mips_any_noreorder = 1;
4012
4013       expr1.X_add_number = 8;
4014       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4015       if (dreg == sreg)
4016         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4017                      0);
4018       else
4019         move_register (&icnt, dreg, sreg);
4020       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4021                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4022
4023       --mips_opts.noreorder;
4024       return;
4025
4026     case M_ADD_I:
4027       s = "addi";
4028       s2 = "add";
4029       goto do_addi;
4030     case M_ADDU_I:
4031       s = "addiu";
4032       s2 = "addu";
4033       goto do_addi;
4034     case M_DADD_I:
4035       dbl = 1;
4036       s = "daddi";
4037       s2 = "dadd";
4038       goto do_addi;
4039     case M_DADDU_I:
4040       dbl = 1;
4041       s = "daddiu";
4042       s2 = "daddu";
4043     do_addi:
4044       if (imm_expr.X_op == O_constant
4045           && imm_expr.X_add_number >= -0x8000
4046           && imm_expr.X_add_number < 0x8000)
4047         {
4048           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4049                        (int) BFD_RELOC_LO16);
4050           return;
4051         }
4052       load_register (&icnt, AT, &imm_expr, dbl);
4053       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4054                    treg, sreg, AT);
4055       break;
4056
4057     case M_AND_I:
4058       s = "andi";
4059       s2 = "and";
4060       goto do_bit;
4061     case M_OR_I:
4062       s = "ori";
4063       s2 = "or";
4064       goto do_bit;
4065     case M_NOR_I:
4066       s = "";
4067       s2 = "nor";
4068       goto do_bit;
4069     case M_XOR_I:
4070       s = "xori";
4071       s2 = "xor";
4072     do_bit:
4073       if (imm_expr.X_op == O_constant
4074           && imm_expr.X_add_number >= 0
4075           && imm_expr.X_add_number < 0x10000)
4076         {
4077           if (mask != M_NOR_I)
4078             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4079                          sreg, (int) BFD_RELOC_LO16);
4080           else
4081             {
4082               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4083                            treg, sreg, (int) BFD_RELOC_LO16);
4084               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4085                            "d,v,t", treg, treg, 0);
4086             }
4087           return;
4088         }
4089
4090       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4091       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4092                    treg, sreg, AT);
4093       break;
4094
4095     case M_BEQ_I:
4096       s = "beq";
4097       goto beq_i;
4098     case M_BEQL_I:
4099       s = "beql";
4100       likely = 1;
4101       goto beq_i;
4102     case M_BNE_I:
4103       s = "bne";
4104       goto beq_i;
4105     case M_BNEL_I:
4106       s = "bnel";
4107       likely = 1;
4108     beq_i:
4109       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4110         {
4111           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4112                        0);
4113           return;
4114         }
4115       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4116       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4117       break;
4118
4119     case M_BGEL:
4120       likely = 1;
4121     case M_BGE:
4122       if (treg == 0)
4123         {
4124           macro_build ((char *) NULL, &icnt, &offset_expr,
4125                        likely ? "bgezl" : "bgez", "s,p", sreg);
4126           return;
4127         }
4128       if (sreg == 0)
4129         {
4130           macro_build ((char *) NULL, &icnt, &offset_expr,
4131                        likely ? "blezl" : "blez", "s,p", treg);
4132           return;
4133         }
4134       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4135                    AT, sreg, treg);
4136       macro_build ((char *) NULL, &icnt, &offset_expr,
4137                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4138       break;
4139
4140     case M_BGTL_I:
4141       likely = 1;
4142     case M_BGT_I:
4143       /* check for > max integer */
4144       maxnum = 0x7fffffff;
4145       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4146         {
4147           maxnum <<= 16;
4148           maxnum |= 0xffff;
4149           maxnum <<= 16;
4150           maxnum |= 0xffff;
4151         }
4152       if (imm_expr.X_op == O_constant
4153           && imm_expr.X_add_number >= maxnum
4154           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4155         {
4156         do_false:
4157           /* result is always false */
4158           if (! likely)
4159             {
4160               if (warn_nops)
4161                 as_warn (_("Branch %s is always false (nop)"),
4162                          ip->insn_mo->name);
4163               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4164                            "", 0);
4165             }
4166           else
4167             {
4168               if (warn_nops)
4169                 as_warn (_("Branch likely %s is always false"),
4170                          ip->insn_mo->name);
4171               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4172                            "s,t,p", 0, 0);
4173             }
4174           return;
4175         }
4176       if (imm_expr.X_op != O_constant)
4177         as_bad (_("Unsupported large constant"));
4178       ++imm_expr.X_add_number;
4179       /* FALLTHROUGH */
4180     case M_BGE_I:
4181     case M_BGEL_I:
4182       if (mask == M_BGEL_I)
4183         likely = 1;
4184       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4185         {
4186           macro_build ((char *) NULL, &icnt, &offset_expr,
4187                        likely ? "bgezl" : "bgez", "s,p", sreg);
4188           return;
4189         }
4190       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4191         {
4192           macro_build ((char *) NULL, &icnt, &offset_expr,
4193                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4194           return;
4195         }
4196       maxnum = 0x7fffffff;
4197       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4198         {
4199           maxnum <<= 16;
4200           maxnum |= 0xffff;
4201           maxnum <<= 16;
4202           maxnum |= 0xffff;
4203         }
4204       maxnum = - maxnum - 1;
4205       if (imm_expr.X_op == O_constant
4206           && imm_expr.X_add_number <= maxnum
4207           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4208         {
4209         do_true:
4210           /* result is always true */
4211           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4212           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4213           return;
4214         }
4215       set_at (&icnt, sreg, 0);
4216       macro_build ((char *) NULL, &icnt, &offset_expr,
4217                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4218       break;
4219
4220     case M_BGEUL:
4221       likely = 1;
4222     case M_BGEU:
4223       if (treg == 0)
4224         goto do_true;
4225       if (sreg == 0)
4226         {
4227           macro_build ((char *) NULL, &icnt, &offset_expr,
4228                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4229           return;
4230         }
4231       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4232                    "d,v,t", AT, sreg, treg);
4233       macro_build ((char *) NULL, &icnt, &offset_expr,
4234                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4235       break;
4236
4237     case M_BGTUL_I:
4238       likely = 1;
4239     case M_BGTU_I:
4240       if (sreg == 0
4241           || (HAVE_32BIT_GPRS
4242               && imm_expr.X_op == O_constant
4243               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4244         goto do_false;
4245       if (imm_expr.X_op != O_constant)
4246         as_bad (_("Unsupported large constant"));
4247       ++imm_expr.X_add_number;
4248       /* FALLTHROUGH */
4249     case M_BGEU_I:
4250     case M_BGEUL_I:
4251       if (mask == M_BGEUL_I)
4252         likely = 1;
4253       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4254         goto do_true;
4255       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4256         {
4257           macro_build ((char *) NULL, &icnt, &offset_expr,
4258                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4259           return;
4260         }
4261       set_at (&icnt, sreg, 1);
4262       macro_build ((char *) NULL, &icnt, &offset_expr,
4263                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4264       break;
4265
4266     case M_BGTL:
4267       likely = 1;
4268     case M_BGT:
4269       if (treg == 0)
4270         {
4271           macro_build ((char *) NULL, &icnt, &offset_expr,
4272                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4273           return;
4274         }
4275       if (sreg == 0)
4276         {
4277           macro_build ((char *) NULL, &icnt, &offset_expr,
4278                        likely ? "bltzl" : "bltz", "s,p", treg);
4279           return;
4280         }
4281       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4282                    AT, treg, sreg);
4283       macro_build ((char *) NULL, &icnt, &offset_expr,
4284                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4285       break;
4286
4287     case M_BGTUL:
4288       likely = 1;
4289     case M_BGTU:
4290       if (treg == 0)
4291         {
4292           macro_build ((char *) NULL, &icnt, &offset_expr,
4293                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4294           return;
4295         }
4296       if (sreg == 0)
4297         goto do_false;
4298       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4299                    "d,v,t", AT, treg, sreg);
4300       macro_build ((char *) NULL, &icnt, &offset_expr,
4301                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4302       break;
4303
4304     case M_BLEL:
4305       likely = 1;
4306     case M_BLE:
4307       if (treg == 0)
4308         {
4309           macro_build ((char *) NULL, &icnt, &offset_expr,
4310                        likely ? "blezl" : "blez", "s,p", sreg);
4311           return;
4312         }
4313       if (sreg == 0)
4314         {
4315           macro_build ((char *) NULL, &icnt, &offset_expr,
4316                        likely ? "bgezl" : "bgez", "s,p", treg);
4317           return;
4318         }
4319       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4320                    AT, treg, sreg);
4321       macro_build ((char *) NULL, &icnt, &offset_expr,
4322                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4323       break;
4324
4325     case M_BLEL_I:
4326       likely = 1;
4327     case M_BLE_I:
4328       maxnum = 0x7fffffff;
4329       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4330         {
4331           maxnum <<= 16;
4332           maxnum |= 0xffff;
4333           maxnum <<= 16;
4334           maxnum |= 0xffff;
4335         }
4336       if (imm_expr.X_op == O_constant
4337           && imm_expr.X_add_number >= maxnum
4338           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4339         goto do_true;
4340       if (imm_expr.X_op != O_constant)
4341         as_bad (_("Unsupported large constant"));
4342       ++imm_expr.X_add_number;
4343       /* FALLTHROUGH */
4344     case M_BLT_I:
4345     case M_BLTL_I:
4346       if (mask == M_BLTL_I)
4347         likely = 1;
4348       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4349         {
4350           macro_build ((char *) NULL, &icnt, &offset_expr,
4351                        likely ? "bltzl" : "bltz", "s,p", sreg);
4352           return;
4353         }
4354       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4355         {
4356           macro_build ((char *) NULL, &icnt, &offset_expr,
4357                        likely ? "blezl" : "blez", "s,p", sreg);
4358           return;
4359         }
4360       set_at (&icnt, sreg, 0);
4361       macro_build ((char *) NULL, &icnt, &offset_expr,
4362                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4363       break;
4364
4365     case M_BLEUL:
4366       likely = 1;
4367     case M_BLEU:
4368       if (treg == 0)
4369         {
4370           macro_build ((char *) NULL, &icnt, &offset_expr,
4371                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4372           return;
4373         }
4374       if (sreg == 0)
4375         goto do_true;
4376       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4377                    "d,v,t", AT, treg, sreg);
4378       macro_build ((char *) NULL, &icnt, &offset_expr,
4379                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4380       break;
4381
4382     case M_BLEUL_I:
4383       likely = 1;
4384     case M_BLEU_I:
4385       if (sreg == 0
4386           || (HAVE_32BIT_GPRS
4387               && imm_expr.X_op == O_constant
4388               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4389         goto do_true;
4390       if (imm_expr.X_op != O_constant)
4391         as_bad (_("Unsupported large constant"));
4392       ++imm_expr.X_add_number;
4393       /* FALLTHROUGH */
4394     case M_BLTU_I:
4395     case M_BLTUL_I:
4396       if (mask == M_BLTUL_I)
4397         likely = 1;
4398       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4399         goto do_false;
4400       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4401         {
4402           macro_build ((char *) NULL, &icnt, &offset_expr,
4403                        likely ? "beql" : "beq",
4404                        "s,t,p", sreg, 0);
4405           return;
4406         }
4407       set_at (&icnt, sreg, 1);
4408       macro_build ((char *) NULL, &icnt, &offset_expr,
4409                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4410       break;
4411
4412     case M_BLTL:
4413       likely = 1;
4414     case M_BLT:
4415       if (treg == 0)
4416         {
4417           macro_build ((char *) NULL, &icnt, &offset_expr,
4418                        likely ? "bltzl" : "bltz", "s,p", sreg);
4419           return;
4420         }
4421       if (sreg == 0)
4422         {
4423           macro_build ((char *) NULL, &icnt, &offset_expr,
4424                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4425           return;
4426         }
4427       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4428                    AT, sreg, treg);
4429       macro_build ((char *) NULL, &icnt, &offset_expr,
4430                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4431       break;
4432
4433     case M_BLTUL:
4434       likely = 1;
4435     case M_BLTU:
4436       if (treg == 0)
4437         goto do_false;
4438       if (sreg == 0)
4439         {
4440           macro_build ((char *) NULL, &icnt, &offset_expr,
4441                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4442           return;
4443         }
4444       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4445                    "d,v,t", AT, sreg,
4446                    treg);
4447       macro_build ((char *) NULL, &icnt, &offset_expr,
4448                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4449       break;
4450
4451     case M_DDIV_3:
4452       dbl = 1;
4453     case M_DIV_3:
4454       s = "mflo";
4455       goto do_div3;
4456     case M_DREM_3:
4457       dbl = 1;
4458     case M_REM_3:
4459       s = "mfhi";
4460     do_div3:
4461       if (treg == 0)
4462         {
4463           as_warn (_("Divide by zero."));
4464           if (mips_trap)
4465             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4466                          "s,t", 0, 0);
4467           else
4468             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4469                          "c", 7);
4470           return;
4471         }
4472
4473       mips_emit_delays (true);
4474       ++mips_opts.noreorder;
4475       mips_any_noreorder = 1;
4476       if (mips_trap)
4477         {
4478           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4479                        "s,t", treg, 0);
4480           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4481                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4482         }
4483       else
4484         {
4485           expr1.X_add_number = 8;
4486           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4487           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4488                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4489           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4490                        "c", 7);
4491         }
4492       expr1.X_add_number = -1;
4493       macro_build ((char *) NULL, &icnt, &expr1,
4494                    dbl ? "daddiu" : "addiu",
4495                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4496       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4497       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4498       if (dbl)
4499         {
4500           expr1.X_add_number = 1;
4501           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4502                        (int) BFD_RELOC_LO16);
4503           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4504                        "d,w,<", AT, AT, 31);
4505         }
4506       else
4507         {
4508           expr1.X_add_number = 0x80000000;
4509           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4510                        (int) BFD_RELOC_HI16);
4511         }
4512       if (mips_trap)
4513         {
4514           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4515                        "s,t", sreg, AT);
4516           /* We want to close the noreorder block as soon as possible, so
4517              that later insns are available for delay slot filling.  */
4518           --mips_opts.noreorder;
4519         }
4520       else
4521         {
4522           expr1.X_add_number = 8;
4523           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4524           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4525                        0);
4526
4527           /* We want to close the noreorder block as soon as possible, so
4528              that later insns are available for delay slot filling.  */
4529           --mips_opts.noreorder;
4530
4531           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4532                        "c", 6);
4533         }
4534       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4535       break;
4536
4537     case M_DIV_3I:
4538       s = "div";
4539       s2 = "mflo";
4540       goto do_divi;
4541     case M_DIVU_3I:
4542       s = "divu";
4543       s2 = "mflo";
4544       goto do_divi;
4545     case M_REM_3I:
4546       s = "div";
4547       s2 = "mfhi";
4548       goto do_divi;
4549     case M_REMU_3I:
4550       s = "divu";
4551       s2 = "mfhi";
4552       goto do_divi;
4553     case M_DDIV_3I:
4554       dbl = 1;
4555       s = "ddiv";
4556       s2 = "mflo";
4557       goto do_divi;
4558     case M_DDIVU_3I:
4559       dbl = 1;
4560       s = "ddivu";
4561       s2 = "mflo";
4562       goto do_divi;
4563     case M_DREM_3I:
4564       dbl = 1;
4565       s = "ddiv";
4566       s2 = "mfhi";
4567       goto do_divi;
4568     case M_DREMU_3I:
4569       dbl = 1;
4570       s = "ddivu";
4571       s2 = "mfhi";
4572     do_divi:
4573       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4574         {
4575           as_warn (_("Divide by zero."));
4576           if (mips_trap)
4577             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4578                          "s,t", 0, 0);
4579           else
4580             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4581                          "c", 7);
4582           return;
4583         }
4584       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4585         {
4586           if (strcmp (s2, "mflo") == 0)
4587             move_register (&icnt, dreg, sreg);
4588           else
4589             move_register (&icnt, dreg, 0);
4590           return;
4591         }
4592       if (imm_expr.X_op == O_constant
4593           && imm_expr.X_add_number == -1
4594           && s[strlen (s) - 1] != 'u')
4595         {
4596           if (strcmp (s2, "mflo") == 0)
4597             {
4598               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4599                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4600             }
4601           else
4602             move_register (&icnt, dreg, 0);
4603           return;
4604         }
4605
4606       load_register (&icnt, AT, &imm_expr, dbl);
4607       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4608                    sreg, AT);
4609       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4610       break;
4611
4612     case M_DIVU_3:
4613       s = "divu";
4614       s2 = "mflo";
4615       goto do_divu3;
4616     case M_REMU_3:
4617       s = "divu";
4618       s2 = "mfhi";
4619       goto do_divu3;
4620     case M_DDIVU_3:
4621       s = "ddivu";
4622       s2 = "mflo";
4623       goto do_divu3;
4624     case M_DREMU_3:
4625       s = "ddivu";
4626       s2 = "mfhi";
4627     do_divu3:
4628       mips_emit_delays (true);
4629       ++mips_opts.noreorder;
4630       mips_any_noreorder = 1;
4631       if (mips_trap)
4632         {
4633           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4634                        "s,t", treg, 0);
4635           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4636                        sreg, treg);
4637           /* We want to close the noreorder block as soon as possible, so
4638              that later insns are available for delay slot filling.  */
4639           --mips_opts.noreorder;
4640         }
4641       else
4642         {
4643           expr1.X_add_number = 8;
4644           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4645           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4646                        sreg, treg);
4647
4648           /* We want to close the noreorder block as soon as possible, so
4649              that later insns are available for delay slot filling.  */
4650           --mips_opts.noreorder;
4651           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4652                        "c", 7);
4653         }
4654       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4655       return;
4656
4657     case M_DLA_AB:
4658       dbl = 1;
4659     case M_LA_AB:
4660       /* Load the address of a symbol into a register.  If breg is not
4661          zero, we then add a base register to it.  */
4662
4663       if (dbl && HAVE_32BIT_GPRS)
4664         as_warn (_("dla used to load 32-bit register"));
4665
4666       if (! dbl && HAVE_64BIT_OBJECTS)
4667         as_warn (_("la used to load 64-bit address"));
4668
4669       if (treg == breg)
4670         {
4671           tempreg = AT;
4672           used_at = 1;
4673         }
4674       else
4675         {
4676           tempreg = treg;
4677           used_at = 0;
4678         }
4679
4680       /* When generating embedded PIC code, we permit expressions of
4681          the form
4682            la   $treg,foo-bar
4683            la   $treg,foo-bar($breg)
4684          where bar is an address in the current section.  These are used
4685          when getting the addresses of functions.  We don't permit
4686          X_add_number to be non-zero, because if the symbol is
4687          external the relaxing code needs to know that any addend is
4688          purely the offset to X_op_symbol.  */
4689       if (mips_pic == EMBEDDED_PIC
4690           && offset_expr.X_op == O_subtract
4691           && (symbol_constant_p (offset_expr.X_op_symbol)
4692               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4693               : (symbol_equated_p (offset_expr.X_op_symbol)
4694                  && (S_GET_SEGMENT
4695                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4696                       ->X_add_symbol)
4697                      == now_seg)))
4698           && (offset_expr.X_add_number == 0
4699               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4700         {
4701           if (breg == 0)
4702             {
4703               tempreg = treg;
4704               used_at = 0;
4705               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4706                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4707             }
4708           else
4709             {
4710               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4711                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4712               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4713                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4714                            "d,v,t", tempreg, tempreg, breg);
4715             }
4716           macro_build ((char *) NULL, &icnt, &offset_expr,
4717                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4718                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4719           if (! used_at)
4720             return;
4721           break;
4722         }
4723
4724       if (offset_expr.X_op != O_symbol
4725           && offset_expr.X_op != O_constant)
4726         {
4727           as_bad (_("expression too complex"));
4728           offset_expr.X_op = O_constant;
4729         }
4730
4731       if (offset_expr.X_op == O_constant)
4732         load_register (&icnt, tempreg, &offset_expr,
4733                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4734                         ? (dbl || HAVE_64BIT_ADDRESSES)
4735                         : HAVE_64BIT_ADDRESSES));
4736       else if (mips_pic == NO_PIC)
4737         {
4738           /* If this is a reference to a GP relative symbol, we want
4739                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4740              Otherwise we want
4741                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4742                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4743              If we have a constant, we need two instructions anyhow,
4744              so we may as well always use the latter form.
4745
4746             With 64bit address space and a usable $at we want
4747               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4748               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4749               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4750               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4751               dsll32    $tempreg,0
4752               dadd      $tempreg,$tempreg,$at
4753
4754             If $at is already in use, we use an path which is suboptimal
4755             on superscalar processors.
4756               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4757               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4758               dsll      $tempreg,16
4759               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4760               dsll      $tempreg,16
4761               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4762           */
4763           char *p = NULL;
4764           if (HAVE_64BIT_ADDRESSES)
4765             {
4766               /* We don't do GP optimization for now because RELAX_ENCODE can't
4767                  hold the data for such large chunks.  */
4768
4769               if (used_at == 0)
4770                 {
4771                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4772                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4773                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4774                                AT, (int) BFD_RELOC_HI16_S);
4775                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4776                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4777                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4778                                AT, AT, (int) BFD_RELOC_LO16);
4779                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4780                                "d,w,<", tempreg, tempreg, 0);
4781                   macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4782                                tempreg, tempreg, AT);
4783                   used_at = 1;
4784                 }
4785               else
4786                 {
4787                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4788                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4789                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4790                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4791                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4792                                tempreg, tempreg, 16);
4793                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4794                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4795                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4796                                tempreg, tempreg, 16);
4797                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4798                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4799                 }
4800             }
4801           else
4802             {
4803               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4804                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4805                 {
4806                   frag_grow (20);
4807                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4808                                "t,r,j", tempreg, mips_gp_register,
4809                                (int) BFD_RELOC_GPREL16);
4810                   p = frag_var (rs_machine_dependent, 8, 0,
4811                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4812                                               mips_opts.warn_about_macros),
4813                                 offset_expr.X_add_symbol, 0, NULL);
4814                 }
4815               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4816               if (p != NULL)
4817                 p += 4;
4818               macro_build (p, &icnt, &offset_expr, "addiu",
4819                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4820             }
4821         }
4822       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4823         {
4824           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4825
4826           /* If this is a reference to an external symbol, and there
4827              is no constant, we want
4828                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4829              or if tempreg is PIC_CALL_REG
4830                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4831              For a local symbol, we want
4832                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4833                nop
4834                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4835
4836              If we have a small constant, and this is a reference to
4837              an external symbol, we want
4838                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4839                nop
4840                addiu    $tempreg,$tempreg,<constant>
4841              For a local symbol, we want the same instruction
4842              sequence, but we output a BFD_RELOC_LO16 reloc on the
4843              addiu instruction.
4844
4845              If we have a large constant, and this is a reference to
4846              an external symbol, we want
4847                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4848                lui      $at,<hiconstant>
4849                addiu    $at,$at,<loconstant>
4850                addu     $tempreg,$tempreg,$at
4851              For a local symbol, we want the same instruction
4852              sequence, but we output a BFD_RELOC_LO16 reloc on the
4853              addiu instruction.  */
4854           expr1.X_add_number = offset_expr.X_add_number;
4855           offset_expr.X_add_number = 0;
4856           frag_grow (32);
4857           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4858             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4859           macro_build ((char *) NULL, &icnt, &offset_expr,
4860                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4861                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4862           if (expr1.X_add_number == 0)
4863             {
4864               int off;
4865               char *p;
4866
4867               if (breg == 0)
4868                 off = 0;
4869               else
4870                 {
4871                   /* We're going to put in an addu instruction using
4872                      tempreg, so we may as well insert the nop right
4873                      now.  */
4874                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4875                                "nop", "");
4876                   off = 4;
4877                 }
4878               p = frag_var (rs_machine_dependent, 8 - off, 0,
4879                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4880                                           (breg == 0
4881                                            ? mips_opts.warn_about_macros
4882                                            : 0)),
4883                             offset_expr.X_add_symbol, 0, NULL);
4884               if (breg == 0)
4885                 {
4886                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4887                   p += 4;
4888                 }
4889               macro_build (p, &icnt, &expr1,
4890                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4891                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4892               /* FIXME: If breg == 0, and the next instruction uses
4893                  $tempreg, then if this variant case is used an extra
4894                  nop will be generated.  */
4895             }
4896           else if (expr1.X_add_number >= -0x8000
4897                    && expr1.X_add_number < 0x8000)
4898             {
4899               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4900                            "nop", "");
4901               macro_build ((char *) NULL, &icnt, &expr1,
4902                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4903                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4904               frag_var (rs_machine_dependent, 0, 0,
4905                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4906                         offset_expr.X_add_symbol, 0, NULL);
4907             }
4908           else
4909             {
4910               int off1;
4911
4912               /* If we are going to add in a base register, and the
4913                  target register and the base register are the same,
4914                  then we are using AT as a temporary register.  Since
4915                  we want to load the constant into AT, we add our
4916                  current AT (from the global offset table) and the
4917                  register into the register now, and pretend we were
4918                  not using a base register.  */
4919               if (breg != treg)
4920                 off1 = 0;
4921               else
4922                 {
4923                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4924                                "nop", "");
4925                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4926                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4927                                "d,v,t", treg, AT, breg);
4928                   breg = 0;
4929                   tempreg = treg;
4930                   off1 = -8;
4931                 }
4932
4933               /* Set mips_optimize around the lui instruction to avoid
4934                  inserting an unnecessary nop after the lw.  */
4935               hold_mips_optimize = mips_optimize;
4936               mips_optimize = 2;
4937               macro_build_lui (NULL, &icnt, &expr1, AT);
4938               mips_optimize = hold_mips_optimize;
4939
4940               macro_build ((char *) NULL, &icnt, &expr1,
4941                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4942                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4943               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4944                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4945                            "d,v,t", tempreg, tempreg, AT);
4946               frag_var (rs_machine_dependent, 0, 0,
4947                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4948                         offset_expr.X_add_symbol, 0, NULL);
4949               used_at = 1;
4950             }
4951         }
4952       else if (mips_pic == SVR4_PIC)
4953         {
4954           int gpdel;
4955           char *p;
4956           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4957           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4958
4959           /* This is the large GOT case.  If this is a reference to an
4960              external symbol, and there is no constant, we want
4961                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4962                addu     $tempreg,$tempreg,$gp
4963                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4964              or if tempreg is PIC_CALL_REG
4965                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4966                addu     $tempreg,$tempreg,$gp
4967                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4968              For a local symbol, we want
4969                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4970                nop
4971                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4972
4973              If we have a small constant, and this is a reference to
4974              an external symbol, we want
4975                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4976                addu     $tempreg,$tempreg,$gp
4977                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4978                nop
4979                addiu    $tempreg,$tempreg,<constant>
4980              For a local symbol, we want
4981                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4982                nop
4983                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4984
4985              If we have a large constant, and this is a reference to
4986              an external symbol, we want
4987                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4988                addu     $tempreg,$tempreg,$gp
4989                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4990                lui      $at,<hiconstant>
4991                addiu    $at,$at,<loconstant>
4992                addu     $tempreg,$tempreg,$at
4993              For a local symbol, we want
4994                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4995                lui      $at,<hiconstant>
4996                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4997                addu     $tempreg,$tempreg,$at
4998
4999              For NewABI, we want for data addresses
5000                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5001              If tempreg is PIC_CALL_REG pointing to a external symbol, we want
5002                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5003            */
5004           if (HAVE_NEWABI)
5005             {
5006               int reloc_type = (tempreg == PIC_CALL_REG
5007                                 ? BFD_RELOC_MIPS_CALL16
5008                                 : BFD_RELOC_MIPS_GOT_DISP);
5009
5010               macro_build ((char *) NULL, &icnt, &offset_expr,
5011                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5012                            "t,o(b)", tempreg, reloc_type, mips_gp_register);
5013
5014               if (breg != 0)
5015                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5016                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5017                              "d,v,t", treg, tempreg, breg);
5018
5019               if (! used_at)
5020                 return;
5021
5022               break;
5023             }
5024           expr1.X_add_number = offset_expr.X_add_number;
5025           offset_expr.X_add_number = 0;
5026           frag_grow (52);
5027           if (reg_needs_delay (mips_gp_register))
5028             gpdel = 4;
5029           else
5030             gpdel = 0;
5031           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5032             {
5033               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5034               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5035             }
5036           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5037                        tempreg, lui_reloc_type);
5038           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5039                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5040                        "d,v,t", tempreg, tempreg, mips_gp_register);
5041           macro_build ((char *) NULL, &icnt, &offset_expr,
5042                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5043                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5044           if (expr1.X_add_number == 0)
5045             {
5046               int off;
5047
5048               if (breg == 0)
5049                 off = 0;
5050               else
5051                 {
5052                   /* We're going to put in an addu instruction using
5053                      tempreg, so we may as well insert the nop right
5054                      now.  */
5055                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5056                                "nop", "");
5057                   off = 4;
5058                 }
5059
5060               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5061                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5062                                           8 + gpdel, 0,
5063                                           (breg == 0
5064                                            ? mips_opts.warn_about_macros
5065                                            : 0)),
5066                             offset_expr.X_add_symbol, 0, NULL);
5067             }
5068           else if (expr1.X_add_number >= -0x8000
5069                    && expr1.X_add_number < 0x8000)
5070             {
5071               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5072                            "nop", "");
5073               macro_build ((char *) NULL, &icnt, &expr1,
5074                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5075                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5076
5077               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5078                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5079                                           (breg == 0
5080                                            ? mips_opts.warn_about_macros
5081                                            : 0)),
5082                             offset_expr.X_add_symbol, 0, NULL);
5083             }
5084           else
5085             {
5086               int adj, dreg;
5087
5088               /* If we are going to add in a base register, and the
5089                  target register and the base register are the same,
5090                  then we are using AT as a temporary register.  Since
5091                  we want to load the constant into AT, we add our
5092                  current AT (from the global offset table) and the
5093                  register into the register now, and pretend we were
5094                  not using a base register.  */
5095               if (breg != treg)
5096                 {
5097                   adj = 0;
5098                   dreg = tempreg;
5099                 }
5100               else
5101                 {
5102                   assert (tempreg == AT);
5103                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5104                                "nop", "");
5105                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5106                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5107                                "d,v,t", treg, AT, breg);
5108                   dreg = treg;
5109                   adj = 8;
5110                 }
5111
5112               /* Set mips_optimize around the lui instruction to avoid
5113                  inserting an unnecessary nop after the lw.  */
5114               hold_mips_optimize = mips_optimize;
5115               mips_optimize = 2;
5116               macro_build_lui (NULL, &icnt, &expr1, AT);
5117               mips_optimize = hold_mips_optimize;
5118
5119               macro_build ((char *) NULL, &icnt, &expr1,
5120                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5121                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5122               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5123                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5124                            "d,v,t", dreg, dreg, AT);
5125
5126               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5127                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5128                                           8 + gpdel, 0,
5129                                           (breg == 0
5130                                            ? mips_opts.warn_about_macros
5131                                            : 0)),
5132                             offset_expr.X_add_symbol, 0, NULL);
5133
5134               used_at = 1;
5135             }
5136
5137           if (gpdel > 0)
5138             {
5139               /* This is needed because this instruction uses $gp, but
5140                  the first instruction on the main stream does not.  */
5141               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5142               p += 4;
5143             }
5144           macro_build (p, &icnt, &offset_expr,
5145                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5146                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5147                        mips_gp_register);
5148           p += 4;
5149           if (expr1.X_add_number >= -0x8000
5150               && expr1.X_add_number < 0x8000)
5151             {
5152               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5153               p += 4;
5154               macro_build (p, &icnt, &expr1,
5155                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5156                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5157               /* FIXME: If add_number is 0, and there was no base
5158                  register, the external symbol case ended with a load,
5159                  so if the symbol turns out to not be external, and
5160                  the next instruction uses tempreg, an unnecessary nop
5161                  will be inserted.  */
5162             }
5163           else
5164             {
5165               if (breg == treg)
5166                 {
5167                   /* We must add in the base register now, as in the
5168                      external symbol case.  */
5169                   assert (tempreg == AT);
5170                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5171                   p += 4;
5172                   macro_build (p, &icnt, (expressionS *) NULL,
5173                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5174                                "d,v,t", treg, AT, breg);
5175                   p += 4;
5176                   tempreg = treg;
5177                   /* We set breg to 0 because we have arranged to add
5178                      it in in both cases.  */
5179                   breg = 0;
5180                 }
5181
5182               macro_build_lui (p, &icnt, &expr1, AT);
5183               p += 4;
5184               macro_build (p, &icnt, &expr1,
5185                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5186                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5187               p += 4;
5188               macro_build (p, &icnt, (expressionS *) NULL,
5189                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5190                            "d,v,t", tempreg, tempreg, AT);
5191               p += 4;
5192             }
5193         }
5194       else if (mips_pic == EMBEDDED_PIC)
5195         {
5196           /* We use
5197                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5198              */
5199           macro_build ((char *) NULL, &icnt, &offset_expr,
5200                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5201                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5202         }
5203       else
5204         abort ();
5205
5206       if (breg != 0)
5207         {
5208           char *s;
5209
5210           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5211             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5212           else
5213             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5214
5215           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5216                        "d,v,t", treg, tempreg, breg);
5217         }
5218
5219       if (! used_at)
5220         return;
5221
5222       break;
5223
5224     case M_J_A:
5225       /* The j instruction may not be used in PIC code, since it
5226          requires an absolute address.  We convert it to a b
5227          instruction.  */
5228       if (mips_pic == NO_PIC)
5229         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5230       else
5231         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5232       return;
5233
5234       /* The jal instructions must be handled as macros because when
5235          generating PIC code they expand to multi-instruction
5236          sequences.  Normally they are simple instructions.  */
5237     case M_JAL_1:
5238       dreg = RA;
5239       /* Fall through.  */
5240     case M_JAL_2:
5241       if (mips_pic == NO_PIC
5242           || mips_pic == EMBEDDED_PIC)
5243         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5244                      "d,s", dreg, sreg);
5245       else if (mips_pic == SVR4_PIC)
5246         {
5247           if (sreg != PIC_CALL_REG)
5248             as_warn (_("MIPS PIC call to register other than $25"));
5249
5250           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5251                        "d,s", dreg, sreg);
5252           if (! HAVE_NEWABI)
5253             {
5254               if (mips_cprestore_offset < 0)
5255                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5256               else
5257                 {
5258                   if (! mips_frame_reg_valid)
5259                     {
5260                       as_warn (_("No .frame pseudo-op used in PIC code"));
5261                       /* Quiet this warning.  */
5262                       mips_frame_reg_valid = 1;
5263                     }
5264                   if (! mips_cprestore_valid)
5265                     {
5266                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5267                       /* Quiet this warning.  */
5268                       mips_cprestore_valid = 1;
5269                     }
5270                   expr1.X_add_number = mips_cprestore_offset;
5271                   macro_build ((char *) NULL, &icnt, &expr1,
5272                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5273                                mips_gp_register, (int) BFD_RELOC_LO16,
5274                                mips_frame_reg);
5275                 }
5276             }
5277         }
5278       else
5279         abort ();
5280
5281       return;
5282
5283     case M_JAL_A:
5284       if (mips_pic == NO_PIC)
5285         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5286       else if (mips_pic == SVR4_PIC)
5287         {
5288           char *p;
5289
5290           /* If this is a reference to an external symbol, and we are
5291              using a small GOT, we want
5292                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5293                nop
5294                jalr     $ra,$25
5295                nop
5296                lw       $gp,cprestore($sp)
5297              The cprestore value is set using the .cprestore
5298              pseudo-op.  If we are using a big GOT, we want
5299                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5300                addu     $25,$25,$gp
5301                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5302                nop
5303                jalr     $ra,$25
5304                nop
5305                lw       $gp,cprestore($sp)
5306              If the symbol is not external, we want
5307                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5308                nop
5309                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5310                jalr     $ra,$25
5311                nop
5312                lw $gp,cprestore($sp)
5313              For NewABI, we want
5314                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5315                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5316            */
5317           if (HAVE_NEWABI)
5318             {
5319               macro_build ((char *) NULL, &icnt, &offset_expr,
5320                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5321                            "t,o(b)", PIC_CALL_REG,
5322                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5323               macro_build_jalr (icnt, &offset_expr);
5324             }
5325           else
5326             {
5327               frag_grow (40);
5328               if (! mips_big_got)
5329                 {
5330                   macro_build ((char *) NULL, &icnt, &offset_expr,
5331                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5332                                "t,o(b)", PIC_CALL_REG,
5333                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5334                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5335                                "nop", "");
5336                   p = frag_var (rs_machine_dependent, 4, 0,
5337                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5338                                 offset_expr.X_add_symbol, 0, NULL);
5339                 }
5340               else
5341                 {
5342                   int gpdel;
5343
5344                   if (reg_needs_delay (mips_gp_register))
5345                     gpdel = 4;
5346                   else
5347                     gpdel = 0;
5348                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5349                                "t,u", PIC_CALL_REG,
5350                                (int) BFD_RELOC_MIPS_CALL_HI16);
5351                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5352                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5353                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5354                                mips_gp_register);
5355                   macro_build ((char *) NULL, &icnt, &offset_expr,
5356                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5357                                "t,o(b)", PIC_CALL_REG,
5358                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5359                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5360                                "nop", "");
5361                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5362                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5363                                               8 + gpdel, 0, 0),
5364                                 offset_expr.X_add_symbol, 0, NULL);
5365                   if (gpdel > 0)
5366                     {
5367                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5368                       p += 4;
5369                     }
5370                   macro_build (p, &icnt, &offset_expr,
5371                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5372                                "t,o(b)", PIC_CALL_REG,
5373                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5374                   p += 4;
5375                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5376                   p += 4;
5377                 }
5378               macro_build (p, &icnt, &offset_expr,
5379                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5380                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5381                            (int) BFD_RELOC_LO16);
5382               macro_build_jalr (icnt, &offset_expr);
5383
5384               if (mips_cprestore_offset < 0)
5385                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5386               else
5387                 {
5388                   if (! mips_frame_reg_valid)
5389                     {
5390                       as_warn (_("No .frame pseudo-op used in PIC code"));
5391                       /* Quiet this warning.  */
5392                       mips_frame_reg_valid = 1;
5393                     }
5394                   if (! mips_cprestore_valid)
5395                     {
5396                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5397                       /* Quiet this warning.  */
5398                       mips_cprestore_valid = 1;
5399                     }
5400                   if (mips_opts.noreorder)
5401                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5402                                  "nop", "");
5403                   expr1.X_add_number = mips_cprestore_offset;
5404                   macro_build ((char *) NULL, &icnt, &expr1,
5405                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5406                                mips_gp_register, (int) BFD_RELOC_LO16,
5407                                mips_frame_reg);
5408                 }
5409             }
5410         }
5411       else if (mips_pic == EMBEDDED_PIC)
5412         {
5413           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5414           /* The linker may expand the call to a longer sequence which
5415              uses $at, so we must break rather than return.  */
5416           break;
5417         }
5418       else
5419         abort ();
5420
5421       return;
5422
5423     case M_LB_AB:
5424       s = "lb";
5425       goto ld;
5426     case M_LBU_AB:
5427       s = "lbu";
5428       goto ld;
5429     case M_LH_AB:
5430       s = "lh";
5431       goto ld;
5432     case M_LHU_AB:
5433       s = "lhu";
5434       goto ld;
5435     case M_LW_AB:
5436       s = "lw";
5437       goto ld;
5438     case M_LWC0_AB:
5439       s = "lwc0";
5440       /* Itbl support may require additional care here.  */
5441       coproc = 1;
5442       goto ld;
5443     case M_LWC1_AB:
5444       s = "lwc1";
5445       /* Itbl support may require additional care here.  */
5446       coproc = 1;
5447       goto ld;
5448     case M_LWC2_AB:
5449       s = "lwc2";
5450       /* Itbl support may require additional care here.  */
5451       coproc = 1;
5452       goto ld;
5453     case M_LWC3_AB:
5454       s = "lwc3";
5455       /* Itbl support may require additional care here.  */
5456       coproc = 1;
5457       goto ld;
5458     case M_LWL_AB:
5459       s = "lwl";
5460       lr = 1;
5461       goto ld;
5462     case M_LWR_AB:
5463       s = "lwr";
5464       lr = 1;
5465       goto ld;
5466     case M_LDC1_AB:
5467       if (mips_arch == CPU_R4650)
5468         {
5469           as_bad (_("opcode not supported on this processor"));
5470           return;
5471         }
5472       s = "ldc1";
5473       /* Itbl support may require additional care here.  */
5474       coproc = 1;
5475       goto ld;
5476     case M_LDC2_AB:
5477       s = "ldc2";
5478       /* Itbl support may require additional care here.  */
5479       coproc = 1;
5480       goto ld;
5481     case M_LDC3_AB:
5482       s = "ldc3";
5483       /* Itbl support may require additional care here.  */
5484       coproc = 1;
5485       goto ld;
5486     case M_LDL_AB:
5487       s = "ldl";
5488       lr = 1;
5489       goto ld;
5490     case M_LDR_AB:
5491       s = "ldr";
5492       lr = 1;
5493       goto ld;
5494     case M_LL_AB:
5495       s = "ll";
5496       goto ld;
5497     case M_LLD_AB:
5498       s = "lld";
5499       goto ld;
5500     case M_LWU_AB:
5501       s = "lwu";
5502     ld:
5503       if (breg == treg || coproc || lr)
5504         {
5505           tempreg = AT;
5506           used_at = 1;
5507         }
5508       else
5509         {
5510           tempreg = treg;
5511           used_at = 0;
5512         }
5513       goto ld_st;
5514     case M_SB_AB:
5515       s = "sb";
5516       goto st;
5517     case M_SH_AB:
5518       s = "sh";
5519       goto st;
5520     case M_SW_AB:
5521       s = "sw";
5522       goto st;
5523     case M_SWC0_AB:
5524       s = "swc0";
5525       /* Itbl support may require additional care here.  */
5526       coproc = 1;
5527       goto st;
5528     case M_SWC1_AB:
5529       s = "swc1";
5530       /* Itbl support may require additional care here.  */
5531       coproc = 1;
5532       goto st;
5533     case M_SWC2_AB:
5534       s = "swc2";
5535       /* Itbl support may require additional care here.  */
5536       coproc = 1;
5537       goto st;
5538     case M_SWC3_AB:
5539       s = "swc3";
5540       /* Itbl support may require additional care here.  */
5541       coproc = 1;
5542       goto st;
5543     case M_SWL_AB:
5544       s = "swl";
5545       goto st;
5546     case M_SWR_AB:
5547       s = "swr";
5548       goto st;
5549     case M_SC_AB:
5550       s = "sc";
5551       goto st;
5552     case M_SCD_AB:
5553       s = "scd";
5554       goto st;
5555     case M_SDC1_AB:
5556       if (mips_arch == CPU_R4650)
5557         {
5558           as_bad (_("opcode not supported on this processor"));
5559           return;
5560         }
5561       s = "sdc1";
5562       coproc = 1;
5563       /* Itbl support may require additional care here.  */
5564       goto st;
5565     case M_SDC2_AB:
5566       s = "sdc2";
5567       /* Itbl support may require additional care here.  */
5568       coproc = 1;
5569       goto st;
5570     case M_SDC3_AB:
5571       s = "sdc3";
5572       /* Itbl support may require additional care here.  */
5573       coproc = 1;
5574       goto st;
5575     case M_SDL_AB:
5576       s = "sdl";
5577       goto st;
5578     case M_SDR_AB:
5579       s = "sdr";
5580     st:
5581       tempreg = AT;
5582       used_at = 1;
5583     ld_st:
5584       /* Itbl support may require additional care here.  */
5585       if (mask == M_LWC1_AB
5586           || mask == M_SWC1_AB
5587           || mask == M_LDC1_AB
5588           || mask == M_SDC1_AB
5589           || mask == M_L_DAB
5590           || mask == M_S_DAB)
5591         fmt = "T,o(b)";
5592       else if (coproc)
5593         fmt = "E,o(b)";
5594       else
5595         fmt = "t,o(b)";
5596
5597       /* For embedded PIC, we allow loads where the offset is calculated
5598          by subtracting a symbol in the current segment from an unknown
5599          symbol, relative to a base register, e.g.:
5600                 <op>    $treg, <sym>-<localsym>($breg)
5601          This is used by the compiler for switch statements.  */
5602       if (mips_pic == EMBEDDED_PIC
5603           && offset_expr.X_op == O_subtract
5604           && (symbol_constant_p (offset_expr.X_op_symbol)
5605               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5606               : (symbol_equated_p (offset_expr.X_op_symbol)
5607                  && (S_GET_SEGMENT
5608                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5609                       ->X_add_symbol)
5610                      == now_seg)))
5611           && breg != 0
5612           && (offset_expr.X_add_number == 0
5613               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5614         {
5615           /* For this case, we output the instructions:
5616                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5617                 addiu   $tempreg,$tempreg,$breg
5618                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5619              If the relocation would fit entirely in 16 bits, it would be
5620              nice to emit:
5621                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5622              instead, but that seems quite difficult.  */
5623           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5624                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5625           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5626                        ((bfd_arch_bits_per_address (stdoutput) == 32
5627                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5628                         ? "addu" : "daddu"),
5629                        "d,v,t", tempreg, tempreg, breg);
5630           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5631                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5632           if (! used_at)
5633             return;
5634           break;
5635         }
5636
5637       if (offset_expr.X_op != O_constant
5638           && offset_expr.X_op != O_symbol)
5639         {
5640           as_bad (_("expression too complex"));
5641           offset_expr.X_op = O_constant;
5642         }
5643
5644       /* A constant expression in PIC code can be handled just as it
5645          is in non PIC code.  */
5646       if (mips_pic == NO_PIC
5647           || offset_expr.X_op == O_constant)
5648         {
5649           char *p;
5650
5651           /* If this is a reference to a GP relative symbol, and there
5652              is no base register, we want
5653                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5654              Otherwise, if there is no base register, we want
5655                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5656                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5657              If we have a constant, we need two instructions anyhow,
5658              so we always use the latter form.
5659
5660              If we have a base register, and this is a reference to a
5661              GP relative symbol, we want
5662                addu     $tempreg,$breg,$gp
5663                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5664              Otherwise we want
5665                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5666                addu     $tempreg,$tempreg,$breg
5667                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5668              With a constant we always use the latter case.
5669
5670              With 64bit address space and no base register and $at usable,
5671              we want
5672                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5673                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5674                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5675                dsll32   $tempreg,0
5676                daddu    $tempreg,$at
5677                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5678              If we have a base register, we want
5679                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5680                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5681                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5682                daddu    $at,$breg
5683                dsll32   $tempreg,0
5684                daddu    $tempreg,$at
5685                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5686
5687              Without $at we can't generate the optimal path for superscalar
5688              processors here since this would require two temporary registers.
5689                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5690                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5691                dsll     $tempreg,16
5692                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5693                dsll     $tempreg,16
5694                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5695              If we have a base register, we want
5696                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5697                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5698                dsll     $tempreg,16
5699                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5700                dsll     $tempreg,16
5701                daddu    $tempreg,$tempreg,$breg
5702                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5703
5704              If we have 64-bit addresses, as an optimization, for
5705              addresses which are 32-bit constants (e.g. kseg0/kseg1
5706              addresses) we fall back to the 32-bit address generation
5707              mechanism since it is more efficient.  This code should
5708              probably attempt to generate 64-bit constants more
5709              efficiently in general.
5710            */
5711           if (HAVE_64BIT_ADDRESSES
5712               && !(offset_expr.X_op == O_constant
5713                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
5714             {
5715               p = NULL;
5716
5717               /* We don't do GP optimization for now because RELAX_ENCODE can't
5718                  hold the data for such large chunks.  */
5719
5720               if (used_at == 0)
5721                 {
5722                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5723                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5724                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5725                                AT, (int) BFD_RELOC_HI16_S);
5726                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5727                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5728                   if (breg != 0)
5729                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5730                                  "d,v,t", AT, AT, breg);
5731                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5732                                "d,w,<", tempreg, tempreg, 0);
5733                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5734                                "d,v,t", tempreg, tempreg, AT);
5735                   macro_build (p, &icnt, &offset_expr, s,
5736                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5737                   used_at = 1;
5738                 }
5739               else
5740                 {
5741                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5742                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5743                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5744                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5745                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5746                                "d,w,<", tempreg, tempreg, 16);
5747                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5748                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5749                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5750                                "d,w,<", tempreg, tempreg, 16);
5751                   if (breg != 0)
5752                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5753                                  "d,v,t", tempreg, tempreg, breg);
5754                   macro_build (p, &icnt, &offset_expr, s,
5755                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5756                 }
5757
5758               return;
5759             }
5760
5761           if (breg == 0)
5762             {
5763               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5764                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5765                 p = NULL;
5766               else
5767                 {
5768                   frag_grow (20);
5769                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5770                                treg, (int) BFD_RELOC_GPREL16,
5771                                mips_gp_register);
5772                   p = frag_var (rs_machine_dependent, 8, 0,
5773                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5774                                               (mips_opts.warn_about_macros
5775                                                || (used_at
5776                                                    && mips_opts.noat))),
5777                                 offset_expr.X_add_symbol, 0, NULL);
5778                   used_at = 0;
5779                 }
5780               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5781               if (p != NULL)
5782                 p += 4;
5783               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5784                            (int) BFD_RELOC_LO16, tempreg);
5785             }
5786           else
5787             {
5788               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5789                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5790                 p = NULL;
5791               else
5792                 {
5793                   frag_grow (28);
5794                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5795                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5796                                "d,v,t", tempreg, breg, mips_gp_register);
5797                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5798                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5799                   p = frag_var (rs_machine_dependent, 12, 0,
5800                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5801                                 offset_expr.X_add_symbol, 0, NULL);
5802                 }
5803               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5804               if (p != NULL)
5805                 p += 4;
5806               macro_build (p, &icnt, (expressionS *) NULL,
5807                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5808                            "d,v,t", tempreg, tempreg, breg);
5809               if (p != NULL)
5810                 p += 4;
5811               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5812                            (int) BFD_RELOC_LO16, tempreg);
5813             }
5814         }
5815       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5816         {
5817           char *p;
5818
5819           /* If this is a reference to an external symbol, we want
5820                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5821                nop
5822                <op>     $treg,0($tempreg)
5823              Otherwise we want
5824                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5825                nop
5826                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5827                <op>     $treg,0($tempreg)
5828              If there is a base register, we add it to $tempreg before
5829              the <op>.  If there is a constant, we stick it in the
5830              <op> instruction.  We don't handle constants larger than
5831              16 bits, because we have no way to load the upper 16 bits
5832              (actually, we could handle them for the subset of cases
5833              in which we are not using $at).  */
5834           assert (offset_expr.X_op == O_symbol);
5835           expr1.X_add_number = offset_expr.X_add_number;
5836           offset_expr.X_add_number = 0;
5837           if (expr1.X_add_number < -0x8000
5838               || expr1.X_add_number >= 0x8000)
5839             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5840           frag_grow (20);
5841           macro_build ((char *) NULL, &icnt, &offset_expr,
5842                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5843                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5844           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5845           p = frag_var (rs_machine_dependent, 4, 0,
5846                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5847                         offset_expr.X_add_symbol, 0, NULL);
5848           macro_build (p, &icnt, &offset_expr,
5849                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5850                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5851           if (breg != 0)
5852             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5853                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5854                          "d,v,t", tempreg, tempreg, breg);
5855           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5856                        (int) BFD_RELOC_LO16, tempreg);
5857         }
5858       else if (mips_pic == SVR4_PIC)
5859         {
5860           int gpdel;
5861           char *p;
5862
5863           /* If this is a reference to an external symbol, we want
5864                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5865                addu     $tempreg,$tempreg,$gp
5866                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5867                <op>     $treg,0($tempreg)
5868              Otherwise we want
5869                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5870                nop
5871                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5872                <op>     $treg,0($tempreg)
5873              If there is a base register, we add it to $tempreg before
5874              the <op>.  If there is a constant, we stick it in the
5875              <op> instruction.  We don't handle constants larger than
5876              16 bits, because we have no way to load the upper 16 bits
5877              (actually, we could handle them for the subset of cases
5878              in which we are not using $at).
5879
5880              For NewABI, we want
5881                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5882                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5883                <op>     $treg,0($tempreg)
5884            */
5885           assert (offset_expr.X_op == O_symbol);
5886           expr1.X_add_number = offset_expr.X_add_number;
5887           offset_expr.X_add_number = 0;
5888           if (expr1.X_add_number < -0x8000
5889               || expr1.X_add_number >= 0x8000)
5890             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5891           if (HAVE_NEWABI)
5892             {
5893               macro_build ((char *) NULL, &icnt, &offset_expr,
5894                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5895                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5896                            mips_gp_register);
5897               macro_build ((char *) NULL, &icnt, &offset_expr,
5898                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5899                            "t,r,j", tempreg, tempreg,
5900                            BFD_RELOC_MIPS_GOT_OFST);
5901               if (breg != 0)
5902                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5903                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5904                              "d,v,t", tempreg, tempreg, breg);
5905               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5906                            (int) BFD_RELOC_LO16, tempreg);
5907
5908               if (! used_at)
5909                 return;
5910
5911               break;
5912             }
5913           if (reg_needs_delay (mips_gp_register))
5914             gpdel = 4;
5915           else
5916             gpdel = 0;
5917           frag_grow (36);
5918           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5919                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5920           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5921                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5922                        "d,v,t", tempreg, tempreg, mips_gp_register);
5923           macro_build ((char *) NULL, &icnt, &offset_expr,
5924                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5925                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5926                        tempreg);
5927           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5928                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5929                         offset_expr.X_add_symbol, 0, NULL);
5930           if (gpdel > 0)
5931             {
5932               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5933               p += 4;
5934             }
5935           macro_build (p, &icnt, &offset_expr,
5936                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5937                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5938                        mips_gp_register);
5939           p += 4;
5940           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5941           p += 4;
5942           macro_build (p, &icnt, &offset_expr,
5943                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5944                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5945           if (breg != 0)
5946             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5947                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5948                          "d,v,t", tempreg, tempreg, breg);
5949           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5950                        (int) BFD_RELOC_LO16, tempreg);
5951         }
5952       else if (mips_pic == EMBEDDED_PIC)
5953         {
5954           /* If there is no base register, we want
5955                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5956              If there is a base register, we want
5957                addu     $tempreg,$breg,$gp
5958                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5959              */
5960           assert (offset_expr.X_op == O_symbol);
5961           if (breg == 0)
5962             {
5963               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5964                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5965               used_at = 0;
5966             }
5967           else
5968             {
5969               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5970                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5971                            "d,v,t", tempreg, breg, mips_gp_register);
5972               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5973                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5974             }
5975         }
5976       else
5977         abort ();
5978
5979       if (! used_at)
5980         return;
5981
5982       break;
5983
5984     case M_LI:
5985     case M_LI_S:
5986       load_register (&icnt, treg, &imm_expr, 0);
5987       return;
5988
5989     case M_DLI:
5990       load_register (&icnt, treg, &imm_expr, 1);
5991       return;
5992
5993     case M_LI_SS:
5994       if (imm_expr.X_op == O_constant)
5995         {
5996           load_register (&icnt, AT, &imm_expr, 0);
5997           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5998                        "mtc1", "t,G", AT, treg);
5999           break;
6000         }
6001       else
6002         {
6003           assert (offset_expr.X_op == O_symbol
6004                   && strcmp (segment_name (S_GET_SEGMENT
6005                                            (offset_expr.X_add_symbol)),
6006                              ".lit4") == 0
6007                   && offset_expr.X_add_number == 0);
6008           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6009                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6010           return;
6011         }
6012
6013     case M_LI_D:
6014       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6015          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6016          order 32 bits of the value and the low order 32 bits are either
6017          zero or in OFFSET_EXPR.  */
6018       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6019         {
6020           if (HAVE_64BIT_GPRS)
6021             load_register (&icnt, treg, &imm_expr, 1);
6022           else
6023             {
6024               int hreg, lreg;
6025
6026               if (target_big_endian)
6027                 {
6028                   hreg = treg;
6029                   lreg = treg + 1;
6030                 }
6031               else
6032                 {
6033                   hreg = treg + 1;
6034                   lreg = treg;
6035                 }
6036
6037               if (hreg <= 31)
6038                 load_register (&icnt, hreg, &imm_expr, 0);
6039               if (lreg <= 31)
6040                 {
6041                   if (offset_expr.X_op == O_absent)
6042                     move_register (&icnt, lreg, 0);
6043                   else
6044                     {
6045                       assert (offset_expr.X_op == O_constant);
6046                       load_register (&icnt, lreg, &offset_expr, 0);
6047                     }
6048                 }
6049             }
6050           return;
6051         }
6052
6053       /* We know that sym is in the .rdata section.  First we get the
6054          upper 16 bits of the address.  */
6055       if (mips_pic == NO_PIC)
6056         {
6057           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6058         }
6059       else if (mips_pic == SVR4_PIC)
6060         {
6061           macro_build ((char *) NULL, &icnt, &offset_expr,
6062                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6063                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6064                        mips_gp_register);
6065         }
6066       else if (mips_pic == EMBEDDED_PIC)
6067         {
6068           /* For embedded PIC we pick up the entire address off $gp in
6069              a single instruction.  */
6070           macro_build ((char *) NULL, &icnt, &offset_expr,
6071                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6072                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6073           offset_expr.X_op = O_constant;
6074           offset_expr.X_add_number = 0;
6075         }
6076       else
6077         abort ();
6078
6079       /* Now we load the register(s).  */
6080       if (HAVE_64BIT_GPRS)
6081         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6082                      treg, (int) BFD_RELOC_LO16, AT);
6083       else
6084         {
6085           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6086                        treg, (int) BFD_RELOC_LO16, AT);
6087           if (treg != RA)
6088             {
6089               /* FIXME: How in the world do we deal with the possible
6090                  overflow here?  */
6091               offset_expr.X_add_number += 4;
6092               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6093                            treg + 1, (int) BFD_RELOC_LO16, AT);
6094             }
6095         }
6096
6097       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6098          does not become a variant frag.  */
6099       frag_wane (frag_now);
6100       frag_new (0);
6101
6102       break;
6103
6104     case M_LI_DD:
6105       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6106          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6107          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6108          the value and the low order 32 bits are either zero or in
6109          OFFSET_EXPR.  */
6110       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6111         {
6112           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6113           if (HAVE_64BIT_FPRS)
6114             {
6115               assert (HAVE_64BIT_GPRS);
6116               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6117                            "dmtc1", "t,S", AT, treg);
6118             }
6119           else
6120             {
6121               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6122                            "mtc1", "t,G", AT, treg + 1);
6123               if (offset_expr.X_op == O_absent)
6124                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6125                              "mtc1", "t,G", 0, treg);
6126               else
6127                 {
6128                   assert (offset_expr.X_op == O_constant);
6129                   load_register (&icnt, AT, &offset_expr, 0);
6130                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6131                                "mtc1", "t,G", AT, treg);
6132                 }
6133             }
6134           break;
6135         }
6136
6137       assert (offset_expr.X_op == O_symbol
6138               && offset_expr.X_add_number == 0);
6139       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6140       if (strcmp (s, ".lit8") == 0)
6141         {
6142           if (mips_opts.isa != ISA_MIPS1)
6143             {
6144               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6145                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6146                            mips_gp_register);
6147               return;
6148             }
6149           breg = mips_gp_register;
6150           r = BFD_RELOC_MIPS_LITERAL;
6151           goto dob;
6152         }
6153       else
6154         {
6155           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6156           if (mips_pic == SVR4_PIC)
6157             macro_build ((char *) NULL, &icnt, &offset_expr,
6158                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6159                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6160                          mips_gp_register);
6161           else
6162             {
6163               /* FIXME: This won't work for a 64 bit address.  */
6164               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6165             }
6166
6167           if (mips_opts.isa != ISA_MIPS1)
6168             {
6169               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6170                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6171
6172               /* To avoid confusion in tc_gen_reloc, we must ensure
6173                  that this does not become a variant frag.  */
6174               frag_wane (frag_now);
6175               frag_new (0);
6176
6177               break;
6178             }
6179           breg = AT;
6180           r = BFD_RELOC_LO16;
6181           goto dob;
6182         }
6183
6184     case M_L_DOB:
6185       if (mips_arch == CPU_R4650)
6186         {
6187           as_bad (_("opcode not supported on this processor"));
6188           return;
6189         }
6190       /* Even on a big endian machine $fn comes before $fn+1.  We have
6191          to adjust when loading from memory.  */
6192       r = BFD_RELOC_LO16;
6193     dob:
6194       assert (mips_opts.isa == ISA_MIPS1);
6195       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6196                    target_big_endian ? treg + 1 : treg,
6197                    (int) r, breg);
6198       /* FIXME: A possible overflow which I don't know how to deal
6199          with.  */
6200       offset_expr.X_add_number += 4;
6201       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6202                    target_big_endian ? treg : treg + 1,
6203                    (int) r, breg);
6204
6205       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6206          does not become a variant frag.  */
6207       frag_wane (frag_now);
6208       frag_new (0);
6209
6210       if (breg != AT)
6211         return;
6212       break;
6213
6214     case M_L_DAB:
6215       /*
6216        * The MIPS assembler seems to check for X_add_number not
6217        * being double aligned and generating:
6218        *        lui     at,%hi(foo+1)
6219        *        addu    at,at,v1
6220        *        addiu   at,at,%lo(foo+1)
6221        *        lwc1    f2,0(at)
6222        *        lwc1    f3,4(at)
6223        * But, the resulting address is the same after relocation so why
6224        * generate the extra instruction?
6225        */
6226       if (mips_arch == CPU_R4650)
6227         {
6228           as_bad (_("opcode not supported on this processor"));
6229           return;
6230         }
6231       /* Itbl support may require additional care here.  */
6232       coproc = 1;
6233       if (mips_opts.isa != ISA_MIPS1)
6234         {
6235           s = "ldc1";
6236           goto ld;
6237         }
6238
6239       s = "lwc1";
6240       fmt = "T,o(b)";
6241       goto ldd_std;
6242
6243     case M_S_DAB:
6244       if (mips_arch == CPU_R4650)
6245         {
6246           as_bad (_("opcode not supported on this processor"));
6247           return;
6248         }
6249
6250       if (mips_opts.isa != ISA_MIPS1)
6251         {
6252           s = "sdc1";
6253           goto st;
6254         }
6255
6256       s = "swc1";
6257       fmt = "T,o(b)";
6258       /* Itbl support may require additional care here.  */
6259       coproc = 1;
6260       goto ldd_std;
6261
6262     case M_LD_AB:
6263       if (HAVE_64BIT_GPRS)
6264         {
6265           s = "ld";
6266           goto ld;
6267         }
6268
6269       s = "lw";
6270       fmt = "t,o(b)";
6271       goto ldd_std;
6272
6273     case M_SD_AB:
6274       if (HAVE_64BIT_GPRS)
6275         {
6276           s = "sd";
6277           goto st;
6278         }
6279
6280       s = "sw";
6281       fmt = "t,o(b)";
6282
6283     ldd_std:
6284       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6285          loads for the case of doing a pair of loads to simulate an 'ld'.
6286          This is not currently done by the compiler, and assembly coders
6287          writing embedded-pic code can cope.  */
6288
6289       if (offset_expr.X_op != O_symbol
6290           && offset_expr.X_op != O_constant)
6291         {
6292           as_bad (_("expression too complex"));
6293           offset_expr.X_op = O_constant;
6294         }
6295
6296       /* Even on a big endian machine $fn comes before $fn+1.  We have
6297          to adjust when loading from memory.  We set coproc if we must
6298          load $fn+1 first.  */
6299       /* Itbl support may require additional care here.  */
6300       if (! target_big_endian)
6301         coproc = 0;
6302
6303       if (mips_pic == NO_PIC
6304           || offset_expr.X_op == O_constant)
6305         {
6306           char *p;
6307
6308           /* If this is a reference to a GP relative symbol, we want
6309                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6310                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6311              If we have a base register, we use this
6312                addu     $at,$breg,$gp
6313                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6314                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6315              If this is not a GP relative symbol, we want
6316                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6317                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6318                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6319              If there is a base register, we add it to $at after the
6320              lui instruction.  If there is a constant, we always use
6321              the last case.  */
6322           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6323               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6324             {
6325               p = NULL;
6326               used_at = 1;
6327             }
6328           else
6329             {
6330               int off;
6331
6332               if (breg == 0)
6333                 {
6334                   frag_grow (28);
6335                   tempreg = mips_gp_register;
6336                   off = 0;
6337                   used_at = 0;
6338                 }
6339               else
6340                 {
6341                   frag_grow (36);
6342                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6343                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6344                                "d,v,t", AT, breg, mips_gp_register);
6345                   tempreg = AT;
6346                   off = 4;
6347                   used_at = 1;
6348                 }
6349
6350               /* Itbl support may require additional care here.  */
6351               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6352                            coproc ? treg + 1 : treg,
6353                            (int) BFD_RELOC_GPREL16, tempreg);
6354               offset_expr.X_add_number += 4;
6355
6356               /* Set mips_optimize to 2 to avoid inserting an
6357                  undesired nop.  */
6358               hold_mips_optimize = mips_optimize;
6359               mips_optimize = 2;
6360               /* Itbl support may require additional care here.  */
6361               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6362                            coproc ? treg : treg + 1,
6363                            (int) BFD_RELOC_GPREL16, tempreg);
6364               mips_optimize = hold_mips_optimize;
6365
6366               p = frag_var (rs_machine_dependent, 12 + off, 0,
6367                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6368                                           used_at && mips_opts.noat),
6369                             offset_expr.X_add_symbol, 0, NULL);
6370
6371               /* We just generated two relocs.  When tc_gen_reloc
6372                  handles this case, it will skip the first reloc and
6373                  handle the second.  The second reloc already has an
6374                  extra addend of 4, which we added above.  We must
6375                  subtract it out, and then subtract another 4 to make
6376                  the first reloc come out right.  The second reloc
6377                  will come out right because we are going to add 4 to
6378                  offset_expr when we build its instruction below.
6379
6380                  If we have a symbol, then we don't want to include
6381                  the offset, because it will wind up being included
6382                  when we generate the reloc.  */
6383
6384               if (offset_expr.X_op == O_constant)
6385                 offset_expr.X_add_number -= 8;
6386               else
6387                 {
6388                   offset_expr.X_add_number = -4;
6389                   offset_expr.X_op = O_constant;
6390                 }
6391             }
6392           macro_build_lui (p, &icnt, &offset_expr, AT);
6393           if (p != NULL)
6394             p += 4;
6395           if (breg != 0)
6396             {
6397               macro_build (p, &icnt, (expressionS *) NULL,
6398                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6399                            "d,v,t", AT, breg, AT);
6400               if (p != NULL)
6401                 p += 4;
6402             }
6403           /* Itbl support may require additional care here.  */
6404           macro_build (p, &icnt, &offset_expr, s, fmt,
6405                        coproc ? treg + 1 : treg,
6406                        (int) BFD_RELOC_LO16, AT);
6407           if (p != NULL)
6408             p += 4;
6409           /* FIXME: How do we handle overflow here?  */
6410           offset_expr.X_add_number += 4;
6411           /* Itbl support may require additional care here.  */
6412           macro_build (p, &icnt, &offset_expr, s, fmt,
6413                        coproc ? treg : treg + 1,
6414                        (int) BFD_RELOC_LO16, AT);
6415         }
6416       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6417         {
6418           int off;
6419
6420           /* If this is a reference to an external symbol, we want
6421                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6422                nop
6423                <op>     $treg,0($at)
6424                <op>     $treg+1,4($at)
6425              Otherwise we want
6426                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6427                nop
6428                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6429                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6430              If there is a base register we add it to $at before the
6431              lwc1 instructions.  If there is a constant we include it
6432              in the lwc1 instructions.  */
6433           used_at = 1;
6434           expr1.X_add_number = offset_expr.X_add_number;
6435           offset_expr.X_add_number = 0;
6436           if (expr1.X_add_number < -0x8000
6437               || expr1.X_add_number >= 0x8000 - 4)
6438             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6439           if (breg == 0)
6440             off = 0;
6441           else
6442             off = 4;
6443           frag_grow (24 + off);
6444           macro_build ((char *) NULL, &icnt, &offset_expr,
6445                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6446                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6447           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6448           if (breg != 0)
6449             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6450                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6451                          "d,v,t", AT, breg, AT);
6452           /* Itbl support may require additional care here.  */
6453           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6454                        coproc ? treg + 1 : treg,
6455                        (int) BFD_RELOC_LO16, AT);
6456           expr1.X_add_number += 4;
6457
6458           /* Set mips_optimize to 2 to avoid inserting an undesired
6459              nop.  */
6460           hold_mips_optimize = mips_optimize;
6461           mips_optimize = 2;
6462           /* Itbl support may require additional care here.  */
6463           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6464                        coproc ? treg : treg + 1,
6465                        (int) BFD_RELOC_LO16, AT);
6466           mips_optimize = hold_mips_optimize;
6467
6468           (void) frag_var (rs_machine_dependent, 0, 0,
6469                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6470                            offset_expr.X_add_symbol, 0, NULL);
6471         }
6472       else if (mips_pic == SVR4_PIC)
6473         {
6474           int gpdel, off;
6475           char *p;
6476
6477           /* If this is a reference to an external symbol, we want
6478                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6479                addu     $at,$at,$gp
6480                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6481                nop
6482                <op>     $treg,0($at)
6483                <op>     $treg+1,4($at)
6484              Otherwise we want
6485                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6486                nop
6487                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6488                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6489              If there is a base register we add it to $at before the
6490              lwc1 instructions.  If there is a constant we include it
6491              in the lwc1 instructions.  */
6492           used_at = 1;
6493           expr1.X_add_number = offset_expr.X_add_number;
6494           offset_expr.X_add_number = 0;
6495           if (expr1.X_add_number < -0x8000
6496               || expr1.X_add_number >= 0x8000 - 4)
6497             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6498           if (reg_needs_delay (mips_gp_register))
6499             gpdel = 4;
6500           else
6501             gpdel = 0;
6502           if (breg == 0)
6503             off = 0;
6504           else
6505             off = 4;
6506           frag_grow (56);
6507           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6508                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6509           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6510                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6511                        "d,v,t", AT, AT, mips_gp_register);
6512           macro_build ((char *) NULL, &icnt, &offset_expr,
6513                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6514                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6515           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6516           if (breg != 0)
6517             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6518                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6519                          "d,v,t", AT, breg, AT);
6520           /* Itbl support may require additional care here.  */
6521           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6522                        coproc ? treg + 1 : treg,
6523                        (int) BFD_RELOC_LO16, AT);
6524           expr1.X_add_number += 4;
6525
6526           /* Set mips_optimize to 2 to avoid inserting an undesired
6527              nop.  */
6528           hold_mips_optimize = mips_optimize;
6529           mips_optimize = 2;
6530           /* Itbl support may require additional care here.  */
6531           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6532                        coproc ? treg : treg + 1,
6533                        (int) BFD_RELOC_LO16, AT);
6534           mips_optimize = hold_mips_optimize;
6535           expr1.X_add_number -= 4;
6536
6537           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6538                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6539                                       8 + gpdel + off, 1, 0),
6540                         offset_expr.X_add_symbol, 0, NULL);
6541           if (gpdel > 0)
6542             {
6543               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6544               p += 4;
6545             }
6546           macro_build (p, &icnt, &offset_expr,
6547                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6548                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6549                        mips_gp_register);
6550           p += 4;
6551           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6552           p += 4;
6553           if (breg != 0)
6554             {
6555               macro_build (p, &icnt, (expressionS *) NULL,
6556                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6557                            "d,v,t", AT, breg, AT);
6558               p += 4;
6559             }
6560           /* Itbl support may require additional care here.  */
6561           macro_build (p, &icnt, &expr1, s, fmt,
6562                        coproc ? treg + 1 : treg,
6563                        (int) BFD_RELOC_LO16, AT);
6564           p += 4;
6565           expr1.X_add_number += 4;
6566
6567           /* Set mips_optimize to 2 to avoid inserting an undesired
6568              nop.  */
6569           hold_mips_optimize = mips_optimize;
6570           mips_optimize = 2;
6571           /* Itbl support may require additional care here.  */
6572           macro_build (p, &icnt, &expr1, s, fmt,
6573                        coproc ? treg : treg + 1,
6574                        (int) BFD_RELOC_LO16, AT);
6575           mips_optimize = hold_mips_optimize;
6576         }
6577       else if (mips_pic == EMBEDDED_PIC)
6578         {
6579           /* If there is no base register, we use
6580                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6581                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6582              If we have a base register, we use
6583                addu     $at,$breg,$gp
6584                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6585                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6586              */
6587           if (breg == 0)
6588             {
6589               tempreg = mips_gp_register;
6590               used_at = 0;
6591             }
6592           else
6593             {
6594               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6595                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6596                            "d,v,t", AT, breg, mips_gp_register);
6597               tempreg = AT;
6598               used_at = 1;
6599             }
6600
6601           /* Itbl support may require additional care here.  */
6602           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6603                        coproc ? treg + 1 : treg,
6604                        (int) BFD_RELOC_GPREL16, tempreg);
6605           offset_expr.X_add_number += 4;
6606           /* Itbl support may require additional care here.  */
6607           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6608                        coproc ? treg : treg + 1,
6609                        (int) BFD_RELOC_GPREL16, tempreg);
6610         }
6611       else
6612         abort ();
6613
6614       if (! used_at)
6615         return;
6616
6617       break;
6618
6619     case M_LD_OB:
6620       s = "lw";
6621       goto sd_ob;
6622     case M_SD_OB:
6623       s = "sw";
6624     sd_ob:
6625       assert (HAVE_32BIT_ADDRESSES);
6626       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6627                    (int) BFD_RELOC_LO16, breg);
6628       offset_expr.X_add_number += 4;
6629       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6630                    (int) BFD_RELOC_LO16, breg);
6631       return;
6632
6633    /* New code added to support COPZ instructions.
6634       This code builds table entries out of the macros in mip_opcodes.
6635       R4000 uses interlocks to handle coproc delays.
6636       Other chips (like the R3000) require nops to be inserted for delays.
6637
6638       FIXME: Currently, we require that the user handle delays.
6639       In order to fill delay slots for non-interlocked chips,
6640       we must have a way to specify delays based on the coprocessor.
6641       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6642       What are the side-effects of the cop instruction?
6643       What cache support might we have and what are its effects?
6644       Both coprocessor & memory require delays. how long???
6645       What registers are read/set/modified?
6646
6647       If an itbl is provided to interpret cop instructions,
6648       this knowledge can be encoded in the itbl spec.  */
6649
6650     case M_COP0:
6651       s = "c0";
6652       goto copz;
6653     case M_COP1:
6654       s = "c1";
6655       goto copz;
6656     case M_COP2:
6657       s = "c2";
6658       goto copz;
6659     case M_COP3:
6660       s = "c3";
6661     copz:
6662       /* For now we just do C (same as Cz).  The parameter will be
6663          stored in insn_opcode by mips_ip.  */
6664       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6665                    ip->insn_opcode);
6666       return;
6667
6668     case M_MOVE:
6669       move_register (&icnt, dreg, sreg);
6670       return;
6671
6672 #ifdef LOSING_COMPILER
6673     default:
6674       /* Try and see if this is a new itbl instruction.
6675          This code builds table entries out of the macros in mip_opcodes.
6676          FIXME: For now we just assemble the expression and pass it's
6677          value along as a 32-bit immediate.
6678          We may want to have the assembler assemble this value,
6679          so that we gain the assembler's knowledge of delay slots,
6680          symbols, etc.
6681          Would it be more efficient to use mask (id) here? */
6682       if (itbl_have_entries
6683           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6684         {
6685           s = ip->insn_mo->name;
6686           s2 = "cop3";
6687           coproc = ITBL_DECODE_PNUM (immed_expr);;
6688           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6689           return;
6690         }
6691       macro2 (ip);
6692       return;
6693     }
6694   if (mips_opts.noat)
6695     as_warn (_("Macro used $at after \".set noat\""));
6696 }
6697
6698 static void
6699 macro2 (ip)
6700      struct mips_cl_insn *ip;
6701 {
6702   register int treg, sreg, dreg, breg;
6703   int tempreg;
6704   int mask;
6705   int icnt = 0;
6706   int used_at;
6707   expressionS expr1;
6708   const char *s;
6709   const char *s2;
6710   const char *fmt;
6711   int likely = 0;
6712   int dbl = 0;
6713   int coproc = 0;
6714   int lr = 0;
6715   int imm = 0;
6716   int off;
6717   offsetT maxnum;
6718   bfd_reloc_code_real_type r;
6719   char *p;
6720
6721   treg = (ip->insn_opcode >> 16) & 0x1f;
6722   dreg = (ip->insn_opcode >> 11) & 0x1f;
6723   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6724   mask = ip->insn_mo->mask;
6725
6726   expr1.X_op = O_constant;
6727   expr1.X_op_symbol = NULL;
6728   expr1.X_add_symbol = NULL;
6729   expr1.X_add_number = 1;
6730
6731   switch (mask)
6732     {
6733 #endif /* LOSING_COMPILER */
6734
6735     case M_DMUL:
6736       dbl = 1;
6737     case M_MUL:
6738       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6739                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6740       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6741                    dreg);
6742       return;
6743
6744     case M_DMUL_I:
6745       dbl = 1;
6746     case M_MUL_I:
6747       /* The MIPS assembler some times generates shifts and adds.  I'm
6748          not trying to be that fancy. GCC should do this for us
6749          anyway.  */
6750       load_register (&icnt, AT, &imm_expr, dbl);
6751       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6752                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6753       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6754                    dreg);
6755       break;
6756
6757     case M_DMULO_I:
6758       dbl = 1;
6759     case M_MULO_I:
6760       imm = 1;
6761       goto do_mulo;
6762
6763     case M_DMULO:
6764       dbl = 1;
6765     case M_MULO:
6766     do_mulo:
6767       mips_emit_delays (true);
6768       ++mips_opts.noreorder;
6769       mips_any_noreorder = 1;
6770       if (imm)
6771         load_register (&icnt, AT, &imm_expr, dbl);
6772       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6773                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6774       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6775                    dreg);
6776       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6777                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6778       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6779                    AT);
6780       if (mips_trap)
6781         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6782                      dreg, AT);
6783       else
6784         {
6785           expr1.X_add_number = 8;
6786           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6787                        AT);
6788           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6789                        0);
6790           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6791                        "c", 6);
6792         }
6793       --mips_opts.noreorder;
6794       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6795       break;
6796
6797     case M_DMULOU_I:
6798       dbl = 1;
6799     case M_MULOU_I:
6800       imm = 1;
6801       goto do_mulou;
6802
6803     case M_DMULOU:
6804       dbl = 1;
6805     case M_MULOU:
6806     do_mulou:
6807       mips_emit_delays (true);
6808       ++mips_opts.noreorder;
6809       mips_any_noreorder = 1;
6810       if (imm)
6811         load_register (&icnt, AT, &imm_expr, dbl);
6812       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6813                    dbl ? "dmultu" : "multu",
6814                    "s,t", sreg, imm ? AT : treg);
6815       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6816                    AT);
6817       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6818                    dreg);
6819       if (mips_trap)
6820         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6821                      AT, 0);
6822       else
6823         {
6824           expr1.X_add_number = 8;
6825           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6826           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6827                        0);
6828           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6829                        "c", 6);
6830         }
6831       --mips_opts.noreorder;
6832       break;
6833
6834     case M_DROL:
6835       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6836                    "d,v,t", AT, 0, treg);
6837       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6838                    "d,t,s", AT, sreg, AT);
6839       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6840                    "d,t,s", dreg, sreg, treg);
6841       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6842                    "d,v,t", dreg, dreg, AT);
6843       break;
6844
6845     case M_ROL:
6846       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6847                    "d,v,t", AT, 0, treg);
6848       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6849                    "d,t,s", AT, sreg, AT);
6850       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6851                    "d,t,s", dreg, sreg, treg);
6852       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6853                    "d,v,t", dreg, dreg, AT);
6854       break;
6855
6856     case M_DROL_I:
6857       {
6858         unsigned int rot;
6859         char *l, *r;
6860
6861         if (imm_expr.X_op != O_constant)
6862           as_bad (_("rotate count too large"));
6863         rot = imm_expr.X_add_number & 0x3f;
6864         if (! rot)
6865           break;
6866         l = (rot < 0x20) ? "dsll" : "dsll32";
6867         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6868         rot &= 0x1f;
6869         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6870                      "d,w,<", AT, sreg, rot);
6871         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6872                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6873         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6874                      "d,v,t", dreg, dreg, AT);
6875       }
6876       break;
6877
6878     case M_ROL_I:
6879       {
6880         unsigned int rot;
6881
6882         if (imm_expr.X_op != O_constant)
6883           as_bad (_("rotate count too large"));
6884         rot = imm_expr.X_add_number & 0x1f;
6885         if (! rot)
6886           break;
6887         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6888                      "d,w,<", AT, sreg, rot);
6889         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6890                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6891         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6892                      "d,v,t", dreg, dreg, AT);
6893       }
6894       break;
6895
6896     case M_DROR:
6897       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6898                    "d,v,t", AT, 0, treg);
6899       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6900                    "d,t,s", AT, sreg, AT);
6901       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6902                    "d,t,s", dreg, sreg, treg);
6903       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6904                    "d,v,t", dreg, dreg, AT);
6905       break;
6906
6907     case M_ROR:
6908       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6909                    "d,v,t", AT, 0, treg);
6910       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6911                    "d,t,s", AT, sreg, AT);
6912       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6913                    "d,t,s", dreg, sreg, treg);
6914       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6915                    "d,v,t", dreg, dreg, AT);
6916       break;
6917
6918     case M_DROR_I:
6919       {
6920         unsigned int rot;
6921         char *l, *r;
6922
6923         if (imm_expr.X_op != O_constant)
6924           as_bad (_("rotate count too large"));
6925         rot = imm_expr.X_add_number & 0x3f;
6926         if (! rot)
6927           break;
6928         r = (rot < 0x20) ? "dsrl" : "dsrl32";
6929         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6930         rot &= 0x1f;
6931         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6932                      "d,w,<", AT, sreg, rot);
6933         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6934                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6935         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6936                      "d,v,t", dreg, dreg, AT);
6937       }
6938       break;
6939
6940     case M_ROR_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 (! rot)
6948           break;
6949         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6950                      "d,w,<", AT, sreg, rot);
6951         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6952                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6953         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6954                      "d,v,t", dreg, dreg, AT);
6955       }
6956       break;
6957
6958     case M_S_DOB:
6959       if (mips_arch == CPU_R4650)
6960         {
6961           as_bad (_("opcode not supported on this processor"));
6962           return;
6963         }
6964       assert (mips_opts.isa == ISA_MIPS1);
6965       /* Even on a big endian machine $fn comes before $fn+1.  We have
6966          to adjust when storing to memory.  */
6967       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6968                    target_big_endian ? treg + 1 : treg,
6969                    (int) BFD_RELOC_LO16, breg);
6970       offset_expr.X_add_number += 4;
6971       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6972                    target_big_endian ? treg : treg + 1,
6973                    (int) BFD_RELOC_LO16, breg);
6974       return;
6975
6976     case M_SEQ:
6977       if (sreg == 0)
6978         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6979                      treg, (int) BFD_RELOC_LO16);
6980       else if (treg == 0)
6981         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6982                      sreg, (int) BFD_RELOC_LO16);
6983       else
6984         {
6985           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6986                        "d,v,t", dreg, sreg, treg);
6987           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6988                        dreg, (int) BFD_RELOC_LO16);
6989         }
6990       return;
6991
6992     case M_SEQ_I:
6993       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6994         {
6995           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6996                        sreg, (int) BFD_RELOC_LO16);
6997           return;
6998         }
6999       if (sreg == 0)
7000         {
7001           as_warn (_("Instruction %s: result is always false"),
7002                    ip->insn_mo->name);
7003           move_register (&icnt, dreg, 0);
7004           return;
7005         }
7006       if (imm_expr.X_op == O_constant
7007           && imm_expr.X_add_number >= 0
7008           && imm_expr.X_add_number < 0x10000)
7009         {
7010           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7011                        sreg, (int) BFD_RELOC_LO16);
7012           used_at = 0;
7013         }
7014       else if (imm_expr.X_op == O_constant
7015                && imm_expr.X_add_number > -0x8000
7016                && imm_expr.X_add_number < 0)
7017         {
7018           imm_expr.X_add_number = -imm_expr.X_add_number;
7019           macro_build ((char *) NULL, &icnt, &imm_expr,
7020                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7021                        "t,r,j", dreg, sreg,
7022                        (int) BFD_RELOC_LO16);
7023           used_at = 0;
7024         }
7025       else
7026         {
7027           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7028           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7029                        "d,v,t", dreg, sreg, AT);
7030           used_at = 1;
7031         }
7032       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7033                    (int) BFD_RELOC_LO16);
7034       if (used_at)
7035         break;
7036       return;
7037
7038     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7039       s = "slt";
7040       goto sge;
7041     case M_SGEU:
7042       s = "sltu";
7043     sge:
7044       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7045                    dreg, sreg, treg);
7046       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7047                    (int) BFD_RELOC_LO16);
7048       return;
7049
7050     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7051     case M_SGEU_I:
7052       if (imm_expr.X_op == O_constant
7053           && imm_expr.X_add_number >= -0x8000
7054           && imm_expr.X_add_number < 0x8000)
7055         {
7056           macro_build ((char *) NULL, &icnt, &imm_expr,
7057                        mask == M_SGE_I ? "slti" : "sltiu",
7058                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7059           used_at = 0;
7060         }
7061       else
7062         {
7063           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7064           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7065                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7066                        AT);
7067           used_at = 1;
7068         }
7069       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7070                    (int) BFD_RELOC_LO16);
7071       if (used_at)
7072         break;
7073       return;
7074
7075     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7076       s = "slt";
7077       goto sgt;
7078     case M_SGTU:
7079       s = "sltu";
7080     sgt:
7081       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7082                    dreg, treg, sreg);
7083       return;
7084
7085     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7086       s = "slt";
7087       goto sgti;
7088     case M_SGTU_I:
7089       s = "sltu";
7090     sgti:
7091       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7092       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7093                    dreg, AT, sreg);
7094       break;
7095
7096     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7097       s = "slt";
7098       goto sle;
7099     case M_SLEU:
7100       s = "sltu";
7101     sle:
7102       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7103                    dreg, treg, sreg);
7104       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7105                    (int) BFD_RELOC_LO16);
7106       return;
7107
7108     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7109       s = "slt";
7110       goto slei;
7111     case M_SLEU_I:
7112       s = "sltu";
7113     slei:
7114       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7115       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7116                    dreg, AT, sreg);
7117       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7118                    (int) BFD_RELOC_LO16);
7119       break;
7120
7121     case M_SLT_I:
7122       if (imm_expr.X_op == O_constant
7123           && imm_expr.X_add_number >= -0x8000
7124           && imm_expr.X_add_number < 0x8000)
7125         {
7126           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7127                        dreg, sreg, (int) BFD_RELOC_LO16);
7128           return;
7129         }
7130       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7131       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7132                    dreg, sreg, AT);
7133       break;
7134
7135     case M_SLTU_I:
7136       if (imm_expr.X_op == O_constant
7137           && imm_expr.X_add_number >= -0x8000
7138           && imm_expr.X_add_number < 0x8000)
7139         {
7140           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7141                        dreg, sreg, (int) BFD_RELOC_LO16);
7142           return;
7143         }
7144       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7145       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7146                    "d,v,t", dreg, sreg, AT);
7147       break;
7148
7149     case M_SNE:
7150       if (sreg == 0)
7151         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7152                      "d,v,t", dreg, 0, treg);
7153       else if (treg == 0)
7154         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7155                      "d,v,t", dreg, 0, sreg);
7156       else
7157         {
7158           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7159                        "d,v,t", dreg, sreg, treg);
7160           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7161                        "d,v,t", dreg, 0, dreg);
7162         }
7163       return;
7164
7165     case M_SNE_I:
7166       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7167         {
7168           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7169                        "d,v,t", dreg, 0, sreg);
7170           return;
7171         }
7172       if (sreg == 0)
7173         {
7174           as_warn (_("Instruction %s: result is always true"),
7175                    ip->insn_mo->name);
7176           macro_build ((char *) NULL, &icnt, &expr1,
7177                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7178                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7179           return;
7180         }
7181       if (imm_expr.X_op == O_constant
7182           && imm_expr.X_add_number >= 0
7183           && imm_expr.X_add_number < 0x10000)
7184         {
7185           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7186                        dreg, sreg, (int) BFD_RELOC_LO16);
7187           used_at = 0;
7188         }
7189       else if (imm_expr.X_op == O_constant
7190                && imm_expr.X_add_number > -0x8000
7191                && imm_expr.X_add_number < 0)
7192         {
7193           imm_expr.X_add_number = -imm_expr.X_add_number;
7194           macro_build ((char *) NULL, &icnt, &imm_expr,
7195                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7196                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7197           used_at = 0;
7198         }
7199       else
7200         {
7201           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7202           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7203                        "d,v,t", dreg, sreg, AT);
7204           used_at = 1;
7205         }
7206       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7207                    "d,v,t", dreg, 0, dreg);
7208       if (used_at)
7209         break;
7210       return;
7211
7212     case M_DSUB_I:
7213       dbl = 1;
7214     case M_SUB_I:
7215       if (imm_expr.X_op == O_constant
7216           && imm_expr.X_add_number > -0x8000
7217           && imm_expr.X_add_number <= 0x8000)
7218         {
7219           imm_expr.X_add_number = -imm_expr.X_add_number;
7220           macro_build ((char *) NULL, &icnt, &imm_expr,
7221                        dbl ? "daddi" : "addi",
7222                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7223           return;
7224         }
7225       load_register (&icnt, AT, &imm_expr, dbl);
7226       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7227                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7228       break;
7229
7230     case M_DSUBU_I:
7231       dbl = 1;
7232     case M_SUBU_I:
7233       if (imm_expr.X_op == O_constant
7234           && imm_expr.X_add_number > -0x8000
7235           && imm_expr.X_add_number <= 0x8000)
7236         {
7237           imm_expr.X_add_number = -imm_expr.X_add_number;
7238           macro_build ((char *) NULL, &icnt, &imm_expr,
7239                        dbl ? "daddiu" : "addiu",
7240                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7241           return;
7242         }
7243       load_register (&icnt, AT, &imm_expr, dbl);
7244       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7245                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7246       break;
7247
7248     case M_TEQ_I:
7249       s = "teq";
7250       goto trap;
7251     case M_TGE_I:
7252       s = "tge";
7253       goto trap;
7254     case M_TGEU_I:
7255       s = "tgeu";
7256       goto trap;
7257     case M_TLT_I:
7258       s = "tlt";
7259       goto trap;
7260     case M_TLTU_I:
7261       s = "tltu";
7262       goto trap;
7263     case M_TNE_I:
7264       s = "tne";
7265     trap:
7266       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7267       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7268                    AT);
7269       break;
7270
7271     case M_TRUNCWS:
7272     case M_TRUNCWD:
7273       assert (mips_opts.isa == ISA_MIPS1);
7274       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7275       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7276
7277       /*
7278        * Is the double cfc1 instruction a bug in the mips assembler;
7279        * or is there a reason for it?
7280        */
7281       mips_emit_delays (true);
7282       ++mips_opts.noreorder;
7283       mips_any_noreorder = 1;
7284       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7285                    treg, RA);
7286       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7287                    treg, RA);
7288       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7289       expr1.X_add_number = 3;
7290       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7291                    (int) BFD_RELOC_LO16);
7292       expr1.X_add_number = 2;
7293       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7294                      (int) BFD_RELOC_LO16);
7295       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7296                    AT, RA);
7297       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7298       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7299               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7300       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7301                    treg, RA);
7302       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7303       --mips_opts.noreorder;
7304       break;
7305
7306     case M_ULH:
7307       s = "lb";
7308       goto ulh;
7309     case M_ULHU:
7310       s = "lbu";
7311     ulh:
7312       if (offset_expr.X_add_number >= 0x7fff)
7313         as_bad (_("operand overflow"));
7314       /* avoid load delay */
7315       if (! target_big_endian)
7316         ++offset_expr.X_add_number;
7317       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7318                    (int) BFD_RELOC_LO16, breg);
7319       if (! target_big_endian)
7320         --offset_expr.X_add_number;
7321       else
7322         ++offset_expr.X_add_number;
7323       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7324                    (int) BFD_RELOC_LO16, breg);
7325       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7326                    treg, treg, 8);
7327       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7328                    treg, treg, AT);
7329       break;
7330
7331     case M_ULD:
7332       s = "ldl";
7333       s2 = "ldr";
7334       off = 7;
7335       goto ulw;
7336     case M_ULW:
7337       s = "lwl";
7338       s2 = "lwr";
7339       off = 3;
7340     ulw:
7341       if (offset_expr.X_add_number >= 0x8000 - off)
7342         as_bad (_("operand overflow"));
7343       if (! target_big_endian)
7344         offset_expr.X_add_number += off;
7345       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7346                    (int) BFD_RELOC_LO16, breg);
7347       if (! target_big_endian)
7348         offset_expr.X_add_number -= off;
7349       else
7350         offset_expr.X_add_number += off;
7351       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7352                    (int) BFD_RELOC_LO16, breg);
7353       return;
7354
7355     case M_ULD_A:
7356       s = "ldl";
7357       s2 = "ldr";
7358       off = 7;
7359       goto ulwa;
7360     case M_ULW_A:
7361       s = "lwl";
7362       s2 = "lwr";
7363       off = 3;
7364     ulwa:
7365       used_at = 1;
7366       load_address (&icnt, AT, &offset_expr, &used_at);
7367       if (breg != 0)
7368         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7369                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7370                      "d,v,t", AT, AT, breg);
7371       if (! target_big_endian)
7372         expr1.X_add_number = off;
7373       else
7374         expr1.X_add_number = 0;
7375       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7376                    (int) BFD_RELOC_LO16, AT);
7377       if (! target_big_endian)
7378         expr1.X_add_number = 0;
7379       else
7380         expr1.X_add_number = off;
7381       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7382                    (int) BFD_RELOC_LO16, AT);
7383       break;
7384
7385     case M_ULH_A:
7386     case M_ULHU_A:
7387       used_at = 1;
7388       load_address (&icnt, AT, &offset_expr, &used_at);
7389       if (breg != 0)
7390         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7391                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7392                      "d,v,t", AT, AT, breg);
7393       if (target_big_endian)
7394         expr1.X_add_number = 0;
7395       macro_build ((char *) NULL, &icnt, &expr1,
7396                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7397                    (int) BFD_RELOC_LO16, AT);
7398       if (target_big_endian)
7399         expr1.X_add_number = 1;
7400       else
7401         expr1.X_add_number = 0;
7402       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7403                    (int) BFD_RELOC_LO16, AT);
7404       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7405                    treg, treg, 8);
7406       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7407                    treg, treg, AT);
7408       break;
7409
7410     case M_USH:
7411       if (offset_expr.X_add_number >= 0x7fff)
7412         as_bad (_("operand overflow"));
7413       if (target_big_endian)
7414         ++offset_expr.X_add_number;
7415       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7416                    (int) BFD_RELOC_LO16, breg);
7417       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7418                    AT, treg, 8);
7419       if (target_big_endian)
7420         --offset_expr.X_add_number;
7421       else
7422         ++offset_expr.X_add_number;
7423       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7424                    (int) BFD_RELOC_LO16, breg);
7425       break;
7426
7427     case M_USD:
7428       s = "sdl";
7429       s2 = "sdr";
7430       off = 7;
7431       goto usw;
7432     case M_USW:
7433       s = "swl";
7434       s2 = "swr";
7435       off = 3;
7436     usw:
7437       if (offset_expr.X_add_number >= 0x8000 - off)
7438         as_bad (_("operand overflow"));
7439       if (! target_big_endian)
7440         offset_expr.X_add_number += off;
7441       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7442                    (int) BFD_RELOC_LO16, breg);
7443       if (! target_big_endian)
7444         offset_expr.X_add_number -= off;
7445       else
7446         offset_expr.X_add_number += off;
7447       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7448                    (int) BFD_RELOC_LO16, breg);
7449       return;
7450
7451     case M_USD_A:
7452       s = "sdl";
7453       s2 = "sdr";
7454       off = 7;
7455       goto uswa;
7456     case M_USW_A:
7457       s = "swl";
7458       s2 = "swr";
7459       off = 3;
7460     uswa:
7461       used_at = 1;
7462       load_address (&icnt, AT, &offset_expr, &used_at);
7463       if (breg != 0)
7464         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7465                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7466                      "d,v,t", AT, AT, breg);
7467       if (! target_big_endian)
7468         expr1.X_add_number = off;
7469       else
7470         expr1.X_add_number = 0;
7471       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7472                    (int) BFD_RELOC_LO16, AT);
7473       if (! target_big_endian)
7474         expr1.X_add_number = 0;
7475       else
7476         expr1.X_add_number = off;
7477       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7478                    (int) BFD_RELOC_LO16, AT);
7479       break;
7480
7481     case M_USH_A:
7482       used_at = 1;
7483       load_address (&icnt, AT, &offset_expr, &used_at);
7484       if (breg != 0)
7485         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7486                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7487                      "d,v,t", AT, AT, breg);
7488       if (! target_big_endian)
7489         expr1.X_add_number = 0;
7490       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7491                    (int) BFD_RELOC_LO16, AT);
7492       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7493                    treg, treg, 8);
7494       if (! target_big_endian)
7495         expr1.X_add_number = 1;
7496       else
7497         expr1.X_add_number = 0;
7498       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7499                    (int) BFD_RELOC_LO16, AT);
7500       if (! target_big_endian)
7501         expr1.X_add_number = 0;
7502       else
7503         expr1.X_add_number = 1;
7504       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7505                    (int) BFD_RELOC_LO16, AT);
7506       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7507                    treg, treg, 8);
7508       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7509                    treg, treg, AT);
7510       break;
7511
7512     default:
7513       /* FIXME: Check if this is one of the itbl macros, since they
7514          are added dynamically.  */
7515       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7516       break;
7517     }
7518   if (mips_opts.noat)
7519     as_warn (_("Macro used $at after \".set noat\""));
7520 }
7521
7522 /* Implement macros in mips16 mode.  */
7523
7524 static void
7525 mips16_macro (ip)
7526      struct mips_cl_insn *ip;
7527 {
7528   int mask;
7529   int xreg, yreg, zreg, tmp;
7530   int icnt;
7531   expressionS expr1;
7532   int dbl;
7533   const char *s, *s2, *s3;
7534
7535   mask = ip->insn_mo->mask;
7536
7537   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7538   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7539   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7540
7541   icnt = 0;
7542
7543   expr1.X_op = O_constant;
7544   expr1.X_op_symbol = NULL;
7545   expr1.X_add_symbol = NULL;
7546   expr1.X_add_number = 1;
7547
7548   dbl = 0;
7549
7550   switch (mask)
7551     {
7552     default:
7553       internalError ();
7554
7555     case M_DDIV_3:
7556       dbl = 1;
7557     case M_DIV_3:
7558       s = "mflo";
7559       goto do_div3;
7560     case M_DREM_3:
7561       dbl = 1;
7562     case M_REM_3:
7563       s = "mfhi";
7564     do_div3:
7565       mips_emit_delays (true);
7566       ++mips_opts.noreorder;
7567       mips_any_noreorder = 1;
7568       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7569                    dbl ? "ddiv" : "div",
7570                    "0,x,y", xreg, yreg);
7571       expr1.X_add_number = 2;
7572       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7573       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7574                    7);
7575
7576       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7577          since that causes an overflow.  We should do that as well,
7578          but I don't see how to do the comparisons without a temporary
7579          register.  */
7580       --mips_opts.noreorder;
7581       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7582       break;
7583
7584     case M_DIVU_3:
7585       s = "divu";
7586       s2 = "mflo";
7587       goto do_divu3;
7588     case M_REMU_3:
7589       s = "divu";
7590       s2 = "mfhi";
7591       goto do_divu3;
7592     case M_DDIVU_3:
7593       s = "ddivu";
7594       s2 = "mflo";
7595       goto do_divu3;
7596     case M_DREMU_3:
7597       s = "ddivu";
7598       s2 = "mfhi";
7599     do_divu3:
7600       mips_emit_delays (true);
7601       ++mips_opts.noreorder;
7602       mips_any_noreorder = 1;
7603       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7604                    xreg, yreg);
7605       expr1.X_add_number = 2;
7606       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7607       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7608                    "6", 7);
7609       --mips_opts.noreorder;
7610       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7611       break;
7612
7613     case M_DMUL:
7614       dbl = 1;
7615     case M_MUL:
7616       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7617                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7618       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7619                    zreg);
7620       return;
7621
7622     case M_DSUBU_I:
7623       dbl = 1;
7624       goto do_subu;
7625     case M_SUBU_I:
7626     do_subu:
7627       if (imm_expr.X_op != O_constant)
7628         as_bad (_("Unsupported large constant"));
7629       imm_expr.X_add_number = -imm_expr.X_add_number;
7630       macro_build ((char *) NULL, &icnt, &imm_expr,
7631                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7632       break;
7633
7634     case M_SUBU_I_2:
7635       if (imm_expr.X_op != O_constant)
7636         as_bad (_("Unsupported large constant"));
7637       imm_expr.X_add_number = -imm_expr.X_add_number;
7638       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7639                    "x,k", xreg);
7640       break;
7641
7642     case M_DSUBU_I_2:
7643       if (imm_expr.X_op != O_constant)
7644         as_bad (_("Unsupported large constant"));
7645       imm_expr.X_add_number = -imm_expr.X_add_number;
7646       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7647                    "y,j", yreg);
7648       break;
7649
7650     case M_BEQ:
7651       s = "cmp";
7652       s2 = "bteqz";
7653       goto do_branch;
7654     case M_BNE:
7655       s = "cmp";
7656       s2 = "btnez";
7657       goto do_branch;
7658     case M_BLT:
7659       s = "slt";
7660       s2 = "btnez";
7661       goto do_branch;
7662     case M_BLTU:
7663       s = "sltu";
7664       s2 = "btnez";
7665       goto do_branch;
7666     case M_BLE:
7667       s = "slt";
7668       s2 = "bteqz";
7669       goto do_reverse_branch;
7670     case M_BLEU:
7671       s = "sltu";
7672       s2 = "bteqz";
7673       goto do_reverse_branch;
7674     case M_BGE:
7675       s = "slt";
7676       s2 = "bteqz";
7677       goto do_branch;
7678     case M_BGEU:
7679       s = "sltu";
7680       s2 = "bteqz";
7681       goto do_branch;
7682     case M_BGT:
7683       s = "slt";
7684       s2 = "btnez";
7685       goto do_reverse_branch;
7686     case M_BGTU:
7687       s = "sltu";
7688       s2 = "btnez";
7689
7690     do_reverse_branch:
7691       tmp = xreg;
7692       xreg = yreg;
7693       yreg = tmp;
7694
7695     do_branch:
7696       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7697                    xreg, yreg);
7698       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7699       break;
7700
7701     case M_BEQ_I:
7702       s = "cmpi";
7703       s2 = "bteqz";
7704       s3 = "x,U";
7705       goto do_branch_i;
7706     case M_BNE_I:
7707       s = "cmpi";
7708       s2 = "btnez";
7709       s3 = "x,U";
7710       goto do_branch_i;
7711     case M_BLT_I:
7712       s = "slti";
7713       s2 = "btnez";
7714       s3 = "x,8";
7715       goto do_branch_i;
7716     case M_BLTU_I:
7717       s = "sltiu";
7718       s2 = "btnez";
7719       s3 = "x,8";
7720       goto do_branch_i;
7721     case M_BLE_I:
7722       s = "slti";
7723       s2 = "btnez";
7724       s3 = "x,8";
7725       goto do_addone_branch_i;
7726     case M_BLEU_I:
7727       s = "sltiu";
7728       s2 = "btnez";
7729       s3 = "x,8";
7730       goto do_addone_branch_i;
7731     case M_BGE_I:
7732       s = "slti";
7733       s2 = "bteqz";
7734       s3 = "x,8";
7735       goto do_branch_i;
7736     case M_BGEU_I:
7737       s = "sltiu";
7738       s2 = "bteqz";
7739       s3 = "x,8";
7740       goto do_branch_i;
7741     case M_BGT_I:
7742       s = "slti";
7743       s2 = "bteqz";
7744       s3 = "x,8";
7745       goto do_addone_branch_i;
7746     case M_BGTU_I:
7747       s = "sltiu";
7748       s2 = "bteqz";
7749       s3 = "x,8";
7750
7751     do_addone_branch_i:
7752       if (imm_expr.X_op != O_constant)
7753         as_bad (_("Unsupported large constant"));
7754       ++imm_expr.X_add_number;
7755
7756     do_branch_i:
7757       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7758       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7759       break;
7760
7761     case M_ABS:
7762       expr1.X_add_number = 0;
7763       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7764       if (xreg != yreg)
7765         move_register (&icnt, xreg, yreg);
7766       expr1.X_add_number = 2;
7767       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7768       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7769                    "neg", "x,w", xreg, xreg);
7770     }
7771 }
7772
7773 /* For consistency checking, verify that all bits are specified either
7774    by the match/mask part of the instruction definition, or by the
7775    operand list.  */
7776 static int
7777 validate_mips_insn (opc)
7778      const struct mips_opcode *opc;
7779 {
7780   const char *p = opc->args;
7781   char c;
7782   unsigned long used_bits = opc->mask;
7783
7784   if ((used_bits & opc->match) != opc->match)
7785     {
7786       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7787               opc->name, opc->args);
7788       return 0;
7789     }
7790 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7791   while (*p)
7792     switch (c = *p++)
7793       {
7794       case ',': break;
7795       case '(': break;
7796       case ')': break;
7797       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7798       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7799       case 'A': break;
7800       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7801       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7802       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7803       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7804       case 'F': break;
7805       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7806       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7807       case 'I': break;
7808       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7809       case 'L': break;
7810       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7811       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7812       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7813       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7814                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7815       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7816       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7817       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7818       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7819       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7820       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7821       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7822       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7823       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7824       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7825       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7826       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7827       case 'f': break;
7828       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7829       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7830       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7831       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7832       case 'l': break;
7833       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7834       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7835       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7836       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7837       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7838       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7839       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7840       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7841       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7842       case 'x': break;
7843       case 'z': break;
7844       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7845       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7846                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7847       default:
7848         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7849                 c, opc->name, opc->args);
7850         return 0;
7851       }
7852 #undef USE_BITS
7853   if (used_bits != 0xffffffff)
7854     {
7855       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7856               ~used_bits & 0xffffffff, opc->name, opc->args);
7857       return 0;
7858     }
7859   return 1;
7860 }
7861
7862 /* This routine assembles an instruction into its binary format.  As a
7863    side effect, it sets one of the global variables imm_reloc or
7864    offset_reloc to the type of relocation to do if one of the operands
7865    is an address expression.  */
7866
7867 static void
7868 mips_ip (str, ip)
7869      char *str;
7870      struct mips_cl_insn *ip;
7871 {
7872   char *s;
7873   const char *args;
7874   char c = 0;
7875   struct mips_opcode *insn;
7876   char *argsStart;
7877   unsigned int regno;
7878   unsigned int lastregno = 0;
7879   char *s_reset;
7880   char save_c = 0;
7881
7882   insn_error = NULL;
7883
7884   /* If the instruction contains a '.', we first try to match an instruction
7885      including the '.'.  Then we try again without the '.'.  */
7886   insn = NULL;
7887   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7888     continue;
7889
7890   /* If we stopped on whitespace, then replace the whitespace with null for
7891      the call to hash_find.  Save the character we replaced just in case we
7892      have to re-parse the instruction.  */
7893   if (ISSPACE (*s))
7894     {
7895       save_c = *s;
7896       *s++ = '\0';
7897     }
7898
7899   insn = (struct mips_opcode *) hash_find (op_hash, str);
7900
7901   /* If we didn't find the instruction in the opcode table, try again, but
7902      this time with just the instruction up to, but not including the
7903      first '.'.  */
7904   if (insn == NULL)
7905     {
7906       /* Restore the character we overwrite above (if any).  */
7907       if (save_c)
7908         *(--s) = save_c;
7909
7910       /* Scan up to the first '.' or whitespace.  */
7911       for (s = str;
7912            *s != '\0' && *s != '.' && !ISSPACE (*s);
7913            ++s)
7914         continue;
7915
7916       /* If we did not find a '.', then we can quit now.  */
7917       if (*s != '.')
7918         {
7919           insn_error = "unrecognized opcode";
7920           return;
7921         }
7922
7923       /* Lookup the instruction in the hash table.  */
7924       *s++ = '\0';
7925       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7926         {
7927           insn_error = "unrecognized opcode";
7928           return;
7929         }
7930     }
7931
7932   argsStart = s;
7933   for (;;)
7934     {
7935       boolean ok;
7936
7937       assert (strcmp (insn->name, str) == 0);
7938
7939       if (OPCODE_IS_MEMBER (insn,
7940                             (mips_opts.isa
7941                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7942                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7943                             mips_arch))
7944         ok = true;
7945       else
7946         ok = false;
7947
7948       if (insn->pinfo != INSN_MACRO)
7949         {
7950           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7951             ok = false;
7952         }
7953
7954       if (! ok)
7955         {
7956           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7957               && strcmp (insn->name, insn[1].name) == 0)
7958             {
7959               ++insn;
7960               continue;
7961             }
7962           else
7963             {
7964               if (!insn_error)
7965                 {
7966                   static char buf[100];
7967                   sprintf (buf,
7968                            _("opcode not supported on this processor: %s (%s)"),
7969                            mips_cpu_to_str (mips_arch),
7970                            mips_isa_to_str (mips_opts.isa));
7971
7972                   insn_error = buf;
7973                 }
7974               if (save_c)
7975                 *(--s) = save_c;
7976               return;
7977             }
7978         }
7979
7980       ip->insn_mo = insn;
7981       ip->insn_opcode = insn->match;
7982       insn_error = NULL;
7983       for (args = insn->args;; ++args)
7984         {
7985           int is_mdmx;
7986
7987           s += strspn (s, " \t");
7988           is_mdmx = 0;
7989           switch (*args)
7990             {
7991             case '\0':          /* end of args */
7992               if (*s == '\0')
7993                 return;
7994               break;
7995
7996             case ',':
7997               if (*s++ == *args)
7998                 continue;
7999               s--;
8000               switch (*++args)
8001                 {
8002                 case 'r':
8003                 case 'v':
8004                   ip->insn_opcode |= lastregno << OP_SH_RS;
8005                   continue;
8006
8007                 case 'w':
8008                   ip->insn_opcode |= lastregno << OP_SH_RT;
8009                   continue;
8010
8011                 case 'W':
8012                   ip->insn_opcode |= lastregno << OP_SH_FT;
8013                   continue;
8014
8015                 case 'V':
8016                   ip->insn_opcode |= lastregno << OP_SH_FS;
8017                   continue;
8018                 }
8019               break;
8020
8021             case '(':
8022               /* Handle optional base register.
8023                  Either the base register is omitted or
8024                  we must have a left paren.  */
8025               /* This is dependent on the next operand specifier
8026                  is a base register specification.  */
8027               assert (args[1] == 'b' || args[1] == '5'
8028                       || args[1] == '-' || args[1] == '4');
8029               if (*s == '\0')
8030                 return;
8031
8032             case ')':           /* these must match exactly */
8033               if (*s++ == *args)
8034                 continue;
8035               break;
8036
8037             case '<':           /* must be at least one digit */
8038               /*
8039                * According to the manual, if the shift amount is greater
8040                * than 31 or less than 0, then the shift amount should be
8041                * mod 32.  In reality the mips assembler issues an error.
8042                * We issue a warning and mask out all but the low 5 bits.
8043                */
8044               my_getExpression (&imm_expr, s);
8045               check_absolute_expr (ip, &imm_expr);
8046               if ((unsigned long) imm_expr.X_add_number > 31)
8047                 {
8048                   as_warn (_("Improper shift amount (%ld)"),
8049                            (long) imm_expr.X_add_number);
8050                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8051                 }
8052               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8053               imm_expr.X_op = O_absent;
8054               s = expr_end;
8055               continue;
8056
8057             case '>':           /* shift amount minus 32 */
8058               my_getExpression (&imm_expr, s);
8059               check_absolute_expr (ip, &imm_expr);
8060               if ((unsigned long) imm_expr.X_add_number < 32
8061                   || (unsigned long) imm_expr.X_add_number > 63)
8062                 break;
8063               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8064               imm_expr.X_op = O_absent;
8065               s = expr_end;
8066               continue;
8067
8068             case 'k':           /* cache code */
8069             case 'h':           /* prefx code */
8070               my_getExpression (&imm_expr, s);
8071               check_absolute_expr (ip, &imm_expr);
8072               if ((unsigned long) imm_expr.X_add_number > 31)
8073                 {
8074                   as_warn (_("Invalid value for `%s' (%lu)"),
8075                            ip->insn_mo->name,
8076                            (unsigned long) imm_expr.X_add_number);
8077                   imm_expr.X_add_number &= 0x1f;
8078                 }
8079               if (*args == 'k')
8080                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8081               else
8082                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8083               imm_expr.X_op = O_absent;
8084               s = expr_end;
8085               continue;
8086
8087             case 'c':           /* break code */
8088               my_getExpression (&imm_expr, s);
8089               check_absolute_expr (ip, &imm_expr);
8090               if ((unsigned) imm_expr.X_add_number > 1023)
8091                 {
8092                   as_warn (_("Illegal break code (%ld)"),
8093                            (long) imm_expr.X_add_number);
8094                   imm_expr.X_add_number &= OP_MASK_CODE;
8095                 }
8096               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8097               imm_expr.X_op = O_absent;
8098               s = expr_end;
8099               continue;
8100
8101             case 'q':           /* lower break code */
8102               my_getExpression (&imm_expr, s);
8103               check_absolute_expr (ip, &imm_expr);
8104               if ((unsigned) imm_expr.X_add_number > 1023)
8105                 {
8106                   as_warn (_("Illegal lower break code (%ld)"),
8107                            (long) imm_expr.X_add_number);
8108                   imm_expr.X_add_number &= OP_MASK_CODE2;
8109                 }
8110               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8111               imm_expr.X_op = O_absent;
8112               s = expr_end;
8113               continue;
8114
8115             case 'B':           /* 20-bit syscall/break code.  */
8116               my_getExpression (&imm_expr, s);
8117               check_absolute_expr (ip, &imm_expr);
8118               if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
8119                 as_warn (_("Illegal 20-bit code (%ld)"),
8120                          (long) imm_expr.X_add_number);
8121               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8122               imm_expr.X_op = O_absent;
8123               s = expr_end;
8124               continue;
8125
8126             case 'C':           /* Coprocessor code */
8127               my_getExpression (&imm_expr, s);
8128               check_absolute_expr (ip, &imm_expr);
8129               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8130                 {
8131                   as_warn (_("Coproccesor code > 25 bits (%ld)"),
8132                            (long) imm_expr.X_add_number);
8133                   imm_expr.X_add_number &= ((1 << 25) - 1);
8134                 }
8135               ip->insn_opcode |= imm_expr.X_add_number;
8136               imm_expr.X_op = O_absent;
8137               s = expr_end;
8138               continue;
8139
8140             case 'J':           /* 19-bit wait code.  */
8141               my_getExpression (&imm_expr, s);
8142               check_absolute_expr (ip, &imm_expr);
8143               if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
8144                 as_warn (_("Illegal 19-bit code (%ld)"),
8145                          (long) imm_expr.X_add_number);
8146               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8147               imm_expr.X_op = O_absent;
8148               s = expr_end;
8149               continue;
8150
8151             case 'P':           /* Performance register */
8152               my_getExpression (&imm_expr, s);
8153               check_absolute_expr (ip, &imm_expr);
8154               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8155                 {
8156                   as_warn (_("Invalid performance register (%ld)"),
8157                            (long) imm_expr.X_add_number);
8158                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8159                 }
8160               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8161               imm_expr.X_op = O_absent;
8162               s = expr_end;
8163               continue;
8164
8165             case 'b':           /* base register */
8166             case 'd':           /* destination register */
8167             case 's':           /* source register */
8168             case 't':           /* target register */
8169             case 'r':           /* both target and source */
8170             case 'v':           /* both dest and source */
8171             case 'w':           /* both dest and target */
8172             case 'E':           /* coprocessor target register */
8173             case 'G':           /* coprocessor destination register */
8174             case 'x':           /* ignore register name */
8175             case 'z':           /* must be zero register */
8176             case 'U':           /* destination register (clo/clz).  */
8177               s_reset = s;
8178               if (s[0] == '$')
8179                 {
8180
8181                   if (ISDIGIT (s[1]))
8182                     {
8183                       ++s;
8184                       regno = 0;
8185                       do
8186                         {
8187                           regno *= 10;
8188                           regno += *s - '0';
8189                           ++s;
8190                         }
8191                       while (ISDIGIT (*s));
8192                       if (regno > 31)
8193                         as_bad (_("Invalid register number (%d)"), regno);
8194                     }
8195                   else if (*args == 'E' || *args == 'G')
8196                     goto notreg;
8197                   else
8198                     {
8199                       if (s[1] == 'r' && s[2] == 'a')
8200                         {
8201                           s += 3;
8202                           regno = RA;
8203                         }
8204                       else if (s[1] == 'f' && s[2] == 'p')
8205                         {
8206                           s += 3;
8207                           regno = FP;
8208                         }
8209                       else if (s[1] == 's' && s[2] == 'p')
8210                         {
8211                           s += 3;
8212                           regno = SP;
8213                         }
8214                       else if (s[1] == 'g' && s[2] == 'p')
8215                         {
8216                           s += 3;
8217                           regno = GP;
8218                         }
8219                       else if (s[1] == 'a' && s[2] == 't')
8220                         {
8221                           s += 3;
8222                           regno = AT;
8223                         }
8224                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8225                         {
8226                           s += 4;
8227                           regno = KT0;
8228                         }
8229                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8230                         {
8231                           s += 4;
8232                           regno = KT1;
8233                         }
8234                       else if (itbl_have_entries)
8235                         {
8236                           char *p, *n;
8237                           unsigned long r;
8238
8239                           p = s + 1;    /* advance past '$' */
8240                           n = itbl_get_field (&p);  /* n is name */
8241
8242                           /* See if this is a register defined in an
8243                              itbl entry.  */
8244                           if (itbl_get_reg_val (n, &r))
8245                             {
8246                               /* Get_field advances to the start of
8247                                  the next field, so we need to back
8248                                  rack to the end of the last field.  */
8249                               if (p)
8250                                 s = p - 1;
8251                               else
8252                                 s = strchr (s, '\0');
8253                               regno = r;
8254                             }
8255                           else
8256                             goto notreg;
8257                         }
8258                       else
8259                         goto notreg;
8260                     }
8261                   if (regno == AT
8262                       && ! mips_opts.noat
8263                       && *args != 'E'
8264                       && *args != 'G')
8265                     as_warn (_("Used $at without \".set noat\""));
8266                   c = *args;
8267                   if (*s == ' ')
8268                     ++s;
8269                   if (args[1] != *s)
8270                     {
8271                       if (c == 'r' || c == 'v' || c == 'w')
8272                         {
8273                           regno = lastregno;
8274                           s = s_reset;
8275                           ++args;
8276                         }
8277                     }
8278                   /* 'z' only matches $0.  */
8279                   if (c == 'z' && regno != 0)
8280                     break;
8281
8282         /* Now that we have assembled one operand, we use the args string
8283          * to figure out where it goes in the instruction.  */
8284                   switch (c)
8285                     {
8286                     case 'r':
8287                     case 's':
8288                     case 'v':
8289                     case 'b':
8290                       ip->insn_opcode |= regno << OP_SH_RS;
8291                       break;
8292                     case 'd':
8293                     case 'G':
8294                       ip->insn_opcode |= regno << OP_SH_RD;
8295                       break;
8296                     case 'U':
8297                       ip->insn_opcode |= regno << OP_SH_RD;
8298                       ip->insn_opcode |= regno << OP_SH_RT;
8299                       break;
8300                     case 'w':
8301                     case 't':
8302                     case 'E':
8303                       ip->insn_opcode |= regno << OP_SH_RT;
8304                       break;
8305                     case 'x':
8306                       /* This case exists because on the r3000 trunc
8307                          expands into a macro which requires a gp
8308                          register.  On the r6000 or r4000 it is
8309                          assembled into a single instruction which
8310                          ignores the register.  Thus the insn version
8311                          is MIPS_ISA2 and uses 'x', and the macro
8312                          version is MIPS_ISA1 and uses 't'.  */
8313                       break;
8314                     case 'z':
8315                       /* This case is for the div instruction, which
8316                          acts differently if the destination argument
8317                          is $0.  This only matches $0, and is checked
8318                          outside the switch.  */
8319                       break;
8320                     case 'D':
8321                       /* Itbl operand; not yet implemented. FIXME ?? */
8322                       break;
8323                       /* What about all other operands like 'i', which
8324                          can be specified in the opcode table? */
8325                     }
8326                   lastregno = regno;
8327                   continue;
8328                 }
8329             notreg:
8330               switch (*args++)
8331                 {
8332                 case 'r':
8333                 case 'v':
8334                   ip->insn_opcode |= lastregno << OP_SH_RS;
8335                   continue;
8336                 case 'w':
8337                   ip->insn_opcode |= lastregno << OP_SH_RT;
8338                   continue;
8339                 }
8340               break;
8341
8342             case 'O':           /* MDMX alignment immediate constant.  */
8343               my_getExpression (&imm_expr, s);
8344               check_absolute_expr (ip, &imm_expr);
8345               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8346                 {
8347                   as_warn ("Improper align amount (%ld), using low bits",
8348                            (long) imm_expr.X_add_number);
8349                   imm_expr.X_add_number &= OP_MASK_ALN;
8350                 }
8351               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8352               imm_expr.X_op = O_absent;
8353               s = expr_end;
8354               continue;
8355
8356             case 'Q':           /* MDMX vector, element sel, or const.  */
8357               if (s[0] != '$')
8358                 {
8359                   /* MDMX Immediate.  */
8360                   my_getExpression (&imm_expr, s);
8361                   check_absolute_expr (ip, &imm_expr);
8362                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8363                     {
8364                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8365                                (long) imm_expr.X_add_number);
8366                       imm_expr.X_add_number &= OP_MASK_FT;
8367                     }
8368                   imm_expr.X_add_number &= OP_MASK_FT;
8369                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8370                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8371                   else
8372                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8373                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8374                   imm_expr.X_op = O_absent;
8375                   s = expr_end;
8376                   continue;
8377                 }
8378               /* Not MDMX Immediate.  Fall through.  */
8379             case 'X':           /* MDMX destination register.  */
8380             case 'Y':           /* MDMX source register.  */
8381             case 'Z':           /* MDMX target register.  */
8382               is_mdmx = 1;
8383             case 'D':           /* floating point destination register */
8384             case 'S':           /* floating point source register */
8385             case 'T':           /* floating point target register */
8386             case 'R':           /* floating point source register */
8387             case 'V':
8388             case 'W':
8389               s_reset = s;
8390               /* Accept $fN for FP and MDMX register numbers, and in
8391                  addition accept $vN for MDMX register numbers.  */
8392               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8393                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8394                       && ISDIGIT (s[2])))
8395                 {
8396                   s += 2;
8397                   regno = 0;
8398                   do
8399                     {
8400                       regno *= 10;
8401                       regno += *s - '0';
8402                       ++s;
8403                     }
8404                   while (ISDIGIT (*s));
8405
8406                   if (regno > 31)
8407                     as_bad (_("Invalid float register number (%d)"), regno);
8408
8409                   if ((regno & 1) != 0
8410                       && HAVE_32BIT_FPRS
8411                       && ! (strcmp (str, "mtc1") == 0
8412                             || strcmp (str, "mfc1") == 0
8413                             || strcmp (str, "lwc1") == 0
8414                             || strcmp (str, "swc1") == 0
8415                             || strcmp (str, "l.s") == 0
8416                             || strcmp (str, "s.s") == 0))
8417                     as_warn (_("Float register should be even, was %d"),
8418                              regno);
8419
8420                   c = *args;
8421                   if (*s == ' ')
8422                     ++s;
8423                   if (args[1] != *s)
8424                     {
8425                       if (c == 'V' || c == 'W')
8426                         {
8427                           regno = lastregno;
8428                           s = s_reset;
8429                           ++args;
8430                         }
8431                     }
8432                   switch (c)
8433                     {
8434                     case 'D':
8435                     case 'X':
8436                       ip->insn_opcode |= regno << OP_SH_FD;
8437                       break;
8438                     case 'V':
8439                     case 'S':
8440                     case 'Y':
8441                       ip->insn_opcode |= regno << OP_SH_FS;
8442                       break;
8443                     case 'Q':
8444                       /* This is like 'Z', but also needs to fix the MDMX
8445                          vector/scalar select bits.  Note that the
8446                          scalar immediate case is handled above.  */
8447                       if (*s == '[')
8448                         {
8449                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8450                           int max_el = (is_qh ? 3 : 7);
8451                           s++;
8452                           my_getExpression(&imm_expr, s);
8453                           check_absolute_expr (ip, &imm_expr);
8454                           s = expr_end;
8455                           if (imm_expr.X_add_number > max_el)
8456                             as_bad(_("Bad element selector %ld"),
8457                                    (long) imm_expr.X_add_number);
8458                           imm_expr.X_add_number &= max_el;
8459                           ip->insn_opcode |= (imm_expr.X_add_number
8460                                               << (OP_SH_VSEL +
8461                                                   (is_qh ? 2 : 1)));
8462                           if (*s != ']')
8463                             as_warn(_("Expecting ']' found '%s'"), s);
8464                           else
8465                             s++;
8466                         }
8467                       else
8468                         {
8469                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8470                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8471                                                 << OP_SH_VSEL);
8472                           else
8473                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8474                                                 OP_SH_VSEL);
8475                         }
8476                       /* Fall through */
8477                     case 'W':
8478                     case 'T':
8479                     case 'Z':
8480                       ip->insn_opcode |= regno << OP_SH_FT;
8481                       break;
8482                     case 'R':
8483                       ip->insn_opcode |= regno << OP_SH_FR;
8484                       break;
8485                     }
8486                   lastregno = regno;
8487                   continue;
8488                 }
8489
8490               switch (*args++)
8491                 {
8492                 case 'V':
8493                   ip->insn_opcode |= lastregno << OP_SH_FS;
8494                   continue;
8495                 case 'W':
8496                   ip->insn_opcode |= lastregno << OP_SH_FT;
8497                   continue;
8498                 }
8499               break;
8500
8501             case 'I':
8502               my_getExpression (&imm_expr, s);
8503               if (imm_expr.X_op != O_big
8504                   && imm_expr.X_op != O_constant)
8505                 insn_error = _("absolute expression required");
8506               s = expr_end;
8507               continue;
8508
8509             case 'A':
8510               my_getExpression (&offset_expr, s);
8511               *imm_reloc = BFD_RELOC_32;
8512               s = expr_end;
8513               continue;
8514
8515             case 'F':
8516             case 'L':
8517             case 'f':
8518             case 'l':
8519               {
8520                 int f64;
8521                 int using_gprs;
8522                 char *save_in;
8523                 char *err;
8524                 unsigned char temp[8];
8525                 int len;
8526                 unsigned int length;
8527                 segT seg;
8528                 subsegT subseg;
8529                 char *p;
8530
8531                 /* These only appear as the last operand in an
8532                    instruction, and every instruction that accepts
8533                    them in any variant accepts them in all variants.
8534                    This means we don't have to worry about backing out
8535                    any changes if the instruction does not match.
8536
8537                    The difference between them is the size of the
8538                    floating point constant and where it goes.  For 'F'
8539                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8540                    is 32 bits.  Where the constant is placed is based
8541                    on how the MIPS assembler does things:
8542                     F -- .rdata
8543                     L -- .lit8
8544                     f -- immediate value
8545                     l -- .lit4
8546
8547                     The .lit4 and .lit8 sections are only used if
8548                     permitted by the -G argument.
8549
8550                     When generating embedded PIC code, we use the
8551                     .lit8 section but not the .lit4 section (we can do
8552                     .lit4 inline easily; we need to put .lit8
8553                     somewhere in the data segment, and using .lit8
8554                     permits the linker to eventually combine identical
8555                     .lit8 entries).
8556
8557                     The code below needs to know whether the target register
8558                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8559                     'F' are used with GPR-based instructions and 'l' and
8560                     'L' are used with FPR-based instructions.  */
8561
8562                 f64 = *args == 'F' || *args == 'L';
8563                 using_gprs = *args == 'F' || *args == 'f';
8564
8565                 save_in = input_line_pointer;
8566                 input_line_pointer = s;
8567                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8568                 length = len;
8569                 s = input_line_pointer;
8570                 input_line_pointer = save_in;
8571                 if (err != NULL && *err != '\0')
8572                   {
8573                     as_bad (_("Bad floating point constant: %s"), err);
8574                     memset (temp, '\0', sizeof temp);
8575                     length = f64 ? 8 : 4;
8576                   }
8577
8578                 assert (length == (unsigned) (f64 ? 8 : 4));
8579
8580                 if (*args == 'f'
8581                     || (*args == 'l'
8582                         && (! USE_GLOBAL_POINTER_OPT
8583                             || mips_pic == EMBEDDED_PIC
8584                             || g_switch_value < 4
8585                             || (temp[0] == 0 && temp[1] == 0)
8586                             || (temp[2] == 0 && temp[3] == 0))))
8587                   {
8588                     imm_expr.X_op = O_constant;
8589                     if (! target_big_endian)
8590                       imm_expr.X_add_number = bfd_getl32 (temp);
8591                     else
8592                       imm_expr.X_add_number = bfd_getb32 (temp);
8593                   }
8594                 else if (length > 4
8595                          && ! mips_disable_float_construction
8596                          /* Constants can only be constructed in GPRs and
8597                             copied to FPRs if the GPRs are at least as wide
8598                             as the FPRs.  Force the constant into memory if
8599                             we are using 64-bit FPRs but the GPRs are only
8600                             32 bits wide.  */
8601                          && (using_gprs
8602                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8603                          && ((temp[0] == 0 && temp[1] == 0)
8604                              || (temp[2] == 0 && temp[3] == 0))
8605                          && ((temp[4] == 0 && temp[5] == 0)
8606                              || (temp[6] == 0 && temp[7] == 0)))
8607                   {
8608                     /* The value is simple enough to load with a couple of
8609                        instructions.  If using 32-bit registers, set
8610                        imm_expr to the high order 32 bits and offset_expr to
8611                        the low order 32 bits.  Otherwise, set imm_expr to
8612                        the entire 64 bit constant.  */
8613                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8614                       {
8615                         imm_expr.X_op = O_constant;
8616                         offset_expr.X_op = O_constant;
8617                         if (! target_big_endian)
8618                           {
8619                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8620                             offset_expr.X_add_number = bfd_getl32 (temp);
8621                           }
8622                         else
8623                           {
8624                             imm_expr.X_add_number = bfd_getb32 (temp);
8625                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8626                           }
8627                         if (offset_expr.X_add_number == 0)
8628                           offset_expr.X_op = O_absent;
8629                       }
8630                     else if (sizeof (imm_expr.X_add_number) > 4)
8631                       {
8632                         imm_expr.X_op = O_constant;
8633                         if (! target_big_endian)
8634                           imm_expr.X_add_number = bfd_getl64 (temp);
8635                         else
8636                           imm_expr.X_add_number = bfd_getb64 (temp);
8637                       }
8638                     else
8639                       {
8640                         imm_expr.X_op = O_big;
8641                         imm_expr.X_add_number = 4;
8642                         if (! target_big_endian)
8643                           {
8644                             generic_bignum[0] = bfd_getl16 (temp);
8645                             generic_bignum[1] = bfd_getl16 (temp + 2);
8646                             generic_bignum[2] = bfd_getl16 (temp + 4);
8647                             generic_bignum[3] = bfd_getl16 (temp + 6);
8648                           }
8649                         else
8650                           {
8651                             generic_bignum[0] = bfd_getb16 (temp + 6);
8652                             generic_bignum[1] = bfd_getb16 (temp + 4);
8653                             generic_bignum[2] = bfd_getb16 (temp + 2);
8654                             generic_bignum[3] = bfd_getb16 (temp);
8655                           }
8656                       }
8657                   }
8658                 else
8659                   {
8660                     const char *newname;
8661                     segT new_seg;
8662
8663                     /* Switch to the right section.  */
8664                     seg = now_seg;
8665                     subseg = now_subseg;
8666                     switch (*args)
8667                       {
8668                       default: /* unused default case avoids warnings.  */
8669                       case 'L':
8670                         newname = RDATA_SECTION_NAME;
8671                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8672                             || mips_pic == EMBEDDED_PIC)
8673                           newname = ".lit8";
8674                         break;
8675                       case 'F':
8676                         if (mips_pic == EMBEDDED_PIC)
8677                           newname = ".lit8";
8678                         else
8679                           newname = RDATA_SECTION_NAME;
8680                         break;
8681                       case 'l':
8682                         assert (!USE_GLOBAL_POINTER_OPT
8683                                 || g_switch_value >= 4);
8684                         newname = ".lit4";
8685                         break;
8686                       }
8687                     new_seg = subseg_new (newname, (subsegT) 0);
8688                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8689                       bfd_set_section_flags (stdoutput, new_seg,
8690                                              (SEC_ALLOC
8691                                               | SEC_LOAD
8692                                               | SEC_READONLY
8693                                               | SEC_DATA));
8694                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8695                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8696                         && strcmp (TARGET_OS, "elf") != 0)
8697                       record_alignment (new_seg, 4);
8698                     else
8699                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8700                     if (seg == now_seg)
8701                       as_bad (_("Can't use floating point insn in this section"));
8702
8703                     /* Set the argument to the current address in the
8704                        section.  */
8705                     offset_expr.X_op = O_symbol;
8706                     offset_expr.X_add_symbol =
8707                       symbol_new ("L0\001", now_seg,
8708                                   (valueT) frag_now_fix (), frag_now);
8709                     offset_expr.X_add_number = 0;
8710
8711                     /* Put the floating point number into the section.  */
8712                     p = frag_more ((int) length);
8713                     memcpy (p, temp, length);
8714
8715                     /* Switch back to the original section.  */
8716                     subseg_set (seg, subseg);
8717                   }
8718               }
8719               continue;
8720
8721             case 'i':           /* 16 bit unsigned immediate */
8722             case 'j':           /* 16 bit signed immediate */
8723               *imm_reloc = BFD_RELOC_LO16;
8724               c = my_getSmallExpression (&imm_expr, s);
8725               if (c != S_EX_NONE)
8726                 {
8727                   if (c != S_EX_LO)
8728                     {
8729                       if (imm_expr.X_op == O_constant)
8730                         imm_expr.X_add_number =
8731                           (imm_expr.X_add_number >> 16) & 0xffff;
8732 #ifdef OBJ_ELF
8733                       else if (c == S_EX_HIGHEST)
8734                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8735                       else if (c == S_EX_HIGHER)
8736                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8737                       else if (c == S_EX_GP_REL)
8738                         {
8739                           /* This occurs in NewABI only.  */
8740                           c = my_getSmallExpression (&imm_expr, s);
8741                           if (c != S_EX_NEG)
8742                             as_bad (_("bad composition of relocations"));
8743                           else
8744                             {
8745                               c = my_getSmallExpression (&imm_expr, s);
8746                               if (c != S_EX_LO)
8747                                 as_bad (_("bad composition of relocations"));
8748                               else
8749                                 {
8750                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8751                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8752                                   imm_reloc[2] = BFD_RELOC_LO16;
8753                                 }
8754                             }
8755                         }
8756 #endif
8757                       else if (c == S_EX_HI)
8758                         {
8759                           *imm_reloc = BFD_RELOC_HI16_S;
8760                           imm_unmatched_hi = true;
8761                         }
8762                       else
8763                         *imm_reloc = BFD_RELOC_HI16;
8764                     }
8765                   else if (imm_expr.X_op == O_constant)
8766                     imm_expr.X_add_number &= 0xffff;
8767                 }
8768               if (*args == 'i')
8769                 {
8770                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8771                       || ((imm_expr.X_add_number < 0
8772                            || imm_expr.X_add_number >= 0x10000)
8773                           && imm_expr.X_op == O_constant))
8774                     {
8775                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8776                           !strcmp (insn->name, insn[1].name))
8777                         break;
8778                       if (imm_expr.X_op == O_constant
8779                           || imm_expr.X_op == O_big)
8780                         as_bad (_("16 bit expression not in range 0..65535"));
8781                     }
8782                 }
8783               else
8784                 {
8785                   int more;
8786                   offsetT max;
8787
8788                   /* The upper bound should be 0x8000, but
8789                      unfortunately the MIPS assembler accepts numbers
8790                      from 0x8000 to 0xffff and sign extends them, and
8791                      we want to be compatible.  We only permit this
8792                      extended range for an instruction which does not
8793                      provide any further alternates, since those
8794                      alternates may handle other cases.  People should
8795                      use the numbers they mean, rather than relying on
8796                      a mysterious sign extension.  */
8797                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8798                           strcmp (insn->name, insn[1].name) == 0);
8799                   if (more)
8800                     max = 0x8000;
8801                   else
8802                     max = 0x10000;
8803                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8804                       || ((imm_expr.X_add_number < -0x8000
8805                            || imm_expr.X_add_number >= max)
8806                           && imm_expr.X_op == O_constant)
8807                       || (more
8808                           && imm_expr.X_add_number < 0
8809                           && HAVE_64BIT_GPRS
8810                           && imm_expr.X_unsigned
8811                           && sizeof (imm_expr.X_add_number) <= 4))
8812                     {
8813                       if (more)
8814                         break;
8815                       if (imm_expr.X_op == O_constant
8816                           || imm_expr.X_op == O_big)
8817                         as_bad (_("16 bit expression not in range -32768..32767"));
8818                     }
8819                 }
8820               s = expr_end;
8821               continue;
8822
8823             case 'o':           /* 16 bit offset */
8824               c = my_getSmallExpression (&offset_expr, s);
8825
8826               /* If this value won't fit into a 16 bit offset, then go
8827                  find a macro that will generate the 32 bit offset
8828                  code pattern.  */
8829               if (c == S_EX_NONE
8830                   && (offset_expr.X_op != O_constant
8831                       || offset_expr.X_add_number >= 0x8000
8832                       || offset_expr.X_add_number < -0x8000))
8833                 break;
8834
8835               if (c == S_EX_HI)
8836                 {
8837                   if (offset_expr.X_op != O_constant)
8838                     break;
8839                   offset_expr.X_add_number =
8840                     (offset_expr.X_add_number >> 16) & 0xffff;
8841                 }
8842               *offset_reloc = BFD_RELOC_LO16;
8843               s = expr_end;
8844               continue;
8845
8846             case 'p':           /* pc relative offset */
8847               if (mips_pic == EMBEDDED_PIC)
8848                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8849               else
8850                 *offset_reloc = BFD_RELOC_16_PCREL;
8851               my_getExpression (&offset_expr, s);
8852               s = expr_end;
8853               continue;
8854
8855             case 'u':           /* upper 16 bits */
8856               c = my_getSmallExpression (&imm_expr, s);
8857               *imm_reloc = BFD_RELOC_LO16;
8858               if (c != S_EX_NONE)
8859                 {
8860                   if (c != S_EX_LO)
8861                     {
8862                       if (imm_expr.X_op == O_constant)
8863                         imm_expr.X_add_number =
8864                           (imm_expr.X_add_number >> 16) & 0xffff;
8865                       else if (c == S_EX_HI)
8866                         {
8867                           *imm_reloc = BFD_RELOC_HI16_S;
8868                           imm_unmatched_hi = true;
8869                         }
8870 #ifdef OBJ_ELF
8871                       else if (c == S_EX_HIGHEST)
8872                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8873                       else if (c == S_EX_GP_REL)
8874                         {
8875                           /* This occurs in NewABI only.  */
8876                           c = my_getSmallExpression (&imm_expr, s);
8877                           if (c != S_EX_NEG)
8878                             as_bad (_("bad composition of relocations"));
8879                           else
8880                             {
8881                               c = my_getSmallExpression (&imm_expr, s);
8882                               if (c != S_EX_HI)
8883                                 as_bad (_("bad composition of relocations"));
8884                               else
8885                                 {
8886                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8887                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8888                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8889                                 }
8890                             }
8891                         }
8892 #endif
8893                       else
8894                         *imm_reloc = BFD_RELOC_HI16;
8895                     }
8896                   else if (imm_expr.X_op == O_constant)
8897                     imm_expr.X_add_number &= 0xffff;
8898                 }
8899               if (imm_expr.X_op == O_constant
8900                   && (imm_expr.X_add_number < 0
8901                       || imm_expr.X_add_number >= 0x10000))
8902                 as_bad (_("lui expression not in range 0..65535"));
8903               s = expr_end;
8904               continue;
8905
8906             case 'a':           /* 26 bit address */
8907               my_getExpression (&offset_expr, s);
8908               s = expr_end;
8909               *offset_reloc = BFD_RELOC_MIPS_JMP;
8910               continue;
8911
8912             case 'N':           /* 3 bit branch condition code */
8913             case 'M':           /* 3 bit compare condition code */
8914               if (strncmp (s, "$fcc", 4) != 0)
8915                 break;
8916               s += 4;
8917               regno = 0;
8918               do
8919                 {
8920                   regno *= 10;
8921                   regno += *s - '0';
8922                   ++s;
8923                 }
8924               while (ISDIGIT (*s));
8925               if (regno > 7)
8926                 as_bad (_("invalid condition code register $fcc%d"), regno);
8927               if (*args == 'N')
8928                 ip->insn_opcode |= regno << OP_SH_BCC;
8929               else
8930                 ip->insn_opcode |= regno << OP_SH_CCC;
8931               continue;
8932
8933             case 'H':
8934               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8935                 s += 2;
8936               if (ISDIGIT (*s))
8937                 {
8938                   c = 0;
8939                   do
8940                     {
8941                       c *= 10;
8942                       c += *s - '0';
8943                       ++s;
8944                     }
8945                   while (ISDIGIT (*s));
8946                 }
8947               else
8948                 c = 8; /* Invalid sel value.  */
8949
8950               if (c > 7)
8951                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8952               ip->insn_opcode |= c;
8953               continue;
8954
8955             default:
8956               as_bad (_("bad char = '%c'\n"), *args);
8957               internalError ();
8958             }
8959           break;
8960         }
8961       /* Args don't match.  */
8962       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8963           !strcmp (insn->name, insn[1].name))
8964         {
8965           ++insn;
8966           s = argsStart;
8967           insn_error = _("illegal operands");
8968           continue;
8969         }
8970       if (save_c)
8971         *(--s) = save_c;
8972       insn_error = _("illegal operands");
8973       return;
8974     }
8975 }
8976
8977 /* This routine assembles an instruction into its binary format when
8978    assembling for the mips16.  As a side effect, it sets one of the
8979    global variables imm_reloc or offset_reloc to the type of
8980    relocation to do if one of the operands is an address expression.
8981    It also sets mips16_small and mips16_ext if the user explicitly
8982    requested a small or extended instruction.  */
8983
8984 static void
8985 mips16_ip (str, ip)
8986      char *str;
8987      struct mips_cl_insn *ip;
8988 {
8989   char *s;
8990   const char *args;
8991   struct mips_opcode *insn;
8992   char *argsstart;
8993   unsigned int regno;
8994   unsigned int lastregno = 0;
8995   char *s_reset;
8996
8997   insn_error = NULL;
8998
8999   mips16_small = false;
9000   mips16_ext = false;
9001
9002   for (s = str; ISLOWER (*s); ++s)
9003     ;
9004   switch (*s)
9005     {
9006     case '\0':
9007       break;
9008
9009     case ' ':
9010       *s++ = '\0';
9011       break;
9012
9013     case '.':
9014       if (s[1] == 't' && s[2] == ' ')
9015         {
9016           *s = '\0';
9017           mips16_small = true;
9018           s += 3;
9019           break;
9020         }
9021       else if (s[1] == 'e' && s[2] == ' ')
9022         {
9023           *s = '\0';
9024           mips16_ext = true;
9025           s += 3;
9026           break;
9027         }
9028       /* Fall through.  */
9029     default:
9030       insn_error = _("unknown opcode");
9031       return;
9032     }
9033
9034   if (mips_opts.noautoextend && ! mips16_ext)
9035     mips16_small = true;
9036
9037   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9038     {
9039       insn_error = _("unrecognized opcode");
9040       return;
9041     }
9042
9043   argsstart = s;
9044   for (;;)
9045     {
9046       assert (strcmp (insn->name, str) == 0);
9047
9048       ip->insn_mo = insn;
9049       ip->insn_opcode = insn->match;
9050       ip->use_extend = false;
9051       imm_expr.X_op = O_absent;
9052       imm_reloc[0] = BFD_RELOC_UNUSED;
9053       imm_reloc[1] = BFD_RELOC_UNUSED;
9054       imm_reloc[2] = BFD_RELOC_UNUSED;
9055       offset_expr.X_op = O_absent;
9056       offset_reloc[0] = BFD_RELOC_UNUSED;
9057       offset_reloc[1] = BFD_RELOC_UNUSED;
9058       offset_reloc[2] = BFD_RELOC_UNUSED;
9059       for (args = insn->args; 1; ++args)
9060         {
9061           int c;
9062
9063           if (*s == ' ')
9064             ++s;
9065
9066           /* In this switch statement we call break if we did not find
9067              a match, continue if we did find a match, or return if we
9068              are done.  */
9069
9070           c = *args;
9071           switch (c)
9072             {
9073             case '\0':
9074               if (*s == '\0')
9075                 {
9076                   /* Stuff the immediate value in now, if we can.  */
9077                   if (imm_expr.X_op == O_constant
9078                       && *imm_reloc > BFD_RELOC_UNUSED
9079                       && insn->pinfo != INSN_MACRO)
9080                     {
9081                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9082                                     imm_expr.X_add_number, true, mips16_small,
9083                                     mips16_ext, &ip->insn_opcode,
9084                                     &ip->use_extend, &ip->extend);
9085                       imm_expr.X_op = O_absent;
9086                       *imm_reloc = BFD_RELOC_UNUSED;
9087                     }
9088
9089                   return;
9090                 }
9091               break;
9092
9093             case ',':
9094               if (*s++ == c)
9095                 continue;
9096               s--;
9097               switch (*++args)
9098                 {
9099                 case 'v':
9100                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9101                   continue;
9102                 case 'w':
9103                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9104                   continue;
9105                 }
9106               break;
9107
9108             case '(':
9109             case ')':
9110               if (*s++ == c)
9111                 continue;
9112               break;
9113
9114             case 'v':
9115             case 'w':
9116               if (s[0] != '$')
9117                 {
9118                   if (c == 'v')
9119                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9120                   else
9121                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9122                   ++args;
9123                   continue;
9124                 }
9125               /* Fall through.  */
9126             case 'x':
9127             case 'y':
9128             case 'z':
9129             case 'Z':
9130             case '0':
9131             case 'S':
9132             case 'R':
9133             case 'X':
9134             case 'Y':
9135               if (s[0] != '$')
9136                 break;
9137               s_reset = s;
9138               if (ISDIGIT (s[1]))
9139                 {
9140                   ++s;
9141                   regno = 0;
9142                   do
9143                     {
9144                       regno *= 10;
9145                       regno += *s - '0';
9146                       ++s;
9147                     }
9148                   while (ISDIGIT (*s));
9149                   if (regno > 31)
9150                     {
9151                       as_bad (_("invalid register number (%d)"), regno);
9152                       regno = 2;
9153                     }
9154                 }
9155               else
9156                 {
9157                   if (s[1] == 'r' && s[2] == 'a')
9158                     {
9159                       s += 3;
9160                       regno = RA;
9161                     }
9162                   else if (s[1] == 'f' && s[2] == 'p')
9163                     {
9164                       s += 3;
9165                       regno = FP;
9166                     }
9167                   else if (s[1] == 's' && s[2] == 'p')
9168                     {
9169                       s += 3;
9170                       regno = SP;
9171                     }
9172                   else if (s[1] == 'g' && s[2] == 'p')
9173                     {
9174                       s += 3;
9175                       regno = GP;
9176                     }
9177                   else if (s[1] == 'a' && s[2] == 't')
9178                     {
9179                       s += 3;
9180                       regno = AT;
9181                     }
9182                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9183                     {
9184                       s += 4;
9185                       regno = KT0;
9186                     }
9187                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9188                     {
9189                       s += 4;
9190                       regno = KT1;
9191                     }
9192                   else
9193                     break;
9194                 }
9195
9196               if (*s == ' ')
9197                 ++s;
9198               if (args[1] != *s)
9199                 {
9200                   if (c == 'v' || c == 'w')
9201                     {
9202                       regno = mips16_to_32_reg_map[lastregno];
9203                       s = s_reset;
9204                       ++args;
9205                     }
9206                 }
9207
9208               switch (c)
9209                 {
9210                 case 'x':
9211                 case 'y':
9212                 case 'z':
9213                 case 'v':
9214                 case 'w':
9215                 case 'Z':
9216                   regno = mips32_to_16_reg_map[regno];
9217                   break;
9218
9219                 case '0':
9220                   if (regno != 0)
9221                     regno = ILLEGAL_REG;
9222                   break;
9223
9224                 case 'S':
9225                   if (regno != SP)
9226                     regno = ILLEGAL_REG;
9227                   break;
9228
9229                 case 'R':
9230                   if (regno != RA)
9231                     regno = ILLEGAL_REG;
9232                   break;
9233
9234                 case 'X':
9235                 case 'Y':
9236                   if (regno == AT && ! mips_opts.noat)
9237                     as_warn (_("used $at without \".set noat\""));
9238                   break;
9239
9240                 default:
9241                   internalError ();
9242                 }
9243
9244               if (regno == ILLEGAL_REG)
9245                 break;
9246
9247               switch (c)
9248                 {
9249                 case 'x':
9250                 case 'v':
9251                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9252                   break;
9253                 case 'y':
9254                 case 'w':
9255                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9256                   break;
9257                 case 'z':
9258                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9259                   break;
9260                 case 'Z':
9261                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9262                 case '0':
9263                 case 'S':
9264                 case 'R':
9265                   break;
9266                 case 'X':
9267                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9268                   break;
9269                 case 'Y':
9270                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9271                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9272                   break;
9273                 default:
9274                   internalError ();
9275                 }
9276
9277               lastregno = regno;
9278               continue;
9279
9280             case 'P':
9281               if (strncmp (s, "$pc", 3) == 0)
9282                 {
9283                   s += 3;
9284                   continue;
9285                 }
9286               break;
9287
9288             case '<':
9289             case '>':
9290             case '[':
9291             case ']':
9292             case '4':
9293             case '5':
9294             case 'H':
9295             case 'W':
9296             case 'D':
9297             case 'j':
9298             case '8':
9299             case 'V':
9300             case 'C':
9301             case 'U':
9302             case 'k':
9303             case 'K':
9304               if (s[0] == '%'
9305                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9306                 {
9307                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9308                      and generate the appropriate reloc.  If the text
9309                      inside %gprel is not a symbol name with an
9310                      optional offset, then we generate a normal reloc
9311                      and will probably fail later.  */
9312                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9313                   if (imm_expr.X_op == O_symbol)
9314                     {
9315                       mips16_ext = true;
9316                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9317                       s = expr_end;
9318                       ip->use_extend = true;
9319                       ip->extend = 0;
9320                       continue;
9321                     }
9322                 }
9323               else
9324                 {
9325                   /* Just pick up a normal expression.  */
9326                   my_getExpression (&imm_expr, s);
9327                 }
9328
9329               if (imm_expr.X_op == O_register)
9330                 {
9331                   /* What we thought was an expression turned out to
9332                      be a register.  */
9333
9334                   if (s[0] == '(' && args[1] == '(')
9335                     {
9336                       /* It looks like the expression was omitted
9337                          before a register indirection, which means
9338                          that the expression is implicitly zero.  We
9339                          still set up imm_expr, so that we handle
9340                          explicit extensions correctly.  */
9341                       imm_expr.X_op = O_constant;
9342                       imm_expr.X_add_number = 0;
9343                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9344                       continue;
9345                     }
9346
9347                   break;
9348                 }
9349
9350               /* We need to relax this instruction.  */
9351               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9352               s = expr_end;
9353               continue;
9354
9355             case 'p':
9356             case 'q':
9357             case 'A':
9358             case 'B':
9359             case 'E':
9360               /* We use offset_reloc rather than imm_reloc for the PC
9361                  relative operands.  This lets macros with both
9362                  immediate and address operands work correctly.  */
9363               my_getExpression (&offset_expr, s);
9364
9365               if (offset_expr.X_op == O_register)
9366                 break;
9367
9368               /* We need to relax this instruction.  */
9369               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9370               s = expr_end;
9371               continue;
9372
9373             case '6':           /* break code */
9374               my_getExpression (&imm_expr, s);
9375               check_absolute_expr (ip, &imm_expr);
9376               if ((unsigned long) imm_expr.X_add_number > 63)
9377                 {
9378                   as_warn (_("Invalid value for `%s' (%lu)"),
9379                            ip->insn_mo->name,
9380                            (unsigned long) imm_expr.X_add_number);
9381                   imm_expr.X_add_number &= 0x3f;
9382                 }
9383               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9384               imm_expr.X_op = O_absent;
9385               s = expr_end;
9386               continue;
9387
9388             case 'a':           /* 26 bit address */
9389               my_getExpression (&offset_expr, s);
9390               s = expr_end;
9391               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9392               ip->insn_opcode <<= 16;
9393               continue;
9394
9395             case 'l':           /* register list for entry macro */
9396             case 'L':           /* register list for exit macro */
9397               {
9398                 int mask;
9399
9400                 if (c == 'l')
9401                   mask = 0;
9402                 else
9403                   mask = 7 << 3;
9404                 while (*s != '\0')
9405                   {
9406                     int freg, reg1, reg2;
9407
9408                     while (*s == ' ' || *s == ',')
9409                       ++s;
9410                     if (*s != '$')
9411                       {
9412                         as_bad (_("can't parse register list"));
9413                         break;
9414                       }
9415                     ++s;
9416                     if (*s != 'f')
9417                       freg = 0;
9418                     else
9419                       {
9420                         freg = 1;
9421                         ++s;
9422                       }
9423                     reg1 = 0;
9424                     while (ISDIGIT (*s))
9425                       {
9426                         reg1 *= 10;
9427                         reg1 += *s - '0';
9428                         ++s;
9429                       }
9430                     if (*s == ' ')
9431                       ++s;
9432                     if (*s != '-')
9433                       reg2 = reg1;
9434                     else
9435                       {
9436                         ++s;
9437                         if (*s != '$')
9438                           break;
9439                         ++s;
9440                         if (freg)
9441                           {
9442                             if (*s == 'f')
9443                               ++s;
9444                             else
9445                               {
9446                                 as_bad (_("invalid register list"));
9447                                 break;
9448                               }
9449                           }
9450                         reg2 = 0;
9451                         while (ISDIGIT (*s))
9452                           {
9453                             reg2 *= 10;
9454                             reg2 += *s - '0';
9455                             ++s;
9456                           }
9457                       }
9458                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9459                       {
9460                         mask &= ~ (7 << 3);
9461                         mask |= 5 << 3;
9462                       }
9463                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9464                       {
9465                         mask &= ~ (7 << 3);
9466                         mask |= 6 << 3;
9467                       }
9468                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9469                       mask |= (reg2 - 3) << 3;
9470                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9471                       mask |= (reg2 - 15) << 1;
9472                     else if (reg1 == RA && reg2 == RA)
9473                       mask |= 1;
9474                     else
9475                       {
9476                         as_bad (_("invalid register list"));
9477                         break;
9478                       }
9479                   }
9480                 /* The mask is filled in in the opcode table for the
9481                    benefit of the disassembler.  We remove it before
9482                    applying the actual mask.  */
9483                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9484                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9485               }
9486             continue;
9487
9488             case 'e':           /* extend code */
9489               my_getExpression (&imm_expr, s);
9490               check_absolute_expr (ip, &imm_expr);
9491               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9492                 {
9493                   as_warn (_("Invalid value for `%s' (%lu)"),
9494                            ip->insn_mo->name,
9495                            (unsigned long) imm_expr.X_add_number);
9496                   imm_expr.X_add_number &= 0x7ff;
9497                 }
9498               ip->insn_opcode |= imm_expr.X_add_number;
9499               imm_expr.X_op = O_absent;
9500               s = expr_end;
9501               continue;
9502
9503             default:
9504               internalError ();
9505             }
9506           break;
9507         }
9508
9509       /* Args don't match.  */
9510       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9511           strcmp (insn->name, insn[1].name) == 0)
9512         {
9513           ++insn;
9514           s = argsstart;
9515           continue;
9516         }
9517
9518       insn_error = _("illegal operands");
9519
9520       return;
9521     }
9522 }
9523
9524 /* This structure holds information we know about a mips16 immediate
9525    argument type.  */
9526
9527 struct mips16_immed_operand
9528 {
9529   /* The type code used in the argument string in the opcode table.  */
9530   int type;
9531   /* The number of bits in the short form of the opcode.  */
9532   int nbits;
9533   /* The number of bits in the extended form of the opcode.  */
9534   int extbits;
9535   /* The amount by which the short form is shifted when it is used;
9536      for example, the sw instruction has a shift count of 2.  */
9537   int shift;
9538   /* The amount by which the short form is shifted when it is stored
9539      into the instruction code.  */
9540   int op_shift;
9541   /* Non-zero if the short form is unsigned.  */
9542   int unsp;
9543   /* Non-zero if the extended form is unsigned.  */
9544   int extu;
9545   /* Non-zero if the value is PC relative.  */
9546   int pcrel;
9547 };
9548
9549 /* The mips16 immediate operand types.  */
9550
9551 static const struct mips16_immed_operand mips16_immed_operands[] =
9552 {
9553   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9554   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9555   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9556   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9557   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9558   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9559   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9560   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9561   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9562   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9563   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9564   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9565   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9566   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9567   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9568   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9569   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9570   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9571   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9572   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9573   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9574 };
9575
9576 #define MIPS16_NUM_IMMED \
9577   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9578
9579 /* Handle a mips16 instruction with an immediate value.  This or's the
9580    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9581    whether an extended value is needed; if one is needed, it sets
9582    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9583    If SMALL is true, an unextended opcode was explicitly requested.
9584    If EXT is true, an extended opcode was explicitly requested.  If
9585    WARN is true, warn if EXT does not match reality.  */
9586
9587 static void
9588 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9589               extend)
9590      char *file;
9591      unsigned int line;
9592      int type;
9593      offsetT val;
9594      boolean warn;
9595      boolean small;
9596      boolean ext;
9597      unsigned long *insn;
9598      boolean *use_extend;
9599      unsigned short *extend;
9600 {
9601   register const struct mips16_immed_operand *op;
9602   int mintiny, maxtiny;
9603   boolean needext;
9604
9605   op = mips16_immed_operands;
9606   while (op->type != type)
9607     {
9608       ++op;
9609       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9610     }
9611
9612   if (op->unsp)
9613     {
9614       if (type == '<' || type == '>' || type == '[' || type == ']')
9615         {
9616           mintiny = 1;
9617           maxtiny = 1 << op->nbits;
9618         }
9619       else
9620         {
9621           mintiny = 0;
9622           maxtiny = (1 << op->nbits) - 1;
9623         }
9624     }
9625   else
9626     {
9627       mintiny = - (1 << (op->nbits - 1));
9628       maxtiny = (1 << (op->nbits - 1)) - 1;
9629     }
9630
9631   /* Branch offsets have an implicit 0 in the lowest bit.  */
9632   if (type == 'p' || type == 'q')
9633     val /= 2;
9634
9635   if ((val & ((1 << op->shift) - 1)) != 0
9636       || val < (mintiny << op->shift)
9637       || val > (maxtiny << op->shift))
9638     needext = true;
9639   else
9640     needext = false;
9641
9642   if (warn && ext && ! needext)
9643     as_warn_where (file, line,
9644                    _("extended operand requested but not required"));
9645   if (small && needext)
9646     as_bad_where (file, line, _("invalid unextended operand value"));
9647
9648   if (small || (! ext && ! needext))
9649     {
9650       int insnval;
9651
9652       *use_extend = false;
9653       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9654       insnval <<= op->op_shift;
9655       *insn |= insnval;
9656     }
9657   else
9658     {
9659       long minext, maxext;
9660       int extval;
9661
9662       if (op->extu)
9663         {
9664           minext = 0;
9665           maxext = (1 << op->extbits) - 1;
9666         }
9667       else
9668         {
9669           minext = - (1 << (op->extbits - 1));
9670           maxext = (1 << (op->extbits - 1)) - 1;
9671         }
9672       if (val < minext || val > maxext)
9673         as_bad_where (file, line,
9674                       _("operand value out of range for instruction"));
9675
9676       *use_extend = true;
9677       if (op->extbits == 16)
9678         {
9679           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9680           val &= 0x1f;
9681         }
9682       else if (op->extbits == 15)
9683         {
9684           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9685           val &= 0xf;
9686         }
9687       else
9688         {
9689           extval = ((val & 0x1f) << 6) | (val & 0x20);
9690           val = 0;
9691         }
9692
9693       *extend = (unsigned short) extval;
9694       *insn |= val;
9695     }
9696 }
9697 \f
9698 static struct percent_op_match
9699 {
9700    const char *str;
9701    const enum small_ex_type type;
9702 } percent_op[] =
9703 {
9704   {"%lo", S_EX_LO},
9705 #ifdef OBJ_ELF
9706   {"%call_hi", S_EX_CALL_HI},
9707   {"%call_lo", S_EX_CALL_LO},
9708   {"%call16", S_EX_CALL16},
9709   {"%got_disp", S_EX_GOT_DISP},
9710   {"%got_page", S_EX_GOT_PAGE},
9711   {"%got_ofst", S_EX_GOT_OFST},
9712   {"%got_hi", S_EX_GOT_HI},
9713   {"%got_lo", S_EX_GOT_LO},
9714   {"%got", S_EX_GOT},
9715   {"%gp_rel", S_EX_GP_REL},
9716   {"%half", S_EX_HALF},
9717   {"%highest", S_EX_HIGHEST},
9718   {"%higher", S_EX_HIGHER},
9719   {"%neg", S_EX_NEG},
9720 #endif
9721   {"%hi", S_EX_HI}
9722 };
9723
9724 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9725    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9726    can be nested, this is handled by blanking the innermost, parsing the
9727    rest by subsequent calls.  */
9728
9729 static int
9730 my_getSmallParser (str, len, nestlevel)
9731      char **str;
9732      unsigned int *len;
9733      int *nestlevel;
9734 {
9735   *len = 0;
9736   *str += strspn (*str, " \t");
9737   /* Check for expression in parentheses.  */
9738   if (**str == '(')
9739     {
9740       char *b = *str + 1 + strspn (*str + 1, " \t");
9741       char *e;
9742
9743       /* Check for base register.  */
9744       if (b[0] == '$')
9745         {
9746           if (strchr (b, ')')
9747               && (e = b + strcspn (b, ") \t"))
9748               && e - b > 1 && e - b < 4)
9749             {
9750               if ((e - b == 3
9751                    && ((b[1] == 'f' && b[2] == 'p')
9752                        || (b[1] == 's' && b[2] == 'p')
9753                        || (b[1] == 'g' && b[2] == 'p')
9754                        || (b[1] == 'a' && b[2] == 't')
9755                        || (ISDIGIT (b[1])
9756                            && ISDIGIT (b[2]))))
9757                   || (ISDIGIT (b[1])))
9758                 {
9759                   *len = strcspn (*str, ")") + 1;
9760                   return S_EX_REGISTER;
9761                 }
9762             }
9763         }
9764       /* Check for percent_op (in parentheses).  */
9765       else if (b[0] == '%')
9766         {
9767           *str = b;
9768           return my_getPercentOp (str, len, nestlevel);
9769         }
9770
9771       /* Some other expression in the parentheses, which can contain
9772          parentheses itself. Attempt to find the matching one.  */
9773       {
9774         int pcnt = 1;
9775         char *s;
9776
9777         *len = 1;
9778         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9779           {
9780             if (*s == '(')
9781               ++pcnt;
9782             else if (*s == ')')
9783               --pcnt;
9784           }
9785       }
9786     }
9787   /* Check for percent_op (outside of parentheses).  */
9788   else if (*str[0] == '%')
9789     return my_getPercentOp (str, len, nestlevel);
9790
9791   /* Any other expression.  */
9792   return S_EX_NONE;
9793 }
9794
9795 static int
9796 my_getPercentOp (str, len, nestlevel)
9797      char **str;
9798      unsigned int *len;
9799      int *nestlevel;
9800 {
9801   char *tmp = *str + 1;
9802   unsigned int i = 0;
9803
9804   while (ISALPHA (*tmp) || *tmp == '_')
9805     {
9806       *tmp = TOLOWER (*tmp);
9807       tmp++;
9808     }
9809   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9810     {
9811       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9812         i++;
9813       else
9814         {
9815           int type = percent_op[i].type;
9816
9817           /* Only %hi and %lo are allowed for OldABI.  */
9818           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9819             return S_EX_NONE;
9820
9821           *len = strlen (percent_op[i].str);
9822           ++(*nestlevel);
9823           return type;
9824         }
9825     }
9826   return S_EX_NONE;
9827 }
9828
9829 static int
9830 my_getSmallExpression (ep, str)
9831      expressionS *ep;
9832      char *str;
9833 {
9834   static char *oldstr = NULL;
9835   int c = S_EX_NONE;
9836   int oldc;
9837   int nestlevel = -1;
9838   unsigned int len;
9839
9840   /* Don't update oldstr if the last call had nested percent_op's. We need
9841      it to parse the outer ones later.  */
9842   if (! oldstr)
9843     oldstr = str;
9844
9845   do
9846     {
9847       oldc = c;
9848       c = my_getSmallParser (&str, &len, &nestlevel);
9849       if (c != S_EX_NONE && c != S_EX_REGISTER)
9850         str += len;
9851     }
9852   while (c != S_EX_NONE && c != S_EX_REGISTER);
9853
9854   if (nestlevel >= 0)
9855     {
9856       /* A percent_op was encountered.  Don't try to get an expression if
9857          it is already blanked out.  */
9858       if (*(str + strspn (str + 1, " )")) != ')')
9859         {
9860           char save;
9861
9862           /* Let my_getExpression() stop at the closing parenthesis.  */
9863           save = *(str + len);
9864           *(str + len) = '\0';
9865           my_getExpression (ep, str);
9866           *(str + len) = save;
9867         }
9868       if (nestlevel > 0)
9869         {
9870           /* Blank out including the % sign and the proper matching
9871              parenthesis.  */
9872           int pcnt = 1;
9873           char *s = strrchr (oldstr, '%');
9874           char *end;
9875
9876           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9877             {
9878               if (*end == '(')
9879                 ++pcnt;
9880               else if (*end == ')')
9881                 --pcnt;
9882             }
9883
9884           memset (s, ' ', end - s);
9885           str = oldstr;
9886         }
9887       else
9888         expr_end = str + len;
9889
9890       c = oldc;
9891     }
9892   else if (c == S_EX_NONE)
9893     {
9894       my_getExpression (ep, str);
9895     }
9896   else if (c == S_EX_REGISTER)
9897     {
9898       ep->X_op = O_constant;
9899       expr_end = str;
9900       ep->X_add_symbol = NULL;
9901       ep->X_op_symbol = NULL;
9902       ep->X_add_number = 0;
9903     }
9904   else
9905     {
9906       as_fatal (_("internal error"));
9907     }
9908
9909   if (nestlevel <= 0)
9910     /* All percent_op's have been handled.  */
9911     oldstr = NULL;
9912
9913   return c;
9914 }
9915
9916 static void
9917 my_getExpression (ep, str)
9918      expressionS *ep;
9919      char *str;
9920 {
9921   char *save_in;
9922   valueT val;
9923
9924   save_in = input_line_pointer;
9925   input_line_pointer = str;
9926   expression (ep);
9927   expr_end = input_line_pointer;
9928   input_line_pointer = save_in;
9929
9930   /* If we are in mips16 mode, and this is an expression based on `.',
9931      then we bump the value of the symbol by 1 since that is how other
9932      text symbols are handled.  We don't bother to handle complex
9933      expressions, just `.' plus or minus a constant.  */
9934   if (mips_opts.mips16
9935       && ep->X_op == O_symbol
9936       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9937       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9938       && symbol_get_frag (ep->X_add_symbol) == frag_now
9939       && symbol_constant_p (ep->X_add_symbol)
9940       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9941     S_SET_VALUE (ep->X_add_symbol, val + 1);
9942 }
9943
9944 /* Turn a string in input_line_pointer into a floating point constant
9945    of type TYPE, and store the appropriate bytes in *LITP.  The number
9946    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9947    returned, or NULL on OK.  */
9948
9949 char *
9950 md_atof (type, litP, sizeP)
9951      int type;
9952      char *litP;
9953      int *sizeP;
9954 {
9955   int prec;
9956   LITTLENUM_TYPE words[4];
9957   char *t;
9958   int i;
9959
9960   switch (type)
9961     {
9962     case 'f':
9963       prec = 2;
9964       break;
9965
9966     case 'd':
9967       prec = 4;
9968       break;
9969
9970     default:
9971       *sizeP = 0;
9972       return _("bad call to md_atof");
9973     }
9974
9975   t = atof_ieee (input_line_pointer, type, words);
9976   if (t)
9977     input_line_pointer = t;
9978
9979   *sizeP = prec * 2;
9980
9981   if (! target_big_endian)
9982     {
9983       for (i = prec - 1; i >= 0; i--)
9984         {
9985           md_number_to_chars (litP, (valueT) words[i], 2);
9986           litP += 2;
9987         }
9988     }
9989   else
9990     {
9991       for (i = 0; i < prec; i++)
9992         {
9993           md_number_to_chars (litP, (valueT) words[i], 2);
9994           litP += 2;
9995         }
9996     }
9997
9998   return NULL;
9999 }
10000
10001 void
10002 md_number_to_chars (buf, val, n)
10003      char *buf;
10004      valueT val;
10005      int n;
10006 {
10007   if (target_big_endian)
10008     number_to_chars_bigendian (buf, val, n);
10009   else
10010     number_to_chars_littleendian (buf, val, n);
10011 }
10012 \f
10013 #ifdef OBJ_ELF
10014 static int support_64bit_objects(void)
10015 {
10016   const char **list, **l;
10017
10018   list = bfd_target_list ();
10019   for (l = list; *l != NULL; l++)
10020 #ifdef TE_TMIPS
10021     /* This is traditional mips */
10022     if (strcmp (*l, "elf64-tradbigmips") == 0
10023         || strcmp (*l, "elf64-tradlittlemips") == 0)
10024 #else
10025     if (strcmp (*l, "elf64-bigmips") == 0
10026         || strcmp (*l, "elf64-littlemips") == 0)
10027 #endif
10028       break;
10029   free (list);
10030   return (*l != NULL);
10031 }
10032 #endif /* OBJ_ELF */
10033
10034 CONST char *md_shortopts = "nO::g::G:";
10035
10036 struct option md_longopts[] =
10037 {
10038 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10039   {"mips0", no_argument, NULL, OPTION_MIPS1},
10040   {"mips1", no_argument, NULL, OPTION_MIPS1},
10041 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10042   {"mips2", no_argument, NULL, OPTION_MIPS2},
10043 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10044   {"mips3", no_argument, NULL, OPTION_MIPS3},
10045 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10046   {"mips4", no_argument, NULL, OPTION_MIPS4},
10047 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10048   {"mips5", no_argument, NULL, OPTION_MIPS5},
10049 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10050   {"mips32", no_argument, NULL, OPTION_MIPS32},
10051 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10052   {"mips64", no_argument, NULL, OPTION_MIPS64},
10053 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10054   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10055 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10056   {"trap", no_argument, NULL, OPTION_TRAP},
10057   {"no-break", no_argument, NULL, OPTION_TRAP},
10058 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10059   {"break", no_argument, NULL, OPTION_BREAK},
10060   {"no-trap", no_argument, NULL, OPTION_BREAK},
10061 #define OPTION_EB (OPTION_MD_BASE + 11)
10062   {"EB", no_argument, NULL, OPTION_EB},
10063 #define OPTION_EL (OPTION_MD_BASE + 12)
10064   {"EL", no_argument, NULL, OPTION_EL},
10065 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10066   {"mips16", no_argument, NULL, OPTION_MIPS16},
10067 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10068   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10069 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10070   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10071 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10072   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10073   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10074 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10075   {"mfp32", no_argument, NULL, OPTION_FP32},
10076 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10077   {"mgp32", no_argument, NULL, OPTION_GP32},
10078 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10079   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10080 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10081   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10082 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10083   {"march", required_argument, NULL, OPTION_MARCH},
10084 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10085   {"mtune", required_argument, NULL, OPTION_MTUNE},
10086 #define OPTION_MCPU (OPTION_MD_BASE + 23)
10087   {"mcpu", required_argument, NULL, OPTION_MCPU},
10088 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10089   {"m4650", no_argument, NULL, OPTION_M4650},
10090 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10091   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10092 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10093   {"m4010", no_argument, NULL, OPTION_M4010},
10094 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10095   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10096 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10097   {"m4100", no_argument, NULL, OPTION_M4100},
10098 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10099   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10100 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10101   {"m3900", no_argument, NULL, OPTION_M3900},
10102 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10103   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10104 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10105   {"mgp64", no_argument, NULL, OPTION_GP64},
10106 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10107   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10108 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10109   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10110 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10111   {"mdmx", no_argument, NULL, OPTION_MDMX},
10112 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10113   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10114 #ifdef OBJ_ELF
10115 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 37)
10116 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10117   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10118   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10119 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10120   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10121 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10122   {"xgot",        no_argument, NULL, OPTION_XGOT},
10123 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10124   {"mabi", required_argument, NULL, OPTION_MABI},
10125 #define OPTION_32          (OPTION_ELF_BASE + 4)
10126   {"32",          no_argument, NULL, OPTION_32},
10127 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10128   {"n32",         no_argument, NULL, OPTION_N32},
10129 #define OPTION_64          (OPTION_ELF_BASE + 6)
10130   {"64",          no_argument, NULL, OPTION_64},
10131 #endif /* OBJ_ELF */
10132   {NULL, no_argument, NULL, 0}
10133 };
10134 size_t md_longopts_size = sizeof (md_longopts);
10135
10136 int
10137 md_parse_option (c, arg)
10138      int c;
10139      char *arg;
10140 {
10141   switch (c)
10142     {
10143     case OPTION_CONSTRUCT_FLOATS:
10144       mips_disable_float_construction = 0;
10145       break;
10146
10147     case OPTION_NO_CONSTRUCT_FLOATS:
10148       mips_disable_float_construction = 1;
10149       break;
10150
10151     case OPTION_TRAP:
10152       mips_trap = 1;
10153       break;
10154
10155     case OPTION_BREAK:
10156       mips_trap = 0;
10157       break;
10158
10159     case OPTION_EB:
10160       target_big_endian = 1;
10161       break;
10162
10163     case OPTION_EL:
10164       target_big_endian = 0;
10165       break;
10166
10167     case 'n':
10168       warn_nops = 1;
10169       break;
10170
10171     case 'O':
10172       if (arg && arg[1] == '0')
10173         mips_optimize = 1;
10174       else
10175         mips_optimize = 2;
10176       break;
10177
10178     case 'g':
10179       if (arg == NULL)
10180         mips_debug = 2;
10181       else
10182         mips_debug = atoi (arg);
10183       /* When the MIPS assembler sees -g or -g2, it does not do
10184          optimizations which limit full symbolic debugging.  We take
10185          that to be equivalent to -O0.  */
10186       if (mips_debug == 2)
10187         mips_optimize = 1;
10188       break;
10189
10190     case OPTION_MIPS1:
10191       mips_opts.isa = ISA_MIPS1;
10192       break;
10193
10194     case OPTION_MIPS2:
10195       mips_opts.isa = ISA_MIPS2;
10196       break;
10197
10198     case OPTION_MIPS3:
10199       mips_opts.isa = ISA_MIPS3;
10200       break;
10201
10202     case OPTION_MIPS4:
10203       mips_opts.isa = ISA_MIPS4;
10204       break;
10205
10206     case OPTION_MIPS5:
10207       mips_opts.isa = ISA_MIPS5;
10208       break;
10209
10210     case OPTION_MIPS32:
10211       mips_opts.isa = ISA_MIPS32;
10212       break;
10213
10214     case OPTION_MIPS64:
10215       mips_opts.isa = ISA_MIPS64;
10216       break;
10217
10218     case OPTION_MTUNE:
10219     case OPTION_MARCH:
10220     case OPTION_MCPU:
10221       {
10222         int cpu = CPU_UNKNOWN;
10223
10224         /* Identify the processor type.  */
10225         if (strcasecmp (arg, "default") != 0)
10226           {
10227             const struct mips_cpu_info *ci;
10228
10229             ci = mips_cpu_info_from_name (arg);
10230             if (ci == NULL || ci->is_isa)
10231               {
10232                 switch (c)
10233                   {
10234                   case OPTION_MTUNE:
10235                     as_fatal (_("invalid architecture -mtune=%s"), arg);
10236                     break;
10237                   case OPTION_MARCH:
10238                     as_fatal (_("invalid architecture -march=%s"), arg);
10239                     break;
10240                   case OPTION_MCPU:
10241                     as_fatal (_("invalid architecture -mcpu=%s"), arg);
10242                     break;
10243                   }
10244               }
10245             else
10246               cpu = ci->cpu;
10247           }
10248
10249         switch (c)
10250           {
10251           case OPTION_MTUNE:
10252             if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
10253               as_warn (_("A different -mtune= was already specified, is now "
10254                          "-mtune=%s"), arg);
10255             mips_tune = cpu;
10256             break;
10257           case OPTION_MARCH:
10258             if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
10259               as_warn (_("A different -march= was already specified, is now "
10260                          "-march=%s"), arg);
10261             mips_arch = cpu;
10262             break;
10263           case OPTION_MCPU:
10264             if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
10265               as_warn (_("A different -mcpu= was already specified, is now "
10266                          "-mcpu=%s"), arg);
10267             mips_cpu = cpu;
10268           }
10269       }
10270       break;
10271
10272     case OPTION_M4650:
10273       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
10274           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
10275         as_warn (_("A different -march= or -mtune= was already specified, "
10276                    "is now -m4650"));
10277       mips_arch = CPU_R4650;
10278       mips_tune = CPU_R4650;
10279       break;
10280
10281     case OPTION_NO_M4650:
10282       break;
10283
10284     case OPTION_M4010:
10285       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
10286           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
10287         as_warn (_("A different -march= or -mtune= was already specified, "
10288                    "is now -m4010"));
10289       mips_arch = CPU_R4010;
10290       mips_tune = CPU_R4010;
10291       break;
10292
10293     case OPTION_NO_M4010:
10294       break;
10295
10296     case OPTION_M4100:
10297       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
10298           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
10299         as_warn (_("A different -march= or -mtune= was already specified, "
10300                    "is now -m4100"));
10301       mips_arch = CPU_VR4100;
10302       mips_tune = CPU_VR4100;
10303       break;
10304
10305     case OPTION_NO_M4100:
10306       break;
10307
10308     case OPTION_M3900:
10309       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
10310           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
10311         as_warn (_("A different -march= or -mtune= was already specified, "
10312                    "is now -m3900"));
10313       mips_arch = CPU_R3900;
10314       mips_tune = CPU_R3900;
10315       break;
10316
10317     case OPTION_NO_M3900:
10318       break;
10319
10320     case OPTION_MDMX:
10321       mips_opts.ase_mdmx = 1;
10322       break;
10323
10324     case OPTION_NO_MDMX:
10325       mips_opts.ase_mdmx = 0;
10326       break;
10327
10328     case OPTION_MIPS16:
10329       mips_opts.mips16 = 1;
10330       mips_no_prev_insn (false);
10331       break;
10332
10333     case OPTION_NO_MIPS16:
10334       mips_opts.mips16 = 0;
10335       mips_no_prev_insn (false);
10336       break;
10337
10338     case OPTION_MIPS3D:
10339       mips_opts.ase_mips3d = 1;
10340       break;
10341
10342     case OPTION_NO_MIPS3D:
10343       mips_opts.ase_mips3d = 0;
10344       break;
10345
10346     case OPTION_MEMBEDDED_PIC:
10347       mips_pic = EMBEDDED_PIC;
10348       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10349         {
10350           as_bad (_("-G may not be used with embedded PIC code"));
10351           return 0;
10352         }
10353       g_switch_value = 0x7fffffff;
10354       break;
10355
10356 #ifdef OBJ_ELF
10357       /* When generating ELF code, we permit -KPIC and -call_shared to
10358          select SVR4_PIC, and -non_shared to select no PIC.  This is
10359          intended to be compatible with Irix 5.  */
10360     case OPTION_CALL_SHARED:
10361       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10362         {
10363           as_bad (_("-call_shared is supported only for ELF format"));
10364           return 0;
10365         }
10366       mips_pic = SVR4_PIC;
10367       if (g_switch_seen && g_switch_value != 0)
10368         {
10369           as_bad (_("-G may not be used with SVR4 PIC code"));
10370           return 0;
10371         }
10372       g_switch_value = 0;
10373       break;
10374
10375     case OPTION_NON_SHARED:
10376       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10377         {
10378           as_bad (_("-non_shared is supported only for ELF format"));
10379           return 0;
10380         }
10381       mips_pic = NO_PIC;
10382       break;
10383
10384       /* The -xgot option tells the assembler to use 32 offsets when
10385          accessing the got in SVR4_PIC mode.  It is for Irix
10386          compatibility.  */
10387     case OPTION_XGOT:
10388       mips_big_got = 1;
10389       break;
10390 #endif /* OBJ_ELF */
10391
10392     case 'G':
10393       if (! USE_GLOBAL_POINTER_OPT)
10394         {
10395           as_bad (_("-G is not supported for this configuration"));
10396           return 0;
10397         }
10398       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10399         {
10400           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10401           return 0;
10402         }
10403       else
10404         g_switch_value = atoi (arg);
10405       g_switch_seen = 1;
10406       break;
10407
10408 #ifdef OBJ_ELF
10409       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10410          and -mabi=64.  */
10411     case OPTION_32:
10412       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10413         {
10414           as_bad (_("-32 is supported for ELF format only"));
10415           return 0;
10416         }
10417       mips_opts.abi = O32_ABI;
10418       break;
10419
10420     case OPTION_N32:
10421       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10422         {
10423           as_bad (_("-n32 is supported for ELF format only"));
10424           return 0;
10425         }
10426       mips_opts.abi = N32_ABI;
10427       break;
10428
10429     case OPTION_64:
10430       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10431         {
10432           as_bad (_("-64 is supported for ELF format only"));
10433           return 0;
10434         }
10435       mips_opts.abi = N64_ABI;
10436       if (! support_64bit_objects())
10437         as_fatal (_("No compiled in support for 64 bit object file format"));
10438       break;
10439 #endif /* OBJ_ELF */
10440
10441     case OPTION_GP32:
10442       file_mips_gp32 = 1;
10443       if (mips_opts.abi != O32_ABI)
10444         mips_opts.abi = NO_ABI;
10445       break;
10446
10447     case OPTION_GP64:
10448       file_mips_gp32 = 0;
10449       if (mips_opts.abi == O32_ABI)
10450         mips_opts.abi = NO_ABI;
10451       break;
10452
10453     case OPTION_FP32:
10454       file_mips_fp32 = 1;
10455       if (mips_opts.abi != O32_ABI)
10456         mips_opts.abi = NO_ABI;
10457       break;
10458
10459 #ifdef OBJ_ELF
10460     case OPTION_MABI:
10461       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10462         {
10463           as_bad (_("-mabi is supported for ELF format only"));
10464           return 0;
10465         }
10466       if (strcmp (arg, "32") == 0)
10467         mips_opts.abi = O32_ABI;
10468       else if (strcmp (arg, "o64") == 0)
10469         mips_opts.abi = O64_ABI;
10470       else if (strcmp (arg, "n32") == 0)
10471         mips_opts.abi = N32_ABI;
10472       else if (strcmp (arg, "64") == 0)
10473         {
10474           mips_opts.abi = N64_ABI;
10475           if (! support_64bit_objects())
10476             as_fatal (_("No compiled in support for 64 bit object file "
10477                         "format"));
10478         }
10479       else if (strcmp (arg, "eabi") == 0)
10480         mips_opts.abi = EABI_ABI;
10481       else
10482         {
10483           as_fatal (_("invalid abi -mabi=%s"), arg);
10484           return 0;
10485         }
10486       break;
10487 #endif /* OBJ_ELF */
10488
10489     case OPTION_M7000_HILO_FIX:
10490       mips_7000_hilo_fix = true;
10491       break;
10492
10493     case OPTION_MNO_7000_HILO_FIX:
10494       mips_7000_hilo_fix = false;
10495       break;
10496
10497     default:
10498       return 0;
10499     }
10500
10501   return 1;
10502 }
10503
10504 static void
10505 show (stream, string, col_p, first_p)
10506      FILE *stream;
10507      char *string;
10508      int *col_p;
10509      int *first_p;
10510 {
10511   if (*first_p)
10512     {
10513       fprintf (stream, "%24s", "");
10514       *col_p = 24;
10515     }
10516   else
10517     {
10518       fprintf (stream, ", ");
10519       *col_p += 2;
10520     }
10521
10522   if (*col_p + strlen (string) > 72)
10523     {
10524       fprintf (stream, "\n%24s", "");
10525       *col_p = 24;
10526     }
10527
10528   fprintf (stream, "%s", string);
10529   *col_p += strlen (string);
10530
10531   *first_p = 0;
10532 }
10533
10534 void
10535 md_show_usage (stream)
10536      FILE *stream;
10537 {
10538   int column, first;
10539
10540   fprintf (stream, _("\
10541 MIPS options:\n\
10542 -membedded-pic          generate embedded position independent code\n\
10543 -EB                     generate big endian output\n\
10544 -EL                     generate little endian output\n\
10545 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
10546 -G NUM                  allow referencing objects up to NUM bytes\n\
10547                         implicitly with the gp register [default 8]\n"));
10548   fprintf (stream, _("\
10549 -mips1                  generate MIPS ISA I instructions\n\
10550 -mips2                  generate MIPS ISA II instructions\n\
10551 -mips3                  generate MIPS ISA III instructions\n\
10552 -mips4                  generate MIPS ISA IV instructions\n\
10553 -mips5                  generate MIPS ISA V instructions\n\
10554 -mips32                 generate MIPS32 ISA instructions\n\
10555 -mips64                 generate MIPS64 ISA instructions\n\
10556 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
10557
10558   first = 1;
10559
10560   show (stream, "2000", &column, &first);
10561   show (stream, "3000", &column, &first);
10562   show (stream, "3900", &column, &first);
10563   show (stream, "4000", &column, &first);
10564   show (stream, "4010", &column, &first);
10565   show (stream, "4100", &column, &first);
10566   show (stream, "4111", &column, &first);
10567   show (stream, "4300", &column, &first);
10568   show (stream, "4400", &column, &first);
10569   show (stream, "4600", &column, &first);
10570   show (stream, "4650", &column, &first);
10571   show (stream, "5000", &column, &first);
10572   show (stream, "5200", &column, &first);
10573   show (stream, "5230", &column, &first);
10574   show (stream, "5231", &column, &first);
10575   show (stream, "5261", &column, &first);
10576   show (stream, "5721", &column, &first);
10577   show (stream, "6000", &column, &first);
10578   show (stream, "8000", &column, &first);
10579   show (stream, "10000", &column, &first);
10580   show (stream, "12000", &column, &first);
10581   show (stream, "sb1", &column, &first);
10582   fputc ('\n', stream);
10583
10584   fprintf (stream, _("\
10585 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10586 -no-mCPU                don't generate code specific to CPU.\n\
10587                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
10588
10589   first = 1;
10590
10591   show (stream, "3900", &column, &first);
10592   show (stream, "4010", &column, &first);
10593   show (stream, "4100", &column, &first);
10594   show (stream, "4650", &column, &first);
10595   fputc ('\n', stream);
10596
10597   fprintf (stream, _("\
10598 -mips16                 generate mips16 instructions\n\
10599 -no-mips16              do not generate mips16 instructions\n"));
10600   fprintf (stream, _("\
10601 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
10602 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
10603 -O0                     remove unneeded NOPs, do not swap branches\n\
10604 -O                      remove unneeded NOPs and swap branches\n\
10605 -n                      warn about NOPs generated from macros\n\
10606 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10607 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
10608 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
10609 #ifdef OBJ_ELF
10610   fprintf (stream, _("\
10611 -KPIC, -call_shared     generate SVR4 position independent code\n\
10612 -non_shared             do not generate position independent code\n\
10613 -xgot                   assume a 32 bit GOT\n\
10614 -mabi=ABI               create ABI conformant object file for:\n"));
10615
10616   first = 1;
10617
10618   show (stream, "32", &column, &first);
10619   show (stream, "o64", &column, &first);
10620   show (stream, "n32", &column, &first);
10621   show (stream, "64", &column, &first);
10622   show (stream, "eabi", &column, &first);
10623
10624   fputc ('\n', stream);
10625
10626   fprintf (stream, _("\
10627 -32                     create o32 ABI object file (default)\n\
10628 -n32                    create n32 ABI object file\n\
10629 -64                     create 64 ABI object file\n"));
10630 #endif
10631 }
10632 \f
10633 void
10634 mips_init_after_args ()
10635 {
10636   /* initialize opcodes */
10637   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10638   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10639 }
10640
10641 long
10642 md_pcrel_from (fixP)
10643      fixS *fixP;
10644 {
10645   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10646       && fixP->fx_addsy != (symbolS *) NULL
10647       && ! S_IS_DEFINED (fixP->fx_addsy))
10648     {
10649       /* This makes a branch to an undefined symbol be a branch to the
10650          current location.  */
10651       if (mips_pic == EMBEDDED_PIC)
10652         return 4;
10653       else
10654         return 1;
10655     }
10656
10657   /* Return the address of the delay slot.  */
10658   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10659 }
10660
10661 /* This is called before the symbol table is processed.  In order to
10662    work with gcc when using mips-tfile, we must keep all local labels.
10663    However, in other cases, we want to discard them.  If we were
10664    called with -g, but we didn't see any debugging information, it may
10665    mean that gcc is smuggling debugging information through to
10666    mips-tfile, in which case we must generate all local labels.  */
10667
10668 void
10669 mips_frob_file_before_adjust ()
10670 {
10671 #ifndef NO_ECOFF_DEBUGGING
10672   if (ECOFF_DEBUGGING
10673       && mips_debug != 0
10674       && ! ecoff_debugging_seen)
10675     flag_keep_locals = 1;
10676 #endif
10677 }
10678
10679 /* Sort any unmatched HI16_S relocs so that they immediately precede
10680    the corresponding LO reloc.  This is called before md_apply_fix3 and
10681    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10682    explicit use of the %hi modifier.  */
10683
10684 void
10685 mips_frob_file ()
10686 {
10687   struct mips_hi_fixup *l;
10688
10689   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10690     {
10691       segment_info_type *seginfo;
10692       int pass;
10693
10694       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10695
10696       /* Check quickly whether the next fixup happens to be a matching
10697          %lo.  */
10698       if (l->fixp->fx_next != NULL
10699           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10700           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10701           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10702         continue;
10703
10704       /* Look through the fixups for this segment for a matching %lo.
10705          When we find one, move the %hi just in front of it.  We do
10706          this in two passes.  In the first pass, we try to find a
10707          unique %lo.  In the second pass, we permit multiple %hi
10708          relocs for a single %lo (this is a GNU extension).  */
10709       seginfo = seg_info (l->seg);
10710       for (pass = 0; pass < 2; pass++)
10711         {
10712           fixS *f, *prev;
10713
10714           prev = NULL;
10715           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10716             {
10717               /* Check whether this is a %lo fixup which matches l->fixp.  */
10718               if (f->fx_r_type == BFD_RELOC_LO16
10719                   && f->fx_addsy == l->fixp->fx_addsy
10720                   && f->fx_offset == l->fixp->fx_offset
10721                   && (pass == 1
10722                       || prev == NULL
10723                       || prev->fx_r_type != BFD_RELOC_HI16_S
10724                       || prev->fx_addsy != f->fx_addsy
10725                       || prev->fx_offset !=  f->fx_offset))
10726                 {
10727                   fixS **pf;
10728
10729                   /* Move l->fixp before f.  */
10730                   for (pf = &seginfo->fix_root;
10731                        *pf != l->fixp;
10732                        pf = &(*pf)->fx_next)
10733                     assert (*pf != NULL);
10734
10735                   *pf = l->fixp->fx_next;
10736
10737                   l->fixp->fx_next = f;
10738                   if (prev == NULL)
10739                     seginfo->fix_root = l->fixp;
10740                   else
10741                     prev->fx_next = l->fixp;
10742
10743                   break;
10744                 }
10745
10746               prev = f;
10747             }
10748
10749           if (f != NULL)
10750             break;
10751
10752 #if 0 /* GCC code motion plus incomplete dead code elimination
10753          can leave a %hi without a %lo.  */
10754           if (pass == 1)
10755             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10756                            _("Unmatched %%hi reloc"));
10757 #endif
10758         }
10759     }
10760 }
10761
10762 /* When generating embedded PIC code we need to use a special
10763    relocation to represent the difference of two symbols in the .text
10764    section (switch tables use a difference of this sort).  See
10765    include/coff/mips.h for details.  This macro checks whether this
10766    fixup requires the special reloc.  */
10767 #define SWITCH_TABLE(fixp) \
10768   ((fixp)->fx_r_type == BFD_RELOC_32 \
10769    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10770    && (fixp)->fx_addsy != NULL \
10771    && (fixp)->fx_subsy != NULL \
10772    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10773    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10774
10775 /* When generating embedded PIC code we must keep all PC relative
10776    relocations, in case the linker has to relax a call.  We also need
10777    to keep relocations for switch table entries.
10778
10779    We may have combined relocations without symbols in the N32/N64 ABI.
10780    We have to prevent gas from dropping them.  */
10781
10782 int
10783 mips_force_relocation (fixp)
10784      fixS *fixp;
10785 {
10786   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10787       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10788     return 1;
10789
10790   if (HAVE_NEWABI
10791       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10792       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10793           || fixp->fx_r_type == BFD_RELOC_HI16_S
10794           || fixp->fx_r_type == BFD_RELOC_LO16))
10795     return 1;
10796
10797   return (mips_pic == EMBEDDED_PIC
10798           && (fixp->fx_pcrel
10799               || SWITCH_TABLE (fixp)
10800               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10801               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10802 }
10803
10804 #ifdef OBJ_ELF
10805 static int
10806 mips_need_elf_addend_fixup (fixP)
10807      fixS *fixP;
10808 {
10809   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10810     return 1;
10811   if (mips_pic == EMBEDDED_PIC
10812       && S_IS_WEAK (fixP->fx_addsy))
10813     return 1;
10814   if (mips_pic != EMBEDDED_PIC
10815       && (S_IS_WEAK (fixP->fx_addsy)
10816           || S_IS_EXTERN (fixP->fx_addsy))
10817       && !S_IS_COMMON (fixP->fx_addsy))
10818     return 1;
10819   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10820       && (((bfd_get_section_flags (stdoutput,
10821                                    S_GET_SEGMENT (fixP->fx_addsy))
10822             & SEC_LINK_ONCE) != 0)
10823           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10824                        ".gnu.linkonce",
10825                        sizeof (".gnu.linkonce") - 1)))
10826     return 1;
10827   return 0;
10828 }
10829 #endif
10830
10831 /* Apply a fixup to the object file.  */
10832
10833 void
10834 md_apply_fix3 (fixP, valP, seg)
10835      fixS *fixP;
10836      valueT *valP;
10837      segT seg ATTRIBUTE_UNUSED;
10838 {
10839   bfd_byte *buf;
10840   long insn;
10841   valueT value;
10842
10843   assert (fixP->fx_size == 4
10844           || fixP->fx_r_type == BFD_RELOC_16
10845           || fixP->fx_r_type == BFD_RELOC_32
10846           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10847           || fixP->fx_r_type == BFD_RELOC_HI16_S
10848           || fixP->fx_r_type == BFD_RELOC_LO16
10849           || fixP->fx_r_type == BFD_RELOC_GPREL16
10850           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10851           || fixP->fx_r_type == BFD_RELOC_GPREL32
10852           || fixP->fx_r_type == BFD_RELOC_64
10853           || fixP->fx_r_type == BFD_RELOC_CTOR
10854           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10855           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10856           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10857           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10858           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10859           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10860           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10861           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10862           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10863
10864   value = *valP;
10865
10866   /* If we aren't adjusting this fixup to be against the section
10867      symbol, we need to adjust the value.  */
10868 #ifdef OBJ_ELF
10869   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10870     {
10871       if (mips_need_elf_addend_fixup (fixP))
10872         {
10873           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10874
10875           value -= symval;
10876           if (value != 0 && ! fixP->fx_pcrel)
10877             {
10878               /* In this case, the bfd_install_relocation routine will
10879                  incorrectly add the symbol value back in.  We just want
10880                  the addend to appear in the object file.  */
10881               value -= symval;
10882
10883               /* Make sure the addend is still non-zero.  If it became zero
10884                  after the last operation, set it to a spurious value and
10885                  subtract the same value from the object file's contents.  */
10886               if (value == 0)
10887                 {
10888                   value = 8;
10889
10890                   /* The in-place addends for LO16 relocations are signed;
10891                      leave the matching HI16 in-place addends as zero.  */
10892                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10893                     {
10894                       reloc_howto_type *howto;
10895                       bfd_vma contents, mask, field;
10896
10897                       howto = bfd_reloc_type_lookup (stdoutput,
10898                                                      fixP->fx_r_type);
10899
10900                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10901                                                + fixP->fx_where,
10902                                                fixP->fx_size * 8,
10903                                                target_big_endian);
10904
10905                       /* MASK has bits set where the relocation should go.
10906                          FIELD is -value, shifted into the appropriate place
10907                          for this relocation.  */
10908                       mask = 1 << (howto->bitsize - 1);
10909                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10910                       field = (-value >> howto->rightshift) << howto->bitpos;
10911
10912                       bfd_put_bits ((field & mask) | (contents & ~mask),
10913                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10914                                     fixP->fx_size * 8,
10915                                     target_big_endian);
10916                     }
10917                 }
10918             }
10919         }
10920
10921       /* This code was generated using trial and error and so is
10922          fragile and not trustworthy.  If you change it, you should
10923          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10924          they still pass.  */
10925       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10926         {
10927           value += fixP->fx_frag->fr_address + fixP->fx_where;
10928
10929           /* BFD's REL handling, for MIPS, is _very_ weird.
10930              This gives the right results, but it can't possibly
10931              be the way things are supposed to work.  */
10932           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10933                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10934               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10935             value += fixP->fx_frag->fr_address + fixP->fx_where;
10936         }
10937     }
10938 #endif
10939
10940   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10941
10942   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10943     fixP->fx_done = 1;
10944
10945   switch (fixP->fx_r_type)
10946     {
10947     case BFD_RELOC_MIPS_JMP:
10948     case BFD_RELOC_MIPS_SHIFT5:
10949     case BFD_RELOC_MIPS_SHIFT6:
10950     case BFD_RELOC_MIPS_GOT_DISP:
10951     case BFD_RELOC_MIPS_GOT_PAGE:
10952     case BFD_RELOC_MIPS_GOT_OFST:
10953     case BFD_RELOC_MIPS_SUB:
10954     case BFD_RELOC_MIPS_INSERT_A:
10955     case BFD_RELOC_MIPS_INSERT_B:
10956     case BFD_RELOC_MIPS_DELETE:
10957     case BFD_RELOC_MIPS_HIGHEST:
10958     case BFD_RELOC_MIPS_HIGHER:
10959     case BFD_RELOC_MIPS_SCN_DISP:
10960     case BFD_RELOC_MIPS_REL16:
10961     case BFD_RELOC_MIPS_RELGOT:
10962     case BFD_RELOC_MIPS_JALR:
10963     case BFD_RELOC_HI16:
10964     case BFD_RELOC_HI16_S:
10965     case BFD_RELOC_GPREL16:
10966     case BFD_RELOC_MIPS_LITERAL:
10967     case BFD_RELOC_MIPS_CALL16:
10968     case BFD_RELOC_MIPS_GOT16:
10969     case BFD_RELOC_GPREL32:
10970     case BFD_RELOC_MIPS_GOT_HI16:
10971     case BFD_RELOC_MIPS_GOT_LO16:
10972     case BFD_RELOC_MIPS_CALL_HI16:
10973     case BFD_RELOC_MIPS_CALL_LO16:
10974     case BFD_RELOC_MIPS16_GPREL:
10975       if (fixP->fx_pcrel)
10976         as_bad_where (fixP->fx_file, fixP->fx_line,
10977                       _("Invalid PC relative reloc"));
10978       /* Nothing needed to do. The value comes from the reloc entry */
10979       break;
10980
10981     case BFD_RELOC_MIPS16_JMP:
10982       /* We currently always generate a reloc against a symbol, which
10983          means that we don't want an addend even if the symbol is
10984          defined.  */
10985       fixP->fx_addnumber = 0;
10986       break;
10987
10988     case BFD_RELOC_PCREL_HI16_S:
10989       /* The addend for this is tricky if it is internal, so we just
10990          do everything here rather than in bfd_install_relocation.  */
10991       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10992           && !fixP->fx_done
10993           && value != 0)
10994         break;
10995       if (fixP->fx_addsy
10996           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10997         {
10998           /* For an external symbol adjust by the address to make it
10999              pcrel_offset.  We use the address of the RELLO reloc
11000              which follows this one.  */
11001           value += (fixP->fx_next->fx_frag->fr_address
11002                     + fixP->fx_next->fx_where);
11003         }
11004       value = ((value + 0x8000) >> 16) & 0xffff;
11005       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11006       if (target_big_endian)
11007         buf += 2;
11008       md_number_to_chars ((char *) buf, value, 2);
11009       break;
11010
11011     case BFD_RELOC_PCREL_LO16:
11012       /* The addend for this is tricky if it is internal, so we just
11013          do everything here rather than in bfd_install_relocation.  */
11014       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11015           && !fixP->fx_done
11016           && value != 0)
11017         break;
11018       if (fixP->fx_addsy
11019           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11020         value += fixP->fx_frag->fr_address + fixP->fx_where;
11021       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11022       if (target_big_endian)
11023         buf += 2;
11024       md_number_to_chars ((char *) buf, value, 2);
11025       break;
11026
11027     case BFD_RELOC_64:
11028       /* This is handled like BFD_RELOC_32, but we output a sign
11029          extended value if we are only 32 bits.  */
11030       if (fixP->fx_done
11031           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11032         {
11033           if (8 <= sizeof (valueT))
11034             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11035                                 value, 8);
11036           else
11037             {
11038               long w1, w2;
11039               long hiv;
11040
11041               w1 = w2 = fixP->fx_where;
11042               if (target_big_endian)
11043                 w1 += 4;
11044               else
11045                 w2 += 4;
11046               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11047               if ((value & 0x80000000) != 0)
11048                 hiv = 0xffffffff;
11049               else
11050                 hiv = 0;
11051               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11052             }
11053         }
11054       break;
11055
11056     case BFD_RELOC_RVA:
11057     case BFD_RELOC_32:
11058       /* If we are deleting this reloc entry, we must fill in the
11059          value now.  This can happen if we have a .word which is not
11060          resolved when it appears but is later defined.  We also need
11061          to fill in the value if this is an embedded PIC switch table
11062          entry.  */
11063       if (fixP->fx_done
11064           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11065         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11066                             value, 4);
11067       break;
11068
11069     case BFD_RELOC_16:
11070       /* If we are deleting this reloc entry, we must fill in the
11071          value now.  */
11072       assert (fixP->fx_size == 2);
11073       if (fixP->fx_done)
11074         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11075                             value, 2);
11076       break;
11077
11078     case BFD_RELOC_LO16:
11079       /* When handling an embedded PIC switch statement, we can wind
11080          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11081       if (fixP->fx_done)
11082         {
11083           if (value + 0x8000 > 0xffff)
11084             as_bad_where (fixP->fx_file, fixP->fx_line,
11085                           _("relocation overflow"));
11086           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11087           if (target_big_endian)
11088             buf += 2;
11089           md_number_to_chars ((char *) buf, value, 2);
11090         }
11091       break;
11092
11093     case BFD_RELOC_16_PCREL_S2:
11094       if ((value & 0x3) != 0)
11095         as_bad_where (fixP->fx_file, fixP->fx_line,
11096                       _("Branch to odd address (%lx)"), (long) value);
11097
11098       /* Fall through.  */
11099
11100     case BFD_RELOC_16_PCREL:
11101       /*
11102        * We need to save the bits in the instruction since fixup_segment()
11103        * might be deleting the relocation entry (i.e., a branch within
11104        * the current segment).
11105        */
11106       if (!fixP->fx_done && value != 0)
11107         break;
11108       /* If 'value' is zero, the remaining reloc code won't actually
11109          do the store, so it must be done here.  This is probably
11110          a bug somewhere.  */
11111       if (!fixP->fx_done
11112           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11113               || fixP->fx_addsy == NULL                 /* ??? */
11114               || ! S_IS_DEFINED (fixP->fx_addsy)))
11115         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11116
11117       value = (offsetT) value >> 2;
11118
11119       /* update old instruction data */
11120       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11121       if (target_big_endian)
11122         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11123       else
11124         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11125
11126       if (value + 0x8000 <= 0xffff)
11127         insn |= value & 0xffff;
11128       else
11129         {
11130           /* The branch offset is too large.  If this is an
11131              unconditional branch, and we are not generating PIC code,
11132              we can convert it to an absolute jump instruction.  */
11133           if (mips_pic == NO_PIC
11134               && fixP->fx_done
11135               && fixP->fx_frag->fr_address >= text_section->vma
11136               && (fixP->fx_frag->fr_address
11137                   < text_section->vma + text_section->_raw_size)
11138               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11139                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11140                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11141             {
11142               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11143                 insn = 0x0c000000;      /* jal */
11144               else
11145                 insn = 0x08000000;      /* j */
11146               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11147               fixP->fx_done = 0;
11148               fixP->fx_addsy = section_symbol (text_section);
11149               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11150             }
11151           else
11152             {
11153               /* FIXME.  It would be possible in principle to handle
11154                  conditional branches which overflow.  They could be
11155                  transformed into a branch around a jump.  This would
11156                  require setting up variant frags for each different
11157                  branch type.  The native MIPS assembler attempts to
11158                  handle these cases, but it appears to do it
11159                  incorrectly.  */
11160               as_bad_where (fixP->fx_file, fixP->fx_line,
11161                             _("Branch out of range"));
11162             }
11163         }
11164
11165       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11166       break;
11167
11168     case BFD_RELOC_VTABLE_INHERIT:
11169       fixP->fx_done = 0;
11170       if (fixP->fx_addsy
11171           && !S_IS_DEFINED (fixP->fx_addsy)
11172           && !S_IS_WEAK (fixP->fx_addsy))
11173         S_SET_WEAK (fixP->fx_addsy);
11174       break;
11175
11176     case BFD_RELOC_VTABLE_ENTRY:
11177       fixP->fx_done = 0;
11178       break;
11179
11180     default:
11181       internalError ();
11182     }
11183 }
11184
11185 #if 0
11186 void
11187 printInsn (oc)
11188      unsigned long oc;
11189 {
11190   const struct mips_opcode *p;
11191   int treg, sreg, dreg, shamt;
11192   short imm;
11193   const char *args;
11194   int i;
11195
11196   for (i = 0; i < NUMOPCODES; ++i)
11197     {
11198       p = &mips_opcodes[i];
11199       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11200         {
11201           printf ("%08lx %s\t", oc, p->name);
11202           treg = (oc >> 16) & 0x1f;
11203           sreg = (oc >> 21) & 0x1f;
11204           dreg = (oc >> 11) & 0x1f;
11205           shamt = (oc >> 6) & 0x1f;
11206           imm = oc;
11207           for (args = p->args;; ++args)
11208             {
11209               switch (*args)
11210                 {
11211                 case '\0':
11212                   printf ("\n");
11213                   break;
11214
11215                 case ',':
11216                 case '(':
11217                 case ')':
11218                   printf ("%c", *args);
11219                   continue;
11220
11221                 case 'r':
11222                   assert (treg == sreg);
11223                   printf ("$%d,$%d", treg, sreg);
11224                   continue;
11225
11226                 case 'd':
11227                 case 'G':
11228                   printf ("$%d", dreg);
11229                   continue;
11230
11231                 case 't':
11232                 case 'E':
11233                   printf ("$%d", treg);
11234                   continue;
11235
11236                 case 'k':
11237                   printf ("0x%x", treg);
11238                   continue;
11239
11240                 case 'b':
11241                 case 's':
11242                   printf ("$%d", sreg);
11243                   continue;
11244
11245                 case 'a':
11246                   printf ("0x%08lx", oc & 0x1ffffff);
11247                   continue;
11248
11249                 case 'i':
11250                 case 'j':
11251                 case 'o':
11252                 case 'u':
11253                   printf ("%d", imm);
11254                   continue;
11255
11256                 case '<':
11257                 case '>':
11258                   printf ("$%d", shamt);
11259                   continue;
11260
11261                 default:
11262                   internalError ();
11263                 }
11264               break;
11265             }
11266           return;
11267         }
11268     }
11269   printf (_("%08lx  UNDEFINED\n"), oc);
11270 }
11271 #endif
11272
11273 static symbolS *
11274 get_symbol ()
11275 {
11276   int c;
11277   char *name;
11278   symbolS *p;
11279
11280   name = input_line_pointer;
11281   c = get_symbol_end ();
11282   p = (symbolS *) symbol_find_or_make (name);
11283   *input_line_pointer = c;
11284   return p;
11285 }
11286
11287 /* Align the current frag to a given power of two.  The MIPS assembler
11288    also automatically adjusts any preceding label.  */
11289
11290 static void
11291 mips_align (to, fill, label)
11292      int to;
11293      int fill;
11294      symbolS *label;
11295 {
11296   mips_emit_delays (false);
11297   frag_align (to, fill, 0);
11298   record_alignment (now_seg, to);
11299   if (label != NULL)
11300     {
11301       assert (S_GET_SEGMENT (label) == now_seg);
11302       symbol_set_frag (label, frag_now);
11303       S_SET_VALUE (label, (valueT) frag_now_fix ());
11304     }
11305 }
11306
11307 /* Align to a given power of two.  .align 0 turns off the automatic
11308    alignment used by the data creating pseudo-ops.  */
11309
11310 static void
11311 s_align (x)
11312      int x ATTRIBUTE_UNUSED;
11313 {
11314   register int temp;
11315   register long temp_fill;
11316   long max_alignment = 15;
11317
11318   /*
11319
11320     o  Note that the assembler pulls down any immediately preceeding label
11321        to the aligned address.
11322     o  It's not documented but auto alignment is reinstated by
11323        a .align pseudo instruction.
11324     o  Note also that after auto alignment is turned off the mips assembler
11325        issues an error on attempt to assemble an improperly aligned data item.
11326        We don't.
11327
11328     */
11329
11330   temp = get_absolute_expression ();
11331   if (temp > max_alignment)
11332     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11333   else if (temp < 0)
11334     {
11335       as_warn (_("Alignment negative: 0 assumed."));
11336       temp = 0;
11337     }
11338   if (*input_line_pointer == ',')
11339     {
11340       ++input_line_pointer;
11341       temp_fill = get_absolute_expression ();
11342     }
11343   else
11344     temp_fill = 0;
11345   if (temp)
11346     {
11347       auto_align = 1;
11348       mips_align (temp, (int) temp_fill,
11349                   insn_labels != NULL ? insn_labels->label : NULL);
11350     }
11351   else
11352     {
11353       auto_align = 0;
11354     }
11355
11356   demand_empty_rest_of_line ();
11357 }
11358
11359 void
11360 mips_flush_pending_output ()
11361 {
11362   mips_emit_delays (false);
11363   mips_clear_insn_labels ();
11364 }
11365
11366 static void
11367 s_change_sec (sec)
11368      int sec;
11369 {
11370   segT seg;
11371
11372   /* When generating embedded PIC code, we only use the .text, .lit8,
11373      .sdata and .sbss sections.  We change the .data and .rdata
11374      pseudo-ops to use .sdata.  */
11375   if (mips_pic == EMBEDDED_PIC
11376       && (sec == 'd' || sec == 'r'))
11377     sec = 's';
11378
11379 #ifdef OBJ_ELF
11380   /* The ELF backend needs to know that we are changing sections, so
11381      that .previous works correctly.  We could do something like check
11382      for an obj_section_change_hook macro, but that might be confusing
11383      as it would not be appropriate to use it in the section changing
11384      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11385      This should be cleaner, somehow.  */
11386   obj_elf_section_change_hook ();
11387 #endif
11388
11389   mips_emit_delays (false);
11390   switch (sec)
11391     {
11392     case 't':
11393       s_text (0);
11394       break;
11395     case 'd':
11396       s_data (0);
11397       break;
11398     case 'b':
11399       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11400       demand_empty_rest_of_line ();
11401       break;
11402
11403     case 'r':
11404       if (USE_GLOBAL_POINTER_OPT)
11405         {
11406           seg = subseg_new (RDATA_SECTION_NAME,
11407                             (subsegT) get_absolute_expression ());
11408           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11409             {
11410               bfd_set_section_flags (stdoutput, seg,
11411                                      (SEC_ALLOC
11412                                       | SEC_LOAD
11413                                       | SEC_READONLY
11414                                       | SEC_RELOC
11415                                       | SEC_DATA));
11416               if (strcmp (TARGET_OS, "elf") != 0)
11417                 record_alignment (seg, 4);
11418             }
11419           demand_empty_rest_of_line ();
11420         }
11421       else
11422         {
11423           as_bad (_("No read only data section in this object file format"));
11424           demand_empty_rest_of_line ();
11425           return;
11426         }
11427       break;
11428
11429     case 's':
11430       if (USE_GLOBAL_POINTER_OPT)
11431         {
11432           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11433           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11434             {
11435               bfd_set_section_flags (stdoutput, seg,
11436                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11437                                      | SEC_DATA);
11438               if (strcmp (TARGET_OS, "elf") != 0)
11439                 record_alignment (seg, 4);
11440             }
11441           demand_empty_rest_of_line ();
11442           break;
11443         }
11444       else
11445         {
11446           as_bad (_("Global pointers not supported; recompile -G 0"));
11447           demand_empty_rest_of_line ();
11448           return;
11449         }
11450     }
11451
11452   auto_align = 1;
11453 }
11454
11455 void
11456 mips_enable_auto_align ()
11457 {
11458   auto_align = 1;
11459 }
11460
11461 static void
11462 s_cons (log_size)
11463      int log_size;
11464 {
11465   symbolS *label;
11466
11467   label = insn_labels != NULL ? insn_labels->label : NULL;
11468   mips_emit_delays (false);
11469   if (log_size > 0 && auto_align)
11470     mips_align (log_size, 0, label);
11471   mips_clear_insn_labels ();
11472   cons (1 << log_size);
11473 }
11474
11475 static void
11476 s_float_cons (type)
11477      int type;
11478 {
11479   symbolS *label;
11480
11481   label = insn_labels != NULL ? insn_labels->label : NULL;
11482
11483   mips_emit_delays (false);
11484
11485   if (auto_align)
11486     {
11487       if (type == 'd')
11488         mips_align (3, 0, label);
11489       else
11490         mips_align (2, 0, label);
11491     }
11492
11493   mips_clear_insn_labels ();
11494
11495   float_cons (type);
11496 }
11497
11498 /* Handle .globl.  We need to override it because on Irix 5 you are
11499    permitted to say
11500        .globl foo .text
11501    where foo is an undefined symbol, to mean that foo should be
11502    considered to be the address of a function.  */
11503
11504 static void
11505 s_mips_globl (x)
11506      int x ATTRIBUTE_UNUSED;
11507 {
11508   char *name;
11509   int c;
11510   symbolS *symbolP;
11511   flagword flag;
11512
11513   name = input_line_pointer;
11514   c = get_symbol_end ();
11515   symbolP = symbol_find_or_make (name);
11516   *input_line_pointer = c;
11517   SKIP_WHITESPACE ();
11518
11519   /* On Irix 5, every global symbol that is not explicitly labelled as
11520      being a function is apparently labelled as being an object.  */
11521   flag = BSF_OBJECT;
11522
11523   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11524     {
11525       char *secname;
11526       asection *sec;
11527
11528       secname = input_line_pointer;
11529       c = get_symbol_end ();
11530       sec = bfd_get_section_by_name (stdoutput, secname);
11531       if (sec == NULL)
11532         as_bad (_("%s: no such section"), secname);
11533       *input_line_pointer = c;
11534
11535       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11536         flag = BSF_FUNCTION;
11537     }
11538
11539   symbol_get_bfdsym (symbolP)->flags |= flag;
11540
11541   S_SET_EXTERNAL (symbolP);
11542   demand_empty_rest_of_line ();
11543 }
11544
11545 static void
11546 s_option (x)
11547      int x ATTRIBUTE_UNUSED;
11548 {
11549   char *opt;
11550   char c;
11551
11552   opt = input_line_pointer;
11553   c = get_symbol_end ();
11554
11555   if (*opt == 'O')
11556     {
11557       /* FIXME: What does this mean?  */
11558     }
11559   else if (strncmp (opt, "pic", 3) == 0)
11560     {
11561       int i;
11562
11563       i = atoi (opt + 3);
11564       if (i == 0)
11565         mips_pic = NO_PIC;
11566       else if (i == 2)
11567         mips_pic = SVR4_PIC;
11568       else
11569         as_bad (_(".option pic%d not supported"), i);
11570
11571       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11572         {
11573           if (g_switch_seen && g_switch_value != 0)
11574             as_warn (_("-G may not be used with SVR4 PIC code"));
11575           g_switch_value = 0;
11576           bfd_set_gp_size (stdoutput, 0);
11577         }
11578     }
11579   else
11580     as_warn (_("Unrecognized option \"%s\""), opt);
11581
11582   *input_line_pointer = c;
11583   demand_empty_rest_of_line ();
11584 }
11585
11586 /* This structure is used to hold a stack of .set values.  */
11587
11588 struct mips_option_stack
11589 {
11590   struct mips_option_stack *next;
11591   struct mips_set_options options;
11592 };
11593
11594 static struct mips_option_stack *mips_opts_stack;
11595
11596 /* Handle the .set pseudo-op.  */
11597
11598 static void
11599 s_mipsset (x)
11600      int x ATTRIBUTE_UNUSED;
11601 {
11602   char *name = input_line_pointer, ch;
11603
11604   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11605     ++input_line_pointer;
11606   ch = *input_line_pointer;
11607   *input_line_pointer = '\0';
11608
11609   if (strcmp (name, "reorder") == 0)
11610     {
11611       if (mips_opts.noreorder && prev_nop_frag != NULL)
11612         {
11613           /* If we still have pending nops, we can discard them.  The
11614              usual nop handling will insert any that are still
11615              needed.  */
11616           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11617                                     * (mips_opts.mips16 ? 2 : 4));
11618           prev_nop_frag = NULL;
11619         }
11620       mips_opts.noreorder = 0;
11621     }
11622   else if (strcmp (name, "noreorder") == 0)
11623     {
11624       mips_emit_delays (true);
11625       mips_opts.noreorder = 1;
11626       mips_any_noreorder = 1;
11627     }
11628   else if (strcmp (name, "at") == 0)
11629     {
11630       mips_opts.noat = 0;
11631     }
11632   else if (strcmp (name, "noat") == 0)
11633     {
11634       mips_opts.noat = 1;
11635     }
11636   else if (strcmp (name, "macro") == 0)
11637     {
11638       mips_opts.warn_about_macros = 0;
11639     }
11640   else if (strcmp (name, "nomacro") == 0)
11641     {
11642       if (mips_opts.noreorder == 0)
11643         as_bad (_("`noreorder' must be set before `nomacro'"));
11644       mips_opts.warn_about_macros = 1;
11645     }
11646   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11647     {
11648       mips_opts.nomove = 0;
11649     }
11650   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11651     {
11652       mips_opts.nomove = 1;
11653     }
11654   else if (strcmp (name, "bopt") == 0)
11655     {
11656       mips_opts.nobopt = 0;
11657     }
11658   else if (strcmp (name, "nobopt") == 0)
11659     {
11660       mips_opts.nobopt = 1;
11661     }
11662   else if (strcmp (name, "mdmx") == 0)
11663     mips_opts.ase_mdmx = 1;
11664   else if (strcmp (name, "nomdmx") == 0)
11665     mips_opts.ase_mdmx = 0;
11666   else if (strcmp (name, "mips16") == 0
11667            || strcmp (name, "MIPS-16") == 0)
11668     mips_opts.mips16 = 1;
11669   else if (strcmp (name, "nomips16") == 0
11670            || strcmp (name, "noMIPS-16") == 0)
11671     mips_opts.mips16 = 0;
11672   else if (strcmp (name, "mips3d") == 0)
11673     mips_opts.ase_mips3d = 1;
11674   else if (strcmp (name, "nomips3d") == 0)
11675     mips_opts.ase_mips3d = 0;
11676   else if (strncmp (name, "mips", 4) == 0)
11677     {
11678       int isa;
11679
11680       /* Permit the user to change the ISA on the fly.  Needless to
11681          say, misuse can cause serious problems.  */
11682       isa = atoi (name + 4);
11683       switch (isa)
11684         {
11685         case  0:
11686           mips_opts.gp32 = file_mips_gp32;
11687           mips_opts.fp32 = file_mips_fp32;
11688           mips_opts.abi = file_mips_abi;
11689           break;
11690         case  1:
11691         case  2:
11692         case 32:
11693           mips_opts.gp32 = 1;
11694           mips_opts.fp32 = 1;
11695           break;
11696         case  3:
11697         case  4:
11698         case  5:
11699         case 64:
11700           /* Loosen ABI register width restriction.  */
11701           if (mips_opts.abi == O32_ABI)
11702             mips_opts.abi = NO_ABI;
11703           mips_opts.gp32 = 0;
11704           mips_opts.fp32 = 0;
11705           break;
11706         default:
11707           as_bad (_("unknown ISA level %s"), name + 4);
11708           break;
11709         }
11710
11711       switch (isa)
11712         {
11713         case  0: mips_opts.isa = file_mips_isa;   break;
11714         case  1: mips_opts.isa = ISA_MIPS1;       break;
11715         case  2: mips_opts.isa = ISA_MIPS2;       break;
11716         case  3: mips_opts.isa = ISA_MIPS3;       break;
11717         case  4: mips_opts.isa = ISA_MIPS4;       break;
11718         case  5: mips_opts.isa = ISA_MIPS5;       break;
11719         case 32: mips_opts.isa = ISA_MIPS32;      break;
11720         case 64: mips_opts.isa = ISA_MIPS64;      break;
11721         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11722         }
11723     }
11724   else if (strcmp (name, "autoextend") == 0)
11725     mips_opts.noautoextend = 0;
11726   else if (strcmp (name, "noautoextend") == 0)
11727     mips_opts.noautoextend = 1;
11728   else if (strcmp (name, "push") == 0)
11729     {
11730       struct mips_option_stack *s;
11731
11732       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11733       s->next = mips_opts_stack;
11734       s->options = mips_opts;
11735       mips_opts_stack = s;
11736     }
11737   else if (strcmp (name, "pop") == 0)
11738     {
11739       struct mips_option_stack *s;
11740
11741       s = mips_opts_stack;
11742       if (s == NULL)
11743         as_bad (_(".set pop with no .set push"));
11744       else
11745         {
11746           /* If we're changing the reorder mode we need to handle
11747              delay slots correctly.  */
11748           if (s->options.noreorder && ! mips_opts.noreorder)
11749             mips_emit_delays (true);
11750           else if (! s->options.noreorder && mips_opts.noreorder)
11751             {
11752               if (prev_nop_frag != NULL)
11753                 {
11754                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11755                                             * (mips_opts.mips16 ? 2 : 4));
11756                   prev_nop_frag = NULL;
11757                 }
11758             }
11759
11760           mips_opts = s->options;
11761           mips_opts_stack = s->next;
11762           free (s);
11763         }
11764     }
11765   else
11766     {
11767       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11768     }
11769   *input_line_pointer = ch;
11770   demand_empty_rest_of_line ();
11771 }
11772
11773 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11774    .option pic2.  It means to generate SVR4 PIC calls.  */
11775
11776 static void
11777 s_abicalls (ignore)
11778      int ignore ATTRIBUTE_UNUSED;
11779 {
11780   mips_pic = SVR4_PIC;
11781   if (USE_GLOBAL_POINTER_OPT)
11782     {
11783       if (g_switch_seen && g_switch_value != 0)
11784         as_warn (_("-G may not be used with SVR4 PIC code"));
11785       g_switch_value = 0;
11786     }
11787   bfd_set_gp_size (stdoutput, 0);
11788   demand_empty_rest_of_line ();
11789 }
11790
11791 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11792    PIC code.  It sets the $gp register for the function based on the
11793    function address, which is in the register named in the argument.
11794    This uses a relocation against _gp_disp, which is handled specially
11795    by the linker.  The result is:
11796         lui     $gp,%hi(_gp_disp)
11797         addiu   $gp,$gp,%lo(_gp_disp)
11798         addu    $gp,$gp,.cpload argument
11799    The .cpload argument is normally $25 == $t9.  */
11800
11801 static void
11802 s_cpload (ignore)
11803      int ignore ATTRIBUTE_UNUSED;
11804 {
11805   expressionS ex;
11806   int icnt = 0;
11807
11808   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11809      .cpload is ignored.  */
11810   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11811     {
11812       s_ignore (0);
11813       return;
11814     }
11815
11816   /* .cpload should be in a .set noreorder section.  */
11817   if (mips_opts.noreorder == 0)
11818     as_warn (_(".cpload not in noreorder section"));
11819
11820   ex.X_op = O_symbol;
11821   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11822   ex.X_op_symbol = NULL;
11823   ex.X_add_number = 0;
11824
11825   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11826   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11827
11828   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11829   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11830                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11831
11832   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11833                mips_gp_register, mips_gp_register, tc_get_register (0));
11834
11835   demand_empty_rest_of_line ();
11836 }
11837
11838 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11839      .cpsetup $reg1, offset|$reg2, label
11840
11841    If offset is given, this results in:
11842      sd         $gp, offset($sp)
11843      lui        $gp, %hi(%neg(%gp_rel(label)))
11844      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11845      daddu      $gp, $gp, $reg1
11846
11847    If $reg2 is given, this results in:
11848      daddu      $reg2, $gp, $0
11849      lui        $gp, %hi(%neg(%gp_rel(label)))
11850      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11851      daddu      $gp, $gp, $reg1
11852    $reg1 is normally $25 == $t9.  */
11853 static void
11854 s_cpsetup (ignore)
11855      int ignore ATTRIBUTE_UNUSED;
11856 {
11857   expressionS ex_off;
11858   expressionS ex_sym;
11859   int reg1;
11860   int icnt = 0;
11861   char *sym;
11862
11863   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11864      We also need NewABI support.  */
11865   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11866     {
11867       s_ignore (0);
11868       return;
11869     }
11870
11871   reg1 = tc_get_register (0);
11872   SKIP_WHITESPACE ();
11873   if (*input_line_pointer != ',')
11874     {
11875       as_bad (_("missing argument separator ',' for .cpsetup"));
11876       return;
11877     }
11878   else
11879     input_line_pointer++;
11880   SKIP_WHITESPACE ();
11881   if (*input_line_pointer == '$')
11882     mips_cpreturn_register = tc_get_register (0);
11883   else
11884     mips_cpreturn_offset = get_absolute_expression ();
11885   SKIP_WHITESPACE ();
11886   if (*input_line_pointer != ',')
11887     {
11888       as_bad (_("missing argument separator ',' for .cpsetup"));
11889       return;
11890     }
11891   else
11892     ++input_line_pointer;
11893   SKIP_WHITESPACE ();
11894   sym = input_line_pointer;
11895   while (ISALNUM (*input_line_pointer))
11896     ++input_line_pointer;
11897   *input_line_pointer = 0;
11898
11899   ex_sym.X_op = O_symbol;
11900   ex_sym.X_add_symbol = symbol_find_or_make (sym);
11901   ex_sym.X_op_symbol = NULL;
11902   ex_sym.X_add_number = 0;
11903
11904   if (mips_cpreturn_register == -1)
11905     {
11906       ex_off.X_op = O_constant;
11907       ex_off.X_add_symbol = NULL;
11908       ex_off.X_op_symbol = NULL;
11909       ex_off.X_add_number = mips_cpreturn_offset;
11910
11911       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11912                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11913     }
11914   else
11915     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11916                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11917
11918   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11919                (int) BFD_RELOC_GPREL16);
11920   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11921   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_HI16_S);
11922   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11923                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11924   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11925   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_LO16);
11926   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11927                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11928                mips_gp_register, mips_gp_register, reg1);
11929
11930   demand_empty_rest_of_line ();
11931 }
11932
11933 static void
11934 s_cplocal (ignore)
11935      int ignore ATTRIBUTE_UNUSED;
11936 {
11937   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11938    .cplocal is ignored.  */
11939   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11940     {
11941       s_ignore (0);
11942       return;
11943     }
11944
11945   mips_gp_register = tc_get_register (0);
11946 }
11947
11948 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11949    offset from $sp.  The offset is remembered, and after making a PIC
11950    call $gp is restored from that location.  */
11951
11952 static void
11953 s_cprestore (ignore)
11954      int ignore ATTRIBUTE_UNUSED;
11955 {
11956   expressionS ex;
11957   int icnt = 0;
11958
11959   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11960      .cprestore is ignored.  */
11961   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11962     {
11963       s_ignore (0);
11964       return;
11965     }
11966
11967   mips_cprestore_offset = get_absolute_expression ();
11968   mips_cprestore_valid = 1;
11969
11970   ex.X_op = O_constant;
11971   ex.X_add_symbol = NULL;
11972   ex.X_op_symbol = NULL;
11973   ex.X_add_number = mips_cprestore_offset;
11974
11975   macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11976                "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
11977
11978   demand_empty_rest_of_line ();
11979 }
11980
11981 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11982    was given in the preceeding .gpsetup, it results in:
11983      ld         $gp, offset($sp)
11984
11985    If a register $reg2 was given there, it results in:
11986      daddiu     $gp, $gp, $reg2
11987  */
11988 static void
11989 s_cpreturn (ignore)
11990      int ignore ATTRIBUTE_UNUSED;
11991 {
11992   expressionS ex;
11993   int icnt = 0;
11994
11995   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11996      We also need NewABI support.  */
11997   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11998     {
11999       s_ignore (0);
12000       return;
12001     }
12002
12003   if (mips_cpreturn_register == -1)
12004     {
12005       ex.X_op = O_constant;
12006       ex.X_add_symbol = NULL;
12007       ex.X_op_symbol = NULL;
12008       ex.X_add_number = mips_cpreturn_offset;
12009
12010       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12011                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12012     }
12013   else
12014     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12015                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12016
12017   demand_empty_rest_of_line ();
12018 }
12019
12020 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12021    code.  It sets the offset to use in gp_rel relocations.  */
12022
12023 static void
12024 s_gpvalue (ignore)
12025      int ignore ATTRIBUTE_UNUSED;
12026 {
12027   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12028      We also need NewABI support.  */
12029   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12030     {
12031       s_ignore (0);
12032       return;
12033     }
12034
12035   mips_gprel_offset = get_absolute_expression ();
12036
12037   demand_empty_rest_of_line ();
12038 }
12039
12040 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12041    code.  It generates a 32 bit GP relative reloc.  */
12042
12043 static void
12044 s_gpword (ignore)
12045      int ignore ATTRIBUTE_UNUSED;
12046 {
12047   symbolS *label;
12048   expressionS ex;
12049   char *p;
12050
12051   /* When not generating PIC code, this is treated as .word.  */
12052   if (mips_pic != SVR4_PIC)
12053     {
12054       s_cons (2);
12055       return;
12056     }
12057
12058   label = insn_labels != NULL ? insn_labels->label : NULL;
12059   mips_emit_delays (true);
12060   if (auto_align)
12061     mips_align (2, 0, label);
12062   mips_clear_insn_labels ();
12063
12064   expression (&ex);
12065
12066   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12067     {
12068       as_bad (_("Unsupported use of .gpword"));
12069       ignore_rest_of_line ();
12070     }
12071
12072   p = frag_more (4);
12073   md_number_to_chars (p, (valueT) 0, 4);
12074   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12075                BFD_RELOC_GPREL32);
12076
12077   demand_empty_rest_of_line ();
12078 }
12079
12080 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12081    tables in SVR4 PIC code.  */
12082
12083 static void
12084 s_cpadd (ignore)
12085      int ignore ATTRIBUTE_UNUSED;
12086 {
12087   int icnt = 0;
12088   int reg;
12089
12090   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12091      code.  */
12092   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12093     {
12094       s_ignore (0);
12095       return;
12096     }
12097
12098   /* Add $gp to the register named as an argument.  */
12099   reg = tc_get_register (0);
12100   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12101                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12102                "d,v,t", reg, reg, mips_gp_register);
12103
12104   demand_empty_rest_of_line ();
12105 }
12106
12107 /* Handle the .insn pseudo-op.  This marks instruction labels in
12108    mips16 mode.  This permits the linker to handle them specially,
12109    such as generating jalx instructions when needed.  We also make
12110    them odd for the duration of the assembly, in order to generate the
12111    right sort of code.  We will make them even in the adjust_symtab
12112    routine, while leaving them marked.  This is convenient for the
12113    debugger and the disassembler.  The linker knows to make them odd
12114    again.  */
12115
12116 static void
12117 s_insn (ignore)
12118      int ignore ATTRIBUTE_UNUSED;
12119 {
12120   mips16_mark_labels ();
12121
12122   demand_empty_rest_of_line ();
12123 }
12124
12125 /* Handle a .stabn directive.  We need these in order to mark a label
12126    as being a mips16 text label correctly.  Sometimes the compiler
12127    will emit a label, followed by a .stabn, and then switch sections.
12128    If the label and .stabn are in mips16 mode, then the label is
12129    really a mips16 text label.  */
12130
12131 static void
12132 s_mips_stab (type)
12133      int type;
12134 {
12135   if (type == 'n')
12136     mips16_mark_labels ();
12137
12138   s_stab (type);
12139 }
12140
12141 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12142  */
12143
12144 static void
12145 s_mips_weakext (ignore)
12146      int ignore ATTRIBUTE_UNUSED;
12147 {
12148   char *name;
12149   int c;
12150   symbolS *symbolP;
12151   expressionS exp;
12152
12153   name = input_line_pointer;
12154   c = get_symbol_end ();
12155   symbolP = symbol_find_or_make (name);
12156   S_SET_WEAK (symbolP);
12157   *input_line_pointer = c;
12158
12159   SKIP_WHITESPACE ();
12160
12161   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12162     {
12163       if (S_IS_DEFINED (symbolP))
12164         {
12165           as_bad ("ignoring attempt to redefine symbol %s",
12166                   S_GET_NAME (symbolP));
12167           ignore_rest_of_line ();
12168           return;
12169         }
12170
12171       if (*input_line_pointer == ',')
12172         {
12173           ++input_line_pointer;
12174           SKIP_WHITESPACE ();
12175         }
12176
12177       expression (&exp);
12178       if (exp.X_op != O_symbol)
12179         {
12180           as_bad ("bad .weakext directive");
12181           ignore_rest_of_line ();
12182           return;
12183         }
12184       symbol_set_value_expression (symbolP, &exp);
12185     }
12186
12187   demand_empty_rest_of_line ();
12188 }
12189
12190 /* Parse a register string into a number.  Called from the ECOFF code
12191    to parse .frame.  The argument is non-zero if this is the frame
12192    register, so that we can record it in mips_frame_reg.  */
12193
12194 int
12195 tc_get_register (frame)
12196      int frame;
12197 {
12198   int reg;
12199
12200   SKIP_WHITESPACE ();
12201   if (*input_line_pointer++ != '$')
12202     {
12203       as_warn (_("expected `$'"));
12204       reg = 0;
12205     }
12206   else if (ISDIGIT (*input_line_pointer))
12207     {
12208       reg = get_absolute_expression ();
12209       if (reg < 0 || reg >= 32)
12210         {
12211           as_warn (_("Bad register number"));
12212           reg = 0;
12213         }
12214     }
12215   else
12216     {
12217       if (strncmp (input_line_pointer, "ra", 2) == 0)
12218         reg = RA;
12219       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12220         reg = FP;
12221       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12222         reg = SP;
12223       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12224         reg = GP;
12225       else if (strncmp (input_line_pointer, "at", 2) == 0)
12226         reg = AT;
12227       else
12228         {
12229           as_warn (_("Unrecognized register name"));
12230           reg = 0;
12231         }
12232       input_line_pointer += 2;
12233     }
12234   if (frame)
12235     {
12236       mips_frame_reg = reg != 0 ? reg : SP;
12237       mips_frame_reg_valid = 1;
12238       mips_cprestore_valid = 0;
12239     }
12240   return reg;
12241 }
12242
12243 valueT
12244 md_section_align (seg, addr)
12245      asection *seg;
12246      valueT addr;
12247 {
12248   int align = bfd_get_section_alignment (stdoutput, seg);
12249
12250 #ifdef OBJ_ELF
12251   /* We don't need to align ELF sections to the full alignment.
12252      However, Irix 5 may prefer that we align them at least to a 16
12253      byte boundary.  We don't bother to align the sections if we are
12254      targeted for an embedded system.  */
12255   if (strcmp (TARGET_OS, "elf") == 0)
12256     return addr;
12257   if (align > 4)
12258     align = 4;
12259 #endif
12260
12261   return ((addr + (1 << align) - 1) & (-1 << align));
12262 }
12263
12264 /* Utility routine, called from above as well.  If called while the
12265    input file is still being read, it's only an approximation.  (For
12266    example, a symbol may later become defined which appeared to be
12267    undefined earlier.)  */
12268
12269 static int
12270 nopic_need_relax (sym, before_relaxing)
12271      symbolS *sym;
12272      int before_relaxing;
12273 {
12274   if (sym == 0)
12275     return 0;
12276
12277   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12278     {
12279       const char *symname;
12280       int change;
12281
12282       /* Find out whether this symbol can be referenced off the $gp
12283          register.  It can be if it is smaller than the -G size or if
12284          it is in the .sdata or .sbss section.  Certain symbols can
12285          not be referenced off the $gp, although it appears as though
12286          they can.  */
12287       symname = S_GET_NAME (sym);
12288       if (symname != (const char *) NULL
12289           && (strcmp (symname, "eprol") == 0
12290               || strcmp (symname, "etext") == 0
12291               || strcmp (symname, "_gp") == 0
12292               || strcmp (symname, "edata") == 0
12293               || strcmp (symname, "_fbss") == 0
12294               || strcmp (symname, "_fdata") == 0
12295               || strcmp (symname, "_ftext") == 0
12296               || strcmp (symname, "end") == 0
12297               || strcmp (symname, "_gp_disp") == 0))
12298         change = 1;
12299       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12300                && (0
12301 #ifndef NO_ECOFF_DEBUGGING
12302                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12303                        && (symbol_get_obj (sym)->ecoff_extern_size
12304                            <= g_switch_value))
12305 #endif
12306                    /* We must defer this decision until after the whole
12307                       file has been read, since there might be a .extern
12308                       after the first use of this symbol.  */
12309                    || (before_relaxing
12310 #ifndef NO_ECOFF_DEBUGGING
12311                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12312 #endif
12313                        && S_GET_VALUE (sym) == 0)
12314                    || (S_GET_VALUE (sym) != 0
12315                        && S_GET_VALUE (sym) <= g_switch_value)))
12316         change = 0;
12317       else
12318         {
12319           const char *segname;
12320
12321           segname = segment_name (S_GET_SEGMENT (sym));
12322           assert (strcmp (segname, ".lit8") != 0
12323                   && strcmp (segname, ".lit4") != 0);
12324           change = (strcmp (segname, ".sdata") != 0
12325                     && strcmp (segname, ".sbss") != 0
12326                     && strncmp (segname, ".sdata.", 7) != 0
12327                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12328         }
12329       return change;
12330     }
12331   else
12332     /* We are not optimizing for the $gp register.  */
12333     return 1;
12334 }
12335
12336 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12337    extended opcode.  SEC is the section the frag is in.  */
12338
12339 static int
12340 mips16_extended_frag (fragp, sec, stretch)
12341      fragS *fragp;
12342      asection *sec;
12343      long stretch;
12344 {
12345   int type;
12346   register const struct mips16_immed_operand *op;
12347   offsetT val;
12348   int mintiny, maxtiny;
12349   segT symsec;
12350   fragS *sym_frag;
12351
12352   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12353     return 0;
12354   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12355     return 1;
12356
12357   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12358   op = mips16_immed_operands;
12359   while (op->type != type)
12360     {
12361       ++op;
12362       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12363     }
12364
12365   if (op->unsp)
12366     {
12367       if (type == '<' || type == '>' || type == '[' || type == ']')
12368         {
12369           mintiny = 1;
12370           maxtiny = 1 << op->nbits;
12371         }
12372       else
12373         {
12374           mintiny = 0;
12375           maxtiny = (1 << op->nbits) - 1;
12376         }
12377     }
12378   else
12379     {
12380       mintiny = - (1 << (op->nbits - 1));
12381       maxtiny = (1 << (op->nbits - 1)) - 1;
12382     }
12383
12384   sym_frag = symbol_get_frag (fragp->fr_symbol);
12385   val = S_GET_VALUE (fragp->fr_symbol);
12386   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12387
12388   if (op->pcrel)
12389     {
12390       addressT addr;
12391
12392       /* We won't have the section when we are called from
12393          mips_relax_frag.  However, we will always have been called
12394          from md_estimate_size_before_relax first.  If this is a
12395          branch to a different section, we mark it as such.  If SEC is
12396          NULL, and the frag is not marked, then it must be a branch to
12397          the same section.  */
12398       if (sec == NULL)
12399         {
12400           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12401             return 1;
12402         }
12403       else
12404         {
12405           /* Must have been called from md_estimate_size_before_relax.  */
12406           if (symsec != sec)
12407             {
12408               fragp->fr_subtype =
12409                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12410
12411               /* FIXME: We should support this, and let the linker
12412                  catch branches and loads that are out of range.  */
12413               as_bad_where (fragp->fr_file, fragp->fr_line,
12414                             _("unsupported PC relative reference to different section"));
12415
12416               return 1;
12417             }
12418           if (fragp != sym_frag && sym_frag->fr_address == 0)
12419             /* Assume non-extended on the first relaxation pass.
12420                The address we have calculated will be bogus if this is
12421                a forward branch to another frag, as the forward frag
12422                will have fr_address == 0.  */
12423             return 0;
12424         }
12425
12426       /* In this case, we know for sure that the symbol fragment is in
12427          the same section.  If the relax_marker of the symbol fragment
12428          differs from the relax_marker of this fragment, we have not
12429          yet adjusted the symbol fragment fr_address.  We want to add
12430          in STRETCH in order to get a better estimate of the address.
12431          This particularly matters because of the shift bits.  */
12432       if (stretch != 0
12433           && sym_frag->relax_marker != fragp->relax_marker)
12434         {
12435           fragS *f;
12436
12437           /* Adjust stretch for any alignment frag.  Note that if have
12438              been expanding the earlier code, the symbol may be
12439              defined in what appears to be an earlier frag.  FIXME:
12440              This doesn't handle the fr_subtype field, which specifies
12441              a maximum number of bytes to skip when doing an
12442              alignment.  */
12443           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12444             {
12445               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12446                 {
12447                   if (stretch < 0)
12448                     stretch = - ((- stretch)
12449                                  & ~ ((1 << (int) f->fr_offset) - 1));
12450                   else
12451                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12452                   if (stretch == 0)
12453                     break;
12454                 }
12455             }
12456           if (f != NULL)
12457             val += stretch;
12458         }
12459
12460       addr = fragp->fr_address + fragp->fr_fix;
12461
12462       /* The base address rules are complicated.  The base address of
12463          a branch is the following instruction.  The base address of a
12464          PC relative load or add is the instruction itself, but if it
12465          is in a delay slot (in which case it can not be extended) use
12466          the address of the instruction whose delay slot it is in.  */
12467       if (type == 'p' || type == 'q')
12468         {
12469           addr += 2;
12470
12471           /* If we are currently assuming that this frag should be
12472              extended, then, the current address is two bytes
12473              higher.  */
12474           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12475             addr += 2;
12476
12477           /* Ignore the low bit in the target, since it will be set
12478              for a text label.  */
12479           if ((val & 1) != 0)
12480             --val;
12481         }
12482       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12483         addr -= 4;
12484       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12485         addr -= 2;
12486
12487       val -= addr & ~ ((1 << op->shift) - 1);
12488
12489       /* Branch offsets have an implicit 0 in the lowest bit.  */
12490       if (type == 'p' || type == 'q')
12491         val /= 2;
12492
12493       /* If any of the shifted bits are set, we must use an extended
12494          opcode.  If the address depends on the size of this
12495          instruction, this can lead to a loop, so we arrange to always
12496          use an extended opcode.  We only check this when we are in
12497          the main relaxation loop, when SEC is NULL.  */
12498       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12499         {
12500           fragp->fr_subtype =
12501             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12502           return 1;
12503         }
12504
12505       /* If we are about to mark a frag as extended because the value
12506          is precisely maxtiny + 1, then there is a chance of an
12507          infinite loop as in the following code:
12508              la $4,foo
12509              .skip      1020
12510              .align     2
12511            foo:
12512          In this case when the la is extended, foo is 0x3fc bytes
12513          away, so the la can be shrunk, but then foo is 0x400 away, so
12514          the la must be extended.  To avoid this loop, we mark the
12515          frag as extended if it was small, and is about to become
12516          extended with a value of maxtiny + 1.  */
12517       if (val == ((maxtiny + 1) << op->shift)
12518           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12519           && sec == NULL)
12520         {
12521           fragp->fr_subtype =
12522             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12523           return 1;
12524         }
12525     }
12526   else if (symsec != absolute_section && sec != NULL)
12527     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12528
12529   if ((val & ((1 << op->shift) - 1)) != 0
12530       || val < (mintiny << op->shift)
12531       || val > (maxtiny << op->shift))
12532     return 1;
12533   else
12534     return 0;
12535 }
12536
12537 /* Estimate the size of a frag before relaxing.  Unless this is the
12538    mips16, we are not really relaxing here, and the final size is
12539    encoded in the subtype information.  For the mips16, we have to
12540    decide whether we are using an extended opcode or not.  */
12541
12542 int
12543 md_estimate_size_before_relax (fragp, segtype)
12544      fragS *fragp;
12545      asection *segtype;
12546 {
12547   int change = 0;
12548   boolean linkonce = false;
12549
12550   if (RELAX_MIPS16_P (fragp->fr_subtype))
12551     /* We don't want to modify the EXTENDED bit here; it might get us
12552        into infinite loops.  We change it only in mips_relax_frag().  */
12553     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12554
12555   if (mips_pic == NO_PIC)
12556     {
12557       change = nopic_need_relax (fragp->fr_symbol, 0);
12558     }
12559   else if (mips_pic == SVR4_PIC)
12560     {
12561       symbolS *sym;
12562       asection *symsec;
12563
12564       sym = fragp->fr_symbol;
12565
12566       /* Handle the case of a symbol equated to another symbol.  */
12567       while (symbol_equated_reloc_p (sym))
12568         {
12569           symbolS *n;
12570
12571           /* It's possible to get a loop here in a badly written
12572              program.  */
12573           n = symbol_get_value_expression (sym)->X_add_symbol;
12574           if (n == sym)
12575             break;
12576           sym = n;
12577         }
12578
12579       symsec = S_GET_SEGMENT (sym);
12580
12581       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12582       if (symsec != segtype && ! S_IS_LOCAL (sym))
12583         {
12584           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12585               != 0)
12586             linkonce = true;
12587
12588           /* The GNU toolchain uses an extension for ELF: a section
12589              beginning with the magic string .gnu.linkonce is a linkonce
12590              section.  */
12591           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12592                        sizeof ".gnu.linkonce" - 1) == 0)
12593             linkonce = true;
12594         }
12595
12596       /* This must duplicate the test in adjust_reloc_syms.  */
12597       change = (symsec != &bfd_und_section
12598                 && symsec != &bfd_abs_section
12599                 && ! bfd_is_com_section (symsec)
12600                 && !linkonce
12601 #ifdef OBJ_ELF
12602                 /* A global or weak symbol is treated as external.  */
12603                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12604                     || (! S_IS_WEAK (sym)
12605                         && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
12606 #endif
12607                 );
12608     }
12609   else
12610     abort ();
12611
12612   if (change)
12613     {
12614       /* Record the offset to the first reloc in the fr_opcode field.
12615          This lets md_convert_frag and tc_gen_reloc know that the code
12616          must be expanded.  */
12617       fragp->fr_opcode = (fragp->fr_literal
12618                           + fragp->fr_fix
12619                           - RELAX_OLD (fragp->fr_subtype)
12620                           + RELAX_RELOC1 (fragp->fr_subtype));
12621       /* FIXME: This really needs as_warn_where.  */
12622       if (RELAX_WARN (fragp->fr_subtype))
12623         as_warn (_("AT used after \".set noat\" or macro used after "
12624                    "\".set nomacro\""));
12625
12626       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12627     }
12628
12629   return 0;
12630 }
12631
12632 /* This is called to see whether a reloc against a defined symbol
12633    should be converted into a reloc against a section.  Don't adjust
12634    MIPS16 jump relocations, so we don't have to worry about the format
12635    of the offset in the .o file.  Don't adjust relocations against
12636    mips16 symbols, so that the linker can find them if it needs to set
12637    up a stub.  */
12638
12639 int
12640 mips_fix_adjustable (fixp)
12641      fixS *fixp;
12642 {
12643 #ifdef OBJ_ELF
12644   /* Prevent all adjustments to global symbols.  */
12645   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12646       && mips_pic != EMBEDDED_PIC
12647       && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12648     return 0;
12649 #endif
12650   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12651     return 0;
12652   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12653       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12654     return 0;
12655   if (fixp->fx_addsy == NULL)
12656     return 1;
12657 #ifdef OBJ_ELF
12658   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12659       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12660       && fixp->fx_subsy == NULL)
12661     return 0;
12662 #endif
12663   return 1;
12664 }
12665
12666 /* Translate internal representation of relocation info to BFD target
12667    format.  */
12668
12669 arelent **
12670 tc_gen_reloc (section, fixp)
12671      asection *section ATTRIBUTE_UNUSED;
12672      fixS *fixp;
12673 {
12674   static arelent *retval[4];
12675   arelent *reloc;
12676   bfd_reloc_code_real_type code;
12677
12678   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12679   retval[1] = NULL;
12680
12681   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12682   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12683   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12684
12685   if (mips_pic == EMBEDDED_PIC
12686       && SWITCH_TABLE (fixp))
12687     {
12688       /* For a switch table entry we use a special reloc.  The addend
12689          is actually the difference between the reloc address and the
12690          subtrahend.  */
12691       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12692       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12693         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12694       fixp->fx_r_type = BFD_RELOC_GPREL32;
12695     }
12696   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12697     {
12698       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12699         reloc->addend = fixp->fx_addnumber;
12700       else
12701         {
12702           /* We use a special addend for an internal RELLO reloc.  */
12703           if (symbol_section_p (fixp->fx_addsy))
12704             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12705           else
12706             reloc->addend = fixp->fx_addnumber + reloc->address;
12707         }
12708     }
12709   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12710     {
12711       assert (fixp->fx_next != NULL
12712               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12713
12714       /* The reloc is relative to the RELLO; adjust the addend
12715          accordingly.  */
12716       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12717         reloc->addend = fixp->fx_next->fx_addnumber;
12718       else
12719         {
12720           /* We use a special addend for an internal RELHI reloc.  */
12721           if (symbol_section_p (fixp->fx_addsy))
12722             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12723                              + fixp->fx_next->fx_where
12724                              - S_GET_VALUE (fixp->fx_subsy));
12725           else
12726             reloc->addend = (fixp->fx_addnumber
12727                              + fixp->fx_next->fx_frag->fr_address
12728                              + fixp->fx_next->fx_where);
12729         }
12730     }
12731   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12732     reloc->addend = fixp->fx_addnumber;
12733   else
12734     {
12735       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12736         /* A gruesome hack which is a result of the gruesome gas reloc
12737            handling.  */
12738         reloc->addend = reloc->address;
12739       else
12740         reloc->addend = -reloc->address;
12741     }
12742
12743   /* If this is a variant frag, we may need to adjust the existing
12744      reloc and generate a new one.  */
12745   if (fixp->fx_frag->fr_opcode != NULL
12746       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12747           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12748           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12749           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12750           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12751           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12752           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12753       && ! HAVE_NEWABI)
12754     {
12755       arelent *reloc2;
12756
12757       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12758
12759       /* If this is not the last reloc in this frag, then we have two
12760          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12761          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12762          the second one handle all of them.  */
12763       if (fixp->fx_next != NULL
12764           && fixp->fx_frag == fixp->fx_next->fx_frag)
12765         {
12766           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12767                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12768                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12769                       && (fixp->fx_next->fx_r_type
12770                           == BFD_RELOC_MIPS_GOT_LO16))
12771                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12772                       && (fixp->fx_next->fx_r_type
12773                           == BFD_RELOC_MIPS_CALL_LO16)));
12774           retval[0] = NULL;
12775           return retval;
12776         }
12777
12778       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12779       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12780       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12781       retval[2] = NULL;
12782       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12783       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12784       reloc2->address = (reloc->address
12785                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12786                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12787       reloc2->addend = fixp->fx_addnumber;
12788       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12789       assert (reloc2->howto != NULL);
12790
12791       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12792         {
12793           arelent *reloc3;
12794
12795           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12796           retval[3] = NULL;
12797           *reloc3 = *reloc2;
12798           reloc3->address += 4;
12799         }
12800
12801       if (mips_pic == NO_PIC)
12802         {
12803           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12804           fixp->fx_r_type = BFD_RELOC_HI16_S;
12805         }
12806       else if (mips_pic == SVR4_PIC)
12807         {
12808           switch (fixp->fx_r_type)
12809             {
12810             default:
12811               abort ();
12812             case BFD_RELOC_MIPS_GOT16:
12813               break;
12814             case BFD_RELOC_MIPS_CALL16:
12815             case BFD_RELOC_MIPS_GOT_LO16:
12816             case BFD_RELOC_MIPS_CALL_LO16:
12817               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12818               break;
12819             }
12820         }
12821       else
12822         abort ();
12823     }
12824
12825   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12826      entry to be used in the relocation's section offset.  */
12827   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12828     {
12829       reloc->address = reloc->addend;
12830       reloc->addend = 0;
12831     }
12832
12833   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12834      fixup_segment converted a non-PC relative reloc into a PC
12835      relative reloc.  In such a case, we need to convert the reloc
12836      code.  */
12837   code = fixp->fx_r_type;
12838   if (fixp->fx_pcrel)
12839     {
12840       switch (code)
12841         {
12842         case BFD_RELOC_8:
12843           code = BFD_RELOC_8_PCREL;
12844           break;
12845         case BFD_RELOC_16:
12846           code = BFD_RELOC_16_PCREL;
12847           break;
12848         case BFD_RELOC_32:
12849           code = BFD_RELOC_32_PCREL;
12850           break;
12851         case BFD_RELOC_64:
12852           code = BFD_RELOC_64_PCREL;
12853           break;
12854         case BFD_RELOC_8_PCREL:
12855         case BFD_RELOC_16_PCREL:
12856         case BFD_RELOC_32_PCREL:
12857         case BFD_RELOC_64_PCREL:
12858         case BFD_RELOC_16_PCREL_S2:
12859         case BFD_RELOC_PCREL_HI16_S:
12860         case BFD_RELOC_PCREL_LO16:
12861           break;
12862         default:
12863           as_bad_where (fixp->fx_file, fixp->fx_line,
12864                         _("Cannot make %s relocation PC relative"),
12865                         bfd_get_reloc_code_name (code));
12866         }
12867     }
12868
12869 #ifdef OBJ_ELF
12870   /* md_apply_fix3 has a double-subtraction hack to get
12871      bfd_install_relocation to behave nicely.  GPREL relocations are
12872      handled correctly without this hack, so undo it here.  We can't
12873      stop md_apply_fix3 from subtracting twice in the first place since
12874      the fake addend is required for variant frags above.  */
12875   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12876       && code == BFD_RELOC_GPREL16
12877       && reloc->addend != 0
12878       && mips_need_elf_addend_fixup (fixp))
12879     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12880 #endif
12881
12882   /* To support a PC relative reloc when generating embedded PIC code
12883      for ECOFF, we use a Cygnus extension.  We check for that here to
12884      make sure that we don't let such a reloc escape normally.  */
12885   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12886        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12887       && code == BFD_RELOC_16_PCREL_S2
12888       && mips_pic != EMBEDDED_PIC)
12889     reloc->howto = NULL;
12890   else
12891     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12892
12893   if (reloc->howto == NULL)
12894     {
12895       as_bad_where (fixp->fx_file, fixp->fx_line,
12896                     _("Can not represent %s relocation in this object file format"),
12897                     bfd_get_reloc_code_name (code));
12898       retval[0] = NULL;
12899     }
12900
12901   return retval;
12902 }
12903
12904 /* Relax a machine dependent frag.  This returns the amount by which
12905    the current size of the frag should change.  */
12906
12907 int
12908 mips_relax_frag (fragp, stretch)
12909      fragS *fragp;
12910      long stretch;
12911 {
12912   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12913     return 0;
12914
12915   if (mips16_extended_frag (fragp, NULL, stretch))
12916     {
12917       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12918         return 0;
12919       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12920       return 2;
12921     }
12922   else
12923     {
12924       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12925         return 0;
12926       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12927       return -2;
12928     }
12929
12930   return 0;
12931 }
12932
12933 /* Convert a machine dependent frag.  */
12934
12935 void
12936 md_convert_frag (abfd, asec, fragp)
12937      bfd *abfd ATTRIBUTE_UNUSED;
12938      segT asec;
12939      fragS *fragp;
12940 {
12941   int old, new;
12942   char *fixptr;
12943
12944   if (RELAX_MIPS16_P (fragp->fr_subtype))
12945     {
12946       int type;
12947       register const struct mips16_immed_operand *op;
12948       boolean small, ext;
12949       offsetT val;
12950       bfd_byte *buf;
12951       unsigned long insn;
12952       boolean use_extend;
12953       unsigned short extend;
12954
12955       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12956       op = mips16_immed_operands;
12957       while (op->type != type)
12958         ++op;
12959
12960       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12961         {
12962           small = false;
12963           ext = true;
12964         }
12965       else
12966         {
12967           small = true;
12968           ext = false;
12969         }
12970
12971       resolve_symbol_value (fragp->fr_symbol);
12972       val = S_GET_VALUE (fragp->fr_symbol);
12973       if (op->pcrel)
12974         {
12975           addressT addr;
12976
12977           addr = fragp->fr_address + fragp->fr_fix;
12978
12979           /* The rules for the base address of a PC relative reloc are
12980              complicated; see mips16_extended_frag.  */
12981           if (type == 'p' || type == 'q')
12982             {
12983               addr += 2;
12984               if (ext)
12985                 addr += 2;
12986               /* Ignore the low bit in the target, since it will be
12987                  set for a text label.  */
12988               if ((val & 1) != 0)
12989                 --val;
12990             }
12991           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12992             addr -= 4;
12993           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12994             addr -= 2;
12995
12996           addr &= ~ (addressT) ((1 << op->shift) - 1);
12997           val -= addr;
12998
12999           /* Make sure the section winds up with the alignment we have
13000              assumed.  */
13001           if (op->shift > 0)
13002             record_alignment (asec, op->shift);
13003         }
13004
13005       if (ext
13006           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13007               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13008         as_warn_where (fragp->fr_file, fragp->fr_line,
13009                        _("extended instruction in delay slot"));
13010
13011       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13012
13013       if (target_big_endian)
13014         insn = bfd_getb16 (buf);
13015       else
13016         insn = bfd_getl16 (buf);
13017
13018       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13019                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13020                     small, ext, &insn, &use_extend, &extend);
13021
13022       if (use_extend)
13023         {
13024           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13025           fragp->fr_fix += 2;
13026           buf += 2;
13027         }
13028
13029       md_number_to_chars ((char *) buf, insn, 2);
13030       fragp->fr_fix += 2;
13031       buf += 2;
13032     }
13033   else
13034     {
13035       if (fragp->fr_opcode == NULL)
13036         return;
13037
13038       old = RELAX_OLD (fragp->fr_subtype);
13039       new = RELAX_NEW (fragp->fr_subtype);
13040       fixptr = fragp->fr_literal + fragp->fr_fix;
13041
13042       if (new > 0)
13043         memcpy (fixptr - old, fixptr, new);
13044
13045       fragp->fr_fix += new - old;
13046     }
13047 }
13048
13049 #ifdef OBJ_ELF
13050
13051 /* This function is called after the relocs have been generated.
13052    We've been storing mips16 text labels as odd.  Here we convert them
13053    back to even for the convenience of the debugger.  */
13054
13055 void
13056 mips_frob_file_after_relocs ()
13057 {
13058   asymbol **syms;
13059   unsigned int count, i;
13060
13061   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13062     return;
13063
13064   syms = bfd_get_outsymbols (stdoutput);
13065   count = bfd_get_symcount (stdoutput);
13066   for (i = 0; i < count; i++, syms++)
13067     {
13068       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13069           && ((*syms)->value & 1) != 0)
13070         {
13071           (*syms)->value &= ~1;
13072           /* If the symbol has an odd size, it was probably computed
13073              incorrectly, so adjust that as well.  */
13074           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13075             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13076         }
13077     }
13078 }
13079
13080 #endif
13081
13082 /* This function is called whenever a label is defined.  It is used
13083    when handling branch delays; if a branch has a label, we assume we
13084    can not move it.  */
13085
13086 void
13087 mips_define_label (sym)
13088      symbolS *sym;
13089 {
13090   struct insn_label_list *l;
13091
13092   if (free_insn_labels == NULL)
13093     l = (struct insn_label_list *) xmalloc (sizeof *l);
13094   else
13095     {
13096       l = free_insn_labels;
13097       free_insn_labels = l->next;
13098     }
13099
13100   l->label = sym;
13101   l->next = insn_labels;
13102   insn_labels = l;
13103 }
13104 \f
13105 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13106
13107 /* Some special processing for a MIPS ELF file.  */
13108
13109 void
13110 mips_elf_final_processing ()
13111 {
13112   /* Write out the register information.  */
13113   if (file_mips_abi != N64_ABI)
13114     {
13115       Elf32_RegInfo s;
13116
13117       s.ri_gprmask = mips_gprmask;
13118       s.ri_cprmask[0] = mips_cprmask[0];
13119       s.ri_cprmask[1] = mips_cprmask[1];
13120       s.ri_cprmask[2] = mips_cprmask[2];
13121       s.ri_cprmask[3] = mips_cprmask[3];
13122       /* The gp_value field is set by the MIPS ELF backend.  */
13123
13124       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13125                                        ((Elf32_External_RegInfo *)
13126                                         mips_regmask_frag));
13127     }
13128   else
13129     {
13130       Elf64_Internal_RegInfo s;
13131
13132       s.ri_gprmask = mips_gprmask;
13133       s.ri_pad = 0;
13134       s.ri_cprmask[0] = mips_cprmask[0];
13135       s.ri_cprmask[1] = mips_cprmask[1];
13136       s.ri_cprmask[2] = mips_cprmask[2];
13137       s.ri_cprmask[3] = mips_cprmask[3];
13138       /* The gp_value field is set by the MIPS ELF backend.  */
13139
13140       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13141                                        ((Elf64_External_RegInfo *)
13142                                         mips_regmask_frag));
13143     }
13144
13145   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13146      sort of BFD interface for this.  */
13147   if (mips_any_noreorder)
13148     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13149   if (mips_pic != NO_PIC)
13150     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13151
13152   /* Set MIPS ELF flags for ASEs.  */
13153 #if 0 /* XXX FIXME */
13154   if (file_ase_mips3d)
13155     elf_elfheader (stdoutput)->e_flags |= ???;
13156 #endif
13157   if (file_ase_mdmx)
13158     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13159
13160   /* Set the MIPS ELF ABI flags.  */
13161   if (file_mips_abi == NO_ABI)
13162     ;
13163   else if (file_mips_abi == O32_ABI)
13164     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13165   else if (file_mips_abi == O64_ABI)
13166     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13167   else if (file_mips_abi == EABI_ABI)
13168     {
13169       if (mips_eabi64)
13170         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13171       else
13172         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13173     }
13174   else if (file_mips_abi == N32_ABI)
13175     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13176
13177   /* Nothing to do for N64_ABI.  */
13178
13179   if (mips_32bitmode)
13180     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13181 }
13182
13183 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13184 \f
13185 typedef struct proc {
13186   symbolS *isym;
13187   unsigned long reg_mask;
13188   unsigned long reg_offset;
13189   unsigned long fpreg_mask;
13190   unsigned long fpreg_offset;
13191   unsigned long frame_offset;
13192   unsigned long frame_reg;
13193   unsigned long pc_reg;
13194 } procS;
13195
13196 static procS cur_proc;
13197 static procS *cur_proc_ptr;
13198 static int numprocs;
13199
13200 /* Fill in an rs_align_code fragment.  */
13201
13202 void
13203 mips_handle_align (fragp)
13204      fragS *fragp;
13205 {
13206   if (fragp->fr_type != rs_align_code)
13207     return;
13208
13209   if (mips_opts.mips16)
13210     {
13211       static const unsigned char be_nop[] = { 0x65, 0x00 };
13212       static const unsigned char le_nop[] = { 0x00, 0x65 };
13213
13214       int bytes;
13215       char *p;
13216
13217       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13218       p = fragp->fr_literal + fragp->fr_fix;
13219
13220       if (bytes & 1)
13221         {
13222           *p++ = 0;
13223           fragp->fr_fix++;
13224         }
13225
13226       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13227       fragp->fr_var = 2;
13228     }
13229
13230   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13231 }
13232
13233 static void
13234 md_obj_begin ()
13235 {
13236 }
13237
13238 static void
13239 md_obj_end ()
13240 {
13241   /* check for premature end, nesting errors, etc */
13242   if (cur_proc_ptr)
13243     as_warn (_("missing .end at end of assembly"));
13244 }
13245
13246 static long
13247 get_number ()
13248 {
13249   int negative = 0;
13250   long val = 0;
13251
13252   if (*input_line_pointer == '-')
13253     {
13254       ++input_line_pointer;
13255       negative = 1;
13256     }
13257   if (!ISDIGIT (*input_line_pointer))
13258     as_bad (_("expected simple number"));
13259   if (input_line_pointer[0] == '0')
13260     {
13261       if (input_line_pointer[1] == 'x')
13262         {
13263           input_line_pointer += 2;
13264           while (ISXDIGIT (*input_line_pointer))
13265             {
13266               val <<= 4;
13267               val |= hex_value (*input_line_pointer++);
13268             }
13269           return negative ? -val : val;
13270         }
13271       else
13272         {
13273           ++input_line_pointer;
13274           while (ISDIGIT (*input_line_pointer))
13275             {
13276               val <<= 3;
13277               val |= *input_line_pointer++ - '0';
13278             }
13279           return negative ? -val : val;
13280         }
13281     }
13282   if (!ISDIGIT (*input_line_pointer))
13283     {
13284       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13285               *input_line_pointer, *input_line_pointer);
13286       as_warn (_("invalid number"));
13287       return -1;
13288     }
13289   while (ISDIGIT (*input_line_pointer))
13290     {
13291       val *= 10;
13292       val += *input_line_pointer++ - '0';
13293     }
13294   return negative ? -val : val;
13295 }
13296
13297 /* The .file directive; just like the usual .file directive, but there
13298    is an initial number which is the ECOFF file index.  */
13299
13300 static void
13301 s_file (x)
13302      int x ATTRIBUTE_UNUSED;
13303 {
13304   get_number ();
13305   s_app_file (0);
13306 }
13307
13308 /* The .end directive.  */
13309
13310 static void
13311 s_mips_end (x)
13312      int x ATTRIBUTE_UNUSED;
13313 {
13314   symbolS *p;
13315   int maybe_text;
13316
13317   /* Following functions need their own .frame and .cprestore directives.  */
13318   mips_frame_reg_valid = 0;
13319   mips_cprestore_valid = 0;
13320
13321   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13322     {
13323       p = get_symbol ();
13324       demand_empty_rest_of_line ();
13325     }
13326   else
13327     p = NULL;
13328
13329 #ifdef BFD_ASSEMBLER
13330   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13331     maybe_text = 1;
13332   else
13333     maybe_text = 0;
13334 #else
13335   if (now_seg != data_section && now_seg != bss_section)
13336     maybe_text = 1;
13337   else
13338     maybe_text = 0;
13339 #endif
13340
13341   if (!maybe_text)
13342     as_warn (_(".end not in text section"));
13343
13344   if (!cur_proc_ptr)
13345     {
13346       as_warn (_(".end directive without a preceding .ent directive."));
13347       demand_empty_rest_of_line ();
13348       return;
13349     }
13350
13351   if (p != NULL)
13352     {
13353       assert (S_GET_NAME (p));
13354       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13355         as_warn (_(".end symbol does not match .ent symbol."));
13356     }
13357   else
13358     as_warn (_(".end directive missing or unknown symbol"));
13359
13360 #ifdef MIPS_STABS_ELF
13361   {
13362     segT saved_seg = now_seg;
13363     subsegT saved_subseg = now_subseg;
13364     valueT dot;
13365     expressionS exp;
13366     char *fragp;
13367
13368     dot = frag_now_fix ();
13369
13370 #ifdef md_flush_pending_output
13371     md_flush_pending_output ();
13372 #endif
13373
13374     assert (pdr_seg);
13375     subseg_set (pdr_seg, 0);
13376
13377     /* Write the symbol.  */
13378     exp.X_op = O_symbol;
13379     exp.X_add_symbol = p;
13380     exp.X_add_number = 0;
13381     emit_expr (&exp, 4);
13382
13383     fragp = frag_more (7 * 4);
13384
13385     md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13386     md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13387     md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13388     md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13389     md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13390     md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13391     md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13392
13393     subseg_set (saved_seg, saved_subseg);
13394   }
13395 #endif /* MIPS_STABS_ELF */
13396
13397   cur_proc_ptr = NULL;
13398 }
13399
13400 /* The .aent and .ent directives.  */
13401
13402 static void
13403 s_mips_ent (aent)
13404      int aent;
13405 {
13406   symbolS *symbolP;
13407   int maybe_text;
13408
13409   symbolP = get_symbol ();
13410   if (*input_line_pointer == ',')
13411     ++input_line_pointer;
13412   SKIP_WHITESPACE ();
13413   if (ISDIGIT (*input_line_pointer)
13414       || *input_line_pointer == '-')
13415     get_number ();
13416
13417 #ifdef BFD_ASSEMBLER
13418   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13419     maybe_text = 1;
13420   else
13421     maybe_text = 0;
13422 #else
13423   if (now_seg != data_section && now_seg != bss_section)
13424     maybe_text = 1;
13425   else
13426     maybe_text = 0;
13427 #endif
13428
13429   if (!maybe_text)
13430     as_warn (_(".ent or .aent not in text section."));
13431
13432   if (!aent && cur_proc_ptr)
13433     as_warn (_("missing .end"));
13434
13435   if (!aent)
13436     {
13437       /* This function needs its own .frame and .cprestore directives.  */
13438       mips_frame_reg_valid = 0;
13439       mips_cprestore_valid = 0;
13440
13441       cur_proc_ptr = &cur_proc;
13442       memset (cur_proc_ptr, '\0', sizeof (procS));
13443
13444       cur_proc_ptr->isym = symbolP;
13445
13446       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13447
13448       ++numprocs;
13449     }
13450
13451   demand_empty_rest_of_line ();
13452 }
13453
13454 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13455    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13456    s_mips_frame is used so that we can set the PDR information correctly.
13457    We can't use the ecoff routines because they make reference to the ecoff
13458    symbol table (in the mdebug section).  */
13459
13460 static void
13461 s_mips_frame (ignore)
13462      int ignore ATTRIBUTE_UNUSED;
13463 {
13464 #ifdef MIPS_STABS_ELF
13465
13466   long val;
13467
13468   if (cur_proc_ptr == (procS *) NULL)
13469     {
13470       as_warn (_(".frame outside of .ent"));
13471       demand_empty_rest_of_line ();
13472       return;
13473     }
13474
13475   cur_proc_ptr->frame_reg = tc_get_register (1);
13476
13477   SKIP_WHITESPACE ();
13478   if (*input_line_pointer++ != ','
13479       || get_absolute_expression_and_terminator (&val) != ',')
13480     {
13481       as_warn (_("Bad .frame directive"));
13482       --input_line_pointer;
13483       demand_empty_rest_of_line ();
13484       return;
13485     }
13486
13487   cur_proc_ptr->frame_offset = val;
13488   cur_proc_ptr->pc_reg = tc_get_register (0);
13489
13490   demand_empty_rest_of_line ();
13491 #else
13492   s_ignore (ignore);
13493 #endif /* MIPS_STABS_ELF */
13494 }
13495
13496 /* The .fmask and .mask directives. If the mdebug section is present
13497    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13498    embedded targets, s_mips_mask is used so that we can set the PDR
13499    information correctly. We can't use the ecoff routines because they
13500    make reference to the ecoff symbol table (in the mdebug section).  */
13501
13502 static void
13503 s_mips_mask (reg_type)
13504      char reg_type;
13505 {
13506 #ifdef MIPS_STABS_ELF
13507   long mask, off;
13508
13509   if (cur_proc_ptr == (procS *) NULL)
13510     {
13511       as_warn (_(".mask/.fmask outside of .ent"));
13512       demand_empty_rest_of_line ();
13513       return;
13514     }
13515
13516   if (get_absolute_expression_and_terminator (&mask) != ',')
13517     {
13518       as_warn (_("Bad .mask/.fmask directive"));
13519       --input_line_pointer;
13520       demand_empty_rest_of_line ();
13521       return;
13522     }
13523
13524   off = get_absolute_expression ();
13525
13526   if (reg_type == 'F')
13527     {
13528       cur_proc_ptr->fpreg_mask = mask;
13529       cur_proc_ptr->fpreg_offset = off;
13530     }
13531   else
13532     {
13533       cur_proc_ptr->reg_mask = mask;
13534       cur_proc_ptr->reg_offset = off;
13535     }
13536
13537   demand_empty_rest_of_line ();
13538 #else
13539   s_ignore (reg_type);
13540 #endif /* MIPS_STABS_ELF */
13541 }
13542
13543 /* The .loc directive.  */
13544
13545 #if 0
13546 static void
13547 s_loc (x)
13548      int x;
13549 {
13550   symbolS *symbolP;
13551   int lineno;
13552   int addroff;
13553
13554   assert (now_seg == text_section);
13555
13556   lineno = get_number ();
13557   addroff = frag_now_fix ();
13558
13559   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13560   S_SET_TYPE (symbolP, N_SLINE);
13561   S_SET_OTHER (symbolP, 0);
13562   S_SET_DESC (symbolP, lineno);
13563   symbolP->sy_segment = now_seg;
13564 }
13565 #endif
13566
13567 /* CPU name/ISA/number mapping table.
13568
13569    Entries are grouped by type.  The first matching CPU or ISA entry
13570    gets chosen by CPU or ISA, so it should be the 'canonical' name
13571    for that type.  Entries after that within the type are sorted
13572    alphabetically.
13573
13574    Case is ignored in comparison, so put the canonical entry in the
13575    appropriate case but everything else in lower case to ease eye pain.  */
13576 static const struct mips_cpu_info mips_cpu_info_table[] =
13577 {
13578   /* MIPS1 ISA */
13579   { "MIPS1",          1,      ISA_MIPS1,      CPU_R3000, },
13580   { "mips",           1,      ISA_MIPS1,      CPU_R3000, },
13581
13582   /* MIPS2 ISA */
13583   { "MIPS2",          1,      ISA_MIPS2,      CPU_R6000, },
13584
13585   /* MIPS3 ISA */
13586   { "MIPS3",          1,      ISA_MIPS3,      CPU_R4000, },
13587
13588   /* MIPS4 ISA */
13589   { "MIPS4",          1,      ISA_MIPS4,      CPU_R8000, },
13590
13591   /* MIPS5 ISA */
13592   { "MIPS5",          1,      ISA_MIPS5,      CPU_MIPS5, },
13593   { "Generic-MIPS5",  0,      ISA_MIPS5,      CPU_MIPS5, },
13594
13595   /* MIPS32 ISA */
13596   { "MIPS32",         1,      ISA_MIPS32,     CPU_MIPS32, },
13597   { "mipsisa32",      0,      ISA_MIPS32,     CPU_MIPS32, },
13598   { "Generic-MIPS32", 0,      ISA_MIPS32,     CPU_MIPS32, },
13599   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13600   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32, },
13601   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32, },
13602
13603   /* For historical reasons.  */
13604   { "MIPS64",         1,      ISA_MIPS3,      CPU_R4000, },
13605
13606   /* MIPS64 ISA */
13607   { "mipsisa64",      1,      ISA_MIPS64,     CPU_MIPS64, },
13608   { "Generic-MIPS64", 0,      ISA_MIPS64,     CPU_MIPS64, },
13609   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64, },
13610   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64, },
13611
13612   /* R2000 CPU */
13613   { "R2000",          0,      ISA_MIPS1,      CPU_R2000, },
13614   { "2000",           0,      ISA_MIPS1,      CPU_R2000, },
13615   { "2k",             0,      ISA_MIPS1,      CPU_R2000, },
13616   { "r2k",            0,      ISA_MIPS1,      CPU_R2000, },
13617
13618   /* R3000 CPU */
13619   { "R3000",          0,      ISA_MIPS1,      CPU_R3000, },
13620   { "3000",           0,      ISA_MIPS1,      CPU_R3000, },
13621   { "3k",             0,      ISA_MIPS1,      CPU_R3000, },
13622   { "r3k",            0,      ISA_MIPS1,      CPU_R3000, },
13623
13624   /* TX3900 CPU */
13625   { "R3900",          0,      ISA_MIPS1,      CPU_R3900, },
13626   { "3900",           0,      ISA_MIPS1,      CPU_R3900, },
13627   { "mipstx39",       0,      ISA_MIPS1,      CPU_R3900, },
13628
13629   /* R4000 CPU */
13630   { "R4000",          0,      ISA_MIPS3,      CPU_R4000, },
13631   { "4000",           0,      ISA_MIPS3,      CPU_R4000, },
13632   { "4k",             0,      ISA_MIPS3,      CPU_R4000, },   /* beware */
13633   { "r4k",            0,      ISA_MIPS3,      CPU_R4000, },
13634
13635   /* R4010 CPU */
13636   { "R4010",          0,      ISA_MIPS2,      CPU_R4010, },
13637   { "4010",           0,      ISA_MIPS2,      CPU_R4010, },
13638
13639   /* R4400 CPU */
13640   { "R4400",          0,      ISA_MIPS3,      CPU_R4400, },
13641   { "4400",           0,      ISA_MIPS3,      CPU_R4400, },
13642
13643   /* R4600 CPU */
13644   { "R4600",          0,      ISA_MIPS3,      CPU_R4600, },
13645   { "4600",           0,      ISA_MIPS3,      CPU_R4600, },
13646   { "mips64orion",    0,      ISA_MIPS3,      CPU_R4600, },
13647   { "orion",          0,      ISA_MIPS3,      CPU_R4600, },
13648
13649   /* R4650 CPU */
13650   { "R4650",          0,      ISA_MIPS3,      CPU_R4650, },
13651   { "4650",           0,      ISA_MIPS3,      CPU_R4650, },
13652
13653   /* R6000 CPU */
13654   { "R6000",          0,      ISA_MIPS2,      CPU_R6000, },
13655   { "6000",           0,      ISA_MIPS2,      CPU_R6000, },
13656   { "6k",             0,      ISA_MIPS2,      CPU_R6000, },
13657   { "r6k",            0,      ISA_MIPS2,      CPU_R6000, },
13658
13659   /* R8000 CPU */
13660   { "R8000",          0,      ISA_MIPS4,      CPU_R8000, },
13661   { "8000",           0,      ISA_MIPS4,      CPU_R8000, },
13662   { "8k",             0,      ISA_MIPS4,      CPU_R8000, },
13663   { "r8k",            0,      ISA_MIPS4,      CPU_R8000, },
13664
13665   /* R10000 CPU */
13666   { "R10000",         0,      ISA_MIPS4,      CPU_R10000, },
13667   { "10000",          0,      ISA_MIPS4,      CPU_R10000, },
13668   { "10k",            0,      ISA_MIPS4,      CPU_R10000, },
13669   { "r10k",           0,      ISA_MIPS4,      CPU_R10000, },
13670
13671   /* R12000 CPU */
13672   { "R12000",         0,      ISA_MIPS4,      CPU_R12000, },
13673   { "12000",          0,      ISA_MIPS4,      CPU_R12000, },
13674   { "12k",            0,      ISA_MIPS4,      CPU_R12000, },
13675   { "r12k",           0,      ISA_MIPS4,      CPU_R12000, },
13676
13677   /* VR4100 CPU */
13678   { "VR4100",         0,      ISA_MIPS3,      CPU_VR4100, },
13679   { "4100",           0,      ISA_MIPS3,      CPU_VR4100, },
13680   { "mips64vr4100",   0,      ISA_MIPS3,      CPU_VR4100, },
13681   { "r4100",          0,      ISA_MIPS3,      CPU_VR4100, },
13682
13683   /* VR4111 CPU */
13684   { "VR4111",         0,      ISA_MIPS3,      CPU_R4111, },
13685   { "4111",           0,      ISA_MIPS3,      CPU_R4111, },
13686   { "mips64vr4111",   0,      ISA_MIPS3,      CPU_R4111, },
13687   { "r4111",          0,      ISA_MIPS3,      CPU_R4111, },
13688
13689   /* VR4300 CPU */
13690   { "VR4300",         0,      ISA_MIPS3,      CPU_R4300, },
13691   { "4300",           0,      ISA_MIPS3,      CPU_R4300, },
13692   { "mips64vr4300",   0,      ISA_MIPS3,      CPU_R4300, },
13693   { "r4300",          0,      ISA_MIPS3,      CPU_R4300, },
13694
13695   /* VR5000 CPU */
13696   { "VR5000",         0,      ISA_MIPS4,      CPU_R5000, },
13697   { "5000",           0,      ISA_MIPS4,      CPU_R5000, },
13698   { "5k",             0,      ISA_MIPS4,      CPU_R5000, },
13699   { "mips64vr5000",   0,      ISA_MIPS4,      CPU_R5000, },
13700   { "r5000",          0,      ISA_MIPS4,      CPU_R5000, },
13701   { "r5200",          0,      ISA_MIPS4,      CPU_R5000, },
13702   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000, },
13703   { "r5230",          0,      ISA_MIPS4,      CPU_R5000, },
13704   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000, },
13705   { "r5231",          0,      ISA_MIPS4,      CPU_R5000, },
13706   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000, },
13707   { "r5261",          0,      ISA_MIPS4,      CPU_R5000, },
13708   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000, },
13709   { "r5721",          0,      ISA_MIPS4,      CPU_R5000, },
13710   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000, },
13711   { "r5k",            0,      ISA_MIPS4,      CPU_R5000, },
13712   { "r7000",          0,      ISA_MIPS4,      CPU_R5000, },
13713
13714   /* Broadcom SB-1 CPU */
13715   { "SB-1",           0,      ISA_MIPS64,     CPU_SB1, },
13716   { "sb-1250",        0,      ISA_MIPS64,     CPU_SB1, },
13717   { "sb1",            0,      ISA_MIPS64,     CPU_SB1, },
13718   { "sb1250",         0,      ISA_MIPS64,     CPU_SB1, },
13719
13720   /* End marker.  */
13721   { NULL, 0, 0, 0, },
13722 };
13723
13724 static const struct mips_cpu_info *
13725 mips_cpu_info_from_name (name)
13726      const char *name;
13727 {
13728   int i;
13729
13730   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13731     if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13732       return (&mips_cpu_info_table[i]);
13733
13734   return NULL;
13735 }
13736
13737 static const struct mips_cpu_info *
13738 mips_cpu_info_from_isa (isa)
13739      int isa;
13740 {
13741   int i;
13742
13743   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13744     if (mips_cpu_info_table[i].is_isa
13745       && isa == mips_cpu_info_table[i].isa)
13746       return (&mips_cpu_info_table[i]);
13747
13748   return NULL;
13749 }
13750
13751 static const struct mips_cpu_info *
13752 mips_cpu_info_from_cpu (cpu)
13753      int cpu;
13754 {
13755   int i;
13756
13757   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13758     if (!mips_cpu_info_table[i].is_isa
13759       && cpu == mips_cpu_info_table[i].cpu)
13760       return (&mips_cpu_info_table[i]);
13761
13762   return NULL;
13763 }