This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too.  */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT  1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP  28
95 #define SP  29
96 #define FP  30
97 #define RA  31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format.  */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section.  */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111                             ? ".data" \
112                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113                             ? ".rdata" \
114                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115                             ? ".rdata" \
116                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117                             ? ".rodata" \
118                             : (abort (), ""))
119
120 /* The ABI to use.  */
121 enum mips_abi_level
122 {
123   NO_ABI = 0,
124   O32_ABI,
125   O64_ABI,
126   N32_ABI,
127   N64_ABI,
128   EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file.  */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* This is the set of options which may be modified by the .set
135    pseudo-op.  We use a struct so that .set push and .set pop are more
136    reliable.  */
137
138 struct mips_set_options
139 {
140   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
141      if it has not been initialized.  Changed by `.set mipsN', and the
142      -mipsN command line option, and the default CPU.  */
143   int isa;
144   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
145      if they have not been initialized.  Changed by `.set <asename>', by
146      command line options, and based on the default architecture.  */
147   int ase_mips3d;
148   int ase_mdmx;
149   /* Whether we are assembling for the mips16 processor.  0 if we are
150      not, 1 if we are, and -1 if the value has not been initialized.
151      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152      -nomips16 command line options, and the default CPU.  */
153   int mips16;
154   /* Non-zero if we should not reorder instructions.  Changed by `.set
155      reorder' and `.set noreorder'.  */
156   int noreorder;
157   /* Non-zero if we should not permit the $at ($1) register to be used
158      in instructions.  Changed by `.set at' and `.set noat'.  */
159   int noat;
160   /* Non-zero if we should warn when a macro instruction expands into
161      more than one machine instruction.  Changed by `.set nomacro' and
162      `.set macro'.  */
163   int warn_about_macros;
164   /* Non-zero if we should not move instructions.  Changed by `.set
165      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
166   int nomove;
167   /* Non-zero if we should not optimize branches by moving the target
168      of the branch into the delay slot.  Actually, we don't perform
169      this optimization anyhow.  Changed by `.set bopt' and `.set
170      nobopt'.  */
171   int nobopt;
172   /* Non-zero if we should not autoextend mips16 instructions.
173      Changed by `.set autoextend' and `.set noautoextend'.  */
174   int noautoextend;
175   /* Restrict general purpose registers and floating point registers
176      to 32 bit.  This is initially determined when -mgp32 or -mfp32
177      is passed but can changed if the assembler code uses .set mipsN.  */
178   int gp32;
179   int fp32;
180 };
181
182 /* True if -mgp32 was passed.  */
183 static int file_mips_gp32 = -1;
184
185 /* True if -mfp32 was passed.  */
186 static int file_mips_fp32 = -1;
187
188 /* This is the struct we use to hold the current set of options.  Note
189    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190    -1 to indicate that they have not been initialized.  */
191
192 static struct mips_set_options mips_opts =
193 {
194   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
195 };
196
197 /* These variables are filled in with the masks of registers used.
198    The object format code reads them and puts them in the appropriate
199    place.  */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
202
203 /* MIPS ISA we are using for this output file.  */
204 static int file_mips_isa = ISA_UNKNOWN;
205
206 /* True if -mips16 was passed or implied by arguments passed on the
207    command line (e.g., by -march).  */
208 static int file_ase_mips16;
209
210 /* True if -mips3d was passed or implied by arguments passed on the
211    command line (e.g., by -march).  */
212 static int file_ase_mips3d;
213
214 /* True if -mdmx was passed or implied by arguments passed on the
215    command line (e.g., by -march).  */
216 static int file_ase_mdmx;
217
218 /* The argument of the -march= flag.  The architecture we are assembling.  */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
222
223 /* The argument of the -mtune= flag.  The architecture for which we
224    are optimizing.  */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
228
229 /* True when generating 32-bit code for a 64-bit processor.  */
230 static int mips_32bitmode = 0;
231
232 /* Some ISA's have delay slots for instructions which read or write
233    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236    delay slot in this ISA.  The uses of this macro assume that any
237    ISA that has delay slots for one of these, has them for all.  They
238    also assume that ISAs which don't have delays for these insns, don't
239    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
240 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
241    (ISA) == ISA_MIPS1                       \
242    || (ISA) == ISA_MIPS2                    \
243    || (ISA) == ISA_MIPS3                    \
244    )
245
246 /* True if the given ABI requires 32-bit registers.  */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249 /* Likewise 64-bit registers.  */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251   ((ABI) == N32_ABI               \
252    || (ABI) == N64_ABI            \
253    || (ABI) == O64_ABI)
254
255 /*  Return true if ISA supports 64 bit gp register instructions.  */
256 #define ISA_HAS_64BIT_REGS(ISA) (    \
257    (ISA) == ISA_MIPS3                \
258    || (ISA) == ISA_MIPS4             \
259    || (ISA) == ISA_MIPS5             \
260    || (ISA) == ISA_MIPS64            \
261    )
262
263 #define HAVE_32BIT_GPRS                            \
264     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
265
266 #define HAVE_32BIT_FPRS                            \
267     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
268
269 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
271
272 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
273
274 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
275
276 /* We can only have 64bit addresses if the object file format
277    supports it.  */
278 #define HAVE_32BIT_ADDRESSES                           \
279    (HAVE_32BIT_GPRS                                    \
280     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
281          || ! HAVE_64BIT_OBJECTS)                      \
282         && mips_pic != EMBEDDED_PIC))
283
284 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
285
286 /* Return true if the given CPU supports the MIPS16 ASE.  */
287 #define CPU_HAS_MIPS16(cpu)                            \
288    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0)
289
290 /* Return true if the given CPU supports the MIPS3D ASE.  */
291 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
292                                  )
293
294 /* Return true if the given CPU supports the MDMX ASE.  */
295 #define CPU_HAS_MDMX(cpu)       (false                 \
296                                  )
297
298 /* Whether the processor uses hardware interlocks to protect
299    reads from the HI and LO registers, and thus does not
300    require nops to be inserted.  */
301
302 #define hilo_interlocks (mips_arch == CPU_R4010                       \
303                          || mips_arch == CPU_SB1                      \
304                          )
305
306 /* Whether the processor uses hardware interlocks to protect reads
307    from the GPRs, and thus does not require nops to be inserted.  */
308 #define gpr_interlocks \
309   (mips_opts.isa != ISA_MIPS1  \
310    || mips_arch == CPU_R3900)
311
312 /* As with other "interlocks" this is used by hardware that has FP
313    (co-processor) interlocks.  */
314 /* Itbl support may require additional care here.  */
315 #define cop_interlocks (mips_arch == CPU_R4300                        \
316                         || mips_arch == CPU_SB1                       \
317                         )
318
319 /* Is this a mfhi or mflo instruction?  */
320 #define MF_HILO_INSN(PINFO) \
321           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
322
323 /* MIPS PIC level.  */
324
325 enum mips_pic_level
326 {
327   /* Do not generate PIC code.  */
328   NO_PIC,
329
330   /* Generate PIC code as in the SVR4 MIPS ABI.  */
331   SVR4_PIC,
332
333   /* Generate PIC code without using a global offset table: the data
334      segment has a maximum size of 64K, all data references are off
335      the $gp register, and all text references are PC relative.  This
336      is used on some embedded systems.  */
337   EMBEDDED_PIC
338 };
339
340 static enum mips_pic_level mips_pic;
341
342 /* Warn about all NOPS that the assembler generates.  */
343 static int warn_nops = 0;
344
345 /* 1 if we should generate 32 bit offsets from the $gp register in
346    SVR4_PIC mode.  Currently has no meaning in other modes.  */
347 static int mips_big_got = 0;
348
349 /* 1 if trap instructions should used for overflow rather than break
350    instructions.  */
351 static int mips_trap = 0;
352
353 /* 1 if double width floating point constants should not be constructed
354    by assembling two single width halves into two single width floating
355    point registers which just happen to alias the double width destination
356    register.  On some architectures this aliasing can be disabled by a bit
357    in the status register, and the setting of this bit cannot be determined
358    automatically at assemble time.  */
359 static int mips_disable_float_construction;
360
361 /* Non-zero if any .set noreorder directives were used.  */
362
363 static int mips_any_noreorder;
364
365 /* Non-zero if nops should be inserted when the register referenced in
366    an mfhi/mflo instruction is read in the next two instructions.  */
367 static int mips_7000_hilo_fix;
368
369 /* The size of the small data section.  */
370 static unsigned int g_switch_value = 8;
371 /* Whether the -G option was used.  */
372 static int g_switch_seen = 0;
373
374 #define N_RMASK 0xc4
375 #define N_VFP   0xd4
376
377 /* If we can determine in advance that GP optimization won't be
378    possible, we can skip the relaxation stuff that tries to produce
379    GP-relative references.  This makes delay slot optimization work
380    better.
381
382    This function can only provide a guess, but it seems to work for
383    gcc output.  It needs to guess right for gcc, otherwise gcc
384    will put what it thinks is a GP-relative instruction in a branch
385    delay slot.
386
387    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
388    fixed it for the non-PIC mode.  KR 95/04/07  */
389 static int nopic_need_relax PARAMS ((symbolS *, int));
390
391 /* handle of the OPCODE hash table */
392 static struct hash_control *op_hash = NULL;
393
394 /* The opcode hash table we use for the mips16.  */
395 static struct hash_control *mips16_op_hash = NULL;
396
397 /* This array holds the chars that always start a comment.  If the
398     pre-processor is disabled, these aren't very useful */
399 const char comment_chars[] = "#";
400
401 /* This array holds the chars that only start a comment at the beginning of
402    a line.  If the line seems to have the form '# 123 filename'
403    .line and .file directives will appear in the pre-processed output */
404 /* Note that input_file.c hand checks for '#' at the beginning of the
405    first line of the input file.  This is because the compiler outputs
406    #NO_APP at the beginning of its output.  */
407 /* Also note that C style comments are always supported.  */
408 const char line_comment_chars[] = "#";
409
410 /* This array holds machine specific line separator characters.  */
411 const char line_separator_chars[] = ";";
412
413 /* Chars that can be used to separate mant from exp in floating point nums */
414 const char EXP_CHARS[] = "eE";
415
416 /* Chars that mean this number is a floating point constant */
417 /* As in 0f12.456 */
418 /* or    0d1.2345e12 */
419 const char FLT_CHARS[] = "rRsSfFdDxXpP";
420
421 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
422    changed in read.c .  Ideally it shouldn't have to know about it at all,
423    but nothing is ideal around here.
424  */
425
426 static char *insn_error;
427
428 static int auto_align = 1;
429
430 /* When outputting SVR4 PIC code, the assembler needs to know the
431    offset in the stack frame from which to restore the $gp register.
432    This is set by the .cprestore pseudo-op, and saved in this
433    variable.  */
434 static offsetT mips_cprestore_offset = -1;
435
436 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
437    more optimizations, it can use a register value instead of a memory-saved
438    offset and even an other register than $gp as global pointer.  */
439 static offsetT mips_cpreturn_offset = -1;
440 static int mips_cpreturn_register = -1;
441 static int mips_gp_register = GP;
442 static int mips_gprel_offset = 0;
443
444 /* Whether mips_cprestore_offset has been set in the current function
445    (or whether it has already been warned about, if not).  */
446 static int mips_cprestore_valid = 0;
447
448 /* This is the register which holds the stack frame, as set by the
449    .frame pseudo-op.  This is needed to implement .cprestore.  */
450 static int mips_frame_reg = SP;
451
452 /* Whether mips_frame_reg has been set in the current function
453    (or whether it has already been warned about, if not).  */
454 static int mips_frame_reg_valid = 0;
455
456 /* To output NOP instructions correctly, we need to keep information
457    about the previous two instructions.  */
458
459 /* Whether we are optimizing.  The default value of 2 means to remove
460    unneeded NOPs and swap branch instructions when possible.  A value
461    of 1 means to not swap branches.  A value of 0 means to always
462    insert NOPs.  */
463 static int mips_optimize = 2;
464
465 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
466    equivalent to seeing no -g option at all.  */
467 static int mips_debug = 0;
468
469 /* The previous instruction.  */
470 static struct mips_cl_insn prev_insn;
471
472 /* The instruction before prev_insn.  */
473 static struct mips_cl_insn prev_prev_insn;
474
475 /* If we don't want information for prev_insn or prev_prev_insn, we
476    point the insn_mo field at this dummy integer.  */
477 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
478
479 /* Non-zero if prev_insn is valid.  */
480 static int prev_insn_valid;
481
482 /* The frag for the previous instruction.  */
483 static struct frag *prev_insn_frag;
484
485 /* The offset into prev_insn_frag for the previous instruction.  */
486 static long prev_insn_where;
487
488 /* The reloc type for the previous instruction, if any.  */
489 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
490
491 /* The reloc for the previous instruction, if any.  */
492 static fixS *prev_insn_fixp[3];
493
494 /* Non-zero if the previous instruction was in a delay slot.  */
495 static int prev_insn_is_delay_slot;
496
497 /* Non-zero if the previous instruction was in a .set noreorder.  */
498 static int prev_insn_unreordered;
499
500 /* Non-zero if the previous instruction uses an extend opcode (if
501    mips16).  */
502 static int prev_insn_extended;
503
504 /* Non-zero if the previous previous instruction was in a .set
505    noreorder.  */
506 static int prev_prev_insn_unreordered;
507
508 /* If this is set, it points to a frag holding nop instructions which
509    were inserted before the start of a noreorder section.  If those
510    nops turn out to be unnecessary, the size of the frag can be
511    decreased.  */
512 static fragS *prev_nop_frag;
513
514 /* The number of nop instructions we created in prev_nop_frag.  */
515 static int prev_nop_frag_holds;
516
517 /* The number of nop instructions that we know we need in
518    prev_nop_frag.  */
519 static int prev_nop_frag_required;
520
521 /* The number of instructions we've seen since prev_nop_frag.  */
522 static int prev_nop_frag_since;
523
524 /* For ECOFF and ELF, relocations against symbols are done in two
525    parts, with a HI relocation and a LO relocation.  Each relocation
526    has only 16 bits of space to store an addend.  This means that in
527    order for the linker to handle carries correctly, it must be able
528    to locate both the HI and the LO relocation.  This means that the
529    relocations must appear in order in the relocation table.
530
531    In order to implement this, we keep track of each unmatched HI
532    relocation.  We then sort them so that they immediately precede the
533    corresponding LO relocation.  */
534
535 struct mips_hi_fixup
536 {
537   /* Next HI fixup.  */
538   struct mips_hi_fixup *next;
539   /* This fixup.  */
540   fixS *fixp;
541   /* The section this fixup is in.  */
542   segT seg;
543 };
544
545 /* The list of unmatched HI relocs.  */
546
547 static struct mips_hi_fixup *mips_hi_fixup_list;
548
549 /* Map normal MIPS register numbers to mips16 register numbers.  */
550
551 #define X ILLEGAL_REG
552 static const int mips32_to_16_reg_map[] =
553 {
554   X, X, 2, 3, 4, 5, 6, 7,
555   X, X, X, X, X, X, X, X,
556   0, 1, X, X, X, X, X, X,
557   X, X, X, X, X, X, X, X
558 };
559 #undef X
560
561 /* Map mips16 register numbers to normal MIPS register numbers.  */
562
563 static const unsigned int mips16_to_32_reg_map[] =
564 {
565   16, 17, 2, 3, 4, 5, 6, 7
566 };
567 \f
568 /* Since the MIPS does not have multiple forms of PC relative
569    instructions, we do not have to do relaxing as is done on other
570    platforms.  However, we do have to handle GP relative addressing
571    correctly, which turns out to be a similar problem.
572
573    Every macro that refers to a symbol can occur in (at least) two
574    forms, one with GP relative addressing and one without.  For
575    example, loading a global variable into a register generally uses
576    a macro instruction like this:
577      lw $4,i
578    If i can be addressed off the GP register (this is true if it is in
579    the .sbss or .sdata section, or if it is known to be smaller than
580    the -G argument) this will generate the following instruction:
581      lw $4,i($gp)
582    This instruction will use a GPREL reloc.  If i can not be addressed
583    off the GP register, the following instruction sequence will be used:
584      lui $at,i
585      lw $4,i($at)
586    In this case the first instruction will have a HI16 reloc, and the
587    second reloc will have a LO16 reloc.  Both relocs will be against
588    the symbol i.
589
590    The issue here is that we may not know whether i is GP addressable
591    until after we see the instruction that uses it.  Therefore, we
592    want to be able to choose the final instruction sequence only at
593    the end of the assembly.  This is similar to the way other
594    platforms choose the size of a PC relative instruction only at the
595    end of assembly.
596
597    When generating position independent code we do not use GP
598    addressing in quite the same way, but the issue still arises as
599    external symbols and local symbols must be handled differently.
600
601    We handle these issues by actually generating both possible
602    instruction sequences.  The longer one is put in a frag_var with
603    type rs_machine_dependent.  We encode what to do with the frag in
604    the subtype field.  We encode (1) the number of existing bytes to
605    replace, (2) the number of new bytes to use, (3) the offset from
606    the start of the existing bytes to the first reloc we must generate
607    (that is, the offset is applied from the start of the existing
608    bytes after they are replaced by the new bytes, if any), (4) the
609    offset from the start of the existing bytes to the second reloc,
610    (5) whether a third reloc is needed (the third reloc is always four
611    bytes after the second reloc), and (6) whether to warn if this
612    variant is used (this is sometimes needed if .set nomacro or .set
613    noat is in effect).  All these numbers are reasonably small.
614
615    Generating two instruction sequences must be handled carefully to
616    ensure that delay slots are handled correctly.  Fortunately, there
617    are a limited number of cases.  When the second instruction
618    sequence is generated, append_insn is directed to maintain the
619    existing delay slot information, so it continues to apply to any
620    code after the second instruction sequence.  This means that the
621    second instruction sequence must not impose any requirements not
622    required by the first instruction sequence.
623
624    These variant frags are then handled in functions called by the
625    machine independent code.  md_estimate_size_before_relax returns
626    the final size of the frag.  md_convert_frag sets up the final form
627    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
628    one if needed.  */
629 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
630   ((relax_substateT) \
631    (((old) << 23) \
632     | ((new) << 16) \
633     | (((reloc1) + 64) << 9) \
634     | (((reloc2) + 64) << 2) \
635     | ((reloc3) ? (1 << 1) : 0) \
636     | ((warn) ? 1 : 0)))
637 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
638 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
639 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
640 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
641 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
642 #define RELAX_WARN(i) ((i) & 1)
643
644 /* For mips16 code, we use an entirely different form of relaxation.
645    mips16 supports two versions of most instructions which take
646    immediate values: a small one which takes some small value, and a
647    larger one which takes a 16 bit value.  Since branches also follow
648    this pattern, relaxing these values is required.
649
650    We can assemble both mips16 and normal MIPS code in a single
651    object.  Therefore, we need to support this type of relaxation at
652    the same time that we support the relaxation described above.  We
653    use the high bit of the subtype field to distinguish these cases.
654
655    The information we store for this type of relaxation is the
656    argument code found in the opcode file for this relocation, whether
657    the user explicitly requested a small or extended form, and whether
658    the relocation is in a jump or jal delay slot.  That tells us the
659    size of the value, and how it should be stored.  We also store
660    whether the fragment is considered to be extended or not.  We also
661    store whether this is known to be a branch to a different section,
662    whether we have tried to relax this frag yet, and whether we have
663    ever extended a PC relative fragment because of a shift count.  */
664 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
665   (0x80000000                                                   \
666    | ((type) & 0xff)                                            \
667    | ((small) ? 0x100 : 0)                                      \
668    | ((ext) ? 0x200 : 0)                                        \
669    | ((dslot) ? 0x400 : 0)                                      \
670    | ((jal_dslot) ? 0x800 : 0))
671 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
672 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
673 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
674 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
675 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
676 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
677 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
678 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
679 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
680 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
681 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
682 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
683 \f
684 /* Prototypes for static functions.  */
685
686 #ifdef __STDC__
687 #define internalError() \
688     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
689 #else
690 #define internalError() as_fatal (_("MIPS internal Error"));
691 #endif
692
693 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
694
695 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
696                                   unsigned int reg, enum mips_regclass class));
697 static int reg_needs_delay PARAMS ((unsigned int));
698 static void mips16_mark_labels PARAMS ((void));
699 static void append_insn PARAMS ((char *place,
700                                  struct mips_cl_insn * ip,
701                                  expressionS * p,
702                                  bfd_reloc_code_real_type *r,
703                                  boolean));
704 static void mips_no_prev_insn PARAMS ((int));
705 static void mips_emit_delays PARAMS ((boolean));
706 #ifdef USE_STDARG
707 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
708                                  const char *name, const char *fmt,
709                                  ...));
710 #else
711 static void macro_build ();
712 #endif
713 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
714                                         const char *, const char *,
715                                         va_list));
716 static void macro_build_jalr PARAMS ((int, expressionS *));
717 static void macro_build_lui PARAMS ((char *place, int *counter,
718                                      expressionS * ep, int regnum));
719 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
720 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
721                                          expressionS *));
722 static void load_register PARAMS ((int *, int, expressionS *, int));
723 static void load_address PARAMS ((int *, int, expressionS *, int *));
724 static void move_register PARAMS ((int *, int, int));
725 static void macro PARAMS ((struct mips_cl_insn * ip));
726 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
727 #ifdef LOSING_COMPILER
728 static void macro2 PARAMS ((struct mips_cl_insn * ip));
729 #endif
730 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
731 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
732 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
733                                   boolean, boolean, unsigned long *,
734                                   boolean *, unsigned short *));
735 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
736 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
737 static int my_getSmallExpression PARAMS ((expressionS *, char *));
738 static void my_getExpression PARAMS ((expressionS *, char *));
739 #ifdef OBJ_ELF
740 static int support_64bit_objects PARAMS((void));
741 #endif
742 static void mips_set_option_string PARAMS ((const char **, const char *));
743 static symbolS *get_symbol PARAMS ((void));
744 static void mips_align PARAMS ((int to, int fill, symbolS *label));
745 static void s_align PARAMS ((int));
746 static void s_change_sec PARAMS ((int));
747 static void s_change_section PARAMS ((int));
748 static void s_cons PARAMS ((int));
749 static void s_float_cons PARAMS ((int));
750 static void s_mips_globl PARAMS ((int));
751 static void s_option PARAMS ((int));
752 static void s_mipsset PARAMS ((int));
753 static void s_abicalls PARAMS ((int));
754 static void s_cpload PARAMS ((int));
755 static void s_cpsetup PARAMS ((int));
756 static void s_cplocal PARAMS ((int));
757 static void s_cprestore PARAMS ((int));
758 static void s_cpreturn PARAMS ((int));
759 static void s_gpvalue PARAMS ((int));
760 static void s_gpword PARAMS ((int));
761 static void s_cpadd PARAMS ((int));
762 static void s_insn PARAMS ((int));
763 static void md_obj_begin PARAMS ((void));
764 static void md_obj_end PARAMS ((void));
765 static long get_number PARAMS ((void));
766 static void s_mips_ent PARAMS ((int));
767 static void s_mips_end PARAMS ((int));
768 static void s_mips_frame PARAMS ((int));
769 static void s_mips_mask PARAMS ((int));
770 static void s_mips_stab PARAMS ((int));
771 static void s_mips_weakext PARAMS ((int));
772 static void s_mips_file PARAMS ((int));
773 static void s_mips_loc PARAMS ((int));
774 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
775 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
776 static void show PARAMS ((FILE *, const char *, int *, int *));
777 #ifdef OBJ_ELF
778 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
779 #endif
780
781 /* Return values of my_getSmallExpression().  */
782
783 enum small_ex_type
784 {
785   S_EX_NONE = 0,
786   S_EX_REGISTER,
787
788   /* Direct relocation creation by %percent_op().  */
789   S_EX_HALF,
790   S_EX_HI,
791   S_EX_LO,
792   S_EX_GP_REL,
793   S_EX_GOT,
794   S_EX_CALL16,
795   S_EX_GOT_DISP,
796   S_EX_GOT_PAGE,
797   S_EX_GOT_OFST,
798   S_EX_GOT_HI,
799   S_EX_GOT_LO,
800   S_EX_NEG,
801   S_EX_HIGHER,
802   S_EX_HIGHEST,
803   S_EX_CALL_HI,
804   S_EX_CALL_LO
805 };
806
807 /* Table and functions used to map between CPU/ISA names, and
808    ISA levels, and CPU numbers.  */
809
810 struct mips_cpu_info
811 {
812   const char *name;           /* CPU or ISA name.  */
813   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
814   int isa;                    /* ISA level.  */
815   int cpu;                    /* CPU number (default CPU if ISA).  */
816 };
817
818 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
819 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
820 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
821                                                         const char *));
822 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
823 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
824                                                            const char *));
825 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
826 \f
827 /* Pseudo-op table.
828
829    The following pseudo-ops from the Kane and Heinrich MIPS book
830    should be defined here, but are currently unsupported: .alias,
831    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
832
833    The following pseudo-ops from the Kane and Heinrich MIPS book are
834    specific to the type of debugging information being generated, and
835    should be defined by the object format: .aent, .begin, .bend,
836    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
837    .vreg.
838
839    The following pseudo-ops from the Kane and Heinrich MIPS book are
840    not MIPS CPU specific, but are also not specific to the object file
841    format.  This file is probably the best place to define them, but
842    they are not currently supported: .asm0, .endr, .lab, .repeat,
843    .struct.  */
844
845 static const pseudo_typeS mips_pseudo_table[] =
846 {
847   /* MIPS specific pseudo-ops.  */
848   {"option", s_option, 0},
849   {"set", s_mipsset, 0},
850   {"rdata", s_change_sec, 'r'},
851   {"sdata", s_change_sec, 's'},
852   {"livereg", s_ignore, 0},
853   {"abicalls", s_abicalls, 0},
854   {"cpload", s_cpload, 0},
855   {"cpsetup", s_cpsetup, 0},
856   {"cplocal", s_cplocal, 0},
857   {"cprestore", s_cprestore, 0},
858   {"cpreturn", s_cpreturn, 0},
859   {"gpvalue", s_gpvalue, 0},
860   {"gpword", s_gpword, 0},
861   {"cpadd", s_cpadd, 0},
862   {"insn", s_insn, 0},
863
864   /* Relatively generic pseudo-ops that happen to be used on MIPS
865      chips.  */
866   {"asciiz", stringer, 1},
867   {"bss", s_change_sec, 'b'},
868   {"err", s_err, 0},
869   {"half", s_cons, 1},
870   {"dword", s_cons, 3},
871   {"weakext", s_mips_weakext, 0},
872
873   /* These pseudo-ops are defined in read.c, but must be overridden
874      here for one reason or another.  */
875   {"align", s_align, 0},
876   {"byte", s_cons, 0},
877   {"data", s_change_sec, 'd'},
878   {"double", s_float_cons, 'd'},
879   {"float", s_float_cons, 'f'},
880   {"globl", s_mips_globl, 0},
881   {"global", s_mips_globl, 0},
882   {"hword", s_cons, 1},
883   {"int", s_cons, 2},
884   {"long", s_cons, 2},
885   {"octa", s_cons, 4},
886   {"quad", s_cons, 3},
887   {"section", s_change_section, 0},
888   {"short", s_cons, 1},
889   {"single", s_float_cons, 'f'},
890   {"stabn", s_mips_stab, 'n'},
891   {"text", s_change_sec, 't'},
892   {"word", s_cons, 2},
893
894   { "extern", ecoff_directive_extern, 0},
895
896   { NULL, NULL, 0 },
897 };
898
899 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
900 {
901   /* These pseudo-ops should be defined by the object file format.
902      However, a.out doesn't support them, so we have versions here.  */
903   {"aent", s_mips_ent, 1},
904   {"bgnb", s_ignore, 0},
905   {"end", s_mips_end, 0},
906   {"endb", s_ignore, 0},
907   {"ent", s_mips_ent, 0},
908   {"file", s_mips_file, 0},
909   {"fmask", s_mips_mask, 'F'},
910   {"frame", s_mips_frame, 0},
911   {"loc", s_mips_loc, 0},
912   {"mask", s_mips_mask, 'R'},
913   {"verstamp", s_ignore, 0},
914   { NULL, NULL, 0 },
915 };
916
917 extern void pop_insert PARAMS ((const pseudo_typeS *));
918
919 void
920 mips_pop_insert ()
921 {
922   pop_insert (mips_pseudo_table);
923   if (! ECOFF_DEBUGGING)
924     pop_insert (mips_nonecoff_pseudo_table);
925 }
926 \f
927 /* Symbols labelling the current insn.  */
928
929 struct insn_label_list
930 {
931   struct insn_label_list *next;
932   symbolS *label;
933 };
934
935 static struct insn_label_list *insn_labels;
936 static struct insn_label_list *free_insn_labels;
937
938 static void mips_clear_insn_labels PARAMS ((void));
939
940 static inline void
941 mips_clear_insn_labels ()
942 {
943   register struct insn_label_list **pl;
944
945   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
946     ;
947   *pl = insn_labels;
948   insn_labels = NULL;
949 }
950 \f
951 static char *expr_end;
952
953 /* Expressions which appear in instructions.  These are set by
954    mips_ip.  */
955
956 static expressionS imm_expr;
957 static expressionS offset_expr;
958
959 /* Relocs associated with imm_expr and offset_expr.  */
960
961 static bfd_reloc_code_real_type imm_reloc[3]
962   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
963 static bfd_reloc_code_real_type offset_reloc[3]
964   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
965
966 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
967
968 static boolean imm_unmatched_hi;
969
970 /* These are set by mips16_ip if an explicit extension is used.  */
971
972 static boolean mips16_small, mips16_ext;
973
974 /* The pdr segment for per procedure frame/regmask info.  Not used for
975    ECOFF debugging.  */
976
977 static segT pdr_seg;
978
979 /* The default target format to use.  */
980
981 const char *
982 mips_target_format ()
983 {
984   switch (OUTPUT_FLAVOR)
985     {
986     case bfd_target_aout_flavour:
987       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
988     case bfd_target_ecoff_flavour:
989       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
990     case bfd_target_coff_flavour:
991       return "pe-mips";
992     case bfd_target_elf_flavour:
993 #ifdef TE_TMIPS
994       /* This is traditional mips.  */
995       return (target_big_endian
996               ? (HAVE_64BIT_OBJECTS
997                  ? "elf64-tradbigmips"
998                  : (HAVE_NEWABI
999                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1000               : (HAVE_64BIT_OBJECTS
1001                  ? "elf64-tradlittlemips"
1002                  : (HAVE_NEWABI
1003                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1004 #else
1005       return (target_big_endian
1006               ? (HAVE_64BIT_OBJECTS
1007                  ? "elf64-bigmips"
1008                  : (HAVE_NEWABI
1009                     ? "elf32-nbigmips" : "elf32-bigmips"))
1010               : (HAVE_64BIT_OBJECTS
1011                  ? "elf64-littlemips"
1012                  : (HAVE_NEWABI
1013                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1014 #endif
1015     default:
1016       abort ();
1017       return NULL;
1018     }
1019 }
1020
1021 /* This function is called once, at assembler startup time.  It should
1022    set up all the tables, etc. that the MD part of the assembler will need.  */
1023
1024 void
1025 md_begin ()
1026 {
1027   register const char *retval = NULL;
1028   int i = 0;
1029   int broken = 0;
1030
1031   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1032     as_warn (_("Could not set architecture and machine"));
1033
1034   op_hash = hash_new ();
1035
1036   for (i = 0; i < NUMOPCODES;)
1037     {
1038       const char *name = mips_opcodes[i].name;
1039
1040       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1041       if (retval != NULL)
1042         {
1043           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1044                    mips_opcodes[i].name, retval);
1045           /* Probably a memory allocation problem?  Give up now.  */
1046           as_fatal (_("Broken assembler.  No assembly attempted."));
1047         }
1048       do
1049         {
1050           if (mips_opcodes[i].pinfo != INSN_MACRO)
1051             {
1052               if (!validate_mips_insn (&mips_opcodes[i]))
1053                 broken = 1;
1054             }
1055           ++i;
1056         }
1057       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1058     }
1059
1060   mips16_op_hash = hash_new ();
1061
1062   i = 0;
1063   while (i < bfd_mips16_num_opcodes)
1064     {
1065       const char *name = mips16_opcodes[i].name;
1066
1067       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1068       if (retval != NULL)
1069         as_fatal (_("internal: can't hash `%s': %s"),
1070                   mips16_opcodes[i].name, retval);
1071       do
1072         {
1073           if (mips16_opcodes[i].pinfo != INSN_MACRO
1074               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1075                   != mips16_opcodes[i].match))
1076             {
1077               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1078                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1079               broken = 1;
1080             }
1081           ++i;
1082         }
1083       while (i < bfd_mips16_num_opcodes
1084              && strcmp (mips16_opcodes[i].name, name) == 0);
1085     }
1086
1087   if (broken)
1088     as_fatal (_("Broken assembler.  No assembly attempted."));
1089
1090   /* We add all the general register names to the symbol table.  This
1091      helps us detect invalid uses of them.  */
1092   for (i = 0; i < 32; i++)
1093     {
1094       char buf[5];
1095
1096       sprintf (buf, "$%d", i);
1097       symbol_table_insert (symbol_new (buf, reg_section, i,
1098                                        &zero_address_frag));
1099     }
1100   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1101                                    &zero_address_frag));
1102   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1103                                    &zero_address_frag));
1104   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1105                                    &zero_address_frag));
1106   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1107                                    &zero_address_frag));
1108   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1109                                    &zero_address_frag));
1110   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1111                                    &zero_address_frag));
1112   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1113                                    &zero_address_frag));
1114   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1115                                    &zero_address_frag));
1116   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1117                                    &zero_address_frag));
1118
1119   mips_no_prev_insn (false);
1120
1121   mips_gprmask = 0;
1122   mips_cprmask[0] = 0;
1123   mips_cprmask[1] = 0;
1124   mips_cprmask[2] = 0;
1125   mips_cprmask[3] = 0;
1126
1127   /* set the default alignment for the text section (2**2) */
1128   record_alignment (text_section, 2);
1129
1130   if (USE_GLOBAL_POINTER_OPT)
1131     bfd_set_gp_size (stdoutput, g_switch_value);
1132
1133   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1134     {
1135       /* On a native system, sections must be aligned to 16 byte
1136          boundaries.  When configured for an embedded ELF target, we
1137          don't bother.  */
1138       if (strcmp (TARGET_OS, "elf") != 0)
1139         {
1140           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1141           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1142           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1143         }
1144
1145       /* Create a .reginfo section for register masks and a .mdebug
1146          section for debugging information.  */
1147       {
1148         segT seg;
1149         subsegT subseg;
1150         flagword flags;
1151         segT sec;
1152
1153         seg = now_seg;
1154         subseg = now_subseg;
1155
1156         /* The ABI says this section should be loaded so that the
1157            running program can access it.  However, we don't load it
1158            if we are configured for an embedded target */
1159         flags = SEC_READONLY | SEC_DATA;
1160         if (strcmp (TARGET_OS, "elf") != 0)
1161           flags |= SEC_ALLOC | SEC_LOAD;
1162
1163         if (mips_abi != N64_ABI)
1164           {
1165             sec = subseg_new (".reginfo", (subsegT) 0);
1166
1167             bfd_set_section_flags (stdoutput, sec, flags);
1168             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1169
1170 #ifdef OBJ_ELF
1171             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1172 #endif
1173           }
1174         else
1175           {
1176             /* The 64-bit ABI uses a .MIPS.options section rather than
1177                .reginfo section.  */
1178             sec = subseg_new (".MIPS.options", (subsegT) 0);
1179             bfd_set_section_flags (stdoutput, sec, flags);
1180             bfd_set_section_alignment (stdoutput, sec, 3);
1181
1182 #ifdef OBJ_ELF
1183             /* Set up the option header.  */
1184             {
1185               Elf_Internal_Options opthdr;
1186               char *f;
1187
1188               opthdr.kind = ODK_REGINFO;
1189               opthdr.size = (sizeof (Elf_External_Options)
1190                              + sizeof (Elf64_External_RegInfo));
1191               opthdr.section = 0;
1192               opthdr.info = 0;
1193               f = frag_more (sizeof (Elf_External_Options));
1194               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1195                                              (Elf_External_Options *) f);
1196
1197               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1198             }
1199 #endif
1200           }
1201
1202         if (ECOFF_DEBUGGING)
1203           {
1204             sec = subseg_new (".mdebug", (subsegT) 0);
1205             (void) bfd_set_section_flags (stdoutput, sec,
1206                                           SEC_HAS_CONTENTS | SEC_READONLY);
1207             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1208           }
1209 #ifdef OBJ_ELF
1210         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1211           {
1212             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1213             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1214                                           SEC_READONLY | SEC_RELOC
1215                                           | SEC_DEBUGGING);
1216             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1217           }
1218 #endif
1219
1220         subseg_set (seg, subseg);
1221       }
1222     }
1223
1224   if (! ECOFF_DEBUGGING)
1225     md_obj_begin ();
1226 }
1227
1228 void
1229 md_mips_end ()
1230 {
1231   if (! ECOFF_DEBUGGING)
1232     md_obj_end ();
1233 }
1234
1235 void
1236 md_assemble (str)
1237      char *str;
1238 {
1239   struct mips_cl_insn insn;
1240   bfd_reloc_code_real_type unused_reloc[3]
1241     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1242
1243   imm_expr.X_op = O_absent;
1244   imm_unmatched_hi = false;
1245   offset_expr.X_op = O_absent;
1246   imm_reloc[0] = BFD_RELOC_UNUSED;
1247   imm_reloc[1] = BFD_RELOC_UNUSED;
1248   imm_reloc[2] = BFD_RELOC_UNUSED;
1249   offset_reloc[0] = BFD_RELOC_UNUSED;
1250   offset_reloc[1] = BFD_RELOC_UNUSED;
1251   offset_reloc[2] = BFD_RELOC_UNUSED;
1252
1253   if (mips_opts.mips16)
1254     mips16_ip (str, &insn);
1255   else
1256     {
1257       mips_ip (str, &insn);
1258       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1259             str, insn.insn_opcode));
1260     }
1261
1262   if (insn_error)
1263     {
1264       as_bad ("%s `%s'", insn_error, str);
1265       return;
1266     }
1267
1268   if (insn.insn_mo->pinfo == INSN_MACRO)
1269     {
1270       if (mips_opts.mips16)
1271         mips16_macro (&insn);
1272       else
1273         macro (&insn);
1274     }
1275   else
1276     {
1277       if (imm_expr.X_op != O_absent)
1278         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1279       else if (offset_expr.X_op != O_absent)
1280         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1281       else
1282         append_insn (NULL, &insn, NULL, unused_reloc, false);
1283     }
1284 }
1285
1286 /* See whether instruction IP reads register REG.  CLASS is the type
1287    of register.  */
1288
1289 static int
1290 insn_uses_reg (ip, reg, class)
1291      struct mips_cl_insn *ip;
1292      unsigned int reg;
1293      enum mips_regclass class;
1294 {
1295   if (class == MIPS16_REG)
1296     {
1297       assert (mips_opts.mips16);
1298       reg = mips16_to_32_reg_map[reg];
1299       class = MIPS_GR_REG;
1300     }
1301
1302   /* Don't report on general register ZERO, since it never changes.  */
1303   if (class == MIPS_GR_REG && reg == ZERO)
1304     return 0;
1305
1306   if (class == MIPS_FP_REG)
1307     {
1308       assert (! mips_opts.mips16);
1309       /* If we are called with either $f0 or $f1, we must check $f0.
1310          This is not optimal, because it will introduce an unnecessary
1311          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1312          need to distinguish reading both $f0 and $f1 or just one of
1313          them.  Note that we don't have to check the other way,
1314          because there is no instruction that sets both $f0 and $f1
1315          and requires a delay.  */
1316       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1317           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1318               == (reg &~ (unsigned) 1)))
1319         return 1;
1320       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1321           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1322               == (reg &~ (unsigned) 1)))
1323         return 1;
1324     }
1325   else if (! mips_opts.mips16)
1326     {
1327       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1328           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1329         return 1;
1330       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1331           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1332         return 1;
1333     }
1334   else
1335     {
1336       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1337           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1338                                     & MIPS16OP_MASK_RX)]
1339               == reg))
1340         return 1;
1341       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1342           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1343                                     & MIPS16OP_MASK_RY)]
1344               == reg))
1345         return 1;
1346       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1347           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1348                                     & MIPS16OP_MASK_MOVE32Z)]
1349               == reg))
1350         return 1;
1351       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1352         return 1;
1353       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1354         return 1;
1355       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1356         return 1;
1357       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1358           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1359               & MIPS16OP_MASK_REGR32) == reg)
1360         return 1;
1361     }
1362
1363   return 0;
1364 }
1365
1366 /* This function returns true if modifying a register requires a
1367    delay.  */
1368
1369 static int
1370 reg_needs_delay (reg)
1371      unsigned int reg;
1372 {
1373   unsigned long prev_pinfo;
1374
1375   prev_pinfo = prev_insn.insn_mo->pinfo;
1376   if (! mips_opts.noreorder
1377       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1378       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1379           || (! gpr_interlocks
1380               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1381     {
1382       /* A load from a coprocessor or from memory.  All load
1383          delays delay the use of general register rt for one
1384          instruction on the r3000.  The r6000 and r4000 use
1385          interlocks.  */
1386       /* Itbl support may require additional care here.  */
1387       know (prev_pinfo & INSN_WRITE_GPR_T);
1388       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1389         return 1;
1390     }
1391
1392   return 0;
1393 }
1394
1395 /* Mark instruction labels in mips16 mode.  This permits the linker to
1396    handle them specially, such as generating jalx instructions when
1397    needed.  We also make them odd for the duration of the assembly, in
1398    order to generate the right sort of code.  We will make them even
1399    in the adjust_symtab routine, while leaving them marked.  This is
1400    convenient for the debugger and the disassembler.  The linker knows
1401    to make them odd again.  */
1402
1403 static void
1404 mips16_mark_labels ()
1405 {
1406   if (mips_opts.mips16)
1407     {
1408       struct insn_label_list *l;
1409       valueT val;
1410
1411       for (l = insn_labels; l != NULL; l = l->next)
1412         {
1413 #ifdef OBJ_ELF
1414           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1415             S_SET_OTHER (l->label, STO_MIPS16);
1416 #endif
1417           val = S_GET_VALUE (l->label);
1418           if ((val & 1) == 0)
1419             S_SET_VALUE (l->label, val + 1);
1420         }
1421     }
1422 }
1423
1424 /* Output an instruction.  PLACE is where to put the instruction; if
1425    it is NULL, this uses frag_more to get room.  IP is the instruction
1426    information.  ADDRESS_EXPR is an operand of the instruction to be
1427    used with RELOC_TYPE.  */
1428
1429 static void
1430 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1431      char *place;
1432      struct mips_cl_insn *ip;
1433      expressionS *address_expr;
1434      bfd_reloc_code_real_type *reloc_type;
1435      boolean unmatched_hi;
1436 {
1437   register unsigned long prev_pinfo, pinfo;
1438   char *f;
1439   fixS *fixp[3];
1440   int nops = 0;
1441
1442   /* Mark instruction labels in mips16 mode.  */
1443   mips16_mark_labels ();
1444
1445   prev_pinfo = prev_insn.insn_mo->pinfo;
1446   pinfo = ip->insn_mo->pinfo;
1447
1448   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1449     {
1450       int prev_prev_nop;
1451
1452       /* If the previous insn required any delay slots, see if we need
1453          to insert a NOP or two.  There are eight kinds of possible
1454          hazards, of which an instruction can have at most one type.
1455          (1) a load from memory delay
1456          (2) a load from a coprocessor delay
1457          (3) an unconditional branch delay
1458          (4) a conditional branch delay
1459          (5) a move to coprocessor register delay
1460          (6) a load coprocessor register from memory delay
1461          (7) a coprocessor condition code delay
1462          (8) a HI/LO special register delay
1463
1464          There are a lot of optimizations we could do that we don't.
1465          In particular, we do not, in general, reorder instructions.
1466          If you use gcc with optimization, it will reorder
1467          instructions and generally do much more optimization then we
1468          do here; repeating all that work in the assembler would only
1469          benefit hand written assembly code, and does not seem worth
1470          it.  */
1471
1472       /* This is how a NOP is emitted.  */
1473 #define emit_nop()                                      \
1474   (mips_opts.mips16                                     \
1475    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1476    : md_number_to_chars (frag_more (4), 0, 4))
1477
1478       /* The previous insn might require a delay slot, depending upon
1479          the contents of the current insn.  */
1480       if (! mips_opts.mips16
1481           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1482           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1483                && ! cop_interlocks)
1484               || (! gpr_interlocks
1485                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1486         {
1487           /* A load from a coprocessor or from memory.  All load
1488              delays delay the use of general register rt for one
1489              instruction on the r3000.  The r6000 and r4000 use
1490              interlocks.  */
1491           /* Itbl support may require additional care here.  */
1492           know (prev_pinfo & INSN_WRITE_GPR_T);
1493           if (mips_optimize == 0
1494               || insn_uses_reg (ip,
1495                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1496                                  & OP_MASK_RT),
1497                                 MIPS_GR_REG))
1498             ++nops;
1499         }
1500       else if (! mips_opts.mips16
1501                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1502                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1503                     && ! cop_interlocks)
1504                    || (mips_opts.isa == ISA_MIPS1
1505                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1506         {
1507           /* A generic coprocessor delay.  The previous instruction
1508              modified a coprocessor general or control register.  If
1509              it modified a control register, we need to avoid any
1510              coprocessor instruction (this is probably not always
1511              required, but it sometimes is).  If it modified a general
1512              register, we avoid using that register.
1513
1514              On the r6000 and r4000 loading a coprocessor register
1515              from memory is interlocked, and does not require a delay.
1516
1517              This case is not handled very well.  There is no special
1518              knowledge of CP0 handling, and the coprocessors other
1519              than the floating point unit are not distinguished at
1520              all.  */
1521           /* Itbl support may require additional care here. FIXME!
1522              Need to modify this to include knowledge about
1523              user specified delays!  */
1524           if (prev_pinfo & INSN_WRITE_FPR_T)
1525             {
1526               if (mips_optimize == 0
1527                   || insn_uses_reg (ip,
1528                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1529                                      & OP_MASK_FT),
1530                                     MIPS_FP_REG))
1531                 ++nops;
1532             }
1533           else if (prev_pinfo & INSN_WRITE_FPR_S)
1534             {
1535               if (mips_optimize == 0
1536                   || insn_uses_reg (ip,
1537                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1538                                      & OP_MASK_FS),
1539                                     MIPS_FP_REG))
1540                 ++nops;
1541             }
1542           else
1543             {
1544               /* We don't know exactly what the previous instruction
1545                  does.  If the current instruction uses a coprocessor
1546                  register, we must insert a NOP.  If previous
1547                  instruction may set the condition codes, and the
1548                  current instruction uses them, we must insert two
1549                  NOPS.  */
1550               /* Itbl support may require additional care here.  */
1551               if (mips_optimize == 0
1552                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1553                       && (pinfo & INSN_READ_COND_CODE)))
1554                 nops += 2;
1555               else if (pinfo & INSN_COP)
1556                 ++nops;
1557             }
1558         }
1559       else if (! mips_opts.mips16
1560                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1561                && (prev_pinfo & INSN_WRITE_COND_CODE)
1562                && ! cop_interlocks)
1563         {
1564           /* The previous instruction sets the coprocessor condition
1565              codes, but does not require a general coprocessor delay
1566              (this means it is a floating point comparison
1567              instruction).  If this instruction uses the condition
1568              codes, we need to insert a single NOP.  */
1569           /* Itbl support may require additional care here.  */
1570           if (mips_optimize == 0
1571               || (pinfo & INSN_READ_COND_CODE))
1572             ++nops;
1573         }
1574
1575       /* If we're fixing up mfhi/mflo for the r7000 and the
1576          previous insn was an mfhi/mflo and the current insn
1577          reads the register that the mfhi/mflo wrote to, then
1578          insert two nops.  */
1579
1580       else if (mips_7000_hilo_fix
1581                && MF_HILO_INSN (prev_pinfo)
1582                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1583                                       & OP_MASK_RD),
1584                                  MIPS_GR_REG))
1585         {
1586           nops += 2;
1587         }
1588
1589       /* If we're fixing up mfhi/mflo for the r7000 and the
1590          2nd previous insn was an mfhi/mflo and the current insn
1591          reads the register that the mfhi/mflo wrote to, then
1592          insert one nop.  */
1593
1594       else if (mips_7000_hilo_fix
1595                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1596                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1597                                        & OP_MASK_RD),
1598                                     MIPS_GR_REG))
1599
1600         {
1601           ++nops;
1602         }
1603
1604       else if (prev_pinfo & INSN_READ_LO)
1605         {
1606           /* The previous instruction reads the LO register; if the
1607              current instruction writes to the LO register, we must
1608              insert two NOPS.  Some newer processors have interlocks.
1609              Also the tx39's multiply instructions can be exectuted
1610              immediatly after a read from HI/LO (without the delay),
1611              though the tx39's divide insns still do require the
1612              delay.  */
1613           if (! (hilo_interlocks
1614                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1615               && (mips_optimize == 0
1616                   || (pinfo & INSN_WRITE_LO)))
1617             nops += 2;
1618           /* Most mips16 branch insns don't have a delay slot.
1619              If a read from LO is immediately followed by a branch
1620              to a write to LO we have a read followed by a write
1621              less than 2 insns away.  We assume the target of
1622              a branch might be a write to LO, and insert a nop
1623              between a read and an immediately following branch.  */
1624           else if (mips_opts.mips16
1625                    && (mips_optimize == 0
1626                        || (pinfo & MIPS16_INSN_BRANCH)))
1627             ++nops;
1628         }
1629       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1630         {
1631           /* The previous instruction reads the HI register; if the
1632              current instruction writes to the HI register, we must
1633              insert a NOP.  Some newer processors have interlocks.
1634              Also the note tx39's multiply above.  */
1635           if (! (hilo_interlocks
1636                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1637               && (mips_optimize == 0
1638                   || (pinfo & INSN_WRITE_HI)))
1639             nops += 2;
1640           /* Most mips16 branch insns don't have a delay slot.
1641              If a read from HI is immediately followed by a branch
1642              to a write to HI we have a read followed by a write
1643              less than 2 insns away.  We assume the target of
1644              a branch might be a write to HI, and insert a nop
1645              between a read and an immediately following branch.  */
1646           else if (mips_opts.mips16
1647                    && (mips_optimize == 0
1648                        || (pinfo & MIPS16_INSN_BRANCH)))
1649             ++nops;
1650         }
1651
1652       /* If the previous instruction was in a noreorder section, then
1653          we don't want to insert the nop after all.  */
1654       /* Itbl support may require additional care here.  */
1655       if (prev_insn_unreordered)
1656         nops = 0;
1657
1658       /* There are two cases which require two intervening
1659          instructions: 1) setting the condition codes using a move to
1660          coprocessor instruction which requires a general coprocessor
1661          delay and then reading the condition codes 2) reading the HI
1662          or LO register and then writing to it (except on processors
1663          which have interlocks).  If we are not already emitting a NOP
1664          instruction, we must check for these cases compared to the
1665          instruction previous to the previous instruction.  */
1666       if ((! mips_opts.mips16
1667            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1668            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1669            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1670            && (pinfo & INSN_READ_COND_CODE)
1671            && ! cop_interlocks)
1672           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1673               && (pinfo & INSN_WRITE_LO)
1674               && ! (hilo_interlocks
1675                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1676           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1677               && (pinfo & INSN_WRITE_HI)
1678               && ! (hilo_interlocks
1679                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1680         prev_prev_nop = 1;
1681       else
1682         prev_prev_nop = 0;
1683
1684       if (prev_prev_insn_unreordered)
1685         prev_prev_nop = 0;
1686
1687       if (prev_prev_nop && nops == 0)
1688         ++nops;
1689
1690       /* If we are being given a nop instruction, don't bother with
1691          one of the nops we would otherwise output.  This will only
1692          happen when a nop instruction is used with mips_optimize set
1693          to 0.  */
1694       if (nops > 0
1695           && ! mips_opts.noreorder
1696           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1697         --nops;
1698
1699       /* Now emit the right number of NOP instructions.  */
1700       if (nops > 0 && ! mips_opts.noreorder)
1701         {
1702           fragS *old_frag;
1703           unsigned long old_frag_offset;
1704           int i;
1705           struct insn_label_list *l;
1706
1707           old_frag = frag_now;
1708           old_frag_offset = frag_now_fix ();
1709
1710           for (i = 0; i < nops; i++)
1711             emit_nop ();
1712
1713           if (listing)
1714             {
1715               listing_prev_line ();
1716               /* We may be at the start of a variant frag.  In case we
1717                  are, make sure there is enough space for the frag
1718                  after the frags created by listing_prev_line.  The
1719                  argument to frag_grow here must be at least as large
1720                  as the argument to all other calls to frag_grow in
1721                  this file.  We don't have to worry about being in the
1722                  middle of a variant frag, because the variants insert
1723                  all needed nop instructions themselves.  */
1724               frag_grow (40);
1725             }
1726
1727           for (l = insn_labels; l != NULL; l = l->next)
1728             {
1729               valueT val;
1730
1731               assert (S_GET_SEGMENT (l->label) == now_seg);
1732               symbol_set_frag (l->label, frag_now);
1733               val = (valueT) frag_now_fix ();
1734               /* mips16 text labels are stored as odd.  */
1735               if (mips_opts.mips16)
1736                 ++val;
1737               S_SET_VALUE (l->label, val);
1738             }
1739
1740 #ifndef NO_ECOFF_DEBUGGING
1741           if (ECOFF_DEBUGGING)
1742             ecoff_fix_loc (old_frag, old_frag_offset);
1743 #endif
1744         }
1745       else if (prev_nop_frag != NULL)
1746         {
1747           /* We have a frag holding nops we may be able to remove.  If
1748              we don't need any nops, we can decrease the size of
1749              prev_nop_frag by the size of one instruction.  If we do
1750              need some nops, we count them in prev_nops_required.  */
1751           if (prev_nop_frag_since == 0)
1752             {
1753               if (nops == 0)
1754                 {
1755                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1756                   --prev_nop_frag_holds;
1757                 }
1758               else
1759                 prev_nop_frag_required += nops;
1760             }
1761           else
1762             {
1763               if (prev_prev_nop == 0)
1764                 {
1765                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1766                   --prev_nop_frag_holds;
1767                 }
1768               else
1769                 ++prev_nop_frag_required;
1770             }
1771
1772           if (prev_nop_frag_holds <= prev_nop_frag_required)
1773             prev_nop_frag = NULL;
1774
1775           ++prev_nop_frag_since;
1776
1777           /* Sanity check: by the time we reach the second instruction
1778              after prev_nop_frag, we should have used up all the nops
1779              one way or another.  */
1780           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1781         }
1782     }
1783
1784   if (*reloc_type > BFD_RELOC_UNUSED)
1785     {
1786       /* We need to set up a variant frag.  */
1787       assert (mips_opts.mips16 && address_expr != NULL);
1788       f = frag_var (rs_machine_dependent, 4, 0,
1789                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1790                                          mips16_small, mips16_ext,
1791                                          (prev_pinfo
1792                                           & INSN_UNCOND_BRANCH_DELAY),
1793                                          (*prev_insn_reloc_type
1794                                           == BFD_RELOC_MIPS16_JMP)),
1795                     make_expr_symbol (address_expr), 0, NULL);
1796     }
1797   else if (place != NULL)
1798     f = place;
1799   else if (mips_opts.mips16
1800            && ! ip->use_extend
1801            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1802     {
1803       /* Make sure there is enough room to swap this instruction with
1804          a following jump instruction.  */
1805       frag_grow (6);
1806       f = frag_more (2);
1807     }
1808   else
1809     {
1810       if (mips_opts.mips16
1811           && mips_opts.noreorder
1812           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1813         as_warn (_("extended instruction in delay slot"));
1814
1815       f = frag_more (4);
1816     }
1817
1818   fixp[0] = fixp[1] = fixp[2] = NULL;
1819   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1820     {
1821       if (address_expr->X_op == O_constant)
1822         {
1823           valueT tmp;
1824
1825           switch (*reloc_type)
1826             {
1827             case BFD_RELOC_32:
1828               ip->insn_opcode |= address_expr->X_add_number;
1829               break;
1830
1831             case BFD_RELOC_MIPS_HIGHEST:
1832               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1833               tmp >>= 16;
1834               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1835               break;
1836
1837             case BFD_RELOC_MIPS_HIGHER:
1838               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1839               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1840               break;
1841
1842             case BFD_RELOC_HI16_S:
1843               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1844                                   >> 16) & 0xffff;
1845               break;
1846
1847             case BFD_RELOC_HI16:
1848               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1849               break;
1850
1851             case BFD_RELOC_LO16:
1852               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1853               break;
1854
1855             case BFD_RELOC_MIPS_JMP:
1856               if ((address_expr->X_add_number & 3) != 0)
1857                 as_bad (_("jump to misaligned address (0x%lx)"),
1858                         (unsigned long) address_expr->X_add_number);
1859               if (address_expr->X_add_number & ~0xfffffff
1860                   || address_expr->X_add_number > 0x7fffffc)
1861                 as_bad (_("jump address range overflow (0x%lx)"),
1862                         (unsigned long) address_expr->X_add_number);
1863               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1864               break;
1865
1866             case BFD_RELOC_MIPS16_JMP:
1867               if ((address_expr->X_add_number & 3) != 0)
1868                 as_bad (_("jump to misaligned address (0x%lx)"),
1869                         (unsigned long) address_expr->X_add_number);
1870               if (address_expr->X_add_number & ~0xfffffff
1871                   || address_expr->X_add_number > 0x7fffffc)
1872                 as_bad (_("jump address range overflow (0x%lx)"),
1873                         (unsigned long) address_expr->X_add_number);
1874               ip->insn_opcode |=
1875                 (((address_expr->X_add_number & 0x7c0000) << 3)
1876                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1877                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1878               break;
1879
1880             case BFD_RELOC_16_PCREL:
1881               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1882               break;
1883
1884             case BFD_RELOC_16_PCREL_S2:
1885               goto need_reloc;
1886
1887             default:
1888               internalError ();
1889             }
1890         }
1891       else
1892         {
1893         need_reloc:
1894           /* Don't generate a reloc if we are writing into a variant frag.  */
1895           if (place == NULL)
1896             {
1897               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1898                                      address_expr,
1899                                      (*reloc_type == BFD_RELOC_16_PCREL
1900                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
1901                                      reloc_type[0]);
1902
1903               /* These relocations can have an addend that won't fit in
1904                  4 octets for 64bit assembly.  */
1905               if (HAVE_64BIT_GPRS &&
1906                   (*reloc_type == BFD_RELOC_16
1907                    || *reloc_type == BFD_RELOC_32
1908                    || *reloc_type == BFD_RELOC_MIPS_JMP
1909                    || *reloc_type == BFD_RELOC_HI16_S
1910                    || *reloc_type == BFD_RELOC_LO16
1911                    || *reloc_type == BFD_RELOC_GPREL16
1912                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
1913                    || *reloc_type == BFD_RELOC_GPREL32
1914                    || *reloc_type == BFD_RELOC_64
1915                    || *reloc_type == BFD_RELOC_CTOR
1916                    || *reloc_type == BFD_RELOC_MIPS_SUB
1917                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1918                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
1919                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1920                    || *reloc_type == BFD_RELOC_MIPS_REL16
1921                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1922                 fixp[0]->fx_no_overflow = 1;
1923
1924               if (unmatched_hi)
1925                 {
1926                   struct mips_hi_fixup *hi_fixup;
1927
1928                   assert (*reloc_type == BFD_RELOC_HI16_S);
1929                   hi_fixup = ((struct mips_hi_fixup *)
1930                               xmalloc (sizeof (struct mips_hi_fixup)));
1931                   hi_fixup->fixp = fixp[0];
1932                   hi_fixup->seg = now_seg;
1933                   hi_fixup->next = mips_hi_fixup_list;
1934                   mips_hi_fixup_list = hi_fixup;
1935                 }
1936
1937               if (reloc_type[1] != BFD_RELOC_UNUSED)
1938                 {
1939                   /* FIXME: This symbol can be one of
1940                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
1941                   address_expr->X_op = O_absent;
1942                   address_expr->X_add_symbol = 0;
1943                   address_expr->X_add_number = 0;
1944
1945                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1946                                          4, address_expr, false,
1947                                          reloc_type[1]);
1948
1949                   /* These relocations can have an addend that won't fit in
1950                      4 octets for 64bit assembly.  */
1951                   if (HAVE_64BIT_GPRS &&
1952                       (*reloc_type == BFD_RELOC_16
1953                        || *reloc_type == BFD_RELOC_32
1954                        || *reloc_type == BFD_RELOC_MIPS_JMP
1955                        || *reloc_type == BFD_RELOC_HI16_S
1956                        || *reloc_type == BFD_RELOC_LO16
1957                        || *reloc_type == BFD_RELOC_GPREL16
1958                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
1959                        || *reloc_type == BFD_RELOC_GPREL32
1960                        || *reloc_type == BFD_RELOC_64
1961                        || *reloc_type == BFD_RELOC_CTOR
1962                        || *reloc_type == BFD_RELOC_MIPS_SUB
1963                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1964                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
1965                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1966                        || *reloc_type == BFD_RELOC_MIPS_REL16
1967                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1968                     fixp[1]->fx_no_overflow = 1;
1969
1970                   if (reloc_type[2] != BFD_RELOC_UNUSED)
1971                     {
1972                       address_expr->X_op = O_absent;
1973                       address_expr->X_add_symbol = 0;
1974                       address_expr->X_add_number = 0;
1975
1976                       fixp[2] = fix_new_exp (frag_now,
1977                                              f - frag_now->fr_literal, 4,
1978                                              address_expr, false,
1979                                              reloc_type[2]);
1980
1981                       /* These relocations can have an addend that won't fit in
1982                          4 octets for 64bit assembly.  */
1983                       if (HAVE_64BIT_GPRS &&
1984                           (*reloc_type == BFD_RELOC_16
1985                            || *reloc_type == BFD_RELOC_32
1986                            || *reloc_type == BFD_RELOC_MIPS_JMP
1987                            || *reloc_type == BFD_RELOC_HI16_S
1988                            || *reloc_type == BFD_RELOC_LO16
1989                            || *reloc_type == BFD_RELOC_GPREL16
1990                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
1991                            || *reloc_type == BFD_RELOC_GPREL32
1992                            || *reloc_type == BFD_RELOC_64
1993                            || *reloc_type == BFD_RELOC_CTOR
1994                            || *reloc_type == BFD_RELOC_MIPS_SUB
1995                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1996                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
1997                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1998                            || *reloc_type == BFD_RELOC_MIPS_REL16
1999                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2000                         fixp[2]->fx_no_overflow = 1;
2001                     }
2002                 }
2003             }
2004         }
2005     }
2006
2007   if (! mips_opts.mips16)
2008     {
2009       md_number_to_chars (f, ip->insn_opcode, 4);
2010 #ifdef OBJ_ELF
2011       dwarf2_emit_insn (4);
2012 #endif
2013     }
2014   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2015     {
2016       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2017       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2018 #ifdef OBJ_ELF
2019       dwarf2_emit_insn (4);
2020 #endif
2021     }
2022   else
2023     {
2024       if (ip->use_extend)
2025         {
2026           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2027           f += 2;
2028         }
2029       md_number_to_chars (f, ip->insn_opcode, 2);
2030 #ifdef OBJ_ELF
2031       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2032 #endif
2033     }
2034
2035   /* Update the register mask information.  */
2036   if (! mips_opts.mips16)
2037     {
2038       if (pinfo & INSN_WRITE_GPR_D)
2039         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2040       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2041         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2042       if (pinfo & INSN_READ_GPR_S)
2043         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2044       if (pinfo & INSN_WRITE_GPR_31)
2045         mips_gprmask |= 1 << RA;
2046       if (pinfo & INSN_WRITE_FPR_D)
2047         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2048       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2049         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2050       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2051         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2052       if ((pinfo & INSN_READ_FPR_R) != 0)
2053         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2054       if (pinfo & INSN_COP)
2055         {
2056           /* We don't keep enough information to sort these cases out.
2057              The itbl support does keep this information however, although
2058              we currently don't support itbl fprmats as part of the cop
2059              instruction.  May want to add this support in the future.  */
2060         }
2061       /* Never set the bit for $0, which is always zero.  */
2062       mips_gprmask &= ~1 << 0;
2063     }
2064   else
2065     {
2066       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2067         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2068                               & MIPS16OP_MASK_RX);
2069       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2070         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2071                               & MIPS16OP_MASK_RY);
2072       if (pinfo & MIPS16_INSN_WRITE_Z)
2073         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2074                               & MIPS16OP_MASK_RZ);
2075       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2076         mips_gprmask |= 1 << TREG;
2077       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2078         mips_gprmask |= 1 << SP;
2079       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2080         mips_gprmask |= 1 << RA;
2081       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2082         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2083       if (pinfo & MIPS16_INSN_READ_Z)
2084         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2085                               & MIPS16OP_MASK_MOVE32Z);
2086       if (pinfo & MIPS16_INSN_READ_GPR_X)
2087         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2088                               & MIPS16OP_MASK_REGR32);
2089     }
2090
2091   if (place == NULL && ! mips_opts.noreorder)
2092     {
2093       /* Filling the branch delay slot is more complex.  We try to
2094          switch the branch with the previous instruction, which we can
2095          do if the previous instruction does not set up a condition
2096          that the branch tests and if the branch is not itself the
2097          target of any branch.  */
2098       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2099           || (pinfo & INSN_COND_BRANCH_DELAY))
2100         {
2101           if (mips_optimize < 2
2102               /* If we have seen .set volatile or .set nomove, don't
2103                  optimize.  */
2104               || mips_opts.nomove != 0
2105               /* If we had to emit any NOP instructions, then we
2106                  already know we can not swap.  */
2107               || nops != 0
2108               /* If we don't even know the previous insn, we can not
2109                  swap.  */
2110               || ! prev_insn_valid
2111               /* If the previous insn is already in a branch delay
2112                  slot, then we can not swap.  */
2113               || prev_insn_is_delay_slot
2114               /* If the previous previous insn was in a .set
2115                  noreorder, we can't swap.  Actually, the MIPS
2116                  assembler will swap in this situation.  However, gcc
2117                  configured -with-gnu-as will generate code like
2118                    .set noreorder
2119                    lw   $4,XXX
2120                    .set reorder
2121                    INSN
2122                    bne  $4,$0,foo
2123                  in which we can not swap the bne and INSN.  If gcc is
2124                  not configured -with-gnu-as, it does not output the
2125                  .set pseudo-ops.  We don't have to check
2126                  prev_insn_unreordered, because prev_insn_valid will
2127                  be 0 in that case.  We don't want to use
2128                  prev_prev_insn_valid, because we do want to be able
2129                  to swap at the start of a function.  */
2130               || prev_prev_insn_unreordered
2131               /* If the branch is itself the target of a branch, we
2132                  can not swap.  We cheat on this; all we check for is
2133                  whether there is a label on this instruction.  If
2134                  there are any branches to anything other than a
2135                  label, users must use .set noreorder.  */
2136               || insn_labels != NULL
2137               /* If the previous instruction is in a variant frag, we
2138                  can not do the swap.  This does not apply to the
2139                  mips16, which uses variant frags for different
2140                  purposes.  */
2141               || (! mips_opts.mips16
2142                   && prev_insn_frag->fr_type == rs_machine_dependent)
2143               /* If the branch reads the condition codes, we don't
2144                  even try to swap, because in the sequence
2145                    ctc1 $X,$31
2146                    INSN
2147                    INSN
2148                    bc1t LABEL
2149                  we can not swap, and I don't feel like handling that
2150                  case.  */
2151               || (! mips_opts.mips16
2152                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2153                   && (pinfo & INSN_READ_COND_CODE))
2154               /* We can not swap with an instruction that requires a
2155                  delay slot, becase the target of the branch might
2156                  interfere with that instruction.  */
2157               || (! mips_opts.mips16
2158                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2159                   && (prev_pinfo
2160               /* Itbl support may require additional care here.  */
2161                       & (INSN_LOAD_COPROC_DELAY
2162                          | INSN_COPROC_MOVE_DELAY
2163                          | INSN_WRITE_COND_CODE)))
2164               || (! (hilo_interlocks
2165                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2166                   && (prev_pinfo
2167                       & (INSN_READ_LO
2168                          | INSN_READ_HI)))
2169               || (! mips_opts.mips16
2170                   && ! gpr_interlocks
2171                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2172               || (! mips_opts.mips16
2173                   && mips_opts.isa == ISA_MIPS1
2174                   /* Itbl support may require additional care here.  */
2175                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2176               /* We can not swap with a branch instruction.  */
2177               || (prev_pinfo
2178                   & (INSN_UNCOND_BRANCH_DELAY
2179                      | INSN_COND_BRANCH_DELAY
2180                      | INSN_COND_BRANCH_LIKELY))
2181               /* We do not swap with a trap instruction, since it
2182                  complicates trap handlers to have the trap
2183                  instruction be in a delay slot.  */
2184               || (prev_pinfo & INSN_TRAP)
2185               /* If the branch reads a register that the previous
2186                  instruction sets, we can not swap.  */
2187               || (! mips_opts.mips16
2188                   && (prev_pinfo & INSN_WRITE_GPR_T)
2189                   && insn_uses_reg (ip,
2190                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2191                                      & OP_MASK_RT),
2192                                     MIPS_GR_REG))
2193               || (! mips_opts.mips16
2194                   && (prev_pinfo & INSN_WRITE_GPR_D)
2195                   && insn_uses_reg (ip,
2196                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2197                                      & OP_MASK_RD),
2198                                     MIPS_GR_REG))
2199               || (mips_opts.mips16
2200                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2201                        && insn_uses_reg (ip,
2202                                          ((prev_insn.insn_opcode
2203                                            >> MIPS16OP_SH_RX)
2204                                           & MIPS16OP_MASK_RX),
2205                                          MIPS16_REG))
2206                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2207                           && insn_uses_reg (ip,
2208                                             ((prev_insn.insn_opcode
2209                                               >> MIPS16OP_SH_RY)
2210                                              & MIPS16OP_MASK_RY),
2211                                             MIPS16_REG))
2212                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2213                           && insn_uses_reg (ip,
2214                                             ((prev_insn.insn_opcode
2215                                               >> MIPS16OP_SH_RZ)
2216                                              & MIPS16OP_MASK_RZ),
2217                                             MIPS16_REG))
2218                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2219                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2220                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2221                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2222                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2223                           && insn_uses_reg (ip,
2224                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2225                                                                      insn_opcode),
2226                                             MIPS_GR_REG))))
2227               /* If the branch writes a register that the previous
2228                  instruction sets, we can not swap (we know that
2229                  branches write only to RD or to $31).  */
2230               || (! mips_opts.mips16
2231                   && (prev_pinfo & INSN_WRITE_GPR_T)
2232                   && (((pinfo & INSN_WRITE_GPR_D)
2233                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2234                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2235                       || ((pinfo & INSN_WRITE_GPR_31)
2236                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2237                                & OP_MASK_RT)
2238                               == RA))))
2239               || (! mips_opts.mips16
2240                   && (prev_pinfo & INSN_WRITE_GPR_D)
2241                   && (((pinfo & INSN_WRITE_GPR_D)
2242                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2243                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2244                       || ((pinfo & INSN_WRITE_GPR_31)
2245                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2246                                & OP_MASK_RD)
2247                               == RA))))
2248               || (mips_opts.mips16
2249                   && (pinfo & MIPS16_INSN_WRITE_31)
2250                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2251                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2252                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2253                               == RA))))
2254               /* If the branch writes a register that the previous
2255                  instruction reads, we can not swap (we know that
2256                  branches only write to RD or to $31).  */
2257               || (! mips_opts.mips16
2258                   && (pinfo & INSN_WRITE_GPR_D)
2259                   && insn_uses_reg (&prev_insn,
2260                                     ((ip->insn_opcode >> OP_SH_RD)
2261                                      & OP_MASK_RD),
2262                                     MIPS_GR_REG))
2263               || (! mips_opts.mips16
2264                   && (pinfo & INSN_WRITE_GPR_31)
2265                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2266               || (mips_opts.mips16
2267                   && (pinfo & MIPS16_INSN_WRITE_31)
2268                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2269               /* If we are generating embedded PIC code, the branch
2270                  might be expanded into a sequence which uses $at, so
2271                  we can't swap with an instruction which reads it.  */
2272               || (mips_pic == EMBEDDED_PIC
2273                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2274               /* If the previous previous instruction has a load
2275                  delay, and sets a register that the branch reads, we
2276                  can not swap.  */
2277               || (! mips_opts.mips16
2278                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2279               /* Itbl support may require additional care here.  */
2280                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2281                       || (! gpr_interlocks
2282                           && (prev_prev_insn.insn_mo->pinfo
2283                               & INSN_LOAD_MEMORY_DELAY)))
2284                   && insn_uses_reg (ip,
2285                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2286                                      & OP_MASK_RT),
2287                                     MIPS_GR_REG))
2288               /* If one instruction sets a condition code and the
2289                  other one uses a condition code, we can not swap.  */
2290               || ((pinfo & INSN_READ_COND_CODE)
2291                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2292               || ((pinfo & INSN_WRITE_COND_CODE)
2293                   && (prev_pinfo & INSN_READ_COND_CODE))
2294               /* If the previous instruction uses the PC, we can not
2295                  swap.  */
2296               || (mips_opts.mips16
2297                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2298               /* If the previous instruction was extended, we can not
2299                  swap.  */
2300               || (mips_opts.mips16 && prev_insn_extended)
2301               /* If the previous instruction had a fixup in mips16
2302                  mode, we can not swap.  This normally means that the
2303                  previous instruction was a 4 byte branch anyhow.  */
2304               || (mips_opts.mips16 && prev_insn_fixp[0])
2305               /* If the previous instruction is a sync, sync.l, or
2306                  sync.p, we can not swap.  */
2307               || (prev_pinfo & INSN_SYNC))
2308             {
2309               /* We could do even better for unconditional branches to
2310                  portions of this object file; we could pick up the
2311                  instruction at the destination, put it in the delay
2312                  slot, and bump the destination address.  */
2313               emit_nop ();
2314               /* Update the previous insn information.  */
2315               prev_prev_insn = *ip;
2316               prev_insn.insn_mo = &dummy_opcode;
2317             }
2318           else
2319             {
2320               /* It looks like we can actually do the swap.  */
2321               if (! mips_opts.mips16)
2322                 {
2323                   char *prev_f;
2324                   char temp[4];
2325
2326                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2327                   memcpy (temp, prev_f, 4);
2328                   memcpy (prev_f, f, 4);
2329                   memcpy (f, temp, 4);
2330                   if (prev_insn_fixp[0])
2331                     {
2332                       prev_insn_fixp[0]->fx_frag = frag_now;
2333                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2334                     }
2335                   if (prev_insn_fixp[1])
2336                     {
2337                       prev_insn_fixp[1]->fx_frag = frag_now;
2338                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2339                     }
2340                   if (prev_insn_fixp[2])
2341                     {
2342                       prev_insn_fixp[2]->fx_frag = frag_now;
2343                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2344                     }
2345                   if (fixp[0])
2346                     {
2347                       fixp[0]->fx_frag = prev_insn_frag;
2348                       fixp[0]->fx_where = prev_insn_where;
2349                     }
2350                   if (fixp[1])
2351                     {
2352                       fixp[1]->fx_frag = prev_insn_frag;
2353                       fixp[1]->fx_where = prev_insn_where;
2354                     }
2355                   if (fixp[2])
2356                     {
2357                       fixp[2]->fx_frag = prev_insn_frag;
2358                       fixp[2]->fx_where = prev_insn_where;
2359                     }
2360                 }
2361               else
2362                 {
2363                   char *prev_f;
2364                   char temp[2];
2365
2366                   assert (prev_insn_fixp[0] == NULL);
2367                   assert (prev_insn_fixp[1] == NULL);
2368                   assert (prev_insn_fixp[2] == NULL);
2369                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2370                   memcpy (temp, prev_f, 2);
2371                   memcpy (prev_f, f, 2);
2372                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2373                     {
2374                       assert (*reloc_type == BFD_RELOC_UNUSED);
2375                       memcpy (f, temp, 2);
2376                     }
2377                   else
2378                     {
2379                       memcpy (f, f + 2, 2);
2380                       memcpy (f + 2, temp, 2);
2381                     }
2382                   if (fixp[0])
2383                     {
2384                       fixp[0]->fx_frag = prev_insn_frag;
2385                       fixp[0]->fx_where = prev_insn_where;
2386                     }
2387                   if (fixp[1])
2388                     {
2389                       fixp[1]->fx_frag = prev_insn_frag;
2390                       fixp[1]->fx_where = prev_insn_where;
2391                     }
2392                   if (fixp[2])
2393                     {
2394                       fixp[2]->fx_frag = prev_insn_frag;
2395                       fixp[2]->fx_where = prev_insn_where;
2396                     }
2397                 }
2398
2399               /* Update the previous insn information; leave prev_insn
2400                  unchanged.  */
2401               prev_prev_insn = *ip;
2402             }
2403           prev_insn_is_delay_slot = 1;
2404
2405           /* If that was an unconditional branch, forget the previous
2406              insn information.  */
2407           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2408             {
2409               prev_prev_insn.insn_mo = &dummy_opcode;
2410               prev_insn.insn_mo = &dummy_opcode;
2411             }
2412
2413           prev_insn_fixp[0] = NULL;
2414           prev_insn_fixp[1] = NULL;
2415           prev_insn_fixp[2] = NULL;
2416           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2417           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2418           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2419           prev_insn_extended = 0;
2420         }
2421       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2422         {
2423           /* We don't yet optimize a branch likely.  What we should do
2424              is look at the target, copy the instruction found there
2425              into the delay slot, and increment the branch to jump to
2426              the next instruction.  */
2427           emit_nop ();
2428           /* Update the previous insn information.  */
2429           prev_prev_insn = *ip;
2430           prev_insn.insn_mo = &dummy_opcode;
2431           prev_insn_fixp[0] = NULL;
2432           prev_insn_fixp[1] = NULL;
2433           prev_insn_fixp[2] = NULL;
2434           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2435           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2436           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2437           prev_insn_extended = 0;
2438         }
2439       else
2440         {
2441           /* Update the previous insn information.  */
2442           if (nops > 0)
2443             prev_prev_insn.insn_mo = &dummy_opcode;
2444           else
2445             prev_prev_insn = prev_insn;
2446           prev_insn = *ip;
2447
2448           /* Any time we see a branch, we always fill the delay slot
2449              immediately; since this insn is not a branch, we know it
2450              is not in a delay slot.  */
2451           prev_insn_is_delay_slot = 0;
2452
2453           prev_insn_fixp[0] = fixp[0];
2454           prev_insn_fixp[1] = fixp[1];
2455           prev_insn_fixp[2] = fixp[2];
2456           prev_insn_reloc_type[0] = reloc_type[0];
2457           prev_insn_reloc_type[1] = reloc_type[1];
2458           prev_insn_reloc_type[2] = reloc_type[2];
2459           if (mips_opts.mips16)
2460             prev_insn_extended = (ip->use_extend
2461                                   || *reloc_type > BFD_RELOC_UNUSED);
2462         }
2463
2464       prev_prev_insn_unreordered = prev_insn_unreordered;
2465       prev_insn_unreordered = 0;
2466       prev_insn_frag = frag_now;
2467       prev_insn_where = f - frag_now->fr_literal;
2468       prev_insn_valid = 1;
2469     }
2470   else if (place == NULL)
2471     {
2472       /* We need to record a bit of information even when we are not
2473          reordering, in order to determine the base address for mips16
2474          PC relative relocs.  */
2475       prev_prev_insn = prev_insn;
2476       prev_insn = *ip;
2477       prev_insn_reloc_type[0] = reloc_type[0];
2478       prev_insn_reloc_type[1] = reloc_type[1];
2479       prev_insn_reloc_type[2] = reloc_type[2];
2480       prev_prev_insn_unreordered = prev_insn_unreordered;
2481       prev_insn_unreordered = 1;
2482     }
2483
2484   /* We just output an insn, so the next one doesn't have a label.  */
2485   mips_clear_insn_labels ();
2486
2487   /* We must ensure that a fixup associated with an unmatched %hi
2488      reloc does not become a variant frag.  Otherwise, the
2489      rearrangement of %hi relocs in frob_file may confuse
2490      tc_gen_reloc.  */
2491   if (unmatched_hi)
2492     {
2493       frag_wane (frag_now);
2494       frag_new (0);
2495     }
2496 }
2497
2498 /* This function forgets that there was any previous instruction or
2499    label.  If PRESERVE is non-zero, it remembers enough information to
2500    know whether nops are needed before a noreorder section.  */
2501
2502 static void
2503 mips_no_prev_insn (preserve)
2504      int preserve;
2505 {
2506   if (! preserve)
2507     {
2508       prev_insn.insn_mo = &dummy_opcode;
2509       prev_prev_insn.insn_mo = &dummy_opcode;
2510       prev_nop_frag = NULL;
2511       prev_nop_frag_holds = 0;
2512       prev_nop_frag_required = 0;
2513       prev_nop_frag_since = 0;
2514     }
2515   prev_insn_valid = 0;
2516   prev_insn_is_delay_slot = 0;
2517   prev_insn_unreordered = 0;
2518   prev_insn_extended = 0;
2519   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2520   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2521   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2522   prev_prev_insn_unreordered = 0;
2523   mips_clear_insn_labels ();
2524 }
2525
2526 /* This function must be called whenever we turn on noreorder or emit
2527    something other than instructions.  It inserts any NOPS which might
2528    be needed by the previous instruction, and clears the information
2529    kept for the previous instructions.  The INSNS parameter is true if
2530    instructions are to follow.  */
2531
2532 static void
2533 mips_emit_delays (insns)
2534      boolean insns;
2535 {
2536   if (! mips_opts.noreorder)
2537     {
2538       int nops;
2539
2540       nops = 0;
2541       if ((! mips_opts.mips16
2542            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2543            && (! cop_interlocks
2544                && (prev_insn.insn_mo->pinfo
2545                    & (INSN_LOAD_COPROC_DELAY
2546                       | INSN_COPROC_MOVE_DELAY
2547                       | INSN_WRITE_COND_CODE))))
2548           || (! hilo_interlocks
2549               && (prev_insn.insn_mo->pinfo
2550                   & (INSN_READ_LO
2551                      | INSN_READ_HI)))
2552           || (! mips_opts.mips16
2553               && ! gpr_interlocks
2554               && (prev_insn.insn_mo->pinfo
2555                   & INSN_LOAD_MEMORY_DELAY))
2556           || (! mips_opts.mips16
2557               && mips_opts.isa == ISA_MIPS1
2558               && (prev_insn.insn_mo->pinfo
2559                   & INSN_COPROC_MEMORY_DELAY)))
2560         {
2561           /* Itbl support may require additional care here.  */
2562           ++nops;
2563           if ((! mips_opts.mips16
2564                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2565                && (! cop_interlocks
2566                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2567               || (! hilo_interlocks
2568                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2569                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2570             ++nops;
2571
2572           if (prev_insn_unreordered)
2573             nops = 0;
2574         }
2575       else if ((! mips_opts.mips16
2576                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2577                 && (! cop_interlocks
2578                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2579                || (! hilo_interlocks
2580                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2581                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2582         {
2583           /* Itbl support may require additional care here.  */
2584           if (! prev_prev_insn_unreordered)
2585             ++nops;
2586         }
2587
2588       if (nops > 0)
2589         {
2590           struct insn_label_list *l;
2591
2592           if (insns)
2593             {
2594               /* Record the frag which holds the nop instructions, so
2595                  that we can remove them if we don't need them.  */
2596               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2597               prev_nop_frag = frag_now;
2598               prev_nop_frag_holds = nops;
2599               prev_nop_frag_required = 0;
2600               prev_nop_frag_since = 0;
2601             }
2602
2603           for (; nops > 0; --nops)
2604             emit_nop ();
2605
2606           if (insns)
2607             {
2608               /* Move on to a new frag, so that it is safe to simply
2609                  decrease the size of prev_nop_frag.  */
2610               frag_wane (frag_now);
2611               frag_new (0);
2612             }
2613
2614           for (l = insn_labels; l != NULL; l = l->next)
2615             {
2616               valueT val;
2617
2618               assert (S_GET_SEGMENT (l->label) == now_seg);
2619               symbol_set_frag (l->label, frag_now);
2620               val = (valueT) frag_now_fix ();
2621               /* mips16 text labels are stored as odd.  */
2622               if (mips_opts.mips16)
2623                 ++val;
2624               S_SET_VALUE (l->label, val);
2625             }
2626         }
2627     }
2628
2629   /* Mark instruction labels in mips16 mode.  */
2630   if (insns)
2631     mips16_mark_labels ();
2632
2633   mips_no_prev_insn (insns);
2634 }
2635
2636 /* Build an instruction created by a macro expansion.  This is passed
2637    a pointer to the count of instructions created so far, an
2638    expression, the name of the instruction to build, an operand format
2639    string, and corresponding arguments.  */
2640
2641 #ifdef USE_STDARG
2642 static void
2643 macro_build (char *place,
2644              int *counter,
2645              expressionS * ep,
2646              const char *name,
2647              const char *fmt,
2648              ...)
2649 #else
2650 static void
2651 macro_build (place, counter, ep, name, fmt, va_alist)
2652      char *place;
2653      int *counter;
2654      expressionS *ep;
2655      const char *name;
2656      const char *fmt;
2657      va_dcl
2658 #endif
2659 {
2660   struct mips_cl_insn insn;
2661   bfd_reloc_code_real_type r[3];
2662   va_list args;
2663
2664 #ifdef USE_STDARG
2665   va_start (args, fmt);
2666 #else
2667   va_start (args);
2668 #endif
2669
2670   /*
2671    * If the macro is about to expand into a second instruction,
2672    * print a warning if needed. We need to pass ip as a parameter
2673    * to generate a better warning message here...
2674    */
2675   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2676     as_warn (_("Macro instruction expanded into multiple instructions"));
2677
2678   /*
2679    * If the macro is about to expand into a second instruction,
2680    * and it is in a delay slot, print a warning.
2681    */
2682   if (place == NULL
2683       && *counter == 1
2684       && mips_opts.noreorder
2685       && (prev_prev_insn.insn_mo->pinfo
2686           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2687              | INSN_COND_BRANCH_LIKELY)) != 0)
2688     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2689
2690   if (place == NULL)
2691     ++*counter;         /* bump instruction counter */
2692
2693   if (mips_opts.mips16)
2694     {
2695       mips16_macro_build (place, counter, ep, name, fmt, args);
2696       va_end (args);
2697       return;
2698     }
2699
2700   r[0] = BFD_RELOC_UNUSED;
2701   r[1] = BFD_RELOC_UNUSED;
2702   r[2] = BFD_RELOC_UNUSED;
2703   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2704   assert (insn.insn_mo);
2705   assert (strcmp (name, insn.insn_mo->name) == 0);
2706
2707   /* Search until we get a match for NAME.  */
2708   while (1)
2709     {
2710       /* It is assumed here that macros will never generate 
2711          MDMX or MIPS-3D instructions.  */
2712       if (strcmp (fmt, insn.insn_mo->args) == 0
2713           && insn.insn_mo->pinfo != INSN_MACRO
2714           && OPCODE_IS_MEMBER (insn.insn_mo,
2715                                (mips_opts.isa
2716                                 | (mips_opts.mips16 ? INSN_MIPS16 : 0)),
2717                                mips_arch)
2718           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2719         break;
2720
2721       ++insn.insn_mo;
2722       assert (insn.insn_mo->name);
2723       assert (strcmp (name, insn.insn_mo->name) == 0);
2724     }
2725
2726   insn.insn_opcode = insn.insn_mo->match;
2727   for (;;)
2728     {
2729       switch (*fmt++)
2730         {
2731         case '\0':
2732           break;
2733
2734         case ',':
2735         case '(':
2736         case ')':
2737           continue;
2738
2739         case 't':
2740         case 'w':
2741         case 'E':
2742           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2743           continue;
2744
2745         case 'c':
2746           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2747           continue;
2748
2749         case 'T':
2750         case 'W':
2751           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2752           continue;
2753
2754         case 'd':
2755         case 'G':
2756           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2757           continue;
2758
2759         case 'U':
2760           {
2761             int tmp = va_arg (args, int);
2762
2763             insn.insn_opcode |= tmp << OP_SH_RT;
2764             insn.insn_opcode |= tmp << OP_SH_RD;
2765             continue;
2766           }
2767
2768         case 'V':
2769         case 'S':
2770           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2771           continue;
2772
2773         case 'z':
2774           continue;
2775
2776         case '<':
2777           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2778           continue;
2779
2780         case 'D':
2781           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2782           continue;
2783
2784         case 'B':
2785           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2786           continue;
2787
2788         case 'J':
2789           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2790           continue;
2791
2792         case 'q':
2793           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2794           continue;
2795
2796         case 'b':
2797         case 's':
2798         case 'r':
2799         case 'v':
2800           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2801           continue;
2802
2803         case 'i':
2804         case 'j':
2805         case 'o':
2806           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2807           assert (*r == BFD_RELOC_GPREL16
2808                   || *r == BFD_RELOC_MIPS_LITERAL
2809                   || *r == BFD_RELOC_MIPS_HIGHER
2810                   || *r == BFD_RELOC_HI16_S
2811                   || *r == BFD_RELOC_LO16
2812                   || *r == BFD_RELOC_MIPS_GOT16
2813                   || *r == BFD_RELOC_MIPS_CALL16
2814                   || *r == BFD_RELOC_MIPS_GOT_DISP
2815                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2816                   || *r == BFD_RELOC_MIPS_GOT_OFST
2817                   || *r == BFD_RELOC_MIPS_GOT_LO16
2818                   || *r == BFD_RELOC_MIPS_CALL_LO16
2819                   || (ep->X_op == O_subtract
2820                       && *r == BFD_RELOC_PCREL_LO16));
2821           continue;
2822
2823         case 'u':
2824           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2825           assert (ep != NULL
2826                   && (ep->X_op == O_constant
2827                       || (ep->X_op == O_symbol
2828                           && (*r == BFD_RELOC_MIPS_HIGHEST
2829                               || *r == BFD_RELOC_HI16_S
2830                               || *r == BFD_RELOC_HI16
2831                               || *r == BFD_RELOC_GPREL16
2832                               || *r == BFD_RELOC_MIPS_GOT_HI16
2833                               || *r == BFD_RELOC_MIPS_CALL_HI16))
2834                       || (ep->X_op == O_subtract
2835                           && *r == BFD_RELOC_PCREL_HI16_S)));
2836           continue;
2837
2838         case 'p':
2839           assert (ep != NULL);
2840           /*
2841            * This allows macro() to pass an immediate expression for
2842            * creating short branches without creating a symbol.
2843            * Note that the expression still might come from the assembly
2844            * input, in which case the value is not checked for range nor
2845            * is a relocation entry generated (yuck).
2846            */
2847           if (ep->X_op == O_constant)
2848             {
2849               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2850               ep = NULL;
2851             }
2852           else
2853             if (mips_pic == EMBEDDED_PIC)
2854               *r = BFD_RELOC_16_PCREL_S2;
2855             else
2856               *r = BFD_RELOC_16_PCREL;
2857           continue;
2858
2859         case 'a':
2860           assert (ep != NULL);
2861           *r = BFD_RELOC_MIPS_JMP;
2862           continue;
2863
2864         case 'C':
2865           insn.insn_opcode |= va_arg (args, unsigned long);
2866           continue;
2867
2868         default:
2869           internalError ();
2870         }
2871       break;
2872     }
2873   va_end (args);
2874   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2875
2876   append_insn (place, &insn, ep, r, false);
2877 }
2878
2879 static void
2880 mips16_macro_build (place, counter, ep, name, fmt, args)
2881      char *place;
2882      int *counter ATTRIBUTE_UNUSED;
2883      expressionS *ep;
2884      const char *name;
2885      const char *fmt;
2886      va_list args;
2887 {
2888   struct mips_cl_insn insn;
2889   bfd_reloc_code_real_type r[3]
2890     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2891
2892   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2893   assert (insn.insn_mo);
2894   assert (strcmp (name, insn.insn_mo->name) == 0);
2895
2896   while (strcmp (fmt, insn.insn_mo->args) != 0
2897          || insn.insn_mo->pinfo == INSN_MACRO)
2898     {
2899       ++insn.insn_mo;
2900       assert (insn.insn_mo->name);
2901       assert (strcmp (name, insn.insn_mo->name) == 0);
2902     }
2903
2904   insn.insn_opcode = insn.insn_mo->match;
2905   insn.use_extend = false;
2906
2907   for (;;)
2908     {
2909       int c;
2910
2911       c = *fmt++;
2912       switch (c)
2913         {
2914         case '\0':
2915           break;
2916
2917         case ',':
2918         case '(':
2919         case ')':
2920           continue;
2921
2922         case 'y':
2923         case 'w':
2924           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2925           continue;
2926
2927         case 'x':
2928         case 'v':
2929           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2930           continue;
2931
2932         case 'z':
2933           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2934           continue;
2935
2936         case 'Z':
2937           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2938           continue;
2939
2940         case '0':
2941         case 'S':
2942         case 'P':
2943         case 'R':
2944           continue;
2945
2946         case 'X':
2947           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2948           continue;
2949
2950         case 'Y':
2951           {
2952             int regno;
2953
2954             regno = va_arg (args, int);
2955             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2956             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2957           }
2958           continue;
2959
2960         case '<':
2961         case '>':
2962         case '4':
2963         case '5':
2964         case 'H':
2965         case 'W':
2966         case 'D':
2967         case 'j':
2968         case '8':
2969         case 'V':
2970         case 'C':
2971         case 'U':
2972         case 'k':
2973         case 'K':
2974         case 'p':
2975         case 'q':
2976           {
2977             assert (ep != NULL);
2978
2979             if (ep->X_op != O_constant)
2980               *r = (int) BFD_RELOC_UNUSED + c;
2981             else
2982               {
2983                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2984                               false, &insn.insn_opcode, &insn.use_extend,
2985                               &insn.extend);
2986                 ep = NULL;
2987                 *r = BFD_RELOC_UNUSED;
2988               }
2989           }
2990           continue;
2991
2992         case '6':
2993           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2994           continue;
2995         }
2996
2997       break;
2998     }
2999
3000   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3001
3002   append_insn (place, &insn, ep, r, false);
3003 }
3004
3005 /*
3006  * Generate a "jalr" instruction with a relocation hint to the called
3007  * function.  This occurs in NewABI PIC code.
3008  */
3009 static void
3010 macro_build_jalr (icnt, ep)
3011      int icnt;
3012      expressionS *ep;
3013 {
3014   char *f;
3015   
3016   if (HAVE_NEWABI)
3017     {
3018       frag_grow (4);
3019       f = frag_more (0);
3020     }
3021   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3022                RA, PIC_CALL_REG);
3023   if (HAVE_NEWABI)
3024     fix_new_exp (frag_now, f - frag_now->fr_literal,
3025                  0, ep, false, BFD_RELOC_MIPS_JALR);
3026 }
3027
3028 /*
3029  * Generate a "lui" instruction.
3030  */
3031 static void
3032 macro_build_lui (place, counter, ep, regnum)
3033      char *place;
3034      int *counter;
3035      expressionS *ep;
3036      int regnum;
3037 {
3038   expressionS high_expr;
3039   struct mips_cl_insn insn;
3040   bfd_reloc_code_real_type r[3]
3041     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3042   const char *name = "lui";
3043   const char *fmt = "t,u";
3044
3045   assert (! mips_opts.mips16);
3046
3047   if (place == NULL)
3048     high_expr = *ep;
3049   else
3050     {
3051       high_expr.X_op = O_constant;
3052       high_expr.X_add_number = ep->X_add_number;
3053     }
3054
3055   if (high_expr.X_op == O_constant)
3056     {
3057       /* we can compute the instruction now without a relocation entry */
3058       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3059                                 >> 16) & 0xffff;
3060       *r = BFD_RELOC_UNUSED;
3061     }
3062   else if (! HAVE_NEWABI)
3063     {
3064       assert (ep->X_op == O_symbol);
3065       /* _gp_disp is a special case, used from s_cpload.  */
3066       assert (mips_pic == NO_PIC
3067               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3068       *r = BFD_RELOC_HI16_S;
3069     }
3070
3071   /*
3072    * If the macro is about to expand into a second instruction,
3073    * print a warning if needed. We need to pass ip as a parameter
3074    * to generate a better warning message here...
3075    */
3076   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3077     as_warn (_("Macro instruction expanded into multiple instructions"));
3078
3079   if (place == NULL)
3080     ++*counter;         /* bump instruction counter */
3081
3082   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3083   assert (insn.insn_mo);
3084   assert (strcmp (name, insn.insn_mo->name) == 0);
3085   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3086
3087   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3088   if (*r == BFD_RELOC_UNUSED)
3089     {
3090       insn.insn_opcode |= high_expr.X_add_number;
3091       append_insn (place, &insn, NULL, r, false);
3092     }
3093   else
3094     append_insn (place, &insn, &high_expr, r, false);
3095 }
3096
3097 /*                      set_at()
3098  * Generates code to set the $at register to true (one)
3099  * if reg is less than the immediate expression.
3100  */
3101 static void
3102 set_at (counter, reg, unsignedp)
3103      int *counter;
3104      int reg;
3105      int unsignedp;
3106 {
3107   if (imm_expr.X_op == O_constant
3108       && imm_expr.X_add_number >= -0x8000
3109       && imm_expr.X_add_number < 0x8000)
3110     macro_build ((char *) NULL, counter, &imm_expr,
3111                  unsignedp ? "sltiu" : "slti",
3112                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3113   else
3114     {
3115       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3116       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3117                    unsignedp ? "sltu" : "slt",
3118                    "d,v,t", AT, reg, AT);
3119     }
3120 }
3121
3122 /* Warn if an expression is not a constant.  */
3123
3124 static void
3125 check_absolute_expr (ip, ex)
3126      struct mips_cl_insn *ip;
3127      expressionS *ex;
3128 {
3129   if (ex->X_op == O_big)
3130     as_bad (_("unsupported large constant"));
3131   else if (ex->X_op != O_constant)
3132     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3133 }
3134
3135 /* Count the leading zeroes by performing a binary chop. This is a
3136    bulky bit of source, but performance is a LOT better for the
3137    majority of values than a simple loop to count the bits:
3138        for (lcnt = 0; (lcnt < 32); lcnt++)
3139          if ((v) & (1 << (31 - lcnt)))
3140            break;
3141   However it is not code size friendly, and the gain will drop a bit
3142   on certain cached systems.
3143 */
3144 #define COUNT_TOP_ZEROES(v)             \
3145   (((v) & ~0xffff) == 0                 \
3146    ? ((v) & ~0xff) == 0                 \
3147      ? ((v) & ~0xf) == 0                \
3148        ? ((v) & ~0x3) == 0              \
3149          ? ((v) & ~0x1) == 0            \
3150            ? !(v)                       \
3151              ? 32                       \
3152              : 31                       \
3153            : 30                         \
3154          : ((v) & ~0x7) == 0            \
3155            ? 29                         \
3156            : 28                         \
3157        : ((v) & ~0x3f) == 0             \
3158          ? ((v) & ~0x1f) == 0           \
3159            ? 27                         \
3160            : 26                         \
3161          : ((v) & ~0x7f) == 0           \
3162            ? 25                         \
3163            : 24                         \
3164      : ((v) & ~0xfff) == 0              \
3165        ? ((v) & ~0x3ff) == 0            \
3166          ? ((v) & ~0x1ff) == 0          \
3167            ? 23                         \
3168            : 22                         \
3169          : ((v) & ~0x7ff) == 0          \
3170            ? 21                         \
3171            : 20                         \
3172        : ((v) & ~0x3fff) == 0           \
3173          ? ((v) & ~0x1fff) == 0         \
3174            ? 19                         \
3175            : 18                         \
3176          : ((v) & ~0x7fff) == 0         \
3177            ? 17                         \
3178            : 16                         \
3179    : ((v) & ~0xffffff) == 0             \
3180      ? ((v) & ~0xfffff) == 0            \
3181        ? ((v) & ~0x3ffff) == 0          \
3182          ? ((v) & ~0x1ffff) == 0        \
3183            ? 15                         \
3184            : 14                         \
3185          : ((v) & ~0x7ffff) == 0        \
3186            ? 13                         \
3187            : 12                         \
3188        : ((v) & ~0x3fffff) == 0         \
3189          ? ((v) & ~0x1fffff) == 0       \
3190            ? 11                         \
3191            : 10                         \
3192          : ((v) & ~0x7fffff) == 0       \
3193            ? 9                          \
3194            : 8                          \
3195      : ((v) & ~0xfffffff) == 0          \
3196        ? ((v) & ~0x3ffffff) == 0        \
3197          ? ((v) & ~0x1ffffff) == 0      \
3198            ? 7                          \
3199            : 6                          \
3200          : ((v) & ~0x7ffffff) == 0      \
3201            ? 5                          \
3202            : 4                          \
3203        : ((v) & ~0x3fffffff) == 0       \
3204          ? ((v) & ~0x1fffffff) == 0     \
3205            ? 3                          \
3206            : 2                          \
3207          : ((v) & ~0x7fffffff) == 0     \
3208            ? 1                          \
3209            : 0)
3210
3211 /* Is the given value a sign-extended 32-bit value?  */
3212 #define IS_SEXT_32BIT_NUM(x)                                            \
3213   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
3214    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3215
3216 /*                      load_register()
3217  *  This routine generates the least number of instructions neccessary to load
3218  *  an absolute expression value into a register.
3219  */
3220 static void
3221 load_register (counter, reg, ep, dbl)
3222      int *counter;
3223      int reg;
3224      expressionS *ep;
3225      int dbl;
3226 {
3227   int freg;
3228   expressionS hi32, lo32;
3229
3230   if (ep->X_op != O_big)
3231     {
3232       assert (ep->X_op == O_constant);
3233       if (ep->X_add_number < 0x8000
3234           && (ep->X_add_number >= 0
3235               || (ep->X_add_number >= -0x8000
3236                   && (! dbl
3237                       || ! ep->X_unsigned
3238                       || sizeof (ep->X_add_number) > 4))))
3239         {
3240           /* We can handle 16 bit signed values with an addiu to
3241              $zero.  No need to ever use daddiu here, since $zero and
3242              the result are always correct in 32 bit mode.  */
3243           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3244                        (int) BFD_RELOC_LO16);
3245           return;
3246         }
3247       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3248         {
3249           /* We can handle 16 bit unsigned values with an ori to
3250              $zero.  */
3251           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3252                        (int) BFD_RELOC_LO16);
3253           return;
3254         }
3255       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3256                 && (! dbl
3257                     || ! ep->X_unsigned
3258                     || sizeof (ep->X_add_number) > 4
3259                     || (ep->X_add_number & 0x80000000) == 0))
3260                || ((HAVE_32BIT_GPRS || ! dbl)
3261                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3262                || (HAVE_32BIT_GPRS
3263                    && ! dbl
3264                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3265                        == ~ (offsetT) 0xffffffff)))
3266         {
3267           /* 32 bit values require an lui.  */
3268           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3269                        (int) BFD_RELOC_HI16);
3270           if ((ep->X_add_number & 0xffff) != 0)
3271             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3272                          (int) BFD_RELOC_LO16);
3273           return;
3274         }
3275     }
3276
3277   /* The value is larger than 32 bits.  */
3278
3279   if (HAVE_32BIT_GPRS)
3280     {
3281       as_bad (_("Number (0x%lx) larger than 32 bits"),
3282               (unsigned long) ep->X_add_number);
3283       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3284                    (int) BFD_RELOC_LO16);
3285       return;
3286     }
3287
3288   if (ep->X_op != O_big)
3289     {
3290       hi32 = *ep;
3291       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3292       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3293       hi32.X_add_number &= 0xffffffff;
3294       lo32 = *ep;
3295       lo32.X_add_number &= 0xffffffff;
3296     }
3297   else
3298     {
3299       assert (ep->X_add_number > 2);
3300       if (ep->X_add_number == 3)
3301         generic_bignum[3] = 0;
3302       else if (ep->X_add_number > 4)
3303         as_bad (_("Number larger than 64 bits"));
3304       lo32.X_op = O_constant;
3305       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3306       hi32.X_op = O_constant;
3307       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3308     }
3309
3310   if (hi32.X_add_number == 0)
3311     freg = 0;
3312   else
3313     {
3314       int shift, bit;
3315       unsigned long hi, lo;
3316
3317       if (hi32.X_add_number == (offsetT) 0xffffffff)
3318         {
3319           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3320             {
3321               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3322                            reg, 0, (int) BFD_RELOC_LO16);
3323               return;
3324             }
3325           if (lo32.X_add_number & 0x80000000)
3326             {
3327               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3328                            (int) BFD_RELOC_HI16);
3329               if (lo32.X_add_number & 0xffff)
3330                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3331                              reg, reg, (int) BFD_RELOC_LO16);
3332               return;
3333             }
3334         }
3335
3336       /* Check for 16bit shifted constant.  We know that hi32 is
3337          non-zero, so start the mask on the first bit of the hi32
3338          value.  */
3339       shift = 17;
3340       do
3341         {
3342           unsigned long himask, lomask;
3343
3344           if (shift < 32)
3345             {
3346               himask = 0xffff >> (32 - shift);
3347               lomask = (0xffff << shift) & 0xffffffff;
3348             }
3349           else
3350             {
3351               himask = 0xffff << (shift - 32);
3352               lomask = 0;
3353             }
3354           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3355               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3356             {
3357               expressionS tmp;
3358
3359               tmp.X_op = O_constant;
3360               if (shift < 32)
3361                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3362                                     | (lo32.X_add_number >> shift));
3363               else
3364                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3365               macro_build ((char *) NULL, counter, &tmp,
3366                            "ori", "t,r,i", reg, 0,
3367                            (int) BFD_RELOC_LO16);
3368               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3369                            (shift >= 32) ? "dsll32" : "dsll",
3370                            "d,w,<", reg, reg,
3371                            (shift >= 32) ? shift - 32 : shift);
3372               return;
3373             }
3374           ++shift;
3375         }
3376       while (shift <= (64 - 16));
3377
3378       /* Find the bit number of the lowest one bit, and store the
3379          shifted value in hi/lo.  */
3380       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3381       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3382       if (lo != 0)
3383         {
3384           bit = 0;
3385           while ((lo & 1) == 0)
3386             {
3387               lo >>= 1;
3388               ++bit;
3389             }
3390           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3391           hi >>= bit;
3392         }
3393       else
3394         {
3395           bit = 32;
3396           while ((hi & 1) == 0)
3397             {
3398               hi >>= 1;
3399               ++bit;
3400             }
3401           lo = hi;
3402           hi = 0;
3403         }
3404
3405       /* Optimize if the shifted value is a (power of 2) - 1.  */
3406       if ((hi == 0 && ((lo + 1) & lo) == 0)
3407           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3408         {
3409           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3410           if (shift != 0)
3411             {
3412               expressionS tmp;
3413
3414               /* This instruction will set the register to be all
3415                  ones.  */
3416               tmp.X_op = O_constant;
3417               tmp.X_add_number = (offsetT) -1;
3418               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3419                            reg, 0, (int) BFD_RELOC_LO16);
3420               if (bit != 0)
3421                 {
3422                   bit += shift;
3423                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3424                                (bit >= 32) ? "dsll32" : "dsll",
3425                                "d,w,<", reg, reg,
3426                                (bit >= 32) ? bit - 32 : bit);
3427                 }
3428               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3429                            (shift >= 32) ? "dsrl32" : "dsrl",
3430                            "d,w,<", reg, reg,
3431                            (shift >= 32) ? shift - 32 : shift);
3432               return;
3433             }
3434         }
3435
3436       /* Sign extend hi32 before calling load_register, because we can
3437          generally get better code when we load a sign extended value.  */
3438       if ((hi32.X_add_number & 0x80000000) != 0)
3439         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3440       load_register (counter, reg, &hi32, 0);
3441       freg = reg;
3442     }
3443   if ((lo32.X_add_number & 0xffff0000) == 0)
3444     {
3445       if (freg != 0)
3446         {
3447           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3448                        "dsll32", "d,w,<", reg, freg, 0);
3449           freg = reg;
3450         }
3451     }
3452   else
3453     {
3454       expressionS mid16;
3455
3456       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3457         {
3458           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3459                        (int) BFD_RELOC_HI16);
3460           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3461                        "dsrl32", "d,w,<", reg, reg, 0);
3462           return;
3463         }
3464
3465       if (freg != 0)
3466         {
3467           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3468                        "d,w,<", reg, freg, 16);
3469           freg = reg;
3470         }
3471       mid16 = lo32;
3472       mid16.X_add_number >>= 16;
3473       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3474                    freg, (int) BFD_RELOC_LO16);
3475       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3476                    "d,w,<", reg, reg, 16);
3477       freg = reg;
3478     }
3479   if ((lo32.X_add_number & 0xffff) != 0)
3480     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3481                  (int) BFD_RELOC_LO16);
3482 }
3483
3484 /* Load an address into a register.  */
3485
3486 static void
3487 load_address (counter, reg, ep, used_at)
3488      int *counter;
3489      int reg;
3490      expressionS *ep;
3491      int *used_at;
3492 {
3493   char *p = NULL;
3494
3495   if (ep->X_op != O_constant
3496       && ep->X_op != O_symbol)
3497     {
3498       as_bad (_("expression too complex"));
3499       ep->X_op = O_constant;
3500     }
3501
3502   if (ep->X_op == O_constant)
3503     {
3504       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3505       return;
3506     }
3507
3508   if (mips_pic == NO_PIC)
3509     {
3510       /* If this is a reference to a GP relative symbol, we want
3511            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3512          Otherwise we want
3513            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3514            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3515          If we have an addend, we always use the latter form.
3516
3517          With 64bit address space and a usable $at we want
3518            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3519            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3520            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3521            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3522            dsll32       $reg,0
3523            daddu        $reg,$reg,$at
3524
3525          If $at is already in use, we use an path which is suboptimal
3526          on superscalar processors.
3527            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3528            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3529            dsll         $reg,16
3530            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3531            dsll         $reg,16
3532            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3533        */
3534       if (HAVE_64BIT_ADDRESSES)
3535         {
3536           /* We don't do GP optimization for now because RELAX_ENCODE can't
3537              hold the data for such large chunks.  */
3538
3539           if (*used_at == 0 && ! mips_opts.noat)
3540             {
3541               macro_build (p, counter, ep, "lui", "t,u",
3542                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3543               macro_build (p, counter, ep, "lui", "t,u",
3544                            AT, (int) BFD_RELOC_HI16_S);
3545               macro_build (p, counter, ep, "daddiu", "t,r,j",
3546                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3547               macro_build (p, counter, ep, "daddiu", "t,r,j",
3548                            AT, AT, (int) BFD_RELOC_LO16);
3549               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3550                            "d,w,<", reg, reg, 0);
3551               macro_build (p, counter, (expressionS *) NULL, "daddu",
3552                            "d,v,t", reg, reg, AT);
3553               *used_at = 1;
3554             }
3555           else
3556             {
3557               macro_build (p, counter, ep, "lui", "t,u",
3558                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3559               macro_build (p, counter, ep, "daddiu", "t,r,j",
3560                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3561               macro_build (p, counter, (expressionS *) NULL, "dsll",
3562                            "d,w,<", reg, reg, 16);
3563               macro_build (p, counter, ep, "daddiu", "t,r,j",
3564                            reg, reg, (int) BFD_RELOC_HI16_S);
3565               macro_build (p, counter, (expressionS *) NULL, "dsll",
3566                            "d,w,<", reg, reg, 16);
3567               macro_build (p, counter, ep, "daddiu", "t,r,j",
3568                            reg, reg, (int) BFD_RELOC_LO16);
3569             }
3570         }
3571       else
3572         {
3573           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3574               && ! nopic_need_relax (ep->X_add_symbol, 1))
3575             {
3576               frag_grow (20);
3577               macro_build ((char *) NULL, counter, ep,
3578                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3579                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3580               p = frag_var (rs_machine_dependent, 8, 0,
3581                             RELAX_ENCODE (4, 8, 0, 4, 0,
3582                                           mips_opts.warn_about_macros),
3583                             ep->X_add_symbol, 0, NULL);
3584             }
3585           macro_build_lui (p, counter, ep, reg);
3586           if (p != NULL)
3587             p += 4;
3588           macro_build (p, counter, ep,
3589                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3590                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3591         }
3592     }
3593   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3594     {
3595       expressionS ex;
3596
3597       /* If this is a reference to an external symbol, we want
3598            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3599          Otherwise we want
3600            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3601            nop
3602            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3603          If there is a constant, it must be added in after.  */
3604       ex.X_add_number = ep->X_add_number;
3605       ep->X_add_number = 0;
3606       frag_grow (20);
3607       macro_build ((char *) NULL, counter, ep,
3608                    HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3609                    reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3610       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3611       p = frag_var (rs_machine_dependent, 4, 0,
3612                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3613                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3614       macro_build (p, counter, ep,
3615                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3616                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3617       if (ex.X_add_number != 0)
3618         {
3619           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3620             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3621           ex.X_op = O_constant;
3622           macro_build ((char *) NULL, counter, &ex,
3623                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3624                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3625         }
3626     }
3627   else if (mips_pic == SVR4_PIC)
3628     {
3629       expressionS ex;
3630       int off;
3631
3632       /* This is the large GOT case.  If this is a reference to an
3633          external symbol, we want
3634            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3635            addu         $reg,$reg,$gp
3636            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3637          Otherwise, for a reference to a local symbol, we want
3638            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3639            nop
3640            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3641          If we have NewABI, we want
3642            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3643            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3644          If there is a constant, it must be added in after.  */
3645       ex.X_add_number = ep->X_add_number;
3646       ep->X_add_number = 0;
3647       if (HAVE_NEWABI)
3648         {
3649           macro_build ((char *) NULL, counter, ep,
3650                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3651                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3652           macro_build (p, counter, ep,
3653                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3654                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3655         }
3656       else
3657         {
3658           if (reg_needs_delay (mips_gp_register))
3659             off = 4;
3660           else
3661             off = 0;
3662           frag_grow (32);
3663           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3664                        (int) BFD_RELOC_MIPS_GOT_HI16);
3665           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3666                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3667                        reg, mips_gp_register);
3668           macro_build ((char *) NULL, counter, ep,
3669                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3670                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3671           p = frag_var (rs_machine_dependent, 12 + off, 0,
3672                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3673                                       mips_opts.warn_about_macros),
3674                         ep->X_add_symbol, 0, NULL);
3675           if (off > 0)
3676             {
3677               /* We need a nop before loading from $gp.  This special
3678                  check is required because the lui which starts the main
3679                  instruction stream does not refer to $gp, and so will not
3680                  insert the nop which may be required.  */
3681               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3682                 p += 4;
3683             }
3684           macro_build (p, counter, ep,
3685                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3686                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3687           p += 4;
3688           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3689           p += 4;
3690           macro_build (p, counter, ep,
3691                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3692                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3693         }
3694
3695       if (ex.X_add_number != 0)
3696         {
3697           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3698             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3699           ex.X_op = O_constant;
3700           macro_build ((char *) NULL, counter, &ex,
3701                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3702                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3703         }
3704     }
3705   else if (mips_pic == EMBEDDED_PIC)
3706     {
3707       /* We always do
3708            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3709        */
3710       macro_build ((char *) NULL, counter, ep,
3711                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3712                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3713     }
3714   else
3715     abort ();
3716 }
3717
3718 /* Move the contents of register SOURCE into register DEST.  */
3719
3720 static void
3721 move_register (counter, dest, source)
3722      int *counter;
3723      int dest;
3724      int source;
3725 {
3726   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3727                HAVE_32BIT_GPRS ? "addu" : "daddu",
3728                "d,v,t", dest, source, 0);
3729 }
3730
3731 /*
3732  *                      Build macros
3733  *   This routine implements the seemingly endless macro or synthesized
3734  * instructions and addressing modes in the mips assembly language. Many
3735  * of these macros are simple and are similar to each other. These could
3736  * probably be handled by some kind of table or grammer aproach instead of
3737  * this verbose method. Others are not simple macros but are more like
3738  * optimizing code generation.
3739  *   One interesting optimization is when several store macros appear
3740  * consecutivly that would load AT with the upper half of the same address.
3741  * The ensuing load upper instructions are ommited. This implies some kind
3742  * of global optimization. We currently only optimize within a single macro.
3743  *   For many of the load and store macros if the address is specified as a
3744  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3745  * first load register 'at' with zero and use it as the base register. The
3746  * mips assembler simply uses register $zero. Just one tiny optimization
3747  * we're missing.
3748  */
3749 static void
3750 macro (ip)
3751      struct mips_cl_insn *ip;
3752 {
3753   register int treg, sreg, dreg, breg;
3754   int tempreg;
3755   int mask;
3756   int icnt = 0;
3757   int used_at = 0;
3758   expressionS expr1;
3759   const char *s;
3760   const char *s2;
3761   const char *fmt;
3762   int likely = 0;
3763   int dbl = 0;
3764   int coproc = 0;
3765   int lr = 0;
3766   int imm = 0;
3767   offsetT maxnum;
3768   int off;
3769   bfd_reloc_code_real_type r;
3770   int hold_mips_optimize;
3771
3772   assert (! mips_opts.mips16);
3773
3774   treg = (ip->insn_opcode >> 16) & 0x1f;
3775   dreg = (ip->insn_opcode >> 11) & 0x1f;
3776   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3777   mask = ip->insn_mo->mask;
3778
3779   expr1.X_op = O_constant;
3780   expr1.X_op_symbol = NULL;
3781   expr1.X_add_symbol = NULL;
3782   expr1.X_add_number = 1;
3783
3784   switch (mask)
3785     {
3786     case M_DABS:
3787       dbl = 1;
3788     case M_ABS:
3789       /* bgez $a0,.+12
3790          move v0,$a0
3791          sub v0,$zero,$a0
3792          */
3793
3794       mips_emit_delays (true);
3795       ++mips_opts.noreorder;
3796       mips_any_noreorder = 1;
3797
3798       expr1.X_add_number = 8;
3799       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3800       if (dreg == sreg)
3801         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3802                      0);
3803       else
3804         move_register (&icnt, dreg, sreg);
3805       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3806                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3807
3808       --mips_opts.noreorder;
3809       return;
3810
3811     case M_ADD_I:
3812       s = "addi";
3813       s2 = "add";
3814       goto do_addi;
3815     case M_ADDU_I:
3816       s = "addiu";
3817       s2 = "addu";
3818       goto do_addi;
3819     case M_DADD_I:
3820       dbl = 1;
3821       s = "daddi";
3822       s2 = "dadd";
3823       goto do_addi;
3824     case M_DADDU_I:
3825       dbl = 1;
3826       s = "daddiu";
3827       s2 = "daddu";
3828     do_addi:
3829       if (imm_expr.X_op == O_constant
3830           && imm_expr.X_add_number >= -0x8000
3831           && imm_expr.X_add_number < 0x8000)
3832         {
3833           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3834                        (int) BFD_RELOC_LO16);
3835           return;
3836         }
3837       load_register (&icnt, AT, &imm_expr, dbl);
3838       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3839                    treg, sreg, AT);
3840       break;
3841
3842     case M_AND_I:
3843       s = "andi";
3844       s2 = "and";
3845       goto do_bit;
3846     case M_OR_I:
3847       s = "ori";
3848       s2 = "or";
3849       goto do_bit;
3850     case M_NOR_I:
3851       s = "";
3852       s2 = "nor";
3853       goto do_bit;
3854     case M_XOR_I:
3855       s = "xori";
3856       s2 = "xor";
3857     do_bit:
3858       if (imm_expr.X_op == O_constant
3859           && imm_expr.X_add_number >= 0
3860           && imm_expr.X_add_number < 0x10000)
3861         {
3862           if (mask != M_NOR_I)
3863             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3864                          sreg, (int) BFD_RELOC_LO16);
3865           else
3866             {
3867               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3868                            treg, sreg, (int) BFD_RELOC_LO16);
3869               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3870                            "d,v,t", treg, treg, 0);
3871             }
3872           return;
3873         }
3874
3875       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3876       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3877                    treg, sreg, AT);
3878       break;
3879
3880     case M_BEQ_I:
3881       s = "beq";
3882       goto beq_i;
3883     case M_BEQL_I:
3884       s = "beql";
3885       likely = 1;
3886       goto beq_i;
3887     case M_BNE_I:
3888       s = "bne";
3889       goto beq_i;
3890     case M_BNEL_I:
3891       s = "bnel";
3892       likely = 1;
3893     beq_i:
3894       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3895         {
3896           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3897                        0);
3898           return;
3899         }
3900       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3901       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3902       break;
3903
3904     case M_BGEL:
3905       likely = 1;
3906     case M_BGE:
3907       if (treg == 0)
3908         {
3909           macro_build ((char *) NULL, &icnt, &offset_expr,
3910                        likely ? "bgezl" : "bgez", "s,p", sreg);
3911           return;
3912         }
3913       if (sreg == 0)
3914         {
3915           macro_build ((char *) NULL, &icnt, &offset_expr,
3916                        likely ? "blezl" : "blez", "s,p", treg);
3917           return;
3918         }
3919       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3920                    AT, sreg, treg);
3921       macro_build ((char *) NULL, &icnt, &offset_expr,
3922                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3923       break;
3924
3925     case M_BGTL_I:
3926       likely = 1;
3927     case M_BGT_I:
3928       /* check for > max integer */
3929       maxnum = 0x7fffffff;
3930       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3931         {
3932           maxnum <<= 16;
3933           maxnum |= 0xffff;
3934           maxnum <<= 16;
3935           maxnum |= 0xffff;
3936         }
3937       if (imm_expr.X_op == O_constant
3938           && imm_expr.X_add_number >= maxnum
3939           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3940         {
3941         do_false:
3942           /* result is always false */
3943           if (! likely)
3944             {
3945               if (warn_nops)
3946                 as_warn (_("Branch %s is always false (nop)"),
3947                          ip->insn_mo->name);
3948               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3949                            "", 0);
3950             }
3951           else
3952             {
3953               if (warn_nops)
3954                 as_warn (_("Branch likely %s is always false"),
3955                          ip->insn_mo->name);
3956               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3957                            "s,t,p", 0, 0);
3958             }
3959           return;
3960         }
3961       if (imm_expr.X_op != O_constant)
3962         as_bad (_("Unsupported large constant"));
3963       ++imm_expr.X_add_number;
3964       /* FALLTHROUGH */
3965     case M_BGE_I:
3966     case M_BGEL_I:
3967       if (mask == M_BGEL_I)
3968         likely = 1;
3969       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3970         {
3971           macro_build ((char *) NULL, &icnt, &offset_expr,
3972                        likely ? "bgezl" : "bgez", "s,p", sreg);
3973           return;
3974         }
3975       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3976         {
3977           macro_build ((char *) NULL, &icnt, &offset_expr,
3978                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
3979           return;
3980         }
3981       maxnum = 0x7fffffff;
3982       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3983         {
3984           maxnum <<= 16;
3985           maxnum |= 0xffff;
3986           maxnum <<= 16;
3987           maxnum |= 0xffff;
3988         }
3989       maxnum = - maxnum - 1;
3990       if (imm_expr.X_op == O_constant
3991           && imm_expr.X_add_number <= maxnum
3992           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3993         {
3994         do_true:
3995           /* result is always true */
3996           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3997           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3998           return;
3999         }
4000       set_at (&icnt, sreg, 0);
4001       macro_build ((char *) NULL, &icnt, &offset_expr,
4002                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4003       break;
4004
4005     case M_BGEUL:
4006       likely = 1;
4007     case M_BGEU:
4008       if (treg == 0)
4009         goto do_true;
4010       if (sreg == 0)
4011         {
4012           macro_build ((char *) NULL, &icnt, &offset_expr,
4013                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4014           return;
4015         }
4016       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4017                    "d,v,t", AT, sreg, treg);
4018       macro_build ((char *) NULL, &icnt, &offset_expr,
4019                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4020       break;
4021
4022     case M_BGTUL_I:
4023       likely = 1;
4024     case M_BGTU_I:
4025       if (sreg == 0
4026           || (HAVE_32BIT_GPRS
4027               && imm_expr.X_op == O_constant
4028               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4029         goto do_false;
4030       if (imm_expr.X_op != O_constant)
4031         as_bad (_("Unsupported large constant"));
4032       ++imm_expr.X_add_number;
4033       /* FALLTHROUGH */
4034     case M_BGEU_I:
4035     case M_BGEUL_I:
4036       if (mask == M_BGEUL_I)
4037         likely = 1;
4038       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4039         goto do_true;
4040       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4041         {
4042           macro_build ((char *) NULL, &icnt, &offset_expr,
4043                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4044           return;
4045         }
4046       set_at (&icnt, sreg, 1);
4047       macro_build ((char *) NULL, &icnt, &offset_expr,
4048                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4049       break;
4050
4051     case M_BGTL:
4052       likely = 1;
4053     case M_BGT:
4054       if (treg == 0)
4055         {
4056           macro_build ((char *) NULL, &icnt, &offset_expr,
4057                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4058           return;
4059         }
4060       if (sreg == 0)
4061         {
4062           macro_build ((char *) NULL, &icnt, &offset_expr,
4063                        likely ? "bltzl" : "bltz", "s,p", treg);
4064           return;
4065         }
4066       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4067                    AT, treg, sreg);
4068       macro_build ((char *) NULL, &icnt, &offset_expr,
4069                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4070       break;
4071
4072     case M_BGTUL:
4073       likely = 1;
4074     case M_BGTU:
4075       if (treg == 0)
4076         {
4077           macro_build ((char *) NULL, &icnt, &offset_expr,
4078                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4079           return;
4080         }
4081       if (sreg == 0)
4082         goto do_false;
4083       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4084                    "d,v,t", AT, treg, sreg);
4085       macro_build ((char *) NULL, &icnt, &offset_expr,
4086                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4087       break;
4088
4089     case M_BLEL:
4090       likely = 1;
4091     case M_BLE:
4092       if (treg == 0)
4093         {
4094           macro_build ((char *) NULL, &icnt, &offset_expr,
4095                        likely ? "blezl" : "blez", "s,p", sreg);
4096           return;
4097         }
4098       if (sreg == 0)
4099         {
4100           macro_build ((char *) NULL, &icnt, &offset_expr,
4101                        likely ? "bgezl" : "bgez", "s,p", treg);
4102           return;
4103         }
4104       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4105                    AT, treg, sreg);
4106       macro_build ((char *) NULL, &icnt, &offset_expr,
4107                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4108       break;
4109
4110     case M_BLEL_I:
4111       likely = 1;
4112     case M_BLE_I:
4113       maxnum = 0x7fffffff;
4114       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4115         {
4116           maxnum <<= 16;
4117           maxnum |= 0xffff;
4118           maxnum <<= 16;
4119           maxnum |= 0xffff;
4120         }
4121       if (imm_expr.X_op == O_constant
4122           && imm_expr.X_add_number >= maxnum
4123           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4124         goto do_true;
4125       if (imm_expr.X_op != O_constant)
4126         as_bad (_("Unsupported large constant"));
4127       ++imm_expr.X_add_number;
4128       /* FALLTHROUGH */
4129     case M_BLT_I:
4130     case M_BLTL_I:
4131       if (mask == M_BLTL_I)
4132         likely = 1;
4133       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4134         {
4135           macro_build ((char *) NULL, &icnt, &offset_expr,
4136                        likely ? "bltzl" : "bltz", "s,p", sreg);
4137           return;
4138         }
4139       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4140         {
4141           macro_build ((char *) NULL, &icnt, &offset_expr,
4142                        likely ? "blezl" : "blez", "s,p", sreg);
4143           return;
4144         }
4145       set_at (&icnt, sreg, 0);
4146       macro_build ((char *) NULL, &icnt, &offset_expr,
4147                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4148       break;
4149
4150     case M_BLEUL:
4151       likely = 1;
4152     case M_BLEU:
4153       if (treg == 0)
4154         {
4155           macro_build ((char *) NULL, &icnt, &offset_expr,
4156                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4157           return;
4158         }
4159       if (sreg == 0)
4160         goto do_true;
4161       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4162                    "d,v,t", AT, treg, sreg);
4163       macro_build ((char *) NULL, &icnt, &offset_expr,
4164                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4165       break;
4166
4167     case M_BLEUL_I:
4168       likely = 1;
4169     case M_BLEU_I:
4170       if (sreg == 0
4171           || (HAVE_32BIT_GPRS
4172               && imm_expr.X_op == O_constant
4173               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4174         goto do_true;
4175       if (imm_expr.X_op != O_constant)
4176         as_bad (_("Unsupported large constant"));
4177       ++imm_expr.X_add_number;
4178       /* FALLTHROUGH */
4179     case M_BLTU_I:
4180     case M_BLTUL_I:
4181       if (mask == M_BLTUL_I)
4182         likely = 1;
4183       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4184         goto do_false;
4185       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4186         {
4187           macro_build ((char *) NULL, &icnt, &offset_expr,
4188                        likely ? "beql" : "beq",
4189                        "s,t,p", sreg, 0);
4190           return;
4191         }
4192       set_at (&icnt, sreg, 1);
4193       macro_build ((char *) NULL, &icnt, &offset_expr,
4194                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4195       break;
4196
4197     case M_BLTL:
4198       likely = 1;
4199     case M_BLT:
4200       if (treg == 0)
4201         {
4202           macro_build ((char *) NULL, &icnt, &offset_expr,
4203                        likely ? "bltzl" : "bltz", "s,p", sreg);
4204           return;
4205         }
4206       if (sreg == 0)
4207         {
4208           macro_build ((char *) NULL, &icnt, &offset_expr,
4209                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4210           return;
4211         }
4212       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4213                    AT, sreg, treg);
4214       macro_build ((char *) NULL, &icnt, &offset_expr,
4215                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4216       break;
4217
4218     case M_BLTUL:
4219       likely = 1;
4220     case M_BLTU:
4221       if (treg == 0)
4222         goto do_false;
4223       if (sreg == 0)
4224         {
4225           macro_build ((char *) NULL, &icnt, &offset_expr,
4226                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4227           return;
4228         }
4229       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4230                    "d,v,t", AT, sreg,
4231                    treg);
4232       macro_build ((char *) NULL, &icnt, &offset_expr,
4233                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4234       break;
4235
4236     case M_DDIV_3:
4237       dbl = 1;
4238     case M_DIV_3:
4239       s = "mflo";
4240       goto do_div3;
4241     case M_DREM_3:
4242       dbl = 1;
4243     case M_REM_3:
4244       s = "mfhi";
4245     do_div3:
4246       if (treg == 0)
4247         {
4248           as_warn (_("Divide by zero."));
4249           if (mips_trap)
4250             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4251                          "s,t,q", 0, 0, 7);
4252           else
4253             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4254                          "c", 7);
4255           return;
4256         }
4257
4258       mips_emit_delays (true);
4259       ++mips_opts.noreorder;
4260       mips_any_noreorder = 1;
4261       if (mips_trap)
4262         {
4263           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4264                        "s,t,q", treg, 0, 7);
4265           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4266                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4267         }
4268       else
4269         {
4270           expr1.X_add_number = 8;
4271           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4272           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4273                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4274           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4275                        "c", 7);
4276         }
4277       expr1.X_add_number = -1;
4278       macro_build ((char *) NULL, &icnt, &expr1,
4279                    dbl ? "daddiu" : "addiu",
4280                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4281       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4282       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4283       if (dbl)
4284         {
4285           expr1.X_add_number = 1;
4286           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4287                        (int) BFD_RELOC_LO16);
4288           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4289                        "d,w,<", AT, AT, 31);
4290         }
4291       else
4292         {
4293           expr1.X_add_number = 0x80000000;
4294           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4295                        (int) BFD_RELOC_HI16);
4296         }
4297       if (mips_trap)
4298         {
4299           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4300                        "s,t,q", sreg, AT, 6);
4301           /* We want to close the noreorder block as soon as possible, so
4302              that later insns are available for delay slot filling.  */
4303           --mips_opts.noreorder;
4304         }
4305       else
4306         {
4307           expr1.X_add_number = 8;
4308           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4309           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4310                        0);
4311
4312           /* We want to close the noreorder block as soon as possible, so
4313              that later insns are available for delay slot filling.  */
4314           --mips_opts.noreorder;
4315
4316           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4317                        "c", 6);
4318         }
4319       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4320       break;
4321
4322     case M_DIV_3I:
4323       s = "div";
4324       s2 = "mflo";
4325       goto do_divi;
4326     case M_DIVU_3I:
4327       s = "divu";
4328       s2 = "mflo";
4329       goto do_divi;
4330     case M_REM_3I:
4331       s = "div";
4332       s2 = "mfhi";
4333       goto do_divi;
4334     case M_REMU_3I:
4335       s = "divu";
4336       s2 = "mfhi";
4337       goto do_divi;
4338     case M_DDIV_3I:
4339       dbl = 1;
4340       s = "ddiv";
4341       s2 = "mflo";
4342       goto do_divi;
4343     case M_DDIVU_3I:
4344       dbl = 1;
4345       s = "ddivu";
4346       s2 = "mflo";
4347       goto do_divi;
4348     case M_DREM_3I:
4349       dbl = 1;
4350       s = "ddiv";
4351       s2 = "mfhi";
4352       goto do_divi;
4353     case M_DREMU_3I:
4354       dbl = 1;
4355       s = "ddivu";
4356       s2 = "mfhi";
4357     do_divi:
4358       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4359         {
4360           as_warn (_("Divide by zero."));
4361           if (mips_trap)
4362             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4363                          "s,t,q", 0, 0, 7);
4364           else
4365             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4366                          "c", 7);
4367           return;
4368         }
4369       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4370         {
4371           if (strcmp (s2, "mflo") == 0)
4372             move_register (&icnt, dreg, sreg);
4373           else
4374             move_register (&icnt, dreg, 0);
4375           return;
4376         }
4377       if (imm_expr.X_op == O_constant
4378           && imm_expr.X_add_number == -1
4379           && s[strlen (s) - 1] != 'u')
4380         {
4381           if (strcmp (s2, "mflo") == 0)
4382             {
4383               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4384                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4385             }
4386           else
4387             move_register (&icnt, dreg, 0);
4388           return;
4389         }
4390
4391       load_register (&icnt, AT, &imm_expr, dbl);
4392       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4393                    sreg, AT);
4394       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4395       break;
4396
4397     case M_DIVU_3:
4398       s = "divu";
4399       s2 = "mflo";
4400       goto do_divu3;
4401     case M_REMU_3:
4402       s = "divu";
4403       s2 = "mfhi";
4404       goto do_divu3;
4405     case M_DDIVU_3:
4406       s = "ddivu";
4407       s2 = "mflo";
4408       goto do_divu3;
4409     case M_DREMU_3:
4410       s = "ddivu";
4411       s2 = "mfhi";
4412     do_divu3:
4413       mips_emit_delays (true);
4414       ++mips_opts.noreorder;
4415       mips_any_noreorder = 1;
4416       if (mips_trap)
4417         {
4418           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4419                        "s,t,q", treg, 0, 7);
4420           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4421                        sreg, treg);
4422           /* We want to close the noreorder block as soon as possible, so
4423              that later insns are available for delay slot filling.  */
4424           --mips_opts.noreorder;
4425         }
4426       else
4427         {
4428           expr1.X_add_number = 8;
4429           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4430           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4431                        sreg, treg);
4432
4433           /* We want to close the noreorder block as soon as possible, so
4434              that later insns are available for delay slot filling.  */
4435           --mips_opts.noreorder;
4436           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4437                        "c", 7);
4438         }
4439       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4440       return;
4441
4442     case M_DLA_AB:
4443       dbl = 1;
4444     case M_LA_AB:
4445       /* Load the address of a symbol into a register.  If breg is not
4446          zero, we then add a base register to it.  */
4447
4448       if (dbl && HAVE_32BIT_GPRS)
4449         as_warn (_("dla used to load 32-bit register"));
4450
4451       if (! dbl && HAVE_64BIT_OBJECTS)
4452         as_warn (_("la used to load 64-bit address"));
4453
4454       if (offset_expr.X_op == O_constant
4455           && offset_expr.X_add_number >= -0x8000
4456           && offset_expr.X_add_number < 0x8000)
4457         {
4458           macro_build ((char *) NULL, &icnt, &offset_expr,
4459                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4460                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4461           return;
4462         }
4463
4464       if (treg == breg)
4465         {
4466           tempreg = AT;
4467           used_at = 1;
4468         }
4469       else
4470         {
4471           tempreg = treg;
4472           used_at = 0;
4473         }
4474
4475       /* When generating embedded PIC code, we permit expressions of
4476          the form
4477            la   $treg,foo-bar
4478            la   $treg,foo-bar($breg)
4479          where bar is an address in the current section.  These are used
4480          when getting the addresses of functions.  We don't permit
4481          X_add_number to be non-zero, because if the symbol is
4482          external the relaxing code needs to know that any addend is
4483          purely the offset to X_op_symbol.  */
4484       if (mips_pic == EMBEDDED_PIC
4485           && offset_expr.X_op == O_subtract
4486           && (symbol_constant_p (offset_expr.X_op_symbol)
4487               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4488               : (symbol_equated_p (offset_expr.X_op_symbol)
4489                  && (S_GET_SEGMENT
4490                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4491                       ->X_add_symbol)
4492                      == now_seg)))
4493           && (offset_expr.X_add_number == 0
4494               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4495         {
4496           if (breg == 0)
4497             {
4498               tempreg = treg;
4499               used_at = 0;
4500               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4501                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4502             }
4503           else
4504             {
4505               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4506                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4507               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4508                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4509                            "d,v,t", tempreg, tempreg, breg);
4510             }
4511           macro_build ((char *) NULL, &icnt, &offset_expr,
4512                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4513                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4514           if (! used_at)
4515             return;
4516           break;
4517         }
4518
4519       if (offset_expr.X_op != O_symbol
4520           && offset_expr.X_op != O_constant)
4521         {
4522           as_bad (_("expression too complex"));
4523           offset_expr.X_op = O_constant;
4524         }
4525
4526       if (offset_expr.X_op == O_constant)
4527         load_register (&icnt, tempreg, &offset_expr,
4528                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4529                         ? (dbl || HAVE_64BIT_ADDRESSES)
4530                         : HAVE_64BIT_ADDRESSES));
4531       else if (mips_pic == NO_PIC)
4532         {
4533           /* If this is a reference to a GP relative symbol, we want
4534                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4535              Otherwise we want
4536                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4537                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4538              If we have a constant, we need two instructions anyhow,
4539              so we may as well always use the latter form.
4540
4541             With 64bit address space and a usable $at we want
4542               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4543               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4544               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4545               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4546               dsll32    $tempreg,0
4547               daddu     $tempreg,$tempreg,$at
4548
4549             If $at is already in use, we use an path which is suboptimal
4550             on superscalar processors.
4551               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4552               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4553               dsll      $tempreg,16
4554               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4555               dsll      $tempreg,16
4556               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4557           */
4558           char *p = NULL;
4559           if (HAVE_64BIT_ADDRESSES)
4560             {
4561               /* We don't do GP optimization for now because RELAX_ENCODE can't
4562                  hold the data for such large chunks.  */
4563
4564               if (used_at == 0 && ! mips_opts.noat)
4565                 {
4566                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4567                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4568                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4569                                AT, (int) BFD_RELOC_HI16_S);
4570                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4571                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4572                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4573                                AT, AT, (int) BFD_RELOC_LO16);
4574                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4575                                "d,w,<", tempreg, tempreg, 0);
4576                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4577                                "d,v,t", tempreg, tempreg, AT);
4578                   used_at = 1;
4579                 }
4580               else
4581                 {
4582                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4583                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4584                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4585                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4586                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4587                                tempreg, tempreg, 16);
4588                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4589                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4590                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4591                                tempreg, tempreg, 16);
4592                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4593                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4594                 }
4595             }
4596           else
4597             {
4598               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4599                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4600                 {
4601                   frag_grow (20);
4602                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4603                                "t,r,j", tempreg, mips_gp_register,
4604                                (int) BFD_RELOC_GPREL16);
4605                   p = frag_var (rs_machine_dependent, 8, 0,
4606                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4607                                               mips_opts.warn_about_macros),
4608                                 offset_expr.X_add_symbol, 0, NULL);
4609                 }
4610               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4611               if (p != NULL)
4612                 p += 4;
4613               macro_build (p, &icnt, &offset_expr, "addiu",
4614                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4615             }
4616         }
4617       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4618         {
4619           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4620
4621           /* If this is a reference to an external symbol, and there
4622              is no constant, we want
4623                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4624              or if tempreg is PIC_CALL_REG
4625                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4626              For a local symbol, we want
4627                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4628                nop
4629                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4630
4631              If we have a small constant, and this is a reference to
4632              an external symbol, we want
4633                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4634                nop
4635                addiu    $tempreg,$tempreg,<constant>
4636              For a local symbol, we want the same instruction
4637              sequence, but we output a BFD_RELOC_LO16 reloc on the
4638              addiu instruction.
4639
4640              If we have a large constant, and this is a reference to
4641              an external symbol, we want
4642                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4643                lui      $at,<hiconstant>
4644                addiu    $at,$at,<loconstant>
4645                addu     $tempreg,$tempreg,$at
4646              For a local symbol, we want the same instruction
4647              sequence, but we output a BFD_RELOC_LO16 reloc on the
4648              addiu instruction.  */
4649           expr1.X_add_number = offset_expr.X_add_number;
4650           offset_expr.X_add_number = 0;
4651           frag_grow (32);
4652           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4653             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4654           macro_build ((char *) NULL, &icnt, &offset_expr,
4655                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4656                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4657           if (expr1.X_add_number == 0)
4658             {
4659               int off;
4660               char *p;
4661
4662               if (breg == 0)
4663                 off = 0;
4664               else
4665                 {
4666                   /* We're going to put in an addu instruction using
4667                      tempreg, so we may as well insert the nop right
4668                      now.  */
4669                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4670                                "nop", "");
4671                   off = 4;
4672                 }
4673               p = frag_var (rs_machine_dependent, 8 - off, 0,
4674                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4675                                           (breg == 0
4676                                            ? mips_opts.warn_about_macros
4677                                            : 0)),
4678                             offset_expr.X_add_symbol, 0, NULL);
4679               if (breg == 0)
4680                 {
4681                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4682                   p += 4;
4683                 }
4684               macro_build (p, &icnt, &expr1,
4685                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4686                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4687               /* FIXME: If breg == 0, and the next instruction uses
4688                  $tempreg, then if this variant case is used an extra
4689                  nop will be generated.  */
4690             }
4691           else if (expr1.X_add_number >= -0x8000
4692                    && expr1.X_add_number < 0x8000)
4693             {
4694               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4695                            "nop", "");
4696               macro_build ((char *) NULL, &icnt, &expr1,
4697                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4698                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4699               frag_var (rs_machine_dependent, 0, 0,
4700                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4701                         offset_expr.X_add_symbol, 0, NULL);
4702             }
4703           else
4704             {
4705               int off1;
4706
4707               /* If we are going to add in a base register, and the
4708                  target register and the base register are the same,
4709                  then we are using AT as a temporary register.  Since
4710                  we want to load the constant into AT, we add our
4711                  current AT (from the global offset table) and the
4712                  register into the register now, and pretend we were
4713                  not using a base register.  */
4714               if (breg != treg)
4715                 off1 = 0;
4716               else
4717                 {
4718                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4719                                "nop", "");
4720                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4721                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4722                                "d,v,t", treg, AT, breg);
4723                   breg = 0;
4724                   tempreg = treg;
4725                   off1 = -8;
4726                 }
4727
4728               /* Set mips_optimize around the lui instruction to avoid
4729                  inserting an unnecessary nop after the lw.  */
4730               hold_mips_optimize = mips_optimize;
4731               mips_optimize = 2;
4732               macro_build_lui (NULL, &icnt, &expr1, AT);
4733               mips_optimize = hold_mips_optimize;
4734
4735               macro_build ((char *) NULL, &icnt, &expr1,
4736                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4737                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4738               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4739                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4740                            "d,v,t", tempreg, tempreg, AT);
4741               frag_var (rs_machine_dependent, 0, 0,
4742                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4743                         offset_expr.X_add_symbol, 0, NULL);
4744               used_at = 1;
4745             }
4746         }
4747       else if (mips_pic == SVR4_PIC)
4748         {
4749           int gpdel;
4750           char *p;
4751           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4752           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4753
4754           /* This is the large GOT case.  If this is a reference to an
4755              external symbol, and there is no constant, we want
4756                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4757                addu     $tempreg,$tempreg,$gp
4758                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4759              or if tempreg is PIC_CALL_REG
4760                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4761                addu     $tempreg,$tempreg,$gp
4762                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4763              For a local symbol, we want
4764                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4765                nop
4766                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4767
4768              If we have a small constant, and this is a reference to
4769              an external symbol, we want
4770                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4771                addu     $tempreg,$tempreg,$gp
4772                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4773                nop
4774                addiu    $tempreg,$tempreg,<constant>
4775              For a local symbol, we want
4776                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4777                nop
4778                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4779
4780              If we have a large constant, and this is a reference to
4781              an external symbol, we want
4782                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4783                addu     $tempreg,$tempreg,$gp
4784                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4785                lui      $at,<hiconstant>
4786                addiu    $at,$at,<loconstant>
4787                addu     $tempreg,$tempreg,$at
4788              For a local symbol, we want
4789                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4790                lui      $at,<hiconstant>
4791                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4792                addu     $tempreg,$tempreg,$at
4793
4794              For NewABI, we want for data addresses
4795                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4796              If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4797                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4798            */
4799           if (HAVE_NEWABI)
4800             {
4801               int reloc_type = (tempreg == PIC_CALL_REG
4802                                 ? BFD_RELOC_MIPS_CALL16
4803                                 : BFD_RELOC_MIPS_GOT_DISP);
4804
4805               macro_build ((char *) NULL, &icnt, &offset_expr,
4806                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4807                            "t,o(b)", tempreg, reloc_type, mips_gp_register);
4808
4809               if (breg != 0)
4810                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4811                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4812                              "d,v,t", treg, tempreg, breg);
4813
4814               if (! used_at)
4815                 return;
4816
4817               break;
4818             }
4819           expr1.X_add_number = offset_expr.X_add_number;
4820           offset_expr.X_add_number = 0;
4821           frag_grow (52);
4822           if (reg_needs_delay (mips_gp_register))
4823             gpdel = 4;
4824           else
4825             gpdel = 0;
4826           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4827             {
4828               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4829               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4830             }
4831           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4832                        tempreg, lui_reloc_type);
4833           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4834                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4835                        "d,v,t", tempreg, tempreg, mips_gp_register);
4836           macro_build ((char *) NULL, &icnt, &offset_expr,
4837                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4838                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4839           if (expr1.X_add_number == 0)
4840             {
4841               int off;
4842
4843               if (breg == 0)
4844                 off = 0;
4845               else
4846                 {
4847                   /* We're going to put in an addu instruction using
4848                      tempreg, so we may as well insert the nop right
4849                      now.  */
4850                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4851                                "nop", "");
4852                   off = 4;
4853                 }
4854
4855               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4856                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4857                                           8 + gpdel, 0,
4858                                           (breg == 0
4859                                            ? mips_opts.warn_about_macros
4860                                            : 0)),
4861                             offset_expr.X_add_symbol, 0, NULL);
4862             }
4863           else if (expr1.X_add_number >= -0x8000
4864                    && expr1.X_add_number < 0x8000)
4865             {
4866               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4867                            "nop", "");
4868               macro_build ((char *) NULL, &icnt, &expr1,
4869                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4870                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4871
4872               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4873                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4874                                           (breg == 0
4875                                            ? mips_opts.warn_about_macros
4876                                            : 0)),
4877                             offset_expr.X_add_symbol, 0, NULL);
4878             }
4879           else
4880             {
4881               int adj, dreg;
4882
4883               /* If we are going to add in a base register, and the
4884                  target register and the base register are the same,
4885                  then we are using AT as a temporary register.  Since
4886                  we want to load the constant into AT, we add our
4887                  current AT (from the global offset table) and the
4888                  register into the register now, and pretend we were
4889                  not using a base register.  */
4890               if (breg != treg)
4891                 {
4892                   adj = 0;
4893                   dreg = tempreg;
4894                 }
4895               else
4896                 {
4897                   assert (tempreg == AT);
4898                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4899                                "nop", "");
4900                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4901                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4902                                "d,v,t", treg, AT, breg);
4903                   dreg = treg;
4904                   adj = 8;
4905                 }
4906
4907               /* Set mips_optimize around the lui instruction to avoid
4908                  inserting an unnecessary nop after the lw.  */
4909               hold_mips_optimize = mips_optimize;
4910               mips_optimize = 2;
4911               macro_build_lui (NULL, &icnt, &expr1, AT);
4912               mips_optimize = hold_mips_optimize;
4913
4914               macro_build ((char *) NULL, &icnt, &expr1,
4915                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4916                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4917               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4918                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4919                            "d,v,t", dreg, dreg, AT);
4920
4921               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4922                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4923                                           8 + gpdel, 0,
4924                                           (breg == 0
4925                                            ? mips_opts.warn_about_macros
4926                                            : 0)),
4927                             offset_expr.X_add_symbol, 0, NULL);
4928
4929               used_at = 1;
4930             }
4931
4932           if (gpdel > 0)
4933             {
4934               /* This is needed because this instruction uses $gp, but
4935                  the first instruction on the main stream does not.  */
4936               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4937               p += 4;
4938             }
4939           macro_build (p, &icnt, &offset_expr,
4940                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4941                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4942                        mips_gp_register);
4943           p += 4;
4944           if (expr1.X_add_number >= -0x8000
4945               && expr1.X_add_number < 0x8000)
4946             {
4947               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4948               p += 4;
4949               macro_build (p, &icnt, &expr1,
4950                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4951                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4952               /* FIXME: If add_number is 0, and there was no base
4953                  register, the external symbol case ended with a load,
4954                  so if the symbol turns out to not be external, and
4955                  the next instruction uses tempreg, an unnecessary nop
4956                  will be inserted.  */
4957             }
4958           else
4959             {
4960               if (breg == treg)
4961                 {
4962                   /* We must add in the base register now, as in the
4963                      external symbol case.  */
4964                   assert (tempreg == AT);
4965                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4966                   p += 4;
4967                   macro_build (p, &icnt, (expressionS *) NULL,
4968                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4969                                "d,v,t", treg, AT, breg);
4970                   p += 4;
4971                   tempreg = treg;
4972                   /* We set breg to 0 because we have arranged to add
4973                      it in in both cases.  */
4974                   breg = 0;
4975                 }
4976
4977               macro_build_lui (p, &icnt, &expr1, AT);
4978               p += 4;
4979               macro_build (p, &icnt, &expr1,
4980                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4981                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4982               p += 4;
4983               macro_build (p, &icnt, (expressionS *) NULL,
4984                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4985                            "d,v,t", tempreg, tempreg, AT);
4986               p += 4;
4987             }
4988         }
4989       else if (mips_pic == EMBEDDED_PIC)
4990         {
4991           /* We use
4992                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4993              */
4994           macro_build ((char *) NULL, &icnt, &offset_expr,
4995                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4996                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4997         }
4998       else
4999         abort ();
5000
5001       if (breg != 0)
5002         {
5003           char *s;
5004
5005           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5006             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5007           else
5008             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5009
5010           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5011                        "d,v,t", treg, tempreg, breg);
5012         }
5013
5014       if (! used_at)
5015         return;
5016
5017       break;
5018
5019     case M_J_A:
5020       /* The j instruction may not be used in PIC code, since it
5021          requires an absolute address.  We convert it to a b
5022          instruction.  */
5023       if (mips_pic == NO_PIC)
5024         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5025       else
5026         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5027       return;
5028
5029       /* The jal instructions must be handled as macros because when
5030          generating PIC code they expand to multi-instruction
5031          sequences.  Normally they are simple instructions.  */
5032     case M_JAL_1:
5033       dreg = RA;
5034       /* Fall through.  */
5035     case M_JAL_2:
5036       if (mips_pic == NO_PIC
5037           || mips_pic == EMBEDDED_PIC)
5038         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5039                      "d,s", dreg, sreg);
5040       else if (mips_pic == SVR4_PIC)
5041         {
5042           if (sreg != PIC_CALL_REG)
5043             as_warn (_("MIPS PIC call to register other than $25"));
5044
5045           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5046                        "d,s", dreg, sreg);
5047           if (! HAVE_NEWABI)
5048             {
5049               if (mips_cprestore_offset < 0)
5050                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5051               else
5052                 {
5053                   if (! mips_frame_reg_valid)
5054                     {
5055                       as_warn (_("No .frame pseudo-op used in PIC code"));
5056                       /* Quiet this warning.  */
5057                       mips_frame_reg_valid = 1;
5058                     }
5059                   if (! mips_cprestore_valid)
5060                     {
5061                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5062                       /* Quiet this warning.  */
5063                       mips_cprestore_valid = 1;
5064                     }
5065                   expr1.X_add_number = mips_cprestore_offset;
5066                   macro_build ((char *) NULL, &icnt, &expr1,
5067                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5068                                mips_gp_register, (int) BFD_RELOC_LO16,
5069                                mips_frame_reg);
5070                 }
5071             }
5072         }
5073       else
5074         abort ();
5075
5076       return;
5077
5078     case M_JAL_A:
5079       if (mips_pic == NO_PIC)
5080         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5081       else if (mips_pic == SVR4_PIC)
5082         {
5083           char *p;
5084
5085           /* If this is a reference to an external symbol, and we are
5086              using a small GOT, we want
5087                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5088                nop
5089                jalr     $ra,$25
5090                nop
5091                lw       $gp,cprestore($sp)
5092              The cprestore value is set using the .cprestore
5093              pseudo-op.  If we are using a big GOT, we want
5094                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5095                addu     $25,$25,$gp
5096                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5097                nop
5098                jalr     $ra,$25
5099                nop
5100                lw       $gp,cprestore($sp)
5101              If the symbol is not external, we want
5102                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5103                nop
5104                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5105                jalr     $ra,$25
5106                nop
5107                lw $gp,cprestore($sp)
5108              For NewABI, we want
5109                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5110                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5111            */
5112           if (HAVE_NEWABI)
5113             {
5114               macro_build ((char *) NULL, &icnt, &offset_expr,
5115                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5116                            "t,o(b)", PIC_CALL_REG,
5117                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5118               macro_build_jalr (icnt, &offset_expr);
5119             }
5120           else
5121             {
5122               frag_grow (40);
5123               if (! mips_big_got)
5124                 {
5125                   macro_build ((char *) NULL, &icnt, &offset_expr,
5126                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5127                                "t,o(b)", PIC_CALL_REG,
5128                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5129                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5130                                "nop", "");
5131                   p = frag_var (rs_machine_dependent, 4, 0,
5132                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5133                                 offset_expr.X_add_symbol, 0, NULL);
5134                 }
5135               else
5136                 {
5137                   int gpdel;
5138
5139                   if (reg_needs_delay (mips_gp_register))
5140                     gpdel = 4;
5141                   else
5142                     gpdel = 0;
5143                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5144                                "t,u", PIC_CALL_REG,
5145                                (int) BFD_RELOC_MIPS_CALL_HI16);
5146                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5147                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5148                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5149                                mips_gp_register);
5150                   macro_build ((char *) NULL, &icnt, &offset_expr,
5151                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5152                                "t,o(b)", PIC_CALL_REG,
5153                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5154                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5155                                "nop", "");
5156                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5157                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5158                                               8 + gpdel, 0, 0),
5159                                 offset_expr.X_add_symbol, 0, NULL);
5160                   if (gpdel > 0)
5161                     {
5162                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5163                       p += 4;
5164                     }
5165                   macro_build (p, &icnt, &offset_expr,
5166                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5167                                "t,o(b)", PIC_CALL_REG,
5168                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5169                   p += 4;
5170                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5171                   p += 4;
5172                 }
5173               macro_build (p, &icnt, &offset_expr,
5174                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5175                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5176                            (int) BFD_RELOC_LO16);
5177               macro_build_jalr (icnt, &offset_expr);
5178
5179               if (mips_cprestore_offset < 0)
5180                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5181               else
5182                 {
5183                   if (! mips_frame_reg_valid)
5184                     {
5185                       as_warn (_("No .frame pseudo-op used in PIC code"));
5186                       /* Quiet this warning.  */
5187                       mips_frame_reg_valid = 1;
5188                     }
5189                   if (! mips_cprestore_valid)
5190                     {
5191                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5192                       /* Quiet this warning.  */
5193                       mips_cprestore_valid = 1;
5194                     }
5195                   if (mips_opts.noreorder)
5196                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5197                                  "nop", "");
5198                   expr1.X_add_number = mips_cprestore_offset;
5199                   macro_build ((char *) NULL, &icnt, &expr1,
5200                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5201                                mips_gp_register, (int) BFD_RELOC_LO16,
5202                                mips_frame_reg);
5203                 }
5204             }
5205         }
5206       else if (mips_pic == EMBEDDED_PIC)
5207         {
5208           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5209           /* The linker may expand the call to a longer sequence which
5210              uses $at, so we must break rather than return.  */
5211           break;
5212         }
5213       else
5214         abort ();
5215
5216       return;
5217
5218     case M_LB_AB:
5219       s = "lb";
5220       goto ld;
5221     case M_LBU_AB:
5222       s = "lbu";
5223       goto ld;
5224     case M_LH_AB:
5225       s = "lh";
5226       goto ld;
5227     case M_LHU_AB:
5228       s = "lhu";
5229       goto ld;
5230     case M_LW_AB:
5231       s = "lw";
5232       goto ld;
5233     case M_LWC0_AB:
5234       s = "lwc0";
5235       /* Itbl support may require additional care here.  */
5236       coproc = 1;
5237       goto ld;
5238     case M_LWC1_AB:
5239       s = "lwc1";
5240       /* Itbl support may require additional care here.  */
5241       coproc = 1;
5242       goto ld;
5243     case M_LWC2_AB:
5244       s = "lwc2";
5245       /* Itbl support may require additional care here.  */
5246       coproc = 1;
5247       goto ld;
5248     case M_LWC3_AB:
5249       s = "lwc3";
5250       /* Itbl support may require additional care here.  */
5251       coproc = 1;
5252       goto ld;
5253     case M_LWL_AB:
5254       s = "lwl";
5255       lr = 1;
5256       goto ld;
5257     case M_LWR_AB:
5258       s = "lwr";
5259       lr = 1;
5260       goto ld;
5261     case M_LDC1_AB:
5262       if (mips_arch == CPU_R4650)
5263         {
5264           as_bad (_("opcode not supported on this processor"));
5265           return;
5266         }
5267       s = "ldc1";
5268       /* Itbl support may require additional care here.  */
5269       coproc = 1;
5270       goto ld;
5271     case M_LDC2_AB:
5272       s = "ldc2";
5273       /* Itbl support may require additional care here.  */
5274       coproc = 1;
5275       goto ld;
5276     case M_LDC3_AB:
5277       s = "ldc3";
5278       /* Itbl support may require additional care here.  */
5279       coproc = 1;
5280       goto ld;
5281     case M_LDL_AB:
5282       s = "ldl";
5283       lr = 1;
5284       goto ld;
5285     case M_LDR_AB:
5286       s = "ldr";
5287       lr = 1;
5288       goto ld;
5289     case M_LL_AB:
5290       s = "ll";
5291       goto ld;
5292     case M_LLD_AB:
5293       s = "lld";
5294       goto ld;
5295     case M_LWU_AB:
5296       s = "lwu";
5297     ld:
5298       if (breg == treg || coproc || lr)
5299         {
5300           tempreg = AT;
5301           used_at = 1;
5302         }
5303       else
5304         {
5305           tempreg = treg;
5306           used_at = 0;
5307         }
5308       goto ld_st;
5309     case M_SB_AB:
5310       s = "sb";
5311       goto st;
5312     case M_SH_AB:
5313       s = "sh";
5314       goto st;
5315     case M_SW_AB:
5316       s = "sw";
5317       goto st;
5318     case M_SWC0_AB:
5319       s = "swc0";
5320       /* Itbl support may require additional care here.  */
5321       coproc = 1;
5322       goto st;
5323     case M_SWC1_AB:
5324       s = "swc1";
5325       /* Itbl support may require additional care here.  */
5326       coproc = 1;
5327       goto st;
5328     case M_SWC2_AB:
5329       s = "swc2";
5330       /* Itbl support may require additional care here.  */
5331       coproc = 1;
5332       goto st;
5333     case M_SWC3_AB:
5334       s = "swc3";
5335       /* Itbl support may require additional care here.  */
5336       coproc = 1;
5337       goto st;
5338     case M_SWL_AB:
5339       s = "swl";
5340       goto st;
5341     case M_SWR_AB:
5342       s = "swr";
5343       goto st;
5344     case M_SC_AB:
5345       s = "sc";
5346       goto st;
5347     case M_SCD_AB:
5348       s = "scd";
5349       goto st;
5350     case M_SDC1_AB:
5351       if (mips_arch == CPU_R4650)
5352         {
5353           as_bad (_("opcode not supported on this processor"));
5354           return;
5355         }
5356       s = "sdc1";
5357       coproc = 1;
5358       /* Itbl support may require additional care here.  */
5359       goto st;
5360     case M_SDC2_AB:
5361       s = "sdc2";
5362       /* Itbl support may require additional care here.  */
5363       coproc = 1;
5364       goto st;
5365     case M_SDC3_AB:
5366       s = "sdc3";
5367       /* Itbl support may require additional care here.  */
5368       coproc = 1;
5369       goto st;
5370     case M_SDL_AB:
5371       s = "sdl";
5372       goto st;
5373     case M_SDR_AB:
5374       s = "sdr";
5375     st:
5376       tempreg = AT;
5377       used_at = 1;
5378     ld_st:
5379       /* Itbl support may require additional care here.  */
5380       if (mask == M_LWC1_AB
5381           || mask == M_SWC1_AB
5382           || mask == M_LDC1_AB
5383           || mask == M_SDC1_AB
5384           || mask == M_L_DAB
5385           || mask == M_S_DAB)
5386         fmt = "T,o(b)";
5387       else if (coproc)
5388         fmt = "E,o(b)";
5389       else
5390         fmt = "t,o(b)";
5391
5392       /* For embedded PIC, we allow loads where the offset is calculated
5393          by subtracting a symbol in the current segment from an unknown
5394          symbol, relative to a base register, e.g.:
5395                 <op>    $treg, <sym>-<localsym>($breg)
5396          This is used by the compiler for switch statements.  */
5397       if (mips_pic == EMBEDDED_PIC
5398           && offset_expr.X_op == O_subtract
5399           && (symbol_constant_p (offset_expr.X_op_symbol)
5400               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5401               : (symbol_equated_p (offset_expr.X_op_symbol)
5402                  && (S_GET_SEGMENT
5403                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5404                       ->X_add_symbol)
5405                      == now_seg)))
5406           && breg != 0
5407           && (offset_expr.X_add_number == 0
5408               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5409         {
5410           /* For this case, we output the instructions:
5411                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5412                 addiu   $tempreg,$tempreg,$breg
5413                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5414              If the relocation would fit entirely in 16 bits, it would be
5415              nice to emit:
5416                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5417              instead, but that seems quite difficult.  */
5418           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5419                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5420           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5421                        ((bfd_arch_bits_per_address (stdoutput) == 32
5422                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5423                         ? "addu" : "daddu"),
5424                        "d,v,t", tempreg, tempreg, breg);
5425           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5426                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5427           if (! used_at)
5428             return;
5429           break;
5430         }
5431
5432       if (offset_expr.X_op != O_constant
5433           && offset_expr.X_op != O_symbol)
5434         {
5435           as_bad (_("expression too complex"));
5436           offset_expr.X_op = O_constant;
5437         }
5438
5439       /* A constant expression in PIC code can be handled just as it
5440          is in non PIC code.  */
5441       if (mips_pic == NO_PIC
5442           || offset_expr.X_op == O_constant)
5443         {
5444           char *p;
5445
5446           /* If this is a reference to a GP relative symbol, and there
5447              is no base register, we want
5448                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5449              Otherwise, if there is no base register, we want
5450                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5451                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5452              If we have a constant, we need two instructions anyhow,
5453              so we always use the latter form.
5454
5455              If we have a base register, and this is a reference to a
5456              GP relative symbol, we want
5457                addu     $tempreg,$breg,$gp
5458                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5459              Otherwise we want
5460                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5461                addu     $tempreg,$tempreg,$breg
5462                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5463              With a constant we always use the latter case.
5464
5465              With 64bit address space and no base register and $at usable,
5466              we want
5467                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5468                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5469                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5470                dsll32   $tempreg,0
5471                daddu    $tempreg,$at
5472                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5473              If we have a base register, we want
5474                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5475                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5476                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5477                daddu    $at,$breg
5478                dsll32   $tempreg,0
5479                daddu    $tempreg,$at
5480                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5481
5482              Without $at we can't generate the optimal path for superscalar
5483              processors here since this would require two temporary registers.
5484                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5485                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5486                dsll     $tempreg,16
5487                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5488                dsll     $tempreg,16
5489                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5490              If we have a base register, we want
5491                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5492                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5493                dsll     $tempreg,16
5494                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5495                dsll     $tempreg,16
5496                daddu    $tempreg,$tempreg,$breg
5497                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5498
5499              If we have 64-bit addresses, as an optimization, for
5500              addresses which are 32-bit constants (e.g. kseg0/kseg1
5501              addresses) we fall back to the 32-bit address generation
5502              mechanism since it is more efficient.  Note that due to
5503              the signed offset used by memory operations, the 32-bit
5504              range is shifted down by 32768 here.  This code should
5505              probably attempt to generate 64-bit constants more
5506              efficiently in general.
5507            */
5508           if (HAVE_64BIT_ADDRESSES
5509               && !(offset_expr.X_op == O_constant
5510                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5511             {
5512               p = NULL;
5513
5514               /* We don't do GP optimization for now because RELAX_ENCODE can't
5515                  hold the data for such large chunks.  */
5516
5517               if (used_at == 0 && ! mips_opts.noat)
5518                 {
5519                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5520                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5521                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5522                                AT, (int) BFD_RELOC_HI16_S);
5523                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5524                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5525                   if (breg != 0)
5526                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5527                                  "d,v,t", AT, AT, breg);
5528                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5529                                "d,w,<", tempreg, tempreg, 0);
5530                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5531                                "d,v,t", tempreg, tempreg, AT);
5532                   macro_build (p, &icnt, &offset_expr, s,
5533                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5534                   used_at = 1;
5535                 }
5536               else
5537                 {
5538                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5539                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5540                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5541                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5542                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5543                                "d,w,<", tempreg, tempreg, 16);
5544                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5545                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5546                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5547                                "d,w,<", tempreg, tempreg, 16);
5548                   if (breg != 0)
5549                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5550                                  "d,v,t", tempreg, tempreg, breg);
5551                   macro_build (p, &icnt, &offset_expr, s,
5552                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5553                 }
5554
5555               return;
5556             }
5557
5558           if (breg == 0)
5559             {
5560               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5561                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5562                 p = NULL;
5563               else
5564                 {
5565                   frag_grow (20);
5566                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5567                                treg, (int) BFD_RELOC_GPREL16,
5568                                mips_gp_register);
5569                   p = frag_var (rs_machine_dependent, 8, 0,
5570                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5571                                               (mips_opts.warn_about_macros
5572                                                || (used_at
5573                                                    && mips_opts.noat))),
5574                                 offset_expr.X_add_symbol, 0, NULL);
5575                   used_at = 0;
5576                 }
5577               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5578               if (p != NULL)
5579                 p += 4;
5580               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5581                            (int) BFD_RELOC_LO16, tempreg);
5582             }
5583           else
5584             {
5585               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5586                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5587                 p = NULL;
5588               else
5589                 {
5590                   frag_grow (28);
5591                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5592                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5593                                "d,v,t", tempreg, breg, mips_gp_register);
5594                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5595                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5596                   p = frag_var (rs_machine_dependent, 12, 0,
5597                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5598                                 offset_expr.X_add_symbol, 0, NULL);
5599                 }
5600               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5601               if (p != NULL)
5602                 p += 4;
5603               macro_build (p, &icnt, (expressionS *) NULL,
5604                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5605                            "d,v,t", tempreg, tempreg, breg);
5606               if (p != NULL)
5607                 p += 4;
5608               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5609                            (int) BFD_RELOC_LO16, tempreg);
5610             }
5611         }
5612       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5613         {
5614           char *p;
5615
5616           /* If this is a reference to an external symbol, we want
5617                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5618                nop
5619                <op>     $treg,0($tempreg)
5620              Otherwise we want
5621                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5622                nop
5623                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5624                <op>     $treg,0($tempreg)
5625              If there is a base register, we add it to $tempreg before
5626              the <op>.  If there is a constant, we stick it in the
5627              <op> instruction.  We don't handle constants larger than
5628              16 bits, because we have no way to load the upper 16 bits
5629              (actually, we could handle them for the subset of cases
5630              in which we are not using $at).  */
5631           assert (offset_expr.X_op == O_symbol);
5632           expr1.X_add_number = offset_expr.X_add_number;
5633           offset_expr.X_add_number = 0;
5634           if (expr1.X_add_number < -0x8000
5635               || expr1.X_add_number >= 0x8000)
5636             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5637           frag_grow (20);
5638           macro_build ((char *) NULL, &icnt, &offset_expr,
5639                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5640                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5641           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5642           p = frag_var (rs_machine_dependent, 4, 0,
5643                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5644                         offset_expr.X_add_symbol, 0, NULL);
5645           macro_build (p, &icnt, &offset_expr,
5646                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5647                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5648           if (breg != 0)
5649             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5650                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5651                          "d,v,t", tempreg, tempreg, breg);
5652           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5653                        (int) BFD_RELOC_LO16, tempreg);
5654         }
5655       else if (mips_pic == SVR4_PIC)
5656         {
5657           int gpdel;
5658           char *p;
5659
5660           /* If this is a reference to an external symbol, we want
5661                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5662                addu     $tempreg,$tempreg,$gp
5663                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5664                <op>     $treg,0($tempreg)
5665              Otherwise we want
5666                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5667                nop
5668                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5669                <op>     $treg,0($tempreg)
5670              If there is a base register, we add it to $tempreg before
5671              the <op>.  If there is a constant, we stick it in the
5672              <op> instruction.  We don't handle constants larger than
5673              16 bits, because we have no way to load the upper 16 bits
5674              (actually, we could handle them for the subset of cases
5675              in which we are not using $at).
5676
5677              For NewABI, we want
5678                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5679                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5680                <op>     $treg,0($tempreg)
5681            */
5682           assert (offset_expr.X_op == O_symbol);
5683           expr1.X_add_number = offset_expr.X_add_number;
5684           offset_expr.X_add_number = 0;
5685           if (expr1.X_add_number < -0x8000
5686               || expr1.X_add_number >= 0x8000)
5687             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5688           if (HAVE_NEWABI)
5689             {
5690               macro_build ((char *) NULL, &icnt, &offset_expr,
5691                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5692                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5693                            mips_gp_register);
5694               macro_build ((char *) NULL, &icnt, &offset_expr,
5695                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5696                            "t,r,j", tempreg, tempreg,
5697                            BFD_RELOC_MIPS_GOT_OFST);
5698               if (breg != 0)
5699                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5700                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5701                              "d,v,t", tempreg, tempreg, breg);
5702               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5703                            (int) BFD_RELOC_LO16, tempreg);
5704
5705               if (! used_at)
5706                 return;
5707
5708               break;
5709             }
5710           if (reg_needs_delay (mips_gp_register))
5711             gpdel = 4;
5712           else
5713             gpdel = 0;
5714           frag_grow (36);
5715           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5716                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5717           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5718                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5719                        "d,v,t", tempreg, tempreg, mips_gp_register);
5720           macro_build ((char *) NULL, &icnt, &offset_expr,
5721                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5722                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5723                        tempreg);
5724           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5725                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5726                         offset_expr.X_add_symbol, 0, NULL);
5727           if (gpdel > 0)
5728             {
5729               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5730               p += 4;
5731             }
5732           macro_build (p, &icnt, &offset_expr,
5733                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5734                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5735                        mips_gp_register);
5736           p += 4;
5737           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5738           p += 4;
5739           macro_build (p, &icnt, &offset_expr,
5740                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5741                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5742           if (breg != 0)
5743             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5744                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5745                          "d,v,t", tempreg, tempreg, breg);
5746           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5747                        (int) BFD_RELOC_LO16, tempreg);
5748         }
5749       else if (mips_pic == EMBEDDED_PIC)
5750         {
5751           /* If there is no base register, we want
5752                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5753              If there is a base register, we want
5754                addu     $tempreg,$breg,$gp
5755                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5756              */
5757           assert (offset_expr.X_op == O_symbol);
5758           if (breg == 0)
5759             {
5760               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5761                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5762               used_at = 0;
5763             }
5764           else
5765             {
5766               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5767                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5768                            "d,v,t", tempreg, breg, mips_gp_register);
5769               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5770                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5771             }
5772         }
5773       else
5774         abort ();
5775
5776       if (! used_at)
5777         return;
5778
5779       break;
5780
5781     case M_LI:
5782     case M_LI_S:
5783       load_register (&icnt, treg, &imm_expr, 0);
5784       return;
5785
5786     case M_DLI:
5787       load_register (&icnt, treg, &imm_expr, 1);
5788       return;
5789
5790     case M_LI_SS:
5791       if (imm_expr.X_op == O_constant)
5792         {
5793           load_register (&icnt, AT, &imm_expr, 0);
5794           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5795                        "mtc1", "t,G", AT, treg);
5796           break;
5797         }
5798       else
5799         {
5800           assert (offset_expr.X_op == O_symbol
5801                   && strcmp (segment_name (S_GET_SEGMENT
5802                                            (offset_expr.X_add_symbol)),
5803                              ".lit4") == 0
5804                   && offset_expr.X_add_number == 0);
5805           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5806                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
5807           return;
5808         }
5809
5810     case M_LI_D:
5811       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5812          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5813          order 32 bits of the value and the low order 32 bits are either
5814          zero or in OFFSET_EXPR.  */
5815       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5816         {
5817           if (HAVE_64BIT_GPRS)
5818             load_register (&icnt, treg, &imm_expr, 1);
5819           else
5820             {
5821               int hreg, lreg;
5822
5823               if (target_big_endian)
5824                 {
5825                   hreg = treg;
5826                   lreg = treg + 1;
5827                 }
5828               else
5829                 {
5830                   hreg = treg + 1;
5831                   lreg = treg;
5832                 }
5833
5834               if (hreg <= 31)
5835                 load_register (&icnt, hreg, &imm_expr, 0);
5836               if (lreg <= 31)
5837                 {
5838                   if (offset_expr.X_op == O_absent)
5839                     move_register (&icnt, lreg, 0);
5840                   else
5841                     {
5842                       assert (offset_expr.X_op == O_constant);
5843                       load_register (&icnt, lreg, &offset_expr, 0);
5844                     }
5845                 }
5846             }
5847           return;
5848         }
5849
5850       /* We know that sym is in the .rdata section.  First we get the
5851          upper 16 bits of the address.  */
5852       if (mips_pic == NO_PIC)
5853         {
5854           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5855         }
5856       else if (mips_pic == SVR4_PIC)
5857         {
5858           macro_build ((char *) NULL, &icnt, &offset_expr,
5859                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5860                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5861                        mips_gp_register);
5862         }
5863       else if (mips_pic == EMBEDDED_PIC)
5864         {
5865           /* For embedded PIC we pick up the entire address off $gp in
5866              a single instruction.  */
5867           macro_build ((char *) NULL, &icnt, &offset_expr,
5868                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5869                        mips_gp_register, (int) BFD_RELOC_GPREL16);
5870           offset_expr.X_op = O_constant;
5871           offset_expr.X_add_number = 0;
5872         }
5873       else
5874         abort ();
5875
5876       /* Now we load the register(s).  */
5877       if (HAVE_64BIT_GPRS)
5878         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5879                      treg, (int) BFD_RELOC_LO16, AT);
5880       else
5881         {
5882           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5883                        treg, (int) BFD_RELOC_LO16, AT);
5884           if (treg != RA)
5885             {
5886               /* FIXME: How in the world do we deal with the possible
5887                  overflow here?  */
5888               offset_expr.X_add_number += 4;
5889               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5890                            treg + 1, (int) BFD_RELOC_LO16, AT);
5891             }
5892         }
5893
5894       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5895          does not become a variant frag.  */
5896       frag_wane (frag_now);
5897       frag_new (0);
5898
5899       break;
5900
5901     case M_LI_DD:
5902       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5903          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5904          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5905          the value and the low order 32 bits are either zero or in
5906          OFFSET_EXPR.  */
5907       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5908         {
5909           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5910           if (HAVE_64BIT_FPRS)
5911             {
5912               assert (HAVE_64BIT_GPRS);
5913               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5914                            "dmtc1", "t,S", AT, treg);
5915             }
5916           else
5917             {
5918               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5919                            "mtc1", "t,G", AT, treg + 1);
5920               if (offset_expr.X_op == O_absent)
5921                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5922                              "mtc1", "t,G", 0, treg);
5923               else
5924                 {
5925                   assert (offset_expr.X_op == O_constant);
5926                   load_register (&icnt, AT, &offset_expr, 0);
5927                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5928                                "mtc1", "t,G", AT, treg);
5929                 }
5930             }
5931           break;
5932         }
5933
5934       assert (offset_expr.X_op == O_symbol
5935               && offset_expr.X_add_number == 0);
5936       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5937       if (strcmp (s, ".lit8") == 0)
5938         {
5939           if (mips_opts.isa != ISA_MIPS1)
5940             {
5941               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5942                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5943                            mips_gp_register);
5944               return;
5945             }
5946           breg = mips_gp_register;
5947           r = BFD_RELOC_MIPS_LITERAL;
5948           goto dob;
5949         }
5950       else
5951         {
5952           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5953           if (mips_pic == SVR4_PIC)
5954             macro_build ((char *) NULL, &icnt, &offset_expr,
5955                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5956                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5957                          mips_gp_register);
5958           else
5959             {
5960               /* FIXME: This won't work for a 64 bit address.  */
5961               macro_build_lui (NULL, &icnt, &offset_expr, AT);
5962             }
5963
5964           if (mips_opts.isa != ISA_MIPS1)
5965             {
5966               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5967                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5968
5969               /* To avoid confusion in tc_gen_reloc, we must ensure
5970                  that this does not become a variant frag.  */
5971               frag_wane (frag_now);
5972               frag_new (0);
5973
5974               break;
5975             }
5976           breg = AT;
5977           r = BFD_RELOC_LO16;
5978           goto dob;
5979         }
5980
5981     case M_L_DOB:
5982       if (mips_arch == CPU_R4650)
5983         {
5984           as_bad (_("opcode not supported on this processor"));
5985           return;
5986         }
5987       /* Even on a big endian machine $fn comes before $fn+1.  We have
5988          to adjust when loading from memory.  */
5989       r = BFD_RELOC_LO16;
5990     dob:
5991       assert (mips_opts.isa == ISA_MIPS1);
5992       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5993                    target_big_endian ? treg + 1 : treg,
5994                    (int) r, breg);
5995       /* FIXME: A possible overflow which I don't know how to deal
5996          with.  */
5997       offset_expr.X_add_number += 4;
5998       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5999                    target_big_endian ? treg : treg + 1,
6000                    (int) r, breg);
6001
6002       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6003          does not become a variant frag.  */
6004       frag_wane (frag_now);
6005       frag_new (0);
6006
6007       if (breg != AT)
6008         return;
6009       break;
6010
6011     case M_L_DAB:
6012       /*
6013        * The MIPS assembler seems to check for X_add_number not
6014        * being double aligned and generating:
6015        *        lui     at,%hi(foo+1)
6016        *        addu    at,at,v1
6017        *        addiu   at,at,%lo(foo+1)
6018        *        lwc1    f2,0(at)
6019        *        lwc1    f3,4(at)
6020        * But, the resulting address is the same after relocation so why
6021        * generate the extra instruction?
6022        */
6023       if (mips_arch == CPU_R4650)
6024         {
6025           as_bad (_("opcode not supported on this processor"));
6026           return;
6027         }
6028       /* Itbl support may require additional care here.  */
6029       coproc = 1;
6030       if (mips_opts.isa != ISA_MIPS1)
6031         {
6032           s = "ldc1";
6033           goto ld;
6034         }
6035
6036       s = "lwc1";
6037       fmt = "T,o(b)";
6038       goto ldd_std;
6039
6040     case M_S_DAB:
6041       if (mips_arch == CPU_R4650)
6042         {
6043           as_bad (_("opcode not supported on this processor"));
6044           return;
6045         }
6046
6047       if (mips_opts.isa != ISA_MIPS1)
6048         {
6049           s = "sdc1";
6050           goto st;
6051         }
6052
6053       s = "swc1";
6054       fmt = "T,o(b)";
6055       /* Itbl support may require additional care here.  */
6056       coproc = 1;
6057       goto ldd_std;
6058
6059     case M_LD_AB:
6060       if (HAVE_64BIT_GPRS)
6061         {
6062           s = "ld";
6063           goto ld;
6064         }
6065
6066       s = "lw";
6067       fmt = "t,o(b)";
6068       goto ldd_std;
6069
6070     case M_SD_AB:
6071       if (HAVE_64BIT_GPRS)
6072         {
6073           s = "sd";
6074           goto st;
6075         }
6076
6077       s = "sw";
6078       fmt = "t,o(b)";
6079
6080     ldd_std:
6081       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6082          loads for the case of doing a pair of loads to simulate an 'ld'.
6083          This is not currently done by the compiler, and assembly coders
6084          writing embedded-pic code can cope.  */
6085
6086       if (offset_expr.X_op != O_symbol
6087           && offset_expr.X_op != O_constant)
6088         {
6089           as_bad (_("expression too complex"));
6090           offset_expr.X_op = O_constant;
6091         }
6092
6093       /* Even on a big endian machine $fn comes before $fn+1.  We have
6094          to adjust when loading from memory.  We set coproc if we must
6095          load $fn+1 first.  */
6096       /* Itbl support may require additional care here.  */
6097       if (! target_big_endian)
6098         coproc = 0;
6099
6100       if (mips_pic == NO_PIC
6101           || offset_expr.X_op == O_constant)
6102         {
6103           char *p;
6104
6105           /* If this is a reference to a GP relative symbol, we want
6106                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6107                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6108              If we have a base register, we use this
6109                addu     $at,$breg,$gp
6110                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6111                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6112              If this is not a GP relative symbol, we want
6113                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6114                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6115                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6116              If there is a base register, we add it to $at after the
6117              lui instruction.  If there is a constant, we always use
6118              the last case.  */
6119           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6120               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6121             {
6122               p = NULL;
6123               used_at = 1;
6124             }
6125           else
6126             {
6127               int off;
6128
6129               if (breg == 0)
6130                 {
6131                   frag_grow (28);
6132                   tempreg = mips_gp_register;
6133                   off = 0;
6134                   used_at = 0;
6135                 }
6136               else
6137                 {
6138                   frag_grow (36);
6139                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6140                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6141                                "d,v,t", AT, breg, mips_gp_register);
6142                   tempreg = AT;
6143                   off = 4;
6144                   used_at = 1;
6145                 }
6146
6147               /* Itbl support may require additional care here.  */
6148               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6149                            coproc ? treg + 1 : treg,
6150                            (int) BFD_RELOC_GPREL16, tempreg);
6151               offset_expr.X_add_number += 4;
6152
6153               /* Set mips_optimize to 2 to avoid inserting an
6154                  undesired nop.  */
6155               hold_mips_optimize = mips_optimize;
6156               mips_optimize = 2;
6157               /* Itbl support may require additional care here.  */
6158               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6159                            coproc ? treg : treg + 1,
6160                            (int) BFD_RELOC_GPREL16, tempreg);
6161               mips_optimize = hold_mips_optimize;
6162
6163               p = frag_var (rs_machine_dependent, 12 + off, 0,
6164                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6165                                           used_at && mips_opts.noat),
6166                             offset_expr.X_add_symbol, 0, NULL);
6167
6168               /* We just generated two relocs.  When tc_gen_reloc
6169                  handles this case, it will skip the first reloc and
6170                  handle the second.  The second reloc already has an
6171                  extra addend of 4, which we added above.  We must
6172                  subtract it out, and then subtract another 4 to make
6173                  the first reloc come out right.  The second reloc
6174                  will come out right because we are going to add 4 to
6175                  offset_expr when we build its instruction below.
6176
6177                  If we have a symbol, then we don't want to include
6178                  the offset, because it will wind up being included
6179                  when we generate the reloc.  */
6180
6181               if (offset_expr.X_op == O_constant)
6182                 offset_expr.X_add_number -= 8;
6183               else
6184                 {
6185                   offset_expr.X_add_number = -4;
6186                   offset_expr.X_op = O_constant;
6187                 }
6188             }
6189           macro_build_lui (p, &icnt, &offset_expr, AT);
6190           if (p != NULL)
6191             p += 4;
6192           if (breg != 0)
6193             {
6194               macro_build (p, &icnt, (expressionS *) NULL,
6195                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6196                            "d,v,t", AT, breg, AT);
6197               if (p != NULL)
6198                 p += 4;
6199             }
6200           /* Itbl support may require additional care here.  */
6201           macro_build (p, &icnt, &offset_expr, s, fmt,
6202                        coproc ? treg + 1 : treg,
6203                        (int) BFD_RELOC_LO16, AT);
6204           if (p != NULL)
6205             p += 4;
6206           /* FIXME: How do we handle overflow here?  */
6207           offset_expr.X_add_number += 4;
6208           /* Itbl support may require additional care here.  */
6209           macro_build (p, &icnt, &offset_expr, s, fmt,
6210                        coproc ? treg : treg + 1,
6211                        (int) BFD_RELOC_LO16, AT);
6212         }
6213       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6214         {
6215           int off;
6216
6217           /* If this is a reference to an external symbol, we want
6218                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6219                nop
6220                <op>     $treg,0($at)
6221                <op>     $treg+1,4($at)
6222              Otherwise we want
6223                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6224                nop
6225                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6226                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6227              If there is a base register we add it to $at before the
6228              lwc1 instructions.  If there is a constant we include it
6229              in the lwc1 instructions.  */
6230           used_at = 1;
6231           expr1.X_add_number = offset_expr.X_add_number;
6232           offset_expr.X_add_number = 0;
6233           if (expr1.X_add_number < -0x8000
6234               || expr1.X_add_number >= 0x8000 - 4)
6235             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6236           if (breg == 0)
6237             off = 0;
6238           else
6239             off = 4;
6240           frag_grow (24 + off);
6241           macro_build ((char *) NULL, &icnt, &offset_expr,
6242                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6243                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6244           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6245           if (breg != 0)
6246             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6247                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6248                          "d,v,t", AT, breg, AT);
6249           /* Itbl support may require additional care here.  */
6250           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6251                        coproc ? treg + 1 : treg,
6252                        (int) BFD_RELOC_LO16, AT);
6253           expr1.X_add_number += 4;
6254
6255           /* Set mips_optimize to 2 to avoid inserting an undesired
6256              nop.  */
6257           hold_mips_optimize = mips_optimize;
6258           mips_optimize = 2;
6259           /* Itbl support may require additional care here.  */
6260           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6261                        coproc ? treg : treg + 1,
6262                        (int) BFD_RELOC_LO16, AT);
6263           mips_optimize = hold_mips_optimize;
6264
6265           (void) frag_var (rs_machine_dependent, 0, 0,
6266                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6267                            offset_expr.X_add_symbol, 0, NULL);
6268         }
6269       else if (mips_pic == SVR4_PIC)
6270         {
6271           int gpdel, off;
6272           char *p;
6273
6274           /* If this is a reference to an external symbol, we want
6275                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6276                addu     $at,$at,$gp
6277                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6278                nop
6279                <op>     $treg,0($at)
6280                <op>     $treg+1,4($at)
6281              Otherwise we want
6282                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6283                nop
6284                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6285                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6286              If there is a base register we add it to $at before the
6287              lwc1 instructions.  If there is a constant we include it
6288              in the lwc1 instructions.  */
6289           used_at = 1;
6290           expr1.X_add_number = offset_expr.X_add_number;
6291           offset_expr.X_add_number = 0;
6292           if (expr1.X_add_number < -0x8000
6293               || expr1.X_add_number >= 0x8000 - 4)
6294             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6295           if (reg_needs_delay (mips_gp_register))
6296             gpdel = 4;
6297           else
6298             gpdel = 0;
6299           if (breg == 0)
6300             off = 0;
6301           else
6302             off = 4;
6303           frag_grow (56);
6304           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6305                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6306           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6307                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6308                        "d,v,t", AT, AT, mips_gp_register);
6309           macro_build ((char *) NULL, &icnt, &offset_expr,
6310                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6311                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6312           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6313           if (breg != 0)
6314             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6315                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6316                          "d,v,t", AT, breg, AT);
6317           /* Itbl support may require additional care here.  */
6318           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6319                        coproc ? treg + 1 : treg,
6320                        (int) BFD_RELOC_LO16, AT);
6321           expr1.X_add_number += 4;
6322
6323           /* Set mips_optimize to 2 to avoid inserting an undesired
6324              nop.  */
6325           hold_mips_optimize = mips_optimize;
6326           mips_optimize = 2;
6327           /* Itbl support may require additional care here.  */
6328           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6329                        coproc ? treg : treg + 1,
6330                        (int) BFD_RELOC_LO16, AT);
6331           mips_optimize = hold_mips_optimize;
6332           expr1.X_add_number -= 4;
6333
6334           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6335                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6336                                       8 + gpdel + off, 1, 0),
6337                         offset_expr.X_add_symbol, 0, NULL);
6338           if (gpdel > 0)
6339             {
6340               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6341               p += 4;
6342             }
6343           macro_build (p, &icnt, &offset_expr,
6344                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6345                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6346                        mips_gp_register);
6347           p += 4;
6348           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6349           p += 4;
6350           if (breg != 0)
6351             {
6352               macro_build (p, &icnt, (expressionS *) NULL,
6353                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6354                            "d,v,t", AT, breg, AT);
6355               p += 4;
6356             }
6357           /* Itbl support may require additional care here.  */
6358           macro_build (p, &icnt, &expr1, s, fmt,
6359                        coproc ? treg + 1 : treg,
6360                        (int) BFD_RELOC_LO16, AT);
6361           p += 4;
6362           expr1.X_add_number += 4;
6363
6364           /* Set mips_optimize to 2 to avoid inserting an undesired
6365              nop.  */
6366           hold_mips_optimize = mips_optimize;
6367           mips_optimize = 2;
6368           /* Itbl support may require additional care here.  */
6369           macro_build (p, &icnt, &expr1, s, fmt,
6370                        coproc ? treg : treg + 1,
6371                        (int) BFD_RELOC_LO16, AT);
6372           mips_optimize = hold_mips_optimize;
6373         }
6374       else if (mips_pic == EMBEDDED_PIC)
6375         {
6376           /* If there is no base register, we use
6377                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6378                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6379              If we have a base register, we use
6380                addu     $at,$breg,$gp
6381                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6382                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6383              */
6384           if (breg == 0)
6385             {
6386               tempreg = mips_gp_register;
6387               used_at = 0;
6388             }
6389           else
6390             {
6391               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6392                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6393                            "d,v,t", AT, breg, mips_gp_register);
6394               tempreg = AT;
6395               used_at = 1;
6396             }
6397
6398           /* Itbl support may require additional care here.  */
6399           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6400                        coproc ? treg + 1 : treg,
6401                        (int) BFD_RELOC_GPREL16, tempreg);
6402           offset_expr.X_add_number += 4;
6403           /* Itbl support may require additional care here.  */
6404           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6405                        coproc ? treg : treg + 1,
6406                        (int) BFD_RELOC_GPREL16, tempreg);
6407         }
6408       else
6409         abort ();
6410
6411       if (! used_at)
6412         return;
6413
6414       break;
6415
6416     case M_LD_OB:
6417       s = "lw";
6418       goto sd_ob;
6419     case M_SD_OB:
6420       s = "sw";
6421     sd_ob:
6422       assert (HAVE_32BIT_ADDRESSES);
6423       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6424                    (int) BFD_RELOC_LO16, breg);
6425       offset_expr.X_add_number += 4;
6426       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6427                    (int) BFD_RELOC_LO16, breg);
6428       return;
6429
6430    /* New code added to support COPZ instructions.
6431       This code builds table entries out of the macros in mip_opcodes.
6432       R4000 uses interlocks to handle coproc delays.
6433       Other chips (like the R3000) require nops to be inserted for delays.
6434
6435       FIXME: Currently, we require that the user handle delays.
6436       In order to fill delay slots for non-interlocked chips,
6437       we must have a way to specify delays based on the coprocessor.
6438       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6439       What are the side-effects of the cop instruction?
6440       What cache support might we have and what are its effects?
6441       Both coprocessor & memory require delays. how long???
6442       What registers are read/set/modified?
6443
6444       If an itbl is provided to interpret cop instructions,
6445       this knowledge can be encoded in the itbl spec.  */
6446
6447     case M_COP0:
6448       s = "c0";
6449       goto copz;
6450     case M_COP1:
6451       s = "c1";
6452       goto copz;
6453     case M_COP2:
6454       s = "c2";
6455       goto copz;
6456     case M_COP3:
6457       s = "c3";
6458     copz:
6459       /* For now we just do C (same as Cz).  The parameter will be
6460          stored in insn_opcode by mips_ip.  */
6461       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6462                    ip->insn_opcode);
6463       return;
6464
6465     case M_MOVE:
6466       move_register (&icnt, dreg, sreg);
6467       return;
6468
6469 #ifdef LOSING_COMPILER
6470     default:
6471       /* Try and see if this is a new itbl instruction.
6472          This code builds table entries out of the macros in mip_opcodes.
6473          FIXME: For now we just assemble the expression and pass it's
6474          value along as a 32-bit immediate.
6475          We may want to have the assembler assemble this value,
6476          so that we gain the assembler's knowledge of delay slots,
6477          symbols, etc.
6478          Would it be more efficient to use mask (id) here? */
6479       if (itbl_have_entries
6480           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6481         {
6482           s = ip->insn_mo->name;
6483           s2 = "cop3";
6484           coproc = ITBL_DECODE_PNUM (immed_expr);;
6485           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6486           return;
6487         }
6488       macro2 (ip);
6489       return;
6490     }
6491   if (mips_opts.noat)
6492     as_warn (_("Macro used $at after \".set noat\""));
6493 }
6494
6495 static void
6496 macro2 (ip)
6497      struct mips_cl_insn *ip;
6498 {
6499   register int treg, sreg, dreg, breg;
6500   int tempreg;
6501   int mask;
6502   int icnt = 0;
6503   int used_at;
6504   expressionS expr1;
6505   const char *s;
6506   const char *s2;
6507   const char *fmt;
6508   int likely = 0;
6509   int dbl = 0;
6510   int coproc = 0;
6511   int lr = 0;
6512   int imm = 0;
6513   int off;
6514   offsetT maxnum;
6515   bfd_reloc_code_real_type r;
6516   char *p;
6517
6518   treg = (ip->insn_opcode >> 16) & 0x1f;
6519   dreg = (ip->insn_opcode >> 11) & 0x1f;
6520   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6521   mask = ip->insn_mo->mask;
6522
6523   expr1.X_op = O_constant;
6524   expr1.X_op_symbol = NULL;
6525   expr1.X_add_symbol = NULL;
6526   expr1.X_add_number = 1;
6527
6528   switch (mask)
6529     {
6530 #endif /* LOSING_COMPILER */
6531
6532     case M_DMUL:
6533       dbl = 1;
6534     case M_MUL:
6535       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6536                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6537       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6538                    dreg);
6539       return;
6540
6541     case M_DMUL_I:
6542       dbl = 1;
6543     case M_MUL_I:
6544       /* The MIPS assembler some times generates shifts and adds.  I'm
6545          not trying to be that fancy. GCC should do this for us
6546          anyway.  */
6547       load_register (&icnt, AT, &imm_expr, dbl);
6548       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6549                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6550       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6551                    dreg);
6552       break;
6553
6554     case M_DMULO_I:
6555       dbl = 1;
6556     case M_MULO_I:
6557       imm = 1;
6558       goto do_mulo;
6559
6560     case M_DMULO:
6561       dbl = 1;
6562     case M_MULO:
6563     do_mulo:
6564       mips_emit_delays (true);
6565       ++mips_opts.noreorder;
6566       mips_any_noreorder = 1;
6567       if (imm)
6568         load_register (&icnt, AT, &imm_expr, dbl);
6569       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6570                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6571       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6572                    dreg);
6573       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6574                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6575       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6576                    AT);
6577       if (mips_trap)
6578         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6579                      "s,t,q", dreg, AT, 6);
6580       else
6581         {
6582           expr1.X_add_number = 8;
6583           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6584                        AT);
6585           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6586                        0);
6587           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6588                        "c", 6);
6589         }
6590       --mips_opts.noreorder;
6591       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6592       break;
6593
6594     case M_DMULOU_I:
6595       dbl = 1;
6596     case M_MULOU_I:
6597       imm = 1;
6598       goto do_mulou;
6599
6600     case M_DMULOU:
6601       dbl = 1;
6602     case M_MULOU:
6603     do_mulou:
6604       mips_emit_delays (true);
6605       ++mips_opts.noreorder;
6606       mips_any_noreorder = 1;
6607       if (imm)
6608         load_register (&icnt, AT, &imm_expr, dbl);
6609       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6610                    dbl ? "dmultu" : "multu",
6611                    "s,t", sreg, imm ? AT : treg);
6612       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6613                    AT);
6614       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6615                    dreg);
6616       if (mips_trap)
6617         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6618                      "s,t,q", AT, 0, 6);
6619       else
6620         {
6621           expr1.X_add_number = 8;
6622           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6623           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6624                        0);
6625           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6626                        "c", 6);
6627         }
6628       --mips_opts.noreorder;
6629       break;
6630
6631     case M_DROL:
6632       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6633                    "d,v,t", AT, 0, treg);
6634       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6635                    "d,t,s", AT, sreg, AT);
6636       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6637                    "d,t,s", dreg, sreg, treg);
6638       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6639                    "d,v,t", dreg, dreg, AT);
6640       break;
6641
6642     case M_ROL:
6643       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6644                    "d,v,t", AT, 0, treg);
6645       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6646                    "d,t,s", AT, sreg, AT);
6647       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6648                    "d,t,s", dreg, sreg, treg);
6649       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6650                    "d,v,t", dreg, dreg, AT);
6651       break;
6652
6653     case M_DROL_I:
6654       {
6655         unsigned int rot;
6656
6657         if (imm_expr.X_op != O_constant)
6658           as_bad (_("rotate count too large"));
6659         rot = imm_expr.X_add_number & 0x3f;
6660         if (rot == 0)
6661           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6662                        "d,w,<", dreg, sreg, 0);
6663         else
6664           {
6665             char *l, *r;
6666
6667             l = (rot < 0x20) ? "dsll" : "dsll32";
6668             r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6669             rot &= 0x1f;
6670             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6671                          "d,w,<", AT, sreg, rot);
6672             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6673                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6674             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6675                          "d,v,t", dreg, dreg, AT);
6676           }
6677       }
6678       break;
6679
6680     case M_ROL_I:
6681       {
6682         unsigned int rot;
6683
6684         if (imm_expr.X_op != O_constant)
6685           as_bad (_("rotate count too large"));
6686         rot = imm_expr.X_add_number & 0x1f;
6687         if (rot == 0)
6688           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6689                        "d,w,<", dreg, sreg, 0);
6690         else
6691           {
6692             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6693                          "d,w,<", AT, sreg, rot);
6694             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6695                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6696             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6697                          "d,v,t", dreg, dreg, AT);
6698           }
6699       }
6700       break;
6701
6702     case M_DROR:
6703       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6704                    "d,v,t", AT, 0, treg);
6705       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6706                    "d,t,s", AT, sreg, AT);
6707       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6708                    "d,t,s", dreg, sreg, treg);
6709       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6710                    "d,v,t", dreg, dreg, AT);
6711       break;
6712
6713     case M_ROR:
6714       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6715                    "d,v,t", AT, 0, treg);
6716       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6717                    "d,t,s", AT, sreg, AT);
6718       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6719                    "d,t,s", dreg, sreg, treg);
6720       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6721                    "d,v,t", dreg, dreg, AT);
6722       break;
6723
6724     case M_DROR_I:
6725       {
6726         unsigned int rot;
6727
6728         if (imm_expr.X_op != O_constant)
6729           as_bad (_("rotate count too large"));
6730         rot = imm_expr.X_add_number & 0x3f;
6731         if (rot == 0)
6732           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6733                        "d,w,<", dreg, sreg, 0);
6734         else
6735           {
6736             char *l, *r;
6737
6738             r = (rot < 0x20) ? "dsrl" : "dsrl32";
6739             l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6740             rot &= 0x1f;
6741             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6742                          "d,w,<", AT, sreg, rot);
6743             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6744                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6745             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6746                          "d,v,t", dreg, dreg, AT);
6747           }
6748       }
6749       break;
6750
6751     case M_ROR_I:
6752       {
6753         unsigned int rot;
6754
6755         if (imm_expr.X_op != O_constant)
6756           as_bad (_("rotate count too large"));
6757         rot = imm_expr.X_add_number & 0x1f;
6758         if (rot == 0)
6759           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6760                        "d,w,<", dreg, sreg, 0);
6761         else
6762           {
6763             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6764                          "d,w,<", AT, sreg, rot);
6765             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6766                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6767             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6768                          "d,v,t", dreg, dreg, AT);
6769           }
6770       }
6771       break;
6772
6773     case M_S_DOB:
6774       if (mips_arch == CPU_R4650)
6775         {
6776           as_bad (_("opcode not supported on this processor"));
6777           return;
6778         }
6779       assert (mips_opts.isa == ISA_MIPS1);
6780       /* Even on a big endian machine $fn comes before $fn+1.  We have
6781          to adjust when storing to memory.  */
6782       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6783                    target_big_endian ? treg + 1 : treg,
6784                    (int) BFD_RELOC_LO16, breg);
6785       offset_expr.X_add_number += 4;
6786       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6787                    target_big_endian ? treg : treg + 1,
6788                    (int) BFD_RELOC_LO16, breg);
6789       return;
6790
6791     case M_SEQ:
6792       if (sreg == 0)
6793         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6794                      treg, (int) BFD_RELOC_LO16);
6795       else if (treg == 0)
6796         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6797                      sreg, (int) BFD_RELOC_LO16);
6798       else
6799         {
6800           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6801                        "d,v,t", dreg, sreg, treg);
6802           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6803                        dreg, (int) BFD_RELOC_LO16);
6804         }
6805       return;
6806
6807     case M_SEQ_I:
6808       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6809         {
6810           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6811                        sreg, (int) BFD_RELOC_LO16);
6812           return;
6813         }
6814       if (sreg == 0)
6815         {
6816           as_warn (_("Instruction %s: result is always false"),
6817                    ip->insn_mo->name);
6818           move_register (&icnt, dreg, 0);
6819           return;
6820         }
6821       if (imm_expr.X_op == O_constant
6822           && imm_expr.X_add_number >= 0
6823           && imm_expr.X_add_number < 0x10000)
6824         {
6825           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6826                        sreg, (int) BFD_RELOC_LO16);
6827           used_at = 0;
6828         }
6829       else if (imm_expr.X_op == O_constant
6830                && imm_expr.X_add_number > -0x8000
6831                && imm_expr.X_add_number < 0)
6832         {
6833           imm_expr.X_add_number = -imm_expr.X_add_number;
6834           macro_build ((char *) NULL, &icnt, &imm_expr,
6835                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6836                        "t,r,j", dreg, sreg,
6837                        (int) BFD_RELOC_LO16);
6838           used_at = 0;
6839         }
6840       else
6841         {
6842           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6843           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6844                        "d,v,t", dreg, sreg, AT);
6845           used_at = 1;
6846         }
6847       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6848                    (int) BFD_RELOC_LO16);
6849       if (used_at)
6850         break;
6851       return;
6852
6853     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6854       s = "slt";
6855       goto sge;
6856     case M_SGEU:
6857       s = "sltu";
6858     sge:
6859       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6860                    dreg, sreg, treg);
6861       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6862                    (int) BFD_RELOC_LO16);
6863       return;
6864
6865     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6866     case M_SGEU_I:
6867       if (imm_expr.X_op == O_constant
6868           && imm_expr.X_add_number >= -0x8000
6869           && imm_expr.X_add_number < 0x8000)
6870         {
6871           macro_build ((char *) NULL, &icnt, &imm_expr,
6872                        mask == M_SGE_I ? "slti" : "sltiu",
6873                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6874           used_at = 0;
6875         }
6876       else
6877         {
6878           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6879           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6880                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6881                        AT);
6882           used_at = 1;
6883         }
6884       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6885                    (int) BFD_RELOC_LO16);
6886       if (used_at)
6887         break;
6888       return;
6889
6890     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6891       s = "slt";
6892       goto sgt;
6893     case M_SGTU:
6894       s = "sltu";
6895     sgt:
6896       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6897                    dreg, treg, sreg);
6898       return;
6899
6900     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6901       s = "slt";
6902       goto sgti;
6903     case M_SGTU_I:
6904       s = "sltu";
6905     sgti:
6906       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6907       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6908                    dreg, AT, sreg);
6909       break;
6910
6911     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6912       s = "slt";
6913       goto sle;
6914     case M_SLEU:
6915       s = "sltu";
6916     sle:
6917       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6918                    dreg, treg, sreg);
6919       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6920                    (int) BFD_RELOC_LO16);
6921       return;
6922
6923     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6924       s = "slt";
6925       goto slei;
6926     case M_SLEU_I:
6927       s = "sltu";
6928     slei:
6929       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6930       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6931                    dreg, AT, sreg);
6932       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6933                    (int) BFD_RELOC_LO16);
6934       break;
6935
6936     case M_SLT_I:
6937       if (imm_expr.X_op == O_constant
6938           && imm_expr.X_add_number >= -0x8000
6939           && imm_expr.X_add_number < 0x8000)
6940         {
6941           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6942                        dreg, sreg, (int) BFD_RELOC_LO16);
6943           return;
6944         }
6945       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6946       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6947                    dreg, sreg, AT);
6948       break;
6949
6950     case M_SLTU_I:
6951       if (imm_expr.X_op == O_constant
6952           && imm_expr.X_add_number >= -0x8000
6953           && imm_expr.X_add_number < 0x8000)
6954         {
6955           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6956                        dreg, sreg, (int) BFD_RELOC_LO16);
6957           return;
6958         }
6959       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6960       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6961                    "d,v,t", dreg, sreg, AT);
6962       break;
6963
6964     case M_SNE:
6965       if (sreg == 0)
6966         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6967                      "d,v,t", dreg, 0, treg);
6968       else if (treg == 0)
6969         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6970                      "d,v,t", dreg, 0, sreg);
6971       else
6972         {
6973           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6974                        "d,v,t", dreg, sreg, treg);
6975           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6976                        "d,v,t", dreg, 0, dreg);
6977         }
6978       return;
6979
6980     case M_SNE_I:
6981       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6982         {
6983           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6984                        "d,v,t", dreg, 0, sreg);
6985           return;
6986         }
6987       if (sreg == 0)
6988         {
6989           as_warn (_("Instruction %s: result is always true"),
6990                    ip->insn_mo->name);
6991           macro_build ((char *) NULL, &icnt, &expr1,
6992                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6993                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6994           return;
6995         }
6996       if (imm_expr.X_op == O_constant
6997           && imm_expr.X_add_number >= 0
6998           && imm_expr.X_add_number < 0x10000)
6999         {
7000           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7001                        dreg, sreg, (int) BFD_RELOC_LO16);
7002           used_at = 0;
7003         }
7004       else if (imm_expr.X_op == O_constant
7005                && imm_expr.X_add_number > -0x8000
7006                && imm_expr.X_add_number < 0)
7007         {
7008           imm_expr.X_add_number = -imm_expr.X_add_number;
7009           macro_build ((char *) NULL, &icnt, &imm_expr,
7010                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7011                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7012           used_at = 0;
7013         }
7014       else
7015         {
7016           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7017           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7018                        "d,v,t", dreg, sreg, AT);
7019           used_at = 1;
7020         }
7021       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7022                    "d,v,t", dreg, 0, dreg);
7023       if (used_at)
7024         break;
7025       return;
7026
7027     case M_DSUB_I:
7028       dbl = 1;
7029     case M_SUB_I:
7030       if (imm_expr.X_op == O_constant
7031           && imm_expr.X_add_number > -0x8000
7032           && imm_expr.X_add_number <= 0x8000)
7033         {
7034           imm_expr.X_add_number = -imm_expr.X_add_number;
7035           macro_build ((char *) NULL, &icnt, &imm_expr,
7036                        dbl ? "daddi" : "addi",
7037                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7038           return;
7039         }
7040       load_register (&icnt, AT, &imm_expr, dbl);
7041       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7042                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7043       break;
7044
7045     case M_DSUBU_I:
7046       dbl = 1;
7047     case M_SUBU_I:
7048       if (imm_expr.X_op == O_constant
7049           && imm_expr.X_add_number > -0x8000
7050           && imm_expr.X_add_number <= 0x8000)
7051         {
7052           imm_expr.X_add_number = -imm_expr.X_add_number;
7053           macro_build ((char *) NULL, &icnt, &imm_expr,
7054                        dbl ? "daddiu" : "addiu",
7055                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7056           return;
7057         }
7058       load_register (&icnt, AT, &imm_expr, dbl);
7059       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7060                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7061       break;
7062
7063     case M_TEQ_I:
7064       s = "teq";
7065       goto trap;
7066     case M_TGE_I:
7067       s = "tge";
7068       goto trap;
7069     case M_TGEU_I:
7070       s = "tgeu";
7071       goto trap;
7072     case M_TLT_I:
7073       s = "tlt";
7074       goto trap;
7075     case M_TLTU_I:
7076       s = "tltu";
7077       goto trap;
7078     case M_TNE_I:
7079       s = "tne";
7080     trap:
7081       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7082       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7083                    AT);
7084       break;
7085
7086     case M_TRUNCWS:
7087     case M_TRUNCWD:
7088       assert (mips_opts.isa == ISA_MIPS1);
7089       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7090       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7091
7092       /*
7093        * Is the double cfc1 instruction a bug in the mips assembler;
7094        * or is there a reason for it?
7095        */
7096       mips_emit_delays (true);
7097       ++mips_opts.noreorder;
7098       mips_any_noreorder = 1;
7099       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7100                    treg, RA);
7101       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7102                    treg, RA);
7103       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7104       expr1.X_add_number = 3;
7105       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7106                    (int) BFD_RELOC_LO16);
7107       expr1.X_add_number = 2;
7108       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7109                      (int) BFD_RELOC_LO16);
7110       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7111                    AT, RA);
7112       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7113       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7114               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7115       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7116                    treg, RA);
7117       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7118       --mips_opts.noreorder;
7119       break;
7120
7121     case M_ULH:
7122       s = "lb";
7123       goto ulh;
7124     case M_ULHU:
7125       s = "lbu";
7126     ulh:
7127       if (offset_expr.X_add_number >= 0x7fff)
7128         as_bad (_("operand overflow"));
7129       /* avoid load delay */
7130       if (! target_big_endian)
7131         ++offset_expr.X_add_number;
7132       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7133                    (int) BFD_RELOC_LO16, breg);
7134       if (! target_big_endian)
7135         --offset_expr.X_add_number;
7136       else
7137         ++offset_expr.X_add_number;
7138       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7139                    (int) BFD_RELOC_LO16, breg);
7140       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7141                    treg, treg, 8);
7142       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7143                    treg, treg, AT);
7144       break;
7145
7146     case M_ULD:
7147       s = "ldl";
7148       s2 = "ldr";
7149       off = 7;
7150       goto ulw;
7151     case M_ULW:
7152       s = "lwl";
7153       s2 = "lwr";
7154       off = 3;
7155     ulw:
7156       if (offset_expr.X_add_number >= 0x8000 - off)
7157         as_bad (_("operand overflow"));
7158       if (! target_big_endian)
7159         offset_expr.X_add_number += off;
7160       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7161                    (int) BFD_RELOC_LO16, breg);
7162       if (! target_big_endian)
7163         offset_expr.X_add_number -= off;
7164       else
7165         offset_expr.X_add_number += off;
7166       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7167                    (int) BFD_RELOC_LO16, breg);
7168       return;
7169
7170     case M_ULD_A:
7171       s = "ldl";
7172       s2 = "ldr";
7173       off = 7;
7174       goto ulwa;
7175     case M_ULW_A:
7176       s = "lwl";
7177       s2 = "lwr";
7178       off = 3;
7179     ulwa:
7180       used_at = 1;
7181       load_address (&icnt, AT, &offset_expr, &used_at);
7182       if (breg != 0)
7183         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7184                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7185                      "d,v,t", AT, AT, breg);
7186       if (! target_big_endian)
7187         expr1.X_add_number = off;
7188       else
7189         expr1.X_add_number = 0;
7190       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7191                    (int) BFD_RELOC_LO16, AT);
7192       if (! target_big_endian)
7193         expr1.X_add_number = 0;
7194       else
7195         expr1.X_add_number = off;
7196       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7197                    (int) BFD_RELOC_LO16, AT);
7198       break;
7199
7200     case M_ULH_A:
7201     case M_ULHU_A:
7202       used_at = 1;
7203       load_address (&icnt, AT, &offset_expr, &used_at);
7204       if (breg != 0)
7205         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7206                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7207                      "d,v,t", AT, AT, breg);
7208       if (target_big_endian)
7209         expr1.X_add_number = 0;
7210       macro_build ((char *) NULL, &icnt, &expr1,
7211                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7212                    (int) BFD_RELOC_LO16, AT);
7213       if (target_big_endian)
7214         expr1.X_add_number = 1;
7215       else
7216         expr1.X_add_number = 0;
7217       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7218                    (int) BFD_RELOC_LO16, AT);
7219       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7220                    treg, treg, 8);
7221       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7222                    treg, treg, AT);
7223       break;
7224
7225     case M_USH:
7226       if (offset_expr.X_add_number >= 0x7fff)
7227         as_bad (_("operand overflow"));
7228       if (target_big_endian)
7229         ++offset_expr.X_add_number;
7230       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7231                    (int) BFD_RELOC_LO16, breg);
7232       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7233                    AT, treg, 8);
7234       if (target_big_endian)
7235         --offset_expr.X_add_number;
7236       else
7237         ++offset_expr.X_add_number;
7238       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7239                    (int) BFD_RELOC_LO16, breg);
7240       break;
7241
7242     case M_USD:
7243       s = "sdl";
7244       s2 = "sdr";
7245       off = 7;
7246       goto usw;
7247     case M_USW:
7248       s = "swl";
7249       s2 = "swr";
7250       off = 3;
7251     usw:
7252       if (offset_expr.X_add_number >= 0x8000 - off)
7253         as_bad (_("operand overflow"));
7254       if (! target_big_endian)
7255         offset_expr.X_add_number += off;
7256       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7257                    (int) BFD_RELOC_LO16, breg);
7258       if (! target_big_endian)
7259         offset_expr.X_add_number -= off;
7260       else
7261         offset_expr.X_add_number += off;
7262       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7263                    (int) BFD_RELOC_LO16, breg);
7264       return;
7265
7266     case M_USD_A:
7267       s = "sdl";
7268       s2 = "sdr";
7269       off = 7;
7270       goto uswa;
7271     case M_USW_A:
7272       s = "swl";
7273       s2 = "swr";
7274       off = 3;
7275     uswa:
7276       used_at = 1;
7277       load_address (&icnt, AT, &offset_expr, &used_at);
7278       if (breg != 0)
7279         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7280                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7281                      "d,v,t", AT, AT, breg);
7282       if (! target_big_endian)
7283         expr1.X_add_number = off;
7284       else
7285         expr1.X_add_number = 0;
7286       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7287                    (int) BFD_RELOC_LO16, AT);
7288       if (! target_big_endian)
7289         expr1.X_add_number = 0;
7290       else
7291         expr1.X_add_number = off;
7292       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7293                    (int) BFD_RELOC_LO16, AT);
7294       break;
7295
7296     case M_USH_A:
7297       used_at = 1;
7298       load_address (&icnt, AT, &offset_expr, &used_at);
7299       if (breg != 0)
7300         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7301                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7302                      "d,v,t", AT, AT, breg);
7303       if (! target_big_endian)
7304         expr1.X_add_number = 0;
7305       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7306                    (int) BFD_RELOC_LO16, AT);
7307       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7308                    treg, treg, 8);
7309       if (! target_big_endian)
7310         expr1.X_add_number = 1;
7311       else
7312         expr1.X_add_number = 0;
7313       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7314                    (int) BFD_RELOC_LO16, AT);
7315       if (! target_big_endian)
7316         expr1.X_add_number = 0;
7317       else
7318         expr1.X_add_number = 1;
7319       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7320                    (int) BFD_RELOC_LO16, AT);
7321       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7322                    treg, treg, 8);
7323       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7324                    treg, treg, AT);
7325       break;
7326
7327     default:
7328       /* FIXME: Check if this is one of the itbl macros, since they
7329          are added dynamically.  */
7330       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7331       break;
7332     }
7333   if (mips_opts.noat)
7334     as_warn (_("Macro used $at after \".set noat\""));
7335 }
7336
7337 /* Implement macros in mips16 mode.  */
7338
7339 static void
7340 mips16_macro (ip)
7341      struct mips_cl_insn *ip;
7342 {
7343   int mask;
7344   int xreg, yreg, zreg, tmp;
7345   int icnt;
7346   expressionS expr1;
7347   int dbl;
7348   const char *s, *s2, *s3;
7349
7350   mask = ip->insn_mo->mask;
7351
7352   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7353   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7354   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7355
7356   icnt = 0;
7357
7358   expr1.X_op = O_constant;
7359   expr1.X_op_symbol = NULL;
7360   expr1.X_add_symbol = NULL;
7361   expr1.X_add_number = 1;
7362
7363   dbl = 0;
7364
7365   switch (mask)
7366     {
7367     default:
7368       internalError ();
7369
7370     case M_DDIV_3:
7371       dbl = 1;
7372     case M_DIV_3:
7373       s = "mflo";
7374       goto do_div3;
7375     case M_DREM_3:
7376       dbl = 1;
7377     case M_REM_3:
7378       s = "mfhi";
7379     do_div3:
7380       mips_emit_delays (true);
7381       ++mips_opts.noreorder;
7382       mips_any_noreorder = 1;
7383       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7384                    dbl ? "ddiv" : "div",
7385                    "0,x,y", xreg, yreg);
7386       expr1.X_add_number = 2;
7387       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7388       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7389                    7);
7390
7391       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7392          since that causes an overflow.  We should do that as well,
7393          but I don't see how to do the comparisons without a temporary
7394          register.  */
7395       --mips_opts.noreorder;
7396       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7397       break;
7398
7399     case M_DIVU_3:
7400       s = "divu";
7401       s2 = "mflo";
7402       goto do_divu3;
7403     case M_REMU_3:
7404       s = "divu";
7405       s2 = "mfhi";
7406       goto do_divu3;
7407     case M_DDIVU_3:
7408       s = "ddivu";
7409       s2 = "mflo";
7410       goto do_divu3;
7411     case M_DREMU_3:
7412       s = "ddivu";
7413       s2 = "mfhi";
7414     do_divu3:
7415       mips_emit_delays (true);
7416       ++mips_opts.noreorder;
7417       mips_any_noreorder = 1;
7418       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7419                    xreg, yreg);
7420       expr1.X_add_number = 2;
7421       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7422       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7423                    "6", 7);
7424       --mips_opts.noreorder;
7425       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7426       break;
7427
7428     case M_DMUL:
7429       dbl = 1;
7430     case M_MUL:
7431       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7432                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7433       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7434                    zreg);
7435       return;
7436
7437     case M_DSUBU_I:
7438       dbl = 1;
7439       goto do_subu;
7440     case M_SUBU_I:
7441     do_subu:
7442       if (imm_expr.X_op != O_constant)
7443         as_bad (_("Unsupported large constant"));
7444       imm_expr.X_add_number = -imm_expr.X_add_number;
7445       macro_build ((char *) NULL, &icnt, &imm_expr,
7446                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7447       break;
7448
7449     case M_SUBU_I_2:
7450       if (imm_expr.X_op != O_constant)
7451         as_bad (_("Unsupported large constant"));
7452       imm_expr.X_add_number = -imm_expr.X_add_number;
7453       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7454                    "x,k", xreg);
7455       break;
7456
7457     case M_DSUBU_I_2:
7458       if (imm_expr.X_op != O_constant)
7459         as_bad (_("Unsupported large constant"));
7460       imm_expr.X_add_number = -imm_expr.X_add_number;
7461       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7462                    "y,j", yreg);
7463       break;
7464
7465     case M_BEQ:
7466       s = "cmp";
7467       s2 = "bteqz";
7468       goto do_branch;
7469     case M_BNE:
7470       s = "cmp";
7471       s2 = "btnez";
7472       goto do_branch;
7473     case M_BLT:
7474       s = "slt";
7475       s2 = "btnez";
7476       goto do_branch;
7477     case M_BLTU:
7478       s = "sltu";
7479       s2 = "btnez";
7480       goto do_branch;
7481     case M_BLE:
7482       s = "slt";
7483       s2 = "bteqz";
7484       goto do_reverse_branch;
7485     case M_BLEU:
7486       s = "sltu";
7487       s2 = "bteqz";
7488       goto do_reverse_branch;
7489     case M_BGE:
7490       s = "slt";
7491       s2 = "bteqz";
7492       goto do_branch;
7493     case M_BGEU:
7494       s = "sltu";
7495       s2 = "bteqz";
7496       goto do_branch;
7497     case M_BGT:
7498       s = "slt";
7499       s2 = "btnez";
7500       goto do_reverse_branch;
7501     case M_BGTU:
7502       s = "sltu";
7503       s2 = "btnez";
7504
7505     do_reverse_branch:
7506       tmp = xreg;
7507       xreg = yreg;
7508       yreg = tmp;
7509
7510     do_branch:
7511       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7512                    xreg, yreg);
7513       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7514       break;
7515
7516     case M_BEQ_I:
7517       s = "cmpi";
7518       s2 = "bteqz";
7519       s3 = "x,U";
7520       goto do_branch_i;
7521     case M_BNE_I:
7522       s = "cmpi";
7523       s2 = "btnez";
7524       s3 = "x,U";
7525       goto do_branch_i;
7526     case M_BLT_I:
7527       s = "slti";
7528       s2 = "btnez";
7529       s3 = "x,8";
7530       goto do_branch_i;
7531     case M_BLTU_I:
7532       s = "sltiu";
7533       s2 = "btnez";
7534       s3 = "x,8";
7535       goto do_branch_i;
7536     case M_BLE_I:
7537       s = "slti";
7538       s2 = "btnez";
7539       s3 = "x,8";
7540       goto do_addone_branch_i;
7541     case M_BLEU_I:
7542       s = "sltiu";
7543       s2 = "btnez";
7544       s3 = "x,8";
7545       goto do_addone_branch_i;
7546     case M_BGE_I:
7547       s = "slti";
7548       s2 = "bteqz";
7549       s3 = "x,8";
7550       goto do_branch_i;
7551     case M_BGEU_I:
7552       s = "sltiu";
7553       s2 = "bteqz";
7554       s3 = "x,8";
7555       goto do_branch_i;
7556     case M_BGT_I:
7557       s = "slti";
7558       s2 = "bteqz";
7559       s3 = "x,8";
7560       goto do_addone_branch_i;
7561     case M_BGTU_I:
7562       s = "sltiu";
7563       s2 = "bteqz";
7564       s3 = "x,8";
7565
7566     do_addone_branch_i:
7567       if (imm_expr.X_op != O_constant)
7568         as_bad (_("Unsupported large constant"));
7569       ++imm_expr.X_add_number;
7570
7571     do_branch_i:
7572       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7573       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7574       break;
7575
7576     case M_ABS:
7577       expr1.X_add_number = 0;
7578       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7579       if (xreg != yreg)
7580         move_register (&icnt, xreg, yreg);
7581       expr1.X_add_number = 2;
7582       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7583       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7584                    "neg", "x,w", xreg, xreg);
7585     }
7586 }
7587
7588 /* For consistency checking, verify that all bits are specified either
7589    by the match/mask part of the instruction definition, or by the
7590    operand list.  */
7591 static int
7592 validate_mips_insn (opc)
7593      const struct mips_opcode *opc;
7594 {
7595   const char *p = opc->args;
7596   char c;
7597   unsigned long used_bits = opc->mask;
7598
7599   if ((used_bits & opc->match) != opc->match)
7600     {
7601       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7602               opc->name, opc->args);
7603       return 0;
7604     }
7605 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7606   while (*p)
7607     switch (c = *p++)
7608       {
7609       case ',': break;
7610       case '(': break;
7611       case ')': break;
7612       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7613       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7614       case 'A': break;
7615       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7616       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7617       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7618       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7619       case 'F': break;
7620       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7621       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7622       case 'I': break;
7623       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7624       case 'L': break;
7625       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7626       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7627       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7628       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7629                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7630       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7631       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7632       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7633       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7634       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7635       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7636       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7637       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7638       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7639       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7640       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7641       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7642       case 'f': break;
7643       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7644       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7645       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7646       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7647       case 'l': break;
7648       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7649       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7650       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7651       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7652       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7653       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7654       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7655       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7656       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7657       case 'x': break;
7658       case 'z': break;
7659       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7660       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7661                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7662       default:
7663         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7664                 c, opc->name, opc->args);
7665         return 0;
7666       }
7667 #undef USE_BITS
7668   if (used_bits != 0xffffffff)
7669     {
7670       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7671               ~used_bits & 0xffffffff, opc->name, opc->args);
7672       return 0;
7673     }
7674   return 1;
7675 }
7676
7677 /* This routine assembles an instruction into its binary format.  As a
7678    side effect, it sets one of the global variables imm_reloc or
7679    offset_reloc to the type of relocation to do if one of the operands
7680    is an address expression.  */
7681
7682 static void
7683 mips_ip (str, ip)
7684      char *str;
7685      struct mips_cl_insn *ip;
7686 {
7687   char *s;
7688   const char *args;
7689   char c = 0;
7690   struct mips_opcode *insn;
7691   char *argsStart;
7692   unsigned int regno;
7693   unsigned int lastregno = 0;
7694   char *s_reset;
7695   char save_c = 0;
7696
7697   insn_error = NULL;
7698
7699   /* If the instruction contains a '.', we first try to match an instruction
7700      including the '.'.  Then we try again without the '.'.  */
7701   insn = NULL;
7702   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7703     continue;
7704
7705   /* If we stopped on whitespace, then replace the whitespace with null for
7706      the call to hash_find.  Save the character we replaced just in case we
7707      have to re-parse the instruction.  */
7708   if (ISSPACE (*s))
7709     {
7710       save_c = *s;
7711       *s++ = '\0';
7712     }
7713
7714   insn = (struct mips_opcode *) hash_find (op_hash, str);
7715
7716   /* If we didn't find the instruction in the opcode table, try again, but
7717      this time with just the instruction up to, but not including the
7718      first '.'.  */
7719   if (insn == NULL)
7720     {
7721       /* Restore the character we overwrite above (if any).  */
7722       if (save_c)
7723         *(--s) = save_c;
7724
7725       /* Scan up to the first '.' or whitespace.  */
7726       for (s = str;
7727            *s != '\0' && *s != '.' && !ISSPACE (*s);
7728            ++s)
7729         continue;
7730
7731       /* If we did not find a '.', then we can quit now.  */
7732       if (*s != '.')
7733         {
7734           insn_error = "unrecognized opcode";
7735           return;
7736         }
7737
7738       /* Lookup the instruction in the hash table.  */
7739       *s++ = '\0';
7740       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7741         {
7742           insn_error = "unrecognized opcode";
7743           return;
7744         }
7745     }
7746
7747   argsStart = s;
7748   for (;;)
7749     {
7750       boolean ok;
7751
7752       assert (strcmp (insn->name, str) == 0);
7753
7754       if (OPCODE_IS_MEMBER (insn,
7755                             (mips_opts.isa
7756                              | (mips_opts.mips16 ? INSN_MIPS16 : 0)
7757                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7758                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7759                             mips_arch))
7760         ok = true;
7761       else
7762         ok = false;
7763
7764       if (insn->pinfo != INSN_MACRO)
7765         {
7766           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7767             ok = false;
7768         }
7769
7770       if (! ok)
7771         {
7772           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7773               && strcmp (insn->name, insn[1].name) == 0)
7774             {
7775               ++insn;
7776               continue;
7777             }
7778           else
7779             {
7780               if (!insn_error)
7781                 {
7782                   static char buf[100];
7783                   if (mips_arch_info->is_isa)
7784                     sprintf (buf,
7785                              _("opcode not supported at this ISA level (%s)"),
7786                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7787                   else
7788                     sprintf (buf,
7789                              _("opcode not supported on this processor: %s (%s)"),
7790                              mips_arch_info->name,
7791                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7792                   insn_error = buf;
7793                 }
7794               if (save_c)
7795                 *(--s) = save_c;
7796               return;
7797             }
7798         }
7799
7800       ip->insn_mo = insn;
7801       ip->insn_opcode = insn->match;
7802       insn_error = NULL;
7803       for (args = insn->args;; ++args)
7804         {
7805           int is_mdmx;
7806
7807           s += strspn (s, " \t");
7808           is_mdmx = 0;
7809           switch (*args)
7810             {
7811             case '\0':          /* end of args */
7812               if (*s == '\0')
7813                 return;
7814               break;
7815
7816             case ',':
7817               if (*s++ == *args)
7818                 continue;
7819               s--;
7820               switch (*++args)
7821                 {
7822                 case 'r':
7823                 case 'v':
7824                   ip->insn_opcode |= lastregno << OP_SH_RS;
7825                   continue;
7826
7827                 case 'w':
7828                   ip->insn_opcode |= lastregno << OP_SH_RT;
7829                   continue;
7830
7831                 case 'W':
7832                   ip->insn_opcode |= lastregno << OP_SH_FT;
7833                   continue;
7834
7835                 case 'V':
7836                   ip->insn_opcode |= lastregno << OP_SH_FS;
7837                   continue;
7838                 }
7839               break;
7840
7841             case '(':
7842               /* Handle optional base register.
7843                  Either the base register is omitted or
7844                  we must have a left paren.  */
7845               /* This is dependent on the next operand specifier
7846                  is a base register specification.  */
7847               assert (args[1] == 'b' || args[1] == '5'
7848                       || args[1] == '-' || args[1] == '4');
7849               if (*s == '\0')
7850                 return;
7851
7852             case ')':           /* these must match exactly */
7853               if (*s++ == *args)
7854                 continue;
7855               break;
7856
7857             case '<':           /* must be at least one digit */
7858               /*
7859                * According to the manual, if the shift amount is greater
7860                * than 31 or less than 0, then the shift amount should be
7861                * mod 32.  In reality the mips assembler issues an error.
7862                * We issue a warning and mask out all but the low 5 bits.
7863                */
7864               my_getExpression (&imm_expr, s);
7865               check_absolute_expr (ip, &imm_expr);
7866               if ((unsigned long) imm_expr.X_add_number > 31)
7867                 {
7868                   as_warn (_("Improper shift amount (%lu)"),
7869                            (unsigned long) imm_expr.X_add_number);
7870                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7871                 }
7872               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7873               imm_expr.X_op = O_absent;
7874               s = expr_end;
7875               continue;
7876
7877             case '>':           /* shift amount minus 32 */
7878               my_getExpression (&imm_expr, s);
7879               check_absolute_expr (ip, &imm_expr);
7880               if ((unsigned long) imm_expr.X_add_number < 32
7881                   || (unsigned long) imm_expr.X_add_number > 63)
7882                 break;
7883               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7884               imm_expr.X_op = O_absent;
7885               s = expr_end;
7886               continue;
7887
7888             case 'k':           /* cache code */
7889             case 'h':           /* prefx code */
7890               my_getExpression (&imm_expr, s);
7891               check_absolute_expr (ip, &imm_expr);
7892               if ((unsigned long) imm_expr.X_add_number > 31)
7893                 {
7894                   as_warn (_("Invalid value for `%s' (%lu)"),
7895                            ip->insn_mo->name,
7896                            (unsigned long) imm_expr.X_add_number);
7897                   imm_expr.X_add_number &= 0x1f;
7898                 }
7899               if (*args == 'k')
7900                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7901               else
7902                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7903               imm_expr.X_op = O_absent;
7904               s = expr_end;
7905               continue;
7906
7907             case 'c':           /* break code */
7908               my_getExpression (&imm_expr, s);
7909               check_absolute_expr (ip, &imm_expr);
7910               if ((unsigned long) imm_expr.X_add_number > 1023)
7911                 {
7912                   as_warn (_("Illegal break code (%lu)"),
7913                            (unsigned long) imm_expr.X_add_number);
7914                   imm_expr.X_add_number &= OP_MASK_CODE;
7915                 }
7916               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7917               imm_expr.X_op = O_absent;
7918               s = expr_end;
7919               continue;
7920
7921             case 'q':           /* lower break code */
7922               my_getExpression (&imm_expr, s);
7923               check_absolute_expr (ip, &imm_expr);
7924               if ((unsigned long) imm_expr.X_add_number > 1023)
7925                 {
7926                   as_warn (_("Illegal lower break code (%lu)"),
7927                            (unsigned long) imm_expr.X_add_number);
7928                   imm_expr.X_add_number &= OP_MASK_CODE2;
7929                 }
7930               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7931               imm_expr.X_op = O_absent;
7932               s = expr_end;
7933               continue;
7934
7935             case 'B':           /* 20-bit syscall/break code.  */
7936               my_getExpression (&imm_expr, s);
7937               check_absolute_expr (ip, &imm_expr);
7938               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7939                 as_warn (_("Illegal 20-bit code (%lu)"),
7940                          (unsigned long) imm_expr.X_add_number);
7941               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7942               imm_expr.X_op = O_absent;
7943               s = expr_end;
7944               continue;
7945
7946             case 'C':           /* Coprocessor code */
7947               my_getExpression (&imm_expr, s);
7948               check_absolute_expr (ip, &imm_expr);
7949               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
7950                 {
7951                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
7952                            (unsigned long) imm_expr.X_add_number);
7953                   imm_expr.X_add_number &= ((1 << 25) - 1);
7954                 }
7955               ip->insn_opcode |= imm_expr.X_add_number;
7956               imm_expr.X_op = O_absent;
7957               s = expr_end;
7958               continue;
7959
7960             case 'J':           /* 19-bit wait code.  */
7961               my_getExpression (&imm_expr, s);
7962               check_absolute_expr (ip, &imm_expr);
7963               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7964                 as_warn (_("Illegal 19-bit code (%lu)"),
7965                          (unsigned long) imm_expr.X_add_number);
7966               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7967               imm_expr.X_op = O_absent;
7968               s = expr_end;
7969               continue;
7970
7971             case 'P':           /* Performance register */
7972               my_getExpression (&imm_expr, s);
7973               check_absolute_expr (ip, &imm_expr);
7974               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7975                 {
7976                   as_warn (_("Invalid performance register (%lu)"),
7977                            (unsigned long) imm_expr.X_add_number);
7978                   imm_expr.X_add_number &= OP_MASK_PERFREG;
7979                 }
7980               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7981               imm_expr.X_op = O_absent;
7982               s = expr_end;
7983               continue;
7984
7985             case 'b':           /* base register */
7986             case 'd':           /* destination register */
7987             case 's':           /* source register */
7988             case 't':           /* target register */
7989             case 'r':           /* both target and source */
7990             case 'v':           /* both dest and source */
7991             case 'w':           /* both dest and target */
7992             case 'E':           /* coprocessor target register */
7993             case 'G':           /* coprocessor destination register */
7994             case 'x':           /* ignore register name */
7995             case 'z':           /* must be zero register */
7996             case 'U':           /* destination register (clo/clz).  */
7997               s_reset = s;
7998               if (s[0] == '$')
7999                 {
8000
8001                   if (ISDIGIT (s[1]))
8002                     {
8003                       ++s;
8004                       regno = 0;
8005                       do
8006                         {
8007                           regno *= 10;
8008                           regno += *s - '0';
8009                           ++s;
8010                         }
8011                       while (ISDIGIT (*s));
8012                       if (regno > 31)
8013                         as_bad (_("Invalid register number (%d)"), regno);
8014                     }
8015                   else if (*args == 'E' || *args == 'G')
8016                     goto notreg;
8017                   else
8018                     {
8019                       if (s[1] == 'r' && s[2] == 'a')
8020                         {
8021                           s += 3;
8022                           regno = RA;
8023                         }
8024                       else if (s[1] == 'f' && s[2] == 'p')
8025                         {
8026                           s += 3;
8027                           regno = FP;
8028                         }
8029                       else if (s[1] == 's' && s[2] == 'p')
8030                         {
8031                           s += 3;
8032                           regno = SP;
8033                         }
8034                       else if (s[1] == 'g' && s[2] == 'p')
8035                         {
8036                           s += 3;
8037                           regno = GP;
8038                         }
8039                       else if (s[1] == 'a' && s[2] == 't')
8040                         {
8041                           s += 3;
8042                           regno = AT;
8043                         }
8044                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8045                         {
8046                           s += 4;
8047                           regno = KT0;
8048                         }
8049                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8050                         {
8051                           s += 4;
8052                           regno = KT1;
8053                         }
8054                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8055                         {
8056                           s += 5;
8057                           regno = ZERO;
8058                         }
8059                       else if (itbl_have_entries)
8060                         {
8061                           char *p, *n;
8062                           unsigned long r;
8063
8064                           p = s + 1;    /* advance past '$' */
8065                           n = itbl_get_field (&p);  /* n is name */
8066
8067                           /* See if this is a register defined in an
8068                              itbl entry.  */
8069                           if (itbl_get_reg_val (n, &r))
8070                             {
8071                               /* Get_field advances to the start of
8072                                  the next field, so we need to back
8073                                  rack to the end of the last field.  */
8074                               if (p)
8075                                 s = p - 1;
8076                               else
8077                                 s = strchr (s, '\0');
8078                               regno = r;
8079                             }
8080                           else
8081                             goto notreg;
8082                         }
8083                       else
8084                         goto notreg;
8085                     }
8086                   if (regno == AT
8087                       && ! mips_opts.noat
8088                       && *args != 'E'
8089                       && *args != 'G')
8090                     as_warn (_("Used $at without \".set noat\""));
8091                   c = *args;
8092                   if (*s == ' ')
8093                     ++s;
8094                   if (args[1] != *s)
8095                     {
8096                       if (c == 'r' || c == 'v' || c == 'w')
8097                         {
8098                           regno = lastregno;
8099                           s = s_reset;
8100                           ++args;
8101                         }
8102                     }
8103                   /* 'z' only matches $0.  */
8104                   if (c == 'z' && regno != 0)
8105                     break;
8106
8107         /* Now that we have assembled one operand, we use the args string
8108          * to figure out where it goes in the instruction.  */
8109                   switch (c)
8110                     {
8111                     case 'r':
8112                     case 's':
8113                     case 'v':
8114                     case 'b':
8115                       ip->insn_opcode |= regno << OP_SH_RS;
8116                       break;
8117                     case 'd':
8118                     case 'G':
8119                       ip->insn_opcode |= regno << OP_SH_RD;
8120                       break;
8121                     case 'U':
8122                       ip->insn_opcode |= regno << OP_SH_RD;
8123                       ip->insn_opcode |= regno << OP_SH_RT;
8124                       break;
8125                     case 'w':
8126                     case 't':
8127                     case 'E':
8128                       ip->insn_opcode |= regno << OP_SH_RT;
8129                       break;
8130                     case 'x':
8131                       /* This case exists because on the r3000 trunc
8132                          expands into a macro which requires a gp
8133                          register.  On the r6000 or r4000 it is
8134                          assembled into a single instruction which
8135                          ignores the register.  Thus the insn version
8136                          is MIPS_ISA2 and uses 'x', and the macro
8137                          version is MIPS_ISA1 and uses 't'.  */
8138                       break;
8139                     case 'z':
8140                       /* This case is for the div instruction, which
8141                          acts differently if the destination argument
8142                          is $0.  This only matches $0, and is checked
8143                          outside the switch.  */
8144                       break;
8145                     case 'D':
8146                       /* Itbl operand; not yet implemented. FIXME ?? */
8147                       break;
8148                       /* What about all other operands like 'i', which
8149                          can be specified in the opcode table? */
8150                     }
8151                   lastregno = regno;
8152                   continue;
8153                 }
8154             notreg:
8155               switch (*args++)
8156                 {
8157                 case 'r':
8158                 case 'v':
8159                   ip->insn_opcode |= lastregno << OP_SH_RS;
8160                   continue;
8161                 case 'w':
8162                   ip->insn_opcode |= lastregno << OP_SH_RT;
8163                   continue;
8164                 }
8165               break;
8166
8167             case 'O':           /* MDMX alignment immediate constant.  */
8168               my_getExpression (&imm_expr, s);
8169               check_absolute_expr (ip, &imm_expr);
8170               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8171                 {
8172                   as_warn ("Improper align amount (%ld), using low bits",
8173                            (long) imm_expr.X_add_number);
8174                   imm_expr.X_add_number &= OP_MASK_ALN;
8175                 }
8176               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8177               imm_expr.X_op = O_absent;
8178               s = expr_end;
8179               continue;
8180
8181             case 'Q':           /* MDMX vector, element sel, or const.  */
8182               if (s[0] != '$')
8183                 {
8184                   /* MDMX Immediate.  */
8185                   my_getExpression (&imm_expr, s);
8186                   check_absolute_expr (ip, &imm_expr);
8187                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8188                     {
8189                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8190                                (long) imm_expr.X_add_number);
8191                       imm_expr.X_add_number &= OP_MASK_FT;
8192                     }
8193                   imm_expr.X_add_number &= OP_MASK_FT;
8194                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8195                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8196                   else
8197                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8198                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8199                   imm_expr.X_op = O_absent;
8200                   s = expr_end;
8201                   continue;
8202                 }
8203               /* Not MDMX Immediate.  Fall through.  */
8204             case 'X':           /* MDMX destination register.  */
8205             case 'Y':           /* MDMX source register.  */
8206             case 'Z':           /* MDMX target register.  */
8207               is_mdmx = 1;
8208             case 'D':           /* floating point destination register */
8209             case 'S':           /* floating point source register */
8210             case 'T':           /* floating point target register */
8211             case 'R':           /* floating point source register */
8212             case 'V':
8213             case 'W':
8214               s_reset = s;
8215               /* Accept $fN for FP and MDMX register numbers, and in
8216                  addition accept $vN for MDMX register numbers.  */
8217               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8218                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8219                       && ISDIGIT (s[2])))
8220                 {
8221                   s += 2;
8222                   regno = 0;
8223                   do
8224                     {
8225                       regno *= 10;
8226                       regno += *s - '0';
8227                       ++s;
8228                     }
8229                   while (ISDIGIT (*s));
8230
8231                   if (regno > 31)
8232                     as_bad (_("Invalid float register number (%d)"), regno);
8233
8234                   if ((regno & 1) != 0
8235                       && HAVE_32BIT_FPRS
8236                       && ! (strcmp (str, "mtc1") == 0
8237                             || strcmp (str, "mfc1") == 0
8238                             || strcmp (str, "lwc1") == 0
8239                             || strcmp (str, "swc1") == 0
8240                             || strcmp (str, "l.s") == 0
8241                             || strcmp (str, "s.s") == 0))
8242                     as_warn (_("Float register should be even, was %d"),
8243                              regno);
8244
8245                   c = *args;
8246                   if (*s == ' ')
8247                     ++s;
8248                   if (args[1] != *s)
8249                     {
8250                       if (c == 'V' || c == 'W')
8251                         {
8252                           regno = lastregno;
8253                           s = s_reset;
8254                           ++args;
8255                         }
8256                     }
8257                   switch (c)
8258                     {
8259                     case 'D':
8260                     case 'X':
8261                       ip->insn_opcode |= regno << OP_SH_FD;
8262                       break;
8263                     case 'V':
8264                     case 'S':
8265                     case 'Y':
8266                       ip->insn_opcode |= regno << OP_SH_FS;
8267                       break;
8268                     case 'Q':
8269                       /* This is like 'Z', but also needs to fix the MDMX
8270                          vector/scalar select bits.  Note that the
8271                          scalar immediate case is handled above.  */
8272                       if (*s == '[')
8273                         {
8274                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8275                           int max_el = (is_qh ? 3 : 7);
8276                           s++;
8277                           my_getExpression(&imm_expr, s);
8278                           check_absolute_expr (ip, &imm_expr);
8279                           s = expr_end;
8280                           if (imm_expr.X_add_number > max_el)
8281                             as_bad(_("Bad element selector %ld"),
8282                                    (long) imm_expr.X_add_number);
8283                           imm_expr.X_add_number &= max_el;
8284                           ip->insn_opcode |= (imm_expr.X_add_number
8285                                               << (OP_SH_VSEL +
8286                                                   (is_qh ? 2 : 1)));
8287                           if (*s != ']')
8288                             as_warn(_("Expecting ']' found '%s'"), s);
8289                           else
8290                             s++;
8291                         }
8292                       else
8293                         {
8294                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8295                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8296                                                 << OP_SH_VSEL);
8297                           else
8298                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8299                                                 OP_SH_VSEL);
8300                         }
8301                       /* Fall through */
8302                     case 'W':
8303                     case 'T':
8304                     case 'Z':
8305                       ip->insn_opcode |= regno << OP_SH_FT;
8306                       break;
8307                     case 'R':
8308                       ip->insn_opcode |= regno << OP_SH_FR;
8309                       break;
8310                     }
8311                   lastregno = regno;
8312                   continue;
8313                 }
8314
8315               switch (*args++)
8316                 {
8317                 case 'V':
8318                   ip->insn_opcode |= lastregno << OP_SH_FS;
8319                   continue;
8320                 case 'W':
8321                   ip->insn_opcode |= lastregno << OP_SH_FT;
8322                   continue;
8323                 }
8324               break;
8325
8326             case 'I':
8327               my_getExpression (&imm_expr, s);
8328               if (imm_expr.X_op != O_big
8329                   && imm_expr.X_op != O_constant)
8330                 insn_error = _("absolute expression required");
8331               s = expr_end;
8332               continue;
8333
8334             case 'A':
8335               my_getExpression (&offset_expr, s);
8336               *imm_reloc = BFD_RELOC_32;
8337               s = expr_end;
8338               continue;
8339
8340             case 'F':
8341             case 'L':
8342             case 'f':
8343             case 'l':
8344               {
8345                 int f64;
8346                 int using_gprs;
8347                 char *save_in;
8348                 char *err;
8349                 unsigned char temp[8];
8350                 int len;
8351                 unsigned int length;
8352                 segT seg;
8353                 subsegT subseg;
8354                 char *p;
8355
8356                 /* These only appear as the last operand in an
8357                    instruction, and every instruction that accepts
8358                    them in any variant accepts them in all variants.
8359                    This means we don't have to worry about backing out
8360                    any changes if the instruction does not match.
8361
8362                    The difference between them is the size of the
8363                    floating point constant and where it goes.  For 'F'
8364                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8365                    is 32 bits.  Where the constant is placed is based
8366                    on how the MIPS assembler does things:
8367                     F -- .rdata
8368                     L -- .lit8
8369                     f -- immediate value
8370                     l -- .lit4
8371
8372                     The .lit4 and .lit8 sections are only used if
8373                     permitted by the -G argument.
8374
8375                     When generating embedded PIC code, we use the
8376                     .lit8 section but not the .lit4 section (we can do
8377                     .lit4 inline easily; we need to put .lit8
8378                     somewhere in the data segment, and using .lit8
8379                     permits the linker to eventually combine identical
8380                     .lit8 entries).
8381
8382                     The code below needs to know whether the target register
8383                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8384                     'F' are used with GPR-based instructions and 'l' and
8385                     'L' are used with FPR-based instructions.  */
8386
8387                 f64 = *args == 'F' || *args == 'L';
8388                 using_gprs = *args == 'F' || *args == 'f';
8389
8390                 save_in = input_line_pointer;
8391                 input_line_pointer = s;
8392                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8393                 length = len;
8394                 s = input_line_pointer;
8395                 input_line_pointer = save_in;
8396                 if (err != NULL && *err != '\0')
8397                   {
8398                     as_bad (_("Bad floating point constant: %s"), err);
8399                     memset (temp, '\0', sizeof temp);
8400                     length = f64 ? 8 : 4;
8401                   }
8402
8403                 assert (length == (unsigned) (f64 ? 8 : 4));
8404
8405                 if (*args == 'f'
8406                     || (*args == 'l'
8407                         && (! USE_GLOBAL_POINTER_OPT
8408                             || mips_pic == EMBEDDED_PIC
8409                             || g_switch_value < 4
8410                             || (temp[0] == 0 && temp[1] == 0)
8411                             || (temp[2] == 0 && temp[3] == 0))))
8412                   {
8413                     imm_expr.X_op = O_constant;
8414                     if (! target_big_endian)
8415                       imm_expr.X_add_number = bfd_getl32 (temp);
8416                     else
8417                       imm_expr.X_add_number = bfd_getb32 (temp);
8418                   }
8419                 else if (length > 4
8420                          && ! mips_disable_float_construction
8421                          /* Constants can only be constructed in GPRs and
8422                             copied to FPRs if the GPRs are at least as wide
8423                             as the FPRs.  Force the constant into memory if
8424                             we are using 64-bit FPRs but the GPRs are only
8425                             32 bits wide.  */
8426                          && (using_gprs
8427                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8428                          && ((temp[0] == 0 && temp[1] == 0)
8429                              || (temp[2] == 0 && temp[3] == 0))
8430                          && ((temp[4] == 0 && temp[5] == 0)
8431                              || (temp[6] == 0 && temp[7] == 0)))
8432                   {
8433                     /* The value is simple enough to load with a couple of
8434                        instructions.  If using 32-bit registers, set
8435                        imm_expr to the high order 32 bits and offset_expr to
8436                        the low order 32 bits.  Otherwise, set imm_expr to
8437                        the entire 64 bit constant.  */
8438                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8439                       {
8440                         imm_expr.X_op = O_constant;
8441                         offset_expr.X_op = O_constant;
8442                         if (! target_big_endian)
8443                           {
8444                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8445                             offset_expr.X_add_number = bfd_getl32 (temp);
8446                           }
8447                         else
8448                           {
8449                             imm_expr.X_add_number = bfd_getb32 (temp);
8450                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8451                           }
8452                         if (offset_expr.X_add_number == 0)
8453                           offset_expr.X_op = O_absent;
8454                       }
8455                     else if (sizeof (imm_expr.X_add_number) > 4)
8456                       {
8457                         imm_expr.X_op = O_constant;
8458                         if (! target_big_endian)
8459                           imm_expr.X_add_number = bfd_getl64 (temp);
8460                         else
8461                           imm_expr.X_add_number = bfd_getb64 (temp);
8462                       }
8463                     else
8464                       {
8465                         imm_expr.X_op = O_big;
8466                         imm_expr.X_add_number = 4;
8467                         if (! target_big_endian)
8468                           {
8469                             generic_bignum[0] = bfd_getl16 (temp);
8470                             generic_bignum[1] = bfd_getl16 (temp + 2);
8471                             generic_bignum[2] = bfd_getl16 (temp + 4);
8472                             generic_bignum[3] = bfd_getl16 (temp + 6);
8473                           }
8474                         else
8475                           {
8476                             generic_bignum[0] = bfd_getb16 (temp + 6);
8477                             generic_bignum[1] = bfd_getb16 (temp + 4);
8478                             generic_bignum[2] = bfd_getb16 (temp + 2);
8479                             generic_bignum[3] = bfd_getb16 (temp);
8480                           }
8481                       }
8482                   }
8483                 else
8484                   {
8485                     const char *newname;
8486                     segT new_seg;
8487
8488                     /* Switch to the right section.  */
8489                     seg = now_seg;
8490                     subseg = now_subseg;
8491                     switch (*args)
8492                       {
8493                       default: /* unused default case avoids warnings.  */
8494                       case 'L':
8495                         newname = RDATA_SECTION_NAME;
8496                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8497                             || mips_pic == EMBEDDED_PIC)
8498                           newname = ".lit8";
8499                         break;
8500                       case 'F':
8501                         if (mips_pic == EMBEDDED_PIC)
8502                           newname = ".lit8";
8503                         else
8504                           newname = RDATA_SECTION_NAME;
8505                         break;
8506                       case 'l':
8507                         assert (!USE_GLOBAL_POINTER_OPT
8508                                 || g_switch_value >= 4);
8509                         newname = ".lit4";
8510                         break;
8511                       }
8512                     new_seg = subseg_new (newname, (subsegT) 0);
8513                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8514                       bfd_set_section_flags (stdoutput, new_seg,
8515                                              (SEC_ALLOC
8516                                               | SEC_LOAD
8517                                               | SEC_READONLY
8518                                               | SEC_DATA));
8519                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8520                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8521                         && strcmp (TARGET_OS, "elf") != 0)
8522                       record_alignment (new_seg, 4);
8523                     else
8524                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8525                     if (seg == now_seg)
8526                       as_bad (_("Can't use floating point insn in this section"));
8527
8528                     /* Set the argument to the current address in the
8529                        section.  */
8530                     offset_expr.X_op = O_symbol;
8531                     offset_expr.X_add_symbol =
8532                       symbol_new ("L0\001", now_seg,
8533                                   (valueT) frag_now_fix (), frag_now);
8534                     offset_expr.X_add_number = 0;
8535
8536                     /* Put the floating point number into the section.  */
8537                     p = frag_more ((int) length);
8538                     memcpy (p, temp, length);
8539
8540                     /* Switch back to the original section.  */
8541                     subseg_set (seg, subseg);
8542                   }
8543               }
8544               continue;
8545
8546             case 'i':           /* 16 bit unsigned immediate */
8547             case 'j':           /* 16 bit signed immediate */
8548               *imm_reloc = BFD_RELOC_LO16;
8549               c = my_getSmallExpression (&imm_expr, s);
8550               if (c != S_EX_NONE)
8551                 {
8552                   if (c != S_EX_LO)
8553                     {
8554                       if (c == S_EX_HI)
8555                         {
8556                           *imm_reloc = BFD_RELOC_HI16_S;
8557                           imm_unmatched_hi = true;
8558                         }
8559 #ifdef OBJ_ELF
8560                       else if (c == S_EX_HIGHEST)
8561                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8562                       else if (c == S_EX_HIGHER)
8563                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8564                       else if (c == S_EX_GP_REL)
8565                         {
8566                           /* This occurs in NewABI only.  */
8567                           c = my_getSmallExpression (&imm_expr, s);
8568                           if (c != S_EX_NEG)
8569                             as_bad (_("bad composition of relocations"));
8570                           else
8571                             {
8572                               c = my_getSmallExpression (&imm_expr, s);
8573                               if (c != S_EX_LO)
8574                                 as_bad (_("bad composition of relocations"));
8575                               else
8576                                 {
8577                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8578                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8579                                   imm_reloc[2] = BFD_RELOC_LO16;
8580                                 }
8581                             }
8582                         }
8583 #endif
8584                       else
8585                         *imm_reloc = BFD_RELOC_HI16;
8586                     }
8587                   else if (imm_expr.X_op == O_constant)
8588                     imm_expr.X_add_number &= 0xffff;
8589                 }
8590               if (*args == 'i')
8591                 {
8592                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8593                       || ((imm_expr.X_add_number < 0
8594                            || imm_expr.X_add_number >= 0x10000)
8595                           && imm_expr.X_op == O_constant))
8596                     {
8597                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8598                           !strcmp (insn->name, insn[1].name))
8599                         break;
8600                       if (imm_expr.X_op == O_constant
8601                           || imm_expr.X_op == O_big)
8602                         as_bad (_("16 bit expression not in range 0..65535"));
8603                     }
8604                 }
8605               else
8606                 {
8607                   int more;
8608                   offsetT max;
8609
8610                   /* The upper bound should be 0x8000, but
8611                      unfortunately the MIPS assembler accepts numbers
8612                      from 0x8000 to 0xffff and sign extends them, and
8613                      we want to be compatible.  We only permit this
8614                      extended range for an instruction which does not
8615                      provide any further alternates, since those
8616                      alternates may handle other cases.  People should
8617                      use the numbers they mean, rather than relying on
8618                      a mysterious sign extension.  */
8619                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8620                           strcmp (insn->name, insn[1].name) == 0);
8621                   if (more)
8622                     max = 0x8000;
8623                   else
8624                     max = 0x10000;
8625                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8626                       || ((imm_expr.X_add_number < -0x8000
8627                            || imm_expr.X_add_number >= max)
8628                           && imm_expr.X_op == O_constant)
8629                       || (more
8630                           && imm_expr.X_add_number < 0
8631                           && HAVE_64BIT_GPRS
8632                           && imm_expr.X_unsigned
8633                           && sizeof (imm_expr.X_add_number) <= 4))
8634                     {
8635                       if (more)
8636                         break;
8637                       if (imm_expr.X_op == O_constant
8638                           || imm_expr.X_op == O_big)
8639                         as_bad (_("16 bit expression not in range -32768..32767"));
8640                     }
8641                 }
8642               s = expr_end;
8643               continue;
8644
8645             case 'o':           /* 16 bit offset */
8646               c = my_getSmallExpression (&offset_expr, s);
8647
8648               /* If this value won't fit into a 16 bit offset, then go
8649                  find a macro that will generate the 32 bit offset
8650                  code pattern.  */
8651               if (c == S_EX_NONE
8652                   && (offset_expr.X_op != O_constant
8653                       || offset_expr.X_add_number >= 0x8000
8654                       || offset_expr.X_add_number < -0x8000))
8655                 break;
8656
8657               if (c == S_EX_HI)
8658                 {
8659                   if (offset_expr.X_op != O_constant)
8660                     break;
8661                   offset_expr.X_add_number =
8662                     (offset_expr.X_add_number >> 16) & 0xffff;
8663                 }
8664               *offset_reloc = BFD_RELOC_LO16;
8665               s = expr_end;
8666               continue;
8667
8668             case 'p':           /* pc relative offset */
8669               if (mips_pic == EMBEDDED_PIC)
8670                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8671               else
8672                 *offset_reloc = BFD_RELOC_16_PCREL;
8673               my_getExpression (&offset_expr, s);
8674               s = expr_end;
8675               continue;
8676
8677             case 'u':           /* upper 16 bits */
8678               c = my_getSmallExpression (&imm_expr, s);
8679               *imm_reloc = BFD_RELOC_LO16;
8680               if (c != S_EX_NONE)
8681                 {
8682                   if (c != S_EX_LO)
8683                     {
8684                       if (c == S_EX_HI)
8685                         {
8686                           *imm_reloc = BFD_RELOC_HI16_S;
8687                           imm_unmatched_hi = true;
8688                         }
8689 #ifdef OBJ_ELF
8690                       else if (c == S_EX_HIGHEST)
8691                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8692                       else if (c == S_EX_GP_REL)
8693                         {
8694                           /* This occurs in NewABI only.  */
8695                           c = my_getSmallExpression (&imm_expr, s);
8696                           if (c != S_EX_NEG)
8697                             as_bad (_("bad composition of relocations"));
8698                           else
8699                             {
8700                               c = my_getSmallExpression (&imm_expr, s);
8701                               if (c != S_EX_HI)
8702                                 as_bad (_("bad composition of relocations"));
8703                               else
8704                                 {
8705                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8706                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8707                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8708                                 }
8709                             }
8710                         }
8711 #endif
8712                       else
8713                         *imm_reloc = BFD_RELOC_HI16;
8714                     }
8715                   else if (imm_expr.X_op == O_constant)
8716                     imm_expr.X_add_number &= 0xffff;
8717                 }
8718               else if (imm_expr.X_op == O_constant
8719                        && (imm_expr.X_add_number < 0
8720                            || imm_expr.X_add_number >= 0x10000))
8721                 as_bad (_("lui expression not in range 0..65535"));
8722               s = expr_end;
8723               continue;
8724
8725             case 'a':           /* 26 bit address */
8726               my_getExpression (&offset_expr, s);
8727               s = expr_end;
8728               *offset_reloc = BFD_RELOC_MIPS_JMP;
8729               continue;
8730
8731             case 'N':           /* 3 bit branch condition code */
8732             case 'M':           /* 3 bit compare condition code */
8733               if (strncmp (s, "$fcc", 4) != 0)
8734                 break;
8735               s += 4;
8736               regno = 0;
8737               do
8738                 {
8739                   regno *= 10;
8740                   regno += *s - '0';
8741                   ++s;
8742                 }
8743               while (ISDIGIT (*s));
8744               if (regno > 7)
8745                 as_bad (_("invalid condition code register $fcc%d"), regno);
8746               if (*args == 'N')
8747                 ip->insn_opcode |= regno << OP_SH_BCC;
8748               else
8749                 ip->insn_opcode |= regno << OP_SH_CCC;
8750               continue;
8751
8752             case 'H':
8753               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8754                 s += 2;
8755               if (ISDIGIT (*s))
8756                 {
8757                   c = 0;
8758                   do
8759                     {
8760                       c *= 10;
8761                       c += *s - '0';
8762                       ++s;
8763                     }
8764                   while (ISDIGIT (*s));
8765                 }
8766               else
8767                 c = 8; /* Invalid sel value.  */
8768
8769               if (c > 7)
8770                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8771               ip->insn_opcode |= c;
8772               continue;
8773
8774             default:
8775               as_bad (_("bad char = '%c'\n"), *args);
8776               internalError ();
8777             }
8778           break;
8779         }
8780       /* Args don't match.  */
8781       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8782           !strcmp (insn->name, insn[1].name))
8783         {
8784           ++insn;
8785           s = argsStart;
8786           insn_error = _("illegal operands");
8787           continue;
8788         }
8789       if (save_c)
8790         *(--s) = save_c;
8791       insn_error = _("illegal operands");
8792       return;
8793     }
8794 }
8795
8796 /* This routine assembles an instruction into its binary format when
8797    assembling for the mips16.  As a side effect, it sets one of the
8798    global variables imm_reloc or offset_reloc to the type of
8799    relocation to do if one of the operands is an address expression.
8800    It also sets mips16_small and mips16_ext if the user explicitly
8801    requested a small or extended instruction.  */
8802
8803 static void
8804 mips16_ip (str, ip)
8805      char *str;
8806      struct mips_cl_insn *ip;
8807 {
8808   char *s;
8809   const char *args;
8810   struct mips_opcode *insn;
8811   char *argsstart;
8812   unsigned int regno;
8813   unsigned int lastregno = 0;
8814   char *s_reset;
8815
8816   insn_error = NULL;
8817
8818   mips16_small = false;
8819   mips16_ext = false;
8820
8821   for (s = str; ISLOWER (*s); ++s)
8822     ;
8823   switch (*s)
8824     {
8825     case '\0':
8826       break;
8827
8828     case ' ':
8829       *s++ = '\0';
8830       break;
8831
8832     case '.':
8833       if (s[1] == 't' && s[2] == ' ')
8834         {
8835           *s = '\0';
8836           mips16_small = true;
8837           s += 3;
8838           break;
8839         }
8840       else if (s[1] == 'e' && s[2] == ' ')
8841         {
8842           *s = '\0';
8843           mips16_ext = true;
8844           s += 3;
8845           break;
8846         }
8847       /* Fall through.  */
8848     default:
8849       insn_error = _("unknown opcode");
8850       return;
8851     }
8852
8853   if (mips_opts.noautoextend && ! mips16_ext)
8854     mips16_small = true;
8855
8856   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8857     {
8858       insn_error = _("unrecognized opcode");
8859       return;
8860     }
8861
8862   argsstart = s;
8863   for (;;)
8864     {
8865       assert (strcmp (insn->name, str) == 0);
8866
8867       ip->insn_mo = insn;
8868       ip->insn_opcode = insn->match;
8869       ip->use_extend = false;
8870       imm_expr.X_op = O_absent;
8871       imm_reloc[0] = BFD_RELOC_UNUSED;
8872       imm_reloc[1] = BFD_RELOC_UNUSED;
8873       imm_reloc[2] = BFD_RELOC_UNUSED;
8874       offset_expr.X_op = O_absent;
8875       offset_reloc[0] = BFD_RELOC_UNUSED;
8876       offset_reloc[1] = BFD_RELOC_UNUSED;
8877       offset_reloc[2] = BFD_RELOC_UNUSED;
8878       for (args = insn->args; 1; ++args)
8879         {
8880           int c;
8881
8882           if (*s == ' ')
8883             ++s;
8884
8885           /* In this switch statement we call break if we did not find
8886              a match, continue if we did find a match, or return if we
8887              are done.  */
8888
8889           c = *args;
8890           switch (c)
8891             {
8892             case '\0':
8893               if (*s == '\0')
8894                 {
8895                   /* Stuff the immediate value in now, if we can.  */
8896                   if (imm_expr.X_op == O_constant
8897                       && *imm_reloc > BFD_RELOC_UNUSED
8898                       && insn->pinfo != INSN_MACRO)
8899                     {
8900                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8901                                     imm_expr.X_add_number, true, mips16_small,
8902                                     mips16_ext, &ip->insn_opcode,
8903                                     &ip->use_extend, &ip->extend);
8904                       imm_expr.X_op = O_absent;
8905                       *imm_reloc = BFD_RELOC_UNUSED;
8906                     }
8907
8908                   return;
8909                 }
8910               break;
8911
8912             case ',':
8913               if (*s++ == c)
8914                 continue;
8915               s--;
8916               switch (*++args)
8917                 {
8918                 case 'v':
8919                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8920                   continue;
8921                 case 'w':
8922                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8923                   continue;
8924                 }
8925               break;
8926
8927             case '(':
8928             case ')':
8929               if (*s++ == c)
8930                 continue;
8931               break;
8932
8933             case 'v':
8934             case 'w':
8935               if (s[0] != '$')
8936                 {
8937                   if (c == 'v')
8938                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8939                   else
8940                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8941                   ++args;
8942                   continue;
8943                 }
8944               /* Fall through.  */
8945             case 'x':
8946             case 'y':
8947             case 'z':
8948             case 'Z':
8949             case '0':
8950             case 'S':
8951             case 'R':
8952             case 'X':
8953             case 'Y':
8954               if (s[0] != '$')
8955                 break;
8956               s_reset = s;
8957               if (ISDIGIT (s[1]))
8958                 {
8959                   ++s;
8960                   regno = 0;
8961                   do
8962                     {
8963                       regno *= 10;
8964                       regno += *s - '0';
8965                       ++s;
8966                     }
8967                   while (ISDIGIT (*s));
8968                   if (regno > 31)
8969                     {
8970                       as_bad (_("invalid register number (%d)"), regno);
8971                       regno = 2;
8972                     }
8973                 }
8974               else
8975                 {
8976                   if (s[1] == 'r' && s[2] == 'a')
8977                     {
8978                       s += 3;
8979                       regno = RA;
8980                     }
8981                   else if (s[1] == 'f' && s[2] == 'p')
8982                     {
8983                       s += 3;
8984                       regno = FP;
8985                     }
8986                   else if (s[1] == 's' && s[2] == 'p')
8987                     {
8988                       s += 3;
8989                       regno = SP;
8990                     }
8991                   else if (s[1] == 'g' && s[2] == 'p')
8992                     {
8993                       s += 3;
8994                       regno = GP;
8995                     }
8996                   else if (s[1] == 'a' && s[2] == 't')
8997                     {
8998                       s += 3;
8999                       regno = AT;
9000                     }
9001                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9002                     {
9003                       s += 4;
9004                       regno = KT0;
9005                     }
9006                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9007                     {
9008                       s += 4;
9009                       regno = KT1;
9010                     }
9011                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9012                     {
9013                       s += 5;
9014                       regno = ZERO;
9015                     }
9016                   else
9017                     break;
9018                 }
9019
9020               if (*s == ' ')
9021                 ++s;
9022               if (args[1] != *s)
9023                 {
9024                   if (c == 'v' || c == 'w')
9025                     {
9026                       regno = mips16_to_32_reg_map[lastregno];
9027                       s = s_reset;
9028                       ++args;
9029                     }
9030                 }
9031
9032               switch (c)
9033                 {
9034                 case 'x':
9035                 case 'y':
9036                 case 'z':
9037                 case 'v':
9038                 case 'w':
9039                 case 'Z':
9040                   regno = mips32_to_16_reg_map[regno];
9041                   break;
9042
9043                 case '0':
9044                   if (regno != 0)
9045                     regno = ILLEGAL_REG;
9046                   break;
9047
9048                 case 'S':
9049                   if (regno != SP)
9050                     regno = ILLEGAL_REG;
9051                   break;
9052
9053                 case 'R':
9054                   if (regno != RA)
9055                     regno = ILLEGAL_REG;
9056                   break;
9057
9058                 case 'X':
9059                 case 'Y':
9060                   if (regno == AT && ! mips_opts.noat)
9061                     as_warn (_("used $at without \".set noat\""));
9062                   break;
9063
9064                 default:
9065                   internalError ();
9066                 }
9067
9068               if (regno == ILLEGAL_REG)
9069                 break;
9070
9071               switch (c)
9072                 {
9073                 case 'x':
9074                 case 'v':
9075                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9076                   break;
9077                 case 'y':
9078                 case 'w':
9079                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9080                   break;
9081                 case 'z':
9082                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9083                   break;
9084                 case 'Z':
9085                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9086                 case '0':
9087                 case 'S':
9088                 case 'R':
9089                   break;
9090                 case 'X':
9091                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9092                   break;
9093                 case 'Y':
9094                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9095                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9096                   break;
9097                 default:
9098                   internalError ();
9099                 }
9100
9101               lastregno = regno;
9102               continue;
9103
9104             case 'P':
9105               if (strncmp (s, "$pc", 3) == 0)
9106                 {
9107                   s += 3;
9108                   continue;
9109                 }
9110               break;
9111
9112             case '<':
9113             case '>':
9114             case '[':
9115             case ']':
9116             case '4':
9117             case '5':
9118             case 'H':
9119             case 'W':
9120             case 'D':
9121             case 'j':
9122             case '8':
9123             case 'V':
9124             case 'C':
9125             case 'U':
9126             case 'k':
9127             case 'K':
9128               if (s[0] == '%'
9129                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9130                 {
9131                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9132                      and generate the appropriate reloc.  If the text
9133                      inside %gprel is not a symbol name with an
9134                      optional offset, then we generate a normal reloc
9135                      and will probably fail later.  */
9136                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9137                   if (imm_expr.X_op == O_symbol)
9138                     {
9139                       mips16_ext = true;
9140                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9141                       s = expr_end;
9142                       ip->use_extend = true;
9143                       ip->extend = 0;
9144                       continue;
9145                     }
9146                 }
9147               else
9148                 {
9149                   /* Just pick up a normal expression.  */
9150                   my_getExpression (&imm_expr, s);
9151                 }
9152
9153               if (imm_expr.X_op == O_register)
9154                 {
9155                   /* What we thought was an expression turned out to
9156                      be a register.  */
9157
9158                   if (s[0] == '(' && args[1] == '(')
9159                     {
9160                       /* It looks like the expression was omitted
9161                          before a register indirection, which means
9162                          that the expression is implicitly zero.  We
9163                          still set up imm_expr, so that we handle
9164                          explicit extensions correctly.  */
9165                       imm_expr.X_op = O_constant;
9166                       imm_expr.X_add_number = 0;
9167                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9168                       continue;
9169                     }
9170
9171                   break;
9172                 }
9173
9174               /* We need to relax this instruction.  */
9175               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9176               s = expr_end;
9177               continue;
9178
9179             case 'p':
9180             case 'q':
9181             case 'A':
9182             case 'B':
9183             case 'E':
9184               /* We use offset_reloc rather than imm_reloc for the PC
9185                  relative operands.  This lets macros with both
9186                  immediate and address operands work correctly.  */
9187               my_getExpression (&offset_expr, s);
9188
9189               if (offset_expr.X_op == O_register)
9190                 break;
9191
9192               /* We need to relax this instruction.  */
9193               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9194               s = expr_end;
9195               continue;
9196
9197             case '6':           /* break code */
9198               my_getExpression (&imm_expr, s);
9199               check_absolute_expr (ip, &imm_expr);
9200               if ((unsigned long) imm_expr.X_add_number > 63)
9201                 {
9202                   as_warn (_("Invalid value for `%s' (%lu)"),
9203                            ip->insn_mo->name,
9204                            (unsigned long) imm_expr.X_add_number);
9205                   imm_expr.X_add_number &= 0x3f;
9206                 }
9207               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9208               imm_expr.X_op = O_absent;
9209               s = expr_end;
9210               continue;
9211
9212             case 'a':           /* 26 bit address */
9213               my_getExpression (&offset_expr, s);
9214               s = expr_end;
9215               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9216               ip->insn_opcode <<= 16;
9217               continue;
9218
9219             case 'l':           /* register list for entry macro */
9220             case 'L':           /* register list for exit macro */
9221               {
9222                 int mask;
9223
9224                 if (c == 'l')
9225                   mask = 0;
9226                 else
9227                   mask = 7 << 3;
9228                 while (*s != '\0')
9229                   {
9230                     int freg, reg1, reg2;
9231
9232                     while (*s == ' ' || *s == ',')
9233                       ++s;
9234                     if (*s != '$')
9235                       {
9236                         as_bad (_("can't parse register list"));
9237                         break;
9238                       }
9239                     ++s;
9240                     if (*s != 'f')
9241                       freg = 0;
9242                     else
9243                       {
9244                         freg = 1;
9245                         ++s;
9246                       }
9247                     reg1 = 0;
9248                     while (ISDIGIT (*s))
9249                       {
9250                         reg1 *= 10;
9251                         reg1 += *s - '0';
9252                         ++s;
9253                       }
9254                     if (*s == ' ')
9255                       ++s;
9256                     if (*s != '-')
9257                       reg2 = reg1;
9258                     else
9259                       {
9260                         ++s;
9261                         if (*s != '$')
9262                           break;
9263                         ++s;
9264                         if (freg)
9265                           {
9266                             if (*s == 'f')
9267                               ++s;
9268                             else
9269                               {
9270                                 as_bad (_("invalid register list"));
9271                                 break;
9272                               }
9273                           }
9274                         reg2 = 0;
9275                         while (ISDIGIT (*s))
9276                           {
9277                             reg2 *= 10;
9278                             reg2 += *s - '0';
9279                             ++s;
9280                           }
9281                       }
9282                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9283                       {
9284                         mask &= ~ (7 << 3);
9285                         mask |= 5 << 3;
9286                       }
9287                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9288                       {
9289                         mask &= ~ (7 << 3);
9290                         mask |= 6 << 3;
9291                       }
9292                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9293                       mask |= (reg2 - 3) << 3;
9294                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9295                       mask |= (reg2 - 15) << 1;
9296                     else if (reg1 == RA && reg2 == RA)
9297                       mask |= 1;
9298                     else
9299                       {
9300                         as_bad (_("invalid register list"));
9301                         break;
9302                       }
9303                   }
9304                 /* The mask is filled in in the opcode table for the
9305                    benefit of the disassembler.  We remove it before
9306                    applying the actual mask.  */
9307                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9308                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9309               }
9310             continue;
9311
9312             case 'e':           /* extend code */
9313               my_getExpression (&imm_expr, s);
9314               check_absolute_expr (ip, &imm_expr);
9315               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9316                 {
9317                   as_warn (_("Invalid value for `%s' (%lu)"),
9318                            ip->insn_mo->name,
9319                            (unsigned long) imm_expr.X_add_number);
9320                   imm_expr.X_add_number &= 0x7ff;
9321                 }
9322               ip->insn_opcode |= imm_expr.X_add_number;
9323               imm_expr.X_op = O_absent;
9324               s = expr_end;
9325               continue;
9326
9327             default:
9328               internalError ();
9329             }
9330           break;
9331         }
9332
9333       /* Args don't match.  */
9334       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9335           strcmp (insn->name, insn[1].name) == 0)
9336         {
9337           ++insn;
9338           s = argsstart;
9339           continue;
9340         }
9341
9342       insn_error = _("illegal operands");
9343
9344       return;
9345     }
9346 }
9347
9348 /* This structure holds information we know about a mips16 immediate
9349    argument type.  */
9350
9351 struct mips16_immed_operand
9352 {
9353   /* The type code used in the argument string in the opcode table.  */
9354   int type;
9355   /* The number of bits in the short form of the opcode.  */
9356   int nbits;
9357   /* The number of bits in the extended form of the opcode.  */
9358   int extbits;
9359   /* The amount by which the short form is shifted when it is used;
9360      for example, the sw instruction has a shift count of 2.  */
9361   int shift;
9362   /* The amount by which the short form is shifted when it is stored
9363      into the instruction code.  */
9364   int op_shift;
9365   /* Non-zero if the short form is unsigned.  */
9366   int unsp;
9367   /* Non-zero if the extended form is unsigned.  */
9368   int extu;
9369   /* Non-zero if the value is PC relative.  */
9370   int pcrel;
9371 };
9372
9373 /* The mips16 immediate operand types.  */
9374
9375 static const struct mips16_immed_operand mips16_immed_operands[] =
9376 {
9377   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9378   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9379   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9380   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9381   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9382   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9383   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9384   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9385   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9386   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9387   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9388   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9389   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9390   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9391   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9392   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9393   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9394   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9395   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9396   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9397   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9398 };
9399
9400 #define MIPS16_NUM_IMMED \
9401   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9402
9403 /* Handle a mips16 instruction with an immediate value.  This or's the
9404    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9405    whether an extended value is needed; if one is needed, it sets
9406    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9407    If SMALL is true, an unextended opcode was explicitly requested.
9408    If EXT is true, an extended opcode was explicitly requested.  If
9409    WARN is true, warn if EXT does not match reality.  */
9410
9411 static void
9412 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9413               extend)
9414      char *file;
9415      unsigned int line;
9416      int type;
9417      offsetT val;
9418      boolean warn;
9419      boolean small;
9420      boolean ext;
9421      unsigned long *insn;
9422      boolean *use_extend;
9423      unsigned short *extend;
9424 {
9425   register const struct mips16_immed_operand *op;
9426   int mintiny, maxtiny;
9427   boolean needext;
9428
9429   op = mips16_immed_operands;
9430   while (op->type != type)
9431     {
9432       ++op;
9433       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9434     }
9435
9436   if (op->unsp)
9437     {
9438       if (type == '<' || type == '>' || type == '[' || type == ']')
9439         {
9440           mintiny = 1;
9441           maxtiny = 1 << op->nbits;
9442         }
9443       else
9444         {
9445           mintiny = 0;
9446           maxtiny = (1 << op->nbits) - 1;
9447         }
9448     }
9449   else
9450     {
9451       mintiny = - (1 << (op->nbits - 1));
9452       maxtiny = (1 << (op->nbits - 1)) - 1;
9453     }
9454
9455   /* Branch offsets have an implicit 0 in the lowest bit.  */
9456   if (type == 'p' || type == 'q')
9457     val /= 2;
9458
9459   if ((val & ((1 << op->shift) - 1)) != 0
9460       || val < (mintiny << op->shift)
9461       || val > (maxtiny << op->shift))
9462     needext = true;
9463   else
9464     needext = false;
9465
9466   if (warn && ext && ! needext)
9467     as_warn_where (file, line,
9468                    _("extended operand requested but not required"));
9469   if (small && needext)
9470     as_bad_where (file, line, _("invalid unextended operand value"));
9471
9472   if (small || (! ext && ! needext))
9473     {
9474       int insnval;
9475
9476       *use_extend = false;
9477       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9478       insnval <<= op->op_shift;
9479       *insn |= insnval;
9480     }
9481   else
9482     {
9483       long minext, maxext;
9484       int extval;
9485
9486       if (op->extu)
9487         {
9488           minext = 0;
9489           maxext = (1 << op->extbits) - 1;
9490         }
9491       else
9492         {
9493           minext = - (1 << (op->extbits - 1));
9494           maxext = (1 << (op->extbits - 1)) - 1;
9495         }
9496       if (val < minext || val > maxext)
9497         as_bad_where (file, line,
9498                       _("operand value out of range for instruction"));
9499
9500       *use_extend = true;
9501       if (op->extbits == 16)
9502         {
9503           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9504           val &= 0x1f;
9505         }
9506       else if (op->extbits == 15)
9507         {
9508           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9509           val &= 0xf;
9510         }
9511       else
9512         {
9513           extval = ((val & 0x1f) << 6) | (val & 0x20);
9514           val = 0;
9515         }
9516
9517       *extend = (unsigned short) extval;
9518       *insn |= val;
9519     }
9520 }
9521 \f
9522 static struct percent_op_match
9523 {
9524    const char *str;
9525    const enum small_ex_type type;
9526 } percent_op[] =
9527 {
9528   {"%lo", S_EX_LO},
9529 #ifdef OBJ_ELF
9530   {"%call_hi", S_EX_CALL_HI},
9531   {"%call_lo", S_EX_CALL_LO},
9532   {"%call16", S_EX_CALL16},
9533   {"%got_disp", S_EX_GOT_DISP},
9534   {"%got_page", S_EX_GOT_PAGE},
9535   {"%got_ofst", S_EX_GOT_OFST},
9536   {"%got_hi", S_EX_GOT_HI},
9537   {"%got_lo", S_EX_GOT_LO},
9538   {"%got", S_EX_GOT},
9539   {"%gp_rel", S_EX_GP_REL},
9540   {"%half", S_EX_HALF},
9541   {"%highest", S_EX_HIGHEST},
9542   {"%higher", S_EX_HIGHER},
9543   {"%neg", S_EX_NEG},
9544 #endif
9545   {"%hi", S_EX_HI}
9546 };
9547
9548 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9549    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9550    can be nested, this is handled by blanking the innermost, parsing the
9551    rest by subsequent calls.  */
9552
9553 static int
9554 my_getSmallParser (str, len, nestlevel)
9555      char **str;
9556      unsigned int *len;
9557      int *nestlevel;
9558 {
9559   *len = 0;
9560   *str += strspn (*str, " \t");
9561   /* Check for expression in parentheses.  */
9562   if (**str == '(')
9563     {
9564       char *b = *str + 1 + strspn (*str + 1, " \t");
9565       char *e;
9566
9567       /* Check for base register.  */
9568       if (b[0] == '$')
9569         {
9570           if (strchr (b, ')')
9571               && (e = b + strcspn (b, ") \t"))
9572               && e - b > 1 && e - b < 4)
9573             {
9574               if ((e - b == 3
9575                    && ((b[1] == 'f' && b[2] == 'p')
9576                        || (b[1] == 's' && b[2] == 'p')
9577                        || (b[1] == 'g' && b[2] == 'p')
9578                        || (b[1] == 'a' && b[2] == 't')
9579                        || (ISDIGIT (b[1])
9580                            && ISDIGIT (b[2]))))
9581                   || (ISDIGIT (b[1])))
9582                 {
9583                   *len = strcspn (*str, ")") + 1;
9584                   return S_EX_REGISTER;
9585                 }
9586             }
9587         }
9588       /* Check for percent_op (in parentheses).  */
9589       else if (b[0] == '%')
9590         {
9591           *str = b;
9592           return my_getPercentOp (str, len, nestlevel);
9593         }
9594
9595       /* Some other expression in the parentheses, which can contain
9596          parentheses itself. Attempt to find the matching one.  */
9597       {
9598         int pcnt = 1;
9599         char *s;
9600
9601         *len = 1;
9602         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9603           {
9604             if (*s == '(')
9605               ++pcnt;
9606             else if (*s == ')')
9607               --pcnt;
9608           }
9609       }
9610     }
9611   /* Check for percent_op (outside of parentheses).  */
9612   else if (*str[0] == '%')
9613     return my_getPercentOp (str, len, nestlevel);
9614
9615   /* Any other expression.  */
9616   return S_EX_NONE;
9617 }
9618
9619 static int
9620 my_getPercentOp (str, len, nestlevel)
9621      char **str;
9622      unsigned int *len;
9623      int *nestlevel;
9624 {
9625   char *tmp = *str + 1;
9626   unsigned int i = 0;
9627
9628   while (ISALPHA (*tmp) || *tmp == '_')
9629     {
9630       *tmp = TOLOWER (*tmp);
9631       tmp++;
9632     }
9633   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9634     {
9635       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9636         i++;
9637       else
9638         {
9639           int type = percent_op[i].type;
9640
9641           /* Only %hi and %lo are allowed for OldABI.  */
9642           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9643             return S_EX_NONE;
9644
9645           *len = strlen (percent_op[i].str);
9646           ++(*nestlevel);
9647           return type;
9648         }
9649     }
9650   return S_EX_NONE;
9651 }
9652
9653 static int
9654 my_getSmallExpression (ep, str)
9655      expressionS *ep;
9656      char *str;
9657 {
9658   static char *oldstr = NULL;
9659   int c = S_EX_NONE;
9660   int oldc;
9661   int nestlevel = -1;
9662   unsigned int len;
9663
9664   /* Don't update oldstr if the last call had nested percent_op's. We need
9665      it to parse the outer ones later.  */
9666   if (! oldstr)
9667     oldstr = str;
9668
9669   do
9670     {
9671       oldc = c;
9672       c = my_getSmallParser (&str, &len, &nestlevel);
9673       if (c != S_EX_NONE && c != S_EX_REGISTER)
9674         str += len;
9675     }
9676   while (c != S_EX_NONE && c != S_EX_REGISTER);
9677
9678   if (nestlevel >= 0)
9679     {
9680       /* A percent_op was encountered.  Don't try to get an expression if
9681          it is already blanked out.  */
9682       if (*(str + strspn (str + 1, " )")) != ')')
9683         {
9684           char save;
9685
9686           /* Let my_getExpression() stop at the closing parenthesis.  */
9687           save = *(str + len);
9688           *(str + len) = '\0';
9689           my_getExpression (ep, str);
9690           *(str + len) = save;
9691         }
9692       if (nestlevel > 0)
9693         {
9694           /* Blank out including the % sign and the proper matching
9695              parenthesis.  */
9696           int pcnt = 1;
9697           char *s = strrchr (oldstr, '%');
9698           char *end;
9699
9700           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9701             {
9702               if (*end == '(')
9703                 ++pcnt;
9704               else if (*end == ')')
9705                 --pcnt;
9706             }
9707
9708           memset (s, ' ', end - s);
9709           str = oldstr;
9710         }
9711       else
9712         expr_end = str + len;
9713
9714       c = oldc;
9715     }
9716   else if (c == S_EX_NONE)
9717     {
9718       my_getExpression (ep, str);
9719     }
9720   else if (c == S_EX_REGISTER)
9721     {
9722       ep->X_op = O_constant;
9723       expr_end = str;
9724       ep->X_add_symbol = NULL;
9725       ep->X_op_symbol = NULL;
9726       ep->X_add_number = 0;
9727     }
9728   else
9729     {
9730       as_fatal (_("internal error"));
9731     }
9732
9733   if (nestlevel <= 0)
9734     /* All percent_op's have been handled.  */
9735     oldstr = NULL;
9736
9737   return c;
9738 }
9739
9740 static void
9741 my_getExpression (ep, str)
9742      expressionS *ep;
9743      char *str;
9744 {
9745   char *save_in;
9746   valueT val;
9747
9748   save_in = input_line_pointer;
9749   input_line_pointer = str;
9750   expression (ep);
9751   expr_end = input_line_pointer;
9752   input_line_pointer = save_in;
9753
9754   /* If we are in mips16 mode, and this is an expression based on `.',
9755      then we bump the value of the symbol by 1 since that is how other
9756      text symbols are handled.  We don't bother to handle complex
9757      expressions, just `.' plus or minus a constant.  */
9758   if (mips_opts.mips16
9759       && ep->X_op == O_symbol
9760       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9761       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9762       && symbol_get_frag (ep->X_add_symbol) == frag_now
9763       && symbol_constant_p (ep->X_add_symbol)
9764       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9765     S_SET_VALUE (ep->X_add_symbol, val + 1);
9766 }
9767
9768 /* Turn a string in input_line_pointer into a floating point constant
9769    of type TYPE, and store the appropriate bytes in *LITP.  The number
9770    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9771    returned, or NULL on OK.  */
9772
9773 char *
9774 md_atof (type, litP, sizeP)
9775      int type;
9776      char *litP;
9777      int *sizeP;
9778 {
9779   int prec;
9780   LITTLENUM_TYPE words[4];
9781   char *t;
9782   int i;
9783
9784   switch (type)
9785     {
9786     case 'f':
9787       prec = 2;
9788       break;
9789
9790     case 'd':
9791       prec = 4;
9792       break;
9793
9794     default:
9795       *sizeP = 0;
9796       return _("bad call to md_atof");
9797     }
9798
9799   t = atof_ieee (input_line_pointer, type, words);
9800   if (t)
9801     input_line_pointer = t;
9802
9803   *sizeP = prec * 2;
9804
9805   if (! target_big_endian)
9806     {
9807       for (i = prec - 1; i >= 0; i--)
9808         {
9809           md_number_to_chars (litP, (valueT) words[i], 2);
9810           litP += 2;
9811         }
9812     }
9813   else
9814     {
9815       for (i = 0; i < prec; i++)
9816         {
9817           md_number_to_chars (litP, (valueT) words[i], 2);
9818           litP += 2;
9819         }
9820     }
9821
9822   return NULL;
9823 }
9824
9825 void
9826 md_number_to_chars (buf, val, n)
9827      char *buf;
9828      valueT val;
9829      int n;
9830 {
9831   if (target_big_endian)
9832     number_to_chars_bigendian (buf, val, n);
9833   else
9834     number_to_chars_littleendian (buf, val, n);
9835 }
9836 \f
9837 #ifdef OBJ_ELF
9838 static int support_64bit_objects(void)
9839 {
9840   const char **list, **l;
9841
9842   list = bfd_target_list ();
9843   for (l = list; *l != NULL; l++)
9844 #ifdef TE_TMIPS
9845     /* This is traditional mips */
9846     if (strcmp (*l, "elf64-tradbigmips") == 0
9847         || strcmp (*l, "elf64-tradlittlemips") == 0)
9848 #else
9849     if (strcmp (*l, "elf64-bigmips") == 0
9850         || strcmp (*l, "elf64-littlemips") == 0)
9851 #endif
9852       break;
9853   free (list);
9854   return (*l != NULL);
9855 }
9856 #endif /* OBJ_ELF */
9857
9858 const char *md_shortopts = "nO::g::G:";
9859
9860 struct option md_longopts[] =
9861 {
9862 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9863   {"mips0", no_argument, NULL, OPTION_MIPS1},
9864   {"mips1", no_argument, NULL, OPTION_MIPS1},
9865 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9866   {"mips2", no_argument, NULL, OPTION_MIPS2},
9867 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9868   {"mips3", no_argument, NULL, OPTION_MIPS3},
9869 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9870   {"mips4", no_argument, NULL, OPTION_MIPS4},
9871 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9872   {"mips5", no_argument, NULL, OPTION_MIPS5},
9873 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9874   {"mips32", no_argument, NULL, OPTION_MIPS32},
9875 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9876   {"mips64", no_argument, NULL, OPTION_MIPS64},
9877 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9878   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9879 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9880   {"trap", no_argument, NULL, OPTION_TRAP},
9881   {"no-break", no_argument, NULL, OPTION_TRAP},
9882 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9883   {"break", no_argument, NULL, OPTION_BREAK},
9884   {"no-trap", no_argument, NULL, OPTION_BREAK},
9885 #define OPTION_EB (OPTION_MD_BASE + 11)
9886   {"EB", no_argument, NULL, OPTION_EB},
9887 #define OPTION_EL (OPTION_MD_BASE + 12)
9888   {"EL", no_argument, NULL, OPTION_EL},
9889 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9890   {"mips16", no_argument, NULL, OPTION_MIPS16},
9891 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9892   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9893 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9894   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9895 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9896   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9897   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9898 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9899   {"mfp32", no_argument, NULL, OPTION_FP32},
9900 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9901   {"mgp32", no_argument, NULL, OPTION_GP32},
9902 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9903   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9904 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9905   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9906 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9907   {"march", required_argument, NULL, OPTION_MARCH},
9908 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9909   {"mtune", required_argument, NULL, OPTION_MTUNE},
9910 #define OPTION_FP64 (OPTION_MD_BASE + 23)
9911   {"mfp64", no_argument, NULL, OPTION_FP64},
9912 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9913   {"m4650", no_argument, NULL, OPTION_M4650},
9914 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9915   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9916 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9917   {"m4010", no_argument, NULL, OPTION_M4010},
9918 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9919   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9920 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9921   {"m4100", no_argument, NULL, OPTION_M4100},
9922 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9923   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9924 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9925   {"m3900", no_argument, NULL, OPTION_M3900},
9926 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9927   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9928 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9929   {"mgp64", no_argument, NULL, OPTION_GP64},
9930 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9931   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9932 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9933   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9934 #define OPTION_MDMX (OPTION_MD_BASE + 35)
9935   {"mdmx", no_argument, NULL, OPTION_MDMX},
9936 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9937   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9938 #ifdef OBJ_ELF
9939 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 37)
9940 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9941   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9942   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9943 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
9944   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
9945 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
9946   {"xgot",        no_argument, NULL, OPTION_XGOT},
9947 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
9948   {"mabi", required_argument, NULL, OPTION_MABI},
9949 #define OPTION_32          (OPTION_ELF_BASE + 4)
9950   {"32",          no_argument, NULL, OPTION_32},
9951 #define OPTION_N32         (OPTION_ELF_BASE + 5)
9952   {"n32",         no_argument, NULL, OPTION_N32},
9953 #define OPTION_64          (OPTION_ELF_BASE + 6)
9954   {"64",          no_argument, NULL, OPTION_64},
9955 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
9956   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
9957 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
9958   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
9959 #endif /* OBJ_ELF */
9960   {NULL, no_argument, NULL, 0}
9961 };
9962 size_t md_longopts_size = sizeof (md_longopts);
9963
9964 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
9965    NEW_VALUE.  Warn if another value was already specified.  Note:
9966    we have to defer parsing the -march and -mtune arguments in order
9967    to handle 'from-abi' correctly, since the ABI might be specified
9968    in a later argument.  */
9969
9970 static void
9971 mips_set_option_string (string_ptr, new_value)
9972      const char **string_ptr, *new_value;
9973 {
9974   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
9975     as_warn (_("A different %s was already specified, is now %s"),
9976              string_ptr == &mips_arch_string ? "-march" : "-mtune",
9977              new_value);
9978
9979   *string_ptr = new_value;
9980 }
9981
9982 int
9983 md_parse_option (c, arg)
9984      int c;
9985      char *arg;
9986 {
9987   switch (c)
9988     {
9989     case OPTION_CONSTRUCT_FLOATS:
9990       mips_disable_float_construction = 0;
9991       break;
9992
9993     case OPTION_NO_CONSTRUCT_FLOATS:
9994       mips_disable_float_construction = 1;
9995       break;
9996
9997     case OPTION_TRAP:
9998       mips_trap = 1;
9999       break;
10000
10001     case OPTION_BREAK:
10002       mips_trap = 0;
10003       break;
10004
10005     case OPTION_EB:
10006       target_big_endian = 1;
10007       break;
10008
10009     case OPTION_EL:
10010       target_big_endian = 0;
10011       break;
10012
10013     case 'n':
10014       warn_nops = 1;
10015       break;
10016
10017     case 'O':
10018       if (arg && arg[1] == '0')
10019         mips_optimize = 1;
10020       else
10021         mips_optimize = 2;
10022       break;
10023
10024     case 'g':
10025       if (arg == NULL)
10026         mips_debug = 2;
10027       else
10028         mips_debug = atoi (arg);
10029       /* When the MIPS assembler sees -g or -g2, it does not do
10030          optimizations which limit full symbolic debugging.  We take
10031          that to be equivalent to -O0.  */
10032       if (mips_debug == 2)
10033         mips_optimize = 1;
10034       break;
10035
10036     case OPTION_MIPS1:
10037       file_mips_isa = ISA_MIPS1;
10038       break;
10039
10040     case OPTION_MIPS2:
10041       file_mips_isa = ISA_MIPS2;
10042       break;
10043
10044     case OPTION_MIPS3:
10045       file_mips_isa = ISA_MIPS3;
10046       break;
10047
10048     case OPTION_MIPS4:
10049       file_mips_isa = ISA_MIPS4;
10050       break;
10051
10052     case OPTION_MIPS5:
10053       file_mips_isa = ISA_MIPS5;
10054       break;
10055
10056     case OPTION_MIPS32:
10057       file_mips_isa = ISA_MIPS32;
10058       break;
10059
10060     case OPTION_MIPS64:
10061       file_mips_isa = ISA_MIPS64;
10062       break;
10063
10064     case OPTION_MTUNE:
10065       mips_set_option_string (&mips_tune_string, arg);
10066       break;
10067
10068     case OPTION_MARCH:
10069       mips_set_option_string (&mips_arch_string, arg);
10070       break;
10071
10072     case OPTION_M4650:
10073       mips_set_option_string (&mips_arch_string, "4650");
10074       mips_set_option_string (&mips_tune_string, "4650");
10075       break;
10076
10077     case OPTION_NO_M4650:
10078       break;
10079
10080     case OPTION_M4010:
10081       mips_set_option_string (&mips_arch_string, "4010");
10082       mips_set_option_string (&mips_tune_string, "4010");
10083       break;
10084
10085     case OPTION_NO_M4010:
10086       break;
10087
10088     case OPTION_M4100:
10089       mips_set_option_string (&mips_arch_string, "4100");
10090       mips_set_option_string (&mips_tune_string, "4100");
10091       break;
10092
10093     case OPTION_NO_M4100:
10094       break;
10095
10096     case OPTION_M3900:
10097       mips_set_option_string (&mips_arch_string, "3900");
10098       mips_set_option_string (&mips_tune_string, "3900");
10099       break;
10100
10101     case OPTION_NO_M3900:
10102       break;
10103
10104     case OPTION_MDMX:
10105       mips_opts.ase_mdmx = 1;
10106       break;
10107
10108     case OPTION_NO_MDMX:
10109       mips_opts.ase_mdmx = 0;
10110       break;
10111
10112     case OPTION_MIPS16:
10113       mips_opts.mips16 = 1;
10114       mips_no_prev_insn (false);
10115       break;
10116
10117     case OPTION_NO_MIPS16:
10118       mips_opts.mips16 = 0;
10119       mips_no_prev_insn (false);
10120       break;
10121
10122     case OPTION_MIPS3D:
10123       mips_opts.ase_mips3d = 1;
10124       break;
10125
10126     case OPTION_NO_MIPS3D:
10127       mips_opts.ase_mips3d = 0;
10128       break;
10129
10130     case OPTION_MEMBEDDED_PIC:
10131       mips_pic = EMBEDDED_PIC;
10132       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10133         {
10134           as_bad (_("-G may not be used with embedded PIC code"));
10135           return 0;
10136         }
10137       g_switch_value = 0x7fffffff;
10138       break;
10139
10140 #ifdef OBJ_ELF
10141       /* When generating ELF code, we permit -KPIC and -call_shared to
10142          select SVR4_PIC, and -non_shared to select no PIC.  This is
10143          intended to be compatible with Irix 5.  */
10144     case OPTION_CALL_SHARED:
10145       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10146         {
10147           as_bad (_("-call_shared is supported only for ELF format"));
10148           return 0;
10149         }
10150       mips_pic = SVR4_PIC;
10151       if (g_switch_seen && g_switch_value != 0)
10152         {
10153           as_bad (_("-G may not be used with SVR4 PIC code"));
10154           return 0;
10155         }
10156       g_switch_value = 0;
10157       break;
10158
10159     case OPTION_NON_SHARED:
10160       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10161         {
10162           as_bad (_("-non_shared is supported only for ELF format"));
10163           return 0;
10164         }
10165       mips_pic = NO_PIC;
10166       break;
10167
10168       /* The -xgot option tells the assembler to use 32 offsets when
10169          accessing the got in SVR4_PIC mode.  It is for Irix
10170          compatibility.  */
10171     case OPTION_XGOT:
10172       mips_big_got = 1;
10173       break;
10174 #endif /* OBJ_ELF */
10175
10176     case 'G':
10177       if (! USE_GLOBAL_POINTER_OPT)
10178         {
10179           as_bad (_("-G is not supported for this configuration"));
10180           return 0;
10181         }
10182       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10183         {
10184           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10185           return 0;
10186         }
10187       else
10188         g_switch_value = atoi (arg);
10189       g_switch_seen = 1;
10190       break;
10191
10192 #ifdef OBJ_ELF
10193       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10194          and -mabi=64.  */
10195     case OPTION_32:
10196       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10197         {
10198           as_bad (_("-32 is supported for ELF format only"));
10199           return 0;
10200         }
10201       mips_abi = O32_ABI;
10202       break;
10203
10204     case OPTION_N32:
10205       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10206         {
10207           as_bad (_("-n32 is supported for ELF format only"));
10208           return 0;
10209         }
10210       mips_abi = N32_ABI;
10211       break;
10212
10213     case OPTION_64:
10214       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10215         {
10216           as_bad (_("-64 is supported for ELF format only"));
10217           return 0;
10218         }
10219       mips_abi = N64_ABI;
10220       if (! support_64bit_objects())
10221         as_fatal (_("No compiled in support for 64 bit object file format"));
10222       break;
10223 #endif /* OBJ_ELF */
10224
10225     case OPTION_GP32:
10226       file_mips_gp32 = 1;
10227       break;
10228
10229     case OPTION_GP64:
10230       file_mips_gp32 = 0;
10231       break;
10232
10233     case OPTION_FP32:
10234       file_mips_fp32 = 1;
10235       break;
10236
10237     case OPTION_FP64:
10238       file_mips_fp32 = 0;
10239       break;
10240
10241 #ifdef OBJ_ELF
10242     case OPTION_MABI:
10243       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10244         {
10245           as_bad (_("-mabi is supported for ELF format only"));
10246           return 0;
10247         }
10248       if (strcmp (arg, "32") == 0)
10249         mips_abi = O32_ABI;
10250       else if (strcmp (arg, "o64") == 0)
10251         mips_abi = O64_ABI;
10252       else if (strcmp (arg, "n32") == 0)
10253         mips_abi = N32_ABI;
10254       else if (strcmp (arg, "64") == 0)
10255         {
10256           mips_abi = N64_ABI;
10257           if (! support_64bit_objects())
10258             as_fatal (_("No compiled in support for 64 bit object file "
10259                         "format"));
10260         }
10261       else if (strcmp (arg, "eabi") == 0)
10262         mips_abi = EABI_ABI;
10263       else
10264         {
10265           as_fatal (_("invalid abi -mabi=%s"), arg);
10266           return 0;
10267         }
10268       break;
10269 #endif /* OBJ_ELF */
10270
10271     case OPTION_M7000_HILO_FIX:
10272       mips_7000_hilo_fix = true;
10273       break;
10274
10275     case OPTION_MNO_7000_HILO_FIX:
10276       mips_7000_hilo_fix = false;
10277       break;
10278
10279 #ifdef OBJ_ELF
10280     case OPTION_MDEBUG:
10281       mips_flag_mdebug = true;
10282       break;
10283
10284     case OPTION_NO_MDEBUG:
10285       mips_flag_mdebug = false;
10286       break;
10287 #endif /* OBJ_ELF */
10288
10289     default:
10290       return 0;
10291     }
10292
10293   return 1;
10294 }
10295 \f
10296 /* Set up globals to generate code for the ISA or processor
10297    described by INFO.  */
10298
10299 static void
10300 mips_set_architecture (info)
10301      const struct mips_cpu_info *info;
10302 {
10303   if (info != 0)
10304     {
10305       mips_arch_info = info;
10306       mips_arch = info->cpu;
10307       mips_opts.isa = info->isa;
10308     }
10309 }
10310
10311
10312 /* Likewise for tuning.  */
10313
10314 static void
10315 mips_set_tune (info)
10316      const struct mips_cpu_info *info;
10317 {
10318   if (info != 0)
10319     {
10320       mips_tune_info = info;
10321       mips_tune = info->cpu;
10322     }
10323 }
10324
10325
10326 void
10327 mips_after_parse_args ()
10328 {
10329   /* GP relative stuff not working for PE */
10330   if (strncmp (TARGET_OS, "pe", 2) == 0
10331       && g_switch_value != 0)
10332     {
10333       if (g_switch_seen)
10334         as_bad (_("-G not supported in this configuration."));
10335       g_switch_value = 0;
10336     }
10337
10338   /* The following code determines the architecture and register size.
10339      Similar code was added to GCC 3.3 (see override_options() in
10340      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10341      as much as possible.  */
10342
10343   if (mips_arch_string != 0)
10344     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10345
10346   if (mips_tune_string != 0)
10347     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10348
10349   if (file_mips_isa != ISA_UNKNOWN)
10350     {
10351       /* Handle -mipsN.  At this point, file_mips_isa contains the
10352          ISA level specified by -mipsN, while mips_opts.isa contains
10353          the -march selection (if any).  */
10354       if (mips_arch_info != 0)
10355         {
10356           /* -march takes precedence over -mipsN, since it is more descriptive.
10357              There's no harm in specifying both as long as the ISA levels
10358              are the same.  */
10359           if (file_mips_isa != mips_opts.isa)
10360             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10361                     mips_cpu_info_from_isa (file_mips_isa)->name,
10362                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10363         }
10364       else
10365         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10366     }
10367
10368   if (mips_arch_info == 0)
10369     mips_set_architecture (mips_parse_cpu ("default CPU",
10370                                            MIPS_CPU_STRING_DEFAULT));
10371
10372   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10373     as_bad ("-march=%s is not compatible with the selected ABI",
10374             mips_arch_info->name);
10375
10376   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10377   if (mips_tune_info == 0)
10378     mips_set_tune (mips_arch_info);
10379
10380   if (file_mips_gp32 >= 0)
10381     {
10382       /* The user specified the size of the integer registers.  Make sure
10383          it agrees with the ABI and ISA.  */
10384       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10385         as_bad (_("-mgp64 used with a 32-bit processor"));
10386       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10387         as_bad (_("-mgp32 used with a 64-bit ABI"));
10388       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10389         as_bad (_("-mgp64 used with a 32-bit ABI"));
10390     }
10391   else
10392     {
10393       /* Infer the integer register size from the ABI and processor.
10394          Restrict ourselves to 32-bit registers if that's all the
10395          processor has, or if the ABI cannot handle 64-bit registers.  */
10396       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10397                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10398     }
10399
10400   /* ??? GAS treats single-float processors as though they had 64-bit
10401      float registers (although it complains when double-precision
10402      instructions are used).  As things stand, saying they have 32-bit
10403      registers would lead to spurious "register must be even" messages.
10404      So here we assume float registers are always the same size as
10405      integer ones, unless the user says otherwise.  */
10406   if (file_mips_fp32 < 0)
10407     file_mips_fp32 = file_mips_gp32;
10408
10409   /* End of GCC-shared inference code.  */
10410
10411   /* ??? When do we want this flag to be set?   Who uses it?  */
10412   if (file_mips_gp32 == 1
10413       && mips_abi == NO_ABI
10414       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10415     mips_32bitmode = 1;
10416
10417   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10418     as_bad (_("trap exception not supported at ISA 1"));
10419
10420   /* If the selected architecture includes support for ASEs, enable
10421      generation of code for them.  */
10422   if (mips_opts.mips16 == -1)
10423     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10424   if (mips_opts.ase_mips3d == -1)
10425     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10426   if (mips_opts.ase_mdmx == -1)
10427     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10428
10429   file_mips_isa = mips_opts.isa;
10430   file_ase_mips16 = mips_opts.mips16;
10431   file_ase_mips3d = mips_opts.ase_mips3d;
10432   file_ase_mdmx = mips_opts.ase_mdmx;
10433   mips_opts.gp32 = file_mips_gp32;
10434   mips_opts.fp32 = file_mips_fp32;
10435
10436   if (HAVE_NEWABI)
10437     mips_big_got = 1;
10438
10439   if (mips_flag_mdebug < 0)
10440     {
10441 #ifdef OBJ_MAYBE_ECOFF
10442       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10443         mips_flag_mdebug = 1;
10444       else
10445 #endif /* OBJ_MAYBE_ECOFF */
10446         mips_flag_mdebug = 0;
10447     }
10448 }
10449 \f
10450 void
10451 mips_init_after_args ()
10452 {
10453   /* initialize opcodes */
10454   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10455   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10456 }
10457
10458 long
10459 md_pcrel_from (fixP)
10460      fixS *fixP;
10461 {
10462   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10463       && fixP->fx_addsy != (symbolS *) NULL
10464       && ! S_IS_DEFINED (fixP->fx_addsy))
10465     {
10466       /* This makes a branch to an undefined symbol be a branch to the
10467          current location.  */
10468       if (mips_pic == EMBEDDED_PIC)
10469         return 4;
10470       else
10471         return 1;
10472     }
10473
10474   /* Return the address of the delay slot.  */
10475   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10476 }
10477
10478 /* This is called before the symbol table is processed.  In order to
10479    work with gcc when using mips-tfile, we must keep all local labels.
10480    However, in other cases, we want to discard them.  If we were
10481    called with -g, but we didn't see any debugging information, it may
10482    mean that gcc is smuggling debugging information through to
10483    mips-tfile, in which case we must generate all local labels.  */
10484
10485 void
10486 mips_frob_file_before_adjust ()
10487 {
10488 #ifndef NO_ECOFF_DEBUGGING
10489   if (ECOFF_DEBUGGING
10490       && mips_debug != 0
10491       && ! ecoff_debugging_seen)
10492     flag_keep_locals = 1;
10493 #endif
10494 }
10495
10496 /* Sort any unmatched HI16_S relocs so that they immediately precede
10497    the corresponding LO reloc.  This is called before md_apply_fix3 and
10498    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10499    explicit use of the %hi modifier.  */
10500
10501 void
10502 mips_frob_file ()
10503 {
10504   struct mips_hi_fixup *l;
10505
10506   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10507     {
10508       segment_info_type *seginfo;
10509       int pass;
10510
10511       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10512
10513       /* Check quickly whether the next fixup happens to be a matching
10514          %lo.  */
10515       if (l->fixp->fx_next != NULL
10516           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10517           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10518           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10519         continue;
10520
10521       /* Look through the fixups for this segment for a matching %lo.
10522          When we find one, move the %hi just in front of it.  We do
10523          this in two passes.  In the first pass, we try to find a
10524          unique %lo.  In the second pass, we permit multiple %hi
10525          relocs for a single %lo (this is a GNU extension).  */
10526       seginfo = seg_info (l->seg);
10527       for (pass = 0; pass < 2; pass++)
10528         {
10529           fixS *f, *prev;
10530
10531           prev = NULL;
10532           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10533             {
10534               /* Check whether this is a %lo fixup which matches l->fixp.  */
10535               if (f->fx_r_type == BFD_RELOC_LO16
10536                   && f->fx_addsy == l->fixp->fx_addsy
10537                   && f->fx_offset == l->fixp->fx_offset
10538                   && (pass == 1
10539                       || prev == NULL
10540                       || prev->fx_r_type != BFD_RELOC_HI16_S
10541                       || prev->fx_addsy != f->fx_addsy
10542                       || prev->fx_offset !=  f->fx_offset))
10543                 {
10544                   fixS **pf;
10545
10546                   /* Move l->fixp before f.  */
10547                   for (pf = &seginfo->fix_root;
10548                        *pf != l->fixp;
10549                        pf = &(*pf)->fx_next)
10550                     assert (*pf != NULL);
10551
10552                   *pf = l->fixp->fx_next;
10553
10554                   l->fixp->fx_next = f;
10555                   if (prev == NULL)
10556                     seginfo->fix_root = l->fixp;
10557                   else
10558                     prev->fx_next = l->fixp;
10559
10560                   break;
10561                 }
10562
10563               prev = f;
10564             }
10565
10566           if (f != NULL)
10567             break;
10568
10569 #if 0 /* GCC code motion plus incomplete dead code elimination
10570          can leave a %hi without a %lo.  */
10571           if (pass == 1)
10572             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10573                            _("Unmatched %%hi reloc"));
10574 #endif
10575         }
10576     }
10577 }
10578
10579 /* When generating embedded PIC code we need to use a special
10580    relocation to represent the difference of two symbols in the .text
10581    section (switch tables use a difference of this sort).  See
10582    include/coff/mips.h for details.  This macro checks whether this
10583    fixup requires the special reloc.  */
10584 #define SWITCH_TABLE(fixp) \
10585   ((fixp)->fx_r_type == BFD_RELOC_32 \
10586    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10587    && (fixp)->fx_addsy != NULL \
10588    && (fixp)->fx_subsy != NULL \
10589    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10590    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10591
10592 /* When generating embedded PIC code we must keep all PC relative
10593    relocations, in case the linker has to relax a call.  We also need
10594    to keep relocations for switch table entries.
10595
10596    We may have combined relocations without symbols in the N32/N64 ABI.
10597    We have to prevent gas from dropping them.  */
10598
10599 int
10600 mips_force_relocation (fixp)
10601      fixS *fixp;
10602 {
10603   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10604       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10605     return 1;
10606
10607   if (HAVE_NEWABI
10608       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10609       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10610           || fixp->fx_r_type == BFD_RELOC_HI16_S
10611           || fixp->fx_r_type == BFD_RELOC_LO16))
10612     return 1;
10613
10614   return (mips_pic == EMBEDDED_PIC
10615           && (fixp->fx_pcrel
10616               || SWITCH_TABLE (fixp)
10617               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10618               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10619 }
10620
10621 #ifdef OBJ_ELF
10622 static int
10623 mips_need_elf_addend_fixup (fixP)
10624      fixS *fixP;
10625 {
10626   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10627     return 1;
10628   if (mips_pic == EMBEDDED_PIC
10629       && S_IS_WEAK (fixP->fx_addsy))
10630     return 1;
10631   if (mips_pic != EMBEDDED_PIC
10632       && (S_IS_WEAK (fixP->fx_addsy)
10633           || S_IS_EXTERNAL (fixP->fx_addsy))
10634       && !S_IS_COMMON (fixP->fx_addsy))
10635     return 1;
10636   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10637       && (((bfd_get_section_flags (stdoutput,
10638                                    S_GET_SEGMENT (fixP->fx_addsy))
10639             & SEC_LINK_ONCE) != 0)
10640           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10641                        ".gnu.linkonce",
10642                        sizeof (".gnu.linkonce") - 1)))
10643     return 1;
10644   return 0;
10645 }
10646 #endif
10647
10648 /* Apply a fixup to the object file.  */
10649
10650 void
10651 md_apply_fix3 (fixP, valP, seg)
10652      fixS *fixP;
10653      valueT *valP;
10654      segT seg ATTRIBUTE_UNUSED;
10655 {
10656   bfd_byte *buf;
10657   long insn;
10658   valueT value;
10659
10660   assert (fixP->fx_size == 4
10661           || fixP->fx_r_type == BFD_RELOC_16
10662           || fixP->fx_r_type == BFD_RELOC_32
10663           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10664           || fixP->fx_r_type == BFD_RELOC_HI16_S
10665           || fixP->fx_r_type == BFD_RELOC_LO16
10666           || fixP->fx_r_type == BFD_RELOC_GPREL16
10667           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10668           || fixP->fx_r_type == BFD_RELOC_GPREL32
10669           || fixP->fx_r_type == BFD_RELOC_64
10670           || fixP->fx_r_type == BFD_RELOC_CTOR
10671           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10672           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10673           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10674           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10675           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10676           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10677           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10678           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10679           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10680
10681   value = *valP;
10682
10683   /* If we aren't adjusting this fixup to be against the section
10684      symbol, we need to adjust the value.  */
10685 #ifdef OBJ_ELF
10686   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10687     {
10688       if (mips_need_elf_addend_fixup (fixP))
10689         {
10690           reloc_howto_type *howto;
10691           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10692
10693           value -= symval;
10694
10695           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10696           if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
10697             {
10698               /* In this case, the bfd_install_relocation routine will
10699                  incorrectly add the symbol value back in.  We just want
10700                  the addend to appear in the object file.  */
10701               value -= symval;
10702
10703               /* Make sure the addend is still non-zero.  If it became zero
10704                  after the last operation, set it to a spurious value and
10705                  subtract the same value from the object file's contents.  */
10706               if (value == 0)
10707                 {
10708                   value = 8;
10709
10710                   /* The in-place addends for LO16 relocations are signed;
10711                      leave the matching HI16 in-place addends as zero.  */
10712                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10713                     {
10714                       bfd_vma contents, mask, field;
10715
10716                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10717                                                + fixP->fx_where,
10718                                                fixP->fx_size * 8,
10719                                                target_big_endian);
10720
10721                       /* MASK has bits set where the relocation should go.
10722                          FIELD is -value, shifted into the appropriate place
10723                          for this relocation.  */
10724                       mask = 1 << (howto->bitsize - 1);
10725                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10726                       field = (-value >> howto->rightshift) << howto->bitpos;
10727
10728                       bfd_put_bits ((field & mask) | (contents & ~mask),
10729                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10730                                     fixP->fx_size * 8,
10731                                     target_big_endian);
10732                     }
10733                 }
10734             }
10735         }
10736
10737       /* This code was generated using trial and error and so is
10738          fragile and not trustworthy.  If you change it, you should
10739          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10740          they still pass.  */
10741       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10742         {
10743           value += fixP->fx_frag->fr_address + fixP->fx_where;
10744
10745           /* BFD's REL handling, for MIPS, is _very_ weird.
10746              This gives the right results, but it can't possibly
10747              be the way things are supposed to work.  */
10748           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10749                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10750               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10751             value += fixP->fx_frag->fr_address + fixP->fx_where;
10752         }
10753     }
10754 #endif
10755
10756   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10757
10758   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10759     fixP->fx_done = 1;
10760
10761   switch (fixP->fx_r_type)
10762     {
10763     case BFD_RELOC_MIPS_JMP:
10764     case BFD_RELOC_MIPS_SHIFT5:
10765     case BFD_RELOC_MIPS_SHIFT6:
10766     case BFD_RELOC_MIPS_GOT_DISP:
10767     case BFD_RELOC_MIPS_GOT_PAGE:
10768     case BFD_RELOC_MIPS_GOT_OFST:
10769     case BFD_RELOC_MIPS_SUB:
10770     case BFD_RELOC_MIPS_INSERT_A:
10771     case BFD_RELOC_MIPS_INSERT_B:
10772     case BFD_RELOC_MIPS_DELETE:
10773     case BFD_RELOC_MIPS_HIGHEST:
10774     case BFD_RELOC_MIPS_HIGHER:
10775     case BFD_RELOC_MIPS_SCN_DISP:
10776     case BFD_RELOC_MIPS_REL16:
10777     case BFD_RELOC_MIPS_RELGOT:
10778     case BFD_RELOC_MIPS_JALR:
10779     case BFD_RELOC_HI16:
10780     case BFD_RELOC_HI16_S:
10781     case BFD_RELOC_GPREL16:
10782     case BFD_RELOC_MIPS_LITERAL:
10783     case BFD_RELOC_MIPS_CALL16:
10784     case BFD_RELOC_MIPS_GOT16:
10785     case BFD_RELOC_GPREL32:
10786     case BFD_RELOC_MIPS_GOT_HI16:
10787     case BFD_RELOC_MIPS_GOT_LO16:
10788     case BFD_RELOC_MIPS_CALL_HI16:
10789     case BFD_RELOC_MIPS_CALL_LO16:
10790     case BFD_RELOC_MIPS16_GPREL:
10791       if (fixP->fx_pcrel)
10792         as_bad_where (fixP->fx_file, fixP->fx_line,
10793                       _("Invalid PC relative reloc"));
10794       /* Nothing needed to do. The value comes from the reloc entry */
10795       break;
10796
10797     case BFD_RELOC_MIPS16_JMP:
10798       /* We currently always generate a reloc against a symbol, which
10799          means that we don't want an addend even if the symbol is
10800          defined.  */
10801       fixP->fx_addnumber = 0;
10802       break;
10803
10804     case BFD_RELOC_PCREL_HI16_S:
10805       /* The addend for this is tricky if it is internal, so we just
10806          do everything here rather than in bfd_install_relocation.  */
10807       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10808           && !fixP->fx_done
10809           && value != 0)
10810         break;
10811       if (fixP->fx_addsy
10812           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10813         {
10814           /* For an external symbol adjust by the address to make it
10815              pcrel_offset.  We use the address of the RELLO reloc
10816              which follows this one.  */
10817           value += (fixP->fx_next->fx_frag->fr_address
10818                     + fixP->fx_next->fx_where);
10819         }
10820       value = ((value + 0x8000) >> 16) & 0xffff;
10821       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10822       if (target_big_endian)
10823         buf += 2;
10824       md_number_to_chars ((char *) buf, value, 2);
10825       break;
10826
10827     case BFD_RELOC_PCREL_LO16:
10828       /* The addend for this is tricky if it is internal, so we just
10829          do everything here rather than in bfd_install_relocation.  */
10830       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10831           && !fixP->fx_done
10832           && value != 0)
10833         break;
10834       if (fixP->fx_addsy
10835           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10836         value += fixP->fx_frag->fr_address + fixP->fx_where;
10837       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10838       if (target_big_endian)
10839         buf += 2;
10840       md_number_to_chars ((char *) buf, value, 2);
10841       break;
10842
10843     case BFD_RELOC_64:
10844       /* This is handled like BFD_RELOC_32, but we output a sign
10845          extended value if we are only 32 bits.  */
10846       if (fixP->fx_done
10847           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10848         {
10849           if (8 <= sizeof (valueT))
10850             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10851                                 value, 8);
10852           else
10853             {
10854               long w1, w2;
10855               long hiv;
10856
10857               w1 = w2 = fixP->fx_where;
10858               if (target_big_endian)
10859                 w1 += 4;
10860               else
10861                 w2 += 4;
10862               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10863               if ((value & 0x80000000) != 0)
10864                 hiv = 0xffffffff;
10865               else
10866                 hiv = 0;
10867               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10868             }
10869         }
10870       break;
10871
10872     case BFD_RELOC_RVA:
10873     case BFD_RELOC_32:
10874       /* If we are deleting this reloc entry, we must fill in the
10875          value now.  This can happen if we have a .word which is not
10876          resolved when it appears but is later defined.  We also need
10877          to fill in the value if this is an embedded PIC switch table
10878          entry.  */
10879       if (fixP->fx_done
10880           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10881         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10882                             value, 4);
10883       break;
10884
10885     case BFD_RELOC_16:
10886       /* If we are deleting this reloc entry, we must fill in the
10887          value now.  */
10888       assert (fixP->fx_size == 2);
10889       if (fixP->fx_done)
10890         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10891                             value, 2);
10892       break;
10893
10894     case BFD_RELOC_LO16:
10895       /* When handling an embedded PIC switch statement, we can wind
10896          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
10897       if (fixP->fx_done)
10898         {
10899           if (value + 0x8000 > 0xffff)
10900             as_bad_where (fixP->fx_file, fixP->fx_line,
10901                           _("relocation overflow"));
10902           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10903           if (target_big_endian)
10904             buf += 2;
10905           md_number_to_chars ((char *) buf, value, 2);
10906         }
10907       break;
10908
10909     case BFD_RELOC_16_PCREL_S2:
10910       if ((value & 0x3) != 0)
10911         as_bad_where (fixP->fx_file, fixP->fx_line,
10912                       _("Branch to odd address (%lx)"), (long) value);
10913
10914       /* Fall through.  */
10915
10916     case BFD_RELOC_16_PCREL:
10917       /*
10918        * We need to save the bits in the instruction since fixup_segment()
10919        * might be deleting the relocation entry (i.e., a branch within
10920        * the current segment).
10921        */
10922       if (!fixP->fx_done && value != 0)
10923         break;
10924       /* If 'value' is zero, the remaining reloc code won't actually
10925          do the store, so it must be done here.  This is probably
10926          a bug somewhere.  */
10927       if (!fixP->fx_done
10928           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10929               || fixP->fx_addsy == NULL                 /* ??? */
10930               || ! S_IS_DEFINED (fixP->fx_addsy)))
10931         value -= fixP->fx_frag->fr_address + fixP->fx_where;
10932
10933       value = (offsetT) value >> 2;
10934
10935       /* update old instruction data */
10936       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10937       if (target_big_endian)
10938         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10939       else
10940         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10941
10942       if (value + 0x8000 <= 0xffff)
10943         insn |= value & 0xffff;
10944       else
10945         {
10946           /* The branch offset is too large.  If this is an
10947              unconditional branch, and we are not generating PIC code,
10948              we can convert it to an absolute jump instruction.  */
10949           if (mips_pic == NO_PIC
10950               && fixP->fx_done
10951               && fixP->fx_frag->fr_address >= text_section->vma
10952               && (fixP->fx_frag->fr_address
10953                   < text_section->vma + text_section->_raw_size)
10954               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
10955                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
10956                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10957             {
10958               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
10959                 insn = 0x0c000000;      /* jal */
10960               else
10961                 insn = 0x08000000;      /* j */
10962               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10963               fixP->fx_done = 0;
10964               fixP->fx_addsy = section_symbol (text_section);
10965               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10966             }
10967           else
10968             {
10969               /* FIXME.  It would be possible in principle to handle
10970                  conditional branches which overflow.  They could be
10971                  transformed into a branch around a jump.  This would
10972                  require setting up variant frags for each different
10973                  branch type.  The native MIPS assembler attempts to
10974                  handle these cases, but it appears to do it
10975                  incorrectly.  */
10976               as_bad_where (fixP->fx_file, fixP->fx_line,
10977                             _("Branch out of range"));
10978             }
10979         }
10980
10981       md_number_to_chars ((char *) buf, (valueT) insn, 4);
10982       break;
10983
10984     case BFD_RELOC_VTABLE_INHERIT:
10985       fixP->fx_done = 0;
10986       if (fixP->fx_addsy
10987           && !S_IS_DEFINED (fixP->fx_addsy)
10988           && !S_IS_WEAK (fixP->fx_addsy))
10989         S_SET_WEAK (fixP->fx_addsy);
10990       break;
10991
10992     case BFD_RELOC_VTABLE_ENTRY:
10993       fixP->fx_done = 0;
10994       break;
10995
10996     default:
10997       internalError ();
10998     }
10999 }
11000
11001 #if 0
11002 void
11003 printInsn (oc)
11004      unsigned long oc;
11005 {
11006   const struct mips_opcode *p;
11007   int treg, sreg, dreg, shamt;
11008   short imm;
11009   const char *args;
11010   int i;
11011
11012   for (i = 0; i < NUMOPCODES; ++i)
11013     {
11014       p = &mips_opcodes[i];
11015       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11016         {
11017           printf ("%08lx %s\t", oc, p->name);
11018           treg = (oc >> 16) & 0x1f;
11019           sreg = (oc >> 21) & 0x1f;
11020           dreg = (oc >> 11) & 0x1f;
11021           shamt = (oc >> 6) & 0x1f;
11022           imm = oc;
11023           for (args = p->args;; ++args)
11024             {
11025               switch (*args)
11026                 {
11027                 case '\0':
11028                   printf ("\n");
11029                   break;
11030
11031                 case ',':
11032                 case '(':
11033                 case ')':
11034                   printf ("%c", *args);
11035                   continue;
11036
11037                 case 'r':
11038                   assert (treg == sreg);
11039                   printf ("$%d,$%d", treg, sreg);
11040                   continue;
11041
11042                 case 'd':
11043                 case 'G':
11044                   printf ("$%d", dreg);
11045                   continue;
11046
11047                 case 't':
11048                 case 'E':
11049                   printf ("$%d", treg);
11050                   continue;
11051
11052                 case 'k':
11053                   printf ("0x%x", treg);
11054                   continue;
11055
11056                 case 'b':
11057                 case 's':
11058                   printf ("$%d", sreg);
11059                   continue;
11060
11061                 case 'a':
11062                   printf ("0x%08lx", oc & 0x1ffffff);
11063                   continue;
11064
11065                 case 'i':
11066                 case 'j':
11067                 case 'o':
11068                 case 'u':
11069                   printf ("%d", imm);
11070                   continue;
11071
11072                 case '<':
11073                 case '>':
11074                   printf ("$%d", shamt);
11075                   continue;
11076
11077                 default:
11078                   internalError ();
11079                 }
11080               break;
11081             }
11082           return;
11083         }
11084     }
11085   printf (_("%08lx  UNDEFINED\n"), oc);
11086 }
11087 #endif
11088
11089 static symbolS *
11090 get_symbol ()
11091 {
11092   int c;
11093   char *name;
11094   symbolS *p;
11095
11096   name = input_line_pointer;
11097   c = get_symbol_end ();
11098   p = (symbolS *) symbol_find_or_make (name);
11099   *input_line_pointer = c;
11100   return p;
11101 }
11102
11103 /* Align the current frag to a given power of two.  The MIPS assembler
11104    also automatically adjusts any preceding label.  */
11105
11106 static void
11107 mips_align (to, fill, label)
11108      int to;
11109      int fill;
11110      symbolS *label;
11111 {
11112   mips_emit_delays (false);
11113   frag_align (to, fill, 0);
11114   record_alignment (now_seg, to);
11115   if (label != NULL)
11116     {
11117       assert (S_GET_SEGMENT (label) == now_seg);
11118       symbol_set_frag (label, frag_now);
11119       S_SET_VALUE (label, (valueT) frag_now_fix ());
11120     }
11121 }
11122
11123 /* Align to a given power of two.  .align 0 turns off the automatic
11124    alignment used by the data creating pseudo-ops.  */
11125
11126 static void
11127 s_align (x)
11128      int x ATTRIBUTE_UNUSED;
11129 {
11130   register int temp;
11131   register long temp_fill;
11132   long max_alignment = 15;
11133
11134   /*
11135
11136     o  Note that the assembler pulls down any immediately preceeding label
11137        to the aligned address.
11138     o  It's not documented but auto alignment is reinstated by
11139        a .align pseudo instruction.
11140     o  Note also that after auto alignment is turned off the mips assembler
11141        issues an error on attempt to assemble an improperly aligned data item.
11142        We don't.
11143
11144     */
11145
11146   temp = get_absolute_expression ();
11147   if (temp > max_alignment)
11148     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11149   else if (temp < 0)
11150     {
11151       as_warn (_("Alignment negative: 0 assumed."));
11152       temp = 0;
11153     }
11154   if (*input_line_pointer == ',')
11155     {
11156       ++input_line_pointer;
11157       temp_fill = get_absolute_expression ();
11158     }
11159   else
11160     temp_fill = 0;
11161   if (temp)
11162     {
11163       auto_align = 1;
11164       mips_align (temp, (int) temp_fill,
11165                   insn_labels != NULL ? insn_labels->label : NULL);
11166     }
11167   else
11168     {
11169       auto_align = 0;
11170     }
11171
11172   demand_empty_rest_of_line ();
11173 }
11174
11175 void
11176 mips_flush_pending_output ()
11177 {
11178   mips_emit_delays (false);
11179   mips_clear_insn_labels ();
11180 }
11181
11182 static void
11183 s_change_sec (sec)
11184      int sec;
11185 {
11186   segT seg;
11187
11188   /* When generating embedded PIC code, we only use the .text, .lit8,
11189      .sdata and .sbss sections.  We change the .data and .rdata
11190      pseudo-ops to use .sdata.  */
11191   if (mips_pic == EMBEDDED_PIC
11192       && (sec == 'd' || sec == 'r'))
11193     sec = 's';
11194
11195 #ifdef OBJ_ELF
11196   /* The ELF backend needs to know that we are changing sections, so
11197      that .previous works correctly.  We could do something like check
11198      for an obj_section_change_hook macro, but that might be confusing
11199      as it would not be appropriate to use it in the section changing
11200      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11201      This should be cleaner, somehow.  */
11202   obj_elf_section_change_hook ();
11203 #endif
11204
11205   mips_emit_delays (false);
11206   switch (sec)
11207     {
11208     case 't':
11209       s_text (0);
11210       break;
11211     case 'd':
11212       s_data (0);
11213       break;
11214     case 'b':
11215       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11216       demand_empty_rest_of_line ();
11217       break;
11218
11219     case 'r':
11220       if (USE_GLOBAL_POINTER_OPT)
11221         {
11222           seg = subseg_new (RDATA_SECTION_NAME,
11223                             (subsegT) get_absolute_expression ());
11224           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11225             {
11226               bfd_set_section_flags (stdoutput, seg,
11227                                      (SEC_ALLOC
11228                                       | SEC_LOAD
11229                                       | SEC_READONLY
11230                                       | SEC_RELOC
11231                                       | SEC_DATA));
11232               if (strcmp (TARGET_OS, "elf") != 0)
11233                 record_alignment (seg, 4);
11234             }
11235           demand_empty_rest_of_line ();
11236         }
11237       else
11238         {
11239           as_bad (_("No read only data section in this object file format"));
11240           demand_empty_rest_of_line ();
11241           return;
11242         }
11243       break;
11244
11245     case 's':
11246       if (USE_GLOBAL_POINTER_OPT)
11247         {
11248           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11249           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11250             {
11251               bfd_set_section_flags (stdoutput, seg,
11252                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11253                                      | SEC_DATA);
11254               if (strcmp (TARGET_OS, "elf") != 0)
11255                 record_alignment (seg, 4);
11256             }
11257           demand_empty_rest_of_line ();
11258           break;
11259         }
11260       else
11261         {
11262           as_bad (_("Global pointers not supported; recompile -G 0"));
11263           demand_empty_rest_of_line ();
11264           return;
11265         }
11266     }
11267
11268   auto_align = 1;
11269 }
11270   
11271 void
11272 s_change_section (ignore)
11273      int ignore ATTRIBUTE_UNUSED;
11274 {
11275   expressionS rep_exp;
11276   
11277   char *section_name;
11278   char c;
11279   char *next_c;
11280   char *p;
11281   int section_type;
11282   int section_flag;
11283   int section_entry_size;
11284   int section_alignment;
11285   int log = -1;
11286   flagword flags;
11287   
11288   section_name = input_line_pointer;
11289   c = get_symbol_end ();
11290   next_c = input_line_pointer + 1;
11291   /* just after name is now '\0' */
11292   p = input_line_pointer;
11293
11294   /* Do we have .section Name<,"flags">  */
11295   if (c == '\n' || (c == ',' && *next_c == '"') || c == '"')
11296     {
11297       *p = c;
11298       input_line_pointer = section_name;
11299       obj_elf_section (ignore);
11300       return;
11301     }
11302   input_line_pointer++;
11303
11304   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11305   if (c == ',')
11306     section_type = get_absolute_expression ();
11307   else
11308     section_type = 0;
11309   if (*input_line_pointer++ == ',')
11310     section_flag = get_absolute_expression ();
11311   else
11312     section_flag = 0;
11313   if (*input_line_pointer++ == ',')
11314     section_entry_size = get_absolute_expression ();
11315   else
11316     section_entry_size = 0;
11317   if (*input_line_pointer++ == ',')
11318     section_alignment = get_absolute_expression ();
11319   else
11320     section_alignment = 0;
11321
11322   obj_elf_change_section (section_name, section_type, section_flag,
11323                           section_entry_size, 0, 0, 0);
11324 }
11325
11326 void
11327 mips_enable_auto_align ()
11328 {
11329   auto_align = 1;
11330 }
11331
11332 static void
11333 s_cons (log_size)
11334      int log_size;
11335 {
11336   symbolS *label;
11337
11338   label = insn_labels != NULL ? insn_labels->label : NULL;
11339   mips_emit_delays (false);
11340   if (log_size > 0 && auto_align)
11341     mips_align (log_size, 0, label);
11342   mips_clear_insn_labels ();
11343   cons (1 << log_size);
11344 }
11345
11346 static void
11347 s_float_cons (type)
11348      int type;
11349 {
11350   symbolS *label;
11351
11352   label = insn_labels != NULL ? insn_labels->label : NULL;
11353
11354   mips_emit_delays (false);
11355
11356   if (auto_align)
11357     {
11358       if (type == 'd')
11359         mips_align (3, 0, label);
11360       else
11361         mips_align (2, 0, label);
11362     }
11363
11364   mips_clear_insn_labels ();
11365
11366   float_cons (type);
11367 }
11368
11369 /* Handle .globl.  We need to override it because on Irix 5 you are
11370    permitted to say
11371        .globl foo .text
11372    where foo is an undefined symbol, to mean that foo should be
11373    considered to be the address of a function.  */
11374
11375 static void
11376 s_mips_globl (x)
11377      int x ATTRIBUTE_UNUSED;
11378 {
11379   char *name;
11380   int c;
11381   symbolS *symbolP;
11382   flagword flag;
11383
11384   name = input_line_pointer;
11385   c = get_symbol_end ();
11386   symbolP = symbol_find_or_make (name);
11387   *input_line_pointer = c;
11388   SKIP_WHITESPACE ();
11389
11390   /* On Irix 5, every global symbol that is not explicitly labelled as
11391      being a function is apparently labelled as being an object.  */
11392   flag = BSF_OBJECT;
11393
11394   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11395     {
11396       char *secname;
11397       asection *sec;
11398
11399       secname = input_line_pointer;
11400       c = get_symbol_end ();
11401       sec = bfd_get_section_by_name (stdoutput, secname);
11402       if (sec == NULL)
11403         as_bad (_("%s: no such section"), secname);
11404       *input_line_pointer = c;
11405
11406       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11407         flag = BSF_FUNCTION;
11408     }
11409
11410   symbol_get_bfdsym (symbolP)->flags |= flag;
11411
11412   S_SET_EXTERNAL (symbolP);
11413   demand_empty_rest_of_line ();
11414 }
11415
11416 static void
11417 s_option (x)
11418      int x ATTRIBUTE_UNUSED;
11419 {
11420   char *opt;
11421   char c;
11422
11423   opt = input_line_pointer;
11424   c = get_symbol_end ();
11425
11426   if (*opt == 'O')
11427     {
11428       /* FIXME: What does this mean?  */
11429     }
11430   else if (strncmp (opt, "pic", 3) == 0)
11431     {
11432       int i;
11433
11434       i = atoi (opt + 3);
11435       if (i == 0)
11436         mips_pic = NO_PIC;
11437       else if (i == 2)
11438         mips_pic = SVR4_PIC;
11439       else
11440         as_bad (_(".option pic%d not supported"), i);
11441
11442       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11443         {
11444           if (g_switch_seen && g_switch_value != 0)
11445             as_warn (_("-G may not be used with SVR4 PIC code"));
11446           g_switch_value = 0;
11447           bfd_set_gp_size (stdoutput, 0);
11448         }
11449     }
11450   else
11451     as_warn (_("Unrecognized option \"%s\""), opt);
11452
11453   *input_line_pointer = c;
11454   demand_empty_rest_of_line ();
11455 }
11456
11457 /* This structure is used to hold a stack of .set values.  */
11458
11459 struct mips_option_stack
11460 {
11461   struct mips_option_stack *next;
11462   struct mips_set_options options;
11463 };
11464
11465 static struct mips_option_stack *mips_opts_stack;
11466
11467 /* Handle the .set pseudo-op.  */
11468
11469 static void
11470 s_mipsset (x)
11471      int x ATTRIBUTE_UNUSED;
11472 {
11473   char *name = input_line_pointer, ch;
11474
11475   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11476     ++input_line_pointer;
11477   ch = *input_line_pointer;
11478   *input_line_pointer = '\0';
11479
11480   if (strcmp (name, "reorder") == 0)
11481     {
11482       if (mips_opts.noreorder && prev_nop_frag != NULL)
11483         {
11484           /* If we still have pending nops, we can discard them.  The
11485              usual nop handling will insert any that are still
11486              needed.  */
11487           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11488                                     * (mips_opts.mips16 ? 2 : 4));
11489           prev_nop_frag = NULL;
11490         }
11491       mips_opts.noreorder = 0;
11492     }
11493   else if (strcmp (name, "noreorder") == 0)
11494     {
11495       mips_emit_delays (true);
11496       mips_opts.noreorder = 1;
11497       mips_any_noreorder = 1;
11498     }
11499   else if (strcmp (name, "at") == 0)
11500     {
11501       mips_opts.noat = 0;
11502     }
11503   else if (strcmp (name, "noat") == 0)
11504     {
11505       mips_opts.noat = 1;
11506     }
11507   else if (strcmp (name, "macro") == 0)
11508     {
11509       mips_opts.warn_about_macros = 0;
11510     }
11511   else if (strcmp (name, "nomacro") == 0)
11512     {
11513       if (mips_opts.noreorder == 0)
11514         as_bad (_("`noreorder' must be set before `nomacro'"));
11515       mips_opts.warn_about_macros = 1;
11516     }
11517   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11518     {
11519       mips_opts.nomove = 0;
11520     }
11521   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11522     {
11523       mips_opts.nomove = 1;
11524     }
11525   else if (strcmp (name, "bopt") == 0)
11526     {
11527       mips_opts.nobopt = 0;
11528     }
11529   else if (strcmp (name, "nobopt") == 0)
11530     {
11531       mips_opts.nobopt = 1;
11532     }
11533   else if (strcmp (name, "mips16") == 0
11534            || strcmp (name, "MIPS-16") == 0)
11535     mips_opts.mips16 = 1;
11536   else if (strcmp (name, "nomips16") == 0
11537            || strcmp (name, "noMIPS-16") == 0)
11538     mips_opts.mips16 = 0;
11539   else if (strcmp (name, "mips3d") == 0)
11540     mips_opts.ase_mips3d = 1;
11541   else if (strcmp (name, "nomips3d") == 0)
11542     mips_opts.ase_mips3d = 0;
11543   else if (strcmp (name, "mdmx") == 0)
11544     mips_opts.ase_mdmx = 1;
11545   else if (strcmp (name, "nomdmx") == 0)
11546     mips_opts.ase_mdmx = 0;
11547   else if (strncmp (name, "mips", 4) == 0)
11548     {
11549       int isa;
11550
11551       /* Permit the user to change the ISA on the fly.  Needless to
11552          say, misuse can cause serious problems.  */
11553       isa = atoi (name + 4);
11554       switch (isa)
11555         {
11556         case  0:
11557           mips_opts.gp32 = file_mips_gp32;
11558           mips_opts.fp32 = file_mips_fp32;
11559           break;
11560         case  1:
11561         case  2:
11562         case 32:
11563           mips_opts.gp32 = 1;
11564           mips_opts.fp32 = 1;
11565           break;
11566         case  3:
11567         case  4:
11568         case  5:
11569         case 64:
11570           mips_opts.gp32 = 0;
11571           mips_opts.fp32 = 0;
11572           break;
11573         default:
11574           as_bad (_("unknown ISA level %s"), name + 4);
11575           break;
11576         }
11577
11578       switch (isa)
11579         {
11580         case  0: mips_opts.isa = file_mips_isa;   break;
11581         case  1: mips_opts.isa = ISA_MIPS1;       break;
11582         case  2: mips_opts.isa = ISA_MIPS2;       break;
11583         case  3: mips_opts.isa = ISA_MIPS3;       break;
11584         case  4: mips_opts.isa = ISA_MIPS4;       break;
11585         case  5: mips_opts.isa = ISA_MIPS5;       break;
11586         case 32: mips_opts.isa = ISA_MIPS32;      break;
11587         case 64: mips_opts.isa = ISA_MIPS64;      break;
11588         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11589         }
11590     }
11591   else if (strcmp (name, "autoextend") == 0)
11592     mips_opts.noautoextend = 0;
11593   else if (strcmp (name, "noautoextend") == 0)
11594     mips_opts.noautoextend = 1;
11595   else if (strcmp (name, "push") == 0)
11596     {
11597       struct mips_option_stack *s;
11598
11599       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11600       s->next = mips_opts_stack;
11601       s->options = mips_opts;
11602       mips_opts_stack = s;
11603     }
11604   else if (strcmp (name, "pop") == 0)
11605     {
11606       struct mips_option_stack *s;
11607
11608       s = mips_opts_stack;
11609       if (s == NULL)
11610         as_bad (_(".set pop with no .set push"));
11611       else
11612         {
11613           /* If we're changing the reorder mode we need to handle
11614              delay slots correctly.  */
11615           if (s->options.noreorder && ! mips_opts.noreorder)
11616             mips_emit_delays (true);
11617           else if (! s->options.noreorder && mips_opts.noreorder)
11618             {
11619               if (prev_nop_frag != NULL)
11620                 {
11621                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11622                                             * (mips_opts.mips16 ? 2 : 4));
11623                   prev_nop_frag = NULL;
11624                 }
11625             }
11626
11627           mips_opts = s->options;
11628           mips_opts_stack = s->next;
11629           free (s);
11630         }
11631     }
11632   else
11633     {
11634       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11635     }
11636   *input_line_pointer = ch;
11637   demand_empty_rest_of_line ();
11638 }
11639
11640 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11641    .option pic2.  It means to generate SVR4 PIC calls.  */
11642
11643 static void
11644 s_abicalls (ignore)
11645      int ignore ATTRIBUTE_UNUSED;
11646 {
11647   mips_pic = SVR4_PIC;
11648   if (USE_GLOBAL_POINTER_OPT)
11649     {
11650       if (g_switch_seen && g_switch_value != 0)
11651         as_warn (_("-G may not be used with SVR4 PIC code"));
11652       g_switch_value = 0;
11653     }
11654   bfd_set_gp_size (stdoutput, 0);
11655   demand_empty_rest_of_line ();
11656 }
11657
11658 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11659    PIC code.  It sets the $gp register for the function based on the
11660    function address, which is in the register named in the argument.
11661    This uses a relocation against _gp_disp, which is handled specially
11662    by the linker.  The result is:
11663         lui     $gp,%hi(_gp_disp)
11664         addiu   $gp,$gp,%lo(_gp_disp)
11665         addu    $gp,$gp,.cpload argument
11666    The .cpload argument is normally $25 == $t9.  */
11667
11668 static void
11669 s_cpload (ignore)
11670      int ignore ATTRIBUTE_UNUSED;
11671 {
11672   expressionS ex;
11673   int icnt = 0;
11674
11675   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11676      .cpload is ignored.  */
11677   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11678     {
11679       s_ignore (0);
11680       return;
11681     }
11682
11683   /* .cpload should be in a .set noreorder section.  */
11684   if (mips_opts.noreorder == 0)
11685     as_warn (_(".cpload not in noreorder section"));
11686
11687   ex.X_op = O_symbol;
11688   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11689   ex.X_op_symbol = NULL;
11690   ex.X_add_number = 0;
11691
11692   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11693   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11694
11695   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11696   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11697                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11698
11699   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11700                mips_gp_register, mips_gp_register, tc_get_register (0));
11701
11702   demand_empty_rest_of_line ();
11703 }
11704
11705 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11706      .cpsetup $reg1, offset|$reg2, label
11707
11708    If offset is given, this results in:
11709      sd         $gp, offset($sp)
11710      lui        $gp, %hi(%neg(%gp_rel(label)))
11711      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11712      daddu      $gp, $gp, $reg1
11713
11714    If $reg2 is given, this results in:
11715      daddu      $reg2, $gp, $0
11716      lui        $gp, %hi(%neg(%gp_rel(label)))
11717      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11718      daddu      $gp, $gp, $reg1
11719    $reg1 is normally $25 == $t9.  */
11720 static void
11721 s_cpsetup (ignore)
11722      int ignore ATTRIBUTE_UNUSED;
11723 {
11724   expressionS ex_off;
11725   expressionS ex_sym;
11726   int reg1;
11727   int icnt = 0;
11728   char *f;
11729
11730   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11731      We also need NewABI support.  */
11732   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11733     {
11734       s_ignore (0);
11735       return;
11736     }
11737
11738   reg1 = tc_get_register (0);
11739   SKIP_WHITESPACE ();
11740   if (*input_line_pointer != ',')
11741     {
11742       as_bad (_("missing argument separator ',' for .cpsetup"));
11743       return;
11744     }
11745   else
11746     ++input_line_pointer;
11747   SKIP_WHITESPACE ();
11748   if (*input_line_pointer == '$')
11749     {
11750       mips_cpreturn_register = tc_get_register (0);
11751       mips_cpreturn_offset = -1;
11752     }
11753   else
11754     {
11755       mips_cpreturn_offset = get_absolute_expression ();
11756       mips_cpreturn_register = -1;
11757     }
11758   SKIP_WHITESPACE ();
11759   if (*input_line_pointer != ',')
11760     {
11761       as_bad (_("missing argument separator ',' for .cpsetup"));
11762       return;
11763     }
11764   else
11765     ++input_line_pointer;
11766   SKIP_WHITESPACE ();
11767   expression (&ex_sym);
11768
11769   if (mips_cpreturn_register == -1)
11770     {
11771       ex_off.X_op = O_constant;
11772       ex_off.X_add_symbol = NULL;
11773       ex_off.X_op_symbol = NULL;
11774       ex_off.X_add_number = mips_cpreturn_offset;
11775
11776       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11777                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11778     }
11779   else
11780     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11781                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11782
11783   /* Ensure there's room for the next two instructions, so that `f'
11784      doesn't end up with an address in the wrong frag.  */
11785   frag_grow (8);
11786   f = frag_more (0);
11787   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11788                (int) BFD_RELOC_GPREL16);
11789   fix_new (frag_now, f - frag_now->fr_literal,
11790            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11791   fix_new (frag_now, f - frag_now->fr_literal,
11792            0, NULL, 0, 0, BFD_RELOC_HI16_S);
11793
11794   f = frag_more (0);
11795   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11796                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11797   fix_new (frag_now, f - frag_now->fr_literal,
11798            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11799   fix_new (frag_now, f - frag_now->fr_literal,
11800            0, NULL, 0, 0, BFD_RELOC_LO16);
11801
11802   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11803                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11804                mips_gp_register, mips_gp_register, reg1);
11805
11806   demand_empty_rest_of_line ();
11807 }
11808
11809 static void
11810 s_cplocal (ignore)
11811      int ignore ATTRIBUTE_UNUSED;
11812 {
11813   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11814    .cplocal is ignored.  */
11815   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11816     {
11817       s_ignore (0);
11818       return;
11819     }
11820
11821   mips_gp_register = tc_get_register (0);
11822   demand_empty_rest_of_line ();
11823 }
11824
11825 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11826    offset from $sp.  The offset is remembered, and after making a PIC
11827    call $gp is restored from that location.  */
11828
11829 static void
11830 s_cprestore (ignore)
11831      int ignore ATTRIBUTE_UNUSED;
11832 {
11833   expressionS ex;
11834   int icnt = 0;
11835
11836   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11837      .cprestore is ignored.  */
11838   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11839     {
11840       s_ignore (0);
11841       return;
11842     }
11843
11844   mips_cprestore_offset = get_absolute_expression ();
11845   mips_cprestore_valid = 1;
11846
11847   ex.X_op = O_constant;
11848   ex.X_add_symbol = NULL;
11849   ex.X_op_symbol = NULL;
11850   ex.X_add_number = mips_cprestore_offset;
11851
11852   macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11853                "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
11854
11855   demand_empty_rest_of_line ();
11856 }
11857
11858 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11859    was given in the preceeding .gpsetup, it results in:
11860      ld         $gp, offset($sp)
11861
11862    If a register $reg2 was given there, it results in:
11863      daddiu     $gp, $gp, $reg2
11864  */
11865 static void
11866 s_cpreturn (ignore)
11867      int ignore ATTRIBUTE_UNUSED;
11868 {
11869   expressionS ex;
11870   int icnt = 0;
11871
11872   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11873      We also need NewABI support.  */
11874   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11875     {
11876       s_ignore (0);
11877       return;
11878     }
11879
11880   if (mips_cpreturn_register == -1)
11881     {
11882       ex.X_op = O_constant;
11883       ex.X_add_symbol = NULL;
11884       ex.X_op_symbol = NULL;
11885       ex.X_add_number = mips_cpreturn_offset;
11886
11887       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11888                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11889     }
11890   else
11891     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11892                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11893
11894   demand_empty_rest_of_line ();
11895 }
11896
11897 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
11898    code.  It sets the offset to use in gp_rel relocations.  */
11899
11900 static void
11901 s_gpvalue (ignore)
11902      int ignore ATTRIBUTE_UNUSED;
11903 {
11904   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11905      We also need NewABI support.  */
11906   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11907     {
11908       s_ignore (0);
11909       return;
11910     }
11911
11912   mips_gprel_offset = get_absolute_expression ();
11913
11914   demand_empty_rest_of_line ();
11915 }
11916
11917 /* Handle the .gpword pseudo-op.  This is used when generating PIC
11918    code.  It generates a 32 bit GP relative reloc.  */
11919
11920 static void
11921 s_gpword (ignore)
11922      int ignore ATTRIBUTE_UNUSED;
11923 {
11924   symbolS *label;
11925   expressionS ex;
11926   char *p;
11927
11928   /* When not generating PIC code, this is treated as .word.  */
11929   if (mips_pic != SVR4_PIC)
11930     {
11931       s_cons (2);
11932       return;
11933     }
11934
11935   label = insn_labels != NULL ? insn_labels->label : NULL;
11936   mips_emit_delays (true);
11937   if (auto_align)
11938     mips_align (2, 0, label);
11939   mips_clear_insn_labels ();
11940
11941   expression (&ex);
11942
11943   if (ex.X_op != O_symbol || ex.X_add_number != 0)
11944     {
11945       as_bad (_("Unsupported use of .gpword"));
11946       ignore_rest_of_line ();
11947     }
11948
11949   p = frag_more (4);
11950   md_number_to_chars (p, (valueT) 0, 4);
11951   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
11952                BFD_RELOC_GPREL32);
11953
11954   demand_empty_rest_of_line ();
11955 }
11956
11957 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
11958    tables in SVR4 PIC code.  */
11959
11960 static void
11961 s_cpadd (ignore)
11962      int ignore ATTRIBUTE_UNUSED;
11963 {
11964   int icnt = 0;
11965   int reg;
11966
11967   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11968      code.  */
11969   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11970     {
11971       s_ignore (0);
11972       return;
11973     }
11974
11975   /* Add $gp to the register named as an argument.  */
11976   reg = tc_get_register (0);
11977   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11978                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11979                "d,v,t", reg, reg, mips_gp_register);
11980
11981   demand_empty_rest_of_line ();
11982 }
11983
11984 /* Handle the .insn pseudo-op.  This marks instruction labels in
11985    mips16 mode.  This permits the linker to handle them specially,
11986    such as generating jalx instructions when needed.  We also make
11987    them odd for the duration of the assembly, in order to generate the
11988    right sort of code.  We will make them even in the adjust_symtab
11989    routine, while leaving them marked.  This is convenient for the
11990    debugger and the disassembler.  The linker knows to make them odd
11991    again.  */
11992
11993 static void
11994 s_insn (ignore)
11995      int ignore ATTRIBUTE_UNUSED;
11996 {
11997   mips16_mark_labels ();
11998
11999   demand_empty_rest_of_line ();
12000 }
12001
12002 /* Handle a .stabn directive.  We need these in order to mark a label
12003    as being a mips16 text label correctly.  Sometimes the compiler
12004    will emit a label, followed by a .stabn, and then switch sections.
12005    If the label and .stabn are in mips16 mode, then the label is
12006    really a mips16 text label.  */
12007
12008 static void
12009 s_mips_stab (type)
12010      int type;
12011 {
12012   if (type == 'n')
12013     mips16_mark_labels ();
12014
12015   s_stab (type);
12016 }
12017
12018 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12019  */
12020
12021 static void
12022 s_mips_weakext (ignore)
12023      int ignore ATTRIBUTE_UNUSED;
12024 {
12025   char *name;
12026   int c;
12027   symbolS *symbolP;
12028   expressionS exp;
12029
12030   name = input_line_pointer;
12031   c = get_symbol_end ();
12032   symbolP = symbol_find_or_make (name);
12033   S_SET_WEAK (symbolP);
12034   *input_line_pointer = c;
12035
12036   SKIP_WHITESPACE ();
12037
12038   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12039     {
12040       if (S_IS_DEFINED (symbolP))
12041         {
12042           as_bad ("ignoring attempt to redefine symbol %s",
12043                   S_GET_NAME (symbolP));
12044           ignore_rest_of_line ();
12045           return;
12046         }
12047
12048       if (*input_line_pointer == ',')
12049         {
12050           ++input_line_pointer;
12051           SKIP_WHITESPACE ();
12052         }
12053
12054       expression (&exp);
12055       if (exp.X_op != O_symbol)
12056         {
12057           as_bad ("bad .weakext directive");
12058           ignore_rest_of_line ();
12059           return;
12060         }
12061       symbol_set_value_expression (symbolP, &exp);
12062     }
12063
12064   demand_empty_rest_of_line ();
12065 }
12066
12067 /* Parse a register string into a number.  Called from the ECOFF code
12068    to parse .frame.  The argument is non-zero if this is the frame
12069    register, so that we can record it in mips_frame_reg.  */
12070
12071 int
12072 tc_get_register (frame)
12073      int frame;
12074 {
12075   int reg;
12076
12077   SKIP_WHITESPACE ();
12078   if (*input_line_pointer++ != '$')
12079     {
12080       as_warn (_("expected `$'"));
12081       reg = ZERO;
12082     }
12083   else if (ISDIGIT (*input_line_pointer))
12084     {
12085       reg = get_absolute_expression ();
12086       if (reg < 0 || reg >= 32)
12087         {
12088           as_warn (_("Bad register number"));
12089           reg = ZERO;
12090         }
12091     }
12092   else
12093     {
12094       if (strncmp (input_line_pointer, "ra", 2) == 0)
12095         {
12096           reg = RA;
12097           input_line_pointer += 2;
12098         }
12099       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12100         {
12101           reg = FP;
12102           input_line_pointer += 2;
12103         }
12104       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12105         {
12106           reg = SP;
12107           input_line_pointer += 2;
12108         }
12109       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12110         {
12111           reg = GP;
12112           input_line_pointer += 2;
12113         }
12114       else if (strncmp (input_line_pointer, "at", 2) == 0)
12115         {
12116           reg = AT;
12117           input_line_pointer += 2;
12118         }
12119       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12120         {
12121           reg = KT0;
12122           input_line_pointer += 3;
12123         }
12124       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12125         {
12126           reg = KT1;
12127           input_line_pointer += 3;
12128         }
12129       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12130         {
12131           reg = ZERO;
12132           input_line_pointer += 4;
12133         }
12134       else
12135         {
12136           as_warn (_("Unrecognized register name"));
12137           reg = ZERO;
12138           while (ISALNUM(*input_line_pointer))
12139            input_line_pointer++;
12140         }
12141     }
12142   if (frame)
12143     {
12144       mips_frame_reg = reg != 0 ? reg : SP;
12145       mips_frame_reg_valid = 1;
12146       mips_cprestore_valid = 0;
12147     }
12148   return reg;
12149 }
12150
12151 valueT
12152 md_section_align (seg, addr)
12153      asection *seg;
12154      valueT addr;
12155 {
12156   int align = bfd_get_section_alignment (stdoutput, seg);
12157
12158 #ifdef OBJ_ELF
12159   /* We don't need to align ELF sections to the full alignment.
12160      However, Irix 5 may prefer that we align them at least to a 16
12161      byte boundary.  We don't bother to align the sections if we are
12162      targeted for an embedded system.  */
12163   if (strcmp (TARGET_OS, "elf") == 0)
12164     return addr;
12165   if (align > 4)
12166     align = 4;
12167 #endif
12168
12169   return ((addr + (1 << align) - 1) & (-1 << align));
12170 }
12171
12172 /* Utility routine, called from above as well.  If called while the
12173    input file is still being read, it's only an approximation.  (For
12174    example, a symbol may later become defined which appeared to be
12175    undefined earlier.)  */
12176
12177 static int
12178 nopic_need_relax (sym, before_relaxing)
12179      symbolS *sym;
12180      int before_relaxing;
12181 {
12182   if (sym == 0)
12183     return 0;
12184
12185   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12186     {
12187       const char *symname;
12188       int change;
12189
12190       /* Find out whether this symbol can be referenced off the $gp
12191          register.  It can be if it is smaller than the -G size or if
12192          it is in the .sdata or .sbss section.  Certain symbols can
12193          not be referenced off the $gp, although it appears as though
12194          they can.  */
12195       symname = S_GET_NAME (sym);
12196       if (symname != (const char *) NULL
12197           && (strcmp (symname, "eprol") == 0
12198               || strcmp (symname, "etext") == 0
12199               || strcmp (symname, "_gp") == 0
12200               || strcmp (symname, "edata") == 0
12201               || strcmp (symname, "_fbss") == 0
12202               || strcmp (symname, "_fdata") == 0
12203               || strcmp (symname, "_ftext") == 0
12204               || strcmp (symname, "end") == 0
12205               || strcmp (symname, "_gp_disp") == 0))
12206         change = 1;
12207       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12208                && (0
12209 #ifndef NO_ECOFF_DEBUGGING
12210                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12211                        && (symbol_get_obj (sym)->ecoff_extern_size
12212                            <= g_switch_value))
12213 #endif
12214                    /* We must defer this decision until after the whole
12215                       file has been read, since there might be a .extern
12216                       after the first use of this symbol.  */
12217                    || (before_relaxing
12218 #ifndef NO_ECOFF_DEBUGGING
12219                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12220 #endif
12221                        && S_GET_VALUE (sym) == 0)
12222                    || (S_GET_VALUE (sym) != 0
12223                        && S_GET_VALUE (sym) <= g_switch_value)))
12224         change = 0;
12225       else
12226         {
12227           const char *segname;
12228
12229           segname = segment_name (S_GET_SEGMENT (sym));
12230           assert (strcmp (segname, ".lit8") != 0
12231                   && strcmp (segname, ".lit4") != 0);
12232           change = (strcmp (segname, ".sdata") != 0
12233                     && strcmp (segname, ".sbss") != 0
12234                     && strncmp (segname, ".sdata.", 7) != 0
12235                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12236         }
12237       return change;
12238     }
12239   else
12240     /* We are not optimizing for the $gp register.  */
12241     return 1;
12242 }
12243
12244 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12245    extended opcode.  SEC is the section the frag is in.  */
12246
12247 static int
12248 mips16_extended_frag (fragp, sec, stretch)
12249      fragS *fragp;
12250      asection *sec;
12251      long stretch;
12252 {
12253   int type;
12254   register const struct mips16_immed_operand *op;
12255   offsetT val;
12256   int mintiny, maxtiny;
12257   segT symsec;
12258   fragS *sym_frag;
12259
12260   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12261     return 0;
12262   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12263     return 1;
12264
12265   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12266   op = mips16_immed_operands;
12267   while (op->type != type)
12268     {
12269       ++op;
12270       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12271     }
12272
12273   if (op->unsp)
12274     {
12275       if (type == '<' || type == '>' || type == '[' || type == ']')
12276         {
12277           mintiny = 1;
12278           maxtiny = 1 << op->nbits;
12279         }
12280       else
12281         {
12282           mintiny = 0;
12283           maxtiny = (1 << op->nbits) - 1;
12284         }
12285     }
12286   else
12287     {
12288       mintiny = - (1 << (op->nbits - 1));
12289       maxtiny = (1 << (op->nbits - 1)) - 1;
12290     }
12291
12292   sym_frag = symbol_get_frag (fragp->fr_symbol);
12293   val = S_GET_VALUE (fragp->fr_symbol);
12294   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12295
12296   if (op->pcrel)
12297     {
12298       addressT addr;
12299
12300       /* We won't have the section when we are called from
12301          mips_relax_frag.  However, we will always have been called
12302          from md_estimate_size_before_relax first.  If this is a
12303          branch to a different section, we mark it as such.  If SEC is
12304          NULL, and the frag is not marked, then it must be a branch to
12305          the same section.  */
12306       if (sec == NULL)
12307         {
12308           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12309             return 1;
12310         }
12311       else
12312         {
12313           /* Must have been called from md_estimate_size_before_relax.  */
12314           if (symsec != sec)
12315             {
12316               fragp->fr_subtype =
12317                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12318
12319               /* FIXME: We should support this, and let the linker
12320                  catch branches and loads that are out of range.  */
12321               as_bad_where (fragp->fr_file, fragp->fr_line,
12322                             _("unsupported PC relative reference to different section"));
12323
12324               return 1;
12325             }
12326           if (fragp != sym_frag && sym_frag->fr_address == 0)
12327             /* Assume non-extended on the first relaxation pass.
12328                The address we have calculated will be bogus if this is
12329                a forward branch to another frag, as the forward frag
12330                will have fr_address == 0.  */
12331             return 0;
12332         }
12333
12334       /* In this case, we know for sure that the symbol fragment is in
12335          the same section.  If the relax_marker of the symbol fragment
12336          differs from the relax_marker of this fragment, we have not
12337          yet adjusted the symbol fragment fr_address.  We want to add
12338          in STRETCH in order to get a better estimate of the address.
12339          This particularly matters because of the shift bits.  */
12340       if (stretch != 0
12341           && sym_frag->relax_marker != fragp->relax_marker)
12342         {
12343           fragS *f;
12344
12345           /* Adjust stretch for any alignment frag.  Note that if have
12346              been expanding the earlier code, the symbol may be
12347              defined in what appears to be an earlier frag.  FIXME:
12348              This doesn't handle the fr_subtype field, which specifies
12349              a maximum number of bytes to skip when doing an
12350              alignment.  */
12351           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12352             {
12353               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12354                 {
12355                   if (stretch < 0)
12356                     stretch = - ((- stretch)
12357                                  & ~ ((1 << (int) f->fr_offset) - 1));
12358                   else
12359                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12360                   if (stretch == 0)
12361                     break;
12362                 }
12363             }
12364           if (f != NULL)
12365             val += stretch;
12366         }
12367
12368       addr = fragp->fr_address + fragp->fr_fix;
12369
12370       /* The base address rules are complicated.  The base address of
12371          a branch is the following instruction.  The base address of a
12372          PC relative load or add is the instruction itself, but if it
12373          is in a delay slot (in which case it can not be extended) use
12374          the address of the instruction whose delay slot it is in.  */
12375       if (type == 'p' || type == 'q')
12376         {
12377           addr += 2;
12378
12379           /* If we are currently assuming that this frag should be
12380              extended, then, the current address is two bytes
12381              higher.  */
12382           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12383             addr += 2;
12384
12385           /* Ignore the low bit in the target, since it will be set
12386              for a text label.  */
12387           if ((val & 1) != 0)
12388             --val;
12389         }
12390       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12391         addr -= 4;
12392       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12393         addr -= 2;
12394
12395       val -= addr & ~ ((1 << op->shift) - 1);
12396
12397       /* Branch offsets have an implicit 0 in the lowest bit.  */
12398       if (type == 'p' || type == 'q')
12399         val /= 2;
12400
12401       /* If any of the shifted bits are set, we must use an extended
12402          opcode.  If the address depends on the size of this
12403          instruction, this can lead to a loop, so we arrange to always
12404          use an extended opcode.  We only check this when we are in
12405          the main relaxation loop, when SEC is NULL.  */
12406       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12407         {
12408           fragp->fr_subtype =
12409             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12410           return 1;
12411         }
12412
12413       /* If we are about to mark a frag as extended because the value
12414          is precisely maxtiny + 1, then there is a chance of an
12415          infinite loop as in the following code:
12416              la $4,foo
12417              .skip      1020
12418              .align     2
12419            foo:
12420          In this case when the la is extended, foo is 0x3fc bytes
12421          away, so the la can be shrunk, but then foo is 0x400 away, so
12422          the la must be extended.  To avoid this loop, we mark the
12423          frag as extended if it was small, and is about to become
12424          extended with a value of maxtiny + 1.  */
12425       if (val == ((maxtiny + 1) << op->shift)
12426           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12427           && sec == NULL)
12428         {
12429           fragp->fr_subtype =
12430             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12431           return 1;
12432         }
12433     }
12434   else if (symsec != absolute_section && sec != NULL)
12435     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12436
12437   if ((val & ((1 << op->shift) - 1)) != 0
12438       || val < (mintiny << op->shift)
12439       || val > (maxtiny << op->shift))
12440     return 1;
12441   else
12442     return 0;
12443 }
12444
12445 /* Estimate the size of a frag before relaxing.  Unless this is the
12446    mips16, we are not really relaxing here, and the final size is
12447    encoded in the subtype information.  For the mips16, we have to
12448    decide whether we are using an extended opcode or not.  */
12449
12450 int
12451 md_estimate_size_before_relax (fragp, segtype)
12452      fragS *fragp;
12453      asection *segtype;
12454 {
12455   int change = 0;
12456   boolean linkonce = false;
12457
12458   if (RELAX_MIPS16_P (fragp->fr_subtype))
12459     /* We don't want to modify the EXTENDED bit here; it might get us
12460        into infinite loops.  We change it only in mips_relax_frag().  */
12461     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12462
12463   if (mips_pic == NO_PIC)
12464     {
12465       change = nopic_need_relax (fragp->fr_symbol, 0);
12466     }
12467   else if (mips_pic == SVR4_PIC)
12468     {
12469       symbolS *sym;
12470       asection *symsec;
12471
12472       sym = fragp->fr_symbol;
12473
12474       /* Handle the case of a symbol equated to another symbol.  */
12475       while (symbol_equated_reloc_p (sym))
12476         {
12477           symbolS *n;
12478
12479           /* It's possible to get a loop here in a badly written
12480              program.  */
12481           n = symbol_get_value_expression (sym)->X_add_symbol;
12482           if (n == sym)
12483             break;
12484           sym = n;
12485         }
12486
12487       symsec = S_GET_SEGMENT (sym);
12488
12489       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12490       if (symsec != segtype && ! S_IS_LOCAL (sym))
12491         {
12492           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12493               != 0)
12494             linkonce = true;
12495
12496           /* The GNU toolchain uses an extension for ELF: a section
12497              beginning with the magic string .gnu.linkonce is a linkonce
12498              section.  */
12499           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12500                        sizeof ".gnu.linkonce" - 1) == 0)
12501             linkonce = true;
12502         }
12503
12504       /* This must duplicate the test in adjust_reloc_syms.  */
12505       change = (symsec != &bfd_und_section
12506                 && symsec != &bfd_abs_section
12507                 && ! bfd_is_com_section (symsec)
12508                 && !linkonce
12509 #ifdef OBJ_ELF
12510                 /* A global or weak symbol is treated as external.  */
12511                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12512                     || (! S_IS_WEAK (sym)
12513                         && (! S_IS_EXTERNAL (sym)
12514                             || mips_pic == EMBEDDED_PIC)))
12515 #endif
12516                 );
12517     }
12518   else
12519     abort ();
12520
12521   if (change)
12522     {
12523       /* Record the offset to the first reloc in the fr_opcode field.
12524          This lets md_convert_frag and tc_gen_reloc know that the code
12525          must be expanded.  */
12526       fragp->fr_opcode = (fragp->fr_literal
12527                           + fragp->fr_fix
12528                           - RELAX_OLD (fragp->fr_subtype)
12529                           + RELAX_RELOC1 (fragp->fr_subtype));
12530       /* FIXME: This really needs as_warn_where.  */
12531       if (RELAX_WARN (fragp->fr_subtype))
12532         as_warn (_("AT used after \".set noat\" or macro used after "
12533                    "\".set nomacro\""));
12534
12535       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12536     }
12537
12538   return 0;
12539 }
12540
12541 /* This is called to see whether a reloc against a defined symbol
12542    should be converted into a reloc against a section.  Don't adjust
12543    MIPS16 jump relocations, so we don't have to worry about the format
12544    of the offset in the .o file.  Don't adjust relocations against
12545    mips16 symbols, so that the linker can find them if it needs to set
12546    up a stub.  */
12547
12548 int
12549 mips_fix_adjustable (fixp)
12550      fixS *fixp;
12551 {
12552 #ifdef OBJ_ELF
12553   /* Prevent all adjustments to global symbols.  */
12554   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12555       && mips_pic != EMBEDDED_PIC
12556       && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12557     return 0;
12558 #endif
12559   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12560     return 0;
12561   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12562       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12563     return 0;
12564   if (fixp->fx_addsy == NULL)
12565     return 1;
12566 #ifdef OBJ_ELF
12567   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12568       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12569       && fixp->fx_subsy == NULL)
12570     return 0;
12571 #endif
12572   return 1;
12573 }
12574
12575 /* Translate internal representation of relocation info to BFD target
12576    format.  */
12577
12578 arelent **
12579 tc_gen_reloc (section, fixp)
12580      asection *section ATTRIBUTE_UNUSED;
12581      fixS *fixp;
12582 {
12583   static arelent *retval[4];
12584   arelent *reloc;
12585   bfd_reloc_code_real_type code;
12586
12587   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12588   retval[1] = NULL;
12589
12590   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12591   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12592   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12593
12594   if (mips_pic == EMBEDDED_PIC
12595       && SWITCH_TABLE (fixp))
12596     {
12597       /* For a switch table entry we use a special reloc.  The addend
12598          is actually the difference between the reloc address and the
12599          subtrahend.  */
12600       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12601       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12602         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12603       fixp->fx_r_type = BFD_RELOC_GPREL32;
12604     }
12605   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12606     {
12607       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12608         reloc->addend = fixp->fx_addnumber;
12609       else
12610         {
12611           /* We use a special addend for an internal RELLO reloc.  */
12612           if (symbol_section_p (fixp->fx_addsy))
12613             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12614           else
12615             reloc->addend = fixp->fx_addnumber + reloc->address;
12616         }
12617     }
12618   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12619     {
12620       assert (fixp->fx_next != NULL
12621               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12622
12623       /* The reloc is relative to the RELLO; adjust the addend
12624          accordingly.  */
12625       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12626         reloc->addend = fixp->fx_next->fx_addnumber;
12627       else
12628         {
12629           /* We use a special addend for an internal RELHI reloc.  */
12630           if (symbol_section_p (fixp->fx_addsy))
12631             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12632                              + fixp->fx_next->fx_where
12633                              - S_GET_VALUE (fixp->fx_subsy));
12634           else
12635             reloc->addend = (fixp->fx_addnumber
12636                              + fixp->fx_next->fx_frag->fr_address
12637                              + fixp->fx_next->fx_where);
12638         }
12639     }
12640   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12641     reloc->addend = fixp->fx_addnumber;
12642   else
12643     {
12644       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12645         /* A gruesome hack which is a result of the gruesome gas reloc
12646            handling.  */
12647         reloc->addend = reloc->address;
12648       else
12649         reloc->addend = -reloc->address;
12650     }
12651
12652   /* If this is a variant frag, we may need to adjust the existing
12653      reloc and generate a new one.  */
12654   if (fixp->fx_frag->fr_opcode != NULL
12655       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12656           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12657           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12658           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12659           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12660           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12661           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12662       && ! HAVE_NEWABI)
12663     {
12664       arelent *reloc2;
12665
12666       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12667
12668       /* If this is not the last reloc in this frag, then we have two
12669          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12670          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12671          the second one handle all of them.  */
12672       if (fixp->fx_next != NULL
12673           && fixp->fx_frag == fixp->fx_next->fx_frag)
12674         {
12675           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12676                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12677                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12678                       && (fixp->fx_next->fx_r_type
12679                           == BFD_RELOC_MIPS_GOT_LO16))
12680                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12681                       && (fixp->fx_next->fx_r_type
12682                           == BFD_RELOC_MIPS_CALL_LO16)));
12683           retval[0] = NULL;
12684           return retval;
12685         }
12686
12687       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12688       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12689       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12690       retval[2] = NULL;
12691       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12692       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12693       reloc2->address = (reloc->address
12694                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12695                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12696       reloc2->addend = fixp->fx_addnumber;
12697       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12698       assert (reloc2->howto != NULL);
12699
12700       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12701         {
12702           arelent *reloc3;
12703
12704           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12705           retval[3] = NULL;
12706           *reloc3 = *reloc2;
12707           reloc3->address += 4;
12708         }
12709
12710       if (mips_pic == NO_PIC)
12711         {
12712           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12713           fixp->fx_r_type = BFD_RELOC_HI16_S;
12714         }
12715       else if (mips_pic == SVR4_PIC)
12716         {
12717           switch (fixp->fx_r_type)
12718             {
12719             default:
12720               abort ();
12721             case BFD_RELOC_MIPS_GOT16:
12722               break;
12723             case BFD_RELOC_MIPS_CALL16:
12724             case BFD_RELOC_MIPS_GOT_LO16:
12725             case BFD_RELOC_MIPS_CALL_LO16:
12726               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12727               break;
12728             }
12729         }
12730       else
12731         abort ();
12732     }
12733
12734   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12735      entry to be used in the relocation's section offset.  */
12736   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12737     {
12738       reloc->address = reloc->addend;
12739       reloc->addend = 0;
12740     }
12741
12742   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12743      fixup_segment converted a non-PC relative reloc into a PC
12744      relative reloc.  In such a case, we need to convert the reloc
12745      code.  */
12746   code = fixp->fx_r_type;
12747   if (fixp->fx_pcrel)
12748     {
12749       switch (code)
12750         {
12751         case BFD_RELOC_8:
12752           code = BFD_RELOC_8_PCREL;
12753           break;
12754         case BFD_RELOC_16:
12755           code = BFD_RELOC_16_PCREL;
12756           break;
12757         case BFD_RELOC_32:
12758           code = BFD_RELOC_32_PCREL;
12759           break;
12760         case BFD_RELOC_64:
12761           code = BFD_RELOC_64_PCREL;
12762           break;
12763         case BFD_RELOC_8_PCREL:
12764         case BFD_RELOC_16_PCREL:
12765         case BFD_RELOC_32_PCREL:
12766         case BFD_RELOC_64_PCREL:
12767         case BFD_RELOC_16_PCREL_S2:
12768         case BFD_RELOC_PCREL_HI16_S:
12769         case BFD_RELOC_PCREL_LO16:
12770           break;
12771         default:
12772           as_bad_where (fixp->fx_file, fixp->fx_line,
12773                         _("Cannot make %s relocation PC relative"),
12774                         bfd_get_reloc_code_name (code));
12775         }
12776     }
12777
12778 #ifdef OBJ_ELF
12779   /* md_apply_fix3 has a double-subtraction hack to get
12780      bfd_install_relocation to behave nicely.  GPREL relocations are
12781      handled correctly without this hack, so undo it here.  We can't
12782      stop md_apply_fix3 from subtracting twice in the first place since
12783      the fake addend is required for variant frags above.  */
12784   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12785       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
12786       && reloc->addend != 0
12787       && mips_need_elf_addend_fixup (fixp))
12788     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12789 #endif
12790
12791   /* To support a PC relative reloc when generating embedded PIC code
12792      for ECOFF, we use a Cygnus extension.  We check for that here to
12793      make sure that we don't let such a reloc escape normally.  */
12794   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12795        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12796       && code == BFD_RELOC_16_PCREL_S2
12797       && mips_pic != EMBEDDED_PIC)
12798     reloc->howto = NULL;
12799   else
12800     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12801
12802   if (reloc->howto == NULL)
12803     {
12804       as_bad_where (fixp->fx_file, fixp->fx_line,
12805                     _("Can not represent %s relocation in this object file format"),
12806                     bfd_get_reloc_code_name (code));
12807       retval[0] = NULL;
12808     }
12809
12810   return retval;
12811 }
12812
12813 /* Relax a machine dependent frag.  This returns the amount by which
12814    the current size of the frag should change.  */
12815
12816 int
12817 mips_relax_frag (fragp, stretch)
12818      fragS *fragp;
12819      long stretch;
12820 {
12821   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12822     return 0;
12823
12824   if (mips16_extended_frag (fragp, NULL, stretch))
12825     {
12826       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12827         return 0;
12828       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12829       return 2;
12830     }
12831   else
12832     {
12833       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12834         return 0;
12835       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12836       return -2;
12837     }
12838
12839   return 0;
12840 }
12841
12842 /* Convert a machine dependent frag.  */
12843
12844 void
12845 md_convert_frag (abfd, asec, fragp)
12846      bfd *abfd ATTRIBUTE_UNUSED;
12847      segT asec;
12848      fragS *fragp;
12849 {
12850   int old, new;
12851   char *fixptr;
12852
12853   if (RELAX_MIPS16_P (fragp->fr_subtype))
12854     {
12855       int type;
12856       register const struct mips16_immed_operand *op;
12857       boolean small, ext;
12858       offsetT val;
12859       bfd_byte *buf;
12860       unsigned long insn;
12861       boolean use_extend;
12862       unsigned short extend;
12863
12864       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12865       op = mips16_immed_operands;
12866       while (op->type != type)
12867         ++op;
12868
12869       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12870         {
12871           small = false;
12872           ext = true;
12873         }
12874       else
12875         {
12876           small = true;
12877           ext = false;
12878         }
12879
12880       resolve_symbol_value (fragp->fr_symbol);
12881       val = S_GET_VALUE (fragp->fr_symbol);
12882       if (op->pcrel)
12883         {
12884           addressT addr;
12885
12886           addr = fragp->fr_address + fragp->fr_fix;
12887
12888           /* The rules for the base address of a PC relative reloc are
12889              complicated; see mips16_extended_frag.  */
12890           if (type == 'p' || type == 'q')
12891             {
12892               addr += 2;
12893               if (ext)
12894                 addr += 2;
12895               /* Ignore the low bit in the target, since it will be
12896                  set for a text label.  */
12897               if ((val & 1) != 0)
12898                 --val;
12899             }
12900           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12901             addr -= 4;
12902           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12903             addr -= 2;
12904
12905           addr &= ~ (addressT) ((1 << op->shift) - 1);
12906           val -= addr;
12907
12908           /* Make sure the section winds up with the alignment we have
12909              assumed.  */
12910           if (op->shift > 0)
12911             record_alignment (asec, op->shift);
12912         }
12913
12914       if (ext
12915           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12916               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12917         as_warn_where (fragp->fr_file, fragp->fr_line,
12918                        _("extended instruction in delay slot"));
12919
12920       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12921
12922       if (target_big_endian)
12923         insn = bfd_getb16 (buf);
12924       else
12925         insn = bfd_getl16 (buf);
12926
12927       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12928                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12929                     small, ext, &insn, &use_extend, &extend);
12930
12931       if (use_extend)
12932         {
12933           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
12934           fragp->fr_fix += 2;
12935           buf += 2;
12936         }
12937
12938       md_number_to_chars ((char *) buf, insn, 2);
12939       fragp->fr_fix += 2;
12940       buf += 2;
12941     }
12942   else
12943     {
12944       if (fragp->fr_opcode == NULL)
12945         return;
12946
12947       old = RELAX_OLD (fragp->fr_subtype);
12948       new = RELAX_NEW (fragp->fr_subtype);
12949       fixptr = fragp->fr_literal + fragp->fr_fix;
12950
12951       if (new > 0)
12952         memcpy (fixptr - old, fixptr, new);
12953
12954       fragp->fr_fix += new - old;
12955     }
12956 }
12957
12958 #ifdef OBJ_ELF
12959
12960 /* This function is called after the relocs have been generated.
12961    We've been storing mips16 text labels as odd.  Here we convert them
12962    back to even for the convenience of the debugger.  */
12963
12964 void
12965 mips_frob_file_after_relocs ()
12966 {
12967   asymbol **syms;
12968   unsigned int count, i;
12969
12970   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12971     return;
12972
12973   syms = bfd_get_outsymbols (stdoutput);
12974   count = bfd_get_symcount (stdoutput);
12975   for (i = 0; i < count; i++, syms++)
12976     {
12977       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12978           && ((*syms)->value & 1) != 0)
12979         {
12980           (*syms)->value &= ~1;
12981           /* If the symbol has an odd size, it was probably computed
12982              incorrectly, so adjust that as well.  */
12983           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12984             ++elf_symbol (*syms)->internal_elf_sym.st_size;
12985         }
12986     }
12987 }
12988
12989 #endif
12990
12991 /* This function is called whenever a label is defined.  It is used
12992    when handling branch delays; if a branch has a label, we assume we
12993    can not move it.  */
12994
12995 void
12996 mips_define_label (sym)
12997      symbolS *sym;
12998 {
12999   struct insn_label_list *l;
13000
13001   if (free_insn_labels == NULL)
13002     l = (struct insn_label_list *) xmalloc (sizeof *l);
13003   else
13004     {
13005       l = free_insn_labels;
13006       free_insn_labels = l->next;
13007     }
13008
13009   l->label = sym;
13010   l->next = insn_labels;
13011   insn_labels = l;
13012 }
13013 \f
13014 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13015
13016 /* Some special processing for a MIPS ELF file.  */
13017
13018 void
13019 mips_elf_final_processing ()
13020 {
13021   /* Write out the register information.  */
13022   if (mips_abi != N64_ABI)
13023     {
13024       Elf32_RegInfo s;
13025
13026       s.ri_gprmask = mips_gprmask;
13027       s.ri_cprmask[0] = mips_cprmask[0];
13028       s.ri_cprmask[1] = mips_cprmask[1];
13029       s.ri_cprmask[2] = mips_cprmask[2];
13030       s.ri_cprmask[3] = mips_cprmask[3];
13031       /* The gp_value field is set by the MIPS ELF backend.  */
13032
13033       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13034                                        ((Elf32_External_RegInfo *)
13035                                         mips_regmask_frag));
13036     }
13037   else
13038     {
13039       Elf64_Internal_RegInfo s;
13040
13041       s.ri_gprmask = mips_gprmask;
13042       s.ri_pad = 0;
13043       s.ri_cprmask[0] = mips_cprmask[0];
13044       s.ri_cprmask[1] = mips_cprmask[1];
13045       s.ri_cprmask[2] = mips_cprmask[2];
13046       s.ri_cprmask[3] = mips_cprmask[3];
13047       /* The gp_value field is set by the MIPS ELF backend.  */
13048
13049       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13050                                        ((Elf64_External_RegInfo *)
13051                                         mips_regmask_frag));
13052     }
13053
13054   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13055      sort of BFD interface for this.  */
13056   if (mips_any_noreorder)
13057     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13058   if (mips_pic != NO_PIC)
13059     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13060
13061   /* Set MIPS ELF flags for ASEs.  */
13062   if (file_ase_mips16)
13063     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13064 #if 0 /* XXX FIXME */
13065   if (file_ase_mips3d)
13066     elf_elfheader (stdoutput)->e_flags |= ???;
13067 #endif
13068   if (file_ase_mdmx)
13069     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13070
13071   /* Set the MIPS ELF ABI flags.  */
13072   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13073     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13074   else if (mips_abi == O64_ABI)
13075     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13076   else if (mips_abi == EABI_ABI)
13077     {
13078       if (!file_mips_gp32)
13079         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13080       else
13081         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13082     }
13083   else if (mips_abi == N32_ABI)
13084     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13085
13086   /* Nothing to do for N64_ABI.  */
13087
13088   if (mips_32bitmode)
13089     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13090 }
13091
13092 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13093 \f
13094 typedef struct proc {
13095   symbolS *isym;
13096   unsigned long reg_mask;
13097   unsigned long reg_offset;
13098   unsigned long fpreg_mask;
13099   unsigned long fpreg_offset;
13100   unsigned long frame_offset;
13101   unsigned long frame_reg;
13102   unsigned long pc_reg;
13103 } procS;
13104
13105 static procS cur_proc;
13106 static procS *cur_proc_ptr;
13107 static int numprocs;
13108
13109 /* Fill in an rs_align_code fragment.  */
13110
13111 void
13112 mips_handle_align (fragp)
13113      fragS *fragp;
13114 {
13115   if (fragp->fr_type != rs_align_code)
13116     return;
13117
13118   if (mips_opts.mips16)
13119     {
13120       static const unsigned char be_nop[] = { 0x65, 0x00 };
13121       static const unsigned char le_nop[] = { 0x00, 0x65 };
13122
13123       int bytes;
13124       char *p;
13125
13126       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13127       p = fragp->fr_literal + fragp->fr_fix;
13128
13129       if (bytes & 1)
13130         {
13131           *p++ = 0;
13132           fragp->fr_fix++;
13133         }
13134
13135       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13136       fragp->fr_var = 2;
13137     }
13138
13139   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13140 }
13141
13142 static void
13143 md_obj_begin ()
13144 {
13145 }
13146
13147 static void
13148 md_obj_end ()
13149 {
13150   /* check for premature end, nesting errors, etc */
13151   if (cur_proc_ptr)
13152     as_warn (_("missing .end at end of assembly"));
13153 }
13154
13155 static long
13156 get_number ()
13157 {
13158   int negative = 0;
13159   long val = 0;
13160
13161   if (*input_line_pointer == '-')
13162     {
13163       ++input_line_pointer;
13164       negative = 1;
13165     }
13166   if (!ISDIGIT (*input_line_pointer))
13167     as_bad (_("expected simple number"));
13168   if (input_line_pointer[0] == '0')
13169     {
13170       if (input_line_pointer[1] == 'x')
13171         {
13172           input_line_pointer += 2;
13173           while (ISXDIGIT (*input_line_pointer))
13174             {
13175               val <<= 4;
13176               val |= hex_value (*input_line_pointer++);
13177             }
13178           return negative ? -val : val;
13179         }
13180       else
13181         {
13182           ++input_line_pointer;
13183           while (ISDIGIT (*input_line_pointer))
13184             {
13185               val <<= 3;
13186               val |= *input_line_pointer++ - '0';
13187             }
13188           return negative ? -val : val;
13189         }
13190     }
13191   if (!ISDIGIT (*input_line_pointer))
13192     {
13193       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13194               *input_line_pointer, *input_line_pointer);
13195       as_warn (_("invalid number"));
13196       return -1;
13197     }
13198   while (ISDIGIT (*input_line_pointer))
13199     {
13200       val *= 10;
13201       val += *input_line_pointer++ - '0';
13202     }
13203   return negative ? -val : val;
13204 }
13205
13206 /* The .file directive; just like the usual .file directive, but there
13207    is an initial number which is the ECOFF file index.  In the non-ECOFF
13208    case .file implies DWARF-2.  */
13209
13210 static void
13211 s_mips_file (x)
13212      int x ATTRIBUTE_UNUSED;
13213 {
13214   static int first_file_directive = 0;
13215
13216   if (ECOFF_DEBUGGING)
13217     {
13218       get_number ();
13219       s_app_file (0);
13220     }
13221   else
13222     {
13223       char *filename;
13224
13225       filename = dwarf2_directive_file (0);
13226
13227       /* Versions of GCC up to 3.1 start files with a ".file"
13228          directive even for stabs output.  Make sure that this
13229          ".file" is handled.  Note that you need a version of GCC
13230          after 3.1 in order to support DWARF-2 on MIPS.  */
13231       if (filename != NULL && ! first_file_directive)
13232         {
13233           (void) new_logical_line (filename, -1);
13234           s_app_file_string (filename);
13235         }
13236       first_file_directive = 1;
13237     }
13238 }
13239
13240 /* The .loc directive, implying DWARF-2.  */
13241
13242 static void
13243 s_mips_loc (x)
13244      int x ATTRIBUTE_UNUSED;
13245 {
13246   if (!ECOFF_DEBUGGING)
13247     dwarf2_directive_loc (0);
13248 }
13249
13250 /* The .end directive.  */
13251
13252 static void
13253 s_mips_end (x)
13254      int x ATTRIBUTE_UNUSED;
13255 {
13256   symbolS *p;
13257   int maybe_text;
13258
13259   /* Following functions need their own .frame and .cprestore directives.  */
13260   mips_frame_reg_valid = 0;
13261   mips_cprestore_valid = 0;
13262
13263   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13264     {
13265       p = get_symbol ();
13266       demand_empty_rest_of_line ();
13267     }
13268   else
13269     p = NULL;
13270
13271 #ifdef BFD_ASSEMBLER
13272   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13273     maybe_text = 1;
13274   else
13275     maybe_text = 0;
13276 #else
13277   if (now_seg != data_section && now_seg != bss_section)
13278     maybe_text = 1;
13279   else
13280     maybe_text = 0;
13281 #endif
13282
13283   if (!maybe_text)
13284     as_warn (_(".end not in text section"));
13285
13286   if (!cur_proc_ptr)
13287     {
13288       as_warn (_(".end directive without a preceding .ent directive."));
13289       demand_empty_rest_of_line ();
13290       return;
13291     }
13292
13293   if (p != NULL)
13294     {
13295       assert (S_GET_NAME (p));
13296       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13297         as_warn (_(".end symbol does not match .ent symbol."));
13298
13299       if (debug_type == DEBUG_STABS)
13300         stabs_generate_asm_endfunc (S_GET_NAME (p),
13301                                     S_GET_NAME (p));
13302     }
13303   else
13304     as_warn (_(".end directive missing or unknown symbol"));
13305
13306 #ifdef OBJ_ELF
13307   /* Generate a .pdr section.  */
13308   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13309     {
13310       segT saved_seg = now_seg;
13311       subsegT saved_subseg = now_subseg;
13312       valueT dot;
13313       expressionS exp;
13314       char *fragp;
13315
13316       dot = frag_now_fix ();
13317
13318 #ifdef md_flush_pending_output
13319       md_flush_pending_output ();
13320 #endif
13321
13322       assert (pdr_seg);
13323       subseg_set (pdr_seg, 0);
13324
13325       /* Write the symbol.  */
13326       exp.X_op = O_symbol;
13327       exp.X_add_symbol = p;
13328       exp.X_add_number = 0;
13329       emit_expr (&exp, 4);
13330
13331       fragp = frag_more (7 * 4);
13332
13333       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13334       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13335       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13336       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13337       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13338       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13339       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13340
13341       subseg_set (saved_seg, saved_subseg);
13342     }
13343 #endif /* OBJ_ELF */
13344
13345   cur_proc_ptr = NULL;
13346 }
13347
13348 /* The .aent and .ent directives.  */
13349
13350 static void
13351 s_mips_ent (aent)
13352      int aent;
13353 {
13354   symbolS *symbolP;
13355   int maybe_text;
13356
13357   symbolP = get_symbol ();
13358   if (*input_line_pointer == ',')
13359     ++input_line_pointer;
13360   SKIP_WHITESPACE ();
13361   if (ISDIGIT (*input_line_pointer)
13362       || *input_line_pointer == '-')
13363     get_number ();
13364
13365 #ifdef BFD_ASSEMBLER
13366   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13367     maybe_text = 1;
13368   else
13369     maybe_text = 0;
13370 #else
13371   if (now_seg != data_section && now_seg != bss_section)
13372     maybe_text = 1;
13373   else
13374     maybe_text = 0;
13375 #endif
13376
13377   if (!maybe_text)
13378     as_warn (_(".ent or .aent not in text section."));
13379
13380   if (!aent && cur_proc_ptr)
13381     as_warn (_("missing .end"));
13382
13383   if (!aent)
13384     {
13385       /* This function needs its own .frame and .cprestore directives.  */
13386       mips_frame_reg_valid = 0;
13387       mips_cprestore_valid = 0;
13388
13389       cur_proc_ptr = &cur_proc;
13390       memset (cur_proc_ptr, '\0', sizeof (procS));
13391
13392       cur_proc_ptr->isym = symbolP;
13393
13394       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13395
13396       ++numprocs;
13397
13398       if (debug_type == DEBUG_STABS)
13399         stabs_generate_asm_func (S_GET_NAME (symbolP),
13400                                  S_GET_NAME (symbolP));
13401     }
13402
13403   demand_empty_rest_of_line ();
13404 }
13405
13406 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13407    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13408    s_mips_frame is used so that we can set the PDR information correctly.
13409    We can't use the ecoff routines because they make reference to the ecoff
13410    symbol table (in the mdebug section).  */
13411
13412 static void
13413 s_mips_frame (ignore)
13414      int ignore ATTRIBUTE_UNUSED;
13415 {
13416 #ifdef OBJ_ELF
13417   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13418     {
13419       long val;
13420
13421       if (cur_proc_ptr == (procS *) NULL)
13422         {
13423           as_warn (_(".frame outside of .ent"));
13424           demand_empty_rest_of_line ();
13425           return;
13426         }
13427
13428       cur_proc_ptr->frame_reg = tc_get_register (1);
13429
13430       SKIP_WHITESPACE ();
13431       if (*input_line_pointer++ != ','
13432           || get_absolute_expression_and_terminator (&val) != ',')
13433         {
13434           as_warn (_("Bad .frame directive"));
13435           --input_line_pointer;
13436           demand_empty_rest_of_line ();
13437           return;
13438         }
13439
13440       cur_proc_ptr->frame_offset = val;
13441       cur_proc_ptr->pc_reg = tc_get_register (0);
13442
13443       demand_empty_rest_of_line ();
13444     }
13445   else
13446 #endif /* OBJ_ELF */
13447     s_ignore (ignore);
13448 }
13449
13450 /* The .fmask and .mask directives. If the mdebug section is present
13451    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13452    embedded targets, s_mips_mask is used so that we can set the PDR
13453    information correctly. We can't use the ecoff routines because they
13454    make reference to the ecoff symbol table (in the mdebug section).  */
13455
13456 static void
13457 s_mips_mask (reg_type)
13458      char reg_type;
13459 {
13460 #ifdef OBJ_ELF
13461   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13462     {
13463       long mask, off;
13464
13465       if (cur_proc_ptr == (procS *) NULL)
13466         {
13467           as_warn (_(".mask/.fmask outside of .ent"));
13468           demand_empty_rest_of_line ();
13469           return;
13470         }
13471
13472       if (get_absolute_expression_and_terminator (&mask) != ',')
13473         {
13474           as_warn (_("Bad .mask/.fmask directive"));
13475           --input_line_pointer;
13476           demand_empty_rest_of_line ();
13477           return;
13478         }
13479
13480       off = get_absolute_expression ();
13481
13482       if (reg_type == 'F')
13483         {
13484           cur_proc_ptr->fpreg_mask = mask;
13485           cur_proc_ptr->fpreg_offset = off;
13486         }
13487       else
13488         {
13489           cur_proc_ptr->reg_mask = mask;
13490           cur_proc_ptr->reg_offset = off;
13491         }
13492
13493       demand_empty_rest_of_line ();
13494     }
13495   else
13496 #endif /* OBJ_ELF */
13497     s_ignore (reg_type);
13498 }
13499
13500 /* The .loc directive.  */
13501
13502 #if 0
13503 static void
13504 s_loc (x)
13505      int x;
13506 {
13507   symbolS *symbolP;
13508   int lineno;
13509   int addroff;
13510
13511   assert (now_seg == text_section);
13512
13513   lineno = get_number ();
13514   addroff = frag_now_fix ();
13515
13516   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13517   S_SET_TYPE (symbolP, N_SLINE);
13518   S_SET_OTHER (symbolP, 0);
13519   S_SET_DESC (symbolP, lineno);
13520   symbolP->sy_segment = now_seg;
13521 }
13522 #endif
13523
13524 /* A table describing all the processors gas knows about.  Names are
13525    matched in the order listed.
13526
13527    To ease comparison, please keep this table in the same order as
13528    gcc's mips_cpu_info_table[].  */
13529 static const struct mips_cpu_info mips_cpu_info_table[] =
13530 {
13531   /* Entries for generic ISAs */
13532   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
13533   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
13534   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
13535   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
13536   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
13537   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
13538   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
13539
13540   /* MIPS I */
13541   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
13542   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
13543   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
13544
13545   /* MIPS II */
13546   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
13547
13548   /* MIPS III */
13549   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
13550   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
13551   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
13552   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
13553   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
13554   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
13555   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
13556   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
13557   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
13558
13559   /* MIPS IV */
13560   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
13561   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
13562   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
13563   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
13564   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
13565   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
13566   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
13567   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
13568   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
13569   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
13570
13571   /* MIPS 32 */
13572   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13573   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
13574   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
13575
13576   /* MIPS 64 */
13577   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
13578   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
13579
13580   /* Broadcom SB-1 CPU core */
13581   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
13582
13583   /* End marker */
13584   { NULL, 0, 0, 0 }
13585 };
13586
13587
13588 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13589    with a final "000" replaced by "k".  Ignore case.
13590
13591    Note: this function is shared between GCC and GAS.  */
13592
13593 static boolean
13594 mips_strict_matching_cpu_name_p (canonical, given)
13595      const char *canonical, *given;
13596 {
13597   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13598     given++, canonical++;
13599
13600   return ((*given == 0 && *canonical == 0)
13601           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13602 }
13603
13604
13605 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13606    CPU name.  We've traditionally allowed a lot of variation here.
13607
13608    Note: this function is shared between GCC and GAS.  */
13609
13610 static boolean
13611 mips_matching_cpu_name_p (canonical, given)
13612      const char *canonical, *given;
13613 {
13614   /* First see if the name matches exactly, or with a final "000"
13615      turned into "k".  */
13616   if (mips_strict_matching_cpu_name_p (canonical, given))
13617     return true;
13618
13619   /* If not, try comparing based on numerical designation alone.
13620      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
13621   if (TOLOWER (*given) == 'r')
13622     given++;
13623   if (!ISDIGIT (*given))
13624     return false;
13625
13626   /* Skip over some well-known prefixes in the canonical name,
13627      hoping to find a number there too.  */
13628   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13629     canonical += 2;
13630   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13631     canonical += 2;
13632   else if (TOLOWER (canonical[0]) == 'r')
13633     canonical += 1;
13634
13635   return mips_strict_matching_cpu_name_p (canonical, given);
13636 }
13637
13638
13639 /* Parse an option that takes the name of a processor as its argument.
13640    OPTION is the name of the option and CPU_STRING is the argument.
13641    Return the corresponding processor enumeration if the CPU_STRING is
13642    recognized, otherwise report an error and return null.
13643
13644    A similar function exists in GCC.  */
13645
13646 static const struct mips_cpu_info *
13647 mips_parse_cpu (option, cpu_string)
13648      const char *option, *cpu_string;
13649 {
13650   const struct mips_cpu_info *p;
13651
13652   /* 'from-abi' selects the most compatible architecture for the given
13653      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
13654      EABIs, we have to decide whether we're using the 32-bit or 64-bit
13655      version.  Look first at the -mgp options, if given, otherwise base
13656      the choice on MIPS_DEFAULT_64BIT.
13657
13658      Treat NO_ABI like the EABIs.  One reason to do this is that the
13659      plain 'mips' and 'mips64' configs have 'from-abi' as their default
13660      architecture.  This code picks MIPS I for 'mips' and MIPS III for
13661      'mips64', just as we did in the days before 'from-abi'.  */
13662   if (strcasecmp (cpu_string, "from-abi") == 0)
13663     {
13664       if (ABI_NEEDS_32BIT_REGS (mips_abi))
13665         return mips_cpu_info_from_isa (ISA_MIPS1);
13666
13667       if (ABI_NEEDS_64BIT_REGS (mips_abi))
13668         return mips_cpu_info_from_isa (ISA_MIPS3);
13669
13670       if (file_mips_gp32 >= 0)
13671         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13672
13673       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13674                                      ? ISA_MIPS3
13675                                      : ISA_MIPS1);
13676     }
13677
13678   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
13679   if (strcasecmp (cpu_string, "default") == 0)
13680     return 0;
13681
13682   for (p = mips_cpu_info_table; p->name != 0; p++)
13683     if (mips_matching_cpu_name_p (p->name, cpu_string))
13684       return p;
13685
13686   as_bad ("Bad value (%s) for %s", cpu_string, option);
13687   return 0;
13688 }
13689
13690 /* Return the canonical processor information for ISA (a member of the
13691    ISA_MIPS* enumeration).  */
13692
13693 static const struct mips_cpu_info *
13694 mips_cpu_info_from_isa (isa)
13695      int isa;
13696 {
13697   int i;
13698
13699   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13700     if (mips_cpu_info_table[i].is_isa
13701         && isa == mips_cpu_info_table[i].isa)
13702       return (&mips_cpu_info_table[i]);
13703
13704   return NULL;
13705 }
13706 \f
13707 static void
13708 show (stream, string, col_p, first_p)
13709      FILE *stream;
13710      const char *string;
13711      int *col_p;
13712      int *first_p;
13713 {
13714   if (*first_p)
13715     {
13716       fprintf (stream, "%24s", "");
13717       *col_p = 24;
13718     }
13719   else
13720     {
13721       fprintf (stream, ", ");
13722       *col_p += 2;
13723     }
13724
13725   if (*col_p + strlen (string) > 72)
13726     {
13727       fprintf (stream, "\n%24s", "");
13728       *col_p = 24;
13729     }
13730
13731   fprintf (stream, "%s", string);
13732   *col_p += strlen (string);
13733
13734   *first_p = 0;
13735 }
13736
13737 void
13738 md_show_usage (stream)
13739      FILE *stream;
13740 {
13741   int column, first;
13742   size_t i;
13743
13744   fprintf (stream, _("\
13745 MIPS options:\n\
13746 -membedded-pic          generate embedded position independent code\n\
13747 -EB                     generate big endian output\n\
13748 -EL                     generate little endian output\n\
13749 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
13750 -G NUM                  allow referencing objects up to NUM bytes\n\
13751                         implicitly with the gp register [default 8]\n"));
13752   fprintf (stream, _("\
13753 -mips1                  generate MIPS ISA I instructions\n\
13754 -mips2                  generate MIPS ISA II instructions\n\
13755 -mips3                  generate MIPS ISA III instructions\n\
13756 -mips4                  generate MIPS ISA IV instructions\n\
13757 -mips5                  generate MIPS ISA V instructions\n\
13758 -mips32                 generate MIPS32 ISA instructions\n\
13759 -mips64                 generate MIPS64 ISA instructions\n\
13760 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
13761
13762   first = 1;
13763
13764   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13765     show (stream, mips_cpu_info_table[i].name, &column, &first);
13766   show (stream, "from-abi", &column, &first);
13767   fputc ('\n', stream);
13768
13769   fprintf (stream, _("\
13770 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13771 -no-mCPU                don't generate code specific to CPU.\n\
13772                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
13773
13774   first = 1;
13775
13776   show (stream, "3900", &column, &first);
13777   show (stream, "4010", &column, &first);
13778   show (stream, "4100", &column, &first);
13779   show (stream, "4650", &column, &first);
13780   fputc ('\n', stream);
13781
13782   fprintf (stream, _("\
13783 -mips16                 generate mips16 instructions\n\
13784 -no-mips16              do not generate mips16 instructions\n"));
13785   fprintf (stream, _("\
13786 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
13787 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
13788 -O0                     remove unneeded NOPs, do not swap branches\n\
13789 -O                      remove unneeded NOPs and swap branches\n\
13790 -n                      warn about NOPs generated from macros\n\
13791 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
13792 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
13793 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
13794 #ifdef OBJ_ELF
13795   fprintf (stream, _("\
13796 -KPIC, -call_shared     generate SVR4 position independent code\n\
13797 -non_shared             do not generate position independent code\n\
13798 -xgot                   assume a 32 bit GOT\n\
13799 -mabi=ABI               create ABI conformant object file for:\n"));
13800
13801   first = 1;
13802
13803   show (stream, "32", &column, &first);
13804   show (stream, "o64", &column, &first);
13805   show (stream, "n32", &column, &first);
13806   show (stream, "64", &column, &first);
13807   show (stream, "eabi", &column, &first);
13808
13809   fputc ('\n', stream);
13810
13811   fprintf (stream, _("\
13812 -32                     create o32 ABI object file (default)\n\
13813 -n32                    create n32 ABI object file\n\
13814 -64                     create 64 ABI object file\n"));
13815 #endif
13816 }