2002-06-14 H.J. Lu <hjl@gnu.org>
[external/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #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 file_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   /* The ABI currently in use. This is changed by .set mipsN to loosen
181      restrictions and doesn't affect the whole file.  */
182   enum mips_abi_level abi;
183 };
184
185 /* True if -mgp32 was passed.  */
186 static int file_mips_gp32 = -1;
187
188 /* True if -mfp32 was passed.  */
189 static int file_mips_fp32 = -1;
190
191 /* This is the struct we use to hold the current set of options.  Note
192    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
193    -1 to indicate that they have not been initialized.  */
194
195 static struct mips_set_options mips_opts =
196 {
197   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, NO_ABI
198 };
199
200 /* These variables are filled in with the masks of registers used.
201    The object format code reads them and puts them in the appropriate
202    place.  */
203 unsigned long mips_gprmask;
204 unsigned long mips_cprmask[4];
205
206 /* MIPS ISA we are using for this output file.  */
207 static int file_mips_isa = ISA_UNKNOWN;
208
209 /* True if -mips16 was passed or implied by arguments passed on the
210    command line (e.g., by -march).  */
211 static int file_ase_mips16;
212
213 /* True if -mips3d was passed or implied by arguments passed on the
214    command line (e.g., by -march).  */
215 static int file_ase_mips3d;
216
217 /* True if -mdmx was passed or implied by arguments passed on the
218    command line (e.g., by -march).  */
219 static int file_ase_mdmx;
220
221 /* The argument of the -mcpu= flag.  Historical for code generation.  */
222 static int mips_cpu = CPU_UNKNOWN;
223
224 /* The argument of the -march= flag.  The architecture we are assembling.  */
225 static int mips_arch = CPU_UNKNOWN;
226
227 /* The argument of the -mtune= flag.  The architecture for which we
228    are optimizing.  */
229 static int mips_tune = CPU_UNKNOWN;
230
231 /* If they asked for mips1 or mips2 and a cpu that is
232    mips3 or greater, then mark the object file 32BITMODE.  */
233 static int mips_32bitmode = 0;
234
235 /* Some ISA's have delay slots for instructions which read or write
236    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
237    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
238    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
239    delay slot in this ISA.  The uses of this macro assume that any
240    ISA that has delay slots for one of these, has them for all.  They
241    also assume that ISAs which don't have delays for these insns, don't
242    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
243 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
244    (ISA) == ISA_MIPS1                       \
245    || (ISA) == ISA_MIPS2                    \
246    || (ISA) == ISA_MIPS3                    \
247    )
248
249 /*  Return true if ISA supports 64 bit gp register instructions.  */
250 #define ISA_HAS_64BIT_REGS(ISA) (    \
251    (ISA) == ISA_MIPS3                \
252    || (ISA) == ISA_MIPS4             \
253    || (ISA) == ISA_MIPS5             \
254    || (ISA) == ISA_MIPS64            \
255    )
256
257 #define HAVE_32BIT_GPRS                            \
258     (mips_opts.gp32                                \
259      || mips_opts.abi == O32_ABI                   \
260      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
261
262 #define HAVE_32BIT_FPRS                            \
263     (mips_opts.fp32                                \
264      || mips_opts.abi == O32_ABI                   \
265      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
266
267 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
268 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
269
270 #define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
271
272 #define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
273
274 /* We can only have 64bit addresses if the object file format
275    supports it.  */
276 #define HAVE_32BIT_ADDRESSES                           \
277    (HAVE_32BIT_GPRS                                    \
278     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
279          || ! HAVE_64BIT_OBJECTS)                      \
280         && mips_pic != EMBEDDED_PIC))
281
282 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
283
284 /* Return true if the given CPU supports the MIPS16 ASE.  */
285 #define CPU_HAS_MIPS16(cpu)                            \
286    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0)
287
288 /* Return true if the given CPU supports the MIPS3D ASE.  */
289 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
290                                  )
291
292 /* Return true if the given CPU supports the MDMX ASE.  */
293 #define CPU_HAS_MDMX(cpu)       (false                 \
294                                  )
295
296 /* Whether the processor uses hardware interlocks to protect
297    reads from the HI and LO registers, and thus does not
298    require nops to be inserted.  */
299
300 #define hilo_interlocks (mips_arch == CPU_R4010                       \
301                          || mips_arch == CPU_SB1                      \
302                          )
303
304 /* Whether the processor uses hardware interlocks to protect reads
305    from the GPRs, and thus does not require nops to be inserted.  */
306 #define gpr_interlocks \
307   (mips_opts.isa != ISA_MIPS1  \
308    || mips_arch == CPU_R3900)
309
310 /* As with other "interlocks" this is used by hardware that has FP
311    (co-processor) interlocks.  */
312 /* Itbl support may require additional care here.  */
313 #define cop_interlocks (mips_arch == CPU_R4300                        \
314                         || mips_arch == CPU_SB1                       \
315                         )
316
317 /* Is this a mfhi or mflo instruction?  */
318 #define MF_HILO_INSN(PINFO) \
319           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
320
321 /* MIPS PIC level.  */
322
323 enum mips_pic_level
324 {
325   /* Do not generate PIC code.  */
326   NO_PIC,
327
328   /* Generate PIC code as in Irix 4.  This is not implemented, and I'm
329      not sure what it is supposed to do.  */
330   IRIX4_PIC,
331
332   /* Generate PIC code as in the SVR4 MIPS ABI.  */
333   SVR4_PIC,
334
335   /* Generate PIC code without using a global offset table: the data
336      segment has a maximum size of 64K, all data references are off
337      the $gp register, and all text references are PC relative.  This
338      is used on some embedded systems.  */
339   EMBEDDED_PIC
340 };
341
342 static enum mips_pic_level mips_pic;
343
344 /* Warn about all NOPS that the assembler generates.  */
345 static int warn_nops = 0;
346
347 /* 1 if we should generate 32 bit offsets from the $gp register in
348    SVR4_PIC mode.  Currently has no meaning in other modes.  */
349 static int mips_big_got = 0;
350
351 /* 1 if trap instructions should used for overflow rather than break
352    instructions.  */
353 static int mips_trap = 0;
354
355 /* 1 if double width floating point constants should not be constructed
356    by assembling two single width halves into two single width floating
357    point registers which just happen to alias the double width destination
358    register.  On some architectures this aliasing can be disabled by a bit
359    in the status register, and the setting of this bit cannot be determined
360    automatically at assemble time.  */
361 static int mips_disable_float_construction;
362
363 /* Non-zero if any .set noreorder directives were used.  */
364
365 static int mips_any_noreorder;
366
367 /* Non-zero if nops should be inserted when the register referenced in
368    an mfhi/mflo instruction is read in the next two instructions.  */
369 static int mips_7000_hilo_fix;
370
371 /* The size of the small data section.  */
372 static unsigned int g_switch_value = 8;
373 /* Whether the -G option was used.  */
374 static int g_switch_seen = 0;
375
376 #define N_RMASK 0xc4
377 #define N_VFP   0xd4
378
379 /* If we can determine in advance that GP optimization won't be
380    possible, we can skip the relaxation stuff that tries to produce
381    GP-relative references.  This makes delay slot optimization work
382    better.
383
384    This function can only provide a guess, but it seems to work for
385    gcc output.  It needs to guess right for gcc, otherwise gcc
386    will put what it thinks is a GP-relative instruction in a branch
387    delay slot.
388
389    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
390    fixed it for the non-PIC mode.  KR 95/04/07  */
391 static int nopic_need_relax PARAMS ((symbolS *, int));
392
393 /* handle of the OPCODE hash table */
394 static struct hash_control *op_hash = NULL;
395
396 /* The opcode hash table we use for the mips16.  */
397 static struct hash_control *mips16_op_hash = NULL;
398
399 /* This array holds the chars that always start a comment.  If the
400     pre-processor is disabled, these aren't very useful */
401 const char comment_chars[] = "#";
402
403 /* This array holds the chars that only start a comment at the beginning of
404    a line.  If the line seems to have the form '# 123 filename'
405    .line and .file directives will appear in the pre-processed output */
406 /* Note that input_file.c hand checks for '#' at the beginning of the
407    first line of the input file.  This is because the compiler outputs
408    #NO_APP at the beginning of its output.  */
409 /* Also note that C style comments are always supported.  */
410 const char line_comment_chars[] = "#";
411
412 /* This array holds machine specific line separator characters.  */
413 const char line_separator_chars[] = ";";
414
415 /* Chars that can be used to separate mant from exp in floating point nums */
416 const char EXP_CHARS[] = "eE";
417
418 /* Chars that mean this number is a floating point constant */
419 /* As in 0f12.456 */
420 /* or    0d1.2345e12 */
421 const char FLT_CHARS[] = "rRsSfFdDxXpP";
422
423 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
424    changed in read.c .  Ideally it shouldn't have to know about it at all,
425    but nothing is ideal around here.
426  */
427
428 static char *insn_error;
429
430 static int auto_align = 1;
431
432 /* When outputting SVR4 PIC code, the assembler needs to know the
433    offset in the stack frame from which to restore the $gp register.
434    This is set by the .cprestore pseudo-op, and saved in this
435    variable.  */
436 static offsetT mips_cprestore_offset = -1;
437
438 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
439    more optimizations, it can use a register value instead of a memory-saved
440    offset and even an other register than $gp as global pointer.  */
441 static offsetT mips_cpreturn_offset = -1;
442 static int mips_cpreturn_register = -1;
443 static int mips_gp_register = GP;
444 static int mips_gprel_offset = 0;
445
446 /* Whether mips_cprestore_offset has been set in the current function
447    (or whether it has already been warned about, if not).  */
448 static int mips_cprestore_valid = 0;
449
450 /* This is the register which holds the stack frame, as set by the
451    .frame pseudo-op.  This is needed to implement .cprestore.  */
452 static int mips_frame_reg = SP;
453
454 /* Whether mips_frame_reg has been set in the current function
455    (or whether it has already been warned about, if not).  */
456 static int mips_frame_reg_valid = 0;
457
458 /* To output NOP instructions correctly, we need to keep information
459    about the previous two instructions.  */
460
461 /* Whether we are optimizing.  The default value of 2 means to remove
462    unneeded NOPs and swap branch instructions when possible.  A value
463    of 1 means to not swap branches.  A value of 0 means to always
464    insert NOPs.  */
465 static int mips_optimize = 2;
466
467 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
468    equivalent to seeing no -g option at all.  */
469 static int mips_debug = 0;
470
471 /* The previous instruction.  */
472 static struct mips_cl_insn prev_insn;
473
474 /* The instruction before prev_insn.  */
475 static struct mips_cl_insn prev_prev_insn;
476
477 /* If we don't want information for prev_insn or prev_prev_insn, we
478    point the insn_mo field at this dummy integer.  */
479 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
480
481 /* Non-zero if prev_insn is valid.  */
482 static int prev_insn_valid;
483
484 /* The frag for the previous instruction.  */
485 static struct frag *prev_insn_frag;
486
487 /* The offset into prev_insn_frag for the previous instruction.  */
488 static long prev_insn_where;
489
490 /* The reloc type for the previous instruction, if any.  */
491 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
492
493 /* The reloc for the previous instruction, if any.  */
494 static fixS *prev_insn_fixp[3];
495
496 /* Non-zero if the previous instruction was in a delay slot.  */
497 static int prev_insn_is_delay_slot;
498
499 /* Non-zero if the previous instruction was in a .set noreorder.  */
500 static int prev_insn_unreordered;
501
502 /* Non-zero if the previous instruction uses an extend opcode (if
503    mips16).  */
504 static int prev_insn_extended;
505
506 /* Non-zero if the previous previous instruction was in a .set
507    noreorder.  */
508 static int prev_prev_insn_unreordered;
509
510 /* If this is set, it points to a frag holding nop instructions which
511    were inserted before the start of a noreorder section.  If those
512    nops turn out to be unnecessary, the size of the frag can be
513    decreased.  */
514 static fragS *prev_nop_frag;
515
516 /* The number of nop instructions we created in prev_nop_frag.  */
517 static int prev_nop_frag_holds;
518
519 /* The number of nop instructions that we know we need in
520    prev_nop_frag.  */
521 static int prev_nop_frag_required;
522
523 /* The number of instructions we've seen since prev_nop_frag.  */
524 static int prev_nop_frag_since;
525
526 /* For ECOFF and ELF, relocations against symbols are done in two
527    parts, with a HI relocation and a LO relocation.  Each relocation
528    has only 16 bits of space to store an addend.  This means that in
529    order for the linker to handle carries correctly, it must be able
530    to locate both the HI and the LO relocation.  This means that the
531    relocations must appear in order in the relocation table.
532
533    In order to implement this, we keep track of each unmatched HI
534    relocation.  We then sort them so that they immediately precede the
535    corresponding LO relocation.  */
536
537 struct mips_hi_fixup
538 {
539   /* Next HI fixup.  */
540   struct mips_hi_fixup *next;
541   /* This fixup.  */
542   fixS *fixp;
543   /* The section this fixup is in.  */
544   segT seg;
545 };
546
547 /* The list of unmatched HI relocs.  */
548
549 static struct mips_hi_fixup *mips_hi_fixup_list;
550
551 /* Map normal MIPS register numbers to mips16 register numbers.  */
552
553 #define X ILLEGAL_REG
554 static const int mips32_to_16_reg_map[] =
555 {
556   X, X, 2, 3, 4, 5, 6, 7,
557   X, X, X, X, X, X, X, X,
558   0, 1, X, X, X, X, X, X,
559   X, X, X, X, X, X, X, X
560 };
561 #undef X
562
563 /* Map mips16 register numbers to normal MIPS register numbers.  */
564
565 static const unsigned int mips16_to_32_reg_map[] =
566 {
567   16, 17, 2, 3, 4, 5, 6, 7
568 };
569 \f
570 /* Since the MIPS does not have multiple forms of PC relative
571    instructions, we do not have to do relaxing as is done on other
572    platforms.  However, we do have to handle GP relative addressing
573    correctly, which turns out to be a similar problem.
574
575    Every macro that refers to a symbol can occur in (at least) two
576    forms, one with GP relative addressing and one without.  For
577    example, loading a global variable into a register generally uses
578    a macro instruction like this:
579      lw $4,i
580    If i can be addressed off the GP register (this is true if it is in
581    the .sbss or .sdata section, or if it is known to be smaller than
582    the -G argument) this will generate the following instruction:
583      lw $4,i($gp)
584    This instruction will use a GPREL reloc.  If i can not be addressed
585    off the GP register, the following instruction sequence will be used:
586      lui $at,i
587      lw $4,i($at)
588    In this case the first instruction will have a HI16 reloc, and the
589    second reloc will have a LO16 reloc.  Both relocs will be against
590    the symbol i.
591
592    The issue here is that we may not know whether i is GP addressable
593    until after we see the instruction that uses it.  Therefore, we
594    want to be able to choose the final instruction sequence only at
595    the end of the assembly.  This is similar to the way other
596    platforms choose the size of a PC relative instruction only at the
597    end of assembly.
598
599    When generating position independent code we do not use GP
600    addressing in quite the same way, but the issue still arises as
601    external symbols and local symbols must be handled differently.
602
603    We handle these issues by actually generating both possible
604    instruction sequences.  The longer one is put in a frag_var with
605    type rs_machine_dependent.  We encode what to do with the frag in
606    the subtype field.  We encode (1) the number of existing bytes to
607    replace, (2) the number of new bytes to use, (3) the offset from
608    the start of the existing bytes to the first reloc we must generate
609    (that is, the offset is applied from the start of the existing
610    bytes after they are replaced by the new bytes, if any), (4) the
611    offset from the start of the existing bytes to the second reloc,
612    (5) whether a third reloc is needed (the third reloc is always four
613    bytes after the second reloc), and (6) whether to warn if this
614    variant is used (this is sometimes needed if .set nomacro or .set
615    noat is in effect).  All these numbers are reasonably small.
616
617    Generating two instruction sequences must be handled carefully to
618    ensure that delay slots are handled correctly.  Fortunately, there
619    are a limited number of cases.  When the second instruction
620    sequence is generated, append_insn is directed to maintain the
621    existing delay slot information, so it continues to apply to any
622    code after the second instruction sequence.  This means that the
623    second instruction sequence must not impose any requirements not
624    required by the first instruction sequence.
625
626    These variant frags are then handled in functions called by the
627    machine independent code.  md_estimate_size_before_relax returns
628    the final size of the frag.  md_convert_frag sets up the final form
629    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
630    one if needed.  */
631 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
632   ((relax_substateT) \
633    (((old) << 23) \
634     | ((new) << 16) \
635     | (((reloc1) + 64) << 9) \
636     | (((reloc2) + 64) << 2) \
637     | ((reloc3) ? (1 << 1) : 0) \
638     | ((warn) ? 1 : 0)))
639 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
640 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
641 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
642 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
643 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
644 #define RELAX_WARN(i) ((i) & 1)
645
646 /* For mips16 code, we use an entirely different form of relaxation.
647    mips16 supports two versions of most instructions which take
648    immediate values: a small one which takes some small value, and a
649    larger one which takes a 16 bit value.  Since branches also follow
650    this pattern, relaxing these values is required.
651
652    We can assemble both mips16 and normal MIPS code in a single
653    object.  Therefore, we need to support this type of relaxation at
654    the same time that we support the relaxation described above.  We
655    use the high bit of the subtype field to distinguish these cases.
656
657    The information we store for this type of relaxation is the
658    argument code found in the opcode file for this relocation, whether
659    the user explicitly requested a small or extended form, and whether
660    the relocation is in a jump or jal delay slot.  That tells us the
661    size of the value, and how it should be stored.  We also store
662    whether the fragment is considered to be extended or not.  We also
663    store whether this is known to be a branch to a different section,
664    whether we have tried to relax this frag yet, and whether we have
665    ever extended a PC relative fragment because of a shift count.  */
666 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
667   (0x80000000                                                   \
668    | ((type) & 0xff)                                            \
669    | ((small) ? 0x100 : 0)                                      \
670    | ((ext) ? 0x200 : 0)                                        \
671    | ((dslot) ? 0x400 : 0)                                      \
672    | ((jal_dslot) ? 0x800 : 0))
673 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
674 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
675 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
676 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
677 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
678 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
679 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
680 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
681 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
682 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
683 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
684 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
685 \f
686 /* Prototypes for static functions.  */
687
688 #ifdef __STDC__
689 #define internalError() \
690     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
691 #else
692 #define internalError() as_fatal (_("MIPS internal Error"));
693 #endif
694
695 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
696
697 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
698                                   unsigned int reg, enum mips_regclass class));
699 static int reg_needs_delay PARAMS ((unsigned int));
700 static void mips16_mark_labels PARAMS ((void));
701 static void append_insn PARAMS ((char *place,
702                                  struct mips_cl_insn * ip,
703                                  expressionS * p,
704                                  bfd_reloc_code_real_type *r,
705                                  boolean));
706 static void mips_no_prev_insn PARAMS ((int));
707 static void mips_emit_delays PARAMS ((boolean));
708 #ifdef USE_STDARG
709 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
710                                  const char *name, const char *fmt,
711                                  ...));
712 #else
713 static void macro_build ();
714 #endif
715 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
716                                         const char *, const char *,
717                                         va_list));
718 static void macro_build_jalr PARAMS ((int, expressionS *));
719 static void macro_build_lui PARAMS ((char *place, int *counter,
720                                      expressionS * ep, int regnum));
721 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
722 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
723                                          expressionS *));
724 static void load_register PARAMS ((int *, int, expressionS *, int));
725 static void load_address PARAMS ((int *, int, expressionS *, int *));
726 static void move_register PARAMS ((int *, int, int));
727 static void macro PARAMS ((struct mips_cl_insn * ip));
728 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
729 #ifdef LOSING_COMPILER
730 static void macro2 PARAMS ((struct mips_cl_insn * ip));
731 #endif
732 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
733 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
734 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
735                                   boolean, boolean, unsigned long *,
736                                   boolean *, unsigned short *));
737 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
738 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
739 static int my_getSmallExpression PARAMS ((expressionS *, char *));
740 static void my_getExpression PARAMS ((expressionS *, char *));
741 #ifdef OBJ_ELF
742 static int support_64bit_objects PARAMS((void));
743 #endif
744 static symbolS *get_symbol PARAMS ((void));
745 static void mips_align PARAMS ((int to, int fill, symbolS *label));
746 static void s_align PARAMS ((int));
747 static void s_change_sec PARAMS ((int));
748 static void s_cons PARAMS ((int));
749 static void s_float_cons PARAMS ((int));
750 static void s_mips_globl PARAMS ((int));
751 static void s_option PARAMS ((int));
752 static void s_mipsset PARAMS ((int));
753 static void s_abicalls PARAMS ((int));
754 static void s_cpload PARAMS ((int));
755 static void s_cpsetup PARAMS ((int));
756 static void s_cplocal PARAMS ((int));
757 static void s_cprestore PARAMS ((int));
758 static void s_cpreturn PARAMS ((int));
759 static void s_gpvalue PARAMS ((int));
760 static void s_gpword PARAMS ((int));
761 static void s_cpadd PARAMS ((int));
762 static void s_insn PARAMS ((int));
763 static void md_obj_begin PARAMS ((void));
764 static void md_obj_end PARAMS ((void));
765 static long get_number PARAMS ((void));
766 static void s_mips_ent PARAMS ((int));
767 static void s_mips_end PARAMS ((int));
768 static void s_mips_frame PARAMS ((int));
769 static void s_mips_mask PARAMS ((int));
770 static void s_mips_stab PARAMS ((int));
771 static void s_mips_weakext PARAMS ((int));
772 static void s_mips_file PARAMS ((int));
773 static void s_mips_loc PARAMS ((int));
774 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
775 static const char *mips_isa_to_str PARAMS ((int));
776 static const char *mips_cpu_to_str PARAMS ((int));
777 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
778 static void show PARAMS ((FILE *, char *, int *, int *));
779 #ifdef OBJ_ELF
780 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
781 #endif
782
783 /* Return values of my_getSmallExpression().  */
784
785 enum small_ex_type
786 {
787   S_EX_NONE = 0,
788   S_EX_REGISTER,
789
790   /* Direct relocation creation by %percent_op().  */
791   S_EX_HALF,
792   S_EX_HI,
793   S_EX_LO,
794   S_EX_GP_REL,
795   S_EX_GOT,
796   S_EX_CALL16,
797   S_EX_GOT_DISP,
798   S_EX_GOT_PAGE,
799   S_EX_GOT_OFST,
800   S_EX_GOT_HI,
801   S_EX_GOT_LO,
802   S_EX_NEG,
803   S_EX_HIGHER,
804   S_EX_HIGHEST,
805   S_EX_CALL_HI,
806   S_EX_CALL_LO
807 };
808
809 /* Table and functions used to map between CPU/ISA names, and
810    ISA levels, and CPU numbers.  */
811
812 struct mips_cpu_info
813 {
814   const char *name;           /* CPU or ISA name.  */
815   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
816   int isa;                    /* ISA level.  */
817   int cpu;                    /* CPU number (default CPU if ISA).  */
818 };
819
820 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
821 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
822 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
823 \f
824 /* Pseudo-op table.
825
826    The following pseudo-ops from the Kane and Heinrich MIPS book
827    should be defined here, but are currently unsupported: .alias,
828    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
829
830    The following pseudo-ops from the Kane and Heinrich MIPS book are
831    specific to the type of debugging information being generated, and
832    should be defined by the object format: .aent, .begin, .bend,
833    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
834    .vreg.
835
836    The following pseudo-ops from the Kane and Heinrich MIPS book are
837    not MIPS CPU specific, but are also not specific to the object file
838    format.  This file is probably the best place to define them, but
839    they are not currently supported: .asm0, .endr, .lab, .repeat,
840    .struct.  */
841
842 static const pseudo_typeS mips_pseudo_table[] =
843 {
844   /* MIPS specific pseudo-ops.  */
845   {"option", s_option, 0},
846   {"set", s_mipsset, 0},
847   {"rdata", s_change_sec, 'r'},
848   {"sdata", s_change_sec, 's'},
849   {"livereg", s_ignore, 0},
850   {"abicalls", s_abicalls, 0},
851   {"cpload", s_cpload, 0},
852   {"cpsetup", s_cpsetup, 0},
853   {"cplocal", s_cplocal, 0},
854   {"cprestore", s_cprestore, 0},
855   {"cpreturn", s_cpreturn, 0},
856   {"gpvalue", s_gpvalue, 0},
857   {"gpword", s_gpword, 0},
858   {"cpadd", s_cpadd, 0},
859   {"insn", s_insn, 0},
860
861   /* Relatively generic pseudo-ops that happen to be used on MIPS
862      chips.  */
863   {"asciiz", stringer, 1},
864   {"bss", s_change_sec, 'b'},
865   {"err", s_err, 0},
866   {"half", s_cons, 1},
867   {"dword", s_cons, 3},
868   {"weakext", s_mips_weakext, 0},
869
870   /* These pseudo-ops are defined in read.c, but must be overridden
871      here for one reason or another.  */
872   {"align", s_align, 0},
873   {"byte", s_cons, 0},
874   {"data", s_change_sec, 'd'},
875   {"double", s_float_cons, 'd'},
876   {"float", s_float_cons, 'f'},
877   {"globl", s_mips_globl, 0},
878   {"global", s_mips_globl, 0},
879   {"hword", s_cons, 1},
880   {"int", s_cons, 2},
881   {"long", s_cons, 2},
882   {"octa", s_cons, 4},
883   {"quad", s_cons, 3},
884   {"short", s_cons, 1},
885   {"single", s_float_cons, 'f'},
886   {"stabn", s_mips_stab, 'n'},
887   {"text", s_change_sec, 't'},
888   {"word", s_cons, 2},
889
890   { "extern", ecoff_directive_extern, 0},
891
892   { NULL, NULL, 0 },
893 };
894
895 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
896 {
897   /* These pseudo-ops should be defined by the object file format.
898      However, a.out doesn't support them, so we have versions here.  */
899   {"aent", s_mips_ent, 1},
900   {"bgnb", s_ignore, 0},
901   {"end", s_mips_end, 0},
902   {"endb", s_ignore, 0},
903   {"ent", s_mips_ent, 0},
904   {"file", s_mips_file, 0},
905   {"fmask", s_mips_mask, 'F'},
906   {"frame", s_mips_frame, 0},
907   {"loc", s_mips_loc, 0},
908   {"mask", s_mips_mask, 'R'},
909   {"verstamp", s_ignore, 0},
910   { NULL, NULL, 0 },
911 };
912
913 extern void pop_insert PARAMS ((const pseudo_typeS *));
914
915 void
916 mips_pop_insert ()
917 {
918   pop_insert (mips_pseudo_table);
919   if (! ECOFF_DEBUGGING)
920     pop_insert (mips_nonecoff_pseudo_table);
921 }
922 \f
923 /* Symbols labelling the current insn.  */
924
925 struct insn_label_list
926 {
927   struct insn_label_list *next;
928   symbolS *label;
929 };
930
931 static struct insn_label_list *insn_labels;
932 static struct insn_label_list *free_insn_labels;
933
934 static void mips_clear_insn_labels PARAMS ((void));
935
936 static inline void
937 mips_clear_insn_labels ()
938 {
939   register struct insn_label_list **pl;
940
941   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
942     ;
943   *pl = insn_labels;
944   insn_labels = NULL;
945 }
946 \f
947 static char *expr_end;
948
949 /* Expressions which appear in instructions.  These are set by
950    mips_ip.  */
951
952 static expressionS imm_expr;
953 static expressionS offset_expr;
954
955 /* Relocs associated with imm_expr and offset_expr.  */
956
957 static bfd_reloc_code_real_type imm_reloc[3]
958   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
959 static bfd_reloc_code_real_type offset_reloc[3]
960   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
961
962 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
963
964 static boolean imm_unmatched_hi;
965
966 /* These are set by mips16_ip if an explicit extension is used.  */
967
968 static boolean mips16_small, mips16_ext;
969
970 /* The pdr segment for per procedure frame/regmask info.  Not used for
971    ECOFF debugging.  */
972
973 static segT pdr_seg;
974
975 static const char *
976 mips_isa_to_str (isa)
977      int isa;
978 {
979   const struct mips_cpu_info *ci;
980   static char s[20];
981
982   ci = mips_cpu_info_from_isa (isa);
983   if (ci != NULL)
984     return (ci->name);
985
986   sprintf (s, "ISA#%d", isa);
987   return s;
988 }
989
990 static const char *
991 mips_cpu_to_str (cpu)
992      int cpu;
993 {
994   const struct mips_cpu_info *ci;
995   static char s[16];
996
997   ci = mips_cpu_info_from_cpu (cpu);
998   if (ci != NULL)
999     return (ci->name);
1000
1001   sprintf (s, "CPU#%d", cpu);
1002   return s;
1003 }
1004
1005 /* The default target format to use.  */
1006
1007 const char *
1008 mips_target_format ()
1009 {
1010   switch (OUTPUT_FLAVOR)
1011     {
1012     case bfd_target_aout_flavour:
1013       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1014     case bfd_target_ecoff_flavour:
1015       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1016     case bfd_target_coff_flavour:
1017       return "pe-mips";
1018     case bfd_target_elf_flavour:
1019 #ifdef TE_TMIPS
1020       /* This is traditional mips */
1021       return (target_big_endian
1022               ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
1023                  : "elf32-tradbigmips")
1024               : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
1025                  : "elf32-tradlittlemips"));
1026 #else
1027       return (target_big_endian
1028               ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
1029               : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1030                  : "elf32-littlemips"));
1031 #endif
1032     default:
1033       abort ();
1034       return NULL;
1035     }
1036 }
1037
1038 /* This function is called once, at assembler startup time.  It should
1039    set up all the tables, etc. that the MD part of the assembler will need.  */
1040
1041 void
1042 md_begin ()
1043 {
1044   register const char *retval = NULL;
1045   int i = 0;
1046   int broken = 0;
1047
1048   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1049     as_warn (_("Could not set architecture and machine"));
1050
1051   op_hash = hash_new ();
1052
1053   for (i = 0; i < NUMOPCODES;)
1054     {
1055       const char *name = mips_opcodes[i].name;
1056
1057       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1058       if (retval != NULL)
1059         {
1060           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1061                    mips_opcodes[i].name, retval);
1062           /* Probably a memory allocation problem?  Give up now.  */
1063           as_fatal (_("Broken assembler.  No assembly attempted."));
1064         }
1065       do
1066         {
1067           if (mips_opcodes[i].pinfo != INSN_MACRO)
1068             {
1069               if (!validate_mips_insn (&mips_opcodes[i]))
1070                 broken = 1;
1071             }
1072           ++i;
1073         }
1074       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1075     }
1076
1077   mips16_op_hash = hash_new ();
1078
1079   i = 0;
1080   while (i < bfd_mips16_num_opcodes)
1081     {
1082       const char *name = mips16_opcodes[i].name;
1083
1084       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1085       if (retval != NULL)
1086         as_fatal (_("internal: can't hash `%s': %s"),
1087                   mips16_opcodes[i].name, retval);
1088       do
1089         {
1090           if (mips16_opcodes[i].pinfo != INSN_MACRO
1091               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1092                   != mips16_opcodes[i].match))
1093             {
1094               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1095                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1096               broken = 1;
1097             }
1098           ++i;
1099         }
1100       while (i < bfd_mips16_num_opcodes
1101              && strcmp (mips16_opcodes[i].name, name) == 0);
1102     }
1103
1104   if (broken)
1105     as_fatal (_("Broken assembler.  No assembly attempted."));
1106
1107   /* We add all the general register names to the symbol table.  This
1108      helps us detect invalid uses of them.  */
1109   for (i = 0; i < 32; i++)
1110     {
1111       char buf[5];
1112
1113       sprintf (buf, "$%d", i);
1114       symbol_table_insert (symbol_new (buf, reg_section, i,
1115                                        &zero_address_frag));
1116     }
1117   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1118                                    &zero_address_frag));
1119   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1120                                    &zero_address_frag));
1121   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1122                                    &zero_address_frag));
1123   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1124                                    &zero_address_frag));
1125   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1126                                    &zero_address_frag));
1127   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1128                                    &zero_address_frag));
1129   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1130                                    &zero_address_frag));
1131   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1132                                    &zero_address_frag));
1133   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1134                                    &zero_address_frag));
1135
1136   mips_no_prev_insn (false);
1137
1138   mips_gprmask = 0;
1139   mips_cprmask[0] = 0;
1140   mips_cprmask[1] = 0;
1141   mips_cprmask[2] = 0;
1142   mips_cprmask[3] = 0;
1143
1144   /* set the default alignment for the text section (2**2) */
1145   record_alignment (text_section, 2);
1146
1147   if (USE_GLOBAL_POINTER_OPT)
1148     bfd_set_gp_size (stdoutput, g_switch_value);
1149
1150   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1151     {
1152       /* On a native system, sections must be aligned to 16 byte
1153          boundaries.  When configured for an embedded ELF target, we
1154          don't bother.  */
1155       if (strcmp (TARGET_OS, "elf") != 0)
1156         {
1157           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1158           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1159           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1160         }
1161
1162       /* Create a .reginfo section for register masks and a .mdebug
1163          section for debugging information.  */
1164       {
1165         segT seg;
1166         subsegT subseg;
1167         flagword flags;
1168         segT sec;
1169
1170         seg = now_seg;
1171         subseg = now_subseg;
1172
1173         /* The ABI says this section should be loaded so that the
1174            running program can access it.  However, we don't load it
1175            if we are configured for an embedded target */
1176         flags = SEC_READONLY | SEC_DATA;
1177         if (strcmp (TARGET_OS, "elf") != 0)
1178           flags |= SEC_ALLOC | SEC_LOAD;
1179
1180         if (file_mips_abi != N64_ABI)
1181           {
1182             sec = subseg_new (".reginfo", (subsegT) 0);
1183
1184             bfd_set_section_flags (stdoutput, sec, flags);
1185             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1186
1187 #ifdef OBJ_ELF
1188             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1189 #endif
1190           }
1191         else
1192           {
1193             /* The 64-bit ABI uses a .MIPS.options section rather than
1194                .reginfo section.  */
1195             sec = subseg_new (".MIPS.options", (subsegT) 0);
1196             bfd_set_section_flags (stdoutput, sec, flags);
1197             bfd_set_section_alignment (stdoutput, sec, 3);
1198
1199 #ifdef OBJ_ELF
1200             /* Set up the option header.  */
1201             {
1202               Elf_Internal_Options opthdr;
1203               char *f;
1204
1205               opthdr.kind = ODK_REGINFO;
1206               opthdr.size = (sizeof (Elf_External_Options)
1207                              + sizeof (Elf64_External_RegInfo));
1208               opthdr.section = 0;
1209               opthdr.info = 0;
1210               f = frag_more (sizeof (Elf_External_Options));
1211               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1212                                              (Elf_External_Options *) f);
1213
1214               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1215             }
1216 #endif
1217           }
1218
1219         if (ECOFF_DEBUGGING)
1220           {
1221             sec = subseg_new (".mdebug", (subsegT) 0);
1222             (void) bfd_set_section_flags (stdoutput, sec,
1223                                           SEC_HAS_CONTENTS | SEC_READONLY);
1224             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1225           }
1226 #ifdef OBJ_ELF
1227         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1228           {
1229             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1230             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1231                                           SEC_READONLY | SEC_RELOC
1232                                           | SEC_DEBUGGING);
1233             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1234           }
1235 #endif
1236
1237         subseg_set (seg, subseg);
1238       }
1239     }
1240
1241   if (! ECOFF_DEBUGGING)
1242     md_obj_begin ();
1243 }
1244
1245 void
1246 md_mips_end ()
1247 {
1248   if (! ECOFF_DEBUGGING)
1249     md_obj_end ();
1250 }
1251
1252 void
1253 md_assemble (str)
1254      char *str;
1255 {
1256   struct mips_cl_insn insn;
1257   bfd_reloc_code_real_type unused_reloc[3]
1258     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1259
1260   imm_expr.X_op = O_absent;
1261   imm_unmatched_hi = false;
1262   offset_expr.X_op = O_absent;
1263   imm_reloc[0] = BFD_RELOC_UNUSED;
1264   imm_reloc[1] = BFD_RELOC_UNUSED;
1265   imm_reloc[2] = BFD_RELOC_UNUSED;
1266   offset_reloc[0] = BFD_RELOC_UNUSED;
1267   offset_reloc[1] = BFD_RELOC_UNUSED;
1268   offset_reloc[2] = BFD_RELOC_UNUSED;
1269
1270   if (mips_opts.mips16)
1271     mips16_ip (str, &insn);
1272   else
1273     {
1274       mips_ip (str, &insn);
1275       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1276             str, insn.insn_opcode));
1277     }
1278
1279   if (insn_error)
1280     {
1281       as_bad ("%s `%s'", insn_error, str);
1282       return;
1283     }
1284
1285   if (insn.insn_mo->pinfo == INSN_MACRO)
1286     {
1287       if (mips_opts.mips16)
1288         mips16_macro (&insn);
1289       else
1290         macro (&insn);
1291     }
1292   else
1293     {
1294       if (imm_expr.X_op != O_absent)
1295         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1296       else if (offset_expr.X_op != O_absent)
1297         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1298       else
1299         append_insn (NULL, &insn, NULL, unused_reloc, false);
1300     }
1301 }
1302
1303 /* See whether instruction IP reads register REG.  CLASS is the type
1304    of register.  */
1305
1306 static int
1307 insn_uses_reg (ip, reg, class)
1308      struct mips_cl_insn *ip;
1309      unsigned int reg;
1310      enum mips_regclass class;
1311 {
1312   if (class == MIPS16_REG)
1313     {
1314       assert (mips_opts.mips16);
1315       reg = mips16_to_32_reg_map[reg];
1316       class = MIPS_GR_REG;
1317     }
1318
1319   /* Don't report on general register ZERO, since it never changes.  */
1320   if (class == MIPS_GR_REG && reg == ZERO)
1321     return 0;
1322
1323   if (class == MIPS_FP_REG)
1324     {
1325       assert (! mips_opts.mips16);
1326       /* If we are called with either $f0 or $f1, we must check $f0.
1327          This is not optimal, because it will introduce an unnecessary
1328          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1329          need to distinguish reading both $f0 and $f1 or just one of
1330          them.  Note that we don't have to check the other way,
1331          because there is no instruction that sets both $f0 and $f1
1332          and requires a delay.  */
1333       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1334           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1335               == (reg &~ (unsigned) 1)))
1336         return 1;
1337       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1338           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1339               == (reg &~ (unsigned) 1)))
1340         return 1;
1341     }
1342   else if (! mips_opts.mips16)
1343     {
1344       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1345           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1346         return 1;
1347       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1348           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1349         return 1;
1350     }
1351   else
1352     {
1353       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1354           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1355                                     & MIPS16OP_MASK_RX)]
1356               == reg))
1357         return 1;
1358       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1359           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1360                                     & MIPS16OP_MASK_RY)]
1361               == reg))
1362         return 1;
1363       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1364           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1365                                     & MIPS16OP_MASK_MOVE32Z)]
1366               == reg))
1367         return 1;
1368       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1369         return 1;
1370       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1371         return 1;
1372       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1373         return 1;
1374       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1375           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1376               & MIPS16OP_MASK_REGR32) == reg)
1377         return 1;
1378     }
1379
1380   return 0;
1381 }
1382
1383 /* This function returns true if modifying a register requires a
1384    delay.  */
1385
1386 static int
1387 reg_needs_delay (reg)
1388      unsigned int reg;
1389 {
1390   unsigned long prev_pinfo;
1391
1392   prev_pinfo = prev_insn.insn_mo->pinfo;
1393   if (! mips_opts.noreorder
1394       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1395       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1396           || (! gpr_interlocks
1397               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1398     {
1399       /* A load from a coprocessor or from memory.  All load
1400          delays delay the use of general register rt for one
1401          instruction on the r3000.  The r6000 and r4000 use
1402          interlocks.  */
1403       /* Itbl support may require additional care here.  */
1404       know (prev_pinfo & INSN_WRITE_GPR_T);
1405       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1406         return 1;
1407     }
1408
1409   return 0;
1410 }
1411
1412 /* Mark instruction labels in mips16 mode.  This permits the linker to
1413    handle them specially, such as generating jalx instructions when
1414    needed.  We also make them odd for the duration of the assembly, in
1415    order to generate the right sort of code.  We will make them even
1416    in the adjust_symtab routine, while leaving them marked.  This is
1417    convenient for the debugger and the disassembler.  The linker knows
1418    to make them odd again.  */
1419
1420 static void
1421 mips16_mark_labels ()
1422 {
1423   if (mips_opts.mips16)
1424     {
1425       struct insn_label_list *l;
1426       valueT val;
1427
1428       for (l = insn_labels; l != NULL; l = l->next)
1429         {
1430 #ifdef OBJ_ELF
1431           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1432             S_SET_OTHER (l->label, STO_MIPS16);
1433 #endif
1434           val = S_GET_VALUE (l->label);
1435           if ((val & 1) == 0)
1436             S_SET_VALUE (l->label, val + 1);
1437         }
1438     }
1439 }
1440
1441 /* Output an instruction.  PLACE is where to put the instruction; if
1442    it is NULL, this uses frag_more to get room.  IP is the instruction
1443    information.  ADDRESS_EXPR is an operand of the instruction to be
1444    used with RELOC_TYPE.  */
1445
1446 static void
1447 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1448      char *place;
1449      struct mips_cl_insn *ip;
1450      expressionS *address_expr;
1451      bfd_reloc_code_real_type *reloc_type;
1452      boolean unmatched_hi;
1453 {
1454   register unsigned long prev_pinfo, pinfo;
1455   char *f;
1456   fixS *fixp[3];
1457   int nops = 0;
1458
1459   /* Mark instruction labels in mips16 mode.  */
1460   mips16_mark_labels ();
1461
1462   prev_pinfo = prev_insn.insn_mo->pinfo;
1463   pinfo = ip->insn_mo->pinfo;
1464
1465   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1466     {
1467       int prev_prev_nop;
1468
1469       /* If the previous insn required any delay slots, see if we need
1470          to insert a NOP or two.  There are eight kinds of possible
1471          hazards, of which an instruction can have at most one type.
1472          (1) a load from memory delay
1473          (2) a load from a coprocessor delay
1474          (3) an unconditional branch delay
1475          (4) a conditional branch delay
1476          (5) a move to coprocessor register delay
1477          (6) a load coprocessor register from memory delay
1478          (7) a coprocessor condition code delay
1479          (8) a HI/LO special register delay
1480
1481          There are a lot of optimizations we could do that we don't.
1482          In particular, we do not, in general, reorder instructions.
1483          If you use gcc with optimization, it will reorder
1484          instructions and generally do much more optimization then we
1485          do here; repeating all that work in the assembler would only
1486          benefit hand written assembly code, and does not seem worth
1487          it.  */
1488
1489       /* This is how a NOP is emitted.  */
1490 #define emit_nop()                                      \
1491   (mips_opts.mips16                                     \
1492    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1493    : md_number_to_chars (frag_more (4), 0, 4))
1494
1495       /* The previous insn might require a delay slot, depending upon
1496          the contents of the current insn.  */
1497       if (! mips_opts.mips16
1498           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1499           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1500                && ! cop_interlocks)
1501               || (! gpr_interlocks
1502                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1503         {
1504           /* A load from a coprocessor or from memory.  All load
1505              delays delay the use of general register rt for one
1506              instruction on the r3000.  The r6000 and r4000 use
1507              interlocks.  */
1508           /* Itbl support may require additional care here.  */
1509           know (prev_pinfo & INSN_WRITE_GPR_T);
1510           if (mips_optimize == 0
1511               || insn_uses_reg (ip,
1512                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1513                                  & OP_MASK_RT),
1514                                 MIPS_GR_REG))
1515             ++nops;
1516         }
1517       else if (! mips_opts.mips16
1518                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1519                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1520                     && ! cop_interlocks)
1521                    || (mips_opts.isa == ISA_MIPS1
1522                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1523         {
1524           /* A generic coprocessor delay.  The previous instruction
1525              modified a coprocessor general or control register.  If
1526              it modified a control register, we need to avoid any
1527              coprocessor instruction (this is probably not always
1528              required, but it sometimes is).  If it modified a general
1529              register, we avoid using that register.
1530
1531              On the r6000 and r4000 loading a coprocessor register
1532              from memory is interlocked, and does not require a delay.
1533
1534              This case is not handled very well.  There is no special
1535              knowledge of CP0 handling, and the coprocessors other
1536              than the floating point unit are not distinguished at
1537              all.  */
1538           /* Itbl support may require additional care here. FIXME!
1539              Need to modify this to include knowledge about
1540              user specified delays!  */
1541           if (prev_pinfo & INSN_WRITE_FPR_T)
1542             {
1543               if (mips_optimize == 0
1544                   || insn_uses_reg (ip,
1545                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1546                                      & OP_MASK_FT),
1547                                     MIPS_FP_REG))
1548                 ++nops;
1549             }
1550           else if (prev_pinfo & INSN_WRITE_FPR_S)
1551             {
1552               if (mips_optimize == 0
1553                   || insn_uses_reg (ip,
1554                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1555                                      & OP_MASK_FS),
1556                                     MIPS_FP_REG))
1557                 ++nops;
1558             }
1559           else
1560             {
1561               /* We don't know exactly what the previous instruction
1562                  does.  If the current instruction uses a coprocessor
1563                  register, we must insert a NOP.  If previous
1564                  instruction may set the condition codes, and the
1565                  current instruction uses them, we must insert two
1566                  NOPS.  */
1567               /* Itbl support may require additional care here.  */
1568               if (mips_optimize == 0
1569                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1570                       && (pinfo & INSN_READ_COND_CODE)))
1571                 nops += 2;
1572               else if (pinfo & INSN_COP)
1573                 ++nops;
1574             }
1575         }
1576       else if (! mips_opts.mips16
1577                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1578                && (prev_pinfo & INSN_WRITE_COND_CODE)
1579                && ! cop_interlocks)
1580         {
1581           /* The previous instruction sets the coprocessor condition
1582              codes, but does not require a general coprocessor delay
1583              (this means it is a floating point comparison
1584              instruction).  If this instruction uses the condition
1585              codes, we need to insert a single NOP.  */
1586           /* Itbl support may require additional care here.  */
1587           if (mips_optimize == 0
1588               || (pinfo & INSN_READ_COND_CODE))
1589             ++nops;
1590         }
1591
1592       /* If we're fixing up mfhi/mflo for the r7000 and the
1593          previous insn was an mfhi/mflo and the current insn
1594          reads the register that the mfhi/mflo wrote to, then
1595          insert two nops.  */
1596
1597       else if (mips_7000_hilo_fix
1598                && MF_HILO_INSN (prev_pinfo)
1599                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1600                                       & OP_MASK_RD),
1601                                  MIPS_GR_REG))
1602         {
1603           nops += 2;
1604         }
1605
1606       /* If we're fixing up mfhi/mflo for the r7000 and the
1607          2nd previous insn was an mfhi/mflo and the current insn
1608          reads the register that the mfhi/mflo wrote to, then
1609          insert one nop.  */
1610
1611       else if (mips_7000_hilo_fix
1612                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1613                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1614                                        & OP_MASK_RD),
1615                                     MIPS_GR_REG))
1616
1617         {
1618           ++nops;
1619         }
1620
1621       else if (prev_pinfo & INSN_READ_LO)
1622         {
1623           /* The previous instruction reads the LO register; if the
1624              current instruction writes to the LO register, we must
1625              insert two NOPS.  Some newer processors have interlocks.
1626              Also the tx39's multiply instructions can be exectuted
1627              immediatly after a read from HI/LO (without the delay),
1628              though the tx39's divide insns still do require the
1629              delay.  */
1630           if (! (hilo_interlocks
1631                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1632               && (mips_optimize == 0
1633                   || (pinfo & INSN_WRITE_LO)))
1634             nops += 2;
1635           /* Most mips16 branch insns don't have a delay slot.
1636              If a read from LO is immediately followed by a branch
1637              to a write to LO we have a read followed by a write
1638              less than 2 insns away.  We assume the target of
1639              a branch might be a write to LO, and insert a nop
1640              between a read and an immediately following branch.  */
1641           else if (mips_opts.mips16
1642                    && (mips_optimize == 0
1643                        || (pinfo & MIPS16_INSN_BRANCH)))
1644             ++nops;
1645         }
1646       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1647         {
1648           /* The previous instruction reads the HI register; if the
1649              current instruction writes to the HI register, we must
1650              insert a NOP.  Some newer processors have interlocks.
1651              Also the note tx39's multiply above.  */
1652           if (! (hilo_interlocks
1653                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1654               && (mips_optimize == 0
1655                   || (pinfo & INSN_WRITE_HI)))
1656             nops += 2;
1657           /* Most mips16 branch insns don't have a delay slot.
1658              If a read from HI is immediately followed by a branch
1659              to a write to HI we have a read followed by a write
1660              less than 2 insns away.  We assume the target of
1661              a branch might be a write to HI, and insert a nop
1662              between a read and an immediately following branch.  */
1663           else if (mips_opts.mips16
1664                    && (mips_optimize == 0
1665                        || (pinfo & MIPS16_INSN_BRANCH)))
1666             ++nops;
1667         }
1668
1669       /* If the previous instruction was in a noreorder section, then
1670          we don't want to insert the nop after all.  */
1671       /* Itbl support may require additional care here.  */
1672       if (prev_insn_unreordered)
1673         nops = 0;
1674
1675       /* There are two cases which require two intervening
1676          instructions: 1) setting the condition codes using a move to
1677          coprocessor instruction which requires a general coprocessor
1678          delay and then reading the condition codes 2) reading the HI
1679          or LO register and then writing to it (except on processors
1680          which have interlocks).  If we are not already emitting a NOP
1681          instruction, we must check for these cases compared to the
1682          instruction previous to the previous instruction.  */
1683       if ((! mips_opts.mips16
1684            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1685            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1686            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1687            && (pinfo & INSN_READ_COND_CODE)
1688            && ! cop_interlocks)
1689           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1690               && (pinfo & INSN_WRITE_LO)
1691               && ! (hilo_interlocks
1692                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1693           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1694               && (pinfo & INSN_WRITE_HI)
1695               && ! (hilo_interlocks
1696                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1697         prev_prev_nop = 1;
1698       else
1699         prev_prev_nop = 0;
1700
1701       if (prev_prev_insn_unreordered)
1702         prev_prev_nop = 0;
1703
1704       if (prev_prev_nop && nops == 0)
1705         ++nops;
1706
1707       /* If we are being given a nop instruction, don't bother with
1708          one of the nops we would otherwise output.  This will only
1709          happen when a nop instruction is used with mips_optimize set
1710          to 0.  */
1711       if (nops > 0
1712           && ! mips_opts.noreorder
1713           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1714         --nops;
1715
1716       /* Now emit the right number of NOP instructions.  */
1717       if (nops > 0 && ! mips_opts.noreorder)
1718         {
1719           fragS *old_frag;
1720           unsigned long old_frag_offset;
1721           int i;
1722           struct insn_label_list *l;
1723
1724           old_frag = frag_now;
1725           old_frag_offset = frag_now_fix ();
1726
1727           for (i = 0; i < nops; i++)
1728             emit_nop ();
1729
1730           if (listing)
1731             {
1732               listing_prev_line ();
1733               /* We may be at the start of a variant frag.  In case we
1734                  are, make sure there is enough space for the frag
1735                  after the frags created by listing_prev_line.  The
1736                  argument to frag_grow here must be at least as large
1737                  as the argument to all other calls to frag_grow in
1738                  this file.  We don't have to worry about being in the
1739                  middle of a variant frag, because the variants insert
1740                  all needed nop instructions themselves.  */
1741               frag_grow (40);
1742             }
1743
1744           for (l = insn_labels; l != NULL; l = l->next)
1745             {
1746               valueT val;
1747
1748               assert (S_GET_SEGMENT (l->label) == now_seg);
1749               symbol_set_frag (l->label, frag_now);
1750               val = (valueT) frag_now_fix ();
1751               /* mips16 text labels are stored as odd.  */
1752               if (mips_opts.mips16)
1753                 ++val;
1754               S_SET_VALUE (l->label, val);
1755             }
1756
1757 #ifndef NO_ECOFF_DEBUGGING
1758           if (ECOFF_DEBUGGING)
1759             ecoff_fix_loc (old_frag, old_frag_offset);
1760 #endif
1761         }
1762       else if (prev_nop_frag != NULL)
1763         {
1764           /* We have a frag holding nops we may be able to remove.  If
1765              we don't need any nops, we can decrease the size of
1766              prev_nop_frag by the size of one instruction.  If we do
1767              need some nops, we count them in prev_nops_required.  */
1768           if (prev_nop_frag_since == 0)
1769             {
1770               if (nops == 0)
1771                 {
1772                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1773                   --prev_nop_frag_holds;
1774                 }
1775               else
1776                 prev_nop_frag_required += nops;
1777             }
1778           else
1779             {
1780               if (prev_prev_nop == 0)
1781                 {
1782                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1783                   --prev_nop_frag_holds;
1784                 }
1785               else
1786                 ++prev_nop_frag_required;
1787             }
1788
1789           if (prev_nop_frag_holds <= prev_nop_frag_required)
1790             prev_nop_frag = NULL;
1791
1792           ++prev_nop_frag_since;
1793
1794           /* Sanity check: by the time we reach the second instruction
1795              after prev_nop_frag, we should have used up all the nops
1796              one way or another.  */
1797           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1798         }
1799     }
1800
1801   if (*reloc_type > BFD_RELOC_UNUSED)
1802     {
1803       /* We need to set up a variant frag.  */
1804       assert (mips_opts.mips16 && address_expr != NULL);
1805       f = frag_var (rs_machine_dependent, 4, 0,
1806                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1807                                          mips16_small, mips16_ext,
1808                                          (prev_pinfo
1809                                           & INSN_UNCOND_BRANCH_DELAY),
1810                                          (*prev_insn_reloc_type
1811                                           == BFD_RELOC_MIPS16_JMP)),
1812                     make_expr_symbol (address_expr), 0, NULL);
1813     }
1814   else if (place != NULL)
1815     f = place;
1816   else if (mips_opts.mips16
1817            && ! ip->use_extend
1818            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1819     {
1820       /* Make sure there is enough room to swap this instruction with
1821          a following jump instruction.  */
1822       frag_grow (6);
1823       f = frag_more (2);
1824     }
1825   else
1826     {
1827       if (mips_opts.mips16
1828           && mips_opts.noreorder
1829           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1830         as_warn (_("extended instruction in delay slot"));
1831
1832       f = frag_more (4);
1833     }
1834
1835   fixp[0] = fixp[1] = fixp[2] = NULL;
1836   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1837     {
1838       if (address_expr->X_op == O_constant)
1839         {
1840           valueT tmp;
1841
1842           switch (*reloc_type)
1843             {
1844             case BFD_RELOC_32:
1845               ip->insn_opcode |= address_expr->X_add_number;
1846               break;
1847
1848             case BFD_RELOC_MIPS_HIGHEST:
1849               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1850               tmp >>= 16;
1851               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1852               break;
1853
1854             case BFD_RELOC_MIPS_HIGHER:
1855               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1856               ip->insn_opcode |= (tmp >> 16) & 0xffff;
1857               break;
1858
1859             case BFD_RELOC_HI16_S:
1860               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1861                                   >> 16) & 0xffff;
1862               break;
1863
1864             case BFD_RELOC_HI16:
1865               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1866               break;
1867
1868             case BFD_RELOC_LO16:
1869               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1870               break;
1871
1872             case BFD_RELOC_MIPS_JMP:
1873               if ((address_expr->X_add_number & 3) != 0)
1874                 as_bad (_("jump to misaligned address (0x%lx)"),
1875                         (unsigned long) address_expr->X_add_number);
1876               if (address_expr->X_add_number & ~0xfffffff
1877                   || address_expr->X_add_number > 0x7fffffc)
1878                 as_bad (_("jump address range overflow (0x%lx)"),
1879                         (unsigned long) address_expr->X_add_number);
1880               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1881               break;
1882
1883             case BFD_RELOC_MIPS16_JMP:
1884               if ((address_expr->X_add_number & 3) != 0)
1885                 as_bad (_("jump to misaligned address (0x%lx)"),
1886                         (unsigned long) address_expr->X_add_number);
1887               if (address_expr->X_add_number & ~0xfffffff
1888                   || address_expr->X_add_number > 0x7fffffc)
1889                 as_bad (_("jump address range overflow (0x%lx)"),
1890                         (unsigned long) address_expr->X_add_number);
1891               ip->insn_opcode |=
1892                 (((address_expr->X_add_number & 0x7c0000) << 3)
1893                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1894                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1895               break;
1896
1897             case BFD_RELOC_16_PCREL:
1898               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1899               break;
1900
1901             case BFD_RELOC_16_PCREL_S2:
1902               goto need_reloc;
1903
1904             default:
1905               internalError ();
1906             }
1907         }
1908       else
1909         {
1910         need_reloc:
1911           /* Don't generate a reloc if we are writing into a variant frag.  */
1912           if (place == NULL)
1913             {
1914               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1915                                      address_expr,
1916                                      (*reloc_type == BFD_RELOC_16_PCREL
1917                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
1918                                      reloc_type[0]);
1919
1920               /* These relocations can have an addend that won't fit in
1921                  4 octets for 64bit assembly.  */
1922               if (HAVE_64BIT_GPRS &&
1923                   (*reloc_type == BFD_RELOC_16
1924                    || *reloc_type == BFD_RELOC_32
1925                    || *reloc_type == BFD_RELOC_MIPS_JMP
1926                    || *reloc_type == BFD_RELOC_HI16_S
1927                    || *reloc_type == BFD_RELOC_LO16
1928                    || *reloc_type == BFD_RELOC_GPREL16
1929                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
1930                    || *reloc_type == BFD_RELOC_GPREL32
1931                    || *reloc_type == BFD_RELOC_64
1932                    || *reloc_type == BFD_RELOC_CTOR
1933                    || *reloc_type == BFD_RELOC_MIPS_SUB
1934                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1935                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
1936                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1937                    || *reloc_type == BFD_RELOC_MIPS_REL16
1938                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1939                 fixp[0]->fx_no_overflow = 1;
1940
1941               if (unmatched_hi)
1942                 {
1943                   struct mips_hi_fixup *hi_fixup;
1944
1945                   assert (*reloc_type == BFD_RELOC_HI16_S);
1946                   hi_fixup = ((struct mips_hi_fixup *)
1947                               xmalloc (sizeof (struct mips_hi_fixup)));
1948                   hi_fixup->fixp = fixp[0];
1949                   hi_fixup->seg = now_seg;
1950                   hi_fixup->next = mips_hi_fixup_list;
1951                   mips_hi_fixup_list = hi_fixup;
1952                 }
1953
1954               if (reloc_type[1] != BFD_RELOC_UNUSED)
1955                 {
1956                   /* FIXME: This symbol can be one of
1957                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
1958                   address_expr->X_op = O_absent;
1959                   address_expr->X_add_symbol = 0;
1960                   address_expr->X_add_number = 0;
1961
1962                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1963                                          4, address_expr, false,
1964                                          reloc_type[1]);
1965
1966                   /* These relocations can have an addend that won't fit in
1967                      4 octets for 64bit assembly.  */
1968                   if (HAVE_64BIT_GPRS &&
1969                       (*reloc_type == BFD_RELOC_16
1970                        || *reloc_type == BFD_RELOC_32
1971                        || *reloc_type == BFD_RELOC_MIPS_JMP
1972                        || *reloc_type == BFD_RELOC_HI16_S
1973                        || *reloc_type == BFD_RELOC_LO16
1974                        || *reloc_type == BFD_RELOC_GPREL16
1975                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
1976                        || *reloc_type == BFD_RELOC_GPREL32
1977                        || *reloc_type == BFD_RELOC_64
1978                        || *reloc_type == BFD_RELOC_CTOR
1979                        || *reloc_type == BFD_RELOC_MIPS_SUB
1980                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1981                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
1982                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1983                        || *reloc_type == BFD_RELOC_MIPS_REL16
1984                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
1985                     fixp[1]->fx_no_overflow = 1;
1986
1987                   if (reloc_type[2] != BFD_RELOC_UNUSED)
1988                     {
1989                       address_expr->X_op = O_absent;
1990                       address_expr->X_add_symbol = 0;
1991                       address_expr->X_add_number = 0;
1992
1993                       fixp[2] = fix_new_exp (frag_now,
1994                                              f - frag_now->fr_literal, 4,
1995                                              address_expr, false,
1996                                              reloc_type[2]);
1997
1998                       /* These relocations can have an addend that won't fit in
1999                          4 octets for 64bit assembly.  */
2000                       if (HAVE_64BIT_GPRS &&
2001                           (*reloc_type == BFD_RELOC_16
2002                            || *reloc_type == BFD_RELOC_32
2003                            || *reloc_type == BFD_RELOC_MIPS_JMP
2004                            || *reloc_type == BFD_RELOC_HI16_S
2005                            || *reloc_type == BFD_RELOC_LO16
2006                            || *reloc_type == BFD_RELOC_GPREL16
2007                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2008                            || *reloc_type == BFD_RELOC_GPREL32
2009                            || *reloc_type == BFD_RELOC_64
2010                            || *reloc_type == BFD_RELOC_CTOR
2011                            || *reloc_type == BFD_RELOC_MIPS_SUB
2012                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2013                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2014                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2015                            || *reloc_type == BFD_RELOC_MIPS_REL16
2016                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2017                         fixp[2]->fx_no_overflow = 1;
2018                     }
2019                 }
2020             }
2021         }
2022     }
2023
2024   if (! mips_opts.mips16)
2025     {
2026       md_number_to_chars (f, ip->insn_opcode, 4);
2027 #ifdef OBJ_ELF
2028       dwarf2_emit_insn (4);
2029 #endif
2030     }
2031   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2032     {
2033       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2034       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2035 #ifdef OBJ_ELF
2036       dwarf2_emit_insn (4);
2037 #endif
2038     }
2039   else
2040     {
2041       if (ip->use_extend)
2042         {
2043           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2044           f += 2;
2045         }
2046       md_number_to_chars (f, ip->insn_opcode, 2);
2047 #ifdef OBJ_ELF
2048       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2049 #endif
2050     }
2051
2052   /* Update the register mask information.  */
2053   if (! mips_opts.mips16)
2054     {
2055       if (pinfo & INSN_WRITE_GPR_D)
2056         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2057       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2058         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2059       if (pinfo & INSN_READ_GPR_S)
2060         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2061       if (pinfo & INSN_WRITE_GPR_31)
2062         mips_gprmask |= 1 << RA;
2063       if (pinfo & INSN_WRITE_FPR_D)
2064         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2065       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2066         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2067       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2068         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2069       if ((pinfo & INSN_READ_FPR_R) != 0)
2070         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2071       if (pinfo & INSN_COP)
2072         {
2073           /* We don't keep enough information to sort these cases out.
2074              The itbl support does keep this information however, although
2075              we currently don't support itbl fprmats as part of the cop
2076              instruction.  May want to add this support in the future.  */
2077         }
2078       /* Never set the bit for $0, which is always zero.  */
2079       mips_gprmask &= ~1 << 0;
2080     }
2081   else
2082     {
2083       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2084         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2085                               & MIPS16OP_MASK_RX);
2086       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2087         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2088                               & MIPS16OP_MASK_RY);
2089       if (pinfo & MIPS16_INSN_WRITE_Z)
2090         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2091                               & MIPS16OP_MASK_RZ);
2092       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2093         mips_gprmask |= 1 << TREG;
2094       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2095         mips_gprmask |= 1 << SP;
2096       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2097         mips_gprmask |= 1 << RA;
2098       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2099         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2100       if (pinfo & MIPS16_INSN_READ_Z)
2101         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2102                               & MIPS16OP_MASK_MOVE32Z);
2103       if (pinfo & MIPS16_INSN_READ_GPR_X)
2104         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2105                               & MIPS16OP_MASK_REGR32);
2106     }
2107
2108   if (place == NULL && ! mips_opts.noreorder)
2109     {
2110       /* Filling the branch delay slot is more complex.  We try to
2111          switch the branch with the previous instruction, which we can
2112          do if the previous instruction does not set up a condition
2113          that the branch tests and if the branch is not itself the
2114          target of any branch.  */
2115       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2116           || (pinfo & INSN_COND_BRANCH_DELAY))
2117         {
2118           if (mips_optimize < 2
2119               /* If we have seen .set volatile or .set nomove, don't
2120                  optimize.  */
2121               || mips_opts.nomove != 0
2122               /* If we had to emit any NOP instructions, then we
2123                  already know we can not swap.  */
2124               || nops != 0
2125               /* If we don't even know the previous insn, we can not
2126                  swap.  */
2127               || ! prev_insn_valid
2128               /* If the previous insn is already in a branch delay
2129                  slot, then we can not swap.  */
2130               || prev_insn_is_delay_slot
2131               /* If the previous previous insn was in a .set
2132                  noreorder, we can't swap.  Actually, the MIPS
2133                  assembler will swap in this situation.  However, gcc
2134                  configured -with-gnu-as will generate code like
2135                    .set noreorder
2136                    lw   $4,XXX
2137                    .set reorder
2138                    INSN
2139                    bne  $4,$0,foo
2140                  in which we can not swap the bne and INSN.  If gcc is
2141                  not configured -with-gnu-as, it does not output the
2142                  .set pseudo-ops.  We don't have to check
2143                  prev_insn_unreordered, because prev_insn_valid will
2144                  be 0 in that case.  We don't want to use
2145                  prev_prev_insn_valid, because we do want to be able
2146                  to swap at the start of a function.  */
2147               || prev_prev_insn_unreordered
2148               /* If the branch is itself the target of a branch, we
2149                  can not swap.  We cheat on this; all we check for is
2150                  whether there is a label on this instruction.  If
2151                  there are any branches to anything other than a
2152                  label, users must use .set noreorder.  */
2153               || insn_labels != NULL
2154               /* If the previous instruction is in a variant frag, we
2155                  can not do the swap.  This does not apply to the
2156                  mips16, which uses variant frags for different
2157                  purposes.  */
2158               || (! mips_opts.mips16
2159                   && prev_insn_frag->fr_type == rs_machine_dependent)
2160               /* If the branch reads the condition codes, we don't
2161                  even try to swap, because in the sequence
2162                    ctc1 $X,$31
2163                    INSN
2164                    INSN
2165                    bc1t LABEL
2166                  we can not swap, and I don't feel like handling that
2167                  case.  */
2168               || (! mips_opts.mips16
2169                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2170                   && (pinfo & INSN_READ_COND_CODE))
2171               /* We can not swap with an instruction that requires a
2172                  delay slot, becase the target of the branch might
2173                  interfere with that instruction.  */
2174               || (! mips_opts.mips16
2175                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2176                   && (prev_pinfo
2177               /* Itbl support may require additional care here.  */
2178                       & (INSN_LOAD_COPROC_DELAY
2179                          | INSN_COPROC_MOVE_DELAY
2180                          | INSN_WRITE_COND_CODE)))
2181               || (! (hilo_interlocks
2182                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2183                   && (prev_pinfo
2184                       & (INSN_READ_LO
2185                          | INSN_READ_HI)))
2186               || (! mips_opts.mips16
2187                   && ! gpr_interlocks
2188                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2189               || (! mips_opts.mips16
2190                   && mips_opts.isa == ISA_MIPS1
2191                   /* Itbl support may require additional care here.  */
2192                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2193               /* We can not swap with a branch instruction.  */
2194               || (prev_pinfo
2195                   & (INSN_UNCOND_BRANCH_DELAY
2196                      | INSN_COND_BRANCH_DELAY
2197                      | INSN_COND_BRANCH_LIKELY))
2198               /* We do not swap with a trap instruction, since it
2199                  complicates trap handlers to have the trap
2200                  instruction be in a delay slot.  */
2201               || (prev_pinfo & INSN_TRAP)
2202               /* If the branch reads a register that the previous
2203                  instruction sets, we can not swap.  */
2204               || (! mips_opts.mips16
2205                   && (prev_pinfo & INSN_WRITE_GPR_T)
2206                   && insn_uses_reg (ip,
2207                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2208                                      & OP_MASK_RT),
2209                                     MIPS_GR_REG))
2210               || (! mips_opts.mips16
2211                   && (prev_pinfo & INSN_WRITE_GPR_D)
2212                   && insn_uses_reg (ip,
2213                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2214                                      & OP_MASK_RD),
2215                                     MIPS_GR_REG))
2216               || (mips_opts.mips16
2217                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2218                        && insn_uses_reg (ip,
2219                                          ((prev_insn.insn_opcode
2220                                            >> MIPS16OP_SH_RX)
2221                                           & MIPS16OP_MASK_RX),
2222                                          MIPS16_REG))
2223                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2224                           && insn_uses_reg (ip,
2225                                             ((prev_insn.insn_opcode
2226                                               >> MIPS16OP_SH_RY)
2227                                              & MIPS16OP_MASK_RY),
2228                                             MIPS16_REG))
2229                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2230                           && insn_uses_reg (ip,
2231                                             ((prev_insn.insn_opcode
2232                                               >> MIPS16OP_SH_RZ)
2233                                              & MIPS16OP_MASK_RZ),
2234                                             MIPS16_REG))
2235                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2236                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2237                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2238                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2239                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2240                           && insn_uses_reg (ip,
2241                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2242                                                                      insn_opcode),
2243                                             MIPS_GR_REG))))
2244               /* If the branch writes a register that the previous
2245                  instruction sets, we can not swap (we know that
2246                  branches write only to RD or to $31).  */
2247               || (! mips_opts.mips16
2248                   && (prev_pinfo & INSN_WRITE_GPR_T)
2249                   && (((pinfo & INSN_WRITE_GPR_D)
2250                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2251                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2252                       || ((pinfo & INSN_WRITE_GPR_31)
2253                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2254                                & OP_MASK_RT)
2255                               == RA))))
2256               || (! mips_opts.mips16
2257                   && (prev_pinfo & INSN_WRITE_GPR_D)
2258                   && (((pinfo & INSN_WRITE_GPR_D)
2259                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2260                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2261                       || ((pinfo & INSN_WRITE_GPR_31)
2262                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2263                                & OP_MASK_RD)
2264                               == RA))))
2265               || (mips_opts.mips16
2266                   && (pinfo & MIPS16_INSN_WRITE_31)
2267                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2268                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2269                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2270                               == RA))))
2271               /* If the branch writes a register that the previous
2272                  instruction reads, we can not swap (we know that
2273                  branches only write to RD or to $31).  */
2274               || (! mips_opts.mips16
2275                   && (pinfo & INSN_WRITE_GPR_D)
2276                   && insn_uses_reg (&prev_insn,
2277                                     ((ip->insn_opcode >> OP_SH_RD)
2278                                      & OP_MASK_RD),
2279                                     MIPS_GR_REG))
2280               || (! mips_opts.mips16
2281                   && (pinfo & INSN_WRITE_GPR_31)
2282                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2283               || (mips_opts.mips16
2284                   && (pinfo & MIPS16_INSN_WRITE_31)
2285                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2286               /* If we are generating embedded PIC code, the branch
2287                  might be expanded into a sequence which uses $at, so
2288                  we can't swap with an instruction which reads it.  */
2289               || (mips_pic == EMBEDDED_PIC
2290                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2291               /* If the previous previous instruction has a load
2292                  delay, and sets a register that the branch reads, we
2293                  can not swap.  */
2294               || (! mips_opts.mips16
2295                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2296               /* Itbl support may require additional care here.  */
2297                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2298                       || (! gpr_interlocks
2299                           && (prev_prev_insn.insn_mo->pinfo
2300                               & INSN_LOAD_MEMORY_DELAY)))
2301                   && insn_uses_reg (ip,
2302                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2303                                      & OP_MASK_RT),
2304                                     MIPS_GR_REG))
2305               /* If one instruction sets a condition code and the
2306                  other one uses a condition code, we can not swap.  */
2307               || ((pinfo & INSN_READ_COND_CODE)
2308                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2309               || ((pinfo & INSN_WRITE_COND_CODE)
2310                   && (prev_pinfo & INSN_READ_COND_CODE))
2311               /* If the previous instruction uses the PC, we can not
2312                  swap.  */
2313               || (mips_opts.mips16
2314                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2315               /* If the previous instruction was extended, we can not
2316                  swap.  */
2317               || (mips_opts.mips16 && prev_insn_extended)
2318               /* If the previous instruction had a fixup in mips16
2319                  mode, we can not swap.  This normally means that the
2320                  previous instruction was a 4 byte branch anyhow.  */
2321               || (mips_opts.mips16 && prev_insn_fixp[0])
2322               /* If the previous instruction is a sync, sync.l, or
2323                  sync.p, we can not swap.  */
2324               || (prev_pinfo & INSN_SYNC))
2325             {
2326               /* We could do even better for unconditional branches to
2327                  portions of this object file; we could pick up the
2328                  instruction at the destination, put it in the delay
2329                  slot, and bump the destination address.  */
2330               emit_nop ();
2331               /* Update the previous insn information.  */
2332               prev_prev_insn = *ip;
2333               prev_insn.insn_mo = &dummy_opcode;
2334             }
2335           else
2336             {
2337               /* It looks like we can actually do the swap.  */
2338               if (! mips_opts.mips16)
2339                 {
2340                   char *prev_f;
2341                   char temp[4];
2342
2343                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2344                   memcpy (temp, prev_f, 4);
2345                   memcpy (prev_f, f, 4);
2346                   memcpy (f, temp, 4);
2347                   if (prev_insn_fixp[0])
2348                     {
2349                       prev_insn_fixp[0]->fx_frag = frag_now;
2350                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2351                     }
2352                   if (prev_insn_fixp[1])
2353                     {
2354                       prev_insn_fixp[1]->fx_frag = frag_now;
2355                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2356                     }
2357                   if (prev_insn_fixp[2])
2358                     {
2359                       prev_insn_fixp[2]->fx_frag = frag_now;
2360                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2361                     }
2362                   if (fixp[0])
2363                     {
2364                       fixp[0]->fx_frag = prev_insn_frag;
2365                       fixp[0]->fx_where = prev_insn_where;
2366                     }
2367                   if (fixp[1])
2368                     {
2369                       fixp[1]->fx_frag = prev_insn_frag;
2370                       fixp[1]->fx_where = prev_insn_where;
2371                     }
2372                   if (fixp[2])
2373                     {
2374                       fixp[2]->fx_frag = prev_insn_frag;
2375                       fixp[2]->fx_where = prev_insn_where;
2376                     }
2377                 }
2378               else
2379                 {
2380                   char *prev_f;
2381                   char temp[2];
2382
2383                   assert (prev_insn_fixp[0] == NULL);
2384                   assert (prev_insn_fixp[1] == NULL);
2385                   assert (prev_insn_fixp[2] == NULL);
2386                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2387                   memcpy (temp, prev_f, 2);
2388                   memcpy (prev_f, f, 2);
2389                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2390                     {
2391                       assert (*reloc_type == BFD_RELOC_UNUSED);
2392                       memcpy (f, temp, 2);
2393                     }
2394                   else
2395                     {
2396                       memcpy (f, f + 2, 2);
2397                       memcpy (f + 2, temp, 2);
2398                     }
2399                   if (fixp[0])
2400                     {
2401                       fixp[0]->fx_frag = prev_insn_frag;
2402                       fixp[0]->fx_where = prev_insn_where;
2403                     }
2404                   if (fixp[1])
2405                     {
2406                       fixp[1]->fx_frag = prev_insn_frag;
2407                       fixp[1]->fx_where = prev_insn_where;
2408                     }
2409                   if (fixp[2])
2410                     {
2411                       fixp[2]->fx_frag = prev_insn_frag;
2412                       fixp[2]->fx_where = prev_insn_where;
2413                     }
2414                 }
2415
2416               /* Update the previous insn information; leave prev_insn
2417                  unchanged.  */
2418               prev_prev_insn = *ip;
2419             }
2420           prev_insn_is_delay_slot = 1;
2421
2422           /* If that was an unconditional branch, forget the previous
2423              insn information.  */
2424           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2425             {
2426               prev_prev_insn.insn_mo = &dummy_opcode;
2427               prev_insn.insn_mo = &dummy_opcode;
2428             }
2429
2430           prev_insn_fixp[0] = NULL;
2431           prev_insn_fixp[1] = NULL;
2432           prev_insn_fixp[2] = NULL;
2433           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2434           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2435           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2436           prev_insn_extended = 0;
2437         }
2438       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2439         {
2440           /* We don't yet optimize a branch likely.  What we should do
2441              is look at the target, copy the instruction found there
2442              into the delay slot, and increment the branch to jump to
2443              the next instruction.  */
2444           emit_nop ();
2445           /* Update the previous insn information.  */
2446           prev_prev_insn = *ip;
2447           prev_insn.insn_mo = &dummy_opcode;
2448           prev_insn_fixp[0] = NULL;
2449           prev_insn_fixp[1] = NULL;
2450           prev_insn_fixp[2] = NULL;
2451           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2452           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2453           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2454           prev_insn_extended = 0;
2455         }
2456       else
2457         {
2458           /* Update the previous insn information.  */
2459           if (nops > 0)
2460             prev_prev_insn.insn_mo = &dummy_opcode;
2461           else
2462             prev_prev_insn = prev_insn;
2463           prev_insn = *ip;
2464
2465           /* Any time we see a branch, we always fill the delay slot
2466              immediately; since this insn is not a branch, we know it
2467              is not in a delay slot.  */
2468           prev_insn_is_delay_slot = 0;
2469
2470           prev_insn_fixp[0] = fixp[0];
2471           prev_insn_fixp[1] = fixp[1];
2472           prev_insn_fixp[2] = fixp[2];
2473           prev_insn_reloc_type[0] = reloc_type[0];
2474           prev_insn_reloc_type[1] = reloc_type[1];
2475           prev_insn_reloc_type[2] = reloc_type[2];
2476           if (mips_opts.mips16)
2477             prev_insn_extended = (ip->use_extend
2478                                   || *reloc_type > BFD_RELOC_UNUSED);
2479         }
2480
2481       prev_prev_insn_unreordered = prev_insn_unreordered;
2482       prev_insn_unreordered = 0;
2483       prev_insn_frag = frag_now;
2484       prev_insn_where = f - frag_now->fr_literal;
2485       prev_insn_valid = 1;
2486     }
2487   else if (place == NULL)
2488     {
2489       /* We need to record a bit of information even when we are not
2490          reordering, in order to determine the base address for mips16
2491          PC relative relocs.  */
2492       prev_prev_insn = prev_insn;
2493       prev_insn = *ip;
2494       prev_insn_reloc_type[0] = reloc_type[0];
2495       prev_insn_reloc_type[1] = reloc_type[1];
2496       prev_insn_reloc_type[2] = reloc_type[2];
2497       prev_prev_insn_unreordered = prev_insn_unreordered;
2498       prev_insn_unreordered = 1;
2499     }
2500
2501   /* We just output an insn, so the next one doesn't have a label.  */
2502   mips_clear_insn_labels ();
2503
2504   /* We must ensure that a fixup associated with an unmatched %hi
2505      reloc does not become a variant frag.  Otherwise, the
2506      rearrangement of %hi relocs in frob_file may confuse
2507      tc_gen_reloc.  */
2508   if (unmatched_hi)
2509     {
2510       frag_wane (frag_now);
2511       frag_new (0);
2512     }
2513 }
2514
2515 /* This function forgets that there was any previous instruction or
2516    label.  If PRESERVE is non-zero, it remembers enough information to
2517    know whether nops are needed before a noreorder section.  */
2518
2519 static void
2520 mips_no_prev_insn (preserve)
2521      int preserve;
2522 {
2523   if (! preserve)
2524     {
2525       prev_insn.insn_mo = &dummy_opcode;
2526       prev_prev_insn.insn_mo = &dummy_opcode;
2527       prev_nop_frag = NULL;
2528       prev_nop_frag_holds = 0;
2529       prev_nop_frag_required = 0;
2530       prev_nop_frag_since = 0;
2531     }
2532   prev_insn_valid = 0;
2533   prev_insn_is_delay_slot = 0;
2534   prev_insn_unreordered = 0;
2535   prev_insn_extended = 0;
2536   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2537   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2538   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2539   prev_prev_insn_unreordered = 0;
2540   mips_clear_insn_labels ();
2541 }
2542
2543 /* This function must be called whenever we turn on noreorder or emit
2544    something other than instructions.  It inserts any NOPS which might
2545    be needed by the previous instruction, and clears the information
2546    kept for the previous instructions.  The INSNS parameter is true if
2547    instructions are to follow.  */
2548
2549 static void
2550 mips_emit_delays (insns)
2551      boolean insns;
2552 {
2553   if (! mips_opts.noreorder)
2554     {
2555       int nops;
2556
2557       nops = 0;
2558       if ((! mips_opts.mips16
2559            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2560            && (! cop_interlocks
2561                && (prev_insn.insn_mo->pinfo
2562                    & (INSN_LOAD_COPROC_DELAY
2563                       | INSN_COPROC_MOVE_DELAY
2564                       | INSN_WRITE_COND_CODE))))
2565           || (! hilo_interlocks
2566               && (prev_insn.insn_mo->pinfo
2567                   & (INSN_READ_LO
2568                      | INSN_READ_HI)))
2569           || (! mips_opts.mips16
2570               && ! gpr_interlocks
2571               && (prev_insn.insn_mo->pinfo
2572                   & INSN_LOAD_MEMORY_DELAY))
2573           || (! mips_opts.mips16
2574               && mips_opts.isa == ISA_MIPS1
2575               && (prev_insn.insn_mo->pinfo
2576                   & INSN_COPROC_MEMORY_DELAY)))
2577         {
2578           /* Itbl support may require additional care here.  */
2579           ++nops;
2580           if ((! mips_opts.mips16
2581                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2582                && (! cop_interlocks
2583                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2584               || (! hilo_interlocks
2585                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2586                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2587             ++nops;
2588
2589           if (prev_insn_unreordered)
2590             nops = 0;
2591         }
2592       else if ((! mips_opts.mips16
2593                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2594                 && (! cop_interlocks
2595                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2596                || (! hilo_interlocks
2597                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2598                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2599         {
2600           /* Itbl support may require additional care here.  */
2601           if (! prev_prev_insn_unreordered)
2602             ++nops;
2603         }
2604
2605       if (nops > 0)
2606         {
2607           struct insn_label_list *l;
2608
2609           if (insns)
2610             {
2611               /* Record the frag which holds the nop instructions, so
2612                  that we can remove them if we don't need them.  */
2613               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2614               prev_nop_frag = frag_now;
2615               prev_nop_frag_holds = nops;
2616               prev_nop_frag_required = 0;
2617               prev_nop_frag_since = 0;
2618             }
2619
2620           for (; nops > 0; --nops)
2621             emit_nop ();
2622
2623           if (insns)
2624             {
2625               /* Move on to a new frag, so that it is safe to simply
2626                  decrease the size of prev_nop_frag.  */
2627               frag_wane (frag_now);
2628               frag_new (0);
2629             }
2630
2631           for (l = insn_labels; l != NULL; l = l->next)
2632             {
2633               valueT val;
2634
2635               assert (S_GET_SEGMENT (l->label) == now_seg);
2636               symbol_set_frag (l->label, frag_now);
2637               val = (valueT) frag_now_fix ();
2638               /* mips16 text labels are stored as odd.  */
2639               if (mips_opts.mips16)
2640                 ++val;
2641               S_SET_VALUE (l->label, val);
2642             }
2643         }
2644     }
2645
2646   /* Mark instruction labels in mips16 mode.  */
2647   if (insns)
2648     mips16_mark_labels ();
2649
2650   mips_no_prev_insn (insns);
2651 }
2652
2653 /* Build an instruction created by a macro expansion.  This is passed
2654    a pointer to the count of instructions created so far, an
2655    expression, the name of the instruction to build, an operand format
2656    string, and corresponding arguments.  */
2657
2658 #ifdef USE_STDARG
2659 static void
2660 macro_build (char *place,
2661              int *counter,
2662              expressionS * ep,
2663              const char *name,
2664              const char *fmt,
2665              ...)
2666 #else
2667 static void
2668 macro_build (place, counter, ep, name, fmt, va_alist)
2669      char *place;
2670      int *counter;
2671      expressionS *ep;
2672      const char *name;
2673      const char *fmt;
2674      va_dcl
2675 #endif
2676 {
2677   struct mips_cl_insn insn;
2678   bfd_reloc_code_real_type r[3];
2679   va_list args;
2680
2681 #ifdef USE_STDARG
2682   va_start (args, fmt);
2683 #else
2684   va_start (args);
2685 #endif
2686
2687   /*
2688    * If the macro is about to expand into a second instruction,
2689    * print a warning if needed. We need to pass ip as a parameter
2690    * to generate a better warning message here...
2691    */
2692   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2693     as_warn (_("Macro instruction expanded into multiple instructions"));
2694
2695   /*
2696    * If the macro is about to expand into a second instruction,
2697    * and it is in a delay slot, print a warning.
2698    */
2699   if (place == NULL
2700       && *counter == 1
2701       && mips_opts.noreorder
2702       && (prev_prev_insn.insn_mo->pinfo
2703           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2704              | INSN_COND_BRANCH_LIKELY)) != 0)
2705     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2706
2707   if (place == NULL)
2708     ++*counter;         /* bump instruction counter */
2709
2710   if (mips_opts.mips16)
2711     {
2712       mips16_macro_build (place, counter, ep, name, fmt, args);
2713       va_end (args);
2714       return;
2715     }
2716
2717   r[0] = BFD_RELOC_UNUSED;
2718   r[1] = BFD_RELOC_UNUSED;
2719   r[2] = BFD_RELOC_UNUSED;
2720   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2721   assert (insn.insn_mo);
2722   assert (strcmp (name, insn.insn_mo->name) == 0);
2723
2724   /* Search until we get a match for NAME.  */
2725   while (1)
2726     {
2727       /* It is assumed here that macros will never generate 
2728          MDMX or MIPS-3D instructions.  */
2729       if (strcmp (fmt, insn.insn_mo->args) == 0
2730           && insn.insn_mo->pinfo != INSN_MACRO
2731           && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2732           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2733         break;
2734
2735       ++insn.insn_mo;
2736       assert (insn.insn_mo->name);
2737       assert (strcmp (name, insn.insn_mo->name) == 0);
2738     }
2739
2740   insn.insn_opcode = insn.insn_mo->match;
2741   for (;;)
2742     {
2743       switch (*fmt++)
2744         {
2745         case '\0':
2746           break;
2747
2748         case ',':
2749         case '(':
2750         case ')':
2751           continue;
2752
2753         case 't':
2754         case 'w':
2755         case 'E':
2756           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2757           continue;
2758
2759         case 'c':
2760           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2761           continue;
2762
2763         case 'T':
2764         case 'W':
2765           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2766           continue;
2767
2768         case 'd':
2769         case 'G':
2770           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2771           continue;
2772
2773         case 'U':
2774           {
2775             int tmp = va_arg (args, int);
2776
2777             insn.insn_opcode |= tmp << OP_SH_RT;
2778             insn.insn_opcode |= tmp << OP_SH_RD;
2779             continue;
2780           }
2781
2782         case 'V':
2783         case 'S':
2784           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2785           continue;
2786
2787         case 'z':
2788           continue;
2789
2790         case '<':
2791           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2792           continue;
2793
2794         case 'D':
2795           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2796           continue;
2797
2798         case 'B':
2799           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2800           continue;
2801
2802         case 'J':
2803           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2804           continue;
2805
2806         case 'q':
2807           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2808           continue;
2809
2810         case 'b':
2811         case 's':
2812         case 'r':
2813         case 'v':
2814           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2815           continue;
2816
2817         case 'i':
2818         case 'j':
2819         case 'o':
2820           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2821           assert (*r == BFD_RELOC_GPREL16
2822                   || *r == BFD_RELOC_MIPS_LITERAL
2823                   || *r == BFD_RELOC_MIPS_HIGHER
2824                   || *r == BFD_RELOC_HI16_S
2825                   || *r == BFD_RELOC_LO16
2826                   || *r == BFD_RELOC_MIPS_GOT16
2827                   || *r == BFD_RELOC_MIPS_CALL16
2828                   || *r == BFD_RELOC_MIPS_GOT_DISP
2829                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2830                   || *r == BFD_RELOC_MIPS_GOT_OFST
2831                   || *r == BFD_RELOC_MIPS_GOT_LO16
2832                   || *r == BFD_RELOC_MIPS_CALL_LO16
2833                   || (ep->X_op == O_subtract
2834                       && *r == BFD_RELOC_PCREL_LO16));
2835           continue;
2836
2837         case 'u':
2838           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2839           assert (ep != NULL
2840                   && (ep->X_op == O_constant
2841                       || (ep->X_op == O_symbol
2842                           && (*r == BFD_RELOC_MIPS_HIGHEST
2843                               || *r == BFD_RELOC_HI16_S
2844                               || *r == BFD_RELOC_HI16
2845                               || *r == BFD_RELOC_GPREL16
2846                               || *r == BFD_RELOC_MIPS_GOT_HI16
2847                               || *r == BFD_RELOC_MIPS_CALL_HI16))
2848                       || (ep->X_op == O_subtract
2849                           && *r == BFD_RELOC_PCREL_HI16_S)));
2850           continue;
2851
2852         case 'p':
2853           assert (ep != NULL);
2854           /*
2855            * This allows macro() to pass an immediate expression for
2856            * creating short branches without creating a symbol.
2857            * Note that the expression still might come from the assembly
2858            * input, in which case the value is not checked for range nor
2859            * is a relocation entry generated (yuck).
2860            */
2861           if (ep->X_op == O_constant)
2862             {
2863               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2864               ep = NULL;
2865             }
2866           else
2867             if (mips_pic == EMBEDDED_PIC)
2868               *r = BFD_RELOC_16_PCREL_S2;
2869             else
2870               *r = BFD_RELOC_16_PCREL;
2871           continue;
2872
2873         case 'a':
2874           assert (ep != NULL);
2875           *r = BFD_RELOC_MIPS_JMP;
2876           continue;
2877
2878         case 'C':
2879           insn.insn_opcode |= va_arg (args, unsigned long);
2880           continue;
2881
2882         default:
2883           internalError ();
2884         }
2885       break;
2886     }
2887   va_end (args);
2888   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2889
2890   append_insn (place, &insn, ep, r, false);
2891 }
2892
2893 static void
2894 mips16_macro_build (place, counter, ep, name, fmt, args)
2895      char *place;
2896      int *counter ATTRIBUTE_UNUSED;
2897      expressionS *ep;
2898      const char *name;
2899      const char *fmt;
2900      va_list args;
2901 {
2902   struct mips_cl_insn insn;
2903   bfd_reloc_code_real_type r[3]
2904     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2905
2906   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2907   assert (insn.insn_mo);
2908   assert (strcmp (name, insn.insn_mo->name) == 0);
2909
2910   while (strcmp (fmt, insn.insn_mo->args) != 0
2911          || insn.insn_mo->pinfo == INSN_MACRO)
2912     {
2913       ++insn.insn_mo;
2914       assert (insn.insn_mo->name);
2915       assert (strcmp (name, insn.insn_mo->name) == 0);
2916     }
2917
2918   insn.insn_opcode = insn.insn_mo->match;
2919   insn.use_extend = false;
2920
2921   for (;;)
2922     {
2923       int c;
2924
2925       c = *fmt++;
2926       switch (c)
2927         {
2928         case '\0':
2929           break;
2930
2931         case ',':
2932         case '(':
2933         case ')':
2934           continue;
2935
2936         case 'y':
2937         case 'w':
2938           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2939           continue;
2940
2941         case 'x':
2942         case 'v':
2943           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2944           continue;
2945
2946         case 'z':
2947           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2948           continue;
2949
2950         case 'Z':
2951           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2952           continue;
2953
2954         case '0':
2955         case 'S':
2956         case 'P':
2957         case 'R':
2958           continue;
2959
2960         case 'X':
2961           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2962           continue;
2963
2964         case 'Y':
2965           {
2966             int regno;
2967
2968             regno = va_arg (args, int);
2969             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2970             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2971           }
2972           continue;
2973
2974         case '<':
2975         case '>':
2976         case '4':
2977         case '5':
2978         case 'H':
2979         case 'W':
2980         case 'D':
2981         case 'j':
2982         case '8':
2983         case 'V':
2984         case 'C':
2985         case 'U':
2986         case 'k':
2987         case 'K':
2988         case 'p':
2989         case 'q':
2990           {
2991             assert (ep != NULL);
2992
2993             if (ep->X_op != O_constant)
2994               *r = (int) BFD_RELOC_UNUSED + c;
2995             else
2996               {
2997                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2998                               false, &insn.insn_opcode, &insn.use_extend,
2999                               &insn.extend);
3000                 ep = NULL;
3001                 *r = BFD_RELOC_UNUSED;
3002               }
3003           }
3004           continue;
3005
3006         case '6':
3007           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3008           continue;
3009         }
3010
3011       break;
3012     }
3013
3014   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3015
3016   append_insn (place, &insn, ep, r, false);
3017 }
3018
3019 /*
3020  * Generate a "jalr" instruction with a relocation hint to the called
3021  * function.  This occurs in NewABI PIC code.
3022  */
3023 static void
3024 macro_build_jalr (icnt, ep)
3025      int icnt;
3026      expressionS *ep;
3027 {
3028   if (HAVE_NEWABI)
3029     frag_more (0);
3030   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3031                RA, PIC_CALL_REG);
3032   if (HAVE_NEWABI)
3033     fix_new_exp (frag_now, 0, 0, ep, false, BFD_RELOC_MIPS_JALR);
3034 }
3035
3036 /*
3037  * Generate a "lui" instruction.
3038  */
3039 static void
3040 macro_build_lui (place, counter, ep, regnum)
3041      char *place;
3042      int *counter;
3043      expressionS *ep;
3044      int regnum;
3045 {
3046   expressionS high_expr;
3047   struct mips_cl_insn insn;
3048   bfd_reloc_code_real_type r[3]
3049     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3050   const char *name = "lui";
3051   const char *fmt = "t,u";
3052
3053   assert (! mips_opts.mips16);
3054
3055   if (place == NULL)
3056     high_expr = *ep;
3057   else
3058     {
3059       high_expr.X_op = O_constant;
3060       high_expr.X_add_number = ep->X_add_number;
3061     }
3062
3063   if (high_expr.X_op == O_constant)
3064     {
3065       /* we can compute the instruction now without a relocation entry */
3066       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3067                                 >> 16) & 0xffff;
3068       *r = BFD_RELOC_UNUSED;
3069     }
3070   else if (! HAVE_NEWABI)
3071     {
3072       assert (ep->X_op == O_symbol);
3073       /* _gp_disp is a special case, used from s_cpload.  */
3074       assert (mips_pic == NO_PIC
3075               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3076       *r = BFD_RELOC_HI16_S;
3077     }
3078
3079   /*
3080    * If the macro is about to expand into a second instruction,
3081    * print a warning if needed. We need to pass ip as a parameter
3082    * to generate a better warning message here...
3083    */
3084   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3085     as_warn (_("Macro instruction expanded into multiple instructions"));
3086
3087   if (place == NULL)
3088     ++*counter;         /* bump instruction counter */
3089
3090   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3091   assert (insn.insn_mo);
3092   assert (strcmp (name, insn.insn_mo->name) == 0);
3093   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3094
3095   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3096   if (*r == BFD_RELOC_UNUSED)
3097     {
3098       insn.insn_opcode |= high_expr.X_add_number;
3099       append_insn (place, &insn, NULL, r, false);
3100     }
3101   else
3102     append_insn (place, &insn, &high_expr, r, false);
3103 }
3104
3105 /*                      set_at()
3106  * Generates code to set the $at register to true (one)
3107  * if reg is less than the immediate expression.
3108  */
3109 static void
3110 set_at (counter, reg, unsignedp)
3111      int *counter;
3112      int reg;
3113      int unsignedp;
3114 {
3115   if (imm_expr.X_op == O_constant
3116       && imm_expr.X_add_number >= -0x8000
3117       && imm_expr.X_add_number < 0x8000)
3118     macro_build ((char *) NULL, counter, &imm_expr,
3119                  unsignedp ? "sltiu" : "slti",
3120                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3121   else
3122     {
3123       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3124       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3125                    unsignedp ? "sltu" : "slt",
3126                    "d,v,t", AT, reg, AT);
3127     }
3128 }
3129
3130 /* Warn if an expression is not a constant.  */
3131
3132 static void
3133 check_absolute_expr (ip, ex)
3134      struct mips_cl_insn *ip;
3135      expressionS *ex;
3136 {
3137   if (ex->X_op == O_big)
3138     as_bad (_("unsupported large constant"));
3139   else if (ex->X_op != O_constant)
3140     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3141 }
3142
3143 /* Count the leading zeroes by performing a binary chop. This is a
3144    bulky bit of source, but performance is a LOT better for the
3145    majority of values than a simple loop to count the bits:
3146        for (lcnt = 0; (lcnt < 32); lcnt++)
3147          if ((v) & (1 << (31 - lcnt)))
3148            break;
3149   However it is not code size friendly, and the gain will drop a bit
3150   on certain cached systems.
3151 */
3152 #define COUNT_TOP_ZEROES(v)             \
3153   (((v) & ~0xffff) == 0                 \
3154    ? ((v) & ~0xff) == 0                 \
3155      ? ((v) & ~0xf) == 0                \
3156        ? ((v) & ~0x3) == 0              \
3157          ? ((v) & ~0x1) == 0            \
3158            ? !(v)                       \
3159              ? 32                       \
3160              : 31                       \
3161            : 30                         \
3162          : ((v) & ~0x7) == 0            \
3163            ? 29                         \
3164            : 28                         \
3165        : ((v) & ~0x3f) == 0             \
3166          ? ((v) & ~0x1f) == 0           \
3167            ? 27                         \
3168            : 26                         \
3169          : ((v) & ~0x7f) == 0           \
3170            ? 25                         \
3171            : 24                         \
3172      : ((v) & ~0xfff) == 0              \
3173        ? ((v) & ~0x3ff) == 0            \
3174          ? ((v) & ~0x1ff) == 0          \
3175            ? 23                         \
3176            : 22                         \
3177          : ((v) & ~0x7ff) == 0          \
3178            ? 21                         \
3179            : 20                         \
3180        : ((v) & ~0x3fff) == 0           \
3181          ? ((v) & ~0x1fff) == 0         \
3182            ? 19                         \
3183            : 18                         \
3184          : ((v) & ~0x7fff) == 0         \
3185            ? 17                         \
3186            : 16                         \
3187    : ((v) & ~0xffffff) == 0             \
3188      ? ((v) & ~0xfffff) == 0            \
3189        ? ((v) & ~0x3ffff) == 0          \
3190          ? ((v) & ~0x1ffff) == 0        \
3191            ? 15                         \
3192            : 14                         \
3193          : ((v) & ~0x7ffff) == 0        \
3194            ? 13                         \
3195            : 12                         \
3196        : ((v) & ~0x3fffff) == 0         \
3197          ? ((v) & ~0x1fffff) == 0       \
3198            ? 11                         \
3199            : 10                         \
3200          : ((v) & ~0x7fffff) == 0       \
3201            ? 9                          \
3202            : 8                          \
3203      : ((v) & ~0xfffffff) == 0          \
3204        ? ((v) & ~0x3ffffff) == 0        \
3205          ? ((v) & ~0x1ffffff) == 0      \
3206            ? 7                          \
3207            : 6                          \
3208          : ((v) & ~0x7ffffff) == 0      \
3209            ? 5                          \
3210            : 4                          \
3211        : ((v) & ~0x3fffffff) == 0       \
3212          ? ((v) & ~0x1fffffff) == 0     \
3213            ? 3                          \
3214            : 2                          \
3215          : ((v) & ~0x7fffffff) == 0     \
3216            ? 1                          \
3217            : 0)
3218
3219 /* Is the given value a sign-extended 32-bit value?  */
3220 #define IS_SEXT_32BIT_NUM(x)                                            \
3221   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
3222    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3223
3224 /*                      load_register()
3225  *  This routine generates the least number of instructions neccessary to load
3226  *  an absolute expression value into a register.
3227  */
3228 static void
3229 load_register (counter, reg, ep, dbl)
3230      int *counter;
3231      int reg;
3232      expressionS *ep;
3233      int dbl;
3234 {
3235   int freg;
3236   expressionS hi32, lo32;
3237
3238   if (ep->X_op != O_big)
3239     {
3240       assert (ep->X_op == O_constant);
3241       if (ep->X_add_number < 0x8000
3242           && (ep->X_add_number >= 0
3243               || (ep->X_add_number >= -0x8000
3244                   && (! dbl
3245                       || ! ep->X_unsigned
3246                       || sizeof (ep->X_add_number) > 4))))
3247         {
3248           /* We can handle 16 bit signed values with an addiu to
3249              $zero.  No need to ever use daddiu here, since $zero and
3250              the result are always correct in 32 bit mode.  */
3251           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3252                        (int) BFD_RELOC_LO16);
3253           return;
3254         }
3255       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3256         {
3257           /* We can handle 16 bit unsigned values with an ori to
3258              $zero.  */
3259           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3260                        (int) BFD_RELOC_LO16);
3261           return;
3262         }
3263       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3264                 && (! dbl
3265                     || ! ep->X_unsigned
3266                     || sizeof (ep->X_add_number) > 4
3267                     || (ep->X_add_number & 0x80000000) == 0))
3268                || ((HAVE_32BIT_GPRS || ! dbl)
3269                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3270                || (HAVE_32BIT_GPRS
3271                    && ! dbl
3272                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3273                        == ~ (offsetT) 0xffffffff)))
3274         {
3275           /* 32 bit values require an lui.  */
3276           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3277                        (int) BFD_RELOC_HI16);
3278           if ((ep->X_add_number & 0xffff) != 0)
3279             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3280                          (int) BFD_RELOC_LO16);
3281           return;
3282         }
3283     }
3284
3285   /* The value is larger than 32 bits.  */
3286
3287   if (HAVE_32BIT_GPRS)
3288     {
3289       as_bad (_("Number (0x%lx) larger than 32 bits"),
3290               (unsigned long) ep->X_add_number);
3291       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3292                    (int) BFD_RELOC_LO16);
3293       return;
3294     }
3295
3296   if (ep->X_op != O_big)
3297     {
3298       hi32 = *ep;
3299       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3300       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3301       hi32.X_add_number &= 0xffffffff;
3302       lo32 = *ep;
3303       lo32.X_add_number &= 0xffffffff;
3304     }
3305   else
3306     {
3307       assert (ep->X_add_number > 2);
3308       if (ep->X_add_number == 3)
3309         generic_bignum[3] = 0;
3310       else if (ep->X_add_number > 4)
3311         as_bad (_("Number larger than 64 bits"));
3312       lo32.X_op = O_constant;
3313       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3314       hi32.X_op = O_constant;
3315       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3316     }
3317
3318   if (hi32.X_add_number == 0)
3319     freg = 0;
3320   else
3321     {
3322       int shift, bit;
3323       unsigned long hi, lo;
3324
3325       if (hi32.X_add_number == (offsetT) 0xffffffff)
3326         {
3327           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3328             {
3329               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3330                            reg, 0, (int) BFD_RELOC_LO16);
3331               return;
3332             }
3333           if (lo32.X_add_number & 0x80000000)
3334             {
3335               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3336                            (int) BFD_RELOC_HI16);
3337               if (lo32.X_add_number & 0xffff)
3338                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3339                              reg, reg, (int) BFD_RELOC_LO16);
3340               return;
3341             }
3342         }
3343
3344       /* Check for 16bit shifted constant.  We know that hi32 is
3345          non-zero, so start the mask on the first bit of the hi32
3346          value.  */
3347       shift = 17;
3348       do
3349         {
3350           unsigned long himask, lomask;
3351
3352           if (shift < 32)
3353             {
3354               himask = 0xffff >> (32 - shift);
3355               lomask = (0xffff << shift) & 0xffffffff;
3356             }
3357           else
3358             {
3359               himask = 0xffff << (shift - 32);
3360               lomask = 0;
3361             }
3362           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3363               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3364             {
3365               expressionS tmp;
3366
3367               tmp.X_op = O_constant;
3368               if (shift < 32)
3369                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3370                                     | (lo32.X_add_number >> shift));
3371               else
3372                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3373               macro_build ((char *) NULL, counter, &tmp,
3374                            "ori", "t,r,i", reg, 0,
3375                            (int) BFD_RELOC_LO16);
3376               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3377                            (shift >= 32) ? "dsll32" : "dsll",
3378                            "d,w,<", reg, reg,
3379                            (shift >= 32) ? shift - 32 : shift);
3380               return;
3381             }
3382           ++shift;
3383         }
3384       while (shift <= (64 - 16));
3385
3386       /* Find the bit number of the lowest one bit, and store the
3387          shifted value in hi/lo.  */
3388       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3389       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3390       if (lo != 0)
3391         {
3392           bit = 0;
3393           while ((lo & 1) == 0)
3394             {
3395               lo >>= 1;
3396               ++bit;
3397             }
3398           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3399           hi >>= bit;
3400         }
3401       else
3402         {
3403           bit = 32;
3404           while ((hi & 1) == 0)
3405             {
3406               hi >>= 1;
3407               ++bit;
3408             }
3409           lo = hi;
3410           hi = 0;
3411         }
3412
3413       /* Optimize if the shifted value is a (power of 2) - 1.  */
3414       if ((hi == 0 && ((lo + 1) & lo) == 0)
3415           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3416         {
3417           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3418           if (shift != 0)
3419             {
3420               expressionS tmp;
3421
3422               /* This instruction will set the register to be all
3423                  ones.  */
3424               tmp.X_op = O_constant;
3425               tmp.X_add_number = (offsetT) -1;
3426               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3427                            reg, 0, (int) BFD_RELOC_LO16);
3428               if (bit != 0)
3429                 {
3430                   bit += shift;
3431                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3432                                (bit >= 32) ? "dsll32" : "dsll",
3433                                "d,w,<", reg, reg,
3434                                (bit >= 32) ? bit - 32 : bit);
3435                 }
3436               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3437                            (shift >= 32) ? "dsrl32" : "dsrl",
3438                            "d,w,<", reg, reg,
3439                            (shift >= 32) ? shift - 32 : shift);
3440               return;
3441             }
3442         }
3443
3444       /* Sign extend hi32 before calling load_register, because we can
3445          generally get better code when we load a sign extended value.  */
3446       if ((hi32.X_add_number & 0x80000000) != 0)
3447         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3448       load_register (counter, reg, &hi32, 0);
3449       freg = reg;
3450     }
3451   if ((lo32.X_add_number & 0xffff0000) == 0)
3452     {
3453       if (freg != 0)
3454         {
3455           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3456                        "dsll32", "d,w,<", reg, freg, 0);
3457           freg = reg;
3458         }
3459     }
3460   else
3461     {
3462       expressionS mid16;
3463
3464       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3465         {
3466           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3467                        (int) BFD_RELOC_HI16);
3468           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3469                        "dsrl32", "d,w,<", reg, reg, 0);
3470           return;
3471         }
3472
3473       if (freg != 0)
3474         {
3475           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3476                        "d,w,<", reg, freg, 16);
3477           freg = reg;
3478         }
3479       mid16 = lo32;
3480       mid16.X_add_number >>= 16;
3481       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3482                    freg, (int) BFD_RELOC_LO16);
3483       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3484                    "d,w,<", reg, reg, 16);
3485       freg = reg;
3486     }
3487   if ((lo32.X_add_number & 0xffff) != 0)
3488     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3489                  (int) BFD_RELOC_LO16);
3490 }
3491
3492 /* Load an address into a register.  */
3493
3494 static void
3495 load_address (counter, reg, ep, used_at)
3496      int *counter;
3497      int reg;
3498      expressionS *ep;
3499      int *used_at;
3500 {
3501   char *p = NULL;
3502
3503   if (ep->X_op != O_constant
3504       && ep->X_op != O_symbol)
3505     {
3506       as_bad (_("expression too complex"));
3507       ep->X_op = O_constant;
3508     }
3509
3510   if (ep->X_op == O_constant)
3511     {
3512       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3513       return;
3514     }
3515
3516   if (mips_pic == NO_PIC)
3517     {
3518       /* If this is a reference to a GP relative symbol, we want
3519            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3520          Otherwise we want
3521            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3522            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3523          If we have an addend, we always use the latter form.
3524
3525          With 64bit address space and a usable $at we want
3526            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3527            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3528            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3529            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3530            dsll32       $reg,0
3531            dadd         $reg,$reg,$at
3532
3533          If $at is already in use, we use an path which is suboptimal
3534          on superscalar processors.
3535            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3536            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3537            dsll         $reg,16
3538            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3539            dsll         $reg,16
3540            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3541        */
3542       if (HAVE_64BIT_ADDRESSES)
3543         {
3544           /* We don't do GP optimization for now because RELAX_ENCODE can't
3545              hold the data for such large chunks.  */
3546
3547           if (*used_at == 0)
3548             {
3549               macro_build (p, counter, ep, "lui", "t,u",
3550                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3551               macro_build (p, counter, ep, "lui", "t,u",
3552                            AT, (int) BFD_RELOC_HI16_S);
3553               macro_build (p, counter, ep, "daddiu", "t,r,j",
3554                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3555               macro_build (p, counter, ep, "daddiu", "t,r,j",
3556                            AT, AT, (int) BFD_RELOC_LO16);
3557               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3558                            "d,w,<", reg, reg, 0);
3559               macro_build (p, counter, (expressionS *) NULL, "dadd",
3560                            "d,v,t", reg, reg, AT);
3561               *used_at = 1;
3562             }
3563           else
3564             {
3565               macro_build (p, counter, ep, "lui", "t,u",
3566                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3567               macro_build (p, counter, ep, "daddiu", "t,r,j",
3568                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3569               macro_build (p, counter, (expressionS *) NULL, "dsll",
3570                            "d,w,<", reg, reg, 16);
3571               macro_build (p, counter, ep, "daddiu", "t,r,j",
3572                            reg, reg, (int) BFD_RELOC_HI16_S);
3573               macro_build (p, counter, (expressionS *) NULL, "dsll",
3574                            "d,w,<", reg, reg, 16);
3575               macro_build (p, counter, ep, "daddiu", "t,r,j",
3576                            reg, reg, (int) BFD_RELOC_LO16);
3577             }
3578         }
3579       else
3580         {
3581           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3582               && ! nopic_need_relax (ep->X_add_symbol, 1))
3583             {
3584               frag_grow (20);
3585               macro_build ((char *) NULL, counter, ep,
3586                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3587                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3588               p = frag_var (rs_machine_dependent, 8, 0,
3589                             RELAX_ENCODE (4, 8, 0, 4, 0,
3590                                           mips_opts.warn_about_macros),
3591                             ep->X_add_symbol, 0, NULL);
3592             }
3593           macro_build_lui (p, counter, ep, reg);
3594           if (p != NULL)
3595             p += 4;
3596           macro_build (p, counter, ep,
3597                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3598                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3599         }
3600     }
3601   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3602     {
3603       expressionS ex;
3604
3605       /* If this is a reference to an external symbol, we want
3606            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3607          Otherwise we want
3608            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3609            nop
3610            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3611          If there is a constant, it must be added in after.  */
3612       ex.X_add_number = ep->X_add_number;
3613       ep->X_add_number = 0;
3614       frag_grow (20);
3615       macro_build ((char *) NULL, counter, ep,
3616                    HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3617                    reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3618       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3619       p = frag_var (rs_machine_dependent, 4, 0,
3620                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3621                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3622       macro_build (p, counter, ep,
3623                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3624                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3625       if (ex.X_add_number != 0)
3626         {
3627           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3628             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3629           ex.X_op = O_constant;
3630           macro_build ((char *) NULL, counter, &ex,
3631                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3632                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3633         }
3634     }
3635   else if (mips_pic == SVR4_PIC)
3636     {
3637       expressionS ex;
3638       int off;
3639
3640       /* This is the large GOT case.  If this is a reference to an
3641          external symbol, we want
3642            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3643            addu         $reg,$reg,$gp
3644            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3645          Otherwise, for a reference to a local symbol, we want
3646            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3647            nop
3648            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3649          If we have NewABI, we want
3650            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3651            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3652          If there is a constant, it must be added in after.  */
3653       ex.X_add_number = ep->X_add_number;
3654       ep->X_add_number = 0;
3655       if (HAVE_NEWABI)
3656         {
3657           macro_build ((char *) NULL, counter, ep,
3658                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3659                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3660           macro_build (p, counter, ep,
3661                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3662                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3663         }
3664       else
3665         {
3666           if (reg_needs_delay (mips_gp_register))
3667             off = 4;
3668           else
3669             off = 0;
3670           frag_grow (32);
3671           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3672                        (int) BFD_RELOC_MIPS_GOT_HI16);
3673           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3674                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3675                        reg, mips_gp_register);
3676           macro_build ((char *) NULL, counter, ep,
3677                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3678                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3679           p = frag_var (rs_machine_dependent, 12 + off, 0,
3680                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3681                                       mips_opts.warn_about_macros),
3682                         ep->X_add_symbol, 0, NULL);
3683           if (off > 0)
3684             {
3685               /* We need a nop before loading from $gp.  This special
3686                  check is required because the lui which starts the main
3687                  instruction stream does not refer to $gp, and so will not
3688                  insert the nop which may be required.  */
3689               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3690                 p += 4;
3691             }
3692           macro_build (p, counter, ep,
3693                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3694                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3695           p += 4;
3696           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3697           p += 4;
3698           macro_build (p, counter, ep,
3699                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3700                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3701         }
3702
3703       if (ex.X_add_number != 0)
3704         {
3705           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3706             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3707           ex.X_op = O_constant;
3708           macro_build ((char *) NULL, counter, &ex,
3709                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3710                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3711         }
3712     }
3713   else if (mips_pic == EMBEDDED_PIC)
3714     {
3715       /* We always do
3716            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3717        */
3718       macro_build ((char *) NULL, counter, ep,
3719                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3720                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3721     }
3722   else
3723     abort ();
3724 }
3725
3726 /* Move the contents of register SOURCE into register DEST.  */
3727
3728 static void
3729 move_register (counter, dest, source)
3730      int *counter;
3731      int dest;
3732      int source;
3733 {
3734   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3735                HAVE_32BIT_GPRS ? "addu" : "daddu",
3736                "d,v,t", dest, source, 0);
3737 }
3738
3739 /*
3740  *                      Build macros
3741  *   This routine implements the seemingly endless macro or synthesized
3742  * instructions and addressing modes in the mips assembly language. Many
3743  * of these macros are simple and are similar to each other. These could
3744  * probably be handled by some kind of table or grammer aproach instead of
3745  * this verbose method. Others are not simple macros but are more like
3746  * optimizing code generation.
3747  *   One interesting optimization is when several store macros appear
3748  * consecutivly that would load AT with the upper half of the same address.
3749  * The ensuing load upper instructions are ommited. This implies some kind
3750  * of global optimization. We currently only optimize within a single macro.
3751  *   For many of the load and store macros if the address is specified as a
3752  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3753  * first load register 'at' with zero and use it as the base register. The
3754  * mips assembler simply uses register $zero. Just one tiny optimization
3755  * we're missing.
3756  */
3757 static void
3758 macro (ip)
3759      struct mips_cl_insn *ip;
3760 {
3761   register int treg, sreg, dreg, breg;
3762   int tempreg;
3763   int mask;
3764   int icnt = 0;
3765   int used_at = 0;
3766   expressionS expr1;
3767   const char *s;
3768   const char *s2;
3769   const char *fmt;
3770   int likely = 0;
3771   int dbl = 0;
3772   int coproc = 0;
3773   int lr = 0;
3774   int imm = 0;
3775   offsetT maxnum;
3776   int off;
3777   bfd_reloc_code_real_type r;
3778   int hold_mips_optimize;
3779
3780   assert (! mips_opts.mips16);
3781
3782   treg = (ip->insn_opcode >> 16) & 0x1f;
3783   dreg = (ip->insn_opcode >> 11) & 0x1f;
3784   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3785   mask = ip->insn_mo->mask;
3786
3787   expr1.X_op = O_constant;
3788   expr1.X_op_symbol = NULL;
3789   expr1.X_add_symbol = NULL;
3790   expr1.X_add_number = 1;
3791
3792   switch (mask)
3793     {
3794     case M_DABS:
3795       dbl = 1;
3796     case M_ABS:
3797       /* bgez $a0,.+12
3798          move v0,$a0
3799          sub v0,$zero,$a0
3800          */
3801
3802       mips_emit_delays (true);
3803       ++mips_opts.noreorder;
3804       mips_any_noreorder = 1;
3805
3806       expr1.X_add_number = 8;
3807       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3808       if (dreg == sreg)
3809         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3810                      0);
3811       else
3812         move_register (&icnt, dreg, sreg);
3813       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3814                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3815
3816       --mips_opts.noreorder;
3817       return;
3818
3819     case M_ADD_I:
3820       s = "addi";
3821       s2 = "add";
3822       goto do_addi;
3823     case M_ADDU_I:
3824       s = "addiu";
3825       s2 = "addu";
3826       goto do_addi;
3827     case M_DADD_I:
3828       dbl = 1;
3829       s = "daddi";
3830       s2 = "dadd";
3831       goto do_addi;
3832     case M_DADDU_I:
3833       dbl = 1;
3834       s = "daddiu";
3835       s2 = "daddu";
3836     do_addi:
3837       if (imm_expr.X_op == O_constant
3838           && imm_expr.X_add_number >= -0x8000
3839           && imm_expr.X_add_number < 0x8000)
3840         {
3841           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3842                        (int) BFD_RELOC_LO16);
3843           return;
3844         }
3845       load_register (&icnt, AT, &imm_expr, dbl);
3846       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3847                    treg, sreg, AT);
3848       break;
3849
3850     case M_AND_I:
3851       s = "andi";
3852       s2 = "and";
3853       goto do_bit;
3854     case M_OR_I:
3855       s = "ori";
3856       s2 = "or";
3857       goto do_bit;
3858     case M_NOR_I:
3859       s = "";
3860       s2 = "nor";
3861       goto do_bit;
3862     case M_XOR_I:
3863       s = "xori";
3864       s2 = "xor";
3865     do_bit:
3866       if (imm_expr.X_op == O_constant
3867           && imm_expr.X_add_number >= 0
3868           && imm_expr.X_add_number < 0x10000)
3869         {
3870           if (mask != M_NOR_I)
3871             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3872                          sreg, (int) BFD_RELOC_LO16);
3873           else
3874             {
3875               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3876                            treg, sreg, (int) BFD_RELOC_LO16);
3877               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3878                            "d,v,t", treg, treg, 0);
3879             }
3880           return;
3881         }
3882
3883       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3884       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3885                    treg, sreg, AT);
3886       break;
3887
3888     case M_BEQ_I:
3889       s = "beq";
3890       goto beq_i;
3891     case M_BEQL_I:
3892       s = "beql";
3893       likely = 1;
3894       goto beq_i;
3895     case M_BNE_I:
3896       s = "bne";
3897       goto beq_i;
3898     case M_BNEL_I:
3899       s = "bnel";
3900       likely = 1;
3901     beq_i:
3902       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3903         {
3904           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3905                        0);
3906           return;
3907         }
3908       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3909       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3910       break;
3911
3912     case M_BGEL:
3913       likely = 1;
3914     case M_BGE:
3915       if (treg == 0)
3916         {
3917           macro_build ((char *) NULL, &icnt, &offset_expr,
3918                        likely ? "bgezl" : "bgez", "s,p", sreg);
3919           return;
3920         }
3921       if (sreg == 0)
3922         {
3923           macro_build ((char *) NULL, &icnt, &offset_expr,
3924                        likely ? "blezl" : "blez", "s,p", treg);
3925           return;
3926         }
3927       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3928                    AT, sreg, treg);
3929       macro_build ((char *) NULL, &icnt, &offset_expr,
3930                    likely ? "beql" : "beq", "s,t,p", AT, 0);
3931       break;
3932
3933     case M_BGTL_I:
3934       likely = 1;
3935     case M_BGT_I:
3936       /* check for > max integer */
3937       maxnum = 0x7fffffff;
3938       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3939         {
3940           maxnum <<= 16;
3941           maxnum |= 0xffff;
3942           maxnum <<= 16;
3943           maxnum |= 0xffff;
3944         }
3945       if (imm_expr.X_op == O_constant
3946           && imm_expr.X_add_number >= maxnum
3947           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3948         {
3949         do_false:
3950           /* result is always false */
3951           if (! likely)
3952             {
3953               if (warn_nops)
3954                 as_warn (_("Branch %s is always false (nop)"),
3955                          ip->insn_mo->name);
3956               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3957                            "", 0);
3958             }
3959           else
3960             {
3961               if (warn_nops)
3962                 as_warn (_("Branch likely %s is always false"),
3963                          ip->insn_mo->name);
3964               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3965                            "s,t,p", 0, 0);
3966             }
3967           return;
3968         }
3969       if (imm_expr.X_op != O_constant)
3970         as_bad (_("Unsupported large constant"));
3971       ++imm_expr.X_add_number;
3972       /* FALLTHROUGH */
3973     case M_BGE_I:
3974     case M_BGEL_I:
3975       if (mask == M_BGEL_I)
3976         likely = 1;
3977       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3978         {
3979           macro_build ((char *) NULL, &icnt, &offset_expr,
3980                        likely ? "bgezl" : "bgez", "s,p", sreg);
3981           return;
3982         }
3983       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3984         {
3985           macro_build ((char *) NULL, &icnt, &offset_expr,
3986                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
3987           return;
3988         }
3989       maxnum = 0x7fffffff;
3990       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3991         {
3992           maxnum <<= 16;
3993           maxnum |= 0xffff;
3994           maxnum <<= 16;
3995           maxnum |= 0xffff;
3996         }
3997       maxnum = - maxnum - 1;
3998       if (imm_expr.X_op == O_constant
3999           && imm_expr.X_add_number <= maxnum
4000           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4001         {
4002         do_true:
4003           /* result is always true */
4004           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4005           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4006           return;
4007         }
4008       set_at (&icnt, sreg, 0);
4009       macro_build ((char *) NULL, &icnt, &offset_expr,
4010                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4011       break;
4012
4013     case M_BGEUL:
4014       likely = 1;
4015     case M_BGEU:
4016       if (treg == 0)
4017         goto do_true;
4018       if (sreg == 0)
4019         {
4020           macro_build ((char *) NULL, &icnt, &offset_expr,
4021                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4022           return;
4023         }
4024       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4025                    "d,v,t", AT, sreg, treg);
4026       macro_build ((char *) NULL, &icnt, &offset_expr,
4027                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4028       break;
4029
4030     case M_BGTUL_I:
4031       likely = 1;
4032     case M_BGTU_I:
4033       if (sreg == 0
4034           || (HAVE_32BIT_GPRS
4035               && imm_expr.X_op == O_constant
4036               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4037         goto do_false;
4038       if (imm_expr.X_op != O_constant)
4039         as_bad (_("Unsupported large constant"));
4040       ++imm_expr.X_add_number;
4041       /* FALLTHROUGH */
4042     case M_BGEU_I:
4043     case M_BGEUL_I:
4044       if (mask == M_BGEUL_I)
4045         likely = 1;
4046       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4047         goto do_true;
4048       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4049         {
4050           macro_build ((char *) NULL, &icnt, &offset_expr,
4051                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4052           return;
4053         }
4054       set_at (&icnt, sreg, 1);
4055       macro_build ((char *) NULL, &icnt, &offset_expr,
4056                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4057       break;
4058
4059     case M_BGTL:
4060       likely = 1;
4061     case M_BGT:
4062       if (treg == 0)
4063         {
4064           macro_build ((char *) NULL, &icnt, &offset_expr,
4065                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4066           return;
4067         }
4068       if (sreg == 0)
4069         {
4070           macro_build ((char *) NULL, &icnt, &offset_expr,
4071                        likely ? "bltzl" : "bltz", "s,p", treg);
4072           return;
4073         }
4074       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4075                    AT, treg, sreg);
4076       macro_build ((char *) NULL, &icnt, &offset_expr,
4077                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4078       break;
4079
4080     case M_BGTUL:
4081       likely = 1;
4082     case M_BGTU:
4083       if (treg == 0)
4084         {
4085           macro_build ((char *) NULL, &icnt, &offset_expr,
4086                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4087           return;
4088         }
4089       if (sreg == 0)
4090         goto do_false;
4091       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4092                    "d,v,t", AT, treg, sreg);
4093       macro_build ((char *) NULL, &icnt, &offset_expr,
4094                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4095       break;
4096
4097     case M_BLEL:
4098       likely = 1;
4099     case M_BLE:
4100       if (treg == 0)
4101         {
4102           macro_build ((char *) NULL, &icnt, &offset_expr,
4103                        likely ? "blezl" : "blez", "s,p", sreg);
4104           return;
4105         }
4106       if (sreg == 0)
4107         {
4108           macro_build ((char *) NULL, &icnt, &offset_expr,
4109                        likely ? "bgezl" : "bgez", "s,p", treg);
4110           return;
4111         }
4112       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4113                    AT, treg, sreg);
4114       macro_build ((char *) NULL, &icnt, &offset_expr,
4115                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4116       break;
4117
4118     case M_BLEL_I:
4119       likely = 1;
4120     case M_BLE_I:
4121       maxnum = 0x7fffffff;
4122       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4123         {
4124           maxnum <<= 16;
4125           maxnum |= 0xffff;
4126           maxnum <<= 16;
4127           maxnum |= 0xffff;
4128         }
4129       if (imm_expr.X_op == O_constant
4130           && imm_expr.X_add_number >= maxnum
4131           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4132         goto do_true;
4133       if (imm_expr.X_op != O_constant)
4134         as_bad (_("Unsupported large constant"));
4135       ++imm_expr.X_add_number;
4136       /* FALLTHROUGH */
4137     case M_BLT_I:
4138     case M_BLTL_I:
4139       if (mask == M_BLTL_I)
4140         likely = 1;
4141       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4142         {
4143           macro_build ((char *) NULL, &icnt, &offset_expr,
4144                        likely ? "bltzl" : "bltz", "s,p", sreg);
4145           return;
4146         }
4147       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4148         {
4149           macro_build ((char *) NULL, &icnt, &offset_expr,
4150                        likely ? "blezl" : "blez", "s,p", sreg);
4151           return;
4152         }
4153       set_at (&icnt, sreg, 0);
4154       macro_build ((char *) NULL, &icnt, &offset_expr,
4155                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4156       break;
4157
4158     case M_BLEUL:
4159       likely = 1;
4160     case M_BLEU:
4161       if (treg == 0)
4162         {
4163           macro_build ((char *) NULL, &icnt, &offset_expr,
4164                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4165           return;
4166         }
4167       if (sreg == 0)
4168         goto do_true;
4169       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4170                    "d,v,t", AT, treg, sreg);
4171       macro_build ((char *) NULL, &icnt, &offset_expr,
4172                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4173       break;
4174
4175     case M_BLEUL_I:
4176       likely = 1;
4177     case M_BLEU_I:
4178       if (sreg == 0
4179           || (HAVE_32BIT_GPRS
4180               && imm_expr.X_op == O_constant
4181               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4182         goto do_true;
4183       if (imm_expr.X_op != O_constant)
4184         as_bad (_("Unsupported large constant"));
4185       ++imm_expr.X_add_number;
4186       /* FALLTHROUGH */
4187     case M_BLTU_I:
4188     case M_BLTUL_I:
4189       if (mask == M_BLTUL_I)
4190         likely = 1;
4191       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4192         goto do_false;
4193       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4194         {
4195           macro_build ((char *) NULL, &icnt, &offset_expr,
4196                        likely ? "beql" : "beq",
4197                        "s,t,p", sreg, 0);
4198           return;
4199         }
4200       set_at (&icnt, sreg, 1);
4201       macro_build ((char *) NULL, &icnt, &offset_expr,
4202                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4203       break;
4204
4205     case M_BLTL:
4206       likely = 1;
4207     case M_BLT:
4208       if (treg == 0)
4209         {
4210           macro_build ((char *) NULL, &icnt, &offset_expr,
4211                        likely ? "bltzl" : "bltz", "s,p", sreg);
4212           return;
4213         }
4214       if (sreg == 0)
4215         {
4216           macro_build ((char *) NULL, &icnt, &offset_expr,
4217                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4218           return;
4219         }
4220       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4221                    AT, sreg, treg);
4222       macro_build ((char *) NULL, &icnt, &offset_expr,
4223                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4224       break;
4225
4226     case M_BLTUL:
4227       likely = 1;
4228     case M_BLTU:
4229       if (treg == 0)
4230         goto do_false;
4231       if (sreg == 0)
4232         {
4233           macro_build ((char *) NULL, &icnt, &offset_expr,
4234                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4235           return;
4236         }
4237       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4238                    "d,v,t", AT, sreg,
4239                    treg);
4240       macro_build ((char *) NULL, &icnt, &offset_expr,
4241                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4242       break;
4243
4244     case M_DDIV_3:
4245       dbl = 1;
4246     case M_DIV_3:
4247       s = "mflo";
4248       goto do_div3;
4249     case M_DREM_3:
4250       dbl = 1;
4251     case M_REM_3:
4252       s = "mfhi";
4253     do_div3:
4254       if (treg == 0)
4255         {
4256           as_warn (_("Divide by zero."));
4257           if (mips_trap)
4258             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4259                          "s,t", 0, 0);
4260           else
4261             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4262                          "c", 7);
4263           return;
4264         }
4265
4266       mips_emit_delays (true);
4267       ++mips_opts.noreorder;
4268       mips_any_noreorder = 1;
4269       if (mips_trap)
4270         {
4271           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4272                        "s,t", treg, 0);
4273           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4274                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4275         }
4276       else
4277         {
4278           expr1.X_add_number = 8;
4279           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4280           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4281                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4282           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4283                        "c", 7);
4284         }
4285       expr1.X_add_number = -1;
4286       macro_build ((char *) NULL, &icnt, &expr1,
4287                    dbl ? "daddiu" : "addiu",
4288                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4289       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4290       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4291       if (dbl)
4292         {
4293           expr1.X_add_number = 1;
4294           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4295                        (int) BFD_RELOC_LO16);
4296           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4297                        "d,w,<", AT, AT, 31);
4298         }
4299       else
4300         {
4301           expr1.X_add_number = 0x80000000;
4302           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4303                        (int) BFD_RELOC_HI16);
4304         }
4305       if (mips_trap)
4306         {
4307           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4308                        "s,t", sreg, AT);
4309           /* We want to close the noreorder block as soon as possible, so
4310              that later insns are available for delay slot filling.  */
4311           --mips_opts.noreorder;
4312         }
4313       else
4314         {
4315           expr1.X_add_number = 8;
4316           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4317           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4318                        0);
4319
4320           /* We want to close the noreorder block as soon as possible, so
4321              that later insns are available for delay slot filling.  */
4322           --mips_opts.noreorder;
4323
4324           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4325                        "c", 6);
4326         }
4327       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4328       break;
4329
4330     case M_DIV_3I:
4331       s = "div";
4332       s2 = "mflo";
4333       goto do_divi;
4334     case M_DIVU_3I:
4335       s = "divu";
4336       s2 = "mflo";
4337       goto do_divi;
4338     case M_REM_3I:
4339       s = "div";
4340       s2 = "mfhi";
4341       goto do_divi;
4342     case M_REMU_3I:
4343       s = "divu";
4344       s2 = "mfhi";
4345       goto do_divi;
4346     case M_DDIV_3I:
4347       dbl = 1;
4348       s = "ddiv";
4349       s2 = "mflo";
4350       goto do_divi;
4351     case M_DDIVU_3I:
4352       dbl = 1;
4353       s = "ddivu";
4354       s2 = "mflo";
4355       goto do_divi;
4356     case M_DREM_3I:
4357       dbl = 1;
4358       s = "ddiv";
4359       s2 = "mfhi";
4360       goto do_divi;
4361     case M_DREMU_3I:
4362       dbl = 1;
4363       s = "ddivu";
4364       s2 = "mfhi";
4365     do_divi:
4366       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4367         {
4368           as_warn (_("Divide by zero."));
4369           if (mips_trap)
4370             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4371                          "s,t", 0, 0);
4372           else
4373             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4374                          "c", 7);
4375           return;
4376         }
4377       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4378         {
4379           if (strcmp (s2, "mflo") == 0)
4380             move_register (&icnt, dreg, sreg);
4381           else
4382             move_register (&icnt, dreg, 0);
4383           return;
4384         }
4385       if (imm_expr.X_op == O_constant
4386           && imm_expr.X_add_number == -1
4387           && s[strlen (s) - 1] != 'u')
4388         {
4389           if (strcmp (s2, "mflo") == 0)
4390             {
4391               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4392                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4393             }
4394           else
4395             move_register (&icnt, dreg, 0);
4396           return;
4397         }
4398
4399       load_register (&icnt, AT, &imm_expr, dbl);
4400       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4401                    sreg, AT);
4402       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4403       break;
4404
4405     case M_DIVU_3:
4406       s = "divu";
4407       s2 = "mflo";
4408       goto do_divu3;
4409     case M_REMU_3:
4410       s = "divu";
4411       s2 = "mfhi";
4412       goto do_divu3;
4413     case M_DDIVU_3:
4414       s = "ddivu";
4415       s2 = "mflo";
4416       goto do_divu3;
4417     case M_DREMU_3:
4418       s = "ddivu";
4419       s2 = "mfhi";
4420     do_divu3:
4421       mips_emit_delays (true);
4422       ++mips_opts.noreorder;
4423       mips_any_noreorder = 1;
4424       if (mips_trap)
4425         {
4426           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4427                        "s,t", treg, 0);
4428           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4429                        sreg, treg);
4430           /* We want to close the noreorder block as soon as possible, so
4431              that later insns are available for delay slot filling.  */
4432           --mips_opts.noreorder;
4433         }
4434       else
4435         {
4436           expr1.X_add_number = 8;
4437           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4438           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4439                        sreg, treg);
4440
4441           /* We want to close the noreorder block as soon as possible, so
4442              that later insns are available for delay slot filling.  */
4443           --mips_opts.noreorder;
4444           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4445                        "c", 7);
4446         }
4447       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4448       return;
4449
4450     case M_DLA_AB:
4451       dbl = 1;
4452     case M_LA_AB:
4453       /* Load the address of a symbol into a register.  If breg is not
4454          zero, we then add a base register to it.  */
4455
4456       if (dbl && HAVE_32BIT_GPRS)
4457         as_warn (_("dla used to load 32-bit register"));
4458
4459       if (! dbl && HAVE_64BIT_OBJECTS)
4460         as_warn (_("la used to load 64-bit address"));
4461
4462       if (treg == breg)
4463         {
4464           tempreg = AT;
4465           used_at = 1;
4466         }
4467       else
4468         {
4469           tempreg = treg;
4470           used_at = 0;
4471         }
4472
4473       /* When generating embedded PIC code, we permit expressions of
4474          the form
4475            la   $treg,foo-bar
4476            la   $treg,foo-bar($breg)
4477          where bar is an address in the current section.  These are used
4478          when getting the addresses of functions.  We don't permit
4479          X_add_number to be non-zero, because if the symbol is
4480          external the relaxing code needs to know that any addend is
4481          purely the offset to X_op_symbol.  */
4482       if (mips_pic == EMBEDDED_PIC
4483           && offset_expr.X_op == O_subtract
4484           && (symbol_constant_p (offset_expr.X_op_symbol)
4485               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4486               : (symbol_equated_p (offset_expr.X_op_symbol)
4487                  && (S_GET_SEGMENT
4488                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4489                       ->X_add_symbol)
4490                      == now_seg)))
4491           && (offset_expr.X_add_number == 0
4492               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4493         {
4494           if (breg == 0)
4495             {
4496               tempreg = treg;
4497               used_at = 0;
4498               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4499                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4500             }
4501           else
4502             {
4503               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4504                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4505               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4506                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4507                            "d,v,t", tempreg, tempreg, breg);
4508             }
4509           macro_build ((char *) NULL, &icnt, &offset_expr,
4510                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4511                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4512           if (! used_at)
4513             return;
4514           break;
4515         }
4516
4517       if (offset_expr.X_op != O_symbol
4518           && offset_expr.X_op != O_constant)
4519         {
4520           as_bad (_("expression too complex"));
4521           offset_expr.X_op = O_constant;
4522         }
4523
4524       if (offset_expr.X_op == O_constant)
4525         load_register (&icnt, tempreg, &offset_expr,
4526                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4527                         ? (dbl || HAVE_64BIT_ADDRESSES)
4528                         : HAVE_64BIT_ADDRESSES));
4529       else if (mips_pic == NO_PIC)
4530         {
4531           /* If this is a reference to a GP relative symbol, we want
4532                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4533              Otherwise we want
4534                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4535                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4536              If we have a constant, we need two instructions anyhow,
4537              so we may as well always use the latter form.
4538
4539             With 64bit address space and a usable $at we want
4540               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4541               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4542               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4543               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4544               dsll32    $tempreg,0
4545               dadd      $tempreg,$tempreg,$at
4546
4547             If $at is already in use, we use an path which is suboptimal
4548             on superscalar processors.
4549               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4550               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4551               dsll      $tempreg,16
4552               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4553               dsll      $tempreg,16
4554               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4555           */
4556           char *p = NULL;
4557           if (HAVE_64BIT_ADDRESSES)
4558             {
4559               /* We don't do GP optimization for now because RELAX_ENCODE can't
4560                  hold the data for such large chunks.  */
4561
4562               if (used_at == 0)
4563                 {
4564                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4565                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4566                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4567                                AT, (int) BFD_RELOC_HI16_S);
4568                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4569                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4570                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4571                                AT, AT, (int) BFD_RELOC_LO16);
4572                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4573                                "d,w,<", tempreg, tempreg, 0);
4574                   macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4575                                tempreg, tempreg, AT);
4576                   used_at = 1;
4577                 }
4578               else
4579                 {
4580                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4581                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4582                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4583                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4584                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4585                                tempreg, tempreg, 16);
4586                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4587                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4588                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4589                                tempreg, tempreg, 16);
4590                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4591                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4592                 }
4593             }
4594           else
4595             {
4596               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4597                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4598                 {
4599                   frag_grow (20);
4600                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4601                                "t,r,j", tempreg, mips_gp_register,
4602                                (int) BFD_RELOC_GPREL16);
4603                   p = frag_var (rs_machine_dependent, 8, 0,
4604                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4605                                               mips_opts.warn_about_macros),
4606                                 offset_expr.X_add_symbol, 0, NULL);
4607                 }
4608               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4609               if (p != NULL)
4610                 p += 4;
4611               macro_build (p, &icnt, &offset_expr, "addiu",
4612                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4613             }
4614         }
4615       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4616         {
4617           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4618
4619           /* If this is a reference to an external symbol, and there
4620              is no constant, we want
4621                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4622              or if tempreg is PIC_CALL_REG
4623                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4624              For a local symbol, we want
4625                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4626                nop
4627                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4628
4629              If we have a small constant, and this is a reference to
4630              an external symbol, we want
4631                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4632                nop
4633                addiu    $tempreg,$tempreg,<constant>
4634              For a local symbol, we want the same instruction
4635              sequence, but we output a BFD_RELOC_LO16 reloc on the
4636              addiu instruction.
4637
4638              If we have a large constant, and this is a reference to
4639              an external symbol, we want
4640                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4641                lui      $at,<hiconstant>
4642                addiu    $at,$at,<loconstant>
4643                addu     $tempreg,$tempreg,$at
4644              For a local symbol, we want the same instruction
4645              sequence, but we output a BFD_RELOC_LO16 reloc on the
4646              addiu instruction.  */
4647           expr1.X_add_number = offset_expr.X_add_number;
4648           offset_expr.X_add_number = 0;
4649           frag_grow (32);
4650           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4651             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4652           macro_build ((char *) NULL, &icnt, &offset_expr,
4653                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4654                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4655           if (expr1.X_add_number == 0)
4656             {
4657               int off;
4658               char *p;
4659
4660               if (breg == 0)
4661                 off = 0;
4662               else
4663                 {
4664                   /* We're going to put in an addu instruction using
4665                      tempreg, so we may as well insert the nop right
4666                      now.  */
4667                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4668                                "nop", "");
4669                   off = 4;
4670                 }
4671               p = frag_var (rs_machine_dependent, 8 - off, 0,
4672                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4673                                           (breg == 0
4674                                            ? mips_opts.warn_about_macros
4675                                            : 0)),
4676                             offset_expr.X_add_symbol, 0, NULL);
4677               if (breg == 0)
4678                 {
4679                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4680                   p += 4;
4681                 }
4682               macro_build (p, &icnt, &expr1,
4683                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4684                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4685               /* FIXME: If breg == 0, and the next instruction uses
4686                  $tempreg, then if this variant case is used an extra
4687                  nop will be generated.  */
4688             }
4689           else if (expr1.X_add_number >= -0x8000
4690                    && expr1.X_add_number < 0x8000)
4691             {
4692               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4693                            "nop", "");
4694               macro_build ((char *) NULL, &icnt, &expr1,
4695                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4696                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4697               frag_var (rs_machine_dependent, 0, 0,
4698                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4699                         offset_expr.X_add_symbol, 0, NULL);
4700             }
4701           else
4702             {
4703               int off1;
4704
4705               /* If we are going to add in a base register, and the
4706                  target register and the base register are the same,
4707                  then we are using AT as a temporary register.  Since
4708                  we want to load the constant into AT, we add our
4709                  current AT (from the global offset table) and the
4710                  register into the register now, and pretend we were
4711                  not using a base register.  */
4712               if (breg != treg)
4713                 off1 = 0;
4714               else
4715                 {
4716                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4717                                "nop", "");
4718                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4719                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4720                                "d,v,t", treg, AT, breg);
4721                   breg = 0;
4722                   tempreg = treg;
4723                   off1 = -8;
4724                 }
4725
4726               /* Set mips_optimize around the lui instruction to avoid
4727                  inserting an unnecessary nop after the lw.  */
4728               hold_mips_optimize = mips_optimize;
4729               mips_optimize = 2;
4730               macro_build_lui (NULL, &icnt, &expr1, AT);
4731               mips_optimize = hold_mips_optimize;
4732
4733               macro_build ((char *) NULL, &icnt, &expr1,
4734                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4735                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4736               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4737                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4738                            "d,v,t", tempreg, tempreg, AT);
4739               frag_var (rs_machine_dependent, 0, 0,
4740                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4741                         offset_expr.X_add_symbol, 0, NULL);
4742               used_at = 1;
4743             }
4744         }
4745       else if (mips_pic == SVR4_PIC)
4746         {
4747           int gpdel;
4748           char *p;
4749           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4750           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4751
4752           /* This is the large GOT case.  If this is a reference to an
4753              external symbol, and there is no constant, we want
4754                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4755                addu     $tempreg,$tempreg,$gp
4756                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4757              or if tempreg is PIC_CALL_REG
4758                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4759                addu     $tempreg,$tempreg,$gp
4760                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4761              For a local symbol, we want
4762                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4763                nop
4764                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4765
4766              If we have a small constant, and this is a reference to
4767              an external symbol, we want
4768                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4769                addu     $tempreg,$tempreg,$gp
4770                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4771                nop
4772                addiu    $tempreg,$tempreg,<constant>
4773              For a local symbol, we want
4774                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4775                nop
4776                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4777
4778              If we have a large constant, and this is a reference to
4779              an external symbol, we want
4780                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4781                addu     $tempreg,$tempreg,$gp
4782                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4783                lui      $at,<hiconstant>
4784                addiu    $at,$at,<loconstant>
4785                addu     $tempreg,$tempreg,$at
4786              For a local symbol, we want
4787                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4788                lui      $at,<hiconstant>
4789                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4790                addu     $tempreg,$tempreg,$at
4791
4792              For NewABI, we want for data addresses
4793                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4794              If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4795                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4796            */
4797           if (HAVE_NEWABI)
4798             {
4799               int reloc_type = (tempreg == PIC_CALL_REG
4800                                 ? BFD_RELOC_MIPS_CALL16
4801                                 : BFD_RELOC_MIPS_GOT_DISP);
4802
4803               macro_build ((char *) NULL, &icnt, &offset_expr,
4804                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4805                            "t,o(b)", tempreg, reloc_type, mips_gp_register);
4806
4807               if (breg != 0)
4808                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4809                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4810                              "d,v,t", treg, tempreg, breg);
4811
4812               if (! used_at)
4813                 return;
4814
4815               break;
4816             }
4817           expr1.X_add_number = offset_expr.X_add_number;
4818           offset_expr.X_add_number = 0;
4819           frag_grow (52);
4820           if (reg_needs_delay (mips_gp_register))
4821             gpdel = 4;
4822           else
4823             gpdel = 0;
4824           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4825             {
4826               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4827               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4828             }
4829           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4830                        tempreg, lui_reloc_type);
4831           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4832                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4833                        "d,v,t", tempreg, tempreg, mips_gp_register);
4834           macro_build ((char *) NULL, &icnt, &offset_expr,
4835                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4836                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4837           if (expr1.X_add_number == 0)
4838             {
4839               int off;
4840
4841               if (breg == 0)
4842                 off = 0;
4843               else
4844                 {
4845                   /* We're going to put in an addu instruction using
4846                      tempreg, so we may as well insert the nop right
4847                      now.  */
4848                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4849                                "nop", "");
4850                   off = 4;
4851                 }
4852
4853               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4854                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4855                                           8 + gpdel, 0,
4856                                           (breg == 0
4857                                            ? mips_opts.warn_about_macros
4858                                            : 0)),
4859                             offset_expr.X_add_symbol, 0, NULL);
4860             }
4861           else if (expr1.X_add_number >= -0x8000
4862                    && expr1.X_add_number < 0x8000)
4863             {
4864               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4865                            "nop", "");
4866               macro_build ((char *) NULL, &icnt, &expr1,
4867                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4868                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4869
4870               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4871                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4872                                           (breg == 0
4873                                            ? mips_opts.warn_about_macros
4874                                            : 0)),
4875                             offset_expr.X_add_symbol, 0, NULL);
4876             }
4877           else
4878             {
4879               int adj, dreg;
4880
4881               /* If we are going to add in a base register, and the
4882                  target register and the base register are the same,
4883                  then we are using AT as a temporary register.  Since
4884                  we want to load the constant into AT, we add our
4885                  current AT (from the global offset table) and the
4886                  register into the register now, and pretend we were
4887                  not using a base register.  */
4888               if (breg != treg)
4889                 {
4890                   adj = 0;
4891                   dreg = tempreg;
4892                 }
4893               else
4894                 {
4895                   assert (tempreg == AT);
4896                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4897                                "nop", "");
4898                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4899                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4900                                "d,v,t", treg, AT, breg);
4901                   dreg = treg;
4902                   adj = 8;
4903                 }
4904
4905               /* Set mips_optimize around the lui instruction to avoid
4906                  inserting an unnecessary nop after the lw.  */
4907               hold_mips_optimize = mips_optimize;
4908               mips_optimize = 2;
4909               macro_build_lui (NULL, &icnt, &expr1, AT);
4910               mips_optimize = hold_mips_optimize;
4911
4912               macro_build ((char *) NULL, &icnt, &expr1,
4913                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4914                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4915               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4916                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4917                            "d,v,t", dreg, dreg, AT);
4918
4919               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4920                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4921                                           8 + gpdel, 0,
4922                                           (breg == 0
4923                                            ? mips_opts.warn_about_macros
4924                                            : 0)),
4925                             offset_expr.X_add_symbol, 0, NULL);
4926
4927               used_at = 1;
4928             }
4929
4930           if (gpdel > 0)
4931             {
4932               /* This is needed because this instruction uses $gp, but
4933                  the first instruction on the main stream does not.  */
4934               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4935               p += 4;
4936             }
4937           macro_build (p, &icnt, &offset_expr,
4938                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4939                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4940                        mips_gp_register);
4941           p += 4;
4942           if (expr1.X_add_number >= -0x8000
4943               && expr1.X_add_number < 0x8000)
4944             {
4945               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4946               p += 4;
4947               macro_build (p, &icnt, &expr1,
4948                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4949                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4950               /* FIXME: If add_number is 0, and there was no base
4951                  register, the external symbol case ended with a load,
4952                  so if the symbol turns out to not be external, and
4953                  the next instruction uses tempreg, an unnecessary nop
4954                  will be inserted.  */
4955             }
4956           else
4957             {
4958               if (breg == treg)
4959                 {
4960                   /* We must add in the base register now, as in the
4961                      external symbol case.  */
4962                   assert (tempreg == AT);
4963                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4964                   p += 4;
4965                   macro_build (p, &icnt, (expressionS *) NULL,
4966                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4967                                "d,v,t", treg, AT, breg);
4968                   p += 4;
4969                   tempreg = treg;
4970                   /* We set breg to 0 because we have arranged to add
4971                      it in in both cases.  */
4972                   breg = 0;
4973                 }
4974
4975               macro_build_lui (p, &icnt, &expr1, AT);
4976               p += 4;
4977               macro_build (p, &icnt, &expr1,
4978                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4979                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4980               p += 4;
4981               macro_build (p, &icnt, (expressionS *) NULL,
4982                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4983                            "d,v,t", tempreg, tempreg, AT);
4984               p += 4;
4985             }
4986         }
4987       else if (mips_pic == EMBEDDED_PIC)
4988         {
4989           /* We use
4990                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4991              */
4992           macro_build ((char *) NULL, &icnt, &offset_expr,
4993                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4994                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4995         }
4996       else
4997         abort ();
4998
4999       if (breg != 0)
5000         {
5001           char *s;
5002
5003           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5004             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5005           else
5006             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5007
5008           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5009                        "d,v,t", treg, tempreg, breg);
5010         }
5011
5012       if (! used_at)
5013         return;
5014
5015       break;
5016
5017     case M_J_A:
5018       /* The j instruction may not be used in PIC code, since it
5019          requires an absolute address.  We convert it to a b
5020          instruction.  */
5021       if (mips_pic == NO_PIC)
5022         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5023       else
5024         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5025       return;
5026
5027       /* The jal instructions must be handled as macros because when
5028          generating PIC code they expand to multi-instruction
5029          sequences.  Normally they are simple instructions.  */
5030     case M_JAL_1:
5031       dreg = RA;
5032       /* Fall through.  */
5033     case M_JAL_2:
5034       if (mips_pic == NO_PIC
5035           || mips_pic == EMBEDDED_PIC)
5036         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5037                      "d,s", dreg, sreg);
5038       else if (mips_pic == SVR4_PIC)
5039         {
5040           if (sreg != PIC_CALL_REG)
5041             as_warn (_("MIPS PIC call to register other than $25"));
5042
5043           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5044                        "d,s", dreg, sreg);
5045           if (! HAVE_NEWABI)
5046             {
5047               if (mips_cprestore_offset < 0)
5048                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5049               else
5050                 {
5051                   if (! mips_frame_reg_valid)
5052                     {
5053                       as_warn (_("No .frame pseudo-op used in PIC code"));
5054                       /* Quiet this warning.  */
5055                       mips_frame_reg_valid = 1;
5056                     }
5057                   if (! mips_cprestore_valid)
5058                     {
5059                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5060                       /* Quiet this warning.  */
5061                       mips_cprestore_valid = 1;
5062                     }
5063                   expr1.X_add_number = mips_cprestore_offset;
5064                   macro_build ((char *) NULL, &icnt, &expr1,
5065                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5066                                mips_gp_register, (int) BFD_RELOC_LO16,
5067                                mips_frame_reg);
5068                 }
5069             }
5070         }
5071       else
5072         abort ();
5073
5074       return;
5075
5076     case M_JAL_A:
5077       if (mips_pic == NO_PIC)
5078         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5079       else if (mips_pic == SVR4_PIC)
5080         {
5081           char *p;
5082
5083           /* If this is a reference to an external symbol, and we are
5084              using a small GOT, we want
5085                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5086                nop
5087                jalr     $ra,$25
5088                nop
5089                lw       $gp,cprestore($sp)
5090              The cprestore value is set using the .cprestore
5091              pseudo-op.  If we are using a big GOT, we want
5092                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5093                addu     $25,$25,$gp
5094                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5095                nop
5096                jalr     $ra,$25
5097                nop
5098                lw       $gp,cprestore($sp)
5099              If the symbol is not external, we want
5100                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5101                nop
5102                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5103                jalr     $ra,$25
5104                nop
5105                lw $gp,cprestore($sp)
5106              For NewABI, we want
5107                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5108                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5109            */
5110           if (HAVE_NEWABI)
5111             {
5112               macro_build ((char *) NULL, &icnt, &offset_expr,
5113                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5114                            "t,o(b)", PIC_CALL_REG,
5115                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5116               macro_build_jalr (icnt, &offset_expr);
5117             }
5118           else
5119             {
5120               frag_grow (40);
5121               if (! mips_big_got)
5122                 {
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_CALL16, mips_gp_register);
5127                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5128                                "nop", "");
5129                   p = frag_var (rs_machine_dependent, 4, 0,
5130                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5131                                 offset_expr.X_add_symbol, 0, NULL);
5132                 }
5133               else
5134                 {
5135                   int gpdel;
5136
5137                   if (reg_needs_delay (mips_gp_register))
5138                     gpdel = 4;
5139                   else
5140                     gpdel = 0;
5141                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5142                                "t,u", PIC_CALL_REG,
5143                                (int) BFD_RELOC_MIPS_CALL_HI16);
5144                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5145                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5146                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5147                                mips_gp_register);
5148                   macro_build ((char *) NULL, &icnt, &offset_expr,
5149                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5150                                "t,o(b)", PIC_CALL_REG,
5151                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5152                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5153                                "nop", "");
5154                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5155                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5156                                               8 + gpdel, 0, 0),
5157                                 offset_expr.X_add_symbol, 0, NULL);
5158                   if (gpdel > 0)
5159                     {
5160                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5161                       p += 4;
5162                     }
5163                   macro_build (p, &icnt, &offset_expr,
5164                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5165                                "t,o(b)", PIC_CALL_REG,
5166                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5167                   p += 4;
5168                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5169                   p += 4;
5170                 }
5171               macro_build (p, &icnt, &offset_expr,
5172                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5173                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5174                            (int) BFD_RELOC_LO16);
5175               macro_build_jalr (icnt, &offset_expr);
5176
5177               if (mips_cprestore_offset < 0)
5178                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5179               else
5180                 {
5181                   if (! mips_frame_reg_valid)
5182                     {
5183                       as_warn (_("No .frame pseudo-op used in PIC code"));
5184                       /* Quiet this warning.  */
5185                       mips_frame_reg_valid = 1;
5186                     }
5187                   if (! mips_cprestore_valid)
5188                     {
5189                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5190                       /* Quiet this warning.  */
5191                       mips_cprestore_valid = 1;
5192                     }
5193                   if (mips_opts.noreorder)
5194                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5195                                  "nop", "");
5196                   expr1.X_add_number = mips_cprestore_offset;
5197                   macro_build ((char *) NULL, &icnt, &expr1,
5198                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5199                                mips_gp_register, (int) BFD_RELOC_LO16,
5200                                mips_frame_reg);
5201                 }
5202             }
5203         }
5204       else if (mips_pic == EMBEDDED_PIC)
5205         {
5206           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5207           /* The linker may expand the call to a longer sequence which
5208              uses $at, so we must break rather than return.  */
5209           break;
5210         }
5211       else
5212         abort ();
5213
5214       return;
5215
5216     case M_LB_AB:
5217       s = "lb";
5218       goto ld;
5219     case M_LBU_AB:
5220       s = "lbu";
5221       goto ld;
5222     case M_LH_AB:
5223       s = "lh";
5224       goto ld;
5225     case M_LHU_AB:
5226       s = "lhu";
5227       goto ld;
5228     case M_LW_AB:
5229       s = "lw";
5230       goto ld;
5231     case M_LWC0_AB:
5232       s = "lwc0";
5233       /* Itbl support may require additional care here.  */
5234       coproc = 1;
5235       goto ld;
5236     case M_LWC1_AB:
5237       s = "lwc1";
5238       /* Itbl support may require additional care here.  */
5239       coproc = 1;
5240       goto ld;
5241     case M_LWC2_AB:
5242       s = "lwc2";
5243       /* Itbl support may require additional care here.  */
5244       coproc = 1;
5245       goto ld;
5246     case M_LWC3_AB:
5247       s = "lwc3";
5248       /* Itbl support may require additional care here.  */
5249       coproc = 1;
5250       goto ld;
5251     case M_LWL_AB:
5252       s = "lwl";
5253       lr = 1;
5254       goto ld;
5255     case M_LWR_AB:
5256       s = "lwr";
5257       lr = 1;
5258       goto ld;
5259     case M_LDC1_AB:
5260       if (mips_arch == CPU_R4650)
5261         {
5262           as_bad (_("opcode not supported on this processor"));
5263           return;
5264         }
5265       s = "ldc1";
5266       /* Itbl support may require additional care here.  */
5267       coproc = 1;
5268       goto ld;
5269     case M_LDC2_AB:
5270       s = "ldc2";
5271       /* Itbl support may require additional care here.  */
5272       coproc = 1;
5273       goto ld;
5274     case M_LDC3_AB:
5275       s = "ldc3";
5276       /* Itbl support may require additional care here.  */
5277       coproc = 1;
5278       goto ld;
5279     case M_LDL_AB:
5280       s = "ldl";
5281       lr = 1;
5282       goto ld;
5283     case M_LDR_AB:
5284       s = "ldr";
5285       lr = 1;
5286       goto ld;
5287     case M_LL_AB:
5288       s = "ll";
5289       goto ld;
5290     case M_LLD_AB:
5291       s = "lld";
5292       goto ld;
5293     case M_LWU_AB:
5294       s = "lwu";
5295     ld:
5296       if (breg == treg || coproc || lr)
5297         {
5298           tempreg = AT;
5299           used_at = 1;
5300         }
5301       else
5302         {
5303           tempreg = treg;
5304           used_at = 0;
5305         }
5306       goto ld_st;
5307     case M_SB_AB:
5308       s = "sb";
5309       goto st;
5310     case M_SH_AB:
5311       s = "sh";
5312       goto st;
5313     case M_SW_AB:
5314       s = "sw";
5315       goto st;
5316     case M_SWC0_AB:
5317       s = "swc0";
5318       /* Itbl support may require additional care here.  */
5319       coproc = 1;
5320       goto st;
5321     case M_SWC1_AB:
5322       s = "swc1";
5323       /* Itbl support may require additional care here.  */
5324       coproc = 1;
5325       goto st;
5326     case M_SWC2_AB:
5327       s = "swc2";
5328       /* Itbl support may require additional care here.  */
5329       coproc = 1;
5330       goto st;
5331     case M_SWC3_AB:
5332       s = "swc3";
5333       /* Itbl support may require additional care here.  */
5334       coproc = 1;
5335       goto st;
5336     case M_SWL_AB:
5337       s = "swl";
5338       goto st;
5339     case M_SWR_AB:
5340       s = "swr";
5341       goto st;
5342     case M_SC_AB:
5343       s = "sc";
5344       goto st;
5345     case M_SCD_AB:
5346       s = "scd";
5347       goto st;
5348     case M_SDC1_AB:
5349       if (mips_arch == CPU_R4650)
5350         {
5351           as_bad (_("opcode not supported on this processor"));
5352           return;
5353         }
5354       s = "sdc1";
5355       coproc = 1;
5356       /* Itbl support may require additional care here.  */
5357       goto st;
5358     case M_SDC2_AB:
5359       s = "sdc2";
5360       /* Itbl support may require additional care here.  */
5361       coproc = 1;
5362       goto st;
5363     case M_SDC3_AB:
5364       s = "sdc3";
5365       /* Itbl support may require additional care here.  */
5366       coproc = 1;
5367       goto st;
5368     case M_SDL_AB:
5369       s = "sdl";
5370       goto st;
5371     case M_SDR_AB:
5372       s = "sdr";
5373     st:
5374       tempreg = AT;
5375       used_at = 1;
5376     ld_st:
5377       /* Itbl support may require additional care here.  */
5378       if (mask == M_LWC1_AB
5379           || mask == M_SWC1_AB
5380           || mask == M_LDC1_AB
5381           || mask == M_SDC1_AB
5382           || mask == M_L_DAB
5383           || mask == M_S_DAB)
5384         fmt = "T,o(b)";
5385       else if (coproc)
5386         fmt = "E,o(b)";
5387       else
5388         fmt = "t,o(b)";
5389
5390       /* For embedded PIC, we allow loads where the offset is calculated
5391          by subtracting a symbol in the current segment from an unknown
5392          symbol, relative to a base register, e.g.:
5393                 <op>    $treg, <sym>-<localsym>($breg)
5394          This is used by the compiler for switch statements.  */
5395       if (mips_pic == EMBEDDED_PIC
5396           && offset_expr.X_op == O_subtract
5397           && (symbol_constant_p (offset_expr.X_op_symbol)
5398               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5399               : (symbol_equated_p (offset_expr.X_op_symbol)
5400                  && (S_GET_SEGMENT
5401                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5402                       ->X_add_symbol)
5403                      == now_seg)))
5404           && breg != 0
5405           && (offset_expr.X_add_number == 0
5406               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5407         {
5408           /* For this case, we output the instructions:
5409                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5410                 addiu   $tempreg,$tempreg,$breg
5411                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5412              If the relocation would fit entirely in 16 bits, it would be
5413              nice to emit:
5414                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5415              instead, but that seems quite difficult.  */
5416           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5417                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5418           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5419                        ((bfd_arch_bits_per_address (stdoutput) == 32
5420                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5421                         ? "addu" : "daddu"),
5422                        "d,v,t", tempreg, tempreg, breg);
5423           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5424                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5425           if (! used_at)
5426             return;
5427           break;
5428         }
5429
5430       if (offset_expr.X_op != O_constant
5431           && offset_expr.X_op != O_symbol)
5432         {
5433           as_bad (_("expression too complex"));
5434           offset_expr.X_op = O_constant;
5435         }
5436
5437       /* A constant expression in PIC code can be handled just as it
5438          is in non PIC code.  */
5439       if (mips_pic == NO_PIC
5440           || offset_expr.X_op == O_constant)
5441         {
5442           char *p;
5443
5444           /* If this is a reference to a GP relative symbol, and there
5445              is no base register, we want
5446                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5447              Otherwise, if there is no base register, we want
5448                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5449                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5450              If we have a constant, we need two instructions anyhow,
5451              so we always use the latter form.
5452
5453              If we have a base register, and this is a reference to a
5454              GP relative symbol, we want
5455                addu     $tempreg,$breg,$gp
5456                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5457              Otherwise we want
5458                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5459                addu     $tempreg,$tempreg,$breg
5460                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5461              With a constant we always use the latter case.
5462
5463              With 64bit address space and no base register and $at usable,
5464              we want
5465                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5466                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5467                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5468                dsll32   $tempreg,0
5469                daddu    $tempreg,$at
5470                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5471              If we have a base register, we want
5472                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5473                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5474                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5475                daddu    $at,$breg
5476                dsll32   $tempreg,0
5477                daddu    $tempreg,$at
5478                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5479
5480              Without $at we can't generate the optimal path for superscalar
5481              processors here since this would require two temporary registers.
5482                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5483                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5484                dsll     $tempreg,16
5485                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5486                dsll     $tempreg,16
5487                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5488              If we have a base register, we want
5489                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5490                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5491                dsll     $tempreg,16
5492                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5493                dsll     $tempreg,16
5494                daddu    $tempreg,$tempreg,$breg
5495                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5496
5497              If we have 64-bit addresses, as an optimization, for
5498              addresses which are 32-bit constants (e.g. kseg0/kseg1
5499              addresses) we fall back to the 32-bit address generation
5500              mechanism since it is more efficient.  This code should
5501              probably attempt to generate 64-bit constants more
5502              efficiently in general.
5503            */
5504           if (HAVE_64BIT_ADDRESSES
5505               && !(offset_expr.X_op == O_constant
5506                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
5507             {
5508               p = NULL;
5509
5510               /* We don't do GP optimization for now because RELAX_ENCODE can't
5511                  hold the data for such large chunks.  */
5512
5513               if (used_at == 0)
5514                 {
5515                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5516                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5517                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5518                                AT, (int) BFD_RELOC_HI16_S);
5519                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5520                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5521                   if (breg != 0)
5522                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5523                                  "d,v,t", AT, AT, breg);
5524                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5525                                "d,w,<", tempreg, tempreg, 0);
5526                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5527                                "d,v,t", tempreg, tempreg, AT);
5528                   macro_build (p, &icnt, &offset_expr, s,
5529                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5530                   used_at = 1;
5531                 }
5532               else
5533                 {
5534                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5535                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5536                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5537                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5538                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5539                                "d,w,<", tempreg, tempreg, 16);
5540                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5541                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5542                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5543                                "d,w,<", tempreg, tempreg, 16);
5544                   if (breg != 0)
5545                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5546                                  "d,v,t", tempreg, tempreg, breg);
5547                   macro_build (p, &icnt, &offset_expr, s,
5548                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5549                 }
5550
5551               return;
5552             }
5553
5554           if (breg == 0)
5555             {
5556               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5557                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5558                 p = NULL;
5559               else
5560                 {
5561                   frag_grow (20);
5562                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5563                                treg, (int) BFD_RELOC_GPREL16,
5564                                mips_gp_register);
5565                   p = frag_var (rs_machine_dependent, 8, 0,
5566                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5567                                               (mips_opts.warn_about_macros
5568                                                || (used_at
5569                                                    && mips_opts.noat))),
5570                                 offset_expr.X_add_symbol, 0, NULL);
5571                   used_at = 0;
5572                 }
5573               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5574               if (p != NULL)
5575                 p += 4;
5576               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5577                            (int) BFD_RELOC_LO16, tempreg);
5578             }
5579           else
5580             {
5581               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5582                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5583                 p = NULL;
5584               else
5585                 {
5586                   frag_grow (28);
5587                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5588                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5589                                "d,v,t", tempreg, breg, mips_gp_register);
5590                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5591                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5592                   p = frag_var (rs_machine_dependent, 12, 0,
5593                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5594                                 offset_expr.X_add_symbol, 0, NULL);
5595                 }
5596               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5597               if (p != NULL)
5598                 p += 4;
5599               macro_build (p, &icnt, (expressionS *) NULL,
5600                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5601                            "d,v,t", tempreg, tempreg, breg);
5602               if (p != NULL)
5603                 p += 4;
5604               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5605                            (int) BFD_RELOC_LO16, tempreg);
5606             }
5607         }
5608       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5609         {
5610           char *p;
5611
5612           /* If this is a reference to an external symbol, we want
5613                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5614                nop
5615                <op>     $treg,0($tempreg)
5616              Otherwise we want
5617                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5618                nop
5619                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5620                <op>     $treg,0($tempreg)
5621              If there is a base register, we add it to $tempreg before
5622              the <op>.  If there is a constant, we stick it in the
5623              <op> instruction.  We don't handle constants larger than
5624              16 bits, because we have no way to load the upper 16 bits
5625              (actually, we could handle them for the subset of cases
5626              in which we are not using $at).  */
5627           assert (offset_expr.X_op == O_symbol);
5628           expr1.X_add_number = offset_expr.X_add_number;
5629           offset_expr.X_add_number = 0;
5630           if (expr1.X_add_number < -0x8000
5631               || expr1.X_add_number >= 0x8000)
5632             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5633           frag_grow (20);
5634           macro_build ((char *) NULL, &icnt, &offset_expr,
5635                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5636                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5637           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5638           p = frag_var (rs_machine_dependent, 4, 0,
5639                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5640                         offset_expr.X_add_symbol, 0, NULL);
5641           macro_build (p, &icnt, &offset_expr,
5642                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5643                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5644           if (breg != 0)
5645             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5646                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5647                          "d,v,t", tempreg, tempreg, breg);
5648           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5649                        (int) BFD_RELOC_LO16, tempreg);
5650         }
5651       else if (mips_pic == SVR4_PIC)
5652         {
5653           int gpdel;
5654           char *p;
5655
5656           /* If this is a reference to an external symbol, we want
5657                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5658                addu     $tempreg,$tempreg,$gp
5659                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5660                <op>     $treg,0($tempreg)
5661              Otherwise we want
5662                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5663                nop
5664                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5665                <op>     $treg,0($tempreg)
5666              If there is a base register, we add it to $tempreg before
5667              the <op>.  If there is a constant, we stick it in the
5668              <op> instruction.  We don't handle constants larger than
5669              16 bits, because we have no way to load the upper 16 bits
5670              (actually, we could handle them for the subset of cases
5671              in which we are not using $at).
5672
5673              For NewABI, we want
5674                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5675                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5676                <op>     $treg,0($tempreg)
5677            */
5678           assert (offset_expr.X_op == O_symbol);
5679           expr1.X_add_number = offset_expr.X_add_number;
5680           offset_expr.X_add_number = 0;
5681           if (expr1.X_add_number < -0x8000
5682               || expr1.X_add_number >= 0x8000)
5683             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5684           if (HAVE_NEWABI)
5685             {
5686               macro_build ((char *) NULL, &icnt, &offset_expr,
5687                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5688                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5689                            mips_gp_register);
5690               macro_build ((char *) NULL, &icnt, &offset_expr,
5691                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5692                            "t,r,j", tempreg, tempreg,
5693                            BFD_RELOC_MIPS_GOT_OFST);
5694               if (breg != 0)
5695                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5696                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5697                              "d,v,t", tempreg, tempreg, breg);
5698               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5699                            (int) BFD_RELOC_LO16, tempreg);
5700
5701               if (! used_at)
5702                 return;
5703
5704               break;
5705             }
5706           if (reg_needs_delay (mips_gp_register))
5707             gpdel = 4;
5708           else
5709             gpdel = 0;
5710           frag_grow (36);
5711           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5712                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5713           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5714                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5715                        "d,v,t", tempreg, tempreg, mips_gp_register);
5716           macro_build ((char *) NULL, &icnt, &offset_expr,
5717                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5718                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5719                        tempreg);
5720           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5721                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5722                         offset_expr.X_add_symbol, 0, NULL);
5723           if (gpdel > 0)
5724             {
5725               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5726               p += 4;
5727             }
5728           macro_build (p, &icnt, &offset_expr,
5729                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5730                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5731                        mips_gp_register);
5732           p += 4;
5733           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5734           p += 4;
5735           macro_build (p, &icnt, &offset_expr,
5736                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5737                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5738           if (breg != 0)
5739             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5740                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5741                          "d,v,t", tempreg, tempreg, breg);
5742           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5743                        (int) BFD_RELOC_LO16, tempreg);
5744         }
5745       else if (mips_pic == EMBEDDED_PIC)
5746         {
5747           /* If there is no base register, we want
5748                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5749              If there is a base register, we want
5750                addu     $tempreg,$breg,$gp
5751                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5752              */
5753           assert (offset_expr.X_op == O_symbol);
5754           if (breg == 0)
5755             {
5756               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5757                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
5758               used_at = 0;
5759             }
5760           else
5761             {
5762               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5763                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5764                            "d,v,t", tempreg, breg, mips_gp_register);
5765               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5766                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5767             }
5768         }
5769       else
5770         abort ();
5771
5772       if (! used_at)
5773         return;
5774
5775       break;
5776
5777     case M_LI:
5778     case M_LI_S:
5779       load_register (&icnt, treg, &imm_expr, 0);
5780       return;
5781
5782     case M_DLI:
5783       load_register (&icnt, treg, &imm_expr, 1);
5784       return;
5785
5786     case M_LI_SS:
5787       if (imm_expr.X_op == O_constant)
5788         {
5789           load_register (&icnt, AT, &imm_expr, 0);
5790           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5791                        "mtc1", "t,G", AT, treg);
5792           break;
5793         }
5794       else
5795         {
5796           assert (offset_expr.X_op == O_symbol
5797                   && strcmp (segment_name (S_GET_SEGMENT
5798                                            (offset_expr.X_add_symbol)),
5799                              ".lit4") == 0
5800                   && offset_expr.X_add_number == 0);
5801           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5802                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
5803           return;
5804         }
5805
5806     case M_LI_D:
5807       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5808          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5809          order 32 bits of the value and the low order 32 bits are either
5810          zero or in OFFSET_EXPR.  */
5811       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5812         {
5813           if (HAVE_64BIT_GPRS)
5814             load_register (&icnt, treg, &imm_expr, 1);
5815           else
5816             {
5817               int hreg, lreg;
5818
5819               if (target_big_endian)
5820                 {
5821                   hreg = treg;
5822                   lreg = treg + 1;
5823                 }
5824               else
5825                 {
5826                   hreg = treg + 1;
5827                   lreg = treg;
5828                 }
5829
5830               if (hreg <= 31)
5831                 load_register (&icnt, hreg, &imm_expr, 0);
5832               if (lreg <= 31)
5833                 {
5834                   if (offset_expr.X_op == O_absent)
5835                     move_register (&icnt, lreg, 0);
5836                   else
5837                     {
5838                       assert (offset_expr.X_op == O_constant);
5839                       load_register (&icnt, lreg, &offset_expr, 0);
5840                     }
5841                 }
5842             }
5843           return;
5844         }
5845
5846       /* We know that sym is in the .rdata section.  First we get the
5847          upper 16 bits of the address.  */
5848       if (mips_pic == NO_PIC)
5849         {
5850           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5851         }
5852       else if (mips_pic == SVR4_PIC)
5853         {
5854           macro_build ((char *) NULL, &icnt, &offset_expr,
5855                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5856                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5857                        mips_gp_register);
5858         }
5859       else if (mips_pic == EMBEDDED_PIC)
5860         {
5861           /* For embedded PIC we pick up the entire address off $gp in
5862              a single instruction.  */
5863           macro_build ((char *) NULL, &icnt, &offset_expr,
5864                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5865                        mips_gp_register, (int) BFD_RELOC_GPREL16);
5866           offset_expr.X_op = O_constant;
5867           offset_expr.X_add_number = 0;
5868         }
5869       else
5870         abort ();
5871
5872       /* Now we load the register(s).  */
5873       if (HAVE_64BIT_GPRS)
5874         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5875                      treg, (int) BFD_RELOC_LO16, AT);
5876       else
5877         {
5878           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5879                        treg, (int) BFD_RELOC_LO16, AT);
5880           if (treg != RA)
5881             {
5882               /* FIXME: How in the world do we deal with the possible
5883                  overflow here?  */
5884               offset_expr.X_add_number += 4;
5885               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5886                            treg + 1, (int) BFD_RELOC_LO16, AT);
5887             }
5888         }
5889
5890       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5891          does not become a variant frag.  */
5892       frag_wane (frag_now);
5893       frag_new (0);
5894
5895       break;
5896
5897     case M_LI_DD:
5898       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5899          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5900          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5901          the value and the low order 32 bits are either zero or in
5902          OFFSET_EXPR.  */
5903       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5904         {
5905           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5906           if (HAVE_64BIT_FPRS)
5907             {
5908               assert (HAVE_64BIT_GPRS);
5909               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5910                            "dmtc1", "t,S", AT, treg);
5911             }
5912           else
5913             {
5914               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5915                            "mtc1", "t,G", AT, treg + 1);
5916               if (offset_expr.X_op == O_absent)
5917                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5918                              "mtc1", "t,G", 0, treg);
5919               else
5920                 {
5921                   assert (offset_expr.X_op == O_constant);
5922                   load_register (&icnt, AT, &offset_expr, 0);
5923                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5924                                "mtc1", "t,G", AT, treg);
5925                 }
5926             }
5927           break;
5928         }
5929
5930       assert (offset_expr.X_op == O_symbol
5931               && offset_expr.X_add_number == 0);
5932       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5933       if (strcmp (s, ".lit8") == 0)
5934         {
5935           if (mips_opts.isa != ISA_MIPS1)
5936             {
5937               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5938                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5939                            mips_gp_register);
5940               return;
5941             }
5942           breg = mips_gp_register;
5943           r = BFD_RELOC_MIPS_LITERAL;
5944           goto dob;
5945         }
5946       else
5947         {
5948           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5949           if (mips_pic == SVR4_PIC)
5950             macro_build ((char *) NULL, &icnt, &offset_expr,
5951                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5952                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5953                          mips_gp_register);
5954           else
5955             {
5956               /* FIXME: This won't work for a 64 bit address.  */
5957               macro_build_lui (NULL, &icnt, &offset_expr, AT);
5958             }
5959
5960           if (mips_opts.isa != ISA_MIPS1)
5961             {
5962               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5963                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5964
5965               /* To avoid confusion in tc_gen_reloc, we must ensure
5966                  that this does not become a variant frag.  */
5967               frag_wane (frag_now);
5968               frag_new (0);
5969
5970               break;
5971             }
5972           breg = AT;
5973           r = BFD_RELOC_LO16;
5974           goto dob;
5975         }
5976
5977     case M_L_DOB:
5978       if (mips_arch == CPU_R4650)
5979         {
5980           as_bad (_("opcode not supported on this processor"));
5981           return;
5982         }
5983       /* Even on a big endian machine $fn comes before $fn+1.  We have
5984          to adjust when loading from memory.  */
5985       r = BFD_RELOC_LO16;
5986     dob:
5987       assert (mips_opts.isa == ISA_MIPS1);
5988       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5989                    target_big_endian ? treg + 1 : treg,
5990                    (int) r, breg);
5991       /* FIXME: A possible overflow which I don't know how to deal
5992          with.  */
5993       offset_expr.X_add_number += 4;
5994       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5995                    target_big_endian ? treg : treg + 1,
5996                    (int) r, breg);
5997
5998       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5999          does not become a variant frag.  */
6000       frag_wane (frag_now);
6001       frag_new (0);
6002
6003       if (breg != AT)
6004         return;
6005       break;
6006
6007     case M_L_DAB:
6008       /*
6009        * The MIPS assembler seems to check for X_add_number not
6010        * being double aligned and generating:
6011        *        lui     at,%hi(foo+1)
6012        *        addu    at,at,v1
6013        *        addiu   at,at,%lo(foo+1)
6014        *        lwc1    f2,0(at)
6015        *        lwc1    f3,4(at)
6016        * But, the resulting address is the same after relocation so why
6017        * generate the extra instruction?
6018        */
6019       if (mips_arch == CPU_R4650)
6020         {
6021           as_bad (_("opcode not supported on this processor"));
6022           return;
6023         }
6024       /* Itbl support may require additional care here.  */
6025       coproc = 1;
6026       if (mips_opts.isa != ISA_MIPS1)
6027         {
6028           s = "ldc1";
6029           goto ld;
6030         }
6031
6032       s = "lwc1";
6033       fmt = "T,o(b)";
6034       goto ldd_std;
6035
6036     case M_S_DAB:
6037       if (mips_arch == CPU_R4650)
6038         {
6039           as_bad (_("opcode not supported on this processor"));
6040           return;
6041         }
6042
6043       if (mips_opts.isa != ISA_MIPS1)
6044         {
6045           s = "sdc1";
6046           goto st;
6047         }
6048
6049       s = "swc1";
6050       fmt = "T,o(b)";
6051       /* Itbl support may require additional care here.  */
6052       coproc = 1;
6053       goto ldd_std;
6054
6055     case M_LD_AB:
6056       if (HAVE_64BIT_GPRS)
6057         {
6058           s = "ld";
6059           goto ld;
6060         }
6061
6062       s = "lw";
6063       fmt = "t,o(b)";
6064       goto ldd_std;
6065
6066     case M_SD_AB:
6067       if (HAVE_64BIT_GPRS)
6068         {
6069           s = "sd";
6070           goto st;
6071         }
6072
6073       s = "sw";
6074       fmt = "t,o(b)";
6075
6076     ldd_std:
6077       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6078          loads for the case of doing a pair of loads to simulate an 'ld'.
6079          This is not currently done by the compiler, and assembly coders
6080          writing embedded-pic code can cope.  */
6081
6082       if (offset_expr.X_op != O_symbol
6083           && offset_expr.X_op != O_constant)
6084         {
6085           as_bad (_("expression too complex"));
6086           offset_expr.X_op = O_constant;
6087         }
6088
6089       /* Even on a big endian machine $fn comes before $fn+1.  We have
6090          to adjust when loading from memory.  We set coproc if we must
6091          load $fn+1 first.  */
6092       /* Itbl support may require additional care here.  */
6093       if (! target_big_endian)
6094         coproc = 0;
6095
6096       if (mips_pic == NO_PIC
6097           || offset_expr.X_op == O_constant)
6098         {
6099           char *p;
6100
6101           /* If this is a reference to a GP relative symbol, we want
6102                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6103                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6104              If we have a base register, we use this
6105                addu     $at,$breg,$gp
6106                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6107                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6108              If this is not a GP relative symbol, we want
6109                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6110                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6111                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6112              If there is a base register, we add it to $at after the
6113              lui instruction.  If there is a constant, we always use
6114              the last case.  */
6115           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6116               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6117             {
6118               p = NULL;
6119               used_at = 1;
6120             }
6121           else
6122             {
6123               int off;
6124
6125               if (breg == 0)
6126                 {
6127                   frag_grow (28);
6128                   tempreg = mips_gp_register;
6129                   off = 0;
6130                   used_at = 0;
6131                 }
6132               else
6133                 {
6134                   frag_grow (36);
6135                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6136                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6137                                "d,v,t", AT, breg, mips_gp_register);
6138                   tempreg = AT;
6139                   off = 4;
6140                   used_at = 1;
6141                 }
6142
6143               /* Itbl support may require additional care here.  */
6144               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6145                            coproc ? treg + 1 : treg,
6146                            (int) BFD_RELOC_GPREL16, tempreg);
6147               offset_expr.X_add_number += 4;
6148
6149               /* Set mips_optimize to 2 to avoid inserting an
6150                  undesired nop.  */
6151               hold_mips_optimize = mips_optimize;
6152               mips_optimize = 2;
6153               /* Itbl support may require additional care here.  */
6154               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6155                            coproc ? treg : treg + 1,
6156                            (int) BFD_RELOC_GPREL16, tempreg);
6157               mips_optimize = hold_mips_optimize;
6158
6159               p = frag_var (rs_machine_dependent, 12 + off, 0,
6160                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6161                                           used_at && mips_opts.noat),
6162                             offset_expr.X_add_symbol, 0, NULL);
6163
6164               /* We just generated two relocs.  When tc_gen_reloc
6165                  handles this case, it will skip the first reloc and
6166                  handle the second.  The second reloc already has an
6167                  extra addend of 4, which we added above.  We must
6168                  subtract it out, and then subtract another 4 to make
6169                  the first reloc come out right.  The second reloc
6170                  will come out right because we are going to add 4 to
6171                  offset_expr when we build its instruction below.
6172
6173                  If we have a symbol, then we don't want to include
6174                  the offset, because it will wind up being included
6175                  when we generate the reloc.  */
6176
6177               if (offset_expr.X_op == O_constant)
6178                 offset_expr.X_add_number -= 8;
6179               else
6180                 {
6181                   offset_expr.X_add_number = -4;
6182                   offset_expr.X_op = O_constant;
6183                 }
6184             }
6185           macro_build_lui (p, &icnt, &offset_expr, AT);
6186           if (p != NULL)
6187             p += 4;
6188           if (breg != 0)
6189             {
6190               macro_build (p, &icnt, (expressionS *) NULL,
6191                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6192                            "d,v,t", AT, breg, AT);
6193               if (p != NULL)
6194                 p += 4;
6195             }
6196           /* Itbl support may require additional care here.  */
6197           macro_build (p, &icnt, &offset_expr, s, fmt,
6198                        coproc ? treg + 1 : treg,
6199                        (int) BFD_RELOC_LO16, AT);
6200           if (p != NULL)
6201             p += 4;
6202           /* FIXME: How do we handle overflow here?  */
6203           offset_expr.X_add_number += 4;
6204           /* Itbl support may require additional care here.  */
6205           macro_build (p, &icnt, &offset_expr, s, fmt,
6206                        coproc ? treg : treg + 1,
6207                        (int) BFD_RELOC_LO16, AT);
6208         }
6209       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6210         {
6211           int off;
6212
6213           /* If this is a reference to an external symbol, we want
6214                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6215                nop
6216                <op>     $treg,0($at)
6217                <op>     $treg+1,4($at)
6218              Otherwise we want
6219                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6220                nop
6221                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6222                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6223              If there is a base register we add it to $at before the
6224              lwc1 instructions.  If there is a constant we include it
6225              in the lwc1 instructions.  */
6226           used_at = 1;
6227           expr1.X_add_number = offset_expr.X_add_number;
6228           offset_expr.X_add_number = 0;
6229           if (expr1.X_add_number < -0x8000
6230               || expr1.X_add_number >= 0x8000 - 4)
6231             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6232           if (breg == 0)
6233             off = 0;
6234           else
6235             off = 4;
6236           frag_grow (24 + off);
6237           macro_build ((char *) NULL, &icnt, &offset_expr,
6238                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6239                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6240           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6241           if (breg != 0)
6242             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6243                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6244                          "d,v,t", AT, breg, AT);
6245           /* Itbl support may require additional care here.  */
6246           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6247                        coproc ? treg + 1 : treg,
6248                        (int) BFD_RELOC_LO16, AT);
6249           expr1.X_add_number += 4;
6250
6251           /* Set mips_optimize to 2 to avoid inserting an undesired
6252              nop.  */
6253           hold_mips_optimize = mips_optimize;
6254           mips_optimize = 2;
6255           /* Itbl support may require additional care here.  */
6256           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6257                        coproc ? treg : treg + 1,
6258                        (int) BFD_RELOC_LO16, AT);
6259           mips_optimize = hold_mips_optimize;
6260
6261           (void) frag_var (rs_machine_dependent, 0, 0,
6262                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6263                            offset_expr.X_add_symbol, 0, NULL);
6264         }
6265       else if (mips_pic == SVR4_PIC)
6266         {
6267           int gpdel, off;
6268           char *p;
6269
6270           /* If this is a reference to an external symbol, we want
6271                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6272                addu     $at,$at,$gp
6273                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6274                nop
6275                <op>     $treg,0($at)
6276                <op>     $treg+1,4($at)
6277              Otherwise we want
6278                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6279                nop
6280                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6281                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6282              If there is a base register we add it to $at before the
6283              lwc1 instructions.  If there is a constant we include it
6284              in the lwc1 instructions.  */
6285           used_at = 1;
6286           expr1.X_add_number = offset_expr.X_add_number;
6287           offset_expr.X_add_number = 0;
6288           if (expr1.X_add_number < -0x8000
6289               || expr1.X_add_number >= 0x8000 - 4)
6290             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6291           if (reg_needs_delay (mips_gp_register))
6292             gpdel = 4;
6293           else
6294             gpdel = 0;
6295           if (breg == 0)
6296             off = 0;
6297           else
6298             off = 4;
6299           frag_grow (56);
6300           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6301                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6302           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6303                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6304                        "d,v,t", AT, AT, mips_gp_register);
6305           macro_build ((char *) NULL, &icnt, &offset_expr,
6306                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6307                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6308           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6309           if (breg != 0)
6310             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6311                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6312                          "d,v,t", AT, breg, AT);
6313           /* Itbl support may require additional care here.  */
6314           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6315                        coproc ? treg + 1 : treg,
6316                        (int) BFD_RELOC_LO16, AT);
6317           expr1.X_add_number += 4;
6318
6319           /* Set mips_optimize to 2 to avoid inserting an undesired
6320              nop.  */
6321           hold_mips_optimize = mips_optimize;
6322           mips_optimize = 2;
6323           /* Itbl support may require additional care here.  */
6324           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6325                        coproc ? treg : treg + 1,
6326                        (int) BFD_RELOC_LO16, AT);
6327           mips_optimize = hold_mips_optimize;
6328           expr1.X_add_number -= 4;
6329
6330           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6331                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6332                                       8 + gpdel + off, 1, 0),
6333                         offset_expr.X_add_symbol, 0, NULL);
6334           if (gpdel > 0)
6335             {
6336               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6337               p += 4;
6338             }
6339           macro_build (p, &icnt, &offset_expr,
6340                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6341                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6342                        mips_gp_register);
6343           p += 4;
6344           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6345           p += 4;
6346           if (breg != 0)
6347             {
6348               macro_build (p, &icnt, (expressionS *) NULL,
6349                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6350                            "d,v,t", AT, breg, AT);
6351               p += 4;
6352             }
6353           /* Itbl support may require additional care here.  */
6354           macro_build (p, &icnt, &expr1, s, fmt,
6355                        coproc ? treg + 1 : treg,
6356                        (int) BFD_RELOC_LO16, AT);
6357           p += 4;
6358           expr1.X_add_number += 4;
6359
6360           /* Set mips_optimize to 2 to avoid inserting an undesired
6361              nop.  */
6362           hold_mips_optimize = mips_optimize;
6363           mips_optimize = 2;
6364           /* Itbl support may require additional care here.  */
6365           macro_build (p, &icnt, &expr1, s, fmt,
6366                        coproc ? treg : treg + 1,
6367                        (int) BFD_RELOC_LO16, AT);
6368           mips_optimize = hold_mips_optimize;
6369         }
6370       else if (mips_pic == EMBEDDED_PIC)
6371         {
6372           /* If there is no base register, we use
6373                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6374                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6375              If we have a base register, we use
6376                addu     $at,$breg,$gp
6377                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6378                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6379              */
6380           if (breg == 0)
6381             {
6382               tempreg = mips_gp_register;
6383               used_at = 0;
6384             }
6385           else
6386             {
6387               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6388                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6389                            "d,v,t", AT, breg, mips_gp_register);
6390               tempreg = AT;
6391               used_at = 1;
6392             }
6393
6394           /* Itbl support may require additional care here.  */
6395           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6396                        coproc ? treg + 1 : treg,
6397                        (int) BFD_RELOC_GPREL16, tempreg);
6398           offset_expr.X_add_number += 4;
6399           /* Itbl support may require additional care here.  */
6400           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6401                        coproc ? treg : treg + 1,
6402                        (int) BFD_RELOC_GPREL16, tempreg);
6403         }
6404       else
6405         abort ();
6406
6407       if (! used_at)
6408         return;
6409
6410       break;
6411
6412     case M_LD_OB:
6413       s = "lw";
6414       goto sd_ob;
6415     case M_SD_OB:
6416       s = "sw";
6417     sd_ob:
6418       assert (HAVE_32BIT_ADDRESSES);
6419       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6420                    (int) BFD_RELOC_LO16, breg);
6421       offset_expr.X_add_number += 4;
6422       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6423                    (int) BFD_RELOC_LO16, breg);
6424       return;
6425
6426    /* New code added to support COPZ instructions.
6427       This code builds table entries out of the macros in mip_opcodes.
6428       R4000 uses interlocks to handle coproc delays.
6429       Other chips (like the R3000) require nops to be inserted for delays.
6430
6431       FIXME: Currently, we require that the user handle delays.
6432       In order to fill delay slots for non-interlocked chips,
6433       we must have a way to specify delays based on the coprocessor.
6434       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6435       What are the side-effects of the cop instruction?
6436       What cache support might we have and what are its effects?
6437       Both coprocessor & memory require delays. how long???
6438       What registers are read/set/modified?
6439
6440       If an itbl is provided to interpret cop instructions,
6441       this knowledge can be encoded in the itbl spec.  */
6442
6443     case M_COP0:
6444       s = "c0";
6445       goto copz;
6446     case M_COP1:
6447       s = "c1";
6448       goto copz;
6449     case M_COP2:
6450       s = "c2";
6451       goto copz;
6452     case M_COP3:
6453       s = "c3";
6454     copz:
6455       /* For now we just do C (same as Cz).  The parameter will be
6456          stored in insn_opcode by mips_ip.  */
6457       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6458                    ip->insn_opcode);
6459       return;
6460
6461     case M_MOVE:
6462       move_register (&icnt, dreg, sreg);
6463       return;
6464
6465 #ifdef LOSING_COMPILER
6466     default:
6467       /* Try and see if this is a new itbl instruction.
6468          This code builds table entries out of the macros in mip_opcodes.
6469          FIXME: For now we just assemble the expression and pass it's
6470          value along as a 32-bit immediate.
6471          We may want to have the assembler assemble this value,
6472          so that we gain the assembler's knowledge of delay slots,
6473          symbols, etc.
6474          Would it be more efficient to use mask (id) here? */
6475       if (itbl_have_entries
6476           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6477         {
6478           s = ip->insn_mo->name;
6479           s2 = "cop3";
6480           coproc = ITBL_DECODE_PNUM (immed_expr);;
6481           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6482           return;
6483         }
6484       macro2 (ip);
6485       return;
6486     }
6487   if (mips_opts.noat)
6488     as_warn (_("Macro used $at after \".set noat\""));
6489 }
6490
6491 static void
6492 macro2 (ip)
6493      struct mips_cl_insn *ip;
6494 {
6495   register int treg, sreg, dreg, breg;
6496   int tempreg;
6497   int mask;
6498   int icnt = 0;
6499   int used_at;
6500   expressionS expr1;
6501   const char *s;
6502   const char *s2;
6503   const char *fmt;
6504   int likely = 0;
6505   int dbl = 0;
6506   int coproc = 0;
6507   int lr = 0;
6508   int imm = 0;
6509   int off;
6510   offsetT maxnum;
6511   bfd_reloc_code_real_type r;
6512   char *p;
6513
6514   treg = (ip->insn_opcode >> 16) & 0x1f;
6515   dreg = (ip->insn_opcode >> 11) & 0x1f;
6516   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6517   mask = ip->insn_mo->mask;
6518
6519   expr1.X_op = O_constant;
6520   expr1.X_op_symbol = NULL;
6521   expr1.X_add_symbol = NULL;
6522   expr1.X_add_number = 1;
6523
6524   switch (mask)
6525     {
6526 #endif /* LOSING_COMPILER */
6527
6528     case M_DMUL:
6529       dbl = 1;
6530     case M_MUL:
6531       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6532                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6533       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6534                    dreg);
6535       return;
6536
6537     case M_DMUL_I:
6538       dbl = 1;
6539     case M_MUL_I:
6540       /* The MIPS assembler some times generates shifts and adds.  I'm
6541          not trying to be that fancy. GCC should do this for us
6542          anyway.  */
6543       load_register (&icnt, AT, &imm_expr, dbl);
6544       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6545                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6546       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6547                    dreg);
6548       break;
6549
6550     case M_DMULO_I:
6551       dbl = 1;
6552     case M_MULO_I:
6553       imm = 1;
6554       goto do_mulo;
6555
6556     case M_DMULO:
6557       dbl = 1;
6558     case M_MULO:
6559     do_mulo:
6560       mips_emit_delays (true);
6561       ++mips_opts.noreorder;
6562       mips_any_noreorder = 1;
6563       if (imm)
6564         load_register (&icnt, AT, &imm_expr, dbl);
6565       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6566                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6567       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6568                    dreg);
6569       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6570                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6571       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6572                    AT);
6573       if (mips_trap)
6574         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6575                      dreg, AT);
6576       else
6577         {
6578           expr1.X_add_number = 8;
6579           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6580                        AT);
6581           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6582                        0);
6583           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6584                        "c", 6);
6585         }
6586       --mips_opts.noreorder;
6587       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6588       break;
6589
6590     case M_DMULOU_I:
6591       dbl = 1;
6592     case M_MULOU_I:
6593       imm = 1;
6594       goto do_mulou;
6595
6596     case M_DMULOU:
6597       dbl = 1;
6598     case M_MULOU:
6599     do_mulou:
6600       mips_emit_delays (true);
6601       ++mips_opts.noreorder;
6602       mips_any_noreorder = 1;
6603       if (imm)
6604         load_register (&icnt, AT, &imm_expr, dbl);
6605       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6606                    dbl ? "dmultu" : "multu",
6607                    "s,t", sreg, imm ? AT : treg);
6608       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6609                    AT);
6610       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6611                    dreg);
6612       if (mips_trap)
6613         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6614                      AT, 0);
6615       else
6616         {
6617           expr1.X_add_number = 8;
6618           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6619           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6620                        0);
6621           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6622                        "c", 6);
6623         }
6624       --mips_opts.noreorder;
6625       break;
6626
6627     case M_DROL:
6628       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6629                    "d,v,t", AT, 0, treg);
6630       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6631                    "d,t,s", AT, sreg, AT);
6632       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6633                    "d,t,s", dreg, sreg, treg);
6634       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6635                    "d,v,t", dreg, dreg, AT);
6636       break;
6637
6638     case M_ROL:
6639       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6640                    "d,v,t", AT, 0, treg);
6641       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6642                    "d,t,s", AT, sreg, AT);
6643       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6644                    "d,t,s", dreg, sreg, treg);
6645       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6646                    "d,v,t", dreg, dreg, AT);
6647       break;
6648
6649     case M_DROL_I:
6650       {
6651         unsigned int rot;
6652         char *l, *r;
6653
6654         if (imm_expr.X_op != O_constant)
6655           as_bad (_("rotate count too large"));
6656         rot = imm_expr.X_add_number & 0x3f;
6657         if (! rot)
6658           break;
6659         l = (rot < 0x20) ? "dsll" : "dsll32";
6660         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6661         rot &= 0x1f;
6662         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6663                      "d,w,<", AT, sreg, rot);
6664         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6665                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6666         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6667                      "d,v,t", dreg, dreg, AT);
6668       }
6669       break;
6670
6671     case M_ROL_I:
6672       {
6673         unsigned int rot;
6674
6675         if (imm_expr.X_op != O_constant)
6676           as_bad (_("rotate count too large"));
6677         rot = imm_expr.X_add_number & 0x1f;
6678         if (! rot)
6679           break;
6680         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6681                      "d,w,<", AT, sreg, rot);
6682         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6683                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6684         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6685                      "d,v,t", dreg, dreg, AT);
6686       }
6687       break;
6688
6689     case M_DROR:
6690       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6691                    "d,v,t", AT, 0, treg);
6692       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6693                    "d,t,s", AT, sreg, AT);
6694       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6695                    "d,t,s", dreg, sreg, treg);
6696       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6697                    "d,v,t", dreg, dreg, AT);
6698       break;
6699
6700     case M_ROR:
6701       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6702                    "d,v,t", AT, 0, treg);
6703       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6704                    "d,t,s", AT, sreg, AT);
6705       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6706                    "d,t,s", dreg, sreg, treg);
6707       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6708                    "d,v,t", dreg, dreg, AT);
6709       break;
6710
6711     case M_DROR_I:
6712       {
6713         unsigned int rot;
6714         char *l, *r;
6715
6716         if (imm_expr.X_op != O_constant)
6717           as_bad (_("rotate count too large"));
6718         rot = imm_expr.X_add_number & 0x3f;
6719         if (! rot)
6720           break;
6721         r = (rot < 0x20) ? "dsrl" : "dsrl32";
6722         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6723         rot &= 0x1f;
6724         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6725                      "d,w,<", AT, sreg, rot);
6726         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6727                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6728         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6729                      "d,v,t", dreg, dreg, AT);
6730       }
6731       break;
6732
6733     case M_ROR_I:
6734       {
6735         unsigned int rot;
6736
6737         if (imm_expr.X_op != O_constant)
6738           as_bad (_("rotate count too large"));
6739         rot = imm_expr.X_add_number & 0x1f;
6740         if (! rot)
6741           break;
6742         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6743                      "d,w,<", AT, sreg, rot);
6744         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6745                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6746         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6747                      "d,v,t", dreg, dreg, AT);
6748       }
6749       break;
6750
6751     case M_S_DOB:
6752       if (mips_arch == CPU_R4650)
6753         {
6754           as_bad (_("opcode not supported on this processor"));
6755           return;
6756         }
6757       assert (mips_opts.isa == ISA_MIPS1);
6758       /* Even on a big endian machine $fn comes before $fn+1.  We have
6759          to adjust when storing to memory.  */
6760       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6761                    target_big_endian ? treg + 1 : treg,
6762                    (int) BFD_RELOC_LO16, breg);
6763       offset_expr.X_add_number += 4;
6764       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6765                    target_big_endian ? treg : treg + 1,
6766                    (int) BFD_RELOC_LO16, breg);
6767       return;
6768
6769     case M_SEQ:
6770       if (sreg == 0)
6771         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6772                      treg, (int) BFD_RELOC_LO16);
6773       else if (treg == 0)
6774         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6775                      sreg, (int) BFD_RELOC_LO16);
6776       else
6777         {
6778           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6779                        "d,v,t", dreg, sreg, treg);
6780           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6781                        dreg, (int) BFD_RELOC_LO16);
6782         }
6783       return;
6784
6785     case M_SEQ_I:
6786       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6787         {
6788           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6789                        sreg, (int) BFD_RELOC_LO16);
6790           return;
6791         }
6792       if (sreg == 0)
6793         {
6794           as_warn (_("Instruction %s: result is always false"),
6795                    ip->insn_mo->name);
6796           move_register (&icnt, dreg, 0);
6797           return;
6798         }
6799       if (imm_expr.X_op == O_constant
6800           && imm_expr.X_add_number >= 0
6801           && imm_expr.X_add_number < 0x10000)
6802         {
6803           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6804                        sreg, (int) BFD_RELOC_LO16);
6805           used_at = 0;
6806         }
6807       else if (imm_expr.X_op == O_constant
6808                && imm_expr.X_add_number > -0x8000
6809                && imm_expr.X_add_number < 0)
6810         {
6811           imm_expr.X_add_number = -imm_expr.X_add_number;
6812           macro_build ((char *) NULL, &icnt, &imm_expr,
6813                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6814                        "t,r,j", dreg, sreg,
6815                        (int) BFD_RELOC_LO16);
6816           used_at = 0;
6817         }
6818       else
6819         {
6820           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6821           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6822                        "d,v,t", dreg, sreg, AT);
6823           used_at = 1;
6824         }
6825       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6826                    (int) BFD_RELOC_LO16);
6827       if (used_at)
6828         break;
6829       return;
6830
6831     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6832       s = "slt";
6833       goto sge;
6834     case M_SGEU:
6835       s = "sltu";
6836     sge:
6837       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6838                    dreg, sreg, treg);
6839       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6840                    (int) BFD_RELOC_LO16);
6841       return;
6842
6843     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6844     case M_SGEU_I:
6845       if (imm_expr.X_op == O_constant
6846           && imm_expr.X_add_number >= -0x8000
6847           && imm_expr.X_add_number < 0x8000)
6848         {
6849           macro_build ((char *) NULL, &icnt, &imm_expr,
6850                        mask == M_SGE_I ? "slti" : "sltiu",
6851                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6852           used_at = 0;
6853         }
6854       else
6855         {
6856           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6857           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6858                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6859                        AT);
6860           used_at = 1;
6861         }
6862       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6863                    (int) BFD_RELOC_LO16);
6864       if (used_at)
6865         break;
6866       return;
6867
6868     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6869       s = "slt";
6870       goto sgt;
6871     case M_SGTU:
6872       s = "sltu";
6873     sgt:
6874       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6875                    dreg, treg, sreg);
6876       return;
6877
6878     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6879       s = "slt";
6880       goto sgti;
6881     case M_SGTU_I:
6882       s = "sltu";
6883     sgti:
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       break;
6888
6889     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6890       s = "slt";
6891       goto sle;
6892     case M_SLEU:
6893       s = "sltu";
6894     sle:
6895       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6896                    dreg, treg, sreg);
6897       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6898                    (int) BFD_RELOC_LO16);
6899       return;
6900
6901     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6902       s = "slt";
6903       goto slei;
6904     case M_SLEU_I:
6905       s = "sltu";
6906     slei:
6907       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6908       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6909                    dreg, AT, sreg);
6910       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6911                    (int) BFD_RELOC_LO16);
6912       break;
6913
6914     case M_SLT_I:
6915       if (imm_expr.X_op == O_constant
6916           && imm_expr.X_add_number >= -0x8000
6917           && imm_expr.X_add_number < 0x8000)
6918         {
6919           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6920                        dreg, sreg, (int) BFD_RELOC_LO16);
6921           return;
6922         }
6923       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6924       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6925                    dreg, sreg, AT);
6926       break;
6927
6928     case M_SLTU_I:
6929       if (imm_expr.X_op == O_constant
6930           && imm_expr.X_add_number >= -0x8000
6931           && imm_expr.X_add_number < 0x8000)
6932         {
6933           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6934                        dreg, sreg, (int) BFD_RELOC_LO16);
6935           return;
6936         }
6937       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6938       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6939                    "d,v,t", dreg, sreg, AT);
6940       break;
6941
6942     case M_SNE:
6943       if (sreg == 0)
6944         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6945                      "d,v,t", dreg, 0, treg);
6946       else if (treg == 0)
6947         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6948                      "d,v,t", dreg, 0, sreg);
6949       else
6950         {
6951           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6952                        "d,v,t", dreg, sreg, treg);
6953           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6954                        "d,v,t", dreg, 0, dreg);
6955         }
6956       return;
6957
6958     case M_SNE_I:
6959       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6960         {
6961           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6962                        "d,v,t", dreg, 0, sreg);
6963           return;
6964         }
6965       if (sreg == 0)
6966         {
6967           as_warn (_("Instruction %s: result is always true"),
6968                    ip->insn_mo->name);
6969           macro_build ((char *) NULL, &icnt, &expr1,
6970                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6971                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6972           return;
6973         }
6974       if (imm_expr.X_op == O_constant
6975           && imm_expr.X_add_number >= 0
6976           && imm_expr.X_add_number < 0x10000)
6977         {
6978           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6979                        dreg, sreg, (int) BFD_RELOC_LO16);
6980           used_at = 0;
6981         }
6982       else if (imm_expr.X_op == O_constant
6983                && imm_expr.X_add_number > -0x8000
6984                && imm_expr.X_add_number < 0)
6985         {
6986           imm_expr.X_add_number = -imm_expr.X_add_number;
6987           macro_build ((char *) NULL, &icnt, &imm_expr,
6988                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6989                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6990           used_at = 0;
6991         }
6992       else
6993         {
6994           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6995           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6996                        "d,v,t", dreg, sreg, AT);
6997           used_at = 1;
6998         }
6999       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7000                    "d,v,t", dreg, 0, dreg);
7001       if (used_at)
7002         break;
7003       return;
7004
7005     case M_DSUB_I:
7006       dbl = 1;
7007     case M_SUB_I:
7008       if (imm_expr.X_op == O_constant
7009           && imm_expr.X_add_number > -0x8000
7010           && imm_expr.X_add_number <= 0x8000)
7011         {
7012           imm_expr.X_add_number = -imm_expr.X_add_number;
7013           macro_build ((char *) NULL, &icnt, &imm_expr,
7014                        dbl ? "daddi" : "addi",
7015                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7016           return;
7017         }
7018       load_register (&icnt, AT, &imm_expr, dbl);
7019       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7020                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7021       break;
7022
7023     case M_DSUBU_I:
7024       dbl = 1;
7025     case M_SUBU_I:
7026       if (imm_expr.X_op == O_constant
7027           && imm_expr.X_add_number > -0x8000
7028           && imm_expr.X_add_number <= 0x8000)
7029         {
7030           imm_expr.X_add_number = -imm_expr.X_add_number;
7031           macro_build ((char *) NULL, &icnt, &imm_expr,
7032                        dbl ? "daddiu" : "addiu",
7033                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7034           return;
7035         }
7036       load_register (&icnt, AT, &imm_expr, dbl);
7037       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7038                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7039       break;
7040
7041     case M_TEQ_I:
7042       s = "teq";
7043       goto trap;
7044     case M_TGE_I:
7045       s = "tge";
7046       goto trap;
7047     case M_TGEU_I:
7048       s = "tgeu";
7049       goto trap;
7050     case M_TLT_I:
7051       s = "tlt";
7052       goto trap;
7053     case M_TLTU_I:
7054       s = "tltu";
7055       goto trap;
7056     case M_TNE_I:
7057       s = "tne";
7058     trap:
7059       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7060       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7061                    AT);
7062       break;
7063
7064     case M_TRUNCWS:
7065     case M_TRUNCWD:
7066       assert (mips_opts.isa == ISA_MIPS1);
7067       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7068       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7069
7070       /*
7071        * Is the double cfc1 instruction a bug in the mips assembler;
7072        * or is there a reason for it?
7073        */
7074       mips_emit_delays (true);
7075       ++mips_opts.noreorder;
7076       mips_any_noreorder = 1;
7077       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7078                    treg, RA);
7079       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7080                    treg, RA);
7081       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7082       expr1.X_add_number = 3;
7083       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7084                    (int) BFD_RELOC_LO16);
7085       expr1.X_add_number = 2;
7086       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7087                      (int) BFD_RELOC_LO16);
7088       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7089                    AT, RA);
7090       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7091       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7092               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7093       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7094                    treg, RA);
7095       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7096       --mips_opts.noreorder;
7097       break;
7098
7099     case M_ULH:
7100       s = "lb";
7101       goto ulh;
7102     case M_ULHU:
7103       s = "lbu";
7104     ulh:
7105       if (offset_expr.X_add_number >= 0x7fff)
7106         as_bad (_("operand overflow"));
7107       /* avoid load delay */
7108       if (! target_big_endian)
7109         ++offset_expr.X_add_number;
7110       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7111                    (int) BFD_RELOC_LO16, breg);
7112       if (! target_big_endian)
7113         --offset_expr.X_add_number;
7114       else
7115         ++offset_expr.X_add_number;
7116       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7117                    (int) BFD_RELOC_LO16, breg);
7118       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7119                    treg, treg, 8);
7120       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7121                    treg, treg, AT);
7122       break;
7123
7124     case M_ULD:
7125       s = "ldl";
7126       s2 = "ldr";
7127       off = 7;
7128       goto ulw;
7129     case M_ULW:
7130       s = "lwl";
7131       s2 = "lwr";
7132       off = 3;
7133     ulw:
7134       if (offset_expr.X_add_number >= 0x8000 - off)
7135         as_bad (_("operand overflow"));
7136       if (! target_big_endian)
7137         offset_expr.X_add_number += off;
7138       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7139                    (int) BFD_RELOC_LO16, breg);
7140       if (! target_big_endian)
7141         offset_expr.X_add_number -= off;
7142       else
7143         offset_expr.X_add_number += off;
7144       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7145                    (int) BFD_RELOC_LO16, breg);
7146       return;
7147
7148     case M_ULD_A:
7149       s = "ldl";
7150       s2 = "ldr";
7151       off = 7;
7152       goto ulwa;
7153     case M_ULW_A:
7154       s = "lwl";
7155       s2 = "lwr";
7156       off = 3;
7157     ulwa:
7158       used_at = 1;
7159       load_address (&icnt, AT, &offset_expr, &used_at);
7160       if (breg != 0)
7161         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7162                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7163                      "d,v,t", AT, AT, breg);
7164       if (! target_big_endian)
7165         expr1.X_add_number = off;
7166       else
7167         expr1.X_add_number = 0;
7168       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7169                    (int) BFD_RELOC_LO16, AT);
7170       if (! target_big_endian)
7171         expr1.X_add_number = 0;
7172       else
7173         expr1.X_add_number = off;
7174       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7175                    (int) BFD_RELOC_LO16, AT);
7176       break;
7177
7178     case M_ULH_A:
7179     case M_ULHU_A:
7180       used_at = 1;
7181       load_address (&icnt, AT, &offset_expr, &used_at);
7182       if (breg != 0)
7183         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7184                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7185                      "d,v,t", AT, AT, breg);
7186       if (target_big_endian)
7187         expr1.X_add_number = 0;
7188       macro_build ((char *) NULL, &icnt, &expr1,
7189                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7190                    (int) BFD_RELOC_LO16, AT);
7191       if (target_big_endian)
7192         expr1.X_add_number = 1;
7193       else
7194         expr1.X_add_number = 0;
7195       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7196                    (int) BFD_RELOC_LO16, AT);
7197       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7198                    treg, treg, 8);
7199       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7200                    treg, treg, AT);
7201       break;
7202
7203     case M_USH:
7204       if (offset_expr.X_add_number >= 0x7fff)
7205         as_bad (_("operand overflow"));
7206       if (target_big_endian)
7207         ++offset_expr.X_add_number;
7208       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7209                    (int) BFD_RELOC_LO16, breg);
7210       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7211                    AT, treg, 8);
7212       if (target_big_endian)
7213         --offset_expr.X_add_number;
7214       else
7215         ++offset_expr.X_add_number;
7216       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7217                    (int) BFD_RELOC_LO16, breg);
7218       break;
7219
7220     case M_USD:
7221       s = "sdl";
7222       s2 = "sdr";
7223       off = 7;
7224       goto usw;
7225     case M_USW:
7226       s = "swl";
7227       s2 = "swr";
7228       off = 3;
7229     usw:
7230       if (offset_expr.X_add_number >= 0x8000 - off)
7231         as_bad (_("operand overflow"));
7232       if (! target_big_endian)
7233         offset_expr.X_add_number += off;
7234       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7235                    (int) BFD_RELOC_LO16, breg);
7236       if (! target_big_endian)
7237         offset_expr.X_add_number -= off;
7238       else
7239         offset_expr.X_add_number += off;
7240       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7241                    (int) BFD_RELOC_LO16, breg);
7242       return;
7243
7244     case M_USD_A:
7245       s = "sdl";
7246       s2 = "sdr";
7247       off = 7;
7248       goto uswa;
7249     case M_USW_A:
7250       s = "swl";
7251       s2 = "swr";
7252       off = 3;
7253     uswa:
7254       used_at = 1;
7255       load_address (&icnt, AT, &offset_expr, &used_at);
7256       if (breg != 0)
7257         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7258                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7259                      "d,v,t", AT, AT, breg);
7260       if (! target_big_endian)
7261         expr1.X_add_number = off;
7262       else
7263         expr1.X_add_number = 0;
7264       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7265                    (int) BFD_RELOC_LO16, AT);
7266       if (! target_big_endian)
7267         expr1.X_add_number = 0;
7268       else
7269         expr1.X_add_number = off;
7270       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7271                    (int) BFD_RELOC_LO16, AT);
7272       break;
7273
7274     case M_USH_A:
7275       used_at = 1;
7276       load_address (&icnt, AT, &offset_expr, &used_at);
7277       if (breg != 0)
7278         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7279                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7280                      "d,v,t", AT, AT, breg);
7281       if (! target_big_endian)
7282         expr1.X_add_number = 0;
7283       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7284                    (int) BFD_RELOC_LO16, AT);
7285       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7286                    treg, treg, 8);
7287       if (! target_big_endian)
7288         expr1.X_add_number = 1;
7289       else
7290         expr1.X_add_number = 0;
7291       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7292                    (int) BFD_RELOC_LO16, AT);
7293       if (! target_big_endian)
7294         expr1.X_add_number = 0;
7295       else
7296         expr1.X_add_number = 1;
7297       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7298                    (int) BFD_RELOC_LO16, AT);
7299       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7300                    treg, treg, 8);
7301       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7302                    treg, treg, AT);
7303       break;
7304
7305     default:
7306       /* FIXME: Check if this is one of the itbl macros, since they
7307          are added dynamically.  */
7308       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7309       break;
7310     }
7311   if (mips_opts.noat)
7312     as_warn (_("Macro used $at after \".set noat\""));
7313 }
7314
7315 /* Implement macros in mips16 mode.  */
7316
7317 static void
7318 mips16_macro (ip)
7319      struct mips_cl_insn *ip;
7320 {
7321   int mask;
7322   int xreg, yreg, zreg, tmp;
7323   int icnt;
7324   expressionS expr1;
7325   int dbl;
7326   const char *s, *s2, *s3;
7327
7328   mask = ip->insn_mo->mask;
7329
7330   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7331   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7332   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7333
7334   icnt = 0;
7335
7336   expr1.X_op = O_constant;
7337   expr1.X_op_symbol = NULL;
7338   expr1.X_add_symbol = NULL;
7339   expr1.X_add_number = 1;
7340
7341   dbl = 0;
7342
7343   switch (mask)
7344     {
7345     default:
7346       internalError ();
7347
7348     case M_DDIV_3:
7349       dbl = 1;
7350     case M_DIV_3:
7351       s = "mflo";
7352       goto do_div3;
7353     case M_DREM_3:
7354       dbl = 1;
7355     case M_REM_3:
7356       s = "mfhi";
7357     do_div3:
7358       mips_emit_delays (true);
7359       ++mips_opts.noreorder;
7360       mips_any_noreorder = 1;
7361       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7362                    dbl ? "ddiv" : "div",
7363                    "0,x,y", xreg, yreg);
7364       expr1.X_add_number = 2;
7365       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7366       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7367                    7);
7368
7369       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7370          since that causes an overflow.  We should do that as well,
7371          but I don't see how to do the comparisons without a temporary
7372          register.  */
7373       --mips_opts.noreorder;
7374       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7375       break;
7376
7377     case M_DIVU_3:
7378       s = "divu";
7379       s2 = "mflo";
7380       goto do_divu3;
7381     case M_REMU_3:
7382       s = "divu";
7383       s2 = "mfhi";
7384       goto do_divu3;
7385     case M_DDIVU_3:
7386       s = "ddivu";
7387       s2 = "mflo";
7388       goto do_divu3;
7389     case M_DREMU_3:
7390       s = "ddivu";
7391       s2 = "mfhi";
7392     do_divu3:
7393       mips_emit_delays (true);
7394       ++mips_opts.noreorder;
7395       mips_any_noreorder = 1;
7396       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7397                    xreg, yreg);
7398       expr1.X_add_number = 2;
7399       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7400       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7401                    "6", 7);
7402       --mips_opts.noreorder;
7403       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7404       break;
7405
7406     case M_DMUL:
7407       dbl = 1;
7408     case M_MUL:
7409       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7410                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7411       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7412                    zreg);
7413       return;
7414
7415     case M_DSUBU_I:
7416       dbl = 1;
7417       goto do_subu;
7418     case M_SUBU_I:
7419     do_subu:
7420       if (imm_expr.X_op != O_constant)
7421         as_bad (_("Unsupported large constant"));
7422       imm_expr.X_add_number = -imm_expr.X_add_number;
7423       macro_build ((char *) NULL, &icnt, &imm_expr,
7424                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7425       break;
7426
7427     case M_SUBU_I_2:
7428       if (imm_expr.X_op != O_constant)
7429         as_bad (_("Unsupported large constant"));
7430       imm_expr.X_add_number = -imm_expr.X_add_number;
7431       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7432                    "x,k", xreg);
7433       break;
7434
7435     case M_DSUBU_I_2:
7436       if (imm_expr.X_op != O_constant)
7437         as_bad (_("Unsupported large constant"));
7438       imm_expr.X_add_number = -imm_expr.X_add_number;
7439       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7440                    "y,j", yreg);
7441       break;
7442
7443     case M_BEQ:
7444       s = "cmp";
7445       s2 = "bteqz";
7446       goto do_branch;
7447     case M_BNE:
7448       s = "cmp";
7449       s2 = "btnez";
7450       goto do_branch;
7451     case M_BLT:
7452       s = "slt";
7453       s2 = "btnez";
7454       goto do_branch;
7455     case M_BLTU:
7456       s = "sltu";
7457       s2 = "btnez";
7458       goto do_branch;
7459     case M_BLE:
7460       s = "slt";
7461       s2 = "bteqz";
7462       goto do_reverse_branch;
7463     case M_BLEU:
7464       s = "sltu";
7465       s2 = "bteqz";
7466       goto do_reverse_branch;
7467     case M_BGE:
7468       s = "slt";
7469       s2 = "bteqz";
7470       goto do_branch;
7471     case M_BGEU:
7472       s = "sltu";
7473       s2 = "bteqz";
7474       goto do_branch;
7475     case M_BGT:
7476       s = "slt";
7477       s2 = "btnez";
7478       goto do_reverse_branch;
7479     case M_BGTU:
7480       s = "sltu";
7481       s2 = "btnez";
7482
7483     do_reverse_branch:
7484       tmp = xreg;
7485       xreg = yreg;
7486       yreg = tmp;
7487
7488     do_branch:
7489       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7490                    xreg, yreg);
7491       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7492       break;
7493
7494     case M_BEQ_I:
7495       s = "cmpi";
7496       s2 = "bteqz";
7497       s3 = "x,U";
7498       goto do_branch_i;
7499     case M_BNE_I:
7500       s = "cmpi";
7501       s2 = "btnez";
7502       s3 = "x,U";
7503       goto do_branch_i;
7504     case M_BLT_I:
7505       s = "slti";
7506       s2 = "btnez";
7507       s3 = "x,8";
7508       goto do_branch_i;
7509     case M_BLTU_I:
7510       s = "sltiu";
7511       s2 = "btnez";
7512       s3 = "x,8";
7513       goto do_branch_i;
7514     case M_BLE_I:
7515       s = "slti";
7516       s2 = "btnez";
7517       s3 = "x,8";
7518       goto do_addone_branch_i;
7519     case M_BLEU_I:
7520       s = "sltiu";
7521       s2 = "btnez";
7522       s3 = "x,8";
7523       goto do_addone_branch_i;
7524     case M_BGE_I:
7525       s = "slti";
7526       s2 = "bteqz";
7527       s3 = "x,8";
7528       goto do_branch_i;
7529     case M_BGEU_I:
7530       s = "sltiu";
7531       s2 = "bteqz";
7532       s3 = "x,8";
7533       goto do_branch_i;
7534     case M_BGT_I:
7535       s = "slti";
7536       s2 = "bteqz";
7537       s3 = "x,8";
7538       goto do_addone_branch_i;
7539     case M_BGTU_I:
7540       s = "sltiu";
7541       s2 = "bteqz";
7542       s3 = "x,8";
7543
7544     do_addone_branch_i:
7545       if (imm_expr.X_op != O_constant)
7546         as_bad (_("Unsupported large constant"));
7547       ++imm_expr.X_add_number;
7548
7549     do_branch_i:
7550       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7551       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7552       break;
7553
7554     case M_ABS:
7555       expr1.X_add_number = 0;
7556       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7557       if (xreg != yreg)
7558         move_register (&icnt, xreg, yreg);
7559       expr1.X_add_number = 2;
7560       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7561       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7562                    "neg", "x,w", xreg, xreg);
7563     }
7564 }
7565
7566 /* For consistency checking, verify that all bits are specified either
7567    by the match/mask part of the instruction definition, or by the
7568    operand list.  */
7569 static int
7570 validate_mips_insn (opc)
7571      const struct mips_opcode *opc;
7572 {
7573   const char *p = opc->args;
7574   char c;
7575   unsigned long used_bits = opc->mask;
7576
7577   if ((used_bits & opc->match) != opc->match)
7578     {
7579       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7580               opc->name, opc->args);
7581       return 0;
7582     }
7583 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7584   while (*p)
7585     switch (c = *p++)
7586       {
7587       case ',': break;
7588       case '(': break;
7589       case ')': break;
7590       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7591       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7592       case 'A': break;
7593       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7594       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7595       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7596       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7597       case 'F': break;
7598       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7599       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7600       case 'I': break;
7601       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7602       case 'L': break;
7603       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7604       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7605       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7606       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7607                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7608       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7609       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7610       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7611       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7612       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7613       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7614       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7615       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7616       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7617       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7618       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7619       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7620       case 'f': break;
7621       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7622       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7623       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7624       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7625       case 'l': break;
7626       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7627       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7628       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7629       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7630       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7631       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7632       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7633       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7634       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7635       case 'x': break;
7636       case 'z': break;
7637       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7638       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7639                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7640       default:
7641         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7642                 c, opc->name, opc->args);
7643         return 0;
7644       }
7645 #undef USE_BITS
7646   if (used_bits != 0xffffffff)
7647     {
7648       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7649               ~used_bits & 0xffffffff, opc->name, opc->args);
7650       return 0;
7651     }
7652   return 1;
7653 }
7654
7655 /* This routine assembles an instruction into its binary format.  As a
7656    side effect, it sets one of the global variables imm_reloc or
7657    offset_reloc to the type of relocation to do if one of the operands
7658    is an address expression.  */
7659
7660 static void
7661 mips_ip (str, ip)
7662      char *str;
7663      struct mips_cl_insn *ip;
7664 {
7665   char *s;
7666   const char *args;
7667   char c = 0;
7668   struct mips_opcode *insn;
7669   char *argsStart;
7670   unsigned int regno;
7671   unsigned int lastregno = 0;
7672   char *s_reset;
7673   char save_c = 0;
7674
7675   insn_error = NULL;
7676
7677   /* If the instruction contains a '.', we first try to match an instruction
7678      including the '.'.  Then we try again without the '.'.  */
7679   insn = NULL;
7680   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7681     continue;
7682
7683   /* If we stopped on whitespace, then replace the whitespace with null for
7684      the call to hash_find.  Save the character we replaced just in case we
7685      have to re-parse the instruction.  */
7686   if (ISSPACE (*s))
7687     {
7688       save_c = *s;
7689       *s++ = '\0';
7690     }
7691
7692   insn = (struct mips_opcode *) hash_find (op_hash, str);
7693
7694   /* If we didn't find the instruction in the opcode table, try again, but
7695      this time with just the instruction up to, but not including the
7696      first '.'.  */
7697   if (insn == NULL)
7698     {
7699       /* Restore the character we overwrite above (if any).  */
7700       if (save_c)
7701         *(--s) = save_c;
7702
7703       /* Scan up to the first '.' or whitespace.  */
7704       for (s = str;
7705            *s != '\0' && *s != '.' && !ISSPACE (*s);
7706            ++s)
7707         continue;
7708
7709       /* If we did not find a '.', then we can quit now.  */
7710       if (*s != '.')
7711         {
7712           insn_error = "unrecognized opcode";
7713           return;
7714         }
7715
7716       /* Lookup the instruction in the hash table.  */
7717       *s++ = '\0';
7718       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7719         {
7720           insn_error = "unrecognized opcode";
7721           return;
7722         }
7723     }
7724
7725   argsStart = s;
7726   for (;;)
7727     {
7728       boolean ok;
7729
7730       assert (strcmp (insn->name, str) == 0);
7731
7732       if (OPCODE_IS_MEMBER (insn,
7733                             (mips_opts.isa
7734                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7735                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7736                             mips_arch))
7737         ok = true;
7738       else
7739         ok = false;
7740
7741       if (insn->pinfo != INSN_MACRO)
7742         {
7743           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7744             ok = false;
7745         }
7746
7747       if (! ok)
7748         {
7749           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7750               && strcmp (insn->name, insn[1].name) == 0)
7751             {
7752               ++insn;
7753               continue;
7754             }
7755           else
7756             {
7757               if (!insn_error)
7758                 {
7759                   static char buf[100];
7760                   sprintf (buf,
7761                            _("opcode not supported on this processor: %s (%s)"),
7762                            mips_cpu_to_str (mips_arch),
7763                            mips_isa_to_str (mips_opts.isa));
7764
7765                   insn_error = buf;
7766                 }
7767               if (save_c)
7768                 *(--s) = save_c;
7769               return;
7770             }
7771         }
7772
7773       ip->insn_mo = insn;
7774       ip->insn_opcode = insn->match;
7775       insn_error = NULL;
7776       for (args = insn->args;; ++args)
7777         {
7778           int is_mdmx;
7779
7780           s += strspn (s, " \t");
7781           is_mdmx = 0;
7782           switch (*args)
7783             {
7784             case '\0':          /* end of args */
7785               if (*s == '\0')
7786                 return;
7787               break;
7788
7789             case ',':
7790               if (*s++ == *args)
7791                 continue;
7792               s--;
7793               switch (*++args)
7794                 {
7795                 case 'r':
7796                 case 'v':
7797                   ip->insn_opcode |= lastregno << OP_SH_RS;
7798                   continue;
7799
7800                 case 'w':
7801                   ip->insn_opcode |= lastregno << OP_SH_RT;
7802                   continue;
7803
7804                 case 'W':
7805                   ip->insn_opcode |= lastregno << OP_SH_FT;
7806                   continue;
7807
7808                 case 'V':
7809                   ip->insn_opcode |= lastregno << OP_SH_FS;
7810                   continue;
7811                 }
7812               break;
7813
7814             case '(':
7815               /* Handle optional base register.
7816                  Either the base register is omitted or
7817                  we must have a left paren.  */
7818               /* This is dependent on the next operand specifier
7819                  is a base register specification.  */
7820               assert (args[1] == 'b' || args[1] == '5'
7821                       || args[1] == '-' || args[1] == '4');
7822               if (*s == '\0')
7823                 return;
7824
7825             case ')':           /* these must match exactly */
7826               if (*s++ == *args)
7827                 continue;
7828               break;
7829
7830             case '<':           /* must be at least one digit */
7831               /*
7832                * According to the manual, if the shift amount is greater
7833                * than 31 or less than 0, then the shift amount should be
7834                * mod 32.  In reality the mips assembler issues an error.
7835                * We issue a warning and mask out all but the low 5 bits.
7836                */
7837               my_getExpression (&imm_expr, s);
7838               check_absolute_expr (ip, &imm_expr);
7839               if ((unsigned long) imm_expr.X_add_number > 31)
7840                 {
7841                   as_warn (_("Improper shift amount (%lu)"),
7842                            (unsigned long) imm_expr.X_add_number);
7843                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7844                 }
7845               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7846               imm_expr.X_op = O_absent;
7847               s = expr_end;
7848               continue;
7849
7850             case '>':           /* shift amount minus 32 */
7851               my_getExpression (&imm_expr, s);
7852               check_absolute_expr (ip, &imm_expr);
7853               if ((unsigned long) imm_expr.X_add_number < 32
7854                   || (unsigned long) imm_expr.X_add_number > 63)
7855                 break;
7856               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7857               imm_expr.X_op = O_absent;
7858               s = expr_end;
7859               continue;
7860
7861             case 'k':           /* cache code */
7862             case 'h':           /* prefx code */
7863               my_getExpression (&imm_expr, s);
7864               check_absolute_expr (ip, &imm_expr);
7865               if ((unsigned long) imm_expr.X_add_number > 31)
7866                 {
7867                   as_warn (_("Invalid value for `%s' (%lu)"),
7868                            ip->insn_mo->name,
7869                            (unsigned long) imm_expr.X_add_number);
7870                   imm_expr.X_add_number &= 0x1f;
7871                 }
7872               if (*args == 'k')
7873                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7874               else
7875                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7876               imm_expr.X_op = O_absent;
7877               s = expr_end;
7878               continue;
7879
7880             case 'c':           /* break code */
7881               my_getExpression (&imm_expr, s);
7882               check_absolute_expr (ip, &imm_expr);
7883               if ((unsigned long) imm_expr.X_add_number > 1023)
7884                 {
7885                   as_warn (_("Illegal break code (%lu)"),
7886                            (unsigned long) imm_expr.X_add_number);
7887                   imm_expr.X_add_number &= OP_MASK_CODE;
7888                 }
7889               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7890               imm_expr.X_op = O_absent;
7891               s = expr_end;
7892               continue;
7893
7894             case 'q':           /* lower break code */
7895               my_getExpression (&imm_expr, s);
7896               check_absolute_expr (ip, &imm_expr);
7897               if ((unsigned long) imm_expr.X_add_number > 1023)
7898                 {
7899                   as_warn (_("Illegal lower break code (%lu)"),
7900                            (unsigned long) imm_expr.X_add_number);
7901                   imm_expr.X_add_number &= OP_MASK_CODE2;
7902                 }
7903               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7904               imm_expr.X_op = O_absent;
7905               s = expr_end;
7906               continue;
7907
7908             case 'B':           /* 20-bit syscall/break code.  */
7909               my_getExpression (&imm_expr, s);
7910               check_absolute_expr (ip, &imm_expr);
7911               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7912                 as_warn (_("Illegal 20-bit code (%lu)"),
7913                          (unsigned long) imm_expr.X_add_number);
7914               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7915               imm_expr.X_op = O_absent;
7916               s = expr_end;
7917               continue;
7918
7919             case 'C':           /* Coprocessor code */
7920               my_getExpression (&imm_expr, s);
7921               check_absolute_expr (ip, &imm_expr);
7922               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
7923                 {
7924                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
7925                            (unsigned long) imm_expr.X_add_number);
7926                   imm_expr.X_add_number &= ((1 << 25) - 1);
7927                 }
7928               ip->insn_opcode |= imm_expr.X_add_number;
7929               imm_expr.X_op = O_absent;
7930               s = expr_end;
7931               continue;
7932
7933             case 'J':           /* 19-bit wait code.  */
7934               my_getExpression (&imm_expr, s);
7935               check_absolute_expr (ip, &imm_expr);
7936               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7937                 as_warn (_("Illegal 19-bit code (%lu)"),
7938                          (unsigned long) imm_expr.X_add_number);
7939               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7940               imm_expr.X_op = O_absent;
7941               s = expr_end;
7942               continue;
7943
7944             case 'P':           /* Performance register */
7945               my_getExpression (&imm_expr, s);
7946               check_absolute_expr (ip, &imm_expr);
7947               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7948                 {
7949                   as_warn (_("Invalid performance register (%lu)"),
7950                            (unsigned long) imm_expr.X_add_number);
7951                   imm_expr.X_add_number &= OP_MASK_PERFREG;
7952                 }
7953               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7954               imm_expr.X_op = O_absent;
7955               s = expr_end;
7956               continue;
7957
7958             case 'b':           /* base register */
7959             case 'd':           /* destination register */
7960             case 's':           /* source register */
7961             case 't':           /* target register */
7962             case 'r':           /* both target and source */
7963             case 'v':           /* both dest and source */
7964             case 'w':           /* both dest and target */
7965             case 'E':           /* coprocessor target register */
7966             case 'G':           /* coprocessor destination register */
7967             case 'x':           /* ignore register name */
7968             case 'z':           /* must be zero register */
7969             case 'U':           /* destination register (clo/clz).  */
7970               s_reset = s;
7971               if (s[0] == '$')
7972                 {
7973
7974                   if (ISDIGIT (s[1]))
7975                     {
7976                       ++s;
7977                       regno = 0;
7978                       do
7979                         {
7980                           regno *= 10;
7981                           regno += *s - '0';
7982                           ++s;
7983                         }
7984                       while (ISDIGIT (*s));
7985                       if (regno > 31)
7986                         as_bad (_("Invalid register number (%d)"), regno);
7987                     }
7988                   else if (*args == 'E' || *args == 'G')
7989                     goto notreg;
7990                   else
7991                     {
7992                       if (s[1] == 'r' && s[2] == 'a')
7993                         {
7994                           s += 3;
7995                           regno = RA;
7996                         }
7997                       else if (s[1] == 'f' && s[2] == 'p')
7998                         {
7999                           s += 3;
8000                           regno = FP;
8001                         }
8002                       else if (s[1] == 's' && s[2] == 'p')
8003                         {
8004                           s += 3;
8005                           regno = SP;
8006                         }
8007                       else if (s[1] == 'g' && s[2] == 'p')
8008                         {
8009                           s += 3;
8010                           regno = GP;
8011                         }
8012                       else if (s[1] == 'a' && s[2] == 't')
8013                         {
8014                           s += 3;
8015                           regno = AT;
8016                         }
8017                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8018                         {
8019                           s += 4;
8020                           regno = KT0;
8021                         }
8022                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8023                         {
8024                           s += 4;
8025                           regno = KT1;
8026                         }
8027                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8028                         {
8029                           s += 5;
8030                           regno = ZERO;
8031                         }
8032                       else if (itbl_have_entries)
8033                         {
8034                           char *p, *n;
8035                           unsigned long r;
8036
8037                           p = s + 1;    /* advance past '$' */
8038                           n = itbl_get_field (&p);  /* n is name */
8039
8040                           /* See if this is a register defined in an
8041                              itbl entry.  */
8042                           if (itbl_get_reg_val (n, &r))
8043                             {
8044                               /* Get_field advances to the start of
8045                                  the next field, so we need to back
8046                                  rack to the end of the last field.  */
8047                               if (p)
8048                                 s = p - 1;
8049                               else
8050                                 s = strchr (s, '\0');
8051                               regno = r;
8052                             }
8053                           else
8054                             goto notreg;
8055                         }
8056                       else
8057                         goto notreg;
8058                     }
8059                   if (regno == AT
8060                       && ! mips_opts.noat
8061                       && *args != 'E'
8062                       && *args != 'G')
8063                     as_warn (_("Used $at without \".set noat\""));
8064                   c = *args;
8065                   if (*s == ' ')
8066                     ++s;
8067                   if (args[1] != *s)
8068                     {
8069                       if (c == 'r' || c == 'v' || c == 'w')
8070                         {
8071                           regno = lastregno;
8072                           s = s_reset;
8073                           ++args;
8074                         }
8075                     }
8076                   /* 'z' only matches $0.  */
8077                   if (c == 'z' && regno != 0)
8078                     break;
8079
8080         /* Now that we have assembled one operand, we use the args string
8081          * to figure out where it goes in the instruction.  */
8082                   switch (c)
8083                     {
8084                     case 'r':
8085                     case 's':
8086                     case 'v':
8087                     case 'b':
8088                       ip->insn_opcode |= regno << OP_SH_RS;
8089                       break;
8090                     case 'd':
8091                     case 'G':
8092                       ip->insn_opcode |= regno << OP_SH_RD;
8093                       break;
8094                     case 'U':
8095                       ip->insn_opcode |= regno << OP_SH_RD;
8096                       ip->insn_opcode |= regno << OP_SH_RT;
8097                       break;
8098                     case 'w':
8099                     case 't':
8100                     case 'E':
8101                       ip->insn_opcode |= regno << OP_SH_RT;
8102                       break;
8103                     case 'x':
8104                       /* This case exists because on the r3000 trunc
8105                          expands into a macro which requires a gp
8106                          register.  On the r6000 or r4000 it is
8107                          assembled into a single instruction which
8108                          ignores the register.  Thus the insn version
8109                          is MIPS_ISA2 and uses 'x', and the macro
8110                          version is MIPS_ISA1 and uses 't'.  */
8111                       break;
8112                     case 'z':
8113                       /* This case is for the div instruction, which
8114                          acts differently if the destination argument
8115                          is $0.  This only matches $0, and is checked
8116                          outside the switch.  */
8117                       break;
8118                     case 'D':
8119                       /* Itbl operand; not yet implemented. FIXME ?? */
8120                       break;
8121                       /* What about all other operands like 'i', which
8122                          can be specified in the opcode table? */
8123                     }
8124                   lastregno = regno;
8125                   continue;
8126                 }
8127             notreg:
8128               switch (*args++)
8129                 {
8130                 case 'r':
8131                 case 'v':
8132                   ip->insn_opcode |= lastregno << OP_SH_RS;
8133                   continue;
8134                 case 'w':
8135                   ip->insn_opcode |= lastregno << OP_SH_RT;
8136                   continue;
8137                 }
8138               break;
8139
8140             case 'O':           /* MDMX alignment immediate constant.  */
8141               my_getExpression (&imm_expr, s);
8142               check_absolute_expr (ip, &imm_expr);
8143               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8144                 {
8145                   as_warn ("Improper align amount (%ld), using low bits",
8146                            (long) imm_expr.X_add_number);
8147                   imm_expr.X_add_number &= OP_MASK_ALN;
8148                 }
8149               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8150               imm_expr.X_op = O_absent;
8151               s = expr_end;
8152               continue;
8153
8154             case 'Q':           /* MDMX vector, element sel, or const.  */
8155               if (s[0] != '$')
8156                 {
8157                   /* MDMX Immediate.  */
8158                   my_getExpression (&imm_expr, s);
8159                   check_absolute_expr (ip, &imm_expr);
8160                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8161                     {
8162                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8163                                (long) imm_expr.X_add_number);
8164                       imm_expr.X_add_number &= OP_MASK_FT;
8165                     }
8166                   imm_expr.X_add_number &= OP_MASK_FT;
8167                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8168                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8169                   else
8170                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8171                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8172                   imm_expr.X_op = O_absent;
8173                   s = expr_end;
8174                   continue;
8175                 }
8176               /* Not MDMX Immediate.  Fall through.  */
8177             case 'X':           /* MDMX destination register.  */
8178             case 'Y':           /* MDMX source register.  */
8179             case 'Z':           /* MDMX target register.  */
8180               is_mdmx = 1;
8181             case 'D':           /* floating point destination register */
8182             case 'S':           /* floating point source register */
8183             case 'T':           /* floating point target register */
8184             case 'R':           /* floating point source register */
8185             case 'V':
8186             case 'W':
8187               s_reset = s;
8188               /* Accept $fN for FP and MDMX register numbers, and in
8189                  addition accept $vN for MDMX register numbers.  */
8190               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8191                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8192                       && ISDIGIT (s[2])))
8193                 {
8194                   s += 2;
8195                   regno = 0;
8196                   do
8197                     {
8198                       regno *= 10;
8199                       regno += *s - '0';
8200                       ++s;
8201                     }
8202                   while (ISDIGIT (*s));
8203
8204                   if (regno > 31)
8205                     as_bad (_("Invalid float register number (%d)"), regno);
8206
8207                   if ((regno & 1) != 0
8208                       && HAVE_32BIT_FPRS
8209                       && ! (strcmp (str, "mtc1") == 0
8210                             || strcmp (str, "mfc1") == 0
8211                             || strcmp (str, "lwc1") == 0
8212                             || strcmp (str, "swc1") == 0
8213                             || strcmp (str, "l.s") == 0
8214                             || strcmp (str, "s.s") == 0))
8215                     as_warn (_("Float register should be even, was %d"),
8216                              regno);
8217
8218                   c = *args;
8219                   if (*s == ' ')
8220                     ++s;
8221                   if (args[1] != *s)
8222                     {
8223                       if (c == 'V' || c == 'W')
8224                         {
8225                           regno = lastregno;
8226                           s = s_reset;
8227                           ++args;
8228                         }
8229                     }
8230                   switch (c)
8231                     {
8232                     case 'D':
8233                     case 'X':
8234                       ip->insn_opcode |= regno << OP_SH_FD;
8235                       break;
8236                     case 'V':
8237                     case 'S':
8238                     case 'Y':
8239                       ip->insn_opcode |= regno << OP_SH_FS;
8240                       break;
8241                     case 'Q':
8242                       /* This is like 'Z', but also needs to fix the MDMX
8243                          vector/scalar select bits.  Note that the
8244                          scalar immediate case is handled above.  */
8245                       if (*s == '[')
8246                         {
8247                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8248                           int max_el = (is_qh ? 3 : 7);
8249                           s++;
8250                           my_getExpression(&imm_expr, s);
8251                           check_absolute_expr (ip, &imm_expr);
8252                           s = expr_end;
8253                           if (imm_expr.X_add_number > max_el)
8254                             as_bad(_("Bad element selector %ld"),
8255                                    (long) imm_expr.X_add_number);
8256                           imm_expr.X_add_number &= max_el;
8257                           ip->insn_opcode |= (imm_expr.X_add_number
8258                                               << (OP_SH_VSEL +
8259                                                   (is_qh ? 2 : 1)));
8260                           if (*s != ']')
8261                             as_warn(_("Expecting ']' found '%s'"), s);
8262                           else
8263                             s++;
8264                         }
8265                       else
8266                         {
8267                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8268                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8269                                                 << OP_SH_VSEL);
8270                           else
8271                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8272                                                 OP_SH_VSEL);
8273                         }
8274                       /* Fall through */
8275                     case 'W':
8276                     case 'T':
8277                     case 'Z':
8278                       ip->insn_opcode |= regno << OP_SH_FT;
8279                       break;
8280                     case 'R':
8281                       ip->insn_opcode |= regno << OP_SH_FR;
8282                       break;
8283                     }
8284                   lastregno = regno;
8285                   continue;
8286                 }
8287
8288               switch (*args++)
8289                 {
8290                 case 'V':
8291                   ip->insn_opcode |= lastregno << OP_SH_FS;
8292                   continue;
8293                 case 'W':
8294                   ip->insn_opcode |= lastregno << OP_SH_FT;
8295                   continue;
8296                 }
8297               break;
8298
8299             case 'I':
8300               my_getExpression (&imm_expr, s);
8301               if (imm_expr.X_op != O_big
8302                   && imm_expr.X_op != O_constant)
8303                 insn_error = _("absolute expression required");
8304               s = expr_end;
8305               continue;
8306
8307             case 'A':
8308               my_getExpression (&offset_expr, s);
8309               *imm_reloc = BFD_RELOC_32;
8310               s = expr_end;
8311               continue;
8312
8313             case 'F':
8314             case 'L':
8315             case 'f':
8316             case 'l':
8317               {
8318                 int f64;
8319                 int using_gprs;
8320                 char *save_in;
8321                 char *err;
8322                 unsigned char temp[8];
8323                 int len;
8324                 unsigned int length;
8325                 segT seg;
8326                 subsegT subseg;
8327                 char *p;
8328
8329                 /* These only appear as the last operand in an
8330                    instruction, and every instruction that accepts
8331                    them in any variant accepts them in all variants.
8332                    This means we don't have to worry about backing out
8333                    any changes if the instruction does not match.
8334
8335                    The difference between them is the size of the
8336                    floating point constant and where it goes.  For 'F'
8337                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8338                    is 32 bits.  Where the constant is placed is based
8339                    on how the MIPS assembler does things:
8340                     F -- .rdata
8341                     L -- .lit8
8342                     f -- immediate value
8343                     l -- .lit4
8344
8345                     The .lit4 and .lit8 sections are only used if
8346                     permitted by the -G argument.
8347
8348                     When generating embedded PIC code, we use the
8349                     .lit8 section but not the .lit4 section (we can do
8350                     .lit4 inline easily; we need to put .lit8
8351                     somewhere in the data segment, and using .lit8
8352                     permits the linker to eventually combine identical
8353                     .lit8 entries).
8354
8355                     The code below needs to know whether the target register
8356                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8357                     'F' are used with GPR-based instructions and 'l' and
8358                     'L' are used with FPR-based instructions.  */
8359
8360                 f64 = *args == 'F' || *args == 'L';
8361                 using_gprs = *args == 'F' || *args == 'f';
8362
8363                 save_in = input_line_pointer;
8364                 input_line_pointer = s;
8365                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8366                 length = len;
8367                 s = input_line_pointer;
8368                 input_line_pointer = save_in;
8369                 if (err != NULL && *err != '\0')
8370                   {
8371                     as_bad (_("Bad floating point constant: %s"), err);
8372                     memset (temp, '\0', sizeof temp);
8373                     length = f64 ? 8 : 4;
8374                   }
8375
8376                 assert (length == (unsigned) (f64 ? 8 : 4));
8377
8378                 if (*args == 'f'
8379                     || (*args == 'l'
8380                         && (! USE_GLOBAL_POINTER_OPT
8381                             || mips_pic == EMBEDDED_PIC
8382                             || g_switch_value < 4
8383                             || (temp[0] == 0 && temp[1] == 0)
8384                             || (temp[2] == 0 && temp[3] == 0))))
8385                   {
8386                     imm_expr.X_op = O_constant;
8387                     if (! target_big_endian)
8388                       imm_expr.X_add_number = bfd_getl32 (temp);
8389                     else
8390                       imm_expr.X_add_number = bfd_getb32 (temp);
8391                   }
8392                 else if (length > 4
8393                          && ! mips_disable_float_construction
8394                          /* Constants can only be constructed in GPRs and
8395                             copied to FPRs if the GPRs are at least as wide
8396                             as the FPRs.  Force the constant into memory if
8397                             we are using 64-bit FPRs but the GPRs are only
8398                             32 bits wide.  */
8399                          && (using_gprs
8400                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8401                          && ((temp[0] == 0 && temp[1] == 0)
8402                              || (temp[2] == 0 && temp[3] == 0))
8403                          && ((temp[4] == 0 && temp[5] == 0)
8404                              || (temp[6] == 0 && temp[7] == 0)))
8405                   {
8406                     /* The value is simple enough to load with a couple of
8407                        instructions.  If using 32-bit registers, set
8408                        imm_expr to the high order 32 bits and offset_expr to
8409                        the low order 32 bits.  Otherwise, set imm_expr to
8410                        the entire 64 bit constant.  */
8411                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8412                       {
8413                         imm_expr.X_op = O_constant;
8414                         offset_expr.X_op = O_constant;
8415                         if (! target_big_endian)
8416                           {
8417                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8418                             offset_expr.X_add_number = bfd_getl32 (temp);
8419                           }
8420                         else
8421                           {
8422                             imm_expr.X_add_number = bfd_getb32 (temp);
8423                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8424                           }
8425                         if (offset_expr.X_add_number == 0)
8426                           offset_expr.X_op = O_absent;
8427                       }
8428                     else if (sizeof (imm_expr.X_add_number) > 4)
8429                       {
8430                         imm_expr.X_op = O_constant;
8431                         if (! target_big_endian)
8432                           imm_expr.X_add_number = bfd_getl64 (temp);
8433                         else
8434                           imm_expr.X_add_number = bfd_getb64 (temp);
8435                       }
8436                     else
8437                       {
8438                         imm_expr.X_op = O_big;
8439                         imm_expr.X_add_number = 4;
8440                         if (! target_big_endian)
8441                           {
8442                             generic_bignum[0] = bfd_getl16 (temp);
8443                             generic_bignum[1] = bfd_getl16 (temp + 2);
8444                             generic_bignum[2] = bfd_getl16 (temp + 4);
8445                             generic_bignum[3] = bfd_getl16 (temp + 6);
8446                           }
8447                         else
8448                           {
8449                             generic_bignum[0] = bfd_getb16 (temp + 6);
8450                             generic_bignum[1] = bfd_getb16 (temp + 4);
8451                             generic_bignum[2] = bfd_getb16 (temp + 2);
8452                             generic_bignum[3] = bfd_getb16 (temp);
8453                           }
8454                       }
8455                   }
8456                 else
8457                   {
8458                     const char *newname;
8459                     segT new_seg;
8460
8461                     /* Switch to the right section.  */
8462                     seg = now_seg;
8463                     subseg = now_subseg;
8464                     switch (*args)
8465                       {
8466                       default: /* unused default case avoids warnings.  */
8467                       case 'L':
8468                         newname = RDATA_SECTION_NAME;
8469                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8470                             || mips_pic == EMBEDDED_PIC)
8471                           newname = ".lit8";
8472                         break;
8473                       case 'F':
8474                         if (mips_pic == EMBEDDED_PIC)
8475                           newname = ".lit8";
8476                         else
8477                           newname = RDATA_SECTION_NAME;
8478                         break;
8479                       case 'l':
8480                         assert (!USE_GLOBAL_POINTER_OPT
8481                                 || g_switch_value >= 4);
8482                         newname = ".lit4";
8483                         break;
8484                       }
8485                     new_seg = subseg_new (newname, (subsegT) 0);
8486                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8487                       bfd_set_section_flags (stdoutput, new_seg,
8488                                              (SEC_ALLOC
8489                                               | SEC_LOAD
8490                                               | SEC_READONLY
8491                                               | SEC_DATA));
8492                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8493                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8494                         && strcmp (TARGET_OS, "elf") != 0)
8495                       record_alignment (new_seg, 4);
8496                     else
8497                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8498                     if (seg == now_seg)
8499                       as_bad (_("Can't use floating point insn in this section"));
8500
8501                     /* Set the argument to the current address in the
8502                        section.  */
8503                     offset_expr.X_op = O_symbol;
8504                     offset_expr.X_add_symbol =
8505                       symbol_new ("L0\001", now_seg,
8506                                   (valueT) frag_now_fix (), frag_now);
8507                     offset_expr.X_add_number = 0;
8508
8509                     /* Put the floating point number into the section.  */
8510                     p = frag_more ((int) length);
8511                     memcpy (p, temp, length);
8512
8513                     /* Switch back to the original section.  */
8514                     subseg_set (seg, subseg);
8515                   }
8516               }
8517               continue;
8518
8519             case 'i':           /* 16 bit unsigned immediate */
8520             case 'j':           /* 16 bit signed immediate */
8521               *imm_reloc = BFD_RELOC_LO16;
8522               c = my_getSmallExpression (&imm_expr, s);
8523               if (c != S_EX_NONE)
8524                 {
8525                   if (c != S_EX_LO)
8526                     {
8527                       if (imm_expr.X_op == O_constant)
8528                         imm_expr.X_add_number =
8529                           (imm_expr.X_add_number >> 16) & 0xffff;
8530 #ifdef OBJ_ELF
8531                       else if (c == S_EX_HIGHEST)
8532                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8533                       else if (c == S_EX_HIGHER)
8534                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8535                       else if (c == S_EX_GP_REL)
8536                         {
8537                           /* This occurs in NewABI only.  */
8538                           c = my_getSmallExpression (&imm_expr, s);
8539                           if (c != S_EX_NEG)
8540                             as_bad (_("bad composition of relocations"));
8541                           else
8542                             {
8543                               c = my_getSmallExpression (&imm_expr, s);
8544                               if (c != S_EX_LO)
8545                                 as_bad (_("bad composition of relocations"));
8546                               else
8547                                 {
8548                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8549                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8550                                   imm_reloc[2] = BFD_RELOC_LO16;
8551                                 }
8552                             }
8553                         }
8554 #endif
8555                       else if (c == S_EX_HI)
8556                         {
8557                           *imm_reloc = BFD_RELOC_HI16_S;
8558                           imm_unmatched_hi = true;
8559                         }
8560                       else
8561                         *imm_reloc = BFD_RELOC_HI16;
8562                     }
8563                   else if (imm_expr.X_op == O_constant)
8564                     imm_expr.X_add_number &= 0xffff;
8565                 }
8566               if (*args == 'i')
8567                 {
8568                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8569                       || ((imm_expr.X_add_number < 0
8570                            || imm_expr.X_add_number >= 0x10000)
8571                           && imm_expr.X_op == O_constant))
8572                     {
8573                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8574                           !strcmp (insn->name, insn[1].name))
8575                         break;
8576                       if (imm_expr.X_op == O_constant
8577                           || imm_expr.X_op == O_big)
8578                         as_bad (_("16 bit expression not in range 0..65535"));
8579                     }
8580                 }
8581               else
8582                 {
8583                   int more;
8584                   offsetT max;
8585
8586                   /* The upper bound should be 0x8000, but
8587                      unfortunately the MIPS assembler accepts numbers
8588                      from 0x8000 to 0xffff and sign extends them, and
8589                      we want to be compatible.  We only permit this
8590                      extended range for an instruction which does not
8591                      provide any further alternates, since those
8592                      alternates may handle other cases.  People should
8593                      use the numbers they mean, rather than relying on
8594                      a mysterious sign extension.  */
8595                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8596                           strcmp (insn->name, insn[1].name) == 0);
8597                   if (more)
8598                     max = 0x8000;
8599                   else
8600                     max = 0x10000;
8601                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8602                       || ((imm_expr.X_add_number < -0x8000
8603                            || imm_expr.X_add_number >= max)
8604                           && imm_expr.X_op == O_constant)
8605                       || (more
8606                           && imm_expr.X_add_number < 0
8607                           && HAVE_64BIT_GPRS
8608                           && imm_expr.X_unsigned
8609                           && sizeof (imm_expr.X_add_number) <= 4))
8610                     {
8611                       if (more)
8612                         break;
8613                       if (imm_expr.X_op == O_constant
8614                           || imm_expr.X_op == O_big)
8615                         as_bad (_("16 bit expression not in range -32768..32767"));
8616                     }
8617                 }
8618               s = expr_end;
8619               continue;
8620
8621             case 'o':           /* 16 bit offset */
8622               c = my_getSmallExpression (&offset_expr, s);
8623
8624               /* If this value won't fit into a 16 bit offset, then go
8625                  find a macro that will generate the 32 bit offset
8626                  code pattern.  */
8627               if (c == S_EX_NONE
8628                   && (offset_expr.X_op != O_constant
8629                       || offset_expr.X_add_number >= 0x8000
8630                       || offset_expr.X_add_number < -0x8000))
8631                 break;
8632
8633               if (c == S_EX_HI)
8634                 {
8635                   if (offset_expr.X_op != O_constant)
8636                     break;
8637                   offset_expr.X_add_number =
8638                     (offset_expr.X_add_number >> 16) & 0xffff;
8639                 }
8640               *offset_reloc = BFD_RELOC_LO16;
8641               s = expr_end;
8642               continue;
8643
8644             case 'p':           /* pc relative offset */
8645               if (mips_pic == EMBEDDED_PIC)
8646                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8647               else
8648                 *offset_reloc = BFD_RELOC_16_PCREL;
8649               my_getExpression (&offset_expr, s);
8650               s = expr_end;
8651               continue;
8652
8653             case 'u':           /* upper 16 bits */
8654               c = my_getSmallExpression (&imm_expr, s);
8655               *imm_reloc = BFD_RELOC_LO16;
8656               if (c != S_EX_NONE)
8657                 {
8658                   if (c != S_EX_LO)
8659                     {
8660                       if (imm_expr.X_op == O_constant)
8661                         imm_expr.X_add_number =
8662                           (imm_expr.X_add_number >> 16) & 0xffff;
8663                       else if (c == S_EX_HI)
8664                         {
8665                           *imm_reloc = BFD_RELOC_HI16_S;
8666                           imm_unmatched_hi = true;
8667                         }
8668 #ifdef OBJ_ELF
8669                       else if (c == S_EX_HIGHEST)
8670                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8671                       else if (c == S_EX_GP_REL)
8672                         {
8673                           /* This occurs in NewABI only.  */
8674                           c = my_getSmallExpression (&imm_expr, s);
8675                           if (c != S_EX_NEG)
8676                             as_bad (_("bad composition of relocations"));
8677                           else
8678                             {
8679                               c = my_getSmallExpression (&imm_expr, s);
8680                               if (c != S_EX_HI)
8681                                 as_bad (_("bad composition of relocations"));
8682                               else
8683                                 {
8684                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8685                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8686                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8687                                 }
8688                             }
8689                         }
8690 #endif
8691                       else
8692                         *imm_reloc = BFD_RELOC_HI16;
8693                     }
8694                   else if (imm_expr.X_op == O_constant)
8695                     imm_expr.X_add_number &= 0xffff;
8696                 }
8697               if (imm_expr.X_op == O_constant
8698                   && (imm_expr.X_add_number < 0
8699                       || imm_expr.X_add_number >= 0x10000))
8700                 as_bad (_("lui expression not in range 0..65535"));
8701               s = expr_end;
8702               continue;
8703
8704             case 'a':           /* 26 bit address */
8705               my_getExpression (&offset_expr, s);
8706               s = expr_end;
8707               *offset_reloc = BFD_RELOC_MIPS_JMP;
8708               continue;
8709
8710             case 'N':           /* 3 bit branch condition code */
8711             case 'M':           /* 3 bit compare condition code */
8712               if (strncmp (s, "$fcc", 4) != 0)
8713                 break;
8714               s += 4;
8715               regno = 0;
8716               do
8717                 {
8718                   regno *= 10;
8719                   regno += *s - '0';
8720                   ++s;
8721                 }
8722               while (ISDIGIT (*s));
8723               if (regno > 7)
8724                 as_bad (_("invalid condition code register $fcc%d"), regno);
8725               if (*args == 'N')
8726                 ip->insn_opcode |= regno << OP_SH_BCC;
8727               else
8728                 ip->insn_opcode |= regno << OP_SH_CCC;
8729               continue;
8730
8731             case 'H':
8732               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8733                 s += 2;
8734               if (ISDIGIT (*s))
8735                 {
8736                   c = 0;
8737                   do
8738                     {
8739                       c *= 10;
8740                       c += *s - '0';
8741                       ++s;
8742                     }
8743                   while (ISDIGIT (*s));
8744                 }
8745               else
8746                 c = 8; /* Invalid sel value.  */
8747
8748               if (c > 7)
8749                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8750               ip->insn_opcode |= c;
8751               continue;
8752
8753             default:
8754               as_bad (_("bad char = '%c'\n"), *args);
8755               internalError ();
8756             }
8757           break;
8758         }
8759       /* Args don't match.  */
8760       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8761           !strcmp (insn->name, insn[1].name))
8762         {
8763           ++insn;
8764           s = argsStart;
8765           insn_error = _("illegal operands");
8766           continue;
8767         }
8768       if (save_c)
8769         *(--s) = save_c;
8770       insn_error = _("illegal operands");
8771       return;
8772     }
8773 }
8774
8775 /* This routine assembles an instruction into its binary format when
8776    assembling for the mips16.  As a side effect, it sets one of the
8777    global variables imm_reloc or offset_reloc to the type of
8778    relocation to do if one of the operands is an address expression.
8779    It also sets mips16_small and mips16_ext if the user explicitly
8780    requested a small or extended instruction.  */
8781
8782 static void
8783 mips16_ip (str, ip)
8784      char *str;
8785      struct mips_cl_insn *ip;
8786 {
8787   char *s;
8788   const char *args;
8789   struct mips_opcode *insn;
8790   char *argsstart;
8791   unsigned int regno;
8792   unsigned int lastregno = 0;
8793   char *s_reset;
8794
8795   insn_error = NULL;
8796
8797   mips16_small = false;
8798   mips16_ext = false;
8799
8800   for (s = str; ISLOWER (*s); ++s)
8801     ;
8802   switch (*s)
8803     {
8804     case '\0':
8805       break;
8806
8807     case ' ':
8808       *s++ = '\0';
8809       break;
8810
8811     case '.':
8812       if (s[1] == 't' && s[2] == ' ')
8813         {
8814           *s = '\0';
8815           mips16_small = true;
8816           s += 3;
8817           break;
8818         }
8819       else if (s[1] == 'e' && s[2] == ' ')
8820         {
8821           *s = '\0';
8822           mips16_ext = true;
8823           s += 3;
8824           break;
8825         }
8826       /* Fall through.  */
8827     default:
8828       insn_error = _("unknown opcode");
8829       return;
8830     }
8831
8832   if (mips_opts.noautoextend && ! mips16_ext)
8833     mips16_small = true;
8834
8835   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8836     {
8837       insn_error = _("unrecognized opcode");
8838       return;
8839     }
8840
8841   argsstart = s;
8842   for (;;)
8843     {
8844       assert (strcmp (insn->name, str) == 0);
8845
8846       ip->insn_mo = insn;
8847       ip->insn_opcode = insn->match;
8848       ip->use_extend = false;
8849       imm_expr.X_op = O_absent;
8850       imm_reloc[0] = BFD_RELOC_UNUSED;
8851       imm_reloc[1] = BFD_RELOC_UNUSED;
8852       imm_reloc[2] = BFD_RELOC_UNUSED;
8853       offset_expr.X_op = O_absent;
8854       offset_reloc[0] = BFD_RELOC_UNUSED;
8855       offset_reloc[1] = BFD_RELOC_UNUSED;
8856       offset_reloc[2] = BFD_RELOC_UNUSED;
8857       for (args = insn->args; 1; ++args)
8858         {
8859           int c;
8860
8861           if (*s == ' ')
8862             ++s;
8863
8864           /* In this switch statement we call break if we did not find
8865              a match, continue if we did find a match, or return if we
8866              are done.  */
8867
8868           c = *args;
8869           switch (c)
8870             {
8871             case '\0':
8872               if (*s == '\0')
8873                 {
8874                   /* Stuff the immediate value in now, if we can.  */
8875                   if (imm_expr.X_op == O_constant
8876                       && *imm_reloc > BFD_RELOC_UNUSED
8877                       && insn->pinfo != INSN_MACRO)
8878                     {
8879                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8880                                     imm_expr.X_add_number, true, mips16_small,
8881                                     mips16_ext, &ip->insn_opcode,
8882                                     &ip->use_extend, &ip->extend);
8883                       imm_expr.X_op = O_absent;
8884                       *imm_reloc = BFD_RELOC_UNUSED;
8885                     }
8886
8887                   return;
8888                 }
8889               break;
8890
8891             case ',':
8892               if (*s++ == c)
8893                 continue;
8894               s--;
8895               switch (*++args)
8896                 {
8897                 case 'v':
8898                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8899                   continue;
8900                 case 'w':
8901                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8902                   continue;
8903                 }
8904               break;
8905
8906             case '(':
8907             case ')':
8908               if (*s++ == c)
8909                 continue;
8910               break;
8911
8912             case 'v':
8913             case 'w':
8914               if (s[0] != '$')
8915                 {
8916                   if (c == 'v')
8917                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8918                   else
8919                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8920                   ++args;
8921                   continue;
8922                 }
8923               /* Fall through.  */
8924             case 'x':
8925             case 'y':
8926             case 'z':
8927             case 'Z':
8928             case '0':
8929             case 'S':
8930             case 'R':
8931             case 'X':
8932             case 'Y':
8933               if (s[0] != '$')
8934                 break;
8935               s_reset = s;
8936               if (ISDIGIT (s[1]))
8937                 {
8938                   ++s;
8939                   regno = 0;
8940                   do
8941                     {
8942                       regno *= 10;
8943                       regno += *s - '0';
8944                       ++s;
8945                     }
8946                   while (ISDIGIT (*s));
8947                   if (regno > 31)
8948                     {
8949                       as_bad (_("invalid register number (%d)"), regno);
8950                       regno = 2;
8951                     }
8952                 }
8953               else
8954                 {
8955                   if (s[1] == 'r' && s[2] == 'a')
8956                     {
8957                       s += 3;
8958                       regno = RA;
8959                     }
8960                   else if (s[1] == 'f' && s[2] == 'p')
8961                     {
8962                       s += 3;
8963                       regno = FP;
8964                     }
8965                   else if (s[1] == 's' && s[2] == 'p')
8966                     {
8967                       s += 3;
8968                       regno = SP;
8969                     }
8970                   else if (s[1] == 'g' && s[2] == 'p')
8971                     {
8972                       s += 3;
8973                       regno = GP;
8974                     }
8975                   else if (s[1] == 'a' && s[2] == 't')
8976                     {
8977                       s += 3;
8978                       regno = AT;
8979                     }
8980                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8981                     {
8982                       s += 4;
8983                       regno = KT0;
8984                     }
8985                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8986                     {
8987                       s += 4;
8988                       regno = KT1;
8989                     }
8990                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8991                     {
8992                       s += 5;
8993                       regno = ZERO;
8994                     }
8995                   else
8996                     break;
8997                 }
8998
8999               if (*s == ' ')
9000                 ++s;
9001               if (args[1] != *s)
9002                 {
9003                   if (c == 'v' || c == 'w')
9004                     {
9005                       regno = mips16_to_32_reg_map[lastregno];
9006                       s = s_reset;
9007                       ++args;
9008                     }
9009                 }
9010
9011               switch (c)
9012                 {
9013                 case 'x':
9014                 case 'y':
9015                 case 'z':
9016                 case 'v':
9017                 case 'w':
9018                 case 'Z':
9019                   regno = mips32_to_16_reg_map[regno];
9020                   break;
9021
9022                 case '0':
9023                   if (regno != 0)
9024                     regno = ILLEGAL_REG;
9025                   break;
9026
9027                 case 'S':
9028                   if (regno != SP)
9029                     regno = ILLEGAL_REG;
9030                   break;
9031
9032                 case 'R':
9033                   if (regno != RA)
9034                     regno = ILLEGAL_REG;
9035                   break;
9036
9037                 case 'X':
9038                 case 'Y':
9039                   if (regno == AT && ! mips_opts.noat)
9040                     as_warn (_("used $at without \".set noat\""));
9041                   break;
9042
9043                 default:
9044                   internalError ();
9045                 }
9046
9047               if (regno == ILLEGAL_REG)
9048                 break;
9049
9050               switch (c)
9051                 {
9052                 case 'x':
9053                 case 'v':
9054                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9055                   break;
9056                 case 'y':
9057                 case 'w':
9058                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9059                   break;
9060                 case 'z':
9061                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9062                   break;
9063                 case 'Z':
9064                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9065                 case '0':
9066                 case 'S':
9067                 case 'R':
9068                   break;
9069                 case 'X':
9070                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9071                   break;
9072                 case 'Y':
9073                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9074                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9075                   break;
9076                 default:
9077                   internalError ();
9078                 }
9079
9080               lastregno = regno;
9081               continue;
9082
9083             case 'P':
9084               if (strncmp (s, "$pc", 3) == 0)
9085                 {
9086                   s += 3;
9087                   continue;
9088                 }
9089               break;
9090
9091             case '<':
9092             case '>':
9093             case '[':
9094             case ']':
9095             case '4':
9096             case '5':
9097             case 'H':
9098             case 'W':
9099             case 'D':
9100             case 'j':
9101             case '8':
9102             case 'V':
9103             case 'C':
9104             case 'U':
9105             case 'k':
9106             case 'K':
9107               if (s[0] == '%'
9108                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9109                 {
9110                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9111                      and generate the appropriate reloc.  If the text
9112                      inside %gprel is not a symbol name with an
9113                      optional offset, then we generate a normal reloc
9114                      and will probably fail later.  */
9115                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9116                   if (imm_expr.X_op == O_symbol)
9117                     {
9118                       mips16_ext = true;
9119                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9120                       s = expr_end;
9121                       ip->use_extend = true;
9122                       ip->extend = 0;
9123                       continue;
9124                     }
9125                 }
9126               else
9127                 {
9128                   /* Just pick up a normal expression.  */
9129                   my_getExpression (&imm_expr, s);
9130                 }
9131
9132               if (imm_expr.X_op == O_register)
9133                 {
9134                   /* What we thought was an expression turned out to
9135                      be a register.  */
9136
9137                   if (s[0] == '(' && args[1] == '(')
9138                     {
9139                       /* It looks like the expression was omitted
9140                          before a register indirection, which means
9141                          that the expression is implicitly zero.  We
9142                          still set up imm_expr, so that we handle
9143                          explicit extensions correctly.  */
9144                       imm_expr.X_op = O_constant;
9145                       imm_expr.X_add_number = 0;
9146                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9147                       continue;
9148                     }
9149
9150                   break;
9151                 }
9152
9153               /* We need to relax this instruction.  */
9154               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9155               s = expr_end;
9156               continue;
9157
9158             case 'p':
9159             case 'q':
9160             case 'A':
9161             case 'B':
9162             case 'E':
9163               /* We use offset_reloc rather than imm_reloc for the PC
9164                  relative operands.  This lets macros with both
9165                  immediate and address operands work correctly.  */
9166               my_getExpression (&offset_expr, s);
9167
9168               if (offset_expr.X_op == O_register)
9169                 break;
9170
9171               /* We need to relax this instruction.  */
9172               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9173               s = expr_end;
9174               continue;
9175
9176             case '6':           /* break code */
9177               my_getExpression (&imm_expr, s);
9178               check_absolute_expr (ip, &imm_expr);
9179               if ((unsigned long) imm_expr.X_add_number > 63)
9180                 {
9181                   as_warn (_("Invalid value for `%s' (%lu)"),
9182                            ip->insn_mo->name,
9183                            (unsigned long) imm_expr.X_add_number);
9184                   imm_expr.X_add_number &= 0x3f;
9185                 }
9186               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9187               imm_expr.X_op = O_absent;
9188               s = expr_end;
9189               continue;
9190
9191             case 'a':           /* 26 bit address */
9192               my_getExpression (&offset_expr, s);
9193               s = expr_end;
9194               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9195               ip->insn_opcode <<= 16;
9196               continue;
9197
9198             case 'l':           /* register list for entry macro */
9199             case 'L':           /* register list for exit macro */
9200               {
9201                 int mask;
9202
9203                 if (c == 'l')
9204                   mask = 0;
9205                 else
9206                   mask = 7 << 3;
9207                 while (*s != '\0')
9208                   {
9209                     int freg, reg1, reg2;
9210
9211                     while (*s == ' ' || *s == ',')
9212                       ++s;
9213                     if (*s != '$')
9214                       {
9215                         as_bad (_("can't parse register list"));
9216                         break;
9217                       }
9218                     ++s;
9219                     if (*s != 'f')
9220                       freg = 0;
9221                     else
9222                       {
9223                         freg = 1;
9224                         ++s;
9225                       }
9226                     reg1 = 0;
9227                     while (ISDIGIT (*s))
9228                       {
9229                         reg1 *= 10;
9230                         reg1 += *s - '0';
9231                         ++s;
9232                       }
9233                     if (*s == ' ')
9234                       ++s;
9235                     if (*s != '-')
9236                       reg2 = reg1;
9237                     else
9238                       {
9239                         ++s;
9240                         if (*s != '$')
9241                           break;
9242                         ++s;
9243                         if (freg)
9244                           {
9245                             if (*s == 'f')
9246                               ++s;
9247                             else
9248                               {
9249                                 as_bad (_("invalid register list"));
9250                                 break;
9251                               }
9252                           }
9253                         reg2 = 0;
9254                         while (ISDIGIT (*s))
9255                           {
9256                             reg2 *= 10;
9257                             reg2 += *s - '0';
9258                             ++s;
9259                           }
9260                       }
9261                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9262                       {
9263                         mask &= ~ (7 << 3);
9264                         mask |= 5 << 3;
9265                       }
9266                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9267                       {
9268                         mask &= ~ (7 << 3);
9269                         mask |= 6 << 3;
9270                       }
9271                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9272                       mask |= (reg2 - 3) << 3;
9273                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9274                       mask |= (reg2 - 15) << 1;
9275                     else if (reg1 == RA && reg2 == RA)
9276                       mask |= 1;
9277                     else
9278                       {
9279                         as_bad (_("invalid register list"));
9280                         break;
9281                       }
9282                   }
9283                 /* The mask is filled in in the opcode table for the
9284                    benefit of the disassembler.  We remove it before
9285                    applying the actual mask.  */
9286                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9287                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9288               }
9289             continue;
9290
9291             case 'e':           /* extend code */
9292               my_getExpression (&imm_expr, s);
9293               check_absolute_expr (ip, &imm_expr);
9294               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9295                 {
9296                   as_warn (_("Invalid value for `%s' (%lu)"),
9297                            ip->insn_mo->name,
9298                            (unsigned long) imm_expr.X_add_number);
9299                   imm_expr.X_add_number &= 0x7ff;
9300                 }
9301               ip->insn_opcode |= imm_expr.X_add_number;
9302               imm_expr.X_op = O_absent;
9303               s = expr_end;
9304               continue;
9305
9306             default:
9307               internalError ();
9308             }
9309           break;
9310         }
9311
9312       /* Args don't match.  */
9313       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9314           strcmp (insn->name, insn[1].name) == 0)
9315         {
9316           ++insn;
9317           s = argsstart;
9318           continue;
9319         }
9320
9321       insn_error = _("illegal operands");
9322
9323       return;
9324     }
9325 }
9326
9327 /* This structure holds information we know about a mips16 immediate
9328    argument type.  */
9329
9330 struct mips16_immed_operand
9331 {
9332   /* The type code used in the argument string in the opcode table.  */
9333   int type;
9334   /* The number of bits in the short form of the opcode.  */
9335   int nbits;
9336   /* The number of bits in the extended form of the opcode.  */
9337   int extbits;
9338   /* The amount by which the short form is shifted when it is used;
9339      for example, the sw instruction has a shift count of 2.  */
9340   int shift;
9341   /* The amount by which the short form is shifted when it is stored
9342      into the instruction code.  */
9343   int op_shift;
9344   /* Non-zero if the short form is unsigned.  */
9345   int unsp;
9346   /* Non-zero if the extended form is unsigned.  */
9347   int extu;
9348   /* Non-zero if the value is PC relative.  */
9349   int pcrel;
9350 };
9351
9352 /* The mips16 immediate operand types.  */
9353
9354 static const struct mips16_immed_operand mips16_immed_operands[] =
9355 {
9356   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9357   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9358   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9359   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9360   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9361   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9362   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9363   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9364   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9365   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9366   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9367   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9368   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9369   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9370   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9371   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9372   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9373   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9374   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9375   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9376   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9377 };
9378
9379 #define MIPS16_NUM_IMMED \
9380   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9381
9382 /* Handle a mips16 instruction with an immediate value.  This or's the
9383    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9384    whether an extended value is needed; if one is needed, it sets
9385    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9386    If SMALL is true, an unextended opcode was explicitly requested.
9387    If EXT is true, an extended opcode was explicitly requested.  If
9388    WARN is true, warn if EXT does not match reality.  */
9389
9390 static void
9391 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9392               extend)
9393      char *file;
9394      unsigned int line;
9395      int type;
9396      offsetT val;
9397      boolean warn;
9398      boolean small;
9399      boolean ext;
9400      unsigned long *insn;
9401      boolean *use_extend;
9402      unsigned short *extend;
9403 {
9404   register const struct mips16_immed_operand *op;
9405   int mintiny, maxtiny;
9406   boolean needext;
9407
9408   op = mips16_immed_operands;
9409   while (op->type != type)
9410     {
9411       ++op;
9412       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9413     }
9414
9415   if (op->unsp)
9416     {
9417       if (type == '<' || type == '>' || type == '[' || type == ']')
9418         {
9419           mintiny = 1;
9420           maxtiny = 1 << op->nbits;
9421         }
9422       else
9423         {
9424           mintiny = 0;
9425           maxtiny = (1 << op->nbits) - 1;
9426         }
9427     }
9428   else
9429     {
9430       mintiny = - (1 << (op->nbits - 1));
9431       maxtiny = (1 << (op->nbits - 1)) - 1;
9432     }
9433
9434   /* Branch offsets have an implicit 0 in the lowest bit.  */
9435   if (type == 'p' || type == 'q')
9436     val /= 2;
9437
9438   if ((val & ((1 << op->shift) - 1)) != 0
9439       || val < (mintiny << op->shift)
9440       || val > (maxtiny << op->shift))
9441     needext = true;
9442   else
9443     needext = false;
9444
9445   if (warn && ext && ! needext)
9446     as_warn_where (file, line,
9447                    _("extended operand requested but not required"));
9448   if (small && needext)
9449     as_bad_where (file, line, _("invalid unextended operand value"));
9450
9451   if (small || (! ext && ! needext))
9452     {
9453       int insnval;
9454
9455       *use_extend = false;
9456       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9457       insnval <<= op->op_shift;
9458       *insn |= insnval;
9459     }
9460   else
9461     {
9462       long minext, maxext;
9463       int extval;
9464
9465       if (op->extu)
9466         {
9467           minext = 0;
9468           maxext = (1 << op->extbits) - 1;
9469         }
9470       else
9471         {
9472           minext = - (1 << (op->extbits - 1));
9473           maxext = (1 << (op->extbits - 1)) - 1;
9474         }
9475       if (val < minext || val > maxext)
9476         as_bad_where (file, line,
9477                       _("operand value out of range for instruction"));
9478
9479       *use_extend = true;
9480       if (op->extbits == 16)
9481         {
9482           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9483           val &= 0x1f;
9484         }
9485       else if (op->extbits == 15)
9486         {
9487           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9488           val &= 0xf;
9489         }
9490       else
9491         {
9492           extval = ((val & 0x1f) << 6) | (val & 0x20);
9493           val = 0;
9494         }
9495
9496       *extend = (unsigned short) extval;
9497       *insn |= val;
9498     }
9499 }
9500 \f
9501 static struct percent_op_match
9502 {
9503    const char *str;
9504    const enum small_ex_type type;
9505 } percent_op[] =
9506 {
9507   {"%lo", S_EX_LO},
9508 #ifdef OBJ_ELF
9509   {"%call_hi", S_EX_CALL_HI},
9510   {"%call_lo", S_EX_CALL_LO},
9511   {"%call16", S_EX_CALL16},
9512   {"%got_disp", S_EX_GOT_DISP},
9513   {"%got_page", S_EX_GOT_PAGE},
9514   {"%got_ofst", S_EX_GOT_OFST},
9515   {"%got_hi", S_EX_GOT_HI},
9516   {"%got_lo", S_EX_GOT_LO},
9517   {"%got", S_EX_GOT},
9518   {"%gp_rel", S_EX_GP_REL},
9519   {"%half", S_EX_HALF},
9520   {"%highest", S_EX_HIGHEST},
9521   {"%higher", S_EX_HIGHER},
9522   {"%neg", S_EX_NEG},
9523 #endif
9524   {"%hi", S_EX_HI}
9525 };
9526
9527 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9528    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9529    can be nested, this is handled by blanking the innermost, parsing the
9530    rest by subsequent calls.  */
9531
9532 static int
9533 my_getSmallParser (str, len, nestlevel)
9534      char **str;
9535      unsigned int *len;
9536      int *nestlevel;
9537 {
9538   *len = 0;
9539   *str += strspn (*str, " \t");
9540   /* Check for expression in parentheses.  */
9541   if (**str == '(')
9542     {
9543       char *b = *str + 1 + strspn (*str + 1, " \t");
9544       char *e;
9545
9546       /* Check for base register.  */
9547       if (b[0] == '$')
9548         {
9549           if (strchr (b, ')')
9550               && (e = b + strcspn (b, ") \t"))
9551               && e - b > 1 && e - b < 4)
9552             {
9553               if ((e - b == 3
9554                    && ((b[1] == 'f' && b[2] == 'p')
9555                        || (b[1] == 's' && b[2] == 'p')
9556                        || (b[1] == 'g' && b[2] == 'p')
9557                        || (b[1] == 'a' && b[2] == 't')
9558                        || (ISDIGIT (b[1])
9559                            && ISDIGIT (b[2]))))
9560                   || (ISDIGIT (b[1])))
9561                 {
9562                   *len = strcspn (*str, ")") + 1;
9563                   return S_EX_REGISTER;
9564                 }
9565             }
9566         }
9567       /* Check for percent_op (in parentheses).  */
9568       else if (b[0] == '%')
9569         {
9570           *str = b;
9571           return my_getPercentOp (str, len, nestlevel);
9572         }
9573
9574       /* Some other expression in the parentheses, which can contain
9575          parentheses itself. Attempt to find the matching one.  */
9576       {
9577         int pcnt = 1;
9578         char *s;
9579
9580         *len = 1;
9581         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9582           {
9583             if (*s == '(')
9584               ++pcnt;
9585             else if (*s == ')')
9586               --pcnt;
9587           }
9588       }
9589     }
9590   /* Check for percent_op (outside of parentheses).  */
9591   else if (*str[0] == '%')
9592     return my_getPercentOp (str, len, nestlevel);
9593
9594   /* Any other expression.  */
9595   return S_EX_NONE;
9596 }
9597
9598 static int
9599 my_getPercentOp (str, len, nestlevel)
9600      char **str;
9601      unsigned int *len;
9602      int *nestlevel;
9603 {
9604   char *tmp = *str + 1;
9605   unsigned int i = 0;
9606
9607   while (ISALPHA (*tmp) || *tmp == '_')
9608     {
9609       *tmp = TOLOWER (*tmp);
9610       tmp++;
9611     }
9612   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9613     {
9614       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9615         i++;
9616       else
9617         {
9618           int type = percent_op[i].type;
9619
9620           /* Only %hi and %lo are allowed for OldABI.  */
9621           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9622             return S_EX_NONE;
9623
9624           *len = strlen (percent_op[i].str);
9625           ++(*nestlevel);
9626           return type;
9627         }
9628     }
9629   return S_EX_NONE;
9630 }
9631
9632 static int
9633 my_getSmallExpression (ep, str)
9634      expressionS *ep;
9635      char *str;
9636 {
9637   static char *oldstr = NULL;
9638   int c = S_EX_NONE;
9639   int oldc;
9640   int nestlevel = -1;
9641   unsigned int len;
9642
9643   /* Don't update oldstr if the last call had nested percent_op's. We need
9644      it to parse the outer ones later.  */
9645   if (! oldstr)
9646     oldstr = str;
9647
9648   do
9649     {
9650       oldc = c;
9651       c = my_getSmallParser (&str, &len, &nestlevel);
9652       if (c != S_EX_NONE && c != S_EX_REGISTER)
9653         str += len;
9654     }
9655   while (c != S_EX_NONE && c != S_EX_REGISTER);
9656
9657   if (nestlevel >= 0)
9658     {
9659       /* A percent_op was encountered.  Don't try to get an expression if
9660          it is already blanked out.  */
9661       if (*(str + strspn (str + 1, " )")) != ')')
9662         {
9663           char save;
9664
9665           /* Let my_getExpression() stop at the closing parenthesis.  */
9666           save = *(str + len);
9667           *(str + len) = '\0';
9668           my_getExpression (ep, str);
9669           *(str + len) = save;
9670         }
9671       if (nestlevel > 0)
9672         {
9673           /* Blank out including the % sign and the proper matching
9674              parenthesis.  */
9675           int pcnt = 1;
9676           char *s = strrchr (oldstr, '%');
9677           char *end;
9678
9679           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9680             {
9681               if (*end == '(')
9682                 ++pcnt;
9683               else if (*end == ')')
9684                 --pcnt;
9685             }
9686
9687           memset (s, ' ', end - s);
9688           str = oldstr;
9689         }
9690       else
9691         expr_end = str + len;
9692
9693       c = oldc;
9694     }
9695   else if (c == S_EX_NONE)
9696     {
9697       my_getExpression (ep, str);
9698     }
9699   else if (c == S_EX_REGISTER)
9700     {
9701       ep->X_op = O_constant;
9702       expr_end = str;
9703       ep->X_add_symbol = NULL;
9704       ep->X_op_symbol = NULL;
9705       ep->X_add_number = 0;
9706     }
9707   else
9708     {
9709       as_fatal (_("internal error"));
9710     }
9711
9712   if (nestlevel <= 0)
9713     /* All percent_op's have been handled.  */
9714     oldstr = NULL;
9715
9716   return c;
9717 }
9718
9719 static void
9720 my_getExpression (ep, str)
9721      expressionS *ep;
9722      char *str;
9723 {
9724   char *save_in;
9725   valueT val;
9726
9727   save_in = input_line_pointer;
9728   input_line_pointer = str;
9729   expression (ep);
9730   expr_end = input_line_pointer;
9731   input_line_pointer = save_in;
9732
9733   /* If we are in mips16 mode, and this is an expression based on `.',
9734      then we bump the value of the symbol by 1 since that is how other
9735      text symbols are handled.  We don't bother to handle complex
9736      expressions, just `.' plus or minus a constant.  */
9737   if (mips_opts.mips16
9738       && ep->X_op == O_symbol
9739       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9740       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9741       && symbol_get_frag (ep->X_add_symbol) == frag_now
9742       && symbol_constant_p (ep->X_add_symbol)
9743       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9744     S_SET_VALUE (ep->X_add_symbol, val + 1);
9745 }
9746
9747 /* Turn a string in input_line_pointer into a floating point constant
9748    of type TYPE, and store the appropriate bytes in *LITP.  The number
9749    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9750    returned, or NULL on OK.  */
9751
9752 char *
9753 md_atof (type, litP, sizeP)
9754      int type;
9755      char *litP;
9756      int *sizeP;
9757 {
9758   int prec;
9759   LITTLENUM_TYPE words[4];
9760   char *t;
9761   int i;
9762
9763   switch (type)
9764     {
9765     case 'f':
9766       prec = 2;
9767       break;
9768
9769     case 'd':
9770       prec = 4;
9771       break;
9772
9773     default:
9774       *sizeP = 0;
9775       return _("bad call to md_atof");
9776     }
9777
9778   t = atof_ieee (input_line_pointer, type, words);
9779   if (t)
9780     input_line_pointer = t;
9781
9782   *sizeP = prec * 2;
9783
9784   if (! target_big_endian)
9785     {
9786       for (i = prec - 1; i >= 0; i--)
9787         {
9788           md_number_to_chars (litP, (valueT) words[i], 2);
9789           litP += 2;
9790         }
9791     }
9792   else
9793     {
9794       for (i = 0; i < prec; i++)
9795         {
9796           md_number_to_chars (litP, (valueT) words[i], 2);
9797           litP += 2;
9798         }
9799     }
9800
9801   return NULL;
9802 }
9803
9804 void
9805 md_number_to_chars (buf, val, n)
9806      char *buf;
9807      valueT val;
9808      int n;
9809 {
9810   if (target_big_endian)
9811     number_to_chars_bigendian (buf, val, n);
9812   else
9813     number_to_chars_littleendian (buf, val, n);
9814 }
9815 \f
9816 #ifdef OBJ_ELF
9817 static int support_64bit_objects(void)
9818 {
9819   const char **list, **l;
9820
9821   list = bfd_target_list ();
9822   for (l = list; *l != NULL; l++)
9823 #ifdef TE_TMIPS
9824     /* This is traditional mips */
9825     if (strcmp (*l, "elf64-tradbigmips") == 0
9826         || strcmp (*l, "elf64-tradlittlemips") == 0)
9827 #else
9828     if (strcmp (*l, "elf64-bigmips") == 0
9829         || strcmp (*l, "elf64-littlemips") == 0)
9830 #endif
9831       break;
9832   free (list);
9833   return (*l != NULL);
9834 }
9835 #endif /* OBJ_ELF */
9836
9837 const char *md_shortopts = "nO::g::G:";
9838
9839 struct option md_longopts[] =
9840 {
9841 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9842   {"mips0", no_argument, NULL, OPTION_MIPS1},
9843   {"mips1", no_argument, NULL, OPTION_MIPS1},
9844 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9845   {"mips2", no_argument, NULL, OPTION_MIPS2},
9846 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9847   {"mips3", no_argument, NULL, OPTION_MIPS3},
9848 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9849   {"mips4", no_argument, NULL, OPTION_MIPS4},
9850 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9851   {"mips5", no_argument, NULL, OPTION_MIPS5},
9852 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9853   {"mips32", no_argument, NULL, OPTION_MIPS32},
9854 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9855   {"mips64", no_argument, NULL, OPTION_MIPS64},
9856 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9857   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9858 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9859   {"trap", no_argument, NULL, OPTION_TRAP},
9860   {"no-break", no_argument, NULL, OPTION_TRAP},
9861 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9862   {"break", no_argument, NULL, OPTION_BREAK},
9863   {"no-trap", no_argument, NULL, OPTION_BREAK},
9864 #define OPTION_EB (OPTION_MD_BASE + 11)
9865   {"EB", no_argument, NULL, OPTION_EB},
9866 #define OPTION_EL (OPTION_MD_BASE + 12)
9867   {"EL", no_argument, NULL, OPTION_EL},
9868 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9869   {"mips16", no_argument, NULL, OPTION_MIPS16},
9870 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9871   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9872 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9873   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9874 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9875   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9876   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9877 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9878   {"mfp32", no_argument, NULL, OPTION_FP32},
9879 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9880   {"mgp32", no_argument, NULL, OPTION_GP32},
9881 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9882   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9883 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9884   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9885 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9886   {"march", required_argument, NULL, OPTION_MARCH},
9887 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9888   {"mtune", required_argument, NULL, OPTION_MTUNE},
9889 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9890   {"mcpu", required_argument, NULL, OPTION_MCPU},
9891 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9892   {"m4650", no_argument, NULL, OPTION_M4650},
9893 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9894   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9895 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9896   {"m4010", no_argument, NULL, OPTION_M4010},
9897 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9898   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9899 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9900   {"m4100", no_argument, NULL, OPTION_M4100},
9901 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9902   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9903 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9904   {"m3900", no_argument, NULL, OPTION_M3900},
9905 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9906   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9907 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9908   {"mgp64", no_argument, NULL, OPTION_GP64},
9909 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9910   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9911 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9912   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9913 #define OPTION_MDMX (OPTION_MD_BASE + 35)
9914   {"mdmx", no_argument, NULL, OPTION_MDMX},
9915 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9916   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9917 #ifdef OBJ_ELF
9918 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 37)
9919 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9920   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9921   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9922 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
9923   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
9924 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
9925   {"xgot",        no_argument, NULL, OPTION_XGOT},
9926 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
9927   {"mabi", required_argument, NULL, OPTION_MABI},
9928 #define OPTION_32          (OPTION_ELF_BASE + 4)
9929   {"32",          no_argument, NULL, OPTION_32},
9930 #define OPTION_N32         (OPTION_ELF_BASE + 5)
9931   {"n32",         no_argument, NULL, OPTION_N32},
9932 #define OPTION_64          (OPTION_ELF_BASE + 6)
9933   {"64",          no_argument, NULL, OPTION_64},
9934 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
9935   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
9936 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
9937   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
9938 #endif /* OBJ_ELF */
9939   {NULL, no_argument, NULL, 0}
9940 };
9941 size_t md_longopts_size = sizeof (md_longopts);
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       mips_opts.isa = ISA_MIPS1;
9999       break;
10000
10001     case OPTION_MIPS2:
10002       mips_opts.isa = ISA_MIPS2;
10003       break;
10004
10005     case OPTION_MIPS3:
10006       mips_opts.isa = ISA_MIPS3;
10007       break;
10008
10009     case OPTION_MIPS4:
10010       mips_opts.isa = ISA_MIPS4;
10011       break;
10012
10013     case OPTION_MIPS5:
10014       mips_opts.isa = ISA_MIPS5;
10015       break;
10016
10017     case OPTION_MIPS32:
10018       mips_opts.isa = ISA_MIPS32;
10019       break;
10020
10021     case OPTION_MIPS64:
10022       mips_opts.isa = ISA_MIPS64;
10023       break;
10024
10025     case OPTION_MTUNE:
10026     case OPTION_MARCH:
10027     case OPTION_MCPU:
10028       {
10029         int cpu = CPU_UNKNOWN;
10030
10031         /* Identify the processor type.  */
10032         if (strcasecmp (arg, "default") != 0)
10033           {
10034             const struct mips_cpu_info *ci;
10035
10036             ci = mips_cpu_info_from_name (arg);
10037             if (ci == NULL || ci->is_isa)
10038               {
10039                 switch (c)
10040                   {
10041                   case OPTION_MTUNE:
10042                     as_fatal (_("invalid architecture -mtune=%s"), arg);
10043                     break;
10044                   case OPTION_MARCH:
10045                     as_fatal (_("invalid architecture -march=%s"), arg);
10046                     break;
10047                   case OPTION_MCPU:
10048                     as_fatal (_("invalid architecture -mcpu=%s"), arg);
10049                     break;
10050                   }
10051               }
10052             else
10053               cpu = ci->cpu;
10054           }
10055
10056         switch (c)
10057           {
10058           case OPTION_MTUNE:
10059             if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
10060               as_warn (_("A different -mtune= was already specified, is now "
10061                          "-mtune=%s"), arg);
10062             mips_tune = cpu;
10063             break;
10064           case OPTION_MARCH:
10065             if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
10066               as_warn (_("A different -march= was already specified, is now "
10067                          "-march=%s"), arg);
10068             mips_arch = cpu;
10069             break;
10070           case OPTION_MCPU:
10071             if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
10072               as_warn (_("A different -mcpu= was already specified, is now "
10073                          "-mcpu=%s"), arg);
10074             mips_cpu = cpu;
10075           }
10076       }
10077       break;
10078
10079     case OPTION_M4650:
10080       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
10081           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
10082         as_warn (_("A different -march= or -mtune= was already specified, "
10083                    "is now -m4650"));
10084       mips_arch = CPU_R4650;
10085       mips_tune = CPU_R4650;
10086       break;
10087
10088     case OPTION_NO_M4650:
10089       break;
10090
10091     case OPTION_M4010:
10092       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
10093           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
10094         as_warn (_("A different -march= or -mtune= was already specified, "
10095                    "is now -m4010"));
10096       mips_arch = CPU_R4010;
10097       mips_tune = CPU_R4010;
10098       break;
10099
10100     case OPTION_NO_M4010:
10101       break;
10102
10103     case OPTION_M4100:
10104       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
10105           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
10106         as_warn (_("A different -march= or -mtune= was already specified, "
10107                    "is now -m4100"));
10108       mips_arch = CPU_VR4100;
10109       mips_tune = CPU_VR4100;
10110       break;
10111
10112     case OPTION_NO_M4100:
10113       break;
10114
10115     case OPTION_M3900:
10116       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
10117           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
10118         as_warn (_("A different -march= or -mtune= was already specified, "
10119                    "is now -m3900"));
10120       mips_arch = CPU_R3900;
10121       mips_tune = CPU_R3900;
10122       break;
10123
10124     case OPTION_NO_M3900:
10125       break;
10126
10127     case OPTION_MDMX:
10128       mips_opts.ase_mdmx = 1;
10129       break;
10130
10131     case OPTION_NO_MDMX:
10132       mips_opts.ase_mdmx = 0;
10133       break;
10134
10135     case OPTION_MIPS16:
10136       mips_opts.mips16 = 1;
10137       mips_no_prev_insn (false);
10138       break;
10139
10140     case OPTION_NO_MIPS16:
10141       mips_opts.mips16 = 0;
10142       mips_no_prev_insn (false);
10143       break;
10144
10145     case OPTION_MIPS3D:
10146       mips_opts.ase_mips3d = 1;
10147       break;
10148
10149     case OPTION_NO_MIPS3D:
10150       mips_opts.ase_mips3d = 0;
10151       break;
10152
10153     case OPTION_MEMBEDDED_PIC:
10154       mips_pic = EMBEDDED_PIC;
10155       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10156         {
10157           as_bad (_("-G may not be used with embedded PIC code"));
10158           return 0;
10159         }
10160       g_switch_value = 0x7fffffff;
10161       break;
10162
10163 #ifdef OBJ_ELF
10164       /* When generating ELF code, we permit -KPIC and -call_shared to
10165          select SVR4_PIC, and -non_shared to select no PIC.  This is
10166          intended to be compatible with Irix 5.  */
10167     case OPTION_CALL_SHARED:
10168       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10169         {
10170           as_bad (_("-call_shared is supported only for ELF format"));
10171           return 0;
10172         }
10173       mips_pic = SVR4_PIC;
10174       if (g_switch_seen && g_switch_value != 0)
10175         {
10176           as_bad (_("-G may not be used with SVR4 PIC code"));
10177           return 0;
10178         }
10179       g_switch_value = 0;
10180       break;
10181
10182     case OPTION_NON_SHARED:
10183       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10184         {
10185           as_bad (_("-non_shared is supported only for ELF format"));
10186           return 0;
10187         }
10188       mips_pic = NO_PIC;
10189       break;
10190
10191       /* The -xgot option tells the assembler to use 32 offsets when
10192          accessing the got in SVR4_PIC mode.  It is for Irix
10193          compatibility.  */
10194     case OPTION_XGOT:
10195       mips_big_got = 1;
10196       break;
10197 #endif /* OBJ_ELF */
10198
10199     case 'G':
10200       if (! USE_GLOBAL_POINTER_OPT)
10201         {
10202           as_bad (_("-G is not supported for this configuration"));
10203           return 0;
10204         }
10205       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10206         {
10207           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10208           return 0;
10209         }
10210       else
10211         g_switch_value = atoi (arg);
10212       g_switch_seen = 1;
10213       break;
10214
10215 #ifdef OBJ_ELF
10216       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10217          and -mabi=64.  */
10218     case OPTION_32:
10219       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10220         {
10221           as_bad (_("-32 is supported for ELF format only"));
10222           return 0;
10223         }
10224       mips_opts.abi = O32_ABI;
10225       break;
10226
10227     case OPTION_N32:
10228       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10229         {
10230           as_bad (_("-n32 is supported for ELF format only"));
10231           return 0;
10232         }
10233       mips_opts.abi = N32_ABI;
10234       break;
10235
10236     case OPTION_64:
10237       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10238         {
10239           as_bad (_("-64 is supported for ELF format only"));
10240           return 0;
10241         }
10242       mips_opts.abi = N64_ABI;
10243       if (! support_64bit_objects())
10244         as_fatal (_("No compiled in support for 64 bit object file format"));
10245       break;
10246 #endif /* OBJ_ELF */
10247
10248     case OPTION_GP32:
10249       file_mips_gp32 = 1;
10250       if (mips_opts.abi != O32_ABI)
10251         mips_opts.abi = NO_ABI;
10252       break;
10253
10254     case OPTION_GP64:
10255       file_mips_gp32 = 0;
10256       if (mips_opts.abi == O32_ABI)
10257         mips_opts.abi = NO_ABI;
10258       break;
10259
10260     case OPTION_FP32:
10261       file_mips_fp32 = 1;
10262       if (mips_opts.abi != O32_ABI)
10263         mips_opts.abi = NO_ABI;
10264       break;
10265
10266 #ifdef OBJ_ELF
10267     case OPTION_MABI:
10268       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10269         {
10270           as_bad (_("-mabi is supported for ELF format only"));
10271           return 0;
10272         }
10273       if (strcmp (arg, "32") == 0)
10274         mips_opts.abi = O32_ABI;
10275       else if (strcmp (arg, "o64") == 0)
10276         mips_opts.abi = O64_ABI;
10277       else if (strcmp (arg, "n32") == 0)
10278         mips_opts.abi = N32_ABI;
10279       else if (strcmp (arg, "64") == 0)
10280         {
10281           mips_opts.abi = N64_ABI;
10282           if (! support_64bit_objects())
10283             as_fatal (_("No compiled in support for 64 bit object file "
10284                         "format"));
10285         }
10286       else if (strcmp (arg, "eabi") == 0)
10287         mips_opts.abi = EABI_ABI;
10288       else
10289         {
10290           as_fatal (_("invalid abi -mabi=%s"), arg);
10291           return 0;
10292         }
10293       break;
10294 #endif /* OBJ_ELF */
10295
10296     case OPTION_M7000_HILO_FIX:
10297       mips_7000_hilo_fix = true;
10298       break;
10299
10300     case OPTION_MNO_7000_HILO_FIX:
10301       mips_7000_hilo_fix = false;
10302       break;
10303
10304 #ifdef OBJ_ELF
10305     case OPTION_MDEBUG:
10306       mips_flag_mdebug = true;
10307       break;
10308
10309     case OPTION_NO_MDEBUG:
10310       mips_flag_mdebug = false;
10311       break;
10312 #endif /* OBJ_ELF */
10313
10314     default:
10315       return 0;
10316     }
10317
10318   return 1;
10319 }
10320
10321 static void
10322 show (stream, string, col_p, first_p)
10323      FILE *stream;
10324      char *string;
10325      int *col_p;
10326      int *first_p;
10327 {
10328   if (*first_p)
10329     {
10330       fprintf (stream, "%24s", "");
10331       *col_p = 24;
10332     }
10333   else
10334     {
10335       fprintf (stream, ", ");
10336       *col_p += 2;
10337     }
10338
10339   if (*col_p + strlen (string) > 72)
10340     {
10341       fprintf (stream, "\n%24s", "");
10342       *col_p = 24;
10343     }
10344
10345   fprintf (stream, "%s", string);
10346   *col_p += strlen (string);
10347
10348   *first_p = 0;
10349 }
10350
10351 void
10352 md_show_usage (stream)
10353      FILE *stream;
10354 {
10355   int column, first;
10356
10357   fprintf (stream, _("\
10358 MIPS options:\n\
10359 -membedded-pic          generate embedded position independent code\n\
10360 -EB                     generate big endian output\n\
10361 -EL                     generate little endian output\n\
10362 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
10363 -G NUM                  allow referencing objects up to NUM bytes\n\
10364                         implicitly with the gp register [default 8]\n"));
10365   fprintf (stream, _("\
10366 -mips1                  generate MIPS ISA I instructions\n\
10367 -mips2                  generate MIPS ISA II instructions\n\
10368 -mips3                  generate MIPS ISA III instructions\n\
10369 -mips4                  generate MIPS ISA IV instructions\n\
10370 -mips5                  generate MIPS ISA V instructions\n\
10371 -mips32                 generate MIPS32 ISA instructions\n\
10372 -mips64                 generate MIPS64 ISA instructions\n\
10373 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
10374
10375   first = 1;
10376
10377   show (stream, "2000", &column, &first);
10378   show (stream, "3000", &column, &first);
10379   show (stream, "3900", &column, &first);
10380   show (stream, "4000", &column, &first);
10381   show (stream, "4010", &column, &first);
10382   show (stream, "4100", &column, &first);
10383   show (stream, "4111", &column, &first);
10384   show (stream, "4300", &column, &first);
10385   show (stream, "4400", &column, &first);
10386   show (stream, "4600", &column, &first);
10387   show (stream, "4650", &column, &first);
10388   show (stream, "5000", &column, &first);
10389   show (stream, "5200", &column, &first);
10390   show (stream, "5230", &column, &first);
10391   show (stream, "5231", &column, &first);
10392   show (stream, "5261", &column, &first);
10393   show (stream, "5721", &column, &first);
10394   show (stream, "6000", &column, &first);
10395   show (stream, "8000", &column, &first);
10396   show (stream, "10000", &column, &first);
10397   show (stream, "12000", &column, &first);
10398   show (stream, "sb1", &column, &first);
10399   fputc ('\n', stream);
10400
10401   fprintf (stream, _("\
10402 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10403 -no-mCPU                don't generate code specific to CPU.\n\
10404                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
10405
10406   first = 1;
10407
10408   show (stream, "3900", &column, &first);
10409   show (stream, "4010", &column, &first);
10410   show (stream, "4100", &column, &first);
10411   show (stream, "4650", &column, &first);
10412   fputc ('\n', stream);
10413
10414   fprintf (stream, _("\
10415 -mips16                 generate mips16 instructions\n\
10416 -no-mips16              do not generate mips16 instructions\n"));
10417   fprintf (stream, _("\
10418 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
10419 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
10420 -O0                     remove unneeded NOPs, do not swap branches\n\
10421 -O                      remove unneeded NOPs and swap branches\n\
10422 -n                      warn about NOPs generated from macros\n\
10423 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10424 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
10425 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
10426 #ifdef OBJ_ELF
10427   fprintf (stream, _("\
10428 -KPIC, -call_shared     generate SVR4 position independent code\n\
10429 -non_shared             do not generate position independent code\n\
10430 -xgot                   assume a 32 bit GOT\n\
10431 -mabi=ABI               create ABI conformant object file for:\n"));
10432
10433   first = 1;
10434
10435   show (stream, "32", &column, &first);
10436   show (stream, "o64", &column, &first);
10437   show (stream, "n32", &column, &first);
10438   show (stream, "64", &column, &first);
10439   show (stream, "eabi", &column, &first);
10440
10441   fputc ('\n', stream);
10442
10443   fprintf (stream, _("\
10444 -32                     create o32 ABI object file (default)\n\
10445 -n32                    create n32 ABI object file\n\
10446 -64                     create 64 ABI object file\n"));
10447 #endif
10448 }
10449 \f
10450 void
10451 mips_after_parse_args ()
10452 {
10453   const char *cpu;
10454   char *a = NULL;
10455   int mips_isa_from_cpu;
10456   const struct mips_cpu_info *ci;
10457
10458   /* GP relative stuff not working for PE */
10459   if (strncmp (TARGET_OS, "pe", 2) == 0
10460       && g_switch_value != 0)
10461     {
10462       if (g_switch_seen)
10463         as_bad (_("-G not supported in this configuration."));
10464       g_switch_value = 0;
10465     }
10466
10467   cpu = TARGET_CPU;
10468   if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
10469     {
10470       a = xmalloc (sizeof TARGET_CPU);
10471       strcpy (a, TARGET_CPU);
10472       a[(sizeof TARGET_CPU) - 3] = '\0';
10473       cpu = a;
10474     }
10475
10476   /* Backward compatibility for historic -mcpu= option.  Check for
10477      incompatible options, warn if -mcpu is used.  */
10478   if (mips_cpu != CPU_UNKNOWN
10479       && mips_arch != CPU_UNKNOWN
10480       && mips_cpu != mips_arch)
10481     {
10482       as_fatal (_("The -mcpu option can't be used together with -march. "
10483                   "Use -mtune instead of -mcpu."));
10484     }
10485
10486   if (mips_cpu != CPU_UNKNOWN
10487       && mips_tune != CPU_UNKNOWN
10488       && mips_cpu != mips_tune)
10489     {
10490       as_fatal (_("The -mcpu option can't be used together with -mtune. "
10491                   "Use -march instead of -mcpu."));
10492     }
10493
10494 #if 1
10495   /* For backward compatibility, let -mipsN set various defaults.  */
10496   /* This code should go away, to be replaced with something rather more
10497      draconian.  Until GCC 3.1 has been released for some reasonable
10498      amount of time, however, we need to support this.  */
10499   if (mips_opts.isa != ISA_UNKNOWN)
10500     {
10501       /* Translate -mipsN to the appropriate settings of file_mips_gp32
10502          and file_mips_fp32.  Tag binaries as using the mipsN ISA.  */
10503       if (file_mips_gp32 < 0)
10504         {
10505           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
10506             file_mips_gp32 = 0;
10507           else
10508             file_mips_gp32 = 1;
10509         }
10510       if (file_mips_fp32 < 0)
10511         {
10512           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
10513             file_mips_fp32 = 0;
10514           else
10515             file_mips_fp32 = 1;
10516         }
10517
10518       ci = mips_cpu_info_from_isa (mips_opts.isa);
10519       assert (ci != NULL);
10520       /* -mipsN has higher priority than -mcpu but lower than -march.  */
10521       if (mips_arch == CPU_UNKNOWN)
10522         mips_arch = ci->cpu;
10523
10524       /* Default mips_abi.  */
10525       if (mips_opts.abi == NO_ABI)
10526         {
10527           if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
10528             mips_opts.abi = O32_ABI;
10529           else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
10530             mips_opts.abi = O64_ABI;
10531         }
10532     }
10533
10534   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10535     {
10536       ci = mips_cpu_info_from_cpu (mips_cpu);
10537       assert (ci != NULL);
10538       mips_arch = ci->cpu;
10539       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
10540                  "-mtune instead."));
10541     }
10542
10543   /* Set tune from -mcpu, not from -mipsN.  */
10544   if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10545     {
10546       ci = mips_cpu_info_from_cpu (mips_cpu);
10547       assert (ci != NULL);
10548       mips_tune = ci->cpu;
10549     }
10550
10551   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
10552      specified on the command line, or some other value if one was.
10553      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
10554      the command line, or will be set otherwise if one was.  */
10555
10556   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10557     /* Handled above.  */;
10558 #else
10559   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10560     {
10561       ci = mips_cpu_info_from_cpu (mips_cpu);
10562       assert (ci != NULL);
10563       mips_arch = ci->cpu;
10564       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
10565                  "-mtune instead."));
10566     }
10567
10568   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
10569      specified on the command line, or some other value if one was.
10570      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
10571      the command line, or will be set otherwise if one was.  */
10572
10573   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10574     {
10575       /* We have to check if the isa is the default isa of arch.  Otherwise
10576          we'll get invalid object file headers.  */
10577       ci = mips_cpu_info_from_cpu (mips_arch);
10578       assert (ci != NULL);
10579       if (mips_opts.isa != ci->isa)
10580         {
10581           /* This really should be an error instead of a warning, but old
10582              compilers only have -mcpu which sets both arch and tune.  For
10583              now, we discard arch and preserve tune.  */
10584           as_warn (_("The -march option is incompatible to -mipsN and "
10585                      "therefore ignored."));
10586           if (mips_tune == CPU_UNKNOWN)
10587             mips_tune = mips_arch;
10588           ci = mips_cpu_info_from_isa (mips_opts.isa);
10589           assert (ci != NULL);
10590           mips_arch = ci->cpu;
10591         }
10592     }
10593 #endif
10594   else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
10595     {
10596       /* We have ARCH, we need ISA.  */
10597       ci = mips_cpu_info_from_cpu (mips_arch);
10598       assert (ci != NULL);
10599       mips_opts.isa = ci->isa;
10600     }
10601   else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10602     {
10603       /* We have ISA, we need default ARCH.  */
10604       ci = mips_cpu_info_from_isa (mips_opts.isa);
10605       assert (ci != NULL);
10606       mips_arch = ci->cpu;
10607     }
10608   else
10609     {
10610       /* We need to set both ISA and ARCH from target cpu.  */
10611       ci = mips_cpu_info_from_name (cpu);
10612       if (ci == NULL)
10613         ci = mips_cpu_info_from_cpu (CPU_R3000);
10614       assert (ci != NULL);
10615       mips_opts.isa = ci->isa;
10616       mips_arch = ci->cpu;
10617     }
10618
10619   if (mips_tune == CPU_UNKNOWN)
10620     mips_tune = mips_arch;
10621
10622   ci = mips_cpu_info_from_cpu (mips_arch);
10623   assert (ci != NULL);
10624   mips_isa_from_cpu = ci->isa;
10625
10626   /* End of TARGET_CPU processing, get rid of malloced memory
10627      if necessary.  */
10628   cpu = NULL;
10629   if (a != NULL)
10630     {
10631       free (a);
10632       a = NULL;
10633     }
10634
10635   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10636     as_bad (_("trap exception not supported at ISA 1"));
10637
10638   /* If they asked for mips1 or mips2 and a cpu that is
10639      mips3 or greater, then mark the object file 32BITMODE.  */
10640   if (mips_isa_from_cpu != ISA_UNKNOWN
10641       && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
10642       && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
10643     mips_32bitmode = 1;
10644
10645   /* If the selected architecture includes support for ASEs, enable
10646      generation of code for them.  */
10647   if (mips_opts.mips16 == -1)
10648     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10649   if (mips_opts.ase_mips3d == -1)
10650     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10651   if (mips_opts.ase_mdmx == -1)
10652     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10653
10654   if (file_mips_gp32 < 0)
10655     file_mips_gp32 = 0;
10656   if (file_mips_fp32 < 0)
10657     file_mips_fp32 = 0;
10658
10659   file_mips_isa = mips_opts.isa;
10660   file_mips_abi = mips_opts.abi;
10661   file_ase_mips16 = mips_opts.mips16;
10662   file_ase_mips3d = mips_opts.ase_mips3d;
10663   file_ase_mdmx = mips_opts.ase_mdmx;
10664   mips_opts.gp32 = file_mips_gp32;
10665   mips_opts.fp32 = file_mips_fp32;
10666
10667   if (HAVE_NEWABI)
10668     mips_big_got = 1;
10669
10670   if (mips_flag_mdebug < 0)
10671     {
10672 #ifdef OBJ_MAYBE_ECOFF
10673       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10674         mips_flag_mdebug = 1;
10675       else
10676 #endif /* OBJ_MAYBE_ECOFF */
10677         mips_flag_mdebug = 0;
10678     }
10679 }
10680 \f
10681 void
10682 mips_init_after_args ()
10683 {
10684   /* initialize opcodes */
10685   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10686   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10687 }
10688
10689 long
10690 md_pcrel_from (fixP)
10691      fixS *fixP;
10692 {
10693   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10694       && fixP->fx_addsy != (symbolS *) NULL
10695       && ! S_IS_DEFINED (fixP->fx_addsy))
10696     {
10697       /* This makes a branch to an undefined symbol be a branch to the
10698          current location.  */
10699       if (mips_pic == EMBEDDED_PIC)
10700         return 4;
10701       else
10702         return 1;
10703     }
10704
10705   /* Return the address of the delay slot.  */
10706   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10707 }
10708
10709 /* This is called before the symbol table is processed.  In order to
10710    work with gcc when using mips-tfile, we must keep all local labels.
10711    However, in other cases, we want to discard them.  If we were
10712    called with -g, but we didn't see any debugging information, it may
10713    mean that gcc is smuggling debugging information through to
10714    mips-tfile, in which case we must generate all local labels.  */
10715
10716 void
10717 mips_frob_file_before_adjust ()
10718 {
10719 #ifndef NO_ECOFF_DEBUGGING
10720   if (ECOFF_DEBUGGING
10721       && mips_debug != 0
10722       && ! ecoff_debugging_seen)
10723     flag_keep_locals = 1;
10724 #endif
10725 }
10726
10727 /* Sort any unmatched HI16_S relocs so that they immediately precede
10728    the corresponding LO reloc.  This is called before md_apply_fix3 and
10729    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10730    explicit use of the %hi modifier.  */
10731
10732 void
10733 mips_frob_file ()
10734 {
10735   struct mips_hi_fixup *l;
10736
10737   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10738     {
10739       segment_info_type *seginfo;
10740       int pass;
10741
10742       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10743
10744       /* Check quickly whether the next fixup happens to be a matching
10745          %lo.  */
10746       if (l->fixp->fx_next != NULL
10747           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10748           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10749           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10750         continue;
10751
10752       /* Look through the fixups for this segment for a matching %lo.
10753          When we find one, move the %hi just in front of it.  We do
10754          this in two passes.  In the first pass, we try to find a
10755          unique %lo.  In the second pass, we permit multiple %hi
10756          relocs for a single %lo (this is a GNU extension).  */
10757       seginfo = seg_info (l->seg);
10758       for (pass = 0; pass < 2; pass++)
10759         {
10760           fixS *f, *prev;
10761
10762           prev = NULL;
10763           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10764             {
10765               /* Check whether this is a %lo fixup which matches l->fixp.  */
10766               if (f->fx_r_type == BFD_RELOC_LO16
10767                   && f->fx_addsy == l->fixp->fx_addsy
10768                   && f->fx_offset == l->fixp->fx_offset
10769                   && (pass == 1
10770                       || prev == NULL
10771                       || prev->fx_r_type != BFD_RELOC_HI16_S
10772                       || prev->fx_addsy != f->fx_addsy
10773                       || prev->fx_offset !=  f->fx_offset))
10774                 {
10775                   fixS **pf;
10776
10777                   /* Move l->fixp before f.  */
10778                   for (pf = &seginfo->fix_root;
10779                        *pf != l->fixp;
10780                        pf = &(*pf)->fx_next)
10781                     assert (*pf != NULL);
10782
10783                   *pf = l->fixp->fx_next;
10784
10785                   l->fixp->fx_next = f;
10786                   if (prev == NULL)
10787                     seginfo->fix_root = l->fixp;
10788                   else
10789                     prev->fx_next = l->fixp;
10790
10791                   break;
10792                 }
10793
10794               prev = f;
10795             }
10796
10797           if (f != NULL)
10798             break;
10799
10800 #if 0 /* GCC code motion plus incomplete dead code elimination
10801          can leave a %hi without a %lo.  */
10802           if (pass == 1)
10803             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10804                            _("Unmatched %%hi reloc"));
10805 #endif
10806         }
10807     }
10808 }
10809
10810 /* When generating embedded PIC code we need to use a special
10811    relocation to represent the difference of two symbols in the .text
10812    section (switch tables use a difference of this sort).  See
10813    include/coff/mips.h for details.  This macro checks whether this
10814    fixup requires the special reloc.  */
10815 #define SWITCH_TABLE(fixp) \
10816   ((fixp)->fx_r_type == BFD_RELOC_32 \
10817    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10818    && (fixp)->fx_addsy != NULL \
10819    && (fixp)->fx_subsy != NULL \
10820    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10821    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10822
10823 /* When generating embedded PIC code we must keep all PC relative
10824    relocations, in case the linker has to relax a call.  We also need
10825    to keep relocations for switch table entries.
10826
10827    We may have combined relocations without symbols in the N32/N64 ABI.
10828    We have to prevent gas from dropping them.  */
10829
10830 int
10831 mips_force_relocation (fixp)
10832      fixS *fixp;
10833 {
10834   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10835       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10836     return 1;
10837
10838   if (HAVE_NEWABI
10839       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10840       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10841           || fixp->fx_r_type == BFD_RELOC_HI16_S
10842           || fixp->fx_r_type == BFD_RELOC_LO16))
10843     return 1;
10844
10845   return (mips_pic == EMBEDDED_PIC
10846           && (fixp->fx_pcrel
10847               || SWITCH_TABLE (fixp)
10848               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10849               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10850 }
10851
10852 #ifdef OBJ_ELF
10853 static int
10854 mips_need_elf_addend_fixup (fixP)
10855      fixS *fixP;
10856 {
10857   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10858     return 1;
10859   if (mips_pic == EMBEDDED_PIC
10860       && S_IS_WEAK (fixP->fx_addsy))
10861     return 1;
10862   if (mips_pic != EMBEDDED_PIC
10863       && (S_IS_WEAK (fixP->fx_addsy)
10864           || S_IS_EXTERN (fixP->fx_addsy))
10865       && !S_IS_COMMON (fixP->fx_addsy))
10866     return 1;
10867   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10868       && (((bfd_get_section_flags (stdoutput,
10869                                    S_GET_SEGMENT (fixP->fx_addsy))
10870             & SEC_LINK_ONCE) != 0)
10871           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10872                        ".gnu.linkonce",
10873                        sizeof (".gnu.linkonce") - 1)))
10874     return 1;
10875   return 0;
10876 }
10877 #endif
10878
10879 /* Apply a fixup to the object file.  */
10880
10881 void
10882 md_apply_fix3 (fixP, valP, seg)
10883      fixS *fixP;
10884      valueT *valP;
10885      segT seg ATTRIBUTE_UNUSED;
10886 {
10887   bfd_byte *buf;
10888   long insn;
10889   valueT value;
10890
10891   assert (fixP->fx_size == 4
10892           || fixP->fx_r_type == BFD_RELOC_16
10893           || fixP->fx_r_type == BFD_RELOC_32
10894           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10895           || fixP->fx_r_type == BFD_RELOC_HI16_S
10896           || fixP->fx_r_type == BFD_RELOC_LO16
10897           || fixP->fx_r_type == BFD_RELOC_GPREL16
10898           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10899           || fixP->fx_r_type == BFD_RELOC_GPREL32
10900           || fixP->fx_r_type == BFD_RELOC_64
10901           || fixP->fx_r_type == BFD_RELOC_CTOR
10902           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10903           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10904           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10905           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10906           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10907           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10908           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10909           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10910           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
10911
10912   value = *valP;
10913
10914   /* If we aren't adjusting this fixup to be against the section
10915      symbol, we need to adjust the value.  */
10916 #ifdef OBJ_ELF
10917   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10918     {
10919       if (mips_need_elf_addend_fixup (fixP))
10920         {
10921           reloc_howto_type *howto;
10922           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10923
10924           value -= symval;
10925
10926           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10927           if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
10928             {
10929               /* In this case, the bfd_install_relocation routine will
10930                  incorrectly add the symbol value back in.  We just want
10931                  the addend to appear in the object file.  */
10932               value -= symval;
10933
10934               /* Make sure the addend is still non-zero.  If it became zero
10935                  after the last operation, set it to a spurious value and
10936                  subtract the same value from the object file's contents.  */
10937               if (value == 0)
10938                 {
10939                   value = 8;
10940
10941                   /* The in-place addends for LO16 relocations are signed;
10942                      leave the matching HI16 in-place addends as zero.  */
10943                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10944                     {
10945                       bfd_vma contents, mask, field;
10946
10947                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10948                                                + fixP->fx_where,
10949                                                fixP->fx_size * 8,
10950                                                target_big_endian);
10951
10952                       /* MASK has bits set where the relocation should go.
10953                          FIELD is -value, shifted into the appropriate place
10954                          for this relocation.  */
10955                       mask = 1 << (howto->bitsize - 1);
10956                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10957                       field = (-value >> howto->rightshift) << howto->bitpos;
10958
10959                       bfd_put_bits ((field & mask) | (contents & ~mask),
10960                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10961                                     fixP->fx_size * 8,
10962                                     target_big_endian);
10963                     }
10964                 }
10965             }
10966         }
10967
10968       /* This code was generated using trial and error and so is
10969          fragile and not trustworthy.  If you change it, you should
10970          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10971          they still pass.  */
10972       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10973         {
10974           value += fixP->fx_frag->fr_address + fixP->fx_where;
10975
10976           /* BFD's REL handling, for MIPS, is _very_ weird.
10977              This gives the right results, but it can't possibly
10978              be the way things are supposed to work.  */
10979           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10980                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10981               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10982             value += fixP->fx_frag->fr_address + fixP->fx_where;
10983         }
10984     }
10985 #endif
10986
10987   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10988
10989   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10990     fixP->fx_done = 1;
10991
10992   switch (fixP->fx_r_type)
10993     {
10994     case BFD_RELOC_MIPS_JMP:
10995     case BFD_RELOC_MIPS_SHIFT5:
10996     case BFD_RELOC_MIPS_SHIFT6:
10997     case BFD_RELOC_MIPS_GOT_DISP:
10998     case BFD_RELOC_MIPS_GOT_PAGE:
10999     case BFD_RELOC_MIPS_GOT_OFST:
11000     case BFD_RELOC_MIPS_SUB:
11001     case BFD_RELOC_MIPS_INSERT_A:
11002     case BFD_RELOC_MIPS_INSERT_B:
11003     case BFD_RELOC_MIPS_DELETE:
11004     case BFD_RELOC_MIPS_HIGHEST:
11005     case BFD_RELOC_MIPS_HIGHER:
11006     case BFD_RELOC_MIPS_SCN_DISP:
11007     case BFD_RELOC_MIPS_REL16:
11008     case BFD_RELOC_MIPS_RELGOT:
11009     case BFD_RELOC_MIPS_JALR:
11010     case BFD_RELOC_HI16:
11011     case BFD_RELOC_HI16_S:
11012     case BFD_RELOC_GPREL16:
11013     case BFD_RELOC_MIPS_LITERAL:
11014     case BFD_RELOC_MIPS_CALL16:
11015     case BFD_RELOC_MIPS_GOT16:
11016     case BFD_RELOC_GPREL32:
11017     case BFD_RELOC_MIPS_GOT_HI16:
11018     case BFD_RELOC_MIPS_GOT_LO16:
11019     case BFD_RELOC_MIPS_CALL_HI16:
11020     case BFD_RELOC_MIPS_CALL_LO16:
11021     case BFD_RELOC_MIPS16_GPREL:
11022       if (fixP->fx_pcrel)
11023         as_bad_where (fixP->fx_file, fixP->fx_line,
11024                       _("Invalid PC relative reloc"));
11025       /* Nothing needed to do. The value comes from the reloc entry */
11026       break;
11027
11028     case BFD_RELOC_MIPS16_JMP:
11029       /* We currently always generate a reloc against a symbol, which
11030          means that we don't want an addend even if the symbol is
11031          defined.  */
11032       fixP->fx_addnumber = 0;
11033       break;
11034
11035     case BFD_RELOC_PCREL_HI16_S:
11036       /* The addend for this is tricky if it is internal, so we just
11037          do everything here rather than in bfd_install_relocation.  */
11038       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11039           && !fixP->fx_done
11040           && value != 0)
11041         break;
11042       if (fixP->fx_addsy
11043           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11044         {
11045           /* For an external symbol adjust by the address to make it
11046              pcrel_offset.  We use the address of the RELLO reloc
11047              which follows this one.  */
11048           value += (fixP->fx_next->fx_frag->fr_address
11049                     + fixP->fx_next->fx_where);
11050         }
11051       value = ((value + 0x8000) >> 16) & 0xffff;
11052       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11053       if (target_big_endian)
11054         buf += 2;
11055       md_number_to_chars ((char *) buf, value, 2);
11056       break;
11057
11058     case BFD_RELOC_PCREL_LO16:
11059       /* The addend for this is tricky if it is internal, so we just
11060          do everything here rather than in bfd_install_relocation.  */
11061       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11062           && !fixP->fx_done
11063           && value != 0)
11064         break;
11065       if (fixP->fx_addsy
11066           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11067         value += fixP->fx_frag->fr_address + fixP->fx_where;
11068       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11069       if (target_big_endian)
11070         buf += 2;
11071       md_number_to_chars ((char *) buf, value, 2);
11072       break;
11073
11074     case BFD_RELOC_64:
11075       /* This is handled like BFD_RELOC_32, but we output a sign
11076          extended value if we are only 32 bits.  */
11077       if (fixP->fx_done
11078           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11079         {
11080           if (8 <= sizeof (valueT))
11081             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11082                                 value, 8);
11083           else
11084             {
11085               long w1, w2;
11086               long hiv;
11087
11088               w1 = w2 = fixP->fx_where;
11089               if (target_big_endian)
11090                 w1 += 4;
11091               else
11092                 w2 += 4;
11093               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11094               if ((value & 0x80000000) != 0)
11095                 hiv = 0xffffffff;
11096               else
11097                 hiv = 0;
11098               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11099             }
11100         }
11101       break;
11102
11103     case BFD_RELOC_RVA:
11104     case BFD_RELOC_32:
11105       /* If we are deleting this reloc entry, we must fill in the
11106          value now.  This can happen if we have a .word which is not
11107          resolved when it appears but is later defined.  We also need
11108          to fill in the value if this is an embedded PIC switch table
11109          entry.  */
11110       if (fixP->fx_done
11111           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11112         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11113                             value, 4);
11114       break;
11115
11116     case BFD_RELOC_16:
11117       /* If we are deleting this reloc entry, we must fill in the
11118          value now.  */
11119       assert (fixP->fx_size == 2);
11120       if (fixP->fx_done)
11121         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11122                             value, 2);
11123       break;
11124
11125     case BFD_RELOC_LO16:
11126       /* When handling an embedded PIC switch statement, we can wind
11127          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11128       if (fixP->fx_done)
11129         {
11130           if (value + 0x8000 > 0xffff)
11131             as_bad_where (fixP->fx_file, fixP->fx_line,
11132                           _("relocation overflow"));
11133           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11134           if (target_big_endian)
11135             buf += 2;
11136           md_number_to_chars ((char *) buf, value, 2);
11137         }
11138       break;
11139
11140     case BFD_RELOC_16_PCREL_S2:
11141       if ((value & 0x3) != 0)
11142         as_bad_where (fixP->fx_file, fixP->fx_line,
11143                       _("Branch to odd address (%lx)"), (long) value);
11144
11145       /* Fall through.  */
11146
11147     case BFD_RELOC_16_PCREL:
11148       /*
11149        * We need to save the bits in the instruction since fixup_segment()
11150        * might be deleting the relocation entry (i.e., a branch within
11151        * the current segment).
11152        */
11153       if (!fixP->fx_done && value != 0)
11154         break;
11155       /* If 'value' is zero, the remaining reloc code won't actually
11156          do the store, so it must be done here.  This is probably
11157          a bug somewhere.  */
11158       if (!fixP->fx_done
11159           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11160               || fixP->fx_addsy == NULL                 /* ??? */
11161               || ! S_IS_DEFINED (fixP->fx_addsy)))
11162         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11163
11164       value = (offsetT) value >> 2;
11165
11166       /* update old instruction data */
11167       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11168       if (target_big_endian)
11169         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11170       else
11171         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11172
11173       if (value + 0x8000 <= 0xffff)
11174         insn |= value & 0xffff;
11175       else
11176         {
11177           /* The branch offset is too large.  If this is an
11178              unconditional branch, and we are not generating PIC code,
11179              we can convert it to an absolute jump instruction.  */
11180           if (mips_pic == NO_PIC
11181               && fixP->fx_done
11182               && fixP->fx_frag->fr_address >= text_section->vma
11183               && (fixP->fx_frag->fr_address
11184                   < text_section->vma + text_section->_raw_size)
11185               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11186                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11187                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11188             {
11189               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11190                 insn = 0x0c000000;      /* jal */
11191               else
11192                 insn = 0x08000000;      /* j */
11193               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11194               fixP->fx_done = 0;
11195               fixP->fx_addsy = section_symbol (text_section);
11196               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11197             }
11198           else
11199             {
11200               /* FIXME.  It would be possible in principle to handle
11201                  conditional branches which overflow.  They could be
11202                  transformed into a branch around a jump.  This would
11203                  require setting up variant frags for each different
11204                  branch type.  The native MIPS assembler attempts to
11205                  handle these cases, but it appears to do it
11206                  incorrectly.  */
11207               as_bad_where (fixP->fx_file, fixP->fx_line,
11208                             _("Branch out of range"));
11209             }
11210         }
11211
11212       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11213       break;
11214
11215     case BFD_RELOC_VTABLE_INHERIT:
11216       fixP->fx_done = 0;
11217       if (fixP->fx_addsy
11218           && !S_IS_DEFINED (fixP->fx_addsy)
11219           && !S_IS_WEAK (fixP->fx_addsy))
11220         S_SET_WEAK (fixP->fx_addsy);
11221       break;
11222
11223     case BFD_RELOC_VTABLE_ENTRY:
11224       fixP->fx_done = 0;
11225       break;
11226
11227     default:
11228       internalError ();
11229     }
11230 }
11231
11232 #if 0
11233 void
11234 printInsn (oc)
11235      unsigned long oc;
11236 {
11237   const struct mips_opcode *p;
11238   int treg, sreg, dreg, shamt;
11239   short imm;
11240   const char *args;
11241   int i;
11242
11243   for (i = 0; i < NUMOPCODES; ++i)
11244     {
11245       p = &mips_opcodes[i];
11246       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11247         {
11248           printf ("%08lx %s\t", oc, p->name);
11249           treg = (oc >> 16) & 0x1f;
11250           sreg = (oc >> 21) & 0x1f;
11251           dreg = (oc >> 11) & 0x1f;
11252           shamt = (oc >> 6) & 0x1f;
11253           imm = oc;
11254           for (args = p->args;; ++args)
11255             {
11256               switch (*args)
11257                 {
11258                 case '\0':
11259                   printf ("\n");
11260                   break;
11261
11262                 case ',':
11263                 case '(':
11264                 case ')':
11265                   printf ("%c", *args);
11266                   continue;
11267
11268                 case 'r':
11269                   assert (treg == sreg);
11270                   printf ("$%d,$%d", treg, sreg);
11271                   continue;
11272
11273                 case 'd':
11274                 case 'G':
11275                   printf ("$%d", dreg);
11276                   continue;
11277
11278                 case 't':
11279                 case 'E':
11280                   printf ("$%d", treg);
11281                   continue;
11282
11283                 case 'k':
11284                   printf ("0x%x", treg);
11285                   continue;
11286
11287                 case 'b':
11288                 case 's':
11289                   printf ("$%d", sreg);
11290                   continue;
11291
11292                 case 'a':
11293                   printf ("0x%08lx", oc & 0x1ffffff);
11294                   continue;
11295
11296                 case 'i':
11297                 case 'j':
11298                 case 'o':
11299                 case 'u':
11300                   printf ("%d", imm);
11301                   continue;
11302
11303                 case '<':
11304                 case '>':
11305                   printf ("$%d", shamt);
11306                   continue;
11307
11308                 default:
11309                   internalError ();
11310                 }
11311               break;
11312             }
11313           return;
11314         }
11315     }
11316   printf (_("%08lx  UNDEFINED\n"), oc);
11317 }
11318 #endif
11319
11320 static symbolS *
11321 get_symbol ()
11322 {
11323   int c;
11324   char *name;
11325   symbolS *p;
11326
11327   name = input_line_pointer;
11328   c = get_symbol_end ();
11329   p = (symbolS *) symbol_find_or_make (name);
11330   *input_line_pointer = c;
11331   return p;
11332 }
11333
11334 /* Align the current frag to a given power of two.  The MIPS assembler
11335    also automatically adjusts any preceding label.  */
11336
11337 static void
11338 mips_align (to, fill, label)
11339      int to;
11340      int fill;
11341      symbolS *label;
11342 {
11343   mips_emit_delays (false);
11344   frag_align (to, fill, 0);
11345   record_alignment (now_seg, to);
11346   if (label != NULL)
11347     {
11348       assert (S_GET_SEGMENT (label) == now_seg);
11349       symbol_set_frag (label, frag_now);
11350       S_SET_VALUE (label, (valueT) frag_now_fix ());
11351     }
11352 }
11353
11354 /* Align to a given power of two.  .align 0 turns off the automatic
11355    alignment used by the data creating pseudo-ops.  */
11356
11357 static void
11358 s_align (x)
11359      int x ATTRIBUTE_UNUSED;
11360 {
11361   register int temp;
11362   register long temp_fill;
11363   long max_alignment = 15;
11364
11365   /*
11366
11367     o  Note that the assembler pulls down any immediately preceeding label
11368        to the aligned address.
11369     o  It's not documented but auto alignment is reinstated by
11370        a .align pseudo instruction.
11371     o  Note also that after auto alignment is turned off the mips assembler
11372        issues an error on attempt to assemble an improperly aligned data item.
11373        We don't.
11374
11375     */
11376
11377   temp = get_absolute_expression ();
11378   if (temp > max_alignment)
11379     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11380   else if (temp < 0)
11381     {
11382       as_warn (_("Alignment negative: 0 assumed."));
11383       temp = 0;
11384     }
11385   if (*input_line_pointer == ',')
11386     {
11387       ++input_line_pointer;
11388       temp_fill = get_absolute_expression ();
11389     }
11390   else
11391     temp_fill = 0;
11392   if (temp)
11393     {
11394       auto_align = 1;
11395       mips_align (temp, (int) temp_fill,
11396                   insn_labels != NULL ? insn_labels->label : NULL);
11397     }
11398   else
11399     {
11400       auto_align = 0;
11401     }
11402
11403   demand_empty_rest_of_line ();
11404 }
11405
11406 void
11407 mips_flush_pending_output ()
11408 {
11409   mips_emit_delays (false);
11410   mips_clear_insn_labels ();
11411 }
11412
11413 static void
11414 s_change_sec (sec)
11415      int sec;
11416 {
11417   segT seg;
11418
11419   /* When generating embedded PIC code, we only use the .text, .lit8,
11420      .sdata and .sbss sections.  We change the .data and .rdata
11421      pseudo-ops to use .sdata.  */
11422   if (mips_pic == EMBEDDED_PIC
11423       && (sec == 'd' || sec == 'r'))
11424     sec = 's';
11425
11426 #ifdef OBJ_ELF
11427   /* The ELF backend needs to know that we are changing sections, so
11428      that .previous works correctly.  We could do something like check
11429      for an obj_section_change_hook macro, but that might be confusing
11430      as it would not be appropriate to use it in the section changing
11431      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11432      This should be cleaner, somehow.  */
11433   obj_elf_section_change_hook ();
11434 #endif
11435
11436   mips_emit_delays (false);
11437   switch (sec)
11438     {
11439     case 't':
11440       s_text (0);
11441       break;
11442     case 'd':
11443       s_data (0);
11444       break;
11445     case 'b':
11446       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11447       demand_empty_rest_of_line ();
11448       break;
11449
11450     case 'r':
11451       if (USE_GLOBAL_POINTER_OPT)
11452         {
11453           seg = subseg_new (RDATA_SECTION_NAME,
11454                             (subsegT) get_absolute_expression ());
11455           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11456             {
11457               bfd_set_section_flags (stdoutput, seg,
11458                                      (SEC_ALLOC
11459                                       | SEC_LOAD
11460                                       | SEC_READONLY
11461                                       | SEC_RELOC
11462                                       | SEC_DATA));
11463               if (strcmp (TARGET_OS, "elf") != 0)
11464                 record_alignment (seg, 4);
11465             }
11466           demand_empty_rest_of_line ();
11467         }
11468       else
11469         {
11470           as_bad (_("No read only data section in this object file format"));
11471           demand_empty_rest_of_line ();
11472           return;
11473         }
11474       break;
11475
11476     case 's':
11477       if (USE_GLOBAL_POINTER_OPT)
11478         {
11479           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11480           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11481             {
11482               bfd_set_section_flags (stdoutput, seg,
11483                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11484                                      | SEC_DATA);
11485               if (strcmp (TARGET_OS, "elf") != 0)
11486                 record_alignment (seg, 4);
11487             }
11488           demand_empty_rest_of_line ();
11489           break;
11490         }
11491       else
11492         {
11493           as_bad (_("Global pointers not supported; recompile -G 0"));
11494           demand_empty_rest_of_line ();
11495           return;
11496         }
11497     }
11498
11499   auto_align = 1;
11500 }
11501
11502 void
11503 mips_enable_auto_align ()
11504 {
11505   auto_align = 1;
11506 }
11507
11508 static void
11509 s_cons (log_size)
11510      int log_size;
11511 {
11512   symbolS *label;
11513
11514   label = insn_labels != NULL ? insn_labels->label : NULL;
11515   mips_emit_delays (false);
11516   if (log_size > 0 && auto_align)
11517     mips_align (log_size, 0, label);
11518   mips_clear_insn_labels ();
11519   cons (1 << log_size);
11520 }
11521
11522 static void
11523 s_float_cons (type)
11524      int type;
11525 {
11526   symbolS *label;
11527
11528   label = insn_labels != NULL ? insn_labels->label : NULL;
11529
11530   mips_emit_delays (false);
11531
11532   if (auto_align)
11533     {
11534       if (type == 'd')
11535         mips_align (3, 0, label);
11536       else
11537         mips_align (2, 0, label);
11538     }
11539
11540   mips_clear_insn_labels ();
11541
11542   float_cons (type);
11543 }
11544
11545 /* Handle .globl.  We need to override it because on Irix 5 you are
11546    permitted to say
11547        .globl foo .text
11548    where foo is an undefined symbol, to mean that foo should be
11549    considered to be the address of a function.  */
11550
11551 static void
11552 s_mips_globl (x)
11553      int x ATTRIBUTE_UNUSED;
11554 {
11555   char *name;
11556   int c;
11557   symbolS *symbolP;
11558   flagword flag;
11559
11560   name = input_line_pointer;
11561   c = get_symbol_end ();
11562   symbolP = symbol_find_or_make (name);
11563   *input_line_pointer = c;
11564   SKIP_WHITESPACE ();
11565
11566   /* On Irix 5, every global symbol that is not explicitly labelled as
11567      being a function is apparently labelled as being an object.  */
11568   flag = BSF_OBJECT;
11569
11570   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11571     {
11572       char *secname;
11573       asection *sec;
11574
11575       secname = input_line_pointer;
11576       c = get_symbol_end ();
11577       sec = bfd_get_section_by_name (stdoutput, secname);
11578       if (sec == NULL)
11579         as_bad (_("%s: no such section"), secname);
11580       *input_line_pointer = c;
11581
11582       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11583         flag = BSF_FUNCTION;
11584     }
11585
11586   symbol_get_bfdsym (symbolP)->flags |= flag;
11587
11588   S_SET_EXTERNAL (symbolP);
11589   demand_empty_rest_of_line ();
11590 }
11591
11592 static void
11593 s_option (x)
11594      int x ATTRIBUTE_UNUSED;
11595 {
11596   char *opt;
11597   char c;
11598
11599   opt = input_line_pointer;
11600   c = get_symbol_end ();
11601
11602   if (*opt == 'O')
11603     {
11604       /* FIXME: What does this mean?  */
11605     }
11606   else if (strncmp (opt, "pic", 3) == 0)
11607     {
11608       int i;
11609
11610       i = atoi (opt + 3);
11611       if (i == 0)
11612         mips_pic = NO_PIC;
11613       else if (i == 2)
11614         mips_pic = SVR4_PIC;
11615       else
11616         as_bad (_(".option pic%d not supported"), i);
11617
11618       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11619         {
11620           if (g_switch_seen && g_switch_value != 0)
11621             as_warn (_("-G may not be used with SVR4 PIC code"));
11622           g_switch_value = 0;
11623           bfd_set_gp_size (stdoutput, 0);
11624         }
11625     }
11626   else
11627     as_warn (_("Unrecognized option \"%s\""), opt);
11628
11629   *input_line_pointer = c;
11630   demand_empty_rest_of_line ();
11631 }
11632
11633 /* This structure is used to hold a stack of .set values.  */
11634
11635 struct mips_option_stack
11636 {
11637   struct mips_option_stack *next;
11638   struct mips_set_options options;
11639 };
11640
11641 static struct mips_option_stack *mips_opts_stack;
11642
11643 /* Handle the .set pseudo-op.  */
11644
11645 static void
11646 s_mipsset (x)
11647      int x ATTRIBUTE_UNUSED;
11648 {
11649   char *name = input_line_pointer, ch;
11650
11651   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11652     ++input_line_pointer;
11653   ch = *input_line_pointer;
11654   *input_line_pointer = '\0';
11655
11656   if (strcmp (name, "reorder") == 0)
11657     {
11658       if (mips_opts.noreorder && prev_nop_frag != NULL)
11659         {
11660           /* If we still have pending nops, we can discard them.  The
11661              usual nop handling will insert any that are still
11662              needed.  */
11663           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11664                                     * (mips_opts.mips16 ? 2 : 4));
11665           prev_nop_frag = NULL;
11666         }
11667       mips_opts.noreorder = 0;
11668     }
11669   else if (strcmp (name, "noreorder") == 0)
11670     {
11671       mips_emit_delays (true);
11672       mips_opts.noreorder = 1;
11673       mips_any_noreorder = 1;
11674     }
11675   else if (strcmp (name, "at") == 0)
11676     {
11677       mips_opts.noat = 0;
11678     }
11679   else if (strcmp (name, "noat") == 0)
11680     {
11681       mips_opts.noat = 1;
11682     }
11683   else if (strcmp (name, "macro") == 0)
11684     {
11685       mips_opts.warn_about_macros = 0;
11686     }
11687   else if (strcmp (name, "nomacro") == 0)
11688     {
11689       if (mips_opts.noreorder == 0)
11690         as_bad (_("`noreorder' must be set before `nomacro'"));
11691       mips_opts.warn_about_macros = 1;
11692     }
11693   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11694     {
11695       mips_opts.nomove = 0;
11696     }
11697   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11698     {
11699       mips_opts.nomove = 1;
11700     }
11701   else if (strcmp (name, "bopt") == 0)
11702     {
11703       mips_opts.nobopt = 0;
11704     }
11705   else if (strcmp (name, "nobopt") == 0)
11706     {
11707       mips_opts.nobopt = 1;
11708     }
11709   else if (strcmp (name, "mips16") == 0
11710            || strcmp (name, "MIPS-16") == 0)
11711     mips_opts.mips16 = 1;
11712   else if (strcmp (name, "nomips16") == 0
11713            || strcmp (name, "noMIPS-16") == 0)
11714     mips_opts.mips16 = 0;
11715   else if (strcmp (name, "mips3d") == 0)
11716     mips_opts.ase_mips3d = 1;
11717   else if (strcmp (name, "nomips3d") == 0)
11718     mips_opts.ase_mips3d = 0;
11719   else if (strcmp (name, "mdmx") == 0)
11720     mips_opts.ase_mdmx = 1;
11721   else if (strcmp (name, "nomdmx") == 0)
11722     mips_opts.ase_mdmx = 0;
11723   else if (strncmp (name, "mips", 4) == 0)
11724     {
11725       int isa;
11726
11727       /* Permit the user to change the ISA on the fly.  Needless to
11728          say, misuse can cause serious problems.  */
11729       isa = atoi (name + 4);
11730       switch (isa)
11731         {
11732         case  0:
11733           mips_opts.gp32 = file_mips_gp32;
11734           mips_opts.fp32 = file_mips_fp32;
11735           mips_opts.abi = file_mips_abi;
11736           break;
11737         case  1:
11738         case  2:
11739         case 32:
11740           mips_opts.gp32 = 1;
11741           mips_opts.fp32 = 1;
11742           break;
11743         case  3:
11744         case  4:
11745         case  5:
11746         case 64:
11747           /* Loosen ABI register width restriction.  */
11748           if (mips_opts.abi == O32_ABI)
11749             mips_opts.abi = NO_ABI;
11750           mips_opts.gp32 = 0;
11751           mips_opts.fp32 = 0;
11752           break;
11753         default:
11754           as_bad (_("unknown ISA level %s"), name + 4);
11755           break;
11756         }
11757
11758       switch (isa)
11759         {
11760         case  0: mips_opts.isa = file_mips_isa;   break;
11761         case  1: mips_opts.isa = ISA_MIPS1;       break;
11762         case  2: mips_opts.isa = ISA_MIPS2;       break;
11763         case  3: mips_opts.isa = ISA_MIPS3;       break;
11764         case  4: mips_opts.isa = ISA_MIPS4;       break;
11765         case  5: mips_opts.isa = ISA_MIPS5;       break;
11766         case 32: mips_opts.isa = ISA_MIPS32;      break;
11767         case 64: mips_opts.isa = ISA_MIPS64;      break;
11768         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11769         }
11770     }
11771   else if (strcmp (name, "autoextend") == 0)
11772     mips_opts.noautoextend = 0;
11773   else if (strcmp (name, "noautoextend") == 0)
11774     mips_opts.noautoextend = 1;
11775   else if (strcmp (name, "push") == 0)
11776     {
11777       struct mips_option_stack *s;
11778
11779       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11780       s->next = mips_opts_stack;
11781       s->options = mips_opts;
11782       mips_opts_stack = s;
11783     }
11784   else if (strcmp (name, "pop") == 0)
11785     {
11786       struct mips_option_stack *s;
11787
11788       s = mips_opts_stack;
11789       if (s == NULL)
11790         as_bad (_(".set pop with no .set push"));
11791       else
11792         {
11793           /* If we're changing the reorder mode we need to handle
11794              delay slots correctly.  */
11795           if (s->options.noreorder && ! mips_opts.noreorder)
11796             mips_emit_delays (true);
11797           else if (! s->options.noreorder && mips_opts.noreorder)
11798             {
11799               if (prev_nop_frag != NULL)
11800                 {
11801                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11802                                             * (mips_opts.mips16 ? 2 : 4));
11803                   prev_nop_frag = NULL;
11804                 }
11805             }
11806
11807           mips_opts = s->options;
11808           mips_opts_stack = s->next;
11809           free (s);
11810         }
11811     }
11812   else
11813     {
11814       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11815     }
11816   *input_line_pointer = ch;
11817   demand_empty_rest_of_line ();
11818 }
11819
11820 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11821    .option pic2.  It means to generate SVR4 PIC calls.  */
11822
11823 static void
11824 s_abicalls (ignore)
11825      int ignore ATTRIBUTE_UNUSED;
11826 {
11827   mips_pic = SVR4_PIC;
11828   if (USE_GLOBAL_POINTER_OPT)
11829     {
11830       if (g_switch_seen && g_switch_value != 0)
11831         as_warn (_("-G may not be used with SVR4 PIC code"));
11832       g_switch_value = 0;
11833     }
11834   bfd_set_gp_size (stdoutput, 0);
11835   demand_empty_rest_of_line ();
11836 }
11837
11838 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11839    PIC code.  It sets the $gp register for the function based on the
11840    function address, which is in the register named in the argument.
11841    This uses a relocation against _gp_disp, which is handled specially
11842    by the linker.  The result is:
11843         lui     $gp,%hi(_gp_disp)
11844         addiu   $gp,$gp,%lo(_gp_disp)
11845         addu    $gp,$gp,.cpload argument
11846    The .cpload argument is normally $25 == $t9.  */
11847
11848 static void
11849 s_cpload (ignore)
11850      int ignore ATTRIBUTE_UNUSED;
11851 {
11852   expressionS ex;
11853   int icnt = 0;
11854
11855   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11856      .cpload is ignored.  */
11857   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11858     {
11859       s_ignore (0);
11860       return;
11861     }
11862
11863   /* .cpload should be in a .set noreorder section.  */
11864   if (mips_opts.noreorder == 0)
11865     as_warn (_(".cpload not in noreorder section"));
11866
11867   ex.X_op = O_symbol;
11868   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11869   ex.X_op_symbol = NULL;
11870   ex.X_add_number = 0;
11871
11872   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11873   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11874
11875   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11876   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11877                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
11878
11879   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11880                mips_gp_register, mips_gp_register, tc_get_register (0));
11881
11882   demand_empty_rest_of_line ();
11883 }
11884
11885 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11886      .cpsetup $reg1, offset|$reg2, label
11887
11888    If offset is given, this results in:
11889      sd         $gp, offset($sp)
11890      lui        $gp, %hi(%neg(%gp_rel(label)))
11891      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11892      daddu      $gp, $gp, $reg1
11893
11894    If $reg2 is given, this results in:
11895      daddu      $reg2, $gp, $0
11896      lui        $gp, %hi(%neg(%gp_rel(label)))
11897      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
11898      daddu      $gp, $gp, $reg1
11899    $reg1 is normally $25 == $t9.  */
11900 static void
11901 s_cpsetup (ignore)
11902      int ignore ATTRIBUTE_UNUSED;
11903 {
11904   expressionS ex_off;
11905   expressionS ex_sym;
11906   int reg1;
11907   int icnt = 0;
11908   char *sym;
11909
11910   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11911      We also need NewABI support.  */
11912   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11913     {
11914       s_ignore (0);
11915       return;
11916     }
11917
11918   reg1 = tc_get_register (0);
11919   SKIP_WHITESPACE ();
11920   if (*input_line_pointer != ',')
11921     {
11922       as_bad (_("missing argument separator ',' for .cpsetup"));
11923       return;
11924     }
11925   else
11926     ++input_line_pointer;
11927   SKIP_WHITESPACE ();
11928   if (*input_line_pointer == '$')
11929     {
11930       mips_cpreturn_register = tc_get_register (0);
11931       mips_cpreturn_offset = -1;
11932     }
11933   else
11934     {
11935       mips_cpreturn_offset = get_absolute_expression ();
11936       mips_cpreturn_register = -1;
11937     }
11938   SKIP_WHITESPACE ();
11939   if (*input_line_pointer != ',')
11940     {
11941       as_bad (_("missing argument separator ',' for .cpsetup"));
11942       return;
11943     }
11944   else
11945     ++input_line_pointer;
11946   SKIP_WHITESPACE ();
11947   sym = input_line_pointer;
11948   while (ISALNUM (*input_line_pointer))
11949     ++input_line_pointer;
11950   *input_line_pointer = 0;
11951
11952   ex_sym.X_op = O_symbol;
11953   ex_sym.X_add_symbol = symbol_find_or_make (sym);
11954   ex_sym.X_op_symbol = NULL;
11955   ex_sym.X_add_number = 0;
11956
11957   if (mips_cpreturn_register == -1)
11958     {
11959       ex_off.X_op = O_constant;
11960       ex_off.X_add_symbol = NULL;
11961       ex_off.X_op_symbol = NULL;
11962       ex_off.X_add_number = mips_cpreturn_offset;
11963
11964       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11965                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11966     }
11967   else
11968     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11969                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11970
11971   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11972                (int) BFD_RELOC_GPREL16);
11973   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11974   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_HI16_S);
11975   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11976                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11977   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11978   fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_LO16);
11979   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11980                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11981                mips_gp_register, mips_gp_register, reg1);
11982
11983   demand_empty_rest_of_line ();
11984 }
11985
11986 static void
11987 s_cplocal (ignore)
11988      int ignore ATTRIBUTE_UNUSED;
11989 {
11990   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11991    .cplocal is ignored.  */
11992   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11993     {
11994       s_ignore (0);
11995       return;
11996     }
11997
11998   mips_gp_register = tc_get_register (0);
11999   demand_empty_rest_of_line ();
12000 }
12001
12002 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12003    offset from $sp.  The offset is remembered, and after making a PIC
12004    call $gp is restored from that location.  */
12005
12006 static void
12007 s_cprestore (ignore)
12008      int ignore ATTRIBUTE_UNUSED;
12009 {
12010   expressionS ex;
12011   int icnt = 0;
12012
12013   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12014      .cprestore is ignored.  */
12015   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12016     {
12017       s_ignore (0);
12018       return;
12019     }
12020
12021   mips_cprestore_offset = get_absolute_expression ();
12022   mips_cprestore_valid = 1;
12023
12024   ex.X_op = O_constant;
12025   ex.X_add_symbol = NULL;
12026   ex.X_op_symbol = NULL;
12027   ex.X_add_number = mips_cprestore_offset;
12028
12029   macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12030                "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
12031
12032   demand_empty_rest_of_line ();
12033 }
12034
12035 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12036    was given in the preceeding .gpsetup, it results in:
12037      ld         $gp, offset($sp)
12038
12039    If a register $reg2 was given there, it results in:
12040      daddiu     $gp, $gp, $reg2
12041  */
12042 static void
12043 s_cpreturn (ignore)
12044      int ignore ATTRIBUTE_UNUSED;
12045 {
12046   expressionS ex;
12047   int icnt = 0;
12048
12049   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12050      We also need NewABI support.  */
12051   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12052     {
12053       s_ignore (0);
12054       return;
12055     }
12056
12057   if (mips_cpreturn_register == -1)
12058     {
12059       ex.X_op = O_constant;
12060       ex.X_add_symbol = NULL;
12061       ex.X_op_symbol = NULL;
12062       ex.X_add_number = mips_cpreturn_offset;
12063
12064       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12065                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12066     }
12067   else
12068     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12069                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12070
12071   demand_empty_rest_of_line ();
12072 }
12073
12074 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12075    code.  It sets the offset to use in gp_rel relocations.  */
12076
12077 static void
12078 s_gpvalue (ignore)
12079      int ignore ATTRIBUTE_UNUSED;
12080 {
12081   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12082      We also need NewABI support.  */
12083   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12084     {
12085       s_ignore (0);
12086       return;
12087     }
12088
12089   mips_gprel_offset = get_absolute_expression ();
12090
12091   demand_empty_rest_of_line ();
12092 }
12093
12094 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12095    code.  It generates a 32 bit GP relative reloc.  */
12096
12097 static void
12098 s_gpword (ignore)
12099      int ignore ATTRIBUTE_UNUSED;
12100 {
12101   symbolS *label;
12102   expressionS ex;
12103   char *p;
12104
12105   /* When not generating PIC code, this is treated as .word.  */
12106   if (mips_pic != SVR4_PIC)
12107     {
12108       s_cons (2);
12109       return;
12110     }
12111
12112   label = insn_labels != NULL ? insn_labels->label : NULL;
12113   mips_emit_delays (true);
12114   if (auto_align)
12115     mips_align (2, 0, label);
12116   mips_clear_insn_labels ();
12117
12118   expression (&ex);
12119
12120   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12121     {
12122       as_bad (_("Unsupported use of .gpword"));
12123       ignore_rest_of_line ();
12124     }
12125
12126   p = frag_more (4);
12127   md_number_to_chars (p, (valueT) 0, 4);
12128   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12129                BFD_RELOC_GPREL32);
12130
12131   demand_empty_rest_of_line ();
12132 }
12133
12134 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12135    tables in SVR4 PIC code.  */
12136
12137 static void
12138 s_cpadd (ignore)
12139      int ignore ATTRIBUTE_UNUSED;
12140 {
12141   int icnt = 0;
12142   int reg;
12143
12144   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12145      code.  */
12146   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12147     {
12148       s_ignore (0);
12149       return;
12150     }
12151
12152   /* Add $gp to the register named as an argument.  */
12153   reg = tc_get_register (0);
12154   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12155                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12156                "d,v,t", reg, reg, mips_gp_register);
12157
12158   demand_empty_rest_of_line ();
12159 }
12160
12161 /* Handle the .insn pseudo-op.  This marks instruction labels in
12162    mips16 mode.  This permits the linker to handle them specially,
12163    such as generating jalx instructions when needed.  We also make
12164    them odd for the duration of the assembly, in order to generate the
12165    right sort of code.  We will make them even in the adjust_symtab
12166    routine, while leaving them marked.  This is convenient for the
12167    debugger and the disassembler.  The linker knows to make them odd
12168    again.  */
12169
12170 static void
12171 s_insn (ignore)
12172      int ignore ATTRIBUTE_UNUSED;
12173 {
12174   mips16_mark_labels ();
12175
12176   demand_empty_rest_of_line ();
12177 }
12178
12179 /* Handle a .stabn directive.  We need these in order to mark a label
12180    as being a mips16 text label correctly.  Sometimes the compiler
12181    will emit a label, followed by a .stabn, and then switch sections.
12182    If the label and .stabn are in mips16 mode, then the label is
12183    really a mips16 text label.  */
12184
12185 static void
12186 s_mips_stab (type)
12187      int type;
12188 {
12189   if (type == 'n')
12190     mips16_mark_labels ();
12191
12192   s_stab (type);
12193 }
12194
12195 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12196  */
12197
12198 static void
12199 s_mips_weakext (ignore)
12200      int ignore ATTRIBUTE_UNUSED;
12201 {
12202   char *name;
12203   int c;
12204   symbolS *symbolP;
12205   expressionS exp;
12206
12207   name = input_line_pointer;
12208   c = get_symbol_end ();
12209   symbolP = symbol_find_or_make (name);
12210   S_SET_WEAK (symbolP);
12211   *input_line_pointer = c;
12212
12213   SKIP_WHITESPACE ();
12214
12215   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12216     {
12217       if (S_IS_DEFINED (symbolP))
12218         {
12219           as_bad ("ignoring attempt to redefine symbol %s",
12220                   S_GET_NAME (symbolP));
12221           ignore_rest_of_line ();
12222           return;
12223         }
12224
12225       if (*input_line_pointer == ',')
12226         {
12227           ++input_line_pointer;
12228           SKIP_WHITESPACE ();
12229         }
12230
12231       expression (&exp);
12232       if (exp.X_op != O_symbol)
12233         {
12234           as_bad ("bad .weakext directive");
12235           ignore_rest_of_line ();
12236           return;
12237         }
12238       symbol_set_value_expression (symbolP, &exp);
12239     }
12240
12241   demand_empty_rest_of_line ();
12242 }
12243
12244 /* Parse a register string into a number.  Called from the ECOFF code
12245    to parse .frame.  The argument is non-zero if this is the frame
12246    register, so that we can record it in mips_frame_reg.  */
12247
12248 int
12249 tc_get_register (frame)
12250      int frame;
12251 {
12252   int reg;
12253
12254   SKIP_WHITESPACE ();
12255   if (*input_line_pointer++ != '$')
12256     {
12257       as_warn (_("expected `$'"));
12258       reg = ZERO;
12259     }
12260   else if (ISDIGIT (*input_line_pointer))
12261     {
12262       reg = get_absolute_expression ();
12263       if (reg < 0 || reg >= 32)
12264         {
12265           as_warn (_("Bad register number"));
12266           reg = ZERO;
12267         }
12268     }
12269   else
12270     {
12271       if (strncmp (input_line_pointer, "ra", 2) == 0)
12272         {
12273           reg = RA;
12274           input_line_pointer += 2;
12275         }
12276       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12277         {
12278           reg = FP;
12279           input_line_pointer += 2;
12280         }
12281       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12282         {
12283           reg = SP;
12284           input_line_pointer += 2;
12285         }
12286       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12287         {
12288           reg = GP;
12289           input_line_pointer += 2;
12290         }
12291       else if (strncmp (input_line_pointer, "at", 2) == 0)
12292         {
12293           reg = AT;
12294           input_line_pointer += 2;
12295         }
12296       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12297         {
12298           reg = KT0;
12299           input_line_pointer += 3;
12300         }
12301       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12302         {
12303           reg = KT1;
12304           input_line_pointer += 3;
12305         }
12306       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12307         {
12308           reg = ZERO;
12309           input_line_pointer += 4;
12310         }
12311       else
12312         {
12313           as_warn (_("Unrecognized register name"));
12314           reg = ZERO;
12315           while (ISALNUM(*input_line_pointer))
12316            input_line_pointer++;
12317         }
12318     }
12319   if (frame)
12320     {
12321       mips_frame_reg = reg != 0 ? reg : SP;
12322       mips_frame_reg_valid = 1;
12323       mips_cprestore_valid = 0;
12324     }
12325   return reg;
12326 }
12327
12328 valueT
12329 md_section_align (seg, addr)
12330      asection *seg;
12331      valueT addr;
12332 {
12333   int align = bfd_get_section_alignment (stdoutput, seg);
12334
12335 #ifdef OBJ_ELF
12336   /* We don't need to align ELF sections to the full alignment.
12337      However, Irix 5 may prefer that we align them at least to a 16
12338      byte boundary.  We don't bother to align the sections if we are
12339      targeted for an embedded system.  */
12340   if (strcmp (TARGET_OS, "elf") == 0)
12341     return addr;
12342   if (align > 4)
12343     align = 4;
12344 #endif
12345
12346   return ((addr + (1 << align) - 1) & (-1 << align));
12347 }
12348
12349 /* Utility routine, called from above as well.  If called while the
12350    input file is still being read, it's only an approximation.  (For
12351    example, a symbol may later become defined which appeared to be
12352    undefined earlier.)  */
12353
12354 static int
12355 nopic_need_relax (sym, before_relaxing)
12356      symbolS *sym;
12357      int before_relaxing;
12358 {
12359   if (sym == 0)
12360     return 0;
12361
12362   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12363     {
12364       const char *symname;
12365       int change;
12366
12367       /* Find out whether this symbol can be referenced off the $gp
12368          register.  It can be if it is smaller than the -G size or if
12369          it is in the .sdata or .sbss section.  Certain symbols can
12370          not be referenced off the $gp, although it appears as though
12371          they can.  */
12372       symname = S_GET_NAME (sym);
12373       if (symname != (const char *) NULL
12374           && (strcmp (symname, "eprol") == 0
12375               || strcmp (symname, "etext") == 0
12376               || strcmp (symname, "_gp") == 0
12377               || strcmp (symname, "edata") == 0
12378               || strcmp (symname, "_fbss") == 0
12379               || strcmp (symname, "_fdata") == 0
12380               || strcmp (symname, "_ftext") == 0
12381               || strcmp (symname, "end") == 0
12382               || strcmp (symname, "_gp_disp") == 0))
12383         change = 1;
12384       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12385                && (0
12386 #ifndef NO_ECOFF_DEBUGGING
12387                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12388                        && (symbol_get_obj (sym)->ecoff_extern_size
12389                            <= g_switch_value))
12390 #endif
12391                    /* We must defer this decision until after the whole
12392                       file has been read, since there might be a .extern
12393                       after the first use of this symbol.  */
12394                    || (before_relaxing
12395 #ifndef NO_ECOFF_DEBUGGING
12396                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12397 #endif
12398                        && S_GET_VALUE (sym) == 0)
12399                    || (S_GET_VALUE (sym) != 0
12400                        && S_GET_VALUE (sym) <= g_switch_value)))
12401         change = 0;
12402       else
12403         {
12404           const char *segname;
12405
12406           segname = segment_name (S_GET_SEGMENT (sym));
12407           assert (strcmp (segname, ".lit8") != 0
12408                   && strcmp (segname, ".lit4") != 0);
12409           change = (strcmp (segname, ".sdata") != 0
12410                     && strcmp (segname, ".sbss") != 0
12411                     && strncmp (segname, ".sdata.", 7) != 0
12412                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12413         }
12414       return change;
12415     }
12416   else
12417     /* We are not optimizing for the $gp register.  */
12418     return 1;
12419 }
12420
12421 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12422    extended opcode.  SEC is the section the frag is in.  */
12423
12424 static int
12425 mips16_extended_frag (fragp, sec, stretch)
12426      fragS *fragp;
12427      asection *sec;
12428      long stretch;
12429 {
12430   int type;
12431   register const struct mips16_immed_operand *op;
12432   offsetT val;
12433   int mintiny, maxtiny;
12434   segT symsec;
12435   fragS *sym_frag;
12436
12437   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12438     return 0;
12439   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12440     return 1;
12441
12442   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12443   op = mips16_immed_operands;
12444   while (op->type != type)
12445     {
12446       ++op;
12447       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12448     }
12449
12450   if (op->unsp)
12451     {
12452       if (type == '<' || type == '>' || type == '[' || type == ']')
12453         {
12454           mintiny = 1;
12455           maxtiny = 1 << op->nbits;
12456         }
12457       else
12458         {
12459           mintiny = 0;
12460           maxtiny = (1 << op->nbits) - 1;
12461         }
12462     }
12463   else
12464     {
12465       mintiny = - (1 << (op->nbits - 1));
12466       maxtiny = (1 << (op->nbits - 1)) - 1;
12467     }
12468
12469   sym_frag = symbol_get_frag (fragp->fr_symbol);
12470   val = S_GET_VALUE (fragp->fr_symbol);
12471   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12472
12473   if (op->pcrel)
12474     {
12475       addressT addr;
12476
12477       /* We won't have the section when we are called from
12478          mips_relax_frag.  However, we will always have been called
12479          from md_estimate_size_before_relax first.  If this is a
12480          branch to a different section, we mark it as such.  If SEC is
12481          NULL, and the frag is not marked, then it must be a branch to
12482          the same section.  */
12483       if (sec == NULL)
12484         {
12485           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12486             return 1;
12487         }
12488       else
12489         {
12490           /* Must have been called from md_estimate_size_before_relax.  */
12491           if (symsec != sec)
12492             {
12493               fragp->fr_subtype =
12494                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12495
12496               /* FIXME: We should support this, and let the linker
12497                  catch branches and loads that are out of range.  */
12498               as_bad_where (fragp->fr_file, fragp->fr_line,
12499                             _("unsupported PC relative reference to different section"));
12500
12501               return 1;
12502             }
12503           if (fragp != sym_frag && sym_frag->fr_address == 0)
12504             /* Assume non-extended on the first relaxation pass.
12505                The address we have calculated will be bogus if this is
12506                a forward branch to another frag, as the forward frag
12507                will have fr_address == 0.  */
12508             return 0;
12509         }
12510
12511       /* In this case, we know for sure that the symbol fragment is in
12512          the same section.  If the relax_marker of the symbol fragment
12513          differs from the relax_marker of this fragment, we have not
12514          yet adjusted the symbol fragment fr_address.  We want to add
12515          in STRETCH in order to get a better estimate of the address.
12516          This particularly matters because of the shift bits.  */
12517       if (stretch != 0
12518           && sym_frag->relax_marker != fragp->relax_marker)
12519         {
12520           fragS *f;
12521
12522           /* Adjust stretch for any alignment frag.  Note that if have
12523              been expanding the earlier code, the symbol may be
12524              defined in what appears to be an earlier frag.  FIXME:
12525              This doesn't handle the fr_subtype field, which specifies
12526              a maximum number of bytes to skip when doing an
12527              alignment.  */
12528           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12529             {
12530               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12531                 {
12532                   if (stretch < 0)
12533                     stretch = - ((- stretch)
12534                                  & ~ ((1 << (int) f->fr_offset) - 1));
12535                   else
12536                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12537                   if (stretch == 0)
12538                     break;
12539                 }
12540             }
12541           if (f != NULL)
12542             val += stretch;
12543         }
12544
12545       addr = fragp->fr_address + fragp->fr_fix;
12546
12547       /* The base address rules are complicated.  The base address of
12548          a branch is the following instruction.  The base address of a
12549          PC relative load or add is the instruction itself, but if it
12550          is in a delay slot (in which case it can not be extended) use
12551          the address of the instruction whose delay slot it is in.  */
12552       if (type == 'p' || type == 'q')
12553         {
12554           addr += 2;
12555
12556           /* If we are currently assuming that this frag should be
12557              extended, then, the current address is two bytes
12558              higher.  */
12559           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12560             addr += 2;
12561
12562           /* Ignore the low bit in the target, since it will be set
12563              for a text label.  */
12564           if ((val & 1) != 0)
12565             --val;
12566         }
12567       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12568         addr -= 4;
12569       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12570         addr -= 2;
12571
12572       val -= addr & ~ ((1 << op->shift) - 1);
12573
12574       /* Branch offsets have an implicit 0 in the lowest bit.  */
12575       if (type == 'p' || type == 'q')
12576         val /= 2;
12577
12578       /* If any of the shifted bits are set, we must use an extended
12579          opcode.  If the address depends on the size of this
12580          instruction, this can lead to a loop, so we arrange to always
12581          use an extended opcode.  We only check this when we are in
12582          the main relaxation loop, when SEC is NULL.  */
12583       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12584         {
12585           fragp->fr_subtype =
12586             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12587           return 1;
12588         }
12589
12590       /* If we are about to mark a frag as extended because the value
12591          is precisely maxtiny + 1, then there is a chance of an
12592          infinite loop as in the following code:
12593              la $4,foo
12594              .skip      1020
12595              .align     2
12596            foo:
12597          In this case when the la is extended, foo is 0x3fc bytes
12598          away, so the la can be shrunk, but then foo is 0x400 away, so
12599          the la must be extended.  To avoid this loop, we mark the
12600          frag as extended if it was small, and is about to become
12601          extended with a value of maxtiny + 1.  */
12602       if (val == ((maxtiny + 1) << op->shift)
12603           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12604           && sec == NULL)
12605         {
12606           fragp->fr_subtype =
12607             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12608           return 1;
12609         }
12610     }
12611   else if (symsec != absolute_section && sec != NULL)
12612     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12613
12614   if ((val & ((1 << op->shift) - 1)) != 0
12615       || val < (mintiny << op->shift)
12616       || val > (maxtiny << op->shift))
12617     return 1;
12618   else
12619     return 0;
12620 }
12621
12622 /* Estimate the size of a frag before relaxing.  Unless this is the
12623    mips16, we are not really relaxing here, and the final size is
12624    encoded in the subtype information.  For the mips16, we have to
12625    decide whether we are using an extended opcode or not.  */
12626
12627 int
12628 md_estimate_size_before_relax (fragp, segtype)
12629      fragS *fragp;
12630      asection *segtype;
12631 {
12632   int change = 0;
12633   boolean linkonce = false;
12634
12635   if (RELAX_MIPS16_P (fragp->fr_subtype))
12636     /* We don't want to modify the EXTENDED bit here; it might get us
12637        into infinite loops.  We change it only in mips_relax_frag().  */
12638     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12639
12640   if (mips_pic == NO_PIC)
12641     {
12642       change = nopic_need_relax (fragp->fr_symbol, 0);
12643     }
12644   else if (mips_pic == SVR4_PIC)
12645     {
12646       symbolS *sym;
12647       asection *symsec;
12648
12649       sym = fragp->fr_symbol;
12650
12651       /* Handle the case of a symbol equated to another symbol.  */
12652       while (symbol_equated_reloc_p (sym))
12653         {
12654           symbolS *n;
12655
12656           /* It's possible to get a loop here in a badly written
12657              program.  */
12658           n = symbol_get_value_expression (sym)->X_add_symbol;
12659           if (n == sym)
12660             break;
12661           sym = n;
12662         }
12663
12664       symsec = S_GET_SEGMENT (sym);
12665
12666       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12667       if (symsec != segtype && ! S_IS_LOCAL (sym))
12668         {
12669           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12670               != 0)
12671             linkonce = true;
12672
12673           /* The GNU toolchain uses an extension for ELF: a section
12674              beginning with the magic string .gnu.linkonce is a linkonce
12675              section.  */
12676           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12677                        sizeof ".gnu.linkonce" - 1) == 0)
12678             linkonce = true;
12679         }
12680
12681       /* This must duplicate the test in adjust_reloc_syms.  */
12682       change = (symsec != &bfd_und_section
12683                 && symsec != &bfd_abs_section
12684                 && ! bfd_is_com_section (symsec)
12685                 && !linkonce
12686 #ifdef OBJ_ELF
12687                 /* A global or weak symbol is treated as external.  */
12688                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12689                     || (! S_IS_WEAK (sym)
12690                         && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
12691 #endif
12692                 );
12693     }
12694   else
12695     abort ();
12696
12697   if (change)
12698     {
12699       /* Record the offset to the first reloc in the fr_opcode field.
12700          This lets md_convert_frag and tc_gen_reloc know that the code
12701          must be expanded.  */
12702       fragp->fr_opcode = (fragp->fr_literal
12703                           + fragp->fr_fix
12704                           - RELAX_OLD (fragp->fr_subtype)
12705                           + RELAX_RELOC1 (fragp->fr_subtype));
12706       /* FIXME: This really needs as_warn_where.  */
12707       if (RELAX_WARN (fragp->fr_subtype))
12708         as_warn (_("AT used after \".set noat\" or macro used after "
12709                    "\".set nomacro\""));
12710
12711       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12712     }
12713
12714   return 0;
12715 }
12716
12717 /* This is called to see whether a reloc against a defined symbol
12718    should be converted into a reloc against a section.  Don't adjust
12719    MIPS16 jump relocations, so we don't have to worry about the format
12720    of the offset in the .o file.  Don't adjust relocations against
12721    mips16 symbols, so that the linker can find them if it needs to set
12722    up a stub.  */
12723
12724 int
12725 mips_fix_adjustable (fixp)
12726      fixS *fixp;
12727 {
12728 #ifdef OBJ_ELF
12729   /* Prevent all adjustments to global symbols.  */
12730   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12731       && mips_pic != EMBEDDED_PIC
12732       && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12733     return 0;
12734 #endif
12735   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12736     return 0;
12737   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12738       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12739     return 0;
12740   if (fixp->fx_addsy == NULL)
12741     return 1;
12742 #ifdef OBJ_ELF
12743   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12744       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12745       && fixp->fx_subsy == NULL)
12746     return 0;
12747 #endif
12748   return 1;
12749 }
12750
12751 /* Translate internal representation of relocation info to BFD target
12752    format.  */
12753
12754 arelent **
12755 tc_gen_reloc (section, fixp)
12756      asection *section ATTRIBUTE_UNUSED;
12757      fixS *fixp;
12758 {
12759   static arelent *retval[4];
12760   arelent *reloc;
12761   bfd_reloc_code_real_type code;
12762
12763   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12764   retval[1] = NULL;
12765
12766   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12767   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12768   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12769
12770   if (mips_pic == EMBEDDED_PIC
12771       && SWITCH_TABLE (fixp))
12772     {
12773       /* For a switch table entry we use a special reloc.  The addend
12774          is actually the difference between the reloc address and the
12775          subtrahend.  */
12776       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12777       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12778         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12779       fixp->fx_r_type = BFD_RELOC_GPREL32;
12780     }
12781   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12782     {
12783       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12784         reloc->addend = fixp->fx_addnumber;
12785       else
12786         {
12787           /* We use a special addend for an internal RELLO reloc.  */
12788           if (symbol_section_p (fixp->fx_addsy))
12789             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12790           else
12791             reloc->addend = fixp->fx_addnumber + reloc->address;
12792         }
12793     }
12794   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12795     {
12796       assert (fixp->fx_next != NULL
12797               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12798
12799       /* The reloc is relative to the RELLO; adjust the addend
12800          accordingly.  */
12801       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12802         reloc->addend = fixp->fx_next->fx_addnumber;
12803       else
12804         {
12805           /* We use a special addend for an internal RELHI reloc.  */
12806           if (symbol_section_p (fixp->fx_addsy))
12807             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12808                              + fixp->fx_next->fx_where
12809                              - S_GET_VALUE (fixp->fx_subsy));
12810           else
12811             reloc->addend = (fixp->fx_addnumber
12812                              + fixp->fx_next->fx_frag->fr_address
12813                              + fixp->fx_next->fx_where);
12814         }
12815     }
12816   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12817     reloc->addend = fixp->fx_addnumber;
12818   else
12819     {
12820       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12821         /* A gruesome hack which is a result of the gruesome gas reloc
12822            handling.  */
12823         reloc->addend = reloc->address;
12824       else
12825         reloc->addend = -reloc->address;
12826     }
12827
12828   /* If this is a variant frag, we may need to adjust the existing
12829      reloc and generate a new one.  */
12830   if (fixp->fx_frag->fr_opcode != NULL
12831       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12832           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12833           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12834           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12835           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12836           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12837           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12838       && ! HAVE_NEWABI)
12839     {
12840       arelent *reloc2;
12841
12842       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12843
12844       /* If this is not the last reloc in this frag, then we have two
12845          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12846          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12847          the second one handle all of them.  */
12848       if (fixp->fx_next != NULL
12849           && fixp->fx_frag == fixp->fx_next->fx_frag)
12850         {
12851           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12852                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12853                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12854                       && (fixp->fx_next->fx_r_type
12855                           == BFD_RELOC_MIPS_GOT_LO16))
12856                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12857                       && (fixp->fx_next->fx_r_type
12858                           == BFD_RELOC_MIPS_CALL_LO16)));
12859           retval[0] = NULL;
12860           return retval;
12861         }
12862
12863       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12864       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12865       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12866       retval[2] = NULL;
12867       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12868       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12869       reloc2->address = (reloc->address
12870                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12871                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12872       reloc2->addend = fixp->fx_addnumber;
12873       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12874       assert (reloc2->howto != NULL);
12875
12876       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12877         {
12878           arelent *reloc3;
12879
12880           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12881           retval[3] = NULL;
12882           *reloc3 = *reloc2;
12883           reloc3->address += 4;
12884         }
12885
12886       if (mips_pic == NO_PIC)
12887         {
12888           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12889           fixp->fx_r_type = BFD_RELOC_HI16_S;
12890         }
12891       else if (mips_pic == SVR4_PIC)
12892         {
12893           switch (fixp->fx_r_type)
12894             {
12895             default:
12896               abort ();
12897             case BFD_RELOC_MIPS_GOT16:
12898               break;
12899             case BFD_RELOC_MIPS_CALL16:
12900             case BFD_RELOC_MIPS_GOT_LO16:
12901             case BFD_RELOC_MIPS_CALL_LO16:
12902               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12903               break;
12904             }
12905         }
12906       else
12907         abort ();
12908     }
12909
12910   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12911      entry to be used in the relocation's section offset.  */
12912   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12913     {
12914       reloc->address = reloc->addend;
12915       reloc->addend = 0;
12916     }
12917
12918   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12919      fixup_segment converted a non-PC relative reloc into a PC
12920      relative reloc.  In such a case, we need to convert the reloc
12921      code.  */
12922   code = fixp->fx_r_type;
12923   if (fixp->fx_pcrel)
12924     {
12925       switch (code)
12926         {
12927         case BFD_RELOC_8:
12928           code = BFD_RELOC_8_PCREL;
12929           break;
12930         case BFD_RELOC_16:
12931           code = BFD_RELOC_16_PCREL;
12932           break;
12933         case BFD_RELOC_32:
12934           code = BFD_RELOC_32_PCREL;
12935           break;
12936         case BFD_RELOC_64:
12937           code = BFD_RELOC_64_PCREL;
12938           break;
12939         case BFD_RELOC_8_PCREL:
12940         case BFD_RELOC_16_PCREL:
12941         case BFD_RELOC_32_PCREL:
12942         case BFD_RELOC_64_PCREL:
12943         case BFD_RELOC_16_PCREL_S2:
12944         case BFD_RELOC_PCREL_HI16_S:
12945         case BFD_RELOC_PCREL_LO16:
12946           break;
12947         default:
12948           as_bad_where (fixp->fx_file, fixp->fx_line,
12949                         _("Cannot make %s relocation PC relative"),
12950                         bfd_get_reloc_code_name (code));
12951         }
12952     }
12953
12954 #ifdef OBJ_ELF
12955   /* md_apply_fix3 has a double-subtraction hack to get
12956      bfd_install_relocation to behave nicely.  GPREL relocations are
12957      handled correctly without this hack, so undo it here.  We can't
12958      stop md_apply_fix3 from subtracting twice in the first place since
12959      the fake addend is required for variant frags above.  */
12960   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12961       && code == BFD_RELOC_GPREL16
12962       && reloc->addend != 0
12963       && mips_need_elf_addend_fixup (fixp))
12964     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12965 #endif
12966
12967   /* To support a PC relative reloc when generating embedded PIC code
12968      for ECOFF, we use a Cygnus extension.  We check for that here to
12969      make sure that we don't let such a reloc escape normally.  */
12970   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12971        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12972       && code == BFD_RELOC_16_PCREL_S2
12973       && mips_pic != EMBEDDED_PIC)
12974     reloc->howto = NULL;
12975   else
12976     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12977
12978   if (reloc->howto == NULL)
12979     {
12980       as_bad_where (fixp->fx_file, fixp->fx_line,
12981                     _("Can not represent %s relocation in this object file format"),
12982                     bfd_get_reloc_code_name (code));
12983       retval[0] = NULL;
12984     }
12985
12986   return retval;
12987 }
12988
12989 /* Relax a machine dependent frag.  This returns the amount by which
12990    the current size of the frag should change.  */
12991
12992 int
12993 mips_relax_frag (fragp, stretch)
12994      fragS *fragp;
12995      long stretch;
12996 {
12997   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12998     return 0;
12999
13000   if (mips16_extended_frag (fragp, NULL, stretch))
13001     {
13002       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13003         return 0;
13004       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13005       return 2;
13006     }
13007   else
13008     {
13009       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13010         return 0;
13011       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13012       return -2;
13013     }
13014
13015   return 0;
13016 }
13017
13018 /* Convert a machine dependent frag.  */
13019
13020 void
13021 md_convert_frag (abfd, asec, fragp)
13022      bfd *abfd ATTRIBUTE_UNUSED;
13023      segT asec;
13024      fragS *fragp;
13025 {
13026   int old, new;
13027   char *fixptr;
13028
13029   if (RELAX_MIPS16_P (fragp->fr_subtype))
13030     {
13031       int type;
13032       register const struct mips16_immed_operand *op;
13033       boolean small, ext;
13034       offsetT val;
13035       bfd_byte *buf;
13036       unsigned long insn;
13037       boolean use_extend;
13038       unsigned short extend;
13039
13040       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13041       op = mips16_immed_operands;
13042       while (op->type != type)
13043         ++op;
13044
13045       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13046         {
13047           small = false;
13048           ext = true;
13049         }
13050       else
13051         {
13052           small = true;
13053           ext = false;
13054         }
13055
13056       resolve_symbol_value (fragp->fr_symbol);
13057       val = S_GET_VALUE (fragp->fr_symbol);
13058       if (op->pcrel)
13059         {
13060           addressT addr;
13061
13062           addr = fragp->fr_address + fragp->fr_fix;
13063
13064           /* The rules for the base address of a PC relative reloc are
13065              complicated; see mips16_extended_frag.  */
13066           if (type == 'p' || type == 'q')
13067             {
13068               addr += 2;
13069               if (ext)
13070                 addr += 2;
13071               /* Ignore the low bit in the target, since it will be
13072                  set for a text label.  */
13073               if ((val & 1) != 0)
13074                 --val;
13075             }
13076           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13077             addr -= 4;
13078           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13079             addr -= 2;
13080
13081           addr &= ~ (addressT) ((1 << op->shift) - 1);
13082           val -= addr;
13083
13084           /* Make sure the section winds up with the alignment we have
13085              assumed.  */
13086           if (op->shift > 0)
13087             record_alignment (asec, op->shift);
13088         }
13089
13090       if (ext
13091           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13092               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13093         as_warn_where (fragp->fr_file, fragp->fr_line,
13094                        _("extended instruction in delay slot"));
13095
13096       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13097
13098       if (target_big_endian)
13099         insn = bfd_getb16 (buf);
13100       else
13101         insn = bfd_getl16 (buf);
13102
13103       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13104                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13105                     small, ext, &insn, &use_extend, &extend);
13106
13107       if (use_extend)
13108         {
13109           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13110           fragp->fr_fix += 2;
13111           buf += 2;
13112         }
13113
13114       md_number_to_chars ((char *) buf, insn, 2);
13115       fragp->fr_fix += 2;
13116       buf += 2;
13117     }
13118   else
13119     {
13120       if (fragp->fr_opcode == NULL)
13121         return;
13122
13123       old = RELAX_OLD (fragp->fr_subtype);
13124       new = RELAX_NEW (fragp->fr_subtype);
13125       fixptr = fragp->fr_literal + fragp->fr_fix;
13126
13127       if (new > 0)
13128         memcpy (fixptr - old, fixptr, new);
13129
13130       fragp->fr_fix += new - old;
13131     }
13132 }
13133
13134 #ifdef OBJ_ELF
13135
13136 /* This function is called after the relocs have been generated.
13137    We've been storing mips16 text labels as odd.  Here we convert them
13138    back to even for the convenience of the debugger.  */
13139
13140 void
13141 mips_frob_file_after_relocs ()
13142 {
13143   asymbol **syms;
13144   unsigned int count, i;
13145
13146   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13147     return;
13148
13149   syms = bfd_get_outsymbols (stdoutput);
13150   count = bfd_get_symcount (stdoutput);
13151   for (i = 0; i < count; i++, syms++)
13152     {
13153       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13154           && ((*syms)->value & 1) != 0)
13155         {
13156           (*syms)->value &= ~1;
13157           /* If the symbol has an odd size, it was probably computed
13158              incorrectly, so adjust that as well.  */
13159           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13160             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13161         }
13162     }
13163 }
13164
13165 #endif
13166
13167 /* This function is called whenever a label is defined.  It is used
13168    when handling branch delays; if a branch has a label, we assume we
13169    can not move it.  */
13170
13171 void
13172 mips_define_label (sym)
13173      symbolS *sym;
13174 {
13175   struct insn_label_list *l;
13176
13177   if (free_insn_labels == NULL)
13178     l = (struct insn_label_list *) xmalloc (sizeof *l);
13179   else
13180     {
13181       l = free_insn_labels;
13182       free_insn_labels = l->next;
13183     }
13184
13185   l->label = sym;
13186   l->next = insn_labels;
13187   insn_labels = l;
13188 }
13189 \f
13190 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13191
13192 /* Some special processing for a MIPS ELF file.  */
13193
13194 void
13195 mips_elf_final_processing ()
13196 {
13197   /* Write out the register information.  */
13198   if (file_mips_abi != N64_ABI)
13199     {
13200       Elf32_RegInfo s;
13201
13202       s.ri_gprmask = mips_gprmask;
13203       s.ri_cprmask[0] = mips_cprmask[0];
13204       s.ri_cprmask[1] = mips_cprmask[1];
13205       s.ri_cprmask[2] = mips_cprmask[2];
13206       s.ri_cprmask[3] = mips_cprmask[3];
13207       /* The gp_value field is set by the MIPS ELF backend.  */
13208
13209       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13210                                        ((Elf32_External_RegInfo *)
13211                                         mips_regmask_frag));
13212     }
13213   else
13214     {
13215       Elf64_Internal_RegInfo s;
13216
13217       s.ri_gprmask = mips_gprmask;
13218       s.ri_pad = 0;
13219       s.ri_cprmask[0] = mips_cprmask[0];
13220       s.ri_cprmask[1] = mips_cprmask[1];
13221       s.ri_cprmask[2] = mips_cprmask[2];
13222       s.ri_cprmask[3] = mips_cprmask[3];
13223       /* The gp_value field is set by the MIPS ELF backend.  */
13224
13225       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13226                                        ((Elf64_External_RegInfo *)
13227                                         mips_regmask_frag));
13228     }
13229
13230   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13231      sort of BFD interface for this.  */
13232   if (mips_any_noreorder)
13233     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13234   if (mips_pic != NO_PIC)
13235     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13236
13237   /* Set MIPS ELF flags for ASEs.  */
13238   if (file_ase_mips16)
13239     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13240 #if 0 /* XXX FIXME */
13241   if (file_ase_mips3d)
13242     elf_elfheader (stdoutput)->e_flags |= ???;
13243 #endif
13244   if (file_ase_mdmx)
13245     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13246
13247   /* Set the MIPS ELF ABI flags.  */
13248   if (file_mips_abi == NO_ABI)
13249     ;
13250   else if (file_mips_abi == O32_ABI)
13251     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13252   else if (file_mips_abi == O64_ABI)
13253     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13254   else if (file_mips_abi == EABI_ABI)
13255     {
13256       /* Set the EABI kind based on the ISA.  This isn't really
13257          the best, but then neither is basing the abi on the isa.  */
13258       if (ISA_HAS_64BIT_REGS (file_mips_isa))
13259         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13260       else
13261         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13262     }
13263   else if (file_mips_abi == N32_ABI)
13264     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13265
13266   /* Nothing to do for N64_ABI.  */
13267
13268   if (mips_32bitmode)
13269     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13270 }
13271
13272 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13273 \f
13274 typedef struct proc {
13275   symbolS *isym;
13276   unsigned long reg_mask;
13277   unsigned long reg_offset;
13278   unsigned long fpreg_mask;
13279   unsigned long fpreg_offset;
13280   unsigned long frame_offset;
13281   unsigned long frame_reg;
13282   unsigned long pc_reg;
13283 } procS;
13284
13285 static procS cur_proc;
13286 static procS *cur_proc_ptr;
13287 static int numprocs;
13288
13289 /* Fill in an rs_align_code fragment.  */
13290
13291 void
13292 mips_handle_align (fragp)
13293      fragS *fragp;
13294 {
13295   if (fragp->fr_type != rs_align_code)
13296     return;
13297
13298   if (mips_opts.mips16)
13299     {
13300       static const unsigned char be_nop[] = { 0x65, 0x00 };
13301       static const unsigned char le_nop[] = { 0x00, 0x65 };
13302
13303       int bytes;
13304       char *p;
13305
13306       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13307       p = fragp->fr_literal + fragp->fr_fix;
13308
13309       if (bytes & 1)
13310         {
13311           *p++ = 0;
13312           fragp->fr_fix++;
13313         }
13314
13315       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13316       fragp->fr_var = 2;
13317     }
13318
13319   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13320 }
13321
13322 static void
13323 md_obj_begin ()
13324 {
13325 }
13326
13327 static void
13328 md_obj_end ()
13329 {
13330   /* check for premature end, nesting errors, etc */
13331   if (cur_proc_ptr)
13332     as_warn (_("missing .end at end of assembly"));
13333 }
13334
13335 static long
13336 get_number ()
13337 {
13338   int negative = 0;
13339   long val = 0;
13340
13341   if (*input_line_pointer == '-')
13342     {
13343       ++input_line_pointer;
13344       negative = 1;
13345     }
13346   if (!ISDIGIT (*input_line_pointer))
13347     as_bad (_("expected simple number"));
13348   if (input_line_pointer[0] == '0')
13349     {
13350       if (input_line_pointer[1] == 'x')
13351         {
13352           input_line_pointer += 2;
13353           while (ISXDIGIT (*input_line_pointer))
13354             {
13355               val <<= 4;
13356               val |= hex_value (*input_line_pointer++);
13357             }
13358           return negative ? -val : val;
13359         }
13360       else
13361         {
13362           ++input_line_pointer;
13363           while (ISDIGIT (*input_line_pointer))
13364             {
13365               val <<= 3;
13366               val |= *input_line_pointer++ - '0';
13367             }
13368           return negative ? -val : val;
13369         }
13370     }
13371   if (!ISDIGIT (*input_line_pointer))
13372     {
13373       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13374               *input_line_pointer, *input_line_pointer);
13375       as_warn (_("invalid number"));
13376       return -1;
13377     }
13378   while (ISDIGIT (*input_line_pointer))
13379     {
13380       val *= 10;
13381       val += *input_line_pointer++ - '0';
13382     }
13383   return negative ? -val : val;
13384 }
13385
13386 /* The .file directive; just like the usual .file directive, but there
13387    is an initial number which is the ECOFF file index.  In the non-ECOFF
13388    case .file implies DWARF-2.  */
13389
13390 static void
13391 s_mips_file (x)
13392      int x ATTRIBUTE_UNUSED;
13393 {
13394   static int first_file_directive = 0;
13395
13396   if (ECOFF_DEBUGGING)
13397     {
13398       get_number ();
13399       s_app_file (0);
13400     }
13401   else
13402     {
13403       char *filename;
13404
13405       filename = dwarf2_directive_file (0);
13406
13407       /* Versions of GCC up to 3.1 start files with a ".file"
13408          directive even for stabs output.  Make sure that this
13409          ".file" is handled.  Note that you need a version of GCC
13410          after 3.1 in order to support DWARF-2 on MIPS.  */
13411       if (filename != NULL && ! first_file_directive)
13412         {
13413           (void) new_logical_line (filename, -1);
13414           s_app_file_string (filename);
13415         }
13416       first_file_directive = 1;
13417     }
13418 }
13419
13420 /* The .loc directive, implying DWARF-2.  */
13421
13422 static void
13423 s_mips_loc (x)
13424      int x ATTRIBUTE_UNUSED;
13425 {
13426   if (!ECOFF_DEBUGGING)
13427     dwarf2_directive_loc (0);
13428 }
13429
13430 /* The .end directive.  */
13431
13432 static void
13433 s_mips_end (x)
13434      int x ATTRIBUTE_UNUSED;
13435 {
13436   symbolS *p;
13437   int maybe_text;
13438
13439   /* Following functions need their own .frame and .cprestore directives.  */
13440   mips_frame_reg_valid = 0;
13441   mips_cprestore_valid = 0;
13442
13443   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13444     {
13445       p = get_symbol ();
13446       demand_empty_rest_of_line ();
13447     }
13448   else
13449     p = NULL;
13450
13451 #ifdef BFD_ASSEMBLER
13452   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13453     maybe_text = 1;
13454   else
13455     maybe_text = 0;
13456 #else
13457   if (now_seg != data_section && now_seg != bss_section)
13458     maybe_text = 1;
13459   else
13460     maybe_text = 0;
13461 #endif
13462
13463   if (!maybe_text)
13464     as_warn (_(".end not in text section"));
13465
13466   if (!cur_proc_ptr)
13467     {
13468       as_warn (_(".end directive without a preceding .ent directive."));
13469       demand_empty_rest_of_line ();
13470       return;
13471     }
13472
13473   if (p != NULL)
13474     {
13475       assert (S_GET_NAME (p));
13476       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13477         as_warn (_(".end symbol does not match .ent symbol."));
13478
13479       if (debug_type == DEBUG_STABS)
13480         stabs_generate_asm_endfunc (S_GET_NAME (p),
13481                                     S_GET_NAME (p));
13482     }
13483   else
13484     as_warn (_(".end directive missing or unknown symbol"));
13485
13486 #ifdef OBJ_ELF
13487   /* Generate a .pdr section.  */
13488   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13489     {
13490       segT saved_seg = now_seg;
13491       subsegT saved_subseg = now_subseg;
13492       valueT dot;
13493       expressionS exp;
13494       char *fragp;
13495
13496       dot = frag_now_fix ();
13497
13498 #ifdef md_flush_pending_output
13499       md_flush_pending_output ();
13500 #endif
13501
13502       assert (pdr_seg);
13503       subseg_set (pdr_seg, 0);
13504
13505       /* Write the symbol.  */
13506       exp.X_op = O_symbol;
13507       exp.X_add_symbol = p;
13508       exp.X_add_number = 0;
13509       emit_expr (&exp, 4);
13510
13511       fragp = frag_more (7 * 4);
13512
13513       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13514       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13515       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13516       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13517       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13518       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13519       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13520
13521       subseg_set (saved_seg, saved_subseg);
13522     }
13523 #endif /* OBJ_ELF */
13524
13525   cur_proc_ptr = NULL;
13526 }
13527
13528 /* The .aent and .ent directives.  */
13529
13530 static void
13531 s_mips_ent (aent)
13532      int aent;
13533 {
13534   symbolS *symbolP;
13535   int maybe_text;
13536
13537   symbolP = get_symbol ();
13538   if (*input_line_pointer == ',')
13539     ++input_line_pointer;
13540   SKIP_WHITESPACE ();
13541   if (ISDIGIT (*input_line_pointer)
13542       || *input_line_pointer == '-')
13543     get_number ();
13544
13545 #ifdef BFD_ASSEMBLER
13546   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13547     maybe_text = 1;
13548   else
13549     maybe_text = 0;
13550 #else
13551   if (now_seg != data_section && now_seg != bss_section)
13552     maybe_text = 1;
13553   else
13554     maybe_text = 0;
13555 #endif
13556
13557   if (!maybe_text)
13558     as_warn (_(".ent or .aent not in text section."));
13559
13560   if (!aent && cur_proc_ptr)
13561     as_warn (_("missing .end"));
13562
13563   if (!aent)
13564     {
13565       /* This function needs its own .frame and .cprestore directives.  */
13566       mips_frame_reg_valid = 0;
13567       mips_cprestore_valid = 0;
13568
13569       cur_proc_ptr = &cur_proc;
13570       memset (cur_proc_ptr, '\0', sizeof (procS));
13571
13572       cur_proc_ptr->isym = symbolP;
13573
13574       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13575
13576       ++numprocs;
13577
13578       if (debug_type == DEBUG_STABS)
13579         stabs_generate_asm_func (S_GET_NAME (symbolP),
13580                                  S_GET_NAME (symbolP));
13581     }
13582
13583   demand_empty_rest_of_line ();
13584 }
13585
13586 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13587    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13588    s_mips_frame is used so that we can set the PDR information correctly.
13589    We can't use the ecoff routines because they make reference to the ecoff
13590    symbol table (in the mdebug section).  */
13591
13592 static void
13593 s_mips_frame (ignore)
13594      int ignore ATTRIBUTE_UNUSED;
13595 {
13596 #ifdef OBJ_ELF
13597   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13598     {
13599       long val;
13600
13601       if (cur_proc_ptr == (procS *) NULL)
13602         {
13603           as_warn (_(".frame outside of .ent"));
13604           demand_empty_rest_of_line ();
13605           return;
13606         }
13607
13608       cur_proc_ptr->frame_reg = tc_get_register (1);
13609
13610       SKIP_WHITESPACE ();
13611       if (*input_line_pointer++ != ','
13612           || get_absolute_expression_and_terminator (&val) != ',')
13613         {
13614           as_warn (_("Bad .frame directive"));
13615           --input_line_pointer;
13616           demand_empty_rest_of_line ();
13617           return;
13618         }
13619
13620       cur_proc_ptr->frame_offset = val;
13621       cur_proc_ptr->pc_reg = tc_get_register (0);
13622
13623       demand_empty_rest_of_line ();
13624     }
13625   else
13626 #endif /* OBJ_ELF */
13627     s_ignore (ignore);
13628 }
13629
13630 /* The .fmask and .mask directives. If the mdebug section is present
13631    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13632    embedded targets, s_mips_mask is used so that we can set the PDR
13633    information correctly. We can't use the ecoff routines because they
13634    make reference to the ecoff symbol table (in the mdebug section).  */
13635
13636 static void
13637 s_mips_mask (reg_type)
13638      char reg_type;
13639 {
13640 #ifdef OBJ_ELF
13641   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13642     {
13643       long mask, off;
13644
13645       if (cur_proc_ptr == (procS *) NULL)
13646         {
13647           as_warn (_(".mask/.fmask outside of .ent"));
13648           demand_empty_rest_of_line ();
13649           return;
13650         }
13651
13652       if (get_absolute_expression_and_terminator (&mask) != ',')
13653         {
13654           as_warn (_("Bad .mask/.fmask directive"));
13655           --input_line_pointer;
13656           demand_empty_rest_of_line ();
13657           return;
13658         }
13659
13660       off = get_absolute_expression ();
13661
13662       if (reg_type == 'F')
13663         {
13664           cur_proc_ptr->fpreg_mask = mask;
13665           cur_proc_ptr->fpreg_offset = off;
13666         }
13667       else
13668         {
13669           cur_proc_ptr->reg_mask = mask;
13670           cur_proc_ptr->reg_offset = off;
13671         }
13672
13673       demand_empty_rest_of_line ();
13674     }
13675   else
13676 #endif /* OBJ_ELF */
13677     s_ignore (reg_type);
13678 }
13679
13680 /* The .loc directive.  */
13681
13682 #if 0
13683 static void
13684 s_loc (x)
13685      int x;
13686 {
13687   symbolS *symbolP;
13688   int lineno;
13689   int addroff;
13690
13691   assert (now_seg == text_section);
13692
13693   lineno = get_number ();
13694   addroff = frag_now_fix ();
13695
13696   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13697   S_SET_TYPE (symbolP, N_SLINE);
13698   S_SET_OTHER (symbolP, 0);
13699   S_SET_DESC (symbolP, lineno);
13700   symbolP->sy_segment = now_seg;
13701 }
13702 #endif
13703
13704 /* CPU name/ISA/number mapping table.
13705
13706    Entries are grouped by type.  The first matching CPU or ISA entry
13707    gets chosen by CPU or ISA, so it should be the 'canonical' name
13708    for that type.  Entries after that within the type are sorted
13709    alphabetically.
13710
13711    Case is ignored in comparison, so put the canonical entry in the
13712    appropriate case but everything else in lower case to ease eye pain.  */
13713 static const struct mips_cpu_info mips_cpu_info_table[] =
13714 {
13715   /* MIPS1 ISA */
13716   { "MIPS1",          1,      ISA_MIPS1,      CPU_R3000, },
13717   { "mips",           1,      ISA_MIPS1,      CPU_R3000, },
13718
13719   /* MIPS2 ISA */
13720   { "MIPS2",          1,      ISA_MIPS2,      CPU_R6000, },
13721
13722   /* MIPS3 ISA */
13723   { "MIPS3",          1,      ISA_MIPS3,      CPU_R4000, },
13724
13725   /* MIPS4 ISA */
13726   { "MIPS4",          1,      ISA_MIPS4,      CPU_R8000, },
13727
13728   /* MIPS5 ISA */
13729   { "MIPS5",          1,      ISA_MIPS5,      CPU_MIPS5, },
13730   { "Generic-MIPS5",  0,      ISA_MIPS5,      CPU_MIPS5, },
13731
13732   /* MIPS32 ISA */
13733   { "MIPS32",         1,      ISA_MIPS32,     CPU_MIPS32, },
13734   { "mipsisa32",      0,      ISA_MIPS32,     CPU_MIPS32, },
13735   { "Generic-MIPS32", 0,      ISA_MIPS32,     CPU_MIPS32, },
13736   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13737   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32, },
13738   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32, },
13739
13740   /* For historical reasons.  */
13741   { "MIPS64",         1,      ISA_MIPS3,      CPU_R4000, },
13742
13743   /* MIPS64 ISA */
13744   { "mipsisa64",      1,      ISA_MIPS64,     CPU_MIPS64, },
13745   { "Generic-MIPS64", 0,      ISA_MIPS64,     CPU_MIPS64, },
13746   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64, },
13747   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64, },
13748
13749   /* R2000 CPU */
13750   { "R2000",          0,      ISA_MIPS1,      CPU_R2000, },
13751   { "2000",           0,      ISA_MIPS1,      CPU_R2000, },
13752   { "2k",             0,      ISA_MIPS1,      CPU_R2000, },
13753   { "r2k",            0,      ISA_MIPS1,      CPU_R2000, },
13754
13755   /* R3000 CPU */
13756   { "R3000",          0,      ISA_MIPS1,      CPU_R3000, },
13757   { "3000",           0,      ISA_MIPS1,      CPU_R3000, },
13758   { "3k",             0,      ISA_MIPS1,      CPU_R3000, },
13759   { "r3k",            0,      ISA_MIPS1,      CPU_R3000, },
13760
13761   /* TX3900 CPU */
13762   { "R3900",          0,      ISA_MIPS1,      CPU_R3900, },
13763   { "3900",           0,      ISA_MIPS1,      CPU_R3900, },
13764   { "mipstx39",       0,      ISA_MIPS1,      CPU_R3900, },
13765
13766   /* R4000 CPU */
13767   { "R4000",          0,      ISA_MIPS3,      CPU_R4000, },
13768   { "4000",           0,      ISA_MIPS3,      CPU_R4000, },
13769   { "4k",             0,      ISA_MIPS3,      CPU_R4000, },   /* beware */
13770   { "r4k",            0,      ISA_MIPS3,      CPU_R4000, },
13771
13772   /* R4010 CPU */
13773   { "R4010",          0,      ISA_MIPS2,      CPU_R4010, },
13774   { "4010",           0,      ISA_MIPS2,      CPU_R4010, },
13775
13776   /* R4400 CPU */
13777   { "R4400",          0,      ISA_MIPS3,      CPU_R4400, },
13778   { "4400",           0,      ISA_MIPS3,      CPU_R4400, },
13779
13780   /* R4600 CPU */
13781   { "R4600",          0,      ISA_MIPS3,      CPU_R4600, },
13782   { "4600",           0,      ISA_MIPS3,      CPU_R4600, },
13783   { "mips64orion",    0,      ISA_MIPS3,      CPU_R4600, },
13784   { "orion",          0,      ISA_MIPS3,      CPU_R4600, },
13785
13786   /* R4650 CPU */
13787   { "R4650",          0,      ISA_MIPS3,      CPU_R4650, },
13788   { "4650",           0,      ISA_MIPS3,      CPU_R4650, },
13789
13790   /* R6000 CPU */
13791   { "R6000",          0,      ISA_MIPS2,      CPU_R6000, },
13792   { "6000",           0,      ISA_MIPS2,      CPU_R6000, },
13793   { "6k",             0,      ISA_MIPS2,      CPU_R6000, },
13794   { "r6k",            0,      ISA_MIPS2,      CPU_R6000, },
13795
13796   /* R8000 CPU */
13797   { "R8000",          0,      ISA_MIPS4,      CPU_R8000, },
13798   { "8000",           0,      ISA_MIPS4,      CPU_R8000, },
13799   { "8k",             0,      ISA_MIPS4,      CPU_R8000, },
13800   { "r8k",            0,      ISA_MIPS4,      CPU_R8000, },
13801
13802   /* R10000 CPU */
13803   { "R10000",         0,      ISA_MIPS4,      CPU_R10000, },
13804   { "10000",          0,      ISA_MIPS4,      CPU_R10000, },
13805   { "10k",            0,      ISA_MIPS4,      CPU_R10000, },
13806   { "r10k",           0,      ISA_MIPS4,      CPU_R10000, },
13807
13808   /* R12000 CPU */
13809   { "R12000",         0,      ISA_MIPS4,      CPU_R12000, },
13810   { "12000",          0,      ISA_MIPS4,      CPU_R12000, },
13811   { "12k",            0,      ISA_MIPS4,      CPU_R12000, },
13812   { "r12k",           0,      ISA_MIPS4,      CPU_R12000, },
13813
13814   /* VR4100 CPU */
13815   { "VR4100",         0,      ISA_MIPS3,      CPU_VR4100, },
13816   { "4100",           0,      ISA_MIPS3,      CPU_VR4100, },
13817   { "mips64vr4100",   0,      ISA_MIPS3,      CPU_VR4100, },
13818   { "r4100",          0,      ISA_MIPS3,      CPU_VR4100, },
13819
13820   /* VR4111 CPU */
13821   { "VR4111",         0,      ISA_MIPS3,      CPU_R4111, },
13822   { "4111",           0,      ISA_MIPS3,      CPU_R4111, },
13823   { "mips64vr4111",   0,      ISA_MIPS3,      CPU_R4111, },
13824   { "r4111",          0,      ISA_MIPS3,      CPU_R4111, },
13825
13826   /* VR4300 CPU */
13827   { "VR4300",         0,      ISA_MIPS3,      CPU_R4300, },
13828   { "4300",           0,      ISA_MIPS3,      CPU_R4300, },
13829   { "mips64vr4300",   0,      ISA_MIPS3,      CPU_R4300, },
13830   { "r4300",          0,      ISA_MIPS3,      CPU_R4300, },
13831
13832   /* VR5000 CPU */
13833   { "VR5000",         0,      ISA_MIPS4,      CPU_R5000, },
13834   { "5000",           0,      ISA_MIPS4,      CPU_R5000, },
13835   { "5k",             0,      ISA_MIPS4,      CPU_R5000, },
13836   { "mips64vr5000",   0,      ISA_MIPS4,      CPU_R5000, },
13837   { "r5000",          0,      ISA_MIPS4,      CPU_R5000, },
13838   { "r5200",          0,      ISA_MIPS4,      CPU_R5000, },
13839   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000, },
13840   { "r5230",          0,      ISA_MIPS4,      CPU_R5000, },
13841   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000, },
13842   { "r5231",          0,      ISA_MIPS4,      CPU_R5000, },
13843   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000, },
13844   { "r5261",          0,      ISA_MIPS4,      CPU_R5000, },
13845   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000, },
13846   { "r5721",          0,      ISA_MIPS4,      CPU_R5000, },
13847   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000, },
13848   { "r5k",            0,      ISA_MIPS4,      CPU_R5000, },
13849   { "r7000",          0,      ISA_MIPS4,      CPU_R5000, },
13850
13851   /* Broadcom SB-1 CPU */
13852   { "SB-1",           0,      ISA_MIPS64,     CPU_SB1, },
13853   { "sb-1250",        0,      ISA_MIPS64,     CPU_SB1, },
13854   { "sb1",            0,      ISA_MIPS64,     CPU_SB1, },
13855   { "sb1250",         0,      ISA_MIPS64,     CPU_SB1, },
13856
13857   /* End marker.  */
13858   { NULL, 0, 0, 0, },
13859 };
13860
13861 static const struct mips_cpu_info *
13862 mips_cpu_info_from_name (name)
13863      const char *name;
13864 {
13865   int i;
13866
13867   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13868     if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13869       return (&mips_cpu_info_table[i]);
13870
13871   return NULL;
13872 }
13873
13874 static const struct mips_cpu_info *
13875 mips_cpu_info_from_isa (isa)
13876      int isa;
13877 {
13878   int i;
13879
13880   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13881     if (mips_cpu_info_table[i].is_isa
13882       && isa == mips_cpu_info_table[i].isa)
13883       return (&mips_cpu_info_table[i]);
13884
13885   return NULL;
13886 }
13887
13888 static const struct mips_cpu_info *
13889 mips_cpu_info_from_cpu (cpu)
13890      int cpu;
13891 {
13892   int i;
13893
13894   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13895     if (!mips_cpu_info_table[i].is_isa
13896       && cpu == mips_cpu_info_table[i].cpu)
13897       return (&mips_cpu_info_table[i]);
13898
13899   return NULL;
13900 }