* tc-mips.c (load_address): Don't clobber $at when loading a
[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_cons PARAMS ((int));
748 static void s_float_cons PARAMS ((int));
749 static void s_mips_globl PARAMS ((int));
750 static void s_option PARAMS ((int));
751 static void s_mipsset PARAMS ((int));
752 static void s_abicalls PARAMS ((int));
753 static void s_cpload PARAMS ((int));
754 static void s_cpsetup PARAMS ((int));
755 static void s_cplocal PARAMS ((int));
756 static void s_cprestore PARAMS ((int));
757 static void s_cpreturn PARAMS ((int));
758 static void s_gpvalue PARAMS ((int));
759 static void s_gpword PARAMS ((int));
760 static void s_cpadd PARAMS ((int));
761 static void s_insn PARAMS ((int));
762 static void md_obj_begin PARAMS ((void));
763 static void md_obj_end PARAMS ((void));
764 static long get_number PARAMS ((void));
765 static void s_mips_ent PARAMS ((int));
766 static void s_mips_end PARAMS ((int));
767 static void s_mips_frame PARAMS ((int));
768 static void s_mips_mask PARAMS ((int));
769 static void s_mips_stab PARAMS ((int));
770 static void s_mips_weakext PARAMS ((int));
771 static void s_mips_file PARAMS ((int));
772 static void s_mips_loc PARAMS ((int));
773 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
774 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
775 static void show PARAMS ((FILE *, const char *, int *, int *));
776 #ifdef OBJ_ELF
777 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
778 #endif
779
780 /* Return values of my_getSmallExpression().  */
781
782 enum small_ex_type
783 {
784   S_EX_NONE = 0,
785   S_EX_REGISTER,
786
787   /* Direct relocation creation by %percent_op().  */
788   S_EX_HALF,
789   S_EX_HI,
790   S_EX_LO,
791   S_EX_GP_REL,
792   S_EX_GOT,
793   S_EX_CALL16,
794   S_EX_GOT_DISP,
795   S_EX_GOT_PAGE,
796   S_EX_GOT_OFST,
797   S_EX_GOT_HI,
798   S_EX_GOT_LO,
799   S_EX_NEG,
800   S_EX_HIGHER,
801   S_EX_HIGHEST,
802   S_EX_CALL_HI,
803   S_EX_CALL_LO
804 };
805
806 /* Table and functions used to map between CPU/ISA names, and
807    ISA levels, and CPU numbers.  */
808
809 struct mips_cpu_info
810 {
811   const char *name;           /* CPU or ISA name.  */
812   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
813   int isa;                    /* ISA level.  */
814   int cpu;                    /* CPU number (default CPU if ISA).  */
815 };
816
817 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
818 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
819 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
820                                                         const char *));
821 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
822 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
823                                                            const char *));
824 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
825 \f
826 /* Pseudo-op table.
827
828    The following pseudo-ops from the Kane and Heinrich MIPS book
829    should be defined here, but are currently unsupported: .alias,
830    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
831
832    The following pseudo-ops from the Kane and Heinrich MIPS book are
833    specific to the type of debugging information being generated, and
834    should be defined by the object format: .aent, .begin, .bend,
835    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
836    .vreg.
837
838    The following pseudo-ops from the Kane and Heinrich MIPS book are
839    not MIPS CPU specific, but are also not specific to the object file
840    format.  This file is probably the best place to define them, but
841    they are not currently supported: .asm0, .endr, .lab, .repeat,
842    .struct.  */
843
844 static const pseudo_typeS mips_pseudo_table[] =
845 {
846   /* MIPS specific pseudo-ops.  */
847   {"option", s_option, 0},
848   {"set", s_mipsset, 0},
849   {"rdata", s_change_sec, 'r'},
850   {"sdata", s_change_sec, 's'},
851   {"livereg", s_ignore, 0},
852   {"abicalls", s_abicalls, 0},
853   {"cpload", s_cpload, 0},
854   {"cpsetup", s_cpsetup, 0},
855   {"cplocal", s_cplocal, 0},
856   {"cprestore", s_cprestore, 0},
857   {"cpreturn", s_cpreturn, 0},
858   {"gpvalue", s_gpvalue, 0},
859   {"gpword", s_gpword, 0},
860   {"cpadd", s_cpadd, 0},
861   {"insn", s_insn, 0},
862
863   /* Relatively generic pseudo-ops that happen to be used on MIPS
864      chips.  */
865   {"asciiz", stringer, 1},
866   {"bss", s_change_sec, 'b'},
867   {"err", s_err, 0},
868   {"half", s_cons, 1},
869   {"dword", s_cons, 3},
870   {"weakext", s_mips_weakext, 0},
871
872   /* These pseudo-ops are defined in read.c, but must be overridden
873      here for one reason or another.  */
874   {"align", s_align, 0},
875   {"byte", s_cons, 0},
876   {"data", s_change_sec, 'd'},
877   {"double", s_float_cons, 'd'},
878   {"float", s_float_cons, 'f'},
879   {"globl", s_mips_globl, 0},
880   {"global", s_mips_globl, 0},
881   {"hword", s_cons, 1},
882   {"int", s_cons, 2},
883   {"long", s_cons, 2},
884   {"octa", s_cons, 4},
885   {"quad", s_cons, 3},
886   {"short", s_cons, 1},
887   {"single", s_float_cons, 'f'},
888   {"stabn", s_mips_stab, 'n'},
889   {"text", s_change_sec, 't'},
890   {"word", s_cons, 2},
891
892   { "extern", ecoff_directive_extern, 0},
893
894   { NULL, NULL, 0 },
895 };
896
897 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
898 {
899   /* These pseudo-ops should be defined by the object file format.
900      However, a.out doesn't support them, so we have versions here.  */
901   {"aent", s_mips_ent, 1},
902   {"bgnb", s_ignore, 0},
903   {"end", s_mips_end, 0},
904   {"endb", s_ignore, 0},
905   {"ent", s_mips_ent, 0},
906   {"file", s_mips_file, 0},
907   {"fmask", s_mips_mask, 'F'},
908   {"frame", s_mips_frame, 0},
909   {"loc", s_mips_loc, 0},
910   {"mask", s_mips_mask, 'R'},
911   {"verstamp", s_ignore, 0},
912   { NULL, NULL, 0 },
913 };
914
915 extern void pop_insert PARAMS ((const pseudo_typeS *));
916
917 void
918 mips_pop_insert ()
919 {
920   pop_insert (mips_pseudo_table);
921   if (! ECOFF_DEBUGGING)
922     pop_insert (mips_nonecoff_pseudo_table);
923 }
924 \f
925 /* Symbols labelling the current insn.  */
926
927 struct insn_label_list
928 {
929   struct insn_label_list *next;
930   symbolS *label;
931 };
932
933 static struct insn_label_list *insn_labels;
934 static struct insn_label_list *free_insn_labels;
935
936 static void mips_clear_insn_labels PARAMS ((void));
937
938 static inline void
939 mips_clear_insn_labels ()
940 {
941   register struct insn_label_list **pl;
942
943   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
944     ;
945   *pl = insn_labels;
946   insn_labels = NULL;
947 }
948 \f
949 static char *expr_end;
950
951 /* Expressions which appear in instructions.  These are set by
952    mips_ip.  */
953
954 static expressionS imm_expr;
955 static expressionS offset_expr;
956
957 /* Relocs associated with imm_expr and offset_expr.  */
958
959 static bfd_reloc_code_real_type imm_reloc[3]
960   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
961 static bfd_reloc_code_real_type offset_reloc[3]
962   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
963
964 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
965
966 static boolean imm_unmatched_hi;
967
968 /* These are set by mips16_ip if an explicit extension is used.  */
969
970 static boolean mips16_small, mips16_ext;
971
972 /* The pdr segment for per procedure frame/regmask info.  Not used for
973    ECOFF debugging.  */
974
975 static segT pdr_seg;
976
977 /* The default target format to use.  */
978
979 const char *
980 mips_target_format ()
981 {
982   switch (OUTPUT_FLAVOR)
983     {
984     case bfd_target_aout_flavour:
985       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
986     case bfd_target_ecoff_flavour:
987       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
988     case bfd_target_coff_flavour:
989       return "pe-mips";
990     case bfd_target_elf_flavour:
991 #ifdef TE_TMIPS
992       /* This is traditional mips */
993       return (target_big_endian
994               ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
995                  : "elf32-tradbigmips")
996               : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
997                  : "elf32-tradlittlemips"));
998 #else
999       return (target_big_endian
1000               ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
1001               : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1002                  : "elf32-littlemips"));
1003 #endif
1004     default:
1005       abort ();
1006       return NULL;
1007     }
1008 }
1009
1010 /* This function is called once, at assembler startup time.  It should
1011    set up all the tables, etc. that the MD part of the assembler will need.  */
1012
1013 void
1014 md_begin ()
1015 {
1016   register const char *retval = NULL;
1017   int i = 0;
1018   int broken = 0;
1019
1020   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1021     as_warn (_("Could not set architecture and machine"));
1022
1023   op_hash = hash_new ();
1024
1025   for (i = 0; i < NUMOPCODES;)
1026     {
1027       const char *name = mips_opcodes[i].name;
1028
1029       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1030       if (retval != NULL)
1031         {
1032           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1033                    mips_opcodes[i].name, retval);
1034           /* Probably a memory allocation problem?  Give up now.  */
1035           as_fatal (_("Broken assembler.  No assembly attempted."));
1036         }
1037       do
1038         {
1039           if (mips_opcodes[i].pinfo != INSN_MACRO)
1040             {
1041               if (!validate_mips_insn (&mips_opcodes[i]))
1042                 broken = 1;
1043             }
1044           ++i;
1045         }
1046       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1047     }
1048
1049   mips16_op_hash = hash_new ();
1050
1051   i = 0;
1052   while (i < bfd_mips16_num_opcodes)
1053     {
1054       const char *name = mips16_opcodes[i].name;
1055
1056       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1057       if (retval != NULL)
1058         as_fatal (_("internal: can't hash `%s': %s"),
1059                   mips16_opcodes[i].name, retval);
1060       do
1061         {
1062           if (mips16_opcodes[i].pinfo != INSN_MACRO
1063               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1064                   != mips16_opcodes[i].match))
1065             {
1066               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1067                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1068               broken = 1;
1069             }
1070           ++i;
1071         }
1072       while (i < bfd_mips16_num_opcodes
1073              && strcmp (mips16_opcodes[i].name, name) == 0);
1074     }
1075
1076   if (broken)
1077     as_fatal (_("Broken assembler.  No assembly attempted."));
1078
1079   /* We add all the general register names to the symbol table.  This
1080      helps us detect invalid uses of them.  */
1081   for (i = 0; i < 32; i++)
1082     {
1083       char buf[5];
1084
1085       sprintf (buf, "$%d", i);
1086       symbol_table_insert (symbol_new (buf, reg_section, i,
1087                                        &zero_address_frag));
1088     }
1089   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1090                                    &zero_address_frag));
1091   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1092                                    &zero_address_frag));
1093   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1094                                    &zero_address_frag));
1095   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1096                                    &zero_address_frag));
1097   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1098                                    &zero_address_frag));
1099   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1100                                    &zero_address_frag));
1101   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1102                                    &zero_address_frag));
1103   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1104                                    &zero_address_frag));
1105   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1106                                    &zero_address_frag));
1107
1108   mips_no_prev_insn (false);
1109
1110   mips_gprmask = 0;
1111   mips_cprmask[0] = 0;
1112   mips_cprmask[1] = 0;
1113   mips_cprmask[2] = 0;
1114   mips_cprmask[3] = 0;
1115
1116   /* set the default alignment for the text section (2**2) */
1117   record_alignment (text_section, 2);
1118
1119   if (USE_GLOBAL_POINTER_OPT)
1120     bfd_set_gp_size (stdoutput, g_switch_value);
1121
1122   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1123     {
1124       /* On a native system, sections must be aligned to 16 byte
1125          boundaries.  When configured for an embedded ELF target, we
1126          don't bother.  */
1127       if (strcmp (TARGET_OS, "elf") != 0)
1128         {
1129           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1130           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1131           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1132         }
1133
1134       /* Create a .reginfo section for register masks and a .mdebug
1135          section for debugging information.  */
1136       {
1137         segT seg;
1138         subsegT subseg;
1139         flagword flags;
1140         segT sec;
1141
1142         seg = now_seg;
1143         subseg = now_subseg;
1144
1145         /* The ABI says this section should be loaded so that the
1146            running program can access it.  However, we don't load it
1147            if we are configured for an embedded target */
1148         flags = SEC_READONLY | SEC_DATA;
1149         if (strcmp (TARGET_OS, "elf") != 0)
1150           flags |= SEC_ALLOC | SEC_LOAD;
1151
1152         if (mips_abi != N64_ABI)
1153           {
1154             sec = subseg_new (".reginfo", (subsegT) 0);
1155
1156             bfd_set_section_flags (stdoutput, sec, flags);
1157             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1158
1159 #ifdef OBJ_ELF
1160             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1161 #endif
1162           }
1163         else
1164           {
1165             /* The 64-bit ABI uses a .MIPS.options section rather than
1166                .reginfo section.  */
1167             sec = subseg_new (".MIPS.options", (subsegT) 0);
1168             bfd_set_section_flags (stdoutput, sec, flags);
1169             bfd_set_section_alignment (stdoutput, sec, 3);
1170
1171 #ifdef OBJ_ELF
1172             /* Set up the option header.  */
1173             {
1174               Elf_Internal_Options opthdr;
1175               char *f;
1176
1177               opthdr.kind = ODK_REGINFO;
1178               opthdr.size = (sizeof (Elf_External_Options)
1179                              + sizeof (Elf64_External_RegInfo));
1180               opthdr.section = 0;
1181               opthdr.info = 0;
1182               f = frag_more (sizeof (Elf_External_Options));
1183               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1184                                              (Elf_External_Options *) f);
1185
1186               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1187             }
1188 #endif
1189           }
1190
1191         if (ECOFF_DEBUGGING)
1192           {
1193             sec = subseg_new (".mdebug", (subsegT) 0);
1194             (void) bfd_set_section_flags (stdoutput, sec,
1195                                           SEC_HAS_CONTENTS | SEC_READONLY);
1196             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1197           }
1198 #ifdef OBJ_ELF
1199         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1200           {
1201             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1202             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1203                                           SEC_READONLY | SEC_RELOC
1204                                           | SEC_DEBUGGING);
1205             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1206           }
1207 #endif
1208
1209         subseg_set (seg, subseg);
1210       }
1211     }
1212
1213   if (! ECOFF_DEBUGGING)
1214     md_obj_begin ();
1215 }
1216
1217 void
1218 md_mips_end ()
1219 {
1220   if (! ECOFF_DEBUGGING)
1221     md_obj_end ();
1222 }
1223
1224 void
1225 md_assemble (str)
1226      char *str;
1227 {
1228   struct mips_cl_insn insn;
1229   bfd_reloc_code_real_type unused_reloc[3]
1230     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1231
1232   imm_expr.X_op = O_absent;
1233   imm_unmatched_hi = false;
1234   offset_expr.X_op = O_absent;
1235   imm_reloc[0] = BFD_RELOC_UNUSED;
1236   imm_reloc[1] = BFD_RELOC_UNUSED;
1237   imm_reloc[2] = BFD_RELOC_UNUSED;
1238   offset_reloc[0] = BFD_RELOC_UNUSED;
1239   offset_reloc[1] = BFD_RELOC_UNUSED;
1240   offset_reloc[2] = BFD_RELOC_UNUSED;
1241
1242   if (mips_opts.mips16)
1243     mips16_ip (str, &insn);
1244   else
1245     {
1246       mips_ip (str, &insn);
1247       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1248             str, insn.insn_opcode));
1249     }
1250
1251   if (insn_error)
1252     {
1253       as_bad ("%s `%s'", insn_error, str);
1254       return;
1255     }
1256
1257   if (insn.insn_mo->pinfo == INSN_MACRO)
1258     {
1259       if (mips_opts.mips16)
1260         mips16_macro (&insn);
1261       else
1262         macro (&insn);
1263     }
1264   else
1265     {
1266       if (imm_expr.X_op != O_absent)
1267         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1268       else if (offset_expr.X_op != O_absent)
1269         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1270       else
1271         append_insn (NULL, &insn, NULL, unused_reloc, false);
1272     }
1273 }
1274
1275 /* See whether instruction IP reads register REG.  CLASS is the type
1276    of register.  */
1277
1278 static int
1279 insn_uses_reg (ip, reg, class)
1280      struct mips_cl_insn *ip;
1281      unsigned int reg;
1282      enum mips_regclass class;
1283 {
1284   if (class == MIPS16_REG)
1285     {
1286       assert (mips_opts.mips16);
1287       reg = mips16_to_32_reg_map[reg];
1288       class = MIPS_GR_REG;
1289     }
1290
1291   /* Don't report on general register ZERO, since it never changes.  */
1292   if (class == MIPS_GR_REG && reg == ZERO)
1293     return 0;
1294
1295   if (class == MIPS_FP_REG)
1296     {
1297       assert (! mips_opts.mips16);
1298       /* If we are called with either $f0 or $f1, we must check $f0.
1299          This is not optimal, because it will introduce an unnecessary
1300          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1301          need to distinguish reading both $f0 and $f1 or just one of
1302          them.  Note that we don't have to check the other way,
1303          because there is no instruction that sets both $f0 and $f1
1304          and requires a delay.  */
1305       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1306           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1307               == (reg &~ (unsigned) 1)))
1308         return 1;
1309       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1310           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1311               == (reg &~ (unsigned) 1)))
1312         return 1;
1313     }
1314   else if (! mips_opts.mips16)
1315     {
1316       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1317           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1318         return 1;
1319       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1320           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1321         return 1;
1322     }
1323   else
1324     {
1325       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1326           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1327                                     & MIPS16OP_MASK_RX)]
1328               == reg))
1329         return 1;
1330       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1331           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1332                                     & MIPS16OP_MASK_RY)]
1333               == reg))
1334         return 1;
1335       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1336           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1337                                     & MIPS16OP_MASK_MOVE32Z)]
1338               == reg))
1339         return 1;
1340       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1341         return 1;
1342       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1343         return 1;
1344       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1345         return 1;
1346       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1347           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1348               & MIPS16OP_MASK_REGR32) == reg)
1349         return 1;
1350     }
1351
1352   return 0;
1353 }
1354
1355 /* This function returns true if modifying a register requires a
1356    delay.  */
1357
1358 static int
1359 reg_needs_delay (reg)
1360      unsigned int reg;
1361 {
1362   unsigned long prev_pinfo;
1363
1364   prev_pinfo = prev_insn.insn_mo->pinfo;
1365   if (! mips_opts.noreorder
1366       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1367       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1368           || (! gpr_interlocks
1369               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1370     {
1371       /* A load from a coprocessor or from memory.  All load
1372          delays delay the use of general register rt for one
1373          instruction on the r3000.  The r6000 and r4000 use
1374          interlocks.  */
1375       /* Itbl support may require additional care here.  */
1376       know (prev_pinfo & INSN_WRITE_GPR_T);
1377       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1378         return 1;
1379     }
1380
1381   return 0;
1382 }
1383
1384 /* Mark instruction labels in mips16 mode.  This permits the linker to
1385    handle them specially, such as generating jalx instructions when
1386    needed.  We also make them odd for the duration of the assembly, in
1387    order to generate the right sort of code.  We will make them even
1388    in the adjust_symtab routine, while leaving them marked.  This is
1389    convenient for the debugger and the disassembler.  The linker knows
1390    to make them odd again.  */
1391
1392 static void
1393 mips16_mark_labels ()
1394 {
1395   if (mips_opts.mips16)
1396     {
1397       struct insn_label_list *l;
1398       valueT val;
1399
1400       for (l = insn_labels; l != NULL; l = l->next)
1401         {
1402 #ifdef OBJ_ELF
1403           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1404             S_SET_OTHER (l->label, STO_MIPS16);
1405 #endif
1406           val = S_GET_VALUE (l->label);
1407           if ((val & 1) == 0)
1408             S_SET_VALUE (l->label, val + 1);
1409         }
1410     }
1411 }
1412
1413 /* Output an instruction.  PLACE is where to put the instruction; if
1414    it is NULL, this uses frag_more to get room.  IP is the instruction
1415    information.  ADDRESS_EXPR is an operand of the instruction to be
1416    used with RELOC_TYPE.  */
1417
1418 static void
1419 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1420      char *place;
1421      struct mips_cl_insn *ip;
1422      expressionS *address_expr;
1423      bfd_reloc_code_real_type *reloc_type;
1424      boolean unmatched_hi;
1425 {
1426   register unsigned long prev_pinfo, pinfo;
1427   char *f;
1428   fixS *fixp[3];
1429   int nops = 0;
1430
1431   /* Mark instruction labels in mips16 mode.  */
1432   mips16_mark_labels ();
1433
1434   prev_pinfo = prev_insn.insn_mo->pinfo;
1435   pinfo = ip->insn_mo->pinfo;
1436
1437   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1438     {
1439       int prev_prev_nop;
1440
1441       /* If the previous insn required any delay slots, see if we need
1442          to insert a NOP or two.  There are eight kinds of possible
1443          hazards, of which an instruction can have at most one type.
1444          (1) a load from memory delay
1445          (2) a load from a coprocessor delay
1446          (3) an unconditional branch delay
1447          (4) a conditional branch delay
1448          (5) a move to coprocessor register delay
1449          (6) a load coprocessor register from memory delay
1450          (7) a coprocessor condition code delay
1451          (8) a HI/LO special register delay
1452
1453          There are a lot of optimizations we could do that we don't.
1454          In particular, we do not, in general, reorder instructions.
1455          If you use gcc with optimization, it will reorder
1456          instructions and generally do much more optimization then we
1457          do here; repeating all that work in the assembler would only
1458          benefit hand written assembly code, and does not seem worth
1459          it.  */
1460
1461       /* This is how a NOP is emitted.  */
1462 #define emit_nop()                                      \
1463   (mips_opts.mips16                                     \
1464    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1465    : md_number_to_chars (frag_more (4), 0, 4))
1466
1467       /* The previous insn might require a delay slot, depending upon
1468          the contents of the current insn.  */
1469       if (! mips_opts.mips16
1470           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1471           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1472                && ! cop_interlocks)
1473               || (! gpr_interlocks
1474                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1475         {
1476           /* A load from a coprocessor or from memory.  All load
1477              delays delay the use of general register rt for one
1478              instruction on the r3000.  The r6000 and r4000 use
1479              interlocks.  */
1480           /* Itbl support may require additional care here.  */
1481           know (prev_pinfo & INSN_WRITE_GPR_T);
1482           if (mips_optimize == 0
1483               || insn_uses_reg (ip,
1484                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1485                                  & OP_MASK_RT),
1486                                 MIPS_GR_REG))
1487             ++nops;
1488         }
1489       else if (! mips_opts.mips16
1490                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1491                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1492                     && ! cop_interlocks)
1493                    || (mips_opts.isa == ISA_MIPS1
1494                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1495         {
1496           /* A generic coprocessor delay.  The previous instruction
1497              modified a coprocessor general or control register.  If
1498              it modified a control register, we need to avoid any
1499              coprocessor instruction (this is probably not always
1500              required, but it sometimes is).  If it modified a general
1501              register, we avoid using that register.
1502
1503              On the r6000 and r4000 loading a coprocessor register
1504              from memory is interlocked, and does not require a delay.
1505
1506              This case is not handled very well.  There is no special
1507              knowledge of CP0 handling, and the coprocessors other
1508              than the floating point unit are not distinguished at
1509              all.  */
1510           /* Itbl support may require additional care here. FIXME!
1511              Need to modify this to include knowledge about
1512              user specified delays!  */
1513           if (prev_pinfo & INSN_WRITE_FPR_T)
1514             {
1515               if (mips_optimize == 0
1516                   || insn_uses_reg (ip,
1517                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1518                                      & OP_MASK_FT),
1519                                     MIPS_FP_REG))
1520                 ++nops;
1521             }
1522           else if (prev_pinfo & INSN_WRITE_FPR_S)
1523             {
1524               if (mips_optimize == 0
1525                   || insn_uses_reg (ip,
1526                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1527                                      & OP_MASK_FS),
1528                                     MIPS_FP_REG))
1529                 ++nops;
1530             }
1531           else
1532             {
1533               /* We don't know exactly what the previous instruction
1534                  does.  If the current instruction uses a coprocessor
1535                  register, we must insert a NOP.  If previous
1536                  instruction may set the condition codes, and the
1537                  current instruction uses them, we must insert two
1538                  NOPS.  */
1539               /* Itbl support may require additional care here.  */
1540               if (mips_optimize == 0
1541                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1542                       && (pinfo & INSN_READ_COND_CODE)))
1543                 nops += 2;
1544               else if (pinfo & INSN_COP)
1545                 ++nops;
1546             }
1547         }
1548       else if (! mips_opts.mips16
1549                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1550                && (prev_pinfo & INSN_WRITE_COND_CODE)
1551                && ! cop_interlocks)
1552         {
1553           /* The previous instruction sets the coprocessor condition
1554              codes, but does not require a general coprocessor delay
1555              (this means it is a floating point comparison
1556              instruction).  If this instruction uses the condition
1557              codes, we need to insert a single NOP.  */
1558           /* Itbl support may require additional care here.  */
1559           if (mips_optimize == 0
1560               || (pinfo & INSN_READ_COND_CODE))
1561             ++nops;
1562         }
1563
1564       /* If we're fixing up mfhi/mflo for the r7000 and the
1565          previous insn was an mfhi/mflo and the current insn
1566          reads the register that the mfhi/mflo wrote to, then
1567          insert two nops.  */
1568
1569       else if (mips_7000_hilo_fix
1570                && MF_HILO_INSN (prev_pinfo)
1571                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1572                                       & OP_MASK_RD),
1573                                  MIPS_GR_REG))
1574         {
1575           nops += 2;
1576         }
1577
1578       /* If we're fixing up mfhi/mflo for the r7000 and the
1579          2nd previous insn was an mfhi/mflo and the current insn
1580          reads the register that the mfhi/mflo wrote to, then
1581          insert one nop.  */
1582
1583       else if (mips_7000_hilo_fix
1584                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1585                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1586                                        & OP_MASK_RD),
1587                                     MIPS_GR_REG))
1588
1589         {
1590           ++nops;
1591         }
1592
1593       else if (prev_pinfo & INSN_READ_LO)
1594         {
1595           /* The previous instruction reads the LO register; if the
1596              current instruction writes to the LO register, we must
1597              insert two NOPS.  Some newer processors have interlocks.
1598              Also the tx39's multiply instructions can be exectuted
1599              immediatly after a read from HI/LO (without the delay),
1600              though the tx39's divide insns still do require the
1601              delay.  */
1602           if (! (hilo_interlocks
1603                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1604               && (mips_optimize == 0
1605                   || (pinfo & INSN_WRITE_LO)))
1606             nops += 2;
1607           /* Most mips16 branch insns don't have a delay slot.
1608              If a read from LO is immediately followed by a branch
1609              to a write to LO we have a read followed by a write
1610              less than 2 insns away.  We assume the target of
1611              a branch might be a write to LO, and insert a nop
1612              between a read and an immediately following branch.  */
1613           else if (mips_opts.mips16
1614                    && (mips_optimize == 0
1615                        || (pinfo & MIPS16_INSN_BRANCH)))
1616             ++nops;
1617         }
1618       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1619         {
1620           /* The previous instruction reads the HI register; if the
1621              current instruction writes to the HI register, we must
1622              insert a NOP.  Some newer processors have interlocks.
1623              Also the note tx39's multiply above.  */
1624           if (! (hilo_interlocks
1625                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1626               && (mips_optimize == 0
1627                   || (pinfo & INSN_WRITE_HI)))
1628             nops += 2;
1629           /* Most mips16 branch insns don't have a delay slot.
1630              If a read from HI is immediately followed by a branch
1631              to a write to HI we have a read followed by a write
1632              less than 2 insns away.  We assume the target of
1633              a branch might be a write to HI, and insert a nop
1634              between a read and an immediately following branch.  */
1635           else if (mips_opts.mips16
1636                    && (mips_optimize == 0
1637                        || (pinfo & MIPS16_INSN_BRANCH)))
1638             ++nops;
1639         }
1640
1641       /* If the previous instruction was in a noreorder section, then
1642          we don't want to insert the nop after all.  */
1643       /* Itbl support may require additional care here.  */
1644       if (prev_insn_unreordered)
1645         nops = 0;
1646
1647       /* There are two cases which require two intervening
1648          instructions: 1) setting the condition codes using a move to
1649          coprocessor instruction which requires a general coprocessor
1650          delay and then reading the condition codes 2) reading the HI
1651          or LO register and then writing to it (except on processors
1652          which have interlocks).  If we are not already emitting a NOP
1653          instruction, we must check for these cases compared to the
1654          instruction previous to the previous instruction.  */
1655       if ((! mips_opts.mips16
1656            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1657            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1658            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1659            && (pinfo & INSN_READ_COND_CODE)
1660            && ! cop_interlocks)
1661           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1662               && (pinfo & INSN_WRITE_LO)
1663               && ! (hilo_interlocks
1664                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1665           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1666               && (pinfo & INSN_WRITE_HI)
1667               && ! (hilo_interlocks
1668                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1669         prev_prev_nop = 1;
1670       else
1671         prev_prev_nop = 0;
1672
1673       if (prev_prev_insn_unreordered)
1674         prev_prev_nop = 0;
1675
1676       if (prev_prev_nop && nops == 0)
1677         ++nops;
1678
1679       /* If we are being given a nop instruction, don't bother with
1680          one of the nops we would otherwise output.  This will only
1681          happen when a nop instruction is used with mips_optimize set
1682          to 0.  */
1683       if (nops > 0
1684           && ! mips_opts.noreorder
1685           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1686         --nops;
1687
1688       /* Now emit the right number of NOP instructions.  */
1689       if (nops > 0 && ! mips_opts.noreorder)
1690         {
1691           fragS *old_frag;
1692           unsigned long old_frag_offset;
1693           int i;
1694           struct insn_label_list *l;
1695
1696           old_frag = frag_now;
1697           old_frag_offset = frag_now_fix ();
1698
1699           for (i = 0; i < nops; i++)
1700             emit_nop ();
1701
1702           if (listing)
1703             {
1704               listing_prev_line ();
1705               /* We may be at the start of a variant frag.  In case we
1706                  are, make sure there is enough space for the frag
1707                  after the frags created by listing_prev_line.  The
1708                  argument to frag_grow here must be at least as large
1709                  as the argument to all other calls to frag_grow in
1710                  this file.  We don't have to worry about being in the
1711                  middle of a variant frag, because the variants insert
1712                  all needed nop instructions themselves.  */
1713               frag_grow (40);
1714             }
1715
1716           for (l = insn_labels; l != NULL; l = l->next)
1717             {
1718               valueT val;
1719
1720               assert (S_GET_SEGMENT (l->label) == now_seg);
1721               symbol_set_frag (l->label, frag_now);
1722               val = (valueT) frag_now_fix ();
1723               /* mips16 text labels are stored as odd.  */
1724               if (mips_opts.mips16)
1725                 ++val;
1726               S_SET_VALUE (l->label, val);
1727             }
1728
1729 #ifndef NO_ECOFF_DEBUGGING
1730           if (ECOFF_DEBUGGING)
1731             ecoff_fix_loc (old_frag, old_frag_offset);
1732 #endif
1733         }
1734       else if (prev_nop_frag != NULL)
1735         {
1736           /* We have a frag holding nops we may be able to remove.  If
1737              we don't need any nops, we can decrease the size of
1738              prev_nop_frag by the size of one instruction.  If we do
1739              need some nops, we count them in prev_nops_required.  */
1740           if (prev_nop_frag_since == 0)
1741             {
1742               if (nops == 0)
1743                 {
1744                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1745                   --prev_nop_frag_holds;
1746                 }
1747               else
1748                 prev_nop_frag_required += nops;
1749             }
1750           else
1751             {
1752               if (prev_prev_nop == 0)
1753                 {
1754                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1755                   --prev_nop_frag_holds;
1756                 }
1757               else
1758                 ++prev_nop_frag_required;
1759             }
1760
1761           if (prev_nop_frag_holds <= prev_nop_frag_required)
1762             prev_nop_frag = NULL;
1763
1764           ++prev_nop_frag_since;
1765
1766           /* Sanity check: by the time we reach the second instruction
1767              after prev_nop_frag, we should have used up all the nops
1768              one way or another.  */
1769           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1770         }
1771     }
1772
1773   if (*reloc_type > BFD_RELOC_UNUSED)
1774     {
1775       /* We need to set up a variant frag.  */
1776       assert (mips_opts.mips16 && address_expr != NULL);
1777       f = frag_var (rs_machine_dependent, 4, 0,
1778                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1779                                          mips16_small, mips16_ext,
1780                                          (prev_pinfo
1781                                           & INSN_UNCOND_BRANCH_DELAY),
1782                                          (*prev_insn_reloc_type
1783                                           == BFD_RELOC_MIPS16_JMP)),
1784                     make_expr_symbol (address_expr), 0, NULL);
1785     }
1786   else if (place != NULL)
1787     f = place;
1788   else if (mips_opts.mips16
1789            && ! ip->use_extend
1790            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1791     {
1792       /* Make sure there is enough room to swap this instruction with
1793          a following jump instruction.  */
1794       frag_grow (6);
1795       f = frag_more (2);
1796     }
1797   else
1798     {
1799       if (mips_opts.mips16
1800           && mips_opts.noreorder
1801           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1802         as_warn (_("extended instruction in delay slot"));
1803
1804       f = frag_more (4);
1805     }
1806
1807   fixp[0] = fixp[1] = fixp[2] = NULL;
1808   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1809     {
1810       if (address_expr->X_op == O_constant)
1811         {
1812           valueT tmp;
1813
1814           switch (*reloc_type)
1815             {
1816             case BFD_RELOC_32:
1817               ip->insn_opcode |= address_expr->X_add_number;
1818               break;
1819
1820             case BFD_RELOC_MIPS_HIGHEST:
1821               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1822               tmp >>= 16;
1823               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1824               break;
1825
1826             case BFD_RELOC_MIPS_HIGHER:
1827               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1828               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1829               break;
1830
1831             case BFD_RELOC_HI16_S:
1832               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1833                                   >> 16) & 0xffff;
1834               break;
1835
1836             case BFD_RELOC_HI16:
1837               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1838               break;
1839
1840             case BFD_RELOC_LO16:
1841               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1842               break;
1843
1844             case BFD_RELOC_MIPS_JMP:
1845               if ((address_expr->X_add_number & 3) != 0)
1846                 as_bad (_("jump to misaligned address (0x%lx)"),
1847                         (unsigned long) address_expr->X_add_number);
1848               if (address_expr->X_add_number & ~0xfffffff
1849                   || address_expr->X_add_number > 0x7fffffc)
1850                 as_bad (_("jump address range overflow (0x%lx)"),
1851                         (unsigned long) address_expr->X_add_number);
1852               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1853               break;
1854
1855             case BFD_RELOC_MIPS16_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 |=
1864                 (((address_expr->X_add_number & 0x7c0000) << 3)
1865                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1866                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1867               break;
1868
1869             case BFD_RELOC_16_PCREL:
1870               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1871               break;
1872
1873             case BFD_RELOC_16_PCREL_S2:
1874               goto need_reloc;
1875
1876             default:
1877               internalError ();
1878             }
1879         }
1880       else
1881         {
1882         need_reloc:
1883           /* Don't generate a reloc if we are writing into a variant frag.  */
1884           if (place == NULL)
1885             {
1886               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1887                                      address_expr,
1888                                      (*reloc_type == BFD_RELOC_16_PCREL
1889                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
1890                                      reloc_type[0]);
1891
1892               /* These relocations can have an addend that won't fit in
1893                  4 octets for 64bit assembly.  */
1894               if (HAVE_64BIT_GPRS &&
1895                   (*reloc_type == BFD_RELOC_16
1896                    || *reloc_type == BFD_RELOC_32
1897                    || *reloc_type == BFD_RELOC_MIPS_JMP
1898                    || *reloc_type == BFD_RELOC_HI16_S
1899                    || *reloc_type == BFD_RELOC_LO16
1900                    || *reloc_type == BFD_RELOC_GPREL16
1901                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
1902                    || *reloc_type == BFD_RELOC_GPREL32
1903                    || *reloc_type == BFD_RELOC_64
1904                    || *reloc_type == BFD_RELOC_CTOR
1905                    || *reloc_type == BFD_RELOC_MIPS_SUB
1906                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1907                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
1908                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1909                    || *reloc_type == BFD_RELOC_MIPS_REL16
1910                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1911                 fixp[0]->fx_no_overflow = 1;
1912
1913               if (unmatched_hi)
1914                 {
1915                   struct mips_hi_fixup *hi_fixup;
1916
1917                   assert (*reloc_type == BFD_RELOC_HI16_S);
1918                   hi_fixup = ((struct mips_hi_fixup *)
1919                               xmalloc (sizeof (struct mips_hi_fixup)));
1920                   hi_fixup->fixp = fixp[0];
1921                   hi_fixup->seg = now_seg;
1922                   hi_fixup->next = mips_hi_fixup_list;
1923                   mips_hi_fixup_list = hi_fixup;
1924                 }
1925
1926               if (reloc_type[1] != BFD_RELOC_UNUSED)
1927                 {
1928                   /* FIXME: This symbol can be one of
1929                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
1930                   address_expr->X_op = O_absent;
1931                   address_expr->X_add_symbol = 0;
1932                   address_expr->X_add_number = 0;
1933
1934                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1935                                          4, address_expr, false,
1936                                          reloc_type[1]);
1937
1938                   /* These relocations can have an addend that won't fit in
1939                      4 octets for 64bit assembly.  */
1940                   if (HAVE_64BIT_GPRS &&
1941                       (*reloc_type == BFD_RELOC_16
1942                        || *reloc_type == BFD_RELOC_32
1943                        || *reloc_type == BFD_RELOC_MIPS_JMP
1944                        || *reloc_type == BFD_RELOC_HI16_S
1945                        || *reloc_type == BFD_RELOC_LO16
1946                        || *reloc_type == BFD_RELOC_GPREL16
1947                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
1948                        || *reloc_type == BFD_RELOC_GPREL32
1949                        || *reloc_type == BFD_RELOC_64
1950                        || *reloc_type == BFD_RELOC_CTOR
1951                        || *reloc_type == BFD_RELOC_MIPS_SUB
1952                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1953                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
1954                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1955                        || *reloc_type == BFD_RELOC_MIPS_REL16
1956                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1957                     fixp[1]->fx_no_overflow = 1;
1958
1959                   if (reloc_type[2] != BFD_RELOC_UNUSED)
1960                     {
1961                       address_expr->X_op = O_absent;
1962                       address_expr->X_add_symbol = 0;
1963                       address_expr->X_add_number = 0;
1964
1965                       fixp[2] = fix_new_exp (frag_now,
1966                                              f - frag_now->fr_literal, 4,
1967                                              address_expr, false,
1968                                              reloc_type[2]);
1969
1970                       /* These relocations can have an addend that won't fit in
1971                          4 octets for 64bit assembly.  */
1972                       if (HAVE_64BIT_GPRS &&
1973                           (*reloc_type == BFD_RELOC_16
1974                            || *reloc_type == BFD_RELOC_32
1975                            || *reloc_type == BFD_RELOC_MIPS_JMP
1976                            || *reloc_type == BFD_RELOC_HI16_S
1977                            || *reloc_type == BFD_RELOC_LO16
1978                            || *reloc_type == BFD_RELOC_GPREL16
1979                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
1980                            || *reloc_type == BFD_RELOC_GPREL32
1981                            || *reloc_type == BFD_RELOC_64
1982                            || *reloc_type == BFD_RELOC_CTOR
1983                            || *reloc_type == BFD_RELOC_MIPS_SUB
1984                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1985                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
1986                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1987                            || *reloc_type == BFD_RELOC_MIPS_REL16
1988                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1989                         fixp[2]->fx_no_overflow = 1;
1990                     }
1991                 }
1992             }
1993         }
1994     }
1995
1996   if (! mips_opts.mips16)
1997     {
1998       md_number_to_chars (f, ip->insn_opcode, 4);
1999 #ifdef OBJ_ELF
2000       dwarf2_emit_insn (4);
2001 #endif
2002     }
2003   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2004     {
2005       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2006       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2007 #ifdef OBJ_ELF
2008       dwarf2_emit_insn (4);
2009 #endif
2010     }
2011   else
2012     {
2013       if (ip->use_extend)
2014         {
2015           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2016           f += 2;
2017         }
2018       md_number_to_chars (f, ip->insn_opcode, 2);
2019 #ifdef OBJ_ELF
2020       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2021 #endif
2022     }
2023
2024   /* Update the register mask information.  */
2025   if (! mips_opts.mips16)
2026     {
2027       if (pinfo & INSN_WRITE_GPR_D)
2028         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2029       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2030         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2031       if (pinfo & INSN_READ_GPR_S)
2032         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2033       if (pinfo & INSN_WRITE_GPR_31)
2034         mips_gprmask |= 1 << RA;
2035       if (pinfo & INSN_WRITE_FPR_D)
2036         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2037       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2038         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2039       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2040         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2041       if ((pinfo & INSN_READ_FPR_R) != 0)
2042         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2043       if (pinfo & INSN_COP)
2044         {
2045           /* We don't keep enough information to sort these cases out.
2046              The itbl support does keep this information however, although
2047              we currently don't support itbl fprmats as part of the cop
2048              instruction.  May want to add this support in the future.  */
2049         }
2050       /* Never set the bit for $0, which is always zero.  */
2051       mips_gprmask &= ~1 << 0;
2052     }
2053   else
2054     {
2055       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2056         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2057                               & MIPS16OP_MASK_RX);
2058       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2059         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2060                               & MIPS16OP_MASK_RY);
2061       if (pinfo & MIPS16_INSN_WRITE_Z)
2062         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2063                               & MIPS16OP_MASK_RZ);
2064       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2065         mips_gprmask |= 1 << TREG;
2066       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2067         mips_gprmask |= 1 << SP;
2068       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2069         mips_gprmask |= 1 << RA;
2070       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2071         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2072       if (pinfo & MIPS16_INSN_READ_Z)
2073         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2074                               & MIPS16OP_MASK_MOVE32Z);
2075       if (pinfo & MIPS16_INSN_READ_GPR_X)
2076         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2077                               & MIPS16OP_MASK_REGR32);
2078     }
2079
2080   if (place == NULL && ! mips_opts.noreorder)
2081     {
2082       /* Filling the branch delay slot is more complex.  We try to
2083          switch the branch with the previous instruction, which we can
2084          do if the previous instruction does not set up a condition
2085          that the branch tests and if the branch is not itself the
2086          target of any branch.  */
2087       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2088           || (pinfo & INSN_COND_BRANCH_DELAY))
2089         {
2090           if (mips_optimize < 2
2091               /* If we have seen .set volatile or .set nomove, don't
2092                  optimize.  */
2093               || mips_opts.nomove != 0
2094               /* If we had to emit any NOP instructions, then we
2095                  already know we can not swap.  */
2096               || nops != 0
2097               /* If we don't even know the previous insn, we can not
2098                  swap.  */
2099               || ! prev_insn_valid
2100               /* If the previous insn is already in a branch delay
2101                  slot, then we can not swap.  */
2102               || prev_insn_is_delay_slot
2103               /* If the previous previous insn was in a .set
2104                  noreorder, we can't swap.  Actually, the MIPS
2105                  assembler will swap in this situation.  However, gcc
2106                  configured -with-gnu-as will generate code like
2107                    .set noreorder
2108                    lw   $4,XXX
2109                    .set reorder
2110                    INSN
2111                    bne  $4,$0,foo
2112                  in which we can not swap the bne and INSN.  If gcc is
2113                  not configured -with-gnu-as, it does not output the
2114                  .set pseudo-ops.  We don't have to check
2115                  prev_insn_unreordered, because prev_insn_valid will
2116                  be 0 in that case.  We don't want to use
2117                  prev_prev_insn_valid, because we do want to be able
2118                  to swap at the start of a function.  */
2119               || prev_prev_insn_unreordered
2120               /* If the branch is itself the target of a branch, we
2121                  can not swap.  We cheat on this; all we check for is
2122                  whether there is a label on this instruction.  If
2123                  there are any branches to anything other than a
2124                  label, users must use .set noreorder.  */
2125               || insn_labels != NULL
2126               /* If the previous instruction is in a variant frag, we
2127                  can not do the swap.  This does not apply to the
2128                  mips16, which uses variant frags for different
2129                  purposes.  */
2130               || (! mips_opts.mips16
2131                   && prev_insn_frag->fr_type == rs_machine_dependent)
2132               /* If the branch reads the condition codes, we don't
2133                  even try to swap, because in the sequence
2134                    ctc1 $X,$31
2135                    INSN
2136                    INSN
2137                    bc1t LABEL
2138                  we can not swap, and I don't feel like handling that
2139                  case.  */
2140               || (! mips_opts.mips16
2141                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2142                   && (pinfo & INSN_READ_COND_CODE))
2143               /* We can not swap with an instruction that requires a
2144                  delay slot, becase the target of the branch might
2145                  interfere with that instruction.  */
2146               || (! mips_opts.mips16
2147                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2148                   && (prev_pinfo
2149               /* Itbl support may require additional care here.  */
2150                       & (INSN_LOAD_COPROC_DELAY
2151                          | INSN_COPROC_MOVE_DELAY
2152                          | INSN_WRITE_COND_CODE)))
2153               || (! (hilo_interlocks
2154                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2155                   && (prev_pinfo
2156                       & (INSN_READ_LO
2157                          | INSN_READ_HI)))
2158               || (! mips_opts.mips16
2159                   && ! gpr_interlocks
2160                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2161               || (! mips_opts.mips16
2162                   && mips_opts.isa == ISA_MIPS1
2163                   /* Itbl support may require additional care here.  */
2164                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2165               /* We can not swap with a branch instruction.  */
2166               || (prev_pinfo
2167                   & (INSN_UNCOND_BRANCH_DELAY
2168                      | INSN_COND_BRANCH_DELAY
2169                      | INSN_COND_BRANCH_LIKELY))
2170               /* We do not swap with a trap instruction, since it
2171                  complicates trap handlers to have the trap
2172                  instruction be in a delay slot.  */
2173               || (prev_pinfo & INSN_TRAP)
2174               /* If the branch reads a register that the previous
2175                  instruction sets, we can not swap.  */
2176               || (! mips_opts.mips16
2177                   && (prev_pinfo & INSN_WRITE_GPR_T)
2178                   && insn_uses_reg (ip,
2179                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2180                                      & OP_MASK_RT),
2181                                     MIPS_GR_REG))
2182               || (! mips_opts.mips16
2183                   && (prev_pinfo & INSN_WRITE_GPR_D)
2184                   && insn_uses_reg (ip,
2185                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2186                                      & OP_MASK_RD),
2187                                     MIPS_GR_REG))
2188               || (mips_opts.mips16
2189                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2190                        && insn_uses_reg (ip,
2191                                          ((prev_insn.insn_opcode
2192                                            >> MIPS16OP_SH_RX)
2193                                           & MIPS16OP_MASK_RX),
2194                                          MIPS16_REG))
2195                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2196                           && insn_uses_reg (ip,
2197                                             ((prev_insn.insn_opcode
2198                                               >> MIPS16OP_SH_RY)
2199                                              & MIPS16OP_MASK_RY),
2200                                             MIPS16_REG))
2201                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2202                           && insn_uses_reg (ip,
2203                                             ((prev_insn.insn_opcode
2204                                               >> MIPS16OP_SH_RZ)
2205                                              & MIPS16OP_MASK_RZ),
2206                                             MIPS16_REG))
2207                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2208                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2209                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2210                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2211                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2212                           && insn_uses_reg (ip,
2213                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2214                                                                      insn_opcode),
2215                                             MIPS_GR_REG))))
2216               /* If the branch writes a register that the previous
2217                  instruction sets, we can not swap (we know that
2218                  branches write only to RD or to $31).  */
2219               || (! mips_opts.mips16
2220                   && (prev_pinfo & INSN_WRITE_GPR_T)
2221                   && (((pinfo & INSN_WRITE_GPR_D)
2222                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2223                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2224                       || ((pinfo & INSN_WRITE_GPR_31)
2225                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2226                                & OP_MASK_RT)
2227                               == RA))))
2228               || (! mips_opts.mips16
2229                   && (prev_pinfo & INSN_WRITE_GPR_D)
2230                   && (((pinfo & INSN_WRITE_GPR_D)
2231                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2232                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2233                       || ((pinfo & INSN_WRITE_GPR_31)
2234                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2235                                & OP_MASK_RD)
2236                               == RA))))
2237               || (mips_opts.mips16
2238                   && (pinfo & MIPS16_INSN_WRITE_31)
2239                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2240                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2241                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2242                               == RA))))
2243               /* If the branch writes a register that the previous
2244                  instruction reads, we can not swap (we know that
2245                  branches only write to RD or to $31).  */
2246               || (! mips_opts.mips16
2247                   && (pinfo & INSN_WRITE_GPR_D)
2248                   && insn_uses_reg (&prev_insn,
2249                                     ((ip->insn_opcode >> OP_SH_RD)
2250                                      & OP_MASK_RD),
2251                                     MIPS_GR_REG))
2252               || (! mips_opts.mips16
2253                   && (pinfo & INSN_WRITE_GPR_31)
2254                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2255               || (mips_opts.mips16
2256                   && (pinfo & MIPS16_INSN_WRITE_31)
2257                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2258               /* If we are generating embedded PIC code, the branch
2259                  might be expanded into a sequence which uses $at, so
2260                  we can't swap with an instruction which reads it.  */
2261               || (mips_pic == EMBEDDED_PIC
2262                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2263               /* If the previous previous instruction has a load
2264                  delay, and sets a register that the branch reads, we
2265                  can not swap.  */
2266               || (! mips_opts.mips16
2267                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2268               /* Itbl support may require additional care here.  */
2269                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2270                       || (! gpr_interlocks
2271                           && (prev_prev_insn.insn_mo->pinfo
2272                               & INSN_LOAD_MEMORY_DELAY)))
2273                   && insn_uses_reg (ip,
2274                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2275                                      & OP_MASK_RT),
2276                                     MIPS_GR_REG))
2277               /* If one instruction sets a condition code and the
2278                  other one uses a condition code, we can not swap.  */
2279               || ((pinfo & INSN_READ_COND_CODE)
2280                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2281               || ((pinfo & INSN_WRITE_COND_CODE)
2282                   && (prev_pinfo & INSN_READ_COND_CODE))
2283               /* If the previous instruction uses the PC, we can not
2284                  swap.  */
2285               || (mips_opts.mips16
2286                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2287               /* If the previous instruction was extended, we can not
2288                  swap.  */
2289               || (mips_opts.mips16 && prev_insn_extended)
2290               /* If the previous instruction had a fixup in mips16
2291                  mode, we can not swap.  This normally means that the
2292                  previous instruction was a 4 byte branch anyhow.  */
2293               || (mips_opts.mips16 && prev_insn_fixp[0])
2294               /* If the previous instruction is a sync, sync.l, or
2295                  sync.p, we can not swap.  */
2296               || (prev_pinfo & INSN_SYNC))
2297             {
2298               /* We could do even better for unconditional branches to
2299                  portions of this object file; we could pick up the
2300                  instruction at the destination, put it in the delay
2301                  slot, and bump the destination address.  */
2302               emit_nop ();
2303               /* Update the previous insn information.  */
2304               prev_prev_insn = *ip;
2305               prev_insn.insn_mo = &dummy_opcode;
2306             }
2307           else
2308             {
2309               /* It looks like we can actually do the swap.  */
2310               if (! mips_opts.mips16)
2311                 {
2312                   char *prev_f;
2313                   char temp[4];
2314
2315                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2316                   memcpy (temp, prev_f, 4);
2317                   memcpy (prev_f, f, 4);
2318                   memcpy (f, temp, 4);
2319                   if (prev_insn_fixp[0])
2320                     {
2321                       prev_insn_fixp[0]->fx_frag = frag_now;
2322                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2323                     }
2324                   if (prev_insn_fixp[1])
2325                     {
2326                       prev_insn_fixp[1]->fx_frag = frag_now;
2327                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2328                     }
2329                   if (prev_insn_fixp[2])
2330                     {
2331                       prev_insn_fixp[2]->fx_frag = frag_now;
2332                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2333                     }
2334                   if (fixp[0])
2335                     {
2336                       fixp[0]->fx_frag = prev_insn_frag;
2337                       fixp[0]->fx_where = prev_insn_where;
2338                     }
2339                   if (fixp[1])
2340                     {
2341                       fixp[1]->fx_frag = prev_insn_frag;
2342                       fixp[1]->fx_where = prev_insn_where;
2343                     }
2344                   if (fixp[2])
2345                     {
2346                       fixp[2]->fx_frag = prev_insn_frag;
2347                       fixp[2]->fx_where = prev_insn_where;
2348                     }
2349                 }
2350               else
2351                 {
2352                   char *prev_f;
2353                   char temp[2];
2354
2355                   assert (prev_insn_fixp[0] == NULL);
2356                   assert (prev_insn_fixp[1] == NULL);
2357                   assert (prev_insn_fixp[2] == NULL);
2358                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2359                   memcpy (temp, prev_f, 2);
2360                   memcpy (prev_f, f, 2);
2361                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2362                     {
2363                       assert (*reloc_type == BFD_RELOC_UNUSED);
2364                       memcpy (f, temp, 2);
2365                     }
2366                   else
2367                     {
2368                       memcpy (f, f + 2, 2);
2369                       memcpy (f + 2, temp, 2);
2370                     }
2371                   if (fixp[0])
2372                     {
2373                       fixp[0]->fx_frag = prev_insn_frag;
2374                       fixp[0]->fx_where = prev_insn_where;
2375                     }
2376                   if (fixp[1])
2377                     {
2378                       fixp[1]->fx_frag = prev_insn_frag;
2379                       fixp[1]->fx_where = prev_insn_where;
2380                     }
2381                   if (fixp[2])
2382                     {
2383                       fixp[2]->fx_frag = prev_insn_frag;
2384                       fixp[2]->fx_where = prev_insn_where;
2385                     }
2386                 }
2387
2388               /* Update the previous insn information; leave prev_insn
2389                  unchanged.  */
2390               prev_prev_insn = *ip;
2391             }
2392           prev_insn_is_delay_slot = 1;
2393
2394           /* If that was an unconditional branch, forget the previous
2395              insn information.  */
2396           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2397             {
2398               prev_prev_insn.insn_mo = &dummy_opcode;
2399               prev_insn.insn_mo = &dummy_opcode;
2400             }
2401
2402           prev_insn_fixp[0] = NULL;
2403           prev_insn_fixp[1] = NULL;
2404           prev_insn_fixp[2] = NULL;
2405           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2406           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2407           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2408           prev_insn_extended = 0;
2409         }
2410       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2411         {
2412           /* We don't yet optimize a branch likely.  What we should do
2413              is look at the target, copy the instruction found there
2414              into the delay slot, and increment the branch to jump to
2415              the next instruction.  */
2416           emit_nop ();
2417           /* Update the previous insn information.  */
2418           prev_prev_insn = *ip;
2419           prev_insn.insn_mo = &dummy_opcode;
2420           prev_insn_fixp[0] = NULL;
2421           prev_insn_fixp[1] = NULL;
2422           prev_insn_fixp[2] = NULL;
2423           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2424           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2425           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2426           prev_insn_extended = 0;
2427         }
2428       else
2429         {
2430           /* Update the previous insn information.  */
2431           if (nops > 0)
2432             prev_prev_insn.insn_mo = &dummy_opcode;
2433           else
2434             prev_prev_insn = prev_insn;
2435           prev_insn = *ip;
2436
2437           /* Any time we see a branch, we always fill the delay slot
2438              immediately; since this insn is not a branch, we know it
2439              is not in a delay slot.  */
2440           prev_insn_is_delay_slot = 0;
2441
2442           prev_insn_fixp[0] = fixp[0];
2443           prev_insn_fixp[1] = fixp[1];
2444           prev_insn_fixp[2] = fixp[2];
2445           prev_insn_reloc_type[0] = reloc_type[0];
2446           prev_insn_reloc_type[1] = reloc_type[1];
2447           prev_insn_reloc_type[2] = reloc_type[2];
2448           if (mips_opts.mips16)
2449             prev_insn_extended = (ip->use_extend
2450                                   || *reloc_type > BFD_RELOC_UNUSED);
2451         }
2452
2453       prev_prev_insn_unreordered = prev_insn_unreordered;
2454       prev_insn_unreordered = 0;
2455       prev_insn_frag = frag_now;
2456       prev_insn_where = f - frag_now->fr_literal;
2457       prev_insn_valid = 1;
2458     }
2459   else if (place == NULL)
2460     {
2461       /* We need to record a bit of information even when we are not
2462          reordering, in order to determine the base address for mips16
2463          PC relative relocs.  */
2464       prev_prev_insn = prev_insn;
2465       prev_insn = *ip;
2466       prev_insn_reloc_type[0] = reloc_type[0];
2467       prev_insn_reloc_type[1] = reloc_type[1];
2468       prev_insn_reloc_type[2] = reloc_type[2];
2469       prev_prev_insn_unreordered = prev_insn_unreordered;
2470       prev_insn_unreordered = 1;
2471     }
2472
2473   /* We just output an insn, so the next one doesn't have a label.  */
2474   mips_clear_insn_labels ();
2475
2476   /* We must ensure that a fixup associated with an unmatched %hi
2477      reloc does not become a variant frag.  Otherwise, the
2478      rearrangement of %hi relocs in frob_file may confuse
2479      tc_gen_reloc.  */
2480   if (unmatched_hi)
2481     {
2482       frag_wane (frag_now);
2483       frag_new (0);
2484     }
2485 }
2486
2487 /* This function forgets that there was any previous instruction or
2488    label.  If PRESERVE is non-zero, it remembers enough information to
2489    know whether nops are needed before a noreorder section.  */
2490
2491 static void
2492 mips_no_prev_insn (preserve)
2493      int preserve;
2494 {
2495   if (! preserve)
2496     {
2497       prev_insn.insn_mo = &dummy_opcode;
2498       prev_prev_insn.insn_mo = &dummy_opcode;
2499       prev_nop_frag = NULL;
2500       prev_nop_frag_holds = 0;
2501       prev_nop_frag_required = 0;
2502       prev_nop_frag_since = 0;
2503     }
2504   prev_insn_valid = 0;
2505   prev_insn_is_delay_slot = 0;
2506   prev_insn_unreordered = 0;
2507   prev_insn_extended = 0;
2508   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2509   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2510   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2511   prev_prev_insn_unreordered = 0;
2512   mips_clear_insn_labels ();
2513 }
2514
2515 /* This function must be called whenever we turn on noreorder or emit
2516    something other than instructions.  It inserts any NOPS which might
2517    be needed by the previous instruction, and clears the information
2518    kept for the previous instructions.  The INSNS parameter is true if
2519    instructions are to follow.  */
2520
2521 static void
2522 mips_emit_delays (insns)
2523      boolean insns;
2524 {
2525   if (! mips_opts.noreorder)
2526     {
2527       int nops;
2528
2529       nops = 0;
2530       if ((! mips_opts.mips16
2531            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2532            && (! cop_interlocks
2533                && (prev_insn.insn_mo->pinfo
2534                    & (INSN_LOAD_COPROC_DELAY
2535                       | INSN_COPROC_MOVE_DELAY
2536                       | INSN_WRITE_COND_CODE))))
2537           || (! hilo_interlocks
2538               && (prev_insn.insn_mo->pinfo
2539                   & (INSN_READ_LO
2540                      | INSN_READ_HI)))
2541           || (! mips_opts.mips16
2542               && ! gpr_interlocks
2543               && (prev_insn.insn_mo->pinfo
2544                   & INSN_LOAD_MEMORY_DELAY))
2545           || (! mips_opts.mips16
2546               && mips_opts.isa == ISA_MIPS1
2547               && (prev_insn.insn_mo->pinfo
2548                   & INSN_COPROC_MEMORY_DELAY)))
2549         {
2550           /* Itbl support may require additional care here.  */
2551           ++nops;
2552           if ((! mips_opts.mips16
2553                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2554                && (! cop_interlocks
2555                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2556               || (! hilo_interlocks
2557                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2558                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2559             ++nops;
2560
2561           if (prev_insn_unreordered)
2562             nops = 0;
2563         }
2564       else if ((! mips_opts.mips16
2565                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2566                 && (! cop_interlocks
2567                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2568                || (! hilo_interlocks
2569                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2570                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2571         {
2572           /* Itbl support may require additional care here.  */
2573           if (! prev_prev_insn_unreordered)
2574             ++nops;
2575         }
2576
2577       if (nops > 0)
2578         {
2579           struct insn_label_list *l;
2580
2581           if (insns)
2582             {
2583               /* Record the frag which holds the nop instructions, so
2584                  that we can remove them if we don't need them.  */
2585               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2586               prev_nop_frag = frag_now;
2587               prev_nop_frag_holds = nops;
2588               prev_nop_frag_required = 0;
2589               prev_nop_frag_since = 0;
2590             }
2591
2592           for (; nops > 0; --nops)
2593             emit_nop ();
2594
2595           if (insns)
2596             {
2597               /* Move on to a new frag, so that it is safe to simply
2598                  decrease the size of prev_nop_frag.  */
2599               frag_wane (frag_now);
2600               frag_new (0);
2601             }
2602
2603           for (l = insn_labels; l != NULL; l = l->next)
2604             {
2605               valueT val;
2606
2607               assert (S_GET_SEGMENT (l->label) == now_seg);
2608               symbol_set_frag (l->label, frag_now);
2609               val = (valueT) frag_now_fix ();
2610               /* mips16 text labels are stored as odd.  */
2611               if (mips_opts.mips16)
2612                 ++val;
2613               S_SET_VALUE (l->label, val);
2614             }
2615         }
2616     }
2617
2618   /* Mark instruction labels in mips16 mode.  */
2619   if (insns)
2620     mips16_mark_labels ();
2621
2622   mips_no_prev_insn (insns);
2623 }
2624
2625 /* Build an instruction created by a macro expansion.  This is passed
2626    a pointer to the count of instructions created so far, an
2627    expression, the name of the instruction to build, an operand format
2628    string, and corresponding arguments.  */
2629
2630 #ifdef USE_STDARG
2631 static void
2632 macro_build (char *place,
2633              int *counter,
2634              expressionS * ep,
2635              const char *name,
2636              const char *fmt,
2637              ...)
2638 #else
2639 static void
2640 macro_build (place, counter, ep, name, fmt, va_alist)
2641      char *place;
2642      int *counter;
2643      expressionS *ep;
2644      const char *name;
2645      const char *fmt;
2646      va_dcl
2647 #endif
2648 {
2649   struct mips_cl_insn insn;
2650   bfd_reloc_code_real_type r[3];
2651   va_list args;
2652
2653 #ifdef USE_STDARG
2654   va_start (args, fmt);
2655 #else
2656   va_start (args);
2657 #endif
2658
2659   /*
2660    * If the macro is about to expand into a second instruction,
2661    * print a warning if needed. We need to pass ip as a parameter
2662    * to generate a better warning message here...
2663    */
2664   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2665     as_warn (_("Macro instruction expanded into multiple instructions"));
2666
2667   /*
2668    * If the macro is about to expand into a second instruction,
2669    * and it is in a delay slot, print a warning.
2670    */
2671   if (place == NULL
2672       && *counter == 1
2673       && mips_opts.noreorder
2674       && (prev_prev_insn.insn_mo->pinfo
2675           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2676              | INSN_COND_BRANCH_LIKELY)) != 0)
2677     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2678
2679   if (place == NULL)
2680     ++*counter;         /* bump instruction counter */
2681
2682   if (mips_opts.mips16)
2683     {
2684       mips16_macro_build (place, counter, ep, name, fmt, args);
2685       va_end (args);
2686       return;
2687     }
2688
2689   r[0] = BFD_RELOC_UNUSED;
2690   r[1] = BFD_RELOC_UNUSED;
2691   r[2] = BFD_RELOC_UNUSED;
2692   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2693   assert (insn.insn_mo);
2694   assert (strcmp (name, insn.insn_mo->name) == 0);
2695
2696   /* Search until we get a match for NAME.  */
2697   while (1)
2698     {
2699       /* It is assumed here that macros will never generate 
2700          MDMX or MIPS-3D instructions.  */
2701       if (strcmp (fmt, insn.insn_mo->args) == 0
2702           && insn.insn_mo->pinfo != INSN_MACRO
2703           && OPCODE_IS_MEMBER (insn.insn_mo,
2704                                (mips_opts.isa
2705                                 | (mips_opts.mips16 ? INSN_MIPS16 : 0)),
2706                                mips_arch)
2707           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2708         break;
2709
2710       ++insn.insn_mo;
2711       assert (insn.insn_mo->name);
2712       assert (strcmp (name, insn.insn_mo->name) == 0);
2713     }
2714
2715   insn.insn_opcode = insn.insn_mo->match;
2716   for (;;)
2717     {
2718       switch (*fmt++)
2719         {
2720         case '\0':
2721           break;
2722
2723         case ',':
2724         case '(':
2725         case ')':
2726           continue;
2727
2728         case 't':
2729         case 'w':
2730         case 'E':
2731           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2732           continue;
2733
2734         case 'c':
2735           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2736           continue;
2737
2738         case 'T':
2739         case 'W':
2740           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2741           continue;
2742
2743         case 'd':
2744         case 'G':
2745           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2746           continue;
2747
2748         case 'U':
2749           {
2750             int tmp = va_arg (args, int);
2751
2752             insn.insn_opcode |= tmp << OP_SH_RT;
2753             insn.insn_opcode |= tmp << OP_SH_RD;
2754             continue;
2755           }
2756
2757         case 'V':
2758         case 'S':
2759           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2760           continue;
2761
2762         case 'z':
2763           continue;
2764
2765         case '<':
2766           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2767           continue;
2768
2769         case 'D':
2770           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2771           continue;
2772
2773         case 'B':
2774           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2775           continue;
2776
2777         case 'J':
2778           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2779           continue;
2780
2781         case 'q':
2782           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2783           continue;
2784
2785         case 'b':
2786         case 's':
2787         case 'r':
2788         case 'v':
2789           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2790           continue;
2791
2792         case 'i':
2793         case 'j':
2794         case 'o':
2795           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2796           assert (*r == BFD_RELOC_GPREL16
2797                   || *r == BFD_RELOC_MIPS_LITERAL
2798                   || *r == BFD_RELOC_MIPS_HIGHER
2799                   || *r == BFD_RELOC_HI16_S
2800                   || *r == BFD_RELOC_LO16
2801                   || *r == BFD_RELOC_MIPS_GOT16
2802                   || *r == BFD_RELOC_MIPS_CALL16
2803                   || *r == BFD_RELOC_MIPS_GOT_DISP
2804                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2805                   || *r == BFD_RELOC_MIPS_GOT_OFST
2806                   || *r == BFD_RELOC_MIPS_GOT_LO16
2807                   || *r == BFD_RELOC_MIPS_CALL_LO16
2808                   || (ep->X_op == O_subtract
2809                       && *r == BFD_RELOC_PCREL_LO16));
2810           continue;
2811
2812         case 'u':
2813           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2814           assert (ep != NULL
2815                   && (ep->X_op == O_constant
2816                       || (ep->X_op == O_symbol
2817                           && (*r == BFD_RELOC_MIPS_HIGHEST
2818                               || *r == BFD_RELOC_HI16_S
2819                               || *r == BFD_RELOC_HI16
2820                               || *r == BFD_RELOC_GPREL16
2821                               || *r == BFD_RELOC_MIPS_GOT_HI16
2822                               || *r == BFD_RELOC_MIPS_CALL_HI16))
2823                       || (ep->X_op == O_subtract
2824                           && *r == BFD_RELOC_PCREL_HI16_S)));
2825           continue;
2826
2827         case 'p':
2828           assert (ep != NULL);
2829           /*
2830            * This allows macro() to pass an immediate expression for
2831            * creating short branches without creating a symbol.
2832            * Note that the expression still might come from the assembly
2833            * input, in which case the value is not checked for range nor
2834            * is a relocation entry generated (yuck).
2835            */
2836           if (ep->X_op == O_constant)
2837             {
2838               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2839               ep = NULL;
2840             }
2841           else
2842             if (mips_pic == EMBEDDED_PIC)
2843               *r = BFD_RELOC_16_PCREL_S2;
2844             else
2845               *r = BFD_RELOC_16_PCREL;
2846           continue;
2847
2848         case 'a':
2849           assert (ep != NULL);
2850           *r = BFD_RELOC_MIPS_JMP;
2851           continue;
2852
2853         case 'C':
2854           insn.insn_opcode |= va_arg (args, unsigned long);
2855           continue;
2856
2857         default:
2858           internalError ();
2859         }
2860       break;
2861     }
2862   va_end (args);
2863   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2864
2865   append_insn (place, &insn, ep, r, false);
2866 }
2867
2868 static void
2869 mips16_macro_build (place, counter, ep, name, fmt, args)
2870      char *place;
2871      int *counter ATTRIBUTE_UNUSED;
2872      expressionS *ep;
2873      const char *name;
2874      const char *fmt;
2875      va_list args;
2876 {
2877   struct mips_cl_insn insn;
2878   bfd_reloc_code_real_type r[3]
2879     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2880
2881   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2882   assert (insn.insn_mo);
2883   assert (strcmp (name, insn.insn_mo->name) == 0);
2884
2885   while (strcmp (fmt, insn.insn_mo->args) != 0
2886          || insn.insn_mo->pinfo == INSN_MACRO)
2887     {
2888       ++insn.insn_mo;
2889       assert (insn.insn_mo->name);
2890       assert (strcmp (name, insn.insn_mo->name) == 0);
2891     }
2892
2893   insn.insn_opcode = insn.insn_mo->match;
2894   insn.use_extend = false;
2895
2896   for (;;)
2897     {
2898       int c;
2899
2900       c = *fmt++;
2901       switch (c)
2902         {
2903         case '\0':
2904           break;
2905
2906         case ',':
2907         case '(':
2908         case ')':
2909           continue;
2910
2911         case 'y':
2912         case 'w':
2913           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2914           continue;
2915
2916         case 'x':
2917         case 'v':
2918           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2919           continue;
2920
2921         case 'z':
2922           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2923           continue;
2924
2925         case 'Z':
2926           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2927           continue;
2928
2929         case '0':
2930         case 'S':
2931         case 'P':
2932         case 'R':
2933           continue;
2934
2935         case 'X':
2936           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2937           continue;
2938
2939         case 'Y':
2940           {
2941             int regno;
2942
2943             regno = va_arg (args, int);
2944             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2945             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2946           }
2947           continue;
2948
2949         case '<':
2950         case '>':
2951         case '4':
2952         case '5':
2953         case 'H':
2954         case 'W':
2955         case 'D':
2956         case 'j':
2957         case '8':
2958         case 'V':
2959         case 'C':
2960         case 'U':
2961         case 'k':
2962         case 'K':
2963         case 'p':
2964         case 'q':
2965           {
2966             assert (ep != NULL);
2967
2968             if (ep->X_op != O_constant)
2969               *r = (int) BFD_RELOC_UNUSED + c;
2970             else
2971               {
2972                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2973                               false, &insn.insn_opcode, &insn.use_extend,
2974                               &insn.extend);
2975                 ep = NULL;
2976                 *r = BFD_RELOC_UNUSED;
2977               }
2978           }
2979           continue;
2980
2981         case '6':
2982           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2983           continue;
2984         }
2985
2986       break;
2987     }
2988
2989   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2990
2991   append_insn (place, &insn, ep, r, false);
2992 }
2993
2994 /*
2995  * Generate a "jalr" instruction with a relocation hint to the called
2996  * function.  This occurs in NewABI PIC code.
2997  */
2998 static void
2999 macro_build_jalr (icnt, ep)
3000      int icnt;
3001      expressionS *ep;
3002 {
3003   if (HAVE_NEWABI)
3004     frag_more (0);
3005   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3006                RA, PIC_CALL_REG);
3007   if (HAVE_NEWABI)
3008     fix_new_exp (frag_now, 0, 0, ep, false, BFD_RELOC_MIPS_JALR);
3009 }
3010
3011 /*
3012  * Generate a "lui" instruction.
3013  */
3014 static void
3015 macro_build_lui (place, counter, ep, regnum)
3016      char *place;
3017      int *counter;
3018      expressionS *ep;
3019      int regnum;
3020 {
3021   expressionS high_expr;
3022   struct mips_cl_insn insn;
3023   bfd_reloc_code_real_type r[3]
3024     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3025   const char *name = "lui";
3026   const char *fmt = "t,u";
3027
3028   assert (! mips_opts.mips16);
3029
3030   if (place == NULL)
3031     high_expr = *ep;
3032   else
3033     {
3034       high_expr.X_op = O_constant;
3035       high_expr.X_add_number = ep->X_add_number;
3036     }
3037
3038   if (high_expr.X_op == O_constant)
3039     {
3040       /* we can compute the instruction now without a relocation entry */
3041       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3042                                 >> 16) & 0xffff;
3043       *r = BFD_RELOC_UNUSED;
3044     }
3045   else if (! HAVE_NEWABI)
3046     {
3047       assert (ep->X_op == O_symbol);
3048       /* _gp_disp is a special case, used from s_cpload.  */
3049       assert (mips_pic == NO_PIC
3050               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3051       *r = BFD_RELOC_HI16_S;
3052     }
3053
3054   /*
3055    * If the macro is about to expand into a second instruction,
3056    * print a warning if needed. We need to pass ip as a parameter
3057    * to generate a better warning message here...
3058    */
3059   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3060     as_warn (_("Macro instruction expanded into multiple instructions"));
3061
3062   if (place == NULL)
3063     ++*counter;         /* bump instruction counter */
3064
3065   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3066   assert (insn.insn_mo);
3067   assert (strcmp (name, insn.insn_mo->name) == 0);
3068   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3069
3070   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3071   if (*r == BFD_RELOC_UNUSED)
3072     {
3073       insn.insn_opcode |= high_expr.X_add_number;
3074       append_insn (place, &insn, NULL, r, false);
3075     }
3076   else
3077     append_insn (place, &insn, &high_expr, r, false);
3078 }
3079
3080 /*                      set_at()
3081  * Generates code to set the $at register to true (one)
3082  * if reg is less than the immediate expression.
3083  */
3084 static void
3085 set_at (counter, reg, unsignedp)
3086      int *counter;
3087      int reg;
3088      int unsignedp;
3089 {
3090   if (imm_expr.X_op == O_constant
3091       && imm_expr.X_add_number >= -0x8000
3092       && imm_expr.X_add_number < 0x8000)
3093     macro_build ((char *) NULL, counter, &imm_expr,
3094                  unsignedp ? "sltiu" : "slti",
3095                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3096   else
3097     {
3098       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3099       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3100                    unsignedp ? "sltu" : "slt",
3101                    "d,v,t", AT, reg, AT);
3102     }
3103 }
3104
3105 /* Warn if an expression is not a constant.  */
3106
3107 static void
3108 check_absolute_expr (ip, ex)
3109      struct mips_cl_insn *ip;
3110      expressionS *ex;
3111 {
3112   if (ex->X_op == O_big)
3113     as_bad (_("unsupported large constant"));
3114   else if (ex->X_op != O_constant)
3115     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3116 }
3117
3118 /* Count the leading zeroes by performing a binary chop. This is a
3119    bulky bit of source, but performance is a LOT better for the
3120    majority of values than a simple loop to count the bits:
3121        for (lcnt = 0; (lcnt < 32); lcnt++)
3122          if ((v) & (1 << (31 - lcnt)))
3123            break;
3124   However it is not code size friendly, and the gain will drop a bit
3125   on certain cached systems.
3126 */
3127 #define COUNT_TOP_ZEROES(v)             \
3128   (((v) & ~0xffff) == 0                 \
3129    ? ((v) & ~0xff) == 0                 \
3130      ? ((v) & ~0xf) == 0                \
3131        ? ((v) & ~0x3) == 0              \
3132          ? ((v) & ~0x1) == 0            \
3133            ? !(v)                       \
3134              ? 32                       \
3135              : 31                       \
3136            : 30                         \
3137          : ((v) & ~0x7) == 0            \
3138            ? 29                         \
3139            : 28                         \
3140        : ((v) & ~0x3f) == 0             \
3141          ? ((v) & ~0x1f) == 0           \
3142            ? 27                         \
3143            : 26                         \
3144          : ((v) & ~0x7f) == 0           \
3145            ? 25                         \
3146            : 24                         \
3147      : ((v) & ~0xfff) == 0              \
3148        ? ((v) & ~0x3ff) == 0            \
3149          ? ((v) & ~0x1ff) == 0          \
3150            ? 23                         \
3151            : 22                         \
3152          : ((v) & ~0x7ff) == 0          \
3153            ? 21                         \
3154            : 20                         \
3155        : ((v) & ~0x3fff) == 0           \
3156          ? ((v) & ~0x1fff) == 0         \
3157            ? 19                         \
3158            : 18                         \
3159          : ((v) & ~0x7fff) == 0         \
3160            ? 17                         \
3161            : 16                         \
3162    : ((v) & ~0xffffff) == 0             \
3163      ? ((v) & ~0xfffff) == 0            \
3164        ? ((v) & ~0x3ffff) == 0          \
3165          ? ((v) & ~0x1ffff) == 0        \
3166            ? 15                         \
3167            : 14                         \
3168          : ((v) & ~0x7ffff) == 0        \
3169            ? 13                         \
3170            : 12                         \
3171        : ((v) & ~0x3fffff) == 0         \
3172          ? ((v) & ~0x1fffff) == 0       \
3173            ? 11                         \
3174            : 10                         \
3175          : ((v) & ~0x7fffff) == 0       \
3176            ? 9                          \
3177            : 8                          \
3178      : ((v) & ~0xfffffff) == 0          \
3179        ? ((v) & ~0x3ffffff) == 0        \
3180          ? ((v) & ~0x1ffffff) == 0      \
3181            ? 7                          \
3182            : 6                          \
3183          : ((v) & ~0x7ffffff) == 0      \
3184            ? 5                          \
3185            : 4                          \
3186        : ((v) & ~0x3fffffff) == 0       \
3187          ? ((v) & ~0x1fffffff) == 0     \
3188            ? 3                          \
3189            : 2                          \
3190          : ((v) & ~0x7fffffff) == 0     \
3191            ? 1                          \
3192            : 0)
3193
3194 /* Is the given value a sign-extended 32-bit value?  */
3195 #define IS_SEXT_32BIT_NUM(x)                                            \
3196   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
3197    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3198
3199 /*                      load_register()
3200  *  This routine generates the least number of instructions neccessary to load
3201  *  an absolute expression value into a register.
3202  */
3203 static void
3204 load_register (counter, reg, ep, dbl)
3205      int *counter;
3206      int reg;
3207      expressionS *ep;
3208      int dbl;
3209 {
3210   int freg;
3211   expressionS hi32, lo32;
3212
3213   if (ep->X_op != O_big)
3214     {
3215       assert (ep->X_op == O_constant);
3216       if (ep->X_add_number < 0x8000
3217           && (ep->X_add_number >= 0
3218               || (ep->X_add_number >= -0x8000
3219                   && (! dbl
3220                       || ! ep->X_unsigned
3221                       || sizeof (ep->X_add_number) > 4))))
3222         {
3223           /* We can handle 16 bit signed values with an addiu to
3224              $zero.  No need to ever use daddiu here, since $zero and
3225              the result are always correct in 32 bit mode.  */
3226           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3227                        (int) BFD_RELOC_LO16);
3228           return;
3229         }
3230       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3231         {
3232           /* We can handle 16 bit unsigned values with an ori to
3233              $zero.  */
3234           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3235                        (int) BFD_RELOC_LO16);
3236           return;
3237         }
3238       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3239                 && (! dbl
3240                     || ! ep->X_unsigned
3241                     || sizeof (ep->X_add_number) > 4
3242                     || (ep->X_add_number & 0x80000000) == 0))
3243                || ((HAVE_32BIT_GPRS || ! dbl)
3244                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3245                || (HAVE_32BIT_GPRS
3246                    && ! dbl
3247                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3248                        == ~ (offsetT) 0xffffffff)))
3249         {
3250           /* 32 bit values require an lui.  */
3251           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3252                        (int) BFD_RELOC_HI16);
3253           if ((ep->X_add_number & 0xffff) != 0)
3254             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3255                          (int) BFD_RELOC_LO16);
3256           return;
3257         }
3258     }
3259
3260   /* The value is larger than 32 bits.  */
3261
3262   if (HAVE_32BIT_GPRS)
3263     {
3264       as_bad (_("Number (0x%lx) larger than 32 bits"),
3265               (unsigned long) ep->X_add_number);
3266       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3267                    (int) BFD_RELOC_LO16);
3268       return;
3269     }
3270
3271   if (ep->X_op != O_big)
3272     {
3273       hi32 = *ep;
3274       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3275       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3276       hi32.X_add_number &= 0xffffffff;
3277       lo32 = *ep;
3278       lo32.X_add_number &= 0xffffffff;
3279     }
3280   else
3281     {
3282       assert (ep->X_add_number > 2);
3283       if (ep->X_add_number == 3)
3284         generic_bignum[3] = 0;
3285       else if (ep->X_add_number > 4)
3286         as_bad (_("Number larger than 64 bits"));
3287       lo32.X_op = O_constant;
3288       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3289       hi32.X_op = O_constant;
3290       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3291     }
3292
3293   if (hi32.X_add_number == 0)
3294     freg = 0;
3295   else
3296     {
3297       int shift, bit;
3298       unsigned long hi, lo;
3299
3300       if (hi32.X_add_number == (offsetT) 0xffffffff)
3301         {
3302           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3303             {
3304               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3305                            reg, 0, (int) BFD_RELOC_LO16);
3306               return;
3307             }
3308           if (lo32.X_add_number & 0x80000000)
3309             {
3310               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3311                            (int) BFD_RELOC_HI16);
3312               if (lo32.X_add_number & 0xffff)
3313                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3314                              reg, reg, (int) BFD_RELOC_LO16);
3315               return;
3316             }
3317         }
3318
3319       /* Check for 16bit shifted constant.  We know that hi32 is
3320          non-zero, so start the mask on the first bit of the hi32
3321          value.  */
3322       shift = 17;
3323       do
3324         {
3325           unsigned long himask, lomask;
3326
3327           if (shift < 32)
3328             {
3329               himask = 0xffff >> (32 - shift);
3330               lomask = (0xffff << shift) & 0xffffffff;
3331             }
3332           else
3333             {
3334               himask = 0xffff << (shift - 32);
3335               lomask = 0;
3336             }
3337           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3338               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3339             {
3340               expressionS tmp;
3341
3342               tmp.X_op = O_constant;
3343               if (shift < 32)
3344                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3345                                     | (lo32.X_add_number >> shift));
3346               else
3347                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3348               macro_build ((char *) NULL, counter, &tmp,
3349                            "ori", "t,r,i", reg, 0,
3350                            (int) BFD_RELOC_LO16);
3351               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3352                            (shift >= 32) ? "dsll32" : "dsll",
3353                            "d,w,<", reg, reg,
3354                            (shift >= 32) ? shift - 32 : shift);
3355               return;
3356             }
3357           ++shift;
3358         }
3359       while (shift <= (64 - 16));
3360
3361       /* Find the bit number of the lowest one bit, and store the
3362          shifted value in hi/lo.  */
3363       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3364       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3365       if (lo != 0)
3366         {
3367           bit = 0;
3368           while ((lo & 1) == 0)
3369             {
3370               lo >>= 1;
3371               ++bit;
3372             }
3373           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3374           hi >>= bit;
3375         }
3376       else
3377         {
3378           bit = 32;
3379           while ((hi & 1) == 0)
3380             {
3381               hi >>= 1;
3382               ++bit;
3383             }
3384           lo = hi;
3385           hi = 0;
3386         }
3387
3388       /* Optimize if the shifted value is a (power of 2) - 1.  */
3389       if ((hi == 0 && ((lo + 1) & lo) == 0)
3390           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3391         {
3392           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3393           if (shift != 0)
3394             {
3395               expressionS tmp;
3396
3397               /* This instruction will set the register to be all
3398                  ones.  */
3399               tmp.X_op = O_constant;
3400               tmp.X_add_number = (offsetT) -1;
3401               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3402                            reg, 0, (int) BFD_RELOC_LO16);
3403               if (bit != 0)
3404                 {
3405                   bit += shift;
3406                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3407                                (bit >= 32) ? "dsll32" : "dsll",
3408                                "d,w,<", reg, reg,
3409                                (bit >= 32) ? bit - 32 : bit);
3410                 }
3411               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3412                            (shift >= 32) ? "dsrl32" : "dsrl",
3413                            "d,w,<", reg, reg,
3414                            (shift >= 32) ? shift - 32 : shift);
3415               return;
3416             }
3417         }
3418
3419       /* Sign extend hi32 before calling load_register, because we can
3420          generally get better code when we load a sign extended value.  */
3421       if ((hi32.X_add_number & 0x80000000) != 0)
3422         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3423       load_register (counter, reg, &hi32, 0);
3424       freg = reg;
3425     }
3426   if ((lo32.X_add_number & 0xffff0000) == 0)
3427     {
3428       if (freg != 0)
3429         {
3430           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3431                        "dsll32", "d,w,<", reg, freg, 0);
3432           freg = reg;
3433         }
3434     }
3435   else
3436     {
3437       expressionS mid16;
3438
3439       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3440         {
3441           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3442                        (int) BFD_RELOC_HI16);
3443           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3444                        "dsrl32", "d,w,<", reg, reg, 0);
3445           return;
3446         }
3447
3448       if (freg != 0)
3449         {
3450           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3451                        "d,w,<", reg, freg, 16);
3452           freg = reg;
3453         }
3454       mid16 = lo32;
3455       mid16.X_add_number >>= 16;
3456       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3457                    freg, (int) BFD_RELOC_LO16);
3458       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3459                    "d,w,<", reg, reg, 16);
3460       freg = reg;
3461     }
3462   if ((lo32.X_add_number & 0xffff) != 0)
3463     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3464                  (int) BFD_RELOC_LO16);
3465 }
3466
3467 /* Load an address into a register.  */
3468
3469 static void
3470 load_address (counter, reg, ep, used_at)
3471      int *counter;
3472      int reg;
3473      expressionS *ep;
3474      int *used_at;
3475 {
3476   char *p = NULL;
3477
3478   if (ep->X_op != O_constant
3479       && ep->X_op != O_symbol)
3480     {
3481       as_bad (_("expression too complex"));
3482       ep->X_op = O_constant;
3483     }
3484
3485   if (ep->X_op == O_constant)
3486     {
3487       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3488       return;
3489     }
3490
3491   if (mips_pic == NO_PIC)
3492     {
3493       /* If this is a reference to a GP relative symbol, we want
3494            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3495          Otherwise we want
3496            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3497            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3498          If we have an addend, we always use the latter form.
3499
3500          With 64bit address space and a usable $at we want
3501            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3502            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3503            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3504            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3505            dsll32       $reg,0
3506            daddu        $reg,$reg,$at
3507
3508          If $at is already in use, we use an path which is suboptimal
3509          on superscalar processors.
3510            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3511            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3512            dsll         $reg,16
3513            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3514            dsll         $reg,16
3515            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3516        */
3517       if (HAVE_64BIT_ADDRESSES)
3518         {
3519           /* We don't do GP optimization for now because RELAX_ENCODE can't
3520              hold the data for such large chunks.  */
3521
3522           if (*used_at == 0 && ! mips_opts.noat)
3523             {
3524               macro_build (p, counter, ep, "lui", "t,u",
3525                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3526               macro_build (p, counter, ep, "lui", "t,u",
3527                            AT, (int) BFD_RELOC_HI16_S);
3528               macro_build (p, counter, ep, "daddiu", "t,r,j",
3529                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3530               macro_build (p, counter, ep, "daddiu", "t,r,j",
3531                            AT, AT, (int) BFD_RELOC_LO16);
3532               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3533                            "d,w,<", reg, reg, 0);
3534               macro_build (p, counter, (expressionS *) NULL, "daddu",
3535                            "d,v,t", reg, reg, AT);
3536               *used_at = 1;
3537             }
3538           else
3539             {
3540               macro_build (p, counter, ep, "lui", "t,u",
3541                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3542               macro_build (p, counter, ep, "daddiu", "t,r,j",
3543                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3544               macro_build (p, counter, (expressionS *) NULL, "dsll",
3545                            "d,w,<", reg, reg, 16);
3546               macro_build (p, counter, ep, "daddiu", "t,r,j",
3547                            reg, reg, (int) BFD_RELOC_HI16_S);
3548               macro_build (p, counter, (expressionS *) NULL, "dsll",
3549                            "d,w,<", reg, reg, 16);
3550               macro_build (p, counter, ep, "daddiu", "t,r,j",
3551                            reg, reg, (int) BFD_RELOC_LO16);
3552             }
3553         }
3554       else
3555         {
3556           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3557               && ! nopic_need_relax (ep->X_add_symbol, 1))
3558             {
3559               frag_grow (20);
3560               macro_build ((char *) NULL, counter, ep,
3561                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3562                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3563               p = frag_var (rs_machine_dependent, 8, 0,
3564                             RELAX_ENCODE (4, 8, 0, 4, 0,
3565                                           mips_opts.warn_about_macros),
3566                             ep->X_add_symbol, 0, NULL);
3567             }
3568           macro_build_lui (p, counter, ep, reg);
3569           if (p != NULL)
3570             p += 4;
3571           macro_build (p, counter, ep,
3572                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3573                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3574         }
3575     }
3576   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3577     {
3578       expressionS ex;
3579
3580       /* If this is a reference to an external symbol, we want
3581            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3582          Otherwise we want
3583            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3584            nop
3585            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3586          If there is a constant, it must be added in after.  */
3587       ex.X_add_number = ep->X_add_number;
3588       ep->X_add_number = 0;
3589       frag_grow (20);
3590       macro_build ((char *) NULL, counter, ep,
3591                    HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3592                    reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3593       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3594       p = frag_var (rs_machine_dependent, 4, 0,
3595                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3596                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3597       macro_build (p, counter, ep,
3598                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3599                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3600       if (ex.X_add_number != 0)
3601         {
3602           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3603             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3604           ex.X_op = O_constant;
3605           macro_build ((char *) NULL, counter, &ex,
3606                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3607                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3608         }
3609     }
3610   else if (mips_pic == SVR4_PIC)
3611     {
3612       expressionS ex;
3613       int off;
3614
3615       /* This is the large GOT case.  If this is a reference to an
3616          external symbol, we want
3617            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3618            addu         $reg,$reg,$gp
3619            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3620          Otherwise, for a reference to a local symbol, we want
3621            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3622            nop
3623            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3624          If we have NewABI, we want
3625            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3626            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3627          If there is a constant, it must be added in after.  */
3628       ex.X_add_number = ep->X_add_number;
3629       ep->X_add_number = 0;
3630       if (HAVE_NEWABI)
3631         {
3632           macro_build ((char *) NULL, counter, ep,
3633                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3634                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3635           macro_build (p, counter, ep,
3636                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3637                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3638         }
3639       else
3640         {
3641           if (reg_needs_delay (mips_gp_register))
3642             off = 4;
3643           else
3644             off = 0;
3645           frag_grow (32);
3646           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3647                        (int) BFD_RELOC_MIPS_GOT_HI16);
3648           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3649                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3650                        reg, mips_gp_register);
3651           macro_build ((char *) NULL, counter, ep,
3652                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3653                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3654           p = frag_var (rs_machine_dependent, 12 + off, 0,
3655                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3656                                       mips_opts.warn_about_macros),
3657                         ep->X_add_symbol, 0, NULL);
3658           if (off > 0)
3659             {
3660               /* We need a nop before loading from $gp.  This special
3661                  check is required because the lui which starts the main
3662                  instruction stream does not refer to $gp, and so will not
3663                  insert the nop which may be required.  */
3664               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3665                 p += 4;
3666             }
3667           macro_build (p, counter, ep,
3668                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3669                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3670           p += 4;
3671           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3672           p += 4;
3673           macro_build (p, counter, ep,
3674                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3675                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3676         }
3677
3678       if (ex.X_add_number != 0)
3679         {
3680           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3681             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3682           ex.X_op = O_constant;
3683           macro_build ((char *) NULL, counter, &ex,
3684                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3685                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3686         }
3687     }
3688   else if (mips_pic == EMBEDDED_PIC)
3689     {
3690       /* We always do
3691            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3692        */
3693       macro_build ((char *) NULL, counter, ep,
3694                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3695                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3696     }
3697   else
3698     abort ();
3699 }
3700
3701 /* Move the contents of register SOURCE into register DEST.  */
3702
3703 static void
3704 move_register (counter, dest, source)
3705      int *counter;
3706      int dest;
3707      int source;
3708 {
3709   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3710                HAVE_32BIT_GPRS ? "addu" : "daddu",
3711                "d,v,t", dest, source, 0);
3712 }
3713
3714 /*
3715  *                      Build macros
3716  *   This routine implements the seemingly endless macro or synthesized
3717  * instructions and addressing modes in the mips assembly language. Many
3718  * of these macros are simple and are similar to each other. These could
3719  * probably be handled by some kind of table or grammer aproach instead of
3720  * this verbose method. Others are not simple macros but are more like
3721  * optimizing code generation.
3722  *   One interesting optimization is when several store macros appear
3723  * consecutivly that would load AT with the upper half of the same address.
3724  * The ensuing load upper instructions are ommited. This implies some kind
3725  * of global optimization. We currently only optimize within a single macro.
3726  *   For many of the load and store macros if the address is specified as a
3727  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3728  * first load register 'at' with zero and use it as the base register. The
3729  * mips assembler simply uses register $zero. Just one tiny optimization
3730  * we're missing.
3731  */
3732 static void
3733 macro (ip)
3734      struct mips_cl_insn *ip;
3735 {
3736   register int treg, sreg, dreg, breg;
3737   int tempreg;
3738   int mask;
3739   int icnt = 0;
3740   int used_at = 0;
3741   expressionS expr1;
3742   const char *s;
3743   const char *s2;
3744   const char *fmt;
3745   int likely = 0;
3746   int dbl = 0;
3747   int coproc = 0;
3748   int lr = 0;
3749   int imm = 0;
3750   offsetT maxnum;
3751   int off;
3752   bfd_reloc_code_real_type r;
3753   int hold_mips_optimize;
3754
3755   assert (! mips_opts.mips16);
3756
3757   treg = (ip->insn_opcode >> 16) & 0x1f;
3758   dreg = (ip->insn_opcode >> 11) & 0x1f;
3759   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3760   mask = ip->insn_mo->mask;
3761
3762   expr1.X_op = O_constant;
3763   expr1.X_op_symbol = NULL;
3764   expr1.X_add_symbol = NULL;
3765   expr1.X_add_number = 1;
3766
3767   switch (mask)
3768     {
3769     case M_DABS:
3770       dbl = 1;
3771     case M_ABS:
3772       /* bgez $a0,.+12
3773          move v0,$a0
3774          sub v0,$zero,$a0
3775          */
3776
3777       mips_emit_delays (true);
3778       ++mips_opts.noreorder;
3779       mips_any_noreorder = 1;
3780
3781       expr1.X_add_number = 8;
3782       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3783       if (dreg == sreg)
3784         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3785                      0);
3786       else
3787         move_register (&icnt, dreg, sreg);
3788       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3789                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3790
3791       --mips_opts.noreorder;
3792       return;
3793
3794     case M_ADD_I:
3795       s = "addi";
3796       s2 = "add";
3797       goto do_addi;
3798     case M_ADDU_I:
3799       s = "addiu";
3800       s2 = "addu";
3801       goto do_addi;
3802     case M_DADD_I:
3803       dbl = 1;
3804       s = "daddi";
3805       s2 = "dadd";
3806       goto do_addi;
3807     case M_DADDU_I:
3808       dbl = 1;
3809       s = "daddiu";
3810       s2 = "daddu";
3811     do_addi:
3812       if (imm_expr.X_op == O_constant
3813           && imm_expr.X_add_number >= -0x8000
3814           && imm_expr.X_add_number < 0x8000)
3815         {
3816           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3817                        (int) BFD_RELOC_LO16);
3818           return;
3819         }
3820       load_register (&icnt, AT, &imm_expr, dbl);
3821       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3822                    treg, sreg, AT);
3823       break;
3824
3825     case M_AND_I:
3826       s = "andi";
3827       s2 = "and";
3828       goto do_bit;
3829     case M_OR_I:
3830       s = "ori";
3831       s2 = "or";
3832       goto do_bit;
3833     case M_NOR_I:
3834       s = "";
3835       s2 = "nor";
3836       goto do_bit;
3837     case M_XOR_I:
3838       s = "xori";
3839       s2 = "xor";
3840     do_bit:
3841       if (imm_expr.X_op == O_constant
3842           && imm_expr.X_add_number >= 0
3843           && imm_expr.X_add_number < 0x10000)
3844         {
3845           if (mask != M_NOR_I)
3846             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3847                          sreg, (int) BFD_RELOC_LO16);
3848           else
3849             {
3850               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3851                            treg, sreg, (int) BFD_RELOC_LO16);
3852               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3853                            "d,v,t", treg, treg, 0);
3854             }
3855           return;
3856         }
3857
3858       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3859       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3860                    treg, sreg, AT);
3861       break;
3862
3863     case M_BEQ_I:
3864       s = "beq";
3865       goto beq_i;
3866     case M_BEQL_I:
3867       s = "beql";
3868       likely = 1;
3869       goto beq_i;
3870     case M_BNE_I:
3871       s = "bne";
3872       goto beq_i;
3873     case M_BNEL_I:
3874       s = "bnel";
3875       likely = 1;
3876     beq_i:
3877       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3878         {
3879           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3880                        0);
3881           return;
3882         }
3883       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3884       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3885       break;
3886
3887     case M_BGEL:
3888       likely = 1;
3889     case M_BGE:
3890       if (treg == 0)
3891         {
3892           macro_build ((char *) NULL, &icnt, &offset_expr,
3893                        likely ? "bgezl" : "bgez", "s,p", sreg);
3894           return;
3895         }
3896       if (sreg == 0)
3897         {
3898           macro_build ((char *) NULL, &icnt, &offset_expr,
3899                        likely ? "blezl" : "blez", "s,p", treg);
3900           return;
3901         }
3902       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3903                    AT, sreg, treg);
3904       macro_build ((char *) NULL, &icnt, &offset_expr,
3905                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3906       break;
3907
3908     case M_BGTL_I:
3909       likely = 1;
3910     case M_BGT_I:
3911       /* check for > max integer */
3912       maxnum = 0x7fffffff;
3913       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3914         {
3915           maxnum <<= 16;
3916           maxnum |= 0xffff;
3917           maxnum <<= 16;
3918           maxnum |= 0xffff;
3919         }
3920       if (imm_expr.X_op == O_constant
3921           && imm_expr.X_add_number >= maxnum
3922           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3923         {
3924         do_false:
3925           /* result is always false */
3926           if (! likely)
3927             {
3928               if (warn_nops)
3929                 as_warn (_("Branch %s is always false (nop)"),
3930                          ip->insn_mo->name);
3931               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3932                            "", 0);
3933             }
3934           else
3935             {
3936               if (warn_nops)
3937                 as_warn (_("Branch likely %s is always false"),
3938                          ip->insn_mo->name);
3939               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3940                            "s,t,p", 0, 0);
3941             }
3942           return;
3943         }
3944       if (imm_expr.X_op != O_constant)
3945         as_bad (_("Unsupported large constant"));
3946       ++imm_expr.X_add_number;
3947       /* FALLTHROUGH */
3948     case M_BGE_I:
3949     case M_BGEL_I:
3950       if (mask == M_BGEL_I)
3951         likely = 1;
3952       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3953         {
3954           macro_build ((char *) NULL, &icnt, &offset_expr,
3955                        likely ? "bgezl" : "bgez", "s,p", sreg);
3956           return;
3957         }
3958       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3959         {
3960           macro_build ((char *) NULL, &icnt, &offset_expr,
3961                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
3962           return;
3963         }
3964       maxnum = 0x7fffffff;
3965       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3966         {
3967           maxnum <<= 16;
3968           maxnum |= 0xffff;
3969           maxnum <<= 16;
3970           maxnum |= 0xffff;
3971         }
3972       maxnum = - maxnum - 1;
3973       if (imm_expr.X_op == O_constant
3974           && imm_expr.X_add_number <= maxnum
3975           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3976         {
3977         do_true:
3978           /* result is always true */
3979           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3980           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3981           return;
3982         }
3983       set_at (&icnt, sreg, 0);
3984       macro_build ((char *) NULL, &icnt, &offset_expr,
3985                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3986       break;
3987
3988     case M_BGEUL:
3989       likely = 1;
3990     case M_BGEU:
3991       if (treg == 0)
3992         goto do_true;
3993       if (sreg == 0)
3994         {
3995           macro_build ((char *) NULL, &icnt, &offset_expr,
3996                        likely ? "beql" : "beq", "s,t,p", 0, treg);
3997           return;
3998         }
3999       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4000                    "d,v,t", AT, sreg, treg);
4001       macro_build ((char *) NULL, &icnt, &offset_expr,
4002                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4003       break;
4004
4005     case M_BGTUL_I:
4006       likely = 1;
4007     case M_BGTU_I:
4008       if (sreg == 0
4009           || (HAVE_32BIT_GPRS
4010               && imm_expr.X_op == O_constant
4011               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4012         goto do_false;
4013       if (imm_expr.X_op != O_constant)
4014         as_bad (_("Unsupported large constant"));
4015       ++imm_expr.X_add_number;
4016       /* FALLTHROUGH */
4017     case M_BGEU_I:
4018     case M_BGEUL_I:
4019       if (mask == M_BGEUL_I)
4020         likely = 1;
4021       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4022         goto do_true;
4023       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4024         {
4025           macro_build ((char *) NULL, &icnt, &offset_expr,
4026                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4027           return;
4028         }
4029       set_at (&icnt, sreg, 1);
4030       macro_build ((char *) NULL, &icnt, &offset_expr,
4031                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4032       break;
4033
4034     case M_BGTL:
4035       likely = 1;
4036     case M_BGT:
4037       if (treg == 0)
4038         {
4039           macro_build ((char *) NULL, &icnt, &offset_expr,
4040                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4041           return;
4042         }
4043       if (sreg == 0)
4044         {
4045           macro_build ((char *) NULL, &icnt, &offset_expr,
4046                        likely ? "bltzl" : "bltz", "s,p", treg);
4047           return;
4048         }
4049       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4050                    AT, treg, sreg);
4051       macro_build ((char *) NULL, &icnt, &offset_expr,
4052                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4053       break;
4054
4055     case M_BGTUL:
4056       likely = 1;
4057     case M_BGTU:
4058       if (treg == 0)
4059         {
4060           macro_build ((char *) NULL, &icnt, &offset_expr,
4061                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4062           return;
4063         }
4064       if (sreg == 0)
4065         goto do_false;
4066       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4067                    "d,v,t", 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_BLEL:
4073       likely = 1;
4074     case M_BLE:
4075       if (treg == 0)
4076         {
4077           macro_build ((char *) NULL, &icnt, &offset_expr,
4078                        likely ? "blezl" : "blez", "s,p", sreg);
4079           return;
4080         }
4081       if (sreg == 0)
4082         {
4083           macro_build ((char *) NULL, &icnt, &offset_expr,
4084                        likely ? "bgezl" : "bgez", "s,p", treg);
4085           return;
4086         }
4087       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4088                    AT, treg, sreg);
4089       macro_build ((char *) NULL, &icnt, &offset_expr,
4090                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4091       break;
4092
4093     case M_BLEL_I:
4094       likely = 1;
4095     case M_BLE_I:
4096       maxnum = 0x7fffffff;
4097       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4098         {
4099           maxnum <<= 16;
4100           maxnum |= 0xffff;
4101           maxnum <<= 16;
4102           maxnum |= 0xffff;
4103         }
4104       if (imm_expr.X_op == O_constant
4105           && imm_expr.X_add_number >= maxnum
4106           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4107         goto do_true;
4108       if (imm_expr.X_op != O_constant)
4109         as_bad (_("Unsupported large constant"));
4110       ++imm_expr.X_add_number;
4111       /* FALLTHROUGH */
4112     case M_BLT_I:
4113     case M_BLTL_I:
4114       if (mask == M_BLTL_I)
4115         likely = 1;
4116       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4117         {
4118           macro_build ((char *) NULL, &icnt, &offset_expr,
4119                        likely ? "bltzl" : "bltz", "s,p", sreg);
4120           return;
4121         }
4122       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4123         {
4124           macro_build ((char *) NULL, &icnt, &offset_expr,
4125                        likely ? "blezl" : "blez", "s,p", sreg);
4126           return;
4127         }
4128       set_at (&icnt, sreg, 0);
4129       macro_build ((char *) NULL, &icnt, &offset_expr,
4130                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4131       break;
4132
4133     case M_BLEUL:
4134       likely = 1;
4135     case M_BLEU:
4136       if (treg == 0)
4137         {
4138           macro_build ((char *) NULL, &icnt, &offset_expr,
4139                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4140           return;
4141         }
4142       if (sreg == 0)
4143         goto do_true;
4144       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4145                    "d,v,t", AT, treg, sreg);
4146       macro_build ((char *) NULL, &icnt, &offset_expr,
4147                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4148       break;
4149
4150     case M_BLEUL_I:
4151       likely = 1;
4152     case M_BLEU_I:
4153       if (sreg == 0
4154           || (HAVE_32BIT_GPRS
4155               && imm_expr.X_op == O_constant
4156               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4157         goto do_true;
4158       if (imm_expr.X_op != O_constant)
4159         as_bad (_("Unsupported large constant"));
4160       ++imm_expr.X_add_number;
4161       /* FALLTHROUGH */
4162     case M_BLTU_I:
4163     case M_BLTUL_I:
4164       if (mask == M_BLTUL_I)
4165         likely = 1;
4166       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4167         goto do_false;
4168       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4169         {
4170           macro_build ((char *) NULL, &icnt, &offset_expr,
4171                        likely ? "beql" : "beq",
4172                        "s,t,p", sreg, 0);
4173           return;
4174         }
4175       set_at (&icnt, sreg, 1);
4176       macro_build ((char *) NULL, &icnt, &offset_expr,
4177                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4178       break;
4179
4180     case M_BLTL:
4181       likely = 1;
4182     case M_BLT:
4183       if (treg == 0)
4184         {
4185           macro_build ((char *) NULL, &icnt, &offset_expr,
4186                        likely ? "bltzl" : "bltz", "s,p", sreg);
4187           return;
4188         }
4189       if (sreg == 0)
4190         {
4191           macro_build ((char *) NULL, &icnt, &offset_expr,
4192                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4193           return;
4194         }
4195       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4196                    AT, sreg, treg);
4197       macro_build ((char *) NULL, &icnt, &offset_expr,
4198                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4199       break;
4200
4201     case M_BLTUL:
4202       likely = 1;
4203     case M_BLTU:
4204       if (treg == 0)
4205         goto do_false;
4206       if (sreg == 0)
4207         {
4208           macro_build ((char *) NULL, &icnt, &offset_expr,
4209                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4210           return;
4211         }
4212       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4213                    "d,v,t", AT, sreg,
4214                    treg);
4215       macro_build ((char *) NULL, &icnt, &offset_expr,
4216                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4217       break;
4218
4219     case M_DDIV_3:
4220       dbl = 1;
4221     case M_DIV_3:
4222       s = "mflo";
4223       goto do_div3;
4224     case M_DREM_3:
4225       dbl = 1;
4226     case M_REM_3:
4227       s = "mfhi";
4228     do_div3:
4229       if (treg == 0)
4230         {
4231           as_warn (_("Divide by zero."));
4232           if (mips_trap)
4233             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4234                          "s,t,q", 0, 0, 7);
4235           else
4236             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4237                          "c", 7);
4238           return;
4239         }
4240
4241       mips_emit_delays (true);
4242       ++mips_opts.noreorder;
4243       mips_any_noreorder = 1;
4244       if (mips_trap)
4245         {
4246           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4247                        "s,t,q", treg, 0, 7);
4248           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4249                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4250         }
4251       else
4252         {
4253           expr1.X_add_number = 8;
4254           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4255           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4256                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4257           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4258                        "c", 7);
4259         }
4260       expr1.X_add_number = -1;
4261       macro_build ((char *) NULL, &icnt, &expr1,
4262                    dbl ? "daddiu" : "addiu",
4263                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4264       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4265       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4266       if (dbl)
4267         {
4268           expr1.X_add_number = 1;
4269           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4270                        (int) BFD_RELOC_LO16);
4271           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4272                        "d,w,<", AT, AT, 31);
4273         }
4274       else
4275         {
4276           expr1.X_add_number = 0x80000000;
4277           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4278                        (int) BFD_RELOC_HI16);
4279         }
4280       if (mips_trap)
4281         {
4282           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4283                        "s,t,q", sreg, AT, 6);
4284           /* We want to close the noreorder block as soon as possible, so
4285              that later insns are available for delay slot filling.  */
4286           --mips_opts.noreorder;
4287         }
4288       else
4289         {
4290           expr1.X_add_number = 8;
4291           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4292           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4293                        0);
4294
4295           /* We want to close the noreorder block as soon as possible, so
4296              that later insns are available for delay slot filling.  */
4297           --mips_opts.noreorder;
4298
4299           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4300                        "c", 6);
4301         }
4302       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4303       break;
4304
4305     case M_DIV_3I:
4306       s = "div";
4307       s2 = "mflo";
4308       goto do_divi;
4309     case M_DIVU_3I:
4310       s = "divu";
4311       s2 = "mflo";
4312       goto do_divi;
4313     case M_REM_3I:
4314       s = "div";
4315       s2 = "mfhi";
4316       goto do_divi;
4317     case M_REMU_3I:
4318       s = "divu";
4319       s2 = "mfhi";
4320       goto do_divi;
4321     case M_DDIV_3I:
4322       dbl = 1;
4323       s = "ddiv";
4324       s2 = "mflo";
4325       goto do_divi;
4326     case M_DDIVU_3I:
4327       dbl = 1;
4328       s = "ddivu";
4329       s2 = "mflo";
4330       goto do_divi;
4331     case M_DREM_3I:
4332       dbl = 1;
4333       s = "ddiv";
4334       s2 = "mfhi";
4335       goto do_divi;
4336     case M_DREMU_3I:
4337       dbl = 1;
4338       s = "ddivu";
4339       s2 = "mfhi";
4340     do_divi:
4341       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4342         {
4343           as_warn (_("Divide by zero."));
4344           if (mips_trap)
4345             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4346                          "s,t,q", 0, 0, 7);
4347           else
4348             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4349                          "c", 7);
4350           return;
4351         }
4352       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4353         {
4354           if (strcmp (s2, "mflo") == 0)
4355             move_register (&icnt, dreg, sreg);
4356           else
4357             move_register (&icnt, dreg, 0);
4358           return;
4359         }
4360       if (imm_expr.X_op == O_constant
4361           && imm_expr.X_add_number == -1
4362           && s[strlen (s) - 1] != 'u')
4363         {
4364           if (strcmp (s2, "mflo") == 0)
4365             {
4366               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4367                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4368             }
4369           else
4370             move_register (&icnt, dreg, 0);
4371           return;
4372         }
4373
4374       load_register (&icnt, AT, &imm_expr, dbl);
4375       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4376                    sreg, AT);
4377       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4378       break;
4379
4380     case M_DIVU_3:
4381       s = "divu";
4382       s2 = "mflo";
4383       goto do_divu3;
4384     case M_REMU_3:
4385       s = "divu";
4386       s2 = "mfhi";
4387       goto do_divu3;
4388     case M_DDIVU_3:
4389       s = "ddivu";
4390       s2 = "mflo";
4391       goto do_divu3;
4392     case M_DREMU_3:
4393       s = "ddivu";
4394       s2 = "mfhi";
4395     do_divu3:
4396       mips_emit_delays (true);
4397       ++mips_opts.noreorder;
4398       mips_any_noreorder = 1;
4399       if (mips_trap)
4400         {
4401           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4402                        "s,t,q", treg, 0, 7);
4403           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4404                        sreg, treg);
4405           /* We want to close the noreorder block as soon as possible, so
4406              that later insns are available for delay slot filling.  */
4407           --mips_opts.noreorder;
4408         }
4409       else
4410         {
4411           expr1.X_add_number = 8;
4412           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4413           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4414                        sreg, treg);
4415
4416           /* We want to close the noreorder block as soon as possible, so
4417              that later insns are available for delay slot filling.  */
4418           --mips_opts.noreorder;
4419           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4420                        "c", 7);
4421         }
4422       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4423       return;
4424
4425     case M_DLA_AB:
4426       dbl = 1;
4427     case M_LA_AB:
4428       /* Load the address of a symbol into a register.  If breg is not
4429          zero, we then add a base register to it.  */
4430
4431       if (dbl && HAVE_32BIT_GPRS)
4432         as_warn (_("dla used to load 32-bit register"));
4433
4434       if (! dbl && HAVE_64BIT_OBJECTS)
4435         as_warn (_("la used to load 64-bit address"));
4436
4437       if (treg == breg)
4438         {
4439           tempreg = AT;
4440           used_at = 1;
4441         }
4442       else
4443         {
4444           tempreg = treg;
4445           used_at = 0;
4446         }
4447
4448       /* When generating embedded PIC code, we permit expressions of
4449          the form
4450            la   $treg,foo-bar
4451            la   $treg,foo-bar($breg)
4452          where bar is an address in the current section.  These are used
4453          when getting the addresses of functions.  We don't permit
4454          X_add_number to be non-zero, because if the symbol is
4455          external the relaxing code needs to know that any addend is
4456          purely the offset to X_op_symbol.  */
4457       if (mips_pic == EMBEDDED_PIC
4458           && offset_expr.X_op == O_subtract
4459           && (symbol_constant_p (offset_expr.X_op_symbol)
4460               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4461               : (symbol_equated_p (offset_expr.X_op_symbol)
4462                  && (S_GET_SEGMENT
4463                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4464                       ->X_add_symbol)
4465                      == now_seg)))
4466           && (offset_expr.X_add_number == 0
4467               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4468         {
4469           if (breg == 0)
4470             {
4471               tempreg = treg;
4472               used_at = 0;
4473               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4474                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4475             }
4476           else
4477             {
4478               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4479                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4480               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4481                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4482                            "d,v,t", tempreg, tempreg, breg);
4483             }
4484           macro_build ((char *) NULL, &icnt, &offset_expr,
4485                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4486                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4487           if (! used_at)
4488             return;
4489           break;
4490         }
4491
4492       if (offset_expr.X_op != O_symbol
4493           && offset_expr.X_op != O_constant)
4494         {
4495           as_bad (_("expression too complex"));
4496           offset_expr.X_op = O_constant;
4497         }
4498
4499       if (offset_expr.X_op == O_constant)
4500         load_register (&icnt, tempreg, &offset_expr,
4501                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4502                         ? (dbl || HAVE_64BIT_ADDRESSES)
4503                         : HAVE_64BIT_ADDRESSES));
4504       else if (mips_pic == NO_PIC)
4505         {
4506           /* If this is a reference to a GP relative symbol, we want
4507                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4508              Otherwise we want
4509                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4510                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4511              If we have a constant, we need two instructions anyhow,
4512              so we may as well always use the latter form.
4513
4514             With 64bit address space and a usable $at we want
4515               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4516               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4517               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4518               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4519               dsll32    $tempreg,0
4520               daddu     $tempreg,$tempreg,$at
4521
4522             If $at is already in use, we use an path which is suboptimal
4523             on superscalar processors.
4524               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4525               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4526               dsll      $tempreg,16
4527               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4528               dsll      $tempreg,16
4529               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4530           */
4531           char *p = NULL;
4532           if (HAVE_64BIT_ADDRESSES)
4533             {
4534               /* We don't do GP optimization for now because RELAX_ENCODE can't
4535                  hold the data for such large chunks.  */
4536
4537               if (used_at == 0 && ! mips_opts.noat)
4538                 {
4539                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4540                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4541                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4542                                AT, (int) BFD_RELOC_HI16_S);
4543                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4544                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4545                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4546                                AT, AT, (int) BFD_RELOC_LO16);
4547                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4548                                "d,w,<", tempreg, tempreg, 0);
4549                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4550                                "d,v,t", tempreg, tempreg, AT);
4551                   used_at = 1;
4552                 }
4553               else
4554                 {
4555                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4556                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4557                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4558                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4559                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4560                                tempreg, tempreg, 16);
4561                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4562                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4563                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4564                                tempreg, tempreg, 16);
4565                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4566                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4567                 }
4568             }
4569           else
4570             {
4571               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4572                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4573                 {
4574                   frag_grow (20);
4575                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4576                                "t,r,j", tempreg, mips_gp_register,
4577                                (int) BFD_RELOC_GPREL16);
4578                   p = frag_var (rs_machine_dependent, 8, 0,
4579                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4580                                               mips_opts.warn_about_macros),
4581                                 offset_expr.X_add_symbol, 0, NULL);
4582                 }
4583               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4584               if (p != NULL)
4585                 p += 4;
4586               macro_build (p, &icnt, &offset_expr, "addiu",
4587                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4588             }
4589         }
4590       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4591         {
4592           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4593
4594           /* If this is a reference to an external symbol, and there
4595              is no constant, we want
4596                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4597              or if tempreg is PIC_CALL_REG
4598                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4599              For a local symbol, we want
4600                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4601                nop
4602                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4603
4604              If we have a small constant, and this is a reference to
4605              an external symbol, we want
4606                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4607                nop
4608                addiu    $tempreg,$tempreg,<constant>
4609              For a local symbol, we want the same instruction
4610              sequence, but we output a BFD_RELOC_LO16 reloc on the
4611              addiu instruction.
4612
4613              If we have a large constant, and this is a reference to
4614              an external symbol, we want
4615                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4616                lui      $at,<hiconstant>
4617                addiu    $at,$at,<loconstant>
4618                addu     $tempreg,$tempreg,$at
4619              For a local symbol, we want the same instruction
4620              sequence, but we output a BFD_RELOC_LO16 reloc on the
4621              addiu instruction.  */
4622           expr1.X_add_number = offset_expr.X_add_number;
4623           offset_expr.X_add_number = 0;
4624           frag_grow (32);
4625           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4626             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4627           macro_build ((char *) NULL, &icnt, &offset_expr,
4628                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4629                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4630           if (expr1.X_add_number == 0)
4631             {
4632               int off;
4633               char *p;
4634
4635               if (breg == 0)
4636                 off = 0;
4637               else
4638                 {
4639                   /* We're going to put in an addu instruction using
4640                      tempreg, so we may as well insert the nop right
4641                      now.  */
4642                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4643                                "nop", "");
4644                   off = 4;
4645                 }
4646               p = frag_var (rs_machine_dependent, 8 - off, 0,
4647                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4648                                           (breg == 0
4649                                            ? mips_opts.warn_about_macros
4650                                            : 0)),
4651                             offset_expr.X_add_symbol, 0, NULL);
4652               if (breg == 0)
4653                 {
4654                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4655                   p += 4;
4656                 }
4657               macro_build (p, &icnt, &expr1,
4658                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4659                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4660               /* FIXME: If breg == 0, and the next instruction uses
4661                  $tempreg, then if this variant case is used an extra
4662                  nop will be generated.  */
4663             }
4664           else if (expr1.X_add_number >= -0x8000
4665                    && expr1.X_add_number < 0x8000)
4666             {
4667               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4668                            "nop", "");
4669               macro_build ((char *) NULL, &icnt, &expr1,
4670                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4671                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4672               frag_var (rs_machine_dependent, 0, 0,
4673                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4674                         offset_expr.X_add_symbol, 0, NULL);
4675             }
4676           else
4677             {
4678               int off1;
4679
4680               /* If we are going to add in a base register, and the
4681                  target register and the base register are the same,
4682                  then we are using AT as a temporary register.  Since
4683                  we want to load the constant into AT, we add our
4684                  current AT (from the global offset table) and the
4685                  register into the register now, and pretend we were
4686                  not using a base register.  */
4687               if (breg != treg)
4688                 off1 = 0;
4689               else
4690                 {
4691                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4692                                "nop", "");
4693                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4694                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4695                                "d,v,t", treg, AT, breg);
4696                   breg = 0;
4697                   tempreg = treg;
4698                   off1 = -8;
4699                 }
4700
4701               /* Set mips_optimize around the lui instruction to avoid
4702                  inserting an unnecessary nop after the lw.  */
4703               hold_mips_optimize = mips_optimize;
4704               mips_optimize = 2;
4705               macro_build_lui (NULL, &icnt, &expr1, AT);
4706               mips_optimize = hold_mips_optimize;
4707
4708               macro_build ((char *) NULL, &icnt, &expr1,
4709                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4710                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4711               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4712                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4713                            "d,v,t", tempreg, tempreg, AT);
4714               frag_var (rs_machine_dependent, 0, 0,
4715                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4716                         offset_expr.X_add_symbol, 0, NULL);
4717               used_at = 1;
4718             }
4719         }
4720       else if (mips_pic == SVR4_PIC)
4721         {
4722           int gpdel;
4723           char *p;
4724           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4725           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4726
4727           /* This is the large GOT case.  If this is a reference to an
4728              external symbol, and there is no constant, we want
4729                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4730                addu     $tempreg,$tempreg,$gp
4731                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4732              or if tempreg is PIC_CALL_REG
4733                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4734                addu     $tempreg,$tempreg,$gp
4735                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4736              For a local symbol, we want
4737                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4738                nop
4739                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4740
4741              If we have a small constant, and this is a reference to
4742              an external symbol, we want
4743                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4744                addu     $tempreg,$tempreg,$gp
4745                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4746                nop
4747                addiu    $tempreg,$tempreg,<constant>
4748              For a local symbol, we want
4749                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4750                nop
4751                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4752
4753              If we have a large constant, and this is a reference to
4754              an external symbol, we want
4755                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4756                addu     $tempreg,$tempreg,$gp
4757                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4758                lui      $at,<hiconstant>
4759                addiu    $at,$at,<loconstant>
4760                addu     $tempreg,$tempreg,$at
4761              For a local symbol, we want
4762                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4763                lui      $at,<hiconstant>
4764                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4765                addu     $tempreg,$tempreg,$at
4766
4767              For NewABI, we want for data addresses
4768                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4769              If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4770                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4771            */
4772           if (HAVE_NEWABI)
4773             {
4774               int reloc_type = (tempreg == PIC_CALL_REG
4775                                 ? BFD_RELOC_MIPS_CALL16
4776                                 : BFD_RELOC_MIPS_GOT_DISP);
4777
4778               macro_build ((char *) NULL, &icnt, &offset_expr,
4779                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4780                            "t,o(b)", tempreg, reloc_type, mips_gp_register);
4781
4782               if (breg != 0)
4783                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4784                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4785                              "d,v,t", treg, tempreg, breg);
4786
4787               if (! used_at)
4788                 return;
4789
4790               break;
4791             }
4792           expr1.X_add_number = offset_expr.X_add_number;
4793           offset_expr.X_add_number = 0;
4794           frag_grow (52);
4795           if (reg_needs_delay (mips_gp_register))
4796             gpdel = 4;
4797           else
4798             gpdel = 0;
4799           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4800             {
4801               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4802               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4803             }
4804           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4805                        tempreg, lui_reloc_type);
4806           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4807                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4808                        "d,v,t", tempreg, tempreg, mips_gp_register);
4809           macro_build ((char *) NULL, &icnt, &offset_expr,
4810                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4811                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4812           if (expr1.X_add_number == 0)
4813             {
4814               int off;
4815
4816               if (breg == 0)
4817                 off = 0;
4818               else
4819                 {
4820                   /* We're going to put in an addu instruction using
4821                      tempreg, so we may as well insert the nop right
4822                      now.  */
4823                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4824                                "nop", "");
4825                   off = 4;
4826                 }
4827
4828               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4829                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4830                                           8 + gpdel, 0,
4831                                           (breg == 0
4832                                            ? mips_opts.warn_about_macros
4833                                            : 0)),
4834                             offset_expr.X_add_symbol, 0, NULL);
4835             }
4836           else if (expr1.X_add_number >= -0x8000
4837                    && expr1.X_add_number < 0x8000)
4838             {
4839               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4840                            "nop", "");
4841               macro_build ((char *) NULL, &icnt, &expr1,
4842                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4843                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4844
4845               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4846                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4847                                           (breg == 0
4848                                            ? mips_opts.warn_about_macros
4849                                            : 0)),
4850                             offset_expr.X_add_symbol, 0, NULL);
4851             }
4852           else
4853             {
4854               int adj, dreg;
4855
4856               /* If we are going to add in a base register, and the
4857                  target register and the base register are the same,
4858                  then we are using AT as a temporary register.  Since
4859                  we want to load the constant into AT, we add our
4860                  current AT (from the global offset table) and the
4861                  register into the register now, and pretend we were
4862                  not using a base register.  */
4863               if (breg != treg)
4864                 {
4865                   adj = 0;
4866                   dreg = tempreg;
4867                 }
4868               else
4869                 {
4870                   assert (tempreg == AT);
4871                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4872                                "nop", "");
4873                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4874                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4875                                "d,v,t", treg, AT, breg);
4876                   dreg = treg;
4877                   adj = 8;
4878                 }
4879
4880               /* Set mips_optimize around the lui instruction to avoid
4881                  inserting an unnecessary nop after the lw.  */
4882               hold_mips_optimize = mips_optimize;
4883               mips_optimize = 2;
4884               macro_build_lui (NULL, &icnt, &expr1, AT);
4885               mips_optimize = hold_mips_optimize;
4886
4887               macro_build ((char *) NULL, &icnt, &expr1,
4888                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4889                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4890               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4891                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4892                            "d,v,t", dreg, dreg, AT);
4893
4894               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4895                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4896                                           8 + gpdel, 0,
4897                                           (breg == 0
4898                                            ? mips_opts.warn_about_macros
4899                                            : 0)),
4900                             offset_expr.X_add_symbol, 0, NULL);
4901
4902               used_at = 1;
4903             }
4904
4905           if (gpdel > 0)
4906             {
4907               /* This is needed because this instruction uses $gp, but
4908                  the first instruction on the main stream does not.  */
4909               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4910               p += 4;
4911             }
4912           macro_build (p, &icnt, &offset_expr,
4913                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4914                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4915                        mips_gp_register);
4916           p += 4;
4917           if (expr1.X_add_number >= -0x8000
4918               && expr1.X_add_number < 0x8000)
4919             {
4920               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4921               p += 4;
4922               macro_build (p, &icnt, &expr1,
4923                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4924                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4925               /* FIXME: If add_number is 0, and there was no base
4926                  register, the external symbol case ended with a load,
4927                  so if the symbol turns out to not be external, and
4928                  the next instruction uses tempreg, an unnecessary nop
4929                  will be inserted.  */
4930             }
4931           else
4932             {
4933               if (breg == treg)
4934                 {
4935                   /* We must add in the base register now, as in the
4936                      external symbol case.  */
4937                   assert (tempreg == AT);
4938                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4939                   p += 4;
4940                   macro_build (p, &icnt, (expressionS *) NULL,
4941                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4942                                "d,v,t", treg, AT, breg);
4943                   p += 4;
4944                   tempreg = treg;
4945                   /* We set breg to 0 because we have arranged to add
4946                      it in in both cases.  */
4947                   breg = 0;
4948                 }
4949
4950               macro_build_lui (p, &icnt, &expr1, AT);
4951               p += 4;
4952               macro_build (p, &icnt, &expr1,
4953                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4954                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4955               p += 4;
4956               macro_build (p, &icnt, (expressionS *) NULL,
4957                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4958                            "d,v,t", tempreg, tempreg, AT);
4959               p += 4;
4960             }
4961         }
4962       else if (mips_pic == EMBEDDED_PIC)
4963         {
4964           /* We use
4965                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4966              */
4967           macro_build ((char *) NULL, &icnt, &offset_expr,
4968                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4969                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4970         }
4971       else
4972         abort ();
4973
4974       if (breg != 0)
4975         {
4976           char *s;
4977
4978           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4979             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4980           else
4981             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
4982
4983           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4984                        "d,v,t", treg, tempreg, breg);
4985         }
4986
4987       if (! used_at)
4988         return;
4989
4990       break;
4991
4992     case M_J_A:
4993       /* The j instruction may not be used in PIC code, since it
4994          requires an absolute address.  We convert it to a b
4995          instruction.  */
4996       if (mips_pic == NO_PIC)
4997         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
4998       else
4999         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5000       return;
5001
5002       /* The jal instructions must be handled as macros because when
5003          generating PIC code they expand to multi-instruction
5004          sequences.  Normally they are simple instructions.  */
5005     case M_JAL_1:
5006       dreg = RA;
5007       /* Fall through.  */
5008     case M_JAL_2:
5009       if (mips_pic == NO_PIC
5010           || mips_pic == EMBEDDED_PIC)
5011         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5012                      "d,s", dreg, sreg);
5013       else if (mips_pic == SVR4_PIC)
5014         {
5015           if (sreg != PIC_CALL_REG)
5016             as_warn (_("MIPS PIC call to register other than $25"));
5017
5018           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5019                        "d,s", dreg, sreg);
5020           if (! HAVE_NEWABI)
5021             {
5022               if (mips_cprestore_offset < 0)
5023                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5024               else
5025                 {
5026                   if (! mips_frame_reg_valid)
5027                     {
5028                       as_warn (_("No .frame pseudo-op used in PIC code"));
5029                       /* Quiet this warning.  */
5030                       mips_frame_reg_valid = 1;
5031                     }
5032                   if (! mips_cprestore_valid)
5033                     {
5034                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5035                       /* Quiet this warning.  */
5036                       mips_cprestore_valid = 1;
5037                     }
5038                   expr1.X_add_number = mips_cprestore_offset;
5039                   macro_build ((char *) NULL, &icnt, &expr1,
5040                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5041                                mips_gp_register, (int) BFD_RELOC_LO16,
5042                                mips_frame_reg);
5043                 }
5044             }
5045         }
5046       else
5047         abort ();
5048
5049       return;
5050
5051     case M_JAL_A:
5052       if (mips_pic == NO_PIC)
5053         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5054       else if (mips_pic == SVR4_PIC)
5055         {
5056           char *p;
5057
5058           /* If this is a reference to an external symbol, and we are
5059              using a small GOT, we want
5060                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5061                nop
5062                jalr     $ra,$25
5063                nop
5064                lw       $gp,cprestore($sp)
5065              The cprestore value is set using the .cprestore
5066              pseudo-op.  If we are using a big GOT, we want
5067                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5068                addu     $25,$25,$gp
5069                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5070                nop
5071                jalr     $ra,$25
5072                nop
5073                lw       $gp,cprestore($sp)
5074              If the symbol is not external, we want
5075                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5076                nop
5077                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5078                jalr     $ra,$25
5079                nop
5080                lw $gp,cprestore($sp)
5081              For NewABI, we want
5082                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5083                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5084            */
5085           if (HAVE_NEWABI)
5086             {
5087               macro_build ((char *) NULL, &icnt, &offset_expr,
5088                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5089                            "t,o(b)", PIC_CALL_REG,
5090                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5091               macro_build_jalr (icnt, &offset_expr);
5092             }
5093           else
5094             {
5095               frag_grow (40);
5096               if (! mips_big_got)
5097                 {
5098                   macro_build ((char *) NULL, &icnt, &offset_expr,
5099                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5100                                "t,o(b)", PIC_CALL_REG,
5101                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5102                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5103                                "nop", "");
5104                   p = frag_var (rs_machine_dependent, 4, 0,
5105                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5106                                 offset_expr.X_add_symbol, 0, NULL);
5107                 }
5108               else
5109                 {
5110                   int gpdel;
5111
5112                   if (reg_needs_delay (mips_gp_register))
5113                     gpdel = 4;
5114                   else
5115                     gpdel = 0;
5116                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5117                                "t,u", PIC_CALL_REG,
5118                                (int) BFD_RELOC_MIPS_CALL_HI16);
5119                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5120                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5121                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5122                                mips_gp_register);
5123                   macro_build ((char *) NULL, &icnt, &offset_expr,
5124                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5125                                "t,o(b)", PIC_CALL_REG,
5126                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5127                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5128                                "nop", "");
5129                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5130                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5131                                               8 + gpdel, 0, 0),
5132                                 offset_expr.X_add_symbol, 0, NULL);
5133                   if (gpdel > 0)
5134                     {
5135                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5136                       p += 4;
5137                     }
5138                   macro_build (p, &icnt, &offset_expr,
5139                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5140                                "t,o(b)", PIC_CALL_REG,
5141                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5142                   p += 4;
5143                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5144                   p += 4;
5145                 }
5146               macro_build (p, &icnt, &offset_expr,
5147                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5148                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5149                            (int) BFD_RELOC_LO16);
5150               macro_build_jalr (icnt, &offset_expr);
5151
5152               if (mips_cprestore_offset < 0)
5153                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5154               else
5155                 {
5156                   if (! mips_frame_reg_valid)
5157                     {
5158                       as_warn (_("No .frame pseudo-op used in PIC code"));
5159                       /* Quiet this warning.  */
5160                       mips_frame_reg_valid = 1;
5161                     }
5162                   if (! mips_cprestore_valid)
5163                     {
5164                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5165                       /* Quiet this warning.  */
5166                       mips_cprestore_valid = 1;
5167                     }
5168                   if (mips_opts.noreorder)
5169                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5170                                  "nop", "");
5171                   expr1.X_add_number = mips_cprestore_offset;
5172                   macro_build ((char *) NULL, &icnt, &expr1,
5173                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5174                                mips_gp_register, (int) BFD_RELOC_LO16,
5175                                mips_frame_reg);
5176                 }
5177             }
5178         }
5179       else if (mips_pic == EMBEDDED_PIC)
5180         {
5181           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5182           /* The linker may expand the call to a longer sequence which
5183              uses $at, so we must break rather than return.  */
5184           break;
5185         }
5186       else
5187         abort ();
5188
5189       return;
5190
5191     case M_LB_AB:
5192       s = "lb";
5193       goto ld;
5194     case M_LBU_AB:
5195       s = "lbu";
5196       goto ld;
5197     case M_LH_AB:
5198       s = "lh";
5199       goto ld;
5200     case M_LHU_AB:
5201       s = "lhu";
5202       goto ld;
5203     case M_LW_AB:
5204       s = "lw";
5205       goto ld;
5206     case M_LWC0_AB:
5207       s = "lwc0";
5208       /* Itbl support may require additional care here.  */
5209       coproc = 1;
5210       goto ld;
5211     case M_LWC1_AB:
5212       s = "lwc1";
5213       /* Itbl support may require additional care here.  */
5214       coproc = 1;
5215       goto ld;
5216     case M_LWC2_AB:
5217       s = "lwc2";
5218       /* Itbl support may require additional care here.  */
5219       coproc = 1;
5220       goto ld;
5221     case M_LWC3_AB:
5222       s = "lwc3";
5223       /* Itbl support may require additional care here.  */
5224       coproc = 1;
5225       goto ld;
5226     case M_LWL_AB:
5227       s = "lwl";
5228       lr = 1;
5229       goto ld;
5230     case M_LWR_AB:
5231       s = "lwr";
5232       lr = 1;
5233       goto ld;
5234     case M_LDC1_AB:
5235       if (mips_arch == CPU_R4650)
5236         {
5237           as_bad (_("opcode not supported on this processor"));
5238           return;
5239         }
5240       s = "ldc1";
5241       /* Itbl support may require additional care here.  */
5242       coproc = 1;
5243       goto ld;
5244     case M_LDC2_AB:
5245       s = "ldc2";
5246       /* Itbl support may require additional care here.  */
5247       coproc = 1;
5248       goto ld;
5249     case M_LDC3_AB:
5250       s = "ldc3";
5251       /* Itbl support may require additional care here.  */
5252       coproc = 1;
5253       goto ld;
5254     case M_LDL_AB:
5255       s = "ldl";
5256       lr = 1;
5257       goto ld;
5258     case M_LDR_AB:
5259       s = "ldr";
5260       lr = 1;
5261       goto ld;
5262     case M_LL_AB:
5263       s = "ll";
5264       goto ld;
5265     case M_LLD_AB:
5266       s = "lld";
5267       goto ld;
5268     case M_LWU_AB:
5269       s = "lwu";
5270     ld:
5271       if (breg == treg || coproc || lr)
5272         {
5273           tempreg = AT;
5274           used_at = 1;
5275         }
5276       else
5277         {
5278           tempreg = treg;
5279           used_at = 0;
5280         }
5281       goto ld_st;
5282     case M_SB_AB:
5283       s = "sb";
5284       goto st;
5285     case M_SH_AB:
5286       s = "sh";
5287       goto st;
5288     case M_SW_AB:
5289       s = "sw";
5290       goto st;
5291     case M_SWC0_AB:
5292       s = "swc0";
5293       /* Itbl support may require additional care here.  */
5294       coproc = 1;
5295       goto st;
5296     case M_SWC1_AB:
5297       s = "swc1";
5298       /* Itbl support may require additional care here.  */
5299       coproc = 1;
5300       goto st;
5301     case M_SWC2_AB:
5302       s = "swc2";
5303       /* Itbl support may require additional care here.  */
5304       coproc = 1;
5305       goto st;
5306     case M_SWC3_AB:
5307       s = "swc3";
5308       /* Itbl support may require additional care here.  */
5309       coproc = 1;
5310       goto st;
5311     case M_SWL_AB:
5312       s = "swl";
5313       goto st;
5314     case M_SWR_AB:
5315       s = "swr";
5316       goto st;
5317     case M_SC_AB:
5318       s = "sc";
5319       goto st;
5320     case M_SCD_AB:
5321       s = "scd";
5322       goto st;
5323     case M_SDC1_AB:
5324       if (mips_arch == CPU_R4650)
5325         {
5326           as_bad (_("opcode not supported on this processor"));
5327           return;
5328         }
5329       s = "sdc1";
5330       coproc = 1;
5331       /* Itbl support may require additional care here.  */
5332       goto st;
5333     case M_SDC2_AB:
5334       s = "sdc2";
5335       /* Itbl support may require additional care here.  */
5336       coproc = 1;
5337       goto st;
5338     case M_SDC3_AB:
5339       s = "sdc3";
5340       /* Itbl support may require additional care here.  */
5341       coproc = 1;
5342       goto st;
5343     case M_SDL_AB:
5344       s = "sdl";
5345       goto st;
5346     case M_SDR_AB:
5347       s = "sdr";
5348     st:
5349       tempreg = AT;
5350       used_at = 1;
5351     ld_st:
5352       /* Itbl support may require additional care here.  */
5353       if (mask == M_LWC1_AB
5354           || mask == M_SWC1_AB
5355           || mask == M_LDC1_AB
5356           || mask == M_SDC1_AB
5357           || mask == M_L_DAB
5358           || mask == M_S_DAB)
5359         fmt = "T,o(b)";
5360       else if (coproc)
5361         fmt = "E,o(b)";
5362       else
5363         fmt = "t,o(b)";
5364
5365       /* For embedded PIC, we allow loads where the offset is calculated
5366          by subtracting a symbol in the current segment from an unknown
5367          symbol, relative to a base register, e.g.:
5368                 <op>    $treg, <sym>-<localsym>($breg)
5369          This is used by the compiler for switch statements.  */
5370       if (mips_pic == EMBEDDED_PIC
5371           && offset_expr.X_op == O_subtract
5372           && (symbol_constant_p (offset_expr.X_op_symbol)
5373               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5374               : (symbol_equated_p (offset_expr.X_op_symbol)
5375                  && (S_GET_SEGMENT
5376                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5377                       ->X_add_symbol)
5378                      == now_seg)))
5379           && breg != 0
5380           && (offset_expr.X_add_number == 0
5381               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5382         {
5383           /* For this case, we output the instructions:
5384                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5385                 addiu   $tempreg,$tempreg,$breg
5386                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5387              If the relocation would fit entirely in 16 bits, it would be
5388              nice to emit:
5389                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5390              instead, but that seems quite difficult.  */
5391           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5392                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5393           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5394                        ((bfd_arch_bits_per_address (stdoutput) == 32
5395                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5396                         ? "addu" : "daddu"),
5397                        "d,v,t", tempreg, tempreg, breg);
5398           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5399                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5400           if (! used_at)
5401             return;
5402           break;
5403         }
5404
5405       if (offset_expr.X_op != O_constant
5406           && offset_expr.X_op != O_symbol)
5407         {
5408           as_bad (_("expression too complex"));
5409           offset_expr.X_op = O_constant;
5410         }
5411
5412       /* A constant expression in PIC code can be handled just as it
5413          is in non PIC code.  */
5414       if (mips_pic == NO_PIC
5415           || offset_expr.X_op == O_constant)
5416         {
5417           char *p;
5418
5419           /* If this is a reference to a GP relative symbol, and there
5420              is no base register, we want
5421                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5422              Otherwise, if there is no base register, we want
5423                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5424                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5425              If we have a constant, we need two instructions anyhow,
5426              so we always use the latter form.
5427
5428              If we have a base register, and this is a reference to a
5429              GP relative symbol, we want
5430                addu     $tempreg,$breg,$gp
5431                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5432              Otherwise we want
5433                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5434                addu     $tempreg,$tempreg,$breg
5435                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5436              With a constant we always use the latter case.
5437
5438              With 64bit address space and no base register and $at usable,
5439              we want
5440                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5441                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5442                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5443                dsll32   $tempreg,0
5444                daddu    $tempreg,$at
5445                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5446              If we have a base register, we want
5447                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5448                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5449                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5450                daddu    $at,$breg
5451                dsll32   $tempreg,0
5452                daddu    $tempreg,$at
5453                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5454
5455              Without $at we can't generate the optimal path for superscalar
5456              processors here since this would require two temporary registers.
5457                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5458                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5459                dsll     $tempreg,16
5460                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5461                dsll     $tempreg,16
5462                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5463              If we have a base register, we want
5464                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5465                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5466                dsll     $tempreg,16
5467                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5468                dsll     $tempreg,16
5469                daddu    $tempreg,$tempreg,$breg
5470                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5471
5472              If we have 64-bit addresses, as an optimization, for
5473              addresses which are 32-bit constants (e.g. kseg0/kseg1
5474              addresses) we fall back to the 32-bit address generation
5475              mechanism since it is more efficient.  Note that due to
5476              the signed offset used by memory operations, the 32-bit
5477              range is shifted down by 32768 here.  This code should
5478              probably attempt to generate 64-bit constants more
5479              efficiently in general.
5480            */
5481           if (HAVE_64BIT_ADDRESSES
5482               && !(offset_expr.X_op == O_constant
5483                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5484             {
5485               p = NULL;
5486
5487               /* We don't do GP optimization for now because RELAX_ENCODE can't
5488                  hold the data for such large chunks.  */
5489
5490               if (used_at == 0 && ! mips_opts.noat)
5491                 {
5492                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5493                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5494                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5495                                AT, (int) BFD_RELOC_HI16_S);
5496                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5497                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5498                   if (breg != 0)
5499                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5500                                  "d,v,t", AT, AT, breg);
5501                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5502                                "d,w,<", tempreg, tempreg, 0);
5503                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5504                                "d,v,t", tempreg, tempreg, AT);
5505                   macro_build (p, &icnt, &offset_expr, s,
5506                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5507                   used_at = 1;
5508                 }
5509               else
5510                 {
5511                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5512                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5513                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5514                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5515                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5516                                "d,w,<", tempreg, tempreg, 16);
5517                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5518                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5519                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5520                                "d,w,<", tempreg, tempreg, 16);
5521                   if (breg != 0)
5522                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5523                                  "d,v,t", tempreg, tempreg, breg);
5524                   macro_build (p, &icnt, &offset_expr, s,
5525                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5526                 }
5527
5528               return;
5529             }
5530
5531           if (breg == 0)
5532             {
5533               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5534                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5535                 p = NULL;
5536               else
5537                 {
5538                   frag_grow (20);
5539                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5540                                treg, (int) BFD_RELOC_GPREL16,
5541                                mips_gp_register);
5542                   p = frag_var (rs_machine_dependent, 8, 0,
5543                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5544                                               (mips_opts.warn_about_macros
5545                                                || (used_at
5546                                                    && mips_opts.noat))),
5547                                 offset_expr.X_add_symbol, 0, NULL);
5548                   used_at = 0;
5549                 }
5550               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5551               if (p != NULL)
5552                 p += 4;
5553               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5554                            (int) BFD_RELOC_LO16, tempreg);
5555             }
5556           else
5557             {
5558               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5559                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5560                 p = NULL;
5561               else
5562                 {
5563                   frag_grow (28);
5564                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5565                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5566                                "d,v,t", tempreg, breg, mips_gp_register);
5567                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5568                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5569                   p = frag_var (rs_machine_dependent, 12, 0,
5570                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5571                                 offset_expr.X_add_symbol, 0, NULL);
5572                 }
5573               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5574               if (p != NULL)
5575                 p += 4;
5576               macro_build (p, &icnt, (expressionS *) NULL,
5577                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5578                            "d,v,t", tempreg, tempreg, breg);
5579               if (p != NULL)
5580                 p += 4;
5581               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5582                            (int) BFD_RELOC_LO16, tempreg);
5583             }
5584         }
5585       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5586         {
5587           char *p;
5588
5589           /* If this is a reference to an external symbol, we want
5590                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5591                nop
5592                <op>     $treg,0($tempreg)
5593              Otherwise we want
5594                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5595                nop
5596                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5597                <op>     $treg,0($tempreg)
5598              If there is a base register, we add it to $tempreg before
5599              the <op>.  If there is a constant, we stick it in the
5600              <op> instruction.  We don't handle constants larger than
5601              16 bits, because we have no way to load the upper 16 bits
5602              (actually, we could handle them for the subset of cases
5603              in which we are not using $at).  */
5604           assert (offset_expr.X_op == O_symbol);
5605           expr1.X_add_number = offset_expr.X_add_number;
5606           offset_expr.X_add_number = 0;
5607           if (expr1.X_add_number < -0x8000
5608               || expr1.X_add_number >= 0x8000)
5609             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5610           frag_grow (20);
5611           macro_build ((char *) NULL, &icnt, &offset_expr,
5612                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5613                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5614           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5615           p = frag_var (rs_machine_dependent, 4, 0,
5616                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5617                         offset_expr.X_add_symbol, 0, NULL);
5618           macro_build (p, &icnt, &offset_expr,
5619                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5620                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5621           if (breg != 0)
5622             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5623                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5624                          "d,v,t", tempreg, tempreg, breg);
5625           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5626                        (int) BFD_RELOC_LO16, tempreg);
5627         }
5628       else if (mips_pic == SVR4_PIC)
5629         {
5630           int gpdel;
5631           char *p;
5632
5633           /* If this is a reference to an external symbol, we want
5634                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5635                addu     $tempreg,$tempreg,$gp
5636                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5637                <op>     $treg,0($tempreg)
5638              Otherwise we want
5639                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5640                nop
5641                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5642                <op>     $treg,0($tempreg)
5643              If there is a base register, we add it to $tempreg before
5644              the <op>.  If there is a constant, we stick it in the
5645              <op> instruction.  We don't handle constants larger than
5646              16 bits, because we have no way to load the upper 16 bits
5647              (actually, we could handle them for the subset of cases
5648              in which we are not using $at).
5649
5650              For NewABI, we want
5651                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5652                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5653                <op>     $treg,0($tempreg)
5654            */
5655           assert (offset_expr.X_op == O_symbol);
5656           expr1.X_add_number = offset_expr.X_add_number;
5657           offset_expr.X_add_number = 0;
5658           if (expr1.X_add_number < -0x8000
5659               || expr1.X_add_number >= 0x8000)
5660             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5661           if (HAVE_NEWABI)
5662             {
5663               macro_build ((char *) NULL, &icnt, &offset_expr,
5664                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5665                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5666                            mips_gp_register);
5667               macro_build ((char *) NULL, &icnt, &offset_expr,
5668                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5669                            "t,r,j", tempreg, tempreg,
5670                            BFD_RELOC_MIPS_GOT_OFST);
5671               if (breg != 0)
5672                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5673                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5674                              "d,v,t", tempreg, tempreg, breg);
5675               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5676                            (int) BFD_RELOC_LO16, tempreg);
5677
5678               if (! used_at)
5679                 return;
5680
5681               break;
5682             }
5683           if (reg_needs_delay (mips_gp_register))
5684             gpdel = 4;
5685           else
5686             gpdel = 0;
5687           frag_grow (36);
5688           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5689                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5690           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5691                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5692                        "d,v,t", tempreg, tempreg, mips_gp_register);
5693           macro_build ((char *) NULL, &icnt, &offset_expr,
5694                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5695                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5696                        tempreg);
5697           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5698                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5699                         offset_expr.X_add_symbol, 0, NULL);
5700           if (gpdel > 0)
5701             {
5702               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5703               p += 4;
5704             }
5705           macro_build (p, &icnt, &offset_expr,
5706                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5707                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5708                        mips_gp_register);
5709           p += 4;
5710           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5711           p += 4;
5712           macro_build (p, &icnt, &offset_expr,
5713                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5714                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5715           if (breg != 0)
5716             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5717                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5718                          "d,v,t", tempreg, tempreg, breg);
5719           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5720                        (int) BFD_RELOC_LO16, tempreg);
5721         }
5722       else if (mips_pic == EMBEDDED_PIC)
5723         {
5724           /* If there is no base register, we want
5725                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5726              If there is a base register, we want
5727                addu     $tempreg,$breg,$gp
5728                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5729              */
5730           assert (offset_expr.X_op == O_symbol);
5731           if (breg == 0)
5732             {
5733               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5734                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5735               used_at = 0;
5736             }
5737           else
5738             {
5739               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5740                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5741                            "d,v,t", tempreg, breg, mips_gp_register);
5742               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5743                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5744             }
5745         }
5746       else
5747         abort ();
5748
5749       if (! used_at)
5750         return;
5751
5752       break;
5753
5754     case M_LI:
5755     case M_LI_S:
5756       load_register (&icnt, treg, &imm_expr, 0);
5757       return;
5758
5759     case M_DLI:
5760       load_register (&icnt, treg, &imm_expr, 1);
5761       return;
5762
5763     case M_LI_SS:
5764       if (imm_expr.X_op == O_constant)
5765         {
5766           load_register (&icnt, AT, &imm_expr, 0);
5767           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5768                        "mtc1", "t,G", AT, treg);
5769           break;
5770         }
5771       else
5772         {
5773           assert (offset_expr.X_op == O_symbol
5774                   && strcmp (segment_name (S_GET_SEGMENT
5775                                            (offset_expr.X_add_symbol)),
5776                              ".lit4") == 0
5777                   && offset_expr.X_add_number == 0);
5778           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5779                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
5780           return;
5781         }
5782
5783     case M_LI_D:
5784       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5785          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5786          order 32 bits of the value and the low order 32 bits are either
5787          zero or in OFFSET_EXPR.  */
5788       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5789         {
5790           if (HAVE_64BIT_GPRS)
5791             load_register (&icnt, treg, &imm_expr, 1);
5792           else
5793             {
5794               int hreg, lreg;
5795
5796               if (target_big_endian)
5797                 {
5798                   hreg = treg;
5799                   lreg = treg + 1;
5800                 }
5801               else
5802                 {
5803                   hreg = treg + 1;
5804                   lreg = treg;
5805                 }
5806
5807               if (hreg <= 31)
5808                 load_register (&icnt, hreg, &imm_expr, 0);
5809               if (lreg <= 31)
5810                 {
5811                   if (offset_expr.X_op == O_absent)
5812                     move_register (&icnt, lreg, 0);
5813                   else
5814                     {
5815                       assert (offset_expr.X_op == O_constant);
5816                       load_register (&icnt, lreg, &offset_expr, 0);
5817                     }
5818                 }
5819             }
5820           return;
5821         }
5822
5823       /* We know that sym is in the .rdata section.  First we get the
5824          upper 16 bits of the address.  */
5825       if (mips_pic == NO_PIC)
5826         {
5827           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5828         }
5829       else if (mips_pic == SVR4_PIC)
5830         {
5831           macro_build ((char *) NULL, &icnt, &offset_expr,
5832                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5833                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5834                        mips_gp_register);
5835         }
5836       else if (mips_pic == EMBEDDED_PIC)
5837         {
5838           /* For embedded PIC we pick up the entire address off $gp in
5839              a single instruction.  */
5840           macro_build ((char *) NULL, &icnt, &offset_expr,
5841                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5842                        mips_gp_register, (int) BFD_RELOC_GPREL16);
5843           offset_expr.X_op = O_constant;
5844           offset_expr.X_add_number = 0;
5845         }
5846       else
5847         abort ();
5848
5849       /* Now we load the register(s).  */
5850       if (HAVE_64BIT_GPRS)
5851         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5852                      treg, (int) BFD_RELOC_LO16, AT);
5853       else
5854         {
5855           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5856                        treg, (int) BFD_RELOC_LO16, AT);
5857           if (treg != RA)
5858             {
5859               /* FIXME: How in the world do we deal with the possible
5860                  overflow here?  */
5861               offset_expr.X_add_number += 4;
5862               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5863                            treg + 1, (int) BFD_RELOC_LO16, AT);
5864             }
5865         }
5866
5867       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5868          does not become a variant frag.  */
5869       frag_wane (frag_now);
5870       frag_new (0);
5871
5872       break;
5873
5874     case M_LI_DD:
5875       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5876          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5877          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5878          the value and the low order 32 bits are either zero or in
5879          OFFSET_EXPR.  */
5880       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5881         {
5882           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5883           if (HAVE_64BIT_FPRS)
5884             {
5885               assert (HAVE_64BIT_GPRS);
5886               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5887                            "dmtc1", "t,S", AT, treg);
5888             }
5889           else
5890             {
5891               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5892                            "mtc1", "t,G", AT, treg + 1);
5893               if (offset_expr.X_op == O_absent)
5894                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5895                              "mtc1", "t,G", 0, treg);
5896               else
5897                 {
5898                   assert (offset_expr.X_op == O_constant);
5899                   load_register (&icnt, AT, &offset_expr, 0);
5900                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5901                                "mtc1", "t,G", AT, treg);
5902                 }
5903             }
5904           break;
5905         }
5906
5907       assert (offset_expr.X_op == O_symbol
5908               && offset_expr.X_add_number == 0);
5909       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5910       if (strcmp (s, ".lit8") == 0)
5911         {
5912           if (mips_opts.isa != ISA_MIPS1)
5913             {
5914               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5915                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5916                            mips_gp_register);
5917               return;
5918             }
5919           breg = mips_gp_register;
5920           r = BFD_RELOC_MIPS_LITERAL;
5921           goto dob;
5922         }
5923       else
5924         {
5925           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5926           if (mips_pic == SVR4_PIC)
5927             macro_build ((char *) NULL, &icnt, &offset_expr,
5928                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5929                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5930                          mips_gp_register);
5931           else
5932             {
5933               /* FIXME: This won't work for a 64 bit address.  */
5934               macro_build_lui (NULL, &icnt, &offset_expr, AT);
5935             }
5936
5937           if (mips_opts.isa != ISA_MIPS1)
5938             {
5939               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5940                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5941
5942               /* To avoid confusion in tc_gen_reloc, we must ensure
5943                  that this does not become a variant frag.  */
5944               frag_wane (frag_now);
5945               frag_new (0);
5946
5947               break;
5948             }
5949           breg = AT;
5950           r = BFD_RELOC_LO16;
5951           goto dob;
5952         }
5953
5954     case M_L_DOB:
5955       if (mips_arch == CPU_R4650)
5956         {
5957           as_bad (_("opcode not supported on this processor"));
5958           return;
5959         }
5960       /* Even on a big endian machine $fn comes before $fn+1.  We have
5961          to adjust when loading from memory.  */
5962       r = BFD_RELOC_LO16;
5963     dob:
5964       assert (mips_opts.isa == ISA_MIPS1);
5965       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5966                    target_big_endian ? treg + 1 : treg,
5967                    (int) r, breg);
5968       /* FIXME: A possible overflow which I don't know how to deal
5969          with.  */
5970       offset_expr.X_add_number += 4;
5971       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5972                    target_big_endian ? treg : treg + 1,
5973                    (int) r, breg);
5974
5975       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5976          does not become a variant frag.  */
5977       frag_wane (frag_now);
5978       frag_new (0);
5979
5980       if (breg != AT)
5981         return;
5982       break;
5983
5984     case M_L_DAB:
5985       /*
5986        * The MIPS assembler seems to check for X_add_number not
5987        * being double aligned and generating:
5988        *        lui     at,%hi(foo+1)
5989        *        addu    at,at,v1
5990        *        addiu   at,at,%lo(foo+1)
5991        *        lwc1    f2,0(at)
5992        *        lwc1    f3,4(at)
5993        * But, the resulting address is the same after relocation so why
5994        * generate the extra instruction?
5995        */
5996       if (mips_arch == CPU_R4650)
5997         {
5998           as_bad (_("opcode not supported on this processor"));
5999           return;
6000         }
6001       /* Itbl support may require additional care here.  */
6002       coproc = 1;
6003       if (mips_opts.isa != ISA_MIPS1)
6004         {
6005           s = "ldc1";
6006           goto ld;
6007         }
6008
6009       s = "lwc1";
6010       fmt = "T,o(b)";
6011       goto ldd_std;
6012
6013     case M_S_DAB:
6014       if (mips_arch == CPU_R4650)
6015         {
6016           as_bad (_("opcode not supported on this processor"));
6017           return;
6018         }
6019
6020       if (mips_opts.isa != ISA_MIPS1)
6021         {
6022           s = "sdc1";
6023           goto st;
6024         }
6025
6026       s = "swc1";
6027       fmt = "T,o(b)";
6028       /* Itbl support may require additional care here.  */
6029       coproc = 1;
6030       goto ldd_std;
6031
6032     case M_LD_AB:
6033       if (HAVE_64BIT_GPRS)
6034         {
6035           s = "ld";
6036           goto ld;
6037         }
6038
6039       s = "lw";
6040       fmt = "t,o(b)";
6041       goto ldd_std;
6042
6043     case M_SD_AB:
6044       if (HAVE_64BIT_GPRS)
6045         {
6046           s = "sd";
6047           goto st;
6048         }
6049
6050       s = "sw";
6051       fmt = "t,o(b)";
6052
6053     ldd_std:
6054       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6055          loads for the case of doing a pair of loads to simulate an 'ld'.
6056          This is not currently done by the compiler, and assembly coders
6057          writing embedded-pic code can cope.  */
6058
6059       if (offset_expr.X_op != O_symbol
6060           && offset_expr.X_op != O_constant)
6061         {
6062           as_bad (_("expression too complex"));
6063           offset_expr.X_op = O_constant;
6064         }
6065
6066       /* Even on a big endian machine $fn comes before $fn+1.  We have
6067          to adjust when loading from memory.  We set coproc if we must
6068          load $fn+1 first.  */
6069       /* Itbl support may require additional care here.  */
6070       if (! target_big_endian)
6071         coproc = 0;
6072
6073       if (mips_pic == NO_PIC
6074           || offset_expr.X_op == O_constant)
6075         {
6076           char *p;
6077
6078           /* If this is a reference to a GP relative symbol, we want
6079                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6080                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6081              If we have a base register, we use this
6082                addu     $at,$breg,$gp
6083                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6084                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6085              If this is not a GP relative symbol, we want
6086                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6087                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6088                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6089              If there is a base register, we add it to $at after the
6090              lui instruction.  If there is a constant, we always use
6091              the last case.  */
6092           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6093               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6094             {
6095               p = NULL;
6096               used_at = 1;
6097             }
6098           else
6099             {
6100               int off;
6101
6102               if (breg == 0)
6103                 {
6104                   frag_grow (28);
6105                   tempreg = mips_gp_register;
6106                   off = 0;
6107                   used_at = 0;
6108                 }
6109               else
6110                 {
6111                   frag_grow (36);
6112                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6113                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6114                                "d,v,t", AT, breg, mips_gp_register);
6115                   tempreg = AT;
6116                   off = 4;
6117                   used_at = 1;
6118                 }
6119
6120               /* Itbl support may require additional care here.  */
6121               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6122                            coproc ? treg + 1 : treg,
6123                            (int) BFD_RELOC_GPREL16, tempreg);
6124               offset_expr.X_add_number += 4;
6125
6126               /* Set mips_optimize to 2 to avoid inserting an
6127                  undesired nop.  */
6128               hold_mips_optimize = mips_optimize;
6129               mips_optimize = 2;
6130               /* Itbl support may require additional care here.  */
6131               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6132                            coproc ? treg : treg + 1,
6133                            (int) BFD_RELOC_GPREL16, tempreg);
6134               mips_optimize = hold_mips_optimize;
6135
6136               p = frag_var (rs_machine_dependent, 12 + off, 0,
6137                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6138                                           used_at && mips_opts.noat),
6139                             offset_expr.X_add_symbol, 0, NULL);
6140
6141               /* We just generated two relocs.  When tc_gen_reloc
6142                  handles this case, it will skip the first reloc and
6143                  handle the second.  The second reloc already has an
6144                  extra addend of 4, which we added above.  We must
6145                  subtract it out, and then subtract another 4 to make
6146                  the first reloc come out right.  The second reloc
6147                  will come out right because we are going to add 4 to
6148                  offset_expr when we build its instruction below.
6149
6150                  If we have a symbol, then we don't want to include
6151                  the offset, because it will wind up being included
6152                  when we generate the reloc.  */
6153
6154               if (offset_expr.X_op == O_constant)
6155                 offset_expr.X_add_number -= 8;
6156               else
6157                 {
6158                   offset_expr.X_add_number = -4;
6159                   offset_expr.X_op = O_constant;
6160                 }
6161             }
6162           macro_build_lui (p, &icnt, &offset_expr, AT);
6163           if (p != NULL)
6164             p += 4;
6165           if (breg != 0)
6166             {
6167               macro_build (p, &icnt, (expressionS *) NULL,
6168                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6169                            "d,v,t", AT, breg, AT);
6170               if (p != NULL)
6171                 p += 4;
6172             }
6173           /* Itbl support may require additional care here.  */
6174           macro_build (p, &icnt, &offset_expr, s, fmt,
6175                        coproc ? treg + 1 : treg,
6176                        (int) BFD_RELOC_LO16, AT);
6177           if (p != NULL)
6178             p += 4;
6179           /* FIXME: How do we handle overflow here?  */
6180           offset_expr.X_add_number += 4;
6181           /* Itbl support may require additional care here.  */
6182           macro_build (p, &icnt, &offset_expr, s, fmt,
6183                        coproc ? treg : treg + 1,
6184                        (int) BFD_RELOC_LO16, AT);
6185         }
6186       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6187         {
6188           int off;
6189
6190           /* If this is a reference to an external symbol, we want
6191                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6192                nop
6193                <op>     $treg,0($at)
6194                <op>     $treg+1,4($at)
6195              Otherwise we want
6196                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6197                nop
6198                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6199                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6200              If there is a base register we add it to $at before the
6201              lwc1 instructions.  If there is a constant we include it
6202              in the lwc1 instructions.  */
6203           used_at = 1;
6204           expr1.X_add_number = offset_expr.X_add_number;
6205           offset_expr.X_add_number = 0;
6206           if (expr1.X_add_number < -0x8000
6207               || expr1.X_add_number >= 0x8000 - 4)
6208             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6209           if (breg == 0)
6210             off = 0;
6211           else
6212             off = 4;
6213           frag_grow (24 + off);
6214           macro_build ((char *) NULL, &icnt, &offset_expr,
6215                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6216                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6217           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6218           if (breg != 0)
6219             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6220                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6221                          "d,v,t", AT, breg, AT);
6222           /* Itbl support may require additional care here.  */
6223           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6224                        coproc ? treg + 1 : treg,
6225                        (int) BFD_RELOC_LO16, AT);
6226           expr1.X_add_number += 4;
6227
6228           /* Set mips_optimize to 2 to avoid inserting an undesired
6229              nop.  */
6230           hold_mips_optimize = mips_optimize;
6231           mips_optimize = 2;
6232           /* Itbl support may require additional care here.  */
6233           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6234                        coproc ? treg : treg + 1,
6235                        (int) BFD_RELOC_LO16, AT);
6236           mips_optimize = hold_mips_optimize;
6237
6238           (void) frag_var (rs_machine_dependent, 0, 0,
6239                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6240                            offset_expr.X_add_symbol, 0, NULL);
6241         }
6242       else if (mips_pic == SVR4_PIC)
6243         {
6244           int gpdel, off;
6245           char *p;
6246
6247           /* If this is a reference to an external symbol, we want
6248                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6249                addu     $at,$at,$gp
6250                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6251                nop
6252                <op>     $treg,0($at)
6253                <op>     $treg+1,4($at)
6254              Otherwise we want
6255                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6256                nop
6257                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6258                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6259              If there is a base register we add it to $at before the
6260              lwc1 instructions.  If there is a constant we include it
6261              in the lwc1 instructions.  */
6262           used_at = 1;
6263           expr1.X_add_number = offset_expr.X_add_number;
6264           offset_expr.X_add_number = 0;
6265           if (expr1.X_add_number < -0x8000
6266               || expr1.X_add_number >= 0x8000 - 4)
6267             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6268           if (reg_needs_delay (mips_gp_register))
6269             gpdel = 4;
6270           else
6271             gpdel = 0;
6272           if (breg == 0)
6273             off = 0;
6274           else
6275             off = 4;
6276           frag_grow (56);
6277           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6278                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6279           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6280                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6281                        "d,v,t", AT, AT, mips_gp_register);
6282           macro_build ((char *) NULL, &icnt, &offset_expr,
6283                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6284                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6285           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6286           if (breg != 0)
6287             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6288                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6289                          "d,v,t", AT, breg, AT);
6290           /* Itbl support may require additional care here.  */
6291           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6292                        coproc ? treg + 1 : treg,
6293                        (int) BFD_RELOC_LO16, AT);
6294           expr1.X_add_number += 4;
6295
6296           /* Set mips_optimize to 2 to avoid inserting an undesired
6297              nop.  */
6298           hold_mips_optimize = mips_optimize;
6299           mips_optimize = 2;
6300           /* Itbl support may require additional care here.  */
6301           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6302                        coproc ? treg : treg + 1,
6303                        (int) BFD_RELOC_LO16, AT);
6304           mips_optimize = hold_mips_optimize;
6305           expr1.X_add_number -= 4;
6306
6307           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6308                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6309                                       8 + gpdel + off, 1, 0),
6310                         offset_expr.X_add_symbol, 0, NULL);
6311           if (gpdel > 0)
6312             {
6313               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6314               p += 4;
6315             }
6316           macro_build (p, &icnt, &offset_expr,
6317                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6318                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6319                        mips_gp_register);
6320           p += 4;
6321           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6322           p += 4;
6323           if (breg != 0)
6324             {
6325               macro_build (p, &icnt, (expressionS *) NULL,
6326                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6327                            "d,v,t", AT, breg, AT);
6328               p += 4;
6329             }
6330           /* Itbl support may require additional care here.  */
6331           macro_build (p, &icnt, &expr1, s, fmt,
6332                        coproc ? treg + 1 : treg,
6333                        (int) BFD_RELOC_LO16, AT);
6334           p += 4;
6335           expr1.X_add_number += 4;
6336
6337           /* Set mips_optimize to 2 to avoid inserting an undesired
6338              nop.  */
6339           hold_mips_optimize = mips_optimize;
6340           mips_optimize = 2;
6341           /* Itbl support may require additional care here.  */
6342           macro_build (p, &icnt, &expr1, s, fmt,
6343                        coproc ? treg : treg + 1,
6344                        (int) BFD_RELOC_LO16, AT);
6345           mips_optimize = hold_mips_optimize;
6346         }
6347       else if (mips_pic == EMBEDDED_PIC)
6348         {
6349           /* If there is no base register, we use
6350                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6351                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6352              If we have a base register, we use
6353                addu     $at,$breg,$gp
6354                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6355                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6356              */
6357           if (breg == 0)
6358             {
6359               tempreg = mips_gp_register;
6360               used_at = 0;
6361             }
6362           else
6363             {
6364               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6365                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6366                            "d,v,t", AT, breg, mips_gp_register);
6367               tempreg = AT;
6368               used_at = 1;
6369             }
6370
6371           /* Itbl support may require additional care here.  */
6372           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6373                        coproc ? treg + 1 : treg,
6374                        (int) BFD_RELOC_GPREL16, tempreg);
6375           offset_expr.X_add_number += 4;
6376           /* Itbl support may require additional care here.  */
6377           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6378                        coproc ? treg : treg + 1,
6379                        (int) BFD_RELOC_GPREL16, tempreg);
6380         }
6381       else
6382         abort ();
6383
6384       if (! used_at)
6385         return;
6386
6387       break;
6388
6389     case M_LD_OB:
6390       s = "lw";
6391       goto sd_ob;
6392     case M_SD_OB:
6393       s = "sw";
6394     sd_ob:
6395       assert (HAVE_32BIT_ADDRESSES);
6396       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6397                    (int) BFD_RELOC_LO16, breg);
6398       offset_expr.X_add_number += 4;
6399       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6400                    (int) BFD_RELOC_LO16, breg);
6401       return;
6402
6403    /* New code added to support COPZ instructions.
6404       This code builds table entries out of the macros in mip_opcodes.
6405       R4000 uses interlocks to handle coproc delays.
6406       Other chips (like the R3000) require nops to be inserted for delays.
6407
6408       FIXME: Currently, we require that the user handle delays.
6409       In order to fill delay slots for non-interlocked chips,
6410       we must have a way to specify delays based on the coprocessor.
6411       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6412       What are the side-effects of the cop instruction?
6413       What cache support might we have and what are its effects?
6414       Both coprocessor & memory require delays. how long???
6415       What registers are read/set/modified?
6416
6417       If an itbl is provided to interpret cop instructions,
6418       this knowledge can be encoded in the itbl spec.  */
6419
6420     case M_COP0:
6421       s = "c0";
6422       goto copz;
6423     case M_COP1:
6424       s = "c1";
6425       goto copz;
6426     case M_COP2:
6427       s = "c2";
6428       goto copz;
6429     case M_COP3:
6430       s = "c3";
6431     copz:
6432       /* For now we just do C (same as Cz).  The parameter will be
6433          stored in insn_opcode by mips_ip.  */
6434       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6435                    ip->insn_opcode);
6436       return;
6437
6438     case M_MOVE:
6439       move_register (&icnt, dreg, sreg);
6440       return;
6441
6442 #ifdef LOSING_COMPILER
6443     default:
6444       /* Try and see if this is a new itbl instruction.
6445          This code builds table entries out of the macros in mip_opcodes.
6446          FIXME: For now we just assemble the expression and pass it's
6447          value along as a 32-bit immediate.
6448          We may want to have the assembler assemble this value,
6449          so that we gain the assembler's knowledge of delay slots,
6450          symbols, etc.
6451          Would it be more efficient to use mask (id) here? */
6452       if (itbl_have_entries
6453           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6454         {
6455           s = ip->insn_mo->name;
6456           s2 = "cop3";
6457           coproc = ITBL_DECODE_PNUM (immed_expr);;
6458           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6459           return;
6460         }
6461       macro2 (ip);
6462       return;
6463     }
6464   if (mips_opts.noat)
6465     as_warn (_("Macro used $at after \".set noat\""));
6466 }
6467
6468 static void
6469 macro2 (ip)
6470      struct mips_cl_insn *ip;
6471 {
6472   register int treg, sreg, dreg, breg;
6473   int tempreg;
6474   int mask;
6475   int icnt = 0;
6476   int used_at;
6477   expressionS expr1;
6478   const char *s;
6479   const char *s2;
6480   const char *fmt;
6481   int likely = 0;
6482   int dbl = 0;
6483   int coproc = 0;
6484   int lr = 0;
6485   int imm = 0;
6486   int off;
6487   offsetT maxnum;
6488   bfd_reloc_code_real_type r;
6489   char *p;
6490
6491   treg = (ip->insn_opcode >> 16) & 0x1f;
6492   dreg = (ip->insn_opcode >> 11) & 0x1f;
6493   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6494   mask = ip->insn_mo->mask;
6495
6496   expr1.X_op = O_constant;
6497   expr1.X_op_symbol = NULL;
6498   expr1.X_add_symbol = NULL;
6499   expr1.X_add_number = 1;
6500
6501   switch (mask)
6502     {
6503 #endif /* LOSING_COMPILER */
6504
6505     case M_DMUL:
6506       dbl = 1;
6507     case M_MUL:
6508       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6509                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6510       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6511                    dreg);
6512       return;
6513
6514     case M_DMUL_I:
6515       dbl = 1;
6516     case M_MUL_I:
6517       /* The MIPS assembler some times generates shifts and adds.  I'm
6518          not trying to be that fancy. GCC should do this for us
6519          anyway.  */
6520       load_register (&icnt, AT, &imm_expr, dbl);
6521       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6522                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6523       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6524                    dreg);
6525       break;
6526
6527     case M_DMULO_I:
6528       dbl = 1;
6529     case M_MULO_I:
6530       imm = 1;
6531       goto do_mulo;
6532
6533     case M_DMULO:
6534       dbl = 1;
6535     case M_MULO:
6536     do_mulo:
6537       mips_emit_delays (true);
6538       ++mips_opts.noreorder;
6539       mips_any_noreorder = 1;
6540       if (imm)
6541         load_register (&icnt, AT, &imm_expr, dbl);
6542       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6543                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6544       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6545                    dreg);
6546       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6547                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6548       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6549                    AT);
6550       if (mips_trap)
6551         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6552                      "s,t,q", dreg, AT, 6);
6553       else
6554         {
6555           expr1.X_add_number = 8;
6556           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6557                        AT);
6558           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6559                        0);
6560           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6561                        "c", 6);
6562         }
6563       --mips_opts.noreorder;
6564       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6565       break;
6566
6567     case M_DMULOU_I:
6568       dbl = 1;
6569     case M_MULOU_I:
6570       imm = 1;
6571       goto do_mulou;
6572
6573     case M_DMULOU:
6574       dbl = 1;
6575     case M_MULOU:
6576     do_mulou:
6577       mips_emit_delays (true);
6578       ++mips_opts.noreorder;
6579       mips_any_noreorder = 1;
6580       if (imm)
6581         load_register (&icnt, AT, &imm_expr, dbl);
6582       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6583                    dbl ? "dmultu" : "multu",
6584                    "s,t", sreg, imm ? AT : treg);
6585       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6586                    AT);
6587       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6588                    dreg);
6589       if (mips_trap)
6590         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6591                      "s,t,q", AT, 0, 6);
6592       else
6593         {
6594           expr1.X_add_number = 8;
6595           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6596           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6597                        0);
6598           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6599                        "c", 6);
6600         }
6601       --mips_opts.noreorder;
6602       break;
6603
6604     case M_DROL:
6605       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6606                    "d,v,t", AT, 0, treg);
6607       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6608                    "d,t,s", AT, sreg, AT);
6609       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6610                    "d,t,s", dreg, sreg, treg);
6611       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6612                    "d,v,t", dreg, dreg, AT);
6613       break;
6614
6615     case M_ROL:
6616       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6617                    "d,v,t", AT, 0, treg);
6618       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6619                    "d,t,s", AT, sreg, AT);
6620       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6621                    "d,t,s", dreg, sreg, treg);
6622       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6623                    "d,v,t", dreg, dreg, AT);
6624       break;
6625
6626     case M_DROL_I:
6627       {
6628         unsigned int rot;
6629         char *l, *r;
6630
6631         if (imm_expr.X_op != O_constant)
6632           as_bad (_("rotate count too large"));
6633         rot = imm_expr.X_add_number & 0x3f;
6634         if (! rot)
6635           break;
6636         l = (rot < 0x20) ? "dsll" : "dsll32";
6637         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6638         rot &= 0x1f;
6639         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6640                      "d,w,<", AT, sreg, rot);
6641         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6642                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6643         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6644                      "d,v,t", dreg, dreg, AT);
6645       }
6646       break;
6647
6648     case M_ROL_I:
6649       {
6650         unsigned int rot;
6651
6652         if (imm_expr.X_op != O_constant)
6653           as_bad (_("rotate count too large"));
6654         rot = imm_expr.X_add_number & 0x1f;
6655         if (! rot)
6656           break;
6657         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6658                      "d,w,<", AT, sreg, rot);
6659         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6660                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6661         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6662                      "d,v,t", dreg, dreg, AT);
6663       }
6664       break;
6665
6666     case M_DROR:
6667       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6668                    "d,v,t", AT, 0, treg);
6669       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6670                    "d,t,s", AT, sreg, AT);
6671       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6672                    "d,t,s", dreg, sreg, treg);
6673       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6674                    "d,v,t", dreg, dreg, AT);
6675       break;
6676
6677     case M_ROR:
6678       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6679                    "d,v,t", AT, 0, treg);
6680       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6681                    "d,t,s", AT, sreg, AT);
6682       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6683                    "d,t,s", dreg, sreg, treg);
6684       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6685                    "d,v,t", dreg, dreg, AT);
6686       break;
6687
6688     case M_DROR_I:
6689       {
6690         unsigned int rot;
6691         char *l, *r;
6692
6693         if (imm_expr.X_op != O_constant)
6694           as_bad (_("rotate count too large"));
6695         rot = imm_expr.X_add_number & 0x3f;
6696         if (! rot)
6697           break;
6698         r = (rot < 0x20) ? "dsrl" : "dsrl32";
6699         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6700         rot &= 0x1f;
6701         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6702                      "d,w,<", AT, sreg, rot);
6703         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6704                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6705         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6706                      "d,v,t", dreg, dreg, AT);
6707       }
6708       break;
6709
6710     case M_ROR_I:
6711       {
6712         unsigned int rot;
6713
6714         if (imm_expr.X_op != O_constant)
6715           as_bad (_("rotate count too large"));
6716         rot = imm_expr.X_add_number & 0x1f;
6717         if (! rot)
6718           break;
6719         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6720                      "d,w,<", AT, sreg, rot);
6721         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6722                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6723         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6724                      "d,v,t", dreg, dreg, AT);
6725       }
6726       break;
6727
6728     case M_S_DOB:
6729       if (mips_arch == CPU_R4650)
6730         {
6731           as_bad (_("opcode not supported on this processor"));
6732           return;
6733         }
6734       assert (mips_opts.isa == ISA_MIPS1);
6735       /* Even on a big endian machine $fn comes before $fn+1.  We have
6736          to adjust when storing to memory.  */
6737       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6738                    target_big_endian ? treg + 1 : treg,
6739                    (int) BFD_RELOC_LO16, breg);
6740       offset_expr.X_add_number += 4;
6741       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6742                    target_big_endian ? treg : treg + 1,
6743                    (int) BFD_RELOC_LO16, breg);
6744       return;
6745
6746     case M_SEQ:
6747       if (sreg == 0)
6748         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6749                      treg, (int) BFD_RELOC_LO16);
6750       else if (treg == 0)
6751         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6752                      sreg, (int) BFD_RELOC_LO16);
6753       else
6754         {
6755           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6756                        "d,v,t", dreg, sreg, treg);
6757           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6758                        dreg, (int) BFD_RELOC_LO16);
6759         }
6760       return;
6761
6762     case M_SEQ_I:
6763       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6764         {
6765           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6766                        sreg, (int) BFD_RELOC_LO16);
6767           return;
6768         }
6769       if (sreg == 0)
6770         {
6771           as_warn (_("Instruction %s: result is always false"),
6772                    ip->insn_mo->name);
6773           move_register (&icnt, dreg, 0);
6774           return;
6775         }
6776       if (imm_expr.X_op == O_constant
6777           && imm_expr.X_add_number >= 0
6778           && imm_expr.X_add_number < 0x10000)
6779         {
6780           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6781                        sreg, (int) BFD_RELOC_LO16);
6782           used_at = 0;
6783         }
6784       else if (imm_expr.X_op == O_constant
6785                && imm_expr.X_add_number > -0x8000
6786                && imm_expr.X_add_number < 0)
6787         {
6788           imm_expr.X_add_number = -imm_expr.X_add_number;
6789           macro_build ((char *) NULL, &icnt, &imm_expr,
6790                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6791                        "t,r,j", dreg, sreg,
6792                        (int) BFD_RELOC_LO16);
6793           used_at = 0;
6794         }
6795       else
6796         {
6797           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6798           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6799                        "d,v,t", dreg, sreg, AT);
6800           used_at = 1;
6801         }
6802       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6803                    (int) BFD_RELOC_LO16);
6804       if (used_at)
6805         break;
6806       return;
6807
6808     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6809       s = "slt";
6810       goto sge;
6811     case M_SGEU:
6812       s = "sltu";
6813     sge:
6814       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6815                    dreg, sreg, treg);
6816       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6817                    (int) BFD_RELOC_LO16);
6818       return;
6819
6820     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6821     case M_SGEU_I:
6822       if (imm_expr.X_op == O_constant
6823           && imm_expr.X_add_number >= -0x8000
6824           && imm_expr.X_add_number < 0x8000)
6825         {
6826           macro_build ((char *) NULL, &icnt, &imm_expr,
6827                        mask == M_SGE_I ? "slti" : "sltiu",
6828                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6829           used_at = 0;
6830         }
6831       else
6832         {
6833           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6834           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6835                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6836                        AT);
6837           used_at = 1;
6838         }
6839       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6840                    (int) BFD_RELOC_LO16);
6841       if (used_at)
6842         break;
6843       return;
6844
6845     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6846       s = "slt";
6847       goto sgt;
6848     case M_SGTU:
6849       s = "sltu";
6850     sgt:
6851       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6852                    dreg, treg, sreg);
6853       return;
6854
6855     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6856       s = "slt";
6857       goto sgti;
6858     case M_SGTU_I:
6859       s = "sltu";
6860     sgti:
6861       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6862       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6863                    dreg, AT, sreg);
6864       break;
6865
6866     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6867       s = "slt";
6868       goto sle;
6869     case M_SLEU:
6870       s = "sltu";
6871     sle:
6872       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6873                    dreg, treg, sreg);
6874       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6875                    (int) BFD_RELOC_LO16);
6876       return;
6877
6878     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6879       s = "slt";
6880       goto slei;
6881     case M_SLEU_I:
6882       s = "sltu";
6883     slei:
6884       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6885       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6886                    dreg, AT, sreg);
6887       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6888                    (int) BFD_RELOC_LO16);
6889       break;
6890
6891     case M_SLT_I:
6892       if (imm_expr.X_op == O_constant
6893           && imm_expr.X_add_number >= -0x8000
6894           && imm_expr.X_add_number < 0x8000)
6895         {
6896           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6897                        dreg, sreg, (int) BFD_RELOC_LO16);
6898           return;
6899         }
6900       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6901       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6902                    dreg, sreg, AT);
6903       break;
6904
6905     case M_SLTU_I:
6906       if (imm_expr.X_op == O_constant
6907           && imm_expr.X_add_number >= -0x8000
6908           && imm_expr.X_add_number < 0x8000)
6909         {
6910           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6911                        dreg, sreg, (int) BFD_RELOC_LO16);
6912           return;
6913         }
6914       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6915       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6916                    "d,v,t", dreg, sreg, AT);
6917       break;
6918
6919     case M_SNE:
6920       if (sreg == 0)
6921         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6922                      "d,v,t", dreg, 0, treg);
6923       else if (treg == 0)
6924         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6925                      "d,v,t", dreg, 0, sreg);
6926       else
6927         {
6928           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6929                        "d,v,t", dreg, sreg, treg);
6930           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6931                        "d,v,t", dreg, 0, dreg);
6932         }
6933       return;
6934
6935     case M_SNE_I:
6936       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6937         {
6938           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6939                        "d,v,t", dreg, 0, sreg);
6940           return;
6941         }
6942       if (sreg == 0)
6943         {
6944           as_warn (_("Instruction %s: result is always true"),
6945                    ip->insn_mo->name);
6946           macro_build ((char *) NULL, &icnt, &expr1,
6947                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6948                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6949           return;
6950         }
6951       if (imm_expr.X_op == O_constant
6952           && imm_expr.X_add_number >= 0
6953           && imm_expr.X_add_number < 0x10000)
6954         {
6955           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6956                        dreg, sreg, (int) BFD_RELOC_LO16);
6957           used_at = 0;
6958         }
6959       else if (imm_expr.X_op == O_constant
6960                && imm_expr.X_add_number > -0x8000
6961                && imm_expr.X_add_number < 0)
6962         {
6963           imm_expr.X_add_number = -imm_expr.X_add_number;
6964           macro_build ((char *) NULL, &icnt, &imm_expr,
6965                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6966                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6967           used_at = 0;
6968         }
6969       else
6970         {
6971           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6972           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6973                        "d,v,t", dreg, sreg, AT);
6974           used_at = 1;
6975         }
6976       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6977                    "d,v,t", dreg, 0, dreg);
6978       if (used_at)
6979         break;
6980       return;
6981
6982     case M_DSUB_I:
6983       dbl = 1;
6984     case M_SUB_I:
6985       if (imm_expr.X_op == O_constant
6986           && imm_expr.X_add_number > -0x8000
6987           && imm_expr.X_add_number <= 0x8000)
6988         {
6989           imm_expr.X_add_number = -imm_expr.X_add_number;
6990           macro_build ((char *) NULL, &icnt, &imm_expr,
6991                        dbl ? "daddi" : "addi",
6992                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6993           return;
6994         }
6995       load_register (&icnt, AT, &imm_expr, dbl);
6996       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6997                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6998       break;
6999
7000     case M_DSUBU_I:
7001       dbl = 1;
7002     case M_SUBU_I:
7003       if (imm_expr.X_op == O_constant
7004           && imm_expr.X_add_number > -0x8000
7005           && imm_expr.X_add_number <= 0x8000)
7006         {
7007           imm_expr.X_add_number = -imm_expr.X_add_number;
7008           macro_build ((char *) NULL, &icnt, &imm_expr,
7009                        dbl ? "daddiu" : "addiu",
7010                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7011           return;
7012         }
7013       load_register (&icnt, AT, &imm_expr, dbl);
7014       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7015                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7016       break;
7017
7018     case M_TEQ_I:
7019       s = "teq";
7020       goto trap;
7021     case M_TGE_I:
7022       s = "tge";
7023       goto trap;
7024     case M_TGEU_I:
7025       s = "tgeu";
7026       goto trap;
7027     case M_TLT_I:
7028       s = "tlt";
7029       goto trap;
7030     case M_TLTU_I:
7031       s = "tltu";
7032       goto trap;
7033     case M_TNE_I:
7034       s = "tne";
7035     trap:
7036       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7037       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7038                    AT);
7039       break;
7040
7041     case M_TRUNCWS:
7042     case M_TRUNCWD:
7043       assert (mips_opts.isa == ISA_MIPS1);
7044       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7045       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7046
7047       /*
7048        * Is the double cfc1 instruction a bug in the mips assembler;
7049        * or is there a reason for it?
7050        */
7051       mips_emit_delays (true);
7052       ++mips_opts.noreorder;
7053       mips_any_noreorder = 1;
7054       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7055                    treg, RA);
7056       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7057                    treg, RA);
7058       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7059       expr1.X_add_number = 3;
7060       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7061                    (int) BFD_RELOC_LO16);
7062       expr1.X_add_number = 2;
7063       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7064                      (int) BFD_RELOC_LO16);
7065       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7066                    AT, RA);
7067       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7068       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7069               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7070       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7071                    treg, RA);
7072       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7073       --mips_opts.noreorder;
7074       break;
7075
7076     case M_ULH:
7077       s = "lb";
7078       goto ulh;
7079     case M_ULHU:
7080       s = "lbu";
7081     ulh:
7082       if (offset_expr.X_add_number >= 0x7fff)
7083         as_bad (_("operand overflow"));
7084       /* avoid load delay */
7085       if (! target_big_endian)
7086         ++offset_expr.X_add_number;
7087       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7088                    (int) BFD_RELOC_LO16, breg);
7089       if (! target_big_endian)
7090         --offset_expr.X_add_number;
7091       else
7092         ++offset_expr.X_add_number;
7093       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7094                    (int) BFD_RELOC_LO16, breg);
7095       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7096                    treg, treg, 8);
7097       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7098                    treg, treg, AT);
7099       break;
7100
7101     case M_ULD:
7102       s = "ldl";
7103       s2 = "ldr";
7104       off = 7;
7105       goto ulw;
7106     case M_ULW:
7107       s = "lwl";
7108       s2 = "lwr";
7109       off = 3;
7110     ulw:
7111       if (offset_expr.X_add_number >= 0x8000 - off)
7112         as_bad (_("operand overflow"));
7113       if (! target_big_endian)
7114         offset_expr.X_add_number += off;
7115       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7116                    (int) BFD_RELOC_LO16, breg);
7117       if (! target_big_endian)
7118         offset_expr.X_add_number -= off;
7119       else
7120         offset_expr.X_add_number += off;
7121       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7122                    (int) BFD_RELOC_LO16, breg);
7123       return;
7124
7125     case M_ULD_A:
7126       s = "ldl";
7127       s2 = "ldr";
7128       off = 7;
7129       goto ulwa;
7130     case M_ULW_A:
7131       s = "lwl";
7132       s2 = "lwr";
7133       off = 3;
7134     ulwa:
7135       used_at = 1;
7136       load_address (&icnt, AT, &offset_expr, &used_at);
7137       if (breg != 0)
7138         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7139                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7140                      "d,v,t", AT, AT, breg);
7141       if (! target_big_endian)
7142         expr1.X_add_number = off;
7143       else
7144         expr1.X_add_number = 0;
7145       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7146                    (int) BFD_RELOC_LO16, AT);
7147       if (! target_big_endian)
7148         expr1.X_add_number = 0;
7149       else
7150         expr1.X_add_number = off;
7151       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7152                    (int) BFD_RELOC_LO16, AT);
7153       break;
7154
7155     case M_ULH_A:
7156     case M_ULHU_A:
7157       used_at = 1;
7158       load_address (&icnt, AT, &offset_expr, &used_at);
7159       if (breg != 0)
7160         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7161                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7162                      "d,v,t", AT, AT, breg);
7163       if (target_big_endian)
7164         expr1.X_add_number = 0;
7165       macro_build ((char *) NULL, &icnt, &expr1,
7166                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7167                    (int) BFD_RELOC_LO16, AT);
7168       if (target_big_endian)
7169         expr1.X_add_number = 1;
7170       else
7171         expr1.X_add_number = 0;
7172       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7173                    (int) BFD_RELOC_LO16, AT);
7174       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7175                    treg, treg, 8);
7176       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7177                    treg, treg, AT);
7178       break;
7179
7180     case M_USH:
7181       if (offset_expr.X_add_number >= 0x7fff)
7182         as_bad (_("operand overflow"));
7183       if (target_big_endian)
7184         ++offset_expr.X_add_number;
7185       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7186                    (int) BFD_RELOC_LO16, breg);
7187       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7188                    AT, treg, 8);
7189       if (target_big_endian)
7190         --offset_expr.X_add_number;
7191       else
7192         ++offset_expr.X_add_number;
7193       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7194                    (int) BFD_RELOC_LO16, breg);
7195       break;
7196
7197     case M_USD:
7198       s = "sdl";
7199       s2 = "sdr";
7200       off = 7;
7201       goto usw;
7202     case M_USW:
7203       s = "swl";
7204       s2 = "swr";
7205       off = 3;
7206     usw:
7207       if (offset_expr.X_add_number >= 0x8000 - off)
7208         as_bad (_("operand overflow"));
7209       if (! target_big_endian)
7210         offset_expr.X_add_number += off;
7211       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7212                    (int) BFD_RELOC_LO16, breg);
7213       if (! target_big_endian)
7214         offset_expr.X_add_number -= off;
7215       else
7216         offset_expr.X_add_number += off;
7217       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7218                    (int) BFD_RELOC_LO16, breg);
7219       return;
7220
7221     case M_USD_A:
7222       s = "sdl";
7223       s2 = "sdr";
7224       off = 7;
7225       goto uswa;
7226     case M_USW_A:
7227       s = "swl";
7228       s2 = "swr";
7229       off = 3;
7230     uswa:
7231       used_at = 1;
7232       load_address (&icnt, AT, &offset_expr, &used_at);
7233       if (breg != 0)
7234         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7235                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7236                      "d,v,t", AT, AT, breg);
7237       if (! target_big_endian)
7238         expr1.X_add_number = off;
7239       else
7240         expr1.X_add_number = 0;
7241       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7242                    (int) BFD_RELOC_LO16, AT);
7243       if (! target_big_endian)
7244         expr1.X_add_number = 0;
7245       else
7246         expr1.X_add_number = off;
7247       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7248                    (int) BFD_RELOC_LO16, AT);
7249       break;
7250
7251     case M_USH_A:
7252       used_at = 1;
7253       load_address (&icnt, AT, &offset_expr, &used_at);
7254       if (breg != 0)
7255         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7256                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7257                      "d,v,t", AT, AT, breg);
7258       if (! target_big_endian)
7259         expr1.X_add_number = 0;
7260       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7261                    (int) BFD_RELOC_LO16, AT);
7262       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7263                    treg, treg, 8);
7264       if (! target_big_endian)
7265         expr1.X_add_number = 1;
7266       else
7267         expr1.X_add_number = 0;
7268       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7269                    (int) BFD_RELOC_LO16, AT);
7270       if (! target_big_endian)
7271         expr1.X_add_number = 0;
7272       else
7273         expr1.X_add_number = 1;
7274       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7275                    (int) BFD_RELOC_LO16, AT);
7276       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7277                    treg, treg, 8);
7278       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7279                    treg, treg, AT);
7280       break;
7281
7282     default:
7283       /* FIXME: Check if this is one of the itbl macros, since they
7284          are added dynamically.  */
7285       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7286       break;
7287     }
7288   if (mips_opts.noat)
7289     as_warn (_("Macro used $at after \".set noat\""));
7290 }
7291
7292 /* Implement macros in mips16 mode.  */
7293
7294 static void
7295 mips16_macro (ip)
7296      struct mips_cl_insn *ip;
7297 {
7298   int mask;
7299   int xreg, yreg, zreg, tmp;
7300   int icnt;
7301   expressionS expr1;
7302   int dbl;
7303   const char *s, *s2, *s3;
7304
7305   mask = ip->insn_mo->mask;
7306
7307   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7308   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7309   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7310
7311   icnt = 0;
7312
7313   expr1.X_op = O_constant;
7314   expr1.X_op_symbol = NULL;
7315   expr1.X_add_symbol = NULL;
7316   expr1.X_add_number = 1;
7317
7318   dbl = 0;
7319
7320   switch (mask)
7321     {
7322     default:
7323       internalError ();
7324
7325     case M_DDIV_3:
7326       dbl = 1;
7327     case M_DIV_3:
7328       s = "mflo";
7329       goto do_div3;
7330     case M_DREM_3:
7331       dbl = 1;
7332     case M_REM_3:
7333       s = "mfhi";
7334     do_div3:
7335       mips_emit_delays (true);
7336       ++mips_opts.noreorder;
7337       mips_any_noreorder = 1;
7338       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7339                    dbl ? "ddiv" : "div",
7340                    "0,x,y", xreg, yreg);
7341       expr1.X_add_number = 2;
7342       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7343       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7344                    7);
7345
7346       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7347          since that causes an overflow.  We should do that as well,
7348          but I don't see how to do the comparisons without a temporary
7349          register.  */
7350       --mips_opts.noreorder;
7351       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7352       break;
7353
7354     case M_DIVU_3:
7355       s = "divu";
7356       s2 = "mflo";
7357       goto do_divu3;
7358     case M_REMU_3:
7359       s = "divu";
7360       s2 = "mfhi";
7361       goto do_divu3;
7362     case M_DDIVU_3:
7363       s = "ddivu";
7364       s2 = "mflo";
7365       goto do_divu3;
7366     case M_DREMU_3:
7367       s = "ddivu";
7368       s2 = "mfhi";
7369     do_divu3:
7370       mips_emit_delays (true);
7371       ++mips_opts.noreorder;
7372       mips_any_noreorder = 1;
7373       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7374                    xreg, yreg);
7375       expr1.X_add_number = 2;
7376       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7377       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7378                    "6", 7);
7379       --mips_opts.noreorder;
7380       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7381       break;
7382
7383     case M_DMUL:
7384       dbl = 1;
7385     case M_MUL:
7386       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7387                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7388       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7389                    zreg);
7390       return;
7391
7392     case M_DSUBU_I:
7393       dbl = 1;
7394       goto do_subu;
7395     case M_SUBU_I:
7396     do_subu:
7397       if (imm_expr.X_op != O_constant)
7398         as_bad (_("Unsupported large constant"));
7399       imm_expr.X_add_number = -imm_expr.X_add_number;
7400       macro_build ((char *) NULL, &icnt, &imm_expr,
7401                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7402       break;
7403
7404     case M_SUBU_I_2:
7405       if (imm_expr.X_op != O_constant)
7406         as_bad (_("Unsupported large constant"));
7407       imm_expr.X_add_number = -imm_expr.X_add_number;
7408       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7409                    "x,k", xreg);
7410       break;
7411
7412     case M_DSUBU_I_2:
7413       if (imm_expr.X_op != O_constant)
7414         as_bad (_("Unsupported large constant"));
7415       imm_expr.X_add_number = -imm_expr.X_add_number;
7416       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7417                    "y,j", yreg);
7418       break;
7419
7420     case M_BEQ:
7421       s = "cmp";
7422       s2 = "bteqz";
7423       goto do_branch;
7424     case M_BNE:
7425       s = "cmp";
7426       s2 = "btnez";
7427       goto do_branch;
7428     case M_BLT:
7429       s = "slt";
7430       s2 = "btnez";
7431       goto do_branch;
7432     case M_BLTU:
7433       s = "sltu";
7434       s2 = "btnez";
7435       goto do_branch;
7436     case M_BLE:
7437       s = "slt";
7438       s2 = "bteqz";
7439       goto do_reverse_branch;
7440     case M_BLEU:
7441       s = "sltu";
7442       s2 = "bteqz";
7443       goto do_reverse_branch;
7444     case M_BGE:
7445       s = "slt";
7446       s2 = "bteqz";
7447       goto do_branch;
7448     case M_BGEU:
7449       s = "sltu";
7450       s2 = "bteqz";
7451       goto do_branch;
7452     case M_BGT:
7453       s = "slt";
7454       s2 = "btnez";
7455       goto do_reverse_branch;
7456     case M_BGTU:
7457       s = "sltu";
7458       s2 = "btnez";
7459
7460     do_reverse_branch:
7461       tmp = xreg;
7462       xreg = yreg;
7463       yreg = tmp;
7464
7465     do_branch:
7466       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7467                    xreg, yreg);
7468       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7469       break;
7470
7471     case M_BEQ_I:
7472       s = "cmpi";
7473       s2 = "bteqz";
7474       s3 = "x,U";
7475       goto do_branch_i;
7476     case M_BNE_I:
7477       s = "cmpi";
7478       s2 = "btnez";
7479       s3 = "x,U";
7480       goto do_branch_i;
7481     case M_BLT_I:
7482       s = "slti";
7483       s2 = "btnez";
7484       s3 = "x,8";
7485       goto do_branch_i;
7486     case M_BLTU_I:
7487       s = "sltiu";
7488       s2 = "btnez";
7489       s3 = "x,8";
7490       goto do_branch_i;
7491     case M_BLE_I:
7492       s = "slti";
7493       s2 = "btnez";
7494       s3 = "x,8";
7495       goto do_addone_branch_i;
7496     case M_BLEU_I:
7497       s = "sltiu";
7498       s2 = "btnez";
7499       s3 = "x,8";
7500       goto do_addone_branch_i;
7501     case M_BGE_I:
7502       s = "slti";
7503       s2 = "bteqz";
7504       s3 = "x,8";
7505       goto do_branch_i;
7506     case M_BGEU_I:
7507       s = "sltiu";
7508       s2 = "bteqz";
7509       s3 = "x,8";
7510       goto do_branch_i;
7511     case M_BGT_I:
7512       s = "slti";
7513       s2 = "bteqz";
7514       s3 = "x,8";
7515       goto do_addone_branch_i;
7516     case M_BGTU_I:
7517       s = "sltiu";
7518       s2 = "bteqz";
7519       s3 = "x,8";
7520
7521     do_addone_branch_i:
7522       if (imm_expr.X_op != O_constant)
7523         as_bad (_("Unsupported large constant"));
7524       ++imm_expr.X_add_number;
7525
7526     do_branch_i:
7527       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7528       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7529       break;
7530
7531     case M_ABS:
7532       expr1.X_add_number = 0;
7533       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7534       if (xreg != yreg)
7535         move_register (&icnt, xreg, yreg);
7536       expr1.X_add_number = 2;
7537       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7538       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7539                    "neg", "x,w", xreg, xreg);
7540     }
7541 }
7542
7543 /* For consistency checking, verify that all bits are specified either
7544    by the match/mask part of the instruction definition, or by the
7545    operand list.  */
7546 static int
7547 validate_mips_insn (opc)
7548      const struct mips_opcode *opc;
7549 {
7550   const char *p = opc->args;
7551   char c;
7552   unsigned long used_bits = opc->mask;
7553
7554   if ((used_bits & opc->match) != opc->match)
7555     {
7556       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7557               opc->name, opc->args);
7558       return 0;
7559     }
7560 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7561   while (*p)
7562     switch (c = *p++)
7563       {
7564       case ',': break;
7565       case '(': break;
7566       case ')': break;
7567       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7568       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7569       case 'A': break;
7570       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7571       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7572       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7573       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7574       case 'F': break;
7575       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7576       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7577       case 'I': break;
7578       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7579       case 'L': break;
7580       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7581       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7582       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7583       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7584                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7585       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7586       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7587       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7588       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7589       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7590       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7591       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7592       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7593       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7594       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7595       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7596       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7597       case 'f': break;
7598       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7599       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7600       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7601       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7602       case 'l': break;
7603       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7604       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7605       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7606       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7607       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7608       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7609       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7610       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7611       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7612       case 'x': break;
7613       case 'z': break;
7614       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7615       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7616                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7617       default:
7618         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7619                 c, opc->name, opc->args);
7620         return 0;
7621       }
7622 #undef USE_BITS
7623   if (used_bits != 0xffffffff)
7624     {
7625       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7626               ~used_bits & 0xffffffff, opc->name, opc->args);
7627       return 0;
7628     }
7629   return 1;
7630 }
7631
7632 /* This routine assembles an instruction into its binary format.  As a
7633    side effect, it sets one of the global variables imm_reloc or
7634    offset_reloc to the type of relocation to do if one of the operands
7635    is an address expression.  */
7636
7637 static void
7638 mips_ip (str, ip)
7639      char *str;
7640      struct mips_cl_insn *ip;
7641 {
7642   char *s;
7643   const char *args;
7644   char c = 0;
7645   struct mips_opcode *insn;
7646   char *argsStart;
7647   unsigned int regno;
7648   unsigned int lastregno = 0;
7649   char *s_reset;
7650   char save_c = 0;
7651
7652   insn_error = NULL;
7653
7654   /* If the instruction contains a '.', we first try to match an instruction
7655      including the '.'.  Then we try again without the '.'.  */
7656   insn = NULL;
7657   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7658     continue;
7659
7660   /* If we stopped on whitespace, then replace the whitespace with null for
7661      the call to hash_find.  Save the character we replaced just in case we
7662      have to re-parse the instruction.  */
7663   if (ISSPACE (*s))
7664     {
7665       save_c = *s;
7666       *s++ = '\0';
7667     }
7668
7669   insn = (struct mips_opcode *) hash_find (op_hash, str);
7670
7671   /* If we didn't find the instruction in the opcode table, try again, but
7672      this time with just the instruction up to, but not including the
7673      first '.'.  */
7674   if (insn == NULL)
7675     {
7676       /* Restore the character we overwrite above (if any).  */
7677       if (save_c)
7678         *(--s) = save_c;
7679
7680       /* Scan up to the first '.' or whitespace.  */
7681       for (s = str;
7682            *s != '\0' && *s != '.' && !ISSPACE (*s);
7683            ++s)
7684         continue;
7685
7686       /* If we did not find a '.', then we can quit now.  */
7687       if (*s != '.')
7688         {
7689           insn_error = "unrecognized opcode";
7690           return;
7691         }
7692
7693       /* Lookup the instruction in the hash table.  */
7694       *s++ = '\0';
7695       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7696         {
7697           insn_error = "unrecognized opcode";
7698           return;
7699         }
7700     }
7701
7702   argsStart = s;
7703   for (;;)
7704     {
7705       boolean ok;
7706
7707       assert (strcmp (insn->name, str) == 0);
7708
7709       if (OPCODE_IS_MEMBER (insn,
7710                             (mips_opts.isa
7711                              | (mips_opts.mips16 ? INSN_MIPS16 : 0)
7712                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7713                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7714                             mips_arch))
7715         ok = true;
7716       else
7717         ok = false;
7718
7719       if (insn->pinfo != INSN_MACRO)
7720         {
7721           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7722             ok = false;
7723         }
7724
7725       if (! ok)
7726         {
7727           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7728               && strcmp (insn->name, insn[1].name) == 0)
7729             {
7730               ++insn;
7731               continue;
7732             }
7733           else
7734             {
7735               if (!insn_error)
7736                 {
7737                   static char buf[100];
7738                   if (mips_arch_info->is_isa)
7739                     sprintf (buf,
7740                              _("opcode not supported at this ISA level (%s)"),
7741                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7742                   else
7743                     sprintf (buf,
7744                              _("opcode not supported on this processor: %s (%s)"),
7745                              mips_arch_info->name,
7746                              mips_cpu_info_from_isa (mips_opts.isa)->name);
7747                   insn_error = buf;
7748                 }
7749               if (save_c)
7750                 *(--s) = save_c;
7751               return;
7752             }
7753         }
7754
7755       ip->insn_mo = insn;
7756       ip->insn_opcode = insn->match;
7757       insn_error = NULL;
7758       for (args = insn->args;; ++args)
7759         {
7760           int is_mdmx;
7761
7762           s += strspn (s, " \t");
7763           is_mdmx = 0;
7764           switch (*args)
7765             {
7766             case '\0':          /* end of args */
7767               if (*s == '\0')
7768                 return;
7769               break;
7770
7771             case ',':
7772               if (*s++ == *args)
7773                 continue;
7774               s--;
7775               switch (*++args)
7776                 {
7777                 case 'r':
7778                 case 'v':
7779                   ip->insn_opcode |= lastregno << OP_SH_RS;
7780                   continue;
7781
7782                 case 'w':
7783                   ip->insn_opcode |= lastregno << OP_SH_RT;
7784                   continue;
7785
7786                 case 'W':
7787                   ip->insn_opcode |= lastregno << OP_SH_FT;
7788                   continue;
7789
7790                 case 'V':
7791                   ip->insn_opcode |= lastregno << OP_SH_FS;
7792                   continue;
7793                 }
7794               break;
7795
7796             case '(':
7797               /* Handle optional base register.
7798                  Either the base register is omitted or
7799                  we must have a left paren.  */
7800               /* This is dependent on the next operand specifier
7801                  is a base register specification.  */
7802               assert (args[1] == 'b' || args[1] == '5'
7803                       || args[1] == '-' || args[1] == '4');
7804               if (*s == '\0')
7805                 return;
7806
7807             case ')':           /* these must match exactly */
7808               if (*s++ == *args)
7809                 continue;
7810               break;
7811
7812             case '<':           /* must be at least one digit */
7813               /*
7814                * According to the manual, if the shift amount is greater
7815                * than 31 or less than 0, then the shift amount should be
7816                * mod 32.  In reality the mips assembler issues an error.
7817                * We issue a warning and mask out all but the low 5 bits.
7818                */
7819               my_getExpression (&imm_expr, s);
7820               check_absolute_expr (ip, &imm_expr);
7821               if ((unsigned long) imm_expr.X_add_number > 31)
7822                 {
7823                   as_warn (_("Improper shift amount (%lu)"),
7824                            (unsigned long) imm_expr.X_add_number);
7825                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7826                 }
7827               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7828               imm_expr.X_op = O_absent;
7829               s = expr_end;
7830               continue;
7831
7832             case '>':           /* shift amount minus 32 */
7833               my_getExpression (&imm_expr, s);
7834               check_absolute_expr (ip, &imm_expr);
7835               if ((unsigned long) imm_expr.X_add_number < 32
7836                   || (unsigned long) imm_expr.X_add_number > 63)
7837                 break;
7838               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7839               imm_expr.X_op = O_absent;
7840               s = expr_end;
7841               continue;
7842
7843             case 'k':           /* cache code */
7844             case 'h':           /* prefx code */
7845               my_getExpression (&imm_expr, s);
7846               check_absolute_expr (ip, &imm_expr);
7847               if ((unsigned long) imm_expr.X_add_number > 31)
7848                 {
7849                   as_warn (_("Invalid value for `%s' (%lu)"),
7850                            ip->insn_mo->name,
7851                            (unsigned long) imm_expr.X_add_number);
7852                   imm_expr.X_add_number &= 0x1f;
7853                 }
7854               if (*args == 'k')
7855                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7856               else
7857                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7858               imm_expr.X_op = O_absent;
7859               s = expr_end;
7860               continue;
7861
7862             case 'c':           /* break code */
7863               my_getExpression (&imm_expr, s);
7864               check_absolute_expr (ip, &imm_expr);
7865               if ((unsigned long) imm_expr.X_add_number > 1023)
7866                 {
7867                   as_warn (_("Illegal break code (%lu)"),
7868                            (unsigned long) imm_expr.X_add_number);
7869                   imm_expr.X_add_number &= OP_MASK_CODE;
7870                 }
7871               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7872               imm_expr.X_op = O_absent;
7873               s = expr_end;
7874               continue;
7875
7876             case 'q':           /* lower break code */
7877               my_getExpression (&imm_expr, s);
7878               check_absolute_expr (ip, &imm_expr);
7879               if ((unsigned long) imm_expr.X_add_number > 1023)
7880                 {
7881                   as_warn (_("Illegal lower break code (%lu)"),
7882                            (unsigned long) imm_expr.X_add_number);
7883                   imm_expr.X_add_number &= OP_MASK_CODE2;
7884                 }
7885               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7886               imm_expr.X_op = O_absent;
7887               s = expr_end;
7888               continue;
7889
7890             case 'B':           /* 20-bit syscall/break code.  */
7891               my_getExpression (&imm_expr, s);
7892               check_absolute_expr (ip, &imm_expr);
7893               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7894                 as_warn (_("Illegal 20-bit code (%lu)"),
7895                          (unsigned long) imm_expr.X_add_number);
7896               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7897               imm_expr.X_op = O_absent;
7898               s = expr_end;
7899               continue;
7900
7901             case 'C':           /* Coprocessor code */
7902               my_getExpression (&imm_expr, s);
7903               check_absolute_expr (ip, &imm_expr);
7904               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
7905                 {
7906                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
7907                            (unsigned long) imm_expr.X_add_number);
7908                   imm_expr.X_add_number &= ((1 << 25) - 1);
7909                 }
7910               ip->insn_opcode |= imm_expr.X_add_number;
7911               imm_expr.X_op = O_absent;
7912               s = expr_end;
7913               continue;
7914
7915             case 'J':           /* 19-bit wait code.  */
7916               my_getExpression (&imm_expr, s);
7917               check_absolute_expr (ip, &imm_expr);
7918               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7919                 as_warn (_("Illegal 19-bit code (%lu)"),
7920                          (unsigned long) imm_expr.X_add_number);
7921               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7922               imm_expr.X_op = O_absent;
7923               s = expr_end;
7924               continue;
7925
7926             case 'P':           /* Performance register */
7927               my_getExpression (&imm_expr, s);
7928               check_absolute_expr (ip, &imm_expr);
7929               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7930                 {
7931                   as_warn (_("Invalid performance register (%lu)"),
7932                            (unsigned long) imm_expr.X_add_number);
7933                   imm_expr.X_add_number &= OP_MASK_PERFREG;
7934                 }
7935               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7936               imm_expr.X_op = O_absent;
7937               s = expr_end;
7938               continue;
7939
7940             case 'b':           /* base register */
7941             case 'd':           /* destination register */
7942             case 's':           /* source register */
7943             case 't':           /* target register */
7944             case 'r':           /* both target and source */
7945             case 'v':           /* both dest and source */
7946             case 'w':           /* both dest and target */
7947             case 'E':           /* coprocessor target register */
7948             case 'G':           /* coprocessor destination register */
7949             case 'x':           /* ignore register name */
7950             case 'z':           /* must be zero register */
7951             case 'U':           /* destination register (clo/clz).  */
7952               s_reset = s;
7953               if (s[0] == '$')
7954                 {
7955
7956                   if (ISDIGIT (s[1]))
7957                     {
7958                       ++s;
7959                       regno = 0;
7960                       do
7961                         {
7962                           regno *= 10;
7963                           regno += *s - '0';
7964                           ++s;
7965                         }
7966                       while (ISDIGIT (*s));
7967                       if (regno > 31)
7968                         as_bad (_("Invalid register number (%d)"), regno);
7969                     }
7970                   else if (*args == 'E' || *args == 'G')
7971                     goto notreg;
7972                   else
7973                     {
7974                       if (s[1] == 'r' && s[2] == 'a')
7975                         {
7976                           s += 3;
7977                           regno = RA;
7978                         }
7979                       else if (s[1] == 'f' && s[2] == 'p')
7980                         {
7981                           s += 3;
7982                           regno = FP;
7983                         }
7984                       else if (s[1] == 's' && s[2] == 'p')
7985                         {
7986                           s += 3;
7987                           regno = SP;
7988                         }
7989                       else if (s[1] == 'g' && s[2] == 'p')
7990                         {
7991                           s += 3;
7992                           regno = GP;
7993                         }
7994                       else if (s[1] == 'a' && s[2] == 't')
7995                         {
7996                           s += 3;
7997                           regno = AT;
7998                         }
7999                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8000                         {
8001                           s += 4;
8002                           regno = KT0;
8003                         }
8004                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8005                         {
8006                           s += 4;
8007                           regno = KT1;
8008                         }
8009                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8010                         {
8011                           s += 5;
8012                           regno = ZERO;
8013                         }
8014                       else if (itbl_have_entries)
8015                         {
8016                           char *p, *n;
8017                           unsigned long r;
8018
8019                           p = s + 1;    /* advance past '$' */
8020                           n = itbl_get_field (&p);  /* n is name */
8021
8022                           /* See if this is a register defined in an
8023                              itbl entry.  */
8024                           if (itbl_get_reg_val (n, &r))
8025                             {
8026                               /* Get_field advances to the start of
8027                                  the next field, so we need to back
8028                                  rack to the end of the last field.  */
8029                               if (p)
8030                                 s = p - 1;
8031                               else
8032                                 s = strchr (s, '\0');
8033                               regno = r;
8034                             }
8035                           else
8036                             goto notreg;
8037                         }
8038                       else
8039                         goto notreg;
8040                     }
8041                   if (regno == AT
8042                       && ! mips_opts.noat
8043                       && *args != 'E'
8044                       && *args != 'G')
8045                     as_warn (_("Used $at without \".set noat\""));
8046                   c = *args;
8047                   if (*s == ' ')
8048                     ++s;
8049                   if (args[1] != *s)
8050                     {
8051                       if (c == 'r' || c == 'v' || c == 'w')
8052                         {
8053                           regno = lastregno;
8054                           s = s_reset;
8055                           ++args;
8056                         }
8057                     }
8058                   /* 'z' only matches $0.  */
8059                   if (c == 'z' && regno != 0)
8060                     break;
8061
8062         /* Now that we have assembled one operand, we use the args string
8063          * to figure out where it goes in the instruction.  */
8064                   switch (c)
8065                     {
8066                     case 'r':
8067                     case 's':
8068                     case 'v':
8069                     case 'b':
8070                       ip->insn_opcode |= regno << OP_SH_RS;
8071                       break;
8072                     case 'd':
8073                     case 'G':
8074                       ip->insn_opcode |= regno << OP_SH_RD;
8075                       break;
8076                     case 'U':
8077                       ip->insn_opcode |= regno << OP_SH_RD;
8078                       ip->insn_opcode |= regno << OP_SH_RT;
8079                       break;
8080                     case 'w':
8081                     case 't':
8082                     case 'E':
8083                       ip->insn_opcode |= regno << OP_SH_RT;
8084                       break;
8085                     case 'x':
8086                       /* This case exists because on the r3000 trunc
8087                          expands into a macro which requires a gp
8088                          register.  On the r6000 or r4000 it is
8089                          assembled into a single instruction which
8090                          ignores the register.  Thus the insn version
8091                          is MIPS_ISA2 and uses 'x', and the macro
8092                          version is MIPS_ISA1 and uses 't'.  */
8093                       break;
8094                     case 'z':
8095                       /* This case is for the div instruction, which
8096                          acts differently if the destination argument
8097                          is $0.  This only matches $0, and is checked
8098                          outside the switch.  */
8099                       break;
8100                     case 'D':
8101                       /* Itbl operand; not yet implemented. FIXME ?? */
8102                       break;
8103                       /* What about all other operands like 'i', which
8104                          can be specified in the opcode table? */
8105                     }
8106                   lastregno = regno;
8107                   continue;
8108                 }
8109             notreg:
8110               switch (*args++)
8111                 {
8112                 case 'r':
8113                 case 'v':
8114                   ip->insn_opcode |= lastregno << OP_SH_RS;
8115                   continue;
8116                 case 'w':
8117                   ip->insn_opcode |= lastregno << OP_SH_RT;
8118                   continue;
8119                 }
8120               break;
8121
8122             case 'O':           /* MDMX alignment immediate constant.  */
8123               my_getExpression (&imm_expr, s);
8124               check_absolute_expr (ip, &imm_expr);
8125               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8126                 {
8127                   as_warn ("Improper align amount (%ld), using low bits",
8128                            (long) imm_expr.X_add_number);
8129                   imm_expr.X_add_number &= OP_MASK_ALN;
8130                 }
8131               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8132               imm_expr.X_op = O_absent;
8133               s = expr_end;
8134               continue;
8135
8136             case 'Q':           /* MDMX vector, element sel, or const.  */
8137               if (s[0] != '$')
8138                 {
8139                   /* MDMX Immediate.  */
8140                   my_getExpression (&imm_expr, s);
8141                   check_absolute_expr (ip, &imm_expr);
8142                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8143                     {
8144                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8145                                (long) imm_expr.X_add_number);
8146                       imm_expr.X_add_number &= OP_MASK_FT;
8147                     }
8148                   imm_expr.X_add_number &= OP_MASK_FT;
8149                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8150                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8151                   else
8152                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8153                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8154                   imm_expr.X_op = O_absent;
8155                   s = expr_end;
8156                   continue;
8157                 }
8158               /* Not MDMX Immediate.  Fall through.  */
8159             case 'X':           /* MDMX destination register.  */
8160             case 'Y':           /* MDMX source register.  */
8161             case 'Z':           /* MDMX target register.  */
8162               is_mdmx = 1;
8163             case 'D':           /* floating point destination register */
8164             case 'S':           /* floating point source register */
8165             case 'T':           /* floating point target register */
8166             case 'R':           /* floating point source register */
8167             case 'V':
8168             case 'W':
8169               s_reset = s;
8170               /* Accept $fN for FP and MDMX register numbers, and in
8171                  addition accept $vN for MDMX register numbers.  */
8172               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8173                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8174                       && ISDIGIT (s[2])))
8175                 {
8176                   s += 2;
8177                   regno = 0;
8178                   do
8179                     {
8180                       regno *= 10;
8181                       regno += *s - '0';
8182                       ++s;
8183                     }
8184                   while (ISDIGIT (*s));
8185
8186                   if (regno > 31)
8187                     as_bad (_("Invalid float register number (%d)"), regno);
8188
8189                   if ((regno & 1) != 0
8190                       && HAVE_32BIT_FPRS
8191                       && ! (strcmp (str, "mtc1") == 0
8192                             || strcmp (str, "mfc1") == 0
8193                             || strcmp (str, "lwc1") == 0
8194                             || strcmp (str, "swc1") == 0
8195                             || strcmp (str, "l.s") == 0
8196                             || strcmp (str, "s.s") == 0))
8197                     as_warn (_("Float register should be even, was %d"),
8198                              regno);
8199
8200                   c = *args;
8201                   if (*s == ' ')
8202                     ++s;
8203                   if (args[1] != *s)
8204                     {
8205                       if (c == 'V' || c == 'W')
8206                         {
8207                           regno = lastregno;
8208                           s = s_reset;
8209                           ++args;
8210                         }
8211                     }
8212                   switch (c)
8213                     {
8214                     case 'D':
8215                     case 'X':
8216                       ip->insn_opcode |= regno << OP_SH_FD;
8217                       break;
8218                     case 'V':
8219                     case 'S':
8220                     case 'Y':
8221                       ip->insn_opcode |= regno << OP_SH_FS;
8222                       break;
8223                     case 'Q':
8224                       /* This is like 'Z', but also needs to fix the MDMX
8225                          vector/scalar select bits.  Note that the
8226                          scalar immediate case is handled above.  */
8227                       if (*s == '[')
8228                         {
8229                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8230                           int max_el = (is_qh ? 3 : 7);
8231                           s++;
8232                           my_getExpression(&imm_expr, s);
8233                           check_absolute_expr (ip, &imm_expr);
8234                           s = expr_end;
8235                           if (imm_expr.X_add_number > max_el)
8236                             as_bad(_("Bad element selector %ld"),
8237                                    (long) imm_expr.X_add_number);
8238                           imm_expr.X_add_number &= max_el;
8239                           ip->insn_opcode |= (imm_expr.X_add_number
8240                                               << (OP_SH_VSEL +
8241                                                   (is_qh ? 2 : 1)));
8242                           if (*s != ']')
8243                             as_warn(_("Expecting ']' found '%s'"), s);
8244                           else
8245                             s++;
8246                         }
8247                       else
8248                         {
8249                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8250                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8251                                                 << OP_SH_VSEL);
8252                           else
8253                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8254                                                 OP_SH_VSEL);
8255                         }
8256                       /* Fall through */
8257                     case 'W':
8258                     case 'T':
8259                     case 'Z':
8260                       ip->insn_opcode |= regno << OP_SH_FT;
8261                       break;
8262                     case 'R':
8263                       ip->insn_opcode |= regno << OP_SH_FR;
8264                       break;
8265                     }
8266                   lastregno = regno;
8267                   continue;
8268                 }
8269
8270               switch (*args++)
8271                 {
8272                 case 'V':
8273                   ip->insn_opcode |= lastregno << OP_SH_FS;
8274                   continue;
8275                 case 'W':
8276                   ip->insn_opcode |= lastregno << OP_SH_FT;
8277                   continue;
8278                 }
8279               break;
8280
8281             case 'I':
8282               my_getExpression (&imm_expr, s);
8283               if (imm_expr.X_op != O_big
8284                   && imm_expr.X_op != O_constant)
8285                 insn_error = _("absolute expression required");
8286               s = expr_end;
8287               continue;
8288
8289             case 'A':
8290               my_getExpression (&offset_expr, s);
8291               *imm_reloc = BFD_RELOC_32;
8292               s = expr_end;
8293               continue;
8294
8295             case 'F':
8296             case 'L':
8297             case 'f':
8298             case 'l':
8299               {
8300                 int f64;
8301                 int using_gprs;
8302                 char *save_in;
8303                 char *err;
8304                 unsigned char temp[8];
8305                 int len;
8306                 unsigned int length;
8307                 segT seg;
8308                 subsegT subseg;
8309                 char *p;
8310
8311                 /* These only appear as the last operand in an
8312                    instruction, and every instruction that accepts
8313                    them in any variant accepts them in all variants.
8314                    This means we don't have to worry about backing out
8315                    any changes if the instruction does not match.
8316
8317                    The difference between them is the size of the
8318                    floating point constant and where it goes.  For 'F'
8319                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8320                    is 32 bits.  Where the constant is placed is based
8321                    on how the MIPS assembler does things:
8322                     F -- .rdata
8323                     L -- .lit8
8324                     f -- immediate value
8325                     l -- .lit4
8326
8327                     The .lit4 and .lit8 sections are only used if
8328                     permitted by the -G argument.
8329
8330                     When generating embedded PIC code, we use the
8331                     .lit8 section but not the .lit4 section (we can do
8332                     .lit4 inline easily; we need to put .lit8
8333                     somewhere in the data segment, and using .lit8
8334                     permits the linker to eventually combine identical
8335                     .lit8 entries).
8336
8337                     The code below needs to know whether the target register
8338                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8339                     'F' are used with GPR-based instructions and 'l' and
8340                     'L' are used with FPR-based instructions.  */
8341
8342                 f64 = *args == 'F' || *args == 'L';
8343                 using_gprs = *args == 'F' || *args == 'f';
8344
8345                 save_in = input_line_pointer;
8346                 input_line_pointer = s;
8347                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8348                 length = len;
8349                 s = input_line_pointer;
8350                 input_line_pointer = save_in;
8351                 if (err != NULL && *err != '\0')
8352                   {
8353                     as_bad (_("Bad floating point constant: %s"), err);
8354                     memset (temp, '\0', sizeof temp);
8355                     length = f64 ? 8 : 4;
8356                   }
8357
8358                 assert (length == (unsigned) (f64 ? 8 : 4));
8359
8360                 if (*args == 'f'
8361                     || (*args == 'l'
8362                         && (! USE_GLOBAL_POINTER_OPT
8363                             || mips_pic == EMBEDDED_PIC
8364                             || g_switch_value < 4
8365                             || (temp[0] == 0 && temp[1] == 0)
8366                             || (temp[2] == 0 && temp[3] == 0))))
8367                   {
8368                     imm_expr.X_op = O_constant;
8369                     if (! target_big_endian)
8370                       imm_expr.X_add_number = bfd_getl32 (temp);
8371                     else
8372                       imm_expr.X_add_number = bfd_getb32 (temp);
8373                   }
8374                 else if (length > 4
8375                          && ! mips_disable_float_construction
8376                          /* Constants can only be constructed in GPRs and
8377                             copied to FPRs if the GPRs are at least as wide
8378                             as the FPRs.  Force the constant into memory if
8379                             we are using 64-bit FPRs but the GPRs are only
8380                             32 bits wide.  */
8381                          && (using_gprs
8382                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8383                          && ((temp[0] == 0 && temp[1] == 0)
8384                              || (temp[2] == 0 && temp[3] == 0))
8385                          && ((temp[4] == 0 && temp[5] == 0)
8386                              || (temp[6] == 0 && temp[7] == 0)))
8387                   {
8388                     /* The value is simple enough to load with a couple of
8389                        instructions.  If using 32-bit registers, set
8390                        imm_expr to the high order 32 bits and offset_expr to
8391                        the low order 32 bits.  Otherwise, set imm_expr to
8392                        the entire 64 bit constant.  */
8393                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8394                       {
8395                         imm_expr.X_op = O_constant;
8396                         offset_expr.X_op = O_constant;
8397                         if (! target_big_endian)
8398                           {
8399                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8400                             offset_expr.X_add_number = bfd_getl32 (temp);
8401                           }
8402                         else
8403                           {
8404                             imm_expr.X_add_number = bfd_getb32 (temp);
8405                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8406                           }
8407                         if (offset_expr.X_add_number == 0)
8408                           offset_expr.X_op = O_absent;
8409                       }
8410                     else if (sizeof (imm_expr.X_add_number) > 4)
8411                       {
8412                         imm_expr.X_op = O_constant;
8413                         if (! target_big_endian)
8414                           imm_expr.X_add_number = bfd_getl64 (temp);
8415                         else
8416                           imm_expr.X_add_number = bfd_getb64 (temp);
8417                       }
8418                     else
8419                       {
8420                         imm_expr.X_op = O_big;
8421                         imm_expr.X_add_number = 4;
8422                         if (! target_big_endian)
8423                           {
8424                             generic_bignum[0] = bfd_getl16 (temp);
8425                             generic_bignum[1] = bfd_getl16 (temp + 2);
8426                             generic_bignum[2] = bfd_getl16 (temp + 4);
8427                             generic_bignum[3] = bfd_getl16 (temp + 6);
8428                           }
8429                         else
8430                           {
8431                             generic_bignum[0] = bfd_getb16 (temp + 6);
8432                             generic_bignum[1] = bfd_getb16 (temp + 4);
8433                             generic_bignum[2] = bfd_getb16 (temp + 2);
8434                             generic_bignum[3] = bfd_getb16 (temp);
8435                           }
8436                       }
8437                   }
8438                 else
8439                   {
8440                     const char *newname;
8441                     segT new_seg;
8442
8443                     /* Switch to the right section.  */
8444                     seg = now_seg;
8445                     subseg = now_subseg;
8446                     switch (*args)
8447                       {
8448                       default: /* unused default case avoids warnings.  */
8449                       case 'L':
8450                         newname = RDATA_SECTION_NAME;
8451                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8452                             || mips_pic == EMBEDDED_PIC)
8453                           newname = ".lit8";
8454                         break;
8455                       case 'F':
8456                         if (mips_pic == EMBEDDED_PIC)
8457                           newname = ".lit8";
8458                         else
8459                           newname = RDATA_SECTION_NAME;
8460                         break;
8461                       case 'l':
8462                         assert (!USE_GLOBAL_POINTER_OPT
8463                                 || g_switch_value >= 4);
8464                         newname = ".lit4";
8465                         break;
8466                       }
8467                     new_seg = subseg_new (newname, (subsegT) 0);
8468                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8469                       bfd_set_section_flags (stdoutput, new_seg,
8470                                              (SEC_ALLOC
8471                                               | SEC_LOAD
8472                                               | SEC_READONLY
8473                                               | SEC_DATA));
8474                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8475                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8476                         && strcmp (TARGET_OS, "elf") != 0)
8477                       record_alignment (new_seg, 4);
8478                     else
8479                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8480                     if (seg == now_seg)
8481                       as_bad (_("Can't use floating point insn in this section"));
8482
8483                     /* Set the argument to the current address in the
8484                        section.  */
8485                     offset_expr.X_op = O_symbol;
8486                     offset_expr.X_add_symbol =
8487                       symbol_new ("L0\001", now_seg,
8488                                   (valueT) frag_now_fix (), frag_now);
8489                     offset_expr.X_add_number = 0;
8490
8491                     /* Put the floating point number into the section.  */
8492                     p = frag_more ((int) length);
8493                     memcpy (p, temp, length);
8494
8495                     /* Switch back to the original section.  */
8496                     subseg_set (seg, subseg);
8497                   }
8498               }
8499               continue;
8500
8501             case 'i':           /* 16 bit unsigned immediate */
8502             case 'j':           /* 16 bit signed immediate */
8503               *imm_reloc = BFD_RELOC_LO16;
8504               c = my_getSmallExpression (&imm_expr, s);
8505               if (c != S_EX_NONE)
8506                 {
8507                   if (c != S_EX_LO)
8508                     {
8509                       if (imm_expr.X_op == O_constant)
8510                         imm_expr.X_add_number =
8511                           (imm_expr.X_add_number >> 16) & 0xffff;
8512 #ifdef OBJ_ELF
8513                       else if (c == S_EX_HIGHEST)
8514                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8515                       else if (c == S_EX_HIGHER)
8516                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8517                       else if (c == S_EX_GP_REL)
8518                         {
8519                           /* This occurs in NewABI only.  */
8520                           c = my_getSmallExpression (&imm_expr, s);
8521                           if (c != S_EX_NEG)
8522                             as_bad (_("bad composition of relocations"));
8523                           else
8524                             {
8525                               c = my_getSmallExpression (&imm_expr, s);
8526                               if (c != S_EX_LO)
8527                                 as_bad (_("bad composition of relocations"));
8528                               else
8529                                 {
8530                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8531                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8532                                   imm_reloc[2] = BFD_RELOC_LO16;
8533                                 }
8534                             }
8535                         }
8536 #endif
8537                       else if (c == S_EX_HI)
8538                         {
8539                           *imm_reloc = BFD_RELOC_HI16_S;
8540                           imm_unmatched_hi = true;
8541                         }
8542                       else
8543                         *imm_reloc = BFD_RELOC_HI16;
8544                     }
8545                   else if (imm_expr.X_op == O_constant)
8546                     imm_expr.X_add_number &= 0xffff;
8547                 }
8548               if (*args == 'i')
8549                 {
8550                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8551                       || ((imm_expr.X_add_number < 0
8552                            || imm_expr.X_add_number >= 0x10000)
8553                           && imm_expr.X_op == O_constant))
8554                     {
8555                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8556                           !strcmp (insn->name, insn[1].name))
8557                         break;
8558                       if (imm_expr.X_op == O_constant
8559                           || imm_expr.X_op == O_big)
8560                         as_bad (_("16 bit expression not in range 0..65535"));
8561                     }
8562                 }
8563               else
8564                 {
8565                   int more;
8566                   offsetT max;
8567
8568                   /* The upper bound should be 0x8000, but
8569                      unfortunately the MIPS assembler accepts numbers
8570                      from 0x8000 to 0xffff and sign extends them, and
8571                      we want to be compatible.  We only permit this
8572                      extended range for an instruction which does not
8573                      provide any further alternates, since those
8574                      alternates may handle other cases.  People should
8575                      use the numbers they mean, rather than relying on
8576                      a mysterious sign extension.  */
8577                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8578                           strcmp (insn->name, insn[1].name) == 0);
8579                   if (more)
8580                     max = 0x8000;
8581                   else
8582                     max = 0x10000;
8583                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8584                       || ((imm_expr.X_add_number < -0x8000
8585                            || imm_expr.X_add_number >= max)
8586                           && imm_expr.X_op == O_constant)
8587                       || (more
8588                           && imm_expr.X_add_number < 0
8589                           && HAVE_64BIT_GPRS
8590                           && imm_expr.X_unsigned
8591                           && sizeof (imm_expr.X_add_number) <= 4))
8592                     {
8593                       if (more)
8594                         break;
8595                       if (imm_expr.X_op == O_constant
8596                           || imm_expr.X_op == O_big)
8597                         as_bad (_("16 bit expression not in range -32768..32767"));
8598                     }
8599                 }
8600               s = expr_end;
8601               continue;
8602
8603             case 'o':           /* 16 bit offset */
8604               c = my_getSmallExpression (&offset_expr, s);
8605
8606               /* If this value won't fit into a 16 bit offset, then go
8607                  find a macro that will generate the 32 bit offset
8608                  code pattern.  */
8609               if (c == S_EX_NONE
8610                   && (offset_expr.X_op != O_constant
8611                       || offset_expr.X_add_number >= 0x8000
8612                       || offset_expr.X_add_number < -0x8000))
8613                 break;
8614
8615               if (c == S_EX_HI)
8616                 {
8617                   if (offset_expr.X_op != O_constant)
8618                     break;
8619                   offset_expr.X_add_number =
8620                     (offset_expr.X_add_number >> 16) & 0xffff;
8621                 }
8622               *offset_reloc = BFD_RELOC_LO16;
8623               s = expr_end;
8624               continue;
8625
8626             case 'p':           /* pc relative offset */
8627               if (mips_pic == EMBEDDED_PIC)
8628                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8629               else
8630                 *offset_reloc = BFD_RELOC_16_PCREL;
8631               my_getExpression (&offset_expr, s);
8632               s = expr_end;
8633               continue;
8634
8635             case 'u':           /* upper 16 bits */
8636               c = my_getSmallExpression (&imm_expr, s);
8637               *imm_reloc = BFD_RELOC_LO16;
8638               if (c != S_EX_NONE)
8639                 {
8640                   if (c != S_EX_LO)
8641                     {
8642                       if (imm_expr.X_op == O_constant)
8643                         imm_expr.X_add_number =
8644                           (imm_expr.X_add_number >> 16) & 0xffff;
8645                       else if (c == S_EX_HI)
8646                         {
8647                           *imm_reloc = BFD_RELOC_HI16_S;
8648                           imm_unmatched_hi = true;
8649                         }
8650 #ifdef OBJ_ELF
8651                       else if (c == S_EX_HIGHEST)
8652                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8653                       else if (c == S_EX_GP_REL)
8654                         {
8655                           /* This occurs in NewABI only.  */
8656                           c = my_getSmallExpression (&imm_expr, s);
8657                           if (c != S_EX_NEG)
8658                             as_bad (_("bad composition of relocations"));
8659                           else
8660                             {
8661                               c = my_getSmallExpression (&imm_expr, s);
8662                               if (c != S_EX_HI)
8663                                 as_bad (_("bad composition of relocations"));
8664                               else
8665                                 {
8666                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8667                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8668                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8669                                 }
8670                             }
8671                         }
8672 #endif
8673                       else
8674                         *imm_reloc = BFD_RELOC_HI16;
8675                     }
8676                   else if (imm_expr.X_op == O_constant)
8677                     imm_expr.X_add_number &= 0xffff;
8678                 }
8679               if (imm_expr.X_op == O_constant
8680                   && (imm_expr.X_add_number < 0
8681                       || imm_expr.X_add_number >= 0x10000))
8682                 as_bad (_("lui expression not in range 0..65535"));
8683               s = expr_end;
8684               continue;
8685
8686             case 'a':           /* 26 bit address */
8687               my_getExpression (&offset_expr, s);
8688               s = expr_end;
8689               *offset_reloc = BFD_RELOC_MIPS_JMP;
8690               continue;
8691
8692             case 'N':           /* 3 bit branch condition code */
8693             case 'M':           /* 3 bit compare condition code */
8694               if (strncmp (s, "$fcc", 4) != 0)
8695                 break;
8696               s += 4;
8697               regno = 0;
8698               do
8699                 {
8700                   regno *= 10;
8701                   regno += *s - '0';
8702                   ++s;
8703                 }
8704               while (ISDIGIT (*s));
8705               if (regno > 7)
8706                 as_bad (_("invalid condition code register $fcc%d"), regno);
8707               if (*args == 'N')
8708                 ip->insn_opcode |= regno << OP_SH_BCC;
8709               else
8710                 ip->insn_opcode |= regno << OP_SH_CCC;
8711               continue;
8712
8713             case 'H':
8714               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8715                 s += 2;
8716               if (ISDIGIT (*s))
8717                 {
8718                   c = 0;
8719                   do
8720                     {
8721                       c *= 10;
8722                       c += *s - '0';
8723                       ++s;
8724                     }
8725                   while (ISDIGIT (*s));
8726                 }
8727               else
8728                 c = 8; /* Invalid sel value.  */
8729
8730               if (c > 7)
8731                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8732               ip->insn_opcode |= c;
8733               continue;
8734
8735             default:
8736               as_bad (_("bad char = '%c'\n"), *args);
8737               internalError ();
8738             }
8739           break;
8740         }
8741       /* Args don't match.  */
8742       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8743           !strcmp (insn->name, insn[1].name))
8744         {
8745           ++insn;
8746           s = argsStart;
8747           insn_error = _("illegal operands");
8748           continue;
8749         }
8750       if (save_c)
8751         *(--s) = save_c;
8752       insn_error = _("illegal operands");
8753       return;
8754     }
8755 }
8756
8757 /* This routine assembles an instruction into its binary format when
8758    assembling for the mips16.  As a side effect, it sets one of the
8759    global variables imm_reloc or offset_reloc to the type of
8760    relocation to do if one of the operands is an address expression.
8761    It also sets mips16_small and mips16_ext if the user explicitly
8762    requested a small or extended instruction.  */
8763
8764 static void
8765 mips16_ip (str, ip)
8766      char *str;
8767      struct mips_cl_insn *ip;
8768 {
8769   char *s;
8770   const char *args;
8771   struct mips_opcode *insn;
8772   char *argsstart;
8773   unsigned int regno;
8774   unsigned int lastregno = 0;
8775   char *s_reset;
8776
8777   insn_error = NULL;
8778
8779   mips16_small = false;
8780   mips16_ext = false;
8781
8782   for (s = str; ISLOWER (*s); ++s)
8783     ;
8784   switch (*s)
8785     {
8786     case '\0':
8787       break;
8788
8789     case ' ':
8790       *s++ = '\0';
8791       break;
8792
8793     case '.':
8794       if (s[1] == 't' && s[2] == ' ')
8795         {
8796           *s = '\0';
8797           mips16_small = true;
8798           s += 3;
8799           break;
8800         }
8801       else if (s[1] == 'e' && s[2] == ' ')
8802         {
8803           *s = '\0';
8804           mips16_ext = true;
8805           s += 3;
8806           break;
8807         }
8808       /* Fall through.  */
8809     default:
8810       insn_error = _("unknown opcode");
8811       return;
8812     }
8813
8814   if (mips_opts.noautoextend && ! mips16_ext)
8815     mips16_small = true;
8816
8817   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8818     {
8819       insn_error = _("unrecognized opcode");
8820       return;
8821     }
8822
8823   argsstart = s;
8824   for (;;)
8825     {
8826       assert (strcmp (insn->name, str) == 0);
8827
8828       ip->insn_mo = insn;
8829       ip->insn_opcode = insn->match;
8830       ip->use_extend = false;
8831       imm_expr.X_op = O_absent;
8832       imm_reloc[0] = BFD_RELOC_UNUSED;
8833       imm_reloc[1] = BFD_RELOC_UNUSED;
8834       imm_reloc[2] = BFD_RELOC_UNUSED;
8835       offset_expr.X_op = O_absent;
8836       offset_reloc[0] = BFD_RELOC_UNUSED;
8837       offset_reloc[1] = BFD_RELOC_UNUSED;
8838       offset_reloc[2] = BFD_RELOC_UNUSED;
8839       for (args = insn->args; 1; ++args)
8840         {
8841           int c;
8842
8843           if (*s == ' ')
8844             ++s;
8845
8846           /* In this switch statement we call break if we did not find
8847              a match, continue if we did find a match, or return if we
8848              are done.  */
8849
8850           c = *args;
8851           switch (c)
8852             {
8853             case '\0':
8854               if (*s == '\0')
8855                 {
8856                   /* Stuff the immediate value in now, if we can.  */
8857                   if (imm_expr.X_op == O_constant
8858                       && *imm_reloc > BFD_RELOC_UNUSED
8859                       && insn->pinfo != INSN_MACRO)
8860                     {
8861                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8862                                     imm_expr.X_add_number, true, mips16_small,
8863                                     mips16_ext, &ip->insn_opcode,
8864                                     &ip->use_extend, &ip->extend);
8865                       imm_expr.X_op = O_absent;
8866                       *imm_reloc = BFD_RELOC_UNUSED;
8867                     }
8868
8869                   return;
8870                 }
8871               break;
8872
8873             case ',':
8874               if (*s++ == c)
8875                 continue;
8876               s--;
8877               switch (*++args)
8878                 {
8879                 case 'v':
8880                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8881                   continue;
8882                 case 'w':
8883                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8884                   continue;
8885                 }
8886               break;
8887
8888             case '(':
8889             case ')':
8890               if (*s++ == c)
8891                 continue;
8892               break;
8893
8894             case 'v':
8895             case 'w':
8896               if (s[0] != '$')
8897                 {
8898                   if (c == 'v')
8899                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8900                   else
8901                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8902                   ++args;
8903                   continue;
8904                 }
8905               /* Fall through.  */
8906             case 'x':
8907             case 'y':
8908             case 'z':
8909             case 'Z':
8910             case '0':
8911             case 'S':
8912             case 'R':
8913             case 'X':
8914             case 'Y':
8915               if (s[0] != '$')
8916                 break;
8917               s_reset = s;
8918               if (ISDIGIT (s[1]))
8919                 {
8920                   ++s;
8921                   regno = 0;
8922                   do
8923                     {
8924                       regno *= 10;
8925                       regno += *s - '0';
8926                       ++s;
8927                     }
8928                   while (ISDIGIT (*s));
8929                   if (regno > 31)
8930                     {
8931                       as_bad (_("invalid register number (%d)"), regno);
8932                       regno = 2;
8933                     }
8934                 }
8935               else
8936                 {
8937                   if (s[1] == 'r' && s[2] == 'a')
8938                     {
8939                       s += 3;
8940                       regno = RA;
8941                     }
8942                   else if (s[1] == 'f' && s[2] == 'p')
8943                     {
8944                       s += 3;
8945                       regno = FP;
8946                     }
8947                   else if (s[1] == 's' && s[2] == 'p')
8948                     {
8949                       s += 3;
8950                       regno = SP;
8951                     }
8952                   else if (s[1] == 'g' && s[2] == 'p')
8953                     {
8954                       s += 3;
8955                       regno = GP;
8956                     }
8957                   else if (s[1] == 'a' && s[2] == 't')
8958                     {
8959                       s += 3;
8960                       regno = AT;
8961                     }
8962                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8963                     {
8964                       s += 4;
8965                       regno = KT0;
8966                     }
8967                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8968                     {
8969                       s += 4;
8970                       regno = KT1;
8971                     }
8972                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8973                     {
8974                       s += 5;
8975                       regno = ZERO;
8976                     }
8977                   else
8978                     break;
8979                 }
8980
8981               if (*s == ' ')
8982                 ++s;
8983               if (args[1] != *s)
8984                 {
8985                   if (c == 'v' || c == 'w')
8986                     {
8987                       regno = mips16_to_32_reg_map[lastregno];
8988                       s = s_reset;
8989                       ++args;
8990                     }
8991                 }
8992
8993               switch (c)
8994                 {
8995                 case 'x':
8996                 case 'y':
8997                 case 'z':
8998                 case 'v':
8999                 case 'w':
9000                 case 'Z':
9001                   regno = mips32_to_16_reg_map[regno];
9002                   break;
9003
9004                 case '0':
9005                   if (regno != 0)
9006                     regno = ILLEGAL_REG;
9007                   break;
9008
9009                 case 'S':
9010                   if (regno != SP)
9011                     regno = ILLEGAL_REG;
9012                   break;
9013
9014                 case 'R':
9015                   if (regno != RA)
9016                     regno = ILLEGAL_REG;
9017                   break;
9018
9019                 case 'X':
9020                 case 'Y':
9021                   if (regno == AT && ! mips_opts.noat)
9022                     as_warn (_("used $at without \".set noat\""));
9023                   break;
9024
9025                 default:
9026                   internalError ();
9027                 }
9028
9029               if (regno == ILLEGAL_REG)
9030                 break;
9031
9032               switch (c)
9033                 {
9034                 case 'x':
9035                 case 'v':
9036                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9037                   break;
9038                 case 'y':
9039                 case 'w':
9040                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9041                   break;
9042                 case 'z':
9043                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9044                   break;
9045                 case 'Z':
9046                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9047                 case '0':
9048                 case 'S':
9049                 case 'R':
9050                   break;
9051                 case 'X':
9052                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9053                   break;
9054                 case 'Y':
9055                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9056                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9057                   break;
9058                 default:
9059                   internalError ();
9060                 }
9061
9062               lastregno = regno;
9063               continue;
9064
9065             case 'P':
9066               if (strncmp (s, "$pc", 3) == 0)
9067                 {
9068                   s += 3;
9069                   continue;
9070                 }
9071               break;
9072
9073             case '<':
9074             case '>':
9075             case '[':
9076             case ']':
9077             case '4':
9078             case '5':
9079             case 'H':
9080             case 'W':
9081             case 'D':
9082             case 'j':
9083             case '8':
9084             case 'V':
9085             case 'C':
9086             case 'U':
9087             case 'k':
9088             case 'K':
9089               if (s[0] == '%'
9090                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9091                 {
9092                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9093                      and generate the appropriate reloc.  If the text
9094                      inside %gprel is not a symbol name with an
9095                      optional offset, then we generate a normal reloc
9096                      and will probably fail later.  */
9097                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9098                   if (imm_expr.X_op == O_symbol)
9099                     {
9100                       mips16_ext = true;
9101                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9102                       s = expr_end;
9103                       ip->use_extend = true;
9104                       ip->extend = 0;
9105                       continue;
9106                     }
9107                 }
9108               else
9109                 {
9110                   /* Just pick up a normal expression.  */
9111                   my_getExpression (&imm_expr, s);
9112                 }
9113
9114               if (imm_expr.X_op == O_register)
9115                 {
9116                   /* What we thought was an expression turned out to
9117                      be a register.  */
9118
9119                   if (s[0] == '(' && args[1] == '(')
9120                     {
9121                       /* It looks like the expression was omitted
9122                          before a register indirection, which means
9123                          that the expression is implicitly zero.  We
9124                          still set up imm_expr, so that we handle
9125                          explicit extensions correctly.  */
9126                       imm_expr.X_op = O_constant;
9127                       imm_expr.X_add_number = 0;
9128                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9129                       continue;
9130                     }
9131
9132                   break;
9133                 }
9134
9135               /* We need to relax this instruction.  */
9136               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9137               s = expr_end;
9138               continue;
9139
9140             case 'p':
9141             case 'q':
9142             case 'A':
9143             case 'B':
9144             case 'E':
9145               /* We use offset_reloc rather than imm_reloc for the PC
9146                  relative operands.  This lets macros with both
9147                  immediate and address operands work correctly.  */
9148               my_getExpression (&offset_expr, s);
9149
9150               if (offset_expr.X_op == O_register)
9151                 break;
9152
9153               /* We need to relax this instruction.  */
9154               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9155               s = expr_end;
9156               continue;
9157
9158             case '6':           /* break code */
9159               my_getExpression (&imm_expr, s);
9160               check_absolute_expr (ip, &imm_expr);
9161               if ((unsigned long) imm_expr.X_add_number > 63)
9162                 {
9163                   as_warn (_("Invalid value for `%s' (%lu)"),
9164                            ip->insn_mo->name,
9165                            (unsigned long) imm_expr.X_add_number);
9166                   imm_expr.X_add_number &= 0x3f;
9167                 }
9168               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9169               imm_expr.X_op = O_absent;
9170               s = expr_end;
9171               continue;
9172
9173             case 'a':           /* 26 bit address */
9174               my_getExpression (&offset_expr, s);
9175               s = expr_end;
9176               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9177               ip->insn_opcode <<= 16;
9178               continue;
9179
9180             case 'l':           /* register list for entry macro */
9181             case 'L':           /* register list for exit macro */
9182               {
9183                 int mask;
9184
9185                 if (c == 'l')
9186                   mask = 0;
9187                 else
9188                   mask = 7 << 3;
9189                 while (*s != '\0')
9190                   {
9191                     int freg, reg1, reg2;
9192
9193                     while (*s == ' ' || *s == ',')
9194                       ++s;
9195                     if (*s != '$')
9196                       {
9197                         as_bad (_("can't parse register list"));
9198                         break;
9199                       }
9200                     ++s;
9201                     if (*s != 'f')
9202                       freg = 0;
9203                     else
9204                       {
9205                         freg = 1;
9206                         ++s;
9207                       }
9208                     reg1 = 0;
9209                     while (ISDIGIT (*s))
9210                       {
9211                         reg1 *= 10;
9212                         reg1 += *s - '0';
9213                         ++s;
9214                       }
9215                     if (*s == ' ')
9216                       ++s;
9217                     if (*s != '-')
9218                       reg2 = reg1;
9219                     else
9220                       {
9221                         ++s;
9222                         if (*s != '$')
9223                           break;
9224                         ++s;
9225                         if (freg)
9226                           {
9227                             if (*s == 'f')
9228                               ++s;
9229                             else
9230                               {
9231                                 as_bad (_("invalid register list"));
9232                                 break;
9233                               }
9234                           }
9235                         reg2 = 0;
9236                         while (ISDIGIT (*s))
9237                           {
9238                             reg2 *= 10;
9239                             reg2 += *s - '0';
9240                             ++s;
9241                           }
9242                       }
9243                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9244                       {
9245                         mask &= ~ (7 << 3);
9246                         mask |= 5 << 3;
9247                       }
9248                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9249                       {
9250                         mask &= ~ (7 << 3);
9251                         mask |= 6 << 3;
9252                       }
9253                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9254                       mask |= (reg2 - 3) << 3;
9255                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9256                       mask |= (reg2 - 15) << 1;
9257                     else if (reg1 == RA && reg2 == RA)
9258                       mask |= 1;
9259                     else
9260                       {
9261                         as_bad (_("invalid register list"));
9262                         break;
9263                       }
9264                   }
9265                 /* The mask is filled in in the opcode table for the
9266                    benefit of the disassembler.  We remove it before
9267                    applying the actual mask.  */
9268                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9269                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9270               }
9271             continue;
9272
9273             case 'e':           /* extend code */
9274               my_getExpression (&imm_expr, s);
9275               check_absolute_expr (ip, &imm_expr);
9276               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9277                 {
9278                   as_warn (_("Invalid value for `%s' (%lu)"),
9279                            ip->insn_mo->name,
9280                            (unsigned long) imm_expr.X_add_number);
9281                   imm_expr.X_add_number &= 0x7ff;
9282                 }
9283               ip->insn_opcode |= imm_expr.X_add_number;
9284               imm_expr.X_op = O_absent;
9285               s = expr_end;
9286               continue;
9287
9288             default:
9289               internalError ();
9290             }
9291           break;
9292         }
9293
9294       /* Args don't match.  */
9295       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9296           strcmp (insn->name, insn[1].name) == 0)
9297         {
9298           ++insn;
9299           s = argsstart;
9300           continue;
9301         }
9302
9303       insn_error = _("illegal operands");
9304
9305       return;
9306     }
9307 }
9308
9309 /* This structure holds information we know about a mips16 immediate
9310    argument type.  */
9311
9312 struct mips16_immed_operand
9313 {
9314   /* The type code used in the argument string in the opcode table.  */
9315   int type;
9316   /* The number of bits in the short form of the opcode.  */
9317   int nbits;
9318   /* The number of bits in the extended form of the opcode.  */
9319   int extbits;
9320   /* The amount by which the short form is shifted when it is used;
9321      for example, the sw instruction has a shift count of 2.  */
9322   int shift;
9323   /* The amount by which the short form is shifted when it is stored
9324      into the instruction code.  */
9325   int op_shift;
9326   /* Non-zero if the short form is unsigned.  */
9327   int unsp;
9328   /* Non-zero if the extended form is unsigned.  */
9329   int extu;
9330   /* Non-zero if the value is PC relative.  */
9331   int pcrel;
9332 };
9333
9334 /* The mips16 immediate operand types.  */
9335
9336 static const struct mips16_immed_operand mips16_immed_operands[] =
9337 {
9338   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9339   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9340   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9341   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9342   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9343   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9344   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9345   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9346   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9347   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9348   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9349   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9350   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9351   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9352   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9353   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9354   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9355   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9356   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9357   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9358   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9359 };
9360
9361 #define MIPS16_NUM_IMMED \
9362   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9363
9364 /* Handle a mips16 instruction with an immediate value.  This or's the
9365    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9366    whether an extended value is needed; if one is needed, it sets
9367    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9368    If SMALL is true, an unextended opcode was explicitly requested.
9369    If EXT is true, an extended opcode was explicitly requested.  If
9370    WARN is true, warn if EXT does not match reality.  */
9371
9372 static void
9373 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9374               extend)
9375      char *file;
9376      unsigned int line;
9377      int type;
9378      offsetT val;
9379      boolean warn;
9380      boolean small;
9381      boolean ext;
9382      unsigned long *insn;
9383      boolean *use_extend;
9384      unsigned short *extend;
9385 {
9386   register const struct mips16_immed_operand *op;
9387   int mintiny, maxtiny;
9388   boolean needext;
9389
9390   op = mips16_immed_operands;
9391   while (op->type != type)
9392     {
9393       ++op;
9394       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9395     }
9396
9397   if (op->unsp)
9398     {
9399       if (type == '<' || type == '>' || type == '[' || type == ']')
9400         {
9401           mintiny = 1;
9402           maxtiny = 1 << op->nbits;
9403         }
9404       else
9405         {
9406           mintiny = 0;
9407           maxtiny = (1 << op->nbits) - 1;
9408         }
9409     }
9410   else
9411     {
9412       mintiny = - (1 << (op->nbits - 1));
9413       maxtiny = (1 << (op->nbits - 1)) - 1;
9414     }
9415
9416   /* Branch offsets have an implicit 0 in the lowest bit.  */
9417   if (type == 'p' || type == 'q')
9418     val /= 2;
9419
9420   if ((val & ((1 << op->shift) - 1)) != 0
9421       || val < (mintiny << op->shift)
9422       || val > (maxtiny << op->shift))
9423     needext = true;
9424   else
9425     needext = false;
9426
9427   if (warn && ext && ! needext)
9428     as_warn_where (file, line,
9429                    _("extended operand requested but not required"));
9430   if (small && needext)
9431     as_bad_where (file, line, _("invalid unextended operand value"));
9432
9433   if (small || (! ext && ! needext))
9434     {
9435       int insnval;
9436
9437       *use_extend = false;
9438       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9439       insnval <<= op->op_shift;
9440       *insn |= insnval;
9441     }
9442   else
9443     {
9444       long minext, maxext;
9445       int extval;
9446
9447       if (op->extu)
9448         {
9449           minext = 0;
9450           maxext = (1 << op->extbits) - 1;
9451         }
9452       else
9453         {
9454           minext = - (1 << (op->extbits - 1));
9455           maxext = (1 << (op->extbits - 1)) - 1;
9456         }
9457       if (val < minext || val > maxext)
9458         as_bad_where (file, line,
9459                       _("operand value out of range for instruction"));
9460
9461       *use_extend = true;
9462       if (op->extbits == 16)
9463         {
9464           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9465           val &= 0x1f;
9466         }
9467       else if (op->extbits == 15)
9468         {
9469           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9470           val &= 0xf;
9471         }
9472       else
9473         {
9474           extval = ((val & 0x1f) << 6) | (val & 0x20);
9475           val = 0;
9476         }
9477
9478       *extend = (unsigned short) extval;
9479       *insn |= val;
9480     }
9481 }
9482 \f
9483 static struct percent_op_match
9484 {
9485    const char *str;
9486    const enum small_ex_type type;
9487 } percent_op[] =
9488 {
9489   {"%lo", S_EX_LO},
9490 #ifdef OBJ_ELF
9491   {"%call_hi", S_EX_CALL_HI},
9492   {"%call_lo", S_EX_CALL_LO},
9493   {"%call16", S_EX_CALL16},
9494   {"%got_disp", S_EX_GOT_DISP},
9495   {"%got_page", S_EX_GOT_PAGE},
9496   {"%got_ofst", S_EX_GOT_OFST},
9497   {"%got_hi", S_EX_GOT_HI},
9498   {"%got_lo", S_EX_GOT_LO},
9499   {"%got", S_EX_GOT},
9500   {"%gp_rel", S_EX_GP_REL},
9501   {"%half", S_EX_HALF},
9502   {"%highest", S_EX_HIGHEST},
9503   {"%higher", S_EX_HIGHER},
9504   {"%neg", S_EX_NEG},
9505 #endif
9506   {"%hi", S_EX_HI}
9507 };
9508
9509 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9510    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9511    can be nested, this is handled by blanking the innermost, parsing the
9512    rest by subsequent calls.  */
9513
9514 static int
9515 my_getSmallParser (str, len, nestlevel)
9516      char **str;
9517      unsigned int *len;
9518      int *nestlevel;
9519 {
9520   *len = 0;
9521   *str += strspn (*str, " \t");
9522   /* Check for expression in parentheses.  */
9523   if (**str == '(')
9524     {
9525       char *b = *str + 1 + strspn (*str + 1, " \t");
9526       char *e;
9527
9528       /* Check for base register.  */
9529       if (b[0] == '$')
9530         {
9531           if (strchr (b, ')')
9532               && (e = b + strcspn (b, ") \t"))
9533               && e - b > 1 && e - b < 4)
9534             {
9535               if ((e - b == 3
9536                    && ((b[1] == 'f' && b[2] == 'p')
9537                        || (b[1] == 's' && b[2] == 'p')
9538                        || (b[1] == 'g' && b[2] == 'p')
9539                        || (b[1] == 'a' && b[2] == 't')
9540                        || (ISDIGIT (b[1])
9541                            && ISDIGIT (b[2]))))
9542                   || (ISDIGIT (b[1])))
9543                 {
9544                   *len = strcspn (*str, ")") + 1;
9545                   return S_EX_REGISTER;
9546                 }
9547             }
9548         }
9549       /* Check for percent_op (in parentheses).  */
9550       else if (b[0] == '%')
9551         {
9552           *str = b;
9553           return my_getPercentOp (str, len, nestlevel);
9554         }
9555
9556       /* Some other expression in the parentheses, which can contain
9557          parentheses itself. Attempt to find the matching one.  */
9558       {
9559         int pcnt = 1;
9560         char *s;
9561
9562         *len = 1;
9563         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9564           {
9565             if (*s == '(')
9566               ++pcnt;
9567             else if (*s == ')')
9568               --pcnt;
9569           }
9570       }
9571     }
9572   /* Check for percent_op (outside of parentheses).  */
9573   else if (*str[0] == '%')
9574     return my_getPercentOp (str, len, nestlevel);
9575
9576   /* Any other expression.  */
9577   return S_EX_NONE;
9578 }
9579
9580 static int
9581 my_getPercentOp (str, len, nestlevel)
9582      char **str;
9583      unsigned int *len;
9584      int *nestlevel;
9585 {
9586   char *tmp = *str + 1;
9587   unsigned int i = 0;
9588
9589   while (ISALPHA (*tmp) || *tmp == '_')
9590     {
9591       *tmp = TOLOWER (*tmp);
9592       tmp++;
9593     }
9594   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9595     {
9596       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9597         i++;
9598       else
9599         {
9600           int type = percent_op[i].type;
9601
9602           /* Only %hi and %lo are allowed for OldABI.  */
9603           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9604             return S_EX_NONE;
9605
9606           *len = strlen (percent_op[i].str);
9607           ++(*nestlevel);
9608           return type;
9609         }
9610     }
9611   return S_EX_NONE;
9612 }
9613
9614 static int
9615 my_getSmallExpression (ep, str)
9616      expressionS *ep;
9617      char *str;
9618 {
9619   static char *oldstr = NULL;
9620   int c = S_EX_NONE;
9621   int oldc;
9622   int nestlevel = -1;
9623   unsigned int len;
9624
9625   /* Don't update oldstr if the last call had nested percent_op's. We need
9626      it to parse the outer ones later.  */
9627   if (! oldstr)
9628     oldstr = str;
9629
9630   do
9631     {
9632       oldc = c;
9633       c = my_getSmallParser (&str, &len, &nestlevel);
9634       if (c != S_EX_NONE && c != S_EX_REGISTER)
9635         str += len;
9636     }
9637   while (c != S_EX_NONE && c != S_EX_REGISTER);
9638
9639   if (nestlevel >= 0)
9640     {
9641       /* A percent_op was encountered.  Don't try to get an expression if
9642          it is already blanked out.  */
9643       if (*(str + strspn (str + 1, " )")) != ')')
9644         {
9645           char save;
9646
9647           /* Let my_getExpression() stop at the closing parenthesis.  */
9648           save = *(str + len);
9649           *(str + len) = '\0';
9650           my_getExpression (ep, str);
9651           *(str + len) = save;
9652         }
9653       if (nestlevel > 0)
9654         {
9655           /* Blank out including the % sign and the proper matching
9656              parenthesis.  */
9657           int pcnt = 1;
9658           char *s = strrchr (oldstr, '%');
9659           char *end;
9660
9661           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9662             {
9663               if (*end == '(')
9664                 ++pcnt;
9665               else if (*end == ')')
9666                 --pcnt;
9667             }
9668
9669           memset (s, ' ', end - s);
9670           str = oldstr;
9671         }
9672       else
9673         expr_end = str + len;
9674
9675       c = oldc;
9676     }
9677   else if (c == S_EX_NONE)
9678     {
9679       my_getExpression (ep, str);
9680     }
9681   else if (c == S_EX_REGISTER)
9682     {
9683       ep->X_op = O_constant;
9684       expr_end = str;
9685       ep->X_add_symbol = NULL;
9686       ep->X_op_symbol = NULL;
9687       ep->X_add_number = 0;
9688     }
9689   else
9690     {
9691       as_fatal (_("internal error"));
9692     }
9693
9694   if (nestlevel <= 0)
9695     /* All percent_op's have been handled.  */
9696     oldstr = NULL;
9697
9698   return c;
9699 }
9700
9701 static void
9702 my_getExpression (ep, str)
9703      expressionS *ep;
9704      char *str;
9705 {
9706   char *save_in;
9707   valueT val;
9708
9709   save_in = input_line_pointer;
9710   input_line_pointer = str;
9711   expression (ep);
9712   expr_end = input_line_pointer;
9713   input_line_pointer = save_in;
9714
9715   /* If we are in mips16 mode, and this is an expression based on `.',
9716      then we bump the value of the symbol by 1 since that is how other
9717      text symbols are handled.  We don't bother to handle complex
9718      expressions, just `.' plus or minus a constant.  */
9719   if (mips_opts.mips16
9720       && ep->X_op == O_symbol
9721       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9722       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9723       && symbol_get_frag (ep->X_add_symbol) == frag_now
9724       && symbol_constant_p (ep->X_add_symbol)
9725       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9726     S_SET_VALUE (ep->X_add_symbol, val + 1);
9727 }
9728
9729 /* Turn a string in input_line_pointer into a floating point constant
9730    of type TYPE, and store the appropriate bytes in *LITP.  The number
9731    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9732    returned, or NULL on OK.  */
9733
9734 char *
9735 md_atof (type, litP, sizeP)
9736      int type;
9737      char *litP;
9738      int *sizeP;
9739 {
9740   int prec;
9741   LITTLENUM_TYPE words[4];
9742   char *t;
9743   int i;
9744
9745   switch (type)
9746     {
9747     case 'f':
9748       prec = 2;
9749       break;
9750
9751     case 'd':
9752       prec = 4;
9753       break;
9754
9755     default:
9756       *sizeP = 0;
9757       return _("bad call to md_atof");
9758     }
9759
9760   t = atof_ieee (input_line_pointer, type, words);
9761   if (t)
9762     input_line_pointer = t;
9763
9764   *sizeP = prec * 2;
9765
9766   if (! target_big_endian)
9767     {
9768       for (i = prec - 1; i >= 0; i--)
9769         {
9770           md_number_to_chars (litP, (valueT) words[i], 2);
9771           litP += 2;
9772         }
9773     }
9774   else
9775     {
9776       for (i = 0; i < prec; i++)
9777         {
9778           md_number_to_chars (litP, (valueT) words[i], 2);
9779           litP += 2;
9780         }
9781     }
9782
9783   return NULL;
9784 }
9785
9786 void
9787 md_number_to_chars (buf, val, n)
9788      char *buf;
9789      valueT val;
9790      int n;
9791 {
9792   if (target_big_endian)
9793     number_to_chars_bigendian (buf, val, n);
9794   else
9795     number_to_chars_littleendian (buf, val, n);
9796 }
9797 \f
9798 #ifdef OBJ_ELF
9799 static int support_64bit_objects(void)
9800 {
9801   const char **list, **l;
9802
9803   list = bfd_target_list ();
9804   for (l = list; *l != NULL; l++)
9805 #ifdef TE_TMIPS
9806     /* This is traditional mips */
9807     if (strcmp (*l, "elf64-tradbigmips") == 0
9808         || strcmp (*l, "elf64-tradlittlemips") == 0)
9809 #else
9810     if (strcmp (*l, "elf64-bigmips") == 0
9811         || strcmp (*l, "elf64-littlemips") == 0)
9812 #endif
9813       break;
9814   free (list);
9815   return (*l != NULL);
9816 }
9817 #endif /* OBJ_ELF */
9818
9819 const char *md_shortopts = "nO::g::G:";
9820
9821 struct option md_longopts[] =
9822 {
9823 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9824   {"mips0", no_argument, NULL, OPTION_MIPS1},
9825   {"mips1", no_argument, NULL, OPTION_MIPS1},
9826 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9827   {"mips2", no_argument, NULL, OPTION_MIPS2},
9828 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9829   {"mips3", no_argument, NULL, OPTION_MIPS3},
9830 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9831   {"mips4", no_argument, NULL, OPTION_MIPS4},
9832 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9833   {"mips5", no_argument, NULL, OPTION_MIPS5},
9834 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9835   {"mips32", no_argument, NULL, OPTION_MIPS32},
9836 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9837   {"mips64", no_argument, NULL, OPTION_MIPS64},
9838 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9839   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9840 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9841   {"trap", no_argument, NULL, OPTION_TRAP},
9842   {"no-break", no_argument, NULL, OPTION_TRAP},
9843 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9844   {"break", no_argument, NULL, OPTION_BREAK},
9845   {"no-trap", no_argument, NULL, OPTION_BREAK},
9846 #define OPTION_EB (OPTION_MD_BASE + 11)
9847   {"EB", no_argument, NULL, OPTION_EB},
9848 #define OPTION_EL (OPTION_MD_BASE + 12)
9849   {"EL", no_argument, NULL, OPTION_EL},
9850 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9851   {"mips16", no_argument, NULL, OPTION_MIPS16},
9852 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9853   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9854 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9855   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9856 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9857   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9858   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9859 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9860   {"mfp32", no_argument, NULL, OPTION_FP32},
9861 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9862   {"mgp32", no_argument, NULL, OPTION_GP32},
9863 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9864   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9865 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9866   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9867 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9868   {"march", required_argument, NULL, OPTION_MARCH},
9869 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9870   {"mtune", required_argument, NULL, OPTION_MTUNE},
9871 #define OPTION_FP64 (OPTION_MD_BASE + 23)
9872   {"mfp64", no_argument, NULL, OPTION_FP64},
9873 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9874   {"m4650", no_argument, NULL, OPTION_M4650},
9875 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9876   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9877 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9878   {"m4010", no_argument, NULL, OPTION_M4010},
9879 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9880   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9881 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9882   {"m4100", no_argument, NULL, OPTION_M4100},
9883 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9884   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9885 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9886   {"m3900", no_argument, NULL, OPTION_M3900},
9887 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9888   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9889 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9890   {"mgp64", no_argument, NULL, OPTION_GP64},
9891 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9892   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9893 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9894   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9895 #define OPTION_MDMX (OPTION_MD_BASE + 35)
9896   {"mdmx", no_argument, NULL, OPTION_MDMX},
9897 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9898   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9899 #ifdef OBJ_ELF
9900 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 37)
9901 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9902   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9903   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9904 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
9905   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
9906 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
9907   {"xgot",        no_argument, NULL, OPTION_XGOT},
9908 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
9909   {"mabi", required_argument, NULL, OPTION_MABI},
9910 #define OPTION_32          (OPTION_ELF_BASE + 4)
9911   {"32",          no_argument, NULL, OPTION_32},
9912 #define OPTION_N32         (OPTION_ELF_BASE + 5)
9913   {"n32",         no_argument, NULL, OPTION_N32},
9914 #define OPTION_64          (OPTION_ELF_BASE + 6)
9915   {"64",          no_argument, NULL, OPTION_64},
9916 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
9917   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
9918 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
9919   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
9920 #endif /* OBJ_ELF */
9921   {NULL, no_argument, NULL, 0}
9922 };
9923 size_t md_longopts_size = sizeof (md_longopts);
9924
9925 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
9926    NEW_VALUE.  Warn if another value was already specified.  Note:
9927    we have to defer parsing the -march and -mtune arguments in order
9928    to handle 'from-abi' correctly, since the ABI might be specified
9929    in a later argument.  */
9930
9931 static void
9932 mips_set_option_string (string_ptr, new_value)
9933      const char **string_ptr, *new_value;
9934 {
9935   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
9936     as_warn (_("A different %s was already specified, is now %s"),
9937              string_ptr == &mips_arch_string ? "-march" : "-mtune",
9938              new_value);
9939
9940   *string_ptr = new_value;
9941 }
9942
9943 int
9944 md_parse_option (c, arg)
9945      int c;
9946      char *arg;
9947 {
9948   switch (c)
9949     {
9950     case OPTION_CONSTRUCT_FLOATS:
9951       mips_disable_float_construction = 0;
9952       break;
9953
9954     case OPTION_NO_CONSTRUCT_FLOATS:
9955       mips_disable_float_construction = 1;
9956       break;
9957
9958     case OPTION_TRAP:
9959       mips_trap = 1;
9960       break;
9961
9962     case OPTION_BREAK:
9963       mips_trap = 0;
9964       break;
9965
9966     case OPTION_EB:
9967       target_big_endian = 1;
9968       break;
9969
9970     case OPTION_EL:
9971       target_big_endian = 0;
9972       break;
9973
9974     case 'n':
9975       warn_nops = 1;
9976       break;
9977
9978     case 'O':
9979       if (arg && arg[1] == '0')
9980         mips_optimize = 1;
9981       else
9982         mips_optimize = 2;
9983       break;
9984
9985     case 'g':
9986       if (arg == NULL)
9987         mips_debug = 2;
9988       else
9989         mips_debug = atoi (arg);
9990       /* When the MIPS assembler sees -g or -g2, it does not do
9991          optimizations which limit full symbolic debugging.  We take
9992          that to be equivalent to -O0.  */
9993       if (mips_debug == 2)
9994         mips_optimize = 1;
9995       break;
9996
9997     case OPTION_MIPS1:
9998       file_mips_isa = ISA_MIPS1;
9999       break;
10000
10001     case OPTION_MIPS2:
10002       file_mips_isa = ISA_MIPS2;
10003       break;
10004
10005     case OPTION_MIPS3:
10006       file_mips_isa = ISA_MIPS3;
10007       break;
10008
10009     case OPTION_MIPS4:
10010       file_mips_isa = ISA_MIPS4;
10011       break;
10012
10013     case OPTION_MIPS5:
10014       file_mips_isa = ISA_MIPS5;
10015       break;
10016
10017     case OPTION_MIPS32:
10018       file_mips_isa = ISA_MIPS32;
10019       break;
10020
10021     case OPTION_MIPS64:
10022       file_mips_isa = ISA_MIPS64;
10023       break;
10024
10025     case OPTION_MTUNE:
10026       mips_set_option_string (&mips_tune_string, arg);
10027       break;
10028
10029     case OPTION_MARCH:
10030       mips_set_option_string (&mips_arch_string, arg);
10031       break;
10032
10033     case OPTION_M4650:
10034       mips_set_option_string (&mips_arch_string, "4650");
10035       mips_set_option_string (&mips_tune_string, "4650");
10036       break;
10037
10038     case OPTION_NO_M4650:
10039       break;
10040
10041     case OPTION_M4010:
10042       mips_set_option_string (&mips_arch_string, "4010");
10043       mips_set_option_string (&mips_tune_string, "4010");
10044       break;
10045
10046     case OPTION_NO_M4010:
10047       break;
10048
10049     case OPTION_M4100:
10050       mips_set_option_string (&mips_arch_string, "4100");
10051       mips_set_option_string (&mips_tune_string, "4100");
10052       break;
10053
10054     case OPTION_NO_M4100:
10055       break;
10056
10057     case OPTION_M3900:
10058       mips_set_option_string (&mips_arch_string, "3900");
10059       mips_set_option_string (&mips_tune_string, "3900");
10060       break;
10061
10062     case OPTION_NO_M3900:
10063       break;
10064
10065     case OPTION_MDMX:
10066       mips_opts.ase_mdmx = 1;
10067       break;
10068
10069     case OPTION_NO_MDMX:
10070       mips_opts.ase_mdmx = 0;
10071       break;
10072
10073     case OPTION_MIPS16:
10074       mips_opts.mips16 = 1;
10075       mips_no_prev_insn (false);
10076       break;
10077
10078     case OPTION_NO_MIPS16:
10079       mips_opts.mips16 = 0;
10080       mips_no_prev_insn (false);
10081       break;
10082
10083     case OPTION_MIPS3D:
10084       mips_opts.ase_mips3d = 1;
10085       break;
10086
10087     case OPTION_NO_MIPS3D:
10088       mips_opts.ase_mips3d = 0;
10089       break;
10090
10091     case OPTION_MEMBEDDED_PIC:
10092       mips_pic = EMBEDDED_PIC;
10093       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10094         {
10095           as_bad (_("-G may not be used with embedded PIC code"));
10096           return 0;
10097         }
10098       g_switch_value = 0x7fffffff;
10099       break;
10100
10101 #ifdef OBJ_ELF
10102       /* When generating ELF code, we permit -KPIC and -call_shared to
10103          select SVR4_PIC, and -non_shared to select no PIC.  This is
10104          intended to be compatible with Irix 5.  */
10105     case OPTION_CALL_SHARED:
10106       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10107         {
10108           as_bad (_("-call_shared is supported only for ELF format"));
10109           return 0;
10110         }
10111       mips_pic = SVR4_PIC;
10112       if (g_switch_seen && g_switch_value != 0)
10113         {
10114           as_bad (_("-G may not be used with SVR4 PIC code"));
10115           return 0;
10116         }
10117       g_switch_value = 0;
10118       break;
10119
10120     case OPTION_NON_SHARED:
10121       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10122         {
10123           as_bad (_("-non_shared is supported only for ELF format"));
10124           return 0;
10125         }
10126       mips_pic = NO_PIC;
10127       break;
10128
10129       /* The -xgot option tells the assembler to use 32 offsets when
10130          accessing the got in SVR4_PIC mode.  It is for Irix
10131          compatibility.  */
10132     case OPTION_XGOT:
10133       mips_big_got = 1;
10134       break;
10135 #endif /* OBJ_ELF */
10136
10137     case 'G':
10138       if (! USE_GLOBAL_POINTER_OPT)
10139         {
10140           as_bad (_("-G is not supported for this configuration"));
10141           return 0;
10142         }
10143       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10144         {
10145           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10146           return 0;
10147         }
10148       else
10149         g_switch_value = atoi (arg);
10150       g_switch_seen = 1;
10151       break;
10152
10153 #ifdef OBJ_ELF
10154       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10155          and -mabi=64.  */
10156     case OPTION_32:
10157       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10158         {
10159           as_bad (_("-32 is supported for ELF format only"));
10160           return 0;
10161         }
10162       mips_abi = O32_ABI;
10163       break;
10164
10165     case OPTION_N32:
10166       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10167         {
10168           as_bad (_("-n32 is supported for ELF format only"));
10169           return 0;
10170         }
10171       mips_abi = N32_ABI;
10172       break;
10173
10174     case OPTION_64:
10175       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10176         {
10177           as_bad (_("-64 is supported for ELF format only"));
10178           return 0;
10179         }
10180       mips_abi = N64_ABI;
10181       if (! support_64bit_objects())
10182         as_fatal (_("No compiled in support for 64 bit object file format"));
10183       break;
10184 #endif /* OBJ_ELF */
10185
10186     case OPTION_GP32:
10187       file_mips_gp32 = 1;
10188       break;
10189
10190     case OPTION_GP64:
10191       file_mips_gp32 = 0;
10192       break;
10193
10194     case OPTION_FP32:
10195       file_mips_fp32 = 1;
10196       break;
10197
10198     case OPTION_FP64:
10199       file_mips_fp32 = 0;
10200       break;
10201
10202 #ifdef OBJ_ELF
10203     case OPTION_MABI:
10204       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10205         {
10206           as_bad (_("-mabi is supported for ELF format only"));
10207           return 0;
10208         }
10209       if (strcmp (arg, "32") == 0)
10210         mips_abi = O32_ABI;
10211       else if (strcmp (arg, "o64") == 0)
10212         mips_abi = O64_ABI;
10213       else if (strcmp (arg, "n32") == 0)
10214         mips_abi = N32_ABI;
10215       else if (strcmp (arg, "64") == 0)
10216         {
10217           mips_abi = N64_ABI;
10218           if (! support_64bit_objects())
10219             as_fatal (_("No compiled in support for 64 bit object file "
10220                         "format"));
10221         }
10222       else if (strcmp (arg, "eabi") == 0)
10223         mips_abi = EABI_ABI;
10224       else
10225         {
10226           as_fatal (_("invalid abi -mabi=%s"), arg);
10227           return 0;
10228         }
10229       break;
10230 #endif /* OBJ_ELF */
10231
10232     case OPTION_M7000_HILO_FIX:
10233       mips_7000_hilo_fix = true;
10234       break;
10235
10236     case OPTION_MNO_7000_HILO_FIX:
10237       mips_7000_hilo_fix = false;
10238       break;
10239
10240 #ifdef OBJ_ELF
10241     case OPTION_MDEBUG:
10242       mips_flag_mdebug = true;
10243       break;
10244
10245     case OPTION_NO_MDEBUG:
10246       mips_flag_mdebug = false;
10247       break;
10248 #endif /* OBJ_ELF */
10249
10250     default:
10251       return 0;
10252     }
10253
10254   return 1;
10255 }
10256 \f
10257 /* Set up globals to generate code for the ISA or processor
10258    described by INFO.  */
10259
10260 static void
10261 mips_set_architecture (info)
10262      const struct mips_cpu_info *info;
10263 {
10264   if (info != 0)
10265     {
10266       mips_arch_info = info;
10267       mips_arch = info->cpu;
10268       mips_opts.isa = info->isa;
10269     }
10270 }
10271
10272
10273 /* Likewise for tuning.  */
10274
10275 static void
10276 mips_set_tune (info)
10277      const struct mips_cpu_info *info;
10278 {
10279   if (info != 0)
10280     {
10281       mips_tune_info = info;
10282       mips_tune = info->cpu;
10283     }
10284 }
10285
10286
10287 void
10288 mips_after_parse_args ()
10289 {
10290   /* GP relative stuff not working for PE */
10291   if (strncmp (TARGET_OS, "pe", 2) == 0
10292       && g_switch_value != 0)
10293     {
10294       if (g_switch_seen)
10295         as_bad (_("-G not supported in this configuration."));
10296       g_switch_value = 0;
10297     }
10298
10299   /* The following code determines the architecture and register size.
10300      Similar code was added to GCC 3.3 (see override_options() in
10301      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10302      as much as possible.  */
10303
10304   if (mips_arch_string != 0)
10305     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10306
10307   if (mips_tune_string != 0)
10308     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10309
10310   if (file_mips_isa != ISA_UNKNOWN)
10311     {
10312       /* Handle -mipsN.  At this point, file_mips_isa contains the
10313          ISA level specified by -mipsN, while mips_opts.isa contains
10314          the -march selection (if any).  */
10315       if (mips_arch_info != 0)
10316         {
10317           /* -march takes precedence over -mipsN, since it is more descriptive.
10318              There's no harm in specifying both as long as the ISA levels
10319              are the same.  */
10320           if (file_mips_isa != mips_opts.isa)
10321             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10322                     mips_cpu_info_from_isa (file_mips_isa)->name,
10323                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10324         }
10325       else
10326         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10327     }
10328
10329   if (mips_arch_info == 0)
10330     mips_set_architecture (mips_parse_cpu ("default CPU",
10331                                            MIPS_CPU_STRING_DEFAULT));
10332
10333   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10334     as_bad ("-march=%s is not compatible with the selected ABI",
10335             mips_arch_info->name);
10336
10337   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10338   if (mips_tune_info == 0)
10339     mips_set_tune (mips_arch_info);
10340
10341   if (file_mips_gp32 >= 0)
10342     {
10343       /* The user specified the size of the integer registers.  Make sure
10344          it agrees with the ABI and ISA.  */
10345       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10346         as_bad (_("-mgp64 used with a 32-bit processor"));
10347       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10348         as_bad (_("-mgp32 used with a 64-bit ABI"));
10349       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10350         as_bad (_("-mgp64 used with a 32-bit ABI"));
10351     }
10352   else
10353     {
10354       /* Infer the integer register size from the ABI and processor.
10355          Restrict ourselves to 32-bit registers if that's all the
10356          processor has, or if the ABI cannot handle 64-bit registers.  */
10357       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10358                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10359     }
10360
10361   /* ??? GAS treats single-float processors as though they had 64-bit
10362      float registers (although it complains when double-precision
10363      instructions are used).  As things stand, saying they have 32-bit
10364      registers would lead to spurious "register must be even" messages.
10365      So here we assume float registers are always the same size as
10366      integer ones, unless the user says otherwise.  */
10367   if (file_mips_fp32 < 0)
10368     file_mips_fp32 = file_mips_gp32;
10369
10370   /* End of GCC-shared inference code.  */
10371
10372   /* ??? When do we want this flag to be set?   Who uses it?  */
10373   if (file_mips_gp32 == 1
10374       && mips_abi == NO_ABI
10375       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10376     mips_32bitmode = 1;
10377
10378   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10379     as_bad (_("trap exception not supported at ISA 1"));
10380
10381   /* If the selected architecture includes support for ASEs, enable
10382      generation of code for them.  */
10383   if (mips_opts.mips16 == -1)
10384     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10385   if (mips_opts.ase_mips3d == -1)
10386     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10387   if (mips_opts.ase_mdmx == -1)
10388     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10389
10390   file_mips_isa = mips_opts.isa;
10391   file_ase_mips16 = mips_opts.mips16;
10392   file_ase_mips3d = mips_opts.ase_mips3d;
10393   file_ase_mdmx = mips_opts.ase_mdmx;
10394   mips_opts.gp32 = file_mips_gp32;
10395   mips_opts.fp32 = file_mips_fp32;
10396
10397   if (HAVE_NEWABI)
10398     mips_big_got = 1;
10399
10400   if (mips_flag_mdebug < 0)
10401     {
10402 #ifdef OBJ_MAYBE_ECOFF
10403       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10404         mips_flag_mdebug = 1;
10405       else
10406 #endif /* OBJ_MAYBE_ECOFF */
10407         mips_flag_mdebug = 0;
10408     }
10409 }
10410 \f
10411 void
10412 mips_init_after_args ()
10413 {
10414   /* initialize opcodes */
10415   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10416   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10417 }
10418
10419 long
10420 md_pcrel_from (fixP)
10421      fixS *fixP;
10422 {
10423   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10424       && fixP->fx_addsy != (symbolS *) NULL
10425       && ! S_IS_DEFINED (fixP->fx_addsy))
10426     {
10427       /* This makes a branch to an undefined symbol be a branch to the
10428          current location.  */
10429       if (mips_pic == EMBEDDED_PIC)
10430         return 4;
10431       else
10432         return 1;
10433     }
10434
10435   /* Return the address of the delay slot.  */
10436   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10437 }
10438
10439 /* This is called before the symbol table is processed.  In order to
10440    work with gcc when using mips-tfile, we must keep all local labels.
10441    However, in other cases, we want to discard them.  If we were
10442    called with -g, but we didn't see any debugging information, it may
10443    mean that gcc is smuggling debugging information through to
10444    mips-tfile, in which case we must generate all local labels.  */
10445
10446 void
10447 mips_frob_file_before_adjust ()
10448 {
10449 #ifndef NO_ECOFF_DEBUGGING
10450   if (ECOFF_DEBUGGING
10451       && mips_debug != 0
10452       && ! ecoff_debugging_seen)
10453     flag_keep_locals = 1;
10454 #endif
10455 }
10456
10457 /* Sort any unmatched HI16_S relocs so that they immediately precede
10458    the corresponding LO reloc.  This is called before md_apply_fix3 and
10459    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10460    explicit use of the %hi modifier.  */
10461
10462 void
10463 mips_frob_file ()
10464 {
10465   struct mips_hi_fixup *l;
10466
10467   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10468     {
10469       segment_info_type *seginfo;
10470       int pass;
10471
10472       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10473
10474       /* Check quickly whether the next fixup happens to be a matching
10475          %lo.  */
10476       if (l->fixp->fx_next != NULL
10477           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10478           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10479           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10480         continue;
10481
10482       /* Look through the fixups for this segment for a matching %lo.
10483          When we find one, move the %hi just in front of it.  We do
10484          this in two passes.  In the first pass, we try to find a
10485          unique %lo.  In the second pass, we permit multiple %hi
10486          relocs for a single %lo (this is a GNU extension).  */
10487       seginfo = seg_info (l->seg);
10488       for (pass = 0; pass < 2; pass++)
10489         {
10490           fixS *f, *prev;
10491
10492           prev = NULL;
10493           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10494             {
10495               /* Check whether this is a %lo fixup which matches l->fixp.  */
10496               if (f->fx_r_type == BFD_RELOC_LO16
10497                   && f->fx_addsy == l->fixp->fx_addsy
10498                   && f->fx_offset == l->fixp->fx_offset
10499                   && (pass == 1
10500                       || prev == NULL
10501                       || prev->fx_r_type != BFD_RELOC_HI16_S
10502                       || prev->fx_addsy != f->fx_addsy
10503                       || prev->fx_offset !=  f->fx_offset))
10504                 {
10505                   fixS **pf;
10506
10507                   /* Move l->fixp before f.  */
10508                   for (pf = &seginfo->fix_root;
10509                        *pf != l->fixp;
10510                        pf = &(*pf)->fx_next)
10511                     assert (*pf != NULL);
10512
10513                   *pf = l->fixp->fx_next;
10514
10515                   l->fixp->fx_next = f;
10516                   if (prev == NULL)
10517                     seginfo->fix_root = l->fixp;
10518                   else
10519                     prev->fx_next = l->fixp;
10520
10521                   break;
10522                 }
10523
10524               prev = f;
10525             }
10526
10527           if (f != NULL)
10528             break;
10529
10530 #if 0 /* GCC code motion plus incomplete dead code elimination
10531          can leave a %hi without a %lo.  */
10532           if (pass == 1)
10533             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10534                            _("Unmatched %%hi reloc"));
10535 #endif
10536         }
10537     }
10538 }
10539
10540 /* When generating embedded PIC code we need to use a special
10541    relocation to represent the difference of two symbols in the .text
10542    section (switch tables use a difference of this sort).  See
10543    include/coff/mips.h for details.  This macro checks whether this
10544    fixup requires the special reloc.  */
10545 #define SWITCH_TABLE(fixp) \
10546   ((fixp)->fx_r_type == BFD_RELOC_32 \
10547    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10548    && (fixp)->fx_addsy != NULL \
10549    && (fixp)->fx_subsy != NULL \
10550    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10551    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10552
10553 /* When generating embedded PIC code we must keep all PC relative
10554    relocations, in case the linker has to relax a call.  We also need
10555    to keep relocations for switch table entries.
10556
10557    We may have combined relocations without symbols in the N32/N64 ABI.
10558    We have to prevent gas from dropping them.  */
10559
10560 int
10561 mips_force_relocation (fixp)
10562      fixS *fixp;
10563 {
10564   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10565       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10566     return 1;
10567
10568   if (HAVE_NEWABI
10569       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10570       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10571           || fixp->fx_r_type == BFD_RELOC_HI16_S
10572           || fixp->fx_r_type == BFD_RELOC_LO16))
10573     return 1;
10574
10575   return (mips_pic == EMBEDDED_PIC
10576           && (fixp->fx_pcrel
10577               || SWITCH_TABLE (fixp)
10578               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10579               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10580 }
10581
10582 #ifdef OBJ_ELF
10583 static int
10584 mips_need_elf_addend_fixup (fixP)
10585      fixS *fixP;
10586 {
10587   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10588     return 1;
10589   if (mips_pic == EMBEDDED_PIC
10590       && S_IS_WEAK (fixP->fx_addsy))
10591     return 1;
10592   if (mips_pic != EMBEDDED_PIC
10593       && (S_IS_WEAK (fixP->fx_addsy)
10594           || S_IS_EXTERNAL (fixP->fx_addsy))
10595       && !S_IS_COMMON (fixP->fx_addsy))
10596     return 1;
10597   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10598       && (((bfd_get_section_flags (stdoutput,
10599                                    S_GET_SEGMENT (fixP->fx_addsy))
10600             & SEC_LINK_ONCE) != 0)
10601           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10602                        ".gnu.linkonce",
10603                        sizeof (".gnu.linkonce") - 1)))
10604     return 1;
10605   return 0;
10606 }
10607 #endif
10608
10609 /* Apply a fixup to the object file.  */
10610
10611 void
10612 md_apply_fix3 (fixP, valP, seg)
10613      fixS *fixP;
10614      valueT *valP;
10615      segT seg ATTRIBUTE_UNUSED;
10616 {
10617   bfd_byte *buf;
10618   long insn;
10619   valueT value;
10620
10621   assert (fixP->fx_size == 4
10622           || fixP->fx_r_type == BFD_RELOC_16
10623           || fixP->fx_r_type == BFD_RELOC_32
10624           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10625           || fixP->fx_r_type == BFD_RELOC_HI16_S
10626           || fixP->fx_r_type == BFD_RELOC_LO16
10627           || fixP->fx_r_type == BFD_RELOC_GPREL16
10628           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10629           || fixP->fx_r_type == BFD_RELOC_GPREL32
10630           || fixP->fx_r_type == BFD_RELOC_64
10631           || fixP->fx_r_type == BFD_RELOC_CTOR
10632           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10633           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10634           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10635           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10636           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10637           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10638           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10639           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10640           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10641
10642   value = *valP;
10643
10644   /* If we aren't adjusting this fixup to be against the section
10645      symbol, we need to adjust the value.  */
10646 #ifdef OBJ_ELF
10647   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10648     {
10649       if (mips_need_elf_addend_fixup (fixP))
10650         {
10651           reloc_howto_type *howto;
10652           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10653
10654           value -= symval;
10655
10656           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10657           if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
10658             {
10659               /* In this case, the bfd_install_relocation routine will
10660                  incorrectly add the symbol value back in.  We just want
10661                  the addend to appear in the object file.  */
10662               value -= symval;
10663
10664               /* Make sure the addend is still non-zero.  If it became zero
10665                  after the last operation, set it to a spurious value and
10666                  subtract the same value from the object file's contents.  */
10667               if (value == 0)
10668                 {
10669                   value = 8;
10670
10671                   /* The in-place addends for LO16 relocations are signed;
10672                      leave the matching HI16 in-place addends as zero.  */
10673                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10674                     {
10675                       bfd_vma contents, mask, field;
10676
10677                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10678                                                + fixP->fx_where,
10679                                                fixP->fx_size * 8,
10680                                                target_big_endian);
10681
10682                       /* MASK has bits set where the relocation should go.
10683                          FIELD is -value, shifted into the appropriate place
10684                          for this relocation.  */
10685                       mask = 1 << (howto->bitsize - 1);
10686                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10687                       field = (-value >> howto->rightshift) << howto->bitpos;
10688
10689                       bfd_put_bits ((field & mask) | (contents & ~mask),
10690                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10691                                     fixP->fx_size * 8,
10692                                     target_big_endian);
10693                     }
10694                 }
10695             }
10696         }
10697
10698       /* This code was generated using trial and error and so is
10699          fragile and not trustworthy.  If you change it, you should
10700          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10701          they still pass.  */
10702       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10703         {
10704           value += fixP->fx_frag->fr_address + fixP->fx_where;
10705
10706           /* BFD's REL handling, for MIPS, is _very_ weird.
10707              This gives the right results, but it can't possibly
10708              be the way things are supposed to work.  */
10709           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10710                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10711               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10712             value += fixP->fx_frag->fr_address + fixP->fx_where;
10713         }
10714     }
10715 #endif
10716
10717   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10718
10719   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10720     fixP->fx_done = 1;
10721
10722   switch (fixP->fx_r_type)
10723     {
10724     case BFD_RELOC_MIPS_JMP:
10725     case BFD_RELOC_MIPS_SHIFT5:
10726     case BFD_RELOC_MIPS_SHIFT6:
10727     case BFD_RELOC_MIPS_GOT_DISP:
10728     case BFD_RELOC_MIPS_GOT_PAGE:
10729     case BFD_RELOC_MIPS_GOT_OFST:
10730     case BFD_RELOC_MIPS_SUB:
10731     case BFD_RELOC_MIPS_INSERT_A:
10732     case BFD_RELOC_MIPS_INSERT_B:
10733     case BFD_RELOC_MIPS_DELETE:
10734     case BFD_RELOC_MIPS_HIGHEST:
10735     case BFD_RELOC_MIPS_HIGHER:
10736     case BFD_RELOC_MIPS_SCN_DISP:
10737     case BFD_RELOC_MIPS_REL16:
10738     case BFD_RELOC_MIPS_RELGOT:
10739     case BFD_RELOC_MIPS_JALR:
10740     case BFD_RELOC_HI16:
10741     case BFD_RELOC_HI16_S:
10742     case BFD_RELOC_GPREL16:
10743     case BFD_RELOC_MIPS_LITERAL:
10744     case BFD_RELOC_MIPS_CALL16:
10745     case BFD_RELOC_MIPS_GOT16:
10746     case BFD_RELOC_GPREL32:
10747     case BFD_RELOC_MIPS_GOT_HI16:
10748     case BFD_RELOC_MIPS_GOT_LO16:
10749     case BFD_RELOC_MIPS_CALL_HI16:
10750     case BFD_RELOC_MIPS_CALL_LO16:
10751     case BFD_RELOC_MIPS16_GPREL:
10752       if (fixP->fx_pcrel)
10753         as_bad_where (fixP->fx_file, fixP->fx_line,
10754                       _("Invalid PC relative reloc"));
10755       /* Nothing needed to do. The value comes from the reloc entry */
10756       break;
10757
10758     case BFD_RELOC_MIPS16_JMP:
10759       /* We currently always generate a reloc against a symbol, which
10760          means that we don't want an addend even if the symbol is
10761          defined.  */
10762       fixP->fx_addnumber = 0;
10763       break;
10764
10765     case BFD_RELOC_PCREL_HI16_S:
10766       /* The addend for this is tricky if it is internal, so we just
10767          do everything here rather than in bfd_install_relocation.  */
10768       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10769           && !fixP->fx_done
10770           && value != 0)
10771         break;
10772       if (fixP->fx_addsy
10773           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10774         {
10775           /* For an external symbol adjust by the address to make it
10776              pcrel_offset.  We use the address of the RELLO reloc
10777              which follows this one.  */
10778           value += (fixP->fx_next->fx_frag->fr_address
10779                     + fixP->fx_next->fx_where);
10780         }
10781       value = ((value + 0x8000) >> 16) & 0xffff;
10782       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10783       if (target_big_endian)
10784         buf += 2;
10785       md_number_to_chars ((char *) buf, value, 2);
10786       break;
10787
10788     case BFD_RELOC_PCREL_LO16:
10789       /* The addend for this is tricky if it is internal, so we just
10790          do everything here rather than in bfd_install_relocation.  */
10791       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10792           && !fixP->fx_done
10793           && value != 0)
10794         break;
10795       if (fixP->fx_addsy
10796           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10797         value += fixP->fx_frag->fr_address + fixP->fx_where;
10798       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10799       if (target_big_endian)
10800         buf += 2;
10801       md_number_to_chars ((char *) buf, value, 2);
10802       break;
10803
10804     case BFD_RELOC_64:
10805       /* This is handled like BFD_RELOC_32, but we output a sign
10806          extended value if we are only 32 bits.  */
10807       if (fixP->fx_done
10808           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10809         {
10810           if (8 <= sizeof (valueT))
10811             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10812                                 value, 8);
10813           else
10814             {
10815               long w1, w2;
10816               long hiv;
10817
10818               w1 = w2 = fixP->fx_where;
10819               if (target_big_endian)
10820                 w1 += 4;
10821               else
10822                 w2 += 4;
10823               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10824               if ((value & 0x80000000) != 0)
10825                 hiv = 0xffffffff;
10826               else
10827                 hiv = 0;
10828               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10829             }
10830         }
10831       break;
10832
10833     case BFD_RELOC_RVA:
10834     case BFD_RELOC_32:
10835       /* If we are deleting this reloc entry, we must fill in the
10836          value now.  This can happen if we have a .word which is not
10837          resolved when it appears but is later defined.  We also need
10838          to fill in the value if this is an embedded PIC switch table
10839          entry.  */
10840       if (fixP->fx_done
10841           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10842         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10843                             value, 4);
10844       break;
10845
10846     case BFD_RELOC_16:
10847       /* If we are deleting this reloc entry, we must fill in the
10848          value now.  */
10849       assert (fixP->fx_size == 2);
10850       if (fixP->fx_done)
10851         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10852                             value, 2);
10853       break;
10854
10855     case BFD_RELOC_LO16:
10856       /* When handling an embedded PIC switch statement, we can wind
10857          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
10858       if (fixP->fx_done)
10859         {
10860           if (value + 0x8000 > 0xffff)
10861             as_bad_where (fixP->fx_file, fixP->fx_line,
10862                           _("relocation overflow"));
10863           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10864           if (target_big_endian)
10865             buf += 2;
10866           md_number_to_chars ((char *) buf, value, 2);
10867         }
10868       break;
10869
10870     case BFD_RELOC_16_PCREL_S2:
10871       if ((value & 0x3) != 0)
10872         as_bad_where (fixP->fx_file, fixP->fx_line,
10873                       _("Branch to odd address (%lx)"), (long) value);
10874
10875       /* Fall through.  */
10876
10877     case BFD_RELOC_16_PCREL:
10878       /*
10879        * We need to save the bits in the instruction since fixup_segment()
10880        * might be deleting the relocation entry (i.e., a branch within
10881        * the current segment).
10882        */
10883       if (!fixP->fx_done && value != 0)
10884         break;
10885       /* If 'value' is zero, the remaining reloc code won't actually
10886          do the store, so it must be done here.  This is probably
10887          a bug somewhere.  */
10888       if (!fixP->fx_done
10889           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10890               || fixP->fx_addsy == NULL                 /* ??? */
10891               || ! S_IS_DEFINED (fixP->fx_addsy)))
10892         value -= fixP->fx_frag->fr_address + fixP->fx_where;
10893
10894       value = (offsetT) value >> 2;
10895
10896       /* update old instruction data */
10897       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10898       if (target_big_endian)
10899         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10900       else
10901         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10902
10903       if (value + 0x8000 <= 0xffff)
10904         insn |= value & 0xffff;
10905       else
10906         {
10907           /* The branch offset is too large.  If this is an
10908              unconditional branch, and we are not generating PIC code,
10909              we can convert it to an absolute jump instruction.  */
10910           if (mips_pic == NO_PIC
10911               && fixP->fx_done
10912               && fixP->fx_frag->fr_address >= text_section->vma
10913               && (fixP->fx_frag->fr_address
10914                   < text_section->vma + text_section->_raw_size)
10915               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
10916                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
10917                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10918             {
10919               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
10920                 insn = 0x0c000000;      /* jal */
10921               else
10922                 insn = 0x08000000;      /* j */
10923               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10924               fixP->fx_done = 0;
10925               fixP->fx_addsy = section_symbol (text_section);
10926               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10927             }
10928           else
10929             {
10930               /* FIXME.  It would be possible in principle to handle
10931                  conditional branches which overflow.  They could be
10932                  transformed into a branch around a jump.  This would
10933                  require setting up variant frags for each different
10934                  branch type.  The native MIPS assembler attempts to
10935                  handle these cases, but it appears to do it
10936                  incorrectly.  */
10937               as_bad_where (fixP->fx_file, fixP->fx_line,
10938                             _("Branch out of range"));
10939             }
10940         }
10941
10942       md_number_to_chars ((char *) buf, (valueT) insn, 4);
10943       break;
10944
10945     case BFD_RELOC_VTABLE_INHERIT:
10946       fixP->fx_done = 0;
10947       if (fixP->fx_addsy
10948           && !S_IS_DEFINED (fixP->fx_addsy)
10949           && !S_IS_WEAK (fixP->fx_addsy))
10950         S_SET_WEAK (fixP->fx_addsy);
10951       break;
10952
10953     case BFD_RELOC_VTABLE_ENTRY:
10954       fixP->fx_done = 0;
10955       break;
10956
10957     default:
10958       internalError ();
10959     }
10960 }
10961
10962 #if 0
10963 void
10964 printInsn (oc)
10965      unsigned long oc;
10966 {
10967   const struct mips_opcode *p;
10968   int treg, sreg, dreg, shamt;
10969   short imm;
10970   const char *args;
10971   int i;
10972
10973   for (i = 0; i < NUMOPCODES; ++i)
10974     {
10975       p = &mips_opcodes[i];
10976       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10977         {
10978           printf ("%08lx %s\t", oc, p->name);
10979           treg = (oc >> 16) & 0x1f;
10980           sreg = (oc >> 21) & 0x1f;
10981           dreg = (oc >> 11) & 0x1f;
10982           shamt = (oc >> 6) & 0x1f;
10983           imm = oc;
10984           for (args = p->args;; ++args)
10985             {
10986               switch (*args)
10987                 {
10988                 case '\0':
10989                   printf ("\n");
10990                   break;
10991
10992                 case ',':
10993                 case '(':
10994                 case ')':
10995                   printf ("%c", *args);
10996                   continue;
10997
10998                 case 'r':
10999                   assert (treg == sreg);
11000                   printf ("$%d,$%d", treg, sreg);
11001                   continue;
11002
11003                 case 'd':
11004                 case 'G':
11005                   printf ("$%d", dreg);
11006                   continue;
11007
11008                 case 't':
11009                 case 'E':
11010                   printf ("$%d", treg);
11011                   continue;
11012
11013                 case 'k':
11014                   printf ("0x%x", treg);
11015                   continue;
11016
11017                 case 'b':
11018                 case 's':
11019                   printf ("$%d", sreg);
11020                   continue;
11021
11022                 case 'a':
11023                   printf ("0x%08lx", oc & 0x1ffffff);
11024                   continue;
11025
11026                 case 'i':
11027                 case 'j':
11028                 case 'o':
11029                 case 'u':
11030                   printf ("%d", imm);
11031                   continue;
11032
11033                 case '<':
11034                 case '>':
11035                   printf ("$%d", shamt);
11036                   continue;
11037
11038                 default:
11039                   internalError ();
11040                 }
11041               break;
11042             }
11043           return;
11044         }
11045     }
11046   printf (_("%08lx  UNDEFINED\n"), oc);
11047 }
11048 #endif
11049
11050 static symbolS *
11051 get_symbol ()
11052 {
11053   int c;
11054   char *name;
11055   symbolS *p;
11056
11057   name = input_line_pointer;
11058   c = get_symbol_end ();
11059   p = (symbolS *) symbol_find_or_make (name);
11060   *input_line_pointer = c;
11061   return p;
11062 }
11063
11064 /* Align the current frag to a given power of two.  The MIPS assembler
11065    also automatically adjusts any preceding label.  */
11066
11067 static void
11068 mips_align (to, fill, label)
11069      int to;
11070      int fill;
11071      symbolS *label;
11072 {
11073   mips_emit_delays (false);
11074   frag_align (to, fill, 0);
11075   record_alignment (now_seg, to);
11076   if (label != NULL)
11077     {
11078       assert (S_GET_SEGMENT (label) == now_seg);
11079       symbol_set_frag (label, frag_now);
11080       S_SET_VALUE (label, (valueT) frag_now_fix ());
11081     }
11082 }
11083
11084 /* Align to a given power of two.  .align 0 turns off the automatic
11085    alignment used by the data creating pseudo-ops.  */
11086
11087 static void
11088 s_align (x)
11089      int x ATTRIBUTE_UNUSED;
11090 {
11091   register int temp;
11092   register long temp_fill;
11093   long max_alignment = 15;
11094
11095   /*
11096
11097     o  Note that the assembler pulls down any immediately preceeding label
11098        to the aligned address.
11099     o  It's not documented but auto alignment is reinstated by
11100        a .align pseudo instruction.
11101     o  Note also that after auto alignment is turned off the mips assembler
11102        issues an error on attempt to assemble an improperly aligned data item.
11103        We don't.
11104
11105     */
11106
11107   temp = get_absolute_expression ();
11108   if (temp > max_alignment)
11109     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11110   else if (temp < 0)
11111     {
11112       as_warn (_("Alignment negative: 0 assumed."));
11113       temp = 0;
11114     }
11115   if (*input_line_pointer == ',')
11116     {
11117       ++input_line_pointer;
11118       temp_fill = get_absolute_expression ();
11119     }
11120   else
11121     temp_fill = 0;
11122   if (temp)
11123     {
11124       auto_align = 1;
11125       mips_align (temp, (int) temp_fill,
11126                   insn_labels != NULL ? insn_labels->label : NULL);
11127     }
11128   else
11129     {
11130       auto_align = 0;
11131     }
11132
11133   demand_empty_rest_of_line ();
11134 }
11135
11136 void
11137 mips_flush_pending_output ()
11138 {
11139   mips_emit_delays (false);
11140   mips_clear_insn_labels ();
11141 }
11142
11143 static void
11144 s_change_sec (sec)
11145      int sec;
11146 {
11147   segT seg;
11148
11149   /* When generating embedded PIC code, we only use the .text, .lit8,
11150      .sdata and .sbss sections.  We change the .data and .rdata
11151      pseudo-ops to use .sdata.  */
11152   if (mips_pic == EMBEDDED_PIC
11153       && (sec == 'd' || sec == 'r'))
11154     sec = 's';
11155
11156 #ifdef OBJ_ELF
11157   /* The ELF backend needs to know that we are changing sections, so
11158      that .previous works correctly.  We could do something like check
11159      for an obj_section_change_hook macro, but that might be confusing
11160      as it would not be appropriate to use it in the section changing
11161      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11162      This should be cleaner, somehow.  */
11163   obj_elf_section_change_hook ();
11164 #endif
11165
11166   mips_emit_delays (false);
11167   switch (sec)
11168     {
11169     case 't':
11170       s_text (0);
11171       break;
11172     case 'd':
11173       s_data (0);
11174       break;
11175     case 'b':
11176       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11177       demand_empty_rest_of_line ();
11178       break;
11179
11180     case 'r':
11181       if (USE_GLOBAL_POINTER_OPT)
11182         {
11183           seg = subseg_new (RDATA_SECTION_NAME,
11184                             (subsegT) get_absolute_expression ());
11185           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11186             {
11187               bfd_set_section_flags (stdoutput, seg,
11188                                      (SEC_ALLOC
11189                                       | SEC_LOAD
11190                                       | SEC_READONLY
11191                                       | SEC_RELOC
11192                                       | SEC_DATA));
11193               if (strcmp (TARGET_OS, "elf") != 0)
11194                 record_alignment (seg, 4);
11195             }
11196           demand_empty_rest_of_line ();
11197         }
11198       else
11199         {
11200           as_bad (_("No read only data section in this object file format"));
11201           demand_empty_rest_of_line ();
11202           return;
11203         }
11204       break;
11205
11206     case 's':
11207       if (USE_GLOBAL_POINTER_OPT)
11208         {
11209           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11210           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11211             {
11212               bfd_set_section_flags (stdoutput, seg,
11213                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11214                                      | SEC_DATA);
11215               if (strcmp (TARGET_OS, "elf") != 0)
11216                 record_alignment (seg, 4);
11217             }
11218           demand_empty_rest_of_line ();
11219           break;
11220         }
11221       else
11222         {
11223           as_bad (_("Global pointers not supported; recompile -G 0"));
11224           demand_empty_rest_of_line ();
11225           return;
11226         }
11227     }
11228
11229   auto_align = 1;
11230 }
11231
11232 void
11233 mips_enable_auto_align ()
11234 {
11235   auto_align = 1;
11236 }
11237
11238 static void
11239 s_cons (log_size)
11240      int log_size;
11241 {
11242   symbolS *label;
11243
11244   label = insn_labels != NULL ? insn_labels->label : NULL;
11245   mips_emit_delays (false);
11246   if (log_size > 0 && auto_align)
11247     mips_align (log_size, 0, label);
11248   mips_clear_insn_labels ();
11249   cons (1 << log_size);
11250 }
11251
11252 static void
11253 s_float_cons (type)
11254      int type;
11255 {
11256   symbolS *label;
11257
11258   label = insn_labels != NULL ? insn_labels->label : NULL;
11259
11260   mips_emit_delays (false);
11261
11262   if (auto_align)
11263     {
11264       if (type == 'd')
11265         mips_align (3, 0, label);
11266       else
11267         mips_align (2, 0, label);
11268     }
11269
11270   mips_clear_insn_labels ();
11271
11272   float_cons (type);
11273 }
11274
11275 /* Handle .globl.  We need to override it because on Irix 5 you are
11276    permitted to say
11277        .globl foo .text
11278    where foo is an undefined symbol, to mean that foo should be
11279    considered to be the address of a function.  */
11280
11281 static void
11282 s_mips_globl (x)
11283      int x ATTRIBUTE_UNUSED;
11284 {
11285   char *name;
11286   int c;
11287   symbolS *symbolP;
11288   flagword flag;
11289
11290   name = input_line_pointer;
11291   c = get_symbol_end ();
11292   symbolP = symbol_find_or_make (name);
11293   *input_line_pointer = c;
11294   SKIP_WHITESPACE ();
11295
11296   /* On Irix 5, every global symbol that is not explicitly labelled as
11297      being a function is apparently labelled as being an object.  */
11298   flag = BSF_OBJECT;
11299
11300   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11301     {
11302       char *secname;
11303       asection *sec;
11304
11305       secname = input_line_pointer;
11306       c = get_symbol_end ();
11307       sec = bfd_get_section_by_name (stdoutput, secname);
11308       if (sec == NULL)
11309         as_bad (_("%s: no such section"), secname);
11310       *input_line_pointer = c;
11311
11312       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11313         flag = BSF_FUNCTION;
11314     }
11315
11316   symbol_get_bfdsym (symbolP)->flags |= flag;
11317
11318   S_SET_EXTERNAL (symbolP);
11319   demand_empty_rest_of_line ();
11320 }
11321
11322 static void
11323 s_option (x)
11324      int x ATTRIBUTE_UNUSED;
11325 {
11326   char *opt;
11327   char c;
11328
11329   opt = input_line_pointer;
11330   c = get_symbol_end ();
11331
11332   if (*opt == 'O')
11333     {
11334       /* FIXME: What does this mean?  */
11335     }
11336   else if (strncmp (opt, "pic", 3) == 0)
11337     {
11338       int i;
11339
11340       i = atoi (opt + 3);
11341       if (i == 0)
11342         mips_pic = NO_PIC;
11343       else if (i == 2)
11344         mips_pic = SVR4_PIC;
11345       else
11346         as_bad (_(".option pic%d not supported"), i);
11347
11348       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11349         {
11350           if (g_switch_seen && g_switch_value != 0)
11351             as_warn (_("-G may not be used with SVR4 PIC code"));
11352           g_switch_value = 0;
11353           bfd_set_gp_size (stdoutput, 0);
11354         }
11355     }
11356   else
11357     as_warn (_("Unrecognized option \"%s\""), opt);
11358
11359   *input_line_pointer = c;
11360   demand_empty_rest_of_line ();
11361 }
11362
11363 /* This structure is used to hold a stack of .set values.  */
11364
11365 struct mips_option_stack
11366 {
11367   struct mips_option_stack *next;
11368   struct mips_set_options options;
11369 };
11370
11371 static struct mips_option_stack *mips_opts_stack;
11372
11373 /* Handle the .set pseudo-op.  */
11374
11375 static void
11376 s_mipsset (x)
11377      int x ATTRIBUTE_UNUSED;
11378 {
11379   char *name = input_line_pointer, ch;
11380
11381   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11382     ++input_line_pointer;
11383   ch = *input_line_pointer;
11384   *input_line_pointer = '\0';
11385
11386   if (strcmp (name, "reorder") == 0)
11387     {
11388       if (mips_opts.noreorder && prev_nop_frag != NULL)
11389         {
11390           /* If we still have pending nops, we can discard them.  The
11391              usual nop handling will insert any that are still
11392              needed.  */
11393           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11394                                     * (mips_opts.mips16 ? 2 : 4));
11395           prev_nop_frag = NULL;
11396         }
11397       mips_opts.noreorder = 0;
11398     }
11399   else if (strcmp (name, "noreorder") == 0)
11400     {
11401       mips_emit_delays (true);
11402       mips_opts.noreorder = 1;
11403       mips_any_noreorder = 1;
11404     }
11405   else if (strcmp (name, "at") == 0)
11406     {
11407       mips_opts.noat = 0;
11408     }
11409   else if (strcmp (name, "noat") == 0)
11410     {
11411       mips_opts.noat = 1;
11412     }
11413   else if (strcmp (name, "macro") == 0)
11414     {
11415       mips_opts.warn_about_macros = 0;
11416     }
11417   else if (strcmp (name, "nomacro") == 0)
11418     {
11419       if (mips_opts.noreorder == 0)
11420         as_bad (_("`noreorder' must be set before `nomacro'"));
11421       mips_opts.warn_about_macros = 1;
11422     }
11423   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11424     {
11425       mips_opts.nomove = 0;
11426     }
11427   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11428     {
11429       mips_opts.nomove = 1;
11430     }
11431   else if (strcmp (name, "bopt") == 0)
11432     {
11433       mips_opts.nobopt = 0;
11434     }
11435   else if (strcmp (name, "nobopt") == 0)
11436     {
11437       mips_opts.nobopt = 1;
11438     }
11439   else if (strcmp (name, "mips16") == 0
11440            || strcmp (name, "MIPS-16") == 0)
11441     mips_opts.mips16 = 1;
11442   else if (strcmp (name, "nomips16") == 0
11443            || strcmp (name, "noMIPS-16") == 0)
11444     mips_opts.mips16 = 0;
11445   else if (strcmp (name, "mips3d") == 0)
11446     mips_opts.ase_mips3d = 1;
11447   else if (strcmp (name, "nomips3d") == 0)
11448     mips_opts.ase_mips3d = 0;
11449   else if (strcmp (name, "mdmx") == 0)
11450     mips_opts.ase_mdmx = 1;
11451   else if (strcmp (name, "nomdmx") == 0)
11452     mips_opts.ase_mdmx = 0;
11453   else if (strncmp (name, "mips", 4) == 0)
11454     {
11455       int isa;
11456
11457       /* Permit the user to change the ISA on the fly.  Needless to
11458          say, misuse can cause serious problems.  */
11459       isa = atoi (name + 4);
11460       switch (isa)
11461         {
11462         case  0:
11463           mips_opts.gp32 = file_mips_gp32;
11464           mips_opts.fp32 = file_mips_fp32;
11465           break;
11466         case  1:
11467         case  2:
11468         case 32:
11469           mips_opts.gp32 = 1;
11470           mips_opts.fp32 = 1;
11471           break;
11472         case  3:
11473         case  4:
11474         case  5:
11475         case 64:
11476           mips_opts.gp32 = 0;
11477           mips_opts.fp32 = 0;
11478           break;
11479         default:
11480           as_bad (_("unknown ISA level %s"), name + 4);
11481           break;
11482         }
11483
11484       switch (isa)
11485         {
11486         case  0: mips_opts.isa = file_mips_isa;   break;
11487         case  1: mips_opts.isa = ISA_MIPS1;       break;
11488         case  2: mips_opts.isa = ISA_MIPS2;       break;
11489         case  3: mips_opts.isa = ISA_MIPS3;       break;
11490         case  4: mips_opts.isa = ISA_MIPS4;       break;
11491         case  5: mips_opts.isa = ISA_MIPS5;       break;
11492         case 32: mips_opts.isa = ISA_MIPS32;      break;
11493         case 64: mips_opts.isa = ISA_MIPS64;      break;
11494         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11495         }
11496     }
11497   else if (strcmp (name, "autoextend") == 0)
11498     mips_opts.noautoextend = 0;
11499   else if (strcmp (name, "noautoextend") == 0)
11500     mips_opts.noautoextend = 1;
11501   else if (strcmp (name, "push") == 0)
11502     {
11503       struct mips_option_stack *s;
11504
11505       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11506       s->next = mips_opts_stack;
11507       s->options = mips_opts;
11508       mips_opts_stack = s;
11509     }
11510   else if (strcmp (name, "pop") == 0)
11511     {
11512       struct mips_option_stack *s;
11513
11514       s = mips_opts_stack;
11515       if (s == NULL)
11516         as_bad (_(".set pop with no .set push"));
11517       else
11518         {
11519           /* If we're changing the reorder mode we need to handle
11520              delay slots correctly.  */
11521           if (s->options.noreorder && ! mips_opts.noreorder)
11522             mips_emit_delays (true);
11523           else if (! s->options.noreorder && mips_opts.noreorder)
11524             {
11525               if (prev_nop_frag != NULL)
11526                 {
11527                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11528                                             * (mips_opts.mips16 ? 2 : 4));
11529                   prev_nop_frag = NULL;
11530                 }
11531             }
11532
11533           mips_opts = s->options;
11534           mips_opts_stack = s->next;
11535           free (s);
11536         }
11537     }
11538   else
11539     {
11540       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11541     }
11542   *input_line_pointer = ch;
11543   demand_empty_rest_of_line ();
11544 }
11545
11546 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11547    .option pic2.  It means to generate SVR4 PIC calls.  */
11548
11549 static void
11550 s_abicalls (ignore)
11551      int ignore ATTRIBUTE_UNUSED;
11552 {
11553   mips_pic = SVR4_PIC;
11554   if (USE_GLOBAL_POINTER_OPT)
11555     {
11556       if (g_switch_seen && g_switch_value != 0)
11557         as_warn (_("-G may not be used with SVR4 PIC code"));
11558       g_switch_value = 0;
11559     }
11560   bfd_set_gp_size (stdoutput, 0);
11561   demand_empty_rest_of_line ();
11562 }
11563
11564 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11565    PIC code.  It sets the $gp register for the function based on the
11566    function address, which is in the register named in the argument.
11567    This uses a relocation against _gp_disp, which is handled specially
11568    by the linker.  The result is:
11569         lui     $gp,%hi(_gp_disp)
11570         addiu   $gp,$gp,%lo(_gp_disp)
11571         addu    $gp,$gp,.cpload argument
11572    The .cpload argument is normally $25 == $t9.  */
11573
11574 static void
11575 s_cpload (ignore)
11576      int ignore ATTRIBUTE_UNUSED;
11577 {
11578   expressionS ex;
11579   int icnt = 0;
11580
11581   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11582      .cpload is ignored.  */
11583   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11584     {
11585       s_ignore (0);
11586       return;
11587     }
11588
11589   /* .cpload should be in a .set noreorder section.  */
11590   if (mips_opts.noreorder == 0)
11591     as_warn (_(".cpload not in noreorder section"));
11592
11593   ex.X_op = O_symbol;
11594   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11595   ex.X_op_symbol = NULL;
11596   ex.X_add_number = 0;
11597
11598   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11599   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11600
11601   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11602   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11603                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11604
11605   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11606                mips_gp_register, mips_gp_register, tc_get_register (0));
11607
11608   demand_empty_rest_of_line ();
11609 }
11610
11611 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11612      .cpsetup $reg1, offset|$reg2, label
11613
11614    If offset is given, this results in:
11615      sd         $gp, offset($sp)
11616      lui        $gp, %hi(%neg(%gp_rel(label)))
11617      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11618      daddu      $gp, $gp, $reg1
11619
11620    If $reg2 is given, this results in:
11621      daddu      $reg2, $gp, $0
11622      lui        $gp, %hi(%neg(%gp_rel(label)))
11623      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11624      daddu      $gp, $gp, $reg1
11625    $reg1 is normally $25 == $t9.  */
11626 static void
11627 s_cpsetup (ignore)
11628      int ignore ATTRIBUTE_UNUSED;
11629 {
11630   expressionS ex_off;
11631   expressionS ex_sym;
11632   int reg1;
11633   int icnt = 0;
11634   char *sym;
11635
11636   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11637      We also need NewABI support.  */
11638   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11639     {
11640       s_ignore (0);
11641       return;
11642     }
11643
11644   reg1 = tc_get_register (0);
11645   SKIP_WHITESPACE ();
11646   if (*input_line_pointer != ',')
11647     {
11648       as_bad (_("missing argument separator ',' for .cpsetup"));
11649       return;
11650     }
11651   else
11652     ++input_line_pointer;
11653   SKIP_WHITESPACE ();
11654   if (*input_line_pointer == '$')
11655     {
11656       mips_cpreturn_register = tc_get_register (0);
11657       mips_cpreturn_offset = -1;
11658     }
11659   else
11660     {
11661       mips_cpreturn_offset = get_absolute_expression ();
11662       mips_cpreturn_register = -1;
11663     }
11664   SKIP_WHITESPACE ();
11665   if (*input_line_pointer != ',')
11666     {
11667       as_bad (_("missing argument separator ',' for .cpsetup"));
11668       return;
11669     }
11670   else
11671     ++input_line_pointer;
11672   SKIP_WHITESPACE ();
11673   sym = input_line_pointer;
11674   while (ISALNUM (*input_line_pointer))
11675     ++input_line_pointer;
11676   *input_line_pointer = 0;
11677
11678   ex_sym.X_op = O_symbol;
11679   ex_sym.X_add_symbol = symbol_find_or_make (sym);
11680   ex_sym.X_op_symbol = NULL;
11681   ex_sym.X_add_number = 0;
11682
11683   if (mips_cpreturn_register == -1)
11684     {
11685       ex_off.X_op = O_constant;
11686       ex_off.X_add_symbol = NULL;
11687       ex_off.X_op_symbol = NULL;
11688       ex_off.X_add_number = mips_cpreturn_offset;
11689
11690       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11691                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11692     }
11693   else
11694     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11695                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11696
11697   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11698                (int) BFD_RELOC_GPREL16);
11699   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11700   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_HI16_S);
11701   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11702                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11703   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11704   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_LO16);
11705   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11706                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11707                mips_gp_register, mips_gp_register, reg1);
11708
11709   demand_empty_rest_of_line ();
11710 }
11711
11712 static void
11713 s_cplocal (ignore)
11714      int ignore ATTRIBUTE_UNUSED;
11715 {
11716   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11717    .cplocal is ignored.  */
11718   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11719     {
11720       s_ignore (0);
11721       return;
11722     }
11723
11724   mips_gp_register = tc_get_register (0);
11725   demand_empty_rest_of_line ();
11726 }
11727
11728 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11729    offset from $sp.  The offset is remembered, and after making a PIC
11730    call $gp is restored from that location.  */
11731
11732 static void
11733 s_cprestore (ignore)
11734      int ignore ATTRIBUTE_UNUSED;
11735 {
11736   expressionS ex;
11737   int icnt = 0;
11738
11739   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11740      .cprestore is ignored.  */
11741   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11742     {
11743       s_ignore (0);
11744       return;
11745     }
11746
11747   mips_cprestore_offset = get_absolute_expression ();
11748   mips_cprestore_valid = 1;
11749
11750   ex.X_op = O_constant;
11751   ex.X_add_symbol = NULL;
11752   ex.X_op_symbol = NULL;
11753   ex.X_add_number = mips_cprestore_offset;
11754
11755   macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11756                "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
11757
11758   demand_empty_rest_of_line ();
11759 }
11760
11761 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11762    was given in the preceeding .gpsetup, it results in:
11763      ld         $gp, offset($sp)
11764
11765    If a register $reg2 was given there, it results in:
11766      daddiu     $gp, $gp, $reg2
11767  */
11768 static void
11769 s_cpreturn (ignore)
11770      int ignore ATTRIBUTE_UNUSED;
11771 {
11772   expressionS ex;
11773   int icnt = 0;
11774
11775   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11776      We also need NewABI support.  */
11777   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11778     {
11779       s_ignore (0);
11780       return;
11781     }
11782
11783   if (mips_cpreturn_register == -1)
11784     {
11785       ex.X_op = O_constant;
11786       ex.X_add_symbol = NULL;
11787       ex.X_op_symbol = NULL;
11788       ex.X_add_number = mips_cpreturn_offset;
11789
11790       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11791                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11792     }
11793   else
11794     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11795                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11796
11797   demand_empty_rest_of_line ();
11798 }
11799
11800 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
11801    code.  It sets the offset to use in gp_rel relocations.  */
11802
11803 static void
11804 s_gpvalue (ignore)
11805      int ignore ATTRIBUTE_UNUSED;
11806 {
11807   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11808      We also need NewABI support.  */
11809   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11810     {
11811       s_ignore (0);
11812       return;
11813     }
11814
11815   mips_gprel_offset = get_absolute_expression ();
11816
11817   demand_empty_rest_of_line ();
11818 }
11819
11820 /* Handle the .gpword pseudo-op.  This is used when generating PIC
11821    code.  It generates a 32 bit GP relative reloc.  */
11822
11823 static void
11824 s_gpword (ignore)
11825      int ignore ATTRIBUTE_UNUSED;
11826 {
11827   symbolS *label;
11828   expressionS ex;
11829   char *p;
11830
11831   /* When not generating PIC code, this is treated as .word.  */
11832   if (mips_pic != SVR4_PIC)
11833     {
11834       s_cons (2);
11835       return;
11836     }
11837
11838   label = insn_labels != NULL ? insn_labels->label : NULL;
11839   mips_emit_delays (true);
11840   if (auto_align)
11841     mips_align (2, 0, label);
11842   mips_clear_insn_labels ();
11843
11844   expression (&ex);
11845
11846   if (ex.X_op != O_symbol || ex.X_add_number != 0)
11847     {
11848       as_bad (_("Unsupported use of .gpword"));
11849       ignore_rest_of_line ();
11850     }
11851
11852   p = frag_more (4);
11853   md_number_to_chars (p, (valueT) 0, 4);
11854   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
11855                BFD_RELOC_GPREL32);
11856
11857   demand_empty_rest_of_line ();
11858 }
11859
11860 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
11861    tables in SVR4 PIC code.  */
11862
11863 static void
11864 s_cpadd (ignore)
11865      int ignore ATTRIBUTE_UNUSED;
11866 {
11867   int icnt = 0;
11868   int reg;
11869
11870   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11871      code.  */
11872   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11873     {
11874       s_ignore (0);
11875       return;
11876     }
11877
11878   /* Add $gp to the register named as an argument.  */
11879   reg = tc_get_register (0);
11880   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11881                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11882                "d,v,t", reg, reg, mips_gp_register);
11883
11884   demand_empty_rest_of_line ();
11885 }
11886
11887 /* Handle the .insn pseudo-op.  This marks instruction labels in
11888    mips16 mode.  This permits the linker to handle them specially,
11889    such as generating jalx instructions when needed.  We also make
11890    them odd for the duration of the assembly, in order to generate the
11891    right sort of code.  We will make them even in the adjust_symtab
11892    routine, while leaving them marked.  This is convenient for the
11893    debugger and the disassembler.  The linker knows to make them odd
11894    again.  */
11895
11896 static void
11897 s_insn (ignore)
11898      int ignore ATTRIBUTE_UNUSED;
11899 {
11900   mips16_mark_labels ();
11901
11902   demand_empty_rest_of_line ();
11903 }
11904
11905 /* Handle a .stabn directive.  We need these in order to mark a label
11906    as being a mips16 text label correctly.  Sometimes the compiler
11907    will emit a label, followed by a .stabn, and then switch sections.
11908    If the label and .stabn are in mips16 mode, then the label is
11909    really a mips16 text label.  */
11910
11911 static void
11912 s_mips_stab (type)
11913      int type;
11914 {
11915   if (type == 'n')
11916     mips16_mark_labels ();
11917
11918   s_stab (type);
11919 }
11920
11921 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11922  */
11923
11924 static void
11925 s_mips_weakext (ignore)
11926      int ignore ATTRIBUTE_UNUSED;
11927 {
11928   char *name;
11929   int c;
11930   symbolS *symbolP;
11931   expressionS exp;
11932
11933   name = input_line_pointer;
11934   c = get_symbol_end ();
11935   symbolP = symbol_find_or_make (name);
11936   S_SET_WEAK (symbolP);
11937   *input_line_pointer = c;
11938
11939   SKIP_WHITESPACE ();
11940
11941   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11942     {
11943       if (S_IS_DEFINED (symbolP))
11944         {
11945           as_bad ("ignoring attempt to redefine symbol %s",
11946                   S_GET_NAME (symbolP));
11947           ignore_rest_of_line ();
11948           return;
11949         }
11950
11951       if (*input_line_pointer == ',')
11952         {
11953           ++input_line_pointer;
11954           SKIP_WHITESPACE ();
11955         }
11956
11957       expression (&exp);
11958       if (exp.X_op != O_symbol)
11959         {
11960           as_bad ("bad .weakext directive");
11961           ignore_rest_of_line ();
11962           return;
11963         }
11964       symbol_set_value_expression (symbolP, &exp);
11965     }
11966
11967   demand_empty_rest_of_line ();
11968 }
11969
11970 /* Parse a register string into a number.  Called from the ECOFF code
11971    to parse .frame.  The argument is non-zero if this is the frame
11972    register, so that we can record it in mips_frame_reg.  */
11973
11974 int
11975 tc_get_register (frame)
11976      int frame;
11977 {
11978   int reg;
11979
11980   SKIP_WHITESPACE ();
11981   if (*input_line_pointer++ != '$')
11982     {
11983       as_warn (_("expected `$'"));
11984       reg = ZERO;
11985     }
11986   else if (ISDIGIT (*input_line_pointer))
11987     {
11988       reg = get_absolute_expression ();
11989       if (reg < 0 || reg >= 32)
11990         {
11991           as_warn (_("Bad register number"));
11992           reg = ZERO;
11993         }
11994     }
11995   else
11996     {
11997       if (strncmp (input_line_pointer, "ra", 2) == 0)
11998         {
11999           reg = RA;
12000           input_line_pointer += 2;
12001         }
12002       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12003         {
12004           reg = FP;
12005           input_line_pointer += 2;
12006         }
12007       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12008         {
12009           reg = SP;
12010           input_line_pointer += 2;
12011         }
12012       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12013         {
12014           reg = GP;
12015           input_line_pointer += 2;
12016         }
12017       else if (strncmp (input_line_pointer, "at", 2) == 0)
12018         {
12019           reg = AT;
12020           input_line_pointer += 2;
12021         }
12022       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12023         {
12024           reg = KT0;
12025           input_line_pointer += 3;
12026         }
12027       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12028         {
12029           reg = KT1;
12030           input_line_pointer += 3;
12031         }
12032       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12033         {
12034           reg = ZERO;
12035           input_line_pointer += 4;
12036         }
12037       else
12038         {
12039           as_warn (_("Unrecognized register name"));
12040           reg = ZERO;
12041           while (ISALNUM(*input_line_pointer))
12042            input_line_pointer++;
12043         }
12044     }
12045   if (frame)
12046     {
12047       mips_frame_reg = reg != 0 ? reg : SP;
12048       mips_frame_reg_valid = 1;
12049       mips_cprestore_valid = 0;
12050     }
12051   return reg;
12052 }
12053
12054 valueT
12055 md_section_align (seg, addr)
12056      asection *seg;
12057      valueT addr;
12058 {
12059   int align = bfd_get_section_alignment (stdoutput, seg);
12060
12061 #ifdef OBJ_ELF
12062   /* We don't need to align ELF sections to the full alignment.
12063      However, Irix 5 may prefer that we align them at least to a 16
12064      byte boundary.  We don't bother to align the sections if we are
12065      targeted for an embedded system.  */
12066   if (strcmp (TARGET_OS, "elf") == 0)
12067     return addr;
12068   if (align > 4)
12069     align = 4;
12070 #endif
12071
12072   return ((addr + (1 << align) - 1) & (-1 << align));
12073 }
12074
12075 /* Utility routine, called from above as well.  If called while the
12076    input file is still being read, it's only an approximation.  (For
12077    example, a symbol may later become defined which appeared to be
12078    undefined earlier.)  */
12079
12080 static int
12081 nopic_need_relax (sym, before_relaxing)
12082      symbolS *sym;
12083      int before_relaxing;
12084 {
12085   if (sym == 0)
12086     return 0;
12087
12088   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12089     {
12090       const char *symname;
12091       int change;
12092
12093       /* Find out whether this symbol can be referenced off the $gp
12094          register.  It can be if it is smaller than the -G size or if
12095          it is in the .sdata or .sbss section.  Certain symbols can
12096          not be referenced off the $gp, although it appears as though
12097          they can.  */
12098       symname = S_GET_NAME (sym);
12099       if (symname != (const char *) NULL
12100           && (strcmp (symname, "eprol") == 0
12101               || strcmp (symname, "etext") == 0
12102               || strcmp (symname, "_gp") == 0
12103               || strcmp (symname, "edata") == 0
12104               || strcmp (symname, "_fbss") == 0
12105               || strcmp (symname, "_fdata") == 0
12106               || strcmp (symname, "_ftext") == 0
12107               || strcmp (symname, "end") == 0
12108               || strcmp (symname, "_gp_disp") == 0))
12109         change = 1;
12110       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12111                && (0
12112 #ifndef NO_ECOFF_DEBUGGING
12113                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12114                        && (symbol_get_obj (sym)->ecoff_extern_size
12115                            <= g_switch_value))
12116 #endif
12117                    /* We must defer this decision until after the whole
12118                       file has been read, since there might be a .extern
12119                       after the first use of this symbol.  */
12120                    || (before_relaxing
12121 #ifndef NO_ECOFF_DEBUGGING
12122                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12123 #endif
12124                        && S_GET_VALUE (sym) == 0)
12125                    || (S_GET_VALUE (sym) != 0
12126                        && S_GET_VALUE (sym) <= g_switch_value)))
12127         change = 0;
12128       else
12129         {
12130           const char *segname;
12131
12132           segname = segment_name (S_GET_SEGMENT (sym));
12133           assert (strcmp (segname, ".lit8") != 0
12134                   && strcmp (segname, ".lit4") != 0);
12135           change = (strcmp (segname, ".sdata") != 0
12136                     && strcmp (segname, ".sbss") != 0
12137                     && strncmp (segname, ".sdata.", 7) != 0
12138                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12139         }
12140       return change;
12141     }
12142   else
12143     /* We are not optimizing for the $gp register.  */
12144     return 1;
12145 }
12146
12147 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12148    extended opcode.  SEC is the section the frag is in.  */
12149
12150 static int
12151 mips16_extended_frag (fragp, sec, stretch)
12152      fragS *fragp;
12153      asection *sec;
12154      long stretch;
12155 {
12156   int type;
12157   register const struct mips16_immed_operand *op;
12158   offsetT val;
12159   int mintiny, maxtiny;
12160   segT symsec;
12161   fragS *sym_frag;
12162
12163   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12164     return 0;
12165   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12166     return 1;
12167
12168   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12169   op = mips16_immed_operands;
12170   while (op->type != type)
12171     {
12172       ++op;
12173       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12174     }
12175
12176   if (op->unsp)
12177     {
12178       if (type == '<' || type == '>' || type == '[' || type == ']')
12179         {
12180           mintiny = 1;
12181           maxtiny = 1 << op->nbits;
12182         }
12183       else
12184         {
12185           mintiny = 0;
12186           maxtiny = (1 << op->nbits) - 1;
12187         }
12188     }
12189   else
12190     {
12191       mintiny = - (1 << (op->nbits - 1));
12192       maxtiny = (1 << (op->nbits - 1)) - 1;
12193     }
12194
12195   sym_frag = symbol_get_frag (fragp->fr_symbol);
12196   val = S_GET_VALUE (fragp->fr_symbol);
12197   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12198
12199   if (op->pcrel)
12200     {
12201       addressT addr;
12202
12203       /* We won't have the section when we are called from
12204          mips_relax_frag.  However, we will always have been called
12205          from md_estimate_size_before_relax first.  If this is a
12206          branch to a different section, we mark it as such.  If SEC is
12207          NULL, and the frag is not marked, then it must be a branch to
12208          the same section.  */
12209       if (sec == NULL)
12210         {
12211           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12212             return 1;
12213         }
12214       else
12215         {
12216           /* Must have been called from md_estimate_size_before_relax.  */
12217           if (symsec != sec)
12218             {
12219               fragp->fr_subtype =
12220                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12221
12222               /* FIXME: We should support this, and let the linker
12223                  catch branches and loads that are out of range.  */
12224               as_bad_where (fragp->fr_file, fragp->fr_line,
12225                             _("unsupported PC relative reference to different section"));
12226
12227               return 1;
12228             }
12229           if (fragp != sym_frag && sym_frag->fr_address == 0)
12230             /* Assume non-extended on the first relaxation pass.
12231                The address we have calculated will be bogus if this is
12232                a forward branch to another frag, as the forward frag
12233                will have fr_address == 0.  */
12234             return 0;
12235         }
12236
12237       /* In this case, we know for sure that the symbol fragment is in
12238          the same section.  If the relax_marker of the symbol fragment
12239          differs from the relax_marker of this fragment, we have not
12240          yet adjusted the symbol fragment fr_address.  We want to add
12241          in STRETCH in order to get a better estimate of the address.
12242          This particularly matters because of the shift bits.  */
12243       if (stretch != 0
12244           && sym_frag->relax_marker != fragp->relax_marker)
12245         {
12246           fragS *f;
12247
12248           /* Adjust stretch for any alignment frag.  Note that if have
12249              been expanding the earlier code, the symbol may be
12250              defined in what appears to be an earlier frag.  FIXME:
12251              This doesn't handle the fr_subtype field, which specifies
12252              a maximum number of bytes to skip when doing an
12253              alignment.  */
12254           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12255             {
12256               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12257                 {
12258                   if (stretch < 0)
12259                     stretch = - ((- stretch)
12260                                  & ~ ((1 << (int) f->fr_offset) - 1));
12261                   else
12262                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12263                   if (stretch == 0)
12264                     break;
12265                 }
12266             }
12267           if (f != NULL)
12268             val += stretch;
12269         }
12270
12271       addr = fragp->fr_address + fragp->fr_fix;
12272
12273       /* The base address rules are complicated.  The base address of
12274          a branch is the following instruction.  The base address of a
12275          PC relative load or add is the instruction itself, but if it
12276          is in a delay slot (in which case it can not be extended) use
12277          the address of the instruction whose delay slot it is in.  */
12278       if (type == 'p' || type == 'q')
12279         {
12280           addr += 2;
12281
12282           /* If we are currently assuming that this frag should be
12283              extended, then, the current address is two bytes
12284              higher.  */
12285           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12286             addr += 2;
12287
12288           /* Ignore the low bit in the target, since it will be set
12289              for a text label.  */
12290           if ((val & 1) != 0)
12291             --val;
12292         }
12293       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12294         addr -= 4;
12295       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12296         addr -= 2;
12297
12298       val -= addr & ~ ((1 << op->shift) - 1);
12299
12300       /* Branch offsets have an implicit 0 in the lowest bit.  */
12301       if (type == 'p' || type == 'q')
12302         val /= 2;
12303
12304       /* If any of the shifted bits are set, we must use an extended
12305          opcode.  If the address depends on the size of this
12306          instruction, this can lead to a loop, so we arrange to always
12307          use an extended opcode.  We only check this when we are in
12308          the main relaxation loop, when SEC is NULL.  */
12309       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12310         {
12311           fragp->fr_subtype =
12312             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12313           return 1;
12314         }
12315
12316       /* If we are about to mark a frag as extended because the value
12317          is precisely maxtiny + 1, then there is a chance of an
12318          infinite loop as in the following code:
12319              la $4,foo
12320              .skip      1020
12321              .align     2
12322            foo:
12323          In this case when the la is extended, foo is 0x3fc bytes
12324          away, so the la can be shrunk, but then foo is 0x400 away, so
12325          the la must be extended.  To avoid this loop, we mark the
12326          frag as extended if it was small, and is about to become
12327          extended with a value of maxtiny + 1.  */
12328       if (val == ((maxtiny + 1) << op->shift)
12329           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12330           && sec == NULL)
12331         {
12332           fragp->fr_subtype =
12333             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12334           return 1;
12335         }
12336     }
12337   else if (symsec != absolute_section && sec != NULL)
12338     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12339
12340   if ((val & ((1 << op->shift) - 1)) != 0
12341       || val < (mintiny << op->shift)
12342       || val > (maxtiny << op->shift))
12343     return 1;
12344   else
12345     return 0;
12346 }
12347
12348 /* Estimate the size of a frag before relaxing.  Unless this is the
12349    mips16, we are not really relaxing here, and the final size is
12350    encoded in the subtype information.  For the mips16, we have to
12351    decide whether we are using an extended opcode or not.  */
12352
12353 int
12354 md_estimate_size_before_relax (fragp, segtype)
12355      fragS *fragp;
12356      asection *segtype;
12357 {
12358   int change = 0;
12359   boolean linkonce = false;
12360
12361   if (RELAX_MIPS16_P (fragp->fr_subtype))
12362     /* We don't want to modify the EXTENDED bit here; it might get us
12363        into infinite loops.  We change it only in mips_relax_frag().  */
12364     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12365
12366   if (mips_pic == NO_PIC)
12367     {
12368       change = nopic_need_relax (fragp->fr_symbol, 0);
12369     }
12370   else if (mips_pic == SVR4_PIC)
12371     {
12372       symbolS *sym;
12373       asection *symsec;
12374
12375       sym = fragp->fr_symbol;
12376
12377       /* Handle the case of a symbol equated to another symbol.  */
12378       while (symbol_equated_reloc_p (sym))
12379         {
12380           symbolS *n;
12381
12382           /* It's possible to get a loop here in a badly written
12383              program.  */
12384           n = symbol_get_value_expression (sym)->X_add_symbol;
12385           if (n == sym)
12386             break;
12387           sym = n;
12388         }
12389
12390       symsec = S_GET_SEGMENT (sym);
12391
12392       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12393       if (symsec != segtype && ! S_IS_LOCAL (sym))
12394         {
12395           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12396               != 0)
12397             linkonce = true;
12398
12399           /* The GNU toolchain uses an extension for ELF: a section
12400              beginning with the magic string .gnu.linkonce is a linkonce
12401              section.  */
12402           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12403                        sizeof ".gnu.linkonce" - 1) == 0)
12404             linkonce = true;
12405         }
12406
12407       /* This must duplicate the test in adjust_reloc_syms.  */
12408       change = (symsec != &bfd_und_section
12409                 && symsec != &bfd_abs_section
12410                 && ! bfd_is_com_section (symsec)
12411                 && !linkonce
12412 #ifdef OBJ_ELF
12413                 /* A global or weak symbol is treated as external.  */
12414                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12415                     || (! S_IS_WEAK (sym)
12416                         && (! S_IS_EXTERNAL (sym)
12417                             || mips_pic == EMBEDDED_PIC)))
12418 #endif
12419                 );
12420     }
12421   else
12422     abort ();
12423
12424   if (change)
12425     {
12426       /* Record the offset to the first reloc in the fr_opcode field.
12427          This lets md_convert_frag and tc_gen_reloc know that the code
12428          must be expanded.  */
12429       fragp->fr_opcode = (fragp->fr_literal
12430                           + fragp->fr_fix
12431                           - RELAX_OLD (fragp->fr_subtype)
12432                           + RELAX_RELOC1 (fragp->fr_subtype));
12433       /* FIXME: This really needs as_warn_where.  */
12434       if (RELAX_WARN (fragp->fr_subtype))
12435         as_warn (_("AT used after \".set noat\" or macro used after "
12436                    "\".set nomacro\""));
12437
12438       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12439     }
12440
12441   return 0;
12442 }
12443
12444 /* This is called to see whether a reloc against a defined symbol
12445    should be converted into a reloc against a section.  Don't adjust
12446    MIPS16 jump relocations, so we don't have to worry about the format
12447    of the offset in the .o file.  Don't adjust relocations against
12448    mips16 symbols, so that the linker can find them if it needs to set
12449    up a stub.  */
12450
12451 int
12452 mips_fix_adjustable (fixp)
12453      fixS *fixp;
12454 {
12455 #ifdef OBJ_ELF
12456   /* Prevent all adjustments to global symbols.  */
12457   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12458       && mips_pic != EMBEDDED_PIC
12459       && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12460     return 0;
12461 #endif
12462   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12463     return 0;
12464   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12465       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12466     return 0;
12467   if (fixp->fx_addsy == NULL)
12468     return 1;
12469 #ifdef OBJ_ELF
12470   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12471       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12472       && fixp->fx_subsy == NULL)
12473     return 0;
12474 #endif
12475   return 1;
12476 }
12477
12478 /* Translate internal representation of relocation info to BFD target
12479    format.  */
12480
12481 arelent **
12482 tc_gen_reloc (section, fixp)
12483      asection *section ATTRIBUTE_UNUSED;
12484      fixS *fixp;
12485 {
12486   static arelent *retval[4];
12487   arelent *reloc;
12488   bfd_reloc_code_real_type code;
12489
12490   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12491   retval[1] = NULL;
12492
12493   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12494   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12495   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12496
12497   if (mips_pic == EMBEDDED_PIC
12498       && SWITCH_TABLE (fixp))
12499     {
12500       /* For a switch table entry we use a special reloc.  The addend
12501          is actually the difference between the reloc address and the
12502          subtrahend.  */
12503       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12504       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12505         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12506       fixp->fx_r_type = BFD_RELOC_GPREL32;
12507     }
12508   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12509     {
12510       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12511         reloc->addend = fixp->fx_addnumber;
12512       else
12513         {
12514           /* We use a special addend for an internal RELLO reloc.  */
12515           if (symbol_section_p (fixp->fx_addsy))
12516             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12517           else
12518             reloc->addend = fixp->fx_addnumber + reloc->address;
12519         }
12520     }
12521   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12522     {
12523       assert (fixp->fx_next != NULL
12524               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12525
12526       /* The reloc is relative to the RELLO; adjust the addend
12527          accordingly.  */
12528       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12529         reloc->addend = fixp->fx_next->fx_addnumber;
12530       else
12531         {
12532           /* We use a special addend for an internal RELHI reloc.  */
12533           if (symbol_section_p (fixp->fx_addsy))
12534             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12535                              + fixp->fx_next->fx_where
12536                              - S_GET_VALUE (fixp->fx_subsy));
12537           else
12538             reloc->addend = (fixp->fx_addnumber
12539                              + fixp->fx_next->fx_frag->fr_address
12540                              + fixp->fx_next->fx_where);
12541         }
12542     }
12543   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12544     reloc->addend = fixp->fx_addnumber;
12545   else
12546     {
12547       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12548         /* A gruesome hack which is a result of the gruesome gas reloc
12549            handling.  */
12550         reloc->addend = reloc->address;
12551       else
12552         reloc->addend = -reloc->address;
12553     }
12554
12555   /* If this is a variant frag, we may need to adjust the existing
12556      reloc and generate a new one.  */
12557   if (fixp->fx_frag->fr_opcode != NULL
12558       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12559           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12560           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12561           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12562           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12563           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12564           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12565       && ! HAVE_NEWABI)
12566     {
12567       arelent *reloc2;
12568
12569       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12570
12571       /* If this is not the last reloc in this frag, then we have two
12572          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12573          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12574          the second one handle all of them.  */
12575       if (fixp->fx_next != NULL
12576           && fixp->fx_frag == fixp->fx_next->fx_frag)
12577         {
12578           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12579                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12580                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12581                       && (fixp->fx_next->fx_r_type
12582                           == BFD_RELOC_MIPS_GOT_LO16))
12583                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12584                       && (fixp->fx_next->fx_r_type
12585                           == BFD_RELOC_MIPS_CALL_LO16)));
12586           retval[0] = NULL;
12587           return retval;
12588         }
12589
12590       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12591       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12592       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12593       retval[2] = NULL;
12594       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12595       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12596       reloc2->address = (reloc->address
12597                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12598                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12599       reloc2->addend = fixp->fx_addnumber;
12600       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12601       assert (reloc2->howto != NULL);
12602
12603       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12604         {
12605           arelent *reloc3;
12606
12607           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12608           retval[3] = NULL;
12609           *reloc3 = *reloc2;
12610           reloc3->address += 4;
12611         }
12612
12613       if (mips_pic == NO_PIC)
12614         {
12615           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12616           fixp->fx_r_type = BFD_RELOC_HI16_S;
12617         }
12618       else if (mips_pic == SVR4_PIC)
12619         {
12620           switch (fixp->fx_r_type)
12621             {
12622             default:
12623               abort ();
12624             case BFD_RELOC_MIPS_GOT16:
12625               break;
12626             case BFD_RELOC_MIPS_CALL16:
12627             case BFD_RELOC_MIPS_GOT_LO16:
12628             case BFD_RELOC_MIPS_CALL_LO16:
12629               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12630               break;
12631             }
12632         }
12633       else
12634         abort ();
12635     }
12636
12637   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12638      entry to be used in the relocation's section offset.  */
12639   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12640     {
12641       reloc->address = reloc->addend;
12642       reloc->addend = 0;
12643     }
12644
12645   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12646      fixup_segment converted a non-PC relative reloc into a PC
12647      relative reloc.  In such a case, we need to convert the reloc
12648      code.  */
12649   code = fixp->fx_r_type;
12650   if (fixp->fx_pcrel)
12651     {
12652       switch (code)
12653         {
12654         case BFD_RELOC_8:
12655           code = BFD_RELOC_8_PCREL;
12656           break;
12657         case BFD_RELOC_16:
12658           code = BFD_RELOC_16_PCREL;
12659           break;
12660         case BFD_RELOC_32:
12661           code = BFD_RELOC_32_PCREL;
12662           break;
12663         case BFD_RELOC_64:
12664           code = BFD_RELOC_64_PCREL;
12665           break;
12666         case BFD_RELOC_8_PCREL:
12667         case BFD_RELOC_16_PCREL:
12668         case BFD_RELOC_32_PCREL:
12669         case BFD_RELOC_64_PCREL:
12670         case BFD_RELOC_16_PCREL_S2:
12671         case BFD_RELOC_PCREL_HI16_S:
12672         case BFD_RELOC_PCREL_LO16:
12673           break;
12674         default:
12675           as_bad_where (fixp->fx_file, fixp->fx_line,
12676                         _("Cannot make %s relocation PC relative"),
12677                         bfd_get_reloc_code_name (code));
12678         }
12679     }
12680
12681 #ifdef OBJ_ELF
12682   /* md_apply_fix3 has a double-subtraction hack to get
12683      bfd_install_relocation to behave nicely.  GPREL relocations are
12684      handled correctly without this hack, so undo it here.  We can't
12685      stop md_apply_fix3 from subtracting twice in the first place since
12686      the fake addend is required for variant frags above.  */
12687   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12688       && code == BFD_RELOC_GPREL16
12689       && reloc->addend != 0
12690       && mips_need_elf_addend_fixup (fixp))
12691     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12692 #endif
12693
12694   /* To support a PC relative reloc when generating embedded PIC code
12695      for ECOFF, we use a Cygnus extension.  We check for that here to
12696      make sure that we don't let such a reloc escape normally.  */
12697   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12698        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12699       && code == BFD_RELOC_16_PCREL_S2
12700       && mips_pic != EMBEDDED_PIC)
12701     reloc->howto = NULL;
12702   else
12703     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12704
12705   if (reloc->howto == NULL)
12706     {
12707       as_bad_where (fixp->fx_file, fixp->fx_line,
12708                     _("Can not represent %s relocation in this object file format"),
12709                     bfd_get_reloc_code_name (code));
12710       retval[0] = NULL;
12711     }
12712
12713   return retval;
12714 }
12715
12716 /* Relax a machine dependent frag.  This returns the amount by which
12717    the current size of the frag should change.  */
12718
12719 int
12720 mips_relax_frag (fragp, stretch)
12721      fragS *fragp;
12722      long stretch;
12723 {
12724   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12725     return 0;
12726
12727   if (mips16_extended_frag (fragp, NULL, stretch))
12728     {
12729       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12730         return 0;
12731       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12732       return 2;
12733     }
12734   else
12735     {
12736       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12737         return 0;
12738       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12739       return -2;
12740     }
12741
12742   return 0;
12743 }
12744
12745 /* Convert a machine dependent frag.  */
12746
12747 void
12748 md_convert_frag (abfd, asec, fragp)
12749      bfd *abfd ATTRIBUTE_UNUSED;
12750      segT asec;
12751      fragS *fragp;
12752 {
12753   int old, new;
12754   char *fixptr;
12755
12756   if (RELAX_MIPS16_P (fragp->fr_subtype))
12757     {
12758       int type;
12759       register const struct mips16_immed_operand *op;
12760       boolean small, ext;
12761       offsetT val;
12762       bfd_byte *buf;
12763       unsigned long insn;
12764       boolean use_extend;
12765       unsigned short extend;
12766
12767       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12768       op = mips16_immed_operands;
12769       while (op->type != type)
12770         ++op;
12771
12772       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12773         {
12774           small = false;
12775           ext = true;
12776         }
12777       else
12778         {
12779           small = true;
12780           ext = false;
12781         }
12782
12783       resolve_symbol_value (fragp->fr_symbol);
12784       val = S_GET_VALUE (fragp->fr_symbol);
12785       if (op->pcrel)
12786         {
12787           addressT addr;
12788
12789           addr = fragp->fr_address + fragp->fr_fix;
12790
12791           /* The rules for the base address of a PC relative reloc are
12792              complicated; see mips16_extended_frag.  */
12793           if (type == 'p' || type == 'q')
12794             {
12795               addr += 2;
12796               if (ext)
12797                 addr += 2;
12798               /* Ignore the low bit in the target, since it will be
12799                  set for a text label.  */
12800               if ((val & 1) != 0)
12801                 --val;
12802             }
12803           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12804             addr -= 4;
12805           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12806             addr -= 2;
12807
12808           addr &= ~ (addressT) ((1 << op->shift) - 1);
12809           val -= addr;
12810
12811           /* Make sure the section winds up with the alignment we have
12812              assumed.  */
12813           if (op->shift > 0)
12814             record_alignment (asec, op->shift);
12815         }
12816
12817       if (ext
12818           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12819               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12820         as_warn_where (fragp->fr_file, fragp->fr_line,
12821                        _("extended instruction in delay slot"));
12822
12823       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12824
12825       if (target_big_endian)
12826         insn = bfd_getb16 (buf);
12827       else
12828         insn = bfd_getl16 (buf);
12829
12830       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12831                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12832                     small, ext, &insn, &use_extend, &extend);
12833
12834       if (use_extend)
12835         {
12836           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
12837           fragp->fr_fix += 2;
12838           buf += 2;
12839         }
12840
12841       md_number_to_chars ((char *) buf, insn, 2);
12842       fragp->fr_fix += 2;
12843       buf += 2;
12844     }
12845   else
12846     {
12847       if (fragp->fr_opcode == NULL)
12848         return;
12849
12850       old = RELAX_OLD (fragp->fr_subtype);
12851       new = RELAX_NEW (fragp->fr_subtype);
12852       fixptr = fragp->fr_literal + fragp->fr_fix;
12853
12854       if (new > 0)
12855         memcpy (fixptr - old, fixptr, new);
12856
12857       fragp->fr_fix += new - old;
12858     }
12859 }
12860
12861 #ifdef OBJ_ELF
12862
12863 /* This function is called after the relocs have been generated.
12864    We've been storing mips16 text labels as odd.  Here we convert them
12865    back to even for the convenience of the debugger.  */
12866
12867 void
12868 mips_frob_file_after_relocs ()
12869 {
12870   asymbol **syms;
12871   unsigned int count, i;
12872
12873   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12874     return;
12875
12876   syms = bfd_get_outsymbols (stdoutput);
12877   count = bfd_get_symcount (stdoutput);
12878   for (i = 0; i < count; i++, syms++)
12879     {
12880       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12881           && ((*syms)->value & 1) != 0)
12882         {
12883           (*syms)->value &= ~1;
12884           /* If the symbol has an odd size, it was probably computed
12885              incorrectly, so adjust that as well.  */
12886           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12887             ++elf_symbol (*syms)->internal_elf_sym.st_size;
12888         }
12889     }
12890 }
12891
12892 #endif
12893
12894 /* This function is called whenever a label is defined.  It is used
12895    when handling branch delays; if a branch has a label, we assume we
12896    can not move it.  */
12897
12898 void
12899 mips_define_label (sym)
12900      symbolS *sym;
12901 {
12902   struct insn_label_list *l;
12903
12904   if (free_insn_labels == NULL)
12905     l = (struct insn_label_list *) xmalloc (sizeof *l);
12906   else
12907     {
12908       l = free_insn_labels;
12909       free_insn_labels = l->next;
12910     }
12911
12912   l->label = sym;
12913   l->next = insn_labels;
12914   insn_labels = l;
12915 }
12916 \f
12917 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12918
12919 /* Some special processing for a MIPS ELF file.  */
12920
12921 void
12922 mips_elf_final_processing ()
12923 {
12924   /* Write out the register information.  */
12925   if (mips_abi != N64_ABI)
12926     {
12927       Elf32_RegInfo s;
12928
12929       s.ri_gprmask = mips_gprmask;
12930       s.ri_cprmask[0] = mips_cprmask[0];
12931       s.ri_cprmask[1] = mips_cprmask[1];
12932       s.ri_cprmask[2] = mips_cprmask[2];
12933       s.ri_cprmask[3] = mips_cprmask[3];
12934       /* The gp_value field is set by the MIPS ELF backend.  */
12935
12936       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12937                                        ((Elf32_External_RegInfo *)
12938                                         mips_regmask_frag));
12939     }
12940   else
12941     {
12942       Elf64_Internal_RegInfo s;
12943
12944       s.ri_gprmask = mips_gprmask;
12945       s.ri_pad = 0;
12946       s.ri_cprmask[0] = mips_cprmask[0];
12947       s.ri_cprmask[1] = mips_cprmask[1];
12948       s.ri_cprmask[2] = mips_cprmask[2];
12949       s.ri_cprmask[3] = mips_cprmask[3];
12950       /* The gp_value field is set by the MIPS ELF backend.  */
12951
12952       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12953                                        ((Elf64_External_RegInfo *)
12954                                         mips_regmask_frag));
12955     }
12956
12957   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
12958      sort of BFD interface for this.  */
12959   if (mips_any_noreorder)
12960     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12961   if (mips_pic != NO_PIC)
12962     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12963
12964   /* Set MIPS ELF flags for ASEs.  */
12965   if (file_ase_mips16)
12966     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
12967 #if 0 /* XXX FIXME */
12968   if (file_ase_mips3d)
12969     elf_elfheader (stdoutput)->e_flags |= ???;
12970 #endif
12971   if (file_ase_mdmx)
12972     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
12973
12974   /* Set the MIPS ELF ABI flags.  */
12975   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
12976     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
12977   else if (mips_abi == O64_ABI)
12978     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
12979   else if (mips_abi == EABI_ABI)
12980     {
12981       if (!file_mips_gp32)
12982         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12983       else
12984         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12985     }
12986   else if (mips_abi == N32_ABI)
12987     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12988
12989   /* Nothing to do for N64_ABI.  */
12990
12991   if (mips_32bitmode)
12992     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12993 }
12994
12995 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12996 \f
12997 typedef struct proc {
12998   symbolS *isym;
12999   unsigned long reg_mask;
13000   unsigned long reg_offset;
13001   unsigned long fpreg_mask;
13002   unsigned long fpreg_offset;
13003   unsigned long frame_offset;
13004   unsigned long frame_reg;
13005   unsigned long pc_reg;
13006 } procS;
13007
13008 static procS cur_proc;
13009 static procS *cur_proc_ptr;
13010 static int numprocs;
13011
13012 /* Fill in an rs_align_code fragment.  */
13013
13014 void
13015 mips_handle_align (fragp)
13016      fragS *fragp;
13017 {
13018   if (fragp->fr_type != rs_align_code)
13019     return;
13020
13021   if (mips_opts.mips16)
13022     {
13023       static const unsigned char be_nop[] = { 0x65, 0x00 };
13024       static const unsigned char le_nop[] = { 0x00, 0x65 };
13025
13026       int bytes;
13027       char *p;
13028
13029       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13030       p = fragp->fr_literal + fragp->fr_fix;
13031
13032       if (bytes & 1)
13033         {
13034           *p++ = 0;
13035           fragp->fr_fix++;
13036         }
13037
13038       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13039       fragp->fr_var = 2;
13040     }
13041
13042   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13043 }
13044
13045 static void
13046 md_obj_begin ()
13047 {
13048 }
13049
13050 static void
13051 md_obj_end ()
13052 {
13053   /* check for premature end, nesting errors, etc */
13054   if (cur_proc_ptr)
13055     as_warn (_("missing .end at end of assembly"));
13056 }
13057
13058 static long
13059 get_number ()
13060 {
13061   int negative = 0;
13062   long val = 0;
13063
13064   if (*input_line_pointer == '-')
13065     {
13066       ++input_line_pointer;
13067       negative = 1;
13068     }
13069   if (!ISDIGIT (*input_line_pointer))
13070     as_bad (_("expected simple number"));
13071   if (input_line_pointer[0] == '0')
13072     {
13073       if (input_line_pointer[1] == 'x')
13074         {
13075           input_line_pointer += 2;
13076           while (ISXDIGIT (*input_line_pointer))
13077             {
13078               val <<= 4;
13079               val |= hex_value (*input_line_pointer++);
13080             }
13081           return negative ? -val : val;
13082         }
13083       else
13084         {
13085           ++input_line_pointer;
13086           while (ISDIGIT (*input_line_pointer))
13087             {
13088               val <<= 3;
13089               val |= *input_line_pointer++ - '0';
13090             }
13091           return negative ? -val : val;
13092         }
13093     }
13094   if (!ISDIGIT (*input_line_pointer))
13095     {
13096       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13097               *input_line_pointer, *input_line_pointer);
13098       as_warn (_("invalid number"));
13099       return -1;
13100     }
13101   while (ISDIGIT (*input_line_pointer))
13102     {
13103       val *= 10;
13104       val += *input_line_pointer++ - '0';
13105     }
13106   return negative ? -val : val;
13107 }
13108
13109 /* The .file directive; just like the usual .file directive, but there
13110    is an initial number which is the ECOFF file index.  In the non-ECOFF
13111    case .file implies DWARF-2.  */
13112
13113 static void
13114 s_mips_file (x)
13115      int x ATTRIBUTE_UNUSED;
13116 {
13117   static int first_file_directive = 0;
13118
13119   if (ECOFF_DEBUGGING)
13120     {
13121       get_number ();
13122       s_app_file (0);
13123     }
13124   else
13125     {
13126       char *filename;
13127
13128       filename = dwarf2_directive_file (0);
13129
13130       /* Versions of GCC up to 3.1 start files with a ".file"
13131          directive even for stabs output.  Make sure that this
13132          ".file" is handled.  Note that you need a version of GCC
13133          after 3.1 in order to support DWARF-2 on MIPS.  */
13134       if (filename != NULL && ! first_file_directive)
13135         {
13136           (void) new_logical_line (filename, -1);
13137           s_app_file_string (filename);
13138         }
13139       first_file_directive = 1;
13140     }
13141 }
13142
13143 /* The .loc directive, implying DWARF-2.  */
13144
13145 static void
13146 s_mips_loc (x)
13147      int x ATTRIBUTE_UNUSED;
13148 {
13149   if (!ECOFF_DEBUGGING)
13150     dwarf2_directive_loc (0);
13151 }
13152
13153 /* The .end directive.  */
13154
13155 static void
13156 s_mips_end (x)
13157      int x ATTRIBUTE_UNUSED;
13158 {
13159   symbolS *p;
13160   int maybe_text;
13161
13162   /* Following functions need their own .frame and .cprestore directives.  */
13163   mips_frame_reg_valid = 0;
13164   mips_cprestore_valid = 0;
13165
13166   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13167     {
13168       p = get_symbol ();
13169       demand_empty_rest_of_line ();
13170     }
13171   else
13172     p = NULL;
13173
13174 #ifdef BFD_ASSEMBLER
13175   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13176     maybe_text = 1;
13177   else
13178     maybe_text = 0;
13179 #else
13180   if (now_seg != data_section && now_seg != bss_section)
13181     maybe_text = 1;
13182   else
13183     maybe_text = 0;
13184 #endif
13185
13186   if (!maybe_text)
13187     as_warn (_(".end not in text section"));
13188
13189   if (!cur_proc_ptr)
13190     {
13191       as_warn (_(".end directive without a preceding .ent directive."));
13192       demand_empty_rest_of_line ();
13193       return;
13194     }
13195
13196   if (p != NULL)
13197     {
13198       assert (S_GET_NAME (p));
13199       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13200         as_warn (_(".end symbol does not match .ent symbol."));
13201
13202       if (debug_type == DEBUG_STABS)
13203         stabs_generate_asm_endfunc (S_GET_NAME (p),
13204                                     S_GET_NAME (p));
13205     }
13206   else
13207     as_warn (_(".end directive missing or unknown symbol"));
13208
13209 #ifdef OBJ_ELF
13210   /* Generate a .pdr section.  */
13211   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13212     {
13213       segT saved_seg = now_seg;
13214       subsegT saved_subseg = now_subseg;
13215       valueT dot;
13216       expressionS exp;
13217       char *fragp;
13218
13219       dot = frag_now_fix ();
13220
13221 #ifdef md_flush_pending_output
13222       md_flush_pending_output ();
13223 #endif
13224
13225       assert (pdr_seg);
13226       subseg_set (pdr_seg, 0);
13227
13228       /* Write the symbol.  */
13229       exp.X_op = O_symbol;
13230       exp.X_add_symbol = p;
13231       exp.X_add_number = 0;
13232       emit_expr (&exp, 4);
13233
13234       fragp = frag_more (7 * 4);
13235
13236       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13237       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13238       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13239       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13240       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13241       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13242       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13243
13244       subseg_set (saved_seg, saved_subseg);
13245     }
13246 #endif /* OBJ_ELF */
13247
13248   cur_proc_ptr = NULL;
13249 }
13250
13251 /* The .aent and .ent directives.  */
13252
13253 static void
13254 s_mips_ent (aent)
13255      int aent;
13256 {
13257   symbolS *symbolP;
13258   int maybe_text;
13259
13260   symbolP = get_symbol ();
13261   if (*input_line_pointer == ',')
13262     ++input_line_pointer;
13263   SKIP_WHITESPACE ();
13264   if (ISDIGIT (*input_line_pointer)
13265       || *input_line_pointer == '-')
13266     get_number ();
13267
13268 #ifdef BFD_ASSEMBLER
13269   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13270     maybe_text = 1;
13271   else
13272     maybe_text = 0;
13273 #else
13274   if (now_seg != data_section && now_seg != bss_section)
13275     maybe_text = 1;
13276   else
13277     maybe_text = 0;
13278 #endif
13279
13280   if (!maybe_text)
13281     as_warn (_(".ent or .aent not in text section."));
13282
13283   if (!aent && cur_proc_ptr)
13284     as_warn (_("missing .end"));
13285
13286   if (!aent)
13287     {
13288       /* This function needs its own .frame and .cprestore directives.  */
13289       mips_frame_reg_valid = 0;
13290       mips_cprestore_valid = 0;
13291
13292       cur_proc_ptr = &cur_proc;
13293       memset (cur_proc_ptr, '\0', sizeof (procS));
13294
13295       cur_proc_ptr->isym = symbolP;
13296
13297       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13298
13299       ++numprocs;
13300
13301       if (debug_type == DEBUG_STABS)
13302         stabs_generate_asm_func (S_GET_NAME (symbolP),
13303                                  S_GET_NAME (symbolP));
13304     }
13305
13306   demand_empty_rest_of_line ();
13307 }
13308
13309 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13310    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13311    s_mips_frame is used so that we can set the PDR information correctly.
13312    We can't use the ecoff routines because they make reference to the ecoff
13313    symbol table (in the mdebug section).  */
13314
13315 static void
13316 s_mips_frame (ignore)
13317      int ignore ATTRIBUTE_UNUSED;
13318 {
13319 #ifdef OBJ_ELF
13320   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13321     {
13322       long val;
13323
13324       if (cur_proc_ptr == (procS *) NULL)
13325         {
13326           as_warn (_(".frame outside of .ent"));
13327           demand_empty_rest_of_line ();
13328           return;
13329         }
13330
13331       cur_proc_ptr->frame_reg = tc_get_register (1);
13332
13333       SKIP_WHITESPACE ();
13334       if (*input_line_pointer++ != ','
13335           || get_absolute_expression_and_terminator (&val) != ',')
13336         {
13337           as_warn (_("Bad .frame directive"));
13338           --input_line_pointer;
13339           demand_empty_rest_of_line ();
13340           return;
13341         }
13342
13343       cur_proc_ptr->frame_offset = val;
13344       cur_proc_ptr->pc_reg = tc_get_register (0);
13345
13346       demand_empty_rest_of_line ();
13347     }
13348   else
13349 #endif /* OBJ_ELF */
13350     s_ignore (ignore);
13351 }
13352
13353 /* The .fmask and .mask directives. If the mdebug section is present
13354    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13355    embedded targets, s_mips_mask is used so that we can set the PDR
13356    information correctly. We can't use the ecoff routines because they
13357    make reference to the ecoff symbol table (in the mdebug section).  */
13358
13359 static void
13360 s_mips_mask (reg_type)
13361      char reg_type;
13362 {
13363 #ifdef OBJ_ELF
13364   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13365     {
13366       long mask, off;
13367
13368       if (cur_proc_ptr == (procS *) NULL)
13369         {
13370           as_warn (_(".mask/.fmask outside of .ent"));
13371           demand_empty_rest_of_line ();
13372           return;
13373         }
13374
13375       if (get_absolute_expression_and_terminator (&mask) != ',')
13376         {
13377           as_warn (_("Bad .mask/.fmask directive"));
13378           --input_line_pointer;
13379           demand_empty_rest_of_line ();
13380           return;
13381         }
13382
13383       off = get_absolute_expression ();
13384
13385       if (reg_type == 'F')
13386         {
13387           cur_proc_ptr->fpreg_mask = mask;
13388           cur_proc_ptr->fpreg_offset = off;
13389         }
13390       else
13391         {
13392           cur_proc_ptr->reg_mask = mask;
13393           cur_proc_ptr->reg_offset = off;
13394         }
13395
13396       demand_empty_rest_of_line ();
13397     }
13398   else
13399 #endif /* OBJ_ELF */
13400     s_ignore (reg_type);
13401 }
13402
13403 /* The .loc directive.  */
13404
13405 #if 0
13406 static void
13407 s_loc (x)
13408      int x;
13409 {
13410   symbolS *symbolP;
13411   int lineno;
13412   int addroff;
13413
13414   assert (now_seg == text_section);
13415
13416   lineno = get_number ();
13417   addroff = frag_now_fix ();
13418
13419   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13420   S_SET_TYPE (symbolP, N_SLINE);
13421   S_SET_OTHER (symbolP, 0);
13422   S_SET_DESC (symbolP, lineno);
13423   symbolP->sy_segment = now_seg;
13424 }
13425 #endif
13426
13427 /* A table describing all the processors gas knows about.  Names are
13428    matched in the order listed.
13429
13430    To ease comparison, please keep this table in the same order as
13431    gcc's mips_cpu_info_table[].  */
13432 static const struct mips_cpu_info mips_cpu_info_table[] =
13433 {
13434   /* Entries for generic ISAs */
13435   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
13436   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
13437   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
13438   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
13439   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
13440   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
13441   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
13442
13443   /* MIPS I */
13444   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
13445   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
13446   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
13447
13448   /* MIPS II */
13449   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
13450
13451   /* MIPS III */
13452   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
13453   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
13454   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
13455   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
13456   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
13457   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
13458   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
13459   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
13460   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
13461
13462   /* MIPS IV */
13463   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
13464   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
13465   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
13466   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
13467   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
13468   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
13469   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
13470   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
13471   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
13472   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
13473
13474   /* MIPS 32 */
13475   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13476   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
13477   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
13478
13479   /* MIPS 64 */
13480   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
13481   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
13482
13483   /* Broadcom SB-1 CPU */
13484   { "SB-1",           0,      ISA_MIPS64,     CPU_SB1 },
13485   { "sb-1250",        0,      ISA_MIPS64,     CPU_SB1 },
13486   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
13487   { "sb1250",         0,      ISA_MIPS64,     CPU_SB1 },
13488
13489   /* End marker */
13490   { NULL, 0, 0, 0 }
13491 };
13492
13493
13494 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13495    with a final "000" replaced by "k".  Ignore case.
13496
13497    Note: this function is shared between GCC and GAS.  */
13498
13499 static boolean
13500 mips_strict_matching_cpu_name_p (canonical, given)
13501      const char *canonical, *given;
13502 {
13503   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13504     given++, canonical++;
13505
13506   return ((*given == 0 && *canonical == 0)
13507           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13508 }
13509
13510
13511 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13512    CPU name.  We've traditionally allowed a lot of variation here.
13513
13514    Note: this function is shared between GCC and GAS.  */
13515
13516 static boolean
13517 mips_matching_cpu_name_p (canonical, given)
13518      const char *canonical, *given;
13519 {
13520   /* First see if the name matches exactly, or with a final "000"
13521      turned into "k".  */
13522   if (mips_strict_matching_cpu_name_p (canonical, given))
13523     return true;
13524
13525   /* If not, try comparing based on numerical designation alone.
13526      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
13527   if (TOLOWER (*given) == 'r')
13528     given++;
13529   if (!ISDIGIT (*given))
13530     return false;
13531
13532   /* Skip over some well-known prefixes in the canonical name,
13533      hoping to find a number there too.  */
13534   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13535     canonical += 2;
13536   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13537     canonical += 2;
13538   else if (TOLOWER (canonical[0]) == 'r')
13539     canonical += 1;
13540
13541   return mips_strict_matching_cpu_name_p (canonical, given);
13542 }
13543
13544
13545 /* Parse an option that takes the name of a processor as its argument.
13546    OPTION is the name of the option and CPU_STRING is the argument.
13547    Return the corresponding processor enumeration if the CPU_STRING is
13548    recognized, otherwise report an error and return null.
13549
13550    A similar function exists in GCC.  */
13551
13552 static const struct mips_cpu_info *
13553 mips_parse_cpu (option, cpu_string)
13554      const char *option, *cpu_string;
13555 {
13556   const struct mips_cpu_info *p;
13557
13558   /* 'from-abi' selects the most compatible architecture for the given
13559      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
13560      EABIs, we have to decide whether we're using the 32-bit or 64-bit
13561      version.  Look first at the -mgp options, if given, otherwise base
13562      the choice on MIPS_DEFAULT_64BIT.
13563
13564      Treat NO_ABI like the EABIs.  One reason to do this is that the
13565      plain 'mips' and 'mips64' configs have 'from-abi' as their default
13566      architecture.  This code picks MIPS I for 'mips' and MIPS III for
13567      'mips64', just as we did in the days before 'from-abi'.  */
13568   if (strcasecmp (cpu_string, "from-abi") == 0)
13569     {
13570       if (ABI_NEEDS_32BIT_REGS (mips_abi))
13571         return mips_cpu_info_from_isa (ISA_MIPS1);
13572
13573       if (ABI_NEEDS_64BIT_REGS (mips_abi))
13574         return mips_cpu_info_from_isa (ISA_MIPS3);
13575
13576       if (file_mips_gp32 >= 0)
13577         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13578
13579       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13580                                      ? ISA_MIPS3
13581                                      : ISA_MIPS1);
13582     }
13583
13584   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
13585   if (strcasecmp (cpu_string, "default") == 0)
13586     return 0;
13587
13588   for (p = mips_cpu_info_table; p->name != 0; p++)
13589     if (mips_matching_cpu_name_p (p->name, cpu_string))
13590       return p;
13591
13592   as_bad ("Bad value (%s) for %s", cpu_string, option);
13593   return 0;
13594 }
13595
13596 /* Return the canonical processor information for ISA (a member of the
13597    ISA_MIPS* enumeration).  */
13598
13599 static const struct mips_cpu_info *
13600 mips_cpu_info_from_isa (isa)
13601      int isa;
13602 {
13603   int i;
13604
13605   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13606     if (mips_cpu_info_table[i].is_isa
13607         && isa == mips_cpu_info_table[i].isa)
13608       return (&mips_cpu_info_table[i]);
13609
13610   return NULL;
13611 }
13612 \f
13613 static void
13614 show (stream, string, col_p, first_p)
13615      FILE *stream;
13616      const char *string;
13617      int *col_p;
13618      int *first_p;
13619 {
13620   if (*first_p)
13621     {
13622       fprintf (stream, "%24s", "");
13623       *col_p = 24;
13624     }
13625   else
13626     {
13627       fprintf (stream, ", ");
13628       *col_p += 2;
13629     }
13630
13631   if (*col_p + strlen (string) > 72)
13632     {
13633       fprintf (stream, "\n%24s", "");
13634       *col_p = 24;
13635     }
13636
13637   fprintf (stream, "%s", string);
13638   *col_p += strlen (string);
13639
13640   *first_p = 0;
13641 }
13642
13643 void
13644 md_show_usage (stream)
13645      FILE *stream;
13646 {
13647   int column, first;
13648   size_t i;
13649
13650   fprintf (stream, _("\
13651 MIPS options:\n\
13652 -membedded-pic          generate embedded position independent code\n\
13653 -EB                     generate big endian output\n\
13654 -EL                     generate little endian output\n\
13655 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
13656 -G NUM                  allow referencing objects up to NUM bytes\n\
13657                         implicitly with the gp register [default 8]\n"));
13658   fprintf (stream, _("\
13659 -mips1                  generate MIPS ISA I instructions\n\
13660 -mips2                  generate MIPS ISA II instructions\n\
13661 -mips3                  generate MIPS ISA III instructions\n\
13662 -mips4                  generate MIPS ISA IV instructions\n\
13663 -mips5                  generate MIPS ISA V instructions\n\
13664 -mips32                 generate MIPS32 ISA instructions\n\
13665 -mips64                 generate MIPS64 ISA instructions\n\
13666 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
13667
13668   first = 1;
13669
13670   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13671     show (stream, mips_cpu_info_table[i].name, &column, &first);
13672   show (stream, "from-abi", &column, &first);
13673   fputc ('\n', stream);
13674
13675   fprintf (stream, _("\
13676 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13677 -no-mCPU                don't generate code specific to CPU.\n\
13678                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
13679
13680   first = 1;
13681
13682   show (stream, "3900", &column, &first);
13683   show (stream, "4010", &column, &first);
13684   show (stream, "4100", &column, &first);
13685   show (stream, "4650", &column, &first);
13686   fputc ('\n', stream);
13687
13688   fprintf (stream, _("\
13689 -mips16                 generate mips16 instructions\n\
13690 -no-mips16              do not generate mips16 instructions\n"));
13691   fprintf (stream, _("\
13692 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
13693 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
13694 -O0                     remove unneeded NOPs, do not swap branches\n\
13695 -O                      remove unneeded NOPs and swap branches\n\
13696 -n                      warn about NOPs generated from macros\n\
13697 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
13698 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
13699 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
13700 #ifdef OBJ_ELF
13701   fprintf (stream, _("\
13702 -KPIC, -call_shared     generate SVR4 position independent code\n\
13703 -non_shared             do not generate position independent code\n\
13704 -xgot                   assume a 32 bit GOT\n\
13705 -mabi=ABI               create ABI conformant object file for:\n"));
13706
13707   first = 1;
13708
13709   show (stream, "32", &column, &first);
13710   show (stream, "o64", &column, &first);
13711   show (stream, "n32", &column, &first);
13712   show (stream, "64", &column, &first);
13713   show (stream, "eabi", &column, &first);
13714
13715   fputc ('\n', stream);
13716
13717   fprintf (stream, _("\
13718 -32                     create o32 ABI object file (default)\n\
13719 -n32                    create n32 ABI object file\n\
13720 -64                     create 64 ABI object file\n"));
13721 #endif
13722 }